Move some more stuff into wsutil.

Move the routines to parse numerical command-line arguments there.

Make cmdarg_err() and cmdarg_err_cont() routines in wsutil that just
call routines specified by a call to cmdarg_err_init(), and have
programs supply the appropriate routines to it.

Change-Id: Ic24fc758c0e647f4ff49eb91673529bcb9587b01
Reviewed-on: https://code.wireshark.org/review/2704
Reviewed-by: Evan Huus <eapache@gmail.com>
Reviewed-by: Guy Harris <guy@alum.mit.edu>
This commit is contained in:
Guy Harris 2014-06-29 14:37:21 -07:00
parent dd63ae2b8c
commit fe42762f23
15 changed files with 166 additions and 131 deletions

View File

@ -915,7 +915,6 @@ set(SHARK_COMMON_SRC
${PLATFORM_PCAP_SRC}
capture-pcap-util.c
cfile.c
clopts_common.c
frame_tvbuff.c
version.h
sync_pipe_write.c
@ -1351,7 +1350,6 @@ if(BUILD_dumpcap AND PCAP_FOUND)
capture_opts.c
capture-pcap-util.c
capture_stop_conditions.c
clopts_common.c
conditions.c
dumpcap.c
pcapio.c

View File

@ -44,7 +44,6 @@ SHARK_COMMON_SRC = \
$(PLATFORM_PCAP_SRC) \
capture-pcap-util.c \
cfile.c \
clopts_common.c \
frame_tvbuff.c \
sync_pipe_write.c \
version_info.c
@ -55,8 +54,6 @@ SHARK_COMMON_INCLUDES = \
capture-pcap-util.h \
capture-pcap-util-int.h \
cfile.h \
clopts_common.h \
cmdarg_err.h \
color.h \
file.h \
fileset.h \
@ -172,7 +169,6 @@ dumpcap_SOURCES = \
capture_opts.c \
capture-pcap-util.c \
capture_stop_conditions.c \
clopts_common.c \
conditions.c \
dumpcap.c \
pcapio.c \

View File

@ -42,11 +42,11 @@
#include "capture_opts.h"
#include "ringbuffer.h"
#include "clopts_common.h"
#include "cmdarg_err.h"
#include <capchild/capture_ifinfo.h>
#include "capture-pcap-util.h"
#include <wsutil/clopts_common.h>
#include <wsutil/cmdarg_err.h>
#include <wsutil/file_util.h>
static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe);

View File

