2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
10 #include <afsconfig.h>
11 #include <afs/param.h>
20 /* declaration of private token type */
22 struct cmd_token *next;
26 static struct cmd_item dummy; /* non-null ptr used for flag existence */
27 static struct cmd_syndesc *allSyntax = 0;
28 static int noOpcodes = 0;
29 static int (*beforeProc) (struct cmd_syndesc * ts, void *beforeRock) = NULL;
30 static int (*afterProc) (struct cmd_syndesc * ts, void *afterRock) = NULL;
31 static int enablePositional = 1;
32 static int enableAbbreviation = 1;
33 static void *beforeRock, *afterRock;
34 static char initcmd_opcode[] = "initcmd"; /*Name of initcmd opcode */
36 /* take name and string, and return null string if name is empty, otherwise return
37 the concatenation of the two strings */
39 NName(char *a1, char *a2)
41 static char tbuffer[300];
42 if (strlen(a1) == 0) {
45 strncpy(tbuffer, a1, sizeof(tbuffer));
46 strncat(tbuffer, a2, sizeof(tbuffer));
47 tbuffer[sizeof(tbuffer)-1]='\0';
52 /* return true if asub is a substring of amain */
54 SubString(char *amain, char *asub)
58 mlen = (int) strlen(amain);
59 slen = (int) strlen(asub);
62 return 0; /* not a substring */
63 for (i = 0; i <= j; i++) {
64 if (strncmp(amain, asub, slen) == 0)
68 return 0; /* didn't find it */
72 FindType(struct cmd_syndesc *as, char *aname)
78 struct cmd_item *alias;
80 /* Allow --long-style options. */
81 if (aname[0] == '-' && aname[1] == '-' && aname[2] && aname[3]) {
85 cmdlen = strlen(aname);
88 for (i = 0; i < CMD_MAXPARMS; i++) {
89 if (as->parms[i].type == 0)
90 continue; /* this slot not set (seeked over) */
91 if (strcmp(as->parms[i].name, aname) == 0)
93 if (strlen(as->parms[i].name) < cmdlen)
96 /* Check for aliases, which must be full matches */
97 alias = as->parms[i].aliases;
98 while (alias != NULL) {
99 if (strcmp(alias->data, aname) == 0)
104 /* A hidden option must be a full match (no best matches) */
105 if (as->parms[i].flags & CMD_HIDE || !enableAbbreviation)
108 if (strncmp(as->parms[i].name, aname, cmdlen) == 0) {
115 return (ambig ? -1 : best);
118 static struct cmd_syndesc *
119 FindSyntax(char *aname, int *aambig)
121 struct cmd_syndesc *ts;
122 struct cmd_syndesc *best;
126 cmdLen = strlen(aname);
127 best = (struct cmd_syndesc *)0;
130 *aambig = 0; /* initialize to unambiguous */
131 for (ts = allSyntax; ts; ts = ts->next) {
132 if (strcmp(aname, ts->name) == 0)
134 if (strlen(ts->name) < cmdLen)
135 continue; /* we typed more than item has */
136 /* A hidden command must be a full match (no best matches) */
137 if (ts->flags & CMD_HIDDEN)
140 /* This is just an alias for *best, or *best is just an alias for us.
141 * If we don't make this check explicitly, then an alias which is just a
142 * short prefix of the real command's name might make things ambiguous
143 * for no apparent reason.
145 if (best && ts->aliasOf == best->aliasOf)
147 if (strncmp(ts->name, aname, cmdLen) == 0) {
149 ambig = 1; /* ambiguous name */
156 *aambig = ambig; /* if ambiguous and they care, tell them */
157 return (struct cmd_syndesc *)0; /* fails */
159 return best; /* otherwise its not ambiguous, and they know */
162 /* print the help for a single parameter */
164 PrintParmHelp(struct cmd_parmdesc *aparm)
166 if (aparm->type == CMD_FLAG) {
168 /* doc people don't like seeing this information */
170 printf(" (%s)", aparm->help);
172 } else if (aparm->help) {
173 printf(" <%s>", aparm->help);
174 if (aparm->type == CMD_LIST)
176 } else if (aparm->type == CMD_SINGLE)
178 else if (aparm->type == CMD_LIST)
182 extern char *AFSVersion;
185 VersionProc(struct cmd_syndesc *as, void *arock)
187 printf("%s\n", AFSVersion);
192 PrintSyntax(struct cmd_syndesc *as)
195 struct cmd_parmdesc *tp;
197 /* now print usage, from syntax table */
199 printf("Usage: %s", as->a0name);
201 if (!strcmp(as->name, initcmd_opcode))
202 printf("Usage: %s[%s]", NName(as->a0name, " "), as->name);
204 printf("Usage: %s%s", NName(as->a0name, " "), as->name);
207 for (i = 0; i < CMD_MAXPARMS; i++) {
210 continue; /* seeked over slot */
211 if (tp->flags & CMD_HIDE)
212 continue; /* skip hidden options */
214 if (tp->flags & CMD_OPTIONAL)
216 printf("%s", tp->name);
218 if (tp->flags & CMD_OPTIONAL)
224 /* must print newline in any case, to terminate preceding line */
226 PrintAliases(struct cmd_syndesc *as)
228 struct cmd_syndesc *ts;
230 if (as->flags & CMD_ALIAS) {
232 printf("(alias for %s)\n", ts->name);
236 return; /* none, print nothing */
238 for (as = as->nextAlias; as; as = as->nextAlias) {
239 printf("%s ", as->name);
246 PrintFlagHelp(struct cmd_syndesc *as)
249 struct cmd_parmdesc *tp;
253 /* find flag name length */
255 for (i = 0; i < CMD_MAXPARMS; i++) {
256 if (i == CMD_HELPPARM)
259 if (tp->type != CMD_FLAG)
261 if (tp->flags & CMD_HIDE)
262 continue; /* skip hidden options */
266 if (strlen(tp->name) > flag_width)
267 flag_width = strlen(tp->name);
270 /* print flag help */
271 flag_prefix = "Where:";
272 for (i = 0; i < CMD_MAXPARMS; i++) {
273 if (i == CMD_HELPPARM)
276 if (tp->type != CMD_FLAG)
278 if (tp->flags & CMD_HIDE)
279 continue; /* skip hidden options */
283 printf("%-7s%-*s %s\n", flag_prefix, flag_width, tp->name, tp->help);
289 AproposProc(struct cmd_syndesc *as, void *arock)
291 struct cmd_syndesc *ts;
296 tsub = as->parms[0].items->data;
297 for (ts = allSyntax; ts; ts = ts->next) {
298 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
300 if (SubString(ts->help, tsub)) {
301 printf("%s: %s\n", ts->name, ts->help);
303 } else if (SubString(ts->name, tsub)) {
304 printf("%s: %s\n", ts->name, ts->help);
309 printf("Sorry, no commands found\n");
314 HelpProc(struct cmd_syndesc *as, void *arock)
316 struct cmd_syndesc *ts;
321 if (as->parms[0].items == 0) {
322 printf("%sCommands are:\n", NName(as->a0name, ": "));
323 for (ts = allSyntax; ts; ts = ts->next) {
324 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
326 printf("%-15s %s\n", ts->name, (ts->help ? ts->help : ""));
329 /* print out individual help topics */
330 for (ti = as->parms[0].items; ti; ti = ti->next) {
332 ts = FindSyntax(ti->data, &ambig);
333 if (ts && (ts->flags & CMD_HIDDEN))
334 ts = 0; /* no hidden commands */
336 /* print out command name and help */
337 printf("%s%s: %s ", NName(as->a0name, " "), ts->name,
338 (ts->help ? ts->help : ""));
339 ts->a0name = as->a0name;
345 fprintf(stderr, "%sUnknown topic '%s'\n",
346 NName(as->a0name, ": "), ti->data);
348 /* ambiguous, list 'em all */
350 "%sAmbiguous topic '%s'; use 'apropos' to list\n",
351 NName(as->a0name, ": "), ti->data);
353 code = CMD_UNKNOWNCMD;
361 cmd_SetBeforeProc(int (*aproc) (struct cmd_syndesc * ts, void *beforeRock),
370 cmd_SetAfterProc(int (*aproc) (struct cmd_syndesc * ts, void *afterRock),
378 /* thread on list in alphabetical order */
380 SortSyntax(struct cmd_syndesc *as)
382 struct cmd_syndesc **ld, *ud;
384 for (ld = &allSyntax, ud = *ld; ud; ld = &ud->next, ud = *ld) {
385 if (strcmp(ud->name, as->name) > 0) { /* next guy is bigger than us */
389 /* thread us on the list now */
396 cmd_CreateSyntax(char *aname,
397 int (*aproc) (struct cmd_syndesc * ts, void *arock),
398 void *arock, char *ahelp)
400 struct cmd_syndesc *td;
402 /* can't have two cmds in no opcode mode */
406 td = calloc(1, sizeof(struct cmd_syndesc));
408 td->aliasOf = td; /* treat aliasOf as pointer to real command, no matter what */
410 /* copy in name, etc */
412 td->name = malloc(strlen(aname) + 1);
414 strcpy(td->name, aname);
420 /* Piggy-back the hidden option onto the help string */
421 if (ahelp == (char *)CMD_HIDDEN) {
422 td->flags |= CMD_HIDDEN;
424 td->help = malloc(strlen(ahelp) + 1);
426 strcpy(td->help, ahelp);
435 cmd_Seek(td, CMD_HELPPARM);
436 cmd_AddParm(td, "-help", CMD_FLAG, CMD_OPTIONAL, "get detailed help");
443 cmd_CreateAlias(struct cmd_syndesc *as, char *aname)
445 struct cmd_syndesc *td;
447 td = malloc(sizeof(struct cmd_syndesc));
449 memcpy(td, as, sizeof(struct cmd_syndesc));
450 td->name = malloc(strlen(aname) + 1);
452 strcpy(td->name, aname);
453 td->flags |= CMD_ALIAS;
454 /* if ever free things, make copy of help string, too */
459 /* thread on alias lists */
460 td->nextAlias = as->nextAlias;
464 return 0; /* all done */
468 cmd_DisablePositionalCommands(void)
470 enablePositional = 0;
474 cmd_DisableAbbreviations(void)
476 enableAbbreviation = 0;
480 cmd_IsAdministratorCommand(struct cmd_syndesc *as)
482 as->flags |= CMD_ADMIN;
487 cmd_Seek(struct cmd_syndesc *as, int apos)
489 if (apos >= CMD_MAXPARMS)
490 return CMD_EXCESSPARMS;
496 cmd_AddParmAtOffset(struct cmd_syndesc *as, char *aname, int atype,
497 afs_int32 aflags, char *ahelp, int ref)
499 struct cmd_parmdesc *tp;
501 if (ref >= CMD_MAXPARMS)
502 return CMD_EXCESSPARMS;
503 tp = &as->parms[ref];
505 tp->name = malloc(strlen(aname) + 1);
507 strcpy(tp->name, aname);
512 tp->help = malloc(strlen(ahelp) + 1);
514 strcpy(tp->help, ahelp);
520 if (as->nParms <= ref)
527 cmd_AddParm(struct cmd_syndesc *as, char *aname, int atype,
528 afs_int32 aflags, char *ahelp)
530 if (as->nParms >= CMD_MAXPARMS)
531 return CMD_EXCESSPARMS;
533 return cmd_AddParmAtOffset(as, aname, atype, aflags, ahelp, as->nParms++);
537 cmd_AddParmAlias(struct cmd_syndesc *as, int pos, char *alias)
539 struct cmd_item *item;
541 if (pos > as->nParms)
542 return CMD_EXCESSPARMS;
544 item = calloc(1, sizeof(struct cmd_item));
545 item->data = strdup(alias);
546 item->next = as->parms[pos].aliases;
547 as->parms[pos].aliases = item;
552 /* add a text item to the end of the parameter list */
554 AddItem(struct cmd_parmdesc *aparm, char *aval)
556 struct cmd_item *ti, *ni;
557 ti = calloc(1, sizeof(struct cmd_item));
559 ti->data = malloc(strlen(aval) + 1);
561 strcpy(ti->data, aval);
562 /* now put ti at the *end* of the list */
563 if ((ni = aparm->items)) {
564 for (; ni; ni = ni->next)
566 break; /* skip to last one */
569 aparm->items = ti; /* we're first */
573 /* skip to next non-flag item, if any */
575 AdvanceType(struct cmd_syndesc *as, afs_int32 aval)
578 struct cmd_parmdesc *tp;
580 /* first see if we should try to grab rest of line for this dude */
581 if (as->parms[aval].flags & CMD_EXPANDS)
584 /* if not, find next non-flag used slot */
585 for (next = aval + 1; next < CMD_MAXPARMS; next++) {
586 tp = &as->parms[next];
587 if (tp->type != 0 && tp->type != CMD_FLAG)
593 /* discard parameters filled in by dispatch */
595 ResetSyntax(struct cmd_syndesc *as)
598 struct cmd_parmdesc *tp;
599 struct cmd_item *ti, *ni;
602 for (i = 0; i < CMD_MAXPARMS; i++, tp++) {
606 /* free whole list in both cases, just for fun */
607 for (ti = tp->items; ti; ti = ni) {
621 /* move the expands flag to the last one in the list */
623 SetupExpandsFlag(struct cmd_syndesc *as)
625 struct cmd_parmdesc *tp;
629 /* find last CMD_LIST type parameter, optional or not, and make it expandable
630 * if no other dude is expandable */
631 for (i = 0; i < CMD_MAXPARMS; i++) {
633 if (tp->type == CMD_LIST) {
634 if (tp->flags & CMD_EXPANDS)
635 return 0; /* done if already specified */
640 as->parms[last].flags |= CMD_EXPANDS;
644 /* Take the current argv & argc and alter them so that the initialization
645 * opcode is made to appear. This is used in cases where the initialization
646 * opcode is implicitly invoked.*/
648 InsertInitOpcode(int *aargc, char **aargv)
650 char **newargv; /*Ptr to new, expanded argv space */
651 char *pinitopcode; /*Ptr to space for name of init opcode */
652 int i; /*Loop counter */
654 /* Allocate the new argv array, plus one for the new opcode, plus one
655 * more for the trailing null pointer */
656 newargv = malloc(((*aargc) + 2) * sizeof(char *));
658 fprintf(stderr, "%s: Can't create new argv array with %d+2 slots\n",
663 /* Create space for the initial opcode & fill it in */
664 pinitopcode = malloc(sizeof(initcmd_opcode));
666 fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]);
670 strcpy(pinitopcode, initcmd_opcode);
672 /* Move all the items in the old argv into the new argv, in their
674 for (i = *aargc; i > 1; i--)
675 newargv[i] = aargv[i - 1];
677 /* Slip in the opcode and the trailing null pointer, and bump the
678 * argument count up by one for the new opcode */
679 newargv[0] = aargv[0];
680 newargv[1] = pinitopcode;
682 newargv[*aargc] = NULL;
684 /* Return the happy news */
687 } /*InsertInitOpcode */
690 NoParmsOK(struct cmd_syndesc *as)
693 struct cmd_parmdesc *td;
695 for (i = 0; i < CMD_MAXPARMS; i++) {
697 if (td->type != 0 && !(td->flags & CMD_OPTIONAL)) {
698 /* found a non-optional (e.g. required) parm, so NoParmsOK
699 * is false (some parms are required) */
706 /* Add help, apropos commands once */
710 struct cmd_syndesc *ts;
713 ts = cmd_CreateSyntax("help", HelpProc, NULL,
714 "get help on commands");
715 cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
716 cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, NULL);
718 ts = cmd_CreateSyntax("apropos", AproposProc, NULL,
719 "search by help text");
720 cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED, "help string");
721 ts = cmd_CreateSyntax("version", VersionProc, NULL,
723 ts = cmd_CreateSyntax("-version", VersionProc, NULL,
725 ts = cmd_CreateSyntax("-help", HelpProc, NULL,
727 ts = cmd_CreateSyntax("--version", VersionProc, NULL,
729 ts = cmd_CreateSyntax("--help", HelpProc, NULL,
734 /* Call the appropriate function, or return syntax error code. Note: if
735 * no opcode is specified, an initialization routine exists, and it has
736 * NOT been called before, we invoke the special initialization opcode
739 cmd_Parse(int argc, char **argv, struct cmd_syndesc **outsyntax)
742 struct cmd_syndesc *ts = NULL;
743 struct cmd_parmdesc *tparm;
749 static int initd = 0; /*Is this the first time this routine has been called? */
750 static int initcmdpossible = 1; /*Should be consider parsing the initial command? */
759 /*Remember the program name */
764 if (!NoParmsOK(allSyntax)) {
765 printf("%s: Type '%s -help' for help\n", pname, pname);
772 /* if there is an initcmd, don't print an error message, just
773 * setup to use the initcmd below. */
774 if (!(initcmdpossible && FindSyntax(initcmd_opcode, NULL))) {
775 printf("%s: Type '%s help' or '%s help <topic>' for help\n",
776 pname, pname, pname);
783 /* Find the syntax descriptor for this command, doing prefix matching properly */
787 ts = (argc < 2 ? 0 : FindSyntax(argv[1], &ambig));
789 /*First token doesn't match a syntax descriptor */
790 if (initcmdpossible) {
791 /*If initial command line handling hasn't been done yet,
792 * see if there is a descriptor for the initialization opcode.
793 * Only try this once. */
795 ts = FindSyntax(initcmd_opcode, NULL);
797 /*There is no initialization opcode available, so we declare
800 fprintf(stderr, "%s", NName(pname, ": "));
802 "Ambiguous operation '%s'; type '%shelp' for list\n",
803 argv[1], NName(pname, " "));
805 fprintf(stderr, "%s", NName(pname, ": "));
807 "Unrecognized operation '%s'; type '%shelp' for list\n",
808 argv[1], NName(pname, " "));
810 code = CMD_UNKNOWNCMD;
813 /*Found syntax structure for an initialization opcode. Fix
814 * up argv and argc to relect what the user
815 * ``should have'' typed */
816 if (!(argv = InsertInitOpcode(&argc, argv))) {
818 "%sCan't insert implicit init opcode into command line\n",
820 code = CMD_INTERNALERROR;
824 } /*Initial opcode not yet attempted */
826 /* init cmd already run and no syntax entry found */
828 fprintf(stderr, "%s", NName(pname, ": "));
830 "Ambiguous operation '%s'; type '%shelp' for list\n",
831 argv[1], NName(pname, " "));
833 fprintf(stderr, "%s", NName(pname, ": "));
835 "Unrecognized operation '%s'; type '%shelp' for list\n",
836 argv[1], NName(pname, " "));
838 code = CMD_UNKNOWNCMD;
841 } /*Argv[1] is not a valid opcode */
842 } /*Opcodes are defined */
844 /* Found the descriptor; start parsing. curType is the type we're
848 /* We start off parsing in "positional" mode, where tokens are put in
849 * slots positionally. If we find a name that takes args, we go
850 * out of positional mode, and from that point on, expect a switch
851 * before any particular token. */
853 positional = enablePositional; /* Accepting positional cmds ? */
854 i = noOpcodes ? 1 : 2;
855 SetupExpandsFlag(ts);
856 for (; i < argc; i++) {
857 /* Only tokens that start with a hyphen and are not followed by a digit
858 * are considered switches. This allow negative numbers. */
859 if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
861 j = FindType(ts, argv[i]);
864 "%sUnrecognized or ambiguous switch '%s'; type ",
865 NName(pname, ": "), argv[i]);
867 fprintf(stderr, "'%s -help' for detailed help\n",
870 fprintf(stderr, "'%shelp %s' for detailed help\n",
871 NName(argv[0], " "), ts->name);
872 code = CMD_UNKNOWNSWITCH;
875 if (j >= CMD_MAXPARMS) {
876 fprintf(stderr, "%sInternal parsing error\n",
878 code = CMD_INTERNALERROR;
881 if (ts->parms[j].type == CMD_FLAG) {
882 ts->parms[j].items = &dummy;
886 ts->parms[j].flags |= CMD_PROCESSED;
889 /* Try to fit in this descr */
890 if (curType >= CMD_MAXPARMS) {
891 fprintf(stderr, "%sToo many arguments\n", NName(pname, ": "));
895 tparm = &ts->parms[curType];
897 if ((tparm->type == 0) || /* No option in this slot */
898 (tparm->type == CMD_FLAG)) { /* A flag (not an argument */
899 /* skipped parm slot */
900 curType++; /* Skip this slot and reprocess this parm */
905 if (!(tparm->flags & CMD_PROCESSED) && (tparm->flags & CMD_HIDE)) {
906 curType++; /* Skip this slot and reprocess this parm */
911 if (tparm->type == CMD_SINGLE) {
913 fprintf(stderr, "%sToo many values after switch %s\n",
914 NName(pname, ": "), tparm->name);
918 AddItem(tparm, argv[i]); /* Add to end of list */
919 } else if (tparm->type == CMD_LIST) {
920 AddItem(tparm, argv[i]); /* Add to end of list */
922 /* Now, if we're in positional mode, advance to the next item */
924 curType = AdvanceType(ts, curType);
928 /* keep track of this for messages */
929 ts->a0name = argv[0];
931 /* If we make it here, all the parameters are filled in. Check to see if
932 * this is a -help version. Must do this before checking for all
933 * required parms, otherwise it is a real nuisance */
934 if (ts->parms[CMD_HELPPARM].items) {
936 /* Display full help syntax if we don't have subcommands */
943 /* Parsing done, see if we have all of our required parameters */
944 for (i = 0; i < CMD_MAXPARMS; i++) {
945 tparm = &ts->parms[i];
946 if (tparm->type == 0)
947 continue; /* Skipped parm slot */
948 if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
949 fprintf(stderr, "%s The field '%s' isn't completed properly\n",
950 NName(pname, ": "), tparm->name);
954 if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
955 fprintf(stderr, "%sMissing required parameter '%s'\n",
956 NName(pname, ": "), tparm->name);
960 tparm->flags &= ~CMD_PROCESSED;
965 if (code && ts != NULL)
972 cmd_Dispatch(int argc, char **argv)
974 struct cmd_syndesc *ts = NULL;
977 code = cmd_Parse(argc, argv, &ts);
982 * Before calling the beforeProc and afterProc and all the implications
983 * from those calls, check if the help procedure was called and call it
986 if ((ts->proc == HelpProc) || (ts->proc == AproposProc)) {
987 code = (*ts->proc) (ts, ts->rock);
991 /* Now, we just call the procedure and return */
993 code = (*beforeProc) (ts, beforeRock);
998 code = (*ts->proc) (ts, ts->rock);
1001 (*afterProc) (ts, afterRock);
1003 cmd_FreeOptions(&ts);
1008 cmd_FreeOptions(struct cmd_syndesc **ts)
1016 /* free token list returned by parseLine */
1018 FreeTokens(struct cmd_token *alist)
1020 struct cmd_token *nlist;
1021 for (; alist; alist = nlist) {
1022 nlist = alist->next;
1029 /* free an argv list returned by parseline */
1031 cmd_FreeArgv(char **argv)
1034 for (tp = *argv; tp; argv++, tp = *argv)
1039 /* copy back the arg list to the argv array, freeing the cmd_tokens as you go;
1040 * the actual data is still malloc'd, and will be freed when the caller calls
1041 * cmd_FreeArgv later on
1045 CopyBackArgs(struct cmd_token *alist, char **argv,
1046 afs_int32 * an, afs_int32 amaxn)
1048 struct cmd_token *next;
1054 *argv = (char *)malloc(strlen(INITSTR) + 1);
1056 strcpy(*argv, INITSTR);
1062 return CMD_TOOMANY; /* argv is too small for his many parms. */
1071 *(argv++) = 0; /* use last slot for terminating null */
1072 /* don't count terminating null */
1080 if (x == '"' || x == 39 /* single quote */ )
1089 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
1096 cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
1100 int inToken, inQuote;
1101 struct cmd_token *first, *last;
1102 struct cmd_token *ttok;
1105 inToken = 0; /* not copying token chars at start */
1108 inQuote = 0; /* not in a quoted string */
1111 if (tc == 0 || (!inQuote && space(tc))) { /* terminating null gets us in here, too */
1113 inToken = 0; /* end of this token */
1115 return -1; /* should never get here */
1118 ttok = malloc(sizeof(struct cmd_token));
1121 ttok->key = malloc(strlen(tbuffer) + 1);
1123 strcpy(ttok->key, tbuffer);
1133 /* an alpha character */
1138 if (tptr - tbuffer >= sizeof(tbuffer)) {
1140 return CMD_TOOBIG; /* token too long */
1143 /* hit a quote, toggle inQuote flag but don't insert character */
1146 /* insert character */
1151 /* last token flushed 'cause space(0) --> true */
1154 return CopyBackArgs(first, argv, an, amaxn);
1160 cmd_OptionAsInt(struct cmd_syndesc *syn, int pos, int *value)
1162 if (pos > syn->nParms)
1163 return CMD_EXCESSPARMS;
1164 if (syn->parms[pos].items == NULL ||
1165 syn->parms[pos].items->data == NULL)
1167 *value = strtol(syn->parms[pos].items->data, NULL, 10);
1173 cmd_OptionAsString(struct cmd_syndesc *syn, int pos, char **value)
1175 if (pos > syn->nParms)
1176 return CMD_EXCESSPARMS;
1177 if (syn->parms[pos].items == NULL || syn->parms[pos].items->data == NULL)
1182 *value = strdup(syn->parms[pos].items->data);
1188 cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **value)
1190 if (pos > syn->nParms)
1191 return CMD_EXCESSPARMS;
1192 if (syn->parms[pos].items == NULL)
1195 *value = syn->parms[pos].items;
1200 cmd_OptionAsFlag(struct cmd_syndesc *syn, int pos, int *value)
1202 if (pos > syn->nParms)
1203 return CMD_EXCESSPARMS;
1204 if (syn->parms[pos].items == NULL)
1212 cmd_OptionPresent(struct cmd_syndesc *syn, int pos)
1214 if (pos > syn->nParms || syn->parms[pos].items == NULL)