home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / Dmake / mac-codewar / ChildApp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-05  |  8.6 KB  |  315 lines  |  [TEXT/R*ch]

  1. /* ChildApp.c
  2.      d.g.gilbert, Dec'94
  3.      
  4.    program main call for Mac apps needing to handle command-line parameters
  5.    true main should be renamed to
  6.        void RealMain(int argc, char** argv);
  7.    used HighLevel event called "Cmdl" that calling application sends to this
  8.    child application.  This isn't an appleevent.
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14.  
  15. #undef DEBUG
  16.  
  17. /* use this only w/ Mac apps */
  18. #define MACINTOSH 1
  19. #include <EPPC.h> /* Apple highlevel events */
  20.  
  21. void  BeFriendly();   // use for multiprocessing
  22. Boolean StopKey();        // check if user wants to quit in middle
  23.  
  24.  
  25.  
  26. #ifdef DEBUG
  27. char* long2str( long val)
  28. {
  29.     static char buf[4];
  30.   buf[0]= (val >> 24) & 0xff;
  31.   buf[1]= (val >> 16) & 0xff;
  32.   buf[2]= (val >> 8) & 0xff;
  33.   buf[3]= val & 0xff;
  34.     return buf;    
  35. }
  36. #endif
  37.  
  38. char* FindWordEnd( char** start)
  39. {
  40.     char *ep, *cp;
  41.     ep= cp= *start;
  42.     if (*cp == '"' || *cp == '\'') { 
  43.         char key= *cp;
  44.         cp++; 
  45.         (*start)++; // need to move word start up 1
  46.         for (ep= cp; *ep && *ep != key; ep++) ; 
  47.         }
  48.     else if (*cp) { 
  49.         for (ep= cp+1; 
  50.             *ep && *ep>' ' && *ep!='>' && *ep!='<' && *ep != '"' && *ep != '\''; 
  51.             ep++) ; 
  52.         }
  53.     return ep;
  54. }   
  55.  
  56. char* FindWordStart( char* cp)
  57. {
  58.     while (*cp && *cp <= ' ') cp++;
  59.     return cp;
  60. }
  61.  
  62.  
  63. main(int argc, char *argv[])
  64. {
  65.     enum { kMaxBuflen = 512 };
  66.     Boolean    doloop = true, flag;
  67.     char     **myargv, * cmdline;
  68.     int        i, err, try, myargc = argc;    
  69.     long    time;
  70.     unsigned long msgRefcon, buflen;
  71.     EventRecord evt;
  72.     TargetID    sender;
  73.  
  74.         // need this to prevent crashes
  75.     MaxApplZone();
  76.     InitGraf((Ptr)&qd.thePort);
  77.     InitFonts();
  78.     InitWindows();
  79.     InitMenus();
  80.     TEInit();
  81.     InitDialogs(nil);
  82.     InitCursor();
  83.  
  84. #ifdef DEBUG
  85.   freopen( "ChildApp.stdout", "w", stdout);  
  86.   fprintf(stdout, "+debug: Test output from ChildApp.c --\n+debug: where is stdout going to??\n");
  87. #endif
  88.  
  89.     try= 0;
  90.     buflen= kMaxBuflen;
  91.     cmdline= (char*) malloc(buflen+1);
  92.     memset(cmdline,0,buflen+1);
  93.     while (WaitNextEvent( highLevelEventMask, &evt, 2, NULL) || try<3)  { 
  94.         try++;    
  95.         err= AcceptHighLevelEvent( &sender, &msgRefcon, cmdline, &buflen);
  96.  
  97. #ifdef DEBUG
  98.         fprintf(stderr, " HL Event %s ", long2str(evt.message));
  99.         fprintf(stderr, " HL message %s ", long2str(msgRefcon));
  100.         fprintf(stderr, " error= %d \n", err);
  101. #endif    
  102. #ifdef DEBUGnot
  103.         msgRefcon= 'Cmdl';
  104.         err= 0;
  105.         //strcpy( cmdline, "ASKwais -d :testgb:genbank phage > 'sys:Folder With Space:test1.out' ");
  106.         strcpy( cmdline, "clustalw /infile=globin.pep /outfile=test.out");
  107.         buflen= strlen(cmdline)+1;
  108. #endif    
  109.     
  110.     if (msgRefcon == 'Cmdl') {
  111.         if (err == bufferIsSmall) {
  112.             free(cmdline);
  113.             buflen= kMaxBuflen;
  114.             cmdline= (char*) malloc( buflen+1);
  115.             err= AcceptHighLevelEvent( &sender, &msgRefcon, cmdline, &buflen);
  116.             }
  117.             
  118.         if (!err) {
  119.             /* push buf into argv, separating words */
  120.         char  *cp, *ep, *sin, *sout, *serr, * maxp, savec;
  121.         char  done, nextStderr, nextStdin, nextStdout;
  122.  
  123.         if (buflen>kMaxBuflen) buflen= kMaxBuflen;
  124.         cmdline[buflen]= 0;        
  125.         sout= sin= serr= NULL;
  126.         nextStderr= nextStdin = nextStdout= 0;
  127.         done= 0;
  128.         myargc= 0;
  129.         myargv= (char**) malloc(  (myargc+2) * sizeof(char*));
  130.         cp= cmdline; 
  131.         maxp= cmdline + buflen;
  132.         
  133. #ifdef DEBUG 
  134. fprintf(stderr, " Cmdline: '%s' \n", cmdline);
  135. #endif
  136.         while (!done && cp<maxp) {
  137.             cp= FindWordStart( cp);
  138.                 ep= FindWordEnd( &cp);
  139.                 
  140. #ifdef DEBUG
  141. savec= *ep; *ep= 0; 
  142. fprintf(stderr, " +cmdline word: '%s' \n", cp);
  143. *ep= savec;
  144. #endif
  145.                 if (nextStderr) {
  146.                     nextStderr= 0;
  147.                     serr= cp;
  148.                     if (*ep) *ep= 0; else done= 1;
  149.                     }
  150.                 else if (nextStdout) {
  151.                     nextStdout= 0;
  152.                     sout= cp;
  153.                     if (*ep) *ep= 0; else done= 1;
  154.                     }
  155.                 else if (nextStdin) {
  156.                     nextStdin= 0;
  157.                     sin= cp;
  158.                     if (*ep) *ep= 0; else done= 1;
  159.                     }
  160.                 else if (*cp == '2' && cp[1] == '>') {
  161.                     nextStderr= 1;
  162.                     ep= cp+1;
  163.                     }
  164.            else if (*cp == '>') {
  165.                     nextStdout= 1;
  166.                     ep= cp;
  167.                }
  168.            else if (*cp == '<') {
  169.                     nextStdin= 1;
  170.                     ep= cp;
  171.                }
  172.            else {
  173.                     if (*ep) *ep= 0; else done= 1;
  174.                     myargc++;
  175.                     myargv= (char**) realloc(myargv, (myargc+2) * sizeof(char*));
  176.                     myargv[myargc-1]= cp;
  177.                     }
  178.                 cp= ep+1;
  179.                 }
  180.         myargv[myargc]= NULL;
  181.  
  182. #ifdef DEBUG
  183. fprintf(stderr, " +comment: stderr '%s' \n", serr);
  184. if (sout) fprintf(stdout, "+comment: new stdout is '%s'\n",sout);
  185. if (serr) fprintf(stdout, "+comment: new stderr is '%s'\n",serr);
  186. if (sin) fprintf(stdout, "+comment: new stdin is '%s'\n",sin);
  187. for (i=0; i<myargc; i++) fprintf(stdout, "arg[%d]= '%s'\n", i, myargv[i]);
  188. #endif
  189.         if (sin) freopen( sin, "r", stdin);
  190.         if (serr) freopen( serr, "a", stderr);  
  191.             if (sout) freopen( sout, "a", stdout);  //where oh where is stdout going !????
  192.             
  193.             err= RealMain(myargc, myargv);
  194.  
  195. #ifdef DEBUG
  196. fprintf(stdout, "+debug: Test stdout from ChildApp.c --\n+debug: where is stdout going to??\n");
  197. fprintf(stderr, "+error: Test stderr from ChildApp.c --\n+error: where is stderr going to??\n");
  198. #endif
  199.             //fflush(stdout);
  200.             //fclose(stdout); //??
  201.             exit(0);   
  202.             }
  203.         exit(0);  
  204.         }
  205.     }
  206.  
  207. #if 1        
  208.     //fprintf(stderr, "\n");  // force display window
  209.     do {
  210.         //fprintf(stderr,"\nEnter command line (command-Q to quit)\n");
  211.         //fprintf(stderr,"-> "); 
  212.       myargc = ccommand( &myargv);
  213.         
  214.         err= RealMain( myargc, myargv);
  215.         fflush(stdout);
  216.         } while (doloop);
  217.         
  218. #endif
  219. }
  220.  
  221.  
  222.  
  223. Boolean StopKey()
  224. {    
  225. EventRecord    ev;
  226.  
  227.     if (EventAvail( keyDownMask+autoKeyMask, &ev)) {
  228.       if (  (ev.modifiers & cmdKey)  
  229.        && ((char)(ev.message & charCodeMask) == '.') ) {
  230.             SysBeep(1);
  231.           (void) GetNextEvent( keyDownMask+autoKeyMask, &ev);
  232.           return true;
  233.           }
  234.       }
  235.     return false;
  236. }
  237.  
  238. Boolean Keypress()
  239. {    EventRecord    ev;
  240.     return EventAvail( keyDownMask+keyUpMask+autoKeyMask, &ev);
  241. }
  242.  
  243. /******
  244. From dowdy@apple.com Wed Sep  4 21:06:10 1991
  245. Received: from apple.com by sunflower.bio.indiana.edu
  246.         (4.1/9.5jsm) id AA04821; Wed, 4 Sep 91 21:06:08 EST
  247. Received: from [90.10.20.25] by apple.com with SMTP (5.61/25-eef)
  248.         id AA07198; Wed, 4 Sep 91 19:06:53 -0700
  249.         for gilbertd@sunflower.bio.indiana.edu
  250. Date: Wed, 4 Sep 91 19:06:53 -0700
  251. Message-Id: <9109050206.AA07198@apple.com>
  252. From: dowdy@apple.com (Tom Dowdy)
  253. To: gilbertd@sunflower.bio.indiana.edu
  254. Subject: Re: MPW SIOW library
  255. References: <1991Sep1.174155.13408@gn.ecn.purdue.edu> <6260@dftsrv.gsfc.nasa.gov
  256. > <1991Sep4.233506.25878@bronze.ucs.indiana.edu>
  257. Organization: Apple Computer, Inc.
  258. Status: R
  259.  
  260. In article <1991Sep4.233506.25878@bronze.ucs.indiana.edu>, gilbertd@sunflower.bi
  261. o.indiana.edu (Don Gilbert) writes:
  262. > In article <6260@dftsrv.gsfc.nasa.gov> rdominy@kong.gsfc.nasa.gov (Robert Domi
  263. ny) writes:
  264. > >
  265. > >I have tried calling the routine EventAvail from an SIOW application to
  266. > >provide background processing time without success.  I suspect you
  267. >
  268. > I just put a commandline c program into SIOW and sprinkled some calls
  269. > to WaitNextEvent thru the time consuming procedures.  It seems to work
  270. > okay, though a bit cranky, for getting the app to work in the background.
  271. > When I say cranky, it will miss some os events telling it to shift into
  272. > background, but if I beat the mouse on the desktop, it will eventually
  273. > shift into background.
  274.  
  275. This is just a guess, but the process manager will continue to call
  276. you with update events if you fail to handle them.  After you have
  277. ignore something like 120 requests to update a window of yours,
  278. the PM will assume you are a "MultiFinder hostile" application
  279. and stop giving them to you until the update rgn changes.
  280.  
  281. I'm wondering if this is causing your initial strangeness.  I know
  282. a fellow programmer here got himself into the same fix and
  283. this ended up being the reason he saw his application perform so slowly
  284. in the background.  Once the events were cleared, things were fine again.
  285.  
  286.  Tom Dowdy                 Internet:  dowdy@apple.COM
  287.  Apple Computer MS:81EQ    UUCP:      {sun,voder,amdahl,decwrl}!apple!dowdy
  288.  20525 Mariani Ave         AppleLink: DOWDY1
  289.  Cupertino, CA 95014
  290.  "The 'Ooh-Ah' Bird is so called because it lays square eggs."
  291. ***************/                                                                  
  292.                                                                                                                                 
  293. void  BeFriendly(void)
  294. /* make this app multifinder-friendly w/ background processing */
  295. /* this can slow the app down quite a bit -- don't use too liberally */
  296. {
  297.     const int cmdKey = 256;  /* command-period == halt */
  298.     const long charCodeMask = 0x000000FF;
  299.     int    eventMask = osMask; /*? +keyDownMask+highLevelEventMask*/
  300.     long    sleepTime    = 1; /* # x 1/60 to sleep for others */
  301.     EventRecord    theEvent;
  302.     
  303.     if (WaitNextEvent( eventMask,    &theEvent, sleepTime, NULL)) {  
  304.     /*****
  305.         if (theEvent.what==keyDown) { 
  306.             if ((theEvent.message & charCodeMask == '.') 
  307.                 && (theEvent.modifiers & cmdKey !=0)) 
  308.                     exit(1); 
  309.             }
  310.         ******/
  311.         }
  312.     /* SpinCursor(1); */ /* !!!? not seeing this, need .rsrc ACUR cursor ? */ 
  313. }
  314.  
  315.