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>
20 /* declaration of private token type */
22 struct cmd_token *next;
26 static struct cmd_item dummy; /* non-null ptr used for flag existence */
27 static struct cmd_syndesc *allSyntax = 0;
28 static int noOpcodes = 0;
29 static int (*beforeProc) (struct cmd_syndesc * ts, void *beforeRock) = NULL;
30 static int (*afterProc) (struct cmd_syndesc * ts, void *afterRock) = NULL;
31 static void *beforeRock, *afterRock;
32 static char initcmd_opcode[] = "initcmd"; /*Name of initcmd opcode */
34 /* take name and string, and return null string if name is empty, otherwise return
35 the concatenation of the two strings */
37 NName(char *a1, char *a2)
39 static char tbuffer[300];
40 if (strlen(a1) == 0) {
43 strncpy(tbuffer, a1, sizeof(tbuffer));
44 strncat(tbuffer, a2, sizeof(tbuffer));
45 tbuffer[sizeof(tbuffer)-1]='\0';
50 /* return true if asub is a substring of amain */
52 SubString(char *amain, char *asub)
56 mlen = (int) strlen(amain);
57 slen = (int) strlen(asub);
60 return 0; /* not a substring */
61 for (i = 0; i <= j; i++) {
62 if (strncmp(amain, asub, slen) == 0)
66 return 0; /* didn't find it */
70 FindType(struct cmd_syndesc *as, char *aname)
77 /* Allow --long-style options. */
78 if (aname[0] == '-' && aname[1] == '-' && aname[2] && aname[3]) {
82 cmdlen = strlen(aname);
85 for (i = 0; i < CMD_MAXPARMS; i++) {
86 if (as->parms[i].type == 0)
87 continue; /* this slot not set (seeked over) */
88 if (strcmp(as->parms[i].name, aname) == 0)
90 if (strlen(as->parms[i].name) < cmdlen)
92 /* A hidden option must be a full match (no best matches) */
93 if (as->parms[i].flags & CMD_HIDE)
96 if (strncmp(as->parms[i].name, aname, cmdlen) == 0) {
103 return (ambig ? -1 : best);
106 static struct cmd_syndesc *
107 FindSyntax(char *aname, int *aambig)
109 struct cmd_syndesc *ts;
110 struct cmd_syndesc *best;
114 cmdLen = strlen(aname);
115 best = (struct cmd_syndesc *)0;
118 *aambig = 0; /* initialize to unambiguous */
119 for (ts = allSyntax; ts; ts = ts->next) {
120 if (strcmp(aname, ts->name) == 0)
122 if (strlen(ts->name) < cmdLen)
123 continue; /* we typed more than item has */
124 /* A hidden command must be a full match (no best matches) */
125 if (ts->flags & CMD_HIDDEN)
128 /* This is just an alias for *best, or *best is just an alias for us.
129 * If we don't make this check explicitly, then an alias which is just a
130 * short prefix of the real command's name might make things ambiguous
131 * for no apparent reason.
133 if (best && ts->aliasOf == best->aliasOf)
135 if (strncmp(ts->name, aname, cmdLen) == 0) {
137 ambig = 1; /* ambiguous name */
144 *aambig = ambig; /* if ambiguous and they care, tell them */
145 return (struct cmd_syndesc *)0; /* fails */
147 return best; /* otherwise its not ambiguous, and they know */
150 /* print the help for a single parameter */
152 PrintParmHelp(struct cmd_parmdesc *aparm)
154 if (aparm->type == CMD_FLAG) {
156 /* doc people don't like seeing this information */
158 printf(" (%s)", aparm->help);
160 } else if (aparm->help) {
161 printf(" <%s>", aparm->help);
162 if (aparm->type == CMD_LIST)
164 } else if (aparm->type == CMD_SINGLE)
166 else if (aparm->type == CMD_LIST)
170 extern char *AFSVersion;
173 VersionProc(struct cmd_syndesc *as, void *arock)
175 printf("%s\n", AFSVersion);
180 PrintSyntax(struct cmd_syndesc *as)
183 struct cmd_parmdesc *tp;
185 /* now print usage, from syntax table */
187 printf("Usage: %s", as->a0name);
189 if (!strcmp(as->name, initcmd_opcode))
190 printf("Usage: %s[%s]", NName(as->a0name, " "), as->name);
192 printf("Usage: %s%s", NName(as->a0name, " "), as->name);
195 for (i = 0; i < CMD_MAXPARMS; i++) {
198 continue; /* seeked over slot */
199 if (tp->flags & CMD_HIDE)
200 continue; /* skip hidden options */
202 if (tp->flags & CMD_OPTIONAL)
204 printf("%s", tp->name);
206 if (tp->flags & CMD_OPTIONAL)
212 /* must print newline in any case, to terminate preceding line */
214 PrintAliases(struct cmd_syndesc *as)
216 struct cmd_syndesc *ts;
218 if (as->flags & CMD_ALIAS) {
220 printf("(alias for %s)\n", ts->name);
224 return; /* none, print nothing */
226 for (as = as->nextAlias; as; as = as->nextAlias) {
227 printf("%s ", as->name);
234 PrintFlagHelp(struct cmd_syndesc *as)
237 struct cmd_parmdesc *tp;
241 /* find flag name length */
243 for (i = 0; i < CMD_MAXPARMS; i++) {
244 if (i == CMD_HELPPARM)
247 if (tp->type != CMD_FLAG)
249 if (tp->flags & CMD_HIDE)
250 continue; /* skip hidden options */
254 if (strlen(tp->name) > flag_width)
255 flag_width = strlen(tp->name);
258 /* print flag help */
259 flag_prefix = "Where:";
260 for (i = 0; i < CMD_MAXPARMS; i++) {
261 if (i == CMD_HELPPARM)
264 if (tp->type != CMD_FLAG)
266 if (tp->flags & CMD_HIDE)
267 continue; /* skip hidden options */
271 printf("%-7s%-*s %s\n", flag_prefix, flag_width, tp->name, tp->help);
277 AproposProc(struct cmd_syndesc *as, void *arock)
279 struct cmd_syndesc *ts;
284 tsub = as->parms[0].items->data;
285 for (ts = allSyntax; ts; ts = ts->next) {
286 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
288 if (SubString(ts->help, tsub)) {
289 printf("%s: %s\n", ts->name, ts->help);
291 } else if (SubString(ts->name, tsub)) {
292 printf("%s: %s\n", ts->name, ts->help);
297 printf("Sorry, no commands found\n");
302 HelpProc(struct cmd_syndesc *as, void *arock)
304 struct cmd_syndesc *ts;
309 if (as->parms[0].items == 0) {
310 printf("%sCommands are:\n", NName(as->a0name, ": "));
311 for (ts = allSyntax; ts; ts = ts->next) {
312 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
314 printf("%-15s %s\n", ts->name, (ts->help ? ts->help : ""));
317 /* print out individual help topics */
318 for (ti = as->parms[0].items; ti; ti = ti->next) {
320 ts = FindSyntax(ti->data, &ambig);
321 if (ts && (ts->flags & CMD_HIDDEN))
322 ts = 0; /* no hidden commands */
324 /* print out command name and help */
325 printf("%s%s: %s ", NName(as->a0name, " "), ts->name,
326 (ts->help ? ts->help : ""));
327 ts->a0name = as->a0name;
333 fprintf(stderr, "%sUnknown topic '%s'\n",
334 NName(as->a0name, ": "), ti->data);
336 /* ambiguous, list 'em all */
338 "%sAmbiguous topic '%s'; use 'apropos' to list\n",
339 NName(as->a0name, ": "), ti->data);
341 code = CMD_UNKNOWNCMD;
349 cmd_SetBeforeProc(int (*aproc) (struct cmd_syndesc * ts, void *beforeRock),
358 cmd_SetAfterProc(int (*aproc) (struct cmd_syndesc * ts, void *afterRock),
366 /* thread on list in alphabetical order */
368 SortSyntax(struct cmd_syndesc *as)
370 struct cmd_syndesc **ld, *ud;
372 for (ld = &allSyntax, ud = *ld; ud; ld = &ud->next, ud = *ld) {
373 if (strcmp(ud->name, as->name) > 0) { /* next guy is bigger than us */
377 /* thread us on the list now */
384 cmd_CreateSyntax(char *aname,
385 int (*aproc) (struct cmd_syndesc * ts, void *arock),
386 void *arock, char *ahelp)
388 struct cmd_syndesc *td;
390 /* can't have two cmds in no opcode mode */
394 td = calloc(1, sizeof(struct cmd_syndesc));
396 td->aliasOf = td; /* treat aliasOf as pointer to real command, no matter what */
398 /* copy in name, etc */
400 td->name = malloc(strlen(aname) + 1);
402 strcpy(td->name, aname);
408 /* Piggy-back the hidden option onto the help string */
409 if (ahelp == (char *)CMD_HIDDEN) {
410 td->flags |= CMD_HIDDEN;
412 td->help = malloc(strlen(ahelp) + 1);
414 strcpy(td->help, ahelp);
423 cmd_Seek(td, CMD_HELPPARM);
424 cmd_AddParm(td, "-help", CMD_FLAG, CMD_OPTIONAL, "get detailed help");
431 cmd_CreateAlias(struct cmd_syndesc *as, char *aname)
433 struct cmd_syndesc *td;
435 td = malloc(sizeof(struct cmd_syndesc));
437 memcpy(td, as, sizeof(struct cmd_syndesc));
438 td->name = malloc(strlen(aname) + 1);
440 strcpy(td->name, aname);
441 td->flags |= CMD_ALIAS;
442 /* if ever free things, make copy of help string, too */
447 /* thread on alias lists */
448 td->nextAlias = as->nextAlias;
452 return 0; /* all done */
456 cmd_IsAdministratorCommand(struct cmd_syndesc *as)
458 as->flags |= CMD_ADMIN;
463 cmd_Seek(struct cmd_syndesc *as, int apos)
465 if (apos >= CMD_MAXPARMS)
466 return CMD_EXCESSPARMS;
472 cmd_AddParm(struct cmd_syndesc *as, char *aname, int atype,
473 afs_int32 aflags, char *ahelp)
475 struct cmd_parmdesc *tp;
477 if (as->nParms >= CMD_MAXPARMS)
478 return CMD_EXCESSPARMS;
479 tp = &as->parms[as->nParms++];
481 tp->name = malloc(strlen(aname) + 1);
483 strcpy(tp->name, aname);
488 tp->help = malloc(strlen(ahelp) + 1);
490 strcpy(tp->help, ahelp);
496 /* add a text item to the end of the parameter list */
498 AddItem(struct cmd_parmdesc *aparm, char *aval)
500 struct cmd_item *ti, *ni;
501 ti = calloc(1, sizeof(struct cmd_item));
503 ti->data = malloc(strlen(aval) + 1);
505 strcpy(ti->data, aval);
506 /* now put ti at the *end* of the list */
507 if ((ni = aparm->items)) {
508 for (; ni; ni = ni->next)
510 break; /* skip to last one */
513 aparm->items = ti; /* we're first */
517 /* skip to next non-flag item, if any */
519 AdvanceType(struct cmd_syndesc *as, afs_int32 aval)
522 struct cmd_parmdesc *tp;
524 /* first see if we should try to grab rest of line for this dude */
525 if (as->parms[aval].flags & CMD_EXPANDS)
528 /* if not, find next non-flag used slot */
529 for (next = aval + 1; next < CMD_MAXPARMS; next++) {
530 tp = &as->parms[next];
531 if (tp->type != 0 && tp->type != CMD_FLAG)
537 /* discard parameters filled in by dispatch */
539 ResetSyntax(struct cmd_syndesc *as)
542 struct cmd_parmdesc *tp;
543 struct cmd_item *ti, *ni;
546 for (i = 0; i < CMD_MAXPARMS; i++, tp++) {
550 /* free whole list in both cases, just for fun */
551 for (ti = tp->items; ti; ti = ni) {
565 /* move the expands flag to the last one in the list */
567 SetupExpandsFlag(struct cmd_syndesc *as)
569 struct cmd_parmdesc *tp;
573 /* find last CMD_LIST type parameter, optional or not, and make it expandable
574 * if no other dude is expandable */
575 for (i = 0; i < CMD_MAXPARMS; i++) {
577 if (tp->type == CMD_LIST) {
578 if (tp->flags & CMD_EXPANDS)
579 return 0; /* done if already specified */
584 as->parms[last].flags |= CMD_EXPANDS;
588 /* Take the current argv & argc and alter them so that the initialization
589 * opcode is made to appear. This is used in cases where the initialization
590 * 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
599 * more for the trailing null pointer */
600 newargv = malloc(((*aargc) + 2) * sizeof(char *));
602 fprintf(stderr, "%s: Can't create new argv array with %d+2 slots\n",
607 /* Create space for the initial opcode & fill it in */
608 pinitopcode = malloc(sizeof(initcmd_opcode));
610 fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]);
614 strcpy(pinitopcode, initcmd_opcode);
616 /* Move all the items in the old argv into the new argv, in their
618 for (i = *aargc; i > 1; i--)
619 newargv[i] = aargv[i - 1];
621 /* Slip in the opcode and the trailing null pointer, and bump the
622 * argument count up by one for the new opcode */
623 newargv[0] = aargv[0];
624 newargv[1] = pinitopcode;
626 newargv[*aargc] = NULL;
628 /* Return the happy news */
631 } /*InsertInitOpcode */
634 NoParmsOK(struct cmd_syndesc *as)
637 struct cmd_parmdesc *td;
639 for (i = 0; i < CMD_MAXPARMS; i++) {
641 if (td->type != 0 && !(td->flags & CMD_OPTIONAL)) {
642 /* found a non-optional (e.g. required) parm, so NoParmsOK
643 * is false (some parms are required) */
650 /* Call the appropriate function, or return syntax error code. Note: if
651 * no opcode is specified, an initialization routine exists, and it has
652 * NOT been called before, we invoke the special initialization opcode
655 cmd_Dispatch(int argc, char **argv)
658 struct cmd_syndesc *ts;
659 struct cmd_parmdesc *tparm;
664 static int initd = 0; /*Is this the first time this routine has been called? */
665 static int initcmdpossible = 1; /*Should be consider parsing the initial command? */
669 /* Add help, apropos commands once */
671 ts = cmd_CreateSyntax("help", HelpProc, NULL,
672 "get help on commands");
673 cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
674 cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, NULL);
676 ts = cmd_CreateSyntax("apropos", AproposProc, NULL,
677 "search by help text");
678 cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED,
680 ts = cmd_CreateSyntax("version", VersionProc, NULL,
682 ts = cmd_CreateSyntax("-version", VersionProc, NULL,
684 ts = cmd_CreateSyntax("-help", HelpProc, NULL,
686 ts = cmd_CreateSyntax("--version", VersionProc, NULL,
688 ts = cmd_CreateSyntax("--help", HelpProc, NULL,
693 /*Remember the program name */
698 if (!NoParmsOK(allSyntax)) {
699 printf("%s: Type '%s -help' for help\n", pname, pname);
705 /* if there is an initcmd, don't print an error message, just
706 * setup to use the initcmd below. */
707 if (!(initcmdpossible && FindSyntax(initcmd_opcode, (int *)0))) {
708 printf("%s: Type '%s help' or '%s help <topic>' for help\n",
709 pname, pname, pname);
715 /* Find the syntax descriptor for this command, doing prefix matching properly */
719 ts = (argc < 2 ? 0 : FindSyntax(argv[1], &ambig));
721 /*First token doesn't match a syntax descriptor */
722 if (initcmdpossible) {
723 /*If initial command line handling hasn't been done yet,
724 * see if there is a descriptor for the initialization opcode.
725 * Only try this once. */
727 ts = FindSyntax(initcmd_opcode, (int *)0);
729 /*There is no initialization opcode available, so we declare
732 fprintf(stderr, "%s", NName(pname, ": "));
734 "Ambiguous operation '%s'; type '%shelp' for list\n",
735 argv[1], NName(pname, " "));
737 fprintf(stderr, "%s", NName(pname, ": "));
739 "Unrecognized operation '%s'; type '%shelp' for list\n",
740 argv[1], NName(pname, " "));
742 return (CMD_UNKNOWNCMD);
744 /*Found syntax structure for an initialization opcode. Fix
745 * up argv and argc to relect what the user
746 * ``should have'' typed */
747 if (!(argv = InsertInitOpcode(&argc, argv))) {
749 "%sCan't insert implicit init opcode into command line\n",
751 return (CMD_INTERNALERROR);
754 } /*Initial opcode not yet attempted */
756 /* init cmd already run and no syntax entry found */
758 fprintf(stderr, "%s", NName(pname, ": "));
760 "Ambiguous operation '%s'; type '%shelp' for list\n",
761 argv[1], NName(pname, " "));
763 fprintf(stderr, "%s", NName(pname, ": "));
765 "Unrecognized operation '%s'; type '%shelp' for list\n",
766 argv[1], NName(pname, " "));
768 return CMD_UNKNOWNCMD;
770 } /*Argv[1] is not a valid opcode */
771 } /*Opcodes are defined */
773 /* Found the descriptor; start parsing. curType is the type we're
777 /* We start off parsing in "positional" mode, where tokens are put in
778 * slots positionally. If we find a name that takes args, we go
779 * out of positional mode, and from that point on, expect a switch
780 * before any particular token. */
782 positional = 1; /* Are we still in the positional region of the cmd line? */
783 i = noOpcodes ? 1 : 2;
784 SetupExpandsFlag(ts);
785 for (; i < argc; i++) {
786 /* Only tokens that start with a hyphen and are not followed by a digit
787 * are considered switches. This allow negative numbers. */
788 if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
790 j = FindType(ts, argv[i]);
793 "%sUnrecognized or ambiguous switch '%s'; type ",
794 NName(pname, ": "), argv[i]);
796 fprintf(stderr, "'%s -help' for detailed help\n",
799 fprintf(stderr, "'%shelp %s' for detailed help\n",
800 NName(argv[0], " "), ts->name);
802 return (CMD_UNKNOWNSWITCH);
804 if (j >= CMD_MAXPARMS) {
805 fprintf(stderr, "%sInternal parsing error\n",
808 return (CMD_INTERNALERROR);
810 if (ts->parms[j].type == CMD_FLAG) {
811 ts->parms[j].items = &dummy;
815 ts->parms[j].flags |= CMD_PROCESSED;
818 /* Try to fit in this descr */
819 if (curType >= CMD_MAXPARMS) {
820 fprintf(stderr, "%sToo many arguments\n", NName(pname, ": "));
822 return (CMD_TOOMANY);
824 tparm = &ts->parms[curType];
826 if ((tparm->type == 0) || /* No option in this slot */
827 (tparm->type == CMD_FLAG)) { /* A flag (not an argument */
828 /* skipped parm slot */
829 curType++; /* Skip this slot and reprocess this parm */
834 if (!(tparm->flags & CMD_PROCESSED) && (tparm->flags & CMD_HIDE)) {
835 curType++; /* Skip this slot and reprocess this parm */
840 if (tparm->type == CMD_SINGLE) {
842 fprintf(stderr, "%sToo many values after switch %s\n",
843 NName(pname, ": "), tparm->name);
845 return (CMD_NOTLIST);
847 AddItem(tparm, argv[i]); /* Add to end of list */
848 } else if (tparm->type == CMD_LIST) {
849 AddItem(tparm, argv[i]); /* Add to end of list */
851 /* Now, if we're in positional mode, advance to the next item */
853 curType = AdvanceType(ts, curType);
857 /* keep track of this for messages */
858 ts->a0name = argv[0];
860 /* If we make it here, all the parameters are filled in. Check to see if
861 * this is a -help version. Must do this before checking for all
862 * required parms, otherwise it is a real nuisance */
863 if (ts->parms[CMD_HELPPARM].items) {
865 /* Display full help syntax if we don't have subcommands */
872 /* Parsing done, see if we have all of our required parameters */
873 for (i = 0; i < CMD_MAXPARMS; i++) {
874 tparm = &ts->parms[i];
875 if (tparm->type == 0)
876 continue; /* Skipped parm slot */
877 if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
878 fprintf(stderr, "%s The field '%s' isn't completed properly\n",
879 NName(pname, ": "), tparm->name);
881 tparm->flags &= ~CMD_PROCESSED;
884 if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
885 fprintf(stderr, "%sMissing required parameter '%s'\n",
886 NName(pname, ": "), tparm->name);
888 tparm->flags &= ~CMD_PROCESSED;
891 tparm->flags &= ~CMD_PROCESSED;
895 * Before calling the beforeProc and afterProc and all the implications
896 * from those calls, check if the help procedure was called and call it
899 if ((ts->proc == HelpProc) || (ts->proc == AproposProc)) {
900 i = (*ts->proc) (ts, ts->rock);
905 /* Now, we just call the procedure and return */
907 i = (*beforeProc) (ts, beforeRock);
914 i = (*ts->proc) (ts, ts->rock);
916 (*afterProc) (ts, afterRock);
917 ResetSyntax(ts); /* Reset and free things */
921 /* free token list returned by parseLine */
923 FreeTokens(struct cmd_token *alist)
925 struct cmd_token *nlist;
926 for (; alist; alist = nlist) {
934 /* free an argv list returned by parseline */
936 cmd_FreeArgv(char **argv)
939 for (tp = *argv; tp; argv++, tp = *argv)
944 /* copy back the arg list to the argv array, freeing the cmd_tokens as you go;
945 * the actual data is still malloc'd, and will be freed when the caller calls
946 * cmd_FreeArgv later on
950 CopyBackArgs(struct cmd_token *alist, char **argv,
951 afs_int32 * an, afs_int32 amaxn)
953 struct cmd_token *next;
959 *argv = (char *)malloc(strlen(INITSTR) + 1);
961 strcpy(*argv, INITSTR);
967 return CMD_TOOMANY; /* argv is too small for his many parms. */
976 *(argv++) = 0; /* use last slot for terminating null */
977 /* don't count terminating null */
985 if (x == '"' || x == 39 /* single quote */ )
994 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
1001 cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
1005 int inToken, inQuote;
1006 struct cmd_token *first, *last;
1007 struct cmd_token *ttok;
1010 inToken = 0; /* not copying token chars at start */
1013 inQuote = 0; /* not in a quoted string */
1016 if (tc == 0 || (!inQuote && space(tc))) { /* terminating null gets us in here, too */
1018 inToken = 0; /* end of this token */
1020 return -1; /* should never get here */
1023 ttok = malloc(sizeof(struct cmd_token));
1026 ttok->key = malloc(strlen(tbuffer) + 1);
1028 strcpy(ttok->key, tbuffer);
1038 /* an alpha character */
1043 if (tptr - tbuffer >= sizeof(tbuffer)) {
1045 return CMD_TOOBIG; /* token too long */
1048 /* hit a quote, toggle inQuote flag but don't insert character */
1051 /* insert character */
1056 /* last token flushed 'cause space(0) --> true */
1059 return CopyBackArgs(first, argv, an, amaxn);