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 <sys/types.h>
18 /* declaration of private token type */
20 struct cmd_token *next;
24 static int dummy; /* non-null ptr used for flag existence */
25 static struct cmd_syndesc *allSyntax=0;
26 static int noOpcodes = 0;
27 static int (*beforeProc)() = 0, (*afterProc)() = 0;
28 static char *beforeRock, *afterRock;
29 static char initcmd_opcode[] = "initcmd"; /*Name of initcmd opcode*/
31 /* take name and string, and return null string if name is empty, otherwise return
32 the concatenation of the two strings */
33 static char *NName(a1, a2)
35 static char tbuffer[80];
36 if (strlen(a1) == 0) {
46 /* return true if asub is a substring of amain */
47 static int SubString(amain, asub)
48 register char *amain, *asub; {
54 if (j < 0) return 0; /* not a substring */
56 if (strncmp(amain, asub, slen) == 0) return 1;
59 return 0; /* didn't find it */
62 static int FindType(as, aname)
63 register struct cmd_syndesc *as;
64 register char *aname; {
70 cmdlen = strlen(aname);
73 for(i=0;i<CMD_MAXPARMS;i++) {
74 if (as->parms[i].type == 0) continue; /* this slot not set (seeked over) */
75 if (strcmp(as->parms[i].name, aname)==0) return i;
76 if (strlen(as->parms[i].name) < cmdlen) continue;
77 /* A hidden option must be a full match (no best matches) */
78 if (as->parms[i].flags & CMD_HIDE) continue;
80 if (strncmp(as->parms[i].name, aname, cmdlen) == 0) {
81 if (best != -1) ambig = 1;
85 return (ambig? -1 : best);
88 static struct cmd_syndesc *FindSyntax(aname, aambig)
91 register struct cmd_syndesc *ts;
92 struct cmd_syndesc *best;
96 cmdLen = strlen(aname);
97 best = (struct cmd_syndesc *) 0;
99 if (aambig) *aambig = 0; /* initialize to unambiguous */
100 for(ts=allSyntax;ts;ts=ts->next) {
101 if (strcmp(aname, ts->name)==0) return(ts);
102 if (strlen(ts->name) < cmdLen) continue; /* we typed more than item has */
103 /* A hidden command must be a full match (no best matches) */
104 if (ts->flags & CMD_HIDDEN) continue;
106 /* This is just an alias for *best, or *best is just an alias for us.
107 * If we don't make this check explicitly, then an alias which is just a
108 * short prefix of the real command's name might make things ambiguous
109 * for no apparent reason.
111 if (best && ts->aliasOf == best->aliasOf) continue;
112 if (strncmp(ts->name, aname, cmdLen) == 0) {
113 if (best) ambig = 1; /* ambiguous name */
119 if(aambig) *aambig = ambig; /* if ambiguous and they care, tell them */
120 return (struct cmd_syndesc *) 0; /* fails */
122 else return best; /* otherwise its not ambiguous, and they know */
125 /* print the help for a single parameter */
126 static void PrintParmHelp(aparm)
127 register struct cmd_parmdesc *aparm; {
128 if (aparm->type == CMD_FLAG) {
130 /* doc people don't like seeing this information */
132 printf(" (%s)", aparm->help);
135 else if (aparm->help) {
136 printf(" <%s>", aparm->help);
137 if (aparm->type == CMD_LIST) printf("+");
139 else if (aparm->type == CMD_SINGLE)
141 else if (aparm->type == CMD_LIST)
146 register struct cmd_syndesc *as; {
148 register struct cmd_parmdesc *tp;
150 /* now print usage, from syntax table */
152 printf("Usage: %s", as->a0name);
154 if (!strcmp(as->name, initcmd_opcode))
155 printf("Usage: %s[%s]", NName(as->a0name, " "), as->name);
157 printf("Usage: %s%s", NName(as->a0name, " "), as->name);
160 for(i=0;i<CMD_MAXPARMS;i++) {
162 if (tp->type == 0) continue; /* seeked over slot */
163 if (tp->flags & CMD_HIDE) continue; /* skip hidden options */
165 if (tp->flags & CMD_OPTIONAL) printf("[");
166 printf("%s", tp->name);
168 if (tp->flags & CMD_OPTIONAL) printf("]");
173 /* must print newline in any case, to terminate preceding line */
174 static void PrintAliases(as)
175 register struct cmd_syndesc *as; {
176 register struct cmd_syndesc *ts;
178 if (as->flags & CMD_ALIAS) {
180 printf("(alias for %s)\n", ts->name);
184 if (!as->nextAlias) return; /* none, print nothing */
186 for(as=as->nextAlias;as;as=as->nextAlias) {
187 printf("%s ", as->name);
193 void PrintFlagHelp(as)
194 register struct cmd_syndesc *as; {
196 register struct cmd_parmdesc *tp;
200 /* find flag name length */
202 for(i=0;i<CMD_MAXPARMS;i++) {
203 if (i == CMD_HELPPARM) continue;
205 if (tp->type != CMD_FLAG) continue;
206 if (tp->flags & CMD_HIDE) continue; /* skip hidden options */
207 if (!tp->help) continue;
209 if (strlen(tp->name) > flag_width) flag_width = strlen(tp->name);
212 /* print flag help */
213 flag_prefix = "Where:";
214 for(i=0;i<CMD_MAXPARMS;i++) {
215 if (i == CMD_HELPPARM) continue;
217 if (tp->type != CMD_FLAG) continue;
218 if (tp->flags & CMD_HIDE) continue; /* skip hidden options */
219 if (!tp->help) continue;
221 printf("%-7s%-*s %s\n", flag_prefix, flag_width, tp->name, tp->help);
226 static AproposProc(as, arock)
228 register struct cmd_syndesc *as; {
229 register struct cmd_syndesc *ts;
234 tsub = as->parms[0].items->data;
235 for(ts=allSyntax;ts;ts=ts->next) {
236 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
238 if (SubString(ts->help, tsub)) {
239 printf("%s: %s\n", ts->name, ts->help);
242 else if (SubString(ts->name, tsub)) {
243 printf("%s: %s\n", ts->name, ts->help);
248 printf("Sorry, no commands found\n");
252 static HelpProc(as, arock)
254 register struct cmd_syndesc *as; {
255 register struct cmd_syndesc *ts;
256 register struct cmd_item *ti;
260 if (as->parms[0].items == 0) {
261 printf("%sCommands are:\n", NName(as->a0name, ": "));
262 for(ts=allSyntax; ts; ts=ts->next) {
263 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
265 printf("%-15s %s\n", ts->name, (ts->help?ts->help:""));
269 /* print out individual help topics */
270 for(ti = as->parms[0].items; ti; ti=ti->next) {
272 ts = FindSyntax(ti->data, &ambig);
273 if (ts && (ts->flags & CMD_HIDDEN)) ts = 0; /* no hidden commands */
275 /* print out command name and help */
276 printf("%s%s: %s ", NName(as->a0name, " "), ts->name, (ts->help?ts->help:""));
277 ts->a0name = as->a0name;
284 fprintf(stderr, "%sUnknown topic '%s'\n", NName(as->a0name, ": "), ti->data);
286 /* ambiguous, list 'em all */
287 fprintf(stderr, "%sAmbiguous topic '%s'; use 'apropos' to list\n",
288 NName(as->a0name, ": "), ti->data);
290 code = CMD_UNKNOWNCMD;
297 cmd_SetBeforeProc(aproc, arock)
305 cmd_SetAfterProc(aproc, arock)
313 /* thread on list in alphabetical order */
314 static SortSyntax(as)
315 struct cmd_syndesc *as; {
316 register struct cmd_syndesc **ld, *ud;
318 for(ld = &allSyntax, ud = *ld; ud; ld = &ud->next, ud = *ld) {
319 if (strcmp(ud->name, as->name) > 0) { /* next guy is bigger than us */
323 /* thread us on the list now */
329 struct cmd_syndesc *cmd_CreateSyntax(aname, aproc, arock, ahelp)
334 register struct cmd_syndesc *td;
336 /* can't have two cmds in no opcode mode */
337 if (noOpcodes) return (struct cmd_syndesc *) 0;
339 td = (struct cmd_syndesc *) calloc(1, sizeof(struct cmd_syndesc));
341 td->aliasOf = td; /* treat aliasOf as pointer to real command, no matter what */
343 /* copy in name, etc */
345 td->name = (char *) malloc(strlen(aname)+1);
347 strcpy(td->name, aname);
350 td->name = (char *) 0;
354 /* Piggy-back the hidden option onto the help string */
355 if (ahelp == (char *)CMD_HIDDEN) {
356 td->flags |= CMD_HIDDEN;
358 td->help = (char *) malloc(strlen(ahelp)+1);
360 strcpy(td->help, ahelp);
363 else td->help = (char *) 0;
369 cmd_Seek(td, CMD_HELPPARM);
370 cmd_AddParm(td, "-help", CMD_FLAG, CMD_OPTIONAL, "get detailed help");
376 int cmd_CreateAlias(as, aname)
377 register struct cmd_syndesc *as;
379 register struct cmd_syndesc *td;
381 td = (struct cmd_syndesc *) malloc(sizeof(struct cmd_syndesc));
383 memcpy(td, as, sizeof(struct cmd_syndesc));
384 td->name = (char *) malloc(strlen(aname)+1);
386 strcpy(td->name, aname);
387 td->flags |= CMD_ALIAS;
388 /* if ever free things, make copy of help string, too */
393 /* thread on alias lists */
394 td->nextAlias = as->nextAlias;
398 return 0; /* all done */
401 int cmd_Seek(as, apos)
402 register struct cmd_syndesc *as;
404 if (apos >= CMD_MAXPARMS) return CMD_EXCESSPARMS;
409 int cmd_AddParm(as, aname, atype, aflags, ahelp)
410 register struct cmd_syndesc *as;
415 register struct cmd_parmdesc *tp;
417 if (as->nParms >= CMD_MAXPARMS) return CMD_EXCESSPARMS;
418 tp = &as->parms[as->nParms++];
420 tp->name = (char *) malloc(strlen(aname)+1);
422 strcpy(tp->name, aname);
425 tp->items = (struct cmd_item *) 0;
427 tp->help = (char *) malloc(strlen(ahelp)+1);
429 strcpy(tp->help, ahelp);
431 else tp->help = (char *) 0;
435 /* add a text item to the end of the parameter list */
436 static AddItem(aparm, aval)
437 register struct cmd_parmdesc *aparm;
438 register char *aval; {
439 register struct cmd_item *ti, *ni;
440 ti = (struct cmd_item *) calloc(1, sizeof(struct cmd_item));
442 ti->data = (char *) malloc(strlen(aval)+1);
444 strcpy(ti->data, aval);
445 /* now put ti at the *end* of the list */
446 if (ni=aparm->items) {
447 for(;ni;ni=ni->next) if (ni->next == 0) break; /* skip to last one */
450 else aparm->items = ti; /* we're first */
454 /* skip to next non-flag item, if any */
455 static AdvanceType(as, aval)
456 register afs_int32 aval;
457 register struct cmd_syndesc *as; {
458 register afs_int32 next;
459 register struct cmd_parmdesc *tp;
461 /* first see if we should try to grab rest of line for this dude */
462 if (as->parms[aval].flags & CMD_EXPANDS) return aval;
464 /* if not, find next non-flag used slot */
465 for(next=aval+1;next<CMD_MAXPARMS;next++) {
466 tp = &as->parms[next];
467 if (tp->type != 0 && tp->type != CMD_FLAG) return next;
472 /* discard parameters filled in by dispatch */
473 static void ResetSyntax(as)
474 register struct cmd_syndesc *as; {
476 register struct cmd_parmdesc *tp;
477 register struct cmd_item *ti, *ni;
480 for(i=0;i<CMD_MAXPARMS;i++, tp++) {
484 /* free whole list in both cases, just for fun */
485 for(ti=tp->items;ti;ti=ni) {
495 tp->items = (struct cmd_item *)0;
499 /* move the expands flag to the last one in the list */
500 static SetupExpandsFlag(as)
501 register struct cmd_syndesc *as; {
502 register struct cmd_parmdesc *tp;
503 register int last, i;
506 /* find last CMD_LIST type parameter, optional or not, and make it expandable
507 if no other dude is expandable */
508 for(i=0;i<CMD_MAXPARMS;i++) {
510 if (tp->type == CMD_LIST) {
511 if (tp->flags & CMD_EXPANDS) return 0; /* done if already specified */
515 if (last >= 0) as->parms[last].flags |= CMD_EXPANDS;
519 /*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.*/
520 static char **InsertInitOpcode(aargc, aargv)
523 { /*InsertInitOpcode*/
525 char **newargv; /*Ptr to new, expanded argv space*/
526 char *pinitopcode; /*Ptr to space for name of init opcode*/
527 int i; /*Loop counter*/
529 /*Allocate the new argv array, plus one for the new opcode, plus one more for the trailing null pointer*/
530 newargv = (char **) malloc(((*aargc)+2) * sizeof(char *));
532 fprintf(stderr, "%s: Can't create new argv array with %d+2 slots\n", aargv[0], *aargc);
536 /*Create space for the initial opcode & fill it in*/
537 pinitopcode = (char *) malloc(sizeof(initcmd_opcode));
539 fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]);
542 strcpy(pinitopcode, initcmd_opcode);
544 /*Move all the items in the old argv into the new argv, in their proper places*/
545 for (i = *aargc; i>1; i--)
546 newargv[i] = aargv[i-1];
548 /*Slip in the opcode and the trailing null pointer, and bump the argument count up by one for the new opcode*/
549 newargv[0] = aargv[0];
550 newargv[1] = pinitopcode;
552 newargv[*aargc] = (char *)0;
554 /*Return the happy news*/
557 } /*InsertInitOpcode*/
560 register struct cmd_syndesc *as; {
562 register struct cmd_parmdesc *td;
564 for(i=0;i<CMD_MAXPARMS;i++) {
566 if (td->type != 0 && !(td->flags & CMD_OPTIONAL)) {
567 /* found a non-optional (e.g. required) parm, so NoParmsOK
568 is false (some parms are required) */
575 /* 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*/
576 cmd_Dispatch(argc, argv)
581 struct cmd_syndesc *ts;
582 struct cmd_parmdesc *tparm;
587 static int initd = 0; /*Is this the first time this routine has been called?*/
588 static int initcmdpossible = 1; /*Should be consider parsing the initial command?*/
592 /* Add help, apropos commands once */
594 ts = cmd_CreateSyntax("help", HelpProc, (char*)0,
595 "get help on commands");
596 cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
598 ts = cmd_CreateSyntax("apropos", AproposProc, (char*)0,
599 "search by help text");
600 cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED, "help string");
604 /*Remember the program name*/
609 if (!NoParmsOK(allSyntax)) {
610 printf("%s: Type '%s -help' for help\n", pname, pname);
617 /* if there is an initcmd, don't print an error message, just
618 setup to use the initcmd below. */
619 if (!(initcmdpossible && FindSyntax(initcmd_opcode, (int *) 0))) {
620 printf("%s: Type '%s help' or '%s help <topic>' for help\n",
621 pname, pname, pname);
627 /* Find the syntax descriptor for this command, doing prefix matching properly */
632 ts = (argc < 2? 0 : FindSyntax(argv[1], &ambig));
634 /*First token doesn't match a syntax descriptor*/
635 if (initcmdpossible) {
636 /*If initial command line handling hasn't been done yet,
637 see if there is a descriptor for the initialization opcode.
638 Only try this once.*/
640 ts = FindSyntax(initcmd_opcode, (int *) 0);
642 /*There is no initialization opcode available, so we declare
645 fprintf(stderr, "%s", NName(pname, ": "));
646 fprintf(stderr, "Ambiguous operation '%s'; type '%shelp' for list\n",
647 argv[1], NName(pname," "));
650 fprintf(stderr, "%s", NName(pname, ": "));
651 fprintf(stderr, "Unrecognized operation '%s'; type '%shelp' for list\n",
652 argv[1], NName(pname, " "));
654 return(CMD_UNKNOWNCMD);
657 /*Found syntax structure for an initialization opcode. Fix
658 up argv and argc to relect what the user
659 ``should have'' typed*/
660 if (!(argv = InsertInitOpcode(&argc, argv))) {
661 fprintf(stderr, "%sCan't insert implicit init opcode into command line\n",
663 return(CMD_INTERNALERROR);
666 } /*Initial opcode not yet attempted*/
668 /* init cmd already run and no syntax entry found */
670 fprintf(stderr, "%s", NName(pname, ": "));
671 fprintf(stderr, "Ambiguous operation '%s'; type '%shelp' for list\n",
672 argv[1], NName(pname, " "));
675 fprintf(stderr, "%s", NName(pname, ": "));
676 fprintf(stderr, "Unrecognized operation '%s'; type '%shelp' for list\n",
677 argv[1], NName(pname, " "));
679 return CMD_UNKNOWNCMD;
681 } /*Argv[1] is not a valid opcode*/
682 } /*Opcodes are defined*/
684 /* Found the descriptor; start parsing. curType is the type we're trying to parse */
687 /* We start off parsing in "positional" mode, where tokens are put in
688 slots positionally. If we find a name that takes args, we go
689 out of positional mode, and from that point on, expect a switch
690 before any particular token. */
691 positional = 1; /* Are we still in the positional region of the cmd line? */
692 i = noOpcodes? 1 : 2;
693 SetupExpandsFlag(ts);
695 /* Only tokens that start with a hyphen and are not followed by a digit
696 * are considered switches. This allow negative numbers. */
697 if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
699 j = FindType(ts, argv[i]);
701 fprintf(stderr, "%sUnrecognized or ambiguous switch '%s'; type ",
702 NName(pname, ": "), argv[i]);
704 fprintf(stderr, "'%s -help' for detailed help\n", argv[0]);
706 fprintf(stderr, "'%shelp %s' for detailed help\n",
707 NName(argv[0], " "), ts->name);
709 return(CMD_UNKNOWNSWITCH);
711 if (j >= CMD_MAXPARMS) {
712 fprintf(stderr, "%sInternal parsing error\n", NName(pname, ": "));
714 return(CMD_INTERNALERROR);
716 if (ts->parms[j].type == CMD_FLAG) {
717 ts->parms[j].items = (struct cmd_item *) &dummy;
722 ts->parms[j].flags |= CMD_PROCESSED;
726 /* Try to fit in this descr */
727 if (curType >= CMD_MAXPARMS) {
728 fprintf(stderr, "%sToo many arguments\n", NName(pname, ": "));
732 tparm = &ts->parms[curType];
734 if ((tparm->type == 0) || /* No option in this slot */
735 (tparm->type == CMD_FLAG)) { /* A flag (not an argument */
736 /* skipped parm slot */
737 curType++; /* Skip this slot and reprocess this parm */
742 if (!(tparm->flags & CMD_PROCESSED) && (tparm->flags & CMD_HIDE)) {
743 curType++; /* Skip this slot and reprocess this parm */
748 if (tparm->type == CMD_SINGLE) {
750 fprintf(stderr, "%sToo many values after switch %s\n",
751 NName(pname, ": "), tparm->name);
755 AddItem(tparm, argv[i]); /* Add to end of list */
757 else if (tparm->type == CMD_LIST) {
758 AddItem(tparm, argv[i]); /* Add to end of list */
760 /* Now, if we're in positional mode, advance to the next item */
762 curType = AdvanceType(ts, curType);
766 /* keep track of this for messages */
767 ts->a0name = argv[0];
769 /* If we make it here, all the parameters are filled in. Check to see if this
770 is a -help version. Must do this before checking for all required parms,
771 otherwise it is a real nuisance */
772 if (ts->parms[CMD_HELPPARM].items) {
778 /* Parsing done, see if we have all of our required parameters */
779 for(i=0; i < CMD_MAXPARMS; i++) {
780 tparm = &ts->parms[i];
781 if (tparm->type == 0) continue; /* Skipped parm slot */
782 if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
783 fprintf(stderr, "%s The field '%s' isn't completed properly\n",
784 NName(pname, ": "), tparm->name);
786 tparm->flags &= ~CMD_PROCESSED;
789 if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
790 fprintf(stderr, "%sMissing required parameter '%s'\n",
791 NName(pname, ": "), tparm->name);
793 tparm->flags &= ~CMD_PROCESSED;
796 tparm->flags &= ~CMD_PROCESSED;
800 * Before calling the beforeProc and afterProc and all the implications
801 * from those calls, check if the help procedure was called and call it now.
803 if ((ts->proc == HelpProc) || (ts->proc == AproposProc))
805 i = (*ts->proc) (ts, ts->rock);
810 /* Now, we just call the procedure and return */
812 i = (*beforeProc)(ts, beforeRock);
819 i=(*ts->proc) (ts, ts->rock);
820 if (afterProc) (*afterProc)(ts, afterRock);
821 ResetSyntax(ts); /* Reset and free things */
825 /* free token list returned by parseLine */
826 static FreeTokens(alist)
827 register struct cmd_token *alist; {
828 register struct cmd_token *nlist;
829 for(; alist; alist = nlist) {
837 /* free an argv list returned by parseline */
839 register char **argv; {
841 for(tp = *argv; tp; argv++, tp = *argv)
846 /* copy back the arg list to the argv array, freeing the cmd_tokens as you go; the actual
847 data is still malloc'd, and will be freed when the caller calls cmd_FreeArgv
850 static CopyBackArgs(alist, argv, an, amaxn)
851 register struct cmd_token *alist;
852 register char **argv;
855 register struct cmd_token *next;
859 if (amaxn <= 1) return CMD_TOOMANY;
860 *argv = (char *) malloc(strlen(INITSTR)+1);
862 strcpy(*argv, INITSTR);
867 if (amaxn <= 1) return CMD_TOOMANY; /* argv is too small for his many parms. */
876 *(argv++) = 0; /* use last slot for terminating null */
877 /* don't count terminating null */
884 if (x == '"' || x == 39 /* single quote */) return 1;
890 if (x == 0 || x == ' ' || x == '\t' || x== '\n') return 1;
894 cmd_ParseLine(aline, argv, an, amaxn)
901 int inToken, inQuote;
902 struct cmd_token *first, *last;
903 register struct cmd_token *ttok;
906 inToken = 0; /* not copying token chars at start */
907 first = (struct cmd_token *) 0;
908 last = (struct cmd_token *) 0;
909 inQuote=0; /* not in a quoted string */
912 if (tc == 0 || (!inQuote && space(tc))) { /* terminating null gets us in here, too */
914 inToken = 0; /* end of this token */
916 ttok = (struct cmd_token *) malloc(sizeof(struct cmd_token));
918 ttok->next = (struct cmd_token *) 0;
919 ttok->key = (char *) malloc(strlen(tbuffer)+1);
921 strcpy(ttok->key, tbuffer);
927 if (!first) first = ttok;
931 /* an alpha character */
936 if (tptr - tbuffer >= sizeof(tbuffer)) {
938 return CMD_TOOBIG; /* token too long */
941 /* hit a quote, toggle inQuote flag but don't insert character */
945 /* insert character */
950 /* last token flushed 'cause space(0) --> true */
951 if (last) last->next = (struct cmd_token *) 0;
952 return CopyBackArgs(first, argv, an, amaxn);