diff options
author | Guy Harris <guy@alum.mit.edu> | 2018-12-31 16:55:23 -0800 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2019-01-01 02:07:06 +0000 |
commit | ba589a4e445a8ad8054073eff846087fc61c9ef8 (patch) | |
tree | ac5bcee756541531a1864ce01c60f3f0395e0dc5 /ui/clopts_common.c | |
parent | 5a5d236d1f1c0149e9e9fa291cbd251006a8dd40 (diff) |
Move some command-line-oriented routines from wsutil to ui.
cmdarg_err() is for reporting errors for command-line programs and
command-line errors in GUI programs; it's not something for any of the
Wireshark libraries to use.
The various routines for parsing numerical command-line arguments are
not for general use, they're just for use when parsing arguments.
Change-Id: I100bd4a55ab8ee4497f41d9651b0c5670e6c1e7f
Reviewed-on: https://code.wireshark.org/review/31281
Petri-Dish: Guy Harris <guy@alum.mit.edu>
Tested-by: Petri Dish Buildbot
Reviewed-by: Guy Harris <guy@alum.mit.edu>
Diffstat (limited to 'ui/clopts_common.c')
-rw-r--r-- | ui/clopts_common.c | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/ui/clopts_common.c b/ui/clopts_common.c new file mode 100644 index 0000000000..464ab18697 --- /dev/null +++ b/ui/clopts_common.c @@ -0,0 +1,121 @@ +/* clopts_common.c + * Handle command-line arguments common to various programs + * + * Wireshark - Network traffic analyzer + * By Gerald Combs <gerald@wireshark.org> + * Copyright 1998 Gerald Combs + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +#include "config.h" + +#include <stdlib.h> +#include <errno.h> + +#include <wsutil/strtoi.h> +#include <ui/cmdarg_err.h> + +#include "clopts_common.h" + +int +get_natural_int(const char *string, const char *name) +{ + gint32 number; + + if (!ws_strtoi32(string, NULL, &number)) { + if (errno == EINVAL) { + cmdarg_err("The specified %s \"%s\" isn't a decimal number", name, string); + exit(1); + } + if (number < 0) { + cmdarg_err("The specified %s \"%s\" is a negative number", name, string); + exit(1); + } + cmdarg_err("The specified %s \"%s\" is too large (greater than %d)", + name, string, number); + exit(1); + } + if (number < 0) { + cmdarg_err("The specified %s \"%s\" is a negative number", name, string); + exit(1); + } + return (int)number; +} + +int +get_positive_int(const char *string, const char *name) +{ + int number; + + number = get_natural_int(string, name); + + if (number == 0) { + cmdarg_err("The specified %s is zero", name); + exit(1); + } + + return number; +} + +guint32 +get_guint32(const char *string, const char *name) +{ + guint32 number; + + if (!ws_strtou32(string, NULL, &number)) { + if (errno == EINVAL) { + cmdarg_err("The specified %s \"%s\" isn't a decimal number", name, string); + exit(1); + } + cmdarg_err("The specified %s \"%s\" is too large (greater than %d)", + name, string, number); + exit(1); + } + return number; +} + +guint32 +get_nonzero_guint32(const char *string, const char *name) +{ + guint32 number; + + number = get_guint32(string, name); + + if (number == 0) { + cmdarg_err("The specified %s is zero", name); + exit(1); + } + + return number; +} + +double +get_positive_double(const char *string, const char *name) +{ + double number = g_ascii_strtod(string, NULL); + + if (errno == EINVAL) { + cmdarg_err("The specified %s \"%s\" isn't a floating point number", name, string); + exit(1); + } + if (number < 0.0) { + cmdarg_err("The specified %s \"%s\" is a negative number", name, string); + exit(1); + } + + return number; +} + +/* + * Editor modelines - http://www.wireshark.org/tools/modelines.html + * + * Local Variables: + * c-basic-offset: 2 + * tab-width: 8 + * indent-tabs-mode: nil + * End: + * + * ex: set shiftwidth=2 tabstop=8 expandtab: + * :indentSize=2:tabSize=8:noTabs=true: + */ |