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 PrintParmHelp(struct cmd_parmdesc *aparm)
166 if (aparm->type == CMD_FLAG) {
168 /* doc people don't like seeing this information */
170 printf(" (%s)", aparm->help);
172 } else if (aparm->help) {
173 printf(" <%s>", aparm->help);
174 if (aparm->type == CMD_LIST)
176 } else if (aparm->type == CMD_SINGLE)
178 else if (aparm->type == CMD_LIST)
182 extern char *AFSVersion;
185 VersionProc(struct cmd_syndesc *as, void *arock)
187 printf("%s\n", AFSVersion);
192 PrintSyntax(struct cmd_syndesc *as)
195 struct cmd_parmdesc *tp;
197 /* now print usage, from syntax table */
199 printf("Usage: %s", as->a0name);
201 if (!strcmp(as->name, initcmd_opcode))
202 printf("Usage: %s[%s]", NName(as->a0name, " "), as->name);
204 printf("Usage: %s%s", NName(as->a0name, " "), as->name);
207 for (i = 0; i < CMD_MAXPARMS; i++) {
210 continue; /* seeked over slot */
211 if (tp->flags & CMD_HIDE)
212 continue; /* skip hidden options */
214 if (tp->flags & CMD_OPTIONAL)
216 printf("%s", tp->name);
218 if (tp->flags & CMD_OPTIONAL)
224 /* must print newline in any case, to terminate preceding line */
226 PrintAliases(struct cmd_syndesc *as)
228 struct cmd_syndesc *ts;
230 if (as->flags & CMD_ALIAS) {
232 printf("(alias for %s)\n", ts->name);
236 return; /* none, print nothing */
238 for (as = as->nextAlias; as; as = as->nextAlias) {
239 printf("%s ", as->name);
246 PrintFlagHelp(struct cmd_syndesc *as)
249 struct cmd_parmdesc *tp;
253 /* find flag name length */
255 for (i = 0; i < CMD_MAXPARMS; i++) {
256 if (i == CMD_HELPPARM)
259 if (tp->type != CMD_FLAG)
261 if (tp->flags & CMD_HIDE)
262 continue; /* skip hidden options */
266 if (strlen(tp->name) > flag_width)
267 flag_width = strlen(tp->name);
270 /* print flag help */
271 flag_prefix = "Where:";
272 for (i = 0; i < CMD_MAXPARMS; i++) {
273 if (i == CMD_HELPPARM)
276 if (tp->type != CMD_FLAG)
278 if (tp->flags & CMD_HIDE)
279 continue; /* skip hidden options */
283 printf("%-7s%-*s %s\n", flag_prefix, flag_width, tp->name, tp->help);
289 AproposProc(struct cmd_syndesc *as, void *arock)
291 struct cmd_syndesc *ts;
296 tsub = as->parms[0].items->data;
297 for (ts = allSyntax; ts; ts = ts->next) {
298 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
300 if (SubString(ts->help, tsub)) {
301 printf("%s: %s\n", ts->name, ts->help);
303 } else if (SubString(ts->name, tsub)) {
304 printf("%s: %s\n", ts->name, ts->help);
309 printf("Sorry, no commands found\n");
314 HelpProc(struct cmd_syndesc *as, void *arock)
316 struct cmd_syndesc *ts;
321 if (as->parms[0].items == 0) {
322 printf("%sCommands are:\n", NName(as->a0name, ": "));
323 for (ts = allSyntax; ts; ts = ts->next) {
324 if ((ts->flags & CMD_ALIAS) || (ts->flags & CMD_HIDDEN))
326 printf("%-15s %s\n", ts->name, (ts->help ? ts->help : ""));
329 /* print out individual help topics */
330 for (ti = as->parms[0].items; ti; ti = ti->next) {
332 ts = FindSyntax(ti->data, &ambig);
333 if (ts && (ts->flags & CMD_HIDDEN))
334 ts = 0; /* no hidden commands */
336 /* print out command name and help */
337 printf("%s%s: %s ", NName(as->a0name, " "), ts->name,
338 (ts->help ? ts->help : ""));
339 ts->a0name = as->a0name;
345 fprintf(stderr, "%sUnknown topic '%s'\n",
346 NName(as->a0name, ": "), ti->data);
348 /* ambiguous, list 'em all */
350 "%sAmbiguous topic '%s'; use 'apropos' to list\n",
351 NName(as->a0name, ": "), ti->data);
353 code = CMD_UNKNOWNCMD;
361 cmd_SetBeforeProc(int (*aproc) (struct cmd_syndesc * ts, void *beforeRock),
370 cmd_SetAfterProc(int (*aproc) (struct cmd_syndesc * ts, void *afterRock),
378 /* thread on list in alphabetical order */
380 SortSyntax(struct cmd_syndesc *as)
382 struct cmd_syndesc **ld, *ud;
384 for (ld = &allSyntax, ud = *ld; ud; ld = &ud->next, ud = *ld) {
385 if (strcmp(ud->name, as->name) > 0) { /* next guy is bigger than us */
389 /* thread us on the list now */
396 cmd_CreateSyntax(char *aname,
397 int (*aproc) (struct cmd_syndesc * ts, void *arock),
398 void *arock, char *ahelp)
400 struct cmd_syndesc *td;
402 /* can't have two cmds in no opcode mode */
406 td = calloc(1, sizeof(struct cmd_syndesc));
408 td->aliasOf = td; /* treat aliasOf as pointer to real command, no matter what */
410 /* copy in name, etc */
412 td->name = malloc(strlen(aname) + 1);
414 strcpy(td->name, aname);
420 /* Piggy-back the hidden option onto the help string */
421 if (ahelp == (char *)CMD_HIDDEN) {
422 td->flags |= CMD_HIDDEN;
424 td->help = malloc(strlen(ahelp) + 1);
426 strcpy(td->help, ahelp);
435 cmd_Seek(td, CMD_HELPPARM);
436 cmd_AddParm(td, "-help", CMD_FLAG, CMD_OPTIONAL, "get detailed help");
443 cmd_CreateAlias(struct cmd_syndesc *as, char *aname)
445 struct cmd_syndesc *td;
447 td = malloc(sizeof(struct cmd_syndesc));
449 memcpy(td, as, sizeof(struct cmd_syndesc));
450 td->name = malloc(strlen(aname) + 1);
452 strcpy(td->name, aname);
453 td->flags |= CMD_ALIAS;
454 /* if ever free things, make copy of help string, too */
459 /* thread on alias lists */
460 td->nextAlias = as->nextAlias;
464 return 0; /* all done */
468 cmd_DisablePositionalCommands(void)
470 enablePositional = 0;
474 cmd_DisableAbbreviations(void)
476 enableAbbreviation = 0;
480 cmd_IsAdministratorCommand(struct cmd_syndesc *as)
482 as->flags |= CMD_ADMIN;
487 cmd_Seek(struct cmd_syndesc *as, int apos)
489 if (apos >= CMD_MAXPARMS)
490 return CMD_EXCESSPARMS;
496 cmd_AddParmAtOffset(struct cmd_syndesc *as, char *aname, int atype,
497 afs_int32 aflags, char *ahelp, int ref)
499 struct cmd_parmdesc *tp;
501 if (ref >= CMD_MAXPARMS)
502 return CMD_EXCESSPARMS;
503 tp = &as->parms[ref];
505 tp->name = malloc(strlen(aname) + 1);
507 strcpy(tp->name, aname);
512 tp->help = malloc(strlen(ahelp) + 1);
514 strcpy(tp->help, ahelp);
520 if (as->nParms <= ref)
527 cmd_AddParm(struct cmd_syndesc *as, char *aname, int atype,
528 afs_int32 aflags, char *ahelp)
530 if (as->nParms >= CMD_MAXPARMS)
531 return CMD_EXCESSPARMS;
533 return cmd_AddParmAtOffset(as, aname, atype, aflags, ahelp, as->nParms++);
537 cmd_AddParmAlias(struct cmd_syndesc *as, int pos, char *alias)
539 struct cmd_item *item;
541 if (pos > as->nParms)
542 return CMD_EXCESSPARMS;
544 item = calloc(1, sizeof(struct cmd_item));
545 item->data = strdup(alias);
546 item->next = as->parms[pos].aliases;
547 as->parms[pos].aliases = item;
552 /* add a text item to the end of the parameter list */
554 AddItem(struct cmd_parmdesc *aparm, char *aval)
556 struct cmd_item *ti, *ni;
557 ti = calloc(1, sizeof(struct cmd_item));
559 ti->data = malloc(strlen(aval) + 1);
561 strcpy(ti->data, aval);
562 /* now put ti at the *end* of the list */
563 if ((ni = aparm->items)) {
564 for (; ni; ni = ni->next)
566 break; /* skip to last one */
569 aparm->items = ti; /* we're first */
573 /* skip to next non-flag item, if any */
575 AdvanceType(struct cmd_syndesc *as, afs_int32 aval)
578 struct cmd_parmdesc *tp;
580 /* first see if we should try to grab rest of line for this dude */
581 if (as->parms[aval].flags & CMD_EXPANDS)
584 /* if not, find next non-flag used slot */
585 for (next = aval + 1; next < CMD_MAXPARMS; next++) {
586 tp = &as->parms[next];
587 if (tp->type != 0 && tp->type != CMD_FLAG)
593 /* discard parameters filled in by dispatch */
595 ResetSyntax(struct cmd_syndesc *as)
598 struct cmd_parmdesc *tp;
599 struct cmd_item *ti, *ni;
602 for (i = 0; i < CMD_MAXPARMS; i++, tp++) {
604 case CMD_SINGLE_OR_FLAG:
605 if (tp->items == &dummy)
607 /* Deliberately fall through here */
610 /* free whole list in both cases, just for fun */
611 for (ti = tp->items; ti; ti = ni) {
625 /* move the expands flag to the last one in the list */
627 SetupExpandsFlag(struct cmd_syndesc *as)
629 struct cmd_parmdesc *tp;
633 /* find last CMD_LIST type parameter, optional or not, and make it expandable
634 * if no other dude is expandable */
635 for (i = 0; i < CMD_MAXPARMS; i++) {
637 if (tp->type == CMD_LIST) {
638 if (tp->flags & CMD_EXPANDS)
639 return 0; /* done if already specified */
644 as->parms[last].flags |= CMD_EXPANDS;
648 /* Take the current argv & argc and alter them so that the initialization
649 * opcode is made to appear. This is used in cases where the initialization
650 * opcode is implicitly invoked.*/
652 InsertInitOpcode(int *aargc, char **aargv)
654 char **newargv; /*Ptr to new, expanded argv space */
655 char *pinitopcode; /*Ptr to space for name of init opcode */
656 int i; /*Loop counter */
658 /* Allocate the new argv array, plus one for the new opcode, plus one
659 * more for the trailing null pointer */
660 newargv = malloc(((*aargc) + 2) * sizeof(char *));
662 fprintf(stderr, "%s: Can't create new argv array with %d+2 slots\n",
667 /* Create space for the initial opcode & fill it in */
668 pinitopcode = malloc(sizeof(initcmd_opcode));
670 fprintf(stderr, "%s: Can't malloc initial opcode space\n", aargv[0]);
674 strcpy(pinitopcode, initcmd_opcode);
676 /* Move all the items in the old argv into the new argv, in their
678 for (i = *aargc; i > 1; i--)
679 newargv[i] = aargv[i - 1];
681 /* Slip in the opcode and the trailing null pointer, and bump the
682 * argument count up by one for the new opcode */
683 newargv[0] = aargv[0];
684 newargv[1] = pinitopcode;
686 newargv[*aargc] = NULL;
688 /* Return the happy news */
691 } /*InsertInitOpcode */
694 NoParmsOK(struct cmd_syndesc *as)
697 struct cmd_parmdesc *td;
699 for (i = 0; i < CMD_MAXPARMS; i++) {
701 if (td->type != 0 && !(td->flags & CMD_OPTIONAL)) {
702 /* found a non-optional (e.g. required) parm, so NoParmsOK
703 * is false (some parms are required) */
710 /* Add help, apropos commands once */
714 struct cmd_syndesc *ts;
717 ts = cmd_CreateSyntax("help", HelpProc, NULL,
718 "get help on commands");
719 cmd_AddParm(ts, "-topic", CMD_LIST, CMD_OPTIONAL, "help string");
720 cmd_AddParm(ts, "-admin", CMD_FLAG, CMD_OPTIONAL, NULL);
722 ts = cmd_CreateSyntax("apropos", AproposProc, NULL,
723 "search by help text");
724 cmd_AddParm(ts, "-topic", CMD_SINGLE, CMD_REQUIRED, "help string");
725 ts = cmd_CreateSyntax("version", VersionProc, NULL,
727 ts = cmd_CreateSyntax("-version", VersionProc, NULL,
729 ts = cmd_CreateSyntax("-help", HelpProc, NULL,
731 ts = cmd_CreateSyntax("--version", VersionProc, NULL,
733 ts = cmd_CreateSyntax("--help", HelpProc, NULL,
738 /* Call the appropriate function, or return syntax error code. Note: if
739 * no opcode is specified, an initialization routine exists, and it has
740 * NOT been called before, we invoke the special initialization opcode
743 cmd_Parse(int argc, char **argv, struct cmd_syndesc **outsyntax)
746 struct cmd_syndesc *ts = NULL;
747 struct cmd_parmdesc *tparm;
753 static int initd = 0; /*Is this the first time this routine has been called? */
754 static int initcmdpossible = 1; /*Should be consider parsing the initial command? */
763 /*Remember the program name */
768 if (!NoParmsOK(allSyntax)) {
769 printf("%s: Type '%s -help' for help\n", pname, pname);
776 /* if there is an initcmd, don't print an error message, just
777 * setup to use the initcmd below. */
778 if (!(initcmdpossible && FindSyntax(initcmd_opcode, NULL))) {
779 printf("%s: Type '%s help' or '%s help <topic>' for help\n",
780 pname, pname, pname);
787 /* Find the syntax descriptor for this command, doing prefix matching properly */
791 ts = (argc < 2 ? 0 : FindSyntax(argv[1], &ambig));
793 /*First token doesn't match a syntax descriptor */
794 if (initcmdpossible) {
795 /*If initial command line handling hasn't been done yet,
796 * see if there is a descriptor for the initialization opcode.
797 * Only try this once. */
799 ts = FindSyntax(initcmd_opcode, NULL);
801 /*There is no initialization opcode available, so we declare
804 fprintf(stderr, "%s", NName(pname, ": "));
806 "Ambiguous operation '%s'; type '%shelp' for list\n",
807 argv[1], NName(pname, " "));
809 fprintf(stderr, "%s", NName(pname, ": "));
811 "Unrecognized operation '%s'; type '%shelp' for list\n",
812 argv[1], NName(pname, " "));
814 code = CMD_UNKNOWNCMD;
817 /*Found syntax structure for an initialization opcode. Fix
818 * up argv and argc to relect what the user
819 * ``should have'' typed */
820 if (!(argv = InsertInitOpcode(&argc, argv))) {
822 "%sCan't insert implicit init opcode into command line\n",
824 code = CMD_INTERNALERROR;
828 } /*Initial opcode not yet attempted */
830 /* init cmd already run and no syntax entry found */
832 fprintf(stderr, "%s", NName(pname, ": "));
834 "Ambiguous operation '%s'; type '%shelp' for list\n",
835 argv[1], NName(pname, " "));
837 fprintf(stderr, "%s", NName(pname, ": "));
839 "Unrecognized operation '%s'; type '%shelp' for list\n",
840 argv[1], NName(pname, " "));
842 code = CMD_UNKNOWNCMD;
845 } /*Argv[1] is not a valid opcode */
846 } /*Opcodes are defined */
848 /* Found the descriptor; start parsing. curType is the type we're
852 /* We start off parsing in "positional" mode, where tokens are put in
853 * slots positionally. If we find a name that takes args, we go
854 * out of positional mode, and from that point on, expect a switch
855 * before any particular token. */
857 positional = enablePositional; /* Accepting positional cmds ? */
858 i = noOpcodes ? 1 : 2;
859 SetupExpandsFlag(ts);
860 for (; i < argc; i++) {
861 /* Only tokens that start with a hyphen and are not followed by a digit
862 * are considered switches. This allow negative numbers. */
863 if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
865 j = FindType(ts, argv[i]);
868 "%sUnrecognized or ambiguous switch '%s'; type ",
869 NName(pname, ": "), argv[i]);
871 fprintf(stderr, "'%s -help' for detailed help\n",
874 fprintf(stderr, "'%shelp %s' for detailed help\n",
875 NName(argv[0], " "), ts->name);
876 code = CMD_UNKNOWNSWITCH;
879 if (j >= CMD_MAXPARMS) {
880 fprintf(stderr, "%sInternal parsing error\n",
882 code = CMD_INTERNALERROR;
885 if (ts->parms[j].type == CMD_FLAG) {
886 ts->parms[j].items = &dummy;
890 ts->parms[j].flags |= CMD_PROCESSED;
893 /* Try to fit in this descr */
894 if (curType >= CMD_MAXPARMS) {
895 fprintf(stderr, "%sToo many arguments\n", NName(pname, ": "));
899 tparm = &ts->parms[curType];
901 if ((tparm->type == 0) || /* No option in this slot */
902 (tparm->type == CMD_FLAG)) { /* A flag (not an argument */
903 /* skipped parm slot */
904 curType++; /* Skip this slot and reprocess this parm */
909 if (!(tparm->flags & CMD_PROCESSED) && (tparm->flags & CMD_HIDE)) {
910 curType++; /* Skip this slot and reprocess this parm */
915 if (tparm->type == CMD_SINGLE ||
916 tparm->type == CMD_SINGLE_OR_FLAG) {
918 fprintf(stderr, "%sToo many values after switch %s\n",
919 NName(pname, ": "), tparm->name);
923 AddItem(tparm, argv[i]); /* Add to end of list */
924 } else if (tparm->type == CMD_LIST) {
925 AddItem(tparm, argv[i]); /* Add to end of list */
927 /* Now, if we're in positional mode, advance to the next item */
929 curType = AdvanceType(ts, curType);
933 /* keep track of this for messages */
934 ts->a0name = argv[0];
936 /* If we make it here, all the parameters are filled in. Check to see if
937 * this is a -help version. Must do this before checking for all
938 * required parms, otherwise it is a real nuisance */
939 if (ts->parms[CMD_HELPPARM].items) {
941 /* Display full help syntax if we don't have subcommands */
948 /* Parsing done, see if we have all of our required parameters */
949 for (i = 0; i < CMD_MAXPARMS; i++) {
950 tparm = &ts->parms[i];
951 if (tparm->type == 0)
952 continue; /* Skipped parm slot */
953 if ((tparm->flags & CMD_PROCESSED) && tparm->items == 0) {
954 if (tparm->type == CMD_SINGLE_OR_FLAG) {
955 tparm->items = &dummy;
957 fprintf(stderr, "%s The field '%s' isn't completed properly\n",
958 NName(pname, ": "), tparm->name);
963 if (!(tparm->flags & CMD_OPTIONAL) && tparm->items == 0) {
964 fprintf(stderr, "%sMissing required parameter '%s'\n",
965 NName(pname, ": "), tparm->name);
969 tparm->flags &= ~CMD_PROCESSED;
974 if (code && ts != NULL)
981 cmd_Dispatch(int argc, char **argv)
983 struct cmd_syndesc *ts = NULL;
986 code = cmd_Parse(argc, argv, &ts);
991 * Before calling the beforeProc and afterProc and all the implications
992 * from those calls, check if the help procedure was called and call it
995 if ((ts->proc == HelpProc) || (ts->proc == AproposProc)) {
996 code = (*ts->proc) (ts, ts->rock);
1000 /* Now, we just call the procedure and return */
1002 code = (*beforeProc) (ts, beforeRock);
1007 code = (*ts->proc) (ts, ts->rock);
1010 (*afterProc) (ts, afterRock);
1012 cmd_FreeOptions(&ts);
1017 cmd_FreeOptions(struct cmd_syndesc **ts)
1025 /* free token list returned by parseLine */
1027 FreeTokens(struct cmd_token *alist)
1029 struct cmd_token *nlist;
1030 for (; alist; alist = nlist) {
1031 nlist = alist->next;
1038 /* free an argv list returned by parseline */
1040 cmd_FreeArgv(char **argv)
1043 for (tp = *argv; tp; argv++, tp = *argv)
1048 /* copy back the arg list to the argv array, freeing the cmd_tokens as you go;
1049 * the actual data is still malloc'd, and will be freed when the caller calls
1050 * cmd_FreeArgv later on
1054 CopyBackArgs(struct cmd_token *alist, char **argv,
1055 afs_int32 * an, afs_int32 amaxn)
1057 struct cmd_token *next;
1063 *argv = (char *)malloc(strlen(INITSTR) + 1);
1065 strcpy(*argv, INITSTR);
1071 return CMD_TOOMANY; /* argv is too small for his many parms. */
1080 *(argv++) = 0; /* use last slot for terminating null */
1081 /* don't count terminating null */
1089 if (x == '"' || x == 39 /* single quote */ )
1098 if (x == 0 || x == ' ' || x == '\t' || x == '\n')
1105 cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn)
1109 int inToken, inQuote;
1110 struct cmd_token *first, *last;
1111 struct cmd_token *ttok;
1114 inToken = 0; /* not copying token chars at start */
1117 inQuote = 0; /* not in a quoted string */
1120 if (tc == 0 || (!inQuote && space(tc))) { /* terminating null gets us in here, too */
1122 inToken = 0; /* end of this token */
1124 return -1; /* should never get here */
1127 ttok = malloc(sizeof(struct cmd_token));
1130 ttok->key = malloc(strlen(tbuffer) + 1);
1132 strcpy(ttok->key, tbuffer);
1142 /* an alpha character */
1147 if (tptr - tbuffer >= sizeof(tbuffer)) {
1149 return CMD_TOOBIG; /* token too long */
1152 /* hit a quote, toggle inQuote flag but don't insert character */
1155 /* insert character */
1160 /* last token flushed 'cause space(0) --> true */
1163 return CopyBackArgs(first, argv, an, amaxn);
1169 cmd_OptionAsInt(struct cmd_syndesc *syn, int pos, int *value)
1171 if (pos > syn->nParms)
1172 return CMD_EXCESSPARMS;
1173 if (syn->parms[pos].items == NULL ||
1174 syn->parms[pos].items->data == NULL)
1176 if (syn->parms[pos].items == &dummy)
1179 *value = strtol(syn->parms[pos].items->data, NULL, 10);
1185 cmd_OptionAsString(struct cmd_syndesc *syn, int pos, char **value)
1187 if (pos > syn->nParms)
1188 return CMD_EXCESSPARMS;
1189 if (syn->parms[pos].items == NULL || syn->parms[pos].items->data == NULL)
1191 if (syn->parms[pos].items == &dummy)
1197 *value = strdup(syn->parms[pos].items->data);
1203 cmd_OptionAsList(struct cmd_syndesc *syn, int pos, struct cmd_item **value)
1205 if (pos > syn->nParms)
1206 return CMD_EXCESSPARMS;
1207 if (syn->parms[pos].items == NULL)
1210 *value = syn->parms[pos].items;
1215 cmd_OptionAsFlag(struct cmd_syndesc *syn, int pos, int *value)
1217 if (pos > syn->nParms)
1218 return CMD_EXCESSPARMS;
1219 if (syn->parms[pos].items == NULL)
1227 cmd_OptionPresent(struct cmd_syndesc *syn, int pos)
1229 if (pos > syn->nParms || syn->parms[pos].items == NULL)