home *** CD-ROM | disk | FTP | other *** search
- #include "go.h"
- #include "xfer.h"
- #include "display.h"
- #include <string.h>
- #include <assert.h>
-
- #include "init.proto.h"
- #include "initicons.proto.h"
- #include "inithotband.proto.h"
- #include "initdirs.proto.h"
- #include "menu.proto.h"
- #include "easymenu.proto.h"
- #include "sharedtransfer.proto.h"
- #include "fontda.h"
-
- short g_helper_file_rn;
-
- void
- checkgrowitemarray (short numitems, ControlHandle (**items)[])
- {
-
- if (numitems % BANDARRAYSIZE == 0)
- {
- SetHandleSize((Handle) items, BANDARRAYSIZE * (numitems / BANDARRAYSIZE + 1)
- * sizeof (ControlHandle));
- }
- }
-
- void
- tail (char *path, Str255 s)
- {
- short i, j, len;
-
- len = strlen (path);
- i = len - 1;
- while (i > 0 && path[i] == ':')
- i--;
- while (i >= 0 && path[i] != ':' )
- i--;
- s[0] = len - i - 1;
- memcpy(s+1, path + i + 1, len - i);
- }
-
- void
- getname (Str255 name, FILE * f)
- {
- short i, c;
-
- name[0] = ' ';
- for (i = 1; (i < 255) && (name[i - 1] != ':') && (name[i - 1] != '\n'); i++)
- {
- c = getc (f);
- if (c == EOF)
- name[i] = '\n';
- else
- name[i] = c;
- }
- name[0] = i - 2;
- }
-
- void
- skip_line (FILE * f)
- {
- short c;
-
- while ((c = getc (f)) && (c != '\n') && (c != EOF))
- ;
- }
-
- OSErr
- cd (short *cwd, Str255 name)
- {
- short oldcwd;
- WDPBRec pb;
- OSErr e;
-
- #if 0
- if (executor_p ())
- {
- CInfoPBRec cpb;
- OSErr e2;
-
- cpb.hFileInfo.ioNamePtr = (StringPtr) name;
- cpb.hFileInfo.ioDirID = 0;
- cpb.hFileInfo.ioVRefNum = *cwd;
- cpb.hFileInfo.ioFDirIndex = 0;
-
- e2 = unixmount (&cpb);
- if (e2 == noErr)
- *cwd = cpb.hFileInfo.ioVRefNum;
- }
- #endif
-
- pb.ioNamePtr = (StringPtr) name;
- pb.ioWDDirID = 0;
- pb.ioWDProcID = CREATOR;
- pb.ioVRefNum = *cwd;
- e = PBOpenWD (&pb, false);
- if (e == noErr)
- {
- oldcwd = *cwd;
- *cwd = pb.ioVRefNum;
- pb.ioVRefNum = oldcwd;
- e = PBCloseWD (&pb, false);
- }
- return e;
- }
-
- void
- appenddir (char ***p, Str255 name)
- {
- short l;
- OSErr e;
-
- l = strlen (**p);
- (**p)[l] = ':';
- e = PtrAndHand (name + 1, *p, name[0] + 1);
- (**p)[l + name[0] + 1] = 0;
- }
-
- OSErr
- getonefileinfo (FILE * f, CInfoPBRec * cpb, Handle *path, short *volume)
- {
- ParamBlockRec pb;
- OSErr e;
- Str255 name;
- short cwd;
-
- pb.volumeParam.ioVolIndex = -1;
- pb.volumeParam.ioVRefNum = 0;
- getname (name, f);
- pb.volumeParam.ioNamePtr = name;
- assert(name[name[0] + 1] == ':');
- name[0]++;
- e = PBGetVInfo (&pb, false);
- if (e != noErr)
- {
- skip_line (f);
- return e;
- }
- *volume = pb.volumeParam.ioVRefNum;
- cwd = pb.volumeParam.ioVRefNum;
- *path = NewHandle (name[0] + 1);
- PtoCstr (name);
- strcpy (**path, (char *) name);
- getname (name, f);
- appenddir (path, name);
-
- while (name[name[0] + 1] == ':')
- {
- e = cd (&cwd, name);
- if (e != noErr)
- {
- skip_line (f);
- return e;
- }
- getname (name, f);
- appenddir (path, name);
- }
-
- cpb->hFileInfo.ioVRefNum = cwd;
- cpb->hFileInfo.ioDirID = 0;
- cpb->hFileInfo.ioFDirIndex = 0;
- cpb->hFileInfo.ioNamePtr = name;
- e = PBGetCatInfo (cpb, false);
-
- #if 1
- {
- HParamBlockRec hpb;
-
- /* Under Executor, the volume may have changed if we crossed a mount point */
- hpb.volumeParam.ioVolIndex = -1;
- hpb.volumeParam.ioVRefNum = cwd;
- hpb.volumeParam.ioNamePtr = name;
- PBHGetVInfo (&hpb, false);
- *volume = hpb.volumeParam.ioVRefNum;
- }
- #endif
- return e;
- }
-
- void
- setupmenus (void)
- {
- Handle h;
- MenuHandle mh;
-
- h = GetNewMBar (GOMBARID);
- SetMenuBar (h);
- DrawMenuBar ();
-
- mh = GetMHandle (FIRSTMENU);
- AddResMenu (mh, 'DRVR');
- menuchoices (false);
- showviewmenu (false);
- }
-
- static void
- remove_leading_dirs (StringPtr name)
- {
- int len;
- Byte *p;
-
- len = name[0];
- for (p = name+len; p > name && *p != ':'; --p)
- ;
- if (p > name)
- {
- len = name + len - p;
- name[0] = len;
- memcpy (name+1, p+1, len);
- }
- }
-
- void
- setdefaulteditor (void)
- {
- Str255 s, name;
- StringHandle sh;
- short i, done;
- short cwd;
- CInfoPBRec cpb;
- OSErr e;
- HParamBlockRec pb;
-
- sh = GetString (DEFAULTEDITORSTRINGID);
- HLock ((Handle) sh);
- mystr255copy (name, *sh);
- HUnlock ((Handle) sh);
-
- #if defined(OK_LETS_DISPOSE_RESOURCES_JUST_TO_BREAK_THINGS)
- DisposHandle ((Handle) sh);
- #endif
-
- mystr255copy (s, name);
- pb.volumeParam.ioNamePtr = s;
- if (name[1] == ':')
- {
- cwd = BootDrive;
- pb.volumeParam.ioVolIndex = -1;
- pb.volumeParam.ioVRefNum = BootDrive;
- e = PBHGetVInfo (&pb, false);
- }
- else
- {
- mystr255copy (s, name);
- for (i = 1; i < s[0] && s[i] != ':'; i++)
- ;
- s[0] = i;
- pb.volumeParam.ioVolIndex = -1;
- pb.volumeParam.ioVRefNum = 0;
- while (i < 255 && s[i] == ':')
- i++;
- i--;
- e = PBHGetVInfo (&pb, false);
- done = (e != noErr);
- cwd = pb.volumeParam.ioVRefNum;
- mystr255copy (s, name);
- s[i] = name[0] - i;
- mystr255copy (name, (StringPtr) &s[i]);
-
- while (!done)
- {
- mystr255copy (s, name);
- for (i = 1; i < s[0] && s[i] != ':'; i++)
- ;
- s[0] = i;
- if (s[i] == ':')
- {
- s[0]--;
- done = cd (&cwd, s);
- s[i] = name[0] - i;
- mystr255copy (name, (StringPtr) &s[i]);
- }
- else
- done = true;
- }
- }
-
- cpb.hFileInfo.ioVRefNum = cwd;
- cpb.hFileInfo.ioDirID = 0;
- cpb.hFileInfo.ioFDirIndex = 0;
- cpb.hFileInfo.ioNamePtr = name;
- e = PBGetCatInfo (&cpb, false);
-
- if (e == noErr)
- {
- sigowners[TEXTEDITORPOS] = (applist **) NewHandle (sizeof (applist));
- (*sigowners[TEXTEDITORPOS])->next = 0;
- (*sigowners[TEXTEDITORPOS])->parid = cpb.hFileInfo.ioFlParID;
- (*sigowners[TEXTEDITORPOS])->vrefnum = pb.volumeParam.ioVRefNum;
- if (name[1] == ':')
- remove_leading_dirs (name);
- mystr255copy ((*sigowners[TEXTEDITORPOS])->name, name);
- }
- }
-
- OSErr
- add_item_to_system_file (ControlHandle c)
- {
- short from_file_rn;
- OSErr err;
-
- from_file_rn = openappres (c);
- if (from_file_rn != -1)
- {
- err = AddToMasterFile (from_file_rn, g_helper_file_rn);
- if (err != noErr)
- {
- ParamText((StringPtr) "\pError adding ", (*c)->contrlTitle,
- (StringPtr) "\p to " , HELPER_FILE_NAME);
- StopAlert(FOUR_PARAM_ALERT, 0);
- }
- CloseResFile(from_file_rn);
- }
- else
- {
- ParamText((StringPtr) "\pFile ", (*c)->contrlTitle,
- (StringPtr) "\p could not be opened.", (StringPtr) "\p");
- NoteAlert(FOUR_PARAM_ALERT, 0);
- }
- return err;
- }
-
- OSErr
- move_band_to_system_file (short which_band)
- {
- short i;
- OSErr err;
-
- err = noErr;
- for (i = 0; i < bands[which_band].numitems && err == noErr; i++)
- err = add_item_to_system_file ((**bands[which_band].items)[i]);
- return err;
- }
-
- OSErr
- build_helper_system_file (void)
- {
- OSErr err;
- ParamBlockRec pb;
-
- /*
- * PBCreate is not what we want here
- * pb.ioParam.ioCompletion = 0;
- * pb.ioParam.ioNamePtr = HELPER_FILE_NAME;
- * pb.ioParam.ioVRefNum = BootDrive;
- * err = PBCreate (&pb, false);
- */
-
- err = SetVol ((StringPtr) 0, BootDrive);
- CreateResFile (HELPER_FILE_NAME);
-
- /*
- * PBOpenRF is used to do raw reads and writes to the resource fork.
- * It is not what we want here.
- * err = PBOpenRF (&pb, false);
- * g_helper_file_rn = pb.ioParam.ioRefNum;
- */
-
- if (err == noErr || err == dupFNErr)
- {
- THz save_zone;
-
- save_zone = GetZone ();
- SetZone (SysZone);
- g_helper_file_rn = OpenRFPerm (HELPER_FILE_NAME, BootDrive, fsRdWrPerm);
- SetZone (save_zone);
- err = ResError();
- }
-
- if (err != noErr)
- {
- err = ResError ();
- ParamText((StringPtr) "\pCouldn't create ", HELPER_FILE_NAME, (StringPtr) "\p", (StringPtr) "\p");
- StopAlert(FOUR_PARAM_ALERT, 0);
- /*->*/return err;
- }
- err = move_band_to_system_file (DABAND);
- if (err == noErr)
- move_band_to_system_file (FONTBAND);
- update_da_menu ();
- return err;
- }
-
- OSErr
- set_up_helper_system_file (void)
- {
- OSErr retval;
- #if 0
- ParamBlockRec pb;
-
- pb.ioParam.ioCompletion = 0;
- pb.ioParam.ioNamePtr = HELPER_FILE_NAME;
- pb.ioParam.ioVRefNum = BootDrive;
- pb.ioParam.ioPermssn = fsRdWrPerm;
- pb.ioParam.ioMisc = 0;
- retval = PBOpenRF (&pb, false);
- #endif
-
- {
- THz save_zone;
-
- save_zone = GetZone ();
- SetZone (SysZone);
- g_helper_file_rn = OpenRFPerm (HELPER_FILE_NAME, BootDrive, fsRdWrPerm);
- SetZone (save_zone);
- }
- retval = ResError ();
- if (retval == fnfErr)
- retval = build_helper_system_file ();
- #if 0
- else
- g_helper_file_rn = pb.ioParam.ioRefNum;
- #endif
- return retval;
- }
-
- void
- init (void)
- {
- FILE *f;
-
- #if 0
- /* todo: find out how this is done these days */
- mystr255copy (FinderName, GONAME);
- #endif /* 0 */
-
- InitGraf ((Ptr) & qd.thePort);
- InitFonts ();
- InitWindows ();
- InitMenus ();
- FlushEvents (everyEvent, 0);
- TEInit ();
- InitDialogs ((ProcPtr) nothing); /* todo: write a resumeproc */
-
- g_selection = (ControlHandle (**)[])NewHandle (sizeof (ControlHandle) * 1);
- (**g_selection)[0] = (ControlHandle) 0;
- g_movecursor = GetCCursor (MOVECURSORID);
- g_copycursor = GetCCursor (COPYCURSORID);
-
- /* Arguments Schmarguments */
- *(short *) (*AppParmHandle + 2) = 0;
-
- setdefaulteditor ();
- verify_flags = VERIFY_OVERWRITE_FILE | VERIFY_OVERWRITE_FOLDER |
- VERIFY_DELETE_FILE | VERIFY_DELETE_FOLDER;
-
- f = fopen (GOSAVEFILE, "r");
- inithotband (f);
- flush_screen_writes ();
- if (f != 0)
- {
- initopendirs (f);
- flush_screen_writes ();
- fclose (f);
- }
-
- set_up_helper_system_file ();
- flush_screen_writes ();
- setupmenus ();
- InitCursor ();
- }
-