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

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