@ -71,6 +71,7 @@
#include <zlib.h> /* to get the libz version number */
#endif
#include <wsutil/cmdarg_err.h>
#include <wsutil/crash_info.h>
#include <wsutil/copyright_info.h>
#include <wsutil/ws_version_info.h>
@ -89,8 +90,6 @@
#endif
#include "ringbuffer.h"
#include "clopts_common.h"
#include "cmdarg_err.h"
#include "version_info.h"
#include "capture-pcap-util.h"
@ -113,6 +112,7 @@
# include "wsutil/inet_v6defs.h"
#endif
#include <wsutil/clopts_common.h>
#include <wsutil/privileges.h>
#include "sync_pipe.h"
@ -580,49 +580,41 @@ show_version(GString *comp_info_str, GString *runtime_info_str)
/*
* Report an error in command-line arguments.
* If we're a capture child, send a message back to the parent, otherwise
* just print it.
*/
void
cmdarg_err(const char *fmt, ...)
static void
dumpcap_cmdarg_err(const char *fmt, va_list ap)
{
va_list ap;
if (capture_child) {
gchar *msg;
/* Generate a 'special format' message back to parent */
va_start(ap, fmt);
msg = g_strdup_vprintf(fmt, ap);
sync_pipe_errmsg_to_parent(2, msg, "");
g_free(msg);
va_end(ap);
} else {
va_start(ap, fmt);
fprintf(stderr, "dumpcap: ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
}
}
/*
* Report additional information for an error in command-line arguments.
* If we're a capture child, send a message back to the parent, otherwise
* just print it.
*/
void
cmdarg_err_cont(const char *fmt, ...)
static void
dumpcap_cmdarg_err_cont(const char *fmt, va_list ap)
{
va_list ap;
if (capture_child) {
gchar *msg;
va_start(ap, fmt);
msg = g_strdup_vprintf(fmt, ap);
sync_pipe_errmsg_to_parent(2, msg, "");
g_free(msg);
va_end(ap);
} else {
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
}
}
@ -4232,6 +4224,8 @@ main(int argc, char *argv[])
#endif
GString *str;
cmdarg_err_init(dumpcap_cmdarg_err, dumpcap_cmdarg_err_cont);
/* Assemble the compile-time version information string */
comp_info_str = g_string_new("Compiled ");
get_compiled_version_info(comp_info_str, NULL, NULL);

View File

@ -64,6 +64,8 @@
#include <glib.h>
#include <epan/epan-int.h>
#include <epan/epan.h>
#include <wsutil/cmdarg_err.h>
#include <wsutil/crash_info.h>
#include <wsutil/privileges.h>
#include <wsutil/file_util.h>
@ -83,8 +85,6 @@
#include <epan/print.h>
#include <epan/addr_resolv.h>
#include "ui/util.h"
#include "clopts_common.h"
#include "cmdarg_err.h"
#include "version_info.h"
#include "register.h"
#include "conditions.h"
@ -102,6 +102,7 @@
#include <wiretap/libpcap.h>
#include <wiretap/pcap-encap.h>
#include <wsutil/clopts_common.h>
#include <wsutil/ws_version_info.h>
#ifdef HAVE_LIBPCAP
@ -158,6 +159,8 @@ static void open_failure_message(const char *filename, int err,
static void failure_message(const char *msg_format, va_list ap);
static void read_failure_message(const char *filename, int err);
static void write_failure_message(const char *filename, int err);
static void rawshark_cmdarg_err(const char *fmt, va_list ap);
static void rawshark_cmdarg_err_cont(const char *fmt, va_list ap);
static void protocolinfo_init(char *field);
static gboolean parse_field_string_format(char *format);
@ -473,6 +476,8 @@ main(int argc, char *argv[])
static const char optstring[] = OPTSTRING_INIT;
cmdarg_err_init(rawshark_cmdarg_err, rawshark_cmdarg_err_cont);
/* Assemble the compile-time version information string */
comp_info_str = g_string_new("Compiled ");
get_compiled_version_info(comp_info_str, NULL, epan_get_compiled_version_info);
@ -1732,33 +1737,24 @@ write_failure_message(const char *filename, int err)
/*
* Report an error in command-line arguments.
*/
void
cmdarg_err(const char *fmt, ...)
static void
rawshark_cmdarg_err(const char *fmt, va_list ap)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "rawshark: ");
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
}
/*
* Report additional information for an error in command-line arguments.
*/
void
cmdarg_err_cont(const char *fmt, ...)
static void
rawshark_cmdarg_err_cont(const char *fmt, va_list ap)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
}
/*
* Editor modelines
*

View File

@ -59,6 +59,9 @@
#include <epan/exceptions.h>
#include <epan/epan-int.h>
#include <epan/epan.h>
#include <wsutil/clopts_common.h>
#include <wsutil/cmdarg_err.h>
#include <wsutil/crash_info.h>
#include <wsutil/privileges.h>
#include <wsutil/file_util.h>
@ -81,8 +84,6 @@
#include <epan/print.h>
#include <epan/addr_resolv.h>
#include "ui/util.h"
#include "clopts_common.h"
#include "cmdarg_err.h"
#include "version_info.h"
#include "register.h"
#include <epan/epan_dissect.h>
@ -792,6 +793,8 @@ main(int argc, char *argv[])
static const char optstring[] = OPTSTRING;
cmdarg_err_init(failure_message, failure_message_cont);
/* Assemble the compile-time version information string */
comp_info_str = g_string_new("Compiled ");
get_compiled_version_info(comp_info_str, NULL, epan_get_compiled_version_info);
@ -2767,34 +2770,16 @@ write_failure_message(const char *filename, int err)
filename, g_strerror(err));
}
/*
* Report an error in command-line arguments.
*/
void
cmdarg_err(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
failure_message(fmt, ap);
va_end(ap);
}
/*
* Report additional information for an error in command-line arguments.
*/
void
cmdarg_err_cont(const char *fmt, ...)
static void
failure_message_cont(const char *msg_format, va_list ap)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
vfprintf(stderr, msg_format, ap);
fprintf(stderr, "\n");
va_end(ap);
}
/*
* Editor modelines - http://www.wireshark.org/tools/modelines.html
*

View File

@ -64,6 +64,9 @@
#include <epan/exceptions.h>
#include <epan/epan-int.h>
#include <epan/epan.h>
#include <wsutil/clopts_common.h>
#include <wsutil/cmdarg_err.h>
#include <wsutil/crash_info.h>
#include <wsutil/privileges.h>
#include <wsutil/file_util.h>
@ -88,8 +91,6 @@
#include <epan/addr_resolv.h>
#include "ui/util.h"
#include "ui/ui_util.h"
#include "clopts_common.h"
#include "cmdarg_err.h"
#include "version_info.h"
#include "register.h"
#include <epan/epan_dissect.h>
@ -212,6 +213,7 @@ static void open_failure_message(const char *filename, int err,
static void failure_message(const char *msg_format, va_list ap);
static void read_failure_message(const char *filename, int err);
static void write_failure_message(const char *filename, int err);
static void failure_message_cont(const char *msg_format, va_list ap);
capture_file cfile;
@ -1009,6 +1011,8 @@ main(int argc, char *argv[])
}
#endif
cmdarg_err_init(failure_message, failure_message_cont);
/* Assemble the compile-time version information string */
comp_info_str = g_string_new("Compiled ");
get_compiled_version_info(comp_info_str, NULL, epan_get_compiled_version_info);
@ -4314,34 +4318,16 @@ write_failure_message(const char *filename, int err)
filename, g_strerror(err));
}
/*
* Report an error in command-line arguments.
*/
void
cmdarg_err(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
failure_message(fmt, ap);
va_end(ap);
}
/*
* Report additional information for an error in command-line arguments.
*/
void
cmdarg_err_cont(const char *fmt, ...)
static void
failure_message_cont(const char *msg_format, va_list ap)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
vfprintf(stderr, msg_format, ap);
fprintf(stderr, "\n");
va_end(ap);
}
/*
* Editor modelines - http://www.wireshark.org/tools/modelines.html
*

View File

@ -60,6 +60,7 @@
#include <portaudio.h>
#endif /* HAVE_LIBPORTAUDIO */
#include <wsutil/clopts_common.h>
#include <wsutil/crash_info.h>
#include <wsutil/filesystem.h>
#include <wsutil/file_util.h>
@ -92,6 +93,7 @@
#include <epan/print.h>
#include <epan/timestamp.h>
#include <wsutil/cmdarg_err.h>
#include <wsutil/plugins.h>
/* general (not GTK specific) */
@ -104,8 +106,6 @@
#include "../register.h"
#include "../ringbuffer.h"
#include "ui/util.h"
#include "../clopts_common.h"
#include "../cmdarg_err.h"
#include "../version_info.h"
#include "../log.h"
@ -1279,19 +1279,17 @@ show_version(void)
/*
* Report an error in command-line arguments.
* Creates a console on Windows.
* XXX - pop this up in a window of some sort on UNIX+X11 if the controlling
* terminal isn't the standard error?
*/
void
cmdarg_err(const char *fmt, ...)
static void
wireshark_cmdarg_err(const char *fmt, va_list ap)
{
va_list ap;
#ifdef _WIN32
create_console();
#endif
fprintf(stderr, "wireshark: ");
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
}
@ -1301,18 +1299,14 @@ cmdarg_err(const char *fmt, ...)
* XXX - pop this up in a window of some sort on UNIX+X11 if the controlling
* terminal isn't the standard error?
*/
void
cmdarg_err_cont(const char *fmt, ...)
static void
wireshark_cmdarg_err_cont(const char *fmt, va_list ap)
{
va_list ap;
#ifdef _WIN32
create_console();
#endif
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
}
/*
@ -2184,6 +2178,7 @@ main(int argc, char *argv[])
static const char optstring[] = OPTSTRING;
cmdarg_err_init(wireshark_cmdarg_err, wireshark_cmdarg_err_cont);
/* Set the C-language locale to the native environment. */
setlocale(LC_ALL, "");

