home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1994 November / macformat-018.iso / Utility Spectacular / Developer / macgambit-20-compiler-src-p1 / Runtime (.c & .h) / os_mac.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-26  |  99.4 KB  |  3,680 lines  |  [TEXT/KAHL]

  1. /* os_mac.c, included by os.c
  2. /* (except under ThinkC, where it's in the project and includes os.c) */
  3. /* portions Copyright © 1992 Marc Feeley and Douglas H. Currie, Jr.   */
  4.  
  5. /* Macintosh specific stuff (for THINK C 4.0 and 5.0 compiler). */
  6.  
  7. #ifdef THINK_C
  8. /* stuff from os.c   6Jul92  e  */
  9.  
  10. #include "params.h"
  11. #include "gambit.h"
  12. #include "os.h"
  13. #include "mem.h"
  14. #include "strings.h"
  15. #include "opcodes.h"
  16.  
  17. OS_FILE os_stdin, os_stdout, os_stderr;
  18. long os_M68020 = 1, os_M68881 = 1;  /* Assume M68020/30 processor and M68881 */
  19. char *os_err = "";
  20.  
  21. #endif
  22.  
  23. #include "os_mac.h"
  24. #include "os_mac_Help.h"
  25.  
  26. #if (THINK_C < 5)
  27. /* */
  28. #else
  29. short errno; /* because we are using "math.c" */
  30. #define use_MacTraps2 (1)
  31. #endif
  32.  
  33. #define eTe_SHOW_POSN (0)
  34.  
  35. static void (*intr_action)();
  36. static void (*io_action)();
  37. static void (*timer_action)();
  38. static void (*fatal_action)();
  39.  
  40. /*---------------------------------------------------------------------------*/
  41.  
  42. /* new stuff  29Dec92  e  */
  43.  
  44. #include <GestaltEqu.h>
  45. #include <AppleEvents.h>
  46.  
  47. /* 1Feb93  e */
  48. #ifdef THINK_C
  49. #undef kAERestart
  50. #undef kAEShutDown
  51. #endif
  52. #include "AERegistry.h"
  53.  
  54. static void init_ae();
  55.  
  56. Boolean        gDoQuit = false;
  57. Boolean        gHasAppleEvents = false;
  58. Boolean        gPrintPage = false;
  59. Boolean        spareB1;
  60.  
  61. /*---------------------------------------------------------------------------*/
  62.  
  63. #if (THINK_C < 5)
  64. TextStyle prefStylNormal =
  65. { monaco, 0, FontSize, { 65536, 65536, 65536 } /* RGBColor Black */ };
  66. TextStyle prefStylHilite =
  67. { monaco, 0, FontSize, { 65536, 65536, 65536 } /* RGBColor Black */ };
  68. #else
  69. TextStyle prefStylNormal =
  70. { monaco, 0, 0, FontSize, { 65536, 65536, 65536 } /* RGBColor Black */ };
  71. TextStyle prefStylHilite =
  72. { monaco, 0, 0, FontSize, { 65536, 65536, 65536 } /* RGBColor Black */ };
  73. #endif
  74.  
  75. short prefTabs = dfltTabs;
  76. short prefWrap = dfltWrap;
  77. short prefAutoInd = dfltAutoInd;
  78.  
  79. short current_menus;
  80.  
  81. MenuHandle menus[7];        /* 10Jan93  e */
  82.  
  83. Cursor watch_cursor;
  84. Cursor gc_cursor;
  85. Cursor ibeam_cursor;
  86. Cursor *current_cursor;
  87.  
  88. Str255 find_string, replace_string;
  89.  
  90. Boolean find_ci   = FALSE;
  91. Boolean find_wrap = FALSE;
  92.  
  93. static Boolean smKeyBdP;
  94. static Boolean showPosnP;    /* 10Jan93  e */
  95.  
  96. short interaction_id = 0;
  97. short abnormal_exit = 1;
  98. short spareS1;
  99. short current_volume = 0;
  100.  
  101. static struct {
  102.   short in_use;
  103.   short bold_input;
  104.   WindowPtr wptr;
  105.   ControlHandle vscroll;
  106.   ControlHandle hscroll;
  107.   TeHANDLE hTE;
  108.   short height;
  109.   short dirty;
  110.   short pos, len;
  111.   char *buf;
  112.   Handle out_buf;
  113.   short out_len;
  114.   short is_file;
  115.   unsigned char *filename;
  116. #if (eTe_SHOW_POSN > 0)
  117.   long needs_inval_ticks;            /* 10Jan93  e --  display of caret position */
  118. #endif
  119.   } wind_table[MAX_NB_WINDOWS];
  120.  
  121. static void io_err( short io );
  122.  
  123. DialogPtr loading_dp = NULL;
  124.  
  125. static long OrigKeysID;
  126. static long OrigIconID;
  127.  
  128. static long intr_task_installed = 0;
  129. static long intr_task_interval = 0;
  130.  
  131. #ifdef USE_eTe_debug
  132.  
  133. static struct
  134. { char   *a5;
  135.   VBLTask task;
  136. } et;
  137.  
  138. static void intr_task()
  139. { asm
  140.   { move.l a5,-(sp)
  141.     move.l -4(a0),a5
  142.     
  143.     cmpa.l 0x904,a5
  144.     beq.s @goon
  145.     _Debugger
  146. goon:
  147.   }
  148.   if (intr_task_interval > 0)
  149.   { timer_action( 0L, 0L, 1L );
  150.     et.task.vblCount = intr_task_interval;
  151.   }
  152.   else
  153.     et.task.vblCount = 1;
  154.   asm
  155.   { move.l (sp)+,a5
  156.   }
  157. }
  158.  
  159. static void start_intr_task()
  160. { asm
  161.   { move.l a5,et.a5
  162.   }
  163.   et.task.qType    = vType;
  164.   et.task.vblAddr  = (ProcPtr)intr_task;
  165.   et.task.vblCount = 1;
  166.   et.task.vblPhase = 0;
  167.   intr_task_installed = (VInstall( (QElemPtr )&et.task ) == noErr);
  168. }
  169.  
  170. static void stop_intr_task()
  171. { if (intr_task_installed)
  172.   { VRemove( (QElemPtr )&et.task ); intr_task_installed = 0; }
  173. }
  174.  
  175. #else
  176.  
  177. static VBLTask task;
  178.  
  179. static void intr_task()
  180. { asm
  181.   { move.l a5,-(sp)
  182.     move.l 0x904,a5
  183.   }
  184.   if (intr_task_interval > 0)
  185.   { timer_action( 0L, 0L, 1L );
  186.     task.vblCount = intr_task_interval;
  187.   }
  188.   else
  189.     task.vblCount = 1;
  190.   asm
  191.   { move.l (sp)+,a5
  192.   }
  193. }
  194.  
  195. static void start_intr_task()
  196. { task.qType    = vType;
  197.   task.vblAddr  = (ProcPtr)intr_task;
  198.   task.vblCount = 1;
  199.   task.vblPhase = 0;
  200.   intr_task_installed = (VInstall( (QElemPtr )&task ) == noErr);
  201. }
  202.  
  203. static void stop_intr_task()
  204. { if (intr_task_installed)
  205.   { VRemove( (QElemPtr )&task ); intr_task_installed = 0; }
  206. }
  207.  
  208. #endif
  209.  
  210. RgnHandle gCursorRgn;
  211. Boolean gHasWNE = true;
  212. Boolean gHasHWPriv = true;
  213. Boolean gCursRgnOK = false;
  214. Boolean gInBackground = false;
  215. long gWaitTicksBG = 5; /* in ticks,  should be 1..15 */
  216. long gWaitTicksFG = 1; /* in ticks,  should be 1..15 */
  217. /** Define trap numbers **/
  218. #ifndef _Unimplemented
  219. #define _Unimplemented 0xA89F /* Unimplemented trap */
  220. #endif
  221. #ifndef _WaitNextEvent
  222. #define _WaitNextEvent 0xA860 /* WaitNextEvent trap */
  223. #endif
  224. #ifndef _HWPriv
  225. #define _HWPriv 0xA098 /* HWPriv trap */
  226. #endif
  227. #if (THINK_C < 5)
  228. typedef unsigned char TrapType;
  229. /* extern pascal long NGetTrapAddress(short, TrapType); */
  230. #define osEvt 15
  231. #define mouseMovedMessage 0xFA
  232. #define suspendResumeMessage 0x01
  233. #define resumeFlag 1
  234. #endif
  235.  
  236. TrapType GetTrapType(short theTrap)
  237. { /* OS traps start with A0, Tool with A8 or AA. */
  238.   if ((theTrap & 0x0800) == 0)
  239.     return (OSTrap);
  240.   else
  241.     return (ToolTrap);
  242. }
  243. short NumToolboxTraps(void)
  244. { /* InitGraf (trap $A86E) is always implemented. */
  245.   if (NGetTrapAddress(0xA86E, ToolTrap) == NGetTrapAddress(0xAA6E, ToolTrap))
  246.     return (0x200);
  247.   else
  248.     return (0x400);
  249. }
  250. Boolean TrapExists(short theTrap)
  251. { TrapType theTrapType;
  252.   theTrapType = GetTrapType(theTrap);
  253.   if ((theTrapType == ToolTrap) && ((theTrap &= 0x07FF) >= NumToolboxTraps()))
  254.     theTrap = _Unimplemented;
  255.   return (NGetTrapAddress(_Unimplemented, ToolTrap) != NGetTrapAddress(theTrap, theTrapType));
  256. }
  257.  
  258. static void setup_cursors()
  259. { CursHandle hCurs;
  260.   hCurs = GetCursor(watchCursor);
  261.   watch_cursor = **hCurs;
  262.   hCurs = GetCursor(gc_cursorID);
  263.   gc_cursor = **hCurs;
  264.   hCurs = GetCursor(iBeamCursor);
  265.   ibeam_cursor = **hCurs;
  266.  
  267.   current_cursor = &watch_cursor;
  268. }
  269.  
  270. /* 02Oct92  e  added ParamText1 */
  271.  
  272. static void ParamText1( Str255 text )
  273. {
  274.     ParamText( text, "\p", "\p", "\p" );
  275. }
  276.  
  277. static void c_to_p( c_str, p_str )
  278. char *c_str;
  279. Str255 p_str;
  280. { short i = 0;
  281.   while (c_str[i] != '\0') { p_str[i+1] = c_str[i]; i++; }
  282.   p_str[0] = i;
  283. }
  284.  
  285. static void p_to_p( p1, p2 )
  286. Str255 p1, p2;
  287. { short len = (unsigned char)*p1++;
  288.   *p2++ = len;
  289.   while (len>0) { *p2++ = *p1++; len--; }
  290. }
  291.  
  292. static void buf_to_p( unsigned short len, char *p1, Str255 p2 )
  293. { if( len > 255 ) len = 255;
  294.   *p2++ = len;
  295.   while (len>0) { *p2++ = *p1++; len--; }
  296. }
  297.  
  298. static void p_to_c( p_str, c_str )
  299. Str255 p_str;
  300. char *c_str;
  301. { short len = (unsigned char)*p_str++, i = 0;
  302.   while (i<len) { *c_str++ = *p_str++; i++; }
  303.   *c_str++ = '\0';
  304. }
  305.  
  306. static short compare_p_to_p( p1, p2 )
  307. Str255 p1, p2;
  308. { short len1 = (unsigned char)*p1++, len2 = (unsigned char)*p2++;
  309.   if (len1 < len2) return -1;
  310.   if (len1 > len2) return 1;
  311.   while (len1>0)
  312.   { short c1 = (unsigned char)*p1++, c2 = (unsigned char)*p2++;
  313.     if (c1 < c2) return -1;
  314.     if (c1 > c2) return 1;
  315.     len1--;
  316.   }
  317.   return 0;
  318. }
  319.  
  320. static void pathstr_to_filename( register Str255 p_str, Str255 f_str )
  321. {    register short plen = p_str[0];
  322.     while( plen && p_str[plen] != ':' ) plen--;
  323.     buf_to_p( p_str[0] - plen, (char *)&p_str[plen+1], f_str );
  324. }
  325.  
  326. /* *** preferences saved to disk *** */
  327.  
  328. static void doKeyBdMode()
  329. {     short temp;
  330.     CheckItem( menus[editM], smKeyBdCommand, smKeyBdP );
  331.     /* switches back and forth turning dead keys off and on */
  332.     temp = SetScript( smRoman, smScriptKeys, smKeyBdP ? LiveKeysID : OrigKeysID );
  333.     if( temp != noErr ) { io_err( temp ); return; }
  334.     temp = SetScript( smRoman, smScriptIcon, smKeyBdP ? LiveKeysID : OrigIconID );
  335.     if( temp != noErr ) { io_err( temp ); return; }
  336.     KeyScript( smRoman );
  337. }
  338.  
  339. #ifdef use_MacTraps2
  340.  
  341. static OSErr findPrefFolder( short *foundVRefNum, long *foundDirID )
  342. {    
  343.   *foundVRefNum = 0;
  344.   *foundDirID = 0;
  345.   /* MacTraps2 has FindFolder() glue */
  346.   return FindFolder( kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  347.                        foundVRefNum, foundDirID );
  348. }
  349.  
  350. #else
  351.  
  352. #include <GestaltEqu.h>
  353.  
  354. #define BTstQ(arg, bitnbr)        (arg & (1 << bitnbr))
  355.  
  356. static OSErr findPrefFolder( short *foundVRefNum, long *foundDirID )
  357. {
  358.   long          gesResponse;
  359.   SysEnvRec     envRec;
  360.   WDPBRec       myWDPB;
  361.   unsigned char volName[34];
  362.   OSErr         err;
  363.     
  364.   *foundVRefNum = 0;
  365.   *foundDirID = 0;
  366.   if ( !Gestalt( gestaltFindFolderAttr, &gesResponse ) 
  367.           && BTstQ( gesResponse, gestaltFindFolderPresent ) )
  368.   { /* Folder Manager exists */
  369.     err = FindFolder( kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  370.                        foundVRefNum, foundDirID );
  371.   }
  372.   else
  373.   { /* Gestalt can't give us the answer, so we resort to SysEnvirons */
  374.     if ( !(err = SysEnvirons( curSysEnvVers, &envRec )) )
  375.     {  myWDPB.ioVRefNum = envRec.sysVRefNum;
  376.        volName[0] = '\000';                   /* Zero volume name */
  377.        myWDPB.ioNamePtr = volName;
  378.        myWDPB.ioWDIndex = 0;
  379.        myWDPB.ioWDProcID = 0;
  380.        if ( !(err = PBGetWDInfo( &myWDPB, 0 )) )
  381.        { *foundVRefNum = myWDPB.ioWDVRefNum;
  382.          *foundDirID = myWDPB.ioWDDirID;
  383.        }
  384.     }
  385.   }
  386.   return (err);
  387. }
  388.  
  389. #endif
  390.  
  391. struct prefs
  392. { short version;
  393.   short tabs;
  394.   short wrap;
  395.   Boolean find_ci;
  396.   Boolean find_wrap;
  397.   Boolean smKeyBdP;        /* 13Aug92  e  */
  398.   Boolean showPosnP;    /* 10Jan93  e  */
  399.   short spare2;
  400.   short spare3;
  401.   short autoInd;
  402.   TextStyle stylNormal;
  403.   TextStyle stylHilite;
  404.   unsigned char fontnmNormal[64];
  405.   unsigned char fontnmHilite[64];
  406. };
  407.  
  408. void savePrefs( ConstStr255Param fn )
  409. {
  410.     short rRef;
  411.       OSErr err;
  412.     struct prefs **hRes;
  413.     struct prefs  *pRes;
  414.     short foundVRefNum;
  415.     long  foundDirID;
  416.     Str255 fontnmNormal, fontnmHilite;
  417.     
  418.     err = findPrefFolder( &foundVRefNum, &foundDirID );
  419.     if( err != noErr ) return;
  420.     
  421.     if( ( rRef = HOpenResFile( foundVRefNum, foundDirID, fn, fsRdWrPerm ) ) < 0)
  422.     {    HCreateResFile( foundVRefNum, foundDirID, fn );
  423.         { HFileInfo pb;
  424.           pb.ioCompletion = (void *)0;
  425.           pb.ioNamePtr = (StringPtr )fn;
  426.           pb.ioVRefNum = foundVRefNum;
  427.           pb.ioFDirIndex = 0;
  428.           pb.ioDirID = foundDirID;
  429.           err = PBHGetFInfo( &pb, FALSE );
  430.           pb.ioCompletion = (void *)0;
  431.           pb.ioNamePtr = (StringPtr )fn;
  432.           pb.ioVRefNum = foundVRefNum;
  433.           pb.ioFDirIndex = 0;
  434.           pb.ioDirID = foundDirID;
  435.           pb.ioFlFndrInfo.fdCreator = 'gamI';
  436.           pb.ioFlFndrInfo.fdType    = 'gamP';
  437.           if( err == noErr )
  438.             PBHSetFInfo( &pb, FALSE );
  439.         }
  440.         rRef = HOpenResFile( foundVRefNum, foundDirID, fn, fsRdWrPerm );
  441.     }
  442.     if( rRef < 0 )
  443.         return;
  444.     hRes = (struct prefs **)GetResource('ePrf', 357);
  445.     if(    hRes == 0 || rRef != HomeResFile( (Handle )hRes ) )
  446.     {    hRes = (struct prefs **)NewHandle( sizeof( struct prefs ) );
  447.         if( hRes == 0 )
  448.         {    CloseResFile( rRef );
  449.             return;
  450.         }
  451.         AddResource( (Handle )hRes, (ResType )'ePrf', 357, (ConstStr255Param)"\p" );
  452.         if( ResErr != noErr )
  453.         {    DisposHandle( (Handle )hRes );
  454.             CloseResFile( rRef );
  455.             return;
  456.         }
  457.     }
  458.     pRes = *hRes;
  459.     (*pRes).version    = 1;
  460.     (*pRes).tabs       = prefTabs;
  461.     (*pRes).wrap       = prefWrap;
  462.     (*pRes).find_ci    = find_ci;
  463.     (*pRes).find_wrap  = find_wrap;
  464.     (*pRes).smKeyBdP   = smKeyBdP;        /* 13Aug92  e  */
  465.     (*pRes).showPosnP  = showPosnP;        /* 10Jan93  e  */
  466.     (*pRes).spare2     = 0;
  467.     (*pRes).spare3     = 0;
  468.     (*pRes).autoInd    = prefAutoInd;
  469.     (*pRes).stylNormal = prefStylNormal;
  470.     (*pRes).stylHilite = prefStylHilite;
  471.     GetFontName( prefStylNormal.tsFont, fontnmNormal );
  472.     if( fontnmNormal[0] > 63 ) fontnmNormal[0] = 63;
  473.     p_to_p( fontnmNormal, (**hRes).fontnmNormal );
  474.     GetFontName( prefStylHilite.tsFont, fontnmHilite );
  475.     if( fontnmHilite[0] > 63 ) fontnmHilite[0] = 63;
  476.     p_to_p( fontnmHilite, (**hRes).fontnmHilite );
  477.     
  478.     ChangedResource( (Handle )hRes );
  479.     CloseResFile( rRef );
  480. }
  481.  
  482. static void readPrefs( ConstStr255Param fn )
  483. {
  484.     short rRef;
  485.       OSErr err;
  486.     struct prefs **hRes;
  487.     struct prefs  *pRes;
  488.     short foundVRefNum;
  489.     long  foundDirID;
  490.     short    fontNumber;
  491.  
  492.     err = findPrefFolder( &foundVRefNum, &foundDirID );
  493.     if( err != noErr ) return;
  494.     
  495.     rRef = HOpenResFile( foundVRefNum, foundDirID, fn, fsRdWrPerm );
  496.     /* don't test rRef here since resource may come from elsewhere, e.g., from appl. */
  497.     hRes = (struct prefs **)GetResource('ePrf', 357);
  498.     if(    hRes != 0 )
  499.     {    pRes = *hRes;
  500.         if( (*pRes).version == 1 )
  501.         {    prefTabs    = (*pRes).tabs;
  502.             prefWrap    = (*pRes).wrap;
  503.             find_ci     = (*pRes).find_ci;
  504.             find_wrap   = (*pRes).find_wrap;
  505.             prefAutoInd = (*pRes).autoInd;
  506.             smKeyBdP    = (*pRes).smKeyBdP; /* 13Aug92  e  */
  507.             showPosnP  = (*pRes).showPosnP;    /* 10Jan93  e  */
  508.             doKeyBdMode();                  /* 13Aug92  e  */
  509.             prefStylNormal = (*pRes).stylNormal;
  510.             prefStylHilite = (*pRes).stylHilite;
  511.             GetFNum( (**hRes).fontnmNormal, &prefStylNormal.tsFont );
  512.             if( prefStylNormal.tsFont < 0 ) prefStylNormal.tsFont = 0;
  513.             GetFNum( (**hRes).fontnmHilite, &prefStylHilite.tsFont );
  514.             if( prefStylHilite.tsFont < 0 ) prefStylHilite.tsFont = 0;
  515.         }
  516.     }
  517.     if( rRef >= 0 ) CloseResFile( rRef );
  518. }
  519.  
  520. /* *** */
  521.  
  522. #define fustItem 3
  523. static short itemtowindid[MAX_NB_WINDOWS];
  524. static short windidtodigit[MAX_NB_WINDOWS];
  525. static short qWindItems = 0;
  526.  
  527. typedef struct { short nbmenus6, right, dummy; struct { MenuHandle mh; short left; } menu[100]; } menu_list;
  528.  
  529. static short cmd_key_exists( c )
  530. char c;
  531. {
  532.   menu_list **ml = (menu_list **)MenuList;
  533.   short n = (*ml)->nbmenus6/6;
  534.   short i, j, m, cmd;
  535.   for (i=0; i<n; i++)
  536.   { MenuHandle mh = (*ml)->menu[i].mh;
  537.     m = CountMItems( mh );
  538.     for (j=1; j <= m; j++)
  539.     {
  540.       GetItemCmd( mh, j, &cmd );
  541.       if (cmd == c) return 1;
  542.     }
  543.   }
  544.   return 0;
  545. }
  546.  
  547. static void addWindMenuItem( short id )
  548. {    short d, i, j;
  549.     Str255 title, key = "\p / ";
  550.     WindowPtr wind = wind_table[id].wptr;
  551.     if (qWindItems >= MAX_NB_WINDOWS) return;
  552.     for (d=1; d<10; d++) if (!cmd_key_exists('0'+d)) break;
  553.     if (d < 10) key[3] = '0'+d; else key[2] = ' ';
  554.     windidtodigit[id] = d;
  555.     for (i=0; i < qWindItems; i++)
  556.       if (windidtodigit[itemtowindid[i]] > d) break;
  557.     for (j=qWindItems-1; j > i; j--)
  558.       itemtowindid[j] = itemtowindid[j-1];
  559.     itemtowindid[i] = id;
  560.     GetWTitle( wind, title );
  561.     InsMenuItem( menus[windowsM], key, i+fustItem-1 );
  562.     SetItem( menus[windowsM], i+fustItem, title );
  563.     qWindItems++;
  564. }
  565.  
  566. static void delWindMenuItem( short id )
  567. {    register short i;
  568.     for(i = 0; i < qWindItems; i++)
  569.     {    if( id == itemtowindid[i] )
  570.         {    DelMenuItem( menus[windowsM], i+fustItem );
  571.             for( i++; i < qWindItems; i++ )
  572.                 itemtowindid[i-1] = itemtowindid[i];
  573.             qWindItems--;
  574.             break;
  575.         }
  576.     }
  577. }
  578.  
  579. void eSmudgeWindow( register short w )
  580. {    register short i;
  581.     if( ! wind_table[w].dirty )
  582.     {    wind_table[w].dirty = 1;
  583.         for( i = 0; i < qWindItems; i++ )
  584.         {    if( w == itemtowindid[i] )
  585.             {    SetItemMark( menus[windowsM], i+fustItem, '◊');
  586.                 break;
  587.             }
  588.         }
  589.     }
  590. }
  591.  
  592. void eUnSmudgeWindow( register short w, TeHANDLE hTE )
  593. {    register short i;
  594.     (**hTE).dirty = 0;
  595.     if( wind_table[w].dirty )
  596.     {    wind_table[w].dirty = 0;
  597.         for( i = 0; i < qWindItems; i++ )
  598.         {    if( w == itemtowindid[i] )
  599.             {    SetItemMark( menus[windowsM], i+fustItem, 0);
  600.                 break;
  601.             }
  602.         }
  603.     }
  604. }
  605.  
  606. void eMaybeSmudgeWindow( short w, TeHANDLE hTE )
  607. {
  608.     if( (**hTE).dirty )
  609.         eSmudgeWindow( w );
  610.     else
  611.         eUnSmudgeWindow( w, hTE );    /* for Undo  14Aug92  e  */
  612. }
  613.  
  614. void select_and_show( wptr )
  615. WindowPtr wptr;
  616. {
  617.   SelectWindow( wptr );
  618.   ShowWindow( wptr );
  619. }
  620.  
  621. /* ************* */
  622.  
  623. /* 10Jan93  e  -- separated out Search menu */
  624.  
  625. static void wind_begin()
  626. { short i;
  627.   for (i=0; i<MAX_NB_WINDOWS; i++) wind_table[i].in_use = 0;
  628.  
  629.   prefStylHilite.tsFace = bold+condense;
  630.  
  631.   menus[appleM] = GetMenu( appleID );
  632.   AddResMenu( menus[appleM], 'DRVR' );
  633.   if (help_error) DisableItem( menus[appleM], 2 );
  634.  
  635.   menus[fileM]    = GetMenu( fileID );
  636.   menus[editM]    = GetMenu( editID );
  637.   menus[findM]    = GetMenu( findID );
  638.   menus[commandM] = GetMenu( commandID );
  639.   menus[windowsM] = GetMenu( windowsID );
  640.  
  641.   ClearMenuBar();
  642.   for ( (i=appleM); (i<=windowsM); i++ ) InsertMenu(menus[i], 0);
  643.   DrawMenuBar();
  644.   current_menus = 0;
  645.  
  646.   setup_cursors();
  647.  
  648.   find_string[0] = '\0';
  649.   replace_string[0] = '\0';
  650.  
  651.   gCursorRgn = NewRgn();
  652.   SetRectRgn(gCursorRgn, -32768, -32768, 32766, 32766);
  653.   gHasWNE = TrapExists(_WaitNextEvent);
  654.  
  655.   gHasHWPriv = TrapExists(_HWPriv);
  656.  
  657.   /* switches back and forth turning dead keys off and on */
  658.   OrigKeysID = GetScript( smRoman, smScriptKeys );
  659.   OrigIconID = GetScript( smRoman, smScriptIcon );
  660.   
  661.   readPrefs( PREFS_FILENAME );
  662.  
  663.   init_ae();    /* 29Dec92  e  */
  664. }
  665.  
  666. static void wind_close( id )
  667. short id;
  668. { TeHANDLE hTE = wind_table[id].hTE;
  669.   if (id == interaction_id)
  670.   { HideWindow( wind_table[id].wptr );
  671.     eTeSetSelect( hTE, 0, eTeTextLength( hTE ));
  672.     eTeDelete( hTE );
  673.   }
  674.   else if (wind_table[id].in_use)
  675.   { wind_table[id].in_use = 0;
  676.     HideWindow( wind_table[id].wptr );
  677.     eTeDispose( hTE );
  678.     delWindMenuItem( id );
  679.     DisposeControl( wind_table[id].vscroll );
  680.     DisposeControl( wind_table[id].hscroll );
  681.     DisposeWindow( wind_table[id].wptr );
  682.     if (wind_table[id].buf != NULL)
  683.     { DisposPtr( wind_table[id].buf ); wind_table[id].buf = NULL; }
  684.     if( wind_table[id].out_buf != NULL )
  685.     { DisposHandle( wind_table[id].out_buf ); wind_table[id].out_buf = NULL; }
  686.     if( wind_table[id].filename != NULL )
  687.     { DisposPtr( wind_table[id].filename ); wind_table[id].filename = NULL; }
  688.   }
  689. }
  690.  
  691. static void wind_end()
  692. { short id;
  693.   for (id=MAX_NB_WINDOWS-1; id>=0; id--) wind_close( id );
  694.   /* switches back to original KMAP */
  695.   id = SetScript( smRoman, smScriptKeys, OrigKeysID );
  696.   if( id != noErr ) { io_err( id ); return; }
  697.   id = SetScript( smRoman, smScriptIcon, OrigIconID );
  698.   if( id != noErr ) { io_err( id ); return; }
  699.   KeyScript( smRoman );
  700.   eTePutScrap();    /* 27Jan93  e */
  701. }
  702.  
  703. static pascal click_scroll();
  704.  
  705. static short wind_open( name, visible, goaway, bold_input )
  706. char *name;
  707. short visible, goaway, bold_input;
  708. { Rect viewRect, vScrollRect, bounds;
  709.   short width = 81 /*(screenBits.bounds.right-SBarWidth-2*Border-11)/FontW*/;
  710.   short height;
  711.   short id;
  712.   Str255 wname;
  713.  
  714.   for (id=0; id<MAX_NB_WINDOWS; id++) if (!wind_table[id].in_use) break;
  715.   if (id == MAX_NB_WINDOWS) return -1;
  716.  
  717.   height = (screenBits.bounds.bottom-Border-48-SBarWidth)/FontH - id;
  718.  
  719.   bounds.left   = 2 + id*5;
  720.   bounds.right  = bounds.left + width*FontW + SBarWidth + Border;
  721.   bounds.top    = 41 + id*(FontH+3);
  722.   bounds.bottom = bounds.top + height*FontH + SBarWidth + Border;
  723.  
  724.   c_to_p( name, wname );
  725.  
  726.   wind_table[id].wptr = (WindowPtr )
  727.     NewWindow( NULL, &bounds, wname, visible, documentProc, (WindowPtr)-1L, goaway, 0L );
  728.   if (wind_table[id].wptr == NULL) return -1;
  729.  
  730.   SetPort( wind_table[id].wptr );
  731.   TextFont( prefStylNormal.tsFont );
  732.   TextSize( prefStylNormal.tsSize );
  733.  
  734.   vScrollRect = (*wind_table[id].wptr).portRect;
  735.   vScrollRect.left = vScrollRect.right-SBarWidth;
  736.   vScrollRect.right += 1;
  737.   vScrollRect.bottom -= SBarWidth-1;
  738.   vScrollRect.top -= 1;
  739.  
  740.   wind_table[id].vscroll =
  741.     NewControl( wind_table[id].wptr, &vScrollRect, "\p", 1, 0, 0, 0,
  742.                 scrollBarProc, 0L);
  743.  
  744.   if (wind_table[id].vscroll != NULL)
  745.   { static FontInfo fInfo;
  746.  
  747.     vScrollRect = (*wind_table[id].wptr).portRect;
  748.     vScrollRect.top = vScrollRect.bottom-SBarWidth;
  749.     vScrollRect.bottom += 1;
  750.     vScrollRect.left += (vScrollRect.right-vScrollRect.left)>>1;
  751.     vScrollRect.right -= SBarWidth-1;
  752.     wind_table[id].hscroll =
  753.       NewControl( wind_table[id].wptr, &vScrollRect, "\p", 1, 0, 0, 0,
  754.                 scrollBarProc, 0L);
  755.     if (wind_table[id].hscroll != NULL)
  756.     {
  757.  
  758.       viewRect = (*wind_table[id].wptr).portRect;
  759.       viewRect.top    += Border;
  760.       viewRect.left   += Border;
  761.       viewRect.bottom -= SBarWidth;
  762.       viewRect.right  -= SBarWidth;
  763.  
  764.       wind_table[id].hTE = eTeNew( wind_table[id].wptr, viewRect, prefTabs, prefWrap,
  765.                                       prefAutoInd, wind_table[id].hscroll, wind_table[id].vscroll );
  766.  
  767.       if (wind_table[id].hTE != NULL)
  768.       {
  769.         eTeSetStyles( wind_table[id].hTE, &prefStylNormal, &prefStylHilite );
  770.         wind_table[id].height     = height;
  771.         wind_table[id].bold_input = bold_input;
  772.         wind_table[id].dirty      = 0;
  773.         wind_table[id].pos        = 0;
  774.         wind_table[id].len        = 0;
  775.         wind_table[id].buf        = NULL;
  776.         wind_table[id].out_buf    = NULL;
  777.         wind_table[id].out_len    = 0;
  778.         wind_table[id].is_file    = 0;
  779.         wind_table[id].filename   = (unsigned char *)NewPtr( FILENAME_LEN );
  780.         if( wind_table[id].filename != NULL )
  781.         { wind_table[id].filename[0] = '\0';
  782.           wind_table[id].in_use   = 1;
  783.           addWindMenuItem( id );
  784.           return id;
  785.         }
  786.         eTeDispose( wind_table[id].hTE );
  787.       }
  788.       DisposeControl( wind_table[id].hscroll );
  789.     }
  790.     DisposeControl( wind_table[id].vscroll );
  791.   }
  792.   DisposeWindow( wind_table[id].wptr );
  793.   return -1;
  794. }
  795.  
  796. static short discard_changes( id )
  797. short id;
  798. { Str255 title;
  799.   GetWTitle( wind_table[id].wptr, title );
  800.   ParamText( "\pDiscard changes to \"", title, "\p\"?", "\p" );
  801.   SysBeep( 10 );
  802.   switch (CautionAlert( ok_cancel_alertID, 0L ))
  803.   { case 3: return 1;
  804.     default: return 0;
  805.   }
  806. }
  807.  
  808. static short mem_full = 0;
  809. static short supress_mem_full_dialog = 0;        /* 27Jan93  e  */
  810.  
  811. pascal long mem_full_err( size )
  812. long size;
  813. { if (mem_full)
  814.   { ParamText1( "\pOut of memory.  The application will exit." );
  815.     SysBeep( 10 );
  816.     StopAlert( ok_alertID, 0L );
  817.     ExitToShell();
  818.   }
  819.   mem_full = 1;
  820.   if ( ! supress_mem_full_dialog )            /* 27Jan93  e  */
  821.   { ParamText1( "\pOut of memory." );
  822.     SysBeep( 10 );
  823.     StopAlert( ok_alertID, 0L );
  824.   }
  825.   return 0;
  826. }
  827.  
  828. static void already_open_err( name )
  829. Str255 name;
  830. { ParamText( "\p\"", name, "\p\"", "\pis already open.  Close it first." );
  831.   SysBeep( 10 );
  832.   StopAlert( ok_alertID, 0L );
  833. }
  834.  
  835. static void path_too_long_err( name )
  836. Str255 name;
  837. { ParamText( "\pPath to file \"", name, "\p\" is too long.", "\p" );
  838.   SysBeep( 10 );
  839.   StopAlert( ok_alertID, 0L );
  840. }
  841.  
  842. static void wind_err()
  843. { ParamText1( "\pCan't open window" );
  844.   SysBeep( 10 );
  845.   StopAlert( ok_alertID, 0L );
  846. }
  847.  
  848. /* 02Oct92  e  added Printer Not Found check, error number displayed for other errors */
  849.  
  850. static void io_err( short io )
  851. { unsigned char errnostr[16];
  852.   switch (io)
  853.   { case wrPermErr    : ParamText1( "\pCan't write file!" ); break;
  854.     case dupFNErr     : ParamText1( "\pDuplicate file name!" ); break;
  855.     case fBsyErr      : ParamText1( "\pFile is busy!" ); break;
  856.     case vLckdErr     : ParamText1( "\pVolume is locked!" ); break;
  857.     case fLckdErr     : ParamText1( "\pFile is locked!" ); break;
  858.     case fnfErr       : ParamText1( "\pFile not found!" ); break;
  859.     case bdNamErr     : ParamText1( "\pBad filename!" ); break;
  860.     case ioErr        : ParamText1( "\pIO transfer error!" ); break;
  861.     case dskFulErr    : ParamText1( "\pDisk full!" ); break;
  862.     case dirFulErr    : ParamText1( "\pDirectory full!" ); break;
  863.     case mFulErr      : ParamText1( "\pFile is too large for memory!" ); break;
  864.                         
  865.     case -4101          : ParamText1( "\pNo printer selected. Use the Chooser." ); break;
  866.     default           : NumToString( io, errnostr );
  867.                         ParamText( "\pIO Error! Mac error number: ", errnostr, "\p", "\p" );
  868.                         break;
  869.   }
  870.   SysBeep( 10 );
  871.   StopAlert( ok_alertID, 0L );
  872. }
  873.  
  874. static void te_limit_err()
  875. { ParamText1( "\pFile unopened due to size limit on buffer (32000 lines)." );
  876.   SysBeep( 10 );
  877.   StopAlert( ok_alertID, 0L );
  878. }
  879.  
  880. static short check_TEWrite( ptr, len, hTE, bold )
  881. char *ptr;
  882. long len;
  883. eTeHandle hTE;
  884. short bold;
  885. {    eTeWrite( hTE, ptr, len, bold );
  886.     if (mem_full) { mem_full = 0; return 1; }
  887.     return 0;
  888. }
  889.  
  890. /* 23Jul92  e  exported for eKeys */
  891. short check_TEInsert( ptr, len, hTE, bold )
  892. char *ptr;
  893. long len;
  894. eTeHandle hTE;
  895. short bold;
  896. {    eTeInsert( hTE, ptr, len, bold );
  897.     if (mem_full) { mem_full = 0; return 1; }
  898.     return 0;
  899. }
  900.  
  901. static short check_TECut( hTE )
  902. eTeHandle hTE;
  903. { eTeCut( hTE );
  904.   if (mem_full) { mem_full = 0; return 1; }
  905.   return 0;
  906. }
  907.  
  908. static short check_TECopy( hTE )
  909. eTeHandle hTE;
  910. { eTeCopy( hTE );
  911.   if (mem_full) { mem_full = 0; return 1; }
  912.   return 0;
  913. }
  914.  
  915. static short check_TEPaste( hTE, bold )
  916. eTeHandle hTE;
  917. short bold;
  918. {     eTePaste( hTE, bold );
  919.     if (mem_full) { mem_full = 0; return 1; }
  920.     return 0;
  921. }
  922.  
  923. static short check_TEKey( c, hTE, bold, event )
  924. char c;
  925. eTeHandle hTE;
  926. short bold;
  927. EventRecord *event;
  928. {    eTeKey( hTE, c, (event->message & keyCodeMask) >> 8, event->modifiers, bold );
  929.     if (mem_full) { mem_full = 0; return 1; }
  930.     return 0;
  931. }
  932.  
  933. short getfullpath( vRefNum, dirID, fName, Path, MaxLength, warn )
  934. short vRefNum;
  935. long dirID;
  936. Str255 fName;
  937. char *Path;
  938. short MaxLength;
  939. short warn;
  940. {
  941.   char *p, *q;
  942.   long len;
  943.   CInfoPBRec block;
  944.   Str255 directoryName;
  945.  
  946.   p = &Path[MaxLength];
  947.   *--p = '\0';
  948.  
  949.   len = (unsigned char)fName[0];
  950.   p -= len;
  951.   if (p < Path) goto too_long;
  952.   BlockMove( &fName[1], p, len );
  953.  
  954.   block.dirInfo.ioNamePtr = directoryName;
  955.   block.dirInfo.ioDrParID = dirID;
  956.  
  957.   do
  958.   { block.dirInfo.ioVRefNum = vRefNum;
  959.     block.dirInfo.ioFDirIndex = -1;
  960.     block.dirInfo.ioDrDirID = block.dirInfo.ioDrParID;
  961.  
  962.     if (PBGetCatInfo( &block, FALSE ) != noErr) return 0;
  963.     *--p = ':';
  964.     len = (unsigned char)directoryName[0];
  965.     p -= len;
  966.     if (p < Path) goto too_long;
  967.     BlockMove( &directoryName[1], p, len );
  968.   } while ( block.dirInfo.ioDrDirID != fsRtDirID );
  969.  
  970.   q = Path;
  971.   while (*p != '\0') *q++ = *p++;
  972.   *q = '\0';
  973.  
  974.   return (short )( q - Path );    /* 2Feb93  e */
  975.  
  976.   too_long:
  977.   if (warn) path_too_long_err( fName );
  978.   return 0;
  979. }
  980.  
  981. /* 3Jul92  e  so I could get rid of MacTraps2 */
  982. #ifdef use_MacTraps2
  983. short getfullpathfromwd( wdRefNum, fName, Path, MaxLength, warn )
  984. long wdRefNum;
  985. Str255 fName;
  986. char *Path;
  987. short MaxLength;
  988. short warn;
  989. {
  990.   short vRefNum;
  991.   long dirID, procID;
  992.  
  993.   if (GetWDInfo( wdRefNum, &vRefNum, &dirID, &procID ) != noErr) return 0;
  994.  
  995.   return getfullpath( vRefNum, dirID, fName, Path, MaxLength, warn );
  996. }
  997. #else
  998. /* pascal OSErr GetWDInfo(short wdRefNum,short *vRefNum,long *dirID,long *procID) */
  999. short getfullpathfromwd( wdRefNum, fName, Path, MaxLength, warn )
  1000. long wdRefNum;
  1001. Str255 fName;
  1002. char *Path;
  1003. short MaxLength;
  1004. short warn;
  1005. {
  1006.   WDPBRec myBlock;
  1007.  
  1008.   myBlock.ioNamePtr = nil;
  1009.   myBlock.ioVRefNum = wdRefNum;
  1010.   myBlock.ioWDIndex = 0;
  1011.   myBlock.ioWDProcID = 0;
  1012.   PBGetWDInfo(&myBlock,false);
  1013.  
  1014.   if ( myBlock.ioResult != noErr ) return 0;
  1015.  
  1016.   return getfullpath( myBlock.ioWDVRefNum, myBlock.ioWDDirID, fName, Path, MaxLength, warn );
  1017. }
  1018. #endif
  1019.  
  1020. short getfullpathfromcurrentvolume( fName, Path, MaxLength, warn )
  1021. Str255 fName;
  1022. char *Path;
  1023. short MaxLength;
  1024. short warn;
  1025. { return getfullpathfromwd( (long)current_volume, fName, Path, MaxLength, warn );
  1026. }
  1027.  
  1028. static Point SFGwhere = { 90, 82 };
  1029. static Point SFPwhere = { 106, 104 };
  1030.  
  1031. short get_file( prompt, nbtypes, ftypes, path, maxlength )
  1032. Str255 prompt;
  1033. short nbtypes;
  1034. char *ftypes;
  1035. char *path;
  1036. short maxlength;
  1037. {
  1038.   SFReply reply;
  1039.  
  1040.   if (nbtypes == 0) nbtypes = -1;
  1041.  
  1042.   eTePutScrap();    /* 10Jan93  e */
  1043.   SFGetFile( SFGwhere, prompt, 0L, nbtypes, (SFTypeList *)ftypes, 0L, &reply );
  1044.   eTeGetScrap();    /* 10Jan93  e */
  1045.   if (!reply.good) return 0;
  1046.  
  1047.   current_volume = reply.vRefNum;
  1048.   return getfullpathfromwd( (long)current_volume, (StringPtr)reply.fName, path, maxlength, 1 );
  1049. }
  1050.  
  1051. short put_file( prompt, deflt, path, maxlength )
  1052. Str255 prompt;
  1053. Str255 deflt;
  1054. char *path;
  1055. short maxlength;
  1056. {
  1057.   SFReply reply;
  1058.  
  1059.   eTePutScrap();    /* 10Jan93  e */
  1060.   SFPutFile( SFPwhere, prompt, deflt, 0L, &reply );
  1061.   eTeGetScrap();    /* 10Jan93  e */
  1062.   if (!reply.good) return 0;
  1063.  
  1064.   current_volume = reply.vRefNum;
  1065.   return getfullpathfromwd( (long)current_volume, (StringPtr)reply.fName, path, maxlength, 1 );
  1066. }
  1067.  
  1068. static short wptr_to_id( w )
  1069. WindowPtr w;
  1070. { short id;
  1071.   for (id=0; id<MAX_NB_WINDOWS; id++)
  1072.     if ((wind_table[id].in_use) && (wind_table[id].wptr == (WindowPtr )w)) return id;
  1073.   return -1;
  1074. }
  1075.  
  1076. static void setup_view( id )
  1077. short id;
  1078. { WindowPtr w = wind_table[id].wptr;
  1079.   TeHANDLE hTE = wind_table[id].hTE;
  1080.   Rect view = w->portRect;
  1081.   /*
  1082.   view.right -= SBarWidth;
  1083.   view.bottom -= SBarWidth;
  1084.   InsetRect(&view, Border, Border);                /*  21Jul92  e  */
  1085.   view.top    += Border;
  1086.   view.left   += Border;
  1087.   view.bottom -= SBarWidth;
  1088.   view.right  -= SBarWidth;
  1089.   eTeNewView( hTE, &view );
  1090. }
  1091.  
  1092. /* 10Jan93  e -- added display of caret position */
  1093. #if (eTe_SHOW_POSN == 0)
  1094. #define inval_msgrect(w)
  1095. static void content( short id, EventRecord *event )
  1096. { WindowPtr w = wind_table[id].wptr;
  1097.   eTeHandle hTE = wind_table[id].hTE;
  1098.   short cntlCode;
  1099.   ControlHandle theControl;
  1100.  
  1101.   SetPort( w );
  1102.   ClipRect( &w->portRect );
  1103.   GlobalToLocal( &event->where );
  1104.   if ((cntlCode = FindControl( event->where, w, &theControl )) == 0 )
  1105.   { if (PtInRect( event->where, &(**hTE).viewRect ))
  1106.       eTeClick( hTE, event->where, event->modifiers, event->when );
  1107.   }
  1108.   else if (cntlCode == inThumb)
  1109.   { cntlCode = GetCtlValue( theControl );
  1110.     TrackControl( theControl, event->where, 0L );
  1111.     if ( ( cntlCode = GetCtlValue( theControl ) - cntlCode ) != 0 )
  1112.         if( (**theControl).contrlRect.left < (**theControl).contrlRect.top ) /* horizontal? */
  1113.             eTeScroll( hTE, cntlCode, 0, TRUE );
  1114.         else
  1115.             eTeScroll( hTE, 0, cntlCode, TRUE );
  1116.   }
  1117.   else
  1118.     TrackControl( theControl, event->where, (ProcPtr )-1L );
  1119. }
  1120. #else
  1121. static void really_inval_msgrect( short w )
  1122. { GrafPtr port = wind_table[w].wptr;
  1123.   Rect r = port->portRect;
  1124.   r.right -= (( r.right - r.left ) >> 1 ) + SBarWidth + 1;
  1125.   r.top = r.bottom - SBarWidth + 1;
  1126.   SetPort( port );
  1127.   InvalRect( &r );
  1128. }
  1129. static void inval_msgrect( short w )
  1130. { if ( showPosnP ) wind_table[w].needs_inval_ticks = TickCount();
  1131. }
  1132. static void content( short id, EventRecord *event )
  1133. { WindowPtr w = wind_table[id].wptr;
  1134.   eTeHandle hTE = wind_table[id].hTE;
  1135.   short cntlCode;
  1136.   ControlHandle theControl;
  1137.   ChPos oldCaret;
  1138.  
  1139.   SetPort( w );
  1140.   ClipRect( &w->portRect );
  1141.   GlobalToLocal( &event->where );
  1142.   if ((cntlCode = FindControl( event->where, w, &theControl )) == 0 )
  1143.   { if (PtInRect( event->where, &(**hTE).viewRect ))
  1144.     { oldCaret = (**hTE).caretChPos;
  1145.       eTeClick( hTE, event->where, event->modifiers, event->when );
  1146.       if ( *(long *)&oldCaret != *(long *)&(**hTE).caretChPos ) inval_msgrect( id );
  1147.     }
  1148.   }
  1149.   else if (cntlCode == inThumb)
  1150.   { cntlCode = GetCtlValue( theControl );
  1151.     TrackControl( theControl, event->where, 0L );
  1152.     if ( ( cntlCode = GetCtlValue( theControl ) - cntlCode ) != 0 )
  1153.         if( (**theControl).contrlRect.left < (**theControl).contrlRect.top ) /* horizontal? */
  1154.             eTeScroll( hTE, cntlCode, 0, TRUE );
  1155.         else
  1156.             eTeScroll( hTE, 0, cntlCode, TRUE );
  1157.   }
  1158.   else
  1159.     TrackControl( theControl, event->where, (ProcPtr )-1L );
  1160. }
  1161. #endif
  1162.  
  1163. /* 10Jan93  e -- added display of caret position */
  1164. #if (eTe_SHOW_POSN == 0)
  1165. static void update_window( short w )
  1166. { GrafPtr port = wind_table[w].wptr;
  1167.   Rect r = port->portRect;
  1168. #ifdef USE_eTe_debug
  1169.   Str255 tStr;
  1170. #endif
  1171.   r.right -= (( r.right - r.left ) >> 1 ) + SBarWidth + 1;
  1172.   r.top = r.bottom - SBarWidth;
  1173.   SetPort( port );
  1174.   ClipRect( &port->portRect );    /* added for eTe  16Apr92  e  */
  1175.   BeginUpdate( port );
  1176.   EraseRect( &port->portRect );
  1177.   DrawControls( port );
  1178.   DrawGrowIcon( port );
  1179.   EraseRect( &r );
  1180.   MoveTo( r.left, r.top );
  1181.   LineTo( r.right, r.top );
  1182. #ifdef USE_eTe_debug
  1183.   NumToString( eTeTextLength( wind_table[w].hTE ), tStr);
  1184.   MoveTo( r.left + 4, r.bottom - 4 );
  1185.   ClipRect( &r );
  1186.   DrawString( (ConstStr255Param )tStr );
  1187. #endif
  1188.   eTeUpdate( wind_table[w].hTE );
  1189.   EndUpdate( port );
  1190. }
  1191. #else
  1192. static void update_window( short w )
  1193. { GrafPtr port = wind_table[w].wptr;
  1194.   Rect r = port->portRect;
  1195.   Str255 tStr;
  1196.   eTeHandle hTE;
  1197.   r.right -= (( r.right - r.left ) >> 1 ) + SBarWidth + 1;
  1198.   r.top = r.bottom - SBarWidth;
  1199.   SetPort( port );
  1200.   ClipRect( &port->portRect );    /* added for eTe  16Apr92  e  */
  1201.   BeginUpdate( port );
  1202.   EraseRect( &port->portRect );
  1203.   DrawControls( port );
  1204.   DrawGrowIcon( port );
  1205.   EraseRect( &r );
  1206.   MoveTo( r.left, r.top );
  1207.   LineTo( r.right, r.top );
  1208. #ifdef USE_eTe_debug
  1209.   NumToString( eTeTextLength( wind_table[w].hTE ), tStr);
  1210.   MoveTo( r.left + 4, r.bottom - 4 );
  1211.   ClipRect( &r );
  1212.   DrawString( (ConstStr255Param )tStr );
  1213. #else
  1214.   if ( showPosnP )
  1215.   { hTE = wind_table[w].hTE;
  1216.     MoveTo( r.left + 4, r.bottom - 4 );
  1217.     ClipRect( &r );
  1218.     NumToString( (long )(**hTE).caretChPos.v, tStr);
  1219.     DrawString( (ConstStr255Param )tStr );
  1220.     DrawString( "\p:" );
  1221.     NumToString( (long )(**hTE).caretChPos.h, tStr);
  1222.     DrawString( (ConstStr255Param )tStr );
  1223.   }
  1224. #endif
  1225.   eTeUpdate( wind_table[w].hTE );
  1226.   EndUpdate( port );
  1227. }
  1228. #endif
  1229.  
  1230. static void grow_window( id, p )
  1231. short id;
  1232. Point p;
  1233. { WindowPtr w = wind_table[id].wptr;
  1234.   long result;
  1235.   short oScroll;
  1236.   Rect r, oView;
  1237.   
  1238.   SetPort( w );
  1239.  
  1240.   /* 16Sep92  e  increase min size for H scroll bar
  1241.   SetRect(&r, 80, 80, screenBits.bounds.right, screenBits.bounds.bottom); */
  1242.   /* 13Oct92  e  per: bernard@sigi.cs.colorado.edu
  1243.   SetRect(&r, 150, 80, screenBits.bounds.right, screenBits.bounds.bottom); */
  1244.   r = (**GetGrayRgn()).rgnBBox;
  1245.   r.top = 80;
  1246.   r.left = 150;
  1247.   /*  13Oct92  e  */
  1248.   result = GrowWindow( w, p, &r );
  1249.   if (result == 0) return;
  1250.   SizeWindow( w, LoWord(result), HiWord(result), 1);
  1251.  
  1252.   InvalRect(&w->portRect);
  1253.   setup_view( id );
  1254.   HideControl( wind_table[id].vscroll );
  1255.   MoveControl( wind_table[id].vscroll, w->portRect.right - SBarWidth, w->portRect.top-1);
  1256.   SizeControl( wind_table[id].vscroll, SBarWidth+1, w->portRect.bottom - w->portRect.top-(SBarWidth-2));
  1257.   HideControl( wind_table[id].hscroll );
  1258.   MoveControl( wind_table[id].hscroll,
  1259.                   w->portRect.right-((w->portRect.right-w->portRect.left)>>1),
  1260.                   w->portRect.bottom - SBarWidth);
  1261.   SizeControl( wind_table[id].hscroll,
  1262.                   ((w->portRect.right-w->portRect.left)>>1)-(SBarWidth-1),
  1263.                   SBarWidth+1);
  1264.   ShowControl( wind_table[id].hscroll );
  1265.   ValidRect(&(*wind_table[id].hscroll)->contrlRect );
  1266.   ShowControl( wind_table[id].vscroll );
  1267.   ValidRect(&(*wind_table[id].vscroll)->contrlRect );
  1268.   update_window( id );
  1269.   /* dunno (sometimes appropriate, sometimes not)... eTeShowCaret( wind_table[id].hTE ); */
  1270. }
  1271.  
  1272. static void put_input( id, ptr, len, cr, flush, freshline )
  1273. short id;
  1274. char *ptr;
  1275. short len;
  1276. short cr, flush, freshline;
  1277. { if (wind_table[id].in_use)
  1278.   { long len1;
  1279.     register long len2 = (wind_table[id].len - wind_table[id].pos);
  1280.     register char *buf, *p1, *p2;
  1281.     if ((len2 < 0) || flush) len2 = 0;
  1282.     len1 = ((cr)?1:0) + len2 + (long)len;
  1283.     buf = NewPtr( len1 );
  1284.     if (buf == NULL) { SysBeep(10); return; }
  1285.     p1 = buf;
  1286.     p2 = wind_table[id].buf + wind_table[id].pos;
  1287.     while (len2 > 0) { *p1++ = *p2++; len2--; }
  1288.     p2 = ptr;
  1289.     while (len > 0) { *p1++ = *p2++; len--; }
  1290.     if (cr) *p1++ = '\r';
  1291.     if (wind_table[id].buf != NULL) DisposPtr( wind_table[id].buf );
  1292.     wind_table[id].buf = buf;
  1293.     wind_table[id].pos = 0;
  1294.     wind_table[id].len = len1;
  1295.     if (freshline)
  1296.     { eTeHandle hTE = wind_table[id].hTE;
  1297.       if ( (**hTE).caretChPos.h > 0 ) check_TEInsert( "\r", 1L, hTE, 0 );
  1298.       (**hTE).writeChPos = (**hTE).caretChPos; /*  9Jul92  e  */
  1299.       eTeShowCaret( hTE );
  1300.     }
  1301.   }
  1302. }
  1303.  
  1304. static void interrupt()
  1305. { intr_action( 0L, 0L, "SIGINT" );
  1306.   put_input( interaction_id, "", 0, 0, 1, 0 );
  1307. }
  1308.  
  1309. static short open_file( filename, for_output, txt, io )
  1310. Str255 filename;
  1311. short for_output;
  1312. short txt;
  1313. short *io;
  1314. { ioParam pb;
  1315.   fileParam fp;
  1316.   short refnum;
  1317.   short len = (unsigned char)filename[0];
  1318.   short file_existed = 1;
  1319.  
  1320.   pb.ioNamePtr = filename;
  1321.   pb.ioVRefNum = current_volume;
  1322.   pb.ioVersNum = 0;
  1323.   pb.ioPermssn = ((for_output) ? fsRdWrPerm : fsRdPerm);
  1324.   pb.ioMisc = 0;
  1325.  
  1326.   if (for_output)
  1327.   { asm
  1328.     { lea    pb,a0
  1329.       _PBCreate
  1330.     }
  1331.     file_existed = (pb.ioResult == dupFNErr);
  1332.     if ((pb.ioResult != noErr) && !file_existed)
  1333.     { *io = pb.ioResult;  return -1; }
  1334.   }
  1335.   
  1336.   asm
  1337.   { lea    pb,a0
  1338.     _PBOpen
  1339.   }
  1340.   if (pb.ioResult != noErr)
  1341.   { if (for_output)
  1342.       asm
  1343.       { lea    pb,a0
  1344.         _PBDelete
  1345.       }
  1346.     *io = pb.ioResult;
  1347.     return -1;
  1348.   }
  1349.   refnum = pb.ioRefNum;
  1350.   
  1351.   if (!for_output) { *io = noErr; return refnum; }
  1352.  
  1353.   asm
  1354.   { lea    pb,a0
  1355.     _PBSetEOF
  1356.   }
  1357.  
  1358.   if (!file_existed)
  1359.   { fp.ioNamePtr = filename;
  1360.     fp.ioVRefNum = current_volume;
  1361.     fp.ioFVersNum = 0;
  1362.     fp.ioFDirIndex = 0;
  1363.     asm
  1364.     { lea    fp,a0
  1365.       _PBGetFInfo
  1366.       bmi.s  @1
  1367.     }
  1368.     if (txt)
  1369.       fp.ioFlFndrInfo.fdType = 'TEXT';
  1370.     else
  1371.       fp.ioFlFndrInfo.fdType = 'gamO';
  1372.     fp.ioFlFndrInfo.fdCreator = 'gamI';
  1373.     asm
  1374.     { lea    fp,a0
  1375.       _PBSetFInfo
  1376.       @1
  1377.     }
  1378.   }
  1379.  
  1380.   *io = noErr;
  1381.   return refnum;
  1382. }
  1383.  
  1384. static short close_file( refnum, io )
  1385. short refnum;
  1386. short *io;
  1387. { *io = FSClose( refnum );
  1388.   return (*io != noErr);
  1389. }
  1390.  
  1391. static short read_file( id, line, chr )
  1392. short id;
  1393. long line;
  1394. long chr;
  1395. { TeHANDLE hTE = wind_table[id].hTE;
  1396.   short refnum;
  1397.   short io, io2;
  1398.   long pos = 0;
  1399.   short result;
  1400.   Rect r;
  1401.   short beep = 0;
  1402.   short text = 0;
  1403.   long size, count;
  1404.   ChPos chPos;
  1405.   Handle hData;
  1406.   
  1407.   if ( line > 32768 )
  1408.   { te_limit_err();
  1409.     return 0;
  1410.   }
  1411.   chPos.v = (line==0) ? 0 : line-1;
  1412.   chPos.h = (line==0) ? 0 : chr;
  1413.  
  1414.   SelectWindow( wind_table[id].wptr );
  1415.  
  1416.   if (wind_table[id].dirty)
  1417.     if (!discard_changes( id )) return 0;
  1418.   
  1419.   refnum = open_file( wind_table[id].filename, 0, 1, &io );
  1420.   if (refnum == -1)
  1421.   { io_err( io );
  1422.     return 0;
  1423.   }
  1424.  
  1425.   eTeSetTextPtr( hTE, (Ptr )&text, 1L );
  1426.   io = GetEOF(refnum, &size);
  1427.   if( io == noErr )    {
  1428.     if( hData = NewHandle( size + 1 ) )
  1429.     {    HLock( hData );
  1430.         count = size;
  1431.         io = FSRead( refnum, &count, *hData );
  1432.         HUnlock( hData );
  1433.         if( ( io != noErr ) || ( count != size ) )
  1434.         {    DisposHandle( hData );
  1435.             if ( io == noErr ) io = ioErr;
  1436.             /* io_err( io ); will happen */
  1437.             /* added these next lines 7Jul92  e */
  1438.             mem_full = 0;
  1439.               eTeSetTextPtr( hTE, (Ptr )&text, 1L );
  1440.         }
  1441.         else
  1442.         {    ((char *)*hData)[size] = '\0';
  1443.             io = eTeSetTextHandleDetabify( hTE, hData, prefTabs );
  1444.             if ( io == noErr )
  1445.             {   count = eTeChPosToOffset( hTE, chPos );
  1446.                 if ( count > 0 )
  1447.                     eTeSetSelect( hTE, count-1, count );
  1448.                 else
  1449.                     eTeSetSelect( hTE, count, count );
  1450.                 eTeShowCaret( hTE );
  1451.             }
  1452.             else
  1453.             {    DisposHandle( hData );
  1454.                 /* io_err( io ); will happen */
  1455.                 /* added these next lines 7Jul92  e */
  1456.                 mem_full = 0;
  1457.                   eTeSetTextPtr( hTE, (Ptr )&text, 1L );
  1458.             }
  1459.           }
  1460.       }
  1461.       else
  1462.       { mem_full = 0; io = mFulErr; }     /* mem_full = 0; added  7Jul92  e */
  1463.   }
  1464.  
  1465.   if (close_file( refnum, &io2 ) || ((io != eofErr) && (io != noErr)))
  1466.   { if ((io != eofErr) && (io != noErr)) io_err( io ); else io_err( io2 );
  1467.       eSmudgeWindow( id );
  1468.       return 0;
  1469.   }
  1470.   if (io != noErr)
  1471.       eSmudgeWindow( id );
  1472.   else
  1473.       eUnSmudgeWindow( id, hTE );
  1474.   return (io == noErr);
  1475. }
  1476.  
  1477. static void write_file( id )
  1478. short id;
  1479. { TeHANDLE hTE = wind_table[id].hTE;
  1480.   short refnum;
  1481.   short io, io2;
  1482.   long count;
  1483.  
  1484.   refnum = open_file( wind_table[id].filename, 1, 1, &io );
  1485.   if (refnum == -1)
  1486.   { io_err( io );
  1487.     return;
  1488.   }
  1489.  
  1490.   count = eTeTextLength( hTE );
  1491.   io = FSWrite( refnum, &count, *((**hTE).hText) );
  1492.  
  1493.   if (close_file( refnum, &io2 ) || (io != noErr))
  1494.   { if (io != noErr) io_err( io ); else io_err( io2 );
  1495.     return;
  1496.   }
  1497.  
  1498.   eUnSmudgeWindow( id, hTE );
  1499. }
  1500.  
  1501. short edit( name, line, chr )
  1502. char *name;
  1503. long line;
  1504. long chr;
  1505. { short id;
  1506.   Str255 pname;
  1507.   c_to_p( name, pname );
  1508.   if (pname[0] != '\0')
  1509.   { for (id=0; id<MAX_NB_WINDOWS; id++)
  1510.       if (wind_table[id].in_use && wind_table[id].is_file &&
  1511.           (compare_p_to_p( wind_table[id].filename, pname ) == 0)) break;
  1512.   }
  1513.   else
  1514.     id = MAX_NB_WINDOWS;
  1515.  
  1516.   if (id == MAX_NB_WINDOWS) id = wind_open( name, /* 27Jan93 e -- TRUE */ FALSE, TRUE, FALSE );
  1517.   if (id < 0)
  1518.     wind_err();
  1519.   else
  1520.   { wind_table[id].is_file = 1;
  1521.     p_to_p( pname, wind_table[id].filename );
  1522.     supress_mem_full_dialog = 1;        /* 27Jan93  e  */
  1523.     if ( read_file( id, line, chr ) )
  1524.         select_and_show( wind_table[id].wptr );
  1525.     else
  1526.         wind_close( id );
  1527.     supress_mem_full_dialog = 0;        /* 27Jan93  e  */
  1528.   }
  1529.   return id;
  1530. }
  1531.  
  1532. void save_as( id, path )
  1533. short id;
  1534. char *path;
  1535. { Str255 ppath;
  1536.   short i;
  1537.   c_to_p( path, ppath );
  1538.   for (i=0; i<MAX_NB_WINDOWS; i++)
  1539.     if (wind_table[i].in_use && wind_table[i].is_file &&
  1540.         (compare_p_to_p( wind_table[i].filename, ppath ) == 0))
  1541.     { already_open_err( ppath );
  1542.       return;
  1543.     }
  1544.   p_to_p( ppath, wind_table[id].filename );
  1545.   if (wind_table[id].is_file) SetWTitle( wind_table[id].wptr, ppath );
  1546.   if ( id != interaction_id ) /* 28Jan93  e  */
  1547.   { delWindMenuItem( id );
  1548.     addWindMenuItem( id );
  1549.   }
  1550.   write_file( id );
  1551. }
  1552.  
  1553. static void wind_quit()
  1554. { short id;
  1555.   for (id=MAX_NB_WINDOWS-1; id>=0; id--)
  1556.   { if (wind_table[id].in_use && wind_table[id].is_file && wind_table[id].dirty)
  1557.     { if (!discard_changes( id )) return;
  1558.       wind_close( id );
  1559.     }
  1560.   }
  1561.   abnormal_exit = 0;
  1562.   /* os_quit();    29Dec92  e  */
  1563.   gDoQuit = 1;    /* 29Dec92  e  */
  1564. }
  1565.  
  1566. static void handle_close( id )
  1567. short id;
  1568. { if (wind_table[id].is_file)
  1569.   { if ((wind_table[id].dirty) && (!discard_changes( id ))) return;
  1570.     wind_close( id );
  1571.   }
  1572.   else if (id != interaction_id)
  1573.     put_input( id, "\004", 1, 0, 1, 0 );
  1574. }
  1575.  
  1576. static char upcase( char c )
  1577. { if( c >= 'a' &&  c <= 'z' ) return c - 0x20;
  1578.   else return c;
  1579. }
  1580. /* 08Jan93  e  -- changed to return Boolean; added test for empty find string */
  1581. Boolean find_next( id )
  1582. short id;
  1583. { TeHANDLE hTE = wind_table[id].hTE;
  1584.   char *txt = *((**hTE).hText);
  1585.   long pos = eTeChPosToOffset( hTE, (**hTE).selActive ? (**hTE).selEnd : (**hTE).caretChPos );
  1586.   register char *start = txt + pos - 1;
  1587.   register short len = (unsigned char)find_string[0];
  1588.   register char *end = txt + eTeTextLength( hTE ) - len;
  1589.   register short i;
  1590.   Boolean again = TRUE;
  1591.   Str255 ci_string;
  1592.   if ( len == 0 ) goto not_found;
  1593.   if( find_ci )
  1594.   { for (i=len; i>0; i--)
  1595.       ci_string[i] = upcase( find_string[i] );
  1596.     /* ci_string[0] = len; -- not used */
  1597.   }
  1598.   find_begin:
  1599.   while (start < end)
  1600.   { if( find_ci )
  1601.     { for (i=len; i>0; i--)
  1602.         if( upcase( start[i] ) != (char )ci_string[i] ) goto next_pos;
  1603.     }
  1604.     else
  1605.     { for (i=len; i>0; i--)
  1606.         if (start[i] != (char )find_string[i]) goto next_pos;
  1607.     }
  1608.     eTeSetSelect( hTE, (long)(start-txt+1), (long)(start-txt+1+len) );
  1609.     return 1;
  1610.     next_pos:
  1611.     start++;
  1612.   }
  1613.   if( find_wrap && again ) 
  1614.   { start = txt;
  1615.     end = start + pos - len;
  1616.     again = FALSE;
  1617.     goto find_begin;
  1618.   }
  1619. not_found:
  1620.   SysBeep(10);
  1621.   return 0;
  1622. }
  1623.  
  1624. void replace_next( id )
  1625. short id;
  1626. { TeHANDLE hTE = wind_table[id].hTE;
  1627.   if ( ! (**hTE).selActive )
  1628.     SysBeep(10);
  1629.   else
  1630.   { long len = (unsigned char)replace_string[0];
  1631.     check_TEInsert( ((char *)replace_string)+1, len, hTE, wind_table[id].bold_input );
  1632.     find_next( id );
  1633.     eTeShowCaret( hTE );
  1634.   }
  1635. }
  1636.  
  1637. /* 08Jan93  e  */
  1638. void replace_all( id )
  1639. short id;
  1640. { TeHANDLE hTE = wind_table[id].hTE;
  1641.   long len = (unsigned char)replace_string[0];
  1642.   
  1643.   if ( (**hTE).selActive )
  1644.   { long pos = eTeChPosToOffset( hTE, (**hTE).selStart );
  1645.     eTeSetSelect( hTE, pos, pos );
  1646.   }
  1647.   while ( find_next( id ) )
  1648.     check_TEInsert( ((char *)replace_string)+1, len, hTE, wind_table[id].bold_input );
  1649.   eTeShowCaret( hTE );
  1650. }
  1651.  
  1652. static long balance( id, bal, prefix )
  1653. short id;
  1654. short bal;
  1655. short prefix;
  1656. {
  1657.   return balance_bwd( wind_table[id].hTE, bal, prefix );
  1658.   /* bulk of code moved to os_mac_eKeys.c  11Jul92  e */
  1659. }
  1660.  
  1661. static void balance_next( id )
  1662. short id;
  1663. { TeHANDLE hTE = wind_table[id].hTE;
  1664.   long start = balance( id, 0, 1 );
  1665.   if (start < 0)
  1666.     SysBeep(10);
  1667.   else
  1668.     eTeSetSelect( hTE, start,
  1669.                      eTeChPosToOffset( hTE, (**hTE).selActive ?
  1670.                                                   (**hTE).selEnd : (**hTE).caretChPos ) );
  1671. }
  1672.  
  1673. static void online_help()
  1674. {
  1675.   help_show();
  1676. }
  1677.  
  1678. static void find_dialog( short id )
  1679. { DialogPtr dp;
  1680.   short i, s;
  1681.   Handle h;
  1682.   Rect r;
  1683.   eTePutScrap();                                          /*  13Oct92  e  */
  1684.   ParamText( "\pFind:", "\pReplace with:", "\p", "\p" );
  1685.   dp = GetNewDialog( find_dlogID, 0L, (WindowPtr)-1L );
  1686.   if (dp == NULL) return;
  1687.   GetDItem( dp, 3, &i, &h, &r );
  1688.   SetIText( h, find_string );
  1689.   GetDItem( dp, 5, &i, &h, &r );
  1690.   SetIText( h, replace_string );
  1691.   GetDItem( dp, 7, &i, &h, &r);
  1692.   SetCtlValue( h, find_wrap );
  1693.   GetDItem( dp, 8, &i, &h, &r);
  1694.   SetCtlValue( h, find_ci );
  1695.   SelIText( dp, 3, 0, find_string[0] );
  1696.   SetCursor( &arrow );
  1697.   do
  1698.   { ModalDialog( 0L, &s );
  1699.     switch( s )
  1700.     { case 1:
  1701.         GetDItem( dp, 3, &i, &h, &r );
  1702.         GetIText( h, find_string );
  1703.         GetDItem( dp, 5, &i, &h, &r );
  1704.         GetIText( h, replace_string );
  1705.         GetDItem( dp, 7, &i, &h, &r);
  1706.         find_wrap = GetCtlValue( h );
  1707.         GetDItem( dp, 8, &i, &h, &r);
  1708.         find_ci   = GetCtlValue( h );
  1709.         break;
  1710.       case 7:
  1711.       case 8:
  1712.         GetDItem( dp, s, &i, &h, &r );
  1713.         SetCtlValue( (ControlHandle )h, GetCtlValue( (ControlHandle )h ) ^ 1 );
  1714.         break;
  1715.     }
  1716.   } while( s != 1 && s != 2 );
  1717.   DisposDialog( dp );
  1718.   if( s == 1 ) find_next( id );
  1719.   eTeGetScrap();                                          /*  13Oct92  e  */
  1720. }
  1721.  
  1722. /* 25Sep92  e  added print menu items */
  1723.  
  1724. static void handle_pgsetup( short id )
  1725.   TeHANDLE hTE = wind_table[id].hTE;
  1726.   SetCursor(&qd.arrow);
  1727.   PageSetupDialog( hTE );
  1728. }
  1729.  
  1730. /* 02Oct92  e  added iPrAbort check */
  1731.  
  1732. static void handle_print( short id )
  1733.   OSErr err;
  1734.   TeHANDLE hTE = wind_table[id].hTE;
  1735.   Str255 flnm;
  1736.   
  1737.   SetCursor(&qd.arrow);
  1738.   err = noErr;
  1739.   if ( ! (**hTE).hPrint )
  1740.     err = PageSetupDialog( hTE );
  1741.   if( ! err )
  1742.   {    pathstr_to_filename( wind_table[id].filename, flnm );
  1743.     err = eTePrint( hTE, true, true, flnm );
  1744.     eTePrint( nil, false, false, flnm );
  1745.   }
  1746.   if( err && err != userCanceledErr && err != iPrAbort ) io_err( err );
  1747. }
  1748.  
  1749. /*----------------------------------------------------------------------*/
  1750.  
  1751. #include "struct.h"
  1752.  
  1753. #define MAC_HEAP_LEN_IN_K  50 /* leave at least 50K free for Mac heap */
  1754. #define MAC_STACK_LEN_IN_K 26 /* leave this much stack space for Mac calls */
  1755.  
  1756. long link_stack_length_in_k = -1;
  1757. long link_heap_length_in_k = -1;
  1758. long link_const_length_in_k = -1;
  1759.  
  1760. struct mem_partition
  1761. { long base_heap,  extend_heap,  spare_heap;
  1762.   long base_const, extend_const, spare_const;
  1763.   long base_zzzzz, extend_zzzzz, spare_zzzzz;
  1764.   long base_mac,   extend_mac,   spare_mac;
  1765.   long spare1, spare2, spare3, spare4;
  1766. } memp;
  1767.  
  1768. long mem_cur_heap, mem_min_heap;
  1769. long mem_cur_const, mem_min_const;
  1770. long mem_cur_zzzzz, mem_min_zzzzz;
  1771. long mem_cur_mac, mem_min_mac;
  1772.  
  1773. #define DEFAULT_EXTEND_HEAP  8
  1774. #define DEFAULT_EXTEND_CONST 1
  1775. #define DEFAULT_EXTEND_ZZZZZ 0
  1776. #define DEFAULT_EXTEND_MAC   1
  1777.  
  1778. static void partition_init( ofiles_len )
  1779. long ofiles_len; /* total length of object files */
  1780. {
  1781.   struct mem_partition **memph;
  1782.   long initial_free_mem;
  1783.   long stack_len, system_mem_less_const_len, processor_mem_len, fixed_len;
  1784.   long free_mem_before_base, free_mem_after_base;
  1785.   long total_extend, extend_heap, extend_const, extend_zzzzz, extend_mac;
  1786.  
  1787.  
  1788.   memph = (struct mem_partition **)GetResource( 'gamP', 128 );
  1789.   if ((memph != NULL) && (SizeResource(memph) == sizeof(struct mem_partition)))
  1790.   { memp = **memph; ReleaseResource(memph); }
  1791.   else
  1792.     memp.base_heap = -1; /* forces initialization later on */
  1793.  
  1794.  
  1795.   /* initial_free_mem is the total free space at present in the Mac's heap */
  1796.  
  1797.   initial_free_mem = ((long)&initial_free_mem) - ((long)HeapEnd);
  1798.  
  1799.  
  1800.   /* compute amount of free memory before partitions are allocated */
  1801.  
  1802.   stack_len = ((long)DEFAULT_STACK_LENGTH_IN_K)*K;
  1803.  
  1804.   system_mem_less_const_len =
  1805.     ceiling8( sizeof(struct sstate_rec) ) +
  1806.     ((long)MAX_NB_GLOBALS)*sizeof(struct global_rec) +
  1807.     ((long)MAX_NB_GLOBALS)*sizeof(short) +
  1808.     ((long)NB_TRAPS)*sizeof(struct trap_rec);
  1809.  
  1810.   processor_mem_len =
  1811.     stack_len*2 +
  1812.     ceiling8( ((long)MAX_NB_EVENTS)*sizeof(long) ) +
  1813.     ((long)LOCAL_HEAP_LENGTH_IN_K)*K +
  1814.     ceiling8( ((long)MAX_NB_STATS)*sizeof(long) ) +
  1815.     ceiling8( ((long)MAX_EMUL_CODE_LENGTH_IN_K)*K ) +
  1816.     ceiling8( sizeof(struct pstate_rec) );
  1817.  
  1818.   fixed_len = ((long)MAC_STACK_LEN_IN_K*K)+system_mem_less_const_len+processor_mem_len;
  1819.  
  1820.   free_mem_before_base = (initial_free_mem - fixed_len)/K;
  1821.  
  1822.  
  1823.   /* setup partitions */
  1824.  
  1825.   mem_min_heap  = (long)MIN_HEAP_LENGTH_IN_K;
  1826.   mem_min_const = (ofiles_len+K-1)/K + (long)ADDITIONAL_CONST_LENGTH_IN_K;
  1827.   mem_min_zzzzz = 0;
  1828.   mem_min_mac   = (long)MAC_HEAP_LEN_IN_K;
  1829.  
  1830.   if ((memp.base_heap  < mem_min_heap ) || (memp.extend_heap  < 0) ||
  1831.       (memp.base_const < mem_min_const) || (memp.extend_const < 0) ||
  1832.       (memp.base_zzzzz < mem_min_zzzzz) || (memp.extend_zzzzz < 0) ||
  1833.       (memp.base_mac   < mem_min_mac  ) || (memp.extend_mac   < 0) ||
  1834.       (memp.base_heap+memp.base_const+memp.base_zzzzz+memp.base_mac > free_mem_before_base))
  1835.   {
  1836.     memp.base_heap  = mem_min_heap;   memp.extend_heap  = DEFAULT_EXTEND_HEAP;
  1837.     memp.base_const = mem_min_const;  memp.extend_const = DEFAULT_EXTEND_CONST;
  1838.     memp.base_zzzzz = mem_min_zzzzz;  memp.extend_zzzzz = DEFAULT_EXTEND_ZZZZZ;
  1839.     memp.base_mac   = mem_min_mac;    memp.extend_mac   = DEFAULT_EXTEND_MAC;
  1840.   }
  1841.  
  1842.   free_mem_after_base = free_mem_before_base -
  1843.                         (memp.base_heap+memp.base_const+memp.base_zzzzz+memp.base_mac);
  1844.   if (free_mem_after_base < 0) free_mem_after_base = 0;
  1845.  
  1846.   total_extend = memp.extend_heap+memp.extend_const+memp.extend_zzzzz+memp.extend_mac;
  1847.   if (total_extend < 1) total_extend = 1; /* to avoid divide by 0 */
  1848.  
  1849.   extend_heap  = (free_mem_after_base*memp.extend_heap )/total_extend;
  1850.   extend_const = (free_mem_after_base*memp.extend_const)/total_extend;
  1851.   extend_zzzzz = (free_mem_after_base*memp.extend_zzzzz)/total_extend;
  1852.   extend_mac   = free_mem_after_base - (extend_heap+extend_const+extend_zzzzz);
  1853.  
  1854.   mem_cur_heap  = memp.base_heap  + extend_heap;
  1855.   mem_cur_const = memp.base_const + extend_const;
  1856.   mem_cur_zzzzz = memp.base_zzzzz + extend_zzzzz;
  1857.   mem_cur_mac   = memp.base_mac   + extend_mac;
  1858.  
  1859.  
  1860.   link_stack_length_in_k = stack_len/K;
  1861.   link_heap_length_in_k  = mem_cur_heap;
  1862.   link_const_length_in_k = mem_cur_const;
  1863. }
  1864.  
  1865. static long get_num_item( dp, item )
  1866. DialogPtr dp;
  1867. short item;
  1868. { short i;
  1869.   Rect r;
  1870.   Handle h;
  1871.   long n;
  1872.   Str255 s;
  1873.   GetDItem( dp, item, &i, &h, &r );
  1874.   GetIText( h, s );
  1875.   for (i=s[0]; i>0; i--) if ((s[i] < '0') || (s[i] > '9')) return -1;
  1876.   StringToNum( s, &n );
  1877.   if (n > 32767) return -1;
  1878.   return n;
  1879. }
  1880.  
  1881. static void set_num_item( dp, item, val )
  1882. DialogPtr dp;
  1883. short item;
  1884. long val;
  1885. { short i;
  1886.   Rect r;
  1887.   Handle h;
  1888.   long n;
  1889.   Str255 s;
  1890.   NumToString( val, s );
  1891.   GetDItem( dp, item, &i, &h, &r );
  1892.   SetIText( h, s );
  1893. }
  1894.  
  1895. static void partition_dialog()
  1896. { Str255 s;
  1897.   DialogPtr dp;
  1898.   short i;
  1899.   Handle h;
  1900.   Rect r;
  1901.  
  1902.   long base_heap, extend_heap;
  1903.   long base_const, extend_const;
  1904.   long base_zzzzz, extend_zzzzz;
  1905.   long base_mac, extend_mac;
  1906.  
  1907.   dp = GetNewDialog( partition_dlogID, 0L, (WindowPtr)-1L );
  1908.   if (dp == NULL) return;
  1909.  
  1910.   SetCursor( &arrow );
  1911.  
  1912.   set_num_item( dp, 6,  mem_cur_heap );  set_num_item( dp, 7,  mem_min_heap );
  1913.   set_num_item( dp, 12, mem_cur_const ); set_num_item( dp, 13, mem_min_const );
  1914.   set_num_item( dp, 18, mem_cur_zzzzz ); set_num_item( dp, 19, mem_min_zzzzz );
  1915.   set_num_item( dp, 24, mem_cur_mac );   set_num_item( dp, 25, mem_min_mac );
  1916.  
  1917.   base_heap  = memp.base_heap;  extend_heap  = memp.extend_heap;
  1918.   base_const = memp.base_const; extend_const = memp.extend_const;
  1919.   base_zzzzz = memp.base_zzzzz; extend_zzzzz = memp.extend_zzzzz;
  1920.   base_mac   = memp.base_mac;   extend_mac   = memp.extend_mac;
  1921.  
  1922.   set_items:
  1923.  
  1924.   set_num_item( dp, 8,  base_heap );  set_num_item( dp, 9,  extend_heap );
  1925.   set_num_item( dp, 14, base_const ); set_num_item( dp, 15, extend_const );
  1926.   set_num_item( dp, 20, base_zzzzz ); set_num_item( dp, 21, extend_zzzzz );
  1927.   set_num_item( dp, 26, base_mac );   set_num_item( dp, 27, extend_mac );
  1928.  
  1929.   SelIText( dp, 8, 0, 32767 );
  1930.  
  1931.   do
  1932.   { ModalDialog( 0L, &i );
  1933.     switch( i )
  1934.     { case 1:
  1935.         base_heap    = get_num_item( dp, 8 );
  1936.         extend_heap  = get_num_item( dp, 9 );
  1937.         base_const   = get_num_item( dp, 14 );
  1938.         extend_const = get_num_item( dp, 15 );
  1939.         base_zzzzz   = get_num_item( dp, 20 );
  1940.         extend_zzzzz = get_num_item( dp, 21 );
  1941.         base_mac     = get_num_item( dp, 26 );
  1942.         extend_mac   = get_num_item( dp, 27 );
  1943.         if ((base_heap  >= mem_min_heap ) && (extend_heap  >= 0) &&
  1944.             (base_const >= mem_min_const) && (extend_const >= 0) &&
  1945.             (base_zzzzz >= mem_min_zzzzz) && (extend_zzzzz >= 0) &&
  1946.             (base_mac   >= mem_min_mac  ) && (extend_mac   >= 0))
  1947.         {
  1948.           if ((base_heap  != memp.base_heap ) || (extend_heap  != memp.extend_heap ) ||
  1949.               (base_const != memp.base_const) || (extend_const != memp.extend_const) ||
  1950.               (base_zzzzz != memp.base_zzzzz) || (extend_zzzzz != memp.extend_zzzzz) ||
  1951.               (base_mac   != memp.base_mac  ) || (extend_mac   != memp.extend_mac  ))
  1952.           {
  1953.             struct mem_partition **memph;
  1954.             memp.base_heap  = base_heap;  memp.extend_heap  = extend_heap;
  1955.             memp.base_const = base_const; memp.extend_const = extend_const;
  1956.             memp.base_zzzzz = base_zzzzz; memp.extend_zzzzz = extend_zzzzz;
  1957.             memp.base_mac   = base_mac;   memp.extend_mac   = extend_mac;
  1958.             memph = (struct mem_partition **)GetResource( 'gamP', 128 );
  1959.             if ((memph != NULL) && (SizeResource(memph) == sizeof(struct mem_partition)))
  1960.             {
  1961.               **memph = memp;
  1962.               ChangedResource(memph);
  1963.               WriteResource(memph);
  1964.               ReleaseResource(memph);
  1965.             }
  1966.           }
  1967.           break;
  1968.         }
  1969.         else
  1970.         {
  1971.           SysBeep( 10 );
  1972.           i = 0;
  1973.           break;
  1974.         }
  1975.       case 2:
  1976.         break;
  1977.       case 3: ;
  1978.         base_heap  = mem_min_heap;  extend_heap  = DEFAULT_EXTEND_HEAP;
  1979.         base_const = mem_min_const; extend_const = DEFAULT_EXTEND_CONST;
  1980.         base_zzzzz = mem_min_zzzzz; extend_zzzzz = DEFAULT_EXTEND_ZZZZZ;
  1981.         base_mac   = mem_min_mac;   extend_mac   = DEFAULT_EXTEND_MAC;
  1982.         goto set_items;
  1983.     }
  1984.   } while( i != 1 && i != 2 );
  1985.   DisposDialog( dp );
  1986. }
  1987.  
  1988. /*----------------------------------------------------------------------*/
  1989.  
  1990. static short handle_menu( result, id )
  1991. long result;
  1992. short id;
  1993. { short theItem, theMenu, m, w;
  1994.   MenuHandle mh;
  1995.   WindowPeek wPtr;
  1996.   Str255 name;
  1997.  
  1998.   theItem = LoWord( result );
  1999.   theMenu = HiWord( result );
  2000.   if (theMenu == 0) return 1;
  2001.   mh = GetMHandle( theMenu );
  2002.   for (m=appleM; m<=windowsM; m++) if (menus[m] == mh) break;
  2003.   switch (m)
  2004.   { case appleM:
  2005.       if (theItem == 1)
  2006.       { DialogPtr dp;
  2007.         short i;
  2008.         ParamText1( "\p" );
  2009.         dp = GetNewDialog( about_dlogID, 0L, (WindowPtr)-1L );
  2010.         if (dp != NULL)
  2011.         { ModalDialog( 0L, &i );
  2012.           DisposDialog( dp );
  2013.         }
  2014.       }
  2015.       else if (theItem == 2)
  2016.         online_help();
  2017.       else
  2018.       { GetItem(menus[appleM], theItem, &name);
  2019.         OpenDeskAcc( &name );
  2020.       }
  2021.       break;
  2022.     case fileM: 
  2023.       switch (theItem)
  2024.       { case newCommand:
  2025.         { short w = wind_open( "Untitled", TRUE, TRUE, FALSE );
  2026.           if (w < 0)
  2027.             wind_err();
  2028.           else
  2029.             wind_table[w].is_file = 1;
  2030.           break;
  2031.         }
  2032.         case openCommand:
  2033.         { short i;
  2034.           for (i=0; i<MAX_NB_WINDOWS; i++) if (!wind_table[i].in_use) break;
  2035.           if (i == MAX_NB_WINDOWS)
  2036.             wind_err();
  2037.           else
  2038.           { char path[FILENAME_LEN];
  2039.             if (get_file( "\p", 1, "TEXT", path, FILENAME_LEN ))
  2040.               edit( path, 0L, 0L );
  2041.           }
  2042.           break;
  2043.         }
  2044.         case closeCommand:
  2045.           if (id >= 0)
  2046.             handle_close( id );
  2047.           else
  2048.             help_hide_if_frontmost();
  2049.           break;
  2050.         case saveCommand:
  2051.           if (id >= 0)
  2052.             if (wind_table[id].is_file && (wind_table[id].filename[0] != '\0'))
  2053.             { write_file( id );
  2054.               break;
  2055.             }
  2056.         case saveasCommand:
  2057.           if (id >= 0)
  2058.           { char path[FILENAME_LEN];
  2059.             Str255 flnm;
  2060.             pathstr_to_filename( wind_table[id].filename, flnm );
  2061.             if (put_file( "\pSave as:", flnm, path, FILENAME_LEN ))
  2062.               save_as( id, path );
  2063.           }
  2064.           break;
  2065.         case revertCommand:
  2066.           if (id >= 0)
  2067.             read_file( id, 0L, 0L );
  2068.           break;
  2069.         /* 25Sep92  e  added print menu items */
  2070.         case pgsetupCommand:
  2071.           if (id >= 0)
  2072.             handle_pgsetup( id );
  2073.           break;
  2074.         case printCommand:
  2075.           if (id >= 0)
  2076.             handle_print( id );
  2077.           break;
  2078.         case quitCommand:
  2079.           wind_quit();
  2080.           if (gDoQuit) os_quit(); /* 29Dec92  e  */
  2081.           break;
  2082.       }
  2083.       break;
  2084.     /* 10Jan93  e  -- separated out Search menu */
  2085.     case editM: 
  2086.       if (id < 0)
  2087.         SystemEdit(theItem-1);
  2088.       else
  2089.       { TeHANDLE hTE = wind_table[id].hTE;
  2090.         switch (theItem)
  2091.         { case undoCommand:
  2092.             eTeUndo( hTE );
  2093.             eMaybeSmudgeWindow( id, hTE );
  2094.             break;
  2095.           case cutCommand:
  2096.             check_TECut( hTE );
  2097.             eMaybeSmudgeWindow( id, hTE );
  2098.             break;
  2099.           case copyCommand:
  2100.             check_TECopy( hTE );
  2101.             break;
  2102.           case pasteCommand:
  2103.             check_TEPaste( hTE, wind_table[id].bold_input );
  2104.             eMaybeSmudgeWindow( id, hTE );
  2105.             break;
  2106.           case clearCommand:
  2107.             eTeDelete( hTE );
  2108.             eMaybeSmudgeWindow( id, hTE );
  2109.             break;
  2110.           case selAllCommand:
  2111.             eTeSetSelect( hTE, 0, eTeTextLength( hTE ));
  2112.             break;
  2113.           case balanceCommand:
  2114.             balance_next( id );
  2115.             break;
  2116.           case smKeyBdCommand:
  2117.               smKeyBdP ^= 1;
  2118.               doKeyBdMode();
  2119.               break;
  2120.           case stylesCommand:
  2121.             eStyleDlg( hTE );
  2122.               break;
  2123.           case partitionCommand:
  2124.             partition_dialog();
  2125.               break;
  2126. #if (eTe_SHOW_POSN > 0)
  2127.           case showPosnCommand:    /* 10Jan93  e  */
  2128.               showPosnP ^= 1;
  2129.             CheckItem( menus[editM], showPosnCommand, showPosnP );
  2130.             for (w=0; w<MAX_NB_WINDOWS; w++)
  2131.                 if (wind_table[w].in_use) really_inval_msgrect( w );
  2132.               break;
  2133. #endif
  2134.           default: ;
  2135.         }
  2136.         eTeShowCaret( hTE );
  2137.       }
  2138.       break;
  2139.     case findM: 
  2140.       if (id >= 0)
  2141.       { TeHANDLE hTE = wind_table[id].hTE;
  2142.         switch (theItem)
  2143.         { case findCommand:
  2144.             find_dialog( id );
  2145.             break;
  2146.           case againCommand:
  2147.             find_next( id );
  2148.             break;
  2149.           case replaceCommand:
  2150.             replace_next( id );
  2151.             eMaybeSmudgeWindow( id, hTE );
  2152.             break;
  2153.           case fEnterCommand:
  2154.           { long sel_sta, sel_end, sel_len = 0;
  2155.             if( (**hTE).selActive ) {
  2156.               sel_sta = eTeChPosToOffset( hTE, (**hTE).selStart );
  2157.               sel_end = eTeChPosToOffset( hTE, (**hTE).selEnd );
  2158.               sel_len = sel_end - sel_sta;
  2159.             }
  2160.             if (sel_len > 0)
  2161.             { if( sel_len > 255 ) sel_len = 255;
  2162.               buf_to_p( (unsigned short)sel_len, *((**hTE).hText) + sel_sta, find_string );
  2163.             }
  2164.             else
  2165.               SysBeep( 1 );
  2166.             break;
  2167.           }
  2168.           case replaceAllCommand:    /* 08Jan93  e  */
  2169.             replace_all( id );
  2170.             eMaybeSmudgeWindow( id, hTE );
  2171.             break;
  2172.         }
  2173.         eTeShowCaret( hTE );
  2174.       }
  2175.       break;
  2176.     case commandM: 
  2177.       switch (theItem)
  2178.       { case helpCommand:
  2179.           put_input( interaction_id, " ,?", 3, 1, 1, 1 );
  2180.           break;
  2181.         case interruptCommand:
  2182.           interrupt();
  2183.           HiliteMenu(0);
  2184.           return 0;
  2185.         case returnCommand:
  2186.           put_input( interaction_id, " ,r", 3, 1, 1, 1 );
  2187.           break;
  2188.         case abortCommand:
  2189.           put_input( ((id < 0) || (wind_table[id].is_file))?interaction_id:id, "\004", 1, 0, 1, 0 );
  2190.           break;
  2191.         case toplevelCommand:
  2192.           put_input( interaction_id, " ,t", 3, 1, 1, 1 );
  2193.           break;
  2194.         case nextCommand:
  2195.           put_input( interaction_id, " ,+", 3, 1, 1, 1 );
  2196.           break;
  2197.         case previousCommand:
  2198.           put_input( interaction_id, " ,-", 3, 1, 1, 1 );
  2199.           break;
  2200.         case backtraceCommand:
  2201.           put_input( interaction_id, " ,b", 3, 1, 1, 1 );
  2202.           break;
  2203.         case procedureCommand:
  2204.           put_input( interaction_id, " ,i", 3, 1, 1, 1 );
  2205.           break;
  2206.         case expressionCommand:
  2207.           put_input( interaction_id, " ,y", 3, 1, 1, 1 );
  2208.           break;
  2209.         case localsCommand:
  2210.           put_input( interaction_id, " ,l", 3, 1, 1, 1 );
  2211.           break;
  2212.         default:
  2213.           break;
  2214.         }
  2215.       break;
  2216.     case windowsM:
  2217.       switch(theItem)
  2218.       { case 1:
  2219.           select_and_show( wind_table[interaction_id].wptr );
  2220.           break;
  2221.         case 2: /* blank line */
  2222.           break;
  2223.         default:
  2224.           theItem -= fustItem;
  2225.           if(theItem < qWindItems)
  2226.             SelectWindow( wind_table[itemtowindid[theItem]].wptr );
  2227.           break;
  2228.       }
  2229.       break;
  2230.     default:
  2231.       { char s[10], *p2 = s+10, *p1 = p2;
  2232.         do { *--p1 = (result%10)+'0'; result /= 10; } while (result > 0);
  2233.         put_input( interaction_id, " (mac#menuselection ", 21, 0, 1, 0 );
  2234.         put_input( interaction_id, p1, p2-p1, 0, 0, 0 );
  2235.         put_input( interaction_id, ")", 1, 1, 0, 1 );
  2236.         return 1;
  2237.       }
  2238.    }
  2239.   HiliteMenu(0);
  2240.   return 1;
  2241. }
  2242.  
  2243. static short do_tasks()
  2244. { short w;
  2245.   register short i;
  2246.   short men;
  2247.   EventRecord event;
  2248.   Rect r;
  2249.   RgnHandle rgn;
  2250. #if (eTe_SHOW_POSN > 0)
  2251.   long ticks = TickCount();
  2252. #endif
  2253.  
  2254.   for (i=0; i<MAX_NB_WINDOWS; i++)
  2255.     if (wind_table[i].in_use)
  2256.     { if (wind_table[i].out_len > 0)
  2257.       { HLock( wind_table[i].out_buf );    /* 10Jan93  e  */
  2258.         check_TEWrite( *wind_table[i].out_buf, (long)wind_table[i].out_len, wind_table[i].hTE, 0 );
  2259.         HUnlock( wind_table[i].out_buf );
  2260.         wind_table[i].out_len = 0;
  2261.       }
  2262. #if (eTe_SHOW_POSN > 0)
  2263.       /* 10Jan93  e -- added display of caret position */
  2264.       if ( wind_table[i].needs_inval_ticks != 0
  2265.            && ( wind_table[i].needs_inval_ticks + 15 ) < ticks )
  2266.       { really_inval_msgrect( i );
  2267.         wind_table[i].needs_inval_ticks = 0;
  2268.       }
  2269. #endif
  2270.     }
  2271.  
  2272.   w = wptr_to_id( FrontWindow() );
  2273.   if ( (!gInBackground) && (w >= 0) )
  2274.   { Point pt;
  2275.     Cursor *curs;
  2276.     OSEventAvail(0L, &event); /* This returns a NULL event with the global mouse location */
  2277.     if( gCursRgnOK && PtInRgn( event.where, gCursorRgn ) )
  2278.     { /* punt... SetCursor( current_cursor ); */
  2279.     }
  2280.     else
  2281.     { SetPort( (GrafPtr)wind_table[w].wptr );
  2282.       r = (**wind_table[w].hTE).viewRect;
  2283.       LocalToGlobal((Point *)&(r.top));      /* topLeft  */
  2284.       LocalToGlobal((Point *)&(r.bottom));   /* botRight */
  2285.       if (PtInRect( event.where, &r ))
  2286.       { curs = &ibeam_cursor;
  2287.         RectRgn(gCursorRgn, &r);
  2288.       }
  2289.       else
  2290.       { curs = &arrow;
  2291.         SetRectRgn(gCursorRgn, -32768, -32768, 32766, 32766);
  2292.         rgn = NewRgn();
  2293.         RectRgn(rgn, &r);
  2294.         DiffRgn(gCursorRgn, rgn, gCursorRgn);
  2295.         DisposeRgn(rgn);
  2296.       }
  2297.       if (!gCursRgnOK || current_cursor != curs)
  2298.       { current_cursor = curs;
  2299.         SetCursor( current_cursor );
  2300.       }
  2301.       gCursRgnOK = true;
  2302.     }
  2303.     eTeIdle( wind_table[w].hTE );
  2304.     men = (wind_table[w].is_file && (wind_table[w].filename[0] != '\0')) ? 1 : 0;
  2305.   }
  2306.   else
  2307.   { SetRectRgn(gCursorRgn, -32768, -32768, 32766, 32766);
  2308.     gCursRgnOK = false;
  2309.     SetCursor(&qd.arrow);    /*  13Oct92  e  */
  2310.     men = 2;
  2311.   }
  2312.  
  2313.   if ( (!gInBackground) && (men != current_menus) )
  2314.   { current_menus = men;
  2315.     switch (men)
  2316.     { case 0:
  2317.       case 1:
  2318.         /*
  2319.         EnableItem( menus[fileM], newCommand );
  2320.         EnableItem( menus[fileM], openCommand );
  2321.         EnableItem( menus[fileM], closeCommand );
  2322.         */
  2323.         EnableItem( menus[fileM], saveCommand );
  2324.         EnableItem( menus[fileM], saveasCommand );
  2325.         if (men == 0)
  2326.           DisableItem( menus[fileM], revertCommand );
  2327.         else
  2328.           EnableItem( menus[fileM], revertCommand );
  2329.         /*
  2330.         EnableItem( menus[fileM], quitCommand );
  2331.         */
  2332.         EnableItem( menus[fileM], pgsetupCommand );
  2333.         EnableItem( menus[fileM], printCommand );
  2334.         EnableItem( menus[editM], selAllCommand );
  2335.         EnableItem( menus[editM], balanceCommand );
  2336.         EnableItem( menus[editM], smKeyBdCommand );
  2337.         EnableItem( menus[editM], stylesCommand );
  2338.         EnableItem( menus[editM], partitionCommand );
  2339.         EnableItem( menus[editM], showPosnCommand );
  2340.         /*
  2341.         EnableItem( menus[findM], findCommand );
  2342.         EnableItem( menus[findM], againCommand );
  2343.         EnableItem( menus[findM], replaceCommand );
  2344.         EnableItem( menus[findM], replaceAllCommand );
  2345.         EnableItem( menus[findM], fEnterCommand ); */
  2346.         EnableItem( menus[findM], 0 );
  2347.         /*
  2348.         EnableItem( menus[commandM], 0 );
  2349.         */
  2350.         DrawMenuBar();
  2351.         break;
  2352.       case 2:
  2353.         /*
  2354.         DisableItem( menus[fileM], newCommand );
  2355.         DisableItem( menus[fileM], openCommand );
  2356.         DisableItem( menus[fileM], closeCommand );
  2357.         */
  2358.         DisableItem( menus[fileM], saveCommand );
  2359.         DisableItem( menus[fileM], saveasCommand );
  2360.         DisableItem( menus[fileM], revertCommand );
  2361.         /*
  2362.         DisableItem( menus[fileM], quitCommand );
  2363.         */
  2364.         DisableItem( menus[fileM], pgsetupCommand );
  2365.         DisableItem( menus[fileM], printCommand );
  2366.         DisableItem( menus[editM], selAllCommand );
  2367.         DisableItem( menus[editM], balanceCommand );
  2368.         DisableItem( menus[editM], smKeyBdCommand );
  2369.         DisableItem( menus[editM], stylesCommand );
  2370.         DisableItem( menus[editM], partitionCommand );
  2371.         DisableItem( menus[editM], showPosnCommand );
  2372.         /*
  2373.         DisableItem( menus[findM], findCommand );
  2374.         DisableItem( menus[findM], againCommand );
  2375.         DisableItem( menus[findM], replaceCommand );
  2376.         DisableItem( menus[findM], replaceAllCommand );
  2377.         DisableItem( menus[findM], fEnterCommand ); */
  2378.         DisableItem( menus[findM], 0 );
  2379.         /*
  2380.         DisableItem( menus[commandM], 0 );
  2381.         */
  2382.         DrawMenuBar();
  2383.         break;
  2384.     }
  2385.   }
  2386.  
  2387.   if ( (!gInBackground) && (w>=0) )
  2388.   { register EvQElPtr q;
  2389.     for (q = (EvQElPtr)EventQueue.qHead; q != NULL; q = (EvQElPtr)q->qLink)
  2390.       if ((q->evtQWhat == keyDown) && ((char)q->evtQMessage == '.') &&
  2391.           (q->evtQModifiers & cmdKey))
  2392.       { while (GetNextEvent(keyDownMask+keyUpMask+autoKeyMask,&event) &&
  2393.                !((event.what == keyDown) && ((event.message & charCodeMask) == '.') &&
  2394.                  ((event.modifiers & cmdKey) != 0) )) ;
  2395.         interrupt();
  2396.         return 0;
  2397.       }
  2398.   }
  2399.   return 1;
  2400. }
  2401.  
  2402. /* from MacDTS */
  2403. #define kDITop                    0x0050        /* kTopLeft - for positioning the Disk */
  2404. #define kDILeft                    0x0070        /*   Initialization dialogs. */
  2405.  
  2406. void handle_activate( WindowPtr wptr, Boolean activep )
  2407. { GrafPtr port;
  2408.   Rect r;
  2409.   short w = wptr_to_id( wptr );
  2410.   if( w >= 0 )
  2411.   { port = wind_table[w].wptr;
  2412.     r = port->portRect;
  2413.     r.left = r.right - (SBarWidth+1);
  2414.     SetPort( port );
  2415.     InvalRect( &r );
  2416.     r = port->portRect;
  2417.     r.top = r.bottom - (SBarWidth+1);
  2418.     InvalRect( &r );
  2419.     if ( activep )
  2420.     { eTeActivate( wind_table[w].hTE );
  2421.       ShowControl( wind_table[w].vscroll );
  2422.       ShowControl( wind_table[w].hscroll );
  2423.     }
  2424.     else
  2425.     { eTeDeactivate( wind_table[w].hTE );
  2426.       HideControl( wind_table[w].vscroll );
  2427.       HideControl( wind_table[w].hscroll );
  2428.     }
  2429.   }
  2430. }
  2431.  
  2432. static short handle_event( event )
  2433. EventRecord *event;
  2434. { register short i;
  2435.   short w;
  2436.   WindowPtr event_window;
  2437.  
  2438.   if (IsDialogEvent( event ))
  2439.   { DialogPtr dp;
  2440.     short item;
  2441.     DialogSelect( event, &dp, &item );
  2442.     return 1;
  2443.   }
  2444.  
  2445.   if (help_event( event )) return 1;
  2446.  
  2447.   w = wptr_to_id( FrontWindow() );
  2448.   { switch (event->what)
  2449.     { case mouseDown:
  2450.         gCursRgnOK = false;
  2451.         switch (FindWindow(event->where,&event_window))
  2452.         { case inDesk: 
  2453.             SysBeep( 10 );
  2454.             break;
  2455.           case inGoAway:
  2456.             if ((w = wptr_to_id( event_window )) >= 0)
  2457.               if (TrackGoAway(event_window,event->where)) handle_close( w );
  2458.             break;
  2459.           case inMenuBar:
  2460.           { TeHANDLE hTE = NULL;
  2461.             if( w >= 0) hTE = wind_table[w].hTE;
  2462.             eTeEditMenuUpdate( hTE, menus[editM] );
  2463.             return handle_menu( MenuSelect(event->where), w );
  2464.           }
  2465.           case inSysWindow:
  2466.             SystemClick(event,event_window);
  2467.             break;
  2468.           case inDrag:
  2469.             if ((w = wptr_to_id( event_window )) >= 0)
  2470.               DragWindow(event_window,event->where,&screenBits.bounds);
  2471.             break;
  2472.           case inGrow:
  2473.             if ((w = wptr_to_id( event_window )) >= 0)
  2474.               grow_window( w, event->where );
  2475.             break;
  2476.           case inContent:
  2477.             if (event_window != FrontWindow())
  2478.               SelectWindow(event_window);
  2479.             else 
  2480.               if ((w = wptr_to_id( event_window )) >= 0)
  2481.                 content( w, event );
  2482.             break;
  2483.           default: ;
  2484.         }
  2485.         gLastMouseDown = *event;
  2486.         break;
  2487.       case mouseUp:
  2488.         gLastMouseUp = *event;
  2489.         break;
  2490.       case keyDown:
  2491.       case autoKey:
  2492.         { char c = event->message & charCodeMask;
  2493.           TeHANDLE hTE = (w>=0)?wind_table[w].hTE:NULL;
  2494.           if ((c == '\r' /*cr*/) && ((event->modifiers & (cmdKey|optionKey)) != 0))
  2495.             event->modifiers = optionKey;
  2496.           else if( smKeyBdP )
  2497.           { /* hokey */
  2498.             if( (event->modifiers & optionKey) != 0 )
  2499.               { if( (event->modifiers & cmdKey) == 0 )
  2500.                   event->modifiers &= ~optionKey;    /* option key gets state of command key */
  2501.                 event->modifiers &= ~cmdKey;        /* turn off command key */
  2502.                 event->modifiers |= controlKey;        /* turn on  control key */
  2503.                 c -= 0x40;    /* its (close to) what control key would have done */
  2504.               }
  2505.           }
  2506.           if ((event->modifiers & cmdKey) != 0)
  2507.           { long res;
  2508.             if (c == '=') c = '+'; else if (c == '/') c = '?';
  2509.             if (hTE != NULL) eTeEditMenuUpdate( hTE, menus[editM] );
  2510.             res = MenuKey( c );
  2511.             if( HiWord( res ) != 0 )
  2512.                 return handle_menu( res, w );
  2513.             if( (hTE != NULL) && (c >= 0) && (c <= 31) )
  2514.             {   check_TEKey( c, hTE, 0, event );
  2515.                 inval_msgrect( w ); /* 10Jan93  e -- added display of caret position */
  2516.             }
  2517.             break;
  2518.           }
  2519.           if (hTE == NULL) break;
  2520.           if( ( event->modifiers & controlKey ) != 0 )
  2521.           { eEditCommand( hTE, c, event->modifiers, wind_table[w].bold_input );
  2522.               eMaybeSmudgeWindow( w, hTE );
  2523.               inval_msgrect( w );
  2524.           }
  2525.           else
  2526.           { if( ( c == 3 /*enter*/ ) || (( c == '\r' /*cr*/ ) && ((event->modifiers & optionKey) != 0 )))
  2527.             { short id = (wind_table[w].is_file) ? interaction_id : w;
  2528.               long sel_sta, sel_end, sel_len, dummy;
  2529.               if( (**hTE).selActive )
  2530.               { sel_sta = eTeChPosToOffset( hTE, (**hTE).selStart );
  2531.                   sel_end = eTeChPosToOffset( hTE, (**hTE).selEnd );
  2532.               }
  2533.               else
  2534.               { sel_end = eTeChPosToOffset( hTE, (**hTE).caretChPos );
  2535.                 if (c == 3 /*enter*/)
  2536.                     eTeGetRun( hTE, &sel_sta, &dummy );
  2537.                 else
  2538.                 { sel_sta = balance( w, 0, 1 );
  2539.                   if (sel_sta < 0) sel_sta = sel_end;
  2540.                 }
  2541.               }
  2542.               sel_len = sel_end - sel_sta;
  2543.               HLock((**hTE).hText);
  2544.               if (sel_len > 0)
  2545.               { eTeSetSelect( hTE, sel_end, sel_end );
  2546.                 put_input( id, *((**hTE).hText) + sel_sta, sel_len, 1, 0, 1 );
  2547.               }
  2548.               else
  2549.                 SysBeep(10);
  2550.               HUnlock((**hTE).hText);
  2551.             }
  2552.             else if (c == 9) /*tab*/
  2553.             { eTabCommand( hTE, event->modifiers, wind_table[w].bold_input );
  2554.               eMaybeSmudgeWindow( w, hTE );
  2555.               inval_msgrect( w ); /* 10Jan93  e -- added display of caret position */
  2556.             }
  2557.             else if( ( c == 127 /* del fwd */ )  || ( (c >= 0) && (c <= 31) ) )
  2558.             { check_TEKey( c, hTE, wind_table[w].bold_input, event );
  2559.               if ((c == '\r') && (**hTE).autoInd)
  2560.                 eTabCommand( hTE, event->modifiers, wind_table[w].bold_input );
  2561.               eMaybeSmudgeWindow( w, hTE );
  2562.               inval_msgrect( w ); /* 10Jan93  e -- added display of caret position */
  2563.             }
  2564.             else
  2565.             { if (!check_TEKey( c, hTE, wind_table[w].bold_input, event ))
  2566.                 if (c == ')')
  2567.                 { long open_paren = balance( w, 0, 0 );
  2568.                   if (open_paren >= 0)
  2569.                   { long now;
  2570.                     long selstart = eTeChPosToOffset( hTE, (**hTE).caretChPos );
  2571.                     eTeSetSelect( hTE, open_paren, open_paren+1 );
  2572.                     eTeShowCaret( hTE );
  2573.                     now = TickCount();
  2574.                     while ((TickCount()-now)<10) ;
  2575.                     while (!EventAvail(everyEvent,event) && ((TickCount()-now)<30)) ;
  2576.                     eTeSetSelect( hTE, selstart, selstart );
  2577.                   }
  2578.                 }
  2579.               eMaybeSmudgeWindow( w, hTE );
  2580.               inval_msgrect( w ); /* 10Jan93  e -- added display of caret position */
  2581.               eTeShowCaret( hTE );
  2582.             }
  2583.           }
  2584.         }
  2585.         break;
  2586.       case activateEvt:
  2587.         gCursRgnOK = false;
  2588.           handle_activate( (WindowPtr)event->message, ((event->modifiers & activeFlag) != 0) );
  2589.         break;
  2590.       case updateEvt: 
  2591.         if ((w = wptr_to_id( (WindowPtr)event->message )) >= 0)
  2592.         { update_window(w);
  2593.         }
  2594.         else
  2595.         { GrafPtr port = (WindowPtr)event->message;
  2596.           SetPort( port );
  2597.           BeginUpdate( port );
  2598.           EndUpdate( port );
  2599.         }
  2600.         break;
  2601.       case osEvt:
  2602.         if( *(char *)(&event->message) == suspendResumeMessage )
  2603.         { gCursRgnOK = false; /* Suspend/resume is also an activate/deactivate. */
  2604.           gInBackground = !(event->message & resumeFlag);
  2605.           if( gInBackground ) eTePutScrap();                  /*  13Aug92  e  */
  2606.           else                  eTeGetScrap();                  /*  13Aug92  e  */
  2607.           handle_activate( FrontWindow(), !gInBackground );
  2608.         }
  2609.         /* punt mouseMovedMessage */
  2610.         break;
  2611.       case diskEvt:
  2612.         gCursRgnOK = false;                     /* It is not a bad idea to at least call */
  2613.         if (HiWord(event->message) != noErr) /* DIBadMount in response to a diskEvt,  */
  2614.         { Point pt;                             /* so that the user can format a floppy. */
  2615.           SetPt( &pt, kDILeft, kDITop );
  2616.           DIBadMount( pt, event->message );
  2617.         }
  2618.         break;
  2619.       /* 29Dec92  e  */
  2620.       case kHighLevelEvent:
  2621.         AEProcessAppleEvent(event);
  2622.         if (gDoQuit) os_quit();
  2623.         break;
  2624.       default: ;
  2625.     }
  2626.   }
  2627.   return 1;
  2628. }
  2629.  
  2630. static long wind_read( id, ptr, n )
  2631. short id;
  2632. char *ptr;
  2633. long n;
  2634. { if (n > 0)
  2635.   { register short len;
  2636.     if ((len = wind_table[id].len - wind_table[id].pos) > 0)
  2637.     { register char *p1 = ptr;
  2638.       register char *p2 = wind_table[id].buf + wind_table[id].pos;
  2639.       register short i = 0;
  2640.       if (p2[i] == '\004') { wind_table[id].pos++; return 0; }
  2641.       if (len > n) len = n;
  2642.       while ((i < len) && (p2[i] != '\004'))
  2643.       { register char c = p2[i++];
  2644.         if (c == '\r') c = '\n';
  2645.         *p1++ = c;
  2646.       }
  2647.       wind_table[id].pos += i;
  2648.       return i;
  2649.     }
  2650.     else
  2651.       return -2;
  2652.   }
  2653.   else
  2654.     return 0;
  2655. }
  2656.  
  2657. static long wind_write( id, ptr, n )
  2658. short id;
  2659. char *ptr;
  2660. long n;
  2661. { register char *p1 = ptr, *p2 = ptr+n;
  2662.   register short len = wind_table[id].out_len;
  2663.   register char *p3; /*  = wind_table[id].out_buf; */
  2664.   /* 10Jan93  e  */
  2665.   if( wind_table[id].out_buf == NULL )
  2666.   { wind_table[id].out_buf = NewHandle( OUT_BUF_LEN );
  2667.     if( wind_table[id].out_buf == NULL )
  2668.     { SysBeep(10);
  2669.       return 0;
  2670.     }
  2671.   }
  2672.   HLock( wind_table[id].out_buf );
  2673.   p3 = (char *)*wind_table[id].out_buf;
  2674.  
  2675.   if (!((WindowPeek)wind_table[id].wptr)->visible)
  2676.     select_and_show( wind_table[id].wptr );
  2677.  
  2678.   while (p1<p2)
  2679.   { register char c = *p1++;
  2680.     if (c == '\n') { p3[len++] = '\r'; goto output; }
  2681.     p3[len++] = c;
  2682.     if (len == OUT_BUF_LEN)
  2683.     { output:
  2684.       if (check_TEWrite( p3, (long)len, wind_table[id].hTE, 0 )) goto werror;
  2685.       wind_table[id].out_len = 0;
  2686.       len = 0;
  2687.     }
  2688.   }
  2689.   wind_table[id].out_len = len;
  2690. werror:
  2691.   HUnlock( wind_table[id].out_buf );
  2692.   return n;
  2693. }
  2694.  
  2695. /*--------------------------------------------------------------------------*/
  2696.  
  2697. /* new stuff  29Dec92  e  */
  2698.  
  2699. /* Used to check for any unread required parameters. Returns true if we
  2700. ** missed at least one. */
  2701.  
  2702. static Boolean    MissedAnyParameters(AppleEvent *message)
  2703. {
  2704.     OSErr        err;
  2705.     DescType    ignoredActualType;
  2706.     AEKeyword    missedKeyword;
  2707.     Size        ignoredActualSize;
  2708.     EventRecord    event;
  2709.  
  2710.     err = AEGetAttributePtr(    /* SEE IF PARAMETERS ARE ALL USED UP.          */
  2711.         message,                /* AppleEvent to check.                          */
  2712.         keyMissedKeywordAttr,    /* Look for unread parameters.                  */
  2713.         typeWildCard,            /* So we can see what type we missed, if any. */
  2714.         &ignoredActualType,        /* What it would have been if not coerced.      */
  2715.         (Ptr)&missedKeyword,    /* Data area.  (Keyword not handled.)          */
  2716.         sizeof(missedKeyword),    /* Size of data area.                          */
  2717.         &ignoredActualSize        /* Actual data size.                          */
  2718.     );
  2719.  
  2720. /* No error means that we found some unused parameters. */
  2721.  
  2722.     if (err == noErr) {
  2723.         event.message = *(long *) &ignoredActualType;
  2724.         event.where = *(Point *) &missedKeyword;
  2725.         err = errAEEventNotHandled;
  2726.     }
  2727.  
  2728. /* errAEDescNotFound means that there are no more parameters.  If we get
  2729. ** an error code other than that, flag it. */
  2730.  
  2731.     return(err != errAEDescNotFound);
  2732. }
  2733.  
  2734. static OSErr    OpenDocEventHandler(AppleEvent *message, AppleEvent *reply, short mode)
  2735. {
  2736.     OSErr        err;
  2737.     OSErr        err2;
  2738.     AEDesc        theDesc;
  2739.     FSSpec        theFSS;
  2740.     short        loop;
  2741.     long        numFilesToOpen;
  2742.     AEKeyword    ignoredKeyWord;
  2743.     DescType    ignoredType;
  2744.     Size        ignoredSize;
  2745.     char path[512];
  2746.     short w;
  2747.     Str255 flnm;
  2748.     FInfo theFInfo;
  2749.  
  2750.     theDesc.dataHandle = nil;
  2751.         /* Make sure disposing of the descriptors is okay in all cases.
  2752.         ** This will not be necessary after 7.0b3, since the calls that
  2753.         ** attempt to create the descriptors will nil automatically
  2754.         ** upon failure. */
  2755.  
  2756.     if ( err = AEGetParamDesc( message, keyDirectObject, typeAEList, &theDesc ) )
  2757.         return(err);
  2758.  
  2759.     if ( ! MissedAnyParameters( message ) ) {
  2760.  
  2761. /* Got all the parameters we need.  Now, go through the direct object,
  2762. ** see what type it is, and parse it up. */
  2763.  
  2764.         err = AECountItems(&theDesc, &numFilesToOpen);
  2765.         if (!err)
  2766.         {    /* We have numFilesToOpen that need opening, as either a window
  2767.             ** or to be printed.  Go to it... */
  2768.  
  2769.             for (loop = 1; ((loop <= numFilesToOpen) && (!err)); ++loop)
  2770.             {    err = AEGetNthPtr(        /* GET NEXT IN THE LIST...         */
  2771.                     &theDesc,            /* List of file names.             */
  2772.                     loop,                /* Item # in the list.             */
  2773.                     typeFSS,            /* Item is of type FSSpec.         */
  2774.                     &ignoredKeyWord,    /* Returned keyword -- we know.  */
  2775.                     &ignoredType,        /* Returned type -- we know.     */
  2776.                     (Ptr)&theFSS,        /* Where to put the FSSpec info. */
  2777.                     sizeof(theFSS),        /* Size of the FSSpec info.         */
  2778.                     &ignoredSize        /* Actual size -- we know.         */
  2779.                 );
  2780.                 if (err) break;
  2781.  
  2782.                 FSpGetFInfo( &theFSS, &theFInfo );
  2783.                 if ( theFInfo.fdType != 'TEXT' )
  2784.                 { SysBeep( 3 );
  2785.                   continue;
  2786.                 }
  2787.  
  2788.                 gPrintPage = mode;
  2789.                     /* Open the window off-screen if we are printing.
  2790.                     ** We use the gPrintPage global to flag this.  Normally, the
  2791.                     ** gPrintPage global is to tell ImageDocument if we are imaging
  2792.                     ** to the window or to paper.  We don't need it for this yet,
  2793.                     ** as we can't image the document until it is opened.  DoNewWindow()
  2794.                     ** uses gPrintPage as a flag to open the window off-screen, but
  2795.                     ** visible, so that PrintMonitor can use the title of the window
  2796.                     ** as the document name that is being printed. */
  2797.                 
  2798.                 if ( getfullpath( theFSS.vRefNum, theFSS.parID, theFSS.name, path, 511, 1 ) )
  2799.                 {    
  2800.                     w = edit( path, 0L, 0L );
  2801.  
  2802.                     if ( gPrintPage && w >= 0 )
  2803.                     {    pathstr_to_filename( wind_table[w].filename, flnm );
  2804.                         err  = eTePrint( wind_table[w].hTE, (mode == 2), (loop == 1), flnm );
  2805.                         mode = 1;    /* No interaction mode (mode == 2) only valid
  2806.                                     ** for the first printed document. */
  2807.                         wind_close( w );
  2808.                     }
  2809.                 }
  2810.                 else err = errAEEventNotHandled;     /* file pathname too long */
  2811.             }
  2812.             if (gPrintPage)
  2813.             {    eTePrint( nil, false, false, flnm ); /* cleanup */
  2814.                 gPrintPage = 0;                     /* back to normal */
  2815.             }
  2816.         }
  2817.     }
  2818.     err2 = AEDisposeDesc(&theDesc);
  2819.     return(err ? err : err2);
  2820. }
  2821.  
  2822. static Boolean install_aehandler( AEEventClass cl, AEEventID id, ProcPtr p )
  2823. { OSErr    err;
  2824.   err = AEInstallEventHandler( cl, id, p, 0L, false );
  2825.   if (err)
  2826.   { io_err(err);
  2827.     return false;
  2828.   }
  2829.   return true;
  2830. }
  2831.  
  2832. static pascal OSErr    DoAEOpenApplication( AppleEvent *message, AppleEvent *reply, long refcon )
  2833. {
  2834.     return(noErr);
  2835. }
  2836.  
  2837. static pascal OSErr    DoAEOpenDocuments( AppleEvent *message, AppleEvent *reply, long refcon )
  2838. {
  2839.     gCursRgnOK = false;
  2840.     return( OpenDocEventHandler( message, reply, 0 ) ); /* 0 means regular open document */
  2841. }
  2842.  
  2843. #define kTimeOutInTicks (60 * 30)    /* 30 second timeout. */
  2844.  
  2845. static pascal OSErr    DoAEPrintDocuments(AppleEvent *message, AppleEvent *reply, long refcon)
  2846. {
  2847.     short                openMode;
  2848.     ProcessSerialNumber    cpsn, fpsn;
  2849.     Boolean                procsSame;
  2850.  
  2851.     gCursRgnOK = false;
  2852.     openMode = 1;
  2853.     if (!AEInteractWithUser(kTimeOutInTicks, nil, nil))
  2854.         ++openMode;
  2855.  
  2856.     GetCurrentProcess(&cpsn);        /* We may have been moved to the front. */
  2857.     GetFrontProcess(&fpsn);
  2858.     SameProcess(&cpsn, &fpsn, &procsSame);
  2859.     gInBackground = !procsSame;
  2860.  
  2861.     return( OpenDocEventHandler( message, reply, openMode ) );
  2862.         /* openMode is either 1 or 2, depending if user interaction is okay. */
  2863. }
  2864.  
  2865. static pascal OSErr    DoAEQuitApplication( AppleEvent *message, AppleEvent *reply, long refcon )
  2866. {
  2867.     gCursRgnOK = false;
  2868.     wind_quit();
  2869.     return( gDoQuit ? noErr : errAEEventNotHandled );
  2870. }
  2871.  
  2872. /* 1Feb93  e  --  DoScript added */
  2873.  
  2874. static OSErr DoScriptAsFile( FSSpec *theFSS )
  2875. {
  2876.     OSErr err = noErr;
  2877.     short len;
  2878.     char  path[512];
  2879.     
  2880.     if( len = getfullpath( theFSS->vRefNum, theFSS->parID, theFSS->name, path, 511, 1 ) )
  2881.     {
  2882.         put_input( interaction_id, " (load \"", 6, 0, 0, 1 );
  2883.         put_input( interaction_id, path, len, 0, 0, 0 );
  2884.         put_input( interaction_id, "\")", 2, 1, 0, 0 );
  2885.     }
  2886.     else err = errAEEventNotHandled;
  2887.     return err;
  2888. }
  2889.  
  2890. static pascal OSErr DoAEDoScript( AppleEvent *theAEEvent, AppleEvent *theAEReply, long *theRefCon)
  2891. {
  2892.     OSErr        theErr;
  2893.     DescType    typeCode;
  2894.     Size        sizeOfParam, actualSize;
  2895.     char        ourScriptText[256];
  2896.     FSSpec        ourScriptFSpec;
  2897.  
  2898.     /* Get the script to run */ 
  2899.  
  2900.     theErr = AESizeOfParam( theAEEvent, keyDirectObject, &typeCode, &sizeOfParam);
  2901.     if (theErr != noErr)
  2902.     {    /*
  2903.             If we fail here just return the error. We don't need to do any clean up since 
  2904.             we've allocated nothing on the heap yet.  The Apple Event Manager automatically 
  2905.             adds the error number to the reply as keyErrorNumber for non zero handler returns.
  2906.         */
  2907.         return theErr;
  2908.     }
  2909.     else if ((typeCode == typeChar) || (typeCode == typeStyledText) || (typeCode == typeIntlText))
  2910.     {
  2911.         theErr = AEGetParamPtr( theAEEvent, keyDirectObject, typeChar, &typeCode,
  2912.                                  (Ptr)&ourScriptText, sizeof(ourScriptText), &actualSize);
  2913.         if (theErr == noErr)
  2914.             if ( MissedAnyParameters( theAEEvent ) )
  2915.                 theErr = errAEEventNotHandled;
  2916.             else put_input( interaction_id, ourScriptText, actualSize, 1, 0, 1 );
  2917.     }
  2918.     else if (typeCode == typeAlias)
  2919.     {
  2920.         theErr = AEGetParamPtr( theAEEvent, keyDirectObject, typeFSS, &typeCode,
  2921.                                 (Ptr)&ourScriptFSpec, sizeof(ourScriptFSpec), &actualSize);
  2922.         if ( theErr == noErr )
  2923.             if ( MissedAnyParameters( theAEEvent ) )
  2924.                 theErr = errAEEventNotHandled;
  2925.             else theErr = DoScriptAsFile( &ourScriptFSpec );
  2926.     }
  2927.     else theErr = errAEEventNotHandled;
  2928.     return theErr;
  2929. }
  2930.  
  2931.  
  2932. static void init_ae()
  2933. {
  2934.     long    result;
  2935.  
  2936.     gHasAppleEvents = (Gestalt(gestaltAppleEventsAttr, &result) ? false : result != 0);
  2937.  
  2938.     if (gHasAppleEvents)
  2939.          install_aehandler( kCoreEventClass, kAEOpenApplication, (ProcPtr)DoAEOpenApplication )
  2940.       && install_aehandler( kCoreEventClass, kAEOpenDocuments, (ProcPtr)DoAEOpenDocuments )
  2941.       && install_aehandler( kCoreEventClass, kAEPrintDocuments, (ProcPtr)DoAEPrintDocuments )
  2942.       && install_aehandler( kCoreEventClass, kAEQuitApplication, (ProcPtr)DoAEQuitApplication )
  2943.       && install_aehandler( kAEMiscStandards, kAEDoScript, (ProcPtr)DoAEDoScript );
  2944. }
  2945.  
  2946. /*---------------------------------------------------------------------------*/
  2947.  
  2948. #define TICKS_PER_SEC 60
  2949.  
  2950. short *link_ofiles[] =
  2951. { (short *)"      ", (short *)"      ", (short *)"      ", (short *)"      ",
  2952.   (short *)"      ", (short *)"      ", (short *)"      ", (short *)"      ",
  2953.   (short *)"      ", (short *)"      ", (short *)"      ", (short *)"      ",
  2954.   (short *)"      ", (short *)"      ", (short *)"      ", (short *)"      ",
  2955.   (short *)"      ", (short *)"      ", (short *)"      ", (short *)"      ",
  2956.   (short *)"      ", (short *)"      ", (short *)"      ", (short *)"      ",
  2957.   (short *)"      ", (short *)"      ", (short *)"      ", (short *)"      ",
  2958.   (short *)"      ", (short *)"      ", (short *)"      ", (short *)"      ",
  2959.   (short *)"      ", (short *)"      ", (short *)"      ", (short *)"      ",
  2960.   (short *)"      ", (short *)"      ", (short *)"      ", (short *)"      ",
  2961.   (short *)"      ", (short *)"      ", (short *)"      ", (short *)"      ",
  2962.   (short *)"      ", (short *)"      ", (short *)"      ", (short *)"      ",
  2963.   (short *)"      ", (short *)"      ", (short *)"      ", (short *)"      ",
  2964.   (short *)"      ", (short *)"      ", (short *)"      ", (short *)"      ",
  2965.   (short *)"      ", (short *)"      ", (short *)"      ", (short *)"      ",
  2966.   (short *)"      ", (short *)"      ", (short *)"      ", (short *)"      "
  2967. };
  2968.  
  2969. static long sizeof_ofiles[] =
  2970. { 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
  2971.   0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
  2972.   0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
  2973.   0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
  2974. };
  2975.  
  2976. long *link_sizeof_ofiles[] =
  2977. { &sizeof_ofiles[ 0], &sizeof_ofiles[ 1], &sizeof_ofiles[ 2], &sizeof_ofiles[ 3],
  2978.   &sizeof_ofiles[ 4], &sizeof_ofiles[ 5], &sizeof_ofiles[ 6], &sizeof_ofiles[ 7],
  2979.   &sizeof_ofiles[ 8], &sizeof_ofiles[ 9], &sizeof_ofiles[10], &sizeof_ofiles[11],
  2980.   &sizeof_ofiles[12], &sizeof_ofiles[13], &sizeof_ofiles[14], &sizeof_ofiles[15],
  2981.   &sizeof_ofiles[16], &sizeof_ofiles[17], &sizeof_ofiles[18], &sizeof_ofiles[19],
  2982.   &sizeof_ofiles[20], &sizeof_ofiles[21], &sizeof_ofiles[22], &sizeof_ofiles[23],
  2983.   &sizeof_ofiles[24], &sizeof_ofiles[25], &sizeof_ofiles[26], &sizeof_ofiles[27],
  2984.   &sizeof_ofiles[28], &sizeof_ofiles[29], &sizeof_ofiles[30], &sizeof_ofiles[31],
  2985.   &sizeof_ofiles[32], &sizeof_ofiles[33], &sizeof_ofiles[34], &sizeof_ofiles[35],
  2986.   &sizeof_ofiles[36], &sizeof_ofiles[37], &sizeof_ofiles[38], &sizeof_ofiles[39],
  2987.   &sizeof_ofiles[40], &sizeof_ofiles[41], &sizeof_ofiles[42], &sizeof_ofiles[43],
  2988.   &sizeof_ofiles[44], &sizeof_ofiles[45], &sizeof_ofiles[46], &sizeof_ofiles[47],
  2989.   &sizeof_ofiles[48], &sizeof_ofiles[49], &sizeof_ofiles[50], &sizeof_ofiles[51],
  2990.   &sizeof_ofiles[52], &sizeof_ofiles[53], &sizeof_ofiles[54], &sizeof_ofiles[55],
  2991.   &sizeof_ofiles[56], &sizeof_ofiles[57], &sizeof_ofiles[58], &sizeof_ofiles[59],
  2992.   &sizeof_ofiles[60], &sizeof_ofiles[61], &sizeof_ofiles[62], &sizeof_ofiles[63]
  2993. };
  2994.  
  2995. long link_nb_ofiles;
  2996.  
  2997. long prog_len;
  2998.  
  2999.  
  3000. long start_real_time;
  3001.  
  3002.  
  3003. long os_nb_processors()
  3004. { return 1;
  3005. }
  3006.  
  3007.  
  3008. long os_fork_on_processors( n )
  3009. long n;
  3010. { return 0;
  3011. }
  3012.  
  3013.  
  3014. void os_shared_malloc8( len, processor, cont )
  3015. long len;
  3016. long processor;
  3017. void (*cont)();
  3018. { long appl_limit = (((long)&appl_limit) - len - ((long)MAC_STACK_LEN_IN_K)*K) & -4L;
  3019.   long lim = ((long)ApplZone) + ((long)MAC_HEAP_LEN_IN_K)*K;
  3020.   if (lim < ((long)HeapEnd)+1024) lim = ((long)HeapEnd)+1024;
  3021.   if (appl_limit < lim)
  3022.     cont( (long)NULL );
  3023.   else
  3024.   { SetApplLimit( (void *)appl_limit );
  3025.     asm
  3026.     { move.l cont,a0
  3027.       move.l sp,d0
  3028.       move.l d0,d1
  3029.       sub.l  len,d1
  3030.       and.w  #-8,d1
  3031.       move.l d1,sp
  3032.       move.l d0,-(sp)
  3033.       move.l d1,-(sp)
  3034.       jsr    (a0)
  3035.       move.l 4(sp),sp
  3036.     }
  3037.   }
  3038. }
  3039.  
  3040.  
  3041. void os_shared_copy_malloc8( len_share, len_copy, processor, cont )
  3042. long len_share, len_copy;
  3043. long processor;
  3044. void (*cont)();
  3045. { long aligned_len_share = ceiling8( len_share );
  3046.   long len = aligned_len_share + len_copy;
  3047.   long offs = aligned_len_share - len_share;
  3048.   long appl_limit = (((long)&appl_limit) - len - ((long)MAC_STACK_LEN_IN_K)*K) & -4L;
  3049.   long lim = ((long)ApplZone) + ((long)MAC_HEAP_LEN_IN_K)*K;
  3050.   if (lim < ((long)HeapEnd)+1024) lim = ((long)HeapEnd)+1024;
  3051.   if (appl_limit < lim)
  3052.     cont( (long)NULL );
  3053.   else
  3054.   { SetApplLimit( (void *)appl_limit );
  3055.     asm
  3056.     { move.l offs,d2
  3057.       move.l cont,a0
  3058.       move.l sp,d0
  3059.       move.l d0,d1
  3060.       sub.l  len,d1
  3061.       and.w  #-8,d1
  3062.       move.l d1,sp
  3063.       move.l d0,-(sp)
  3064.       add.l  d2,d1
  3065.       move.l d1,-(sp)
  3066.       jsr    (a0)
  3067.       move.l 4(sp),sp
  3068.     }
  3069.   }
  3070. }
  3071.  
  3072.  
  3073. void os_block_copy( src, dst, len )
  3074. char *src, *dst;
  3075. long len;
  3076. { register char *p1 = src, *p2 = dst;
  3077.   register long i = len;
  3078.   while (i > 0) { *(p2++) = *(p1++); i--; }
  3079. }
  3080.  
  3081.  
  3082. void os_flush_caches()
  3083. { if (gHasHWPriv)
  3084.   asm
  3085.   { moveq #1,d0
  3086.     dc.w _HWPriv
  3087.   }
  3088.   else if (os_M68020)
  3089.   {
  3090. #define INSTR_CACHE_LENGTH_IN_K 64
  3091. #define DATA_CACHE_LENGTH_IN_K  8
  3092.  
  3093.     short *start = (short *)pstate->heap_old;
  3094.     short *p = start;
  3095.     long i;
  3096.  
  3097.     for (i=((long)INSTR_CACHE_LENGTH_IN_K)*K/2-1; i>0; i--) *p++ = NOP_OP;
  3098.     *p++ = RTS_OP;
  3099.     for (i=((long)DATA_CACHE_LENGTH_IN_K)*K/2; i>0; i--) *p++ = 0;
  3100.  
  3101.     ((void (*)())start)();
  3102.   }
  3103. }
  3104.  
  3105.  
  3106. void os_flush_writes()
  3107. { if (gHasHWPriv)
  3108.   asm
  3109.   { moveq #1,d0
  3110.     dc.w _HWPriv
  3111.   }
  3112. }
  3113.  
  3114.  
  3115. long os_clock()
  3116. { return TickCount() - start_real_time;
  3117. }
  3118.  
  3119.  
  3120. long os_real_time_clock()
  3121. { return TickCount() - start_real_time;
  3122. }
  3123.  
  3124.  
  3125. long os_clock_to_msec( ticks )
  3126. long ticks;
  3127. { return ticks * 1000 / TICKS_PER_SEC;
  3128. }
  3129.  
  3130.  
  3131. void os_cpu_times( buf )
  3132. long *buf;
  3133. { buf[0] = (TickCount() - start_real_time) * 1000 / TICKS_PER_SEC;
  3134.   buf[1] = 0;
  3135. }
  3136.  
  3137.  
  3138. void os_profil( buff, bufsiz, offset, shift )
  3139. short *buff;
  3140. long bufsiz, offset;
  3141. long shift;
  3142. {
  3143. }
  3144.  
  3145.  
  3146. long os_ticks_to_msec( ticks )
  3147. long ticks;
  3148. { return ticks * 1000 / TICKS_PER_SEC;
  3149. }
  3150.  
  3151.  
  3152. static void fatal_signal( pc, sp, kind )
  3153. long pc, sp;
  3154. char *kind;
  3155. { os_warn( "Fatal signal %s, terminating...\n", (long)kind );
  3156.   os_quit();
  3157. }
  3158.  
  3159.  
  3160. void os_install_trap_handlers( intr_proc, timer_proc, io_proc, fatal_proc )
  3161. void (*intr_proc)();
  3162. void (*timer_proc)();
  3163. void (*io_proc)();
  3164. void (*fatal_proc)();
  3165. { intr_action = intr_proc;
  3166.   timer_action = timer_proc;
  3167.   io_action = io_proc;
  3168.   if (fatal_proc != (void (*)())0)
  3169.     fatal_action = fatal_proc;
  3170.   else
  3171.     fatal_action = fatal_signal;
  3172.  
  3173.   if (loading_dp != NULL)
  3174.   { DisposDialog( loading_dp ); loading_dp = NULL; }
  3175.  
  3176.   MaxApplZone();
  3177.  
  3178.   start_intr_task();
  3179. }
  3180.  
  3181.  
  3182. long os_get_next_event( event )
  3183. EventRecord *event;
  3184. { long result = 0;
  3185.   GrafPtr save;
  3186.   GetPort( &save );
  3187.   if (do_tasks())
  3188.   { if (gHasWNE)
  3189.       { result = (long)( gInBackground ? gWaitTicksBG    /* 22Jul92  e   -- added gMaxSleep */
  3190.                                          : (long )( (gWaitTicksFG > gMaxSleep) ? gMaxSleep
  3191.                                                                                 : gWaitTicksFG) );
  3192.       result = WaitNextEvent( everyEvent, event, result, gCursorRgn );
  3193.     }
  3194.     else
  3195.     { SystemTask();
  3196.       result = GetNextEvent( everyEvent, event );
  3197.     }
  3198.   }
  3199.   SetPort( save );
  3200.   return result;
  3201. }
  3202.  
  3203. long os_handle_event( event )
  3204. EventRecord *event;
  3205. { long result;
  3206.   GrafPtr save;
  3207.   GetPort( &save );
  3208.   result = handle_event( event );
  3209.   SetPort( save );
  3210.   return result;
  3211. }
  3212.  
  3213.  
  3214. char *os_expand_filename( str )
  3215. char *str;
  3216. { return str;
  3217. }
  3218.  
  3219.  
  3220. struct {
  3221.   short type; /* -1 = not in use, 0 = text file, 1 = bin file, 2 = resource, 3 = window */
  3222.   long pos;
  3223.   short refnum;
  3224.   } os_file[MAX_NB_OPEN_FILES];
  3225.  
  3226.  
  3227. short appl_res_file;
  3228. Handle appl_res_map;
  3229.  
  3230. static short read_resource( type, id, n, ptr )  /* Ugly, but works... */
  3231. ResType type;
  3232. short id;
  3233. long n;
  3234. char *ptr;
  3235. { short i, j;
  3236.   long count;
  3237.   struct type_list_rec { short id, name_offs; long data_offs, reserved; };
  3238.   struct map_entry_rec { ResType type; short nb_minus_1; short offs; };
  3239.   struct map_rec
  3240.   { char pad[24]; short type_list_offs, name_list_offs, nb_minus_1;
  3241.     struct map_entry_rec entry[1];
  3242.   };
  3243.   struct map_rec *map = (struct map_rec *)*appl_res_map;
  3244.  
  3245.   i = map->nb_minus_1;
  3246.   while (i >= 0)
  3247.   { if (map->entry[i].type == type)
  3248.     { struct type_list_rec *item = (struct type_list_rec *)(((char *)map)+map->type_list_offs+map->entry[i].offs);
  3249.       j = map->entry[i].nb_minus_1;
  3250.       while (j >= 0)
  3251.       { if (item[j].id == id)
  3252.         { if (SetFPos( appl_res_file, 1, item[j].data_offs + 260L ) != noErr) return 0;
  3253.           count = n;
  3254.           if (FSRead( appl_res_file, &count, ptr ) != noErr) return 0;
  3255.           return 1;
  3256.         }
  3257.         j--;
  3258.       }
  3259.     }
  3260.     i--;
  3261.   }
  3262.   return 0;
  3263. }
  3264.  
  3265.  
  3266. static long file_open( name, for_output )
  3267. char *name;
  3268. long for_output;
  3269. { long i;
  3270.   for (i=0; i<(long)MAX_NB_OPEN_FILES; i++) if (os_file[i].type < 0) break;
  3271.   if (i == (long)MAX_NB_OPEN_FILES) return -1;
  3272.  
  3273.   if (name[0] == 1) /* opening 'gamO' resource */
  3274.   { Handle h;
  3275.     short j = 1, n = 0;
  3276.     EventRecord event;
  3277.     while (os_get_next_event( &event )) os_handle_event( &event );
  3278.     if (for_output) return -1;
  3279.     while ((name[j]>='0') && (name[j]<='9')) n = n*10 + (name[j++] - '0');
  3280.     SetResLoad( FALSE );
  3281.     h = GetResource( 'gamO', n );
  3282.     SetResLoad( TRUE );
  3283.     if (h == NULL) return -1;
  3284.     ReleaseResource( h );
  3285.     os_file[i].type   = 2;
  3286.     os_file[i].refnum = n;
  3287.     os_file[i].pos    = 0;
  3288.   }
  3289.   else if (name[0] == 2) /* opening window */
  3290.   { GrafPtr save;
  3291.     GetPort( &save );
  3292.     os_file[i].refnum = wind_open( name+1, TRUE, TRUE, TRUE );
  3293.     if (os_file[i].refnum < 0) i = -1; else os_file[i].type = 3;
  3294.     SetPort( save );
  3295.   }
  3296.   else
  3297.   { short io;
  3298.     short bin, len;
  3299.     Str255 pname;
  3300.     c_to_p( name, pname );
  3301.     len = (unsigned char)pname[0];
  3302.     bin = ((len >= 2) && (pname[len-1] == '.') && (pname[len] == 'O'));
  3303.     os_file[i].refnum = open_file( pname, (short)for_output, !bin, &io );
  3304.     if (os_file[i].refnum == -1) return -1;
  3305.     os_file[i].type = bin;
  3306.   }
  3307.  
  3308.   return i;
  3309. }
  3310.  
  3311. OS_FILE os_file_open_input( name )
  3312. char *name;
  3313. { return (OS_FILE)file_open( name, 0L );
  3314. }
  3315.  
  3316.  
  3317. OS_FILE os_file_open_output( name )
  3318. char *name;
  3319. { return (OS_FILE)file_open( name, 1L );
  3320. }
  3321.  
  3322.  
  3323. OS_FILE os_file_open_input_output( name )
  3324. char *name;
  3325. { return (OS_FILE)file_open( name, 1L );
  3326. }
  3327.  
  3328.  
  3329. long os_file_length( f )
  3330. OS_FILE f;
  3331. { long len;
  3332.   switch (os_file[f].type)
  3333.   { case 0:
  3334.     case 1:
  3335.     { if (GetEOF( os_file[f].refnum, &len ) != noErr) return -1;
  3336.       break;
  3337.     }
  3338.     case 2:
  3339.     { Handle h;
  3340.       SetResLoad( FALSE );
  3341.       h = GetResource( 'gamO', os_file[f].refnum );
  3342.       SetResLoad( TRUE );
  3343.       if (h == NULL) return -1;
  3344.       len = SizeResource( h );
  3345.       ReleaseResource( h );
  3346.       break;
  3347.     }
  3348.     case 3:
  3349.       return -1;
  3350.     default:
  3351.       return -1;
  3352.   }
  3353.   return len;
  3354. }
  3355.  
  3356.  
  3357. long os_file_read_ready( f )
  3358. OS_FILE f;
  3359. { if (os_file[f].type == 3)
  3360.   { short id = os_file[f].refnum;
  3361.     return ((wind_table[id].len - wind_table[id].pos) > 0);
  3362.   }
  3363.   else
  3364.     return 1;
  3365. }
  3366.  
  3367.  
  3368. long os_file_write_ready( f )
  3369. OS_FILE f;
  3370. { return 1;
  3371. }
  3372.  
  3373.  
  3374. void os_file_block_read( f )
  3375. OS_FILE f;
  3376. {
  3377. }
  3378.  
  3379.  
  3380. void os_file_block_write( f )
  3381. OS_FILE f;
  3382. {
  3383. }
  3384.  
  3385.  
  3386. long os_file_read( f, ptr, n )
  3387. OS_FILE f;
  3388. char *ptr;
  3389. long n;
  3390. { switch (os_file[f].type)
  3391.   { case 0:
  3392.     case 1:
  3393.     { long count = n;
  3394.       FSRead( os_file[f].refnum, &count, ptr );
  3395.       if (os_file[f].type == 0)
  3396.       { register char *p = ptr;
  3397.         register long len = count;
  3398.         while (len-- > 0) { if (*p == '\r') *p = '\n'; p++; }
  3399.       }
  3400.       return count;
  3401.     }
  3402.     case 2:
  3403.       if (read_resource( 'gamO', os_file[f].refnum, n, ptr ))
  3404.         return n;
  3405.       else
  3406.         return -2;
  3407.     case 3:
  3408.     { long result;
  3409.       GrafPtr save;
  3410.       GetPort( &save );
  3411.       result = wind_read( os_file[f].refnum, ptr, n );
  3412.       SetPort( save );
  3413.       return result;
  3414.     }
  3415.     default:
  3416.       return -2;
  3417.   }
  3418. }
  3419.  
  3420.  
  3421. long os_file_write( f, ptr, n )
  3422. OS_FILE f;
  3423. char *ptr;
  3424. long n;
  3425. { switch (os_file[f].type)
  3426.   { case 0:
  3427.     { char buffer[1024+8];
  3428.       register char *p1 = ptr, *p2 = ptr+n;
  3429.       register short len = 0;
  3430.  
  3431.       while (p1<p2)
  3432.       { register char c = *p1++;
  3433.         if (c == '\n') buffer[len++] = '\r'; else buffer[len++] = c;
  3434.         if (len == 1024)
  3435.         { long count = len;
  3436.           FSWrite( os_file[f].refnum, &count, buffer );
  3437.           if (count != len) return (p1-ptr)-len+count;
  3438.           len = 0;
  3439.         }
  3440.       }
  3441.       if (len > 0)
  3442.       { long count = len;
  3443.         FSWrite( os_file[f].refnum, &count, buffer );
  3444.         if (count != len) return (p1-ptr)-len+count;
  3445.       }
  3446.       return (p1-ptr);
  3447.     }
  3448.     case 1:
  3449.     { long count = n;
  3450.       FSWrite( os_file[f].refnum, &count, ptr );
  3451.       return count;
  3452.     }
  3453.     case 2:
  3454.       return -2;
  3455.     case 3:
  3456.     { long result;
  3457.       GrafPtr save;
  3458.       GetPort( &save );
  3459.       result = wind_write( os_file[f].refnum, ptr, n );
  3460.       SetPort( save );
  3461.       return result;
  3462.     }
  3463.     default:
  3464.       return -2;
  3465.   }
  3466. }
  3467.  
  3468.  
  3469. long os_file_close( f )
  3470. OS_FILE f;
  3471. { switch (os_file[f].type)
  3472.   { case 0:
  3473.     case 1:
  3474.     { short io;
  3475.       os_file[f].type = -1;
  3476.       if (close_file( os_file[f].refnum, &io )) return -1;
  3477.       return 0;
  3478.     }
  3479.     case 2:
  3480.     { os_file[f].type = -1;
  3481.       return 0;
  3482.     }
  3483.     case 3:
  3484.     { long result = 0;
  3485.       GrafPtr save;
  3486.       GetPort( &save );
  3487.       if (os_file[f].refnum < 0)
  3488.         result = -1;
  3489.       else
  3490.       { if (os_file[f].refnum != interaction_id) os_file[f].type = -1;
  3491.         wind_close( os_file[f].refnum );
  3492.       }
  3493.       SetPort( save );
  3494.       return result;
  3495.     }
  3496.     default:
  3497.       return -1;
  3498.   }
  3499. }
  3500.  
  3501.  
  3502. extern void os_set_timer_interval( interval )
  3503. long interval;
  3504. { intr_task_interval = (interval * TICKS_PER_SEC + 999) / 1000;
  3505. }
  3506.  
  3507.  
  3508. void os_quit()
  3509. { short i;
  3510.   long ticks;
  3511.   stop_intr_task();
  3512.   for (i=MAX_NB_OPEN_FILES-1; i>0; i--) os_file_close( (OS_FILE)i );
  3513.   if (abnormal_exit)
  3514.   { for (i=0; i<3; i++) { SysBeep(3); Delay( 3, &ticks ); }
  3515.     Delay( 60, &ticks );
  3516.   }
  3517.   wind_end();
  3518.   help_end();
  3519.   ExitToShell();
  3520. }
  3521.  
  3522.  
  3523. static void os_notify_gc_begin_internal()
  3524. { SetCursor( &gc_cursor );
  3525. }
  3526.  
  3527.  
  3528. static void os_notify_gc_end_internal()
  3529. { SetCursor( current_cursor );
  3530. }
  3531.  
  3532.  
  3533. extern void main_gambit();
  3534.  
  3535.  
  3536. #define MAX_ARGS 2
  3537.  
  3538. static short main_internal( argc, argv, envp )
  3539. short argc;
  3540. char *argv[], *envp[];
  3541. { char new_args[256];
  3542.   short new_argc;
  3543.   char *new_argv[MAX_ARGS+1];
  3544.   char *new_envp[] = { "MAC", NULL };
  3545.   SysEnvRec sysenv;
  3546.   Handle h;
  3547.   short i;
  3548.   long ofiles_len;
  3549.   DialogPtr dp;
  3550.   THz heap = ApplicZone();
  3551.  
  3552.   appl_res_file = CurResFile();
  3553.   appl_res_map = TopMapHndl;
  3554.  
  3555.   MoreMasters();
  3556.   MoreMasters();
  3557.   MoreMasters();
  3558.   MoreMasters();
  3559.   CouldAlert( ok_alertID );
  3560.   SetGrowZone( mem_full_err );
  3561.  
  3562.   InitGraf( &thePort );
  3563.   InitFonts();
  3564.   FlushEvents( everyEvent, 0 );
  3565.   InitWindows();
  3566.   InitMenus();
  3567.   TEInit();
  3568.   eTeInit();
  3569.   InitDialogs( 0L );
  3570.   InitCursor();
  3571.  
  3572.   GetVol( new_args, ¤t_volume );
  3573.   if (!getfullpathfromcurrentvolume( CurApName, new_args, 256, 0 ))
  3574.     p_to_c( CurApName, new_args );
  3575.  
  3576.   help_begin( "\pOnline Help", CurApName );
  3577.  
  3578.   wind_begin();
  3579.  
  3580.   new_argc = 0;
  3581.   new_argv[new_argc++] = new_args;
  3582.   new_argv[new_argc] = NULL;
  3583.  
  3584.   for (i=0; i<MAX_NB_OPEN_FILES; i++) os_file[i].type = -1;
  3585.   os_file[0].type   = 3;
  3586.   os_file[0].refnum = wind_open( "Interaction", FALSE, FALSE, TRUE );
  3587.   interaction_id = os_file[0].refnum;
  3588.   if (interaction_id < 0) os_quit();
  3589.   delWindMenuItem( interaction_id );
  3590.  
  3591.   i = 128;
  3592.   ofiles_len = 0;
  3593.   link_nb_ofiles = 0;
  3594.   again:
  3595.   SetResLoad( FALSE ); h = GetResource( 'gamO', i ); SetResLoad( TRUE );
  3596.   if (h != NULL)
  3597.   { short j = 0, d = 10000, n = i;
  3598.     ((char *)link_ofiles[link_nb_ofiles])[j++] = 1;
  3599.     while (d>0)
  3600.     { ((char *)link_ofiles[link_nb_ofiles])[j++] = n/d + '0'; n = n%d; d = d/10; }
  3601.     ((char *)link_ofiles[link_nb_ofiles])[j++] = '\0';
  3602.     ofiles_len += SizeResource(h);
  3603.     ReleaseResource( h );
  3604.     link_nb_ofiles++;
  3605.     i++;
  3606.     goto again;
  3607.   }
  3608.  
  3609.   { short msg, count;
  3610.     CountAppFiles( &msg, &count );
  3611.     if (msg == appOpen)
  3612.     { for (i=1; i<=count; i++)
  3613.       { char path[FILENAME_LEN];
  3614.         AppFile file_info;
  3615.         GetAppFiles( i, &file_info );
  3616.         if ((file_info.fType == 'TEXT') &&
  3617.             getfullpathfromwd( (long)file_info.vRefNum, (StringPtr)file_info.fName, path, FILENAME_LEN, 1 ))
  3618.         { if (edit( path, 0L, 0L ) < 0) break;
  3619.           current_volume = file_info.vRefNum;
  3620.           ClrAppFiles( i );
  3621.         }
  3622.         else if (file_info.fType == 'gamO')
  3623.         { char *ofile;
  3624.           long count;
  3625.           short refnum;
  3626.           short io;
  3627.           io = FSOpen( file_info.fName, file_info.vRefNum, &refnum );
  3628.           if (io != noErr) { io_err( io ); return 0; }
  3629.           io = GetEOF( refnum, &prog_len );
  3630.           if (io != noErr) { FSClose( refnum ); io_err( io ); return 0; }
  3631.           ofile = NewPtr( prog_len );
  3632.           if (ofile == NULL) return 0;
  3633.           count = prog_len;
  3634.           io = FSRead( refnum, &count, ofile );
  3635.           if ((io != noErr) || (count != prog_len)) { FSClose( refnum ); io_err( io ); return 0; }
  3636.           FSClose( refnum );
  3637.           link_ofiles[link_nb_ofiles-1] = (short *)ofile;
  3638.           link_sizeof_ofiles[link_nb_ofiles-1] = &prog_len;
  3639.           ofiles_len += prog_len;
  3640.           current_volume = file_info.vRefNum;
  3641.           ClrAppFiles( i );
  3642.           break;
  3643.         }
  3644.       }
  3645.     }
  3646.   }
  3647.  
  3648.   SysEnvirons( curSysEnvVers, &sysenv );
  3649.   os_M68020 = (sysenv.processor >= env68020);
  3650.   os_M68881 = sysenv.hasFPU;
  3651.   os_stdin  = 0;
  3652.   os_stdout = 0;
  3653.   os_stderr = 0;
  3654.  
  3655.   start_real_time = TickCount();
  3656.  
  3657.   SetCursor( current_cursor );
  3658.  
  3659.   ParamText1( "\p" );
  3660.   loading_dp = GetNewDialog( loading_dlogID, 0L, (WindowPtr)-1L );
  3661.   if (loading_dp != NULL) DrawDialog( loading_dp );
  3662.  
  3663.   partition_init( ofiles_len );
  3664.  
  3665.   main_gambit( new_argc, new_argv, new_envp );
  3666.  
  3667.   os_quit();
  3668.  
  3669.   return 0;
  3670. }
  3671.  
  3672. /* 6Jul92  e */
  3673. #ifdef THINK_C
  3674. #include "os.c"
  3675. #endif
  3676.  
  3677. /*--------------------------------------------------------------------------*/
  3678.