1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Construct argc and argv for main() by using Apple Events */
26 /* From Jack's implementation for STDWIN */
30 #ifndef SystemSevenOrLater
31 #define SystemSevenOrLater 1
38 #include <Processes.h>
40 #include <AppleEvents.h>
41 #include <AEObjects.h>
49 #ifdef GENERATINGCFM /* Defined to 0 or 1 in Universal headers */
50 #define HAVE_UNIVERSAL_HEADERS
57 #ifndef HAVE_UNIVERSAL_HEADERS
58 #define NewAEEventHandlerProc(x) (x)
59 #define AEEventHandlerUPP EventHandlerProcPtr
63 static char *arg_vector
[256];
65 /* Duplicate a string to the heap */
70 char *dst
= malloc(strlen(src
) + 1);
76 /* Return FSSpec of current application */
79 current_process_location(FSSpec
*applicationSpec
)
81 ProcessSerialNumber currentPSN
;
84 currentPSN
.highLongOfPSN
= 0;
85 currentPSN
.lowLongOfPSN
= kCurrentProcess
;
86 info
.processInfoLength
= sizeof(ProcessInfoRec
);
87 info
.processName
= NULL
;
88 info
.processAppSpec
= applicationSpec
;
89 return GetProcessInformation(¤tPSN
, &info
);
92 /* Given an FSSpec, return the FSSpec of the parent folder */
95 get_folder_parent (FSSpec
* fss
, FSSpec
* parent
)
101 rec
.hFileInfo
.ioNamePtr
= parent
->name
;
102 rec
.hFileInfo
.ioVRefNum
= parent
->vRefNum
;
103 rec
.hFileInfo
.ioDirID
= parent
->parID
;
104 rec
.hFileInfo
.ioFDirIndex
= -1;
105 rec
.hFileInfo
.ioFVersNum
= 0;
106 if (err
= PBGetCatInfoSync (& rec
))
108 parent
->parID
= rec
.dirInfo
.ioDrParID
;
109 /* parent->name[0] = 0; */
113 /* Given an FSSpec return a full, colon-separated pathname */
116 get_full_path (FSSpec
*fss
, char *buf
)
119 FSSpec fss_parent
, fss_current
;
124 plen
= fss_current
.name
[0];
125 memcpy(buf
, &fss_current
.name
[1], plen
);
127 while (fss_current
.parID
> 1) {
128 /* Get parent folder name */
129 if (err
= get_folder_parent(&fss_current
, &fss_parent
))
131 fss_current
= fss_parent
;
132 /* Prepend path component just found to buf */
133 plen
= fss_current
.name
[0];
134 if (strlen(buf
) + plen
+ 1 > 256) {
135 /* Oops... Not enough space (shouldn't happen) */
139 memcpy(tmpbuf
, &fss_current
.name
[1], plen
);
141 strcpy(&tmpbuf
[plen
+1], buf
);
147 /* Return the full program name */
150 get_application_name()
152 static char appname
[256];
155 if (current_process_location(&appspec
))
157 if (get_full_path(&appspec
, appname
))
162 /* Check that there aren't any args remaining in the event */
165 get_missing_params(AppleEvent
*theAppleEvent
)
171 err
= AEGetAttributePtr(theAppleEvent
, keyMissedKeywordAttr
, typeWildCard
,
172 &theType
, nil
, 0, &actualSize
);
173 if (err
== errAEDescNotFound
)
176 return errAEEventNotHandled
;
179 static int got_one
; /* Flag that we can stop getting events */
181 /* Handle the Print or Quit events (by failing) */
184 handle_not(AppleEvent
*theAppleEvent
, AppleEvent
*reply
, long refCon
)
186 #pragma unused (reply, refCon)
188 return errAEEventNotHandled
;
191 /* Handle the Open Application event (by ignoring it) */
194 handle_open_app(AppleEvent
*theAppleEvent
, AppleEvent
*reply
, long refCon
)
196 #pragma unused (reply, refCon)
198 return get_missing_params(theAppleEvent
);
201 /* Handle the Open Document event, by adding an argument */
204 handle_open_doc(AppleEvent
*theAppleEvent
, AppleEvent
*reply
, long refCon
)
206 #pragma unused (reply, refCon)
216 if (err
= AEGetParamDesc(theAppleEvent
,
217 keyDirectObject
, typeAEList
, &doclist
))
219 if (err
= get_missing_params(theAppleEvent
))
221 if (err
= AECountItems(&doclist
, &ndocs
))
223 for(i
= 1; i
<= ndocs
; i
++) {
224 err
= AEGetNthPtr(&doclist
, i
, typeFSS
,
225 &keywd
, &rttype
, &fss
, sizeof(fss
), &size
);
228 get_full_path(&fss
, path
);
229 arg_vector
[arg_count
++] = strdup(path
);
234 /* Install standard core event handlers */
239 AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
240 NewAEEventHandlerProc(handle_open_app
), 0L, false);
241 AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
242 NewAEEventHandlerProc(handle_open_doc
), 0L, false);
243 AEInstallEventHandler(kCoreEventClass
, kAEPrintDocuments
,
244 NewAEEventHandlerProc(handle_not
), 0L, false);
245 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
246 NewAEEventHandlerProc(handle_not
), 0L, false);
249 /* Uninstall standard core event handlers */
254 AERemoveEventHandler(kCoreEventClass
, kAEOpenApplication
,
255 NewAEEventHandlerProc(handle_open_app
), false);
256 AERemoveEventHandler(kCoreEventClass
, kAEOpenDocuments
,
257 NewAEEventHandlerProc(handle_open_doc
), false);
258 AERemoveEventHandler(kCoreEventClass
, kAEPrintDocuments
,
259 NewAEEventHandlerProc(handle_not
), false);
260 AERemoveEventHandler(kCoreEventClass
, kAEQuitApplication
,
261 NewAEEventHandlerProc(handle_not
), false);
264 /* Wait for events until a core event has been handled */
274 for (n
= 0; n
< 100 && !got_one
; n
++) {
276 ok
= GetNextEvent(everyEvent
, &event
);
277 if (ok
&& event
.what
== kHighLevelEvent
) {
278 AEProcessAppleEvent(&event
);
283 /* Initialize the Mac toolbox world */
292 InitGraf(&qd
.thePort
);
296 InitDialogs((long)0);
301 /* Get the argv vector, return argc */
310 arg_vector
[arg_count
++] = strdup(get_application_name());
316 arg_vector
[arg_count
] = NULL
;