home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright (C) 1993, 1994 Marc Parmet.
- * This file is part of the Macintosh port of GNU Emacs.
- *
- * GNU Emacs is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- */
-
- #if defined(THINK_C)
- #include <MacHeaders>
- #else
- #include <Types.h>
- #include <Memory.h>
- #include <Quickdraw.h>
- #include <Windows.h>
- #include <Fonts.h>
- #include <Dialogs.h>
- #include <TextEdit.h>
- #include <ToolUtils.h>
- #include <Resources.h>
- #endif
-
- #include <GestaltEqu.h>
- #include <AppleEvents.h>
- #include "sys/param.h"
- #include "unix-types.h"
-
- static Cursor watch,beachball[8];
- static int init_argc;
- static char ***init_argv;
-
- void
- spin_beachball(void)
- {
- static char k;
- SetCursor(&beachball[k++ & 7]);
- }
-
- static int
- patch(char **h,long code,long replacement)
- {
- long i,hsize,*p;
-
- hsize = GetHandleSize(h);
- if (MemError()) return -1;
-
- for (i = 0; i<hsize-3; i += 2) {
- p = (long *)(*h + i);
- if (*p == code) {
- *p = replacement;
- return 0;
- }
- }
-
- return -1;
- }
-
- static int
- have_required_params(AppleEvent *e)
- {
- short err;
- long actualSize;
- DescType returnedType;
-
- err = AEGetAttributePtr(e,keyMissedKeywordAttr,typeWildCard,
- &returnedType,0L,0,&actualSize);
-
- if (err == errAEDescNotFound)
- return 0;
- else if (err == 0)
- return errAEEventNotHandled;
- else
- return err;
- }
-
- static pascal short
- do_ae_peel_args(AppleEvent *e,AppleEvent *reply,long refCon)
- {
- FSSpec fs;
- short err;
- FSSpec cwd;
- int have_cwd;
- CInfoPBRec pb;
- AEKeyword keywd;
- char *t,**s;
- unsigned char u[256];
- AEDescList docList;
- DescType returnedType;
- long i,itemsInList,actualSize;
-
- err = AEGetParamDesc(e,keyDirectObject,typeAEList,&docList);
- if (err) return err;
- err = have_required_params(e);
- if (err) return err;
- err = AECountItems(&docList,&itemsInList);
- if (err) return err;
- have_cwd = 0;
- for (i = 1; i<=itemsInList; ++i) {
- err = AEGetNthPtr(&docList,i,typeFSS,&keywd,&returnedType,&fs,
- sizeof(FSSpec),&actualSize);
- if (err) continue;
-
- if (!have_cwd) {
- pb.dirInfo.ioFDirIndex = -1;
- pb.dirInfo.ioNamePtr = u;
- pb.dirInfo.ioVRefNum = fs.vRefNum;
- pb.dirInfo.ioDrDirID = fs.parID;
- err = PBGetCatInfo(&pb,0);
- if (!err) {
- err = FSMakeFSSpec(fs.vRefNum,pb.dirInfo.ioDrParID,
- pb.dirInfo.ioNamePtr,&cwd);
- if (!err) {
- err = FSSpec2unixfn_internal(&cwd,&s);
- if (!err) {
- HLock(s);
- chdir_internal(*s);
- DisposHandle(s);
- have_cwd = 1;
- }
- }
- }
- }
-
- err = FSSpec2unixfn_internal(&fs,&s);
- if (err) return err;
- SetHandleSize((Handle)init_argv,(init_argc+1) * sizeof(char *));
- err = MemError();
- if (err) { DisposHandle(s); return err; }
- HLock(s);
- t = NewPtr(strlen(*s) + 1);
- (*init_argv)[init_argc] = t;
- err = MemError();
- if (err) { DisposHandle(s); return err; }
- strcpy((char *)(*init_argv)[init_argc],*s);
- DisposHandle(s);
- ++init_argc;
- }
-
- err = AEDisposeDesc(&docList);
- if (err) return err;
- return 0;
- }
-
- static pascal Boolean
- specific_filter(Ptr p,HighLevelEventMsgPtr mb,TargetID sender)
- {
- short err;
- long class,id;
- EventRecord *e = &mb->theMsgEvent;
-
- class = e->message;
- id = *(long *)&e->where;
- if (class == kCoreEventClass && id == kAEOpenDocuments) {
- err = AEProcessAppleEvent(e);
- return -1;
- }
- else
- return 0;
- }
-
- static pascal short
- do_nothing(AppleEvent *ae,AppleEvent *reply,long refCon)
- {
- return errAEEventNotHandled;
- }
-
- static void
- build_argument_list(void)
- {
- char *t,**apParam;
- short err,apRefNum;
- unsigned char s[256];
- static GetSpecificFilterUPP specific_filter_upp;
-
- if (specific_filter_upp == 0L)
- specific_filter_upp = NewGetSpecificFilterProc(specific_filter);
-
- init_argc = 1;
- init_argv = (char ***)NewHandle(1 * sizeof(char *));
- if (MemError()) ExitToShell();
- /* GetAppParms(s,&apRefNum,&apParam); */
- pstrcpy(s,"\pEmacs");
- t = NewPtr(s[0] + 1L);
- if (MemError()) ExitToShell();
- (*init_argv)[0] = t;
- PtoCstr(s);
- strcpy((*init_argv)[0],(char *)s);
-
- while (GetSpecificHighLevelEvent(specific_filter_upp,0L,&err))
- if (err) break;
-
- // Add null pointer to end of argv.
- SetHandleSize((Handle)init_argv,(init_argc+1) * sizeof(char *));
- if (MemError()) ExitToShell();
- (*init_argv)[init_argc] = 0L;
- }
-
- void
- init_unix(int stacksize,char ***environ_address,int *argc,char ****argv)
- {
- short err;
- FSSpec spec;
- int i,fd,appsize,n;
- SelectorFunctionUPP oldfunc;
- char *t,**s,**dispatch,u[MAXPATHLEN];
- char *appbottom,*apptop;
- static AEEventHandlerUPP do_ae_peel_args_upp,do_nothing_upp;
- static SelectorFunctionUPP dispatch_upp;
- extern int unix_entry(int,int *,va_list);
- UniversalProcPtr unix_entry_upp;
-
- appbottom = (char *)ApplicationZone();
- apptop = (char *)&appbottom;
- appsize = apptop - appbottom;
- SetApplLimit(appbottom + appsize - stacksize);
- MaxApplZone();
- // StackSpace(); DebugStr("\pStack size is in d0;d0");
-
- if (do_ae_peel_args_upp == 0L) {
- do_ae_peel_args_upp = NewAEEventHandlerProc(do_ae_peel_args);
- do_nothing_upp = NewAEEventHandlerProc(do_nothing);
-
- /* Must make sure routine descriptor is in the system heap */
- dispatch = GetResource('GDEF',128);
- if (dispatch == 0L) goto error;
- DetachResource(dispatch);
- HLock(dispatch);
- HNoPurge(dispatch);
- #if defined(powerc)
- SetZone(SystemZone());
- dispatch_upp = NewRoutineDescriptor((long (*)())*dispatch,
- uppSelectorFunctionProcInfo,kM68kISA);
- SetZone(ApplicZone());
- #else
- dispatch_upp = NewSelectorFunctionProc((long (*)())*dispatch);
- #endif
- unix_entry_upp = NewRoutineDescriptor((long (*)())unix_entry,
- unix_entry_ProcInfo,GetCurrentISA());
- err = patch(dispatch,0x89abcdef,(long)unix_entry_upp);
- if (err) goto error;
- }
-
- LMStkLowPt = 0L;
-
- InitGraf(&qd.thePort);
- FlushEvents(everyEvent,0);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
-
- object_list = 0L;
- init_proc();
-
- err = 0;
- err |= AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,do_ae_peel_args_upp,0,0);
- err |= AEInstallEventHandler(kCoreEventClass,kAEPrintDocuments,do_nothing_upp,0,0);
- err |= AEInstallEventHandler(kCoreEventClass,kAEQuitApplication,do_nothing_upp,0,0);
- err |= AEInstallEventHandler(kCoreEventClass,kAEOpenApplication,do_nothing_upp,0,0);
- if (err) goto error;
- build_argument_list();
- *argc = init_argc;
- *argv = init_argv;
- err = AEInstallEventHandler(kCoreEventClass,kAEOpenDocuments,do_nothing_upp,0,0);
- if (err) goto error;
- if (getwd_internal(u) == 0L) chdir_internal("~/");
-
- watch = **GetCursor(watchCursor);
- beachball[0] = watch;
- for (i = 1; i<8; ++i)
- beachball[i] = **GetCursor(128+i-1);
- SetCursor(&watch);
-
- unix_init_kbd();
-
- err = NewGestalt('uniX',dispatch_upp);
- if (err) {
- err = ReplaceGestalt('uniX',dispatch_upp,&oldfunc);
- if (err) goto error;
- }
-
- fd = open_internal("/dev/tty",0);
- dup_internal(fd);
- dup_internal(fd);
-
- err = unixfn2FSSpec_internal("~/",&spec,0);
- if (err) goto error;
- err = FSSpec2unixfn_internal(&spec,&s);
- if (err) goto error;
- t = NewPtr(GetHandleSize(s) + 5);
- if (MemError()) goto error;
- strcpy(t,"HOME=");
- strcat(t,*s);
- DisposHandle(s);
- if (environ_address) {
- *environ_address = (char **)NewPtr(2 * sizeof(char *));
- if (MemError()) goto error;
- (*environ_address)[0] = t;
- (*environ_address)[1] = 0L;
- proctable[current_process_index].environ_address = environ_address;
- }
-
- return;
-
- error:
- ExitToShell();
- }
-