home *** CD-ROM | disk | FTP | other *** search
/ Executor 2.0 / executorv2.0.iso / pc / dos / extra / source / browser / browser.hqx / Browser / init.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-06  |  9.4 KB  |  464 lines

  1. #include "go.h"
  2. #include "xfer.h"
  3. #include "display.h"
  4. #include <string.h>
  5. #include <assert.h>
  6.  
  7. #include "init.proto.h"
  8. #include "initicons.proto.h"
  9. #include "inithotband.proto.h"
  10. #include "initdirs.proto.h"
  11. #include "menu.proto.h"
  12. #include "easymenu.proto.h"
  13. #include "sharedtransfer.proto.h"
  14. #include "fontda.h"
  15.  
  16. short g_helper_file_rn;
  17.  
  18. void
  19. checkgrowitemarray (short numitems, ControlHandle (**items)[])
  20. {
  21.  
  22.   if (numitems % BANDARRAYSIZE == 0)
  23.     {
  24.       SetHandleSize((Handle) items, BANDARRAYSIZE * (numitems / BANDARRAYSIZE + 1)
  25.                     * sizeof (ControlHandle));
  26.     }
  27. }
  28.  
  29. void
  30. tail (char *path, Str255 s)
  31. {
  32.   short i, j, len;
  33.  
  34.   len = strlen (path);
  35.   i = len - 1;
  36.   while (i > 0 && path[i] == ':')
  37.     i--;
  38.   while (i >= 0 && path[i] != ':' )
  39.     i--;
  40.   s[0] = len - i - 1;
  41.   memcpy(s+1, path + i + 1, len - i);
  42. }
  43.  
  44. void
  45. getname (Str255 name, FILE * f)
  46. {
  47.   short i, c;
  48.  
  49.   name[0] = ' ';
  50.   for (i = 1; (i < 255) && (name[i - 1] != ':') && (name[i - 1] != '\n'); i++)
  51.     {
  52.       c = getc (f);
  53.       if (c == EOF)
  54.         name[i] = '\n';
  55.       else
  56.         name[i] = c;
  57.     }
  58.   name[0] = i - 2;
  59. }
  60.  
  61. void
  62. skip_line (FILE * f)
  63. {
  64.   short c;
  65.  
  66.   while ((c = getc (f)) && (c != '\n') && (c != EOF))
  67.     ;
  68. }
  69.  
  70. OSErr
  71. cd (short *cwd, Str255 name)
  72. {
  73.   short oldcwd;
  74.   WDPBRec pb;
  75.   OSErr e;
  76.  
  77. #if 0
  78.   if (executor_p ())
  79.     {
  80.        CInfoPBRec cpb;
  81.        OSErr e2;
  82.        
  83.        cpb.hFileInfo.ioNamePtr = (StringPtr) name;
  84.        cpb.hFileInfo.ioDirID = 0;
  85.        cpb.hFileInfo.ioVRefNum = *cwd;
  86.        cpb.hFileInfo.ioFDirIndex = 0;
  87.        
  88.        e2 = unixmount (&cpb);
  89.        if (e2 == noErr)
  90.          *cwd = cpb.hFileInfo.ioVRefNum;
  91.     }
  92. #endif
  93.  
  94.   pb.ioNamePtr = (StringPtr) name;
  95.   pb.ioWDDirID = 0;
  96.   pb.ioWDProcID = CREATOR;
  97.   pb.ioVRefNum = *cwd;
  98.   e = PBOpenWD (&pb, false);
  99.   if (e == noErr)
  100.     {
  101.       oldcwd = *cwd;
  102.       *cwd = pb.ioVRefNum;
  103.       pb.ioVRefNum = oldcwd;
  104.       e = PBCloseWD (&pb, false);
  105.     }
  106.   return e;
  107. }
  108.  
  109. void
  110. appenddir (char ***p, Str255 name)
  111. {
  112.   short l;
  113.   OSErr e;
  114.  
  115.   l = strlen (**p);
  116.   (**p)[l] = ':';
  117.   e = PtrAndHand (name + 1, *p, name[0] + 1);
  118.   (**p)[l + name[0] + 1] = 0;
  119. }
  120.  
  121. OSErr
  122. getonefileinfo (FILE * f, CInfoPBRec * cpb, Handle *path, short *volume)
  123. {
  124.   ParamBlockRec pb;
  125.   OSErr e;
  126.   Str255 name;
  127.   short cwd;
  128.  
  129.   pb.volumeParam.ioVolIndex = -1;
  130.   pb.volumeParam.ioVRefNum = 0;
  131.   getname (name, f);
  132.   pb.volumeParam.ioNamePtr = name;
  133.   assert(name[name[0] + 1] == ':');
  134.   name[0]++;
  135.   e = PBGetVInfo (&pb, false);
  136.   if (e != noErr)
  137.     {
  138.       skip_line (f);
  139.       return e;
  140.     }
  141.   *volume = pb.volumeParam.ioVRefNum;
  142.   cwd = pb.volumeParam.ioVRefNum;
  143.   *path = NewHandle (name[0] + 1);
  144.   PtoCstr (name);
  145.   strcpy (**path, (char *) name);
  146.   getname (name, f);
  147.   appenddir (path, name);
  148.  
  149.   while (name[name[0] + 1] == ':')
  150.     {
  151.       e = cd (&cwd, name);
  152.       if (e != noErr)
  153.       {
  154.         skip_line (f);
  155.         return e;
  156.       }
  157.       getname (name, f);
  158.       appenddir (path, name);
  159.     }
  160.  
  161.   cpb->hFileInfo.ioVRefNum = cwd;
  162.   cpb->hFileInfo.ioDirID = 0;
  163.   cpb->hFileInfo.ioFDirIndex = 0;
  164.   cpb->hFileInfo.ioNamePtr = name;
  165.   e = PBGetCatInfo (cpb, false);
  166.  
  167. #if 1
  168.   {
  169.   HParamBlockRec hpb;
  170.   
  171.   /* Under Executor, the volume may have changed if we crossed a mount point */
  172.   hpb.volumeParam.ioVolIndex = -1;
  173.   hpb.volumeParam.ioVRefNum = cwd;
  174.   hpb.volumeParam.ioNamePtr = name;
  175.   PBHGetVInfo (&hpb, false);
  176.   *volume = hpb.volumeParam.ioVRefNum;
  177.   }
  178. #endif
  179.   return e;
  180. }
  181.  
  182. void
  183. setupmenus (void)
  184. {
  185.   Handle h;
  186.   MenuHandle mh;
  187.  
  188.   h = GetNewMBar (GOMBARID);
  189.   SetMenuBar (h);
  190.   DrawMenuBar ();
  191.  
  192.   mh = GetMHandle (FIRSTMENU);
  193.   AddResMenu (mh, 'DRVR');
  194.   menuchoices (false);
  195.   showviewmenu (false);
  196. }
  197.  
  198. static void
  199. remove_leading_dirs (StringPtr name)
  200. {
  201.   int len;
  202.   Byte *p;
  203.     
  204.   len = name[0];
  205.   for (p = name+len; p > name && *p != ':'; --p)
  206.     ;
  207.   if (p > name)
  208.     {
  209.       len = name + len - p;
  210.       name[0] = len;
  211.       memcpy (name+1, p+1, len);
  212.     }
  213. }
  214.  
  215. void
  216. setdefaulteditor (void)
  217. {
  218.   Str255 s, name;
  219.   StringHandle sh;
  220.   short i, done;
  221.   short cwd;
  222.   CInfoPBRec cpb;
  223.   OSErr e;
  224.   HParamBlockRec pb;
  225.  
  226.   sh = GetString (DEFAULTEDITORSTRINGID);
  227.   HLock ((Handle) sh);
  228.   mystr255copy (name, *sh);
  229.   HUnlock ((Handle) sh);
  230.  
  231. #if defined(OK_LETS_DISPOSE_RESOURCES_JUST_TO_BREAK_THINGS)
  232.   DisposHandle ((Handle) sh);
  233. #endif
  234.  
  235.   mystr255copy (s, name);
  236.   pb.volumeParam.ioNamePtr = s;
  237.   if (name[1] == ':')
  238.     {
  239.       cwd = BootDrive;
  240.       pb.volumeParam.ioVolIndex = -1;
  241.       pb.volumeParam.ioVRefNum = BootDrive;
  242.       e = PBHGetVInfo (&pb, false);
  243.     }
  244.   else
  245.     {
  246.       mystr255copy (s, name);
  247.       for (i = 1; i < s[0] && s[i] != ':'; i++)
  248.         ;
  249.       s[0] = i;
  250.       pb.volumeParam.ioVolIndex = -1;
  251.       pb.volumeParam.ioVRefNum = 0;
  252.       while (i < 255 && s[i] == ':')
  253.         i++;
  254.       i--;
  255.       e = PBHGetVInfo (&pb, false);
  256.       done = (e != noErr);
  257.       cwd = pb.volumeParam.ioVRefNum;
  258.       mystr255copy (s, name);
  259.       s[i] = name[0] - i;
  260.       mystr255copy (name, (StringPtr) &s[i]);
  261.     
  262.       while (!done)
  263.     {
  264.       mystr255copy (s, name);
  265.       for (i = 1; i < s[0] && s[i] != ':'; i++)
  266.         ;
  267.       s[0] = i;
  268.       if (s[i] == ':')
  269.         {
  270.           s[0]--;
  271.           done = cd (&cwd, s);
  272.           s[i] = name[0] - i;
  273.           mystr255copy (name, (StringPtr) &s[i]);
  274.         }
  275.       else
  276.         done = true;
  277.       }
  278.       }
  279.     
  280.   cpb.hFileInfo.ioVRefNum = cwd;
  281.   cpb.hFileInfo.ioDirID = 0;
  282.   cpb.hFileInfo.ioFDirIndex = 0;
  283.   cpb.hFileInfo.ioNamePtr = name;
  284.   e = PBGetCatInfo (&cpb, false);
  285.  
  286.   if (e == noErr)
  287.     {
  288.       sigowners[TEXTEDITORPOS] = (applist **) NewHandle (sizeof (applist));
  289.       (*sigowners[TEXTEDITORPOS])->next = 0;
  290.       (*sigowners[TEXTEDITORPOS])->parid = cpb.hFileInfo.ioFlParID;
  291.       (*sigowners[TEXTEDITORPOS])->vrefnum = pb.volumeParam.ioVRefNum;
  292.       if (name[1] == ':')
  293.           remove_leading_dirs (name);
  294.       mystr255copy ((*sigowners[TEXTEDITORPOS])->name, name);
  295.     }
  296. }
  297.  
  298. OSErr
  299. add_item_to_system_file (ControlHandle c)
  300. {
  301.   short from_file_rn;
  302.   OSErr err;
  303.   
  304.   from_file_rn = openappres (c);
  305.   if (from_file_rn != -1)
  306.     {
  307.       err = AddToMasterFile (from_file_rn, g_helper_file_rn);
  308.       if (err != noErr)
  309.         {
  310.           ParamText((StringPtr) "\pError adding ", (*c)->contrlTitle, 
  311.                     (StringPtr) "\p to " , HELPER_FILE_NAME);
  312.           StopAlert(FOUR_PARAM_ALERT, 0);
  313.         }
  314.       CloseResFile(from_file_rn);
  315.         }
  316.   else
  317.     {
  318.       ParamText((StringPtr) "\pFile ", (*c)->contrlTitle,
  319.                 (StringPtr) "\p could not be opened.", (StringPtr) "\p");
  320.       NoteAlert(FOUR_PARAM_ALERT, 0);
  321.     }
  322.   return err;
  323. }
  324.  
  325. OSErr
  326. move_band_to_system_file (short which_band)
  327. {
  328.   short i;
  329.   OSErr err;
  330.  
  331.   err = noErr;
  332.   for (i = 0; i < bands[which_band].numitems && err == noErr; i++)
  333.     err = add_item_to_system_file ((**bands[which_band].items)[i]);
  334.   return err;
  335. }
  336.  
  337. OSErr
  338. build_helper_system_file (void)
  339. {
  340.   OSErr err;
  341.   ParamBlockRec pb;
  342.  
  343. /*
  344.  * PBCreate is not what we want here
  345.  * pb.ioParam.ioCompletion = 0;
  346.  * pb.ioParam.ioNamePtr = HELPER_FILE_NAME;
  347.  * pb.ioParam.ioVRefNum = BootDrive;
  348.  * err = PBCreate (&pb, false);
  349.  */
  350.  
  351.   err = SetVol ((StringPtr) 0, BootDrive);  
  352.   CreateResFile (HELPER_FILE_NAME);
  353.     
  354. /*
  355.  * PBOpenRF is used to do raw reads and writes to the resource fork.
  356.  * It is not what we want here.
  357.  * err = PBOpenRF (&pb, false);
  358.  * g_helper_file_rn = pb.ioParam.ioRefNum;
  359.  */
  360.  
  361.   if (err == noErr || err == dupFNErr)
  362.     {
  363.       THz save_zone;
  364.       
  365.       save_zone = GetZone ();
  366.       SetZone (SysZone);
  367.       g_helper_file_rn = OpenRFPerm (HELPER_FILE_NAME, BootDrive, fsRdWrPerm);
  368.       SetZone (save_zone);
  369.       err = ResError();
  370.     }
  371.   
  372.   if (err != noErr)
  373.     {
  374.       err = ResError ();
  375.       ParamText((StringPtr) "\pCouldn't create ", HELPER_FILE_NAME, (StringPtr) "\p", (StringPtr) "\p");
  376.       StopAlert(FOUR_PARAM_ALERT, 0);
  377. /*->*/return err;
  378.     }
  379.   err = move_band_to_system_file (DABAND);
  380.   if (err == noErr)
  381.     move_band_to_system_file (FONTBAND);
  382.   update_da_menu ();
  383.   return err;
  384. }
  385.  
  386. OSErr
  387. set_up_helper_system_file (void)
  388. {
  389.   OSErr retval;
  390. #if 0
  391.   ParamBlockRec pb;
  392.   
  393.   pb.ioParam.ioCompletion = 0;
  394.   pb.ioParam.ioNamePtr = HELPER_FILE_NAME;
  395.   pb.ioParam.ioVRefNum = BootDrive;
  396.   pb.ioParam.ioPermssn = fsRdWrPerm;
  397.   pb.ioParam.ioMisc = 0;
  398.   retval = PBOpenRF (&pb, false);
  399. #endif
  400.   
  401.   {
  402.     THz save_zone;
  403.     
  404.     save_zone = GetZone ();
  405.     SetZone (SysZone);
  406.     g_helper_file_rn = OpenRFPerm (HELPER_FILE_NAME, BootDrive, fsRdWrPerm);
  407.     SetZone (save_zone);
  408.   }
  409.   retval = ResError ();
  410.   if (retval == fnfErr)
  411.     retval = build_helper_system_file ();
  412. #if 0
  413.   else
  414.     g_helper_file_rn = pb.ioParam.ioRefNum;
  415. #endif
  416.   return retval;
  417. }
  418.  
  419. void
  420. init (void)
  421. {
  422.   FILE *f;
  423.  
  424. #if 0
  425. /* todo: find out how this is done these days */
  426.   mystr255copy (FinderName, GONAME);
  427. #endif /* 0 */
  428.  
  429.   InitGraf ((Ptr) & qd.thePort);
  430.   InitFonts ();
  431.   InitWindows ();
  432.   InitMenus ();
  433.   FlushEvents (everyEvent, 0);
  434.   TEInit ();
  435.   InitDialogs ((ProcPtr) nothing);        /* todo: write a resumeproc */
  436.  
  437.   g_selection = (ControlHandle (**)[])NewHandle (sizeof (ControlHandle) * 1);
  438.   (**g_selection)[0] = (ControlHandle) 0;
  439.   g_movecursor = GetCCursor (MOVECURSORID);
  440.   g_copycursor = GetCCursor (COPYCURSORID);
  441.  
  442. /* Arguments Schmarguments */
  443.   *(short *) (*AppParmHandle + 2) = 0;
  444.  
  445.   setdefaulteditor ();
  446.   verify_flags = VERIFY_OVERWRITE_FILE | VERIFY_OVERWRITE_FOLDER |
  447.     VERIFY_DELETE_FILE | VERIFY_DELETE_FOLDER;
  448.  
  449.   f = fopen (GOSAVEFILE, "r");
  450.   inithotband (f);
  451.   flush_screen_writes ();
  452.   if (f != 0)
  453.     {
  454.       initopendirs (f);
  455.       flush_screen_writes ();
  456.       fclose (f);
  457.     }
  458.  
  459.   set_up_helper_system_file ();
  460.   flush_screen_writes ();
  461.   setupmenus ();
  462.   InitCursor ();
  463. }
  464.