home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff353.lzh / AztecArp / _main.c < prev    next >
C/C++ Source or Header  |  1990-06-02  |  7KB  |  261 lines

  1. /* Created 11/08/87 by -=+SDB+=- from file _main.c provided by Manx */
  2. /* Copyright (C) 1987 by Scott Ballantyne */
  3. /* May be freely used by ARP users/supporters */
  4. /* Memory is allocated for the MANX device buffers, etc., but the
  5.  * ARP tracking functions are used.
  6.  * tweeked for v39
  7.  * further tweeked to handle v1.4 workbench startup
  8.  */
  9.  
  10. extern long _savsp, _stkbase;
  11.  
  12. extern int errno, Enable_Abort;
  13.  
  14. extern int _argc;
  15. extern char **_argv;
  16. extern struct WBStartup *WBenchMsg;
  17.  
  18. extern struct _dev *_devtab;
  19. extern short _numdev;
  20.  
  21. extern struct ArpBase *ArpBase;
  22. extern void *IntuitionBase, *GfxBase, *DOSBase;
  23.  
  24. extern struct ExecBase *SysBase;
  25.  
  26. /* !!! alot of things here depend on receiving WBenchMsg before opening arp.library */
  27.  
  28. #ifdef DETACH
  29. static long _alen = 0;
  30. #endif
  31.  
  32. _main(alen, aptr)
  33. long alen;
  34. char *aptr;
  35. {
  36.     register struct Process *pp;
  37.  
  38.     pp = (struct Process *)SysBase -> ThisTask;
  39.  
  40.     if (!pp->pr_CLI
  41. #ifdef DETACH
  42.         && !_alen        /* !!! alen is here because pr_CLI is NULL for bg task, but _alen (hopefully) isn't */
  43. #endif
  44.     ) {
  45.         WaitPort(&pp->pr_MsgPort);
  46.         WBenchMsg = (struct WBStartup *)GetMsg(&pp->pr_MsgPort);
  47.     }
  48.  
  49.     if (!(ArpBase = (struct ArpBase *)OpenLibrary (ArpName,ArpVersion))) {
  50.         complain();
  51.         Forbid();
  52.         if (WBenchMsg) ReplyMsg ((struct Message *)WBenchMsg);
  53.         return(20);
  54.     }
  55.  
  56.     if(!DOSBase)
  57.         DOSBase = ArpBase->DosBase;
  58.  
  59.     GfxBase = ArpBase->GfxBase;
  60.     IntuitionBase = ArpBase->IntuiBase;
  61.  
  62. #ifdef DETACH
  63.     {
  64.         void do_detach();
  65.  
  66.         if (!WBenchMsg) do_detach(&alen, &aptr);
  67.     }
  68. #endif
  69.  
  70.     if ( (_devtab = ArpAlloc( _numdev*(long)sizeof(struct _dev))) == 0)
  71.     {
  72.         Alert(AG_NoMemory, 0L);
  73.         ArpExit(20L, ERROR_NO_FREE_STORE );
  74.     }
  75.  
  76.     _devtab[0].mode = O_RDONLY;
  77.     _devtab[1].mode = _devtab[2].mode = O_WRONLY;
  78.  
  79.     _stkbase = _savsp - *((long *)_savsp+1) + 8;
  80.     *(long *)_stkbase = 0x4d414e58L;
  81.  
  82.     if (!WBenchMsg) {
  83.         _cli_parse(pp, alen, aptr);
  84.         Enable_Abort = 1;
  85. #ifndef DETACH
  86.         _devtab[0].mode |= O_STDIO;        /* shouldn't close if CLI */
  87.         _devtab[1].mode |= O_STDIO;
  88. #endif
  89.           /* !!! wb 1.4 tweek */
  90.         _devtab[0].fd = (struct FileHandle *)Input();
  91.         if (_devtab[1].fd = (struct FileHandle *)Output())
  92.             _devtab[2].fd = (struct FileHandle *)Open("*", MODE_OLDFILE);
  93.           /* !!! wb 1.4 tweek */
  94.     }
  95.     else {
  96.           /* !!! wb 1.4 tweek */
  97.         _devtab[0].mode |= O_STDIO;        /* shouldn't close if WB opened something for us */
  98.         _devtab[1].mode |= O_STDIO;
  99.         _devtab[2].mode |= O_STDIO;
  100.           /* !!! wb 1.4 tweek */
  101.  
  102.         if (WBenchMsg->sm_ArgList)
  103.             CurrentDir((BPTR)WBenchMsg->sm_ArgList->wa_Lock);
  104.         _wb_parse(pp, WBenchMsg);
  105.         _argv = (char **)WBenchMsg;
  106.  
  107.           /* !!! wb 1.4 tweek */
  108.         _devtab[0].fd = (struct FileHandle *)Input();
  109.         _devtab[1].fd = _devtab[2].fd = (struct FileHandle *)Output();
  110.           /* !!! wb 1.4 tweek */
  111.     }
  112.     main(_argc, _argv);
  113.     exit(0);        /* Need to also close files, etc. */
  114. }
  115.  
  116.  
  117. /*#if (int)ArpVersion != 39*/
  118. /*    !!! this needs fixing!*/
  119. /*#endif*/
  120.  
  121. static
  122. complain()
  123. {
  124.     long output;
  125.     static char complaint1[] = "You need arp.library V39+";
  126.     static char complaint2[] = "\n";
  127.     static struct IntuiText comptext = { AUTOFRONTPEN, 0, JAM1, 30, 15, NULL, (void *)complaint1 };
  128.     static struct IntuiText oktext = { AUTOFRONTPEN, AUTOBACKPEN, AUTODRAWMODE, AUTOLEFTEDGE, AUTOTOPEDGE, NULL, (void *)"Ok" };
  129.     struct Process *pp;
  130.  
  131.     pp = (struct Process *)SysBase -> ThisTask;
  132.  
  133.     if (pp->pr_CLI && (DOSBase = OpenLibrary ("dos.library",33L)) && (output = (long)Output()))
  134.     {
  135.     Write (output, complaint1, (long)sizeof complaint1-1);
  136.     Write (output, complaint2, (long)sizeof complaint2-1);
  137.     }
  138.     else if (IntuitionBase = OpenLibrary ("intuition.library",33L)) {
  139.     AutoRequest (NULL,&comptext,NULL,&oktext,0L,0L,320L,72L);
  140.     CloseLibrary ((struct Library *)IntuitionBase);
  141.     }
  142.     else {
  143.     Alert (AG_OpenLib | AO_ArpLib, 0L);
  144.     }
  145.  
  146.     if (DOSBase) {
  147.     CloseLibrary ((struct Library *)DOSBase);
  148.     DOSBase = NULL;
  149.     }
  150. }
  151.  
  152.  
  153. #ifdef DETACH
  154. extern long _stack, _priority, _BackGroundIO;
  155. extern char *_procname;
  156. BPTR _Backstdout = 0;
  157. extern BPTR _detach_curdir;
  158. extern char *_detach_name;
  159. static char *_aptr = 0;
  160.  
  161. static void
  162. do_detach(alen, aptr)
  163. long *alen;
  164. char **aptr;
  165. {
  166.     struct Process *pp;
  167. /*      long l;  */
  168. /*      struct MemList *mm;  */
  169. /*      register unsigned short c;  */
  170.  
  171.     pp = (struct Process *)SysBase -> ThisTask;
  172.     if (pp->pr_CLI) {                       /* first time through!! */
  173.         register char *cp;
  174.         register struct CommandLineInterface *cli;
  175.  
  176.         CurrentDir(_detach_curdir = CurrentDir(NULL));
  177.         _detach_curdir = DupLock(_detach_curdir);
  178.  
  179.         cli = (struct CommandLineInterface *) ((long)pp->pr_CLI << 2);
  180.  
  181.         #if 0
  182.         l = cli->cli_Module;
  183.         if ((sav = OpenLibrary(DOSNAME, 33L)) == 0) {
  184.  
  185.             lp = (long *)*((long *)*((long *)*((long *)*((long *)
  186.                                             _savsp+2)+1)-3)-3)+107;
  187.             if (*lp != cli->cli_Module)
  188.                 exit(20);
  189.         }
  190.         else {
  191.             CloseLibrary((struct Library *)sav);
  192.             lp = 0;
  193.         }
  194.         if (lp)
  195.             *lp = 0;
  196.         #endif
  197.  
  198.         if (_stack == 0)
  199.             _stack = cli->cli_DefaultStack * 4;
  200.         if (_BackGroundIO)
  201.             _Backstdout = (BPTR)Open("*", MODE_OLDFILE);
  202.         _alen = *alen;
  203.  
  204.         if (!(_aptr = DosAllocMem(_alen))) goto clean;
  205.         CopyMem(*aptr, _aptr, _alen);
  206.         cp = (char *)((long)cli->cli_CommandName << 2);
  207.         if (!(_detach_name = DosAllocMem((long)cp[0]+1))) goto clean;
  208.         CopyMem(cp, _detach_name, (long)cp[0]+1);
  209.  
  210.         if (CreateProc(_procname, _priority, cli->cli_Module, _stack)) {
  211.             cli->cli_Module = 0;
  212.             ArpExit (0L,0L);
  213.         }
  214.  
  215.         clean:
  216.         if (_aptr) DosFreeMem (_aptr);
  217.         if (_detach_name) DosFreeMem (_detach_name);
  218.         ArpExit (20L,ERROR_NO_FREE_STORE);
  219.     }
  220.  
  221.         /* !!! why is this strcmp() here???
  222.          *
  223.          * Olsen says: This strcmp succeeds if the detach
  224.          * startup code has successfully created the background
  225.          * process with the name given in '_procname'.
  226.          *
  227.          * There is a nasty pitfall; say '_procname = "foo";' 
  228.          * the resulting program is called 'foo', too, and is
  229.          * called from Workbench. What happens? Bang!
  230.          * pp->pr_CLI is zero and the strcmp succeeds.
  231.          * If 'foo' hasn't crashed yet it will crash on exit.
  232.          *
  233.          * Note that the Arp _main() routine fixes this bug
  234.          * by taking a look on WBenchMsg before calling do_detach.
  235.          */
  236.  
  237.     else if (strcmp(pp->pr_Task.tc_Node.ln_Name, _procname) == 0) { /* second time */
  238.         register long *lp;
  239.         register struct DefaultTracker *tr;
  240.  
  241.         if (tr = GetTracker (TRAK_SEGLIST)) {
  242.             lp = (long *)((long)pp->pr_SegList << 2);
  243.             tr->dt_Object.dt_Resource = (CPTR)lp[3];
  244.         }
  245.         if (tr = GetTracker (TRAK_DAMEM))
  246.             tr->dt_Object.dt_Resource = (CPTR)_aptr;
  247.         if (tr = GetTracker (TRAK_DAMEM))
  248.             tr->dt_Object.dt_Resource = (CPTR)_detach_name;
  249.         if (tr = GetTracker (TRAK_LOCK))
  250.             tr->dt_Object.dt_Resource = (CPTR)_detach_curdir;
  251.  
  252.         CurrentDir(_detach_curdir);
  253.  
  254.         pp->pr_COS = _Backstdout;
  255.  
  256.         *alen = _alen;
  257.         *aptr = _aptr;
  258.     }
  259. }
  260. #endif
  261.