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