home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckc095.tar.gz / ckc095.tar / ckmsav.c < prev    next >
C/C++ Source or Header  |  1990-03-07  |  11KB  |  360 lines

  1. /* Paul Placeway, Ohio State -- changed the format of saved Is and Cs to */
  2. /*  {number, item, item, ...} */
  3. /* Version 0.8(35) - Jim Noble at Planning Research Corporation, June 1987. */
  4. /* Ported to Megamax native Macintosh C compiler. */
  5. /* Edit by Frank on Jun 20 17:20 */
  6. /* Don't set sio chip parity */
  7. /* Edit by Bill on May 29 01:01 */
  8. /* Add key configurator */
  9. /* Edit by Bill on May 10 9:24 */
  10. /* Saving settings file to a different disk doesn't work and may bomb */
  11. /* Edit by Bill on May 8 7:17 */
  12. /* Save default file settings, now incompatable with existing save files! */
  13. /* Edit by Bill & Jeff on May 1 14:16 */
  14. /* findfinderfiles was bombing because of fName[1] definition of myAppFile */
  15. /* Edit by Bill on Apr 30 05:50 */
  16. /* Call FlushVol after saving the settings */
  17.  
  18. /*
  19.  * file ckmsav.c
  20.  *
  21.  * Module of MacKermit containing code for saving and restoring
  22.  * various variables.
  23.  *
  24.  * Copyright (C) 1985, Trustees of Columbia University in the City of
  25.  * New York.  Permission is granted to any individual or institution to
  26.  * use, copy, or redistribute this software so long as it is not sold
  27.  * for profit, provided this copyright notice is retained.
  28.  *
  29.  */
  30.  
  31. #include "ckcdeb.h"        /* Kermit definitions */
  32. #include "ckcker.h"        /* Kermit definitions */
  33.  
  34. #define    __SEG__ ckmsav
  35. #include <files.h>
  36. #include <errors.h>
  37. #include <resources.h>
  38. #include <packages.h>
  39. #include <segload.h>
  40. #include <windows.h>
  41. #include <osutils.h>
  42. #include <menus.h>
  43.  
  44. #include "ckmdef.h"        /* Common Mac module definitions */
  45. #include "ckmres.h"        /* resource defs */
  46.  
  47. OSType kermtype = ApplCreator, settingstype = 'KERS';
  48.  
  49. int scrinvert;            /* intermediate container for screeninvert */
  50. int scrsize;            /* ditto for size */
  51. int savinnum;            /* intermediate container for innum (I/O port) */
  52. char savmcmdactive;        /* intermediate container for mcmdactive */
  53.  
  54. extern int drop_dtr;
  55.  
  56. extern int dfprty;                      /* Default parity */
  57. extern int dfflow;                      /* Default flow control */
  58.  
  59. int *inames[] = {
  60.     &speed, &parity, &duplex, &delay, &mypadn,
  61.     &npad, &timint, &rtimo, &urpsiz, &spsiz,
  62.     &turnch, &turn, &bctr, &filargs.fildflg,
  63.     &newline, &autowrap, &scrinvert, &autorepeat,
  64.     &smoothscroll, &transparent, &keep, &blockcursor,
  65.     &mouse_arrows, &visible_bell, &eightbit_disp,
  66.     &blinkcursor, &scrsize, &savinnum, &sendusercvdef,
  67.     &drop_dtr, &flow
  68. };
  69.  
  70. #define NINAMES (sizeof(inames) / sizeof(int *))
  71.  
  72. char *cnames[] = {
  73.     &mypadc, &padch, &eol, &seol, &stchr, &mystch,
  74.     &fkeysactive, &savmcmdactive
  75. };
  76.  
  77. #define NCNAMES (sizeof(cnames) / sizeof(char *))
  78.  
  79. typedef long **IHandle;        /* handle to int[] */
  80. typedef char **CHandle;        /* handle to char[] */
  81.  
  82. extern WindowPtr terminalWindow;
  83.  
  84.  
  85.  
  86. /****************************************************************************/
  87. /* Delete the specified resource if it exists in the current resource file */
  88. /****************************************************************************/
  89. KillResource (type, id)
  90. ResType type;
  91. int id;
  92. {
  93.     Handle theRsrc;
  94.  
  95.     theRsrc = GetResource (type, id);
  96.     if ((theRsrc != NIL) && (HomeResFile (theRsrc) == CurResFile ())) {
  97.     RmveResource (theRsrc);
  98.     if (ResError () != noErr) {    /* check for error */
  99.         printerr ("Could not remove old resource: ", ResError ());
  100.         return;
  101.     }
  102.     DisposHandle (theRsrc);
  103.     }
  104. }                /* KillResource */
  105.  
  106.  
  107.  
  108. /****************************************************************************/
  109. /* savevals - save variables for MacKermit */
  110. /****************************************************************************/
  111. savevals ()
  112. {
  113.     IHandle ihdl;
  114.     CHandle chdl;
  115.  
  116.     SFReply savr;
  117.     Point where;
  118.     int err;
  119.     int rfnum;
  120.     int i;
  121.     FInfo finf;
  122.     char name[256];
  123.  
  124.     GetWTitle (terminalWindow, name);
  125.  
  126.     SetPt (&where, 75, 115);
  127.     SFPutFile (&where, "Save variables in file:", name, NILPROC, &savr);
  128.     if (!savr.good)        /* did they hit cancel? */
  129.     return;            /* yes, so return now */
  130.  
  131.     SetVol (NILPTR, savr.vRefNum);    /* select volume for rsrc file */
  132.  
  133.     p2cstr (&savr.fName);
  134.     rfnum = OpenResFile (&savr.fName);
  135.     err = ResError ();
  136.     if (err == noErr) {        /* file exists, clear old resources */
  137.     /* Be sure to delete the old resources, if they alraedy */
  138.     /* exist. Otherwise the resources will be added to the */
  139.     /* existing file (adding more and more resources with */
  140.     /* the same type and number). Unfortunately, for setting */
  141.     /* the parameters, Kermit always uses the first, i.e. */
  142.     /* the !oldest! set of resources! */
  143.     KillResource (SAVI_TYPE, SIVER);
  144.     KillResource (SAVC_TYPE, SCVER);
  145.     KillResource (KSET_TYPE, KSVER);
  146.     KillResource (MSET_TYPE, KMVER);
  147.     } else if ((err == resFNotFound) || (err == fnfErr)) {
  148.     /* file not existing ? */
  149.     CreateResFile (&savr.fName);    /* try to create */
  150.     if (ResError () != noErr) {    /* check for error */
  151.         printerr ("Unknown error from create: ", ResError ());
  152.         return;
  153.     }
  154.     /* set the file finder infos */
  155.     err = GetFInfo (&savr.fName, savr.vRefNum, &finf);
  156.     if (err != noErr)
  157.         printerr ("Can't get finder info for file: ", err);
  158.     else {
  159.         finf.fdFldr = filargs.filfldr;    /* use same folder as
  160.                          * application */
  161.         finf.fdType = settingstype;    /* set type */
  162.         finf.fdCreator = kermtype;    /* set creator */
  163.         err = SetFInfo (&savr.fName, savr.vRefNum, &finf);
  164.         if (err != noErr)
  165.         printerr ("Can't set finder info: ", err);
  166.     }
  167.     /* try open again */
  168.     rfnum = OpenResFile (&savr.fName);
  169.     if (rfnum == -1) {    /* failed to open? */
  170.         printerr ("Couldn't Open resource file: ", ResError ());
  171.         return;
  172.     }
  173.     } else {
  174.     printerr ("Couldn't Open resource file: ", err);
  175.     return;
  176.     }
  177.  
  178.     scrinvert = screeninvert;    /* save the current value */
  179.     scrsize = screensize;
  180.     savinnum = innum;        /* save current port too */
  181.     savmcmdactive = mcmdactive;
  182.  
  183.     /*
  184.      * PWP: changed the format so {count, item, item, ...} so that we can
  185.      * load older versions without dying
  186.      */
  187.  
  188.     ihdl = (IHandle) NewHandle ((long) (NINAMES + 1) * sizeof (int));
  189.     (*ihdl)[0] = NINAMES;
  190.     for (i = 0; i < NINAMES; i++)    /* copy from indirect table */
  191.     (*ihdl)[i + 1] = *inames[i];
  192.     AddResource ((Handle) ihdl, SAVI_TYPE, SIVER, "");
  193.  
  194.     chdl = (CHandle) NewHandle ((long) (NCNAMES + 1) * sizeof (char));
  195.     (*chdl)[0] = NCNAMES;
  196.     for (i = 0; i < NCNAMES; i++)    /* copy from indirect table */
  197.     (*chdl)[i + 1] = *cnames[i];
  198.     AddResource ((Handle) chdl, SAVC_TYPE, SCVER, "");
  199.  
  200.     savekset ();        /* save key bit table */
  201.     savemset ();        /* save key macros table */
  202.  
  203.     CloseResFile (rfnum);
  204.     FlushVol (NILPTR, savr.vRefNum);    /* flush the bits out */
  205.  
  206.     SetWTitle (terminalWindow, &savr.fName);
  207. }                /* savevals */
  208.  
  209.  
  210.  
  211. /****************************************************************************/
  212. /* do a Load settings... dialog */
  213. /****************************************************************************/
  214. loadvals ()
  215. {
  216.     SFReply savr;
  217.     Point where;
  218.  
  219.     SetPt (&where, 75, 115);
  220.     SFGetFile (&where, "Load variables from:", NILPROC, 1, &settingstype,
  221.            NILPROC, &savr);
  222.  
  223.     if (!savr.good)        /* did they hit cancel? */
  224.     return;            /* yes, so return now */
  225.  
  226.     p2cstr (&savr.fName);
  227.     doloadvals (&savr.fName, savr.vRefNum);    /* do the load */
  228. }                /* loadvals */
  229.  
  230.  
  231.  
  232. /****************************************************************************/
  233. /****************************************************************************/
  234. findfinderfiles ()
  235. {
  236.     short msg, cnt;
  237.     int err;
  238.     AppFile apf;
  239.     FInfo ainfo;
  240.  
  241.     CountAppFiles (&msg, &cnt);    /* anything clicked by user? */
  242.     if (cnt == 0 || msg == appPrint) {    /* or they want us to print (?) */
  243.     filargs.filfldr = fDesktop;    /* forget about loading values */
  244.     loadkset ();        /* make new files appear on desk */
  245.     return;            /* use our default KSET */
  246.     }
  247.     GetAppFiles (1, &apf);    /* get the first one */
  248.     p2cstr (&apf.fName);
  249.  
  250.     ClrAppFiles (1);        /* done with this */
  251.     doloadvals (&apf.fName, apf.vRefNum);    /* load the file */
  252.     err = GetFInfo (&apf.fName,    /* get settings file info */
  253.             apf.vRefNum, &ainfo);
  254.     if (err != noErr)
  255.     printerr ("Couldn't GetFInfo for default folder: ", err);
  256.     filargs.filfldr = ainfo.fdFldr;    /* use appl or text file's folder */
  257. }                /* findfinderfiles */
  258.  
  259.  
  260.  
  261. extern MenuHandle menus[MAX_MENU + 1];    /* handle on our menus */
  262.  
  263. /****************************************************************************/
  264. /****************************************************************************/
  265. doloadvals (fn, refnum)
  266. char *fn;
  267. int refnum;
  268. {
  269.     int rfnum;
  270.     int i, n;
  271.     IHandle resinames;
  272.     CHandle rescnames;
  273.     int old_screensize = screensize;
  274.  
  275.     SetVol (NILPTR, refnum);    /* select volume */
  276.     rfnum = OpenResFile (fn);    /* open the resource file */
  277.     if (rfnum == -1) {
  278.     printerr ("Couldn't open file: ", ResError ());
  279.     return;
  280.     };
  281.  
  282.     /* load 'SAVI' resource, the saved integer values, */
  283.     /* 'SAVC' saved characters */
  284.  
  285.     if ((resinames = (IHandle) GetResource (SAVI_TYPE, SIVER)) == NIL ||
  286.     (rescnames = (CHandle) GetResource (SAVC_TYPE, SCVER)) == NIL) {
  287.     CloseResFile (rfnum);
  288.     printerr ("Can't load your settings, damaged file or wrong version.",
  289.           0);
  290.     return;            /* and return */
  291.     }
  292.     cursor_erase ();        /* hide the current cursor */
  293.  
  294.     /*
  295.      * PWP: changed the format to {count, item, item, ...} so that we can
  296.      * load older versions without dieing
  297.      */
  298.  
  299.     n = (*resinames)[0];
  300.     if (n > NINAMES)
  301.     n = NINAMES;
  302.     for (i = 0; i < n; i++)
  303.     *inames[i] = (*resinames)[i + 1];
  304.  
  305.     n = (*rescnames)[0];
  306.     if (n > NCNAMES)
  307.     n = NCNAMES;
  308.     for (i = 0; i < n; i++)
  309.     *cnames[i] = (*rescnames)[i + 1];
  310.  
  311.     cursor_draw ();        /* show the new cursor */
  312.  
  313.     loadkset ();        /* load new KSET */
  314.     loadmset ();        /* release current MSET and load new one */
  315.  
  316.     CloseResFile (rfnum);    /* no longer needed */
  317.     
  318.     /* change the screen if necessary */
  319.     if (scrsize != screensize)    /* if we changed size */
  320.     grow_term_to (scrsize);
  321.  
  322.     if (scrinvert != screeninvert)
  323.     invert_term ();
  324.  
  325.     if (savinnum != innum) {    /* if using the other port */
  326.     port_close();
  327.     port_open(savinnum);
  328.     }
  329.     
  330.     /* tell serial driver about new vals */
  331.     (void) setserial (innum, outnum, speed, KPARITY_NONE);
  332.     
  333.     /* Frank changed main() to call init and then set flow, parity, etc.
  334.        so we make sure they will be set right (again) after we return. */
  335.     dfprty = parity;                    /* Set initial parity, */
  336.     dfflow = flow;                      /* and flow control. */
  337.  
  338.     /* set the two check menus */
  339.     ScrDmpEnb = (fkeysactive) ? scrdmpenabled : scrdmpdisabled;
  340.     CheckItem (menus[SETG_MENU], SCRD_SETG, (fkeysactive));
  341.     if (savmcmdactive != mcmdactive) {
  342.     mcmdactive = savmcmdactive;
  343.     setup_menus();
  344.     }
  345.     CheckItem (menus[SETG_MENU], MCDM_SETG, (mcmdactive));
  346.  
  347.     SetWTitle (terminalWindow, fn);
  348.  
  349.     /* (PWP) bounds check the values we just got to be double extra safe */
  350.     
  351.     if (urpsiz > MAXRP-8) {
  352.         printerr("Recieve packet lengh is too big", urpsiz);
  353.     urpsiz = MAXRP-8;
  354.     }
  355.     if (spsiz > MAXSP) {
  356.         printerr("Send packet length is too big", spsiz);
  357.     spsiz = MAXSP;
  358.     }
  359. }                /* doloadvals */
  360.