home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 154_01 / makearg.c < prev    next >
Text File  |  1979-12-31  |  1KB  |  61 lines

  1. /* ..make a global argv[] by parsing command line.. */
  2.  
  3. #include <stdio.h>
  4. #include <dos.h>
  5. #include <ctype.h>
  6.  
  7. #define MAXARGS 30
  8. int argc;
  9. extern unsigned _pspbase;
  10. char *cl, tail[0x80], **argv, *getarg(), *argbuf[MAXARGS];
  11.  
  12. makeargv()
  13. {
  14.     /* ..initialize argc/argv[].. */
  15.     argc = 1;
  16.     argv = argbuf;
  17.     argv[0] = "";
  18.  
  19.     /* ..get command line from DOS.. */
  20.     _copy(PTR(tail),0x80,_pspbase,0x80);
  21.     cl = &tail[1];
  22.     cl[tail[0]] = '\0';
  23.  
  24.     /* ..parse arguments.. */
  25.     while ((argv[argc++] = getarg()) != '\0') ;
  26.     argv[argc--] = NULL;
  27. }
  28.  
  29. char *getarg()
  30. {
  31.     char arg[0x80], *q;
  32.     int i;
  33.  
  34.     while (*cl != '\0' && isspace(*cl))
  35.         ++cl;
  36.  
  37.     if (*cl == '\0')
  38.         return NULL;
  39.  
  40.     i = 0;
  41.  
  42.     /* ..process quoted argument.. */
  43.     if (*cl == '"')
  44.     {
  45.         ++cl;        /* ..eat open-quote.. */
  46.         while (*cl != '\0' && *cl != '"')
  47.             arg[i++] = *cl++;
  48.         if (*cl == '"')
  49.             ++cl;    /* ..eat close-quote.. */
  50.     }
  51.     else
  52.     /* ..process unquoted argument.. */
  53.         while (*cl != '\0' && !isspace(*cl))
  54.             arg[i++] = *cl++;
  55.  
  56.     arg[i] = '\0';
  57.     q = (char *) malloc(strlen(arg)+1);
  58.     strcpy(q,arg);
  59.     return q;
  60. }
  61.