wslog: Parse cmd line options in one pass

This commit is contained in:
João Valverde 2021-06-14 16:51:19 +01:00 committed by Wireshark GitLab Utility
parent 2d2cfa8d14
commit e86ac706ca
11 changed files with 89 additions and 132 deletions

View File

@ -234,10 +234,9 @@ libwsutil.so.0 libwsutil0 #MINVER#
ws_log_init_with_data@Base 3.5.0
ws_log_level_is_active@Base 3.5.0
ws_log_level_to_string@Base 3.5.0
ws_log_set_domain_filter_args@Base 3.5.0
ws_log_parse_args@Base 3.5.0
ws_log_set_domain_filter_str@Base 3.5.0
ws_log_set_level@Base 3.5.0
ws_log_set_level_args@Base 3.5.0
ws_log_set_level_str@Base 3.5.0
ws_logv@Base 3.5.0
ws_mempbrk_compile@Base 1.99.4

View File

@ -4856,12 +4856,8 @@ main(int argc, char *argv[])
/* Command line options are parsed too late to configure logging, do it
manually. */
const char *opt_err_val;
if ((opt_err_val = ws_log_set_level_args(&argc, argv)) != NULL) {
cmdarg_err("Invalid log level \"%s\"", opt_err_val);
exit (1);
}
ws_log_set_domain_filter_args(&argc, argv);
if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0)
exit(1);
#ifdef _WIN32
create_app_running_mutex();

View File

@ -2537,12 +2537,8 @@ int main(int argc, char *argv[]) {
/* Command line options are parsed too late to configure logging, do it
manually. */
const char *opt_err_val;
if ((opt_err_val = ws_log_set_level_args(&argc, argv)) != NULL) {
cmdarg_err("Invalid log level \"%s\"", opt_err_val);
if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0)
return EXIT_FAILURE;
}
ws_log_set_domain_filter_args(&argc, argv);
/*
* Get credential information for later use.

View File

@ -160,12 +160,8 @@ int main(int argc, char *argv[])
/* Command line options are parsed too late to configure logging, do it
manually. */
const char *opt_err_val;
if ((opt_err_val = ws_log_set_level_args(&argc, argv)) != NULL) {
cmdarg_err("Invalid log level \"%s\"", opt_err_val);
if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0)
return EXIT_FAILURE;
}
ws_log_set_domain_filter_args(&argc, argv);
/*
* Get credential information for later use.

View File

@ -456,12 +456,8 @@ main(int argc, char *argv[])
/* Command line options are parsed too late to configure logging, do it
manually. */
const char *opt_err_val;
if ((opt_err_val = ws_log_set_level_args(&argc, argv)) != NULL) {
cmdarg_err("Invalid log level \"%s\"", opt_err_val);
if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0)
return INVALID_OPTION;
}
ws_log_set_domain_filter_args(&argc, argv);
/* Initialize the version information. */
ws_init_version_info("Rawshark (Wireshark)", NULL,

View File

@ -123,12 +123,8 @@ main(int argc, char *argv[])
/* Command line options are parsed too late to configure logging, do it
manually. */
const char *opt_err_val;
if ((opt_err_val = ws_log_set_level_args(&argc, argv)) != NULL) {
cmdarg_err("Invalid log level \"%s\"", opt_err_val);
if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0)
return INIT_FAILED;
}
ws_log_set_domain_filter_args(&argc, argv);
/*
* Get credential information for later use, and drop privileges

View File

@ -356,12 +356,8 @@ main(int argc, char *argv[])
/* Command line options are parsed too late to configure logging, do it
manually. */
const char *opt_err_val;
if ((opt_err_val = ws_log_set_level_args(&argc, argv)) != NULL) {
cmdarg_err("Invalid log level \"%s\"", opt_err_val);
if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0)
return INVALID_OPTION;
}
ws_log_set_domain_filter_args(&argc, argv);
#ifdef _WIN32
create_app_running_mutex();

View File

@ -781,12 +781,8 @@ main(int argc, char *argv[])
/* Command line options are parsed too late to configure logging, do it
manually. */
const char *opt_err_val;
if ((opt_err_val = ws_log_set_level_args(&argc, argv)) != NULL) {
cmdarg_err("Invalid log level \"%s\"", opt_err_val);
if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0)
return INVALID_OPTION;
}
ws_log_set_domain_filter_args(&argc, argv);
ws_debug("tshark started with %d args", argc);

View File

