summaryrefslogtreecommitdiff
path: root/apple_main.c
blob: 0166b577c926508b49b80584ebbdb0ef20323d43 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
#define CLEAN2
#define CG_PPC_XO

#ifndef CLEAN2
# include "compiledefines.h"
#endif

#ifdef KARBON
# define TARGET_API_MAC_CARBON 1
#endif

#include <stdio.h>
#ifndef PROJECT_BUILDER
# include <unix.h>
# include <SIOUX.h>
#endif

#include <quickdraw.h>
#include <fonts.h>
#include <events.h>
#include <windows.h>
#include <memory.h>
#include <resources.h>
#include <menus.h>
#include <OSUtils.h>
#include "AppleEvents.h"
#include "Gestalt.h"
#include "AERegistry.h"

#ifndef CLEAN2
#include "types.t"
#include "system.h"
#include "path_cache.h"
#include "compiler.h"
#endif

extern void clear_inline_cache (void);

#undef BACKGROUND
#define MW_DEBUG 0
#define NO68K

#ifndef BACKGROUND
#	undef NO_REDIRECT_STDFILES
#	undef STDIO_WINDOW
#endif

#define LINKER
#define CODE_GENERATOR
#undef PROFILE

#if 1

#define kSleepMax 50000
 
static Boolean gAppleEventsFlag, gQuitFlag;
static long gSleepVal;

static pascal OSErr DoAEOpenApplication (const AppleEvent *theAppleEvent,AppleEvent *replyAppleEvent,long refCon)
{
	return noErr;
}
 
static pascal OSErr DoAEOpenDocuments (const AppleEvent *theAppleEvent,AppleEvent *replyAppleEvent,long refCon)
{
	return errAEEventNotHandled;
}
 
static pascal OSErr DoAEPrintDocuments (const AppleEvent *theAppleEvent,AppleEvent *replyAppleEvent,long refCon)
{
	return errAEEventNotHandled;
}
 
static pascal OSErr DoAEQuitApplication (const AppleEvent *theAppleEvent,AppleEvent *replyAppleEvent,long refCon)
{
	gQuitFlag = true;
	return noErr;
}

#include <string.h>

extern int CallCompiler (int argc,char **argv);

#ifdef CODE_GENERATOR
# ifdef __cplusplus
 extern "C" { int generate_code (int,char **); }
#  ifndef NO68K
 extern int generate_code68 (int,char **);
#  endif
# else
 extern int generate_code (int,char **);
#  ifndef NO68K
 extern int generate_code68__FiPPc (int,char **);
#define generate_code68 generate_code68__FiPPc
#  endif
# endif
#endif

#if defined (LINKER) && !defined (NO68K)
# ifdef __cplusplus
 extern "C" { int link_application_argc_argv (int,char **); }
# else
 extern int link_application_argc_argv (int,char **);
# endif
#endif

char return_error_string[200];

#ifdef CG_PPC_XO
extern int generate_code_xo (int argc,char **argv,char *return_error_string_p,int *compiler_id_p);
extern int generate_code_o (int argc,char **argv,char *return_error_string_p,int *compiler_id_p);
#endif

#ifdef CLEAN2
int compiler_id=-1;
#else
extern int compiler_id;
#endif

