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 */
35 static cmd_config_section *globalConfig = NULL;
36 static const char *commandName = NULL;
38 /* take name and string, and return null string if name is empty, otherwise return
39 the concatenation of the two strings */
41 NName(char *a1, char *a2)
43 static char tbuffer[300];
44 if (strlen(a1) == 0) {
47 strlcpy(tbuffer, a1, sizeof(tbuffer));
48 strlcat(tbuffer, a2, sizeof(tbuffer));
53 /* return true if asub is a substring of amain */
55 SubString(char *amain, char *asub)
59 mlen = (int) strlen(amain);
60 slen = (int) strlen(asub);
63 return 0; /* not a substring */
64 for (i = 0; i <= j; i++) {
65 if (strncmp(amain, asub, slen) == 0)
69 return 0; /* didn't find it */
73 FindType(struct cmd_syndesc *as, char *aname)
79 struct cmd_item *alias;
81 /* Allow --long-style options. */
82 if (aname[0] == '-' && aname[1] == '-' && aname[2] && aname[3]) {
86 cmdlen = strlen(aname);
89 for (i = 0; i < CMD_MAXPARMS; i++) {
90 if (as->parms[i].type == 0)
91 continue; /* this slot not set (seeked over) */
92 if (strcmp(as->parms[i].name, aname) == 0)
94 if (strlen(as->parms[i].name) < cmdlen)
97 /* Check for aliases, which must be full matches */
98 alias = as->parms[i].aliases;
99 while (alias != NULL) {
100 if (strcmp(alias->data, aname) == 0)
105 /* A hidden option, or one which cannot be abbreviated,
106 * must be a full match (no best matches) */
107 if (as->parms[i].flags & CMD_HIDE ||
108 as->parms[i].flags & CMD_NOABBRV ||
112 if (strncmp(as->parms[i].name, aname, cmdlen) == 0) {
119 return (ambig ? -1 : best);
122 static struct cmd_syndesc *
123 FindSyntax(char *aname, int *aambig)
125 struct cmd_syndesc *ts;
126 struct cmd_syndesc *best;
130 cmdLen = strlen(aname);
131 best = (struct cmd_syndesc *)0;
134 *aambig = 0; /* initialize to unambiguous */
135 for (ts = allSyntax; ts; ts = ts->next) {
136 if (strcmp(aname, ts->name) == 0)
138 if (strlen(ts->name) < cmdLen)
139 continue; /* we typed more than item has */
140 /* A hidden command must be a full match (no best matches) */
141 if (ts->flags & CMD_HIDDEN)
144 /* This is just an alias for *best, or *best is just an alias for us.
145 * If we don't make this check explicitly, then an alias which is just a
146 * short prefix of the real command's name might make things ambiguous
147 * for no apparent reason.
149 if (best && ts->aliasOf == best->aliasOf)
151 if (strncmp(ts->name, aname, cmdLen) == 0) {
153 ambig = 1; /* ambiguous name */
160 *aambig = ambig; /* if ambiguous and they care, tell them */
161 return (struct cmd_syndesc *)0; /* fails */
163 return best; /* otherwise its not ambiguous, and they know */
166 /* print the help for a single parameter */
168 ParmHelpString(struct cmd_parmdesc *aparm)
171 if (aparm->type == CMD_FLAG) {
174 asprintf(&str, " %s<%s>%s%s",
175 aparm->type == CMD_SINGLE_OR_FLAG?"[":"",
176 aparm->help?aparm->help:"arg",
177 aparm->type == CMD_LIST?"+":"",
178 aparm->type == CMD_SINGLE_OR_FLAG?"]":"");
183 extern char *AFSVersion;
186 VersionProc(struct cmd_syndesc *as, void *arock)
188 printf("%s\n", AFSVersion);
193 PrintSyntax(struct cmd_syndesc *as)
196 struct cmd_parmdesc *tp;
202 /* now print usage, from syntax table */
204 asprintf(&str, "Usage: %s", as->a0name);
206 if (!strcmp(as->name, initcmd_opcode))
207 asprintf(&str, "Usage: %s[%s]", NName(as->a0name, " "), as->name);
209 asprintf(&str, "Usage: %s%s", NName(as->a0name, " "), as->name);
216 for (i = 0; i < CMD_MAXPARMS; i++) {
219 continue; /* seeked over slot */
220 if (tp->flags & CMD_HIDE)
221 continue; /* skip hidden options */
223 /* The parameter name is the real name, plus any aliases */
225 name = strdup(tp->name);
228 struct cmd_item *alias;
229 namelen = strlen(tp->name) + 1;
230 for (alias = tp->aliases; alias != NULL; alias = alias->next)
231 namelen+=strlen(alias->data) + 3;
233 name = malloc(namelen);
234 strlcpy(name, tp->name, namelen);
236 for (alias = tp->aliases; alias != NULL; alias = alias->next) {
237 strlcat(name, " | ", namelen);
238 strlcat(name, alias->data, namelen);
242 /* Work out if we can fit what we want to on this line, or if we need to
244 str = ParmHelpString(tp);
245 xtralen = 1 + strlen(name) + strlen(str) +
246 ((tp->flags & CMD_OPTIONAL)? 2: 0);
248 if (len + xtralen > 78) {
254 tp->flags & CMD_OPTIONAL?"[":"",
257 tp->flags & CMD_OPTIONAL?"]":"");
265 /* must print newline in any case, to terminate preceding line */
267 PrintAliases(struct cmd_syndesc *as)
269 struct cmd_syndesc *ts;
271 if (as->flags & CMD_ALIAS) {
273 printf("(alias for %s)\n", ts->name);
277 return; /* none, print nothing */
279 for (as = as->nextAlias; as; as = as->nextAlias) {
280 printf("%s ", as->name);
287 PrintFlagHelp(struct cmd_syndesc *as)
290 struct cmd_parmdesc *tp;
294 /* find flag name length */
296 for (i = 0; i < CMD_MAXPARMS; i++) {
297 if (i == CMD_HELPPARM)
300 if (tp->type != CMD_FLAG)
302 if (tp->flags & CMD_HIDE)
303 continue; /* skip hidden options */
307 if (strlen(tp->name) > flag_width)
308 flag_width = strlen(tp->name);
311 /* print flag help */
312 flag_prefix = "Where:";
313 for (i = 0; i < CMD_MAXPARMS; i++) {
314 if (i == CMD_HELPPARM)
317 if (tp->type != CMD_FLAG)
319 if (tp->flags & CMD_HIDE)
320 continue; /* skip hidden options */
324 printf("%-7s%-*s %s\n", flag_prefix, flag_width, tp->name, tp->help);
330 AproposProc(struct cmd_syndesc *as, void *arock)
332 struct cmd_syndesc *ts;
337 tsub = as->parms[0].items->data;
338 for (ts = allSyntax; ts; ts = ts->next) {
339 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
341 if (SubString(ts->help, tsub)) {
342 printf("%s: %s\n", ts->name, ts->help);
344 } else if (SubString(ts->name, tsub)) {
345 printf("%s: %s\n", ts->name, ts->help);
350 printf("Sorry, no commands found\n");
355 HelpProc(struct cmd_syndesc *as, void *arock)
357 struct cmd_syndesc *ts;
362 if (as->parms[0].items == 0) {
363 printf("%sCommands are:\n", NName(as->a0name, ": "));
364 for (ts = allSyntax; ts; ts = ts->next) {
365 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
367 printf("%-15s %s\n", ts->name, (ts->help ? ts->help : ""));
370 /* print out individual help topics */
371 for (ti = as->parms[0].items; ti; ti = ti->next) {
373 ts = FindSyntax(ti->data, &ambig);
374 if (ts && (ts->flags & CMD_HIDDEN))
375 ts = 0; /* no hidden commands */
377 /* print out command name and help */
378 printf("%s%s: %s ", NName(as->a0name, " "), ts->name,
379 (ts->help ? ts->help : ""));
380 ts->a0name = as->a0name;
386 fprintf(stderr, "%sUnknown topic '%s'\n",
387 NName(as->a0name, ": "), ti->data);
389 /* ambiguous, list 'em all */
391 "%sAmbiguous topic '%s'; use 'apropos' to list\n",
392 NName(as->a0name, ": "), ti->data);
394 code = CMD_UNKNOWNCMD;
402 cmd_SetBeforeProc(int (*aproc) (struct cmd_syndesc * ts, void *beforeRock),
411 cmd_SetAfterProc(int (*aproc) (struct cmd_syndesc * ts, void *afterRock),
419 /* thread on list in alphabetical order */
421 SortSyntax(struct cmd_syndesc *as)
423 struct cmd_syndesc **ld, *ud;
425 for (ld = &allSyntax, ud = *ld; ud; ld = &ud->next, ud = *ld) {
426 if (strcmp(ud->name, as->name) > 0) { /* next guy is bigger than us */
430 /* thread us on the list now */
437 cmd_CreateSyntax(char *aname,
438 int (*aproc) (struct cmd_syndesc * ts, void *arock),
439 void *arock, char *ahelp)
441 struct cmd_syndesc *td;
443 /* can't have two cmds in no opcode mode */
447 td = calloc(1, sizeof(struct cmd_syndesc));
449 td->aliasOf = td; /* treat aliasOf as pointer to real command, no matter what */
451 /* copy in name, etc */
453 td->name = malloc(strlen(aname) + 1);
455 strcpy(td->name, aname);
461 /* Piggy-back the hidden option onto the help string */
462 if (ahelp == (char *)CMD_HIDDEN) {
463 td->flags |= CMD_HIDDEN;
465 td->help = malloc(strlen(ahelp) + 1);
467 strcpy(td->help, ahelp);
476 cmd_Seek(td, CMD_HELPPARM);
477 cmd_AddParm(td, "-help", CMD_FLAG, CMD_OPTIONAL, "get detailed help");
484 cmd_CreateAlias(struct cmd_syndesc *as, char *aname)
486 struct cmd_syndesc *td;
488 td = malloc(sizeof(struct cmd_syndesc));
490 memcpy(td, as, sizeof(struct cmd_syndesc));
491 td->name = malloc(strlen(aname) + 1);
493 strcpy(td->name, aname);
494 td->flags |= CMD_ALIAS;
495 /* if ever free things, make copy of help string, too */
500 /* thread on alias lists */
501 td->nextAlias = as->nextAlias;
505 return 0; /* all done */
509 cmd_DisablePositionalCommands(void)
511 enablePositional = 0;
515 cmd_DisableAbbreviations(void)
517 enableAbbreviation = 0;
521 cmd_IsAdministratorCommand(struct cmd_syndesc *as)
523 as->flags |= CMD_ADMIN;
528 cmd_Seek(struct cmd_syndesc *as, int apos)
530 if (apos >= CMD_MAXPARMS)
531 return CMD_EXCESSPARMS;
537 cmd_AddParmAtOffset(struct cmd_syndesc *as, int ref, char *aname, int atype,
538 afs_int32 aflags, char *ahelp)
540 struct cmd_parmdesc *tp;
542 if (ref >= CMD_MAXPARMS)
543 return CMD_EXCESSPARMS;
544 tp = &as->parms[ref];
546 tp->name = malloc(strlen(aname) + 1);
548 strcpy(tp->name, aname);
553 tp->help = malloc(strlen(ahelp) + 1);
555 strcpy(tp->help, ahelp);
561 if (as->nParms <= ref)
568 cmd_AddParm(struct cmd_syndesc *as, char *aname, int atype,
569 afs_int32 aflags, char *ahelp)
571 if (as->nParms >= CMD_MAXPARMS)
572 return CMD_EXCESSPARMS;
574 return cmd_AddParmAtOffset(as, as->nParms++, aname, atype, aflags, ahelp);
578 cmd_AddParmAlias(struct cmd_syndesc *as, int pos, char *alias)
580 struct cmd_item *item;
582 if (pos > as->nParms)
583 return CMD_EXCESSPARMS;
585 item = calloc(1, sizeof(struct cmd_item));
586 item->data = strdup(alias);
587 item->next = as->parms[pos].aliases;
588 as->parms[pos].aliases = item;
593 /* add a text item to the end of the parameter list */
595 AddItem(struct cmd_parmdesc *aparm, char *aval, char *pname)
597 struct cmd_item *ti, *ni;
599 if (aparm->type == CMD_SINGLE ||
600 aparm->type == CMD_SINGLE_OR_FLAG) {
602 fprintf(stderr, "%sToo many values after switch %s\n",
603 NName(pname, ": "), aparm->name);
608 ti = calloc(1, sizeof(struct cmd_item));
610 ti->data = malloc(strlen(aval) + 1);
612 strcpy(ti->data, aval);
613 /* now put ti at the *end* of the list */
614 if ((ni = aparm->items)) {
615 for (; ni; ni = ni->next)
617 break; /* skip to last one */
620 aparm->items = ti; /* we're first */
624 /* skip to next non-flag item, if any */
626 AdvanceType(struct cmd_syndesc *as, afs_int32 aval)
629 struct cmd_parmdesc *tp;
631 /* first see if we should try to grab rest of line for this dude */
632 if (as->parms[aval].flags & CMD_EXPANDS)
635 /* if not, find next non-flag used slot */
636 for (next = aval + 1; next < CMD_MAXPARMS; next++) {
637 tp = &as->parms[next];
638 if (tp->type != 0 && tp->type != CMD_FLAG)
644 /* discard parameters filled in by dispatch */
646 ResetSyntax(struct cmd_syndesc *as)
649 struct cmd_parmdesc *tp;
650 struct cmd_item *ti, *ni;
653 for (i = 0; i < CMD_MAXPARMS; i++, tp++) {
655 case CMD_SINGLE_OR_FLAG:
656 if (tp->items == &dummy)
658 /* Deliberately fall through here */
661 /* free whole list in both cases, just for fun */
662 for (ti = tp->items; ti; ti = ni) {
676 /* move the expands flag to the last one in the list */
678 SetupExpandsFlag(struct cmd_syndesc *as)
680 struct cmd_parmdesc *tp;
684 /* find last CMD_LIST type parameter, optional or not, and make it expandable
685 * if no other dude is expandable */
686 for (i = 0; i < CMD_MAXPARMS; i++) {
688 if (tp->type == CMD_LIST) {
689 if (tp->flags & CMD_EXPANDS)
690 return 0; /* done if already specified */
695 as->parms[last].flags |= CMD_EXPANDS;
699 /* Take the current argv & argc and alter them so that the initialization
700 * opcode is made to appear. This is used in cases where the initialization
701 * opcode is implicitly invoked.*/
703 InsertInitOpcode(int *aargc, char **aargv)
705 char **newargv; /*Ptr to new, expanded argv space */
706 char *pinitopcode; /*Ptr to space for name of init opcode */
707 int i; /*Loop counter */
709 /* Allocate the new argv array, plus one for the new opcode, plus one
710 * more for the trailing null pointer */
711 newargv = malloc(((*aargc) + 2) * sizeof(char *));
713 fprintf(stderr, "%s: Can't create new argv array with %d+2 slots\n",
718 /* Create space for the initial opcode & fill it in */
719 pinitopcode = malloc(sizeof(initcmd_opcode));
721 fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]);
725 strcpy(pinitopcode, initcmd_opcode);
727 /* Move all the items in the old argv into the new argv, in their
729 for (i = *aargc; i > 1; i--)
730 newargv[i] = aargv[i - 1];
732 /* Slip in the opcode and the trailing null pointer, and bump the
733 * argument count up by one for the new opcode */
734 newargv[0] = aargv[0];
735 newargv[1] = pinitopcode;
737 newargv[*aargc] = NULL;
739 /* Return the happy news */
742 } /*InsertInitOpcode */
745 NoParmsOK(struct cmd_syndesc *as)
748 struct cmd_parmdesc *td;
750 for (i = 0; i < CMD_MAXPARMS; i++) {
752 if (td->type != 0 && !(td->flags & CMD_OPTIONAL)) {
753 /* found a non-optional (e.g. required) parm, so NoParmsOK
754 * is false (some parms are required) */
761 /* Add help, apropos commands once */
765 struct cmd_syndesc *ts;
768 ts = cmd_CreateSyntax("help", HelpProc, NULL,
769 "get help on commands");
770 cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
771 cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, NULL);
773 ts = cmd_CreateSyntax("apropos", AproposProc, NULL,
774 "search by help text");
775 cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED, "help string");
777 cmd_CreateSyntax("version", VersionProc, NULL,
779 cmd_CreateSyntax("-version", VersionProc, NULL,
781 cmd_CreateSyntax("-help", HelpProc, NULL,
783 cmd_CreateSyntax("--version", VersionProc, NULL,
785 cmd_CreateSyntax("--help", HelpProc, NULL,
790 /* Call the appropriate function, or return syntax error code. Note: if
791 * no opcode is specified, an initialization routine exists, and it has
792 * NOT been called before, we invoke the special initialization opcode
795 cmd_Parse(int argc, char **argv, struct cmd_syndesc **outsyntax)
798 struct cmd_syndesc *ts = NULL;
799 struct cmd_parmdesc *tparm;
806 char *embeddedvalue = NULL;
807 static int initd = 0; /*Is this the first time this routine has been called? */
808 static int initcmdpossible = 1; /*Should be consider parsing the initial command? */
817 /*Remember the program name */
822 if (!NoParmsOK(allSyntax)) {
823 printf("%s: Type '%s -help' for help\n", pname, pname);
830 /* if there is an initcmd, don't print an error message, just
831 * setup to use the initcmd below. */
832 if (!(initcmdpossible && FindSyntax(initcmd_opcode, NULL))) {
833 printf("%s: Type '%s help' or '%s help <topic>' for help\n",
834 pname, pname, pname);
841 /* Find the syntax descriptor for this command, doing prefix matching properly */
845 ts = (argc < 2 ? 0 : FindSyntax(argv[1], &ambig));
847 /*First token doesn't match a syntax descriptor */
848 if (initcmdpossible) {
849 /*If initial command line handling hasn't been done yet,
850 * see if there is a descriptor for the initialization opcode.
851 * Only try this once. */
853 ts = FindSyntax(initcmd_opcode, NULL);
855 /*There is no initialization opcode available, so we declare
858 fprintf(stderr, "%s", NName(pname, ": "));
860 "Ambiguous operation '%s'; type '%shelp' for list\n",
861 argv[1], NName(pname, " "));
863 fprintf(stderr, "%s", NName(pname, ": "));
865 "Unrecognized operation '%s'; type '%shelp' for list\n",
866 argv[1], NName(pname, " "));
868 code = CMD_UNKNOWNCMD;
871 /*Found syntax structure for an initialization opcode. Fix
872 * up argv and argc to relect what the user
873 * ``should have'' typed */
874 if (!(argv = InsertInitOpcode(&argc, argv))) {
876 "%sCan't insert implicit init opcode into command line\n",
878 code = CMD_INTERNALERROR;
882 } /*Initial opcode not yet attempted */
884 /* init cmd already run and no syntax entry found */
886 fprintf(stderr, "%s", NName(pname, ": "));
888 "Ambiguous operation '%s'; type '%shelp' for list\n",
889 argv[1], NName(pname, " "));
891 fprintf(stderr, "%s", NName(pname, ": "));
893 "Unrecognized operation '%s'; type '%shelp' for list\n",
894 argv[1], NName(pname, " "));
896 code = CMD_UNKNOWNCMD;
899 } /*Argv[1] is not a valid opcode */
900 } /*Opcodes are defined */
902 /* Found the descriptor; start parsing. curType is the type we're
906 /* We start off parsing in "positional" mode, where tokens are put in
907 * slots positionally. If we find a name that takes args, we go
908 * out of positional mode, and from that point on, expect a switch
909 * before any particular token. */
911 positional = enablePositional; /* Accepting positional cmds ? */
912 i = noOpcodes ? 1 : 2;
913 SetupExpandsFlag(ts);
914 for (; i < argc; i++) {
918 embeddedvalue = NULL;
921 /* Only tokens that start with a hyphen and are not followed by a digit
922 * are considered switches. This allow negative numbers. */
924 if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
928 if (strrchr(argv[i], '=') != NULL) {
929 param = strdup(argv[i]);
930 embeddedvalue = strrchr(param, '=');
931 *embeddedvalue = '\0';
933 j = FindType(ts, param);
935 j = FindType(ts, argv[i]);
940 "%sUnrecognized or ambiguous switch '%s'; type ",
941 NName(pname, ": "), argv[i]);
943 fprintf(stderr, "'%s -help' for detailed help\n",
946 fprintf(stderr, "'%shelp %s' for detailed help\n",
947 NName(argv[0], " "), ts->name);
948 code = CMD_UNKNOWNSWITCH;
951 if (j >= CMD_MAXPARMS) {
952 fprintf(stderr, "%sInternal parsing error\n",
954 code = CMD_INTERNALERROR;
957 if (ts->parms[j].type == CMD_FLAG) {
958 ts->parms[j].items = &dummy;
961 fprintf(stderr, "%sSwitch '%s' doesn't take an argument\n",
962 NName(pname, ": "), ts->parms[j].name);
969 ts->parms[j].flags |= CMD_PROCESSED;
972 AddItem(&ts->parms[curType], embeddedvalue, pname);
976 /* Try to fit in this descr */
977 if (curType >= CMD_MAXPARMS) {
978 fprintf(stderr, "%sToo many arguments\n", NName(pname, ": "));
982 tparm = &ts->parms[curType];
984 if ((tparm->type == 0) || /* No option in this slot */
985 (tparm->type == CMD_FLAG)) { /* A flag (not an argument */
986 /* skipped parm slot */
987 curType++; /* Skip this slot and reprocess this parm */
992 if (!(tparm->flags & CMD_PROCESSED) && (tparm->flags & CMD_HIDE)) {
993 curType++; /* Skip this slot and reprocess this parm */
998 if (tparm->type == CMD_SINGLE ||
999 tparm->type == CMD_SINGLE_OR_FLAG) {
1001 fprintf(stderr, "%sToo many values after switch %s\n",
1002 NName(pname, ": "), tparm->name);
1006 AddItem(tparm, argv[i], pname); /* Add to end of list */
1007 } else if (tparm->type == CMD_LIST) {
1008 AddItem(tparm, argv[i], pname); /* Add to end of list */
1011 /* Now, if we're in positional mode, advance to the next item */
1013 curType = AdvanceType(ts, curType);
1017 /* keep track of this for messages */
1018 ts->a0name = argv[0];
1020 /* If we make it here, all the parameters are filled in. Check to see if
1021 * this is a -help version. Must do this before checking for all
1022 * required parms, otherwise it is a real nuisance */
1023 if (ts->parms[CMD_HELPPARM].items) {
1025 /* Display full help syntax if we don't have subcommands */
1032 /* Parsing done, see if we have all of our required parameters */
1033 for (i = 0; i < CMD_MAXPARMS; i++) {
1034 tparm = &ts->parms[i];
1035 if (tparm->type == 0)
1036 continue; /* Skipped parm slot */
1037 if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
1038 if (tparm->type == CMD_SINGLE_OR_FLAG) {
1039 tparm->items = &dummy;
1041 fprintf(stderr, "%s The field '%s' isn't completed properly\n",
1042 NName(pname, ": "), tparm->name);
1047 if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
1048 fprintf(stderr, "%sMissing required parameter '%s'\n",
1049 NName(pname, ": "), tparm->name);
1053 tparm->flags &= ~CMD_PROCESSED;
1058 if (code && ts != NULL)
1065 cmd_Dispatch(int argc, char **argv)
1067 struct cmd_syndesc *ts = NULL;
1070 code = cmd_Parse(argc, argv, &ts);
1075 * Before calling the beforeProc and afterProc and all the implications
1076 * from those calls, check if the help procedure was called and call it
1079 if ((ts->proc == HelpProc) || (ts->proc == AproposProc)) {
1080 code = (*ts->proc) (ts, ts->rock);
1084 /* Now, we just call the procedure and return */
1086 code = (*beforeProc) (ts, beforeRock);
1091 code = (*ts->proc) (ts, ts->rock);
1094 (*afterProc) (ts, afterRock);
1096 cmd_FreeOptions(&ts);
1101 cmd_FreeOptions(struct cmd_syndesc **ts)
1109 /* free token list returned by parseLine */
1111 FreeTokens(struct cmd_token *alist)
1113 struct cmd_token *nlist;
1114 for (; alist; alist = nlist) {
1115 nlist = alist->next;
1122 /* free an argv list returned by parseline */
1124 cmd_FreeArgv(char **argv)
1127 for (tp = *argv; tp; argv++, tp = *argv)
1132 /* copy back the arg list to the argv array, freeing the cmd_tokens as you go;
1133 * the actual data is still malloc'd, and will be freed when the caller calls
1134 * cmd_FreeArgv later on
1138 CopyBackArgs(struct cmd_token *alist, char **argv,
1139 afs_int32 * an, afs_int32 amaxn)
1141 struct cmd_token *next;
1147 *argv = (char *)malloc(strlen(INITSTR) + 1);
1149 strcpy(*argv, INITSTR);
1155 return CMD_TOOMANY; /* argv is too small for his many parms. */
1164 *argv = NULL; /* use last slot for terminating null */
1165 /* don't count terminating null */
1173 if (x == '"' || x == 39 /* single quote */ )
1182 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
1189 cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
1193 int inToken, inQuote;
1194 struct cmd_token *first, *last;
1195 struct cmd_token *ttok;
1198 inToken = 0; /* not copying token chars at start */
1201 inQuote = 0; /* not in a quoted string */
1204 if (tc == 0 || (!inQuote && space(tc))) { /* terminating null gets us in here, too */
1206 inToken = 0; /* end of this token */
1208 return -1; /* should never get here */
1211 ttok = malloc(sizeof(struct cmd_token));
1214 ttok->key = malloc(strlen(tbuffer) + 1);
1216 strcpy(ttok->key, tbuffer);
1226 /* an alpha character */
1231 if (tptr - tbuffer >= sizeof(tbuffer)) {
1233 return CMD_TOOBIG; /* token too long */
1236 /* hit a quote, toggle inQuote flag but don't insert character */
1239 /* insert character */
1244 /* last token flushed 'cause space(0) --> true */
1247 return CopyBackArgs(first, argv, an, amaxn);
1252 /* Read a string in from our configuration file. This checks in
1253 * multiple places within this file - firstly in the section
1254 * [command_subcommand], then in [command], then in [subcommand]
1256 * Returns CMD_MISSING if there is no configuration file configured,
1257 * or if the file doesn't contain information for the specified option
1258 * in any of these sections.
1262 _get_file_string(struct cmd_syndesc *syn, int pos, const char **str)
1267 /* Nothing on the command line, try the config file if we have one */
1268 if (globalConfig == NULL)
1271 /* March past any leading -'s */
1272 for (optionName = syn->parms[pos].name;
1273 *optionName == '-'; optionName++);
1275 /* First, try the command_subcommand form */
1276 if (syn->name != NULL && commandName != NULL) {
1277 asprintf(§ion, "%s_%s", commandName, syn->name);
1278 *str = cmd_RawConfigGetString(globalConfig, NULL, section,
1285 /* Then, try the command form */
1286 if (commandName != NULL) {
1287 *str = cmd_RawConfigGetString(globalConfig, NULL, commandName,
1293 /* Then, the defaults section */
1294 *str = cmd_RawConfigGetString(globalConfig, NULL, "defaults",
1299 /* Nothing there, return MISSING */
1304 _get_config_string(struct cmd_syndesc *syn, int pos, const char **str)
1308 if (pos > syn->nParms)
1309 return CMD_EXCESSPARMS;
1311 /* It's a flag, they probably shouldn't be using this interface to access
1312 * it, but don't blow up for now */
1313 if (syn->parms[pos].items == &dummy)
1316 /* We have a value on the command line - this overrides anything in the
1317 * configuration file */
1318 if (syn->parms[pos].items != NULL &&
1319 syn->parms[pos].items->data != NULL) {
1320 *str = syn->parms[pos].items->data;
1324 return _get_file_string(syn, pos, str);
1328 cmd_OptionAsInt(struct cmd_syndesc *syn, int pos, int *value)
1333 code =_get_config_string(syn, pos, &str);
1340 *value = strtol(str, NULL, 10);
1346 cmd_OptionAsUint(struct cmd_syndesc *syn, int pos,
1347 unsigned int *value)
1352 code = _get_config_string(syn, pos, &str);
1359 *value = strtoul(str, NULL, 10);
1365 cmd_OptionAsString(struct cmd_syndesc *syn, int pos, char **value)
1373 code = _get_config_string(syn, pos, &str);
1380 *value = strdup(str);
1386 cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **value)
1389 struct cmd_item *item, **last;
1390 const char *start, *end;
1393 if (pos > syn->nParms)
1394 return CMD_EXCESSPARMS;
1396 /* If we have a list already, just return the existing list */
1397 if (syn->parms[pos].items != NULL) {
1398 *value = syn->parms[pos].items;
1402 code = _get_file_string(syn, pos, &str);
1406 /* Use strchr to split str into elements, and build a recursive list
1407 * from them. Hang this list off the configuration structure, so that
1408 * it is returned by any future calls to this function, and is freed
1409 * along with everything else when the syntax description is freed
1411 last = &syn->parms[pos].items;
1413 while ((end = strchr(start, ' '))) {
1414 item = calloc(1, sizeof(struct cmd_item));
1415 item->data = malloc(end - start + 1);
1416 strlcpy(item->data, start, end - start + 1);
1419 for (start = end; *start == ' '; start++); /* skip any whitespace */
1422 /* Catch the final element */
1423 if (*start != '\0') {
1424 item = calloc(1, sizeof(struct cmd_item));
1425 item->data = malloc(strlen(start) + 1);
1426 strlcpy(item->data, start, strlen(start) + 1);
1430 *value = syn->parms[pos].items;
1436 cmd_OptionAsFlag(struct cmd_syndesc *syn, int pos, int *value)
1438 const char *str = NULL;
1441 code = _get_config_string(syn, pos, &str);
1446 strcasecmp(str, "yes") == 0 ||
1447 strcasecmp(str, "true") == 0 ||
1457 cmd_OptionPresent(struct cmd_syndesc *syn, int pos)
1462 code = _get_config_string(syn, pos, &str);
1470 cmd_OpenConfigFile(const char *file)
1473 cmd_RawConfigFileFree(globalConfig);
1474 globalConfig = NULL;
1477 return cmd_RawConfigParseFile(file, &globalConfig);
1481 cmd_SetCommandName(const char *command)
1483 commandName = command;
1486 const cmd_config_section *
1489 return globalConfig;
1492 const cmd_config_section *
1493 cmd_RawSection(void)
1495 if (globalConfig == NULL || commandName == NULL)
1498 return cmd_RawConfigGetList(globalConfig, commandName, NULL);