home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / comm / revrdist.sit / RevRdist / RevRdist src / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-08-23  |  8.5 KB  |  384 lines  |  [TEXT/KAHL]

  1. /*
  2.  * init.c - initialization routines
  3.  */
  4. #include "RevRdist.h"
  5. #include <string.h>
  6. #include <TransSkelProto.h>
  7. #include <TransDisplayProto.h>
  8.  
  9. extern unsigned char ApplScratch[12] : 0xa78;
  10.  
  11.  
  12.  
  13.  
  14. /*
  15.  *=========================================================================
  16.  * getSet - make sure we have enough information to proceed
  17.  * returns:    0 if all set
  18.  *            <> 0 if something missing
  19.  *            Quit may or may not be set
  20.  *=========================================================================
  21.  */
  22.  
  23. OSErr
  24. getSet()
  25. {
  26.     OSErr                error;
  27.     cnode_t                *cn;            /* ptr to fi->f_info */
  28.     file_info_t            *fi;            /* temp ptr into File_list[] */
  29.     StringHandle        sh;                /* temp string handle */
  30.     Integer                vol;            /* volume reference temp */
  31.     HParamBlockRec        hpb;            /* for GetVInfo, etc calls */
  32.     Str255                s;                /* string temp */
  33.  
  34.     fi = &File_list[FL_PREF];
  35.     cn = &fi->f_info;
  36.     if (fi->f_set)
  37.     {
  38.     /*
  39.      * If prefs file from launch, fetch values from it.
  40.      * The file had better be there.
  41.      */
  42.          if (fi->f_launch)
  43.          {
  44.              fi->f_launch = false;    /* get only one shot */
  45.              error = prefFFetch (fi);
  46.              if (error)
  47.              {
  48.                  sh = fi->f_path;
  49.                  HLock ((Handle) sh);
  50.                  panic (true, E_PREFS, *sh, nil);
  51.                  HUnlock ((Handle) sh);
  52.                  return error;
  53.              }
  54.              prefMerge (P_WORK, true);
  55.          }
  56.      }
  57.     /*
  58.      * Figure out where the server master folder is.
  59.      * Its name (absolute path) is a preferences resource.
  60.      * (If the name doesn't include a ':', add one to make sure it
  61.      * is treated as an absolute path.)
  62.      */
  63.     sh = Prefs[P_WORK].p[PS_MASTF];
  64.     if (!sh)
  65.     {
  66.         panic (true, E_NOSERVER, nil);
  67.         return E_NOSERVER;
  68.     }
  69.     fi = &File_list[FL_MAST];
  70.     cn = &fi->f_info;
  71.     COPYPS (*sh, s);
  72.     error = getInfoByPath (s, fi);
  73.     if (error)
  74.     {
  75.         ClueID = error;
  76.         if (error == fnfErr || error == nsvErr)
  77.             panic (true, E_NOSERVER, s, nil);
  78.         else
  79.             panic (true, E_SYS, Clue0, (SP) "\pgetInfoByPath", nil);
  80.     }
  81.     /*
  82.      * We must have both see files and see folders access to the master
  83.      */
  84.     if (cn->access & 0x03)
  85.     {
  86.         panic (true, E_NOACCESS, s, nil);
  87.         error = nsvErr;
  88.     }
  89.     if (error || Quit)
  90.         return error;
  91.     ServerVol = fi->f_vol;
  92.     ServerRoot = fi->f_info.dirID;
  93.     /*
  94.      * Set master folder as default volume
  95.      */
  96.     error = SetVol (s, ServerVol);
  97.     /*
  98.      * If we don't have a config file yet, locate it via preferences
  99.      */
  100.     fi = &File_list[FL_DIST];
  101.     cn = &fi->f_info;
  102.     if (! fi->f_set )
  103.     {
  104.         sh = Prefs[P_WORK].p[PS_DISTF];
  105.         if (!sh)
  106.         {
  107.             panic (true, E_DISTF, "", nil);
  108.             return fnfErr;
  109.         }
  110.         HLock ((Handle) sh);
  111.         fi->f_vol = 0;
  112.         error = getInfoByPath (*sh, fi);
  113.         if (error)
  114.         {
  115.             panic (true, E_DISTF, *sh, nil);
  116.             HUnlock ((Handle) sh);
  117.             return error;
  118.         }
  119.         HUnlock ((Handle) sh);
  120.      }
  121.     /*
  122.      * okay, now find the volume to update.
  123.      * It is the volume containing the preferences file, if present,
  124.      * else the volume containing a distribution file used to launch us,
  125.      * else the volume with the the blessed folder.
  126.      */
  127.     if (File_list[FL_PREF].f_set)
  128.         vol = File_list[FL_PREF].f_vol;
  129.     else if (File_list[FL_DIST].f_launch)
  130.         vol = File_list[FL_DIST].f_vol;
  131.     else
  132.         vol = BlessedWD;
  133.     fi = &File_list[FL_ROOT];
  134.     fi->f_ref = vol;
  135.     fi->f_info.ctype = C_FOLDER;
  136.     s[0] = 0;
  137.     error = getInfoByPath (s, fi);
  138.     if (error)
  139.     {
  140.         panic (true, E_NOVOL, nil);
  141.         return E_NOVOL;
  142.     }
  143.     ClientVol = fi->f_vol;
  144.     ClientRoot = ROOTDIRID;
  145.     /*
  146.      * Make sure that client is HFS volume
  147.      */
  148.     ZERO (hpb);
  149.     s[0] = 0;
  150.     hpb.volumeParam.ioNamePtr = s;
  151.     hpb.volumeParam.ioVRefNum = ClientVol;
  152.     PBHGetVInfo (&hpb, false);
  153.     if (hpb.volumeParam.ioVSigWord == 0xD2D7)
  154.     {
  155.         panic (true, E_MFS, CurApName, s, nil);
  156.         return E_MFS;
  157.     }
  158.     ClientSp = (unsigned long)(unsigned)hpb.volumeParam.ioVFrBlk
  159.             * (unsigned)hpb.volumeParam.ioVAlBlkSiz;
  160.     /*
  161.      * Check if the client volume is writeable
  162.      * If not, warn and set ListOnly
  163.      */
  164.     if (!(Flags & DB_LISTONLY) && (hpb.volumeParam.ioVAtrb & 0x8080))
  165.     {
  166.         warning (E_LISTONLY, s, nil);
  167.         Flags |= DB_LISTONLY;
  168.     }
  169.     /*
  170.      * Create/access junk folder.
  171.      * If fails, just warn about it.
  172.      */
  173.     error = makeJunk ();
  174.     return error;
  175. }
  176.  
  177.  
  178. /*
  179.  *=========================================================================
  180.  * initGlobals - set up the (too many) global variables
  181.  * exit:    Quit = false if initialization succeeded
  182.  *            Quit = true if application must quit immediately
  183.  *=========================================================================
  184.  */
  185. void
  186. initGlobals ()
  187. {
  188.     OSErr            error;
  189.     Integer            fcount, fmsg;
  190.     file_info_t *    fi;                    /* ptr into File_list[] */
  191.     Handle            h;                    /* temp handle */
  192.     Integer            i;                    /* temp index */
  193.     long *            lp;                    /* ptr into ApplScratch */
  194.     StringHandle    sh;                    /* temp string handle */
  195.     SysEnvRec        theWorld;            /* environment info */
  196.     WindowPtr        w;                    /* working window pointer */
  197.  
  198.     HighValue[0] = 3;
  199.     HighValue[1] = HighValue[2] = HighValue[3] = 255;
  200.     Clue0 = (SP) "\pinitGlobals";
  201.     Quit = false;
  202.     Ap_refNum = CurResFile ();
  203.     error = SysEnvirons (1, &theWorld);
  204.     if (error)
  205.     {
  206.         Quit = true;
  207.         return;
  208.     }
  209.     BlessedWD = theWorld.sysVRefNum;
  210.     /*
  211.      * Set processing flag bits
  212.      * Check ApplScratch for our signature to see if
  213.      * we are running automatically at boot time.
  214.      */
  215.     Flags = 0;
  216.     h = Get1Resource (TYPE_LONG, FLAG_PARM);
  217.     if (h)
  218.     {
  219.         Flags = **((long **)h);
  220.         ReleaseResource (h);
  221.     }
  222.     lp = (long *)(ApplScratch + 8);
  223.     if (lp[0] == CREATOR)
  224.     {
  225.         Flags |= DB_STARTUP;
  226.         lp[0] = 0;
  227.     }
  228.     if (((Flags & DB_DIALOG) || Button ()) && (Flags & DB_LOCKED) == 0)
  229.         setDebug ();
  230.     if (Quit)
  231.         return;
  232.     ActivityWind = GetNewDWindow (RSRC_BASE+WIND_ACTIVITY, (WindowPtr) -1L);
  233.     ErrorWind = GetNewDWindow (RSRC_BASE+WIND_ERRORS, (WindowPtr) -1L);
  234.     SetDWindow (ActivityWind);
  235.     /*
  236.      * Make sure we are running under HFS
  237.      */
  238.     if (FSFCBLen < 0)
  239.     {
  240.         panic (true, E_HFS, CurApName, nil);
  241.         return;
  242.     }
  243.     /*
  244.      * Get junking renaming suffix
  245.      */
  246.     h = GetResource ('STR ', STR_JUNKSUF);
  247.     if (!h || **h > sizeof (Junksuf) - 1 || **h < 2)
  248.     {
  249.         panic (true, E_BADJUNKF, nil);
  250.         return;
  251.     }
  252.     COPYPS (*h, Junksuf);
  253.     ReleaseResource (h);
  254.     /*
  255.      * Get name for anonymous documents
  256.      */
  257.     h = GetResource ('STR ', STR_UNTITLED);
  258.     if (!h)
  259.     {
  260.         error = ResError ();
  261.         goto sysdie;
  262.     }
  263.     Untitled = (StringHandle) h;
  264.     /*
  265.      * Allocate File_list and fill parts of it in, including
  266.      *    info about Application
  267.      *    info about any files passed as parameters
  268.      */
  269.     File_list = (file_info_t *) NewPtr (FL_MAX * sizeof (* File_list));
  270.     if (!File_list)
  271.         goto nomem;
  272.     setmem ((char *)File_list, FL_MAX * sizeof (* File_list), 0);
  273.  
  274.     fi = &File_list[FL_APPL];
  275.     fi->f_ref = CurResFile ();
  276.     fi->f_info.ctype = C_FILE;
  277.     if (error = getInfoByPath ((SP)"\p", fi))
  278.         goto sysdie;
  279.     fi->f_launch = true;
  280.  
  281.     CountAppFiles (&fmsg, &fcount);
  282.     if (fmsg == 1 && fcount > 0)
  283.     {
  284.         panic (false, E_NOPRINT, nil);
  285.         if (Quit)
  286.             return;
  287.     }
  288.  
  289.     Ap_file.fType = 0;
  290.     for (i = 1; i <= fcount; i++)
  291.     {
  292.         int    j;
  293.         
  294.         GetAppFiles (i, &Ap_file);
  295.         switch (Ap_file.fType)
  296.         {
  297.         case TYPE_PREF: j = FL_PREF; break;
  298.         case TYPE_CONT:    j = FL_DIST; break;
  299.         case 'INIT':    j = -1; Flags |= DB_STARTUP; break;
  300.         default:        j = -1; break;
  301.         }
  302.         if (j >= 0)
  303.         {
  304.             fi = &File_list[j];
  305.             fi->f_vol = Ap_file.vRefNum;
  306.             error = getInfoByPath (Ap_file.fName, fi);
  307.             if (error == 0)
  308.                 fi->f_launch = true;
  309.         }
  310.         ClrAppFiles (i);
  311.     }
  312.     /*
  313.      * Initialize preferences from application
  314.      */
  315.     setmem ((char *)&Prefs[0], 2 * sizeof (prefs_t), 0);
  316.     if (prefFetch (File_list[FL_APPL].f_ref) == 0)
  317.         prefMerge (P_WORK, false);
  318.     /*
  319.      * If we haven't located a preferences file yet, try finding the one
  320.      * given by the preferences string within the application.
  321.      * It's not a problem if we cannot find the file.
  322.      */
  323.     fi = &File_list[FL_PREF];
  324.     if (!fi->f_set)
  325.     {
  326.         sh = (StringHandle) GetResource ('STR ', STR_PREFS);
  327.         if (!sh)
  328.         {
  329.             panic (true, E_SYS, Clue0,
  330.                 (SP)"\pGetResource", (SP)"\ppref file name");
  331.             return;
  332.         }
  333.         HLock ((Handle) sh);
  334.         fi->f_vol = 0;
  335.         error = getInfoByPath (*sh, fi);
  336.         if (error == 0)
  337.         {
  338.             error = prefFFetch (fi);
  339.             if (error == 0)
  340.                 prefMerge (P_WORK, true);
  341.             Prefs[P_WORK].p_modified = false;
  342.         }
  343.         if (error)
  344.         {
  345.             /*
  346.              * If not startup, tell about no prefs and see if we
  347.              * should continue.
  348.              */
  349.             if ((Flags & DB_STARTUP) == 0)
  350.                 panic (false, E_PREFS, *sh, nil);
  351.         }
  352.         HUnlock ((Handle) sh);
  353.         ReleaseResource ((Handle) sh);
  354.         if (Quit)
  355.             return;
  356.     }
  357.  
  358.     return;
  359.     
  360. sysdie:
  361.     ClueID = error;
  362.     panic (true, E_SYS, Clue0, nil);
  363.     Quit = true;
  364.     return;
  365.  
  366. nomem:
  367.     SysBeep (5);
  368.     Quit = true;
  369.     return;
  370. }
  371.  
  372.  
  373. /*
  374.  *=========================================================================
  375.  * tidyUp () - do final cleanup before exiting
  376.  *=========================================================================
  377.  */
  378. void
  379. tidyUp ()
  380. {
  381.     if (Deskrefnum != -1)
  382.         CloseResFile (Deskrefnum);
  383. }
  384.