home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 466.lha / AztecArp_v1.9 / Source.LZH / gads.c < prev    next >
C/C++ Source or Header  |  1990-12-05  |  2KB  |  71 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(struct Process *pp,long alen,char *aptr)
  22. {
  23.     struct CommandLineInterface    *cli;
  24.     char                *c,*cp;
  25.     int                 argcount,length;
  26.  
  27.     if(pp -> pr_CLI)
  28.     {
  29.         cli = (struct CommandLineInterface *)BADDR(pp -> pr_CLI);
  30.         cp = (char *)BADDR(cli -> cli_CommandName);
  31.     }
  32.     else
  33.         cp = _detach_name;
  34.  
  35.     length = cp[0];         /* Length of command name */
  36.  
  37.     /* argcount *must* start at 3, do not change this.
  38.      * we need one for argv[0] = progname, and GADS() always
  39.      * requires one arg entry (for error messages, etc.)
  40.      * Then it is quasi standard for 'C' to have a final NULL as
  41.      * the argv array...
  42.      */
  43.  
  44.     for(argcount = 3,c = CLI_Template ; *c ; c++) /* Size we need for argv */
  45.     {
  46.         if(*c == ',')
  47.             argcount++;
  48.     }
  49.  
  50.     if(!(c = ArpAlloc(length)))  /* Get mem for name */
  51.         ArpExit(20L,ERROR_NO_FREE_STORE);
  52.  
  53.     strncpy(c,&cp[1],cp[0]);
  54.  
  55.     if(!(_argv = ArpAlloc((argcount * sizeof(*_argv)))))
  56.         ArpExit(20L,ERROR_NO_FREE_STORE);
  57.  
  58.     _argv[0] = c;
  59.  
  60.     _argc = (int)GADS(aptr,alen,CLI_Help,(_argv + 1),CLI_Template);
  61.  
  62.     if(_argc < 0)
  63.     {
  64.         Printf("Bad Args for %s: %s\n",_argv[0],_argv[1]);
  65.  
  66.         ArpExit(20,ERROR_LINE_TOO_LONG);
  67.     }
  68.  
  69.     _argc++;
  70. }
  71.