Diff of /shell_mac.c [000000] .. [e19029]  Maximize  Restore

  Switch to unified view

a b/shell_mac.c
1
#include <Controls.h>
2
#include <TextUtils.h>
3
#include <Menus.h>
4
#include <Errors.h>
5
#include <StandardFile.h>
6
#include <ToolUtils.h>
7
#include <Devices.h>
8
#include <Folders.h>
9
#include <Events.h>
10
#include <LowMem.h>
11
#include <Dialogs.h>
12
#include <MacMemory.h>
13
#include <Files.h>
14
#include <LowMem.h>
15
#include <stdio.h>
16
#include <stdlib.h>
17
#include <stdarg.h>
18
#include <Files.h>
19
#include <Fonts.h>
20
#include "filter.h"
21
#include <AppleEvents.h>
22
#include <AERegistry.h>
23
24
25
26
static  AEEventHandlerUPP OAPPHandlerUPP, ODOCHandlerUPP, PDOCHandlerUPP, 
27
                          QUITHandlerUPP, DOSCHandlerUPP;
28
29
30
31
static void           EventInit( void );
32
static pascal OSErr   DoOpenApp( AppleEvent *theAppleEvent,AppleEvent *reply, long refCon);
33
static pascal OSErr   DoOpenDoc( AppleEvent *theAppleEvent,AppleEvent *reply, long refCon);
34
static pascal OSErr   DoScript(  AppleEvent *theAppleEvent,AppleEvent *reply, long refCon);
35
static void       SetUpMenus(void);
36
static void       DoCommand(long mResult);
37
38
static OSErr      GotRequiredParams( AppleEvent *appleEventPtr );
39
static int            FullToFolder(StringPtr name, short *volume, long *folder);
40
41
42
43
// Globals
44
45
static Boolean        gDone = false;
46
47
static QDGlobals  qd;
48
static int            argc;
49
static char       **argv, appName[64];
50
51
int ccommand( char ***argvPtr)
52
{
53
  int                     i;
54
  EventRecord             event;
55
  OSErr                   myErr = noErr;
56
  fullPath                app;
57
  Point                   thePoint;
58
  short                   thePart;
59
  WindowPtr               whichWindow;
60
  unsigned char           theChar;
61
  
62
  
63
  InitGraf(&qd.thePort);
64
  InitFonts();
65
  InitWindows();
66
  InitMenus();
67
  FlushEvents(everyEvent,0);
68
  TEInit();
69
  InitDialogs( nil );
70
  InitCursor();
71
  MaxApplZone();
72
73
  for(i=0; i<10; i++)
74
      MoreMasters();
75
76
  setLibToResFile();
77
  
78
  if( makePathToHost ( &app ) == 0 )
79
  {
80
      p2cstr( app.name );
81
      strcpy( appName, (char*)app.name);
82
  }
83
  else
84
      strcpy( appName, "dummy");
85
      
86
87
88
  EventInit();
89
  SetUpMenus();
90
  
91
  argc = 0; argv = nil;
92
93
94
  while( !gDone )
95
  {
96
      if ( WaitNextEvent ( everyEvent, &event, 20L, nil) )
97
      {
98
          switch ( event.what )
99
          {
100
              case    kHighLevelEvent:
101
                  AEProcessAppleEvent( &event );
102
                  break;
103
              case mouseDown:
104
                  thePoint = event.where;
105
                  switch (thePart = FindWindow(thePoint, &whichWindow)) 
106
                  {
107
                      case inSysWindow:
108
                          SystemClick(&event, whichWindow);
109
                          break;
110
111
                      case inMenuBar:
112
                          DoCommand(MenuSelect(thePoint));
113
                          break;
114
                  }
115
                  break;
116
              case keyDown:
117
                  case autoKey:
118
                  theChar = (unsigned char)BitAnd(event.message, charCodeMask);
119
                  if (BitAnd(event.modifiers, cmdKey) != 0) 
120
                          DoCommand(MenuKey(theChar));
121
                  break;
122
          }
123
      }                     
124
  }
125
  
126
  *argvPtr = argv;
127
  return argc;
128
}
129
  
130
  
131
  
