diff --git a/include/player.h b/include/player.h index 78b1a61c..ede5ad72 100644 --- a/include/player.h +++ b/include/player.h @@ -60,11 +60,12 @@ struct player { natid cnum; int state; int flags; - struct cmndstr *command; + struct cmndstr *command; /* currently executing command */ struct iop *iop; char combuf[1024]; /* command input buffer, UTF-8 */ - char *argp[128]; /* arguments, ASCII */ - char *condarg; /* conditional, ASCII */ + char *argp[128]; /* arguments, ASCII, valid if command */ + char *condarg; /* conditional, ASCII, valid if command */ + char *comtail[128]; /* start of args in combuf[] */ time_t lasttime; /* when minleft was last debited */ int ncomstat; int minleft; diff --git a/include/prototypes.h b/include/prototypes.h index 670ea38c..3c1d3d63 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -368,7 +368,7 @@ extern int deltay(int, int); extern int mapdist(int, int, int, int); extern char *effadv(int); extern int onearg(char *, char *); -extern int parse(char *, char **, char **, char *, char **); +extern int parse(char *, char *, char **, char **, char **, char **); extern int ldround(double, int); extern int roundintby(int, int); extern int scthash(int, int, int); diff --git a/src/lib/commands/navi.c b/src/lib/commands/navi.c index 0d0dcc40..1f589dc1 100644 --- a/src/lib/commands/navi.c +++ b/src/lib/commands/navi.c @@ -183,7 +183,7 @@ do_unit_move(struct emp_qelem *ulist, int *together, cp++; continue; } - ac = parse(cp, player->argp, NULL, scanspace, NULL); + ac = parse(cp, scanspace, player->argp, NULL, NULL, NULL); if (ac <= 1) { sprintf(dp, "%d", leader->uid); player->argp[1] = dp; diff --git a/src/lib/gen/emp_config.c b/src/lib/gen/emp_config.c index 37ba63ba..b9651b3e 100644 --- a/src/lib/gen/emp_config.c +++ b/src/lib/gen/emp_config.c @@ -99,7 +99,7 @@ emp_config(char *file) for (i = 0; buf[i] && isspace(buf[i]); ++i) ; if (!buf[i] || buf[i] == '#') continue; - if (parse(buf, av, NULL, scanspace, NULL) < 0) { + if (parse(buf, scanspace, av, NULL, NULL, NULL) < 0) { fprintf(stderr, "%s:%d: Can't parse line %s", file, lno, buf); errors = 1; continue; diff --git a/src/lib/gen/parse.c b/src/lib/gen/parse.c index fc8a7003..526e4469 100644 --- a/src/lib/gen/parse.c +++ b/src/lib/gen/parse.c @@ -39,21 +39,22 @@ /* * Parse user command in BUF. * BUF is UTF-8. - * Set ARG[0] to point to the command name. - * Set ARG[1..N] to point to arguments, where N is the number of - * arguments. Set ARG[N+1..127] to NULL. - * If CONDP is not null, recognize conditional argument syntax, and - * set *CONDP to the conditional argument if present, else NULL. * Command name and arguments are copied into SPACE[], whose size must * be at least strlen(BUF) + 1. + * Set ARG[0] to the zero-terminated command name. + * Set ARG[1..N] to the zero-terminated arguments, where N is the + * number of arguments. Set ARG[N+1..127] to NULL. + * Set TAIL[0..N] to beginning of ARG[0] in BUF[]. + * If CONDP is not null, recognize conditional argument syntax, and + * set *CONDP to the conditional argument if present, else NULL. * If REDIR is not null, recognize redirection syntax, and set *REDIR * to UTF-8 redirection string if present, else NULL. * Return number of slots used in ARG[], or -1 on error. */ int -parse(char *buf, char **arg, char **condp, char *space, char **redir) +parse(char *buf, char *space, char **arg, + char **tail, char **condp, char **redir) { - char *ap; int i, quoted, argnum; if (redir) @@ -72,9 +73,17 @@ parse(char *buf, char **arg, char **condp, char *space, char **redir) break; } + if (condp && *buf == '?') { + buf++; + *condp = space; + } else { + if (tail) + tail[argnum] = buf; + arg[argnum++] = space; + } + /* copy argument */ quoted = 0; - ap = space; for (; *buf && (quoted || !isspace(*(unsigned char *)buf)); buf++) { if (*buf == '"') quoted = !quoted; @@ -83,16 +92,13 @@ parse(char *buf, char **arg, char **condp, char *space, char **redir) /* else funny character; ignore */ } *space++ = 0; - - /* store copied argument as conditional or regular argument */ - if (condp && *ap == '?') - *condp = ap + 1; - else - arg[argnum++] = ap; } - for (i = argnum; i < 128; i++) + for (i = argnum; i < 128; i++) { arg[i] = NULL; + if (tail) + tail[i] = NULL; + } return argnum; } diff --git a/src/lib/player/login.c b/src/lib/player/login.c index bf30d7c6..64b24e71 100644 --- a/src/lib/player/login.c +++ b/src/lib/player/login.c @@ -91,7 +91,7 @@ player_login(void *ud) io_input(player->iop, IO_WAIT); continue; } - ac = parse(buf, player->argp, NULL, space, NULL); + ac = parse(buf, space, player->argp, NULL, NULL, NULL); cmd = comtch(player->argp[0], login_coms, 0); if (cmd < 0) { pr_id(player, C_BADCMD, "Command %s not found\n", player->argp[0]); diff --git a/src/lib/player/player.c b/src/lib/player/player.c index 4f925342..8d3762c4 100644 --- a/src/lib/player/player.c +++ b/src/lib/player/player.c @@ -141,8 +141,8 @@ command(void) if (getcommand(player->combuf) < 0) return 0; - if (parse(player->combuf, player->argp, &player->condarg, - scanspace, &redir) < 0) { + if (parse(player->combuf, scanspace, player->argp, player->comtail, + &player->condarg, &redir) < 0) { pr("See \"info Syntax\"?\n"); } else { if (dispatch(player->combuf, redir) < 0) @@ -287,8 +287,8 @@ execute(void) while (!failed && status()) { if (recvclient(buf, sizeof(buf)) < 0) break; - if (parse(buf, player->argp, &player->condarg, - scanspace, &redir) < 0) { + if (parse(buf, scanspace, player->argp, player->comtail, + &player->condarg, &redir) < 0) { failed = 1; continue; }