home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / old / misc / ck5a189 / ckmini.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  23KB  |  843 lines

  1. /* $Id: ckmini.c,v 1.8 91/12/27 21:44:49 fdc Exp Locker: fdc $
  2.  * $Source: /uw/mackermit/RCS/ckmini.c,v $
  3.  *------------------------------------------------------------------
  4.  * $Log:    ckmini.c,v $
  5.  * Revision 1.8  91/12/27  21:44:49  fdc
  6.  * Change fatal to macfatal, make all lines less than 80 chars wide.
  7.  * 
  8.  * Revision 1.7  91/12/15  23:17:32  rick
  9.  * ut9
  10.  * 
  11.  * Revision 1.6  91/10/13  13:43:24  rick
  12.  * UT(7)
  13.  * 
  14.  * Revision 1.5  91/10/01  12:16:30  rick
  15.  * UT(5)
  16.  * 
  17.  * Revision 1.4  91/09/25  12:17:05  rick
  18.  * Command window in TE. Multiple vt100 windows for command window.
  19.  * 
  20.  * Revision 1.3  91/09/12  21:50:43  rick
  21.  * UT(3). Install on watsun
  22.  * 
  23.  * Revision 1.2  1991/09/10  22:21:43  rick
  24.  * Update to UTexas(2)
  25.  *
  26.  * Revision 1.1  1991/09/10  19:17:54  rick
  27.  * Initial revision
  28.  *
  29.  *------------------------------------------------------------------
  30.  * $Endlog$
  31.  */
  32.  
  33. /*
  34.  John A. Oberschelp for Emory University -- vt102 printer support 22 May 1989
  35.  */
  36. /* Emory contact is Peter W. Day, ospwd@emoryu1.cc.emory.edu */ 
  37. /* Paul Placeway 4/89    - fixed up things for profiling, minor changes */
  38. /* Paul Placeway 3/28/88 - created by moving a bunch of junk out of ckmusr.c */
  39. /*
  40.  * file ckmini.c
  41.  *
  42.  * Module of mackermit containing code for the menus and other MacIntosh
  43.  * things.
  44.  *
  45.  */
  46.  
  47. /*
  48.   Copyright (C) 1985, 1992, Trustees of Columbia University in the City of New
  49.   York.  Permission is granted to any individual or institution to use this
  50.   software as long as it is not sold for profit.  This copyright notice must be
  51.   retained.  This software may not be included in commercial products without
  52.   written permission of Columbia University.
  53. */
  54.  
  55. #include "ckcdeb.h"
  56. #include "ckcker.h"
  57. #include "ckmdef.h"        /* General Mac defs */
  58. #include "ckmres.h"        /* Mac resource equates */
  59. #include "ckmasm.h"        /* new A8 and A9 traps */
  60. #include "ckuusr.h"
  61. #include "ckcasc.h"
  62. #include "ckmcon.h"
  63. #include "ckmwin.h"
  64. #include "ckmptp.h"        /* ckm* Prototypes */
  65.  
  66. extern Handle hPrintBuffer;
  67. extern long lPrintBufferChars = 0L;
  68.  
  69. /* Global Variables */
  70.  
  71. MenuHandle menus[LAST_MENU + 1];/* handle on our menus */
  72.  
  73. short innum;            /* Input driver refnum */
  74. short outnum;            /* Output driver refnum */
  75. int protocmd;            /* protocol file cmd, or -1 for */
  76.  /* remote cmds, or 0 if not in */
  77.  /* protocol */
  78.  
  79. Cursor *watchcurs;        /* the watch cursor */
  80. Cursor *textcurs, *normcurs;
  81.  
  82. int quit = FALSE;
  83.  
  84. Boolean mcmdactive = TRUE;    /* Enable menu command keys */
  85. Boolean fkeysactive = TRUE;    /* Enable FKEYs */
  86.  
  87. #ifdef notdef
  88. WindowPtr terminalWindow;    /* the terminal window */
  89. #endif
  90. WindowPtr commandWindow;        /* the command window */
  91.  
  92. extern int dfloc;                       /* Default location: remote/local */
  93. extern int dfprty;                      /* Default parity */
  94. extern int dfflow;                      /* Default flow control */
  95.  
  96. extern int local;            /* running local or remote? */
  97.  
  98. extern long mf_sleep_time;    /* this is the number of (60Hz) ticks to
  99.                  * sleep before getting a nullEvent (to
  100.                  * flash our cursor) (and input chars from
  101.                  * the serial line)
  102.                  */
  103.  
  104. extern Boolean have_128roms;    /* actually, a Mac + or better */
  105. extern struct cmdw *cmdwl;
  106.  
  107. /* local variables */
  108.  
  109. Boolean have_fourone = FALSE;    /* true if we are running system 4.1 or
  110.                  * better */
  111. Boolean have_ctrl_key = FALSE; /* true if we have an ADB (SE or II) keyboard */
  112.  
  113. Boolean usingRAMdriver = FALSE;    /* true if using the RAM serial driver */
  114.  
  115. short takeFRefNum;        /* file reference number of the take file */
  116.  
  117. /****************************************************************************/
  118. /****************************************************************************/
  119. VOID prescan()
  120. {
  121. }
  122.  
  123. VOID cmdini ()
  124. {
  125.     short vRefNum;
  126.     Str255 volName;
  127.     OSErr err;
  128.  
  129.     GetVol (&volName, &vRefNum);
  130.     err = FSOpen ("\pKermit Takefile", vRefNum, &takeFRefNum);
  131.     /* try to open the take file */
  132.     if (err == noErr) {
  133.     tlevel = 1;
  134.     getch ();        /* get first character of take file */
  135.     gettoken ();
  136.     };
  137. }                /* cmdini */
  138.  
  139.  
  140.  
  141. /****************************************************************************/
  142. /* return uppercase for a letter */
  143. /****************************************************************************/
  144. char
  145. CAP (c)
  146. char c;
  147. {
  148.     if (islower (c))
  149.     return (toupper (c));
  150.     else
  151.     return (c);
  152. }                /* CAP */
  153.  
  154.  
  155. #define TAK_SERV    1
  156. #define TAK_QUIT    2
  157. #define TAK_SEND    3
  158. #define TAK_RECV    4
  159. #define TAK_GET        5
  160. #define TAK_INP        6
  161. #define TAK_OUT        7
  162. #define TAK_UNK 255
  163.  
  164. char *takecmdtab[] = {
  165.     "SERVER",
  166.     "QUIT",
  167.     "SEND",
  168.     "RECEIVE",
  169.     "GET",
  170.     "INPUT",
  171.     "OUTPUT"
  172. };
  173.  
  174. int taketoktab[] = {
  175.     TAK_SERV,
  176.     TAK_QUIT,
  177.     TAK_SEND,
  178.     TAK_RECV,
  179.     TAK_GET,
  180.     TAK_INP,
  181.     TAK_OUT
  182. };
  183.  
  184. #define NUMOFCMDS (sizeof (taketoktab)/sizeof(int))
  185.  
  186. /****************************************************************************/
  187. /* return the token number for a specific take command */
  188. /****************************************************************************/
  189. int
  190. findcmd (cmd)
  191. char *cmd;
  192. {
  193.     int k;
  194.  
  195.     for (k = 0; k < NUMOFCMDS; k++)
  196.     if (strcmp (takecmdtab[k], cmd) == 0)
  197.         return (taketoktab[k]);    /* and return ID */
  198.     return (TAK_UNK);        /* else unknown */
  199. }                /* findcmd */
  200.  
  201.  
  202.  
  203. char ch;
  204.  
  205. /****************************************************************************/
  206. /****************************************************************************/
  207. getch ()
  208. {
  209.     long count;
  210.  
  211.     count = 1;
  212.     if (FSRead (takeFRefNum, &count, &ch) != noErr)
  213.     ch = '\0';
  214. }                /* getch */
  215.  
  216.  
  217.  
  218. #define TOK_CMD        1    /* command id in 'theCmd' */
  219. #define TOK_STR        2    /* string in 'theString' */
  220. #define TOK_NUM        3    /* number in 'theNumber' */
  221. #define TOK_ID        4    /* identifier token */
  222. #define TOK_EOF        5    /* end of file token */
  223. #define TOK_SLS        6    /* ',' */
  224. #define TOK_DOT        7    /* '.' */
  225. #define TOK_UNK    255        /* unknown token */
  226.  
  227. int token;
  228. int theCmd;
  229. char theString[256];
  230. long theNumber;
  231.  
  232. /****************************************************************************/
  233. /****************************************************************************/
  234. gettoken ()
  235. {
  236.     int cmdid;
  237.     char *c;
  238.     char buffer[30];
  239.     Boolean comment;
  240.  
  241.     while ((ch <= ' ') || (ch == '/')) {
  242.     if (ch <= ' ')        /* skip all characters <= blank */
  243.         if (ch == '\0') {    /* except eof character */
  244.         token = TOK_EOF;
  245.         return;
  246.         } else
  247.         getch ();
  248.  
  249.     if (ch == '/') {    /* slash / comment */
  250.         getch ();
  251.  
  252.         if (ch != '*') {
  253.         token = TOK_SLS;
  254.         return;
  255.         }
  256.         getch ();
  257.         comment = TRUE;
  258.         while (comment) {
  259.         if (ch == '\0') {
  260.             token = TOK_EOF;
  261.             return;
  262.         }
  263.         if (ch == '*') {
  264.             getch ();
  265.             if (ch == '/') {
  266.             comment = FALSE;
  267.             getch ();
  268.             }
  269.         } else
  270.             getch ();
  271.         }            /* while (comment) */
  272.  
  273.     }            /* if (ch == '/') */
  274.     }                /* while ((ch <= ' ') || (ch == '/')) */
  275.  
  276.     if (ch == '"') {        /* string */
  277.     token = TOK_STR;
  278.     c = theString;
  279.     getch ();
  280.  
  281.     while (TRUE) {
  282.         if (ch == '"')
  283.         ch = '\0';
  284.  
  285.         if (ch == '\\') {
  286.         getch ();
  287.         if (ch == 'n')
  288.             ch = '\n';
  289.         else if (ch == 'b')
  290.             ch = '\b';
  291.         else if (ch == 't')
  292.             ch = '\t';
  293.         }
  294.         if ((c - theString) < (sizeof (theString) - 1))
  295.         *c++ = ch;
  296.  
  297.         if (ch == '\0') {
  298.         *c = '\0';
  299.         getch ();
  300.         return;
  301.         } else
  302.         getch ();
  303.     }
  304.     }                /* TOK_STR */
  305.     if ((ch >= '0') && (ch <= '9')) {    /* number */
  306.     token = TOK_NUM;
  307.     theNumber = 0;
  308.     getch ();
  309.     return;
  310.     }                /* TOK_NUM */
  311.     ch = CAP (ch);
  312.     if ((ch >= 'A') && (ch <= 'Z')) {    /* command / identifier */
  313.     c = buffer;
  314.     while ((((ch >= 'A') && (ch <= 'Z')) ||    /* get the whole string */
  315.         ((ch >= '0') && (ch <= '9'))) &&
  316.            ((c - buffer) < (sizeof (buffer) - 1))) {
  317.         *c++ = ch;
  318.         getch ();
  319.         ch = CAP (ch);
  320.     }
  321.     *c = '\0';        /* end the buffer with \0 */
  322.  
  323.     cmdid = findcmd (buffer);    /* check for command */
  324.     if (cmdid != TAK_UNK) {
  325.         token = TOK_CMD;
  326.         theCmd = cmdid;    /* return the command id if true */
  327.     } else
  328.         token = TOK_ID;    /* return the identifier id if true */
  329.  
  330.     return;
  331.     }                /* TOK_CMD / TOK_ID */
  332.     switch (ch) {
  333.       case '.':        /* dot */
  334.     token = TOK_DOT;
  335.     break;
  336.  
  337.       default:            /* unknown character */
  338.     token = TOK_UNK;
  339.     }
  340. }                /* gettoken */
  341.  
  342.  
  343.  
  344. /****************************************************************************/
  345. /****************************************************************************/
  346. char
  347. nextcmd ()
  348. {
  349.     if (token == TOK_CMD) {
  350.     switch (theCmd) {
  351.         case TAK_SERV:
  352.         displa = TRUE;
  353.         scrcreate ();    /* create the packet display dialog */
  354.         protocmd = SERV_REMO;    /* run the mac as server */
  355.         gettoken ();
  356.         return ('x');
  357.  
  358.       case TAK_QUIT:
  359.         quit = TRUE;
  360.         FSClose (takeFRefNum);
  361.         return (0);
  362.  
  363.       case TAK_SEND:    /* send a file: local, remote files */
  364.         gettoken ();
  365.         if (token != TOK_STR)
  366.         return (0);
  367.  
  368.         strcpy (filargs.fillcl, theString);    /* file to send */
  369.  
  370.         gettoken ();
  371.         if (token == TOK_STR) {    /* send as */
  372.         strcpy (filargs.filrem, theString);
  373.         gettoken ();
  374.         } else
  375.         zltor (filargs.fillcl, filargs.filrem);
  376.  
  377.         cmarg = filargs.fillcl;
  378.         cmarg2 = filargs.filrem;
  379.  
  380.         nfils = -1;        /* Use cmarg, not cmlist */
  381.         protocmd = SEND_REMO;
  382.         scrcreate ();
  383.         return ('s');    /* return with send state */
  384.  
  385.       case TAK_RECV:
  386.         initfilrecv ();    /* init recv flags */
  387.         protocmd = RECV_REMO;
  388.         scrcreate ();
  389.         gettoken ();
  390.         return ('v');    /* return with recv state */
  391.  
  392.       case TAK_GET:    /* Get from server */
  393.         gettoken ();
  394.         if (token != TOK_STR)
  395.         return (0);
  396.  
  397.         strcpy (cmarg, theString);
  398.         protocmd = GETS_REMO;
  399.         scrcreate ();
  400.         gettoken ();
  401.         return ('r');
  402.  
  403.       default:
  404.         return (0);
  405.     }            /* switch (theCmd) */
  406.  
  407.     } else {
  408.     tlevel = -1;        /* no more commands */
  409.     FSClose (takeFRefNum);
  410.     return (0);
  411.     }
  412.  
  413. }                /* nextcmd */
  414.  
  415.  
  416. /****************************************************************************/
  417. /* init_menus - create the menu bar. */
  418. /****************************************************************************/
  419. setup_menus ()
  420. {
  421.     int i;
  422.     static int menus_are_drawn = 0;
  423.     THz curZone;
  424.  
  425.     if (!menus_are_drawn) {    /* if the first time through */
  426.     /*
  427.      * PWP: we do command keys by default ONLY on a keyboard that has
  428.      * a CTRL key
  429.      */
  430.     mcmdactive = have_ctrl_key;
  431.     
  432.     /* setup Apple menu */
  433.     if ((menus[APPL_MENU] = GetMenu (APPL_MENU)) == NIL)
  434.         printerr("Couldn't get MENU", APPL_MENU);
  435.     else
  436.         AddResMenu (menus[APPL_MENU], 'DRVR');
  437.         
  438.     /* setup Font menu */
  439.     if ((menus[FONT_MENU] = GetMenu (FONT_MENU)) == NIL)
  440.         printerr("Couldn't get MENU", FONT_MENU);
  441.     else
  442.         AddResMenu (menus[FONT_MENU], 'FONT');
  443.     
  444.     } else {
  445.         ClearMenuBar();        /* remove all menus from the list */
  446.     }
  447.     
  448.     InsertMenu (menus[APPL_MENU], 0);    /* Put Apple Menu on menu line */
  449.  
  450.     for (i = MIN_MENU; i <= LAST_MENU; i++) {    /* For all menus */
  451.         if (menus_are_drawn && menus[i]) {
  452.         curZone = GetZone();       /* as per John Norstad's Disinfectant */
  453.         SetZone(HandleZone((Handle) menus[i]));    /* "Toolbox Gotchas" */
  454.         ReleaseResource((Handle) menus[i]);        /* free old resource */
  455.         SetZone(curZone);
  456.     }
  457.         if (mcmdactive) {
  458.         if ((menus[i] = GetMenu (i)) == NIL)
  459.                 /* Fetch it from resource file */
  460.         printerr("Couldn't get MENU", i);
  461.     } else {
  462.         if ((menus[i] = GetMenu (i+32)) == NIL) {
  463.                 /* try to get w/o clover */
  464.             printerr("Couldn't get MENU", i+32);
  465.         menus[i] = GetMenu (i);    /* Fetch normal from resource file */
  466.         }
  467.     }
  468.     if (i <= MAX_MENU)
  469.         InsertMenu (menus[i], 0);    /* Put it on menu line */
  470.     else
  471.         InsertMenu (menus[i], -1);    /* make it a submenu */
  472.     }
  473.     
  474.     /* add the font menu too */
  475.     InsertMenu (menus[FONT_MENU], REMO_MENU);
  476.  
  477.     /* Disable various items */
  478.     DisableItem(menus[FILE_MENU], PBUF_FIL);
  479.     DisableItem(menus[FILE_MENU], PSTAT_FIL);
  480.     DisableItem(menus[FILE_MENU], PDISC_FIL);
  481.     DisableItem(menus[FILE_MENU], TAKEW_FIL);
  482.     DisableItem(menus[FILE_MENU], POPEN_FIL);
  483.  
  484.     /* setup_font_menu(); -- we do this AFTER we have set up the terminal */
  485.  
  486.     DrawMenuBar ();        /* Finish up by displaying the bar */
  487.  
  488.     CheckItem (menus[SETG_MENU], MCDM_SETG, mcmdactive);
  489.     menus_are_drawn = 1;
  490. }                /* setup_menus */
  491.  
  492. Boolean
  493. IsWNEImplemented ()
  494. {
  495.     int err;
  496.     SysEnvRec theWorld;
  497. #define FOURONEVERSION    1040    /* version == (short) (4.1 * 256.) */
  498.  
  499.     /*
  500.      * (from Mac Tech Note #158) We need ot call SysEnvirons to make sure
  501.      * that WaitNextEvent is implemented.  If we are running on 64K ROMs, and
  502.      * RAM HFS is running (trap 0xA060), then GetTrapAddress(0x60) will
  503.      * return a value different from the unimplemented trap since trap 60 is
  504.      * implemented for HFS and the 64K ROM version of GetTrapAddress doesn't
  505.      * differentiate between OS and Tool traps.
  506.      */
  507.  
  508.     /* These are both toolbox traps, hence the 1 */
  509.     err = SysEnvirons (1, &theWorld);    /* we have the glue, so machineType
  510.                      * will allways be filled in */
  511.  
  512.     /* to see if we can use the script manager */
  513.     have_fourone = (theWorld.systemVersion >= FOURONEVERSION);
  514.  
  515.     /* let's hope that Apple gets sane about the CTRL key... */
  516.     have_ctrl_key = (theWorld.keyBoardType == envAExtendKbd) ||
  517.     (theWorld.keyBoardType == envStandADBKbd);
  518.  
  519.     have_128roms = !((theWorld.machineType == envMac) ||
  520.                  (theWorld.machineType == envXL) ||
  521.                  (theWorld.machineType == envMachUnknown));
  522.     
  523.     /* is WNE implemented? */
  524.     if (theWorld.machineType < 0)
  525.     return FALSE;        /* we don't know what kind of Mac this is. */
  526.  
  527.     /* "..., 1" 'cause these are tooltraps: */
  528.     /* 6.0.2 bug fixed by RWR <CES00661%UDACSVM.BITNET@cunyvm.cuny.edu> */
  529.     
  530.     if ((NGetTrapAddress (num_WaitNextEvent, 1) !=
  531.      NGetTrapAddress (num_UnknownTrap, 1)) &&    /* RWR  */
  532.     (NGetTrapAddress (num_JugglDispatch, 1) !=    /* RWR  */
  533.      NGetTrapAddress (num_UnknownTrap, 1)))        /* RWR  */
  534.     return TRUE;
  535.  
  536.     return FALSE;
  537. }
  538.  
  539. extern hmacrodefs macroshdl;    /* handle to the macro table */
  540. extern modrec modtable[NUMOFMODS];    /* modifier records */
  541. extern RgnHandle dummyRgn;    /* dummy region for ckmcon */
  542. extern char **myclip_h;        /* internal clipboard */
  543. extern int myclip_size;        /* size of above */
  544. extern int my_scrapcount;        /* value of pss->scrapCount when we cut */
  545. extern long MyCaretTime;    /* ticks between flashes for cursor */
  546. extern int deblog;        /* should we do debugging? */
  547.  
  548. /****************************************************************************/
  549. /* mac_init - Initialize the macintosh and any window, menu, or other */
  550. /* resources we will be using. */
  551. /****************************************************************************/
  552. mac_init ()
  553. {
  554.     int err;
  555.     int i;
  556.     CursHandle cursh;
  557.  
  558.     deblog = 0;            /* don't do deugging to start */
  559.     
  560. /*    DebugStr("\p;hs");    */
  561.  
  562.     MaxApplZone ();        /* Make appl. heap big as can be */
  563.  
  564.     /*
  565.      * Last time I looked, Kermit was using 131 relocatable blocks.
  566.      * MoreMasters() allocates 64, and we want to add a bit of slop
  567.      * to our guess (say 1.25 * measured).  Then divide this number by
  568.      * 64 to see how many times to call MoreMasters (3, in this case)
  569.      */
  570.     MoreMasters ();        /* Create 64 master pointers */
  571.     MoreMasters ();        /* Create 64 more master pointers */
  572.     MoreMasters ();        /* Create 64 more master pointers */
  573.     err = MemError ();
  574.     if (err != noErr)
  575.     printerr ("Unable to create masters", err);
  576.  
  577.     InitGraf (&qd.thePort);    /* Init the graf port */
  578.     InitFonts ();        /* The fonts */
  579.     FlushEvents (everyEvent, 0);    /* clear click ahead */
  580.     InitWindows ();        /* The windows */
  581.  
  582. /* Debugger(); */
  583.     /*
  584.      * PWP: we MUST call IsWNEImplemented() BEFORE using have_fourone, or
  585.      * have_ctrl_key (in InitMenus() )
  586.      */
  587.     have_multifinder = IsWNEImplemented ();    /* See Above. */
  588.  
  589.     InitMenus ();
  590.     TEInit ();            /* Init text edit */
  591.     InitDialogs ((ResumeProcPtr) NILPROC);    /* The dialog manager */
  592.     InitCursor ();        /* start with a nice cursor */
  593.     SetEventMask (everyEvent - keyUpMask);
  594.  
  595.     dummyRgn = NewRgn ();
  596.  
  597.     normcurs = &qd.arrow;
  598.     if ((cursh = GetCursor (watchCursor)) != NIL) {
  599.     HLock((Handle) cursh);
  600.     watchcurs = *cursh;        /* the waiting cursor */
  601.     } else {
  602.         watchcurs = &qd.arrow;
  603.     }
  604.     if ((cursh = GetCursor (iBeamCursor)) != NIL) {
  605.     HLock((Handle) cursh);
  606.     textcurs = *cursh;        /* the text body cursor */
  607.     } else {
  608.         textcurs = &qd.arrow;
  609.     }
  610.  
  611.     MyCaretTime = GetCaretTime();
  612.     if (MyCaretTime < 3 || MyCaretTime > 300)
  613.     MyCaretTime = 20L;
  614.     
  615.     setup_menus ();        /* build our menus */
  616.     enable_fkeys(TRUE);        /* enable FKEYs */
  617.  
  618.     inittiobufs();        /* init terminal I/O buffers */
  619.  
  620.     initfilset ();        /* init file settings */
  621.  
  622.     port_open(-6);        /* open Modem port by default */
  623.     
  624.     parity = DEFPAR;
  625.     if (!setserial (innum, outnum, DSPEED, DEFPAR))    /* set speed parity */
  626.     macfatal("Couldn't set serial port to default speed",0);
  627.     (void) sershake(flow);
  628.     
  629.     ttres();        /* (PWP) set up flow control for interactive use */
  630.     
  631.     ttermw = consetup(TERMBOXID);    /* initialize terminal window */
  632.     ctermw = consetup(LCMDBOXID);    /* initialize command window */
  633.     rcmdw = initcmdw(RCMDBOXID, RCMDVSCROLL, RCMDHSCROLL);
  634.  
  635.     displa = TRUE;            /* Make everything goes to screen */
  636.  
  637.     /* init (internal) clipboard */
  638.     myclip_h = (char **) NewHandle (32);
  639.     myclip_size = 0;
  640.     my_scrapcount = -1;
  641.     
  642.     /* init the macro table */
  643.     macroshdl = (hmacrodefs) NewHandle (MacroBaseSize);
  644.     (*macroshdl)->numOfMacros = 0;
  645.  
  646.     /* clear the prefix strings */
  647.     for (i = 0; i < NUMOFMODS; i++)
  648.     modtable[i].prefix[0] = '\0';
  649.  
  650.     loadkset ();        /* PWP: get our defaults for these */
  651.     loadmset ();
  652.  
  653.     /* Frank changed main() to call init and then set flow, parity, etc.
  654.        so we make sure they will be set right (again) after we return. */
  655. /***    dfloc = local;                  /* And whether it's local or remote. */
  656.     dfprty = parity;                    /* Set initial parity, */
  657.     dfflow = flow;                      /* and flow control. */
  658.  
  659.     /* we have to set up the font menu AFTER we have inited the terminal
  660.        and loaded any init file */
  661.     setup_font_menu();
  662.  
  663. #ifdef PROFILE
  664.     if (!INITPERF(&ThePGlobals, 1, 8, FALSE, TRUE, "\pCODE", 0, "\pROMII",
  665.                   FALSE, 0, 0, 0))
  666.     macfatal("Could not start profiling", 0);
  667.     (void) PerfControl(ThePGlobals, TRUE);
  668. #endif /* PROFILE */
  669. }                /* mac_init */
  670.  
  671. /****************************************************************************/
  672. /* mac_post_load_init - Finish initializing anything that needs to be done */
  673. /* after loading whatever init file the user clicked on */
  674. /****************************************************************************/
  675. mac_post_load_init()
  676. {
  677.     extern int startconnected;
  678.  
  679.     kShowWindow(ttermw);        /* show terminal win */
  680.     if (startconnected)
  681.     kSelectWindow(ttermw->window);    /* and make sure it's in front */
  682.     else {
  683.     kShowWindow(ctermw);
  684.     kSelectWindow(ctermw->window);
  685.     /*
  686.      * Normally we just let the activate event set in_front. 
  687.      * For some reason this is not working in this startup case
  688.      * so go ahead and set it until someone figures out the bug.
  689.      * Otherwise, neither window ends up in_front.
  690.      */
  691.     ctermw->in_front = TRUE;
  692.     }
  693. }
  694.  
  695. /****************************************************************************/
  696. /* syscleanup() - called before leaving this program to clean up any */
  697. /*                     dangling Mac stuff. */
  698. /* Called by doexit, transfer and zkself. */
  699. /****************************************************************************/
  700. syscleanup ()
  701. {
  702.     enable_fkeys(TRUE);        /* re-enabled screen dumping */
  703.     
  704.     FutzOptKey(0);        /* reset old Mac key map */
  705.     
  706. #ifdef PROFILE
  707.     if (PERFDUMP(ThePGlobals, "\pPerform.out", true, 80))
  708.     macfatal("Could not dump profiling output", 0);
  709.     (void) TermPerf (ThePGlobals);
  710. #endif /* PROFILE */
  711.  
  712.     port_close();        /* close the REAL serial port down */
  713.  
  714.     DisposeMacros ();        /* dipose all macro strings */
  715.     if (macroshdl)
  716.     DisposHandle ((Handle) macroshdl);    /* release the macro table */
  717.     macroshdl = 0L;
  718.     
  719.     if (myclip_h)
  720.     DisposHandle(myclip_h);
  721.     myclip_h = 0L;
  722.  
  723.     if (hPrintBuffer)
  724.     DisposHandle(hPrintBuffer);
  725.     hPrintBuffer = 0L;
  726.     lPrintBufferChars = 0L;
  727.  
  728.     DisposeRgn (dummyRgn);
  729. }                /* syscleanup */
  730.  
  731. #ifndef NOICP
  732. #ifndef NOSPL
  733.     extern struct mtab *mactab;        /* For ON_EXIT macro. */
  734.     extern int nmac;
  735. #endif /* NOSPL */
  736. #endif /* NOICP */
  737.  
  738. VOID
  739. doclean() {                /* General cleanup upon exit */
  740.     struct cmdw *cmdw;
  741.  
  742.     if (pktlog) {
  743.     zclose(ZPFILE);
  744.     pktlog = 0;
  745.     }
  746.     if (seslog) {
  747.     zclose(ZSFILE);
  748.     seslog = 0;
  749.     }
  750. #ifdef TLOG
  751.     if (tralog) {
  752.     tlog(F100,"Transaction Log Closed","",0L);
  753.     zclose(ZTFILE);
  754.     tralog = 0;
  755.     }
  756. #endif /* TLOG */
  757.  
  758. #ifndef NOICP
  759. #ifndef NOSPL
  760.     zclose(ZRFILE);            /* READ and WRITE files, if any. */
  761.     zclose(ZWFILE);
  762. /*
  763.   If a macro named "on_exit" is defined, execute it.  Also remove it from the
  764.   macro table, in case its definition includes an EXIT or QUIT command, which
  765.   would cause much recursion and would prevent the program from ever actually
  766.   EXITing.
  767. */
  768.     if (nmac) {                            /* Any macros defined? */
  769.     int k;                            /* Yes */
  770.     k = mlook(mactab,"on_exit",nmac); /* Look up "on_exit" */
  771.     if (k >= 0) {                    /* If found, */
  772.         *(mactab[k].kwd) = NUL;        /* poke its name from the table, */
  773.         if (dodo(k,"") > -1)        /* set it up, */
  774.         parser(1);                /* and execute it */
  775.     }
  776.     }
  777. #endif /* NOSPL */
  778. #endif /* NOICP */
  779.  
  780. /*
  781.   Put console terminal back to normal.  This is done here because the
  782.   ON_EXIT macro calls the parser, which meddles with console terminal modes.
  783. */
  784.     ttclos();                /* Close external line, if any */    
  785.  
  786.     /*
  787.      * Check about saving any modified buffers.
  788.      */
  789.     for (cmdw = cmdwl; cmdw; cmdw = cmdw->next)
  790.     if (cmdw->flags & CMDWF_MODIFIED)
  791.         checksave(cmdw);
  792.  
  793.     /*
  794.      * We don't need to do anything with the terminal name or resetting
  795.      * the console here, since there really is no "console".
  796.      */
  797.  
  798. #ifdef DEBUG
  799.     /* Close the debug log as the LAST thing */
  800.     
  801.     if (deblog) {
  802.     debug(F100,"Debug Log Closed","",0);
  803.     zclose(ZDFILE);
  804.     deblog = 0;
  805.     }
  806. #endif /* DEBUG */
  807.  
  808.     syscleanup();            /* System-dependent cleanup, last */
  809. }
  810.  
  811. /****************************************************************************/
  812. /* doexit(status) - exit to shell.  Perhaps we should check for abnormal */
  813. /*                      status codes... */
  814. /*
  815.  * First arg is general, system-independent symbol: GOOD_EXIT or BAD_EXIT.
  816.  * If second arg is -1, take 1st arg literally.
  817.  * If second arg is not -1, work it into the exit code.
  818.  */
  819. /****************************************************************************/
  820. VOID
  821. doexit(exitstat,what)
  822. int exitstat, what;
  823. {
  824.     debug(F101,"doexit exitstat","",exitstat);
  825.     debug(F101,"doexit what","",what);
  826.     
  827.     doclean();            /* First, clean up everything */
  828.     
  829.     /* the Mac doesn't really have a good/bad exit value, so just exit */
  830.     
  831.     ExitToShell ();
  832. }                /* doexit */
  833.  
  834. /*
  835.  * Junk so Emacs will set local variables to be compatible with Mac/MPW.
  836.  * Should be at end of file.
  837.  * This routine uses 8 char tabs
  838.  * 
  839.  * Local Variables:
  840.  * tab-width: 8
  841.  * End:
  842.  */
  843.