132
static void   EventInit( void )
133
{
134
  OSErr   err;
135
  OAPPHandlerUPP = NewAEEventHandlerProc(DoOpenApp);
136
  err = AEInstallEventHandler ( kCoreEventClass, kAEOpenApplication, OAPPHandlerUPP, 0L, false );
137
  
138
  ODOCHandlerUPP = NewAEEventHandlerProc(DoOpenDoc);
139
  if( err == noErr) err = AEInstallEventHandler ( kCoreEventClass, kAEOpenDocuments, ODOCHandlerUPP, 0L, false );
140
141
  PDOCHandlerUPP = NewAEEventHandlerProc(DoOpenApp);
142
  if( err == noErr) err = AEInstallEventHandler ( kCoreEventClass, kAEPrintDocuments, PDOCHandlerUPP, 0L, false );
143
  
144
  QUITHandlerUPP = NewAEEventHandlerProc(DoOpenApp);
145
  if( err == noErr) err = AEInstallEventHandler ( kCoreEventClass, kAEQuitApplication, QUITHandlerUPP, 0L, false );
146
  
147
  DOSCHandlerUPP = NewAEEventHandlerProc(DoScript);
148
  if( err == noErr) err = AEInstallEventHandler ( kAEMiscStandards, kAEDoScript, DOSCHandlerUPP, 0L, false);
149
150
  if( err != noErr) PrintError("Could not initialize AE Handler");
151
  return;
152
}
153
154
static pascal OSErr   DoOpenApp( AppleEvent *theAppleEvent,
155
                      AppleEvent *reply, long refCon)
156
{
157
  OSErr       err = noErr;
158
  return err;
159
}
160
161
162
static pascal OSErr   DoScript( AppleEvent *theAppleEvent,
163
                      AppleEvent *reply, long refCon)
164
{
165
  DescType    returned;
166
  OSErr       err = noErr;
167
  long        length;
168
  char        *theScript, *ch, *c;
169
  int         i;
170
  
171
  theScript = (char*) NewPtr(10);
172
  if(theScript == nil) 
173
  {
174
      PrintError("Not enough memory");
175
      goto _DoScript_Exit;
176
  }
177
  err = AEGetParamPtr( theAppleEvent, keyDirectObject,
178
                      typeChar, &returned,  theScript , 10, &length);
179
180
  DisposePtr(theScript);
181
  theScript = (char*) NewPtr(length + 1);
182
  if(theScript == nil)        
183
  {
184
      PrintError("Not enough memory");
185
      goto _DoScript_Exit;
186
  }
187
188
  err = AEGetParamPtr( theAppleEvent, keyDirectObject,
189
                      typeChar, &returned,  theScript , length, &length);
190
191
  if( err!= noErr) 
192
  {
193
      PrintError( "Could not receive DoScript Command");
194
      goto _DoScript_Exit;
195
  }
196
  theScript[length] = 0;
197
  // printf("%s\n", theScript); fflush(stdout);
198
  
199
  // get number of args, calculate argc
200
  
201
  argc = 1; ch = theScript;
202
  
203
  while( *ch != 0 )
204
  {
205
      switch( *ch )
206
      {
207
          case ' ': 
208
          case '\t':  ch++; 
209
                      break;
210
          case '"':   argc++; ch++;
211
                      while( *ch!='"' && *ch!=0 ) ch++;
212
                      if( *ch == 0 )
213
                      {
214
                          PrintError("Syntax Error in Script");
215
                          goto _DoScript_Exit;
216
                      }
217
                      ch++;
218
                      break;
219
          default:    argc++; while( *ch!=' ' && *ch!='\t' && *ch!=0 ) ch++;
220
                      break;
221
      }
222
  }
223
  
224
  // Fill argv array
225
  
226
  argv = (char**)NewPtr( argc * sizeof( char* ) );
227
  if( argv == nil )
228
  {
229
      PrintError("Not enough memory");
230
      goto _DoScript_Exit;
231
  }
232
  
233
  argv[0] = (char*) NewPtr( strlen( appName ) + 1 );
234
  if( argv[0] == nil )
235
  {
236
      PrintError("Not enough memory");
237
      goto _DoScript_Exit;
238
  }
239
  strcpy( argv[0], appName );
240
  
241
  ch = theScript; i=0;
242
  
243
  while( *ch != 0 && i<argc )
244
  {
245
      switch( *ch )
246
      {
247
          case ' ': 
248
          case '\t':  ch++; 
249
                      break;
250
          case '"':   i++; ch++; c = ch;
251
                      while( *c!='"' && *c!=0 ) c++;
252
                      argv[i] = (char*) NewPtr( c-ch+1 );
253
                      if( argv[i] == nil )
254
                      {
255
                          PrintError("Not enough memory");
256
                          goto _DoScript_Exit;
257
                      }
258
                      c=argv[i];
259
                      while(*ch!='"') *c++=*ch++;
260
                      *c=0; ch++;
261
                      break;
262
          default:    i++; c = ch;
263
                      while( *c!=' ' && *c!='\t' && *c!=0 ) c++;
264
                      argv[i] = (char*) NewPtr( c-ch+1 );
265
                      if( argv[i] == nil )
266
                      {
267
                          PrintError("Not enough memory");
268
                          goto _DoScript_Exit;
269
                      }
270
                      c=argv[i];
271
                      while(*ch!=' ' && *ch!='\t' && *ch!=0 ) *c++=*ch++;
272
                      *c=0;
273
                      break;
274
      }
275
  }
276
277
_DoScript_Exit:
278
279
  gDone = true;
280
  DisposePtr( theScript );
281
  
282
  return err;
283
}
284
285
286
287
static pascal OSErr   DoOpenDoc( AppleEvent *theAppleEvent,
288
                      AppleEvent *reply, long refCon)
