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, or one which cannot be abbreviated,
105 * must be a full match (no best matches) */
106 if (as->parms[i].flags & CMD_HIDE ||
107 as->parms[i].flags & CMD_NOABBRV ||
111 if (strncmp(as->parms[i].name, aname, cmdlen) == 0) {
118 return (ambig ? -1 : best);
121 static struct cmd_syndesc *
122 FindSyntax(char *aname, int *aambig)
124 struct cmd_syndesc *ts;
125 struct cmd_syndesc *best;
129 cmdLen = strlen(aname);
130 best = (struct cmd_syndesc *)0;
133 *aambig = 0; /* initialize to unambiguous */
134 for (ts = allSyntax; ts; ts = ts->next) {
135 if (strcmp(aname, ts->name) == 0)
137 if (strlen(ts->name) < cmdLen)
138 continue; /* we typed more than item has */
139 /* A hidden command must be a full match (no best matches) */
140 if (ts->flags & CMD_HIDDEN)
143 /* This is just an alias for *best, or *best is just an alias for us.
144 * If we don't make this check explicitly, then an alias which is just a
145 * short prefix of the real command's name might make things ambiguous
146 * for no apparent reason.
148 if (best && ts->aliasOf == best->aliasOf)
150 if (strncmp(ts->name, aname, cmdLen) == 0) {
152 ambig = 1; /* ambiguous name */
159 *aambig = ambig; /* if ambiguous and they care, tell them */
160 return (struct cmd_syndesc *)0; /* fails */
162 return best; /* otherwise its not ambiguous, and they know */
165 /* print the help for a single parameter */
167 ParmHelpString(struct cmd_parmdesc *aparm)
170 if (aparm->type == CMD_FLAG) {
173 asprintf(&str, " %s<%s>%s%s",
174 aparm->type == CMD_SINGLE_OR_FLAG?"[":"",
175 aparm->help?aparm->help:"arg",
176 aparm->type == CMD_LIST?"+":"",
177 aparm->type == CMD_SINGLE_OR_FLAG?"]":"");
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;
201 /* now print usage, from syntax table */
203 asprintf(&str, "Usage: %s", as->a0name);
205 if (!strcmp(as->name, initcmd_opcode))
206 asprintf(&str, "Usage: %s[%s]", NName(as->a0name, " "), as->name);
208 asprintf(&str, "Usage: %s%s", NName(as->a0name, " "), as->name);
215 for (i = 0; i < CMD_MAXPARMS; i++) {
218 continue; /* seeked over slot */
219 if (tp->flags & CMD_HIDE)
220 continue; /* skip hidden options */
222 /* The parameter name is the real name, plus any aliases */
224 name = strdup(tp->name);
227 struct cmd_item *alias;
228 namelen = strlen(tp->name) + 1;
229 for (alias = tp->aliases; alias != NULL; alias = alias->next)
230 namelen+=strlen(alias->data) + 3;
232 name = malloc(namelen);
233 strlcpy(name, tp->name, namelen);
235 for (alias = tp->aliases; alias != NULL; alias = alias->next) {
236 strlcat(name, " | ", namelen);
237 strlcat(name, alias->data, namelen);
241 /* Work out if we can fit what we want to on this line, or if we need to
243 str = ParmHelpString(tp);
244 xtralen = 1 + strlen(name) + strlen(str) +
245 ((tp->flags & CMD_OPTIONAL)? 2: 0);
247 if (len + xtralen > 78) {
253 tp->flags & CMD_OPTIONAL?"[":"",
256 tp->flags & CMD_OPTIONAL?"]":"");
263 /* must print newline in any case, to terminate preceding line */
265 PrintAliases(struct cmd_syndesc *as)
267 struct cmd_syndesc *ts;
269 if (as->flags & CMD_ALIAS) {
271 printf("(alias for %s)\n", ts->name);
275 return; /* none, print nothing */
277 for (as = as->nextAlias; as; as = as->nextAlias) {
278 printf("%s ", as->name);
285 PrintFlagHelp(struct cmd_syndesc *as)
288 struct cmd_parmdesc *tp;
292 /* find flag name length */
294 for (i = 0; i < CMD_MAXPARMS; i++) {
295 if (i == CMD_HELPPARM)
298 if (tp->type != CMD_FLAG)
300 if (tp->flags & CMD_HIDE)
301 continue; /* skip hidden options */
305 if (strlen(tp->name) > flag_width)
306 flag_width = strlen(tp->name);
309 /* print flag help */
310 flag_prefix = "Where:";
311 for (i = 0; i < CMD_MAXPARMS; i++) {
312 if (i == CMD_HELPPARM)
315 if (tp->type != CMD_FLAG)
317 if (tp->flags & CMD_HIDE)
318 continue; /* skip hidden options */
322 printf("%-7s%-*s %s\n", flag_prefix, flag_width, tp->name, tp->help);
328 AproposProc(struct cmd_syndesc *as, void *arock)
330 struct cmd_syndesc *ts;
335 tsub = as->parms[0].items->data;
336 for (ts = allSyntax; ts; ts = ts->next) {
337 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
339 if (SubString(ts->help, tsub)) {
340 printf("%s: %s\n", ts->name, ts->help);
342 } else if (SubString(ts->name, tsub)) {
343 printf("%s: %s\n", ts->name, ts->help);
348 printf("Sorry, no commands found\n");
353 HelpProc(struct cmd_syndesc *as, void *arock)
355 struct cmd_syndesc *ts;
360 if (as->parms[0].items == 0) {
361 printf("%sCommands are:\n", NName(as->a0name, ": "));
362 for (ts = allSyntax; ts; ts = ts->next) {
363 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
365 printf("%-15s %s\n", ts->name, (ts->help ? ts->help : ""));
368 /* print out individual help topics */
369 for (ti = as->parms[0].items; ti; ti = ti->next) {
371 ts = FindSyntax(ti->data, &ambig);
372 if (ts && (ts->flags & CMD_HIDDEN))
373 ts = 0; /* no hidden commands */
375 /* print out command name and help */
376 printf("%s%s: %s ", NName(as->a0name, " "), ts->name,
377 (ts->help ? ts->help : ""));
378 ts->a0name = as->a0name;
384 fprintf(stderr, "%sUnknown topic '%s'\n",
385 NName(as->a0name, ": "), ti->data);
387 /* ambiguous, list 'em all */
389 "%sAmbiguous topic '%s'; use 'apropos' to list\n",
390 NName(as->a0name, ": "), ti->data);
392 code = CMD_UNKNOWNCMD;
400 cmd_SetBeforeProc(int (*aproc) (struct cmd_syndesc * ts, void *beforeRock),
409 cmd_SetAfterProc(int (*aproc) (struct cmd_syndesc * ts, void *afterRock),
417 /* thread on list in alphabetical order */
419 SortSyntax(struct cmd_syndesc *as)
421 struct cmd_syndesc **ld, *ud;
423 for (ld = &allSyntax, ud = *ld; ud; ld = &ud->next, ud = *ld) {
424 if (strcmp(ud->name, as->name) > 0) { /* next guy is bigger than us */
428 /* thread us on the list now */
435 cmd_CreateSyntax(char *aname,
436 int (*aproc) (struct cmd_syndesc * ts, void *arock),
437 void *arock, char *ahelp)
439 struct cmd_syndesc *td;
441 /* can't have two cmds in no opcode mode */
445 td = calloc(1, sizeof(struct cmd_syndesc));
447 td->aliasOf = td; /* treat aliasOf as pointer to real command, no matter what */
449 /* copy in name, etc */
451 td->name = malloc(strlen(aname) + 1);
453 strcpy(td->name, aname);
459 /* Piggy-back the hidden option onto the help string */
460 if (ahelp == (char *)CMD_HIDDEN) {
461 td->flags |= CMD_HIDDEN;
463 td->help = malloc(strlen(ahelp) + 1);
465 strcpy(td->help, ahelp);
474 cmd_Seek(td, CMD_HELPPARM);
475 cmd_AddParm(td, "-help", CMD_FLAG, CMD_OPTIONAL, "get detailed help");
482 cmd_CreateAlias(struct cmd_syndesc *as, char *aname)
484 struct cmd_syndesc *td;
486 td = malloc(sizeof(struct cmd_syndesc));
488 memcpy(td, as, sizeof(struct cmd_syndesc));
489 td->name = malloc(strlen(aname) + 1);
491 strcpy(td->name, aname);
492 td->flags |= CMD_ALIAS;
493 /* if ever free things, make copy of help string, too */
498 /* thread on alias lists */
499 td->nextAlias = as->nextAlias;
503 return 0; /* all done */
507 cmd_DisablePositionalCommands(void)
509 enablePositional = 0;
513 cmd_DisableAbbreviations(void)
515 enableAbbreviation = 0;
519 cmd_IsAdministratorCommand(struct cmd_syndesc *as)
521 as->flags |= CMD_ADMIN;
526 cmd_Seek(struct cmd_syndesc *as, int apos)
528 if (apos >= CMD_MAXPARMS)
529 return CMD_EXCESSPARMS;
535 cmd_AddParmAtOffset(struct cmd_syndesc *as, int ref, char *aname, int atype,
536 afs_int32 aflags, char *ahelp)
538 struct cmd_parmdesc *tp;
540 if (ref >= CMD_MAXPARMS)
541 return CMD_EXCESSPARMS;
542 tp = &as->parms[ref];
544 tp->name = malloc(strlen(aname) + 1);
546 strcpy(tp->name, aname);
551 tp->help = malloc(strlen(ahelp) + 1);
553 strcpy(tp->help, ahelp);
559 if (as->nParms <= ref)
566 cmd_AddParm(struct cmd_syndesc *as, char *aname, int atype,
567 afs_int32 aflags, char *ahelp)
569 if (as->nParms >= CMD_MAXPARMS)
570 return CMD_EXCESSPARMS;
572 return cmd_AddParmAtOffset(as, as->nParms++, aname, atype, aflags, ahelp);
576 cmd_AddParmAlias(struct cmd_syndesc *as, int pos, char *alias)
578 struct cmd_item *item;
580 if (pos > as->nParms)
581 return CMD_EXCESSPARMS;
583 item = calloc(1, sizeof(struct cmd_item));
584 item->data = strdup(alias);
585 item->next = as->parms[pos].aliases;
586 as->parms[pos].aliases = item;
591 /* add a text item to the end of the parameter list */
593 AddItem(struct cmd_parmdesc *aparm, char *aval, char *pname)
595 struct cmd_item *ti, *ni;
597 if (aparm->type == CMD_SINGLE ||
598 aparm->type == CMD_SINGLE_OR_FLAG) {
600 fprintf(stderr, "%sToo many values after switch %s\n",
601 NName(pname, ": "), aparm->name);
606 ti = calloc(1, sizeof(struct cmd_item));
608 ti->data = malloc(strlen(aval) + 1);
610 strcpy(ti->data, aval);
611 /* now put ti at the *end* of the list */
612 if ((ni = aparm->items)) {
613 for (; ni; ni = ni->next)
615 break; /* skip to last one */
618 aparm->items = ti; /* we're first */
622 /* skip to next non-flag item, if any */
624 AdvanceType(struct cmd_syndesc *as, afs_int32 aval)
627 struct cmd_parmdesc *tp;
629 /* first see if we should try to grab rest of line for this dude */
630 if (as->parms[aval].flags & CMD_EXPANDS)
633 /* if not, find next non-flag used slot */
634 for (next = aval + 1; next < CMD_MAXPARMS; next++) {
635 tp = &as->parms[next];
636 if (tp->type != 0 && tp->type != CMD_FLAG)
642 /* discard parameters filled in by dispatch */
644 ResetSyntax(struct cmd_syndesc *as)
647 struct cmd_parmdesc *tp;
648 struct cmd_item *ti, *ni;
651 for (i = 0; i < CMD_MAXPARMS; i++, tp++) {
653 case CMD_SINGLE_OR_FLAG:
654 if (tp->items == &dummy)
656 /* Deliberately fall through here */
659 /* free whole list in both cases, just for fun */
660 for (ti = tp->items; ti; ti = ni) {
674 /* move the expands flag to the last one in the list */
676 SetupExpandsFlag(struct cmd_syndesc *as)
678 struct cmd_parmdesc *tp;
682 /* find last CMD_LIST type parameter, optional or not, and make it expandable
683 * if no other dude is expandable */
684 for (i = 0; i < CMD_MAXPARMS; i++) {
686 if (tp->type == CMD_LIST) {
687 if (tp->flags & CMD_EXPANDS)
688 return 0; /* done if already specified */
693 as->parms[last].flags |= CMD_EXPANDS;
697 /* Take the current argv & argc and alter them so that the initialization
698 * opcode is made to appear. This is used in cases where the initialization
699 * opcode is implicitly invoked.*/
701 InsertInitOpcode(int *aargc, char **aargv)
703 char **newargv; /*Ptr to new, expanded argv space */
704 char *pinitopcode; /*Ptr to space for name of init opcode */
705 int i; /*Loop counter */
707 /* Allocate the new argv array, plus one for the new opcode, plus one
708 * more for the trailing null pointer */
709 newargv = malloc(((*aargc) + 2) * sizeof(char *));
711 fprintf(stderr, "%s: Can't create new argv array with %d+2 slots\n",
716 /* Create space for the initial opcode & fill it in */
717 pinitopcode = malloc(sizeof(initcmd_opcode));
719 fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]);
723 strcpy(pinitopcode, initcmd_opcode);
725 /* Move all the items in the old argv into the new argv, in their
727 for (i = *aargc; i > 1; i--)
728 newargv[i] = aargv[i - 1];
730 /* Slip in the opcode and the trailing null pointer, and bump the
731 * argument count up by one for the new opcode */
732 newargv[0] = aargv[0];
733 newargv[1] = pinitopcode;
735 newargv[*aargc] = NULL;
737 /* Return the happy news */
740 } /*InsertInitOpcode */
743 NoParmsOK(struct cmd_syndesc *as)
746 struct cmd_parmdesc *td;
748 for (i = 0; i < CMD_MAXPARMS; i++) {
750 if (td->type != 0 && !(td->flags & CMD_OPTIONAL)) {
751 /* found a non-optional (e.g. required) parm, so NoParmsOK
752 * is false (some parms are required) */
759 /* Add help, apropos commands once */
763 struct cmd_syndesc *ts;
766 ts = cmd_CreateSyntax("help", HelpProc, NULL,
767 "get help on commands");
768 cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
769 cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, NULL);
771 ts = cmd_CreateSyntax("apropos", AproposProc, NULL,
772 "search by help text");
773 cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED, "help string");
774 ts = cmd_CreateSyntax("version", VersionProc, NULL,
776 ts = cmd_CreateSyntax("-version", VersionProc, NULL,
778 ts = cmd_CreateSyntax("-help", HelpProc, NULL,
780 ts = cmd_CreateSyntax("--version", VersionProc, NULL,
782 ts = cmd_CreateSyntax("--help", HelpProc, NULL,
787 /* Call the appropriate function, or return syntax error code. Note: if
788 * no opcode is specified, an initialization routine exists, and it has
789 * NOT been called before, we invoke the special initialization opcode
792 cmd_Parse(int argc, char **argv, struct cmd_syndesc **outsyntax)
795 struct cmd_syndesc *ts = NULL;
796 struct cmd_parmdesc *tparm;
803 char *embeddedvalue = NULL;
804 static int initd = 0; /*Is this the first time this routine has been called? */
805 static int initcmdpossible = 1; /*Should be consider parsing the initial command? */
814 /*Remember the program name */
819 if (!NoParmsOK(allSyntax)) {
820 printf("%s: Type '%s -help' for help\n", pname, pname);
827 /* if there is an initcmd, don't print an error message, just
828 * setup to use the initcmd below. */
829 if (!(initcmdpossible && FindSyntax(initcmd_opcode, NULL))) {
830 printf("%s: Type '%s help' or '%s help <topic>' for help\n",
831 pname, pname, pname);
838 /* Find the syntax descriptor for this command, doing prefix matching properly */
842 ts = (argc < 2 ? 0 : FindSyntax(argv[1], &ambig));
844 /*First token doesn't match a syntax descriptor */
845 if (initcmdpossible) {
846 /*If initial command line handling hasn't been done yet,
847 * see if there is a descriptor for the initialization opcode.
848 * Only try this once. */
850 ts = FindSyntax(initcmd_opcode, NULL);
852 /*There is no initialization opcode available, so we declare
855 fprintf(stderr, "%s", NName(pname, ": "));
857 "Ambiguous operation '%s'; type '%shelp' for list\n",
858 argv[1], NName(pname, " "));
860 fprintf(stderr, "%s", NName(pname, ": "));
862 "Unrecognized operation '%s'; type '%shelp' for list\n",
863 argv[1], NName(pname, " "));
865 code = CMD_UNKNOWNCMD;
868 /*Found syntax structure for an initialization opcode. Fix
869 * up argv and argc to relect what the user
870 * ``should have'' typed */
871 if (!(argv = InsertInitOpcode(&argc, argv))) {
873 "%sCan't insert implicit init opcode into command line\n",
875 code = CMD_INTERNALERROR;
879 } /*Initial opcode not yet attempted */
881 /* init cmd already run and no syntax entry found */
883 fprintf(stderr, "%s", NName(pname, ": "));
885 "Ambiguous operation '%s'; type '%shelp' for list\n",
886 argv[1], NName(pname, " "));
888 fprintf(stderr, "%s", NName(pname, ": "));
890 "Unrecognized operation '%s'; type '%shelp' for list\n",
891 argv[1], NName(pname, " "));
893 code = CMD_UNKNOWNCMD;
896 } /*Argv[1] is not a valid opcode */
897 } /*Opcodes are defined */
899 /* Found the descriptor; start parsing. curType is the type we're
903 /* We start off parsing in "positional" mode, where tokens are put in
904 * slots positionally. If we find a name that takes args, we go
905 * out of positional mode, and from that point on, expect a switch
906 * before any particular token. */
908 positional = enablePositional; /* Accepting positional cmds ? */
909 i = noOpcodes ? 1 : 2;
910 SetupExpandsFlag(ts);
911 for (; i < argc; i++) {
915 embeddedvalue = NULL;
918 /* Only tokens that start with a hyphen and are not followed by a digit
919 * are considered switches. This allow negative numbers. */
921 if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
925 if (strrchr(argv[i], '=') != NULL) {
926 param = strdup(argv[i]);
927 embeddedvalue = strrchr(param, '=');
928 *embeddedvalue = '\0';
930 j = FindType(ts, param);
932 j = FindType(ts, argv[i]);
937 "%sUnrecognized or ambiguous switch '%s'; type ",
938 NName(pname, ": "), argv[i]);
940 fprintf(stderr, "'%s -help' for detailed help\n",
943 fprintf(stderr, "'%shelp %s' for detailed help\n",
944 NName(argv[0], " "), ts->name);
945 code = CMD_UNKNOWNSWITCH;
948 if (j >= CMD_MAXPARMS) {
949 fprintf(stderr, "%sInternal parsing error\n",
951 code = CMD_INTERNALERROR;
954 if (ts->parms[j].type == CMD_FLAG) {
955 ts->parms[j].items = &dummy;
958 fprintf(stderr, "%sSwitch '%s' doesn't take an argument\n",
959 NName(pname, ": "), ts->parms[j].name);
966 ts->parms[j].flags |= CMD_PROCESSED;
969 AddItem(&ts->parms[curType], embeddedvalue, pname);
973 /* Try to fit in this descr */
974 if (curType >= CMD_MAXPARMS) {
975 fprintf(stderr, "%sToo many arguments\n", NName(pname, ": "));
979 tparm = &ts->parms[curType];
981 if ((tparm->type == 0) || /* No option in this slot */
982 (tparm->type == CMD_FLAG)) { /* A flag (not an argument */
983 /* skipped parm slot */
984 curType++; /* Skip this slot and reprocess this parm */
989 if (!(tparm->flags & CMD_PROCESSED) && (tparm->flags & CMD_HIDE)) {
990 curType++; /* Skip this slot and reprocess this parm */
995 if (tparm->type == CMD_SINGLE ||
996 tparm->type == CMD_SINGLE_OR_FLAG) {
998 fprintf(stderr, "%sToo many values after switch %s\n",
999 NName(pname, ": "), tparm->name);
1003 AddItem(tparm, argv[i], pname); /* Add to end of list */
1004 } else if (tparm->type == CMD_LIST) {
1005 AddItem(tparm, argv[i], pname); /* Add to end of list */
1008 /* Now, if we're in positional mode, advance to the next item */
1010 curType = AdvanceType(ts, curType);
1014 /* keep track of this for messages */
1015 ts->a0name = argv[0];
1017 /* If we make it here, all the parameters are filled in. Check to see if
1018 * this is a -help version. Must do this before checking for all
1019 * required parms, otherwise it is a real nuisance */
1020 if (ts->parms[CMD_HELPPARM].items) {
1022 /* Display full help syntax if we don't have subcommands */
1029 /* Parsing done, see if we have all of our required parameters */
1030 for (i = 0; i < CMD_MAXPARMS; i++) {
1031 tparm = &ts->parms[i];
1032 if (tparm->type == 0)
1033 continue; /* Skipped parm slot */
1034 if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
1035 if (tparm->type == CMD_SINGLE_OR_FLAG) {
1036 tparm->items = &dummy;
1038 fprintf(stderr, "%s The field '%s' isn't completed properly\n",
1039 NName(pname, ": "), tparm->name);
1044 if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
1045 fprintf(stderr, "%sMissing required parameter '%s'\n",
1046 NName(pname, ": "), tparm->name);
1050 tparm->flags &= ~CMD_PROCESSED;
1055 if (code && ts != NULL)
1062 cmd_Dispatch(int argc, char **argv)
1064 struct cmd_syndesc *ts = NULL;
1067 code = cmd_Parse(argc, argv, &ts);
1072 * Before calling the beforeProc and afterProc and all the implications
1073 * from those calls, check if the help procedure was called and call it
1076 if ((ts->proc == HelpProc) || (ts->proc == AproposProc)) {
1077 code = (*ts->proc) (ts, ts->rock);
1081 /* Now, we just call the procedure and return */
1083 code = (*beforeProc) (ts, beforeRock);
1088 code = (*ts->proc) (ts, ts->rock);
1091 (*afterProc) (ts, afterRock);
1093 cmd_FreeOptions(&ts);
1098 cmd_FreeOptions(struct cmd_syndesc **ts)
1106 /* free token list returned by parseLine */
1108 FreeTokens(struct cmd_token *alist)
1110 struct cmd_token *nlist;
1111 for (; alist; alist = nlist) {
1112 nlist = alist->next;
1119 /* free an argv list returned by parseline */
1121 cmd_FreeArgv(char **argv)
1124 for (tp = *argv; tp; argv++, tp = *argv)
1129 /* copy back the arg list to the argv array, freeing the cmd_tokens as you go;
1130 * the actual data is still malloc'd, and will be freed when the caller calls
1131 * cmd_FreeArgv later on
1135 CopyBackArgs(struct cmd_token *alist, char **argv,
1136 afs_int32 * an, afs_int32 amaxn)
1138 struct cmd_token *next;
1144 *argv = (char *)malloc(strlen(INITSTR) + 1);
1146 strcpy(*argv, INITSTR);
1152 return CMD_TOOMANY; /* argv is too small for his many parms. */
1161 *(argv++) = 0; /* use last slot for terminating null */
1162 /* don't count terminating null */
1170 if (x == '"' || x == 39 /* single quote */ )
1179 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
1186 cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
1190 int inToken, inQuote;
1191 struct cmd_token *first, *last;
1192 struct cmd_token *ttok;
1195 inToken = 0; /* not copying token chars at start */
1198 inQuote = 0; /* not in a quoted string */
1201 if (tc == 0 || (!inQuote && space(tc))) { /* terminating null gets us in here, too */
1203 inToken = 0; /* end of this token */
1205 return -1; /* should never get here */
1208 ttok = malloc(sizeof(struct cmd_token));
1211 ttok->key = malloc(strlen(tbuffer) + 1);
1213 strcpy(ttok->key, tbuffer);
1223 /* an alpha character */
1228 if (tptr - tbuffer >= sizeof(tbuffer)) {
1230 return CMD_TOOBIG; /* token too long */
1233 /* hit a quote, toggle inQuote flag but don't insert character */
1236 /* insert character */
1241 /* last token flushed 'cause space(0) --> true */
1244 return CopyBackArgs(first, argv, an, amaxn);
1250 cmd_OptionAsInt(struct cmd_syndesc *syn, int pos, int *value)
1252 if (pos > syn->nParms)
1253 return CMD_EXCESSPARMS;
1254 if (syn->parms[pos].items == NULL ||
1255 syn->parms[pos].items->data == NULL)
1257 if (syn->parms[pos].items == &dummy)
1260 *value = strtol(syn->parms[pos].items->data, NULL, 10);
1266 cmd_OptionAsUint(struct cmd_syndesc *syn, int pos,
1267 unsigned int *value)
1269 if (pos > syn->nParms)
1270 return CMD_EXCESSPARMS;
1271 if (syn->parms[pos].items == NULL ||
1272 syn->parms[pos].items->data == NULL)
1274 if (syn->parms[pos].items == &dummy)
1277 *value = strtoul(syn->parms[pos].items->data, NULL, 10);
1283 cmd_OptionAsString(struct cmd_syndesc *syn, int pos, char **value)
1285 if (pos > syn->nParms)
1286 return CMD_EXCESSPARMS;
1287 if (syn->parms[pos].items == NULL || syn->parms[pos].items->data == NULL)
1289 if (syn->parms[pos].items == &dummy)
1295 *value = strdup(syn->parms[pos].items->data);
1301 cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **value)
1303 if (pos > syn->nParms)
1304 return CMD_EXCESSPARMS;
1305 if (syn->parms[pos].items == NULL)
1308 *value = syn->parms[pos].items;
1313 cmd_OptionAsFlag(struct cmd_syndesc *syn, int pos, int *value)
1315 if (pos > syn->nParms)
1316 return CMD_EXCESSPARMS;
1317 if (syn->parms[pos].items == NULL)
1325 cmd_OptionPresent(struct cmd_syndesc *syn, int pos)
1327 if (pos > syn->nParms || syn->parms[pos].items == NULL)