dftest: Fix command-line argument parsing

Expressions that start with hyphen clash with command-line options.
In that case we need to pass "--" to dftest to stop processing
options.

Fix the test suite to do this. Fixes failures with dftest and
expressions like:

    -2 == tcp.port

Replace the GLib option parser with getopt while at it. The GLib API
is nice but isn't a good fit for this utility and the code appears to
be inconsistent on whether "--" is left in the argv or not.
This commit is contained in:
João Valverde 2022-12-30 16:11:53 +00:00
parent d3d06c2552
commit bc74d2e3e4
2 changed files with 37 additions and 26 deletions

View File

@ -31,6 +31,7 @@
#include <wsutil/privileges.h> #include <wsutil/privileges.h>
#include <wsutil/report_message.h> #include <wsutil/report_message.h>
#include <wsutil/wslog.h> #include <wsutil/wslog.h>
#include <wsutil/ws_getopt.h>
#include <wiretap/wtap.h> #include <wiretap/wtap.h>
@ -45,14 +46,6 @@ static int debug_noisy = 0;
static int debug_flex = 0; static int debug_flex = 0;
static int debug_lemon = 0; static int debug_lemon = 0;
static GOptionEntry entries[] =
{
{ "debug", 'd', 0, G_OPTION_ARG_NONE, &debug_noisy, "Enable verbose debug logs", NULL },
{ "debug-flex", 'f', 0, G_OPTION_ARG_NONE, &debug_flex, "Enable debugging for Flex", NULL },
{ "debug-lemon", 'l', 0, G_OPTION_ARG_NONE, &debug_lemon, "Enable debugging for Lemon", NULL },
{ NULL, 0, 0, G_OPTION_ARG_NONE, NULL, "", NULL }
};
static void static void
putloc(FILE *fp, df_loc_t loc) putloc(FILE *fp, df_loc_t loc)
{ {
@ -67,6 +60,12 @@ putloc(FILE *fp, df_loc_t loc)
fputc('\n', fp); fputc('\n', fp);
} }
static void
print_usage(void)
{
fprintf(stderr, "Usage: dftest [OPTIONS] -- <EXPR>\n");
}
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
@ -93,8 +92,7 @@ main(int argc, char **argv)
gdouble elapsed_expand, elapsed_compile; gdouble elapsed_expand, elapsed_compile;
gboolean ok; gboolean ok;
int exit_status = 0; int exit_status = 0;
GError *error = NULL; int opt;
GOptionContext *context;
cmdarg_err_init(dftest_cmdarg_err, dftest_cmdarg_err_cont); cmdarg_err_init(dftest_cmdarg_err, dftest_cmdarg_err_cont);
@ -116,12 +114,21 @@ main(int argc, char **argv)
setlocale(LC_ALL, ""); setlocale(LC_ALL, "");
#endif #endif
context = g_option_context_new("EXPR"); while ((opt = ws_getopt(argc, argv, "dfl")) != -1) {
g_option_context_add_main_entries(context, entries, NULL); switch (opt) {
if (!g_option_context_parse(context, &argc, &argv, &error)) { case 'd':
printf("Error parsing arguments: %s\n", error->message); debug_noisy = 1;
g_error_free(error); break;
exit(1); case 'f':
debug_flex = 1;
break;
case 'l':
debug_lemon = 1;
break;
default: /* '?' */
print_usage();
exit(EXIT_FAILURE);
}
} }
if (debug_noisy) if (debug_noisy)
@ -171,17 +178,22 @@ main(int argc, char **argv)
prefs_apply_all(); prefs_apply_all();
/* Check for filter on command line */ /* Check for filter on command line */
if (argc <= 1) { if (argv[ws_optind] == NULL) {
char *help = g_option_context_get_help(context, TRUE, NULL); print_usage();
fprintf(stderr, "%s", help); exit(1);
g_free(help);
goto out;
} }
timer = g_timer_new(); /* This is useful to prevent confusion with option parsing.
* Skips printing options and argv[0]. */
for (int i = ws_optind; i < argc; i++) {
printf("argv[%d]: %s\n", i, argv[i]);
}
printf("\n");
/* Get filter text */ /* Get filter text */
text = get_args_as_string(argc, argv, 1); text = get_args_as_string(argc, argv, ws_optind);
timer = g_timer_new();
/* Expand macros. */ /* Expand macros. */
g_timer_start(timer); g_timer_start(timer);
@ -244,7 +256,6 @@ out:
g_free(expanded_text); g_free(expanded_text);
if (timer != NULL) if (timer != NULL)
g_timer_destroy(timer); g_timer_destroy(timer);
g_option_context_free(context);
exit(exit_status); exit(exit_status);
} }

View File

@ -67,7 +67,7 @@ def checkDFilterCountWithSelectedFrame(dfilter_cmd, base_env):
def checkDFilterFail(cmd_dftest, base_env): def checkDFilterFail(cmd_dftest, base_env):
def checkDFilterFail_real(dfilter, error_message): def checkDFilterFail_real(dfilter, error_message):
"""Run a display filter and expect dftest to fail.""" """Run a display filter and expect dftest to fail."""
proc = subprocess.Popen([cmd_dftest, dfilter], proc = subprocess.Popen([cmd_dftest, '--', dfilter],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True, universal_newlines=True,
@ -84,7 +84,7 @@ def checkDFilterFail(cmd_dftest, base_env):
def checkDFilterSucceed(cmd_dftest, base_env): def checkDFilterSucceed(cmd_dftest, base_env):
def checkDFilterSucceed_real(dfilter, expect_stdout=None): def checkDFilterSucceed_real(dfilter, expect_stdout=None):
"""Run a display filter and expect dftest to succeed.""" """Run a display filter and expect dftest to succeed."""
proc = subprocess.Popen([cmd_dftest, dfilter], proc = subprocess.Popen([cmd_dftest, '--', dfilter],
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
universal_newlines=True, universal_newlines=True,