View File

@ -41,6 +41,8 @@
# include <getopt.h>
#endif
#include <wsutil/clopts_common.h>
#include <wsutil/cmdarg_err.h>
#include <wsutil/crash_info.h>
#include <wsutil/filesystem.h>
#include <wsutil/file_util.h>
@ -88,8 +90,6 @@
#include "register.h"
#include "ringbuffer.h"
#include "ui/util.h"
#include "clopts_common.h"
#include "cmdarg_err.h"
#include "version_info.h"
#include "log.h"
@ -317,18 +317,14 @@ show_version(void)
* Creates a console on Windows.
*/
// xxx copied from ../gtk/main.c
void
cmdarg_err(const char *fmt, ...)
static void
wireshark_cmdarg_err(const char *fmt, va_list ap)
{
va_list ap;
#ifdef _WIN32
create_console();
#endif
fprintf(stderr, "wireshark: ");
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
}
@ -339,18 +335,14 @@ cmdarg_err(const char *fmt, ...)
* terminal isn't the standard error?
*/
// xxx copied from ../gtk/main.c
void
cmdarg_err_cont(const char *fmt, ...)
static void
wireshark_cmdarg_err_cont(const char *fmt, va_list ap)
{
va_list ap;
#ifdef _WIN32
create_console();
#endif
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
}
static void
@ -493,6 +485,8 @@ int main(int argc, char *argv[])
e_prefs *prefs_p;
GLogLevelFlags log_flags;
cmdarg_err_init(wireshark_cmdarg_err, wireshark_cmdarg_err_cont);
#ifdef _WIN32
create_app_running_mutex();
#endif

