client: add noninteractive support for cmds

This commit is contained in:
Tim Kourt 2018-08-03 14:35:39 -07:00 committed by Denis Kenzior
parent ef2878d5ee
commit 1d931d8a68
2 changed files with 96 additions and 17 deletions

View File

@ -2,7 +2,7 @@
* *
* Wireless daemon for Linux * Wireless daemon for Linux
* *
* Copyright (C) 2017 Intel Corporation. All rights reserved. * Copyright (C) 2017-2018 Intel Corporation. All rights reserved.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -32,6 +32,12 @@
#include "display.h" #include "display.h"
static struct l_queue *command_families; static struct l_queue *command_families;
static int exit_status;
static bool interactive_mode;
static struct command_noninteractive {
char **argv;
int argc;
} command_noninteractive;
static enum cmd_status cmd_version(const char *entity, static enum cmd_status cmd_version(const char *entity,
char **argv, int argc) char **argv, int argc)
@ -337,6 +343,15 @@ static void execute_cmd(const char *family, const char *entity,
if (status != CMD_STATUS_TRIGGERED && status != CMD_STATUS_DONE) if (status != CMD_STATUS_TRIGGERED && status != CMD_STATUS_DONE)
goto error; goto error;
if (status == CMD_STATUS_DONE && !interactive_mode) {
l_main_quit();
return;
}
if (!interactive_mode)
return;
if (cmd->refreshable) if (cmd->refreshable)
display_refresh_timeout_set(); display_refresh_timeout_set();
@ -359,12 +374,19 @@ error:
break; break;
case CMD_STATUS_FAILED: case CMD_STATUS_FAILED:
l_main_quit(); goto failure;
break;
default: default:
l_error("Unknown command status."); l_error("Unknown command status.");
} }
if (interactive_mode)
return;
failure:
exit_status = EXIT_FAILURE;
l_main_quit();
} }
static bool match_cmd(const char *family, const char *param, static bool match_cmd(const char *family, const char *param,
@ -453,6 +475,29 @@ static void list_cmd_families(void)
} }
} }
static bool command_match_misc_commands(char **argv, int argc)
{
if (match_cmd(NULL, argv[0], argv + 1, argc - 1, misc_commands))
return true;
if (strcmp(argv[0], "help"))
return false;
display_table_header("Available commands", MARGIN "%-*s%-*s",
50, "Commands", 28, "Description");
list_cmd_families();
if (!interactive_mode)
return true;
display("\nMiscellaneous:\n");
list_commands(NULL, misc_commands);
return true;
}
void command_process_prompt(char **argv, int argc) void command_process_prompt(char **argv, int argc)
{ {
if (argc == 0) if (argc == 0)
@ -461,24 +506,43 @@ void command_process_prompt(char **argv, int argc)
if (match_cmd_family(argv, argc)) if (match_cmd_family(argv, argc))
return; return;
display_refresh_reset(); if (!interactive_mode) {
display_error("Invalid command\n");
if (match_cmd(NULL, argv[0], argv + 1, argc - 1, misc_commands)) exit_status = EXIT_FAILURE;
return; l_main_quit();
if (strcmp(argv[0], "help")) {
display("Invalid command\n");
return; return;
} }
display_table_header("Available commands", MARGIN "%-*s%-*s", display_refresh_reset();
50, "Commands", 28, "Description");
list_cmd_families(); if (command_match_misc_commands(argv, argc))
return;
display("\nMiscellaneous:\n"); display_error("Invalid command\n");
}
list_commands(NULL, misc_commands); void command_noninteractive_trigger(void)
{
if (!command_noninteractive.argc)
return;
command_process_prompt(command_noninteractive.argv,
command_noninteractive.argc);
}
bool command_is_interactive_mode(void)
{
return interactive_mode;
}
void command_set_exit_status(int status)
{
exit_status = status;
}
int command_get_exit_status(void)
{
return exit_status;
} }
void command_family_register(const struct command_family *family) void command_family_register(const struct command_family *family)
@ -494,7 +558,7 @@ void command_family_unregister(const struct command_family *family)
extern struct command_family_desc __start___command[]; extern struct command_family_desc __start___command[];
extern struct command_family_desc __stop___command[]; extern struct command_family_desc __stop___command[];
void command_init(void) bool command_init(char **argv, int argc)
{ {
struct command_family_desc *desc; struct command_family_desc *desc;
@ -506,6 +570,16 @@ void command_init(void)
desc->init(); desc->init();
} }
if (argc < 2) {
interactive_mode = true;
return true;
}
command_noninteractive.argv = argv + 1;
command_noninteractive.argc = argc - 1;
return false;
} }
void command_exit(void) void command_exit(void)

View File

@ -58,6 +58,11 @@ char *command_entity_arg_completion(const char *text, int state,
void command_process_prompt(char **argv, int argc); void command_process_prompt(char **argv, int argc);
void command_noninteractive_trigger(void);
bool command_is_interactive_mode(void);
int command_get_exit_status(void);
void command_set_exit_status(int status);
void command_family_register(const struct command_family *family); void command_family_register(const struct command_family *family);
void command_family_unregister(const struct command_family *family); void command_family_unregister(const struct command_family *family);
@ -73,5 +78,5 @@ struct command_family_desc {
#name, init, exit \ #name, init, exit \
}; \ }; \
void command_init(void); bool command_init(char **argv, int argc);
void command_exit(void); void command_exit(void);