poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] pk-cmd: Check for command in less arror-prone way


From: Tim Rühsen
Subject: [PATCH] pk-cmd: Check for command in less arror-prone way
Date: Sat, 2 May 2020 18:42:23 +0200

2020-05-02  Tim Rühsen  <address@hidden>

        * src/pk-cmd.c: Add macro is_command.
        (pk_cmd_exec): Use is_command to check for command.
---
 ChangeLog    |  5 +++++
 src/pk-cmd.c | 22 ++++++++++------------
 2 files changed, 15 insertions(+), 12 deletions(-)

diff --git a/src/pk-cmd.c b/src/pk-cmd.c
index 6a966041..dbe3f6f5 100644
--- a/src/pk-cmd.c
+++ b/src/pk-cmd.c
@@ -549,6 +549,10 @@ extern struct pk_trie *set_trie; /* pk-set.c */

 static struct pk_trie *cmds_trie;

+#define is_command(input, cmd) \
+  (strncmp ((input), (cmd), sizeof (cmd) - 1) == 0 \
+   && ((input)[sizeof (cmd) - 1] == ' ' || (input)[sizeof (cmd) - 1] == '\t'))
+
 int
 pk_cmd_exec (const char *str)
 {
@@ -567,25 +571,17 @@ pk_cmd_exec (const char *str)
       int what; /* 0 -> declaration, 1 -> statement */
       int retval = 1;

-      if (strncmp (ecmd, "defun ", 6) == 0
-          || strncmp (ecmd, "defun\t", 6) == 0)
+      if (is_command(ecmd, "defun"))
         what = 0;
       else
         {
-          if (strncmp (ecmd, "defvar ", 7) == 0
-              || strncmp (ecmd, "defvar\t", 7) == 0
-              || strncmp (ecmd, "deftype ", 8) == 0
-              || strncmp (ecmd, "deftype\t", 8) == 0
-              || strncmp (ecmd, "defunit ", 8) == 0
-              || strncmp (ecmd, "defunit\t", 8) == 0)
+          if (is_command(ecmd, "defvar")
+           || is_command(ecmd, "deftype")
+           || is_command(ecmd, "defunit"))
             what = 0;
           else
             what = 1;
-        }

-      if (strncmp (ecmd, "defun ", 6) != 0
-          && strncmp (ecmd, "defun\t", 6) != 0)
-        {
           cmd_alloc = pk_str_concat (cmd, ";", NULL);
           ecmd = cmd_alloc;
         }
@@ -614,6 +610,8 @@ pk_cmd_exec (const char *str)
       return retval;
     }
 }
+#undef is_command
+

 int
 pk_cmd_exec_script (const char *filename)
--
2.26.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]