]> git.pond.sub.org Git - empserver/blobdiff - src/lib/gen/parse.c
Update copyright notice
[empserver] / src / lib / gen / parse.c
index e978b155d9dbde6c0d738d4a755bf6cb4f594f86..1e9532e778dc6cf35917a6d7cb89a057c6424ece 100644 (file)
@@ -1,11 +1,11 @@
 /*
  *  Empire - A multi-player, client/server Internet based war game.
- *  Copyright (C) 1986-2000, Dave Pare, Jeff Bailey, Thomas Ruschak,
- *                           Ken Stevens, Steve McClure
+ *  Copyright (C) 1986-2017, Dave Pare, Jeff Bailey, Thomas Ruschak,
+ *                Ken Stevens, Steve McClure, Markus Armbruster
  *
- *  This program is free software; you can redistribute it and/or modify
+ *  Empire is free software: you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
- *  the Free Software Foundation; either version 2 of the License, or
+ *  the Free Software Foundation, either version 3 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  *  ---
  *
- *  See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the
- *  related information and legal notices. It is expected that any future
- *  projects/authors will amend these files as needed.
+ *  See files README, COPYING and CREDITS in the root of the source
+ *  tree for related information and legal notices.  It is expected
+ *  that future projects/authors will amend these files as needed.
  *
  *  ---
  *
  *  parse.c: Parse an Empire command line
- * 
+ *
  *  Known contributors to this file:
- *     
+ *
  */
 
-/*
- * parse empire command line, chop into argp
- * If values argpp and spacep passed, parse will use them.
- * otherwise, parse will use static space and global argp.
- * parse assumes that argpp is a char *buf[16], and that spacep
- * points to a buf of at least 256 bytes.
- */
+#include <config.h>
 
 #include <ctype.h>
-#include "misc.h"
-#include "gen.h"
+#include "prototypes.h"
 
+/*
+ * Parse user command in @buf.
+ * @buf is UTF-8.
+ * 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(register s_char *buf, s_char **argpp, s_char **condp, s_char *space,
-      s_char **redir)
+parse(char *buf, char *space, char **arg,
+      char **tail, char **condp, char **redir)
 {
-    register s_char *bp2;
-    register s_char *bp1 = space;
-    register s_char **arg = argpp;
-    int fs;
-    int quoted;
-    int argnum;
+    int i, quoted, argnum;
 
-    if (space == 0)
-       return -1;
     if (redir)
-       *redir = 0;
-    if (condp != 0)
-       *condp = 0;
-    for (argnum = 0; *buf && argnum < 100;) {
-       arg[argnum] = bp1;
-       argnum++;
-       while (isspace(*buf))
-           buf++;
+       *redir = NULL;
+    if (condp)
+       *condp = NULL;
+
+    for (argnum = 0; argnum < 127;) {
+       for (; isspace(*(unsigned char *)buf); buf++) ;
+       if (!*buf)
+           break;
+
+       /* recognize redirection syntax */
        if (redir && (*buf == '>' || *buf == '|')) {
            *redir = buf;
-           argnum--;
-           arg[argnum] = 0;
            break;
        }
+
+       if (condp && *buf == '?') {
+           buf++;
+           *condp = space;
+       } else {
+           if (tail)
+               tail[argnum] = buf;
+           arg[argnum++] = space;
+       }
+
+       /* copy argument */
        quoted = 0;
-       for (bp2 = bp1; *buf;) {
-           if (!quoted && isspace(*buf)) {
-               buf++;
-               break;
-           }
-           if (*buf == '"') {
+       for (; *buf && (quoted || !isspace(*(unsigned char *)buf)); buf++) {
+           if (*buf == '"')
                quoted = !quoted;
-               buf++;
-           } else {
-               *bp1++ = *buf++;
-           }
-       }
-       *bp1++ = 0;
-       if (*bp2 == '?' && condp != 0) {
-           *condp = bp2 + 1;
-           argnum--;
+           else if ((*buf >= 0x20 && *buf <= 0x7e) || *buf == '\t')
+               *space++ = *buf;
+           /* else funny character; ignore */
        }
+       *space++ = 0;
+    }
+
+    for (i = argnum; i < 128; i++) {
+       arg[i] = NULL;
+       if (tail)
+           tail[i] = NULL;
     }
-    arg[argnum] = 0;
-    for (fs = argnum + 1; fs < 16; fs++)
-       arg[fs] = 0;
+
     return argnum;
 }