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 int enablePositional = 1;
32 static int enableAbbreviation = 1;
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)
78 struct cmd_item *alias;
80 /* Allow --long-style options. */
81 if (aname[0] == '-' && aname[1] == '-' && aname[2] && aname[3]) {
85 cmdlen = strlen(aname);
88 for (i = 0; i < CMD_MAXPARMS; i++) {
89 if (as->parms[i].type == 0)
90 continue; /* this slot not set (seeked over) */
91 if (strcmp(as->parms[i].name, aname) == 0)
93 if (strlen(as->parms[i].name) < cmdlen)
96 /* Check for aliases, which must be full matches */
97 alias = as->parms[i].aliases;
98 while (alias != NULL) {
99 if (strcmp(alias->data, aname) == 0)
104 /* A hidden option must be a full match (no best matches) */
105 if (as->parms[i].flags & CMD_HIDE || !enableAbbreviation)
108 if (strncmp(as->parms[i].name, aname, cmdlen) == 0) {
115 return (ambig ? -1 : best);
118 static struct cmd_syndesc *
119 FindSyntax(char *aname, int *aambig)
121 struct cmd_syndesc *ts;
122 struct cmd_syndesc *best;
126 cmdLen = strlen(aname);
127 best = (struct cmd_syndesc *)0;
130 *aambig = 0; /* initialize to unambiguous */
131 for (ts = allSyntax; ts; ts = ts->next) {
132 if (strcmp(aname, ts->name) == 0)
134 if (strlen(ts->name) < cmdLen)
135 continue; /* we typed more than item has */
136 /* A hidden command must be a full match (no best matches) */
137 if (ts->flags & CMD_HIDDEN)
140 /* This is just an alias for *best, or *best is just an alias for us.
141 * If we don't make this check explicitly, then an alias which is just a
142 * short prefix of the real command's name might make things ambiguous
143 * for no apparent reason.
145 if (best && ts->aliasOf == best->aliasOf)
147 if (strncmp(ts->name, aname, cmdLen) == 0) {
149 ambig = 1; /* ambiguous name */
156 *aambig = ambig; /* if ambiguous and they care, tell them */
157 return (struct cmd_syndesc *)0; /* fails */
159 return best; /* otherwise its not ambiguous, and they know */
162 /* print the help for a single parameter */
164 ParmHelpString(struct cmd_parmdesc *aparm)
167 if (aparm->type == CMD_FLAG) {
170 asprintf(&str, " %s<%s>%s%s",
171 aparm->type == CMD_SINGLE_OR_FLAG?"[":"",
172 aparm->help?aparm->help:"arg",
173 aparm->type == CMD_LIST?"+":"",
174 aparm->type == CMD_SINGLE_OR_FLAG?"]":"");
179 extern char *AFSVersion;
182 VersionProc(struct cmd_syndesc *as, void *arock)
184 printf("%s\n", AFSVersion);
189 PrintSyntax(struct cmd_syndesc *as)
192 struct cmd_parmdesc *tp;
197 /* now print usage, from syntax table */
199 asprintf(&str, "Usage: %s", as->a0name);
201 if (!strcmp(as->name, initcmd_opcode))
202 asprintf(&str, "Usage: %s[%s]", NName(as->a0name, " "), as->name);
204 asprintf(&str, "Usage: %s%s", NName(as->a0name, " "), as->name);
211 for (i = 0; i < CMD_MAXPARMS; i++) {
214 continue; /* seeked over slot */
215 if (tp->flags & CMD_HIDE)
216 continue; /* skip hidden options */
217 /* Work out if we can fit what we want to on this line, or if we need to
219 str = ParmHelpString(tp);
220 xtralen = 1 + strlen(tp->name) + strlen(str) +
221 ((tp->flags & CMD_OPTIONAL)? 2: 0);
223 if (len + xtralen > 78) {
229 tp->flags & CMD_OPTIONAL?"[":"",
232 tp->flags & CMD_OPTIONAL?"]":"");
239 /* must print newline in any case, to terminate preceding line */
241 PrintAliases(struct cmd_syndesc *as)
243 struct cmd_syndesc *ts;
245 if (as->flags & CMD_ALIAS) {
247 printf("(alias for %s)\n", ts->name);
251 return; /* none, print nothing */
253 for (as = as->nextAlias; as; as = as->nextAlias) {
254 printf("%s ", as->name);
261 PrintFlagHelp(struct cmd_syndesc *as)
264 struct cmd_parmdesc *tp;
268 /* find flag name length */
270 for (i = 0; i < CMD_MAXPARMS; i++) {
271 if (i == CMD_HELPPARM)
274 if (tp->type != CMD_FLAG)
276 if (tp->flags & CMD_HIDE)
277 continue; /* skip hidden options */
281 if (strlen(tp->name) > flag_width)
282 flag_width = strlen(tp->name);
285 /* print flag help */
286 flag_prefix = "Where:";
287 for (i = 0; i < CMD_MAXPARMS; i++) {
288 if (i == CMD_HELPPARM)
291 if (tp->type != CMD_FLAG)
293 if (tp->flags & CMD_HIDE)
294 continue; /* skip hidden options */
298 printf("%-7s%-*s %s\n", flag_prefix, flag_width, tp->name, tp->help);
304 AproposProc(struct cmd_syndesc *as, void *arock)
306 struct cmd_syndesc *ts;
311 tsub = as->parms[0].items->data;
312 for (ts = allSyntax; ts; ts = ts->next) {
313 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
315 if (SubString(ts->help, tsub)) {
316 printf("%s: %s\n", ts->name, ts->help);
318 } else if (SubString(ts->name, tsub)) {
319 printf("%s: %s\n", ts->name, ts->help);
324 printf("Sorry, no commands found\n");
329 HelpProc(struct cmd_syndesc *as, void *arock)
331 struct cmd_syndesc *ts;
336 if (as->parms[0].items == 0) {
337 printf("%sCommands are:\n", NName(as->a0name, ": "));
338 for (ts = allSyntax; ts; ts = ts->next) {
339 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
341 printf("%-15s %s\n", ts->name, (ts->help ? ts->help : ""));
344 /* print out individual help topics */
345 for (ti = as->parms[0].items; ti; ti = ti->next) {
347 ts = FindSyntax(ti->data, &ambig);
348 if (ts && (ts->flags & CMD_HIDDEN))
349 ts = 0; /* no hidden commands */
351 /* print out command name and help */
352 printf("%s%s: %s ", NName(as->a0name, " "), ts->name,
353 (ts->help ? ts->help : ""));
354 ts->a0name = as->a0name;
360 fprintf(stderr, "%sUnknown topic '%s'\n",
361 NName(as->a0name, ": "), ti->data);
363 /* ambiguous, list 'em all */
365 "%sAmbiguous topic '%s'; use 'apropos' to list\n",
366 NName(as->a0name, ": "), ti->data);
368 code = CMD_UNKNOWNCMD;
376 cmd_SetBeforeProc(int (*aproc) (struct cmd_syndesc * ts, void *beforeRock),
385 cmd_SetAfterProc(int (*aproc) (struct cmd_syndesc * ts, void *afterRock),
393 /* thread on list in alphabetical order */
395 SortSyntax(struct cmd_syndesc *as)
397 struct cmd_syndesc **ld, *ud;
399 for (ld = &allSyntax, ud = *ld; ud; ld = &ud->next, ud = *ld) {
400 if (strcmp(ud->name, as->name) > 0) { /* next guy is bigger than us */
404 /* thread us on the list now */
411 cmd_CreateSyntax(char *aname,
412 int (*aproc) (struct cmd_syndesc * ts, void *arock),
413 void *arock, char *ahelp)
415 struct cmd_syndesc *td;
417 /* can't have two cmds in no opcode mode */
421 td = calloc(1, sizeof(struct cmd_syndesc));
423 td->aliasOf = td; /* treat aliasOf as pointer to real command, no matter what */
425 /* copy in name, etc */
427 td->name = malloc(strlen(aname) + 1);
429 strcpy(td->name, aname);
435 /* Piggy-back the hidden option onto the help string */
436 if (ahelp == (char *)CMD_HIDDEN) {
437 td->flags |= CMD_HIDDEN;
439 td->help = malloc(strlen(ahelp) + 1);
441 strcpy(td->help, ahelp);
450 cmd_Seek(td, CMD_HELPPARM);
451 cmd_AddParm(td, "-help", CMD_FLAG, CMD_OPTIONAL, "get detailed help");
458 cmd_CreateAlias(struct cmd_syndesc *as, char *aname)
460 struct cmd_syndesc *td;
462 td = malloc(sizeof(struct cmd_syndesc));
464 memcpy(td, as, sizeof(struct cmd_syndesc));
465 td->name = malloc(strlen(aname) + 1);
467 strcpy(td->name, aname);
468 td->flags |= CMD_ALIAS;
469 /* if ever free things, make copy of help string, too */
474 /* thread on alias lists */
475 td->nextAlias = as->nextAlias;
479 return 0; /* all done */
483 cmd_DisablePositionalCommands(void)
485 enablePositional = 0;
489 cmd_DisableAbbreviations(void)
491 enableAbbreviation = 0;
495 cmd_IsAdministratorCommand(struct cmd_syndesc *as)
497 as->flags |= CMD_ADMIN;
502 cmd_Seek(struct cmd_syndesc *as, int apos)
504 if (apos >= CMD_MAXPARMS)
505 return CMD_EXCESSPARMS;
511 cmd_AddParmAtOffset(struct cmd_syndesc *as, char *aname, int atype,
512 afs_int32 aflags, char *ahelp, int ref)
514 struct cmd_parmdesc *tp;
516 if (ref >= CMD_MAXPARMS)
517 return CMD_EXCESSPARMS;
518 tp = &as->parms[ref];
520 tp->name = malloc(strlen(aname) + 1);
522 strcpy(tp->name, aname);
527 tp->help = malloc(strlen(ahelp) + 1);
529 strcpy(tp->help, ahelp);
535 if (as->nParms <= ref)
542 cmd_AddParm(struct cmd_syndesc *as, char *aname, int atype,
543 afs_int32 aflags, char *ahelp)
545 if (as->nParms >= CMD_MAXPARMS)
546 return CMD_EXCESSPARMS;
548 return cmd_AddParmAtOffset(as, aname, atype, aflags, ahelp, as->nParms++);
552 cmd_AddParmAlias(struct cmd_syndesc *as, int pos, char *alias)
554 struct cmd_item *item;
556 if (pos > as->nParms)
557 return CMD_EXCESSPARMS;
559 item = calloc(1, sizeof(struct cmd_item));
560 item->data = strdup(alias);
561 item->next = as->parms[pos].aliases;
562 as->parms[pos].aliases = item;
567 /* add a text item to the end of the parameter list */
569 AddItem(struct cmd_parmdesc *aparm, char *aval)
571 struct cmd_item *ti, *ni;
572 ti = calloc(1, sizeof(struct cmd_item));
574 ti->data = malloc(strlen(aval) + 1);
576 strcpy(ti->data, aval);
577 /* now put ti at the *end* of the list */
578 if ((ni = aparm->items)) {
579 for (; ni; ni = ni->next)
581 break; /* skip to last one */
584 aparm->items = ti; /* we're first */
588 /* skip to next non-flag item, if any */
590 AdvanceType(struct cmd_syndesc *as, afs_int32 aval)
593 struct cmd_parmdesc *tp;
595 /* first see if we should try to grab rest of line for this dude */
596 if (as->parms[aval].flags & CMD_EXPANDS)
599 /* if not, find next non-flag used slot */
600 for (next = aval + 1; next < CMD_MAXPARMS; next++) {
601 tp = &as->parms[next];
602 if (tp->type != 0 && tp->type != CMD_FLAG)
608 /* discard parameters filled in by dispatch */
610 ResetSyntax(struct cmd_syndesc *as)
613 struct cmd_parmdesc *tp;
614 struct cmd_item *ti, *ni;
617 for (i = 0; i < CMD_MAXPARMS; i++, tp++) {
619 case CMD_SINGLE_OR_FLAG:
620 if (tp->items == &dummy)
622 /* Deliberately fall through here */
625 /* free whole list in both cases, just for fun */
626 for (ti = tp->items; ti; ti = ni) {
640 /* move the expands flag to the last one in the list */
642 SetupExpandsFlag(struct cmd_syndesc *as)
644 struct cmd_parmdesc *tp;
648 /* find last CMD_LIST type parameter, optional or not, and make it expandable
649 * if no other dude is expandable */
650 for (i = 0; i < CMD_MAXPARMS; i++) {
652 if (tp->type == CMD_LIST) {
653 if (tp->flags & CMD_EXPANDS)
654 return 0; /* done if already specified */
659 as->parms[last].flags |= CMD_EXPANDS;
663 /* Take the current argv & argc and alter them so that the initialization
664 * opcode is made to appear. This is used in cases where the initialization
665 * opcode is implicitly invoked.*/
667 InsertInitOpcode(int *aargc, char **aargv)
669 char **newargv; /*Ptr to new, expanded argv space */
670 char *pinitopcode; /*Ptr to space for name of init opcode */
671 int i; /*Loop counter */
673 /* Allocate the new argv array, plus one for the new opcode, plus one
674 * more for the trailing null pointer */
675 newargv = malloc(((*aargc) + 2) * sizeof(char *));
677 fprintf(stderr, "%s: Can't create new argv array with %d+2 slots\n",
682 /* Create space for the initial opcode & fill it in */
683 pinitopcode = malloc(sizeof(initcmd_opcode));
685 fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]);
689 strcpy(pinitopcode, initcmd_opcode);
691 /* Move all the items in the old argv into the new argv, in their
693 for (i = *aargc; i > 1; i--)
694 newargv[i] = aargv[i - 1];
696 /* Slip in the opcode and the trailing null pointer, and bump the
697 * argument count up by one for the new opcode */
698 newargv[0] = aargv[0];
699 newargv[1] = pinitopcode;
701 newargv[*aargc] = NULL;
703 /* Return the happy news */
706 } /*InsertInitOpcode */
709 NoParmsOK(struct cmd_syndesc *as)
712 struct cmd_parmdesc *td;
714 for (i = 0; i < CMD_MAXPARMS; i++) {
716 if (td->type != 0 && !(td->flags & CMD_OPTIONAL)) {
717 /* found a non-optional (e.g. required) parm, so NoParmsOK
718 * is false (some parms are required) */
725 /* Add help, apropos commands once */
729 struct cmd_syndesc *ts;
732 ts = cmd_CreateSyntax("help", HelpProc, NULL,
733 "get help on commands");
734 cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
735 cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, NULL);
737 ts = cmd_CreateSyntax("apropos", AproposProc, NULL,
738 "search by help text");
739 cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED, "help string");
740 ts = cmd_CreateSyntax("version", VersionProc, NULL,
742 ts = cmd_CreateSyntax("-version", VersionProc, NULL,
744 ts = cmd_CreateSyntax("-help", HelpProc, NULL,
746 ts = cmd_CreateSyntax("--version", VersionProc, NULL,
748 ts = cmd_CreateSyntax("--help", HelpProc, NULL,
753 /* Call the appropriate function, or return syntax error code. Note: if
754 * no opcode is specified, an initialization routine exists, and it has
755 * NOT been called before, we invoke the special initialization opcode
758 cmd_Parse(int argc, char **argv, struct cmd_syndesc **outsyntax)
761 struct cmd_syndesc *ts = NULL;
762 struct cmd_parmdesc *tparm;
768 static int initd = 0; /*Is this the first time this routine has been called? */
769 static int initcmdpossible = 1; /*Should be consider parsing the initial command? */
778 /*Remember the program name */
783 if (!NoParmsOK(allSyntax)) {
784 printf("%s: Type '%s -help' for help\n", pname, pname);
791 /* if there is an initcmd, don't print an error message, just
792 * setup to use the initcmd below. */
793 if (!(initcmdpossible && FindSyntax(initcmd_opcode, NULL))) {
794 printf("%s: Type '%s help' or '%s help <topic>' for help\n",
795 pname, pname, pname);
802 /* Find the syntax descriptor for this command, doing prefix matching properly */
806 ts = (argc < 2 ? 0 : FindSyntax(argv[1], &ambig));
808 /*First token doesn't match a syntax descriptor */
809 if (initcmdpossible) {
810 /*If initial command line handling hasn't been done yet,
811 * see if there is a descriptor for the initialization opcode.
812 * Only try this once. */
814 ts = FindSyntax(initcmd_opcode, NULL);
816 /*There is no initialization opcode available, so we declare
819 fprintf(stderr, "%s", NName(pname, ": "));
821 "Ambiguous operation '%s'; type '%shelp' for list\n",
822 argv[1], NName(pname, " "));
824 fprintf(stderr, "%s", NName(pname, ": "));
826 "Unrecognized operation '%s'; type '%shelp' for list\n",
827 argv[1], NName(pname, " "));
829 code = CMD_UNKNOWNCMD;
832 /*Found syntax structure for an initialization opcode. Fix
833 * up argv and argc to relect what the user
834 * ``should have'' typed */
835 if (!(argv = InsertInitOpcode(&argc, argv))) {
837 "%sCan't insert implicit init opcode into command line\n",
839 code = CMD_INTERNALERROR;
843 } /*Initial opcode not yet attempted */
845 /* init cmd already run and no syntax entry found */
847 fprintf(stderr, "%s", NName(pname, ": "));
849 "Ambiguous operation '%s'; type '%shelp' for list\n",
850 argv[1], NName(pname, " "));
852 fprintf(stderr, "%s", NName(pname, ": "));
854 "Unrecognized operation '%s'; type '%shelp' for list\n",
855 argv[1], NName(pname, " "));
857 code = CMD_UNKNOWNCMD;
860 } /*Argv[1] is not a valid opcode */
861 } /*Opcodes are defined */
863 /* Found the descriptor; start parsing. curType is the type we're
867 /* We start off parsing in "positional" mode, where tokens are put in
868 * slots positionally. If we find a name that takes args, we go
869 * out of positional mode, and from that point on, expect a switch
870 * before any particular token. */
872 positional = enablePositional; /* Accepting positional cmds ? */
873 i = noOpcodes ? 1 : 2;
874 SetupExpandsFlag(ts);
875 for (; i < argc; i++) {
876 /* Only tokens that start with a hyphen and are not followed by a digit
877 * are considered switches. This allow negative numbers. */
878 if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
880 j = FindType(ts, argv[i]);
883 "%sUnrecognized or ambiguous switch '%s'; type ",
884 NName(pname, ": "), argv[i]);
886 fprintf(stderr, "'%s -help' for detailed help\n",
889 fprintf(stderr, "'%shelp %s' for detailed help\n",
890 NName(argv[0], " "), ts->name);
891 code = CMD_UNKNOWNSWITCH;
894 if (j >= CMD_MAXPARMS) {
895 fprintf(stderr, "%sInternal parsing error\n",
897 code = CMD_INTERNALERROR;
900 if (ts->parms[j].type == CMD_FLAG) {
901 ts->parms[j].items = &dummy;
905 ts->parms[j].flags |= CMD_PROCESSED;
908 /* Try to fit in this descr */
909 if (curType >= CMD_MAXPARMS) {
910 fprintf(stderr, "%sToo many arguments\n", NName(pname, ": "));
914 tparm = &ts->parms[curType];
916 if ((tparm->type == 0) || /* No option in this slot */
917 (tparm->type == CMD_FLAG)) { /* A flag (not an argument */
918 /* skipped parm slot */
919 curType++; /* Skip this slot and reprocess this parm */
924 if (!(tparm->flags & CMD_PROCESSED) && (tparm->flags & CMD_HIDE)) {
925 curType++; /* Skip this slot and reprocess this parm */
930 if (tparm->type == CMD_SINGLE ||
931 tparm->type == CMD_SINGLE_OR_FLAG) {
933 fprintf(stderr, "%sToo many values after switch %s\n",
934 NName(pname, ": "), tparm->name);
938 AddItem(tparm, argv[i]); /* Add to end of list */
939 } else if (tparm->type == CMD_LIST) {
940 AddItem(tparm, argv[i]); /* Add to end of list */
942 /* Now, if we're in positional mode, advance to the next item */
944 curType = AdvanceType(ts, curType);
948 /* keep track of this for messages */
949 ts->a0name = argv[0];
951 /* If we make it here, all the parameters are filled in. Check to see if
952 * this is a -help version. Must do this before checking for all
953 * required parms, otherwise it is a real nuisance */
954 if (ts->parms[CMD_HELPPARM].items) {
956 /* Display full help syntax if we don't have subcommands */
963 /* Parsing done, see if we have all of our required parameters */
964 for (i = 0; i < CMD_MAXPARMS; i++) {
965 tparm = &ts->parms[i];
966 if (tparm->type == 0)
967 continue; /* Skipped parm slot */
968 if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
969 if (tparm->type == CMD_SINGLE_OR_FLAG) {
970 tparm->items = &dummy;
972 fprintf(stderr, "%s The field '%s' isn't completed properly\n",
973 NName(pname, ": "), tparm->name);
978 if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
979 fprintf(stderr, "%sMissing required parameter '%s'\n",
980 NName(pname, ": "), tparm->name);
984 tparm->flags &= ~CMD_PROCESSED;
989 if (code && ts != NULL)
996 cmd_Dispatch(int argc, char **argv)
998 struct cmd_syndesc *ts = NULL;
1001 code = cmd_Parse(argc, argv, &ts);
1006 * Before calling the beforeProc and afterProc and all the implications
1007 * from those calls, check if the help procedure was called and call it
1010 if ((ts->proc == HelpProc) || (ts->proc == AproposProc)) {
1011 code = (*ts->proc) (ts, ts->rock);
1015 /* Now, we just call the procedure and return */
1017 code = (*beforeProc) (ts, beforeRock);
1022 code = (*ts->proc) (ts, ts->rock);
1025 (*afterProc) (ts, afterRock);
1027 cmd_FreeOptions(&ts);
1032 cmd_FreeOptions(struct cmd_syndesc **ts)
1040 /* free token list returned by parseLine */
1042 FreeTokens(struct cmd_token *alist)
1044 struct cmd_token *nlist;
1045 for (; alist; alist = nlist) {
1046 nlist = alist->next;
1053 /* free an argv list returned by parseline */
1055 cmd_FreeArgv(char **argv)
1058 for (tp = *argv; tp; argv++, tp = *argv)
1063 /* copy back the arg list to the argv array, freeing the cmd_tokens as you go;
1064 * the actual data is still malloc'd, and will be freed when the caller calls
1065 * cmd_FreeArgv later on
1069 CopyBackArgs(struct cmd_token *alist, char **argv,
1070 afs_int32 * an, afs_int32 amaxn)
1072 struct cmd_token *next;
1078 *argv = (char *)malloc(strlen(INITSTR) + 1);
1080 strcpy(*argv, INITSTR);
1086 return CMD_TOOMANY; /* argv is too small for his many parms. */
1095 *(argv++) = 0; /* use last slot for terminating null */
1096 /* don't count terminating null */
1104 if (x == '"' || x == 39 /* single quote */ )
1113 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
1120 cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
1124 int inToken, inQuote;
1125 struct cmd_token *first, *last;
1126 struct cmd_token *ttok;
1129 inToken = 0; /* not copying token chars at start */
1132 inQuote = 0; /* not in a quoted string */
1135 if (tc == 0 || (!inQuote && space(tc))) { /* terminating null gets us in here, too */
1137 inToken = 0; /* end of this token */
1139 return -1; /* should never get here */
1142 ttok = malloc(sizeof(struct cmd_token));
1145 ttok->key = malloc(strlen(tbuffer) + 1);
1147 strcpy(ttok->key, tbuffer);
1157 /* an alpha character */
1162 if (tptr - tbuffer >= sizeof(tbuffer)) {
1164 return CMD_TOOBIG; /* token too long */
1167 /* hit a quote, toggle inQuote flag but don't insert character */
1170 /* insert character */
1175 /* last token flushed 'cause space(0) --> true */
1178 return CopyBackArgs(first, argv, an, amaxn);
1184 cmd_OptionAsInt(struct cmd_syndesc *syn, int pos, int *value)
1186 if (pos > syn->nParms)
1187 return CMD_EXCESSPARMS;
1188 if (syn->parms[pos].items == NULL ||
1189 syn->parms[pos].items->data == NULL)
1191 if (syn->parms[pos].items == &dummy)
1194 *value = strtol(syn->parms[pos].items->data, NULL, 10);
1200 cmd_OptionAsString(struct cmd_syndesc *syn, int pos, char **value)
1202 if (pos > syn->nParms)
1203 return CMD_EXCESSPARMS;
1204 if (syn->parms[pos].items == NULL || syn->parms[pos].items->data == NULL)
1206 if (syn->parms[pos].items == &dummy)
1212 *value = strdup(syn->parms[pos].items->data);
1218 cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **value)
1220 if (pos > syn->nParms)
1221 return CMD_EXCESSPARMS;
1222 if (syn->parms[pos].items == NULL)
1225 *value = syn->parms[pos].items;
1230 cmd_OptionAsFlag(struct cmd_syndesc *syn, int pos, int *value)
1232 if (pos > syn->nParms)
1233 return CMD_EXCESSPARMS;
1234 if (syn->parms[pos].items == NULL)
1242 cmd_OptionPresent(struct cmd_syndesc *syn, int pos)
1244 if (pos > syn->nParms || syn->parms[pos].items == NULL)