[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH] pk-cmd: Check for command in less arror-prone way
From: |
Jose E. Marchesi |
Subject: |
Re: [PATCH] pk-cmd: Check for command in less arror-prone way |
Date: |
Sat, 02 May 2020 18:49:13 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
Hi Tim.
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.
I usually use ALL_CAPS for both macro names and macro arguments,
especially for little utility macros like this. If you wouldn't mind to
use the same convention it would be great.
Otherwise the change looks ok to me!
Thanks!
---
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