home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d2xx / d282 / rcs.lha / RCS / rcs.zoo / rcs / rsbx.lib / uxmain.c < prev    next >
C/C++ Source or Header  |  1989-11-12  |  4KB  |  216 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <ios1.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #include <libraries/dos.h>
  7. #include <libraries/dosextens.h>
  8. #include <proto/dos.h>
  9. #include <proto/exec.h>
  10.  
  11. #define MAXARG 256    /* maximum command line arguments */
  12. #define QUOTE  '"'
  13. #define ESCAPE '*'
  14. #define ESC '\027'
  15. #define NL '\n'
  16.  
  17. #define isspace(c) ((c == ' ')||(c == '\t') || (c == '\n'))
  18.  
  19. #ifndef TINY
  20. extern int _fmode,_iomode;
  21. extern int (*_ONBREAK)();
  22. extern int CXBRK();
  23. #endif
  24.  
  25. extern void main(int argc, char **argv);
  26.  
  27. extern char *_ProgramName;
  28. extern struct UFB _ufbs[];
  29. static int argc;        /* arg count */
  30. static char *argv[MAXARG];    /* arg pointers */
  31. static void badarg(char *program);
  32.  
  33. #define MAXWINDOW 40
  34.  
  35. /*
  36.  * name        _xmain - process command line, open files, and call "main"
  37.  *
  38.  * synopsis    _xmain(line, length);
  39.  *        char *line;    argument string to command
  40.  *        int  length    length of argument string
  41.  *
  42.  * description    This function performs the standard pre-processing for
  43.  *        the main module of a C program. It accepts a command
  44.  *        line of the form
  45.  *
  46.  *            arg1 arg2 ...
  47.  *
  48.  *        and builds a list of pointers to each argument. The first
  49.  *        pointer is to the program name. For some environments, the
  50.  *        standard I/O files are also opened, using file names that
  51.  *        were set up by the OS interface module XCMAIN.
  52.  */
  53.  
  54. /*
  55.  * This is a modified umain.c, supplied with the Lattice 5.04 compiler,
  56.  *    that correctly handles AmigaDos escapes.
  57.  *
  58.  *                    Ray
  59.  */
  60.  
  61. void _xmain(char *line, int length)
  62.     {
  63.     char *argbuf;
  64.     int x;
  65.  
  66.     /*
  67.      *
  68.      * Open standard files
  69.      *
  70.      */
  71. #ifndef TINY
  72.  
  73.     _ufbs[0].ufbfh = Input();
  74.     _ufbs[1].ufbfh = Output();
  75.     _ufbs[2].ufbfh = Open("*", MODE_OLDFILE);
  76.  
  77.     _ufbs[0].ufbflg |= UFB_RA | O_RAW | UFB_NC;
  78.     _ufbs[1].ufbflg |= UFB_WA | O_RAW | UFB_NC;
  79.     _ufbs[2].ufbflg |= UFB_RA | O_RAW | UFB_WA;
  80.  
  81.     stdin->_file  = 0;
  82.     stdout->_file = 1;
  83.     stderr->_file = 2;
  84.  
  85.     x = (_fmode) ? 0 : _IOXLAT;
  86.     stdin->_flag  = _IOREAD | x;
  87.     stdout->_flag = _IOWRT  | x;
  88.     stderr->_flag = _IORW   | x;
  89.  
  90.     /* establish control-c handler */
  91.  
  92.     _ONBREAK = CXBRK;
  93.  
  94. #endif
  95.  
  96.     if (!(argbuf = malloc(_ProgramName[-1] + length + 2)))
  97.         {
  98.         _exit(1);
  99.         }
  100.  
  101.     argv[argc++] = argbuf;
  102.     strncpy(argbuf, _ProgramName, _ProgramName[-1]);
  103.     argbuf += _ProgramName[-1];
  104.     *argbuf++ = '\0';
  105.  
  106.     /*
  107.      *
  108.      * Build argument pointer list
  109.      *
  110.      */
  111.     while (argc < MAXARG)
  112.         {
  113.         while (isspace(*line) && length)
  114.             {
  115.             line++;
  116.             length--;
  117.             }
  118.         if (!length)
  119.             {
  120.             break;
  121.             }
  122.         argv[argc++] = argbuf;
  123.         if (*line == QUOTE)
  124.             {
  125.             line++;
  126.             length--;
  127.             while (length && (*line != QUOTE))
  128.                 {
  129.                 if (*line == ESCAPE)
  130.                     {
  131.                     line++;
  132.                     length--;
  133.                     if (!length)
  134.                         {
  135.                         badarg(*argv);
  136.                         }
  137.                     else
  138.                         {
  139.                         switch (*line)
  140.                             {
  141.                             case 'E':
  142.                                 *argbuf++ = ESC;
  143.                                 break;
  144.                             case 'N':
  145.                                 *argbuf++ = NL;
  146.                                 break;
  147.                             default:
  148.                                 *argbuf++ = *line;
  149.                             }
  150.                         length--;
  151.                         line++;
  152.                         }
  153.                     }
  154.                 else
  155.                     {
  156.                     *argbuf++ = *line++;
  157.                     length--;
  158.                     }
  159.                 }
  160.  
  161.             if (!length)
  162.                 {
  163.                 badarg(*argv);
  164.                 }
  165.  
  166.             line++;
  167.             length--;
  168.             *argbuf++ = '\0';    /* terminate arg */
  169.  
  170.             if (length && !isspace(*line))
  171.                 {
  172.                 badarg(*argv);
  173.                 }
  174.             }
  175.         else  /* non-quoted arg */
  176.             {
  177.             while (length && (!isspace(*line)))
  178.                 {
  179.                 *argbuf++ = *line++;
  180.                 length--;
  181.                 }
  182.             if (!length)
  183.                 {
  184.                 *argbuf = '\0';
  185.                 break;
  186.                 }
  187.             else
  188.                 {
  189.                 *argbuf++ = '\0';    /* terminate arg */
  190.                 }
  191.             }
  192.         }  /* while */
  193.  
  194.     /*
  195.      *
  196.      * Call user's main program
  197.      *
  198.      */
  199.  
  200.     main(argc, argv);    /* call main function */
  201. #ifndef TINY
  202.     exit(0);
  203. #else
  204.     _exit(0);
  205. #endif
  206.     }
  207.  
  208.  
  209. static void badarg(char *program)
  210.     {
  211.     fputs("Invalid argument to ", stderr);
  212.     fputs(program, stderr);
  213.     fputc('\n', stderr);
  214.     exit(100);
  215.     }
  216.