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 strlcpy(tbuffer, a1, sizeof(tbuffer));
46 strlcat(tbuffer, a2, sizeof(tbuffer));
51 /* return true if asub is a substring of amain */
53 SubString(char *amain, char *asub)
57 mlen = (int) strlen(amain);
58 slen = (int) strlen(asub);
61 return 0; /* not a substring */
62 for (i = 0; i <= j; i++) {
63 if (strncmp(amain, asub, slen) == 0)
67 return 0; /* didn't find it */
71 FindType(struct cmd_syndesc *as, char *aname)
77 struct cmd_item *alias;
79 /* Allow --long-style options. */
80 if (aname[0] == '-' && aname[1] == '-' && aname[2] && aname[3]) {
84 cmdlen = strlen(aname);
87 for (i = 0; i < CMD_MAXPARMS; i++) {
88 if (as->parms[i].type == 0)
89 continue; /* this slot not set (seeked over) */
90 if (strcmp(as->parms[i].name, aname) == 0)
92 if (strlen(as->parms[i].name) < cmdlen)
95 /* Check for aliases, which must be full matches */
96 alias = as->parms[i].aliases;
97 while (alias != NULL) {
98 if (strcmp(alias->data, aname) == 0)
103 /* A hidden option, or one which cannot be abbreviated,
104 * must be a full match (no best matches) */
105 if (as->parms[i].flags & CMD_HIDE ||
106 as->parms[i].flags & CMD_NOABBRV ||
110 if (strncmp(as->parms[i].name, aname, cmdlen) == 0) {
117 return (ambig ? -1 : best);
120 static struct cmd_syndesc *
121 FindSyntax(char *aname, int *aambig)
123 struct cmd_syndesc *ts;
124 struct cmd_syndesc *best;
128 cmdLen = strlen(aname);
129 best = (struct cmd_syndesc *)0;
132 *aambig = 0; /* initialize to unambiguous */
133 for (ts = allSyntax; ts; ts = ts->next) {
134 if (strcmp(aname, ts->name) == 0)
136 if (strlen(ts->name) < cmdLen)
137 continue; /* we typed more than item has */
138 /* A hidden command must be a full match (no best matches) */
139 if (ts->flags & CMD_HIDDEN)
142 /* This is just an alias for *best, or *best is just an alias for us.
143 * If we don't make this check explicitly, then an alias which is just a
144 * short prefix of the real command's name might make things ambiguous
145 * for no apparent reason.
147 if (best && ts->aliasOf == best->aliasOf)
149 if (strncmp(ts->name, aname, cmdLen) == 0) {
151 ambig = 1; /* ambiguous name */
158 *aambig = ambig; /* if ambiguous and they care, tell them */
159 return (struct cmd_syndesc *)0; /* fails */
161 return best; /* otherwise its not ambiguous, and they know */
164 /* print the help for a single parameter */
166 ParmHelpString(struct cmd_parmdesc *aparm)
169 if (aparm->type == CMD_FLAG) {
172 asprintf(&str, " %s<%s>%s%s",
173 aparm->type == CMD_SINGLE_OR_FLAG?"[":"",
174 aparm->help?aparm->help:"arg",
175 aparm->type == CMD_LIST?"+":"",
176 aparm->type == CMD_SINGLE_OR_FLAG?"]":"");
181 extern char *AFSVersion;
184 VersionProc(struct cmd_syndesc *as, void *arock)
186 printf("%s\n", AFSVersion);
191 PrintSyntax(struct cmd_syndesc *as)
194 struct cmd_parmdesc *tp;
200 /* now print usage, from syntax table */
202 asprintf(&str, "Usage: %s", as->a0name);
204 if (!strcmp(as->name, initcmd_opcode))
205 asprintf(&str, "Usage: %s[%s]", NName(as->a0name, " "), as->name);
207 asprintf(&str, "Usage: %s%s", NName(as->a0name, " "), as->name);
214 for (i = 0; i < CMD_MAXPARMS; i++) {
217 continue; /* seeked over slot */
218 if (tp->flags & CMD_HIDE)
219 continue; /* skip hidden options */
221 /* The parameter name is the real name, plus any aliases */
223 name = strdup(tp->name);
226 struct cmd_item *alias;
227 namelen = strlen(tp->name) + 1;
228 for (alias = tp->aliases; alias != NULL; alias = alias->next)
229 namelen+=strlen(alias->data) + 3;
231 name = malloc(namelen);
232 strlcpy(name, tp->name, namelen);
234 for (alias = tp->aliases; alias != NULL; alias = alias->next) {
235 strlcat(name, " | ", namelen);
236 strlcat(name, alias->data, namelen);
240 /* Work out if we can fit what we want to on this line, or if we need to
242 str = ParmHelpString(tp);
243 xtralen = 1 + strlen(name) + strlen(str) +
244 ((tp->flags & CMD_OPTIONAL)? 2: 0);
246 if (len + xtralen > 78) {
252 tp->flags & CMD_OPTIONAL?"[":"",
255 tp->flags & CMD_OPTIONAL?"]":"");
262 /* must print newline in any case, to terminate preceding line */
264 PrintAliases(struct cmd_syndesc *as)
266 struct cmd_syndesc *ts;
268 if (as->flags & CMD_ALIAS) {
270 printf("(alias for %s)\n", ts->name);
274 return; /* none, print nothing */
276 for (as = as->nextAlias; as; as = as->nextAlias) {
277 printf("%s ", as->name);
284 PrintFlagHelp(struct cmd_syndesc *as)
287 struct cmd_parmdesc *tp;
291 /* find flag name length */
293 for (i = 0; i < CMD_MAXPARMS; i++) {
294 if (i == CMD_HELPPARM)
297 if (tp->type != CMD_FLAG)
299 if (tp->flags & CMD_HIDE)
300 continue; /* skip hidden options */
304 if (strlen(tp->name) > flag_width)
305 flag_width = strlen(tp->name);
308 /* print flag help */
309 flag_prefix = "Where:";
310 for (i = 0; i < CMD_MAXPARMS; i++) {
311 if (i == CMD_HELPPARM)
314 if (tp->type != CMD_FLAG)
316 if (tp->flags & CMD_HIDE)
317 continue; /* skip hidden options */
321 printf("%-7s%-*s %s\n", flag_prefix, flag_width, tp->name, tp->help);
327 AproposProc(struct cmd_syndesc *as, void *arock)
329 struct cmd_syndesc *ts;
334 tsub = as->parms[0].items->data;
335 for (ts = allSyntax; ts; ts = ts->next) {
336 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
338 if (SubString(ts->help, tsub)) {
339 printf("%s: %s\n", ts->name, ts->help);
341 } else if (SubString(ts->name, tsub)) {
342 printf("%s: %s\n", ts->name, ts->help);
347 printf("Sorry, no commands found\n");
352 HelpProc(struct cmd_syndesc *as, void *arock)
354 struct cmd_syndesc *ts;
359 if (as->parms[0].items == 0) {
360 printf("%sCommands are:\n", NName(as->a0name, ": "));
361 for (ts = allSyntax; ts; ts = ts->next) {
362 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
364 printf("%-15s %s\n", ts->name, (ts->help ? ts->help : ""));
367 /* print out individual help topics */
368 for (ti = as->parms[0].items; ti; ti = ti->next) {
370 ts = FindSyntax(ti->data, &ambig);
371 if (ts && (ts->flags & CMD_HIDDEN))
372 ts = 0; /* no hidden commands */
374 /* print out command name and help */
375 printf("%s%s: %s ", NName(as->a0name, " "), ts->name,
376 (ts->help ? ts->help : ""));
377 ts->a0name = as->a0name;
383 fprintf(stderr, "%sUnknown topic '%s'\n",
384 NName(as->a0name, ": "), ti->data);
386 /* ambiguous, list 'em all */
388 "%sAmbiguous topic '%s'; use 'apropos' to list\n",
389 NName(as->a0name, ": "), ti->data);
391 code = CMD_UNKNOWNCMD;
399 cmd_SetBeforeProc(int (*aproc) (struct cmd_syndesc * ts, void *beforeRock),
408 cmd_SetAfterProc(int (*aproc) (struct cmd_syndesc * ts, void *afterRock),
416 /* thread on list in alphabetical order */
418 SortSyntax(struct cmd_syndesc *as)
420 struct cmd_syndesc **ld, *ud;
422 for (ld = &allSyntax, ud = *ld; ud; ld = &ud->next, ud = *ld) {
423 if (strcmp(ud->name, as->name) > 0) { /* next guy is bigger than us */
427 /* thread us on the list now */
434 cmd_CreateSyntax(char *aname,
435 int (*aproc) (struct cmd_syndesc * ts, void *arock),
436 void *arock, char *ahelp)
438 struct cmd_syndesc *td;
440 /* can't have two cmds in no opcode mode */
444 td = calloc(1, sizeof(struct cmd_syndesc));
446 td->aliasOf = td; /* treat aliasOf as pointer to real command, no matter what */
448 /* copy in name, etc */
450 td->name = malloc(strlen(aname) + 1);
452 strcpy(td->name, aname);
458 /* Piggy-back the hidden option onto the help string */
459 if (ahelp == (char *)CMD_HIDDEN) {
460 td->flags |= CMD_HIDDEN;
462 td->help = malloc(strlen(ahelp) + 1);
464 strcpy(td->help, ahelp);
473 cmd_Seek(td, CMD_HELPPARM);
474 cmd_AddParm(td, "-help", CMD_FLAG, CMD_OPTIONAL, "get detailed help");
481 cmd_CreateAlias(struct cmd_syndesc *as, char *aname)
483 struct cmd_syndesc *td;
485 td = malloc(sizeof(struct cmd_syndesc));
487 memcpy(td, as, sizeof(struct cmd_syndesc));
488 td->name = malloc(strlen(aname) + 1);
490 strcpy(td->name, aname);
491 td->flags |= CMD_ALIAS;
492 /* if ever free things, make copy of help string, too */
497 /* thread on alias lists */
498 td->nextAlias = as->nextAlias;
502 return 0; /* all done */
506 cmd_DisablePositionalCommands(void)
508 enablePositional = 0;
512 cmd_DisableAbbreviations(void)
514 enableAbbreviation = 0;
518 cmd_IsAdministratorCommand(struct cmd_syndesc *as)
520 as->flags |= CMD_ADMIN;
525 cmd_Seek(struct cmd_syndesc *as, int apos)
527 if (apos >= CMD_MAXPARMS)
528 return CMD_EXCESSPARMS;
534 cmd_AddParmAtOffset(struct cmd_syndesc *as, int ref, char *aname, int atype,
535 afs_int32 aflags, char *ahelp)
537 struct cmd_parmdesc *tp;
539 if (ref >= CMD_MAXPARMS)
540 return CMD_EXCESSPARMS;
541 tp = &as->parms[ref];
543 tp->name = malloc(strlen(aname) + 1);
545 strcpy(tp->name, aname);
550 tp->help = malloc(strlen(ahelp) + 1);
552 strcpy(tp->help, ahelp);
558 if (as->nParms <= ref)
565 cmd_AddParm(struct cmd_syndesc *as, char *aname, int atype,
566 afs_int32 aflags, char *ahelp)
568 if (as->nParms >= CMD_MAXPARMS)
569 return CMD_EXCESSPARMS;
571 return cmd_AddParmAtOffset(as, as->nParms++, aname, atype, aflags, ahelp);
575 cmd_AddParmAlias(struct cmd_syndesc *as, int pos, char *alias)
577 struct cmd_item *item;
579 if (pos > as->nParms)
580 return CMD_EXCESSPARMS;
582 item = calloc(1, sizeof(struct cmd_item));
583 item->data = strdup(alias);
584 item->next = as->parms[pos].aliases;
585 as->parms[pos].aliases = item;
590 /* add a text item to the end of the parameter list */
592 AddItem(struct cmd_parmdesc *aparm, char *aval, char *pname)
594 struct cmd_item *ti, *ni;
596 if (aparm->type == CMD_SINGLE ||
597 aparm->type == CMD_SINGLE_OR_FLAG) {
599 fprintf(stderr, "%sToo many values after switch %s\n",
600 NName(pname, ": "), aparm->name);
605 ti = calloc(1, sizeof(struct cmd_item));
607 ti->data = malloc(strlen(aval) + 1);
609 strcpy(ti->data, aval);
610 /* now put ti at the *end* of the list */
611 if ((ni = aparm->items)) {
612 for (; ni; ni = ni->next)
614 break; /* skip to last one */
617 aparm->items = ti; /* we're first */
621 /* skip to next non-flag item, if any */
623 AdvanceType(struct cmd_syndesc *as, afs_int32 aval)
626 struct cmd_parmdesc *tp;
628 /* first see if we should try to grab rest of line for this dude */
629 if (as->parms[aval].flags & CMD_EXPANDS)
632 /* if not, find next non-flag used slot */
633 for (next = aval + 1; next < CMD_MAXPARMS; next++) {
634 tp = &as->parms[next];
635 if (tp->type != 0 && tp->type != CMD_FLAG)
641 /* discard parameters filled in by dispatch */
643 ResetSyntax(struct cmd_syndesc *as)
646 struct cmd_parmdesc *tp;
647 struct cmd_item *ti, *ni;
650 for (i = 0; i < CMD_MAXPARMS; i++, tp++) {
652 case CMD_SINGLE_OR_FLAG:
653 if (tp->items == &dummy)
655 /* Deliberately fall through here */
658 /* free whole list in both cases, just for fun */
659 for (ti = tp->items; ti; ti = ni) {
673 /* move the expands flag to the last one in the list */
675 SetupExpandsFlag(struct cmd_syndesc *as)
677 struct cmd_parmdesc *tp;
681 /* find last CMD_LIST type parameter, optional or not, and make it expandable
682 * if no other dude is expandable */
683 for (i = 0; i < CMD_MAXPARMS; i++) {
685 if (tp->type == CMD_LIST) {
686 if (tp->flags & CMD_EXPANDS)
687 return 0; /* done if already specified */
692 as->parms[last].flags |= CMD_EXPANDS;
696 /* Take the current argv & argc and alter them so that the initialization
697 * opcode is made to appear. This is used in cases where the initialization
698 * opcode is implicitly invoked.*/
700 InsertInitOpcode(int *aargc, char **aargv)
702 char **newargv; /*Ptr to new, expanded argv space */
703 char *pinitopcode; /*Ptr to space for name of init opcode */
704 int i; /*Loop counter */
706 /* Allocate the new argv array, plus one for the new opcode, plus one
707 * more for the trailing null pointer */
708 newargv = malloc(((*aargc) + 2) * sizeof(char *));
710 fprintf(stderr, "%s: Can't create new argv array with %d+2 slots\n",
715 /* Create space for the initial opcode & fill it in */
716 pinitopcode = malloc(sizeof(initcmd_opcode));
718 fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]);
722 strcpy(pinitopcode, initcmd_opcode);
724 /* Move all the items in the old argv into the new argv, in their
726 for (i = *aargc; i > 1; i--)
727 newargv[i] = aargv[i - 1];
729 /* Slip in the opcode and the trailing null pointer, and bump the
730 * argument count up by one for the new opcode */
731 newargv[0] = aargv[0];
732 newargv[1] = pinitopcode;
734 newargv[*aargc] = NULL;
736 /* Return the happy news */
739 } /*InsertInitOpcode */
742 NoParmsOK(struct cmd_syndesc *as)
745 struct cmd_parmdesc *td;
747 for (i = 0; i < CMD_MAXPARMS; i++) {
749 if (td->type != 0 && !(td->flags & CMD_OPTIONAL)) {
750 /* found a non-optional (e.g. required) parm, so NoParmsOK
751 * is false (some parms are required) */
758 /* Add help, apropos commands once */
762 struct cmd_syndesc *ts;
765 ts = cmd_CreateSyntax("help", HelpProc, NULL,
766 "get help on commands");
767 cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
768 cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, NULL);
770 ts = cmd_CreateSyntax("apropos", AproposProc, NULL,
771 "search by help text");
772 cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED, "help string");
774 cmd_CreateSyntax("version", VersionProc, NULL,
776 cmd_CreateSyntax("-version", VersionProc, NULL,
778 cmd_CreateSyntax("-help", HelpProc, NULL,
780 cmd_CreateSyntax("--version", VersionProc, NULL,
782 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 = NULL; /* 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)