home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckc072.zip / ckmsav.c < prev    next >
C/C++ Source or Header  |  1988-08-16  |  10KB  |  319 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.  
  51. int *inames[] = {
  52.     &speed, &parity, &duplex, &delay, &mypadn,
  53.     &npad, &timint, &rtimo, &urpsiz, &spsiz,
  54.     &turnch, &turn, &bctr, &filargs.fildflg,
  55.     &newline, &autowrap, &scrinvert, &autorepeat,
  56.     &smoothscroll, &transparent, &keep, &blockcursor,
  57.     &mouse_arrows, &visible_bell, &nat_chars,
  58.     &blinkcursor
  59. };
  60.  
  61. #define NINAMES (sizeof(inames) / sizeof(int *))
  62.  
  63. char *cnames[] = {
  64.     &mypadc, &padch, &eol, &seol, &stchr, &mystch,
  65.     &fkeysactive, &mcmdactive
  66. };
  67.  
  68. #define NCNAMES (sizeof(cnames) / sizeof(char *))
  69.  
  70. typedef long **IHandle;        /* handle to int[] */
  71. typedef char **CHandle;        /* handle to char[] */
  72.  
  73. extern WindowPtr terminalWindow;
  74.  
  75.  
  76.  
  77. /****************************************************************************/
  78. /* Delete the specified resource if it exists in the current resource file */
  79. /****************************************************************************/
  80. KillResource (type, id)
  81. ResType type;
  82. int id;
  83. {
  84.     Handle theRsrc;
  85.  
  86.     theRsrc = GetResource (type, id);
  87.     if ((theRsrc != NIL) && (HomeResFile (theRsrc) == CurResFile ())) {
  88.     RmveResource (theRsrc);
  89.     if (ResError () != noErr) {    /* check for error */
  90.         printerr ("Could not remove old resource: ", ResError ());
  91.         return;
  92.     }
  93.     DisposHandle (theRsrc);
  94.     }
  95. }                /* KillResource */
  96.  
  97.  
  98.  
  99. /****************************************************************************/
  100. /* savevals - save variables for MacKermit */
  101. /****************************************************************************/
  102. savevals ()
  103. {
  104.     IHandle ihdl;
  105.     CHandle chdl;
  106.  
  107.     SFReply savr;
  108.     Point where;
  109.     int err;
  110.     int rfnum;
  111.     int i;
  112.     FInfo finf;
  113.     char name[256];
  114.  
  115.     GetWTitle (terminalWindow, &name);
  116.  
  117.     SetPt (&where, 75, 115);
  118.     SFPutFile (&where, "Save variables in file:", name, NILPROC, &savr);
  119.     if (!savr.good)        /* did they hit cancel? */
  120.     return;            /* yes, so return now */
  121.  
  122.     SetVol (NILPTR, savr.vRefNum);    /* select volume for rsrc file */
  123.  
  124.     p2cstr (&savr.fName);
  125.     rfnum = OpenResFile (&savr.fName);
  126.     err = ResError ();
  127.     if (err == noErr) {        /* file exists, clear old resources */
  128.     /* Be sure to delete the old resources, if they alraedy */
  129.     /* exist. Otherwise the resources will be added to the */
  130.     /* existing file (adding more and more resources with */
  131.     /* the same type and number). Unfortunately, for setting */
  132.     /* the parameters, Kermit always uses the first, i.e. */
  133.     /* the !oldest! set of resources! */
  134.     KillResource (SAVI_TYPE, SIVER);
  135.     KillResource (SAVC_TYPE, SCVER);
  136.     KillResource (KSET_TYPE, KSVER);
  137.     KillResource (MSET_TYPE, KMVER);
  138.     } else if ((err == resFNotFound) || (err == fnfErr)) {
  139.     /* file not existing ? */
  140.     CreateResFile (&savr.fName);    /* try to create */
  141.     if (ResError () != noErr) {    /* check for error */
  142.         printerr ("Unknown error from create: ", ResError ());
  143.         return;
  144.     }
  145.     /* set the file finder infos */
  146.     err = GetFInfo (&savr.fName, savr.vRefNum, &finf);
  147.     if (err != noErr)
  148.         printerr ("Can't get finder info for file: ", err);
  149.     else {
  150.         finf.fdFldr = filargs.filfldr;    /* use same folder as
  151.                          * application */
  152.         finf.fdType = settingstype;    /* set type */
  153.         finf.fdCreator = kermtype;    /* set creator */
  154.         err = SetFInfo (&savr.fName, savr.vRefNum, &finf);
  155.         if (err != noErr)
  156.         printerr ("Can't set finder info: ", err);
  157.     }
  158.     /* try open again */
  159.     rfnum = OpenResFile (&savr.fName);
  160.     if (rfnum == -1) {    /* failed to open? */
  161.         printerr ("Couldn't Open resource file: ", ResError ());
  162.         return;
  163.     }
  164.     } else {
  165.     printerr ("Couldn't Open resource file: ", err);
  166.     return;
  167.     }
  168.  
  169.     scrinvert = screeninvert;    /* save the current value */
  170.  
  171.     /*
  172.      * PWP: changed the format so {count, item, item, ...} so that we can
  173.      * load older versions without dieing
  174.      */
  175.  
  176.     ihdl = (IHandle) NewHandle ((long) (NINAMES + 1) * sizeof (int));
  177.     (*ihdl)[0] = NINAMES;
  178.     for (i = 0; i < NINAMES; i++)    /* copy from indirect table */
  179.     (*ihdl)[i + 1] = *inames[i];
  180.     AddResource ((Handle) ihdl, SAVI_TYPE, SIVER, "");
  181.  
  182.     chdl = (CHandle) NewHandle ((long) (NCNAMES + 1) * sizeof (char));
  183.     (*chdl)[0] = NCNAMES;
  184.     for (i = 0; i < NCNAMES; i++)    /* copy from indirect table */
  185.     (*chdl)[i + 1] = *cnames[i];
  186.     AddResource ((Handle) chdl, SAVC_TYPE, SCVER, "");
  187.  
  188.     savekset ();        /* save key bit table */
  189.     savemset ();        /* save key macros table */
  190.  
  191.     CloseResFile (rfnum);
  192.     FlushVol (NILPTR, savr.vRefNum);    /* flush the bits out */
  193.  
  194.     SetWTitle (terminalWindow, &savr.fName);
  195. }                /* savevals */
  196.  
  197.  
  198.  
  199. /****************************************************************************/
  200. /* do a Load settings... dialog */
  201. /****************************************************************************/
  202. loadvals ()
  203. {
  204.     SFReply savr;
  205.     Point where;
  206.  
  207.     SetPt (&where, 75, 115);
  208.     SFGetFile (&where, "Load variables from:", NILPROC, 1, &settingstype,
  209.            NILPROC, &savr);
  210.  
  211.     if (!savr.good)        /* did they hit cancel? */
  212.     return;            /* yes, so return now */
  213.  
  214.     p2cstr (&savr.fName);
  215.     doloadvals (&savr.fName, savr.vRefNum);    /* do the load */
  216. }                /* loadvals */
  217.  
  218.  
  219.  
  220. /****************************************************************************/
  221. /****************************************************************************/
  222. findfinderfiles ()
  223. {
  224.     short msg, cnt;
  225.     int err;
  226.     AppFile apf;
  227.     FInfo ainfo;
  228.  
  229.     CountAppFiles (&msg, &cnt);    /* anything clicked by user? */
  230.     if (cnt == 0 || msg == appPrint) {    /* or they want us to print (?) */
  231.     filargs.filfldr = fDesktop;    /* forget about loading values */
  232.     loadkset ();        /* make new files appear on desk */
  233.     return;            /* use our default KSET */
  234.     }
  235.     GetAppFiles (1, &apf);    /* get the first one */
  236.     p2cstr (&apf.fName);
  237.  
  238.     ClrAppFiles (1);        /* done with this */
  239.     doloadvals (&apf.fName, apf.vRefNum);    /* load the file */
  240.     err = GetFInfo (&apf.fName,    /* get settings file info */
  241.             apf.vRefNum, &ainfo);
  242.     if (err != noErr)
  243.     printerr ("Couldn't GetFInfo for default folder: ", err);
  244.     filargs.filfldr = ainfo.fdFldr;    /* use appl or text file's folder */
  245. }                /* findfinderfiles */
  246.  
  247.  
  248.  
  249. extern MenuHandle menus[MAX_MENU + 1];    /* handle on our menus */
  250.  
  251. /****************************************************************************/
  252. /****************************************************************************/
  253. doloadvals (fn, refnum)
  254. char *fn;
  255. int refnum;
  256. {
  257.     int rfnum;
  258.     int i, n;
  259.     IHandle resinames;
  260.     CHandle rescnames;
  261.  
  262.     SetVol (NILPTR, refnum);    /* select volume */
  263.     rfnum = OpenResFile (fn);    /* open the resource file */
  264.     if (rfnum == -1) {
  265.     printerr ("Couldn't open file: ", ResError ());
  266.     return;
  267.     };
  268.  
  269.     /* load 'SAVI' resource, the saved integer values, */
  270.     /* 'SAVC' saved characters */
  271.  
  272.     if ((resinames = (IHandle) GetResource (SAVI_TYPE, SIVER)) == NIL ||
  273.     (rescnames = (CHandle) GetResource (SAVC_TYPE, SCVER)) == NIL) {
  274.     CloseResFile (rfnum);
  275.     printerr ("Can't load your settings, damaged file or wrong version.",
  276.           0);
  277.     return;            /* and return */
  278.     }
  279.     cursor_erase ();        /* hide the current cursor */
  280.  
  281.     /*
  282.      * PWP: changed the format so {count, item, item, ...} so that we can
  283.      * load older versions without dieing
  284.      */
  285.  
  286.     n = (*resinames)[0];
  287.     if (n > NINAMES)
  288.     n = NINAMES;
  289.     for (i = 0; i < n; i++)
  290.     *inames[i] = (*resinames)[i + 1];
  291.  
  292.     n = (*rescnames)[0];
  293.     if (n > NCNAMES)
  294.     n = NCNAMES;
  295.     for (i = 0; i < n; i++)
  296.     *cnames[i] = (*rescnames)[i + 1];
  297.  
  298.     cursor_draw ();        /* show the new cursor */
  299.  
  300.     loadkset ();        /* load new KSET */
  301.     loadmset ();        /* release current MSET and load new one */
  302.  
  303.     CloseResFile (rfnum);    /* no longer needed */
  304.  
  305.     /* change the screen if necessary */
  306.     if (scrinvert != screeninvert)
  307.     invert_term ();
  308.  
  309.     /* tell serial driver about new vals */
  310.     setserial (innum, outnum, speed, KPARITY_NONE);
  311.  
  312.     /* set the two check menus */
  313.     ScrDmpEnb = (fkeysactive) ? scrdmpenabled : scrdmpdisabled;
  314.     CheckItem (menus[SETG_MENU], SCRD_SETG, (fkeysactive));
  315.     CheckItem (menus[SETG_MENU], MCDM_SETG, (mcmdactive));
  316.  
  317.     SetWTitle (terminalWindow, fn);
  318. }                /* doloadvals */
  319.