int do_command (char *command)
{
	char *p,*(argv[257]);
	int argc,result;
	int redirect_stdout,redirect_stderr;

	result=0;
	
	redirect_stdout=0;
	redirect_stderr=0;
	
	argc=0;
	p=command;

	while (*p==' ' || *p=='\t')
		++p;

	while (*p!='\0' && argc<256){
		if (*p=='>' || *p=='�'){
			int redirection_char;
			char *file_name;
			
			redirection_char=*p;
			
			++p;
			while (*p==' ' || *p=='\t')
				++p;
			if (*p=='\0')
				break;

			if (*p=='\''){
				char c,*d_p;
				
				++p;
				file_name=p;
				
				d_p=p;
				
				c=*p;
				while (!(c=='\'' && p[1]!='\'') && c!='\0'){
					*d_p++=c;
					if (c=='\'')
						++p;
					c=*++p;
				}
				
				if (*p=='\0'){
					*d_p='\0';
					break;
				}
				
				*d_p='\0';
				++p;
			} else {
				file_name=p;
				
				while (*p!=' ' && *p!='\t' && *p!='\0')
					++p;
				if (*p!='\0')
					*p++='\0';
			}

#ifdef PROJECT_BUILDER
			{
				static char file_name_s[257];
				char *p;
				
				strcpy (file_name_s,"/Volumes/");
				strcat (file_name_s,file_name);
				
				for (p=file_name_s; *p!='\0'; ++p)
					if (*p==':')
						*p='/';
				
				file_name=file_name_s;
			}
#endif
			
			if (redirection_char=='>' && redirect_stdout==0){
#ifndef NO_REDIRECT_STDFILES
				freopen (file_name,"w",stdout);
				redirect_stdout=1;
#endif
			} else if (redirection_char=='�' && redirect_stderr==0){
#ifndef NO_REDIRECT_STDFILES
				freopen (file_name,"w",stderr);
				redirect_stderr=1;
#endif
			}

			if (*p=='\0')
				break;
			
			while (*p==' ' || *p=='\t')
				++p;
			continue;
		}

		if (*p=='\''){
			char c,*d_p;
			
			++p;
			argv[argc]=p;

			d_p=p;
			
			c=*p;
			while (!(c=='\'' && p[1]!='\'') && c!='\0'){
				*d_p++=c;
				if (c=='\'')
					++p;
				c=*++p;
			}
			
			if (*p=='\0'){
				*d_p='\0';
				break;
			}
			
			++argc;
			*d_p='\0';
			++p;
		} else {
			argv[argc++]=p;
			while (*p!=' ' && *p!='\t' && *p!='\0')
				++p;
	
			if (*p!='\0')
				*p++='\0';
		}
				
		while (*p==' ' || *p=='\t')
			++p;			
	}
	argv[argc]=NULL;

/*	{
		int n;
		
		for (n=0; n<argc; ++n)
			printf ("%d %s\n",n,argv[n]);
	}
*/

	if (argc>0){
#ifdef CLEAN2
		if (0)
			;
#else
		if (!strcmp (argv[0],"cocl")){
			if (argc>=2 && !strcmp ("-clear_cache",argv[1])){
				result=CallCompiler (argc-2,&argv[2]);
				clear_path_cache();
				clear_inline_cache();
				FreePathList();	
			} else
				result=CallCompiler (argc-1,&argv[1]);
		}
#endif
#ifdef CODE_GENERATOR
		else if (!strcmp (argv[0],"cg"))
			result=generate_code (argc,&argv[0]);
#ifdef CG_PPC_XO
		else if (!strcmp (argv[0],"cg_xo"))
			result=generate_code_xo (argc,&argv[0],return_error_string,&compiler_id);
		else if (!strcmp (argv[0],"cg_o"))
			result=generate_code_o (argc,&argv[0],return_error_string,&compiler_id);
#endif

# ifndef NO68K
		else if (!strcmp (argv[0],"cg68"))
			result=generate_code68 (argc,&argv[0]);
# endif
#endif
#if defined (LINKER) && !defined (NO68K)
		else if (!strcmp (argv[0],"linker"))
			result=link_application_argc_argv (argc,&argv[0]);
#endif
#ifndef CLEAN2
		else if (!strcmp (argv[0],"clear_cache")){		
			clear_path_cache();
			clear_inline_cache();
			FreePathList();
		}
#endif
		else {
			result=-1;
			strcpy (return_error_string,"unknown command");
		}
	}

	if (redirect_stdout)
		fclose (stdout);
	
	if (redirect_stderr)
		fclose (stderr);
			
	return result;
}

static char script_string[16001];

#ifndef CG_PPC_XO
# ifdef CLEAN2
int compiler_id=-1;
# else
extern int compiler_id;
# endif
#endif

pascal OSErr do_script_apple_event (const AppleEvent *apple_event,AppleEvent *replyAppleEvent,long refCon)
{
	DescType returned_type;
	long actual_size;
	int error;
	
	error=AEGetParamPtr (apple_event,keyDirectObject,'TEXT',&returned_type,&script_string,sizeof (script_string),&actual_size);

	if (error==noErr && actual_size<=16000){
		int return_error_string_length;
		
		script_string[actual_size]='\0';
		return_error_string[0]='\0';

#if !MW_DEBUG
		error=do_command (script_string);
#endif

		if (compiler_id>=0){
			error += (compiler_id+1)<<1;

			compiler_id = -1;
		}

		return_error_string_length=strlen (return_error_string);
		if (return_error_string_length!=0){
			AEPutParamPtr (replyAppleEvent,keyErrorString,typeChar,return_error_string,return_error_string_length);
		}
	}
	
	return error;
}

