command_util.bash: Kill make_proto().
authorAndre Noll <maan@tuebingen.mpg.de>
Tue, 13 Jan 2015 23:52:02 +0000 (00:52 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Wed, 12 Aug 2015 21:23:47 +0000 (23:23 +0200)
It is always a bad idea to parse C code with a regex in a script. In
addition, it is completely unnecessary in this case.

This commit changes command_util.bash to define, in addition to the
old XXX_CMD_ARRAY, another preprocessor macro XXX__COMMAND_HANDLERS
containing the comma separated list of command handlers without
any type information instead of grepping the source files. A simple
typedef is used to declare all command handlers.

Avoiding all the grep/sed calls reduces the (warm cache) make dep
time by ~10%.

audiod_command.c
command.c
command_util.bash
play.c

index 2b18837..68cc15f 100644 (file)
 extern struct sched sched;
 extern char *stat_item_values[NUM_STAT_ITEMS];
 
+typedef int audiod_command_handler_t(int, int, char **);
+static audiod_command_handler_t AUDIOD_COMMAND_HANDLERS;
+
 /* Defines one command of para_audiod. */
 struct audiod_command {
        const char *name;
        /* Pointer to the function that handles the command. */
-       int (*handler)(int, int, char **);
+       audiod_command_handler_t *handler;
        /* One-line description. */
        const char *description;
        /* Summary of the command line options. */
index 5d6a099..eac09f0 100644 (file)
--- a/command.c
+++ b/command.c
 #include "signal.h"
 #include "version.h"
 
+typedef int server_command_handler_t(struct command_context *);
+static server_command_handler_t SERVER_COMMAND_HANDLERS;
+server_command_handler_t AFS_COMMAND_HANDLERS;
+
 /* Defines one command of para_server. */
 struct server_command {
        /* The name of the command. */
        const char *name;
        /* Pointer to the function that handles the command. */
-       int (*handler)(struct command_context *);
+       server_command_handler_t *handler;
        /* The privileges a user must have to execute this command. */
        unsigned int perms;
        /* One-line description of the command. */
index e33e076..5e9dbcc 100755 (executable)
@@ -162,24 +162,9 @@ com_man()
        done
 }
 
-make_proto()
+cmd_handler_name()
 {
-       local regex='\(__noreturn \)*\(static \)*int com_'
-       local source_file match="" all_commands CR='
-'
-       if test -n "$prototype"; then
-               result="$prototype$CR"
-               return
-       fi
-       all_commands="$(cat $source_files | grep "$regex")"
-       result=
-       for source_file in $source_files; do
-               match=$(grep "$regex$name_txt(" <<< "$all_commands" | head -n 1 | sed -e 's/$/;/1')
-               if test -n "$match"; then
-                       result="$result$match$CR"
-                       break
-               fi
-       done
+       result="com_$name_txt,"
 }
 
 make_array_member()
@@ -239,7 +224,7 @@ template_loop()
 
 com_header()
 {
-       local array_members CR='
+       local array_members handlers= CR='
 '
 
        while : ; do
@@ -251,19 +236,20 @@ com_header()
                        break
                fi
                if test $template -eq 0; then
-                       make_proto
-                       printf "%s" "$result"
+                       cmd_handler_name
+                       handlers+="$result"
                        make_array_member
                        array_members="$array_members$result"
                        continue
                fi
-               template_loop make_proto
-               printf "%s" "$result"
+               template_loop cmd_handler_name
+               handlers+="$result"
                template_loop make_array_member
                array_members="$array_members$result"
        done
        array_members="$array_members{.name = NULL} \\$CR"
-       echo "#define DEFINE_$(tr 'a-z' 'A-Z' <<< "$base_name")_CMD_ARRAY $array_members"
+       echo "#define DEFINE_${base_name^^}_CMD_ARRAY $array_members"
+       echo "#define ${base_name^^}_COMMAND_HANDLERS ${handlers%,}"
 }
 
 com_completion()
diff --git a/play.c b/play.c
index 4fb2050..61c30ae 100644 (file)
--- a/play.c
+++ b/play.c
@@ -633,16 +633,20 @@ static char **get_mapped_keyseqs(void)
        return result;
 }
 
+#include "play.command_list.h"
+
+typedef int play_command_handler_t(struct play_task *, int, char**);
+static play_command_handler_t PLAY_COMMAND_HANDLERS;
+
 /* defines one command of para_play */
 struct pp_command {
        const char *name;
-       int (*handler)(struct play_task *, int, char**);
+       play_command_handler_t *handler;
        const char *description;
        const char *usage;
        const char *help;
 };
 
-#include "play.command_list.h"
 static struct pp_command pp_cmds[] = {DEFINE_PLAY_CMD_ARRAY};
 #define FOR_EACH_COMMAND(c) for (c = 0; pp_cmds[c].name; c++)