@ -584,12 +584,8 @@ int main(int argc, char *qt_argv[])
/* Command line options are parsed too late to configure logging, do it
manually. */
const char *opt_err_val;
if ((opt_err_val = ws_log_set_level_args(&argc, argv)) != NULL) {
cmdarg_err("Invalid log level \"%s\"", opt_err_val);
if (ws_log_parse_args(&argc, argv, cmdarg_err) != 0)
exit_application(INVALID_OPTION);
}
ws_log_set_domain_filter_args(&argc, argv);
/*
* Get credential information for later use, and drop privileges

View File

@ -153,75 +153,87 @@ enum ws_log_level ws_log_set_level_str(const char *str_level)
}
static const char *log_prune_argv(int count, char **ptr, int prune_extra,
const char *arg, int *ret_argc)
static const char *opt_level = "--log-level";
static const char *opt_domains = "--log-domains";
int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char *, ...))
{
/*
* We found a log option. We will remove it from
* the argv by moving up the other strings in the array. This is
* so that it doesn't generate an unrecognized option
* error further along in the initialization process.
*/
char **ptr = argv;
int count = *argc_ptr;
int ret = 0;
size_t optlen;
const char *option, *value;
int prune_extra;
/* Include the terminating NULL in the memmove. */
memmove(ptr, ptr + 1 + prune_extra, (count - prune_extra) * sizeof(*ptr));
*ret_argc -= (1 + prune_extra);
return arg;
}
const char *log_parse_args(int *argc_ptr, char *argv[], const char *option)
{
char **p;
int c;
size_t optlen = strlen(option);
const char *value;
for (p = argv, c = *argc_ptr; *p != NULL; p++, c--) {
if (strncmp(*p, option, optlen) == 0) {
value = *p + optlen;
/* Two possibilities:
* --<option> <value>
* or
* --<option>=<value>
*/
if (value[0] == '\0') {
/* value is separated with blank space */
value = *(p + 1);
/* If the option value after the blank is missing or stars with '-' just ignore it.
* But we should probably signal an error (missing required value). */
if (value == NULL || !*value || *value == '-') {
return log_prune_argv(c, p, 0, NULL, argc_ptr);
}
return log_prune_argv(c, p, 1, value, argc_ptr);
}
else if (value[0] == '=') {
/* value is after equals */
value += 1;
return log_prune_argv(c, p, 0, value, argc_ptr);
}
/* we didn't find what we want */
while (*ptr != NULL) {
if (g_str_has_prefix(*ptr, opt_level)) {
option = opt_level;
optlen = strlen(opt_level);
}
else if (g_str_has_prefix(*ptr, opt_domains)) {
option = opt_domains;
optlen = strlen(opt_domains);
}
else {
ptr += 1;
count -= 1;
continue;
}
value = *ptr + optlen;
/* Two possibilities:
* --<option> <value>
* or
* --<option>=<value>
*/
if (value[0] == '\0') {
/* value is separated with blank space */
value = *(ptr + 1);
prune_extra = 1;
/* If the option value after the blank is missing or stars with '-' just ignore it.
* But we should probably signal an error (missing required value). */
if (value == NULL || !*value || *value == '-') {
option = NULL;
prune_extra = 0;
}
}
else if (value[0] == '=') {
/* value is after equals */
value += 1;
prune_extra = 0;
}
else {
ptr += 1;
count -= 1;
continue;
}
if (option == opt_level) {
if (ws_log_set_level_str(value) == LOG_LEVEL_NONE) {
print_err("Invalid log level \"%s\"\n", value);
ret += 1;
}
}
else if (option == opt_domains) {
ws_log_set_domain_filter_str(value);
}
/*
* We found a log option. We will remove it from
* the argv by moving up the other strings in the array. This is
* so that it doesn't generate an unrecognized option
* error further along in the initialization process.
*/
/* Include the terminating NULL in the memmove. */
memmove(ptr, ptr + 1 + prune_extra, (count - prune_extra) * sizeof(*ptr));
/* No need to increment ptr here. */
count -= (1 + prune_extra);
*argc_ptr -= (1 + prune_extra);
}
return NULL; /* No log-level option, ignore and return success. */
}
const char *ws_log_set_level_args(int *argc_ptr, char *argv[])
{
const char *optval = NULL;
enum ws_log_level level;
optval = log_parse_args(argc_ptr, argv, "--log-level");
if (optval == NULL)
return NULL;
level = ws_log_set_level_str(optval);
if (level == LOG_LEVEL_NONE)
return optval;
return NULL;
return ret;
}
@ -246,18 +258,6 @@ void ws_log_set_domain_filter_str(const char *str_filter)
}
void ws_log_set_domain_filter_args(int *argc_ptr, char *argv[])
{
const char *optval = NULL;
optval = log_parse_args(argc_ptr, argv, "--log-domains");
if (optval == NULL)
return;
ws_log_set_domain_filter_str(optval);
}
void ws_log_init(ws_log_writer_cb *_writer)
{
registered_appname = g_get_prgname();

View File

@ -93,16 +93,6 @@ WS_DLL_PUBLIC
enum ws_log_level ws_log_set_level_str(const char *str_level);
/** Set the active log level from an argv vector.
*
* Will search the arv for the option parameter "--log-level=<string>".
* If it finds the parameter and the string is valid sets the log level and
* returns NULL (success). Othwerise returns the invalid option argument after '='.
*/
WS_DLL_PUBLIC
const char *ws_log_set_level_args(int *argcp, char **argv);
/** Set a domain filter from a string.
*
* Domain filter is a case insensitive list separated by ',' or ';'. Only
@ -112,12 +102,12 @@ WS_DLL_PUBLIC
void ws_log_set_domain_filter_str(const char *domain_filter);
/** Set the active domain from an argv vector.
/** Parses the command line arguments for log options.
*
* Same as above but parses the filter from the command line arguments.
* Returns zero for no error, non-zero for a bad option value.
*/
WS_DLL_PUBLIC
void ws_log_set_domain_filter_args(int *argcp, char **argv);
int ws_log_parse_args(int *argc_ptr, char *argv[], void (*print_err)(const char *, ...));
/** Initializes the logging code.