home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / INFO / C / DLIBSSRC.ZIP / INITARGS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-08  |  5.4 KB  |  221 lines

  1. #include <osbind.h>
  2. #include <stdio.h>
  3. #include <io.h>
  4.  
  5. #define    REDIRECT
  6. /*
  7.  *    Since the operating system and many shells don't do i/o
  8.  *    redirection like they should, this #define includes code
  9.  *    to handle redirection during argument processing.  If
  10.  *    you don't want this to happen, #undef REDIRECT
  11.  */
  12.  
  13. extern char    *_base;
  14. extern int    _argc;
  15. extern char    **_argv;
  16. extern char    *_envp;
  17.  
  18. static char    args[512];        /* argc/argv buffer */
  19.  
  20. void _initargs(cmdline, cmdlen)
  21. register char *cmdline;
  22. register int cmdlen;
  23. /*
  24.  *    Process command line and retrieve the environment string.  This
  25.  *    function is responsible for allocating space to hold the parsed
  26.  *    command line arguments, etc.  Values to be passed to the user's
  27.  *    main() function are stored in the global variables "_argc",
  28.  *    "_argv" and "_envp".  _main() initiallizes these variables to
  29.  *    indicate that no arguments are available.  If a program doesn't
  30.  *    use command line arguments or the environment string, an
  31.  *    _initargs() function, which does nothing, should be defined in
  32.  *    the module containing main().  This will result in a smaller
  33.  *    executable size and a smaller run-time image.
  34.  */
  35. {
  36.     register char *p, **q;
  37.     register int i, j, n;
  38.     char *t, *strtok();
  39.  
  40.     /* process command line args */
  41.     if(q = p = args) {
  42.         n = 1;
  43.         p += 256;
  44.         _argv = q;
  45.         *q++ = p;
  46.         t = *((char **) (_base + 0x24));    /* parent's basepage */
  47.         t = *((char **) (t + 0x20));        /* dta buffer */
  48.         t += 30;                /* name field */
  49.         while(*p++ = *t++)            /* make argv[0] */
  50.             ;
  51.         for(t=p; cmdlen--; *p++ = *cmdline++)
  52.             ;
  53.         *p = '\0';
  54.         p = strtok(t, " \t");
  55.         while(p) {
  56. #ifdef REDIRECT
  57.             switch(*p) {
  58.                 case '<':    /* redirect stdin */
  59.                     i = open(++p, _OPNr);
  60.                     if(i < 0) {
  61.                         Cconws("Can't open ");
  62.                         Cconws(p);
  63.                         Cconws(" as stdin.\r\n");
  64.                         exit(ERROR);
  65.                     }
  66.                     stdin->F_h = i;
  67.                     stdin->F_stat &= ~F_DEVICE;
  68.                     break;
  69.                 case '>':    /* redirect stdout */
  70.                     j = FALSE;
  71.                     if(*++p == '>') {    /* append */
  72.                         i = open(++p, _OPNw);
  73.                         j = TRUE;
  74.                     }
  75.                     else
  76.                         i = creat(p, _CRErw);
  77.                     if(i < 0) {
  78.                         Cconws("Can't open ");
  79.                         Cconws(p);
  80.                         Cconws(" as stdout.\r\n");
  81.                         exit(ERROR);
  82.                     }
  83.                     if(j)
  84.                         lseek(i, 0L, _LSKend);
  85.                     stdout->F_h = i;
  86.                     stdout->F_stat &= ~F_DEVICE;
  87.                     break;
  88.                 default:    /* normal argument */
  89.                     ++n;
  90.                     *q++ = p;
  91.             }
  92. #else
  93.             ++n;
  94.             *q++ = p;
  95. #endif
  96.             p = strtok(NULL, " \t");
  97.         }
  98.         _argc = n;
  99.     }
  100. }
  101.  
  102. /*------------------------------------------------------------------------*/
  103. /*    The following are alternate _initargs() implementations,      */
  104. /*    each of which has certain features that may be useful in      */
  105. /*    various applications.                -Dal          */
  106. /*------------------------------------------------------------------------*/
  107.  
  108. #ifdef NEVER
  109.  
  110. /*
  111.  *    This _initargs() function implements redirection and wildcard
  112.  *    filename expansion, and allocates space for arguments dynamically.
  113.  *    It does not support quoting of any kind.  This should be added.
  114.  *                        -Dal  (10/01/87)
  115.  */
  116.  
  117. #define    ARGLIMIT    64        /* maximum number of arguments */
  118. #define    WILDCARD    TRUE        /* expand wildcards */
  119.  
  120. extern char    *_base;
  121. extern int    _argc;
  122. extern char    **_argv;
  123. extern char    *_envp;
  124.  
  125. static char    *args[ARGLIMIT];    /* argv pointer buffer */
  126.  
  127. void _initargs(cmdline, cmdlen)
  128. char *cmdline;
  129. int cmdlen;
  130. /*
  131.  *    Process command line, handling redirection and wildcards.  This
  132.  *    function is responsible for allocating space to hold the parsed
  133.  *    command line arguments, etc.  Values to be passed to the user's
  134.  *    main() function are stored in the global variables "_argc",
  135.  *    "_argv" and "_envp".
  136.  */
  137. {
  138.     register char *p, **q, *s;
  139.     register int i, j, n;
  140.     STAT dir, *oldirp;
  141.     char tmp[128];
  142.     char *strtok(), *strdup(), *strrpbrk();
  143.  
  144.     /* process command line args */
  145.     if(q = args) {
  146.     n = 1;
  147.     _argv = q;
  148.     s = *((char **) (_base + 0x24));    /* parent's basepage */
  149.     s = *((char **) (t + 0x20));        /* dta buffer */
  150.     s += 30;                /* name field */
  151.     *q++ = strdup(s);            /* make argv[0] */
  152.     cmdline[cmdlen] = '\0';
  153.     p = strtok(cmdline, " \t");
  154.     while(p && (n < ARGLIMIT)) {
  155.         switch(*p) {
  156.         case '<':    /* redirect stdin */
  157.             i = open(++p, _OPNr);
  158.             if(i < 0) {
  159.                 Cconws("Can't open ");
  160.                 Cconws(p);
  161.                 Cconws(" as stdin.\r\n");
  162.                 exit(ERROR);
  163.             }
  164.             stdin->F_h = i;
  165.             stdin->F_stat &= ~F_DEVICE;
  166.             break;
  167.         case '>':    /* redirect stdout */
  168.             j = FALSE;
  169.             if(*++p == '>') {    /* append */
  170.                 i = open(++p, _OPNw);
  171.                 j = TRUE;
  172.             }
  173.             else
  174.                 i = creat(p, _CRErw);
  175.             if(i < 0) {
  176.                 Cconws("Can't open ");
  177.                 Cconws(p);
  178.                 Cconws(" as stdout.\r\n");
  179.                 exit(ERROR);
  180.             }
  181.             if(j)
  182.                 lseek(i, 0L, _LSKend);
  183.             stdout->F_h = i;
  184.             stdout->F_stat &= ~F_DEVICE;
  185.             break;
  186.         default:
  187. #ifdef WILDCARD
  188.             if(strchr(p, '*') || strchr(p, '?')) {
  189.                 oldirp = Fgetdta();
  190.                 Fsetdta(&dir);
  191.                 if(Fsfirst(p, 0x00) == 0) {
  192.                     if(s = strrpbrk(p, ":\\"))
  193.                         *++s = '\0';
  194.                     else
  195.                         *p = '\0';
  196.                     do {
  197.                         ++n;
  198.                         strcpy(tmp, p);
  199.                         strcat(tmp, dir.S_name);
  200.                         *q++ = strdup(tmp);
  201.                     } while(Fsnext() == 0);
  202.                 }
  203.                 Fsetdta(oldirp);
  204.             }
  205.             else {            /* normal argument */
  206.                 ++n;
  207.                 *q++ = strdup(p);
  208.             }
  209. #else /* !WILDCARD */
  210.             ++n;
  211.             *q++ = strdup(p);
  212. #endif /* WILDCARD */
  213.         }
  214.         p = strtok(NULL, " \t");
  215.     }
  216.     _argc = n;
  217.     }
  218. }
  219.  
  220. #endif /* NEVER */
  221.