home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 493.lha / OnTrap / umain.c < prev   
Encoding:
C/C++ Source or Header  |  1991-04-06  |  4.3 KB  |  182 lines

  1. /*      _main.c         Copyright (C) 1985  Lattice, Inc.       */
  2.  
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <ios1.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <workbench/startup.h>
  9. #include <libraries/dos.h>
  10. #include <libraries/dosextens.h>
  11. #include <proto/dos.h>
  12. #include <proto/exec.h>
  13.  
  14. #define MAXARG 32              /* maximum command line arguments */
  15. #define QUOTE  '"'
  16. #define ESCAPE '*'
  17. #define ESC '\027'
  18. #define NL '\n'
  19.  
  20. #define isspace(c)      ((c == ' ')||(c == '\t') || (c == '\n'))
  21.  
  22. #ifndef TINY
  23. extern int _fmode,_iomode;
  24. extern int (*_ONBREAK)();
  25. extern int CXBRK();
  26. /*JVP*/
  27. extern int (*_ONTRAP)();
  28. extern int CXTRAP();
  29. #endif
  30.  
  31. extern struct UFB _ufbs[];
  32. static int argc;                       /* arg count */
  33. static char **targv, *argv[MAXARG];     /* arg pointers */
  34. static void badarg(char *program);
  35.  
  36. #define MAXWINDOW 40
  37. extern struct WBStartup *WBenchMsg;
  38. /**
  39. *
  40. * name         _main - process command line, open files, and call "main"
  41. *
  42. * synopsis     _main(line);
  43. *              char *line;     ptr to command line that caused execution
  44. *
  45. * description   This function performs the standard pre-processing for
  46. *               the main module of a C program.  It accepts a command
  47. *               line of the form
  48. *
  49. *                       pgmname arg1 arg2 ...
  50. *
  51. *               and builds a list of pointers to each argument.  The first
  52. *               pointer is to the program name.  For some environments, the
  53. *               standard I/O files are also opened, using file names that
  54. *               were set up by the OS interface module XCMAIN.
  55. *
  56. **/
  57. void _main(line)
  58. register char *line;
  59. {
  60.    register char **pargv;
  61.    register int x;
  62.    struct Process *process;
  63.    struct FileHandle *handle;
  64.    static char window[MAXWINDOW+18];
  65.     char *argbuf;
  66.  
  67. /*
  68. *
  69. * Build argument pointer list
  70. *
  71. */
  72.    
  73.    while (argc < MAXARG)
  74.    {
  75.         while (isspace(*line))  line++;
  76.         if (*line == '\0')      break;
  77.         pargv = &argv[argc++];
  78.         if (*line == QUOTE)
  79.         {
  80.             argbuf = *pargv = ++line;  /* ptr inside quoted string */
  81.                 while (*line != QUOTE)
  82.                 {
  83.                    if (*line == ESCAPE)
  84.                     {
  85.                           line++;
  86.                         switch (*line)
  87.                         {
  88.                             case 'E':
  89.                                 *argbuf++ = ESC;
  90.                                 break;
  91.                             case 'N':
  92.                                 *argbuf++ = NL;
  93.                                 break;
  94.                             default:
  95.                                 *argbuf++ = *line;
  96.                         }
  97.                         line++;
  98.                     }
  99.                    else
  100.                    {
  101.                      *argbuf++ = *line++;
  102.                    }
  103.               }
  104.               line++;
  105.               *argbuf++ = '\0';    /* terminate arg */
  106.         }
  107.         else            /* non-quoted arg */
  108.         {       
  109.             *pargv = line;
  110.             while ((*line != '\0') && (!isspace(*line))) line++;
  111.             if (*line == '\0')  break;
  112.             else                *line++ = '\0';  /* terminate arg */
  113.         }
  114.    }  /* while */
  115.    targv = (argc == 0) ? (char **)WBenchMsg : (char **)&argv[0];
  116.  
  117.  
  118. /*
  119. *
  120. * Open standard files
  121. *
  122. */
  123. #ifndef TINY
  124.  
  125.    if (argc == 0)          /* running under workbench      */
  126.    {
  127.         strcpy(window, "con:10/10/320/80/");
  128.         strncat(window, WBenchMsg->sm_ArgList->wa_Name,MAXWINDOW);
  129.         _ufbs[0].ufbfh = Open(window,MODE_NEWFILE);
  130.         _ufbs[1].ufbfh = _ufbs[0].ufbfh;
  131.         _ufbs[1].ufbflg = UFB_NC;
  132.         _ufbs[2].ufbfh = _ufbs[0].ufbfh;
  133.         _ufbs[2].ufbflg = UFB_NC;
  134.         handle = (struct FileHandle *)(_ufbs[0].ufbfh << 2);
  135.         process = (struct Process *)FindTask(0);
  136.         process->pr_ConsoleTask = (APTR)handle->fh_Type;
  137.         x = 0;
  138.    }
  139.    else                    /* running under CLI            */
  140.    {
  141.         _ufbs[0].ufbfh = Input();
  142.         _ufbs[1].ufbfh = Output();
  143.         _ufbs[2].ufbfh = Open("*", MODE_OLDFILE);
  144.         x = UFB_NC;                     /* do not close CLI defaults    */
  145.    }
  146.  
  147.    _ufbs[0].ufbflg |= UFB_RA | O_RAW | x;
  148.    _ufbs[1].ufbflg |= UFB_WA | O_RAW | x;
  149.    _ufbs[2].ufbflg |= UFB_RA | UFB_WA | O_RAW;
  150.  
  151.    x = (_fmode) ? 0 : _IOXLAT;
  152.    stdin->_file = 0;
  153.    stdin->_flag = _IOREAD | x;   
  154.    stdout->_file = 1;
  155.    stdout->_flag = _IOWRT | x;
  156.    stderr->_file = 2;
  157.    stderr->_flag = _IORW | x;
  158.  
  159. /*      establish control-c handler */
  160.  
  161. _ONBREAK = CXBRK;
  162.  
  163. /*JVP   establish trap handler */
  164.  
  165. _ONTRAP = CXTRAP;
  166.  
  167. #endif
  168.  
  169. /*
  170. *
  171. * Call user's main program
  172. *
  173. */
  174.  
  175.    main(argc,targv);              /* call main function */
  176. #ifndef TINY
  177.    exit(0);
  178. #else
  179. _  exit(0);
  180. #endif
  181. }
  182.