}
/* print the help for a single parameter */
-static void
-PrintParmHelp(struct cmd_parmdesc *aparm)
+static char *
+ParmHelpString(struct cmd_parmdesc *aparm)
{
+ char *str;
if (aparm->type == CMD_FLAG) {
-#ifdef notdef
- /* doc people don't like seeing this information */
- if (aparm->help)
- printf(" (%s)", aparm->help);
-#endif
- } else if (aparm->help) {
- printf(" <%s>", aparm->help);
- if (aparm->type == CMD_LIST)
- printf("+");
- } else if (aparm->type == CMD_SINGLE)
- printf(" <arg>");
- else if (aparm->type == CMD_LIST)
- printf(" <arg>+");
+ return strdup("");
+ } else {
+ asprintf(&str, " %s<%s>%s%s",
+ aparm->type == CMD_SINGLE_OR_FLAG?"[":"",
+ aparm->help?aparm->help:"arg",
+ aparm->type == CMD_LIST?"+":"",
+ aparm->type == CMD_SINGLE_OR_FLAG?"]":"");
+ return str;
+ }
}
extern char *AFSVersion;
{
int i;
struct cmd_parmdesc *tp;
+ char *str;
+ size_t len;
+ size_t xtralen;
/* now print usage, from syntax table */
if (noOpcodes)
- printf("Usage: %s", as->a0name);
+ asprintf(&str, "Usage: %s", as->a0name);
else {
if (!strcmp(as->name, initcmd_opcode))
- printf("Usage: %s[%s]", NName(as->a0name, " "), as->name);
+ asprintf(&str, "Usage: %s[%s]", NName(as->a0name, " "), as->name);
else
- printf("Usage: %s%s", NName(as->a0name, " "), as->name);
+ asprintf(&str, "Usage: %s%s", NName(as->a0name, " "), as->name);
}
+ len = strlen(str);
+ printf("%s", str);
+ free(str);
+
for (i = 0; i < CMD_MAXPARMS; i++) {
tp = &as->parms[i];
if (tp->type == 0)
continue; /* seeked over slot */
if (tp->flags & CMD_HIDE)
continue; /* skip hidden options */
- printf(" ");
- if (tp->flags & CMD_OPTIONAL)
- printf("[");
- printf("%s", tp->name);
- PrintParmHelp(tp);
- if (tp->flags & CMD_OPTIONAL)
- printf("]");
+ /* Work out if we can fit what we want to on this line, or if we need to
+ * start a new one */
+ str = ParmHelpString(tp);
+ xtralen = 1 + strlen(tp->name) + strlen(str) +
+ ((tp->flags & CMD_OPTIONAL)? 2: 0);
+
+ if (len + xtralen > 78) {
+ printf("\n ");
+ len = 8;
+ }
+
+ printf(" %s%s%s%s",
+ tp->flags & CMD_OPTIONAL?"[":"",
+ tp->name,
+ str,
+ tp->flags & CMD_OPTIONAL?"]":"");
+ free(str);
+ len+=xtralen;
}
printf("\n");
}
/* add a text item to the end of the parameter list */
static int
-AddItem(struct cmd_parmdesc *aparm, char *aval)
+AddItem(struct cmd_parmdesc *aparm, char *aval, char *pname)
{
struct cmd_item *ti, *ni;
+
+ if (aparm->type == CMD_SINGLE ||
+ aparm->type == CMD_SINGLE_OR_FLAG) {
+ if (aparm->items) {
+ fprintf(stderr, "%sToo many values after switch %s\n",
+ NName(pname, ": "), aparm->name);
+ return CMD_NOTLIST;
+ }
+ }
+
ti = calloc(1, sizeof(struct cmd_item));
assert(ti);
ti->data = malloc(strlen(aval) + 1);
char *pname;
struct cmd_syndesc *ts = NULL;
struct cmd_parmdesc *tparm;
- afs_int32 i, j;
+ int i;
+ int j = 0;
int curType;
int positional;
int ambig;
int code = 0;
+ char *param = NULL;
+ char *embeddedvalue = NULL;
static int initd = 0; /*Is this the first time this routine has been called? */
static int initcmdpossible = 1; /*Should be consider parsing the initial command? */
i = noOpcodes ? 1 : 2;
SetupExpandsFlag(ts);
for (; i < argc; i++) {
+ if (param) {
+ free(param);
+ param = NULL;
+ embeddedvalue = NULL;
+ }
+
/* Only tokens that start with a hyphen and are not followed by a digit
* are considered switches. This allow negative numbers. */
+
if ((argv[i][0] == '-') && !isdigit(argv[i][1])) {
+
/* Find switch */
- j = FindType(ts, argv[i]);
+ if (strrchr(argv[i], '=') != NULL) {
+ param = strdup(argv[i]);
+ embeddedvalue = strrchr(param, '=');
+ *embeddedvalue = '\0';
+ embeddedvalue ++;
+ j = FindType(ts, param);
+ } else {
+ j = FindType(ts, argv[i]);
+ }
+
if (j < 0) {
fprintf(stderr,
"%sUnrecognized or ambiguous switch '%s'; type ",
}
if (ts->parms[j].type == CMD_FLAG) {
ts->parms[j].items = &dummy;
+
+ if (embeddedvalue) {
+ fprintf(stderr, "%sSwitch '%s' doesn't take an argument\n",
+ NName(pname, ": "), ts->parms[j].name);
+ code = CMD_TOOMANY;
+ goto out;
+ }
} else {
positional = 0;
curType = j;
ts->parms[j].flags |= CMD_PROCESSED;
+
+ if (embeddedvalue) {
+ AddItem(&ts->parms[curType], embeddedvalue, pname);
+ }
}
} else {
/* Try to fit in this descr */
continue;
}
- if (tparm->type == CMD_SINGLE ||
- tparm->type == CMD_SINGLE_OR_FLAG) {
- if (tparm->items) {
- fprintf(stderr, "%sToo many values after switch %s\n",
- NName(pname, ": "), tparm->name);
- code = CMD_NOTLIST;
+ if (ts->parms[j].type != CMD_FLAG) {
+ code = AddItem(tparm, argv[i], pname);
+ if (code)
goto out;
- }
- AddItem(tparm, argv[i]); /* Add to end of list */
- } else if (tparm->type == CMD_LIST) {
- AddItem(tparm, argv[i]); /* Add to end of list */
}
+
/* Now, if we're in positional mode, advance to the next item */
if (positional)
curType = AdvanceType(ts, curType);