home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 300-399 / ff353.lzh / AztecArp / gads.c < prev    next >
C/C++ Source or Header  |  1990-06-02  |  2KB  |  93 lines

  1. /* Created 11/08/87 by -=+SDB+=- from cliparse.c provided by Manx */
  2. /* Copyright (C) 1987 by Scott Ballantyne */
  3. /* May be freely used by arp supporters/users */
  4.  
  5. /* This routine is called from _main() and parses the arguments passed from
  6.  * the CLI to the program.  It uses the ARP tracking functions to allocate
  7.  * memory for the argv array, and sets up _argc and _argv (using GADS)
  8.  * which will eventually be passed as parameters to main().
  9.  *
  10.  * It uses the Global variables CLI_Template and CLI_Help to set the command
  11.  * templates and extra help string for GADS() - if you don't set these up
  12.  * yourself, you get the defaults.
  13.  */
  14.  
  15. extern int _argc;
  16. extern char **_argv;
  17. extern char *CLI_Template;
  18. extern char *CLI_Help;
  19. extern char *_detach_name;            /* for DETACHED programs */
  20.  
  21. _cli_parse(pp, alen, aptr)
  22. struct Process *pp;
  23. long alen;
  24. char *aptr;
  25. {
  26.     register char *cp;
  27.     register struct CommandLineInterface *cli;
  28.     register char *c;
  29.     register int argcount;
  30.     int length;
  31.  
  32.     if (pp->pr_CLI) {
  33.         cli = (struct CommandLineInterface *) ((long)pp->pr_CLI << 2);
  34.         cp = (char *)((long)cli->cli_CommandName << 2);
  35.     }
  36.     else
  37.         cp = _detach_name;
  38.  
  39.     length = cp[0];         /* Length of command name */
  40.  
  41.     /* argcount *must* start at 3, do not change this.
  42.      * we need one for argv[0] = progname, and GADS() always
  43.      * requires one arg entry (for error messages, etc.)
  44.      * Then it is quasi standard for 'C' to have a final NULL as
  45.      * the argv array...
  46.      */
  47.     for (argcount = 3, c = CLI_Template; *c; c++ ) /* Size we need for argv */
  48.         if (*c == ',')
  49.             argcount++;
  50.  
  51.     if ((c = ArpAlloc((long)length)) == 0)  /* Get mem for name */
  52.         ArpExit(20L, ERROR_NO_FREE_STORE);
  53.     strncpy(c, cp+1, cp[0]);
  54.  
  55.     if ( (_argv = ArpAlloc( (long)(argcount * sizeof(*_argv)))) == 0)
  56.         ArpExit(20L, ERROR_NO_FREE_STORE);
  57.  
  58.     _argv[0] = c;
  59.     _argc = (int)GADS(aptr, alen, CLI_Help, (_argv+1), CLI_Template);
  60.     if (_argc < 0 )
  61.     {
  62.         Printf("Bad Args for %s: %s\n", _argv[0], _argv[1] );
  63.         ArpExit(20L, ERROR_LINE_TOO_LONG);
  64.     }
  65.     _argc++;
  66.  
  67. #if 0
  68.     if (GADS(aptr, alen, CLI_Help, (_argv+1), CLI_Template) < 0) {
  69.         Printf("Bad Args for %s: %s\n", _argv[0], _argv[1] );
  70.         ArpExit(20L, ERROR_LINE_TOO_LONG);
  71.     }
  72.  
  73.     _argc = countargs(argcount-1,_argv);
  74. #endif
  75. }
  76.  
  77. #if 0
  78.  
  79. static
  80. countargs (argc,argv)
  81. register int argc;
  82. register char **argv;
  83. {
  84.     argv += argc;
  85.  
  86.     while (--argc)
  87.     if (*(--argv)) break;
  88.  
  89.     return argc+1;
  90. }
  91.  
  92. #endif
  93.