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 void *beforeRock, *afterRock;
33 static char initcmd_opcode[] = "initcmd"; /*Name of initcmd opcode */
35 /* take name and string, and return null string if name is empty, otherwise return
36 the concatenation of the two strings */
38 NName(char *a1, char *a2)
40 static char tbuffer[300];
41 if (strlen(a1) == 0) {
44 strncpy(tbuffer, a1, sizeof(tbuffer));
45 strncat(tbuffer, a2, sizeof(tbuffer));
46 tbuffer[sizeof(tbuffer)-1]='\0';
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)
78 /* Allow --long-style options. */
79 if (aname[0] == '-' && aname[1] == '-' && aname[2] && aname[3]) {
83 cmdlen = strlen(aname);
86 for (i = 0; i < CMD_MAXPARMS; i++) {
87 if (as->parms[i].type == 0)
88 continue; /* this slot not set (seeked over) */
89 if (strcmp(as->parms[i].name, aname) == 0)
91 if (strlen(as->parms[i].name) < cmdlen)
93 /* A hidden option must be a full match (no best matches) */
94 if (as->parms[i].flags & CMD_HIDE)
97 if (strncmp(as->parms[i].name, aname, cmdlen) == 0) {
104 return (ambig ? -1 : best);
107 static struct cmd_syndesc *
108 FindSyntax(char *aname, int *aambig)
110 struct cmd_syndesc *ts;
111 struct cmd_syndesc *best;
115 cmdLen = strlen(aname);
116 best = (struct cmd_syndesc *)0;
119 *aambig = 0; /* initialize to unambiguous */
120 for (ts = allSyntax; ts; ts = ts->next) {
121 if (strcmp(aname, ts->name) == 0)
123 if (strlen(ts->name) < cmdLen)
124 continue; /* we typed more than item has */
125 /* A hidden command must be a full match (no best matches) */
126 if (ts->flags & CMD_HIDDEN)
129 /* This is just an alias for *best, or *best is just an alias for us.
130 * If we don't make this check explicitly, then an alias which is just a
131 * short prefix of the real command's name might make things ambiguous
132 * for no apparent reason.
134 if (best && ts->aliasOf == best->aliasOf)
136 if (strncmp(ts->name, aname, cmdLen) == 0) {
138 ambig = 1; /* ambiguous name */
145 *aambig = ambig; /* if ambiguous and they care, tell them */
146 return (struct cmd_syndesc *)0; /* fails */
148 return best; /* otherwise its not ambiguous, and they know */
151 /* print the help for a single parameter */
153 PrintParmHelp(struct cmd_parmdesc *aparm)
155 if (aparm->type == CMD_FLAG) {
157 /* doc people don't like seeing this information */
159 printf(" (%s)", aparm->help);
161 } else if (aparm->help) {
162 printf(" <%s>", aparm->help);
163 if (aparm->type == CMD_LIST)
165 } else if (aparm->type == CMD_SINGLE)
167 else if (aparm->type == CMD_LIST)
171 extern char *AFSVersion;
174 VersionProc(struct cmd_syndesc *as, void *arock)
176 printf("%s\n", AFSVersion);
181 PrintSyntax(struct cmd_syndesc *as)
184 struct cmd_parmdesc *tp;
186 /* now print usage, from syntax table */
188 printf("Usage: %s", as->a0name);
190 if (!strcmp(as->name, initcmd_opcode))
191 printf("Usage: %s[%s]", NName(as->a0name, " "), as->name);
193 printf("Usage: %s%s", NName(as->a0name, " "), as->name);
196 for (i = 0; i < CMD_MAXPARMS; i++) {
199 continue; /* seeked over slot */
200 if (tp->flags & CMD_HIDE)
201 continue; /* skip hidden options */
203 if (tp->flags & CMD_OPTIONAL)
205 printf("%s", tp->name);
207 if (tp->flags & CMD_OPTIONAL)
213 /* must print newline in any case, to terminate preceding line */
215 PrintAliases(struct cmd_syndesc *as)
217 struct cmd_syndesc *ts;
219 if (as->flags & CMD_ALIAS) {
221 printf("(alias for %s)\n", ts->name);
225 return; /* none, print nothing */
227 for (as = as->nextAlias; as; as = as->nextAlias) {
228 printf("%s ", as->name);
235 PrintFlagHelp(struct cmd_syndesc *as)
238 struct cmd_parmdesc *tp;
242 /* find flag name length */
244 for (i = 0; i < CMD_MAXPARMS; i++) {
245 if (i == CMD_HELPPARM)
248 if (tp->type != CMD_FLAG)
250 if (tp->flags & CMD_HIDE)
251 continue; /* skip hidden options */
255 if (strlen(tp->name) > flag_width)
256 flag_width = strlen(tp->name);
259 /* print flag help */
260 flag_prefix = "Where:";
261 for (i = 0; i < CMD_MAXPARMS; i++) {
262 if (i == CMD_HELPPARM)
265 if (tp->type != CMD_FLAG)
267 if (tp->flags & CMD_HIDE)
268 continue; /* skip hidden options */
272 printf("%-7s%-*s %s\n", flag_prefix, flag_width, tp->name, tp->help);
278 AproposProc(struct cmd_syndesc *as, void *arock)
280 struct cmd_syndesc *ts;
285 tsub = as->parms[0].items->data;
286 for (ts = allSyntax; ts; ts = ts->next) {
287 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
289 if (SubString(ts->help, tsub)) {
290 printf("%s: %s\n", ts->name, ts->help);
292 } else if (SubString(ts->name, tsub)) {
293 printf("%s: %s\n", ts->name, ts->help);
298 printf("Sorry, no commands found\n");
303 HelpProc(struct cmd_syndesc *as, void *arock)
305 struct cmd_syndesc *ts;
310 if (as->parms[0].items == 0) {
311 printf("%sCommands are:\n", NName(as->a0name, ": "));
312 for (ts = allSyntax; ts; ts = ts->next) {
313 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
315 printf("%-15s %s\n", ts->name, (ts->help ? ts->help : ""));
318 /* print out individual help topics */
319 for (ti = as->parms[0].items; ti; ti = ti->next) {
321 ts = FindSyntax(ti->data, &ambig);
322 if (ts && (ts->flags & CMD_HIDDEN))
323 ts = 0; /* no hidden commands */
325 /* print out command name and help */
326 printf("%s%s: %s ", NName(as->a0name, " "), ts->name,
327 (ts->help ? ts->help : ""));
328 ts->a0name = as->a0name;
334 fprintf(stderr, "%sUnknown topic '%s'\n",
335 NName(as->a0name, ": "), ti->data);
337 /* ambiguous, list 'em all */
339 "%sAmbiguous topic '%s'; use 'apropos' to list\n",
340 NName(as->a0name, ": "), ti->data);
342 code = CMD_UNKNOWNCMD;
350 cmd_SetBeforeProc(int (*aproc) (struct cmd_syndesc * ts, void *beforeRock),
359 cmd_SetAfterProc(int (*aproc) (struct cmd_syndesc * ts, void *afterRock),
367 /* thread on list in alphabetical order */
369 SortSyntax(struct cmd_syndesc *as)
371 struct cmd_syndesc **ld, *ud;
373 for (ld = &allSyntax, ud = *ld; ud; ld = &ud->next, ud = *ld) {
374 if (strcmp(ud->name, as->name) > 0) { /* next guy is bigger than us */
378 /* thread us on the list now */
385 cmd_CreateSyntax(char *aname,
386 int (*aproc) (struct cmd_syndesc * ts, void *arock),
387 void *arock, char *ahelp)
389 struct cmd_syndesc *td;
391 /* can't have two cmds in no opcode mode */
395 td = calloc(1, sizeof(struct cmd_syndesc));
397 td->aliasOf = td; /* treat aliasOf as pointer to real command, no matter what */
399 /* copy in name, etc */
401 td->name = malloc(strlen(aname) + 1);
403 strcpy(td->name, aname);
409 /* Piggy-back the hidden option onto the help string */
410 if (ahelp == (char *)CMD_HIDDEN) {
411 td->flags |= CMD_HIDDEN;
413 td->help = malloc(strlen(ahelp) + 1);
415 strcpy(td->help, ahelp);
424 cmd_Seek(td, CMD_HELPPARM);
425 cmd_AddParm(td, "-help", CMD_FLAG, CMD_OPTIONAL, "get detailed help");
432 cmd_CreateAlias(struct cmd_syndesc *as, char *aname)
434 struct cmd_syndesc *td;
436 td = malloc(sizeof(struct cmd_syndesc));
438 memcpy(td, as, sizeof(struct cmd_syndesc));
439 td->name = malloc(strlen(aname) + 1);
441 strcpy(td->name, aname);
442 td->flags |= CMD_ALIAS;
443 /* if ever free things, make copy of help string, too */
448 /* thread on alias lists */
449 td->nextAlias = as->nextAlias;
453 return 0; /* all done */
457 cmd_DisablePositionalCommands(void)
459 enablePositional = 0;
463 cmd_IsAdministratorCommand(struct cmd_syndesc *as)
465 as->flags |= CMD_ADMIN;
470 cmd_Seek(struct cmd_syndesc *as, int apos)
472 if (apos >= CMD_MAXPARMS)
473 return CMD_EXCESSPARMS;
479 cmd_AddParm(struct cmd_syndesc *as, char *aname, int atype,
480 afs_int32 aflags, char *ahelp)
482 struct cmd_parmdesc *tp;
484 if (as->nParms >= CMD_MAXPARMS)
485 return CMD_EXCESSPARMS;
486 tp = &as->parms[as->nParms++];
488 tp->name = malloc(strlen(aname) + 1);
490 strcpy(tp->name, aname);
495 tp->help = malloc(strlen(ahelp) + 1);
497 strcpy(tp->help, ahelp);
503 /* add a text item to the end of the parameter list */
505 AddItem(struct cmd_parmdesc *aparm, char *aval)
507 struct cmd_item *ti, *ni;
508 ti = calloc(1, sizeof(struct cmd_item));
510 ti->data = malloc(strlen(aval) + 1);
512 strcpy(ti->data, aval);
513 /* now put ti at the *end* of the list */
514 if ((ni = aparm->items)) {
515 for (; ni; ni = ni->next)
517 break; /* skip to last one */
520 aparm->items = ti; /* we're first */
524 /* skip to next non-flag item, if any */
526 AdvanceType(struct cmd_syndesc *as, afs_int32 aval)
529 struct cmd_parmdesc *tp;
531 /* first see if we should try to grab rest of line for this dude */
532 if (as->parms[aval].flags & CMD_EXPANDS)
535 /* if not, find next non-flag used slot */
536 for (next = aval + 1; next < CMD_MAXPARMS; next++) {
537 tp = &as->parms[next];
538 if (tp->type != 0 && tp->type != CMD_FLAG)
544 /* discard parameters filled in by dispatch */
546 ResetSyntax(struct cmd_syndesc *as)
549 struct cmd_parmdesc *tp;
550 struct cmd_item *ti, *ni;
553 for (i = 0; i < CMD_MAXPARMS; i++, tp++) {
557 /* free whole list in both cases, just for fun */
558 for (ti = tp->items; ti; ti = ni) {
572 /* move the expands flag to the last one in the list */
574 SetupExpandsFlag(struct cmd_syndesc *as)
576 struct cmd_parmdesc *tp;
580 /* find last CMD_LIST type parameter, optional or not, and make it expandable
581 * if no other dude is expandable */
582 for (i = 0; i < CMD_MAXPARMS; i++) {
584 if (tp->type == CMD_LIST) {
585 if (tp->flags & CMD_EXPANDS)
586 return 0; /* done if already specified */
591 as->parms[last].flags |= CMD_EXPANDS;
595 /* Take the current argv & argc and alter them so that the initialization
596 * opcode is made to appear. This is used in cases where the initialization
597 * opcode is implicitly invoked.*/
599 InsertInitOpcode(int *aargc, char **aargv)
601 char **newargv; /*Ptr to new, expanded argv space */
602 char *pinitopcode; /*Ptr to space for name of init opcode */
603 int i; /*Loop counter */
605 /* Allocate the new argv array, plus one for the new opcode, plus one
606 * more for the trailing null pointer */
607 newargv = malloc(((*aargc) + 2) * sizeof(char *));
609 fprintf(stderr, "%s: Can't create new argv array with %d+2 slots\n",
614 /* Create space for the initial opcode & fill it in */
615 pinitopcode = malloc(sizeof(initcmd_opcode));
617 fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]);
621 strcpy(pinitopcode, initcmd_opcode);
623 /* Move all the items in the old argv into the new argv, in their
625 for (i = *aargc; i > 1; i--)
626 newargv[i] = aargv[i - 1];
628 /* Slip in the opcode and the trailing null pointer, and bump the
629 * argument count up by one for the new opcode */
630 newargv[0] = aargv[0];
631 newargv[1] = pinitopcode;
633 newargv[*aargc] = NULL;
635 /* Return the happy news */
638 } /*InsertInitOpcode */
641 NoParmsOK(struct cmd_syndesc *as)
644 struct cmd_parmdesc *td;
646 for (i = 0; i < CMD_MAXPARMS; i++) {
648 if (td->type != 0 && !(td->flags & CMD_OPTIONAL)) {
649 /* found a non-optional (e.g. required) parm, so NoParmsOK
650 * is false (some parms are required) */
657 /* Call the appropriate function, or return syntax error code. Note: if
658 * no opcode is specified, an initialization routine exists, and it has
659 * NOT been called before, we invoke the special initialization opcode
662 cmd_Dispatch(int argc, char **argv)
665 struct cmd_syndesc *ts;
666 struct cmd_parmdesc *tparm;
671 static int initd = 0; /*Is this the first time this routine has been called? */
672 static int initcmdpossible = 1; /*Should be consider parsing the initial command? */
676 /* Add help, apropos commands once */
678 ts = cmd_CreateSyntax("help", HelpProc, NULL,
679 "get help on commands");
680 cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
681 cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, NULL);
683 ts = cmd_CreateSyntax("apropos", AproposProc, NULL,
684 "search by help text");
685 cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED,
687 ts = cmd_CreateSyntax("version", VersionProc, NULL,
689 ts = cmd_CreateSyntax("-version", VersionProc, NULL,
691 ts = cmd_CreateSyntax("-help", HelpProc, NULL,
693 ts = cmd_CreateSyntax("--version", VersionProc, NULL,
695 ts = cmd_CreateSyntax("--help", HelpProc, NULL,
700 /*Remember the program name */
705 if (!NoParmsOK(allSyntax)) {
706 printf("%s: Type '%s -help' for help\n", pname, pname);
712 /* if there is an initcmd, don't print an error message, just
713 * setup to use the initcmd below. */
714 if (!(initcmdpossible && FindSyntax(initcmd_opcode, (int *)0))) {
715 printf("%s: Type '%s help' or '%s help <topic>' for help\n",
716 pname, pname, pname);
722 /* Find the syntax descriptor for this command, doing prefix matching properly */
726 ts = (argc < 2 ? 0 : FindSyntax(argv[1], &ambig));
728 /*First token doesn't match a syntax descriptor */
729 if (initcmdpossible) {
730 /*If initial command line handling hasn't been done yet,
731 * see if there is a descriptor for the initialization opcode.
732 * Only try this once. */
734 ts = FindSyntax(initcmd_opcode, (int *)0);
736 /*There is no initialization opcode available, so we declare
739 fprintf(stderr, "%s", NName(pname, ": "));
741 "Ambiguous operation '%s'; type '%shelp' for list\n",
742 argv[1], NName(pname, " "));
744 fprintf(stderr, "%s", NName(pname, ": "));
746 "Unrecognized operation '%s'; type '%shelp' for list\n",
747 argv[1], NName(pname, " "));
749 return (CMD_UNKNOWNCMD);
751 /*Found syntax structure for an initialization opcode. Fix
752 * up argv and argc to relect what the user
753 * ``should have'' typed */
754 if (!(argv = InsertInitOpcode(&argc, argv))) {
756 "%sCan't insert implicit init opcode into command line\n",
758 return (CMD_INTERNALERROR);
761 } /*Initial opcode not yet attempted */
763 /* init cmd already run and no syntax entry found */
765 fprintf(stderr, "%s", NName(pname, ": "));
767 "Ambiguous operation '%s'; type '%shelp' for list\n",
768 argv[1], NName(pname, " "));
770 fprintf(stderr, "%s", NName(pname, ": "));
772 "Unrecognized operation '%s'; type '%shelp' for list\n",
773 argv[1], NName(pname, " "));
775 return CMD_UNKNOWNCMD;
777 } /*Argv[1] is not a valid opcode */
778 } /*Opcodes are defined */
780 /* Found the descriptor; start parsing. curType is the type we're
784 /* We start off parsing in "positional" mode, where tokens are put in
785 * slots positionally. If we find a name that takes args, we go
786 * out of positional mode, and from that point on, expect a switch
787 * before any particular token. */
789 positional = enablePositional; /* Accepting positional cmds ? */
790 i = noOpcodes ? 1 : 2;
791 SetupExpandsFlag(ts);
792 for (; i < argc; i++) {
793 /* Only tokens that start with a hyphen and are not followed by a digit
794 * are considered switches. This allow negative numbers. */
795 if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
797 j = FindType(ts, argv[i]);
800 "%sUnrecognized or ambiguous switch '%s'; type ",
801 NName(pname, ": "), argv[i]);
803 fprintf(stderr, "'%s -help' for detailed help\n",
806 fprintf(stderr, "'%shelp %s' for detailed help\n",
807 NName(argv[0], " "), ts->name);
809 return (CMD_UNKNOWNSWITCH);
811 if (j >= CMD_MAXPARMS) {
812 fprintf(stderr, "%sInternal parsing error\n",
815 return (CMD_INTERNALERROR);
817 if (ts->parms[j].type == CMD_FLAG) {
818 ts->parms[j].items = &dummy;
822 ts->parms[j].flags |= CMD_PROCESSED;
825 /* Try to fit in this descr */
826 if (curType >= CMD_MAXPARMS) {
827 fprintf(stderr, "%sToo many arguments\n", NName(pname, ": "));
829 return (CMD_TOOMANY);
831 tparm = &ts->parms[curType];
833 if ((tparm->type == 0) || /* No option in this slot */
834 (tparm->type == CMD_FLAG)) { /* A flag (not an argument */
835 /* skipped parm slot */
836 curType++; /* Skip this slot and reprocess this parm */
841 if (!(tparm->flags & CMD_PROCESSED) && (tparm->flags & CMD_HIDE)) {
842 curType++; /* Skip this slot and reprocess this parm */
847 if (tparm->type == CMD_SINGLE) {
849 fprintf(stderr, "%sToo many values after switch %s\n",
850 NName(pname, ": "), tparm->name);
852 return (CMD_NOTLIST);
854 AddItem(tparm, argv[i]); /* Add to end of list */
855 } else if (tparm->type == CMD_LIST) {
856 AddItem(tparm, argv[i]); /* Add to end of list */
858 /* Now, if we're in positional mode, advance to the next item */
860 curType = AdvanceType(ts, curType);
864 /* keep track of this for messages */
865 ts->a0name = argv[0];
867 /* If we make it here, all the parameters are filled in. Check to see if
868 * this is a -help version. Must do this before checking for all
869 * required parms, otherwise it is a real nuisance */
870 if (ts->parms[CMD_HELPPARM].items) {
872 /* Display full help syntax if we don't have subcommands */
879 /* Parsing done, see if we have all of our required parameters */
880 for (i = 0; i < CMD_MAXPARMS; i++) {
881 tparm = &ts->parms[i];
882 if (tparm->type == 0)
883 continue; /* Skipped parm slot */
884 if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
885 fprintf(stderr, "%s The field '%s' isn't completed properly\n",
886 NName(pname, ": "), tparm->name);
888 tparm->flags &= ~CMD_PROCESSED;
891 if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
892 fprintf(stderr, "%sMissing required parameter '%s'\n",
893 NName(pname, ": "), tparm->name);
895 tparm->flags &= ~CMD_PROCESSED;
898 tparm->flags &= ~CMD_PROCESSED;
902 * Before calling the beforeProc and afterProc and all the implications
903 * from those calls, check if the help procedure was called and call it
906 if ((ts->proc == HelpProc) || (ts->proc == AproposProc)) {
907 i = (*ts->proc) (ts, ts->rock);
912 /* Now, we just call the procedure and return */
914 i = (*beforeProc) (ts, beforeRock);
921 i = (*ts->proc) (ts, ts->rock);
923 (*afterProc) (ts, afterRock);
924 ResetSyntax(ts); /* Reset and free things */
928 /* free token list returned by parseLine */
930 FreeTokens(struct cmd_token *alist)
932 struct cmd_token *nlist;
933 for (; alist; alist = nlist) {
941 /* free an argv list returned by parseline */
943 cmd_FreeArgv(char **argv)
946 for (tp = *argv; tp; argv++, tp = *argv)
951 /* copy back the arg list to the argv array, freeing the cmd_tokens as you go;
952 * the actual data is still malloc'd, and will be freed when the caller calls
953 * cmd_FreeArgv later on
957 CopyBackArgs(struct cmd_token *alist, char **argv,
958 afs_int32 * an, afs_int32 amaxn)
960 struct cmd_token *next;
966 *argv = (char *)malloc(strlen(INITSTR) + 1);
968 strcpy(*argv, INITSTR);
974 return CMD_TOOMANY; /* argv is too small for his many parms. */
983 *(argv++) = 0; /* use last slot for terminating null */
984 /* don't count terminating null */
992 if (x == '"' || x == 39 /* single quote */ )
1001 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
1008 cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
1012 int inToken, inQuote;
1013 struct cmd_token *first, *last;
1014 struct cmd_token *ttok;
1017 inToken = 0; /* not copying token chars at start */
1020 inQuote = 0; /* not in a quoted string */
1023 if (tc == 0 || (!inQuote && space(tc))) { /* terminating null gets us in here, too */
1025 inToken = 0; /* end of this token */
1027 return -1; /* should never get here */
1030 ttok = malloc(sizeof(struct cmd_token));
1033 ttok->key = malloc(strlen(tbuffer) + 1);
1035 strcpy(ttok->key, tbuffer);
1045 /* an alpha character */
1050 if (tptr - tbuffer >= sizeof(tbuffer)) {
1052 return CMD_TOOBIG; /* token too long */
1055 /* hit a quote, toggle inQuote flag but don't insert character */
1058 /* insert character */
1063 /* last token flushed 'cause space(0) --> true */
1066 return CopyBackArgs(first, argv, an, amaxn);