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>
14 #include <sys/types.h>
22 /* declaration of private token type */
24 struct cmd_token *next;
28 static struct cmd_item dummy; /* non-null ptr used for flag existence */
29 static struct cmd_syndesc *allSyntax = 0;
30 static int noOpcodes = 0;
31 static int (*beforeProc) (struct cmd_syndesc * ts, void *beforeRock) = NULL;
32 static int (*afterProc) (struct cmd_syndesc * ts, void *afterRock) = NULL;
33 static void *beforeRock, *afterRock;
34 static char initcmd_opcode[] = "initcmd"; /*Name of initcmd opcode */
36 /* take name and string, and return null string if name is empty, otherwise return
37 the concatenation of the two strings */
39 NName(char *a1, char *a2)
41 static char tbuffer[300];
42 if (strlen(a1) == 0) {
45 strncpy(tbuffer, a1, sizeof(tbuffer));
46 strncat(tbuffer, a2, sizeof(tbuffer));
47 tbuffer[sizeof(tbuffer)-1]='\0';
52 /* return true if asub is a substring of amain */
54 SubString(char *amain, char *asub)
58 mlen = (int) strlen(amain);
59 slen = (int) strlen(asub);
62 return 0; /* not a substring */
63 for (i = 0; i <= j; i++) {
64 if (strncmp(amain, asub, slen) == 0)
68 return 0; /* didn't find it */
72 FindType(struct cmd_syndesc *as, char *aname)
79 /* Allow --long-style options. */
80 if (aname[0] == '-' && aname[1] == '-' && aname[2] && aname[3]) {
84 cmdlen = strlen(aname);
87 for (i = 0; i < CMD_MAXPARMS; i++) {
88 if (as->parms[i].type == 0)
89 continue; /* this slot not set (seeked over) */
90 if (strcmp(as->parms[i].name, aname) == 0)
92 if (strlen(as->parms[i].name) < cmdlen)
94 /* A hidden option must be a full match (no best matches) */
95 if (as->parms[i].flags & CMD_HIDE)
98 if (strncmp(as->parms[i].name, aname, cmdlen) == 0) {
105 return (ambig ? -1 : best);
108 static struct cmd_syndesc *
109 FindSyntax(char *aname, int *aambig)
111 struct cmd_syndesc *ts;
112 struct cmd_syndesc *best;
116 cmdLen = strlen(aname);
117 best = (struct cmd_syndesc *)0;
120 *aambig = 0; /* initialize to unambiguous */
121 for (ts = allSyntax; ts; ts = ts->next) {
122 if (strcmp(aname, ts->name) == 0)
124 if (strlen(ts->name) < cmdLen)
125 continue; /* we typed more than item has */
126 /* A hidden command must be a full match (no best matches) */
127 if (ts->flags & CMD_HIDDEN)
130 /* This is just an alias for *best, or *best is just an alias for us.
131 * If we don't make this check explicitly, then an alias which is just a
132 * short prefix of the real command's name might make things ambiguous
133 * for no apparent reason.
135 if (best && ts->aliasOf == best->aliasOf)
137 if (strncmp(ts->name, aname, cmdLen) == 0) {
139 ambig = 1; /* ambiguous name */
146 *aambig = ambig; /* if ambiguous and they care, tell them */
147 return (struct cmd_syndesc *)0; /* fails */
149 return best; /* otherwise its not ambiguous, and they know */
152 /* print the help for a single parameter */
154 PrintParmHelp(struct cmd_parmdesc *aparm)
156 if (aparm->type == CMD_FLAG) {
158 /* doc people don't like seeing this information */
160 printf(" (%s)", aparm->help);
162 } else if (aparm->help) {
163 printf(" <%s>", aparm->help);
164 if (aparm->type == CMD_LIST)
166 } else if (aparm->type == CMD_SINGLE)
168 else if (aparm->type == CMD_LIST)
172 extern char *AFSVersion;
175 VersionProc(struct cmd_syndesc *as, void *arock)
177 printf("%s\n", AFSVersion);
182 PrintSyntax(struct cmd_syndesc *as)
185 struct cmd_parmdesc *tp;
187 /* now print usage, from syntax table */
189 printf("Usage: %s", as->a0name);
191 if (!strcmp(as->name, initcmd_opcode))
192 printf("Usage: %s[%s]", NName(as->a0name, " "), as->name);
194 printf("Usage: %s%s", NName(as->a0name, " "), as->name);
197 for (i = 0; i < CMD_MAXPARMS; i++) {
200 continue; /* seeked over slot */
201 if (tp->flags & CMD_HIDE)
202 continue; /* skip hidden options */
204 if (tp->flags & CMD_OPTIONAL)
206 printf("%s", tp->name);
208 if (tp->flags & CMD_OPTIONAL)
214 /* must print newline in any case, to terminate preceding line */
216 PrintAliases(struct cmd_syndesc *as)
218 struct cmd_syndesc *ts;
220 if (as->flags & CMD_ALIAS) {
222 printf("(alias for %s)\n", ts->name);
226 return; /* none, print nothing */
228 for (as = as->nextAlias; as; as = as->nextAlias) {
229 printf("%s ", as->name);
236 PrintFlagHelp(struct cmd_syndesc *as)
239 struct cmd_parmdesc *tp;
243 /* find flag name length */
245 for (i = 0; i < CMD_MAXPARMS; i++) {
246 if (i == CMD_HELPPARM)
249 if (tp->type != CMD_FLAG)
251 if (tp->flags & CMD_HIDE)
252 continue; /* skip hidden options */
256 if (strlen(tp->name) > flag_width)
257 flag_width = strlen(tp->name);
260 /* print flag help */
261 flag_prefix = "Where:";
262 for (i = 0; i < CMD_MAXPARMS; i++) {
263 if (i == CMD_HELPPARM)
266 if (tp->type != CMD_FLAG)
268 if (tp->flags & CMD_HIDE)
269 continue; /* skip hidden options */
273 printf("%-7s%-*s %s\n", flag_prefix, flag_width, tp->name, tp->help);
279 AproposProc(struct cmd_syndesc *as, void *arock)
281 struct cmd_syndesc *ts;
286 tsub = as->parms[0].items->data;
287 for (ts = allSyntax; ts; ts = ts->next) {
288 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
290 if (SubString(ts->help, tsub)) {
291 printf("%s: %s\n", ts->name, ts->help);
293 } else if (SubString(ts->name, tsub)) {
294 printf("%s: %s\n", ts->name, ts->help);
299 printf("Sorry, no commands found\n");
304 HelpProc(struct cmd_syndesc *as, void *arock)
306 struct cmd_syndesc *ts;
311 if (as->parms[0].items == 0) {
312 printf("%sCommands are:\n", NName(as->a0name, ": "));
313 for (ts = allSyntax; ts; ts = ts->next) {
314 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
316 printf("%-15s %s\n", ts->name, (ts->help ? ts->help : ""));
319 /* print out individual help topics */
320 for (ti = as->parms[0].items; ti; ti = ti->next) {
322 ts = FindSyntax(ti->data, &ambig);
323 if (ts && (ts->flags & CMD_HIDDEN))
324 ts = 0; /* no hidden commands */
326 /* print out command name and help */
327 printf("%s%s: %s ", NName(as->a0name, " "), ts->name,
328 (ts->help ? ts->help : ""));
329 ts->a0name = as->a0name;
335 fprintf(stderr, "%sUnknown topic '%s'\n",
336 NName(as->a0name, ": "), ti->data);
338 /* ambiguous, list 'em all */
340 "%sAmbiguous topic '%s'; use 'apropos' to list\n",
341 NName(as->a0name, ": "), ti->data);
343 code = CMD_UNKNOWNCMD;
351 cmd_SetBeforeProc(int (*aproc) (struct cmd_syndesc * ts, void *beforeRock),
360 cmd_SetAfterProc(int (*aproc) (struct cmd_syndesc * ts, void *afterRock),
368 /* thread on list in alphabetical order */
370 SortSyntax(struct cmd_syndesc *as)
372 struct cmd_syndesc **ld, *ud;
374 for (ld = &allSyntax, ud = *ld; ud; ld = &ud->next, ud = *ld) {
375 if (strcmp(ud->name, as->name) > 0) { /* next guy is bigger than us */
379 /* thread us on the list now */
386 cmd_CreateSyntax(char *aname,
387 int (*aproc) (struct cmd_syndesc * ts, void *arock),
388 void *arock, char *ahelp)
390 struct cmd_syndesc *td;
392 /* can't have two cmds in no opcode mode */
394 return (struct cmd_syndesc *)0;
396 td = (struct cmd_syndesc *)calloc(1, sizeof(struct cmd_syndesc));
398 td->aliasOf = td; /* treat aliasOf as pointer to real command, no matter what */
400 /* copy in name, etc */
402 td->name = (char *)malloc(strlen(aname) + 1);
404 strcpy(td->name, aname);
410 /* Piggy-back the hidden option onto the help string */
411 if (ahelp == (char *)CMD_HIDDEN) {
412 td->flags |= CMD_HIDDEN;
414 td->help = (char *)malloc(strlen(ahelp) + 1);
416 strcpy(td->help, ahelp);
425 cmd_Seek(td, CMD_HELPPARM);
426 cmd_AddParm(td, "-help", CMD_FLAG, CMD_OPTIONAL, "get detailed help");
433 cmd_CreateAlias(struct cmd_syndesc *as, char *aname)
435 struct cmd_syndesc *td;
437 td = (struct cmd_syndesc *)malloc(sizeof(struct cmd_syndesc));
439 memcpy(td, as, sizeof(struct cmd_syndesc));
440 td->name = (char *)malloc(strlen(aname) + 1);
442 strcpy(td->name, aname);
443 td->flags |= CMD_ALIAS;
444 /* if ever free things, make copy of help string, too */
449 /* thread on alias lists */
450 td->nextAlias = as->nextAlias;
454 return 0; /* all done */
458 cmd_IsAdministratorCommand(struct cmd_syndesc *as)
460 as->flags |= CMD_ADMIN;
465 cmd_Seek(struct cmd_syndesc *as, int apos)
467 if (apos >= CMD_MAXPARMS)
468 return CMD_EXCESSPARMS;
474 cmd_AddParm(struct cmd_syndesc *as, char *aname, int atype,
475 afs_int32 aflags, char *ahelp)
477 struct cmd_parmdesc *tp;
479 if (as->nParms >= CMD_MAXPARMS)
480 return CMD_EXCESSPARMS;
481 tp = &as->parms[as->nParms++];
483 tp->name = (char *)malloc(strlen(aname) + 1);
485 strcpy(tp->name, aname);
490 tp->help = (char *)malloc(strlen(ahelp) + 1);
492 strcpy(tp->help, ahelp);
498 /* add a text item to the end of the parameter list */
500 AddItem(struct cmd_parmdesc *aparm, char *aval)
502 struct cmd_item *ti, *ni;
503 ti = (struct cmd_item *)calloc(1, sizeof(struct cmd_item));
505 ti->data = (char *)malloc(strlen(aval) + 1);
507 strcpy(ti->data, aval);
508 /* now put ti at the *end* of the list */
509 if ((ni = aparm->items)) {
510 for (; ni; ni = ni->next)
512 break; /* skip to last one */
515 aparm->items = ti; /* we're first */
519 /* skip to next non-flag item, if any */
521 AdvanceType(struct cmd_syndesc *as, afs_int32 aval)
524 struct cmd_parmdesc *tp;
526 /* first see if we should try to grab rest of line for this dude */
527 if (as->parms[aval].flags & CMD_EXPANDS)
530 /* if not, find next non-flag used slot */
531 for (next = aval + 1; next < CMD_MAXPARMS; next++) {
532 tp = &as->parms[next];
533 if (tp->type != 0 && tp->type != CMD_FLAG)
539 /* discard parameters filled in by dispatch */
541 ResetSyntax(struct cmd_syndesc *as)
544 struct cmd_parmdesc *tp;
545 struct cmd_item *ti, *ni;
548 for (i = 0; i < CMD_MAXPARMS; i++, tp++) {
552 /* free whole list in both cases, just for fun */
553 for (ti = tp->items; ti; ti = ni) {
567 /* move the expands flag to the last one in the list */
569 SetupExpandsFlag(struct cmd_syndesc *as)
571 struct cmd_parmdesc *tp;
575 /* find last CMD_LIST type parameter, optional or not, and make it expandable
576 * if no other dude is expandable */
577 for (i = 0; i < CMD_MAXPARMS; i++) {
579 if (tp->type == CMD_LIST) {
580 if (tp->flags & CMD_EXPANDS)
581 return 0; /* done if already specified */
586 as->parms[last].flags |= CMD_EXPANDS;
590 /*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.*/
592 InsertInitOpcode(int *aargc, char **aargv)
594 char **newargv; /*Ptr to new, expanded argv space */
595 char *pinitopcode; /*Ptr to space for name of init opcode */
596 int i; /*Loop counter */
598 /*Allocate the new argv array, plus one for the new opcode, plus one more for the trailing null pointer */
599 newargv = (char **)malloc(((*aargc) + 2) * sizeof(char *));
601 fprintf(stderr, "%s: Can't create new argv array with %d+2 slots\n",
606 /*Create space for the initial opcode & fill it in */
607 pinitopcode = (char *)malloc(sizeof(initcmd_opcode));
609 fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]);
613 strcpy(pinitopcode, initcmd_opcode);
615 /*Move all the items in the old argv into the new argv, in their proper places */
616 for (i = *aargc; i > 1; i--)
617 newargv[i] = aargv[i - 1];
619 /*Slip in the opcode and the trailing null pointer, and bump the argument count up by one for the new opcode */
620 newargv[0] = aargv[0];
621 newargv[1] = pinitopcode;
623 newargv[*aargc] = NULL;
625 /*Return the happy news */
628 } /*InsertInitOpcode */
631 NoParmsOK(struct cmd_syndesc *as)
634 struct cmd_parmdesc *td;
636 for (i = 0; i < CMD_MAXPARMS; i++) {
638 if (td->type != 0 && !(td->flags & CMD_OPTIONAL)) {
639 /* found a non-optional (e.g. required) parm, so NoParmsOK
640 * is false (some parms are required) */
647 /* 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*/
649 cmd_Dispatch(int argc, char **argv)
652 struct cmd_syndesc *ts;
653 struct cmd_parmdesc *tparm;
658 static int initd = 0; /*Is this the first time this routine has been called? */
659 static int initcmdpossible = 1; /*Should be consider parsing the initial command? */
663 /* Add help, apropos commands once */
665 ts = cmd_CreateSyntax("help", HelpProc, (char *)0,
666 "get help on commands");
667 cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
668 cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, NULL);
670 ts = cmd_CreateSyntax("apropos", AproposProc, (char *)0,
671 "search by help text");
672 cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED,
674 ts = cmd_CreateSyntax("version", VersionProc, (char *)0,
676 ts = cmd_CreateSyntax("-version", VersionProc, (char *)0,
678 ts = cmd_CreateSyntax("-help", HelpProc, (char *)0,
680 ts = cmd_CreateSyntax("--version", VersionProc, (char *)0,
682 ts = cmd_CreateSyntax("--help", HelpProc, (char *)0,
687 /*Remember the program name */
692 if (!NoParmsOK(allSyntax)) {
693 printf("%s: Type '%s -help' for help\n", pname, pname);
699 /* if there is an initcmd, don't print an error message, just
700 * setup to use the initcmd below. */
701 if (!(initcmdpossible && FindSyntax(initcmd_opcode, (int *)0))) {
702 printf("%s: Type '%s help' or '%s help <topic>' for help\n",
703 pname, pname, pname);
709 /* Find the syntax descriptor for this command, doing prefix matching properly */
713 ts = (argc < 2 ? 0 : FindSyntax(argv[1], &ambig));
715 /*First token doesn't match a syntax descriptor */
716 if (initcmdpossible) {
717 /*If initial command line handling hasn't been done yet,
718 * see if there is a descriptor for the initialization opcode.
719 * Only try this once. */
721 ts = FindSyntax(initcmd_opcode, (int *)0);
723 /*There is no initialization opcode available, so we declare
726 fprintf(stderr, "%s", NName(pname, ": "));
728 "Ambiguous operation '%s'; type '%shelp' for list\n",
729 argv[1], NName(pname, " "));
731 fprintf(stderr, "%s", NName(pname, ": "));
733 "Unrecognized operation '%s'; type '%shelp' for list\n",
734 argv[1], NName(pname, " "));
736 return (CMD_UNKNOWNCMD);
738 /*Found syntax structure for an initialization opcode. Fix
739 * up argv and argc to relect what the user
740 * ``should have'' typed */
741 if (!(argv = InsertInitOpcode(&argc, argv))) {
743 "%sCan't insert implicit init opcode into command line\n",
745 return (CMD_INTERNALERROR);
748 } /*Initial opcode not yet attempted */
750 /* init cmd already run and no syntax entry found */
752 fprintf(stderr, "%s", NName(pname, ": "));
754 "Ambiguous operation '%s'; type '%shelp' for list\n",
755 argv[1], NName(pname, " "));
757 fprintf(stderr, "%s", NName(pname, ": "));
759 "Unrecognized operation '%s'; type '%shelp' for list\n",
760 argv[1], NName(pname, " "));
762 return CMD_UNKNOWNCMD;
764 } /*Argv[1] is not a valid opcode */
765 } /*Opcodes are defined */
767 /* Found the descriptor; start parsing. curType is the type we're trying to parse */
770 /* We start off parsing in "positional" mode, where tokens are put in
771 * slots positionally. If we find a name that takes args, we go
772 * out of positional mode, and from that point on, expect a switch
773 * before any particular token. */
774 positional = 1; /* Are we still in the positional region of the cmd line? */
775 i = noOpcodes ? 1 : 2;
776 SetupExpandsFlag(ts);
777 for (; i < argc; i++) {
778 /* Only tokens that start with a hyphen and are not followed by a digit
779 * are considered switches. This allow negative numbers. */
780 if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
782 j = FindType(ts, argv[i]);
785 "%sUnrecognized or ambiguous switch '%s'; type ",
786 NName(pname, ": "), argv[i]);
788 fprintf(stderr, "'%s -help' for detailed help\n",
791 fprintf(stderr, "'%shelp %s' for detailed help\n",
792 NName(argv[0], " "), ts->name);
794 return (CMD_UNKNOWNSWITCH);
796 if (j >= CMD_MAXPARMS) {
797 fprintf(stderr, "%sInternal parsing error\n",
800 return (CMD_INTERNALERROR);
802 if (ts->parms[j].type == CMD_FLAG) {
803 ts->parms[j].items = &dummy;
807 ts->parms[j].flags |= CMD_PROCESSED;
810 /* Try to fit in this descr */
811 if (curType >= CMD_MAXPARMS) {
812 fprintf(stderr, "%sToo many arguments\n", NName(pname, ": "));
814 return (CMD_TOOMANY);
816 tparm = &ts->parms[curType];
818 if ((tparm->type == 0) || /* No option in this slot */
819 (tparm->type == CMD_FLAG)) { /* A flag (not an argument */
820 /* skipped parm slot */
821 curType++; /* Skip this slot and reprocess this parm */
826 if (!(tparm->flags & CMD_PROCESSED) && (tparm->flags & CMD_HIDE)) {
827 curType++; /* Skip this slot and reprocess this parm */
832 if (tparm->type == CMD_SINGLE) {
834 fprintf(stderr, "%sToo many values after switch %s\n",
835 NName(pname, ": "), tparm->name);
837 return (CMD_NOTLIST);
839 AddItem(tparm, argv[i]); /* Add to end of list */
840 } else if (tparm->type == CMD_LIST) {
841 AddItem(tparm, argv[i]); /* Add to end of list */
843 /* Now, if we're in positional mode, advance to the next item */
845 curType = AdvanceType(ts, curType);
849 /* keep track of this for messages */
850 ts->a0name = argv[0];
852 /* If we make it here, all the parameters are filled in. Check to see if this
853 * is a -help version. Must do this before checking for all required parms,
854 * otherwise it is a real nuisance */
855 if (ts->parms[CMD_HELPPARM].items) {
857 /* Display full help syntax if we don't have subcommands */
864 /* Parsing done, see if we have all of our required parameters */
865 for (i = 0; i < CMD_MAXPARMS; i++) {
866 tparm = &ts->parms[i];
867 if (tparm->type == 0)
868 continue; /* Skipped parm slot */
869 if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
870 fprintf(stderr, "%s The field '%s' isn't completed properly\n",
871 NName(pname, ": "), tparm->name);
873 tparm->flags &= ~CMD_PROCESSED;
876 if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
877 fprintf(stderr, "%sMissing required parameter '%s'\n",
878 NName(pname, ": "), tparm->name);
880 tparm->flags &= ~CMD_PROCESSED;
883 tparm->flags &= ~CMD_PROCESSED;
887 * Before calling the beforeProc and afterProc and all the implications
888 * from those calls, check if the help procedure was called and call it now.
890 if ((ts->proc == HelpProc) || (ts->proc == AproposProc)) {
891 i = (*ts->proc) (ts, ts->rock);
896 /* Now, we just call the procedure and return */
898 i = (*beforeProc) (ts, beforeRock);
905 i = (*ts->proc) (ts, ts->rock);
907 (*afterProc) (ts, afterRock);
908 ResetSyntax(ts); /* Reset and free things */
912 /* free token list returned by parseLine */
914 FreeTokens(struct cmd_token *alist)
916 struct cmd_token *nlist;
917 for (; alist; alist = nlist) {
925 /* free an argv list returned by parseline */
927 cmd_FreeArgv(char **argv)
930 for (tp = *argv; tp; argv++, tp = *argv)
935 /* copy back the arg list to the argv array, freeing the cmd_tokens as you go; the actual
936 data is still malloc'd, and will be freed when the caller calls cmd_FreeArgv
940 CopyBackArgs(struct cmd_token *alist, char **argv,
941 afs_int32 * an, afs_int32 amaxn)
943 struct cmd_token *next;
949 *argv = (char *)malloc(strlen(INITSTR) + 1);
951 strcpy(*argv, INITSTR);
957 return CMD_TOOMANY; /* argv is too small for his many parms. */
966 *(argv++) = 0; /* use last slot for terminating null */
967 /* don't count terminating null */
975 if (x == '"' || x == 39 /* single quote */ )
984 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
991 cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
995 int inToken, inQuote;
996 struct cmd_token *first, *last;
997 struct cmd_token *ttok;
1000 inToken = 0; /* not copying token chars at start */
1001 first = (struct cmd_token *)0;
1002 last = (struct cmd_token *)0;
1003 inQuote = 0; /* not in a quoted string */
1006 if (tc == 0 || (!inQuote && space(tc))) { /* terminating null gets us in here, too */
1008 inToken = 0; /* end of this token */
1010 return -1; /* should never get here */
1013 ttok = (struct cmd_token *)malloc(sizeof(struct cmd_token));
1015 ttok->next = (struct cmd_token *)0;
1016 ttok->key = (char *)malloc(strlen(tbuffer) + 1);
1018 strcpy(ttok->key, tbuffer);
1028 /* an alpha character */
1033 if (tptr - tbuffer >= sizeof(tbuffer)) {
1035 return CMD_TOOBIG; /* token too long */
1038 /* hit a quote, toggle inQuote flag but don't insert character */
1041 /* insert character */
1046 /* last token flushed 'cause space(0) --> true */
1048 last->next = (struct cmd_token *)0;
1049 return CopyBackArgs(first, argv, an, amaxn);