home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff376.lzh / AztecArp / Sources.LZH / cliparse.c < prev    next >
C/C++ Source or Header  |  1990-08-01  |  2KB  |  102 lines

  1. /* Copyright (C) 1986,1987 by Manx Software Systems, Inc. */
  2.  
  3. /*
  4.  *    This routine is called from the _main() routine and is used to
  5.  *    parse the arguments passed from the CLI to the program. It sets
  6.  *    up an array of pointers to arguments in the global variables and
  7.  *    and sets up _argc and _argv which will be passed by _main() to
  8.  *    the main() procedure. If no arguments are ever going to be
  9.  *    parsed, this routine may be replaced by a stub routine to reduce
  10.  *    program size.
  11.  *
  12.  *    If _arg_lin is non-zero, the _exit() routine will call FreeMem()
  13.  *    with _arg_lin as the memory to free and _arg_len as the size.
  14.  *
  15.  */
  16.  
  17. extern int _argc, _arg_len;
  18. extern char **_argv, *_arg_lin;
  19. extern char *_detach_name;            /* for DETACHED programs */
  20.  
  21. void
  22. _cli_parse(struct Process *pp, long alen, char *aptr)
  23. {
  24.     char *cp;
  25.     struct CommandLineInterface *cli;
  26.     int c;
  27.  
  28.     if (pp -> pr_CLI)
  29.     {
  30.         cli = (struct CommandLineInterface *) ((long)pp -> pr_CLI << 2);
  31.         cp = (char *)((long)cli -> cli_CommandName << 2);
  32.     }
  33.     else
  34.         cp = _detach_name;
  35.  
  36.     _arg_len = cp[0] + alen + 2;
  37.  
  38.     if (!(_arg_lin = ArpAlloc(_arg_len)))
  39.         ArpExit(20L,ERROR_NO_FREE_STORE);
  40.  
  41.     c = cp[0];
  42.  
  43.     strncpy(_arg_lin, cp + 1, (size_t)c);
  44.     strcpy(_arg_lin + c, " ");
  45.     strncat(_arg_lin, aptr, (size_t)alen);
  46.  
  47.     _arg_lin[c] = 0;
  48.  
  49.     for (_argc = 1, aptr = cp = _arg_lin + c + 1 ; ; _argc++)
  50.     {
  51.         while ((c = *cp) == ' ' || c == '\t' || c == '\f' || c == '\r' || c == '\n')
  52.             cp++;
  53.  
  54.         if (*cp < ' ')
  55.             break;
  56.  
  57.         if (*cp == '"')
  58.         {
  59.             cp++;
  60.  
  61.             while (c = *cp++)
  62.             {
  63.                 *aptr++ = c;
  64.  
  65.                 if (c == '"')
  66.                 {
  67.                     if (*cp == '"')
  68.                         cp++;
  69.                     else
  70.                     {
  71.                         aptr[-1] = 0;
  72.                         break;
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.         else
  78.         {
  79.             while ((c = *cp++) && c != ' ' && c != '\t' && c != '\f' && c != '\r' && c != '\n')
  80.                 *aptr++ = c;
  81.  
  82.             *aptr++ = 0;
  83.         }
  84.  
  85.         if (c == 0)
  86.             --cp;
  87.     }
  88.  
  89.     *aptr = 0;
  90.  
  91.     if (!(_argv = ArpAlloc((_argc + 1) * sizeof(*_argv))))
  92.         ArpExit(20L,ERROR_NO_FREE_STORE);
  93.  
  94.     for (c = 0 , cp = _arg_lin ; c < _argc ; c++)
  95.     {
  96.         _argv[c] = cp;
  97.         cp += strlen(cp) + 1;
  98.     }
  99.  
  100.     _argv[c] = 0;
  101. }
  102.