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 void *beforeRock, *afterRock;
32 static char initcmd_opcode[] = "initcmd"; /*Name of initcmd opcode */
34 /* take name and string, and return null string if name is empty, otherwise return
35 the concatenation of the two strings */
37 NName(char *a1, char *a2)
39 static char tbuffer[300];
40 if (strlen(a1) == 0) {
43 strncpy(tbuffer, a1, sizeof(tbuffer));
44 strncat(tbuffer, a2, sizeof(tbuffer));
45 tbuffer[sizeof(tbuffer)-1]='\0';
50 /* return true if asub is a substring of amain */
52 SubString(char *amain, char *asub)
56 mlen = (int) strlen(amain);
57 slen = (int) strlen(asub);
60 return 0; /* not a substring */
61 for (i = 0; i <= j; i++) {
62 if (strncmp(amain, asub, slen) == 0)
66 return 0; /* didn't find it */
70 FindType(struct cmd_syndesc *as, char *aname)
77 /* Allow --long-style options. */
78 if (aname[0] == '-' && aname[1] == '-' && aname[2] && aname[3]) {
82 cmdlen = strlen(aname);
85 for (i = 0; i < CMD_MAXPARMS; i++) {
86 if (as->parms[i].type == 0)
87 continue; /* this slot not set (seeked over) */
88 if (strcmp(as->parms[i].name, aname) == 0)
90 if (strlen(as->parms[i].name) < cmdlen)
92 /* A hidden option must be a full match (no best matches) */
93 if (as->parms[i].flags & CMD_HIDE)
96 if (strncmp(as->parms[i].name, aname, cmdlen) == 0) {
103 return (ambig ? -1 : best);
106 static struct cmd_syndesc *
107 FindSyntax(char *aname, int *aambig)
109 struct cmd_syndesc *ts;
110 struct cmd_syndesc *best;
114 cmdLen = strlen(aname);
115 best = (struct cmd_syndesc *)0;
118 *aambig = 0; /* initialize to unambiguous */
119 for (ts = allSyntax; ts; ts = ts->next) {
120 if (strcmp(aname, ts->name) == 0)
122 if (strlen(ts->name) < cmdLen)
123 continue; /* we typed more than item has */
124 /* A hidden command must be a full match (no best matches) */
125 if (ts->flags & CMD_HIDDEN)
128 /* This is just an alias for *best, or *best is just an alias for us.
129 * If we don't make this check explicitly, then an alias which is just a
130 * short prefix of the real command's name might make things ambiguous
131 * for no apparent reason.
133 if (best && ts->aliasOf == best->aliasOf)
135 if (strncmp(ts->name, aname, cmdLen) == 0) {
137 ambig = 1; /* ambiguous name */
144 *aambig = ambig; /* if ambiguous and they care, tell them */
145 return (struct cmd_syndesc *)0; /* fails */
147 return best; /* otherwise its not ambiguous, and they know */
150 /* print the help for a single parameter */
152 PrintParmHelp(struct cmd_parmdesc *aparm)
154 if (aparm->type == CMD_FLAG) {
156 /* doc people don't like seeing this information */
158 printf(" (%s)", aparm->help);
160 } else if (aparm->help) {
161 printf(" <%s>", aparm->help);
162 if (aparm->type == CMD_LIST)
164 } else if (aparm->type == CMD_SINGLE)
166 else if (aparm->type == CMD_LIST)
170 extern char *AFSVersion;
173 VersionProc(struct cmd_syndesc *as, void *arock)
175 printf("%s\n", AFSVersion);
180 PrintSyntax(struct cmd_syndesc *as)
183 struct cmd_parmdesc *tp;
185 /* now print usage, from syntax table */
187 printf("Usage: %s", as->a0name);
189 if (!strcmp(as->name, initcmd_opcode))
190 printf("Usage: %s[%s]", NName(as->a0name, " "), as->name);
192 printf("Usage: %s%s", NName(as->a0name, " "), as->name);
195 for (i = 0; i < CMD_MAXPARMS; i++) {
198 continue; /* seeked over slot */
199 if (tp->flags & CMD_HIDE)
200 continue; /* skip hidden options */
202 if (tp->flags & CMD_OPTIONAL)
204 printf("%s", tp->name);
206 if (tp->flags & CMD_OPTIONAL)
212 /* must print newline in any case, to terminate preceding line */
214 PrintAliases(struct cmd_syndesc *as)
216 struct cmd_syndesc *ts;
218 if (as->flags & CMD_ALIAS) {
220 printf("(alias for %s)\n", ts->name);
224 return; /* none, print nothing */
226 for (as = as->nextAlias; as; as = as->nextAlias) {
227 printf("%s ", as->name);
234 PrintFlagHelp(struct cmd_syndesc *as)
237 struct cmd_parmdesc *tp;
241 /* find flag name length */
243 for (i = 0; i < CMD_MAXPARMS; i++) {
244 if (i == CMD_HELPPARM)
247 if (tp->type != CMD_FLAG)
249 if (tp->flags & CMD_HIDE)
250 continue; /* skip hidden options */
254 if (strlen(tp->name) > flag_width)
255 flag_width = strlen(tp->name);
258 /* print flag help */
259 flag_prefix = "Where:";
260 for (i = 0; i < CMD_MAXPARMS; i++) {
261 if (i == CMD_HELPPARM)
264 if (tp->type != CMD_FLAG)
266 if (tp->flags & CMD_HIDE)
267 continue; /* skip hidden options */
271 printf("%-7s%-*s %s\n", flag_prefix, flag_width, tp->name, tp->help);
277 AproposProc(struct cmd_syndesc *as, void *arock)
279 struct cmd_syndesc *ts;
284 tsub = as->parms[0].items->data;
285 for (ts = allSyntax; ts; ts = ts->next) {
286 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
288 if (SubString(ts->help, tsub)) {
289 printf("%s: %s\n", ts->name, ts->help);
291 } else if (SubString(ts->name, tsub)) {
292 printf("%s: %s\n", ts->name, ts->help);
297 printf("Sorry, no commands found\n");
302 HelpProc(struct cmd_syndesc *as, void *arock)
304 struct cmd_syndesc *ts;
309 if (as->parms[0].items == 0) {
310 printf("%sCommands are:\n", NName(as->a0name, ": "));
311 for (ts = allSyntax; ts; ts = ts->next) {
312 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
314 printf("%-15s %s\n", ts->name, (ts->help ? ts->help : ""));
317 /* print out individual help topics */
318 for (ti = as->parms[0].items; ti; ti = ti->next) {
320 ts = FindSyntax(ti->data, &ambig);
321 if (ts && (ts->flags & CMD_HIDDEN))
322 ts = 0; /* no hidden commands */
324 /* print out command name and help */
325 printf("%s%s: %s ", NName(as->a0name, " "), ts->name,
326 (ts->help ? ts->help : ""));
327 ts->a0name = as->a0name;
333 fprintf(stderr, "%sUnknown topic '%s'\n",
334 NName(as->a0name, ": "), ti->data);
336 /* ambiguous, list 'em all */
338 "%sAmbiguous topic '%s'; use 'apropos' to list\n",
339 NName(as->a0name, ": "), ti->data);
341 code = CMD_UNKNOWNCMD;
349 cmd_SetBeforeProc(int (*aproc) (struct cmd_syndesc * ts, void *beforeRock),
358 cmd_SetAfterProc(int (*aproc) (struct cmd_syndesc * ts, void *afterRock),
366 /* thread on list in alphabetical order */
368 SortSyntax(struct cmd_syndesc *as)
370 struct cmd_syndesc **ld, *ud;
372 for (ld = &allSyntax, ud = *ld; ud; ld = &ud->next, ud = *ld) {
373 if (strcmp(ud->name, as->name) > 0) { /* next guy is bigger than us */
377 /* thread us on the list now */
384 cmd_CreateSyntax(char *aname,
385 int (*aproc) (struct cmd_syndesc * ts, void *arock),
386 void *arock, char *ahelp)
388 struct cmd_syndesc *td;
390 /* can't have two cmds in no opcode mode */
392 return (struct cmd_syndesc *)0;
394 td = (struct cmd_syndesc *)calloc(1, sizeof(struct cmd_syndesc));
396 td->aliasOf = td; /* treat aliasOf as pointer to real command, no matter what */
398 /* copy in name, etc */
400 td->name = (char *)malloc(strlen(aname) + 1);
402 strcpy(td->name, aname);
408 /* Piggy-back the hidden option onto the help string */
409 if (ahelp == (char *)CMD_HIDDEN) {
410 td->flags |= CMD_HIDDEN;
412 td->help = (char *)malloc(strlen(ahelp) + 1);
414 strcpy(td->help, ahelp);
423 cmd_Seek(td, CMD_HELPPARM);
424 cmd_AddParm(td, "-help", CMD_FLAG, CMD_OPTIONAL, "get detailed help");
431 cmd_CreateAlias(struct cmd_syndesc *as, char *aname)
433 struct cmd_syndesc *td;
435 td = (struct cmd_syndesc *)malloc(sizeof(struct cmd_syndesc));
437 memcpy(td, as, sizeof(struct cmd_syndesc));
438 td->name = (char *)malloc(strlen(aname) + 1);
440 strcpy(td->name, aname);
441 td->flags |= CMD_ALIAS;
442 /* if ever free things, make copy of help string, too */
447 /* thread on alias lists */
448 td->nextAlias = as->nextAlias;
452 return 0; /* all done */
456 cmd_IsAdministratorCommand(struct cmd_syndesc *as)
458 as->flags |= CMD_ADMIN;
463 cmd_Seek(struct cmd_syndesc *as, int apos)
465 if (apos >= CMD_MAXPARMS)
466 return CMD_EXCESSPARMS;
472 cmd_AddParm(struct cmd_syndesc *as, char *aname, int atype,
473 afs_int32 aflags, char *ahelp)
475 struct cmd_parmdesc *tp;
477 if (as->nParms >= CMD_MAXPARMS)
478 return CMD_EXCESSPARMS;
479 tp = &as->parms[as->nParms++];
481 tp->name = (char *)malloc(strlen(aname) + 1);
483 strcpy(tp->name, aname);
488 tp->help = (char *)malloc(strlen(ahelp) + 1);
490 strcpy(tp->help, ahelp);
496 /* add a text item to the end of the parameter list */
498 AddItem(struct cmd_parmdesc *aparm, char *aval)
500 struct cmd_item *ti, *ni;
501 ti = (struct cmd_item *)calloc(1, sizeof(struct cmd_item));
503 ti->data = (char *)malloc(strlen(aval) + 1);
505 strcpy(ti->data, aval);
506 /* now put ti at the *end* of the list */
507 if ((ni = aparm->items)) {
508 for (; ni; ni = ni->next)
510 break; /* skip to last one */
513 aparm->items = ti; /* we're first */
517 /* skip to next non-flag item, if any */
519 AdvanceType(struct cmd_syndesc *as, afs_int32 aval)
522 struct cmd_parmdesc *tp;
524 /* first see if we should try to grab rest of line for this dude */
525 if (as->parms[aval].flags & CMD_EXPANDS)
528 /* if not, find next non-flag used slot */
529 for (next = aval + 1; next < CMD_MAXPARMS; next++) {
530 tp = &as->parms[next];
531 if (tp->type != 0 && tp->type != CMD_FLAG)
537 /* discard parameters filled in by dispatch */
539 ResetSyntax(struct cmd_syndesc *as)
542 struct cmd_parmdesc *tp;
543 struct cmd_item *ti, *ni;
546 for (i = 0; i < CMD_MAXPARMS; i++, tp++) {
550 /* free whole list in both cases, just for fun */
551 for (ti = tp->items; ti; ti = ni) {
565 /* move the expands flag to the last one in the list */
567 SetupExpandsFlag(struct cmd_syndesc *as)
569 struct cmd_parmdesc *tp;
573 /* find last CMD_LIST type parameter, optional or not, and make it expandable
574 * if no other dude is expandable */
575 for (i = 0; i < CMD_MAXPARMS; i++) {
577 if (tp->type == CMD_LIST) {
578 if (tp->flags & CMD_EXPANDS)
579 return 0; /* done if already specified */
584 as->parms[last].flags |= CMD_EXPANDS;
588 /*Take the current argv & argc and alter them so that the initialization opcode is made to appear. This is used in cases where the initialization opcode is implicitly invoked.*/
590 InsertInitOpcode(int *aargc, char **aargv)
592 char **newargv; /*Ptr to new, expanded argv space */
593 char *pinitopcode; /*Ptr to space for name of init opcode */
594 int i; /*Loop counter */
596 /*Allocate the new argv array, plus one for the new opcode, plus one more for the trailing null pointer */
597 newargv = (char **)malloc(((*aargc) + 2) * sizeof(char *));
599 fprintf(stderr, "%s: Can't create new argv array with %d+2 slots\n",
604 /*Create space for the initial opcode & fill it in */
605 pinitopcode = (char *)malloc(sizeof(initcmd_opcode));
607 fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]);
611 strcpy(pinitopcode, initcmd_opcode);
613 /*Move all the items in the old argv into the new argv, in their proper places */
614 for (i = *aargc; i > 1; i--)
615 newargv[i] = aargv[i - 1];
617 /*Slip in the opcode and the trailing null pointer, and bump the argument count up by one for the new opcode */
618 newargv[0] = aargv[0];
619 newargv[1] = pinitopcode;
621 newargv[*aargc] = NULL;
623 /*Return the happy news */
626 } /*InsertInitOpcode */
629 NoParmsOK(struct cmd_syndesc *as)
632 struct cmd_parmdesc *td;
634 for (i = 0; i < CMD_MAXPARMS; i++) {
636 if (td->type != 0 && !(td->flags & CMD_OPTIONAL)) {
637 /* found a non-optional (e.g. required) parm, so NoParmsOK
638 * is false (some parms are required) */
645 /* Call the appropriate function, or return syntax error code. Note: if no opcode is specified, an initialization routine exists, and it has NOT been called before, we invoke the special initialization opcode*/
647 cmd_Dispatch(int argc, char **argv)
650 struct cmd_syndesc *ts;
651 struct cmd_parmdesc *tparm;
656 static int initd = 0; /*Is this the first time this routine has been called? */
657 static int initcmdpossible = 1; /*Should be consider parsing the initial command? */
661 /* Add help, apropos commands once */
663 ts = cmd_CreateSyntax("help", HelpProc, (char *)0,
664 "get help on commands");
665 cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
666 cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, NULL);
668 ts = cmd_CreateSyntax("apropos", AproposProc, (char *)0,
669 "search by help text");
670 cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED,
672 ts = cmd_CreateSyntax("version", VersionProc, (char *)0,
674 ts = cmd_CreateSyntax("-version", VersionProc, (char *)0,
676 ts = cmd_CreateSyntax("-help", HelpProc, (char *)0,
678 ts = cmd_CreateSyntax("--version", VersionProc, (char *)0,
680 ts = cmd_CreateSyntax("--help", HelpProc, (char *)0,
685 /*Remember the program name */
690 if (!NoParmsOK(allSyntax)) {
691 printf("%s: Type '%s -help' for help\n", pname, pname);
697 /* if there is an initcmd, don't print an error message, just
698 * setup to use the initcmd below. */
699 if (!(initcmdpossible && FindSyntax(initcmd_opcode, (int *)0))) {
700 printf("%s: Type '%s help' or '%s help <topic>' for help\n",
701 pname, pname, pname);
707 /* Find the syntax descriptor for this command, doing prefix matching properly */
711 ts = (argc < 2 ? 0 : FindSyntax(argv[1], &ambig));
713 /*First token doesn't match a syntax descriptor */
714 if (initcmdpossible) {
715 /*If initial command line handling hasn't been done yet,
716 * see if there is a descriptor for the initialization opcode.
717 * Only try this once. */
719 ts = FindSyntax(initcmd_opcode, (int *)0);
721 /*There is no initialization opcode available, so we declare
724 fprintf(stderr, "%s", NName(pname, ": "));
726 "Ambiguous operation '%s'; type '%shelp' for list\n",
727 argv[1], NName(pname, " "));
729 fprintf(stderr, "%s", NName(pname, ": "));
731 "Unrecognized operation '%s'; type '%shelp' for list\n",
732 argv[1], NName(pname, " "));
734 return (CMD_UNKNOWNCMD);
736 /*Found syntax structure for an initialization opcode. Fix
737 * up argv and argc to relect what the user
738 * ``should have'' typed */
739 if (!(argv = InsertInitOpcode(&argc, argv))) {
741 "%sCan't insert implicit init opcode into command line\n",
743 return (CMD_INTERNALERROR);
746 } /*Initial opcode not yet attempted */
748 /* init cmd already run and no syntax entry found */
750 fprintf(stderr, "%s", NName(pname, ": "));
752 "Ambiguous operation '%s'; type '%shelp' for list\n",
753 argv[1], NName(pname, " "));
755 fprintf(stderr, "%s", NName(pname, ": "));
757 "Unrecognized operation '%s'; type '%shelp' for list\n",
758 argv[1], NName(pname, " "));
760 return CMD_UNKNOWNCMD;
762 } /*Argv[1] is not a valid opcode */
763 } /*Opcodes are defined */
765 /* Found the descriptor; start parsing. curType is the type we're trying to parse */
768 /* We start off parsing in "positional" mode, where tokens are put in
769 * slots positionally. If we find a name that takes args, we go
770 * out of positional mode, and from that point on, expect a switch
771 * before any particular token. */
772 positional = 1; /* Are we still in the positional region of the cmd line? */
773 i = noOpcodes ? 1 : 2;
774 SetupExpandsFlag(ts);
775 for (; i < argc; i++) {
776 /* Only tokens that start with a hyphen and are not followed by a digit
777 * are considered switches. This allow negative numbers. */
778 if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
780 j = FindType(ts, argv[i]);
783 "%sUnrecognized or ambiguous switch '%s'; type ",
784 NName(pname, ": "), argv[i]);
786 fprintf(stderr, "'%s -help' for detailed help\n",
789 fprintf(stderr, "'%shelp %s' for detailed help\n",
790 NName(argv[0], " "), ts->name);
792 return (CMD_UNKNOWNSWITCH);
794 if (j >= CMD_MAXPARMS) {
795 fprintf(stderr, "%sInternal parsing error\n",
798 return (CMD_INTERNALERROR);
800 if (ts->parms[j].type == CMD_FLAG) {
801 ts->parms[j].items = &dummy;
805 ts->parms[j].flags |= CMD_PROCESSED;
808 /* Try to fit in this descr */
809 if (curType >= CMD_MAXPARMS) {
810 fprintf(stderr, "%sToo many arguments\n", NName(pname, ": "));
812 return (CMD_TOOMANY);
814 tparm = &ts->parms[curType];
816 if ((tparm->type == 0) || /* No option in this slot */
817 (tparm->type == CMD_FLAG)) { /* A flag (not an argument */
818 /* skipped parm slot */
819 curType++; /* Skip this slot and reprocess this parm */
824 if (!(tparm->flags & CMD_PROCESSED) && (tparm->flags & CMD_HIDE)) {
825 curType++; /* Skip this slot and reprocess this parm */
830 if (tparm->type == CMD_SINGLE) {
832 fprintf(stderr, "%sToo many values after switch %s\n",
833 NName(pname, ": "), tparm->name);
835 return (CMD_NOTLIST);
837 AddItem(tparm, argv[i]); /* Add to end of list */
838 } else if (tparm->type == CMD_LIST) {
839 AddItem(tparm, argv[i]); /* Add to end of list */
841 /* Now, if we're in positional mode, advance to the next item */
843 curType = AdvanceType(ts, curType);
847 /* keep track of this for messages */
848 ts->a0name = argv[0];
850 /* If we make it here, all the parameters are filled in. Check to see if this
851 * is a -help version. Must do this before checking for all required parms,
852 * otherwise it is a real nuisance */
853 if (ts->parms[CMD_HELPPARM].items) {
855 /* Display full help syntax if we don't have subcommands */
862 /* Parsing done, see if we have all of our required parameters */
863 for (i = 0; i < CMD_MAXPARMS; i++) {
864 tparm = &ts->parms[i];
865 if (tparm->type == 0)
866 continue; /* Skipped parm slot */
867 if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
868 fprintf(stderr, "%s The field '%s' isn't completed properly\n",
869 NName(pname, ": "), tparm->name);
871 tparm->flags &= ~CMD_PROCESSED;
874 if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
875 fprintf(stderr, "%sMissing required parameter '%s'\n",
876 NName(pname, ": "), tparm->name);
878 tparm->flags &= ~CMD_PROCESSED;
881 tparm->flags &= ~CMD_PROCESSED;
885 * Before calling the beforeProc and afterProc and all the implications
886 * from those calls, check if the help procedure was called and call it now.
888 if ((ts->proc == HelpProc) || (ts->proc == AproposProc)) {
889 i = (*ts->proc) (ts, ts->rock);
894 /* Now, we just call the procedure and return */
896 i = (*beforeProc) (ts, beforeRock);
903 i = (*ts->proc) (ts, ts->rock);
905 (*afterProc) (ts, afterRock);
906 ResetSyntax(ts); /* Reset and free things */
910 /* free token list returned by parseLine */
912 FreeTokens(struct cmd_token *alist)
914 struct cmd_token *nlist;
915 for (; alist; alist = nlist) {
923 /* free an argv list returned by parseline */
925 cmd_FreeArgv(char **argv)
928 for (tp = *argv; tp; argv++, tp = *argv)
933 /* copy back the arg list to the argv array, freeing the cmd_tokens as you go; the actual
934 data is still malloc'd, and will be freed when the caller calls cmd_FreeArgv
938 CopyBackArgs(struct cmd_token *alist, char **argv,
939 afs_int32 * an, afs_int32 amaxn)
941 struct cmd_token *next;
947 *argv = (char *)malloc(strlen(INITSTR) + 1);
949 strcpy(*argv, INITSTR);
955 return CMD_TOOMANY; /* argv is too small for his many parms. */
964 *(argv++) = 0; /* use last slot for terminating null */
965 /* don't count terminating null */
973 if (x == '"' || x == 39 /* single quote */ )
982 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
989 cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
993 int inToken, inQuote;
994 struct cmd_token *first, *last;
995 struct cmd_token *ttok;
998 inToken = 0; /* not copying token chars at start */
999 first = (struct cmd_token *)0;
1000 last = (struct cmd_token *)0;
1001 inQuote = 0; /* not in a quoted string */
1004 if (tc == 0 || (!inQuote && space(tc))) { /* terminating null gets us in here, too */
1006 inToken = 0; /* end of this token */
1008 return -1; /* should never get here */
1011 ttok = (struct cmd_token *)malloc(sizeof(struct cmd_token));
1013 ttok->next = (struct cmd_token *)0;
1014 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
1016 strcpy(ttok->key, tbuffer);
1026 /* an alpha character */
1031 if (tptr - tbuffer >= sizeof(tbuffer)) {
1033 return CMD_TOOBIG; /* token too long */
1036 /* hit a quote, toggle inQuote flag but don't insert character */
1039 /* insert character */
1044 /* last token flushed 'cause space(0) --> true */
1046 last->next = (struct cmd_token *)0;
1047 return CopyBackArgs(first, argv, an, amaxn);