summaryrefslogtreecommitdiff
path: root/src/common/HPM.c
diff options
context:
space:
mode:
authorHaru <haru@dotalux.com>2014-11-09 02:07:09 +0100
committerHaru <haru@dotalux.com>2015-01-18 21:35:36 +0100
commitce08d6238d902590dbfb650f889a8ab8887356bf (patch)
tree218266957f961e71a51a307b432b02ed9647243a /src/common/HPM.c
parent0285ddf7cee2f6569a513fe6118c43f99fd71279 (diff)
downloadhercules-ce08d6238d902590dbfb650f889a8ab8887356bf.tar.gz
hercules-ce08d6238d902590dbfb650f889a8ab8887356bf.tar.bz2
hercules-ce08d6238d902590dbfb650f889a8ab8887356bf.tar.xz
hercules-ce08d6238d902590dbfb650f889a8ab8887356bf.zip
Command line arguments handling overhaul
- login_server, char_server, map_server as well as the tools (mapcache) now have a common command line arguments handling mechanism. - All of them now accept `--help` (`-h`), `--version` (`-v`) and `--load-plugin`. - login_server now accepts `--login-config` and `--lan-config` instead of relying on positional arguments to override those files. The old syntax will no longer work, please update your custom startup scripts. - char_server now accepts `--char-config`, `--inter-config`, `--lan-config` instead of relying on positional arguments. The old syntax will no longer work, please update your custom startup scripts. - mapcache now accepts `--grf-list`, `--map-list`, `--map-cache`, `--rebuild` in place of, respectively, `-grf`, `-list`, `-cache`, `-rebuild`. - A new macro `CMDLINEARG()` is provided, to help defining new command line argument handlers (i.e. in plugins). the `addArg()` call is still required, but its syntax has changed. The `help` argument is now of type `const char *` rather than a function pointer, and it is supposed to contain the message to show in the `--help` screen. Pass `NULL` if no help message is desired. Signed-off-by: Haru <haru@dotalux.com>
Diffstat (limited to 'src/common/HPM.c')
-rw-r--r--src/common/HPM.c104
1 files changed, 48 insertions, 56 deletions
diff --git a/src/common/HPM.c b/src/common/HPM.c
index 6d9a2cb6b..10208cdad 100644
--- a/src/common/HPM.c
+++ b/src/common/HPM.c
@@ -293,7 +293,20 @@ void hplugin_unload(struct hplugin* plugin) {
}
}
-void hplugins_config_read(const char * const *extra_plugins, int extra_plugins_count) {
+/**
+ * Adds a plugin requested from the command line to the auto-load list.
+ */
+CMDLINEARG(loadplugin)
+{
+ RECREATE(HPM->cmdline_plugins, char *, ++HPM->cmdline_plugins_count);
+ HPM->cmdline_plugins[HPM->cmdline_plugins_count-1] = aStrdup(params);
+ return true;
+}
+
+/**
+ * Reads the plugin configuration and loads the plugins as necessary.
+ */
+void hplugins_config_read(void) {
config_t plugins_conf;
config_setting_t *plist = NULL;
const char *config_filename = "conf/plugins.conf"; // FIXME hardcoded name
@@ -313,9 +326,9 @@ void hplugins_config_read(const char * const *extra_plugins, int extra_plugins_c
HPM->symbol_defaults_sub();
plist = libconfig->lookup(&plugins_conf, "plugins_list");
- for (i = 0; i < extra_plugins_count; i++) {
+ for (i = 0; i < HPM->cmdline_plugins_count; i++) {
config_setting_t *entry = libconfig->setting_add(plist, NULL, CONFIG_TYPE_STRING);
- config_setting_set_string(entry, extra_plugins[i]);
+ config_setting_set_string(entry, HPM->cmdline_plugins[i]);
}
if (plist != NULL) {
@@ -562,6 +575,9 @@ unsigned char hplugins_parse_packets(int fd, enum HPluginPacketHookingPoints poi
char *hplugins_id2name (unsigned int pid) {
unsigned int i;
+ if (pid == HPM_PID_CORE)
+ return "core";
+
for( i = 0; i < HPM->plugin_count; i++ ) {
if( HPM->plugins[i]->idx == pid )
return HPM->plugins[i]->info->name;
@@ -624,50 +640,32 @@ void HPM_HookStop (const char *func, unsigned int pID) {
bool HPM_HookStopped (void) {
return HPM->force_return;
}
-/* command-line args */
-bool hpm_parse_arg(const char *arg, int *index, char *argv[], bool param) {
- struct HPMArgData *data;
-
- if( (data = strdb_get(HPM->arg_db,arg)) ) {
- data->func((data->has_param && param)?argv[(*index)+1]:NULL);
- if( data->has_param && param ) *index += 1;
- return true;
- }
-
- return false;
-}
-void hpm_arg_help(void) {
- DBIterator *iter = db_iterator(HPM->arg_db);
- struct HPMArgData *data = NULL;
-
- for( data = dbi_first(iter); dbi_exists(iter); data = dbi_next(iter) ) {
- if( data->help != NULL )
- data->help();
- else
- ShowInfo(" %s (%s)\t\t<no description provided>\n",data->name,HPM->pid2name(data->pluginID));
- }
-
- dbi_destroy(iter);
-}
-bool hpm_add_arg(unsigned int pluginID, char *name, bool has_param, void (*func) (char *param),void (*help) (void)) {
- struct HPMArgData *data = NULL;
+/**
+ * Adds a plugin-defined command-line argument.
+ *
+ * @param pluginID the current plugin's ID.
+ * @param name the command line argument's name, including the leading '--'.
+ * @param has_param whether the command line argument expects to be followed by a value.
+ * @param func the triggered function.
+ * @param help the help string to be displayed by '--help', if any.
+ * @return the success status.
+ */
+bool hpm_add_arg(unsigned int pluginID, char *name, bool has_param, CmdlineExecFunc func, const char *help) {
+ int i;
- if( strdb_exists(HPM->arg_db, name) ) {
- ShowError("HPM:add_arg:%s duplicate! (from %s)\n",name,HPM->pid2name(pluginID));
+ if (!name || strlen(name) < 3 || name[0] != '-' || name[1] != '-') {
+ ShowError("HPM:add_arg:%s invalid argument name: arguments must begin with '--' (from %s)\n", name, HPM->pid2name(pluginID));
return false;
}
- CREATE(data, struct HPMArgData, 1);
+ ARR_FIND(0, cmdline->args_data_count, i, strcmp(cmdline->args_data[i].name, name) == 0);
- data->pluginID = pluginID;
- data->name = aStrdup(name);
- data->func = func;
- data->help = help;
- data->has_param = has_param;
+ if (i < cmdline->args_data_count) {
+ ShowError("HPM:add_arg:%s duplicate! (from %s)\n",name,HPM->pid2name(pluginID));
+ return false;
+ }
- strdb_put(HPM->arg_db, data->name, data);
-
- return true;
+ return cmdline->arg_add(pluginID, name, '\0', func, help, has_param ? CMDLINE_OPT_PARAM : CMDLINE_OPT_NORMAL);
}
bool hplugins_addconf(unsigned int pluginID, enum HPluginConfType type, char *name, void (*func) (const char *val)) {
struct HPConfListenStorage *conf;
@@ -833,8 +831,6 @@ void hpm_init(void) {
HPM->packetsc[i] = 0;
}
- HPM->arg_db = strdb_alloc(DB_OPT_RELEASE_DATA, 0);
-
HPM->symbol_defaults();
#ifdef CONSOLE_INPUT
@@ -855,13 +851,6 @@ void hpm_memdown(void) {
free(HPM->fnames);
}
}
-int hpm_arg_db_clear_sub(DBKey key, DBData *data, va_list args) {
- struct HPMArgData *a = DB->data2ptr(data);
-
- aFree(a->name);
-
- return 0;
-}
void hpm_final(void) {
unsigned int i;
@@ -892,8 +881,13 @@ void hpm_final(void) {
if( HPM->confsc[i] )
aFree(HPM->confs[i]);
}
-
- HPM->arg_db->destroy(HPM->arg_db,HPM->arg_db_clear_sub);
+ if (HPM->cmdline_plugins) {
+ for (i = 0; i < HPM->cmdline_plugins_count; i++)
+ aFree(HPM->cmdline_plugins[i]);
+ aFree(HPM->cmdline_plugins);
+ HPM->cmdline_plugins = NULL;
+ HPM->cmdline_plugins_count = 0;
+ }
/* HPM->fnames is cleared after the memory manager goes down */
iMalloc->post_shutdown = hpm_memdown;
@@ -919,7 +913,8 @@ void hpm_defaults(void) {
HPM->confs[i] = NULL;
HPM->confsc[i] = 0;
}
- HPM->arg_db = NULL;
+ HPM->cmdline_plugins = NULL;
+ HPM->cmdline_plugins_count = 0;
/* */
HPM->init = hpm_init;
HPM->final = hpm_final;
@@ -940,9 +935,6 @@ void hpm_defaults(void) {
HPM->parse_packets = hplugins_parse_packets;
HPM->load_sub = NULL;
HPM->addhook_sub = NULL;
- HPM->arg_db_clear_sub = hpm_arg_db_clear_sub;
- HPM->parse_arg = hpm_parse_arg;
- HPM->arg_help = hpm_arg_help;
HPM->grabHPData = hplugins_grabHPData;
HPM->grabHPDataSub = NULL;
HPM->parseConf = hplugins_parse_conf;