]> git.draconx.ca Git - cdecl99.git/commitdiff
Avoid modifying the input command string.
authorNick Bowler <nbowler@draconx.ca>
Wed, 13 Jul 2011 00:41:15 +0000 (20:41 -0400)
committerNick Bowler <nbowler@draconx.ca>
Wed, 13 Jul 2011 00:41:15 +0000 (20:41 -0400)
This gives the individual command functions access to the entire
(unadultered) input from the user.

src/cdecl99.c

index 5c67ed8de9af74456901115c4f57fd41e9503c98..2df7ec8c98543293a7c980178a3fd6b28a9cfd89 100644 (file)
@@ -92,7 +92,7 @@ retry:
        return buf;
 }
 
-static int cmd_explain(char *cmd, char *arg)
+static int cmd_explain(const char *cmd, const char *arg)
 {
        struct cdecl *decl;
        const char *str;
@@ -116,7 +116,7 @@ out:
        return ret;
 }
 
-static int cmd_simplify(char *cmd, char *arg)
+static int cmd_simplify(const char *cmd, const char *arg)
 {
        struct cdecl *decl;
        const char *str;
@@ -151,16 +151,16 @@ out:
        return ret;
 }
 
-static int cmd_quit(char *cmd, char *arg)
+static int cmd_quit(const char *cmd, const char *arg)
 {
        return 0;
 }
 
-static int cmd_help(char *cmd, char *arg);
+static int cmd_help(const char *cmd, const char *arg);
 
 static const struct command {
        char name[16];
-       int (*func)(char *cmd, char *arg);
+       int (*func)(const char *cmd, const char *arg);
        const char *blurb;
 } commands[] = {
        { "explain",  cmd_explain,  "Explain a C declaration." },
@@ -171,7 +171,7 @@ static const struct command {
 };
 static const size_t ncommands = sizeof commands / sizeof commands[0];
 
-static int cmd_help(char *cmd, char *arg)
+static int cmd_help(const char *cmd, const char *arg)
 {
        for (size_t i = 0; i < ncommands; i++) {
                if (!commands[i].blurb)
@@ -183,24 +183,37 @@ static int cmd_help(char *cmd, char *arg)
        return 1;
 }
 
-static int run_command(char *line)
+/*
+ * Ensure that the first n characters of str equal the entire (null-terminated)
+ * string cmd.
+ */
+static int cmd_cmp(const char *str, const char *cmd, size_t n)
+{
+       size_t cmdlen = strlen(cmd);
+
+       if (n < cmdlen)
+               return -1;
+       if (n > cmdlen)
+               return 1;
+       return memcmp(str, cmd, n);
+}
+
+static int run_command(const char *line)
 {
-       char *cmd = line + strspn(line, " \t");
-       char *arg = cmd  + strcspn(cmd, " \t");
+       const char *cmd = line + strspn(line, " \t");
+       const char *arg = cmd  + strcspn(cmd, " \t");
 
        if (cmd[0] == '\0')
                return 1;
-       if (arg[0] != '\0')
-               *arg++ = '\0';
 
        for (size_t i = 0; i < ncommands; i++) {
-               if (strcmp(cmd, commands[i].name) != 0)
+               if (cmd_cmp(cmd, commands[i].name, arg-cmd) != 0)
                        continue;
 
                return commands[i].func(cmd, arg);
        }
 
-       fprintf(stderr, "Undefined command: %s\n", cmd);
+       fprintf(stderr, "Undefined command: %.*s\n", (int)(arg-cmd), cmd);
        return -1;
 }