home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / Moscow ML 1.31 / source code / ANSIshellƒ / os_mac.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-11  |  72.8 KB  |  2,716 lines  |  [TEXT/R*ch]

  1. /* os_mac.c
  2.  * 12Sep92  e  adapted from the file of the same name in the Gambit Project
  3.  * Macintosh specific stuff (for THINK C 7 or CodeWarrior 4 compiler).
  4.  * portions Copyright © 1992-1994 Marc Feeley and Douglas H. Currie, Jr.
  5.  */
  6.  
  7. #ifdef THINK_C
  8. #include <MacHeaders>
  9. #else
  10. #ifdef __MWERKS__
  11. #else
  12. "Unknown compiler!"
  13. #endif
  14. #endif
  15.  
  16. #include "os_mac.h"
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <errno.h>
  21. #include <signal.h>
  22. #ifdef THINK_C
  23. #include <ansi_e.h>
  24. #else
  25. int XlateRETURN = 0;
  26. #endif
  27.  
  28. #define use_MacTraps2 (1)
  29.  
  30. /*---------------------------------------------------------------------------*/
  31.  
  32. #include <GestaltEqu.h>
  33. #include <AppleEvents.h>
  34.  
  35. #ifdef THINK_C
  36. #undef kAERestart
  37. #undef kAEShutDown
  38. #endif
  39. #include <AERegistry.h>
  40.  
  41. static void init_ae();
  42.  
  43. Boolean        gDoQuit = false;
  44. Boolean        gHasAppleEvents = false;
  45. Boolean        gPrintPage = false;
  46. Boolean        spareB1;
  47.  
  48. #include "os_mac_eventchk.h"
  49. long next_eventchk_ticks;
  50.  
  51. /*---------------------------------------------------------------------------*/
  52.  
  53. TextStyle prefStylNormal =
  54. { monaco, 0, 0, FontSize, { 65536, 65536, 65536 } /* RGBColor Black */ };
  55. TextStyle prefStylHilite =
  56. { monaco, 0, 0, FontSize, { 65536, 65536, 65536 } /* RGBColor Black */ };
  57.  
  58. short prefTabs = dfltTabs;
  59. short prefWrap = dfltWrap;
  60. short prefAutoInd = dfltAutoInd;
  61.  
  62. short current_menus;
  63.  
  64. MenuHandle menus[7];
  65.  
  66. Cursor watch_cursor;
  67. Cursor gc_cursor;
  68. Cursor ibeam_cursor;
  69. Cursor *current_cursor;
  70.  
  71. unsigned char *find_string, *replace_string;
  72.  
  73. Boolean find_ci   = FALSE;
  74. Boolean find_wrap = FALSE;
  75.  
  76. static Boolean smKeyBdP;
  77. static Boolean showPosnP;
  78.  
  79. short interaction_id = 0;
  80. short abnormal_exit = 0;
  81. short interrupted = 0;
  82. short current_volume = 0;
  83.  
  84. static struct {
  85.   short in_use;
  86.   short bold_input;
  87.   WindowPtr wptr;
  88.   ControlHandle vscroll;
  89.   ControlHandle hscroll;
  90.   TeHANDLE hTE;
  91.   short height;
  92.   short dirty;
  93.   long pos, len; /* was short -- 24Aug94 e */
  94.   char *buf;
  95.   Handle out_buf;
  96.   short out_len;
  97.   short is_file;
  98.   unsigned char *filename;
  99.   long needs_inval_ticks;
  100.   } wind_table[MAX_NB_WINDOWS];
  101.  
  102. #if 0
  103.  
  104. static struct
  105. { char   *a5;
  106.   VBLTask task;
  107. } et;
  108.  
  109. static void intr_task()
  110. { asm
  111.   { move.l a5,-(sp)
  112.     move.l -4(a0),a5
  113.     
  114.     cmpa.l 0x904,a5
  115.     beq.s @goon
  116.     _Debugger
  117. goon:
  118.   }
  119.   if (intr_task_interval > 0)
  120.   { timer_action( 0L, 0L, 1L );
  121.     et.task.vblCount = intr_task_interval;
  122.   }
  123.   else
  124.     et.task.vblCount = 1;
  125.   asm
  126.   { move.l (sp)+,a5
  127.   }
  128. }
  129.  
  130. static void start_intr_task()
  131. { asm
  132.   { move.l a5,et.a5
  133.   }
  134.   et.task.qType    = vType;
  135.   et.task.vblAddr  = (ProcPtr)intr_task;
  136.   et.task.vblCount = 1;
  137.   et.task.vblPhase = 0;
  138.   intr_task_installed = (VInstall( (QElemPtr )&et.task ) == noErr);
  139. }
  140.  
  141. static void stop_intr_task()
  142. { if (intr_task_installed)
  143.   { VRemove( (QElemPtr )&et.task ); intr_task_installed = 0; }
  144. }
  145.  
  146. #endif
  147.  
  148. RgnHandle gCursorRgn;
  149. Boolean gHasWNE = true;
  150. Boolean gHasHWPriv = true;
  151. Boolean gCursRgnOK = false;
  152. Boolean gInBackground = false;
  153. long gWaitTicksBG = 20; /* in ticks, should be 6..60  --  was: 5, 04Oct93  e */
  154. long gWaitTicksFG =  1; /* in ticks, should be 1..15 */
  155. /** Define trap numbers **/
  156. #ifndef _Unimplemented
  157. #define _Unimplemented 0xA89F /* Unimplemented trap */
  158. #endif
  159. #ifndef _WaitNextEvent
  160. #define _WaitNextEvent 0xA860 /* WaitNextEvent trap */
  161. #endif
  162. #ifndef _HWPriv
  163. #define _HWPriv 0xA098 /* HWPriv trap */
  164. #endif
  165.  
  166. TrapType GetTrapType(short theTrap)
  167. { /* OS traps start with A0, Tool with A8 or AA. */
  168.   if ((theTrap & 0x0800) == 0)
  169.     return (OSTrap);
  170.   else
  171.     return (ToolTrap);
  172. }
  173. short NumToolboxTraps(void)
  174. { /* InitGraf (trap $A86E) is always implemented. */
  175.   if (NGetTrapAddress(0xA86E, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  176.     return (0x200);
  177.   else
  178.     return (0x400);
  179. }
  180. Boolean TrapExists(short theTrap)
  181. { TrapType theTrapType;
  182.   theTrapType = GetTrapType(theTrap);
  183.   if ((theTrapType == ToolTrap) && ((theTrap &= 0x07FF) >= NumToolboxTraps()))
  184.     theTrap = _Unimplemented;
  185.   return (NGetTrapAddress(_Unimplemented, ToolTrap) != NGetTrapAddress(theTrap, theTrapType));
  186. }
  187.  
  188. static void setup_cursors()
  189. { CursHandle hCurs;
  190.   hCurs = GetCursor(watchCursor);
  191.   watch_cursor = **hCurs;
  192.   /* 28Jan93  e
  193.   hCurs = GetCursor(gc_cursorID);
  194.   gc_cursor = **hCurs;
  195.   */
  196.   hCurs = GetCursor(iBeamCursor);
  197.   ibeam_cursor = **hCurs;
  198.  
  199.   current_cursor = &watch_cursor;
  200. }
  201.  
  202. static void ParamText1( Str255 text )
  203. {
  204.     ParamText( text, "\p", "\p", "\p" );
  205. }
  206.  
  207. static void pathstr_to_filename( register Str255 p_str, Str255 f_str )
  208. {    register short plen = p_str[0];
  209.     while( plen && p_str[plen] != ':' ) plen--;
  210.     buf_to_p( p_str[0] - plen, (char *)&p_str[plen+1], f_str );
  211. }
  212.  
  213. /* *** preferences saved to disk *** */
  214.  
  215. #ifdef use_MacTraps2
  216.  
  217. static OSErr findPrefFolder( short *foundVRefNum, long *foundDirID )
  218. {    
  219.   *foundVRefNum = 0;
  220.   *foundDirID = 0;
  221.   /* MacTraps2 has FindFolder() glue */
  222.   return FindFolder( kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  223.                        foundVRefNum, foundDirID );
  224. }
  225.  
  226. #else
  227.  
  228. #include <GestaltEqu.h>
  229.  
  230. #define BTstQ(arg, bitnbr)        (arg & (1 << bitnbr))
  231.  
  232. static OSErr findPrefFolder( short *foundVRefNum, long *foundDirID )
  233. {
  234.   long          gesResponse;
  235.   SysEnvRec     envRec;
  236.   WDPBRec       myWDPB;
  237.   unsigned char volName[34];
  238.   OSErr         err;
  239.     
  240.   *foundVRefNum = 0;
  241.   *foundDirID = 0;
  242.   if ( !Gestalt( gestaltFindFolderAttr, &gesResponse ) 
  243.           && BTstQ( gesResponse, gestaltFindFolderPresent ) )
  244.   { /* Folder Manager exists */
  245.     err = FindFolder( kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  246.                        foundVRefNum, foundDirID );
  247.   }
  248.   else
  249.   { /* Gestalt can't give us the answer, so we resort to SysEnvirons */
  250.     if ( !(err = SysEnvirons( curSysEnvVers, &envRec )) )
  251.     {  myWDPB.ioVRefNum = envRec.sysVRefNum;
  252.        volName[0] = '\000';                   /* Zero volume name */
  253.        myWDPB.ioNamePtr = volName;
  254.        myWDPB.ioWDIndex = 0;
  255.        myWDPB.ioWDProcID = 0;
  256.        if ( !(err = PBGetWDInfo( &myWDPB, 0 )) )
  257.        { *foundVRefNum = myWDPB.ioWDVRefNum;
  258.          *foundDirID = myWDPB.ioWDDirID;
  259.        }
  260.     }
  261.   }
  262.   return (err);
  263. }
  264.  
  265. #endif
  266.  
  267. struct prefs
  268. { short version;
  269.   short tabs;
  270.   short wrap;
  271.   Boolean find_ci;
  272.   Boolean find_wrap;
  273.   Boolean spare0;        /* smKeyBdP in Gambit */
  274.   Boolean showPosnP;
  275.   short spare2;
  276.   short spare3;
  277.   short autoInd;
  278.   TextStyle stylNormal;
  279.   TextStyle stylHilite;
  280.   unsigned char fontnmNormal[64];
  281.   unsigned char fontnmHilite[64];
  282. };
  283.  
  284. void savePrefs( ConstStr255Param fn )
  285. {
  286.     short rRef;
  287.       OSErr err;
  288.     struct prefs **hRes;
  289.     struct prefs  *pRes;
  290.     short foundVRefNum;
  291.     long  foundDirID;
  292.     Str255 fontnmNormal, fontnmHilite;
  293.     
  294.     err = findPrefFolder( &foundVRefNum, &foundDirID );
  295.     if( err != noErr ) return;
  296.     
  297.     if( ( rRef = HOpenResFile( foundVRefNum, foundDirID, fn, fsRdWrPerm ) ) < 0)
  298.     {    HCreateResFile( foundVRefNum, foundDirID, fn );
  299.         { HFileInfo pb;
  300.           pb.ioCompletion = (void *)0;
  301.           pb.ioNamePtr = (StringPtr )fn;
  302.           pb.ioVRefNum = foundVRefNum;
  303.           pb.ioFDirIndex = 0;
  304.           pb.ioDirID = foundDirID;
  305.           err = PBHGetFInfo( (HParmBlkPtr )&pb, FALSE );
  306.           pb.ioCompletion = (void *)0;
  307.           pb.ioNamePtr = (StringPtr )fn;
  308.           pb.ioVRefNum = foundVRefNum;
  309.           pb.ioFDirIndex = 0;
  310.           pb.ioDirID = foundDirID;
  311.           pb.ioFlFndrInfo.fdCreator = CREATOR;
  312.           pb.ioFlFndrInfo.fdType    = 'gamP';
  313.           if( err == noErr )
  314.             PBHSetFInfo( (HParmBlkPtr )&pb, FALSE );
  315.         }
  316.         rRef = HOpenResFile( foundVRefNum, foundDirID, fn, fsRdWrPerm );
  317.     }
  318.     if( rRef < 0 )
  319.         return;
  320.     hRes = (struct prefs **)GetResource('ePrf', 357);
  321.     if(    hRes == 0 || rRef != HomeResFile( (Handle )hRes ) )
  322.     {    hRes = (struct prefs **)NewHandle( sizeof( struct prefs ) );
  323.         if( hRes == 0 )
  324.         {    CloseResFile( rRef );
  325.             return;
  326.         }
  327.         AddResource( (Handle )hRes, (ResType )'ePrf', 357, (ConstStr255Param)"\p" );
  328.         if( LMGetResErr() != noErr )
  329.         {    DisposHandle( (Handle )hRes );
  330.             CloseResFile( rRef );
  331.             return;
  332.         }
  333.     }
  334.     pRes = *hRes;
  335.     (*pRes).version    = 1;
  336.     (*pRes).tabs       = prefTabs;
  337.     (*pRes).wrap       = prefWrap;
  338.     (*pRes).find_ci    = find_ci;
  339.     (*pRes).find_wrap  = find_wrap;
  340.     (*pRes).spare0     = 0;
  341.     (*pRes).showPosnP  = showPosnP;
  342.     (*pRes).spare2     = 0;
  343.     (*pRes).spare3     = 0;
  344.     (*pRes).autoInd    = prefAutoInd;
  345.     (*pRes).stylNormal = prefStylNormal;
  346.     (*pRes).stylHilite = prefStylHilite;
  347.     GetFontName( prefStylNormal.tsFont, fontnmNormal );
  348.     if( fontnmNormal[0] > 63 ) fontnmNormal[0] = 63;
  349.     p_to_p( fontnmNormal, (**hRes).fontnmNormal );
  350.     GetFontName( prefStylHilite.tsFont, fontnmHilite );
  351.     if( fontnmHilite[0] > 63 ) fontnmHilite[0] = 63;
  352.     p_to_p( fontnmHilite, (**hRes).fontnmHilite );
  353.     
  354.     ChangedResource( (Handle )hRes );
  355.     CloseResFile( rRef );
  356. }
  357.  
  358. static void readPrefs( ConstStr255Param fn )
  359. {
  360.     short rRef;
  361.       OSErr err;
  362.     struct prefs **hRes;
  363.     struct prefs  *pRes;
  364.     short foundVRefNum;
  365.     long  foundDirID;
  366.     short    fontNumber;
  367.  
  368.     err = findPrefFolder( &foundVRefNum, &foundDirID );
  369.     if( err != noErr ) return;
  370.     
  371.     rRef = HOpenResFile( foundVRefNum, foundDirID, fn, fsRdWrPerm );
  372.     /* don't test rRef here since resource may come from elsewhere, e.g., from appl. */
  373.     hRes = (struct prefs **)GetResource('ePrf', 357);
  374.     if(    hRes != 0 )
  375.     {    pRes = *hRes;
  376.         if( (*pRes).version == 1 )
  377.         {    prefTabs   = (*pRes).tabs;
  378.             prefWrap   = (*pRes).wrap;
  379.             find_ci    = (*pRes).find_ci;
  380.             find_wrap  = (*pRes).find_wrap;
  381.             prefAutoInd = (*pRes).autoInd;
  382.             showPosnP  = (*pRes).showPosnP;
  383.             prefStylNormal = (*pRes).stylNormal;
  384.             prefStylHilite = (*pRes).stylHilite;
  385.             GetFNum( (**hRes).fontnmNormal, &prefStylNormal.tsFont );
  386.             if( prefStylNormal.tsFont < 0 ) prefStylNormal.tsFont = 0;
  387.             GetFNum( (**hRes).fontnmHilite, &prefStylHilite.tsFont );
  388.             if( prefStylHilite.tsFont < 0 ) prefStylHilite.tsFont = 0;
  389.         }
  390.     }
  391.     if( rRef >= 0 ) CloseResFile( rRef );
  392. }
  393.  
  394. /* *** */
  395.  
  396. #define fustItem 3
  397. static short itemtowindid[MAX_NB_WINDOWS];
  398. static short windidtodigit[MAX_NB_WINDOWS];
  399. static short qWindItems = 0;
  400.  
  401. #if GENERATINGPOWERPC
  402. #pragma options align=mac68k
  403. #endif
  404.  
  405. typedef struct { short nbmenus6, right, dummy; struct { MenuHandle mh; short left; } menu[100]; } menu_list;
  406.  
  407. #if GENERATINGPOWERPC
  408. #pragma options align=reset
  409. #endif
  410.  
  411. static short cmd_key_exists( char c )
  412. {
  413.   menu_list **ml = (menu_list **)LMGetMenuList();
  414.   short n = (*ml)->nbmenus6/6;
  415.   short i, j, m, cmd;
  416.   for (i=0; i<n; i++)
  417.   { MenuHandle mh = (*ml)->menu[i].mh;
  418.     m = CountMItems( mh );
  419.     for (j=1; j <= m; j++)
  420.     {
  421.       GetItemCmd( mh, j, &cmd );
  422.       if (cmd == c) return 1;
  423.     }
  424.   }
  425.   return 0;
  426. }
  427.  
  428. static void addWindMenuItem( short id )
  429. {    short d, i, j;
  430.     Str255 title, key = "\p / ";
  431.     WindowPtr wind = wind_table[id].wptr;
  432.     if (qWindItems >= MAX_NB_WINDOWS) return;
  433.     for (d=1; d<10; d++) if (!cmd_key_exists('0'+d)) break;
  434.     if (d < 10) key[3] = '0'+d; else key[2] = ' ';
  435.     windidtodigit[id] = d;
  436.     for (i=0; i < qWindItems; i++)
  437.       if (windidtodigit[itemtowindid[i]] > d) break;
  438.     for (j=qWindItems-1; j > i; j--)
  439.       itemtowindid[j] = itemtowindid[j-1];
  440.     itemtowindid[i] = id;
  441.     GetWTitle( wind, title );
  442.     InsMenuItem( menus[windowsM], key, i+fustItem-1 );
  443.     SetItem( menus[windowsM], i+fustItem, title );
  444.     qWindItems++;
  445. }
  446.  
  447. static void delWindMenuItem( short id )
  448. {    register short i;
  449.     for(i = 0; i < qWindItems; i++)
  450.     {    if( id == itemtowindid[i] )
  451.         {    DelMenuItem( menus[windowsM], i+fustItem );
  452.             for( i++; i < qWindItems; i++ )
  453.                 itemtowindid[i-1] = itemtowindid[i];
  454.             qWindItems--;
  455.             break;
  456.         }
  457.     }
  458. }
  459.  
  460. static void eSmudgeWindow( register short w )
  461. {    register short i;
  462.     if( ! wind_table[w].dirty )
  463.     {    wind_table[w].dirty = 1;
  464.         for( i = 0; i < qWindItems; i++ )
  465.         {    if( w == itemtowindid[i] )
  466.             {    SetItemMark( menus[windowsM], i+fustItem, '◊');
  467.                 break;
  468.             }
  469.         }
  470.     }
  471. }
  472.  
  473. static void eUnSmudgeWindow( register short w, TeHANDLE hTE )
  474. {    register short i;
  475.     (**hTE).dirty = 0;
  476.     if( wind_table[w].dirty )
  477.     {    wind_table[w].dirty = 0;
  478.         for( i = 0; i < qWindItems; i++ )
  479.         {    if( w == itemtowindid[i] )
  480.             {    SetItemMark( menus[windowsM], i+fustItem, 0);
  481.                 break;
  482.             }
  483.         }
  484.     }
  485. }
  486.  
  487. static void eMaybeSmudgeWindow( short w, TeHANDLE hTE )
  488. {
  489.     if( (**hTE).dirty )
  490.         eSmudgeWindow( w );
  491.     else
  492.         eUnSmudgeWindow( w, hTE );    /* for Undo */
  493. }
  494.  
  495. void select_and_show( WindowPtr wptr )
  496. {
  497.   SelectWindow( wptr );
  498.   ShowWindow( wptr );
  499. }
  500.  
  501. /* ************* */
  502.  
  503. static void wind_begin(void)
  504. { short i;
  505.   for (i=0; i<MAX_NB_WINDOWS; i++) wind_table[i].in_use = 0;
  506.   InitGraf( &qd.thePort );
  507.   InitFonts();
  508.   FlushEvents( everyEvent, 0 );
  509.   InitWindows();
  510.   InitMenus();
  511.   TEInit();
  512.   eTeInit();
  513.   InitDialogs( 0L );
  514.   InitCursor();
  515.  
  516.   prefStylHilite.tsFace = bold+condense;
  517.  
  518.   menus[appleM] = GetMenu( appleID );
  519.   AddResMenu( menus[appleM], 'DRVR' );
  520.   menus[fileM]    = GetMenu( fileID );
  521.   menus[editM]    = GetMenu( editID );
  522.   menus[findM]    = GetMenu( findID );
  523.   menus[commandM] = GetMenu( commandID );
  524.   menus[windowsM] = GetMenu( windowsID );
  525.  
  526.   ClearMenuBar();
  527.   for ( (i=appleM); (i<=windowsM); i++ ) InsertMenu(menus[i], 0);
  528.   DrawMenuBar();
  529.   current_menus = 0;
  530.  
  531.   setup_cursors();
  532.  
  533.   find_string =    (unsigned char *)NewPtr( 256 );
  534.   replace_string = (unsigned char *)NewPtr( 256 );
  535.   if( find_string == NULL || replace_string == NULL )
  536.   {    SysBeep(10);
  537.       os_quit();
  538.   }
  539.   find_string[0] = '\0';
  540.   replace_string[0] = '\0';
  541.  
  542.   gCursorRgn = NewRgn();
  543.   SetRectRgn(gCursorRgn, -32768, -32768, 32766, 32766);
  544.   gHasWNE = TrapExists(_WaitNextEvent);
  545.  
  546.   gHasHWPriv = TrapExists(_HWPriv);
  547.  
  548.   readPrefs( PREFS_FILENAME );
  549.  
  550.   XlateRETURN = XlateRETURNp;
  551.  
  552.   init_ae();
  553. }
  554.  
  555. static void wind_close( short id )
  556. { TeHANDLE hTE = wind_table[id].hTE;
  557.   if (id == interaction_id)
  558.   { HideWindow( wind_table[id].wptr );
  559.     eTeSetSelect( hTE, 0, eTeTextLength( hTE ));
  560.     eTeDelete( hTE );
  561.   }
  562.   else if (wind_table[id].in_use)
  563.   { wind_table[id].in_use = 0;
  564.     HideWindow( wind_table[id].wptr );
  565.     eTeDispose( hTE );
  566.     delWindMenuItem( id );
  567.     DisposeControl( wind_table[id].vscroll );
  568.     DisposeControl( wind_table[id].hscroll );
  569.     DisposeWindow( wind_table[id].wptr );
  570.     if (wind_table[id].buf != NULL)
  571.     { DisposPtr( wind_table[id].buf ); wind_table[id].buf = NULL; }
  572.     if( wind_table[id].out_buf != NULL )
  573.     { DisposHandle( wind_table[id].out_buf ); wind_table[id].out_buf = NULL; }
  574.     if( wind_table[id].filename != NULL )
  575.     { DisposPtr( (Ptr )wind_table[id].filename ); wind_table[id].filename = NULL; }
  576.   }
  577. }
  578.  
  579. static void wind_end(void)
  580. { short id;
  581.   for (id=MAX_NB_WINDOWS-1; id>=0; id--) wind_close( id );
  582.   eTePutScrap();
  583. }
  584.  
  585. static pascal click_scroll();
  586.  
  587. static short wind_open( char *name, short visible, short goaway, short bold_input )
  588. { Rect viewRect, vScrollRect, bounds;
  589.   short width = 80 /*(screenBits.bounds.right-SBarWidth-2*Border-11)/FontW*/;
  590.   short height;
  591.   short id;
  592.   Str255 wname;
  593.  
  594.   for (id=0; id<MAX_NB_WINDOWS; id++) if (!wind_table[id].in_use) break;
  595.   if (id == MAX_NB_WINDOWS) return -1;
  596.  
  597.   height = (qd.screenBits.bounds.bottom-Border-48-SBarWidth)/FontH - id;
  598.  
  599.   bounds.left   = 3 + id*5;
  600.   bounds.right  = bounds.left + width*FontW + SBarWidth + Border;
  601.   bounds.top    = 41 + id*(FontH+3);
  602.   bounds.bottom = bounds.top + height*FontH + SBarWidth + Border;
  603.  
  604.   c_to_p( name, wname );
  605.  
  606.   wind_table[id].wptr = (WindowPtr )
  607.     NewWindow( NULL, &bounds, wname, visible, documentProc, (WindowPtr)-1L, goaway, 0L );
  608.   if (wind_table[id].wptr == NULL) return -1;
  609.  
  610.   SetPort( wind_table[id].wptr );
  611.   TextFont( prefStylNormal.tsFont );
  612.   TextSize( prefStylNormal.tsSize );
  613.  
  614.   vScrollRect = (*wind_table[id].wptr).portRect;
  615.   vScrollRect.left = vScrollRect.right-SBarWidth;
  616.   vScrollRect.right += 1;
  617.   vScrollRect.bottom -= SBarWidth-1;
  618.   vScrollRect.top -= 1;
  619.  
  620.   wind_table[id].vscroll =
  621.     NewControl( wind_table[id].wptr, &vScrollRect, "\p", 1, 0, 0, 0,
  622.                 scrollBarProc, 0L);
  623.  
  624.   if (wind_table[id].vscroll != NULL)
  625.   { static FontInfo fInfo;
  626.  
  627.     vScrollRect = (*wind_table[id].wptr).portRect;
  628.     vScrollRect.top = vScrollRect.bottom-SBarWidth;
  629.     vScrollRect.bottom += 1;
  630.     vScrollRect.left += (vScrollRect.right-vScrollRect.left)>>1;
  631.     vScrollRect.right -= SBarWidth-1;
  632.     wind_table[id].hscroll =
  633.       NewControl( wind_table[id].wptr, &vScrollRect, "\p", 1, 0, 0, 0,
  634.                 scrollBarProc, 0L);
  635.     if (wind_table[id].hscroll != NULL)
  636.     {
  637.  
  638.       viewRect = (*wind_table[id].wptr).portRect;
  639.       viewRect.top    += Border;
  640.       viewRect.left   += Border;
  641.       viewRect.bottom -= SBarWidth;
  642.       viewRect.right  -= SBarWidth;
  643.  
  644.       wind_table[id].hTE = eTeNew( wind_table[id].wptr, viewRect, prefTabs, prefWrap,
  645.                                       prefAutoInd, wind_table[id].hscroll, wind_table[id].vscroll );
  646.  
  647.       if (wind_table[id].hTE != NULL)
  648.       {
  649.         eTeSetStyles( wind_table[id].hTE, &prefStylNormal, &prefStylHilite );
  650.         wind_table[id].height     = height;
  651.         wind_table[id].bold_input = bold_input;
  652.         wind_table[id].dirty      = 0;
  653.         wind_table[id].pos        = 0;
  654.         wind_table[id].len        = 0;
  655.         wind_table[id].buf        = NULL;
  656.         wind_table[id].out_buf    = NULL;
  657.         wind_table[id].out_len    = 0;
  658.         wind_table[id].is_file    = 0;
  659.         wind_table[id].filename   = (unsigned char *)NewPtr( FILENAME_LEN );
  660.         if( wind_table[id].filename != NULL )
  661.         { wind_table[id].filename[0] = '\0';
  662.           wind_table[id].in_use   = 1;
  663.           addWindMenuItem( id );
  664.           return id;
  665.         }
  666.         eTeDispose( wind_table[id].hTE );
  667.       }
  668.       DisposeControl( wind_table[id].hscroll );
  669.     }
  670.     DisposeControl( wind_table[id].vscroll );
  671.   }
  672.   DisposeWindow( wind_table[id].wptr );
  673.   return -1;
  674. }
  675.  
  676. static short discard_changes( short id )
  677. { Str255 title;
  678.   GetWTitle( wind_table[id].wptr, title );
  679.   ParamText( "\pDiscard changes to \"", title, "\p\"?", "\p" );
  680.   SysBeep( 10 );
  681.   switch (CautionAlert( ok_cancel_alertID, 0L ))
  682.   { case 3: return 1;
  683.     default: return 0;
  684.   }
  685. }
  686.  
  687. static short mem_full = 0;
  688. static short supress_mem_full_dialog = 0;
  689.  
  690. static pascal long mem_full_err( long size )
  691. { if (mem_full)
  692.   { ParamText1( "\pOut of memory.  The application will exit." );
  693.     SysBeep( 10 );
  694.     StopAlert( ok_alertID, 0L );
  695.     ExitToShell();
  696.   }
  697.   mem_full = 1;
  698.   if ( ! supress_mem_full_dialog )
  699.   { ParamText1( "\pOut of memory." );
  700.     SysBeep( 10 );
  701.     StopAlert( ok_alertID, 0L );
  702.   }
  703.   return 0;
  704. }
  705.  
  706. static void already_open_err( Str255 name )
  707. { ParamText( "\p\"", name, "\p\"", "\pis already open.  Close it first." );
  708.   SysBeep( 10 );
  709.   StopAlert( ok_alertID, 0L );
  710. }
  711.  
  712. static void path_too_long_err( Str255 name )
  713. { ParamText( "\pPath to file \"", name, "\p\" is too long.", "\p" );
  714.   SysBeep( 10 );
  715.   StopAlert( ok_alertID, 0L );
  716. }
  717.  
  718. static void wind_err(void)
  719. { ParamText1( "\pCan't open window" );
  720.   SysBeep( 10 );
  721.   StopAlert( ok_alertID, 0L );
  722. }
  723.  
  724. void io_err( short io )
  725. { unsigned char errnostr[16];
  726.   switch (io)
  727.   { case wrPermErr    : ParamText1( "\pCan't write file!" ); break;
  728.     case dupFNErr     : ParamText1( "\pDuplicate file name!" ); break;
  729.     case fBsyErr      : ParamText1( "\pFile is busy!" ); break;
  730.     case vLckdErr     : ParamText1( "\pVolume is locked!" ); break;
  731.     case fLckdErr     : ParamText1( "\pFile is locked!" ); break;
  732.     case fnfErr       : ParamText1( "\pFile not found!" ); break;
  733.     case bdNamErr     : ParamText1( "\pBad filename!" ); break;
  734.     case ioErr        : ParamText1( "\pIO transfer error!" ); break;
  735.     case dskFulErr    : ParamText1( "\pDisk full!" ); break;
  736.     case dirFulErr    : ParamText1( "\pDirectory full!" ); break;
  737.     case mFulErr      : ParamText1( "\pFile is too large for memory!" ); break;
  738.                         
  739.     case -4101          : ParamText1( "\pNo printer selected. Use the Chooser." ); break;
  740.     default           : NumToString( io, errnostr );
  741.                         ParamText( "\pIO Error! Mac error number: ", errnostr, "\p", "\p" );
  742.                         break;
  743.   }
  744.   SysBeep( 10 );
  745.   StopAlert( ok_alertID, 0L );
  746. }
  747.  
  748. static void te_limit_err(void)
  749. { ParamText1( "\pFile unopened due to size limit on buffer (32000 lines)." );
  750.   SysBeep( 10 );
  751.   StopAlert( ok_alertID, 0L );
  752. }
  753.  
  754. static short check_TEWrite( char *ptr, long len, eTeHandle hTE, short bold )
  755. {    eTeWrite( hTE, ptr, len, bold );
  756.     if (mem_full) { mem_full = 0; return 1; }
  757.     return 0;
  758. }
  759.  
  760. short check_TEInsert( char *ptr, long len, eTeHandle hTE, short bold )
  761. {    eTeInsert( hTE, ptr, len, bold );
  762.     if (mem_full) { mem_full = 0; return 1; }
  763.     return 0;
  764. }
  765.  
  766. static short check_TECut( eTeHandle hTE )
  767. { eTeCut( hTE );
  768.   if (mem_full) { mem_full = 0; return 1; }
  769.   return 0;
  770. }
  771.  
  772. static short check_TECopy( eTeHandle hTE )
  773. { eTeCopy( hTE );
  774.   if (mem_full) { mem_full = 0; return 1; }
  775.   return 0;
  776. }
  777.  
  778. static short check_TEPaste( eTeHandle hTE, short bold )
  779. {     eTePaste( hTE, bold );
  780.     if (mem_full) { mem_full = 0; return 1; }
  781.     return 0;
  782. }
  783.  
  784. static short check_TEKey( char c, eTeHandle hTE, short bold, EventRecord *event )
  785. {    eTeKey( hTE, c, (event->message & keyCodeMask) >> 8, event->modifiers, bold );
  786.     if (mem_full) { mem_full = 0; return 1; }
  787.     return 0;
  788. }
  789.  
  790. short getfullpath( 
  791.     short vRefNum,
  792.     long dirID,
  793.     Str255 fName,
  794.     char *Path,
  795.     short MaxLength,
  796.     short warn )
  797. {
  798.   char *p, *q;
  799.   long len;
  800.   CInfoPBRec block;
  801.   Str255 directoryName;
  802.  
  803.   p = &Path[MaxLength];
  804.   *--p = '\0';
  805.  
  806.   len = (unsigned char)fName[0];
  807.   p -= len;
  808.   if (p < Path) goto too_long;
  809.   BlockMove( &fName[1], p, len );
  810.  
  811.   block.dirInfo.ioNamePtr = directoryName;
  812.   block.dirInfo.ioDrParID = dirID;
  813.  
  814.   do
  815.   { block.dirInfo.ioVRefNum = vRefNum;
  816.     block.dirInfo.ioFDirIndex = -1;
  817.     block.dirInfo.ioDrDirID = block.dirInfo.ioDrParID;
  818.  
  819.     if (PBGetCatInfo( &block, FALSE ) != noErr) return 0;
  820.     *--p = ':';
  821.     len = (unsigned char)directoryName[0];
  822.     p -= len;
  823.     if (p < Path) goto too_long;
  824.     BlockMove( &directoryName[1], p, len );
  825.   } while ( block.dirInfo.ioDrDirID != fsRtDirID );
  826.  
  827.   q = Path;
  828.   while (*p != '\0') *q++ = *p++;
  829.   *q = '\0';
  830.  
  831.   return (short )( q - Path );
  832.  
  833.   too_long:
  834.   if (warn) path_too_long_err( fName );
  835.   return 0;
  836. }
  837.  
  838. #ifdef use_MacTraps2
  839. short getfullpathfromwd(
  840.     long wdRefNum,
  841.     Str255 fName,
  842.     char *Path,
  843.     short MaxLength,
  844.     short warn )
  845. {
  846.   short vRefNum;
  847.   long dirID, procID;
  848.  
  849.   if (GetWDInfo( wdRefNum, &vRefNum, &dirID, &procID ) != noErr) return 0;
  850.  
  851.   return getfullpath( vRefNum, dirID, fName, Path, MaxLength, warn );
  852. }
  853. #else
  854. /* pascal OSErr GetWDInfo(short wdRefNum,short *vRefNum,long *dirID,long *procID) */
  855. short getfullpathfromwd( wdRefNum, fName, Path, MaxLength, warn )
  856. long wdRefNum;
  857. Str255 fName;
  858. char *Path;
  859. short MaxLength;
  860. short warn;
  861. {
  862.   WDPBRec myBlock;
  863.  
  864.   myBlock.ioNamePtr = nil;
  865.   myBlock.ioVRefNum = wdRefNum;
  866.   myBlock.ioWDIndex = 0;
  867.   myBlock.ioWDProcID = 0;
  868.   PBGetWDInfo(&myBlock,false);
  869.  
  870.   if ( myBlock.ioResult != noErr ) return 0;
  871.  
  872.   return getfullpath( myBlock.ioWDVRefNum, myBlock.ioWDDirID, fName, Path, MaxLength, warn );
  873. }
  874. #endif
  875.  
  876. short getfullpathfromcurrentvolume(
  877.     Str255 fName,
  878.     char *Path,
  879.     short MaxLength,
  880.     short warn )
  881. { return getfullpathfromwd( (long)current_volume, fName, Path, MaxLength, warn );
  882. }
  883.  
  884. Point SFGwhere = { 90, 82 };
  885. Point SFPwhere = { 106, 104 };
  886.  
  887. short get_file(
  888.     Str255 prompt,
  889.     short nbtypes,
  890.     SFTypeList ftypes,
  891.     char *path,
  892.     short maxlength )
  893. {
  894.   SFReply reply;
  895.  
  896.   if (nbtypes == 0) nbtypes = -1;
  897.  
  898.   eTePutScrap();
  899.   SFGetFile( SFGwhere, prompt, 0L, nbtypes, ftypes, 0L, &reply );
  900.   eTeGetScrap();
  901.   if (!reply.good) return 0;
  902.  
  903.   current_volume = reply.vRefNum;
  904.   return getfullpathfromwd( (long)current_volume, (StringPtr)reply.fName, path, maxlength, 1 );
  905. }
  906.  
  907. short put_file(
  908.     Str255 prompt,
  909.     Str255 deflt,
  910.     char *path,
  911.     short maxlength )
  912. {
  913.   SFReply reply;
  914.  
  915.   eTePutScrap();
  916.   SFPutFile( SFPwhere, prompt, deflt, 0L, &reply );
  917.   eTeGetScrap();
  918.   if (!reply.good) return 0;
  919.  
  920.   current_volume = reply.vRefNum;
  921.   return getfullpathfromwd( (long)current_volume, (StringPtr)reply.fName, path, maxlength, 1 );
  922. }
  923.  
  924. static short wptr_to_id( WindowPtr w )
  925. { short id;
  926.   for (id=0; id<MAX_NB_WINDOWS; id++)
  927.     if ((wind_table[id].in_use) && (wind_table[id].wptr == (WindowPtr )w)) return id;
  928.   return -1;
  929. }
  930.  
  931. static void setup_view( short id )
  932. { WindowPtr w = wind_table[id].wptr;
  933.   TeHANDLE hTE = wind_table[id].hTE;
  934.   Rect view = w->portRect;
  935.   view.top    += Border;
  936.   view.left   += Border;
  937.   view.bottom -= SBarWidth;
  938.   view.right  -= SBarWidth;
  939.   eTeNewView( hTE, &view );
  940. }
  941.  
  942. static void really_inval_msgrect( short w )
  943. { GrafPtr port = wind_table[w].wptr;
  944.   Rect r = port->portRect;
  945.   r.right -= (( r.right - r.left ) >> 1 ) + SBarWidth + 1;
  946.   r.top = r.bottom - SBarWidth + 1;
  947.   SetPort( port );
  948.   InvalRect( &r );
  949. }
  950. static void inval_msgrect( short w )
  951. { if ( showPosnP ) wind_table[w].needs_inval_ticks = TickCount();
  952. }
  953. static void content( short id, EventRecord *event )
  954. { WindowPtr w = wind_table[id].wptr;
  955.   eTeHandle hTE = wind_table[id].hTE;
  956.   short cntlCode;
  957.   ControlHandle theControl;
  958.   ChPos oldCaret;
  959.  
  960.   SetPort( w );
  961.   ClipRect( &w->portRect );
  962.   GlobalToLocal( &event->where );
  963.   if ((cntlCode = FindControl( event->where, w, &theControl )) == 0 )
  964.   { if (PtInRect( event->where, &(**hTE).viewRect ))
  965.     { oldCaret = (**hTE).caretChPos;
  966.       eTeClick( hTE, event->where, event->modifiers, event->when );
  967.       if ( *(long *)&oldCaret != *(long *)&(**hTE).caretChPos ) inval_msgrect( id );
  968.     }
  969.   }
  970.   else if (cntlCode == inThumb)
  971.   { cntlCode = GetCtlValue( theControl );
  972.     TrackControl( theControl, event->where, 0L );
  973.     if ( ( cntlCode = GetCtlValue( theControl ) - cntlCode ) != 0 )
  974.         if( (**theControl).contrlRect.left < (**theControl).contrlRect.top ) /* horizontal? */
  975.             eTeScroll( hTE, cntlCode, 0, TRUE );
  976.         else
  977.             eTeScroll( hTE, 0, cntlCode, TRUE );
  978.   }
  979.   else
  980.     TrackControl( theControl, event->where, (void *)-1L );
  981. }
  982.  
  983. static void update_window( short w )
  984. { GrafPtr port = wind_table[w].wptr;
  985.   Rect r = port->portRect;
  986.   Str255 tStr;
  987.   eTeHandle hTE;
  988.   r.right -= (( r.right - r.left ) >> 1 ) + SBarWidth + 1;
  989.   r.top = r.bottom - SBarWidth;
  990.   SetPort( port );
  991.   ClipRect( &port->portRect );    /* added for eTe  16Apr92  e  */
  992.   BeginUpdate( port );
  993.   EraseRect( &port->portRect );
  994.   DrawControls( port );
  995.   DrawGrowIcon( port );
  996.   EraseRect( &r );
  997.   MoveTo( r.left, r.top );
  998.   LineTo( r.right, r.top );
  999. #ifdef USE_eTe_debug
  1000.   NumToString( eTeTextLength( wind_table[w].hTE ), tStr);
  1001.   MoveTo( r.left + 4, r.bottom - 4 );
  1002.   ClipRect( &r );
  1003.   DrawString( (ConstStr255Param )tStr );
  1004. #else
  1005.   if ( showPosnP )
  1006.   { hTE = wind_table[w].hTE;
  1007.     MoveTo( r.left + 4, r.bottom - 4 );
  1008.     ClipRect( &r );
  1009.     NumToString( (long )(**hTE).caretChPos.v+1, tStr);
  1010.     DrawString( (ConstStr255Param )tStr );
  1011.     DrawString( "\p:" );
  1012.     NumToString( (long )(**hTE).caretChPos.h, tStr);
  1013.     DrawString( (ConstStr255Param )tStr );
  1014.   }
  1015. #endif
  1016.   eTeUpdate( wind_table[w].hTE );
  1017.   EndUpdate( port );
  1018. }
  1019.  
  1020. static void grow_window( short id, Point p )
  1021. { WindowPtr w = wind_table[id].wptr;
  1022.   long result;
  1023.   short oScroll;
  1024.   Rect r, oView;
  1025.   
  1026.   SetPort( w );
  1027.  
  1028.   /* 16Sep92  e  increase min size for H scroll bar
  1029.   SetRect(&r, 80, 80, screenBits.bounds.right, screenBits.bounds.bottom); */
  1030.   /* 13Oct92  e  per: bernard@sigi.cs.colorado.edu
  1031.   SetRect(&r, 150, 80, screenBits.bounds.right, screenBits.bounds.bottom); */
  1032.   r = (**GetGrayRgn()).rgnBBox;
  1033.   r.top = 80;
  1034.   r.left = 150;
  1035.   /*  13Oct92  e  */
  1036.   result = GrowWindow( w, p, &r );
  1037.   if (result == 0) return;
  1038.   SizeWindow( w, LoWord(result), HiWord(result), 1);
  1039.  
  1040.   InvalRect(&w->portRect);
  1041.   setup_view( id );
  1042.   HideControl( wind_table[id].vscroll );
  1043.   MoveControl( wind_table[id].vscroll, w->portRect.right - SBarWidth, w->portRect.top-1);
  1044.   SizeControl( wind_table[id].vscroll, SBarWidth+1, w->portRect.bottom - w->portRect.top-(SBarWidth-2));
  1045.   HideControl( wind_table[id].hscroll );
  1046.   MoveControl( wind_table[id].hscroll,
  1047.                   w->portRect.right-((w->portRect.right-w->portRect.left)>>1),
  1048.                   w->portRect.bottom - SBarWidth);
  1049.   SizeControl( wind_table[id].hscroll,
  1050.                   ((w->portRect.right-w->portRect.left)>>1)-(SBarWidth-1),
  1051.                   SBarWidth+1);
  1052.   ShowControl( wind_table[id].hscroll );
  1053.   ValidRect(&(*wind_table[id].hscroll)->contrlRect );
  1054.   ShowControl( wind_table[id].vscroll );
  1055.   ValidRect(&(*wind_table[id].vscroll)->contrlRect );
  1056.   update_window( id );
  1057.   /* dunno (sometimes appropriate, sometimes not)... eTeShowCaret( wind_table[id].hTE ); */
  1058. }
  1059.  
  1060. static void put_input( short id, char *ptr, long len, short cr, short flush, short freshline )
  1061. { if (wind_table[id].in_use)
  1062.   { long len1;
  1063.     register long len2 = (wind_table[id].len - wind_table[id].pos);
  1064.     register char *buf, *p1, *p2;
  1065.     if ((len2 < 0) || flush) len2 = 0;
  1066.     len1 = ((cr)?1:0) + len2 + (long)len;
  1067.     buf = NewPtr( len1 );
  1068.     if (buf == NULL) { SysBeep(10); return; }
  1069.     p1 = buf;
  1070.     p2 = wind_table[id].buf + wind_table[id].pos;
  1071.     while (len2 > 0) { *p1++ = *p2++; len2--; }
  1072.     p2 = ptr;
  1073.     while (len > 0) { *p1++ = *p2++; len--; }
  1074.     if (cr) *p1++ = '\r';
  1075.     if (wind_table[id].buf != NULL) DisposPtr( wind_table[id].buf );
  1076.     wind_table[id].buf = buf;
  1077.     wind_table[id].pos = 0;
  1078.     wind_table[id].len = len1;
  1079.     if (freshline)
  1080.     { eTeHandle hTE = wind_table[id].hTE;
  1081.       if ( (**hTE).caretChPos.h > 0 )
  1082.         check_TEInsert( "\r", 1L, hTE, 0 );
  1083.       (**hTE).writeChPos = (**hTE).caretChPos;
  1084.       eTeShowCaret( hTE );
  1085.     }
  1086.   }
  1087. }
  1088.  
  1089. void put_input_str (char *text)
  1090. {
  1091.   check_TEInsert( text, strlen(text), wind_table[interaction_id].hTE, 1 );
  1092.   put_input( interaction_id, text, strlen (text), 1, 0, 1 );
  1093. }
  1094.  
  1095. static void handle_interrupt()
  1096. { interrupted = 1;
  1097.   /* FlushEvents(keyDownMask,0); */
  1098.   /* raise(SIGINT); */
  1099.   /* errno = EINTR; */
  1100. }
  1101.  
  1102. #ifdef THINK_C
  1103.  
  1104. static short open_file( filename, for_output, txt, io )
  1105. Str255 filename;
  1106. short for_output;
  1107. short txt;
  1108. short *io;
  1109. { ioParam pb;
  1110.   fileParam fp;
  1111.   short refnum;
  1112.   short len = (unsigned char)filename[0];
  1113.   short file_existed = 1;
  1114.  
  1115.   pb.ioNamePtr = filename;
  1116.   pb.ioVRefNum = current_volume;
  1117.   pb.ioVersNum = 0;
  1118.   pb.ioPermssn = ((for_output) ? fsRdWrPerm : fsRdPerm);
  1119.   pb.ioMisc = 0;
  1120.  
  1121.   if (for_output)
  1122.   { asm
  1123.     { lea    pb,a0
  1124.       _PBCreate
  1125.     }
  1126.     file_existed = (pb.ioResult == dupFNErr);
  1127.     if ((pb.ioResult != noErr) && !file_existed)
  1128.     { *io = pb.ioResult;  return -1; }
  1129.   }
  1130.   
  1131.   asm
  1132.   { lea    pb,a0
  1133.     _PBOpen
  1134.   }
  1135.   if (pb.ioResult != noErr)
  1136.   { if (for_output)
  1137.       asm
  1138.       { lea    pb,a0
  1139.         _PBDelete
  1140.       }
  1141.     *io = pb.ioResult;
  1142.     return -1;
  1143.   }
  1144.   refnum = pb.ioRefNum;
  1145.   
  1146.   if (!for_output) { *io = noErr; return refnum; }
  1147.  
  1148.   asm
  1149.   { lea    pb,a0
  1150.     _PBSetEOF
  1151.   }
  1152.  
  1153.   if (!file_existed)
  1154.   { fp.ioNamePtr = filename;
  1155.     fp.ioVRefNum = current_volume;
  1156.     fp.ioFVersNum = 0;
  1157.     fp.ioFDirIndex = 0;
  1158.     asm
  1159.     { lea    fp,a0
  1160.       _PBGetFInfo
  1161.       bmi.s  @1
  1162.     }
  1163.     if (txt)
  1164.       fp.ioFlFndrInfo.fdType = 'TEXT';
  1165.     else
  1166.       fp.ioFlFndrInfo.fdType = 'DATA';
  1167.     fp.ioFlFndrInfo.fdCreator = CREATOR;
  1168.     asm
  1169.     { lea    fp,a0
  1170.       _PBSetFInfo
  1171.       @1
  1172.     }
  1173.   }
  1174.  
  1175.   *io = noErr;
  1176.   return refnum;
  1177. }
  1178.  
  1179. #else
  1180.  
  1181. static short open_file( Str255 filename, short for_output, short txt, short *io )
  1182. { HParamBlockRec pb;
  1183.   short refnum;
  1184.   short len = (unsigned char)filename[0];
  1185.   short file_existed = 1;
  1186.  
  1187.   pb.ioParam.ioNamePtr = filename;
  1188.   pb.ioParam.ioVRefNum = current_volume;
  1189.   pb.ioParam.ioVersNum = 0;
  1190.   pb.ioParam.ioPermssn = ((for_output) ? fsRdWrPerm : fsRdPerm);
  1191.   pb.ioParam.ioMisc = 0;
  1192.  
  1193.   if (for_output)
  1194.   { PBCreateSync( (ParmBlkPtr )&pb );
  1195.     file_existed = (pb.ioParam.ioResult == dupFNErr);
  1196.     if ((pb.ioParam.ioResult != noErr) && !file_existed)
  1197.     { *io = pb.ioParam.ioResult;  return -1; }
  1198.   }
  1199.   
  1200.   PBOpenSync( (ParmBlkPtr )&pb );
  1201.   if (pb.ioParam.ioResult != noErr)
  1202.   { if (for_output) PBDeleteSync( (ParmBlkPtr )&pb );
  1203.     *io = pb.ioParam.ioResult;
  1204.     return -1;
  1205.   }
  1206.   refnum = pb.ioParam.ioRefNum;
  1207.   
  1208.   if (!for_output) { *io = noErr; return refnum; }
  1209.  
  1210.   PBSetEOFSync( (ParmBlkPtr )&pb );
  1211.  
  1212.   if (!file_existed)
  1213.   { pb.fileParam.ioNamePtr = filename;
  1214.     pb.fileParam.ioVRefNum = current_volume;
  1215.     pb.fileParam.ioFVersNum = 0;
  1216.     pb.fileParam.ioFDirIndex = 0;
  1217.  
  1218.     if( PBGetFInfoSync( (ParmBlkPtr )&pb ) >= 0 )
  1219.     {   if (txt)
  1220.           pb.fileParam.ioFlFndrInfo.fdType = 'TEXT';
  1221.         else
  1222.           pb.fileParam.ioFlFndrInfo.fdType = 'DATA';
  1223.         pb.fileParam.ioFlFndrInfo.fdCreator = CREATOR;
  1224.         PBSetFInfoSync( (ParmBlkPtr )&pb );
  1225.     }
  1226.   }
  1227.   *io = noErr;
  1228.   return refnum;
  1229. }
  1230.  
  1231. #endif
  1232.  
  1233. static short close_file( short refnum, short *io )
  1234. { *io = FSClose( refnum );
  1235.   return (*io != noErr);
  1236. }
  1237.  
  1238. static short read_file( short id, long line, long chr )
  1239. { TeHANDLE hTE = wind_table[id].hTE;
  1240.   short refnum;
  1241.   short io, io2;
  1242.   long pos = 0;
  1243.   short result;
  1244.   Rect r;
  1245.   short beep = 0;
  1246.   short text = 0;
  1247.   long size, count;
  1248.   ChPos chPos;
  1249.   Handle hData;
  1250.   
  1251.   if ( line > 32768 )
  1252.   { te_limit_err();
  1253.     return 0;
  1254.   }
  1255.   chPos.v = (line==0) ? 0 : line-1;
  1256.   chPos.h = (line==0) ? 0 : chr;
  1257.  
  1258.   SelectWindow( wind_table[id].wptr );
  1259.  
  1260.   if (wind_table[id].dirty)
  1261.     if (!discard_changes( id )) return 0;
  1262.  
  1263.   refnum = open_file( wind_table[id].filename, 0, 1, &io );
  1264.   if (refnum == -1)
  1265.   { io_err( io );
  1266.     return 0;
  1267.   }
  1268.  
  1269.   eTeSetTextPtr( hTE, (Ptr )&text, 1L );
  1270.   io = GetEOF(refnum, &size);
  1271.   if( io == noErr )    {
  1272.     if( hData = NewHandle( size + 1 ) )
  1273.     {    HLock( hData );
  1274.         count = size;
  1275.         io = FSRead( refnum, &count, *hData );
  1276.         HUnlock( hData );
  1277.         if( ( io != noErr ) || ( count != size ) )
  1278.         {    DisposHandle( hData );
  1279.             if ( io == noErr ) io = ioErr;
  1280.             /* io_err( io ); will happen */
  1281.             /* added these next lines 7Jul92  e */
  1282.             mem_full = 0;
  1283.               eTeSetTextPtr( hTE, (Ptr )&text, 1L );
  1284.         }
  1285.         else
  1286.         {    ((char *)*hData)[size] = '\0';
  1287.             io = eTeSetTextHandleDetabify( hTE, hData, prefTabs );
  1288.             if ( io == noErr )
  1289.             {   count = eTeChPosToOffset( hTE, chPos );
  1290.                 if ( count > 0 )
  1291.                     eTeSetSelect( hTE, count-1, count );
  1292.                 else
  1293.                     eTeSetSelect( hTE, count, count );
  1294.                 eTeShowCaret( hTE );
  1295.             }
  1296.             else
  1297.             {    DisposHandle( hData );
  1298.                 /* io_err( io ); will happen */
  1299.                 /* added these next lines 7Jul92  e */
  1300.                 mem_full = 0;
  1301.                   eTeSetTextPtr( hTE, (Ptr )&text, 1L );
  1302.             }
  1303.           }
  1304.       }
  1305.       else
  1306.       { mem_full = 0; io = mFulErr; }
  1307.   }
  1308.  
  1309.   if (close_file( refnum, &io2 ) || ((io != eofErr) && (io != noErr)))
  1310.   { if ((io != eofErr) && (io != noErr)) io_err( io ); else io_err( io2 );
  1311.       eSmudgeWindow( id );
  1312.       return 0;
  1313.   }
  1314.   if (io != noErr)
  1315.       eSmudgeWindow( id );
  1316.   else
  1317.       eUnSmudgeWindow( id, hTE );
  1318.   return (io == noErr);
  1319. }
  1320.  
  1321. static void write_file( short id )
  1322. { TeHANDLE hTE = wind_table[id].hTE;
  1323.   short refnum;
  1324.   short io, io2;
  1325.   long count;
  1326.  
  1327.   refnum = open_file( wind_table[id].filename, 1, 1, &io );
  1328.   if (refnum == -1)
  1329.   { io_err( io );
  1330.     return;
  1331.   }
  1332.  
  1333.   count = eTeTextLength( hTE );
  1334.   io = FSWrite( refnum, &count, *((**hTE).hText) );
  1335.   
  1336.   /* FlushVol added 19Apr93  e  */
  1337.   if ( io == noErr )
  1338.   { io = GetVRefNum( refnum, &io2 );
  1339.       if ( io == noErr )
  1340.         io = FlushVol( 0L, io2 );
  1341.   }
  1342.   
  1343.   if (close_file( refnum, &io2 ) || (io != noErr))
  1344.   { if (io != noErr) io_err( io ); else io_err( io2 );
  1345.     return;
  1346.   }
  1347.   
  1348.   FlushVol( 0L, current_volume ); /* 21Mar94 e */
  1349.  
  1350.   eUnSmudgeWindow( id, hTE );
  1351. }
  1352.  
  1353. short edit( char *name, long line, long chr )
  1354. { short id;
  1355.   Str255 pname;
  1356.   c_to_p( name, pname );
  1357.   if (pname[0] != '\0')
  1358.   { for (id=0; id<MAX_NB_WINDOWS; id++)
  1359.       if (wind_table[id].in_use && wind_table[id].is_file &&
  1360.           (compare_p_to_p( wind_table[id].filename, pname ) == 0)) break;
  1361.   }
  1362.   else
  1363.     id = MAX_NB_WINDOWS;
  1364.  
  1365.   if (id == MAX_NB_WINDOWS) id = wind_open( name, /* 27Jan93 e -- TRUE */ FALSE, TRUE, FALSE );
  1366.   if (id < 0)
  1367.     wind_err();
  1368.   else
  1369.   { wind_table[id].is_file = 1;
  1370.     p_to_p( pname, wind_table[id].filename );
  1371.     supress_mem_full_dialog = 1;        /* 27Jan93  e  */
  1372.     if ( read_file( id, line, chr ) || wind_table[id].dirty ) /* added dirty check 19May93  e */
  1373.         select_and_show( wind_table[id].wptr );
  1374.     else
  1375.     {
  1376.         wind_close( id );
  1377.         id = -1;
  1378.     }
  1379.     supress_mem_full_dialog = 0;
  1380.   }
  1381.   return id;
  1382. }
  1383.  
  1384. void save_as( short id, char *path )
  1385. { Str255 ppath;
  1386.   short i;
  1387.   c_to_p( path, ppath );
  1388.   for (i=0; i<MAX_NB_WINDOWS; i++)
  1389.     if (wind_table[i].in_use && wind_table[i].is_file &&
  1390.         (compare_p_to_p( wind_table[i].filename, ppath ) == 0))
  1391.     { already_open_err( ppath );
  1392.       return;
  1393.     }
  1394.   p_to_p( ppath, wind_table[id].filename );
  1395.   if (wind_table[id].is_file) SetWTitle( wind_table[id].wptr, ppath );
  1396.   if ( id != interaction_id )
  1397.   { delWindMenuItem( id );
  1398.     addWindMenuItem( id );
  1399.   }
  1400.   write_file( id );
  1401. }
  1402.  
  1403. #if 0
  1404. static void wind_quit()
  1405. { short id;
  1406.   for (id=MAX_NB_WINDOWS-1; id>=0; id--)
  1407.   { if (wind_table[id].in_use && wind_table[id].is_file && wind_table[id].dirty)
  1408.     { if (!discard_changes( id )) return;
  1409.       wind_close( id );
  1410.     }
  1411.   }
  1412.   abnormal_exit = 0;
  1413.   gDoQuit = 1;
  1414. }
  1415. #else
  1416. /* 14Jan95 e */
  1417. static void wind_quit(void)
  1418. { short id;
  1419.   // gDoQuit = 1; moved 31Aug95 e
  1420.   for (id=MAX_NB_WINDOWS-1; id>=0; id--)
  1421.   { if (wind_table[id].in_use && wind_table[id].is_file && wind_table[id].dirty)
  1422.     { if (!discard_changes( id )) gDoQuit = 0;
  1423.       else wind_close( id );
  1424.     }
  1425.   }
  1426.   eTePutScrap();
  1427. }
  1428. #endif
  1429.  
  1430. static void handle_close( short id )
  1431. { if (wind_table[id].is_file)
  1432.   { if ((wind_table[id].dirty) && (!discard_changes( id ))) return;
  1433.     wind_close( id );
  1434.   }
  1435. }
  1436.  
  1437. static char upcase( char c )
  1438. { if( c >= 'a' &&  c <= 'z' ) return c - 0x20;
  1439.   else return c;
  1440. }
  1441.  
  1442. Boolean find_next( short id )
  1443. { TeHANDLE hTE = wind_table[id].hTE;
  1444.   char *txt = *((**hTE).hText);
  1445.   long pos = eTeChPosToOffset( hTE, (**hTE).selActive ? (**hTE).selEnd : (**hTE).caretChPos );
  1446.   register char *start = txt + pos - 1;
  1447.   register short len = (unsigned char)find_string[0];
  1448.   register char *end = txt + eTeTextLength( hTE ) - len;
  1449.   register short i;
  1450.   Boolean again = TRUE;
  1451.   Str255 ci_string;
  1452.   if ( len == 0 ) goto not_found;
  1453.   if( find_ci )
  1454.   { for (i=len; i>0; i--)
  1455.       ci_string[i] = upcase( find_string[i] );
  1456.     /* ci_string[0] = len; -- not used */
  1457.   }
  1458.   find_begin:
  1459.   while (start < end)
  1460.   { if( find_ci )
  1461.     { for (i=len; i>0; i--)
  1462.         if( upcase( start[i] ) != (char )ci_string[i] ) goto next_pos;
  1463.     }
  1464.     else
  1465.     { for (i=len; i>0; i--)
  1466.         if (start[i] != (char )find_string[i]) goto next_pos;
  1467.     }
  1468.     eTeSetSelect( hTE, (long)(start-txt+1), (long)(start-txt+1+len) );
  1469.     return 1;
  1470.     next_pos:
  1471.     start++;
  1472.   }
  1473.   if( find_wrap && again ) 
  1474.   { start = txt;
  1475.     end = start + pos - len;
  1476.     again = FALSE;
  1477.     goto find_begin;
  1478.   }
  1479. not_found:
  1480.   SysBeep(10);
  1481.   return 0;
  1482. }
  1483.  
  1484. void replace_next( short id )
  1485. { TeHANDLE hTE = wind_table[id].hTE;
  1486.   if ( ! (**hTE).selActive )
  1487.     SysBeep(10);
  1488.   else
  1489.   { long len = (unsigned char)replace_string[0];
  1490.     check_TEInsert( ((char *)replace_string)+1, len, hTE, wind_table[id].bold_input );
  1491.     find_next( id );
  1492.     eTeShowCaret( hTE );
  1493.   }
  1494. }
  1495.  
  1496. void replace_all( short id )
  1497. { TeHANDLE hTE = wind_table[id].hTE;
  1498.   long len = (unsigned char)replace_string[0];
  1499.   
  1500.   if ( (**hTE).selActive )
  1501.   { long pos = eTeChPosToOffset( hTE, (**hTE).selStart );
  1502.     eTeSetSelect( hTE, pos, pos );
  1503.   }
  1504.   while ( find_next( id ) )
  1505.     check_TEInsert( ((char *)replace_string)+1, len, hTE, wind_table[id].bold_input );
  1506.   eTeShowCaret( hTE );
  1507. }
  1508.  
  1509. static void find_dialog( short id )
  1510. { DialogPtr dp;
  1511.   short i, s;
  1512.   Handle h;
  1513.   Rect r;
  1514.   eTePutScrap();
  1515.   ParamText( "\pFind:", "\pReplace with:", "\p", "\p" );
  1516.   dp = GetNewDialog( find_dlogID, 0L, (WindowPtr)-1L );
  1517.   if (dp == NULL) return;
  1518.   GetDItem( dp, 3, &i, &h, &r );
  1519.   SetIText( h, find_string );
  1520.   GetDItem( dp, 5, &i, &h, &r );
  1521.   SetIText( h, replace_string );
  1522.   GetDItem( dp, 7, &i, &h, &r);
  1523.   SetCtlValue( (ControlHandle )h, find_wrap );
  1524.   GetDItem( dp, 8, &i, &h, &r);
  1525.   SetCtlValue( (ControlHandle )h, find_ci );
  1526.   SelIText( dp, 3, 0, find_string[0] );
  1527.   SetCursor( &qd.arrow );
  1528.   do
  1529.   { ModalDialog( 0L, &s );
  1530.     switch( s )
  1531.     { case 1:
  1532.         GetDItem( dp, 3, &i, &h, &r );
  1533.         GetIText( h, find_string );
  1534.         GetDItem( dp, 5, &i, &h, &r );
  1535.         GetIText( h, replace_string );
  1536.         GetDItem( dp, 7, &i, &h, &r);
  1537.         find_wrap = GetCtlValue( (ControlHandle )h );
  1538.         GetDItem( dp, 8, &i, &h, &r);
  1539.         find_ci   = GetCtlValue( (ControlHandle )h );
  1540.         break;
  1541.       case 7:
  1542.       case 8:
  1543.         GetDItem( dp, s, &i, &h, &r );
  1544.         SetCtlValue( (ControlHandle )h, GetCtlValue( (ControlHandle )h ) ^ 1 );
  1545.         break;
  1546.     }
  1547.   } while( s != 1 && s != 2 );
  1548.   CloseDialog( dp );
  1549.   if( s == 1 ) find_next( id );
  1550.   eTeGetScrap();
  1551. }
  1552.  
  1553. static void handle_pgsetup( short id )
  1554.   TeHANDLE hTE = wind_table[id].hTE;
  1555.   SetCursor(&qd.arrow);
  1556.   PageSetupDialog( hTE );
  1557. }
  1558.  
  1559. static void handle_print( short id )
  1560.   OSErr err;
  1561.   TeHANDLE hTE = wind_table[id].hTE;
  1562.   Str255 flnm;
  1563.   
  1564.   SetCursor(&qd.arrow);
  1565.   err = noErr;
  1566.   if ( ! (**hTE).hPrint )
  1567.     err = PageSetupDialog( hTE );
  1568.   if( ! err )
  1569.   {    pathstr_to_filename( wind_table[id].filename, flnm );
  1570.     err = eTePrint( hTE, true, true, flnm );
  1571.     eTePrint( nil, false, false, flnm );
  1572.   }
  1573.   if( err && err != userCanceledErr && err != iPrAbort ) io_err( err );
  1574. }
  1575.  
  1576. static short handle_menu( long result, short id )
  1577. { short theItem, theMenu, m, w;
  1578.   MenuHandle mh;
  1579.   WindowPeek wPtr;
  1580.   Str255 name;
  1581.  
  1582.   theItem = LoWord( result );
  1583.   theMenu = HiWord( result );
  1584.   if (theMenu == 0) return 1;
  1585.   mh = GetMHandle( theMenu );
  1586.   for (m=appleM; m<=windowsM; m++) if (menus[m] == mh) break;
  1587.   switch (m)
  1588.   { case appleM:
  1589.       if (theItem == 1)
  1590.       { eDoDialog(0);    /* about */
  1591.       }
  1592.       else if (theItem == 2)
  1593.         ;
  1594.       else
  1595.       { GetItem(menus[appleM], theItem, name);
  1596.         OpenDeskAcc( name );
  1597.       }
  1598.       break;
  1599.     case fileM: 
  1600.       switch (theItem)
  1601.       { case newCommand:
  1602.         { short w = wind_open( "Untitled", TRUE, TRUE, FALSE );
  1603.           if (w < 0)
  1604.             wind_err();
  1605.           else
  1606.             wind_table[w].is_file = 1;
  1607.           break;
  1608.         }
  1609.         case openCommand:
  1610.         { short i;
  1611.           for (i=0; i<MAX_NB_WINDOWS; i++) if (!wind_table[i].in_use) break;
  1612.           if (i == MAX_NB_WINDOWS)
  1613.             wind_err();
  1614.           else
  1615.           { char path[FILENAME_LEN];
  1616.               SFTypeList tl;
  1617.               tl[0] = 'TEXT';
  1618.             if (get_file( "\p", 1, tl, path, FILENAME_LEN ))
  1619.               edit( path, 0L, 0L );
  1620.           }
  1621.           break;
  1622.         }
  1623.         case closeCommand:
  1624.           if (id >= 0)
  1625.             handle_close( id );
  1626.           break;
  1627.         case saveCommand:
  1628.           if (id >= 0)
  1629.             if (wind_table[id].is_file && (wind_table[id].filename[0] != '\0'))
  1630.             { write_file( id );
  1631.               break;
  1632.             }
  1633.         case saveasCommand:
  1634.           if (id >= 0)
  1635.           { char path[FILENAME_LEN];
  1636.             Str255 flnm;
  1637.             pathstr_to_filename( wind_table[id].filename, flnm );
  1638.             if (put_file( "\pSave as:", flnm, path, FILENAME_LEN ))
  1639.               save_as( id, path );
  1640.           }
  1641.           break;
  1642.         case revertCommand:
  1643.           if (id >= 0)
  1644.             read_file( id, 0L, 0L );
  1645.           break;
  1646.         /* 25Sep92  e  added print menu items */
  1647.         case pgsetupCommand:
  1648.           if (id >= 0)
  1649.             handle_pgsetup( id );
  1650.           break;
  1651.         case printCommand:
  1652.           if (id >= 0)
  1653.             handle_print( id );
  1654.           break;
  1655.         case quitCommand:
  1656.           gDoQuit = 1; // moved here 31Aug95 e
  1657.           /* 31Aug95 e -- I wonder if there's a non-interactive quit option? */
  1658.           wind_quit();
  1659.           if (gDoQuit) os_quit(); /* 29Dec92  e  */
  1660.           break;
  1661.       }
  1662.       break;
  1663.     case editM: 
  1664.       if (id < 0)
  1665.         SystemEdit(theItem-1);
  1666.       else
  1667.       { TeHANDLE hTE = wind_table[id].hTE;
  1668.         switch (theItem)
  1669.         { case undoCommand:
  1670.             eTeUndo( hTE );
  1671.             eMaybeSmudgeWindow( id, hTE );
  1672.             break;
  1673.           case cutCommand:
  1674.             check_TECut( hTE );
  1675.             eMaybeSmudgeWindow( id, hTE );
  1676.             break;
  1677.           case copyCommand:
  1678.             check_TECopy( hTE );
  1679.             break;
  1680.           case pasteCommand:
  1681.             check_TEPaste( hTE, wind_table[id].bold_input );
  1682.             eMaybeSmudgeWindow( id, hTE );
  1683.             break;
  1684.           case clearCommand:
  1685.             eTeDelete( hTE );
  1686.             eMaybeSmudgeWindow( id, hTE );
  1687.             break;
  1688.           case selAllCommand:
  1689.             eTeSetSelect( hTE, 0, eTeTextLength( hTE ));
  1690.             break;
  1691.           case stylesCommand:
  1692.             eStyleDlg( hTE );
  1693.               break;
  1694.           case showPosnCommand:
  1695.               showPosnP ^= 1;
  1696.             CheckItem( menus[editM], showPosnCommand, showPosnP );
  1697.             for (w=0; w<MAX_NB_WINDOWS; w++)
  1698.                 if (wind_table[w].in_use) really_inval_msgrect( w );
  1699.               break;
  1700.           default: ;
  1701.         }
  1702.         eTeShowCaret( hTE );
  1703.       }
  1704.       break;
  1705.     case findM:
  1706.       if (id >= 0)
  1707.       { TeHANDLE hTE = wind_table[id].hTE;
  1708.         switch (theItem)
  1709.         { case findCommand:
  1710.             find_dialog( id );
  1711.             break;
  1712.           case againCommand:
  1713.             find_next( id );
  1714.             break;
  1715.           case replaceCommand:
  1716.             replace_next( id );
  1717.             eMaybeSmudgeWindow( id, hTE );
  1718.             break;
  1719.           case fEnterCommand:
  1720.           { long sel_sta, sel_end, sel_len = 0;
  1721.             if( (**hTE).selActive ) {
  1722.               sel_sta = eTeChPosToOffset( hTE, (**hTE).selStart );
  1723.               sel_end = eTeChPosToOffset( hTE, (**hTE).selEnd );
  1724.               sel_len = sel_end - sel_sta;
  1725.             }
  1726.             if (sel_len > 0)
  1727.             { if( sel_len > 255 ) sel_len = 255;
  1728.               buf_to_p( (unsigned short)sel_len, *((**hTE).hText) + sel_sta, find_string );
  1729.             }
  1730.             else
  1731.               SysBeep( 1 );
  1732.             break;
  1733.           }
  1734.           case replaceAllCommand:
  1735.             replace_all( id );
  1736.             eMaybeSmudgeWindow( id, hTE );
  1737.             break;
  1738.         }
  1739.         eTeShowCaret( hTE );
  1740.       }
  1741.       break;
  1742.     case commandM: 
  1743.       switch (theItem)
  1744.       { case interruptCommand:
  1745.           HiliteMenu(0);
  1746.           handle_interrupt();
  1747.           return 0;
  1748.         default:
  1749.           eDoCommand(theItem);
  1750.           break;
  1751.       }
  1752.       break;
  1753.     case windowsM:
  1754.       switch(theItem)
  1755.       { case 1:
  1756.           select_and_show( wind_table[interaction_id].wptr );
  1757.           break;
  1758.         case 2: /* blank line */
  1759.           break;
  1760.         default:
  1761.           theItem -= fustItem;
  1762.           if(theItem < qWindItems)
  1763.             SelectWindow( wind_table[itemtowindid[theItem]].wptr );
  1764.           break;
  1765.       }
  1766.       break;
  1767.    }
  1768.   HiliteMenu(0);
  1769.   return 1;
  1770. }
  1771.  
  1772. static short do_tasks()
  1773. { short w;
  1774.   register short i;
  1775.   short men;
  1776.   EventRecord event;
  1777.   Rect r;
  1778.   RgnHandle rgn;
  1779.   long ticks = LMGetTicks();
  1780.  
  1781.   for (i=0; i<MAX_NB_WINDOWS; i++)
  1782.     if (wind_table[i].in_use)
  1783.     { if (wind_table[i].out_len > 0)
  1784.       { HLock( wind_table[i].out_buf );
  1785.         check_TEWrite( *wind_table[i].out_buf, (long)wind_table[i].out_len, wind_table[i].hTE, 0 );
  1786.         HUnlock( wind_table[i].out_buf );
  1787.         wind_table[i].out_len = 0;
  1788.       }
  1789.       /* 10Jan93  e -- added display of caret position */
  1790.       if ( wind_table[i].needs_inval_ticks != 0
  1791.            && ( wind_table[i].needs_inval_ticks + 15 ) < ticks )
  1792.       { really_inval_msgrect( i );
  1793.         wind_table[i].needs_inval_ticks = 0;
  1794.       }
  1795.     }
  1796.   w = wptr_to_id( FrontWindow() );
  1797.   if ( (!gInBackground) && (w >= 0) )
  1798.   { Point pt;
  1799.     Cursor *curs;
  1800.     OSEventAvail(0L, &event); /* This returns a NULL event with the global mouse location */
  1801.     if( gCursRgnOK && PtInRgn( event.where, gCursorRgn ) )
  1802.     { /* punt... SetCursor( current_cursor ); */
  1803.     }
  1804.     else
  1805.     { SetPort( (GrafPtr)wind_table[w].wptr );
  1806.       r = (**wind_table[w].hTE).viewRect;
  1807.       LocalToGlobal((Point *)&(r.top));      /* topLeft  */
  1808.       LocalToGlobal((Point *)&(r.bottom));   /* botRight */
  1809.       if (PtInRect( event.where, &r ))
  1810.       { curs = &ibeam_cursor;
  1811.         RectRgn(gCursorRgn, &r);
  1812.       }
  1813.       else
  1814.       { curs = &qd.arrow;
  1815.         SetRectRgn(gCursorRgn, -32768, -32768, 32766, 32766);
  1816.         rgn = NewRgn();
  1817.         RectRgn(rgn, &r);
  1818.         DiffRgn(gCursorRgn, rgn, gCursorRgn);
  1819.         DisposeRgn(rgn);
  1820.       }
  1821.       if (!gCursRgnOK || current_cursor != curs)
  1822.       { current_cursor = curs;
  1823.         SetCursor( current_cursor );
  1824.       }
  1825.       gCursRgnOK = true;
  1826.     }
  1827.     eTeIdle( wind_table[w].hTE );
  1828.     men = (wind_table[w].is_file && (wind_table[w].filename[0] != '\0')) ? 1 : 0;
  1829.   }
  1830.   else
  1831.   { SetRectRgn(gCursorRgn, -32768, -32768, 32766, 32766);
  1832.     gCursRgnOK = false;
  1833.     SetCursor(&qd.arrow);
  1834.     men = 2;
  1835.   }
  1836.  
  1837.   if ( (!gInBackground) && (men != current_menus) )
  1838.   { current_menus = men;
  1839.     switch (men)
  1840.     { case 0:
  1841.       case 1:
  1842.         EnableItem( menus[fileM], saveCommand );
  1843.         EnableItem( menus[fileM], saveasCommand );
  1844.         if (men == 0)
  1845.           DisableItem( menus[fileM], revertCommand );
  1846.         else
  1847.           EnableItem( menus[fileM], revertCommand );
  1848.         EnableItem( menus[fileM], pgsetupCommand );
  1849.         EnableItem( menus[fileM], printCommand );
  1850.         EnableItem( menus[editM], selAllCommand );
  1851.         EnableItem( menus[editM], stylesCommand );
  1852.         EnableItem( menus[editM], showPosnCommand );
  1853.         EnableItem( menus[findM], 0 );
  1854.         DrawMenuBar();
  1855.         break;
  1856.       case 2:
  1857.         DisableItem( menus[fileM], saveCommand );
  1858.         DisableItem( menus[fileM], saveasCommand );
  1859.         DisableItem( menus[fileM], revertCommand );
  1860.         DisableItem( menus[fileM], pgsetupCommand );
  1861.         DisableItem( menus[fileM], printCommand );
  1862.         DisableItem( menus[editM], selAllCommand );
  1863.         DisableItem( menus[editM], stylesCommand );
  1864.         DisableItem( menus[editM], showPosnCommand );
  1865.         DisableItem( menus[findM], 0 );
  1866.         DrawMenuBar();
  1867.         break;
  1868.     }
  1869.   }
  1870.  
  1871.   if ( (!gInBackground) && (w>=0) )
  1872.   { register EvQElPtr q = (EvQElPtr )LMGetEventQueue();
  1873.     for (q = (EvQElPtr)((QHdrPtr )q)->qHead; q != NULL; q = (EvQElPtr)q->qLink)
  1874.       if ((q->evtQWhat == keyDown) && ((char)q->evtQMessage == '.') &&
  1875.           (q->evtQModifiers & cmdKey))
  1876.       { while (GetNextEvent(keyDownMask+keyUpMask+autoKeyMask,&event) &&
  1877.                !((event.what == keyDown) && ((event.message & charCodeMask) == '.') &&
  1878.                  ((event.modifiers & cmdKey) != 0) )) ;
  1879.         handle_interrupt();
  1880.         return 0;
  1881.       }
  1882.   }
  1883.   return 1;
  1884. }
  1885.  
  1886. /* from MacDTS */
  1887. #define kDITop                    0x0050        /* kTopLeft - for positioning the Disk */
  1888. #define kDILeft                    0x0070        /*   Initialization dialogs. */
  1889.  
  1890. void handle_activate( WindowPtr wptr, Boolean activep )
  1891. { GrafPtr port;
  1892.   Rect r;
  1893.   short w = wptr_to_id( wptr );
  1894.   if( w >= 0 )
  1895.   { port = wind_table[w].wptr;
  1896.     r = port->portRect;
  1897.     r.left = r.right - (SBarWidth+1);
  1898.     SetPort( port );
  1899.     InvalRect( &r );
  1900.     r = port->portRect;
  1901.     r.top = r.bottom - (SBarWidth+1);
  1902.     InvalRect( &r );
  1903.     if ( activep )
  1904.     { eTeActivate( wind_table[w].hTE );
  1905.       ShowControl( wind_table[w].vscroll );
  1906.       ShowControl( wind_table[w].hscroll );
  1907.     }
  1908.     else
  1909.     { eTeDeactivate( wind_table[w].hTE );
  1910.       HideControl( wind_table[w].vscroll );
  1911.       HideControl( wind_table[w].hscroll );
  1912.     }
  1913.   }
  1914. }
  1915.  
  1916. static short handle_event( EventRecord *event )
  1917. { register short i;
  1918.   short w;
  1919.   WindowPtr event_window;
  1920.  
  1921. #if (CREATOR == 'Caml') || (CREATOR == 'Moml')
  1922. extern int DoGraphEvent(EventRecord *event, WindowPtr window);
  1923. #endif
  1924.  
  1925.   if (IsDialogEvent( event ))
  1926.   { DialogPtr dp;
  1927.     short item;
  1928.     DialogSelect( event, &dp, &item );
  1929.     return 1;
  1930.   }
  1931.   
  1932.   event_window = FrontWindow();
  1933.   
  1934. #if (CREATOR == 'Caml') || (CREATOR == 'Moml')
  1935.   if ( DoGraphEvent( event, event_window ) ) return 1;
  1936. #endif
  1937.  
  1938.   w = wptr_to_id( event_window );
  1939.   { switch (event->what)
  1940.     { case mouseDown:
  1941.         gCursRgnOK = false;
  1942.         switch (FindWindow(event->where,&event_window))
  1943.         { case inDesk: 
  1944.             SysBeep( 10 );
  1945.             break;
  1946.           case inGoAway:
  1947.             if ((w = wptr_to_id( event_window )) >= 0)
  1948.               if (TrackGoAway(event_window,event->where)) handle_close( w );
  1949.             break;
  1950.           case inMenuBar:
  1951.           { TeHANDLE hTE = NULL;
  1952.             if( w >= 0) hTE = wind_table[w].hTE;
  1953.             eTeEditMenuUpdate( hTE, menus[editM] );
  1954.             return handle_menu( MenuSelect(event->where), w );
  1955.           }
  1956.           case inSysWindow:
  1957.             SystemClick(event,event_window);
  1958.             break;
  1959.           case inDrag:
  1960.             if ((w = wptr_to_id( event_window )) >= 0)
  1961.               DragWindow(event_window,event->where,&qd.screenBits.bounds);
  1962.             break;
  1963.           case inGrow:
  1964.             if ((w = wptr_to_id( event_window )) >= 0)
  1965.               grow_window( w, event->where );
  1966.             break;
  1967.           case inContent:
  1968.             if (event_window != FrontWindow())
  1969.               SelectWindow(event_window);
  1970.             else 
  1971.               if ((w = wptr_to_id( event_window )) >= 0)
  1972.                 content( w, event );
  1973.             break;
  1974.           default: ;
  1975.         }
  1976.         gLastMouseDown = *event;
  1977.         break;
  1978.       case mouseUp:
  1979.         gLastMouseUp = *event;
  1980.         break;
  1981.       case keyDown:
  1982.       case autoKey:
  1983.         { unsigned char c = event->message & charCodeMask;
  1984.           TeHANDLE hTE = (w>=0)?wind_table[w].hTE:NULL;
  1985.           if ((event->modifiers & cmdKey) != 0)
  1986.           { long res;
  1987.             if (c == '=') c = '+'; else if (c == '/') c = '?';
  1988.             if (hTE != NULL) eTeEditMenuUpdate( hTE, menus[editM] );
  1989.             res = MenuKey( c );
  1990.             if( HiWord( res ) != 0 )
  1991.                 return handle_menu( res, w );
  1992.             if( (hTE != NULL) && (c >= 0) && (c <= 31) )
  1993.             {    check_TEKey( c, hTE, 0, event );
  1994.                 inval_msgrect( w );
  1995.             }
  1996.             break;
  1997.           }
  1998.           if (hTE == NULL) break;
  1999. #if LISPmodeP
  2000.           if( ( event->modifiers & controlKey ) != 0 )
  2001.           { eEditCommand( hTE, c, event->modifiers, wind_table[w].bold_input );
  2002.               eMaybeSmudgeWindow( w, hTE );
  2003.               inval_msgrect( w );
  2004.           }
  2005. #endif
  2006.           else if( ( c == 127 /* del fwd */ )  || ( c <= 31 ) )
  2007.           { if( ( c == 3 /*enter*/ ) || (( c == '\r' /*cr*/ ) && ((event->modifiers & optionKey) != 0 )))
  2008.             { short id = (wind_table[w].is_file) ? interaction_id : w;
  2009.               long sel_sta;
  2010.               long sel_end;
  2011.               long sel_len;
  2012.               if( (**hTE).selActive ) {
  2013.                   sel_sta = eTeChPosToOffset( hTE, (**hTE).selStart );
  2014.                   sel_end = eTeChPosToOffset( hTE, (**hTE).selEnd );
  2015.               }
  2016.               else
  2017.               { extern void eTeGetRun( eRec **hE, long *sta, long *end );
  2018.                 if ( w == interaction_id )
  2019.                   eTeGetRun( hTE, &sel_sta, &sel_end );
  2020.                 else
  2021. #if LISPmodeP
  2022.                 { sel_end = eTeChPosToOffset( hTE, (**hTE).caretChPos );
  2023.                   sel_sta = balance_bwd( wind_table[w].hTE, 0, 1 );
  2024.                   if (sel_sta < 0) sel_sta = sel_end;
  2025.                 }
  2026. #else
  2027.                   sel_end = sel_sta = eTeChPosToOffset( hTE, (**hTE).caretChPos );
  2028. #endif
  2029.               }
  2030.               eTeSetSelect( hTE, sel_end, sel_end );
  2031.               sel_len = sel_end - sel_sta;
  2032.               HLock((**hTE).hText);
  2033.               if (sel_len > 0)
  2034.                 put_input( id, *((**hTE).hText) + sel_sta, sel_len, 1, 0, 1 );
  2035.               else
  2036.                 put_input( id, NULL, 0, 1, 0, 1 ); /*  not done yet!?  12Sep92  e  */
  2037.               HUnlock((**hTE).hText);
  2038.             }
  2039.             else if (c == 9) /*tab*/
  2040.             { eTabCommand( hTE, event->modifiers, wind_table[w].bold_input );
  2041.               eMaybeSmudgeWindow( w, hTE );
  2042.               inval_msgrect( w );
  2043.             }
  2044.             else
  2045.             { check_TEKey( c, hTE, wind_table[w].bold_input, event );
  2046.               if ((c == '\r') && (**hTE).autoInd)
  2047.                 eTabCommand( hTE, optionKey, wind_table[w].bold_input );
  2048.               eMaybeSmudgeWindow( w, hTE );
  2049.               inval_msgrect( w );
  2050.               if ( (c=='\r' /*cr*/) || (c=='\b' /*bs*/) ) eTeShowCaret( hTE );
  2051.             }
  2052.           }
  2053.           else
  2054.           { 
  2055. #if LISPmodeP
  2056.               if (!check_TEKey( c, hTE, wind_table[w].bold_input, event ))
  2057.                 if (c == ')')
  2058.                 { long open_paren = balance_bwd( wind_table[w].hTE, 0, 0 );
  2059.                   if (open_paren >= 0)
  2060.                   { long now;
  2061.                     long selstart = eTeChPosToOffset( hTE, (**hTE).caretChPos );
  2062.                     eTeSetSelect( hTE, open_paren, open_paren+1 );
  2063.                     eTeShowCaret( hTE );
  2064.                     now = TickCount();
  2065.                     while ((TickCount()-now)<10) ;
  2066.                     while (!EventAvail(everyEvent,event) && ((TickCount()-now)<30)) ;
  2067.                     eTeSetSelect( hTE, selstart, selstart );
  2068.                   }
  2069.                 }
  2070. #else
  2071.             check_TEKey( c, hTE, wind_table[w].bold_input, event );
  2072. #endif
  2073.             eMaybeSmudgeWindow( w, hTE );
  2074.             inval_msgrect( w ); /* 10Jan93  e -- added display of caret position */
  2075.             eTeShowCaret( hTE );
  2076.           }
  2077.         }
  2078.         break;
  2079.       case activateEvt:
  2080.         gCursRgnOK = false;
  2081.           handle_activate( (WindowPtr)event->message, ((event->modifiers & activeFlag) != 0) );
  2082.         break;
  2083.       case updateEvt: 
  2084.         if ((w = wptr_to_id( (WindowPtr)event->message )) >= 0)
  2085.         { update_window(w);
  2086.         }
  2087.         else
  2088.         { GrafPtr port = (WindowPtr)event->message;
  2089.           SetPort( port );
  2090.           BeginUpdate( port );
  2091.           EndUpdate( port );
  2092.         }
  2093.         break;
  2094.       case osEvt:
  2095.         if( *(char *)(&event->message) == suspendResumeMessage )
  2096.         { gCursRgnOK = false; /* Suspend/resume is also an activate/deactivate. */
  2097.           gInBackground = !(event->message & resumeFlag);
  2098.           if( gInBackground ) eTePutScrap();
  2099.           else                  eTeGetScrap();
  2100.           handle_activate( FrontWindow(), !gInBackground );
  2101.         }
  2102.         /* punt mouseMovedMessage */
  2103.         break;
  2104.       case diskEvt:
  2105.         gCursRgnOK = false;                     /* It is not a bad idea to at least call */
  2106.         if (HiWord(event->message) != noErr) /* DIBadMount in response to a diskEvt,  */
  2107.         { Point pt;                             /* so that the user can format a floppy. */
  2108.           SetPt( &pt, kDILeft, kDITop );
  2109.           DIBadMount( pt, event->message );
  2110.         }
  2111.         break;
  2112.       /* 29Dec92  e  */
  2113.       case kHighLevelEvent:
  2114.         AEProcessAppleEvent(event);
  2115.         if (gDoQuit) os_quit();
  2116.         break;
  2117.       default: ;
  2118.     }
  2119.   }
  2120.   return 1;
  2121. }
  2122.  
  2123. long os_get_next_event( EventRecord *event )
  2124. { long result = 0;
  2125.   GrafPtr save;
  2126.   GetPort( &save );
  2127.   if (do_tasks())
  2128.   { beg_mf_time(); /* 11Jan95 e */
  2129.     if (gHasWNE)
  2130.       { result = (long)( gInBackground ? gWaitTicksBG
  2131.                                          : (long )( (gWaitTicksFG > gMaxSleep) ? gMaxSleep
  2132.                                                                                 : gWaitTicksFG) );
  2133.       result = WaitNextEvent( everyEvent, event, result, gCursorRgn );
  2134.     }
  2135.     else
  2136.     { SystemTask();
  2137.       result = GetNextEvent( everyEvent, event );
  2138.     }
  2139.     end_mf_time(); /* 11Jan95 e */
  2140.   }
  2141.   SetPort( save );
  2142.   return result;
  2143. }
  2144.  
  2145. long os_handle_event( EventRecord *event )
  2146. { long result;
  2147.   GrafPtr save;
  2148.   GetPort( &save );
  2149.   result = handle_event( event );
  2150.   SetPort( save );
  2151.   return result;
  2152. }
  2153.  
  2154. void os_event_check( void )
  2155. { EventRecord event;
  2156.   os_get_next_event( &event );
  2157.   os_handle_event( &event );
  2158. #if USE_EVENTCHK_TIMER
  2159.   next_eventchk_ticks = 0;
  2160. #else
  2161.   next_eventchk_ticks = LMGetTicks() + TicksBetweenEventChecks;
  2162. #endif
  2163.   if ( interrupted ) { interrupted = 0; raise(SIGINT); }
  2164. }
  2165.  
  2166. void os_quit()
  2167. { short i;
  2168.   long ticks;
  2169. #if 0
  2170.   stop_intr_task();
  2171.   for (i=MAX_NB_OPEN_FILES-1; i>0; i--) os_file_close( (OS_FILE)i );
  2172.   if (abnormal_exit)
  2173.   { for (i=0; i<3; i++) { SysBeep(3); Delay( 3, &ticks ); }
  2174.     Delay( 60, &ticks );
  2175.   }
  2176. #endif
  2177. #if ANSIquitP
  2178.   exit(0);
  2179. #else
  2180.   wind_end();
  2181. #if USE_EVENTCHK_TIMER
  2182.   cancel_eck_timer();
  2183. #endif
  2184.   ExitToShell();
  2185. #endif
  2186. }
  2187.  
  2188. /* ******* console io ******* */
  2189.  
  2190. #if 0
  2191. void os_flush_caches()
  2192. { if (gHasHWPriv)
  2193.   asm
  2194.   { moveq #1,d0
  2195.     dc.w _HWPriv
  2196.   }
  2197. }
  2198. #endif
  2199.  
  2200. static GrowZoneUPP mem_full_errUPP;                 /* 04Jan95 e */
  2201.  
  2202. static short main_internal( )
  2203. { short i;
  2204.   MaxApplZone();
  2205.   for (i = 0; i < 10; i++)
  2206.         MoreMasters();
  2207.   /* CouldAlert( ok_alertID ); */
  2208.   mem_full_errUPP = NewGrowZoneProc(mem_full_err); /* 04Jan95 e */
  2209.   SetGrowZone( mem_full_errUPP );                  /* 04Jan95 e  was: mem_full_err */
  2210.  
  2211.   wind_begin();
  2212.  
  2213. #if ANSIquitP
  2214.   atexit(wind_quit);  /* 14Jan95 e */
  2215. #endif
  2216.  
  2217. #if 0
  2218.   GetVol( new_args, ¤t_volume );
  2219.   if (!getfullpathfromcurrentvolume( CurApName, new_args, 256, 0 ))
  2220.     p_to_c( CurApName, new_args );
  2221. #endif
  2222.  
  2223.   SetCursor( current_cursor );
  2224.  
  2225.   ParamText1( "\p" );
  2226.  
  2227. #if 0
  2228.   loading_dp = GetNewDialog( loading_dlogID, 0L, (WindowPtr)-1L );
  2229.   if (loading_dp != NULL) DrawDialog( loading_dp );
  2230. #endif
  2231.  
  2232. #if USE_EVENTCHK_TIMER
  2233.   init_eck_timer();
  2234. #endif
  2235.  
  2236.   return 0;
  2237. }
  2238.  
  2239. static short console_initialized = 0;
  2240.  
  2241. WindowPeek os_console_new( unsigned char *name )
  2242. { char cname[256];
  2243.   short id;
  2244.   WindowPeek wp;
  2245.   if( !console_initialized )
  2246.   { main_internal();
  2247.     console_initialized = 1;
  2248.   }
  2249.   p_to_c( name, cname );
  2250.   id = wind_open( cname, FALSE, FALSE, TRUE );
  2251.   if( id < 0 ) return (WindowPeek )0;
  2252.   interaction_id = id;
  2253.   delWindMenuItem( interaction_id );
  2254.   wp = (WindowPeek )wind_table[id].wptr;
  2255.   wp->refCon = (long )id;
  2256.   return(wp);
  2257. }
  2258.  
  2259. long os_console_chars_ready(WindowPeek wp)
  2260. {  short id = wp->refCon;
  2261.    return wind_table[id].len - wind_table[id].pos;
  2262. }
  2263.  
  2264. long os_console_read( WindowPeek wp, unsigned char *ptr, long cnt )
  2265. #if ReadHangsP
  2266.   long result;
  2267.   do
  2268.   { result = os_console_read_nohang( wp, ptr, cnt );
  2269.   } while (result == 0);
  2270.   return result;
  2271. #else
  2272.   return os_console_read_nohang( wp, ptr, cnt );
  2273. #endif
  2274. }
  2275.  
  2276. long os_console_read_nohang( WindowPeek wp, unsigned char *ptr, long cnt )
  2277. { long result = 0;
  2278.   GrafPtr save;
  2279.   GetPort( &save );
  2280.   if( cnt > 0 )
  2281.   { register unsigned char *p1 = ptr;
  2282.     short id = wp->refCon;
  2283.     EventRecord event;
  2284.     do
  2285.     { os_get_next_event( &event );
  2286.       os_handle_event( &event );
  2287.       if( interrupted )
  2288.         goto ocr_done;
  2289.       else
  2290.       { register unsigned char *p2 = (unsigned char *)wind_table[id].buf + wind_table[id].pos;
  2291.         register long len = wind_table[id].len - wind_table[id].pos;
  2292.         register short i = 0;
  2293.         if (len > cnt) len = cnt;
  2294.         cnt = len; /* nohang */
  2295.         while ( i < len )
  2296.         { register unsigned char c = p2[i++];
  2297.           if (c == '\r')
  2298.           {
  2299.             if (XlateRETURN != 0) c = '\n';
  2300.             len = i; /* to exit inner loop */
  2301.             cnt = i; /* to exit outer loop */
  2302.           }
  2303.           *p1++ = c;
  2304.         }
  2305.         wind_table[id].pos += i;
  2306.         cnt -= i;
  2307.         result += i;
  2308.       }
  2309.     } while( cnt > 0 );
  2310.   }
  2311. ocr_done:
  2312.   SetPort( save );
  2313.   return result;
  2314. }
  2315.  
  2316. long os_console_write( WindowPeek wp, unsigned char *ptr, long cnt)
  2317. { long result = 0;
  2318.   GrafPtr save;
  2319.   short id = (short )wp->refCon;
  2320.  
  2321.   GetPort( &save );
  2322.  
  2323.   if (!(wp->visible))
  2324.     select_and_show( (WindowPtr )wp );
  2325.  
  2326.   if (XlateRETURN)
  2327.   {
  2328.     register unsigned char *p1 = ptr, *p2 = ptr+cnt;
  2329.     register short len = wind_table[id].out_len;
  2330.     register unsigned char *p3;
  2331.     if( wind_table[id].out_buf == NULL )
  2332.     { wind_table[id].out_buf = NewHandle( OUT_BUF_LEN );
  2333.       if( wind_table[id].out_buf == NULL )
  2334.       { SysBeep(10);
  2335.         return result;
  2336.       }
  2337.     }
  2338.     HLock( wind_table[id].out_buf );
  2339.     p3 = (unsigned char *)*wind_table[id].out_buf;
  2340.  
  2341.     while( p1 < p2 )
  2342.     { register unsigned char c = *p1++;
  2343.       if (c == '\n') { p3[len++] = '\r'; goto output; }
  2344.       p3[len++] = c;
  2345.       if (len == OUT_BUF_LEN)
  2346.       { output:
  2347.         if( check_TEWrite( (char *)p3, (long)len, wind_table[id].hTE, 0 ) ) goto werror;
  2348.         wind_table[id].out_len = 0;
  2349.         len = 0;
  2350.       }
  2351.     }
  2352.     wind_table[id].out_len = len;
  2353.     result = cnt;
  2354. werror:
  2355.     SetPort( save );
  2356.     HUnlock( wind_table[id].out_buf );
  2357.   }
  2358.   else
  2359.   {
  2360.     if( check_TEWrite( (char *)ptr, (long)cnt, wind_table[id].hTE, 0 ) == 0 )
  2361.       result = cnt;
  2362.   }
  2363.   return result;
  2364. }
  2365.  
  2366. long os_console_close( WindowPeek wp )
  2367. { long result = 0;
  2368.   GrafPtr save;
  2369.   short id = (short )wp->refCon;
  2370.   GetPort( &save );
  2371.   if( id < 0 )
  2372.     result = -1;
  2373.   else
  2374.     wind_close( id );
  2375.   SetPort( save );
  2376.   return result;
  2377. }
  2378.  
  2379. /*--------------------------------------------------------------------------*/
  2380.  
  2381. /* Used to check for any unread required parameters. Returns true if we
  2382. ** missed at least one. */
  2383.  
  2384. static Boolean    MissedAnyParameters(AppleEvent *message)
  2385. {
  2386.     OSErr        err;
  2387.     DescType    ignoredActualType;
  2388.     AEKeyword    missedKeyword;
  2389.     Size        ignoredActualSize;
  2390.     EventRecord    event;
  2391.  
  2392.     err = AEGetAttributePtr(    /* SEE IF PARAMETERS ARE ALL USED UP.          */
  2393.         message,                /* AppleEvent to check.                          */
  2394.         keyMissedKeywordAttr,    /* Look for unread parameters.                  */
  2395.         typeWildCard,            /* So we can see what type we missed, if any. */
  2396.         &ignoredActualType,        /* What it would have been if not coerced.      */
  2397.         (Ptr)&missedKeyword,    /* Data area.  (Keyword not handled.)          */
  2398.         sizeof(missedKeyword),    /* Size of data area.                          */
  2399.         &ignoredActualSize        /* Actual data size.                          */
  2400.     );
  2401.  
  2402. /* No error means that we found some unused parameters. */
  2403.  
  2404.     if (err == noErr) {
  2405.         event.message = *(long *) &ignoredActualType;
  2406.         event.where = *(Point *) &missedKeyword;
  2407.         err = errAEEventNotHandled;
  2408.     }
  2409.  
  2410. /* errAEDescNotFound means that there are no more parameters.  If we get
  2411. ** an error code other than that, flag it. */
  2412.  
  2413.     return(err != errAEDescNotFound);
  2414. }
  2415.  
  2416. static OSErr    OpenDocEventHandler(AppleEvent *message, AppleEvent *reply, short mode)
  2417. {
  2418.     OSErr        err;
  2419.     OSErr        err2;
  2420.     AEDesc        theDesc;
  2421.     FSSpec        theFSS;
  2422.     short        loop;
  2423.     long        numFilesToOpen;
  2424.     AEKeyword    ignoredKeyWord;
  2425.     DescType    ignoredType;
  2426.     Size        ignoredSize;
  2427.     char path[512];
  2428.     short w;
  2429.     Str255 flnm;
  2430.     FInfo theFInfo;
  2431.  
  2432.     theDesc.dataHandle = nil;
  2433.         /* Make sure disposing of the descriptors is okay in all cases.
  2434.         ** This will not be necessary after 7.0b3, since the calls that
  2435.         ** attempt to create the descriptors will nil automatically
  2436.         ** upon failure. */
  2437.  
  2438.     if ( err = AEGetParamDesc( message, keyDirectObject, typeAEList, &theDesc ) )
  2439.         return(err);
  2440.  
  2441.     if ( ! MissedAnyParameters( message ) ) {
  2442.  
  2443. /* Got all the parameters we need.  Now, go through the direct object,
  2444. ** see what type it is, and parse it up. */
  2445.  
  2446.         err = AECountItems(&theDesc, &numFilesToOpen);
  2447.         if (!err)
  2448.         {    /* We have numFilesToOpen that need opening, as either a window
  2449.             ** or to be printed.  Go to it... */
  2450.  
  2451.             for (loop = 1; ((loop <= numFilesToOpen) && (!err)); ++loop)
  2452.             {    err = AEGetNthPtr(        /* GET NEXT IN THE LIST...         */
  2453.                     &theDesc,            /* List of file names.             */
  2454.                     loop,                /* Item # in the list.             */
  2455.                     typeFSS,            /* Item is of type FSSpec.         */
  2456.                     &ignoredKeyWord,    /* Returned keyword -- we know.  */
  2457.                     &ignoredType,        /* Returned type -- we know.     */
  2458.                     (Ptr)&theFSS,        /* Where to put the FSSpec info. */
  2459.                     sizeof(theFSS),        /* Size of the FSSpec info.         */
  2460.                     &ignoredSize        /* Actual size -- we know.         */
  2461.                 );
  2462.                 if (err) break;
  2463.  
  2464.                 FSpGetFInfo( &theFSS, &theFInfo );
  2465.                 w = getfullpath( theFSS.vRefNum, theFSS.parID, theFSS.name, path, 511, 1 );
  2466.                 if ( theFInfo.fdType != 'TEXT' )
  2467.                 { 
  2468. #ifdef PFILmode
  2469.                   if( theFInfo.fdType == 'FILi' ) pfil_ae_load( &theFSS ); /* 7Jun94 e */
  2470.                   else
  2471. #endif
  2472. #if ( CREATOR == 's48e' )
  2473.                   /* 07Jan95 e */
  2474.                   char *cmnd = "vm -i ";
  2475.                   if ( ( theFInfo.fdType == 'BINA' ) && w )
  2476.                   {
  2477.                     check_TEInsert( cmnd, 6, wind_table[interaction_id].hTE, 1 );
  2478.                     check_TEInsert( path, w, wind_table[interaction_id].hTE, 1 );
  2479.                     put_input( interaction_id, cmnd, 6, 0, 0, 1 );
  2480.                     put_input( interaction_id, path, w, 1, 0, 0 );
  2481.                   }
  2482.                   else
  2483. #endif
  2484.                   SysBeep( 3 );
  2485.                   continue;
  2486.                 }
  2487.                 gPrintPage = mode;
  2488.                     /* Open the window off-screen if we are printing.
  2489.                     ** We use the gPrintPage global to flag this.  Normally, the
  2490.                     ** gPrintPage global is to tell ImageDocument if we are imaging
  2491.                     ** to the window or to paper.  We don't need it for this yet,
  2492.                     ** as we can't image the document until it is opened.  DoNewWindow()
  2493.                     ** uses gPrintPage as a flag to open the window off-screen, but
  2494.                     ** visible, so that PrintMonitor can use the title of the window
  2495.                     ** as the document name that is being printed. */
  2496.                 
  2497.                 if ( w ) /* moved getfullpath above -- 07Jan95 e */
  2498.                 {    
  2499.                     w = edit( path, 0L, 0L );
  2500.  
  2501.                     if ( gPrintPage && w >= 0 )
  2502.                     {    pathstr_to_filename( wind_table[w].filename, flnm );
  2503.                         err  = eTePrint( wind_table[w].hTE, (mode == 2), (loop == 1), flnm );
  2504.                         mode = 1;    /* No interaction mode (mode == 2) only valid
  2505.                                     ** for the first printed document. */
  2506.                         wind_close( w );
  2507.                     }
  2508.                 }
  2509.                 else err = errAEEventNotHandled;     /* file pathname too long */
  2510.             }
  2511.             if (gPrintPage)
  2512.             {    eTePrint( nil, false, false, flnm ); /* cleanup */
  2513.                 gPrintPage = 0;                      /* back to normal */
  2514.             }
  2515.         }
  2516.     }
  2517.     err2 = AEDisposeDesc(&theDesc);
  2518.     return(err ? err : err2);
  2519. }
  2520.  
  2521. static Boolean install_aehandler( AEEventClass cl, AEEventID id, ProcPtr p )
  2522. { OSErr    err;
  2523.   AEEventHandlerUPP upp = NewAEEventHandlerProc(p);
  2524.   
  2525.   if ( upp == NULL ) err = memFullErr;
  2526.   else err = AEInstallEventHandler( cl, id, upp, 0L, false );
  2527.   if (err)
  2528.   { io_err(err);
  2529.     return false;
  2530.   }
  2531.   return true;
  2532. }
  2533.  
  2534. static pascal OSErr    DoAEOpenApplication( AppleEvent *message, AppleEvent *reply, long refcon )
  2535. {
  2536.     return(noErr);
  2537. }
  2538.  
  2539. static pascal OSErr    DoAEOpenDocuments( AppleEvent *message, AppleEvent *reply, long refcon )
  2540. {
  2541.     gCursRgnOK = false;
  2542.     return( OpenDocEventHandler( message, reply, 0 ) ); /* 0 means regular open document */
  2543. }
  2544.  
  2545. #define kTimeOutInTicks (60 * 30)    /* 30 second timeout. */
  2546.  
  2547. static pascal OSErr    DoAEPrintDocuments(AppleEvent *message, AppleEvent *reply, long refcon)
  2548. {
  2549.     short                openMode;
  2550.     ProcessSerialNumber    cpsn, fpsn;
  2551.     Boolean                procsSame;
  2552.  
  2553.     gCursRgnOK = false;
  2554.     openMode = 1;
  2555.     if (!AEInteractWithUser(kTimeOutInTicks, nil, nil))
  2556.         ++openMode;
  2557.  
  2558.     GetCurrentProcess(&cpsn);        /* We may have been moved to the front. */
  2559.     GetFrontProcess(&fpsn);
  2560.     SameProcess(&cpsn, &fpsn, &procsSame);
  2561.     gInBackground = !procsSame;
  2562.  
  2563.     return( OpenDocEventHandler( message, reply, openMode ) );
  2564.         /* openMode is either 1 or 2, depending if user interaction is okay. */
  2565. }
  2566.  
  2567. static pascal OSErr    DoAEQuitApplication( AppleEvent *message, AppleEvent *reply, long refcon )
  2568. {
  2569.     gCursRgnOK = false;
  2570.     gDoQuit = 1; // moved here 31Aug95 e
  2571.     wind_quit();
  2572.     return( gDoQuit ? noErr : errAEEventNotHandled );
  2573. }
  2574.  
  2575. /* 1Feb93  e  --  DoScript added */
  2576.  
  2577. static OSErr DoScriptAsFile( FSSpec *theFSS )
  2578. {
  2579.     OSErr err = noErr;
  2580.     short len;
  2581.     char  path[512];
  2582.     
  2583.     if( len = getfullpath( theFSS->vRefNum, theFSS->parID, theFSS->name, path, 511, 1 ) )
  2584.     {
  2585.         put_input( interaction_id, dosc_open_str, dosc_open_str_sz, 0, 0, 1 );
  2586.         put_input( interaction_id, path, len, 0, 0, 0 );
  2587.         put_input( interaction_id, dosc_clos_str, dosc_clos_str_sz, 1, 0, 0 );
  2588.     }
  2589.     else err = errAEEventNotHandled;
  2590.     return err;
  2591. }
  2592.  
  2593. static pascal OSErr DoAEDoScript( AppleEvent *theAEEvent, AppleEvent *theAEReply, long *theRefCon)
  2594. {
  2595.     OSErr        theErr;
  2596.     DescType    typeCode;
  2597.     Size        sizeOfParam, actualSize;
  2598.     char        ourScriptText[256];
  2599.     FSSpec        ourScriptFSpec;
  2600.  
  2601.     /* Get the script to run */ 
  2602.  
  2603.     theErr = AESizeOfParam( theAEEvent, keyDirectObject, &typeCode, &sizeOfParam);
  2604.     if (theErr != noErr)
  2605.     {    /*
  2606.             If we fail here just return the error. We don't need to do any clean up since 
  2607.             we've allocated nothing on the heap yet.  The Apple Event Manager automatically 
  2608.             adds the error number to the reply as keyErrorNumber for non zero handler returns.
  2609.         */
  2610.         return theErr;
  2611.     }
  2612.     else if ((typeCode == typeChar) || (typeCode == typeStyledText) || (typeCode == typeIntlText))
  2613.     {
  2614.         theErr = AEGetParamPtr( theAEEvent, keyDirectObject, typeChar, &typeCode,
  2615.                                  (Ptr)ourScriptText, sizeof(ourScriptText), &actualSize);
  2616.         if (theErr == noErr)
  2617.             if ( MissedAnyParameters( theAEEvent ) )
  2618.                 theErr = errAEEventNotHandled;
  2619. #if 0
  2620.             else put_input( interaction_id, ourScriptText, actualSize, 1, 0, 1 );
  2621. #else
  2622. // 1Sep95 e
  2623.             else
  2624.             { check_TEInsert( ourScriptText, actualSize, wind_table[interaction_id].hTE, 1 );
  2625.               put_input( interaction_id, ourScriptText, actualSize, 1, 0, 1 );
  2626.             }
  2627. #endif
  2628.     }
  2629.     else if (typeCode == typeAlias)
  2630.     {
  2631.         theErr = AEGetParamPtr( theAEEvent, keyDirectObject, typeFSS, &typeCode,
  2632.                                 (Ptr)&ourScriptFSpec, sizeof(ourScriptFSpec), &actualSize);
  2633.         if ( theErr == noErr )
  2634.             if ( MissedAnyParameters( theAEEvent ) )
  2635.                 theErr = errAEEventNotHandled;
  2636.             else theErr = DoScriptAsFile( &ourScriptFSpec );
  2637.     }
  2638.     else theErr = errAEEventNotHandled;
  2639.     return theErr;
  2640. }
  2641.  
  2642. #ifdef PFILmode
  2643.  
  2644. /* 7Jun94  e */
  2645. extern int pfil_ae_eval( char *in_buf, int in_siz, char* ou_buf, int ou_siz );
  2646.  
  2647. static pascal OSErr DoAEDoEval( AppleEvent *theAEEvent, AppleEvent *theAEReply, long *theRefCon)
  2648. {
  2649.     OSErr        theErr;
  2650.     DescType    typeCode;
  2651.     Size        sizeOfParam, actualSize;
  2652.     char        inScriptText[256];
  2653.     char        ouScriptText[256];
  2654.     FSSpec        ourScriptFSpec;
  2655.  
  2656.     /* Get the script to run */ 
  2657.  
  2658.     theErr = AESizeOfParam( theAEEvent, keyDirectObject, &typeCode, &sizeOfParam);
  2659.     if (theErr != noErr)
  2660.     {    return theErr;
  2661.     }
  2662.     else if ((typeCode == typeChar) || (typeCode == typeStyledText) || (typeCode == typeIntlText))
  2663.     {
  2664.         theErr = AEGetParamPtr( theAEEvent, keyDirectObject, typeChar, &typeCode,
  2665.                                  (Ptr)inScriptText, sizeof(inScriptText), &actualSize);
  2666.         if (theErr == noErr)
  2667.             if ( MissedAnyParameters( theAEEvent ) )
  2668.                 theErr = errAEEventNotHandled;
  2669.     }
  2670.     else if (typeCode == typeAlias)
  2671.     {
  2672.         theErr = AEGetParamPtr( theAEEvent, keyDirectObject, typeFSS, &typeCode,
  2673.                                 (Ptr)&ourScriptFSpec, sizeof(ourScriptFSpec), &actualSize);
  2674.         if ( theErr == noErr )
  2675.             if ( MissedAnyParameters( theAEEvent ) )
  2676.                 theErr = errAEEventNotHandled;
  2677.             else theErr = errAEEventNotHandled; /* DoScriptAsFile( &ourScriptFSpec ); */
  2678.     }
  2679.     else theErr = errAEEventNotHandled;
  2680.  
  2681.     if ( theErr == noErr )
  2682.     {
  2683.         actualSize = pfil_ae_eval( inScriptText, actualSize,
  2684.                                    ouScriptText, sizeof(ouScriptText) );
  2685.         theErr = AEPutParamPtr( theAEReply, keyDirectObject, typeChar,
  2686.                                  (Ptr)ouScriptText, actualSize);
  2687.     }
  2688.     return theErr;
  2689. }
  2690.  
  2691. #endif
  2692.  
  2693. static void init_ae()
  2694. {
  2695.     long    result;
  2696.  
  2697.     gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &result) ? false : result != 0);
  2698.  
  2699.     if (gHasAppleEvents)
  2700.          install_aehandler( kCoreEventClass, kAEOpenApplication, (ProcPtr )DoAEOpenApplication )
  2701.       && install_aehandler( kCoreEventClass, kAEOpenDocuments, (ProcPtr )DoAEOpenDocuments )
  2702.       && install_aehandler( kCoreEventClass, kAEPrintDocuments, (ProcPtr )DoAEPrintDocuments )
  2703.       && install_aehandler( kCoreEventClass, kAEQuitApplication, (ProcPtr )DoAEQuitApplication )
  2704.       && install_aehandler( kAEMiscStandards, kAEDoScript, (ProcPtr )DoAEDoScript )
  2705. #ifdef PFILmode
  2706.       && install_aehandler( kAEMiscStandards, (OSType )'eval', (ProcPtr )DoAEDoEval )
  2707. #endif
  2708.     ;
  2709. }
  2710.  
  2711. /*--------------------------------------------------------------------------*/
  2712.  
  2713.