home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_c / zc.lzh / ZC / ZCSRC.LZH / IOLib / misc / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-22  |  3.5 KB  |  156 lines

  1. /*
  2.  * This startup code is called from both the CLI and from Workbench.  If
  3.  * from WorkBench, argc is 0 and the argv parameter points to
  4.  * a WBStartup type of structure.
  5.  */
  6.  
  7. #include <exec/types.h>
  8. #include <exec/alerts.h>
  9. #include <exec/memory.h>
  10. #include <libraries/dosextens.h>
  11. #include <workbench/startup.h>
  12. #include <stdio.h>
  13. #include <fcntl.h>
  14.  
  15. extern long SysBase;        /* Used by exec functions */
  16. extern long DOSBase;        /* Required by AmigaDos functions. */
  17. extern long AbsExecBase;    /* Address of Exec base. */
  18. extern int errno;
  19. extern int _argc;
  20. extern char **_argv;
  21.  
  22. int Enable_Abort;
  23. struct WBStartup *WBenchMsg;
  24. typedef struct _device DEVICE;
  25. DEVICE *_devtab;
  26.  
  27. void (*_fcloseall)();
  28. void (*_freeall)();
  29. void (*_closeall)();
  30. void (*_MathBaseClose)();
  31. void (*_MathTransClose)();
  32.  
  33. FILE    _iob[OPEN_MAX];    /* stream buffers */
  34.  
  35. static FILE    startup[] =    /* standard stream files */
  36.     {
  37. /* stdin */    {0, NULL, NULL, (_IOREAD | _IOFBF), 0, 0, '\0'},
  38. /* stdout */    {0, NULL, NULL, (_IOWRT | _IOFBF), 1, 0, '\0'},
  39. /* stderr */    {0, NULL, NULL, (_IOWRT | _IOFBF), 2, 0, '\0'}
  40.     };
  41.  
  42.     long
  43. _main(length, ptr)
  44. long length;        /* Length of the command string */
  45. char *ptr;        /* Command line (BCPL format)   */
  46. {
  47.     void exit();
  48.     register FILE *f;
  49.     register int i, rv;
  50.     struct Process *taskPtr;
  51.     struct WBArg *wp;
  52.     extern struct Process *FindTask();
  53.     extern void *OpenLibrary(), *GetMsg(), *AllocMem();
  54.     extern long Input(), Output(), Open();
  55.     extern void CurrentDir(), Alert(), CloseLibrary(), _exit();
  56.  
  57.     /*
  58.      * Let Exec and AmigaDos know where to find things.
  59.      */
  60.     SysBase = AbsExecBase;
  61.     if ( (DOSBase = (long)OpenLibrary( "dos.library", 0L )) == 0 ){
  62.         Alert( AG_OpenLib|AO_DOSLib, 0L);
  63.         _exit( 12L );
  64.         }
  65.  
  66.     /*
  67.      * Allocate space for the device table.  (Used for lower level
  68.      * i/o open(), close(), read(), write() etc. functions.
  69.      */
  70.  
  71.     _devtab = (DEVICE *)malloc( (int)(OPEN_MAX*sizeof(struct _device)) );
  72.     if ( _devtab == (DEVICE *)NULL){
  73.         Alert(AG_NoMemory, 0L);
  74.         CloseLibrary( DOSBase );
  75.         _exit(12L);
  76.     }
  77.  
  78.     _devtab[0].mode = O_RDONLY | O_STDIO;    /* stdin */
  79.     _devtab[1].mode = O_WRONLY | O_STDIO;    /* stdout */
  80.     _devtab[0].fileHandle = Input();
  81.     _devtab[1].fileHandle = Output();
  82.  
  83.     /*
  84.      * Parse the command line.  Method depends on whether we are
  85.      * running under the CLI or under Workbench.
  86.      */
  87.  
  88.     taskPtr = FindTask(0L);
  89.     if (taskPtr->pr_CLI != 0) {
  90.         _cli_parse(taskPtr, length, ptr);
  91.         Enable_Abort = 1;
  92.     }
  93.     else {
  94.         WaitPort(&taskPtr->pr_MsgPort);
  95.         WBenchMsg = (struct WBStartup *)GetMsg(&taskPtr->pr_MsgPort);
  96.         if (WBenchMsg->sm_ArgList) {
  97.             wp = WBenchMsg->sm_ArgList;
  98.             CurrentDir(wp->wa_Lock);
  99.         }
  100.         _wb_parse(taskPtr, WBenchMsg);
  101.         _argv = (char **)WBenchMsg;
  102.     }
  103.  
  104.     if ( _devtab[1].fileHandle ) {
  105.         _devtab[2].fileHandle = Open("*", MODE_OLDFILE);
  106.         _devtab[2].mode |= O_WRONLY;    /* stderr */
  107.     }
  108.  
  109.     /*
  110.      * Initialize device streams.
  111.      */
  112.  
  113.     memcpy( _iob, startup, sizeof startup );
  114.     for(i = 0, f = _iob; i < 3; ++i, ++f)    /* flag device streams */
  115.         if(isatty(f->_file))
  116.             f->_flag |= _IODEV;
  117.  
  118.     main(_argc, _argv);            /* if main() returns... */
  119.     exit(EXIT_SUCCESS);            /* ...exit with OK status */
  120. }
  121.  
  122. /*
  123.  * Cleanup and exit.
  124.  */
  125.  
  126. void exit(status)
  127. int status;        
  128. {
  129.     int fd;
  130.  
  131.     if ( _fcloseall)
  132.         (*_fcloseall)();
  133.  
  134.     if ( _closeall)
  135.         (*_closeall)();
  136.  
  137.     if ( _freeall)
  138.         (*_freeall)(); /* Free any malloc()ed memory */    
  139.  
  140.     if ( _MathBaseClose )
  141.         (*_MathBaseClose)();
  142.  
  143.     if ( _MathTransClose )
  144.         (*_MathTransClose)();
  145.  
  146.     if ( DOSBase )
  147.         CloseLibrary( DOSBase );
  148.  
  149.     if ( WBenchMsg != NULL ) {
  150.         Forbid();
  151.         ReplyMsg(WBenchMsg);
  152.     }
  153.  
  154.     _exit ((long)status);
  155. }
  156.