home *** CD-ROM | disk | FTP | other *** search
/ Software 2000 / Software 2000 Volume 1 (Disc 1 of 2).iso / utilities / u264.dms / in.adf / ARPPRO3.0 / PRO.RUN / _main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-18  |  6.2 KB  |  253 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. #include <exec/alerts.h>
  11. #include <exec/memory.h>
  12. #include <intuition/intuition.h>
  13. #include <libraries/arpbase.h>
  14. #include <libraries/dosextens.h>
  15. #include <workbench/startup.h>
  16. #include <functions.h>
  17. #include <fcntl.h>
  18.  
  19. extern long _savsp, _stkbase;
  20.  
  21. extern int errno, Enable_Abort;
  22.  
  23. extern int _argc;
  24. extern char **_argv;
  25. extern struct WBStartup *WBenchMsg;
  26.  
  27. extern struct _dev *_devtab;
  28. extern short _numdev;
  29.  
  30. extern struct ArpBase *ArpBase;
  31. extern void *IntuitionBase, *GfxBase, *DOSBase;
  32.  
  33.  
  34. /* !!! alot of things here depend on receiving WBenchMsg before opening arp.library */
  35.  
  36. #ifdef DETACH
  37. static long _alen = 0;
  38. #endif
  39.  
  40. _main(alen, aptr)
  41. long alen;
  42. char *aptr;
  43. {
  44.     register struct Process *pp, *_FindTask();
  45.     void *_GetMsg();
  46.     long _Input(), _Output(), _Open();
  47.  
  48.     pp = _FindTask(0L);
  49.     if (!pp->pr_CLI
  50. #ifdef DETACH
  51.         && !_alen        /* !!! alen is here because pr_CLI is NULL for bg task, but _alen (hopefully) isn't */
  52. #endif
  53.     ) {
  54.         _WaitPort(&pp->pr_MsgPort);
  55.         WBenchMsg = _GetMsg(&pp->pr_MsgPort);
  56.     }
  57.  
  58.     if (!(ArpBase = (struct ArpBase *)OpenLibrary (ArpName,ArpVersion))) {
  59.         complain();
  60.         _Forbid();
  61.         if (WBenchMsg) _ReplyMsg (WBenchMsg);
  62.         return 20;
  63.     }
  64.  
  65.     DOSBase = ArpBase->DosBase;
  66.     GfxBase = ArpBase->GfxBase;
  67.     IntuitionBase = ArpBase->IntuiBase;
  68.  
  69. #ifdef DETACH
  70.     {
  71.         void do_detach();
  72.  
  73.         if (!WBenchMsg) do_detach(&alen, &aptr);
  74.     }
  75. #endif
  76.  
  77.     if ( (_devtab = ArpAlloc( _numdev*(long)sizeof(struct _dev))) == 0)
  78.     {
  79.         Alert(AG_NoMemory, 0L);
  80.         ArpExit(20L, ERROR_NO_FREE_STORE );
  81.     }
  82.  
  83.     _devtab[0].mode = O_RDONLY;
  84.     _devtab[1].mode = _devtab[2].mode = O_WRONLY;
  85.  
  86.     _stkbase = _savsp - *((long *)_savsp+1) + 8;
  87.     *(long *)_stkbase = 0x4d414e58L;
  88.  
  89.     if (!WBenchMsg) {
  90.         _cli_parse(pp, alen, aptr);
  91.         Enable_Abort = 1;
  92. #ifndef DETACH
  93.         _devtab[0].mode |= O_STDIO;        /* shouldn't close if CLI */
  94.         _devtab[1].mode |= O_STDIO;
  95. #endif
  96.           /* !!! wb 1.4 tweek */
  97.         _devtab[0].fd = _Input();
  98.         if (_devtab[1].fd = _Output())
  99.             _devtab[2].fd = _Open("*", MODE_OLDFILE);
  100.           /* !!! wb 1.4 tweek */
  101.     }
  102.     else {
  103.           /* !!! wb 1.4 tweek */
  104.         _devtab[0].mode |= O_STDIO;        /* shouldn't close if WB opened something for us */
  105.         _devtab[1].mode |= O_STDIO;
  106.         _devtab[2].mode |= O_STDIO;
  107.           /* !!! wb 1.4 tweek */
  108.  
  109.         if (WBenchMsg->sm_ArgList)
  110.             _CurrentDir(WBenchMsg->sm_ArgList->wa_Lock);
  111.         _wb_parse(pp, WBenchMsg);
  112.         _argv = (char **)WBenchMsg;
  113.  
  114.           /* !!! wb 1.4 tweek */
  115.         _devtab[0].fd = _Input();
  116.         _devtab[1].fd = _devtab[2].fd = _Output();
  117.           /* !!! wb 1.4 tweek */
  118.     }
  119.     main(_argc, _argv);
  120.     exit(0);        /* Need to also close files, etc. */
  121. }
  122.  
  123.  
  124. #if (int)ArpVersion != 39
  125.     !!! this needs fixing!
  126. #endif
  127.  
  128. static
  129. complain()
  130. {
  131.     long _Output(), output;
  132.     static char complaint1[] = "You need arp.library V39+";
  133.     static char complaint2[] = "\n";
  134.     static struct IntuiText comptext = { AUTOFRONTPEN, 0, JAM1, 30, 15, NULL, (void *)complaint1 };
  135.     static struct IntuiText oktext = { AUTOFRONTPEN, AUTOBACKPEN, AUTODRAWMODE, AUTOLEFTEDGE, AUTOTOPEDGE, NULL, (void *)"Ok" };
  136.     struct Process *pp, *_FindTask();
  137.  
  138.     pp = _FindTask(0L);
  139.  
  140.     if (pp->pr_CLI && (DOSBase = OpenLibrary ("dos.library",LIBRARY_VERSION)) && (output = _Output())) {
  141.     _Write (output, complaint1, (long)sizeof complaint1-1);
  142.     _Write (output, complaint2, (long)sizeof complaint2-1);
  143.     }
  144.     else if (IntuitionBase = OpenLibrary ("intuition.library",LIBRARY_VERSION)) {
  145.     AutoRequest (NULL,&comptext,NULL,&oktext,0L,0L,320L,72L);
  146.     CloseLibrary (IntuitionBase);
  147.     }
  148.     else {
  149.     Alert (AG_OpenLib | AO_ArpLib, 0L);
  150.     }
  151.  
  152.     if (DOSBase) {
  153.     CloseLibrary (DOSBase);
  154.     DOSBase = NULL;
  155.     }
  156. }
  157.  
  158.  
  159. #ifdef DETACH
  160. extern long _stack, _priority, _BackGroundIO;
  161. extern char *_procname;
  162. BPTR _Backstdout = 0;
  163. extern BPTR _detach_curdir;
  164. extern char *_detach_name;
  165. static char *_aptr = 0;
  166.  
  167. static void
  168. do_detach(alen, aptr)
  169. long *alen;
  170. char **aptr;
  171. {
  172.     struct Process *pp, *_FindTask();
  173.     void /* *sav, */ *_OpenLibrary(), *_GetMsg(), *_AllocMem();
  174.     long _Open();
  175. /*      long l;  */
  176. /*      struct MemList *mm;  */
  177. /*      register unsigned short c;  */
  178.  
  179.     pp = _FindTask(0L);
  180.     if (pp->pr_CLI) {                       /* first time through!! */
  181.         register char *cp;
  182.         register struct CommandLineInterface *cli;
  183.  
  184.         CurrentDir(_detach_curdir = CurrentDir(0L));
  185.         _detach_curdir = DupLock(_detach_curdir);
  186.  
  187.         cli = (struct CommandLineInterface *) ((long)pp->pr_CLI << 2);
  188.  
  189.         #if 0
  190.         l = cli->cli_Module;
  191.         if ((sav = _OpenLibrary(DOSNAME, 33L)) == 0) {
  192.  
  193.             lp = (long *)*((long *)*((long *)*((long *)*((long *)
  194.                                             _savsp+2)+1)-3)-3)+107;
  195.             if (*lp != cli->cli_Module)
  196.                 exit(100);
  197.         }
  198.         else {
  199.             _CloseLibrary(sav);
  200.             lp = 0;
  201.         }
  202.         if (lp)
  203.             *lp = 0;
  204.         #endif
  205.  
  206.         if (_stack == 0)
  207.             _stack = cli->cli_DefaultStack * 4;
  208.         if (_BackGroundIO)
  209.             _Backstdout = (BPTR)_Open("*", MODE_OLDFILE);
  210.         _alen = *alen;
  211.  
  212.         if (!(_aptr = DosAllocMem(_alen))) goto clean;
  213.         CopyMem(*aptr, _aptr, _alen);
  214.         cp = (char *)((long)cli->cli_CommandName << 2);
  215.         if (!(_detach_name = DosAllocMem((long)cp[0]+1))) goto clean;
  216.         CopyMem(cp, _detach_name, (long)cp[0]+1);
  217.  
  218.         if (CreateProc(_procname, _priority, cli->cli_Module, _stack)) {
  219.             cli->cli_Module = 0;
  220.             ArpExit (0L);
  221.         }
  222.  
  223.         clean:
  224.         if (_aptr) DosFreeMem (_aptr);
  225.         if (_detach_name) DosFreeMem (_detach_name);
  226.         ArpExit (20L,ERROR_NO_FREE_STORE);
  227.     }
  228.             /* !!! why is this strcmp() here??? */
  229.     else if (strcmp(pp->pr_Task.tc_Node.ln_Name, _procname) == 0) { /* second time */
  230.         register long *lp;
  231.         register struct DefaultTracker *tr;
  232.  
  233.         if (tr = GetTracker (TRAK_SEGLIST)) {
  234.             lp = (long *)((long)pp->pr_SegList << 2);
  235.             tr->dt_Object.dt_Resource = (CPTR)lp[3];
  236.         }
  237.         if (tr = GetTracker (TRAK_DAMEM))
  238.             tr->dt_Object.dt_Resource = (CPTR)_aptr;
  239.         if (tr = GetTracker (TRAK_DAMEM))
  240.             tr->dt_Object.dt_Resource = (CPTR)_detach_name;
  241.         if (tr = GetTracker (TRAK_LOCK))
  242.             tr->dt_Object.dt_Resource = (CPTR)_detach_curdir;
  243.  
  244.         CurrentDir(_detach_curdir);
  245.  
  246.         pp->pr_COS = _Backstdout;
  247.  
  248.         *alen = _alen;
  249.         *aptr = _aptr;
  250.     }
  251. }
  252. #endif
  253.