289
{
290
  AEDescList          fileSpecList;
291
  OSErr               err = noErr;
292
  DescType            type;
293
  Size                actual;
294
  AEKeyword           keyword;
295
  int                 i;
296
  long                numFiles=0;
297
  FSSpec f;
298
299
300
  err = AEGetParamDesc( theAppleEvent, keyDirectObject,
301
                      typeAEList, &fileSpecList );
302
  if(err == noErr)
303
  {
304
      err = GotRequiredParams( theAppleEvent );
305
      if(err == noErr)
306
          {
307
              err = AECountItems( &fileSpecList, &numFiles );
308
          }
309
  }
310
  if(err != noErr)
311
  {
312
      PrintError("Error receiving ODOC AppleEvent");
313
      goto _ODOC_exit;
314
  } 
315
  
316
  argc = numFiles + 1;
317
318
  // Fill argv array
319
  
320
  argv = (char**)NewPtr( argc * sizeof( char* ) );
321
  if( argv == nil )
322
  {
323
      PrintError("Not enough memory");
324
      goto _ODOC_exit;
325
  }
326
327
328
  argv[0] = (char*) NewPtr( strlen( appName ) + 1 );
329
  if( argv[0] == nil )
330
  {
331
      PrintError("Not enough memory");
332
      goto _ODOC_exit;
333
  }
334
  strcpy( argv[0], appName );
335
336
  for( i=1; i<argc; i++)
337
  {
338
      argv[i] = (char*) NewPtr( 256 );
339
      if( argv[i] == nil )
340
      {
341
          PrintError("Not enough memory");
342
          goto _ODOC_exit;
343
      }
344
      err = AEGetNthPtr( &fileSpecList, i,
345
              typeFSS, &keyword, &type, (Ptr)&f,
346
              sizeof(FSSpec), &actual);
347
348
      if( err != noErr )
349
      {
350
          PrintError("Error processing file list");
351
          goto _ODOC_exit;
352
      }
353
      
354
      if( FullPathtoString ( &f, argv[i]) )
355
      {
356
          PrintError("Error processing file list");
357
          goto _ODOC_exit;
358
      }
359
  }
360
361
362
_ODOC_exit:
363
  gDone = true;
364
  return err;
365
  
366
}                     
367
368
369
static OSErr  GotRequiredParams( AppleEvent *appleEventPtr )
370
{
371
  DescType        returnedType;
372
  Size            actualSize;
373
  OSErr           err;
374
  
375
  err = AEGetAttributePtr( appleEventPtr,
376
                  keyMissedKeywordAttr, typeWildCard,
377
                  &returnedType, nil, 0, &actualSize );
378
  if ( err == errAEDescNotFound )
379
          return(noErr);
380
  else if (err == noErr)
381
      return(errAEEventNotHandled);
382
  else
383
      return(err);
384
}
385
386
  
