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, char *beforeRock) =
34 0, (*afterProc) (struct cmd_syndesc * ts, char *afterRock) = 0;
35 static char *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)
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)
169 PrintSyntax(register struct cmd_syndesc *as)
172 register struct cmd_parmdesc *tp;
174 /* now print usage, from syntax table */
176 printf("Usage: %s", as->a0name);
178 if (!strcmp(as->name, initcmd_opcode))
179 printf("Usage: %s[%s]", NName(as->a0name, " "), as->name);
181 printf("Usage: %s%s", NName(as->a0name, " "), as->name);
184 for (i = 0; i < CMD_MAXPARMS; i++) {
187 continue; /* seeked over slot */
188 if (tp->flags & CMD_HIDE)
189 continue; /* skip hidden options */
191 if (tp->flags & CMD_OPTIONAL)
193 printf("%s", tp->name);
195 if (tp->flags & CMD_OPTIONAL)
201 /* must print newline in any case, to terminate preceding line */
203 PrintAliases(register struct cmd_syndesc *as)
205 register struct cmd_syndesc *ts;
207 if (as->flags & CMD_ALIAS) {
209 printf("(alias for %s)\n", ts->name);
213 return; /* none, print nothing */
215 for (as = as->nextAlias; as; as = as->nextAlias) {
216 printf("%s ", as->name);
223 PrintFlagHelp(register struct cmd_syndesc *as)
226 register struct cmd_parmdesc *tp;
230 /* find flag name length */
232 for (i = 0; i < CMD_MAXPARMS; i++) {
233 if (i == CMD_HELPPARM)
236 if (tp->type != CMD_FLAG)
238 if (tp->flags & CMD_HIDE)
239 continue; /* skip hidden options */
243 if (strlen(tp->name) > flag_width)
244 flag_width = strlen(tp->name);
247 /* print flag help */
248 flag_prefix = "Where:";
249 for (i = 0; i < CMD_MAXPARMS; i++) {
250 if (i == CMD_HELPPARM)
253 if (tp->type != CMD_FLAG)
255 if (tp->flags & CMD_HIDE)
256 continue; /* skip hidden options */
260 printf("%-7s%-*s %s\n", flag_prefix, flag_width, tp->name, tp->help);
266 AproposProc(register struct cmd_syndesc *as, char *arock)
268 register struct cmd_syndesc *ts;
273 tsub = as->parms[0].items->data;
274 for (ts = allSyntax; ts; ts = ts->next) {
275 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
277 if (SubString(ts->help, tsub)) {
278 printf("%s: %s\n", ts->name, ts->help);
280 } else if (SubString(ts->name, tsub)) {
281 printf("%s: %s\n", ts->name, ts->help);
286 printf("Sorry, no commands found\n");
291 HelpProc(register struct cmd_syndesc *as, char *arock)
293 register struct cmd_syndesc *ts;
294 register struct cmd_item *ti;
298 if (as->parms[0].items == 0) {
299 printf("%sCommands are:\n", NName(as->a0name, ": "));
300 for (ts = allSyntax; ts; ts = ts->next) {
301 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
303 printf("%-15s %s\n", ts->name, (ts->help ? ts->help : ""));
306 /* print out individual help topics */
307 for (ti = as->parms[0].items; ti; ti = ti->next) {
309 ts = FindSyntax(ti->data, &ambig);
310 if (ts && (ts->flags & CMD_HIDDEN))
311 ts = 0; /* no hidden commands */
313 /* print out command name and help */
314 printf("%s%s: %s ", NName(as->a0name, " "), ts->name,
315 (ts->help ? ts->help : ""));
316 ts->a0name = as->a0name;
322 fprintf(stderr, "%sUnknown topic '%s'\n",
323 NName(as->a0name, ": "), ti->data);
325 /* ambiguous, list 'em all */
327 "%sAmbiguous topic '%s'; use 'apropos' to list\n",
328 NName(as->a0name, ": "), ti->data);
330 code = CMD_UNKNOWNCMD;
338 cmd_SetBeforeProc(int (*aproc) (struct cmd_syndesc * ts, char *beforeRock),
347 cmd_SetAfterProc(int (*aproc) (struct cmd_syndesc * ts, char *afterRock),
355 /* thread on list in alphabetical order */
357 SortSyntax(struct cmd_syndesc *as)
359 register struct cmd_syndesc **ld, *ud;
361 for (ld = &allSyntax, ud = *ld; ud; ld = &ud->next, ud = *ld) {
362 if (strcmp(ud->name, as->name) > 0) { /* next guy is bigger than us */
366 /* thread us on the list now */
373 cmd_CreateSyntax(char *aname,
374 int (*aproc) (struct cmd_syndesc * ts, char *arock),
375 char *arock, char *ahelp)
377 register struct cmd_syndesc *td;
379 /* can't have two cmds in no opcode mode */
381 return (struct cmd_syndesc *)0;
383 td = (struct cmd_syndesc *)calloc(1, sizeof(struct cmd_syndesc));
385 td->aliasOf = td; /* treat aliasOf as pointer to real command, no matter what */
387 /* copy in name, etc */
389 td->name = (char *)malloc(strlen(aname) + 1);
391 strcpy(td->name, aname);
397 /* Piggy-back the hidden option onto the help string */
398 if (ahelp == (char *)CMD_HIDDEN) {
399 td->flags |= CMD_HIDDEN;
401 td->help = (char *)malloc(strlen(ahelp) + 1);
403 strcpy(td->help, ahelp);
412 cmd_Seek(td, CMD_HELPPARM);
413 cmd_AddParm(td, "-help", CMD_FLAG, CMD_OPTIONAL, "get detailed help");
420 cmd_CreateAlias(register struct cmd_syndesc *as, char *aname)
422 register struct cmd_syndesc *td;
424 td = (struct cmd_syndesc *)malloc(sizeof(struct cmd_syndesc));
426 memcpy(td, as, sizeof(struct cmd_syndesc));
427 td->name = (char *)malloc(strlen(aname) + 1);
429 strcpy(td->name, aname);
430 td->flags |= CMD_ALIAS;
431 /* if ever free things, make copy of help string, too */
436 /* thread on alias lists */
437 td->nextAlias = as->nextAlias;
441 return 0; /* all done */
445 cmd_IsAdministratorCommand(register struct cmd_syndesc *as)
447 as->flags |= CMD_ADMIN;
452 cmd_Seek(register struct cmd_syndesc *as, int apos)
454 if (apos >= CMD_MAXPARMS)
455 return CMD_EXCESSPARMS;
461 cmd_AddParm(register struct cmd_syndesc *as, char *aname, int atype,
462 afs_int32 aflags, char *ahelp)
464 register struct cmd_parmdesc *tp;
466 if (as->nParms >= CMD_MAXPARMS)
467 return CMD_EXCESSPARMS;
468 tp = &as->parms[as->nParms++];
470 tp->name = (char *)malloc(strlen(aname) + 1);
472 strcpy(tp->name, aname);
475 tp->items = (struct cmd_item *)0;
477 tp->help = (char *)malloc(strlen(ahelp) + 1);
479 strcpy(tp->help, ahelp);
485 /* add a text item to the end of the parameter list */
487 AddItem(register struct cmd_parmdesc *aparm, register char *aval)
489 register struct cmd_item *ti, *ni;
490 ti = (struct cmd_item *)calloc(1, sizeof(struct cmd_item));
492 ti->data = (char *)malloc(strlen(aval) + 1);
494 strcpy(ti->data, aval);
495 /* now put ti at the *end* of the list */
496 if ((ni = aparm->items)) {
497 for (; ni; ni = ni->next)
499 break; /* skip to last one */
502 aparm->items = ti; /* we're first */
506 /* skip to next non-flag item, if any */
508 AdvanceType(register struct cmd_syndesc *as, register afs_int32 aval)
510 register afs_int32 next;
511 register struct cmd_parmdesc *tp;
513 /* first see if we should try to grab rest of line for this dude */
514 if (as->parms[aval].flags & CMD_EXPANDS)
517 /* if not, find next non-flag used slot */
518 for (next = aval + 1; next < CMD_MAXPARMS; next++) {
519 tp = &as->parms[next];
520 if (tp->type != 0 && tp->type != CMD_FLAG)
526 /* discard parameters filled in by dispatch */
528 ResetSyntax(register struct cmd_syndesc *as)
531 register struct cmd_parmdesc *tp;
532 register struct cmd_item *ti, *ni;
535 for (i = 0; i < CMD_MAXPARMS; i++, tp++) {
539 /* free whole list in both cases, just for fun */
540 for (ti = tp->items; ti; ti = ni) {
550 tp->items = (struct cmd_item *)0;
554 /* move the expands flag to the last one in the list */
556 SetupExpandsFlag(register struct cmd_syndesc *as)
558 register struct cmd_parmdesc *tp;
559 register int last, i;
562 /* find last CMD_LIST type parameter, optional or not, and make it expandable
563 * if no other dude is expandable */
564 for (i = 0; i < CMD_MAXPARMS; i++) {
566 if (tp->type == CMD_LIST) {
567 if (tp->flags & CMD_EXPANDS)
568 return 0; /* done if already specified */
573 as->parms[last].flags |= CMD_EXPANDS;
577 /*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.*/
579 InsertInitOpcode(int *aargc, char **aargv)
581 char **newargv; /*Ptr to new, expanded argv space */
582 char *pinitopcode; /*Ptr to space for name of init opcode */
583 int i; /*Loop counter */
585 /*Allocate the new argv array, plus one for the new opcode, plus one more for the trailing null pointer */
586 newargv = (char **)malloc(((*aargc) + 2) * sizeof(char *));
588 fprintf(stderr, "%s: Can't create new argv array with %d+2 slots\n",
593 /*Create space for the initial opcode & fill it in */
594 pinitopcode = (char *)malloc(sizeof(initcmd_opcode));
596 fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]);
599 strcpy(pinitopcode, initcmd_opcode);
601 /*Move all the items in the old argv into the new argv, in their proper places */
602 for (i = *aargc; i > 1; i--)
603 newargv[i] = aargv[i - 1];
605 /*Slip in the opcode and the trailing null pointer, and bump the argument count up by one for the new opcode */
606 newargv[0] = aargv[0];
607 newargv[1] = pinitopcode;
609 newargv[*aargc] = NULL;
611 /*Return the happy news */
614 } /*InsertInitOpcode */
617 NoParmsOK(register struct cmd_syndesc *as)
620 register struct cmd_parmdesc *td;
622 for (i = 0; i < CMD_MAXPARMS; i++) {
624 if (td->type != 0 && !(td->flags & CMD_OPTIONAL)) {
625 /* found a non-optional (e.g. required) parm, so NoParmsOK
626 * is false (some parms are required) */
633 /* 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*/
635 cmd_Dispatch(int argc, char **argv)
638 struct cmd_syndesc *ts;
639 struct cmd_parmdesc *tparm;
644 static int initd = 0; /*Is this the first time this routine has been called? */
645 static int initcmdpossible = 1; /*Should be consider parsing the initial command? */
649 /* Add help, apropos commands once */
651 ts = cmd_CreateSyntax("help", HelpProc, (char *)0,
652 "get help on commands");
653 cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
654 cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, NULL);
656 ts = cmd_CreateSyntax("apropos", AproposProc, (char *)0,
657 "search by help text");
658 cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED,
663 /*Remember the program name */
668 if (!NoParmsOK(allSyntax)) {
669 printf("%s: Type '%s -help' for help\n", pname, pname);
675 /* if there is an initcmd, don't print an error message, just
676 * setup to use the initcmd below. */
677 if (!(initcmdpossible && FindSyntax(initcmd_opcode, (int *)0))) {
678 printf("%s: Type '%s help' or '%s help <topic>' for help\n",
679 pname, pname, pname);
685 /* Find the syntax descriptor for this command, doing prefix matching properly */
689 ts = (argc < 2 ? 0 : FindSyntax(argv[1], &ambig));
691 /*First token doesn't match a syntax descriptor */
692 if (initcmdpossible) {
693 /*If initial command line handling hasn't been done yet,
694 * see if there is a descriptor for the initialization opcode.
695 * Only try this once. */
697 ts = FindSyntax(initcmd_opcode, (int *)0);
699 /*There is no initialization opcode available, so we declare
702 fprintf(stderr, "%s", NName(pname, ": "));
704 "Ambiguous operation '%s'; type '%shelp' for list\n",
705 argv[1], NName(pname, " "));
707 fprintf(stderr, "%s", NName(pname, ": "));
709 "Unrecognized operation '%s'; type '%shelp' for list\n",
710 argv[1], NName(pname, " "));
712 return (CMD_UNKNOWNCMD);
714 /*Found syntax structure for an initialization opcode. Fix
715 * up argv and argc to relect what the user
716 * ``should have'' typed */
717 if (!(argv = InsertInitOpcode(&argc, argv))) {
719 "%sCan't insert implicit init opcode into command line\n",
721 return (CMD_INTERNALERROR);
724 } /*Initial opcode not yet attempted */
726 /* init cmd already run and no syntax entry found */
728 fprintf(stderr, "%s", NName(pname, ": "));
730 "Ambiguous operation '%s'; type '%shelp' for list\n",
731 argv[1], NName(pname, " "));
733 fprintf(stderr, "%s", NName(pname, ": "));
735 "Unrecognized operation '%s'; type '%shelp' for list\n",
736 argv[1], NName(pname, " "));
738 return CMD_UNKNOWNCMD;
740 } /*Argv[1] is not a valid opcode */
741 } /*Opcodes are defined */
743 /* Found the descriptor; start parsing. curType is the type we're trying to parse */
746 /* We start off parsing in "positional" mode, where tokens are put in
747 * slots positionally. If we find a name that takes args, we go
748 * out of positional mode, and from that point on, expect a switch
749 * before any particular token. */
750 positional = 1; /* Are we still in the positional region of the cmd line? */
751 i = noOpcodes ? 1 : 2;
752 SetupExpandsFlag(ts);
753 for (; i < argc; i++) {
754 /* Only tokens that start with a hyphen and are not followed by a digit
755 * are considered switches. This allow negative numbers. */
756 if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
758 j = FindType(ts, argv[i]);
761 "%sUnrecognized or ambiguous switch '%s'; type ",
762 NName(pname, ": "), argv[i]);
764 fprintf(stderr, "'%s -help' for detailed help\n",
767 fprintf(stderr, "'%shelp %s' for detailed help\n",
768 NName(argv[0], " "), ts->name);
770 return (CMD_UNKNOWNSWITCH);
772 if (j >= CMD_MAXPARMS) {
773 fprintf(stderr, "%sInternal parsing error\n",
776 return (CMD_INTERNALERROR);
778 if (ts->parms[j].type == CMD_FLAG) {
779 ts->parms[j].items = (struct cmd_item *)&dummy;
783 ts->parms[j].flags |= CMD_PROCESSED;
786 /* Try to fit in this descr */
787 if (curType >= CMD_MAXPARMS) {
788 fprintf(stderr, "%sToo many arguments\n", NName(pname, ": "));
790 return (CMD_TOOMANY);
792 tparm = &ts->parms[curType];
794 if ((tparm->type == 0) || /* No option in this slot */
795 (tparm->type == CMD_FLAG)) { /* A flag (not an argument */
796 /* skipped parm slot */
797 curType++; /* Skip this slot and reprocess this parm */
802 if (!(tparm->flags & CMD_PROCESSED) && (tparm->flags & CMD_HIDE)) {
803 curType++; /* Skip this slot and reprocess this parm */
808 if (tparm->type == CMD_SINGLE) {
810 fprintf(stderr, "%sToo many values after switch %s\n",
811 NName(pname, ": "), tparm->name);
813 return (CMD_NOTLIST);
815 AddItem(tparm, argv[i]); /* Add to end of list */
816 } else if (tparm->type == CMD_LIST) {
817 AddItem(tparm, argv[i]); /* Add to end of list */
819 /* Now, if we're in positional mode, advance to the next item */
821 curType = AdvanceType(ts, curType);
825 /* keep track of this for messages */
826 ts->a0name = argv[0];
828 /* If we make it here, all the parameters are filled in. Check to see if this
829 * is a -help version. Must do this before checking for all required parms,
830 * otherwise it is a real nuisance */
831 if (ts->parms[CMD_HELPPARM].items) {
833 /* Display full help syntax if we don't have subcommands */
840 /* Parsing done, see if we have all of our required parameters */
841 for (i = 0; i < CMD_MAXPARMS; i++) {
842 tparm = &ts->parms[i];
843 if (tparm->type == 0)
844 continue; /* Skipped parm slot */
845 if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
846 fprintf(stderr, "%s The field '%s' isn't completed properly\n",
847 NName(pname, ": "), tparm->name);
849 tparm->flags &= ~CMD_PROCESSED;
852 if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
853 fprintf(stderr, "%sMissing required parameter '%s'\n",
854 NName(pname, ": "), tparm->name);
856 tparm->flags &= ~CMD_PROCESSED;
859 tparm->flags &= ~CMD_PROCESSED;
863 * Before calling the beforeProc and afterProc and all the implications
864 * from those calls, check if the help procedure was called and call it now.
866 if ((ts->proc == HelpProc) || (ts->proc == AproposProc)) {
867 i = (*ts->proc) (ts, ts->rock);
872 /* Now, we just call the procedure and return */
874 i = (*beforeProc) (ts, beforeRock);
881 i = (*ts->proc) (ts, ts->rock);
883 (*afterProc) (ts, afterRock);
884 ResetSyntax(ts); /* Reset and free things */
888 /* free token list returned by parseLine */
890 FreeTokens(register struct cmd_token *alist)
892 register struct cmd_token *nlist;
893 for (; alist; alist = nlist) {
901 /* free an argv list returned by parseline */
903 cmd_FreeArgv(register char **argv)
906 for (tp = *argv; tp; argv++, tp = *argv)
911 /* copy back the arg list to the argv array, freeing the cmd_tokens as you go; the actual
912 data is still malloc'd, and will be freed when the caller calls cmd_FreeArgv
916 CopyBackArgs(register struct cmd_token *alist, register char **argv,
917 afs_int32 * an, afs_int32 amaxn)
919 register struct cmd_token *next;
925 *argv = (char *)malloc(strlen(INITSTR) + 1);
927 strcpy(*argv, INITSTR);
933 return CMD_TOOMANY; /* argv is too small for his many parms. */
942 *(argv++) = 0; /* use last slot for terminating null */
943 /* don't count terminating null */
949 quote(register int x)
951 if (x == '"' || x == 39 /* single quote */ )
958 space(register int x)
960 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
967 cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
970 register char *tptr = 0;
971 int inToken, inQuote;
972 struct cmd_token *first, *last;
973 register struct cmd_token *ttok;
976 inToken = 0; /* not copying token chars at start */
977 first = (struct cmd_token *)0;
978 last = (struct cmd_token *)0;
979 inQuote = 0; /* not in a quoted string */
982 if (tc == 0 || (!inQuote && space(tc))) { /* terminating null gets us in here, too */
984 inToken = 0; /* end of this token */
986 return -1; /* should never get here */
989 ttok = (struct cmd_token *)malloc(sizeof(struct cmd_token));
991 ttok->next = (struct cmd_token *)0;
992 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
994 strcpy(ttok->key, tbuffer);
1004 /* an alpha character */
1009 if (tptr - tbuffer >= sizeof(tbuffer)) {
1011 return CMD_TOOBIG; /* token too long */
1014 /* hit a quote, toggle inQuote flag but don't insert character */
1017 /* insert character */
1022 /* last token flushed 'cause space(0) --> true */
1024 last->next = (struct cmd_token *)0;
1025 return CopyBackArgs(first, argv, an, amaxn);