#if defined (PROJECT_BUILDER) || defined (KARBON)
# define NewAEEventHandlerProc(userRoutine) NewAEEventHandlerUPP(userRoutine)
#endif

static void InitAppleEventsStuff (void)
{
	OSErr retCode;

	if (!gAppleEventsFlag)
		return;
	
	retCode = AEInstallEventHandler (kCoreEventClass,kAEOpenApplication,NewAEEventHandlerProc (DoAEOpenApplication),0,false);

	if (retCode==noErr)
		retCode = AEInstallEventHandler (kCoreEventClass,kAEOpenDocuments,NewAEEventHandlerProc (DoAEOpenDocuments),0,false);

	if (retCode==noErr)
		retCode = AEInstallEventHandler (kCoreEventClass,kAEPrintDocuments,NewAEEventHandlerProc (DoAEPrintDocuments),0,false);

	if (retCode==noErr)
		retCode = AEInstallEventHandler (kCoreEventClass,kAEQuitApplication,NewAEEventHandlerProc (DoAEQuitApplication),0,false);

	if (retCode==noErr)
		retCode = AEInstallEventHandler (kAEMiscStandards,kAEDoScript,NewAEEventHandlerProc (do_script_apple_event),0,false);

	if (retCode!=noErr)
		DebugStr("\pInstall event handler failed");
}

static void do_high_level_event (EventRecord *theEventRecPtr)
{
#if MW_DEBUG
	script_string[0]=0;
#endif

	AEProcessAppleEvent (theEventRecPtr);
	
#if MW_DEBUG
	if (script_string[0]){
		do_command (script_string);
		script_string[0]=0;
	}
#endif

}
 
extern short InstallConsole (short fd);

#ifdef PROFILE
# include <Profiler.h>
#endif

#ifndef PROJECT_BUILDER

int /*clean_compiler_*/ main (void)
{
	OSErr retCode;
	long gestResponse;
	EventRecord mainEventRec;
	Boolean eventFlag;

#ifndef KARBON
	SetApplLimit (GetApplLimit() - 200*1024);

	InitGraf (&qd.thePort);
	InitFonts();
#endif
	FlushEvents (everyEvent,0);

#ifndef BACKGROUND
# ifndef KARBON
	InitWindows();
# endif
	InitCursor();
# ifndef KARBON
	InitMenus();
# endif
#endif

#ifndef PROJECT_BUILDER
	_fcreator='3PRM';
#endif
	gQuitFlag = false;
	gSleepVal = kSleepMax;
	
	retCode = Gestalt(gestaltAppleEventsAttr,&gestResponse);
	if (retCode==noErr && (gestResponse & (1<<gestaltAppleEventsPresent))!=0)
		gAppleEventsFlag = true;
	else
		gAppleEventsFlag = false;

#if defined (STDIO_WINDOW)
	SIOUXSettings.autocloseonquit=1;
	SIOUXSettings.showstatusline=0;
	SIOUXSettings.asktosaveonclose=0;

	printf ("\n");
#endif

#if !defined (BACKGROUND) && !defined (STDIO_WINDOW)
 	fclose (stdout);
 	fclose (stderr);
#endif
 
	InitAppleEventsStuff();

#ifdef PROFILE
	if (ProfilerInit(/*collectSummary*/collectDetailed,bestTimeBase,10000,10)!=0)
		return 0;
#endif
    
	while (!gQuitFlag) {
		eventFlag = WaitNextEvent (everyEvent,&mainEventRec,gSleepVal,nil);

#ifdef STDIO_WINDOW
		if (SIOUXHandleOneEvent (&mainEventRec))
			continue;
#endif
		if (mainEventRec.what==keyDown)
			break;
		
		if (mainEventRec.what==kHighLevelEvent)
			do_high_level_event (&mainEventRec);
	}

#ifdef PROFILE
	ProfilerDump ("\pProfile");
	ProfilerTerm();
#endif
	
	return 1;
}

#endif

#endif