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>
16 #include <sys/types.h>
24 /* declaration of private token type */
26 struct cmd_token *next;
30 static int dummy; /* non-null ptr used for flag existence */
31 static struct cmd_syndesc *allSyntax = 0;
32 static int noOpcodes = 0;
33 static int (*beforeProc) (struct cmd_syndesc * ts, void *beforeRock) = NULL;
34 static int (*afterProc) (struct cmd_syndesc * ts, void *afterRock) = NULL;
35 static void *beforeRock, *afterRock;
36 static char initcmd_opcode[] = "initcmd"; /*Name of initcmd opcode */
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[80];
44 if (strlen(a1) == 0) {
53 /* return true if asub is a substring of amain */
55 SubString(register char *amain, register 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(register struct cmd_syndesc *as, register char *aname)
80 cmdlen = strlen(aname);
83 for (i = 0; i < CMD_MAXPARMS; i++) {
84 if (as->parms[i].type == 0)
85 continue; /* this slot not set (seeked over) */
86 if (strcmp(as->parms[i].name, aname) == 0)
88 if (strlen(as->parms[i].name) < cmdlen)
90 /* A hidden option must be a full match (no best matches) */
91 if (as->parms[i].flags & CMD_HIDE)
94 if (strncmp(as->parms[i].name, aname, cmdlen) == 0) {
101 return (ambig ? -1 : best);
104 static struct cmd_syndesc *
105 FindSyntax(char *aname, int *aambig)
107 register struct cmd_syndesc *ts;
108 struct cmd_syndesc *best;
112 cmdLen = strlen(aname);
113 best = (struct cmd_syndesc *)0;
116 *aambig = 0; /* initialize to unambiguous */
117 for (ts = allSyntax; ts; ts = ts->next) {
118 if (strcmp(aname, ts->name) == 0)
120 if (strlen(ts->name) < cmdLen)
121 continue; /* we typed more than item has */
122 /* A hidden command must be a full match (no best matches) */
123 if (ts->flags & CMD_HIDDEN)
126 /* This is just an alias for *best, or *best is just an alias for us.
127 * If we don't make this check explicitly, then an alias which is just a
128 * short prefix of the real command's name might make things ambiguous
129 * for no apparent reason.
131 if (best && ts->aliasOf == best->aliasOf)
133 if (strncmp(ts->name, aname, cmdLen) == 0) {
135 ambig = 1; /* ambiguous name */
142 *aambig = ambig; /* if ambiguous and they care, tell them */
143 return (struct cmd_syndesc *)0; /* fails */
145 return best; /* otherwise its not ambiguous, and they know */
148 /* print the help for a single parameter */
150 PrintParmHelp(register struct cmd_parmdesc *aparm)
152 if (aparm->type == CMD_FLAG) {
154 /* doc people don't like seeing this information */
156 printf(" (%s)", aparm->help);
158 } else if (aparm->help) {
159 printf(" <%s>", aparm->help);
160 if (aparm->type == CMD_LIST)
162 } else if (aparm->type == CMD_SINGLE)
164 else if (aparm->type == CMD_LIST)
168 extern char *AFSVersion;
171 VersionProc(register struct cmd_syndesc *as, void *arock)
173 printf("%s\n", AFSVersion);
178 PrintSyntax(register struct cmd_syndesc *as)
181 register struct cmd_parmdesc *tp;
183 /* now print usage, from syntax table */
185 printf("Usage: %s", as->a0name);
187 if (!strcmp(as->name, initcmd_opcode))
188 printf("Usage: %s[%s]", NName(as->a0name, " "), as->name);
190 printf("Usage: %s%s", NName(as->a0name, " "), as->name);
193 for (i = 0; i < CMD_MAXPARMS; i++) {
196 continue; /* seeked over slot */
197 if (tp->flags & CMD_HIDE)
198 continue; /* skip hidden options */
200 if (tp->flags & CMD_OPTIONAL)
202 printf("%s", tp->name);
204 if (tp->flags & CMD_OPTIONAL)
210 /* must print newline in any case, to terminate preceding line */
212 PrintAliases(register struct cmd_syndesc *as)
214 register struct cmd_syndesc *ts;
216 if (as->flags & CMD_ALIAS) {
218 printf("(alias for %s)\n", ts->name);
222 return; /* none, print nothing */
224 for (as = as->nextAlias; as; as = as->nextAlias) {
225 printf("%s ", as->name);
232 PrintFlagHelp(register struct cmd_syndesc *as)
235 register struct cmd_parmdesc *tp;
239 /* find flag name length */
241 for (i = 0; i < CMD_MAXPARMS; i++) {
242 if (i == CMD_HELPPARM)
245 if (tp->type != CMD_FLAG)
247 if (tp->flags & CMD_HIDE)
248 continue; /* skip hidden options */
252 if (strlen(tp->name) > flag_width)
253 flag_width = strlen(tp->name);
256 /* print flag help */
257 flag_prefix = "Where:";
258 for (i = 0; i < CMD_MAXPARMS; i++) {
259 if (i == CMD_HELPPARM)
262 if (tp->type != CMD_FLAG)
264 if (tp->flags & CMD_HIDE)
265 continue; /* skip hidden options */
269 printf("%-7s%-*s %s\n", flag_prefix, flag_width, tp->name, tp->help);
275 AproposProc(register struct cmd_syndesc *as, void *arock)
277 register struct cmd_syndesc *ts;
282 tsub = as->parms[0].items->data;
283 for (ts = allSyntax; ts; ts = ts->next) {
284 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
286 if (SubString(ts->help, tsub)) {
287 printf("%s: %s\n", ts->name, ts->help);
289 } else if (SubString(ts->name, tsub)) {
290 printf("%s: %s\n", ts->name, ts->help);
295 printf("Sorry, no commands found\n");
300 HelpProc(register struct cmd_syndesc *as, void *arock)
302 register struct cmd_syndesc *ts;
303 register struct cmd_item *ti;
307 if (as->parms[0].items == 0) {
308 printf("%sCommands are:\n", NName(as->a0name, ": "));
309 for (ts = allSyntax; ts; ts = ts->next) {
310 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
312 printf("%-15s %s\n", ts->name, (ts->help ? ts->help : ""));
315 /* print out individual help topics */
316 for (ti = as->parms[0].items; ti; ti = ti->next) {
318 ts = FindSyntax(ti->data, &ambig);
319 if (ts && (ts->flags & CMD_HIDDEN))
320 ts = 0; /* no hidden commands */
322 /* print out command name and help */
323 printf("%s%s: %s ", NName(as->a0name, " "), ts->name,
324 (ts->help ? ts->help : ""));
325 ts->a0name = as->a0name;
331 fprintf(stderr, "%sUnknown topic '%s'\n",
332 NName(as->a0name, ": "), ti->data);
334 /* ambiguous, list 'em all */
336 "%sAmbiguous topic '%s'; use 'apropos' to list\n",
337 NName(as->a0name, ": "), ti->data);
339 code = CMD_UNKNOWNCMD;
347 cmd_SetBeforeProc(int (*aproc) (struct cmd_syndesc * ts, void *beforeRock),
356 cmd_SetAfterProc(int (*aproc) (struct cmd_syndesc * ts, void *afterRock),
364 /* thread on list in alphabetical order */
366 SortSyntax(struct cmd_syndesc *as)
368 register struct cmd_syndesc **ld, *ud;
370 for (ld = &allSyntax, ud = *ld; ud; ld = &ud->next, ud = *ld) {
371 if (strcmp(ud->name, as->name) > 0) { /* next guy is bigger than us */
375 /* thread us on the list now */
382 cmd_CreateSyntax(char *aname,
383 int (*aproc) (struct cmd_syndesc * ts, void *arock),
384 void *arock, char *ahelp)
386 register struct cmd_syndesc *td;
388 /* can't have two cmds in no opcode mode */
390 return (struct cmd_syndesc *)0;
392 td = (struct cmd_syndesc *)calloc(1, sizeof(struct cmd_syndesc));
394 td->aliasOf = td; /* treat aliasOf as pointer to real command, no matter what */
396 /* copy in name, etc */
398 td->name = (char *)malloc(strlen(aname) + 1);
400 strcpy(td->name, aname);
406 /* Piggy-back the hidden option onto the help string */
407 if (ahelp == (char *)CMD_HIDDEN) {
408 td->flags |= CMD_HIDDEN;
410 td->help = (char *)malloc(strlen(ahelp) + 1);
412 strcpy(td->help, ahelp);
421 cmd_Seek(td, CMD_HELPPARM);
422 cmd_AddParm(td, "-help", CMD_FLAG, CMD_OPTIONAL, "get detailed help");
429 cmd_CreateAlias(register struct cmd_syndesc *as, char *aname)
431 register struct cmd_syndesc *td;
433 td = (struct cmd_syndesc *)malloc(sizeof(struct cmd_syndesc));
435 memcpy(td, as, sizeof(struct cmd_syndesc));
436 td->name = (char *)malloc(strlen(aname) + 1);
438 strcpy(td->name, aname);
439 td->flags |= CMD_ALIAS;
440 /* if ever free things, make copy of help string, too */
445 /* thread on alias lists */
446 td->nextAlias = as->nextAlias;
450 return 0; /* all done */
454 cmd_IsAdministratorCommand(register struct cmd_syndesc *as)
456 as->flags |= CMD_ADMIN;
461 cmd_Seek(register struct cmd_syndesc *as, int apos)
463 if (apos >= CMD_MAXPARMS)
464 return CMD_EXCESSPARMS;
470 cmd_AddParm(register struct cmd_syndesc *as, char *aname, int atype,
471 afs_int32 aflags, char *ahelp)
473 register struct cmd_parmdesc *tp;
475 if (as->nParms >= CMD_MAXPARMS)
476 return CMD_EXCESSPARMS;
477 tp = &as->parms[as->nParms++];
479 tp->name = (char *)malloc(strlen(aname) + 1);
481 strcpy(tp->name, aname);
486 tp->help = (char *)malloc(strlen(ahelp) + 1);
488 strcpy(tp->help, ahelp);
494 /* add a text item to the end of the parameter list */
496 AddItem(register struct cmd_parmdesc *aparm, register char *aval)
498 register struct cmd_item *ti, *ni;
499 ti = (struct cmd_item *)calloc(1, sizeof(struct cmd_item));
501 ti->data = (char *)malloc(strlen(aval) + 1);
503 strcpy(ti->data, aval);
504 /* now put ti at the *end* of the list */
505 if ((ni = aparm->items)) {
506 for (; ni; ni = ni->next)
508 break; /* skip to last one */
511 aparm->items = ti; /* we're first */
515 /* skip to next non-flag item, if any */
517 AdvanceType(register struct cmd_syndesc *as, register afs_int32 aval)
519 register afs_int32 next;
520 register struct cmd_parmdesc *tp;
522 /* first see if we should try to grab rest of line for this dude */
523 if (as->parms[aval].flags & CMD_EXPANDS)
526 /* if not, find next non-flag used slot */
527 for (next = aval + 1; next < CMD_MAXPARMS; next++) {
528 tp = &as->parms[next];
529 if (tp->type != 0 && tp->type != CMD_FLAG)
535 /* discard parameters filled in by dispatch */
537 ResetSyntax(register struct cmd_syndesc *as)
540 register struct cmd_parmdesc *tp;
541 register struct cmd_item *ti, *ni;
544 for (i = 0; i < CMD_MAXPARMS; i++, tp++) {
548 /* free whole list in both cases, just for fun */
549 for (ti = tp->items; ti; ti = ni) {
563 /* move the expands flag to the last one in the list */
565 SetupExpandsFlag(register struct cmd_syndesc *as)
567 register struct cmd_parmdesc *tp;
568 register int last, i;
571 /* find last CMD_LIST type parameter, optional or not, and make it expandable
572 * if no other dude is expandable */
573 for (i = 0; i < CMD_MAXPARMS; i++) {
575 if (tp->type == CMD_LIST) {
576 if (tp->flags & CMD_EXPANDS)
577 return 0; /* done if already specified */
582 as->parms[last].flags |= CMD_EXPANDS;
586 /*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.*/
588 InsertInitOpcode(int *aargc, char **aargv)
590 char **newargv; /*Ptr to new, expanded argv space */
591 char *pinitopcode; /*Ptr to space for name of init opcode */
592 int i; /*Loop counter */
594 /*Allocate the new argv array, plus one for the new opcode, plus one more for the trailing null pointer */
595 newargv = (char **)malloc(((*aargc) + 2) * sizeof(char *));
597 fprintf(stderr, "%s: Can't create new argv array with %d+2 slots\n",
602 /*Create space for the initial opcode & fill it in */
603 pinitopcode = (char *)malloc(sizeof(initcmd_opcode));
605 fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]);
609 strcpy(pinitopcode, initcmd_opcode);
611 /*Move all the items in the old argv into the new argv, in their proper places */
612 for (i = *aargc; i > 1; i--)
613 newargv[i] = aargv[i - 1];
615 /*Slip in the opcode and the trailing null pointer, and bump the argument count up by one for the new opcode */
616 newargv[0] = aargv[0];
617 newargv[1] = pinitopcode;
619 newargv[*aargc] = NULL;
621 /*Return the happy news */
624 } /*InsertInitOpcode */
627 NoParmsOK(register struct cmd_syndesc *as)
630 register struct cmd_parmdesc *td;
632 for (i = 0; i < CMD_MAXPARMS; i++) {
634 if (td->type != 0 && !(td->flags & CMD_OPTIONAL)) {
635 /* found a non-optional (e.g. required) parm, so NoParmsOK
636 * is false (some parms are required) */
643 /* 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*/
645 cmd_Dispatch(int argc, char **argv)
648 struct cmd_syndesc *ts;
649 struct cmd_parmdesc *tparm;
654 static int initd = 0; /*Is this the first time this routine has been called? */
655 static int initcmdpossible = 1; /*Should be consider parsing the initial command? */
659 /* Add help, apropos commands once */
661 ts = cmd_CreateSyntax("help", HelpProc, (char *)0,
662 "get help on commands");
663 cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
664 cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, NULL);
666 ts = cmd_CreateSyntax("apropos", AproposProc, (char *)0,
667 "search by help text");
668 cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED,
670 ts = cmd_CreateSyntax("version", VersionProc, (char *)0,
672 ts = cmd_CreateSyntax("-version", VersionProc, (char *)0,
674 ts = cmd_CreateSyntax("-help", HelpProc, (char *)0,
676 ts = cmd_CreateSyntax("--version", VersionProc, (char *)0,
678 ts = cmd_CreateSyntax("--help", HelpProc, (char *)0,
683 /*Remember the program name */
688 if (!NoParmsOK(allSyntax)) {
689 printf("%s: Type '%s -help' for help\n", pname, pname);
695 /* if there is an initcmd, don't print an error message, just
696 * setup to use the initcmd below. */
697 if (!(initcmdpossible && FindSyntax(initcmd_opcode, (int *)0))) {
698 printf("%s: Type '%s help' or '%s help <topic>' for help\n",
699 pname, pname, pname);
705 /* Find the syntax descriptor for this command, doing prefix matching properly */
709 ts = (argc < 2 ? 0 : FindSyntax(argv[1], &ambig));
711 /*First token doesn't match a syntax descriptor */
712 if (initcmdpossible) {
713 /*If initial command line handling hasn't been done yet,
714 * see if there is a descriptor for the initialization opcode.
715 * Only try this once. */
717 ts = FindSyntax(initcmd_opcode, (int *)0);
719 /*There is no initialization opcode available, so we declare
722 fprintf(stderr, "%s", NName(pname, ": "));
724 "Ambiguous operation '%s'; type '%shelp' for list\n",
725 argv[1], NName(pname, " "));
727 fprintf(stderr, "%s", NName(pname, ": "));
729 "Unrecognized operation '%s'; type '%shelp' for list\n",
730 argv[1], NName(pname, " "));
732 return (CMD_UNKNOWNCMD);
734 /*Found syntax structure for an initialization opcode. Fix
735 * up argv and argc to relect what the user
736 * ``should have'' typed */
737 if (!(argv = InsertInitOpcode(&argc, argv))) {
739 "%sCan't insert implicit init opcode into command line\n",
741 return (CMD_INTERNALERROR);
744 } /*Initial opcode not yet attempted */
746 /* init cmd already run and no syntax entry found */
748 fprintf(stderr, "%s", NName(pname, ": "));
750 "Ambiguous operation '%s'; type '%shelp' for list\n",
751 argv[1], NName(pname, " "));
753 fprintf(stderr, "%s", NName(pname, ": "));
755 "Unrecognized operation '%s'; type '%shelp' for list\n",
756 argv[1], NName(pname, " "));
758 return CMD_UNKNOWNCMD;
760 } /*Argv[1] is not a valid opcode */
761 } /*Opcodes are defined */
763 /* Found the descriptor; start parsing. curType is the type we're trying to parse */
766 /* We start off parsing in "positional" mode, where tokens are put in
767 * slots positionally. If we find a name that takes args, we go
768 * out of positional mode, and from that point on, expect a switch
769 * before any particular token. */
770 positional = 1; /* Are we still in the positional region of the cmd line? */
771 i = noOpcodes ? 1 : 2;
772 SetupExpandsFlag(ts);
773 for (; i < argc; i++) {
774 /* Only tokens that start with a hyphen and are not followed by a digit
775 * are considered switches. This allow negative numbers. */
776 if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
778 j = FindType(ts, argv[i]);
781 "%sUnrecognized or ambiguous switch '%s'; type ",
782 NName(pname, ": "), argv[i]);
784 fprintf(stderr, "'%s -help' for detailed help\n",
787 fprintf(stderr, "'%shelp %s' for detailed help\n",
788 NName(argv[0], " "), ts->name);
790 return (CMD_UNKNOWNSWITCH);
792 if (j >= CMD_MAXPARMS) {
793 fprintf(stderr, "%sInternal parsing error\n",
796 return (CMD_INTERNALERROR);
798 if (ts->parms[j].type == CMD_FLAG) {
799 ts->parms[j].items = (struct cmd_item *)&dummy;
803 ts->parms[j].flags |= CMD_PROCESSED;
806 /* Try to fit in this descr */
807 if (curType >= CMD_MAXPARMS) {
808 fprintf(stderr, "%sToo many arguments\n", NName(pname, ": "));
810 return (CMD_TOOMANY);
812 tparm = &ts->parms[curType];
814 if ((tparm->type == 0) || /* No option in this slot */
815 (tparm->type == CMD_FLAG)) { /* A flag (not an argument */
816 /* skipped parm slot */
817 curType++; /* Skip this slot and reprocess this parm */
822 if (!(tparm->flags & CMD_PROCESSED) && (tparm->flags & CMD_HIDE)) {
823 curType++; /* Skip this slot and reprocess this parm */
828 if (tparm->type == CMD_SINGLE) {
830 fprintf(stderr, "%sToo many values after switch %s\n",
831 NName(pname, ": "), tparm->name);
833 return (CMD_NOTLIST);
835 AddItem(tparm, argv[i]); /* Add to end of list */
836 } else if (tparm->type == CMD_LIST) {
837 AddItem(tparm, argv[i]); /* Add to end of list */
839 /* Now, if we're in positional mode, advance to the next item */
841 curType = AdvanceType(ts, curType);
845 /* keep track of this for messages */
846 ts->a0name = argv[0];
848 /* If we make it here, all the parameters are filled in. Check to see if this
849 * is a -help version. Must do this before checking for all required parms,
850 * otherwise it is a real nuisance */
851 if (ts->parms[CMD_HELPPARM].items) {
853 /* Display full help syntax if we don't have subcommands */
860 /* Parsing done, see if we have all of our required parameters */
861 for (i = 0; i < CMD_MAXPARMS; i++) {
862 tparm = &ts->parms[i];
863 if (tparm->type == 0)
864 continue; /* Skipped parm slot */
865 if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
866 fprintf(stderr, "%s The field '%s' isn't completed properly\n",
867 NName(pname, ": "), tparm->name);
869 tparm->flags &= ~CMD_PROCESSED;
872 if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
873 fprintf(stderr, "%sMissing required parameter '%s'\n",
874 NName(pname, ": "), tparm->name);
876 tparm->flags &= ~CMD_PROCESSED;
879 tparm->flags &= ~CMD_PROCESSED;
883 * Before calling the beforeProc and afterProc and all the implications
884 * from those calls, check if the help procedure was called and call it now.
886 if ((ts->proc == HelpProc) || (ts->proc == AproposProc)) {
887 i = (*ts->proc) (ts, ts->rock);
892 /* Now, we just call the procedure and return */
894 i = (*beforeProc) (ts, beforeRock);
901 i = (*ts->proc) (ts, ts->rock);
903 (*afterProc) (ts, afterRock);
904 ResetSyntax(ts); /* Reset and free things */
908 /* free token list returned by parseLine */
910 FreeTokens(register struct cmd_token *alist)
912 register struct cmd_token *nlist;
913 for (; alist; alist = nlist) {
921 /* free an argv list returned by parseline */
923 cmd_FreeArgv(register char **argv)
926 for (tp = *argv; tp; argv++, tp = *argv)
931 /* copy back the arg list to the argv array, freeing the cmd_tokens as you go; the actual
932 data is still malloc'd, and will be freed when the caller calls cmd_FreeArgv
936 CopyBackArgs(register struct cmd_token *alist, register char **argv,
937 afs_int32 * an, afs_int32 amaxn)
939 register struct cmd_token *next;
945 *argv = (char *)malloc(strlen(INITSTR) + 1);
947 strcpy(*argv, INITSTR);
953 return CMD_TOOMANY; /* argv is too small for his many parms. */
962 *(argv++) = 0; /* use last slot for terminating null */
963 /* don't count terminating null */
969 quote(register int x)
971 if (x == '"' || x == 39 /* single quote */ )
978 space(register int x)
980 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
987 cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
990 register char *tptr = 0;
991 int inToken, inQuote;
992 struct cmd_token *first, *last;
993 register struct cmd_token *ttok;
996 inToken = 0; /* not copying token chars at start */
997 first = (struct cmd_token *)0;
998 last = (struct cmd_token *)0;
999 inQuote = 0; /* not in a quoted string */
1002 if (tc == 0 || (!inQuote && space(tc))) { /* terminating null gets us in here, too */
1004 inToken = 0; /* end of this token */
1006 return -1; /* should never get here */
1009 ttok = (struct cmd_token *)malloc(sizeof(struct cmd_token));
1011 ttok->next = (struct cmd_token *)0;
1012 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
1014 strcpy(ttok->key, tbuffer);
1024 /* an alpha character */
1029 if (tptr - tbuffer >= sizeof(tbuffer)) {
1031 return CMD_TOOBIG; /* token too long */
1034 /* hit a quote, toggle inQuote flag but don't insert character */
1037 /* insert character */
1042 /* last token flushed 'cause space(0) --> true */
1044 last->next = (struct cmd_token *)0;
1045 return CopyBackArgs(first, argv, an, amaxn);