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 ParmHelpString(struct cmd_parmdesc *aparm)
167 if (aparm->type == CMD_FLAG) {
170 asprintf(&str, " %s<%s>%s%s",
171 aparm->type == CMD_SINGLE_OR_FLAG?"[":"",
172 aparm->help?aparm->help:"arg",
173 aparm->type == CMD_LIST?"+":"",
174 aparm->type == CMD_SINGLE_OR_FLAG?"]":"");
179 extern char *AFSVersion;
182 VersionProc(struct cmd_syndesc *as, void *arock)
184 printf("%s\n", AFSVersion);
189 PrintSyntax(struct cmd_syndesc *as)
192 struct cmd_parmdesc *tp;
198 /* now print usage, from syntax table */
200 asprintf(&str, "Usage: %s", as->a0name);
202 if (!strcmp(as->name, initcmd_opcode))
203 asprintf(&str, "Usage: %s[%s]", NName(as->a0name, " "), as->name);
205 asprintf(&str, "Usage: %s%s", NName(as->a0name, " "), as->name);
212 for (i = 0; i < CMD_MAXPARMS; i++) {
215 continue; /* seeked over slot */
216 if (tp->flags & CMD_HIDE)
217 continue; /* skip hidden options */
219 /* The parameter name is the real name, plus any aliases */
221 name = strdup(tp->name);
224 struct cmd_item *alias;
225 namelen = strlen(tp->name) + 1;
226 for (alias = tp->aliases; alias != NULL; alias = alias->next)
227 namelen+=strlen(alias->data) + 3;
229 name = malloc(namelen);
230 strlcpy(name, tp->name, namelen);
232 for (alias = tp->aliases; alias != NULL; alias = alias->next) {
233 strlcat(name, " | ", namelen);
234 strlcat(name, alias->data, namelen);
238 /* Work out if we can fit what we want to on this line, or if we need to
240 str = ParmHelpString(tp);
241 xtralen = 1 + strlen(name) + strlen(str) +
242 ((tp->flags & CMD_OPTIONAL)? 2: 0);
244 if (len + xtralen > 78) {
250 tp->flags & CMD_OPTIONAL?"[":"",
253 tp->flags & CMD_OPTIONAL?"]":"");
260 /* must print newline in any case, to terminate preceding line */
262 PrintAliases(struct cmd_syndesc *as)
264 struct cmd_syndesc *ts;
266 if (as->flags & CMD_ALIAS) {
268 printf("(alias for %s)\n", ts->name);
272 return; /* none, print nothing */
274 for (as = as->nextAlias; as; as = as->nextAlias) {
275 printf("%s ", as->name);
282 PrintFlagHelp(struct cmd_syndesc *as)
285 struct cmd_parmdesc *tp;
289 /* find flag name length */
291 for (i = 0; i < CMD_MAXPARMS; i++) {
292 if (i == CMD_HELPPARM)
295 if (tp->type != CMD_FLAG)
297 if (tp->flags & CMD_HIDE)
298 continue; /* skip hidden options */
302 if (strlen(tp->name) > flag_width)
303 flag_width = strlen(tp->name);
306 /* print flag help */
307 flag_prefix = "Where:";
308 for (i = 0; i < CMD_MAXPARMS; i++) {
309 if (i == CMD_HELPPARM)
312 if (tp->type != CMD_FLAG)
314 if (tp->flags & CMD_HIDE)
315 continue; /* skip hidden options */
319 printf("%-7s%-*s %s\n", flag_prefix, flag_width, tp->name, tp->help);
325 AproposProc(struct cmd_syndesc *as, void *arock)
327 struct cmd_syndesc *ts;
332 tsub = as->parms[0].items->data;
333 for (ts = allSyntax; ts; ts = ts->next) {
334 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
336 if (SubString(ts->help, tsub)) {
337 printf("%s: %s\n", ts->name, ts->help);
339 } else if (SubString(ts->name, tsub)) {
340 printf("%s: %s\n", ts->name, ts->help);
345 printf("Sorry, no commands found\n");
350 HelpProc(struct cmd_syndesc *as, void *arock)
352 struct cmd_syndesc *ts;
357 if (as->parms[0].items == 0) {
358 printf("%sCommands are:\n", NName(as->a0name, ": "));
359 for (ts = allSyntax; ts; ts = ts->next) {
360 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
362 printf("%-15s %s\n", ts->name, (ts->help ? ts->help : ""));
365 /* print out individual help topics */
366 for (ti = as->parms[0].items; ti; ti = ti->next) {
368 ts = FindSyntax(ti->data, &ambig);
369 if (ts && (ts->flags & CMD_HIDDEN))
370 ts = 0; /* no hidden commands */
372 /* print out command name and help */
373 printf("%s%s: %s ", NName(as->a0name, " "), ts->name,
374 (ts->help ? ts->help : ""));
375 ts->a0name = as->a0name;
381 fprintf(stderr, "%sUnknown topic '%s'\n",
382 NName(as->a0name, ": "), ti->data);
384 /* ambiguous, list 'em all */
386 "%sAmbiguous topic '%s'; use 'apropos' to list\n",
387 NName(as->a0name, ": "), ti->data);
389 code = CMD_UNKNOWNCMD;
397 cmd_SetBeforeProc(int (*aproc) (struct cmd_syndesc * ts, void *beforeRock),
406 cmd_SetAfterProc(int (*aproc) (struct cmd_syndesc * ts, void *afterRock),
414 /* thread on list in alphabetical order */
416 SortSyntax(struct cmd_syndesc *as)
418 struct cmd_syndesc **ld, *ud;
420 for (ld = &allSyntax, ud = *ld; ud; ld = &ud->next, ud = *ld) {
421 if (strcmp(ud->name, as->name) > 0) { /* next guy is bigger than us */
425 /* thread us on the list now */
432 cmd_CreateSyntax(char *aname,
433 int (*aproc) (struct cmd_syndesc * ts, void *arock),
434 void *arock, char *ahelp)
436 struct cmd_syndesc *td;
438 /* can't have two cmds in no opcode mode */
442 td = calloc(1, sizeof(struct cmd_syndesc));
444 td->aliasOf = td; /* treat aliasOf as pointer to real command, no matter what */
446 /* copy in name, etc */
448 td->name = malloc(strlen(aname) + 1);
450 strcpy(td->name, aname);
456 /* Piggy-back the hidden option onto the help string */
457 if (ahelp == (char *)CMD_HIDDEN) {
458 td->flags |= CMD_HIDDEN;
460 td->help = malloc(strlen(ahelp) + 1);
462 strcpy(td->help, ahelp);
471 cmd_Seek(td, CMD_HELPPARM);
472 cmd_AddParm(td, "-help", CMD_FLAG, CMD_OPTIONAL, "get detailed help");
479 cmd_CreateAlias(struct cmd_syndesc *as, char *aname)
481 struct cmd_syndesc *td;
483 td = malloc(sizeof(struct cmd_syndesc));
485 memcpy(td, as, sizeof(struct cmd_syndesc));
486 td->name = malloc(strlen(aname) + 1);
488 strcpy(td->name, aname);
489 td->flags |= CMD_ALIAS;
490 /* if ever free things, make copy of help string, too */
495 /* thread on alias lists */
496 td->nextAlias = as->nextAlias;
500 return 0; /* all done */
504 cmd_DisablePositionalCommands(void)
506 enablePositional = 0;
510 cmd_DisableAbbreviations(void)
512 enableAbbreviation = 0;
516 cmd_IsAdministratorCommand(struct cmd_syndesc *as)
518 as->flags |= CMD_ADMIN;
523 cmd_Seek(struct cmd_syndesc *as, int apos)
525 if (apos >= CMD_MAXPARMS)
526 return CMD_EXCESSPARMS;
532 cmd_AddParmAtOffset(struct cmd_syndesc *as, int ref, char *aname, int atype,
533 afs_int32 aflags, char *ahelp)
535 struct cmd_parmdesc *tp;
537 if (ref >= CMD_MAXPARMS)
538 return CMD_EXCESSPARMS;
539 tp = &as->parms[ref];
541 tp->name = malloc(strlen(aname) + 1);
543 strcpy(tp->name, aname);
548 tp->help = malloc(strlen(ahelp) + 1);
550 strcpy(tp->help, ahelp);
556 if (as->nParms <= ref)
563 cmd_AddParm(struct cmd_syndesc *as, char *aname, int atype,
564 afs_int32 aflags, char *ahelp)
566 if (as->nParms >= CMD_MAXPARMS)
567 return CMD_EXCESSPARMS;
569 return cmd_AddParmAtOffset(as, as->nParms++, aname, atype, aflags, ahelp);
573 cmd_AddParmAlias(struct cmd_syndesc *as, int pos, char *alias)
575 struct cmd_item *item;
577 if (pos > as->nParms)
578 return CMD_EXCESSPARMS;
580 item = calloc(1, sizeof(struct cmd_item));
581 item->data = strdup(alias);
582 item->next = as->parms[pos].aliases;
583 as->parms[pos].aliases = item;
588 /* add a text item to the end of the parameter list */
590 AddItem(struct cmd_parmdesc *aparm, char *aval, char *pname)
592 struct cmd_item *ti, *ni;
594 if (aparm->type == CMD_SINGLE ||
595 aparm->type == CMD_SINGLE_OR_FLAG) {
597 fprintf(stderr, "%sToo many values after switch %s\n",
598 NName(pname, ": "), aparm->name);
603 ti = calloc(1, sizeof(struct cmd_item));
605 ti->data = malloc(strlen(aval) + 1);
607 strcpy(ti->data, aval);
608 /* now put ti at the *end* of the list */
609 if ((ni = aparm->items)) {
610 for (; ni; ni = ni->next)
612 break; /* skip to last one */
615 aparm->items = ti; /* we're first */
619 /* skip to next non-flag item, if any */
621 AdvanceType(struct cmd_syndesc *as, afs_int32 aval)
624 struct cmd_parmdesc *tp;
626 /* first see if we should try to grab rest of line for this dude */
627 if (as->parms[aval].flags & CMD_EXPANDS)
630 /* if not, find next non-flag used slot */
631 for (next = aval + 1; next < CMD_MAXPARMS; next++) {
632 tp = &as->parms[next];
633 if (tp->type != 0 && tp->type != CMD_FLAG)
639 /* discard parameters filled in by dispatch */
641 ResetSyntax(struct cmd_syndesc *as)
644 struct cmd_parmdesc *tp;
645 struct cmd_item *ti, *ni;
648 for (i = 0; i < CMD_MAXPARMS; i++, tp++) {
650 case CMD_SINGLE_OR_FLAG:
651 if (tp->items == &dummy)
653 /* Deliberately fall through here */
656 /* free whole list in both cases, just for fun */
657 for (ti = tp->items; ti; ti = ni) {
671 /* move the expands flag to the last one in the list */
673 SetupExpandsFlag(struct cmd_syndesc *as)
675 struct cmd_parmdesc *tp;
679 /* find last CMD_LIST type parameter, optional or not, and make it expandable
680 * if no other dude is expandable */
681 for (i = 0; i < CMD_MAXPARMS; i++) {
683 if (tp->type == CMD_LIST) {
684 if (tp->flags & CMD_EXPANDS)
685 return 0; /* done if already specified */
690 as->parms[last].flags |= CMD_EXPANDS;
694 /* Take the current argv & argc and alter them so that the initialization
695 * opcode is made to appear. This is used in cases where the initialization
696 * opcode is implicitly invoked.*/
698 InsertInitOpcode(int *aargc, char **aargv)
700 char **newargv; /*Ptr to new, expanded argv space */
701 char *pinitopcode; /*Ptr to space for name of init opcode */
702 int i; /*Loop counter */
704 /* Allocate the new argv array, plus one for the new opcode, plus one
705 * more for the trailing null pointer */
706 newargv = malloc(((*aargc) + 2) * sizeof(char *));
708 fprintf(stderr, "%s: Can't create new argv array with %d+2 slots\n",
713 /* Create space for the initial opcode & fill it in */
714 pinitopcode = malloc(sizeof(initcmd_opcode));
716 fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]);
720 strcpy(pinitopcode, initcmd_opcode);
722 /* Move all the items in the old argv into the new argv, in their
724 for (i = *aargc; i > 1; i--)
725 newargv[i] = aargv[i - 1];
727 /* Slip in the opcode and the trailing null pointer, and bump the
728 * argument count up by one for the new opcode */
729 newargv[0] = aargv[0];
730 newargv[1] = pinitopcode;
732 newargv[*aargc] = NULL;
734 /* Return the happy news */
737 } /*InsertInitOpcode */
740 NoParmsOK(struct cmd_syndesc *as)
743 struct cmd_parmdesc *td;
745 for (i = 0; i < CMD_MAXPARMS; i++) {
747 if (td->type != 0 && !(td->flags & CMD_OPTIONAL)) {
748 /* found a non-optional (e.g. required) parm, so NoParmsOK
749 * is false (some parms are required) */
756 /* Add help, apropos commands once */
760 struct cmd_syndesc *ts;
763 ts = cmd_CreateSyntax("help", HelpProc, NULL,
764 "get help on commands");
765 cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
766 cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, NULL);
768 ts = cmd_CreateSyntax("apropos", AproposProc, NULL,
769 "search by help text");
770 cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED, "help string");
771 ts = cmd_CreateSyntax("version", VersionProc, NULL,
773 ts = cmd_CreateSyntax("-version", VersionProc, NULL,
775 ts = cmd_CreateSyntax("-help", HelpProc, NULL,
777 ts = cmd_CreateSyntax("--version", VersionProc, NULL,
779 ts = cmd_CreateSyntax("--help", HelpProc, NULL,
784 /* Call the appropriate function, or return syntax error code. Note: if
785 * no opcode is specified, an initialization routine exists, and it has
786 * NOT been called before, we invoke the special initialization opcode
789 cmd_Parse(int argc, char **argv, struct cmd_syndesc **outsyntax)
792 struct cmd_syndesc *ts = NULL;
793 struct cmd_parmdesc *tparm;
800 char *embeddedvalue = NULL;
801 static int initd = 0; /*Is this the first time this routine has been called? */
802 static int initcmdpossible = 1; /*Should be consider parsing the initial command? */
811 /*Remember the program name */
816 if (!NoParmsOK(allSyntax)) {
817 printf("%s: Type '%s -help' for help\n", pname, pname);
824 /* if there is an initcmd, don't print an error message, just
825 * setup to use the initcmd below. */
826 if (!(initcmdpossible && FindSyntax(initcmd_opcode, NULL))) {
827 printf("%s: Type '%s help' or '%s help <topic>' for help\n",
828 pname, pname, pname);
835 /* Find the syntax descriptor for this command, doing prefix matching properly */
839 ts = (argc < 2 ? 0 : FindSyntax(argv[1], &ambig));
841 /*First token doesn't match a syntax descriptor */
842 if (initcmdpossible) {
843 /*If initial command line handling hasn't been done yet,
844 * see if there is a descriptor for the initialization opcode.
845 * Only try this once. */
847 ts = FindSyntax(initcmd_opcode, NULL);
849 /*There is no initialization opcode available, so we declare
852 fprintf(stderr, "%s", NName(pname, ": "));
854 "Ambiguous operation '%s'; type '%shelp' for list\n",
855 argv[1], NName(pname, " "));
857 fprintf(stderr, "%s", NName(pname, ": "));
859 "Unrecognized operation '%s'; type '%shelp' for list\n",
860 argv[1], NName(pname, " "));
862 code = CMD_UNKNOWNCMD;
865 /*Found syntax structure for an initialization opcode. Fix
866 * up argv and argc to relect what the user
867 * ``should have'' typed */
868 if (!(argv = InsertInitOpcode(&argc, argv))) {
870 "%sCan't insert implicit init opcode into command line\n",
872 code = CMD_INTERNALERROR;
876 } /*Initial opcode not yet attempted */
878 /* init cmd already run and no syntax entry found */
880 fprintf(stderr, "%s", NName(pname, ": "));
882 "Ambiguous operation '%s'; type '%shelp' for list\n",
883 argv[1], NName(pname, " "));
885 fprintf(stderr, "%s", NName(pname, ": "));
887 "Unrecognized operation '%s'; type '%shelp' for list\n",
888 argv[1], NName(pname, " "));
890 code = CMD_UNKNOWNCMD;
893 } /*Argv[1] is not a valid opcode */
894 } /*Opcodes are defined */
896 /* Found the descriptor; start parsing. curType is the type we're
900 /* We start off parsing in "positional" mode, where tokens are put in
901 * slots positionally. If we find a name that takes args, we go
902 * out of positional mode, and from that point on, expect a switch
903 * before any particular token. */
905 positional = enablePositional; /* Accepting positional cmds ? */
906 i = noOpcodes ? 1 : 2;
907 SetupExpandsFlag(ts);
908 for (; i < argc; i++) {
912 embeddedvalue = NULL;
915 /* Only tokens that start with a hyphen and are not followed by a digit
916 * are considered switches. This allow negative numbers. */
918 if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
922 if (strrchr(argv[i], '=') != NULL) {
923 param = strdup(argv[i]);
924 embeddedvalue = strrchr(param, '=');
925 *embeddedvalue = '\0';
927 j = FindType(ts, param);
929 j = FindType(ts, argv[i]);
934 "%sUnrecognized or ambiguous switch '%s'; type ",
935 NName(pname, ": "), argv[i]);
937 fprintf(stderr, "'%s -help' for detailed help\n",
940 fprintf(stderr, "'%shelp %s' for detailed help\n",
941 NName(argv[0], " "), ts->name);
942 code = CMD_UNKNOWNSWITCH;
945 if (j >= CMD_MAXPARMS) {
946 fprintf(stderr, "%sInternal parsing error\n",
948 code = CMD_INTERNALERROR;
951 if (ts->parms[j].type == CMD_FLAG) {
952 ts->parms[j].items = &dummy;
955 fprintf(stderr, "%sSwitch '%s' doesn't take an argument\n",
956 NName(pname, ": "), ts->parms[j].name);
963 ts->parms[j].flags |= CMD_PROCESSED;
966 AddItem(&ts->parms[curType], embeddedvalue, pname);
970 /* Try to fit in this descr */
971 if (curType >= CMD_MAXPARMS) {
972 fprintf(stderr, "%sToo many arguments\n", NName(pname, ": "));
976 tparm = &ts->parms[curType];
978 if ((tparm->type == 0) || /* No option in this slot */
979 (tparm->type == CMD_FLAG)) { /* A flag (not an argument */
980 /* skipped parm slot */
981 curType++; /* Skip this slot and reprocess this parm */
986 if (!(tparm->flags & CMD_PROCESSED) && (tparm->flags & CMD_HIDE)) {
987 curType++; /* Skip this slot and reprocess this parm */
992 if (tparm->type == CMD_SINGLE ||
993 tparm->type == CMD_SINGLE_OR_FLAG) {
995 fprintf(stderr, "%sToo many values after switch %s\n",
996 NName(pname, ": "), tparm->name);
1000 AddItem(tparm, argv[i], pname); /* Add to end of list */
1001 } else if (tparm->type == CMD_LIST) {
1002 AddItem(tparm, argv[i], pname); /* Add to end of list */
1005 /* Now, if we're in positional mode, advance to the next item */
1007 curType = AdvanceType(ts, curType);
1011 /* keep track of this for messages */
1012 ts->a0name = argv[0];
1014 /* If we make it here, all the parameters are filled in. Check to see if
1015 * this is a -help version. Must do this before checking for all
1016 * required parms, otherwise it is a real nuisance */
1017 if (ts->parms[CMD_HELPPARM].items) {
1019 /* Display full help syntax if we don't have subcommands */
1026 /* Parsing done, see if we have all of our required parameters */
1027 for (i = 0; i < CMD_MAXPARMS; i++) {
1028 tparm = &ts->parms[i];
1029 if (tparm->type == 0)
1030 continue; /* Skipped parm slot */
1031 if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
1032 if (tparm->type == CMD_SINGLE_OR_FLAG) {
1033 tparm->items = &dummy;
1035 fprintf(stderr, "%s The field '%s' isn't completed properly\n",
1036 NName(pname, ": "), tparm->name);
1041 if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
1042 fprintf(stderr, "%sMissing required parameter '%s'\n",
1043 NName(pname, ": "), tparm->name);
1047 tparm->flags &= ~CMD_PROCESSED;
1052 if (code && ts != NULL)
1059 cmd_Dispatch(int argc, char **argv)
1061 struct cmd_syndesc *ts = NULL;
1064 code = cmd_Parse(argc, argv, &ts);
1069 * Before calling the beforeProc and afterProc and all the implications
1070 * from those calls, check if the help procedure was called and call it
1073 if ((ts->proc == HelpProc) || (ts->proc == AproposProc)) {
1074 code = (*ts->proc) (ts, ts->rock);
1078 /* Now, we just call the procedure and return */
1080 code = (*beforeProc) (ts, beforeRock);
1085 code = (*ts->proc) (ts, ts->rock);
1088 (*afterProc) (ts, afterRock);
1090 cmd_FreeOptions(&ts);
1095 cmd_FreeOptions(struct cmd_syndesc **ts)
1103 /* free token list returned by parseLine */
1105 FreeTokens(struct cmd_token *alist)
1107 struct cmd_token *nlist;
1108 for (; alist; alist = nlist) {
1109 nlist = alist->next;
1116 /* free an argv list returned by parseline */
1118 cmd_FreeArgv(char **argv)
1121 for (tp = *argv; tp; argv++, tp = *argv)
1126 /* copy back the arg list to the argv array, freeing the cmd_tokens as you go;
1127 * the actual data is still malloc'd, and will be freed when the caller calls
1128 * cmd_FreeArgv later on
1132 CopyBackArgs(struct cmd_token *alist, char **argv,
1133 afs_int32 * an, afs_int32 amaxn)
1135 struct cmd_token *next;
1141 *argv = (char *)malloc(strlen(INITSTR) + 1);
1143 strcpy(*argv, INITSTR);
1149 return CMD_TOOMANY; /* argv is too small for his many parms. */
1158 *(argv++) = 0; /* use last slot for terminating null */
1159 /* don't count terminating null */
1167 if (x == '"' || x == 39 /* single quote */ )
1176 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
1183 cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
1187 int inToken, inQuote;
1188 struct cmd_token *first, *last;
1189 struct cmd_token *ttok;
1192 inToken = 0; /* not copying token chars at start */
1195 inQuote = 0; /* not in a quoted string */
1198 if (tc == 0 || (!inQuote && space(tc))) { /* terminating null gets us in here, too */
1200 inToken = 0; /* end of this token */
1202 return -1; /* should never get here */
1205 ttok = malloc(sizeof(struct cmd_token));
1208 ttok->key = malloc(strlen(tbuffer) + 1);
1210 strcpy(ttok->key, tbuffer);
1220 /* an alpha character */
1225 if (tptr - tbuffer >= sizeof(tbuffer)) {
1227 return CMD_TOOBIG; /* token too long */
1230 /* hit a quote, toggle inQuote flag but don't insert character */
1233 /* insert character */
1238 /* last token flushed 'cause space(0) --> true */
1241 return CopyBackArgs(first, argv, an, amaxn);
1247 cmd_OptionAsInt(struct cmd_syndesc *syn, int pos, int *value)
1249 if (pos > syn->nParms)
1250 return CMD_EXCESSPARMS;
1251 if (syn->parms[pos].items == NULL ||
1252 syn->parms[pos].items->data == NULL)
1254 if (syn->parms[pos].items == &dummy)
1257 *value = strtol(syn->parms[pos].items->data, NULL, 10);
1263 cmd_OptionAsUint(struct cmd_syndesc *syn, int pos,
1264 unsigned int *value)
1266 if (pos > syn->nParms)
1267 return CMD_EXCESSPARMS;
1268 if (syn->parms[pos].items == NULL ||
1269 syn->parms[pos].items->data == NULL)
1271 if (syn->parms[pos].items == &dummy)
1274 *value = strtoul(syn->parms[pos].items->data, NULL, 10);
1280 cmd_OptionAsString(struct cmd_syndesc *syn, int pos, char **value)
1282 if (pos > syn->nParms)
1283 return CMD_EXCESSPARMS;
1284 if (syn->parms[pos].items == NULL || syn->parms[pos].items->data == NULL)
1286 if (syn->parms[pos].items == &dummy)
1292 *value = strdup(syn->parms[pos].items->data);
1298 cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **value)
1300 if (pos > syn->nParms)
1301 return CMD_EXCESSPARMS;
1302 if (syn->parms[pos].items == NULL)
1305 *value = syn->parms[pos].items;
1310 cmd_OptionAsFlag(struct cmd_syndesc *syn, int pos, int *value)
1312 if (pos > syn->nParms)
1313 return CMD_EXCESSPARMS;
1314 if (syn->parms[pos].items == NULL)
1322 cmd_OptionPresent(struct cmd_syndesc *syn, int pos)
1324 if (pos > syn->nParms || syn->parms[pos].items == NULL)