View File

@ -45,6 +45,8 @@ set(WSUTIL_FILES
base64.c
bitswap.c
cfutils.c
clopts_common.c
cmdarg_err.c
compiler_info.c
copyright_info.c
cpu_info.c

View File

@ -33,6 +33,8 @@ LIBWSUTIL_SRC = \
base64.c \
bitswap.c \
cfutils.c \
clopts_common.c \
cmdarg_err.c \
compiler_info.c \
copyright_info.c \
cpu_info.c \
@ -79,8 +81,10 @@ LIBWSUTIL_INCLUDES = \
base64.h \
bits_ctz.h \
bits_count_ones.h \
bitswap.h \
bitswap.h \
cfutils.h \
clopts_common.h \
cmdarg_err.h \
compiler_info.h \
copyright_info.h \
cpu_info.h \

View File

@ -1,5 +1,5 @@
/* clopts_common.c
* Handle command-line arguments common to Wireshark and TShark
* Handle command-line arguments common to various programs
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
@ -26,8 +26,9 @@
#include <string.h>
#include <stdlib.h>
#include "clopts_common.h"
#include "cmdarg_err.h"
#include <wsutil/cmdarg_err.h>
#include <wsutil/clopts_common.h>
int
get_natural_int(const char *string, const char *name)

View File

@ -1,5 +1,5 @@
/* clopts_common.h
* Handle command-line arguments common to Wireshark and TShark
* Handle command-line arguments common to various programs
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
@ -20,19 +20,23 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef __PROTO_DUMPOPTS_H__
#define __PROTO_DUMPOPTS_H__
#ifndef __WSUTIL_CLOPTS_COMMON_H__
#define __WSUTIL_CLOPTS_COMMON_H__
#include "ws_symbol_export.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
int get_natural_int(const char *string, const char *name);
WS_DLL_PUBLIC int
get_natural_int(const char *string, const char *name);
int get_positive_int(const char *string, const char *name);
WS_DLL_PUBLIC int
get_positive_int(const char *string, const char *name);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __PROTO_DUMPOPTS_H__ */
#endif /* __WSUTIL_CLOPTS_COMMON_H__ */

67
wsutil/cmdarg_err.c Normal file
View File

@ -0,0 +1,67 @@
/* cmdarg_err.c
* Routines to report command-line argument errors.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "config.h"
#include <stdio.h>
#include <wsutil/cmdarg_err.h>
static void (*print_err)(const char *, va_list ap);
static void (*print_err_cont)(const char *, va_list ap);
/*
* Set the reporting functions for error messages.
*/
void
cmdarg_err_init(void (*err)(const char *, va_list),
void (*err_cont)(const char *, va_list))
{
print_err = err;
print_err_cont = err_cont;
}
/*
* Report an error in command-line arguments.
*/
void
cmdarg_err(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
print_err(fmt, ap);
va_end(ap);
}
/*
* Report additional information for an error in command-line arguments.
*/
void
cmdarg_err_cont(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
print_err_cont(fmt, ap);
va_end(ap);
}

View File

@ -1,5 +1,5 @@
/* cmdarg_err.h
* Declarations of routines to report command-line errors.
* Declarations of routines to report command-line argument errors.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
@ -20,29 +20,42 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifndef __CMDARG_ERR_H__
#define __CMDARG_ERR_H__
#ifndef __WSUTIL_CMDARG_ERR_H__
#define __WSUTIL_CMDARG_ERR_H__
#include <stdarg.h>
#include <glib.h>
#include "ws_symbol_export.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/*
* Set the reporting functions for error messages.
*/
WS_DLL_PUBLIC void
cmdarg_err_init(void (*err)(const char *, va_list),
void (*err_cont)(const char *, va_list));
/*
* Report an error in command-line arguments.
*/
extern void cmdarg_err(const char *fmt, ...)
WS_DLL_PUBLIC void
cmdarg_err(const char *fmt, ...)
G_GNUC_PRINTF(1, 2);
/*
* Report additional information for an error in command-line arguments.
*/
extern void cmdarg_err_cont(const char *fmt, ...)
WS_DLL_PUBLIC void
cmdarg_err_cont(const char *fmt, ...)
G_GNUC_PRINTF(1, 2);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __CMDARG_ERR_H__ */
#endif /* __WSUTIL_CMDARG_ERR_H__ */