Commit b17e5e4d authored by Daniel Salzman's avatar Daniel Salzman

knotc: command flags refactoring

parent 17db3ea1
......@@ -73,22 +73,22 @@ static int check_args(cmd_args_t *args, unsigned count)
static int check_conf_args(cmd_args_t *args)
{
// Mask relevant flags.
cmd_conf_flag_t flags = args->desc->flags;
flags &= CMD_CONF_FOPT_ITEM | CMD_CONF_FREQ_ITEM | CMD_CONF_FOPT_DATA;
cmd_flag_t flags = args->desc->flags;
flags &= CMD_FOPT_ITEM | CMD_FREQ_ITEM | CMD_FOPT_DATA;
switch (args->argc) {
case 0:
if (flags == CMD_CONF_FNONE || (flags & CMD_CONF_FOPT_ITEM)) {
if (flags == CMD_FNONE || (flags & CMD_FOPT_ITEM)) {
return KNOT_EOK;
}
break;
case 1:
if (flags & (CMD_CONF_FOPT_ITEM | CMD_CONF_FREQ_ITEM)) {
if (flags & (CMD_FOPT_ITEM | CMD_FREQ_ITEM)) {
return KNOT_EOK;
}
break;
default:
if (flags != CMD_CONF_FNONE) {
if (flags != CMD_FNONE) {
return KNOT_EOK;
}
break;
......@@ -519,7 +519,7 @@ static int cmd_conf_init(cmd_args_t *args)
ret = conf_db_check(conf(), &conf()->read_txn);
if ((ret >= KNOT_EOK || ret == KNOT_CONF_EVERSION)) {
if (ret != KNOT_EOK && !(args->flags & CMD_FFORCE)) {
if (ret != KNOT_EOK && !args->force) {
log_error("use force option to overwrite the existing "
"destination and ensure the server is not running!");
return KNOT_EDENIED;
......@@ -558,7 +558,7 @@ static int cmd_conf_import(cmd_args_t *args)
ret = conf_db_check(conf(), &conf()->read_txn);
if ((ret >= KNOT_EOK || ret == KNOT_CONF_EVERSION)) {
if (ret != KNOT_EOK && !(args->flags & CMD_FFORCE)) {
if (ret != KNOT_EOK && !args->force) {
log_error("use force option to overwrite the existing "
"destination and ensure the server is not running!");
return KNOT_EDENIED;
......@@ -625,7 +625,7 @@ static int cmd_conf_ctl(cmd_args_t *args)
}
// Send if only one argument or item without values.
if (args->argc == 1 || !(args->desc->flags & CMD_CONF_FOPT_DATA)) {
if (args->argc == 1 || !(args->desc->flags & CMD_FOPT_DATA)) {
ret = knot_ctl_send(args->ctl, KNOT_CTL_TYPE_DATA, &data);
if (ret != KNOT_EOK) {
log_error("failed to control (%s)", knot_strerror(ret));
......@@ -636,7 +636,7 @@ static int cmd_conf_ctl(cmd_args_t *args)
// Send the item values or the other items.
for (int i = 1; i < args->argc; i++) {
if (args->desc->flags & CMD_CONF_FOPT_DATA) {
if (args->desc->flags & CMD_FOPT_DATA) {
data[KNOT_CTL_IDX_DATA] = args->argv[i];
} else {
ret = get_conf_key(args->argv[i], &data);
......@@ -669,28 +669,28 @@ const cmd_desc_t cmd_table[] = {
{ CMD_STOP, cmd_ctl, CTL_STOP },
{ CMD_RELOAD, cmd_ctl, CTL_RELOAD },
{ CMD_ZONE_CHECK, cmd_zone_check, CTL_NONE, CMD_CONF_FOPT_ZONE | CMD_CONF_FREAD },
{ CMD_ZONE_MEMSTATS, cmd_zone_memstats, CTL_NONE, CMD_CONF_FOPT_ZONE | CMD_CONF_FREAD },
{ CMD_ZONE_STATUS, cmd_zone_ctl, CTL_ZONE_STATUS, CMD_CONF_FOPT_ZONE },
{ CMD_ZONE_RELOAD, cmd_zone_ctl, CTL_ZONE_RELOAD, CMD_CONF_FOPT_ZONE },
{ CMD_ZONE_REFRESH, cmd_zone_ctl, CTL_ZONE_REFRESH, CMD_CONF_FOPT_ZONE },
{ CMD_ZONE_RETRANSFER, cmd_zone_ctl, CTL_ZONE_RETRANSFER, CMD_CONF_FOPT_ZONE },
{ CMD_ZONE_FLUSH, cmd_zone_ctl, CTL_ZONE_FLUSH, CMD_CONF_FOPT_ZONE },
{ CMD_ZONE_SIGN, cmd_zone_ctl, CTL_ZONE_SIGN, CMD_CONF_FOPT_ZONE },
{ CMD_CONF_INIT, cmd_conf_init, CTL_NONE, CMD_CONF_FWRITE },
{ CMD_CONF_CHECK, cmd_conf_check, CTL_NONE, CMD_CONF_FREAD },
{ CMD_CONF_IMPORT, cmd_conf_import, CTL_NONE, CMD_CONF_FWRITE },
{ CMD_CONF_EXPORT, cmd_conf_export, CTL_NONE, CMD_CONF_FREAD },
{ CMD_CONF_LIST, cmd_conf_ctl, CTL_CONF_LIST, CMD_CONF_FOPT_ITEM },
{ CMD_CONF_READ, cmd_conf_ctl, CTL_CONF_READ, CMD_CONF_FOPT_ITEM },
{ CMD_ZONE_CHECK, cmd_zone_check, CTL_NONE, CMD_FOPT_ZONE | CMD_FREAD },
{ CMD_ZONE_MEMSTATS, cmd_zone_memstats, CTL_NONE, CMD_FOPT_ZONE | CMD_FREAD },
{ CMD_ZONE_STATUS, cmd_zone_ctl, CTL_ZONE_STATUS, CMD_FOPT_ZONE },
{ CMD_ZONE_RELOAD, cmd_zone_ctl, CTL_ZONE_RELOAD, CMD_FOPT_ZONE },
{ CMD_ZONE_REFRESH, cmd_zone_ctl, CTL_ZONE_REFRESH, CMD_FOPT_ZONE },
{ CMD_ZONE_RETRANSFER, cmd_zone_ctl, CTL_ZONE_RETRANSFER, CMD_FOPT_ZONE },
{ CMD_ZONE_FLUSH, cmd_zone_ctl, CTL_ZONE_FLUSH, CMD_FOPT_ZONE },
{ CMD_ZONE_SIGN, cmd_zone_ctl, CTL_ZONE_SIGN, CMD_FOPT_ZONE },
{ CMD_CONF_INIT, cmd_conf_init, CTL_NONE, CMD_FWRITE },
{ CMD_CONF_CHECK, cmd_conf_check, CTL_NONE, CMD_FREAD },
{ CMD_CONF_IMPORT, cmd_conf_import, CTL_NONE, CMD_FWRITE },
{ CMD_CONF_EXPORT, cmd_conf_export, CTL_NONE, CMD_FREAD },
{ CMD_CONF_LIST, cmd_conf_ctl, CTL_CONF_LIST, CMD_FOPT_ITEM },
{ CMD_CONF_READ, cmd_conf_ctl, CTL_CONF_READ, CMD_FOPT_ITEM },
{ CMD_CONF_BEGIN, cmd_conf_ctl, CTL_CONF_BEGIN },
{ CMD_CONF_COMMIT, cmd_conf_ctl, CTL_CONF_COMMIT },
{ CMD_CONF_ABORT, cmd_conf_ctl, CTL_CONF_ABORT },
{ CMD_CONF_DIFF, cmd_conf_ctl, CTL_CONF_DIFF, CMD_CONF_FOPT_ITEM | CMD_CONF_FREQ_TXN },
{ CMD_CONF_GET, cmd_conf_ctl, CTL_CONF_GET, CMD_CONF_FOPT_ITEM | CMD_CONF_FREQ_TXN },
{ CMD_CONF_SET, cmd_conf_ctl, CTL_CONF_SET, CMD_CONF_FREQ_ITEM | CMD_CONF_FOPT_DATA | CMD_CONF_FREQ_TXN },
{ CMD_CONF_UNSET, cmd_conf_ctl, CTL_CONF_UNSET, CMD_CONF_FOPT_ITEM | CMD_CONF_FOPT_DATA | CMD_CONF_FREQ_TXN },
{ CMD_CONF_DIFF, cmd_conf_ctl, CTL_CONF_DIFF, CMD_FOPT_ITEM | CMD_FREQ_TXN },
{ CMD_CONF_GET, cmd_conf_ctl, CTL_CONF_GET, CMD_FOPT_ITEM | CMD_FREQ_TXN },
{ CMD_CONF_SET, cmd_conf_ctl, CTL_CONF_SET, CMD_FREQ_ITEM | CMD_FOPT_DATA | CMD_FREQ_TXN },
{ CMD_CONF_UNSET, cmd_conf_ctl, CTL_CONF_UNSET, CMD_FOPT_ITEM | CMD_FOPT_DATA | CMD_FREQ_TXN },
{ NULL }
};
......
......@@ -27,23 +27,17 @@
#include "libknot/control/control.h"
#include "knot/ctl/commands.h"
/*! \brief Command parameter flags. */
typedef enum {
CMD_FNONE = 0, /*!< Empty flag. */
CMD_FFORCE = 1 << 0, /*!< Forced operation. */
} cmd_flag_t;
/*! \brief Command condition flags. */
typedef enum {
CMD_CONF_FNONE = 0, /*!< Empty flag. */
CMD_CONF_FREAD = 1 << 0, /*!< Required read access to config or confdb. */
CMD_CONF_FWRITE = 1 << 1, /*!< Required write access to confdb. */
CMD_CONF_FOPT_ITEM = 1 << 2, /*!< Optional item argument. */
CMD_CONF_FREQ_ITEM = 1 << 3, /*!< Required item argument. */
CMD_CONF_FOPT_DATA = 1 << 4, /*!< Optional item data argument. */
CMD_CONF_FOPT_ZONE = 1 << 5, /*!< Optional zone name argument. */
CMD_CONF_FREQ_TXN = 1 << 6, /*!< Required opened confdb transaction. */
} cmd_conf_flag_t;
CMD_FNONE = 0, /*!< Empty flag. */
CMD_FREAD = 1 << 0, /*!< Required read access to config or confdb. */
CMD_FWRITE = 1 << 1, /*!< Required write access to confdb. */
CMD_FOPT_ITEM = 1 << 2, /*!< Optional item argument. */
CMD_FREQ_ITEM = 1 << 3, /*!< Required item argument. */
CMD_FOPT_DATA = 1 << 4, /*!< Optional item data argument. */
CMD_FOPT_ZONE = 1 << 5, /*!< Optional zone name argument. */
CMD_FREQ_TXN = 1 << 6, /*!< Required open confdb transaction. */
} cmd_flag_t;
struct cmd_desc;
typedef struct cmd_desc cmd_desc_t;
......@@ -54,7 +48,7 @@ typedef struct {
knot_ctl_t *ctl;
int argc;
const char **argv;
cmd_flag_t flags;
bool force;
} cmd_args_t;
/*! \brief Command callback description. */
......@@ -62,7 +56,7 @@ struct cmd_desc {
const char *name;
int (*fcn)(cmd_args_t *);
ctl_cmd_t cmd;
cmd_conf_flag_t flags;
cmd_flag_t flags;
};
/*! \brief Command description. */
......
......@@ -118,7 +118,7 @@ static void id_lookup(EditLine *el, const char *str, size_t str_len,
const cmd_desc_t *cmd_desc, const char *section, bool add_space)
{
// Decide which confdb transaction to ask.
unsigned ctl_code = (cmd_desc->flags & CMD_CONF_FREQ_TXN) ?
unsigned ctl_code = (cmd_desc->flags & CMD_FREQ_TXN) ?
CTL_CONF_GET : CTL_CONF_READ;
const cmd_desc_t *desc = cmd_table;
......@@ -314,8 +314,8 @@ static unsigned char complete(EditLine *el, int ch)
}
// Finish if a command with no or unsupported arguments.
if (desc->flags == CMD_CONF_FNONE || desc->flags == CMD_CONF_FREAD ||
desc->flags == CMD_CONF_FWRITE) {
if (desc->flags == CMD_FNONE || desc->flags == CMD_FREAD ||
desc->flags == CMD_FWRITE) {
goto complete_exit;
}
......@@ -325,15 +325,15 @@ static unsigned char complete(EditLine *el, int ch)
}
// Complete the zone name.
if (desc->flags & CMD_CONF_FOPT_ZONE) {
if (desc->flags & CMD_CONF_FREAD) {
if (desc->flags & CMD_FOPT_ZONE) {
if (desc->flags & CMD_FREAD) {
local_zones_lookup(el, argv[token], pos);
} else {
id_lookup(el, argv[token], pos, desc, "zone", true);
}
goto complete_exit;
// Complete the section/id/item name.
} else if (desc->flags & (CMD_CONF_FOPT_ITEM | CMD_CONF_FREQ_ITEM)) {
} else if (desc->flags & (CMD_FOPT_ITEM | CMD_FREQ_ITEM)) {
if (token == 1) {
item_lookup(el, argv[1], desc);
}
......
......@@ -52,7 +52,6 @@ static void print_help(void)
}
params_t params = {
.flags = CMD_FNONE,
.timeout = DEFAULT_CTL_TIMEOUT * 1000
};
......@@ -93,7 +92,7 @@ int main(int argc, char **argv)
params.timeout *= 1000;
break;
case 'f':
params.flags |= CMD_FFORCE;
params.force = true;
break;
case 'v':
params.verbose = true;
......
......@@ -47,17 +47,17 @@ int set_config(const cmd_desc_t *desc, params_t *params)
}
/* Mask relevant flags. */
cmd_conf_flag_t flags = desc->flags & (CMD_CONF_FREAD | CMD_CONF_FWRITE);
cmd_flag_t flags = desc->flags & (CMD_FREAD | CMD_FWRITE);
/* Choose the optimal config source. */
struct stat st;
bool import = false;
if (flags == CMD_CONF_FNONE && params->socket != NULL) {
if (flags == CMD_FNONE && params->socket != NULL) {
import = false;
params->confdb = NULL;
} else if (params->confdb != NULL) {
import = false;
} else if (flags == CMD_CONF_FWRITE) {
} else if (flags == CMD_FWRITE) {
import = false;
params->confdb = CONF_DEFAULT_DBDIR;
} else if (params->config != NULL){
......@@ -68,7 +68,7 @@ int set_config(const cmd_desc_t *desc, params_t *params)
} else if (stat(CONF_DEFAULT_FILE, &st) == 0) {
import = true;
params->config = CONF_DEFAULT_FILE;
} else if (flags != CMD_CONF_FNONE) {
} else if (flags != CMD_FNONE) {
log_error("no configuration source available");
return KNOT_EINVAL;
}
......@@ -79,7 +79,7 @@ int set_config(const cmd_desc_t *desc, params_t *params)
/* Prepare config flags. */
conf_flag_t conf_flags = CONF_FNOHOSTNAME;
if (params->confdb != NULL && !(flags & CMD_CONF_FWRITE)) {
if (params->confdb != NULL && !(flags & CMD_FWRITE)) {
conf_flags |= CONF_FREADONLY;
}
......@@ -118,10 +118,10 @@ int set_ctl(knot_ctl_t **ctl, const cmd_desc_t *desc, params_t *params)
}
/* Mask relevant flags. */
cmd_conf_flag_t flags = desc->flags & (CMD_CONF_FREAD | CMD_CONF_FWRITE);
cmd_flag_t flags = desc->flags & (CMD_FREAD | CMD_FWRITE);
/* Check if control socket is required. */
if (flags != CMD_CONF_FNONE) {
if (flags != CMD_FNONE) {
*ctl = NULL;
return KNOT_EOK;
}
......@@ -210,7 +210,7 @@ int process_cmd(int argc, const char **argv, params_t *params)
.desc = desc,
.argc = argc - 1,
.argv = argv + 1,
.flags = params->flags
.force = params->force
};
/* Set control interface if necessary. */
......
......@@ -28,11 +28,11 @@
/*! Utility command line parameters. */
typedef struct {
cmd_flag_t flags;
const char *config;
const char *confdb;
const char *socket;
bool verbose;
bool force;
int timeout;
} params_t;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment