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)
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)
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]);
600 strcpy(pinitopcode, initcmd_opcode);
602 /*Move all the items in the old argv into the new argv, in their proper places */
603 for (i = *aargc; i > 1; i--)
604 newargv[i] = aargv[i - 1];
606 /*Slip in the opcode and the trailing null pointer, and bump the argument count up by one for the new opcode */
607 newargv[0] = aargv[0];
608 newargv[1] = pinitopcode;
610 newargv[*aargc] = NULL;
612 /*Return the happy news */
615 } /*InsertInitOpcode */
618 NoParmsOK(register struct cmd_syndesc *as)
621 register struct cmd_parmdesc *td;
623 for (i = 0; i < CMD_MAXPARMS; i++) {
625 if (td->type != 0 && !(td->flags & CMD_OPTIONAL)) {
626 /* found a non-optional (e.g. required) parm, so NoParmsOK
627 * is false (some parms are required) */
634 /* 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*/
636 cmd_Dispatch(int argc, char **argv)
639 struct cmd_syndesc *ts;
640 struct cmd_parmdesc *tparm;
645 static int initd = 0; /*Is this the first time this routine has been called? */
646 static int initcmdpossible = 1; /*Should be consider parsing the initial command? */
650 /* Add help, apropos commands once */
652 ts = cmd_CreateSyntax("help", HelpProc, (char *)0,
653 "get help on commands");
654 cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
655 cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, NULL);
657 ts = cmd_CreateSyntax("apropos", AproposProc, (char *)0,
658 "search by help text");
659 cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED,
664 /*Remember the program name */
669 if (!NoParmsOK(allSyntax)) {
670 printf("%s: Type '%s -help' for help\n", pname, pname);
676 /* if there is an initcmd, don't print an error message, just
677 * setup to use the initcmd below. */
678 if (!(initcmdpossible && FindSyntax(initcmd_opcode, (int *)0))) {
679 printf("%s: Type '%s help' or '%s help <topic>' for help\n",
680 pname, pname, pname);
686 /* Find the syntax descriptor for this command, doing prefix matching properly */
690 ts = (argc < 2 ? 0 : FindSyntax(argv[1], &ambig));
692 /*First token doesn't match a syntax descriptor */
693 if (initcmdpossible) {
694 /*If initial command line handling hasn't been done yet,
695 * see if there is a descriptor for the initialization opcode.
696 * Only try this once. */
698 ts = FindSyntax(initcmd_opcode, (int *)0);
700 /*There is no initialization opcode available, so we declare
703 fprintf(stderr, "%s", NName(pname, ": "));
705 "Ambiguous operation '%s'; type '%shelp' for list\n",
706 argv[1], NName(pname, " "));
708 fprintf(stderr, "%s", NName(pname, ": "));
710 "Unrecognized operation '%s'; type '%shelp' for list\n",
711 argv[1], NName(pname, " "));
713 return (CMD_UNKNOWNCMD);
715 /*Found syntax structure for an initialization opcode. Fix
716 * up argv and argc to relect what the user
717 * ``should have'' typed */
718 if (!(argv = InsertInitOpcode(&argc, argv))) {
720 "%sCan't insert implicit init opcode into command line\n",
722 return (CMD_INTERNALERROR);
725 } /*Initial opcode not yet attempted */
727 /* init cmd already run and no syntax entry found */
729 fprintf(stderr, "%s", NName(pname, ": "));
731 "Ambiguous operation '%s'; type '%shelp' for list\n",
732 argv[1], NName(pname, " "));
734 fprintf(stderr, "%s", NName(pname, ": "));
736 "Unrecognized operation '%s'; type '%shelp' for list\n",
737 argv[1], NName(pname, " "));
739 return CMD_UNKNOWNCMD;
741 } /*Argv[1] is not a valid opcode */
742 } /*Opcodes are defined */
744 /* Found the descriptor; start parsing. curType is the type we're trying to parse */
747 /* We start off parsing in "positional" mode, where tokens are put in
748 * slots positionally. If we find a name that takes args, we go
749 * out of positional mode, and from that point on, expect a switch
750 * before any particular token. */
751 positional = 1; /* Are we still in the positional region of the cmd line? */
752 i = noOpcodes ? 1 : 2;
753 SetupExpandsFlag(ts);
754 for (; i < argc; i++) {
755 /* Only tokens that start with a hyphen and are not followed by a digit
756 * are considered switches. This allow negative numbers. */
757 if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
759 j = FindType(ts, argv[i]);
762 "%sUnrecognized or ambiguous switch '%s'; type ",
763 NName(pname, ": "), argv[i]);
765 fprintf(stderr, "'%s -help' for detailed help\n",
768 fprintf(stderr, "'%shelp %s' for detailed help\n",
769 NName(argv[0], " "), ts->name);
771 return (CMD_UNKNOWNSWITCH);
773 if (j >= CMD_MAXPARMS) {
774 fprintf(stderr, "%sInternal parsing error\n",
777 return (CMD_INTERNALERROR);
779 if (ts->parms[j].type == CMD_FLAG) {
780 ts->parms[j].items = (struct cmd_item *)&dummy;
784 ts->parms[j].flags |= CMD_PROCESSED;
787 /* Try to fit in this descr */
788 if (curType >= CMD_MAXPARMS) {
789 fprintf(stderr, "%sToo many arguments\n", NName(pname, ": "));
791 return (CMD_TOOMANY);
793 tparm = &ts->parms[curType];
795 if ((tparm->type == 0) || /* No option in this slot */
796 (tparm->type == CMD_FLAG)) { /* A flag (not an argument */
797 /* skipped parm slot */
798 curType++; /* Skip this slot and reprocess this parm */
803 if (!(tparm->flags & CMD_PROCESSED) && (tparm->flags & CMD_HIDE)) {
804 curType++; /* Skip this slot and reprocess this parm */
809 if (tparm->type == CMD_SINGLE) {
811 fprintf(stderr, "%sToo many values after switch %s\n",
812 NName(pname, ": "), tparm->name);
814 return (CMD_NOTLIST);
816 AddItem(tparm, argv[i]); /* Add to end of list */
817 } else if (tparm->type == CMD_LIST) {
818 AddItem(tparm, argv[i]); /* Add to end of list */
820 /* Now, if we're in positional mode, advance to the next item */
822 curType = AdvanceType(ts, curType);
826 /* keep track of this for messages */
827 ts->a0name = argv[0];
829 /* If we make it here, all the parameters are filled in. Check to see if this
830 * is a -help version. Must do this before checking for all required parms,
831 * otherwise it is a real nuisance */
832 if (ts->parms[CMD_HELPPARM].items) {
834 /* Display full help syntax if we don't have subcommands */
841 /* Parsing done, see if we have all of our required parameters */
842 for (i = 0; i < CMD_MAXPARMS; i++) {
843 tparm = &ts->parms[i];
844 if (tparm->type == 0)
845 continue; /* Skipped parm slot */
846 if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
847 fprintf(stderr, "%s The field '%s' isn't completed properly\n",
848 NName(pname, ": "), tparm->name);
850 tparm->flags &= ~CMD_PROCESSED;
853 if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
854 fprintf(stderr, "%sMissing required parameter '%s'\n",
855 NName(pname, ": "), tparm->name);
857 tparm->flags &= ~CMD_PROCESSED;
860 tparm->flags &= ~CMD_PROCESSED;
864 * Before calling the beforeProc and afterProc and all the implications
865 * from those calls, check if the help procedure was called and call it now.
867 if ((ts->proc == HelpProc) || (ts->proc == AproposProc)) {
868 i = (*ts->proc) (ts, ts->rock);
873 /* Now, we just call the procedure and return */
875 i = (*beforeProc) (ts, beforeRock);
882 i = (*ts->proc) (ts, ts->rock);
884 (*afterProc) (ts, afterRock);
885 ResetSyntax(ts); /* Reset and free things */
889 /* free token list returned by parseLine */
891 FreeTokens(register struct cmd_token *alist)
893 register struct cmd_token *nlist;
894 for (; alist; alist = nlist) {
902 /* free an argv list returned by parseline */
904 cmd_FreeArgv(register char **argv)
907 for (tp = *argv; tp; argv++, tp = *argv)
912 /* copy back the arg list to the argv array, freeing the cmd_tokens as you go; the actual
913 data is still malloc'd, and will be freed when the caller calls cmd_FreeArgv
917 CopyBackArgs(register struct cmd_token *alist, register char **argv,
918 afs_int32 * an, afs_int32 amaxn)
920 register struct cmd_token *next;
926 *argv = (char *)malloc(strlen(INITSTR) + 1);
928 strcpy(*argv, INITSTR);
934 return CMD_TOOMANY; /* argv is too small for his many parms. */
943 *(argv++) = 0; /* use last slot for terminating null */
944 /* don't count terminating null */
950 quote(register int x)
952 if (x == '"' || x == 39 /* single quote */ )
959 space(register int x)
961 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
968 cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
971 register char *tptr = 0;
972 int inToken, inQuote;
973 struct cmd_token *first, *last;
974 register struct cmd_token *ttok;
977 inToken = 0; /* not copying token chars at start */
978 first = (struct cmd_token *)0;
979 last = (struct cmd_token *)0;
980 inQuote = 0; /* not in a quoted string */
983 if (tc == 0 || (!inQuote && space(tc))) { /* terminating null gets us in here, too */
985 inToken = 0; /* end of this token */
987 return -1; /* should never get here */
990 ttok = (struct cmd_token *)malloc(sizeof(struct cmd_token));
992 ttok->next = (struct cmd_token *)0;
993 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
995 strcpy(ttok->key, tbuffer);
1005 /* an alpha character */
1010 if (tptr - tbuffer >= sizeof(tbuffer)) {
1012 return CMD_TOOBIG; /* token too long */
1015 /* hit a quote, toggle inQuote flag but don't insert character */
1018 /* insert character */
1023 /* last token flushed 'cause space(0) --> true */
1025 last->next = (struct cmd_token *)0;
1026 return CopyBackArgs(first, argv, an, amaxn);