Skip to content
Snippets Groups Projects
Commit f0fadee7 authored by Marek Vavrusa's avatar Marek Vavrusa
Browse files

Simplified knotc flags.

refs #1560
parent 2d121bd0
No related branches found
No related tags found
No related merge requests found
......@@ -36,6 +36,20 @@ enum knotc_constants_t {
WAITPID_TIMEOUT = 10 /*!< \brief Timeout for waiting for process. */
};
/*! \brief Controller flags. */
enum knotc_flag_t {
F_NULL = 0 << 0,
F_FORCE = 1 << 0,
F_VERBOSE = 1 << 1,
F_WAIT = 1 << 2,
F_INTERACTIVE = 1 << 3,
F_AUTO = 1 << 4
};
static inline unsigned has_flag(unsigned flags, enum knotc_flag_t f) {
return flags & f;
}
/*! \brief Print help. */
void help(int argc, char **argv)
{
......@@ -253,9 +267,8 @@ int zctask_add(knotc_zctask_t *tasks, int count, pid_t pid, conf_zone_t *zone)
*
* \todo Make enumerated flags instead of many parameters...
*/
int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
int force, int wait, int interactive, int automatic,
int jobs, const char *pidfile)
int execute(const char *action, char **argv, int argc, pid_t pid,
unsigned flags, unsigned jobs, const char *pidfile)
{
int valid_cmd = 0;
int rc = 0;
......@@ -273,7 +286,7 @@ int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
fprintf(stderr, "control: Server PID found, "
"already running.\n");
if (!force) {
if (!has_flag(flags, F_FORCE)) {
return 1;
} else {
fprintf(stderr, "control: forcing "
......@@ -285,9 +298,9 @@ int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
}
// Recompile zones if needed
if (automatic) {
rc = execute("compile", argv, argc, -1, verbose, force,
wait, interactive, automatic, jobs, pidfile);
if (has_flag(flags, F_AUTO)) {
rc = execute("compile", argv, argc, -1, flags,
jobs, pidfile);
}
// Lock configuration
......@@ -297,10 +310,10 @@ int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
const char *cfg = conf()->filename;
const char *args[] = {
PROJECT_EXEC,
interactive ? "" : "-d",
has_flag(flags, F_INTERACTIVE) ? "" : "-d",
cfg ? "-c" : "",
cfg ? cfg : "",
verbose ? "-v" : "",
has_flag(flags, F_VERBOSE) ? "-v" : "",
argc > 0 ? argv[0] : ""
};
......@@ -308,7 +321,7 @@ int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
conf_read_unlock();
// Execute command
if (interactive) {
if (has_flag(flags, F_INTERACTIVE)) {
printf("control: Running in interactive mode.\n");
fflush(stderr);
fflush(stdout);
......@@ -321,8 +334,8 @@ int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
fflush(stdout);
// Wait for finish
if (wait && !interactive) {
if (verbose) {
if (has_flag(flags, F_WAIT) && !has_flag(flags, F_INTERACTIVE)) {
if (has_flag(flags, F_VERBOSE)) {
fprintf(stdout, "control: waiting for server "
"to load.\n");
}
......@@ -347,7 +360,7 @@ int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
fprintf(stderr, "Server PID not found, "
"probably not running.\n");
if (!force) {
if (!has_flag(flags, F_FORCE)) {
rc = 1;
} else {
fprintf(stderr, "control: forcing "
......@@ -364,8 +377,8 @@ int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
}
// Wait for finish
if (rc == 0 && wait) {
if (verbose) {
if (rc == 0 && has_flag(flags, F_WAIT)) {
if (has_flag(flags, F_VERBOSE)) {
fprintf(stdout, "control: waiting for server "
"to stop.\n");
}
......@@ -381,8 +394,7 @@ int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
}
if (strcmp(action, "restart") == 0) {
valid_cmd = 1;
execute("stop", argv, argc, pid, verbose, force, wait,
interactive, automatic, jobs, pidfile);
execute("stop", argv, argc, pid, flags, jobs, pidfile);
int i = 0;
while((pid = pid_read(pidfile)) > 0) {
......@@ -403,8 +415,7 @@ int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
}
printf("Restarting server.\n");
rc = execute("start", argv, argc, -1, verbose, force, wait,
interactive, automatic, jobs, pidfile);
rc = execute("start", argv, argc, -1, flags, jobs, pidfile);
}
if (strcmp(action, "reload") == 0) {
......@@ -414,7 +425,7 @@ int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
fprintf(stderr, "Server PID not found, "
"probably not running.\n");
if (force) {
if (has_flag(flags, F_FORCE)) {
fprintf(stderr, "control: forcing "
"server stop.\n");
} else {
......@@ -423,9 +434,9 @@ int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
}
// Recompile zones if needed
if (automatic) {
rc = execute("compile", argv, argc, -1, verbose, force, wait,
interactive, automatic, jobs, pidfile);
if (has_flag(flags, F_AUTO)) {
rc = execute("compile", argv, argc, -1, flags,
jobs, pidfile);
}
// Stop
......@@ -485,7 +496,7 @@ int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
printf("Zone '%s' is up-to-date.\n",
zone->name);
if (force) {
if (has_flag(flags, F_FORCE)) {
fprintf(stderr, "control: forcing "
"zone recompilation.\n");
} else {
......@@ -507,7 +518,7 @@ int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
const char *args[] = {
ZONEPARSER_EXEC,
zone->enable_checks ? "-s" : "",
verbose ? "-v" : "",
has_flag(flags, F_VERBOSE) ? "-v" : "",
"-o",
zone->db,
zone->name,
......@@ -515,7 +526,7 @@ int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
};
// Execute command
if (verbose) {
if (has_flag(flags, F_VERBOSE)) {
printf("Compiling '%s' as '%s'...\n",
zone->name, zone->db);
}
......@@ -542,7 +553,7 @@ int execute(const char *action, char **argv, int argc, pid_t pid, int verbose,
}
// Log
if (verbose) {
if (has_flag(flags, F_VERBOSE)) {
printf("'%s' finished (return code %d)\n", action, rc);
}
return rc;
......@@ -552,12 +563,8 @@ int main(int argc, char **argv)
{
// Parse command line arguments
int c = 0, li = 0;
int force = 0;
int verbose = 0;
int wait = 0;
int interactive = 0;
int jobs = 1;
int automatic = 0;
unsigned jobs = 1;
unsigned flags = F_NULL;
const char* config_fn = 0;
/* Long options. */
......@@ -575,26 +582,15 @@ int main(int argc, char **argv)
};
while ((c = getopt_long(argc, argv, "wfc:viaj:Vh", opts, &li)) != -1) {
switch (c)
{
case 'w':
wait = 1;
break;
case 'f':
force = 1;
break;
switch (c) {
case 'w': flags |= F_WAIT; break;
case 'f': flags |= F_FORCE; break;
case 'v': flags |= F_VERBOSE; break;
case 'i': flags |= F_INTERACTIVE; break;
case 'a': flags |= F_AUTO; break;
case 'c':
config_fn = optarg;
break;
case 'v':
verbose = 1;
break;
case 'i':
interactive = 1;
break;
case 'a':
automatic = 1;
break;
case 'j':
jobs = atoi(optarg);
if (jobs < 1) {
......@@ -647,9 +643,9 @@ int main(int argc, char **argv)
free(default_fn);
// Verbose mode
if (verbose) {
int mask = LOG_MASK(LOG_INFO)|LOG_MASK(LOG_DEBUG);
log_levels_add(LOGT_STDOUT, LOG_ANY, mask);
if (has_flag(flags, F_VERBOSE)) {
log_levels_add(LOGT_STDOUT, LOG_ANY,
LOG_MASK(LOG_INFO)|LOG_MASK(LOG_DEBUG));
}
// Fetch PID
......@@ -668,8 +664,7 @@ int main(int argc, char **argv)
// Execute action
int rc = execute(action, argv + optind + 1, argc - optind - 1,
pid, verbose, force, wait, interactive, automatic,
jobs, pidfile);
pid, flags, jobs, pidfile);
// Finish
free(pidfile);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment