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 int 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 int 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 int cmd_SetBeforeProc(aproc, arock)
305 int cmd_SetAfterProc(aproc, arock)
313 /* thread on list in alphabetical order */
314 static int 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_IsAdministratorCommand(as)
402 register struct cmd_syndesc *as;
404 as->flags |= CMD_ADMIN;
408 int cmd_Seek(as, apos)
409 register struct cmd_syndesc *as;
411 if (apos >= CMD_MAXPARMS) return CMD_EXCESSPARMS;
416 int cmd_AddParm(as, aname, atype, aflags, ahelp)
417 register struct cmd_syndesc *as;
422 register struct cmd_parmdesc *tp;
424 if (as->nParms >= CMD_MAXPARMS) return CMD_EXCESSPARMS;
425 tp = &as->parms[as->nParms++];
427 tp->name = (char *) malloc(strlen(aname)+1);
429 strcpy(tp->name, aname);
432 tp->items = (struct cmd_item *) 0;
434 tp->help = (char *) malloc(strlen(ahelp)+1);
436 strcpy(tp->help, ahelp);
438 else tp->help = (char *) 0;
442 /* add a text item to the end of the parameter list */
443 static int AddItem(aparm, aval)
444 register struct cmd_parmdesc *aparm;
445 register char *aval; {
446 register struct cmd_item *ti, *ni;
447 ti = (struct cmd_item *) calloc(1, sizeof(struct cmd_item));
449 ti->data = (char *) malloc(strlen(aval)+1);
451 strcpy(ti->data, aval);
452 /* now put ti at the *end* of the list */
453 if ((ni=aparm->items)) {
454 for(;ni;ni=ni->next) if (ni->next == 0) break; /* skip to last one */
457 else aparm->items = ti; /* we're first */
461 /* skip to next non-flag item, if any */
462 static int AdvanceType(as, aval)
463 register afs_int32 aval;
464 register struct cmd_syndesc *as; {
465 register afs_int32 next;
466 register struct cmd_parmdesc *tp;
468 /* first see if we should try to grab rest of line for this dude */
469 if (as->parms[aval].flags & CMD_EXPANDS) return aval;
471 /* if not, find next non-flag used slot */
472 for(next=aval+1;next<CMD_MAXPARMS;next++) {
473 tp = &as->parms[next];
474 if (tp->type != 0 && tp->type != CMD_FLAG) return next;
479 /* discard parameters filled in by dispatch */
480 static void ResetSyntax(as)
481 register struct cmd_syndesc *as; {
483 register struct cmd_parmdesc *tp;
484 register struct cmd_item *ti, *ni;
487 for(i=0;i<CMD_MAXPARMS;i++, tp++) {
491 /* free whole list in both cases, just for fun */
492 for(ti=tp->items;ti;ti=ni) {
502 tp->items = (struct cmd_item *)0;
506 /* move the expands flag to the last one in the list */
507 static int SetupExpandsFlag(as)
508 register struct cmd_syndesc *as; {
509 register struct cmd_parmdesc *tp;
510 register int last, i;
513 /* find last CMD_LIST type parameter, optional or not, and make it expandable
514 if no other dude is expandable */
515 for(i=0;i<CMD_MAXPARMS;i++) {
517 if (tp->type == CMD_LIST) {
518 if (tp->flags & CMD_EXPANDS) return 0; /* done if already specified */
522 if (last >= 0) as->parms[last].flags |= CMD_EXPANDS;
526 /*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.*/
527 static char **InsertInitOpcode(aargc, aargv)
530 { /*InsertInitOpcode*/
532 char **newargv; /*Ptr to new, expanded argv space*/
533 char *pinitopcode; /*Ptr to space for name of init opcode*/
534 int i; /*Loop counter*/
536 /*Allocate the new argv array, plus one for the new opcode, plus one more for the trailing null pointer*/
537 newargv = (char **) malloc(((*aargc)+2) * sizeof(char *));
539 fprintf(stderr, "%s: Can't create new argv array with %d+2 slots\n", aargv[0], *aargc);
543 /*Create space for the initial opcode & fill it in*/
544 pinitopcode = (char *) malloc(sizeof(initcmd_opcode));
546 fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]);
549 strcpy(pinitopcode, initcmd_opcode);
551 /*Move all the items in the old argv into the new argv, in their proper places*/
552 for (i = *aargc; i>1; i--)
553 newargv[i] = aargv[i-1];
555 /*Slip in the opcode and the trailing null pointer, and bump the argument count up by one for the new opcode*/
556 newargv[0] = aargv[0];
557 newargv[1] = pinitopcode;
559 newargv[*aargc] = (char *)0;
561 /*Return the happy news*/
564 } /*InsertInitOpcode*/
566 static int NoParmsOK(as)
567 register struct cmd_syndesc *as; {
569 register struct cmd_parmdesc *td;
571 for(i=0;i<CMD_MAXPARMS;i++) {
573 if (td->type != 0 && !(td->flags & CMD_OPTIONAL)) {
574 /* found a non-optional (e.g. required) parm, so NoParmsOK
575 is false (some parms are required) */
582 /* 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*/
583 int cmd_Dispatch(argc, argv)
588 struct cmd_syndesc *ts;
589 struct cmd_parmdesc *tparm;
594 static int initd = 0; /*Is this the first time this routine has been called?*/
595 static int initcmdpossible = 1; /*Should be consider parsing the initial command?*/
599 /* Add help, apropos commands once */
601 ts = cmd_CreateSyntax("help", HelpProc, (char*)0,
602 "get help on commands");
603 cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
604 cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, (char *)0);
606 ts = cmd_CreateSyntax("apropos", AproposProc, (char*)0,
607 "search by help text");
608 cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED, "help string");
612 /*Remember the program name*/
617 if (!NoParmsOK(allSyntax)) {
618 printf("%s: Type '%s -help' for help\n", pname, pname);
625 /* if there is an initcmd, don't print an error message, just
626 setup to use the initcmd below. */
627 if (!(initcmdpossible && FindSyntax(initcmd_opcode, (int *) 0))) {
628 printf("%s: Type '%s help' or '%s help <topic>' for help\n",
629 pname, pname, pname);
635 /* Find the syntax descriptor for this command, doing prefix matching properly */
640 ts = (argc < 2? 0 : FindSyntax(argv[1], &ambig));
642 /*First token doesn't match a syntax descriptor*/
643 if (initcmdpossible) {
644 /*If initial command line handling hasn't been done yet,
645 see if there is a descriptor for the initialization opcode.
646 Only try this once.*/
648 ts = FindSyntax(initcmd_opcode, (int *) 0);
650 /*There is no initialization opcode available, so we declare
653 fprintf(stderr, "%s", NName(pname, ": "));
654 fprintf(stderr, "Ambiguous operation '%s'; type '%shelp' for list\n",
655 argv[1], NName(pname," "));
658 fprintf(stderr, "%s", NName(pname, ": "));
659 fprintf(stderr, "Unrecognized operation '%s'; type '%shelp' for list\n",
660 argv[1], NName(pname, " "));
662 return(CMD_UNKNOWNCMD);
665 /*Found syntax structure for an initialization opcode. Fix
666 up argv and argc to relect what the user
667 ``should have'' typed*/
668 if (!(argv = InsertInitOpcode(&argc, argv))) {
669 fprintf(stderr, "%sCan't insert implicit init opcode into command line\n",
671 return(CMD_INTERNALERROR);
674 } /*Initial opcode not yet attempted*/
676 /* init cmd already run and no syntax entry found */
678 fprintf(stderr, "%s", NName(pname, ": "));
679 fprintf(stderr, "Ambiguous operation '%s'; type '%shelp' for list\n",
680 argv[1], NName(pname, " "));
683 fprintf(stderr, "%s", NName(pname, ": "));
684 fprintf(stderr, "Unrecognized operation '%s'; type '%shelp' for list\n",
685 argv[1], NName(pname, " "));
687 return CMD_UNKNOWNCMD;
689 } /*Argv[1] is not a valid opcode*/
690 } /*Opcodes are defined*/
692 /* Found the descriptor; start parsing. curType is the type we're trying to parse */
695 /* We start off parsing in "positional" mode, where tokens are put in
696 slots positionally. If we find a name that takes args, we go
697 out of positional mode, and from that point on, expect a switch
698 before any particular token. */
699 positional = 1; /* Are we still in the positional region of the cmd line? */
700 i = noOpcodes? 1 : 2;
701 SetupExpandsFlag(ts);
703 /* Only tokens that start with a hyphen and are not followed by a digit
704 * are considered switches. This allow negative numbers. */
705 if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
707 j = FindType(ts, argv[i]);
709 fprintf(stderr, "%sUnrecognized or ambiguous switch '%s'; type ",
710 NName(pname, ": "), argv[i]);
712 fprintf(stderr, "'%s -help' for detailed help\n", argv[0]);
714 fprintf(stderr, "'%shelp %s' for detailed help\n",
715 NName(argv[0], " "), ts->name);
717 return(CMD_UNKNOWNSWITCH);
719 if (j >= CMD_MAXPARMS) {
720 fprintf(stderr, "%sInternal parsing error\n", NName(pname, ": "));
722 return(CMD_INTERNALERROR);
724 if (ts->parms[j].type == CMD_FLAG) {
725 ts->parms[j].items = (struct cmd_item *) &dummy;
730 ts->parms[j].flags |= CMD_PROCESSED;
734 /* Try to fit in this descr */
735 if (curType >= CMD_MAXPARMS) {
736 fprintf(stderr, "%sToo many arguments\n", NName(pname, ": "));
740 tparm = &ts->parms[curType];
742 if ((tparm->type == 0) || /* No option in this slot */
743 (tparm->type == CMD_FLAG)) { /* A flag (not an argument */
744 /* skipped parm slot */
745 curType++; /* Skip this slot and reprocess this parm */
750 if (!(tparm->flags & CMD_PROCESSED) && (tparm->flags & CMD_HIDE)) {
751 curType++; /* Skip this slot and reprocess this parm */
756 if (tparm->type == CMD_SINGLE) {
758 fprintf(stderr, "%sToo many values after switch %s\n",
759 NName(pname, ": "), tparm->name);
763 AddItem(tparm, argv[i]); /* Add to end of list */
765 else if (tparm->type == CMD_LIST) {
766 AddItem(tparm, argv[i]); /* Add to end of list */
768 /* Now, if we're in positional mode, advance to the next item */
770 curType = AdvanceType(ts, curType);
774 /* keep track of this for messages */
775 ts->a0name = argv[0];
777 /* If we make it here, all the parameters are filled in. Check to see if this
778 is a -help version. Must do this before checking for all required parms,
779 otherwise it is a real nuisance */
780 if (ts->parms[CMD_HELPPARM].items) {
786 /* Parsing done, see if we have all of our required parameters */
787 for(i=0; i < CMD_MAXPARMS; i++) {
788 tparm = &ts->parms[i];
789 if (tparm->type == 0) continue; /* Skipped parm slot */
790 if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
791 fprintf(stderr, "%s The field '%s' isn't completed properly\n",
792 NName(pname, ": "), tparm->name);
794 tparm->flags &= ~CMD_PROCESSED;
797 if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
798 fprintf(stderr, "%sMissing required parameter '%s'\n",
799 NName(pname, ": "), tparm->name);
801 tparm->flags &= ~CMD_PROCESSED;
804 tparm->flags &= ~CMD_PROCESSED;
808 * Before calling the beforeProc and afterProc and all the implications
809 * from those calls, check if the help procedure was called and call it now.
811 if ((ts->proc == HelpProc) || (ts->proc == AproposProc))
813 i = (*ts->proc) (ts, ts->rock);
818 /* Now, we just call the procedure and return */
820 i = (*beforeProc)(ts, beforeRock);
827 i=(*ts->proc) (ts, ts->rock);
828 if (afterProc) (*afterProc)(ts, afterRock);
829 ResetSyntax(ts); /* Reset and free things */
833 /* free token list returned by parseLine */
834 static int FreeTokens(alist)
835 register struct cmd_token *alist; {
836 register struct cmd_token *nlist;
837 for(; alist; alist = nlist) {
845 /* free an argv list returned by parseline */
846 int cmd_FreeArgv(argv)
847 register char **argv; {
849 for(tp = *argv; tp; argv++, tp = *argv)
854 /* copy back the arg list to the argv array, freeing the cmd_tokens as you go; the actual
855 data is still malloc'd, and will be freed when the caller calls cmd_FreeArgv
858 static int CopyBackArgs(alist, argv, an, amaxn)
859 register struct cmd_token *alist;
860 register char **argv;
863 register struct cmd_token *next;
867 if (amaxn <= 1) return CMD_TOOMANY;
868 *argv = (char *) malloc(strlen(INITSTR)+1);
870 strcpy(*argv, INITSTR);
875 if (amaxn <= 1) return CMD_TOOMANY; /* argv is too small for his many parms. */
884 *(argv++) = 0; /* use last slot for terminating null */
885 /* don't count terminating null */
892 if (x == '"' || x == 39 /* single quote */) return 1;
898 if (x == 0 || x == ' ' || x == '\t' || x== '\n') return 1;
902 int cmd_ParseLine(aline, argv, an, amaxn)
908 register char *tptr = 0;
909 int inToken, inQuote;
910 struct cmd_token *first, *last;
911 register struct cmd_token *ttok;
914 inToken = 0; /* not copying token chars at start */
915 first = (struct cmd_token *) 0;
916 last = (struct cmd_token *) 0;
917 inQuote=0; /* not in a quoted string */
920 if (tc == 0 || (!inQuote && space(tc))) { /* terminating null gets us in here, too */
922 inToken = 0; /* end of this token */
924 return -1; /* should never get here */
927 ttok = (struct cmd_token *) malloc(sizeof(struct cmd_token));
929 ttok->next = (struct cmd_token *) 0;
930 ttok->key = (char *) malloc(strlen(tbuffer)+1);
932 strcpy(ttok->key, tbuffer);
938 if (!first) first = ttok;
942 /* an alpha character */
947 if (tptr - tbuffer >= sizeof(tbuffer)) {
949 return CMD_TOOBIG; /* token too long */
952 /* hit a quote, toggle inQuote flag but don't insert character */
956 /* insert character */
961 /* last token flushed 'cause space(0) --> true */
962 if (last) last->next = (struct cmd_token *) 0;
963 return CopyBackArgs(first, argv, an, amaxn);