387
388
static int FullToFolder(StringPtr name, short *volume, long *folder)
389
{ CInfoPBRec dirInfo; HVolumeParam hvp; short i;
390
  dirInfo.dirInfo.ioNamePtr = name;
391
  dirInfo.dirInfo.ioDrDirID = 0;
392
  dirInfo.dirInfo.ioVRefNum = 0;
393
  dirInfo.dirInfo.ioFDirIndex = 0;
394
  if (PBGetCatInfo (&dirInfo, 0) == noErr) {
395
      if (dirInfo.dirInfo.ioFlAttrib & 0x10) {
396
          *folder = dirInfo.dirInfo.ioDrDirID;
397
          hvp.ioNamePtr = name;
398
          for (i = 0; i < name[0]; i++)
399
              if (name[i+1] == ':')
400
                  { name[0] = i + 1; break; }
401
          hvp.ioVolIndex = -1;
402
          hvp.ioVRefNum = 0;
403
          PBHGetVInfo ((HParmBlkPtr)&hvp, 0);
404
          *volume = hvp.ioVRefNum;
405
      }
406
      else {  *volume = 0, *folder = 0; return -1; }
407
  }
408
  else if (dirInfo.dirInfo.ioResult == fnfErr
409
       || dirInfo.dirInfo.ioResult == dirNFErr)
410
  {   dirInfo.dirInfo.ioNamePtr = (StringPtr)name;
411
      dirInfo.dirInfo.ioDrDirID = 0;
412
      dirInfo.dirInfo.ioVRefNum = 0;
413
      if (PBDirCreate((HParmBlkPtr)&dirInfo, false) == noErr) {
414
          *volume = dirInfo.dirInfo.ioVRefNum;
415
          *folder = dirInfo.dirInfo.ioDrDirID;
416
      }
417
      else {  *volume = 0, *folder = 0;
418
          return dirInfo.dirInfo.ioResult;
419
      }
420
  }
421
  else    { *volume = 0, *folder = 0; return dirInfo.dirInfo.ioResult; }
422
  return noErr;
423
}
424
425
426
int StringtoFullPath(fullPath *file, char *path)
427
{
428
  unsigned char ppath[255];
429
  int i,k;
430
  
431
  /* Make p string */
432
  
433
  strcpy((char*)&(ppath[1]), path);
434
  *ppath = (unsigned char) strlen(path);
435
  
436
  /* Get basename */
437
  k = 0;
438
  for(i = 0; i<= ppath[0]; i++)
439
      if(ppath[i] == ':') k=i;
440
  for(i=k; i  <= ppath[0]; i++)
441
      file->name[i-k] = ppath[i];
442
  file->name[0] = ppath[0] - k;
443
  
444
  /* and dirname */ 
445
  ppath[0] = k;
446
      
447
  return( FullToFolder((StringPtr) ppath, &(file->vRefNum), &(file->parID)));
448
  
449
}
450
451
452
453
454
char *getFileName( fullPath *path )
455
{
456
  p2cstr( path->name );
457
  return (char*) path->name;
458
}
459
460
static void SetUpMenus(void)
461
{
462
  MenuHandle menu;
463
  
464
  DeleteMenu(1);  
465
  InsertMenu(menu = NewMenu(1, "\p\024"), 0);
466
  AppendMenu(menu, "\pAbout...;(-");
467
  AppendResMenu(menu, 'DRVR');
468
  DeleteMenu(2);
469
  InsertMenu(menu = NewMenu(2, "\pFile"), 0);
470
  AppendMenu(menu, "\p(New/N;Open/O;Run/R;Save/S;Save As...;(-;Quit/Q");
471
  DeleteMenu(3);
472
  InsertMenu(menu = NewMenu(3, "\pEdit"), 0);
473
  AppendMenu(menu, "\p(Undo/Z;(-;(Cut/X;(Copy/C;(Paste/V;(Clear");
474
  
475
  DrawMenuBar();
476
}
477
478
479
static void       DoCommand(long mResult)
480
{
481
  short theItem, theMenu;
482
  MenuHandle appleMenu;
483
  Str255 name;
484
485
  theItem = mResult & 65535L;
486
  theMenu = ((unsigned long)mResult) >> 16;
487
488
  switch (theMenu) 
489
  {
490
      case 1:     /*  Apple  */
491
          switch(theItem)
492
          {
493
              case 1: PrintError( "%s\n %s", appName, LONGVERSION );
494
                      break;
495
              default:appleMenu = GetMenuHandle( 1);
496
                      GetMenuItemText(appleMenu, theItem, name);
497
                      OpenDeskAcc(name);
498
                      break;
499
          }
500
          break;
501
      case 2:     /*  File  */
502
          switch(theItem)
503
          {
504
              case 1: break;
505
              case 2: {   // Open file
506
                          fullPath f;
507
                          
508
                          if( FindFile( &f ) == 0 )
509
                          {
510
                              char** temp;
511
                              int i;
512
                              if( argc==0 )
513
                              {
514
                                  argc=1;
515
                                  argv=(char**)NewPtr(sizeof(char*));
516
                                  if(argv==nil)
517
                                  {
518
                                      PrintError("Not enough memory");
519
                                      exit(0);
520
                                  }
521
                                  argv[0] = (char*)NewPtr(strlen(appName)+1);
522
                                  strcpy(argv[0], appName );
523
                              }
524
                              argc+=1;
525
                              temp = (char**)NewPtr(argc * sizeof(char*));
526
                              for(i=0; i<argc-1; i++)
527
                                  temp[i]=argv[i];
528
                              temp[argc-1] = (char*) NewPtr( 256 );
529
                              if( temp[argc-1] == nil )
530
                              {
531
                                  PrintError("Not enough memory");
532
                                  exit(0);
533
                              }
534
                              if( FullPathtoString ( &f, temp[argc-1]) )
535
                              {
536
                                  PrintError("Error processing file list");
537
                                  exit(0);
538
                              }
539
                              DisposePtr( (char*)argv );
540
                              argv = temp;
541
                          }
542
                      }
543
                      break;
544
              case 3: // Run
545
                      gDone = TRUE;
546
                      break;
547
              case 4: // Save
548
              case 5: {// Save As...
549
                          fullPath f;
550
                          
551
                          if( SaveFileAs( &f, "Save Result As...", "Result" ) == 0 )
552
                          {
553
                              char** temp;
554
                              int i;
555
                              if( argc==0 )
556
                              {
557
                                  argc=1;
558
                                  argv=(char**)NewPtr(sizeof(char*));
559
                                  if(argv==nil)
560
                                  {
561
                                      PrintError("Not enough memory");
562
                                      exit(0);
563
                                  }
564
                                  argv[0] = (char*)NewPtr(strlen(appName)+1);
565
                                  strcpy(argv[0], appName );
566
                              }
567
                              argc+=2;
568
                              temp = (char**)NewPtr(argc * sizeof(char*));
569
                              for(i=0; i<argc-2; i++)
570
                                  temp[i]=argv[i];
571
                              temp[argc-2] = (char*) NewPtr( 3 );
572
                              strcpy(temp[argc-2], "-o");
573
                              temp[argc-1] = (char*) NewPtr( 256 );
574
                              if( temp[argc-1] == nil )
575
                              {
576
                                  PrintError("Not enough memory");
577
                                  exit(0);
578
                              }
579
                              if( FullPathtoString ( &f, temp[argc-1]) )
580
                              {
581
                                  PrintError("Error processing file list");
582
                                  exit(0);
583
                              }
584
                              DisposePtr( (char*)argv );
585
                              argv = temp;
586
                          }
587
                      }
588
                      break;
589
                      
590
              case 7: exit(0);    
591
                      break;
592
          }
593
  }
594
  HiliteMenu(0);
595
}
596
  

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks