Commit 2f7fd680 authored by Guy Harris's avatar Guy Harris

Handle "-G" only in Tethereal - it doesn't work in Ethereal, and isn't

necessary there.

Add a "cmdarg_err()" routine to report command-line option errors; it
creates a console if necessary, and prints the command name and the
trailing newline.  Also add "cmdarg_err_cont()", which also creates a
console if necessary, and prints a trailing newline but no command name;
it's used to continue the message.  Use those, rather than
"g_warning()", for errors.

That means that we no longer need to pass the command name to various
command-line argument parsing routines.

svn path=/trunk/; revision=16526
parent 7586ab64
......@@ -71,6 +71,7 @@ ETHEREAL_COMMON_INCLUDES = \
capture_ui_utils.h \
cfile.h \
clopts_common.h \
cmdarg_err.h \
color.h \
conditions.h \
disabled_protos.h \
......
......@@ -104,7 +104,7 @@ extern void
capture_opts_init(capture_options *capture_opts, void *cfile);
extern void
capture_opts_add_opt(capture_options *capture_opts, const char *appname, int opt, const char *optarg, gboolean *start_capture);
capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg, gboolean *start_capture);
/* log content of capture_opts */
extern void
......
......@@ -40,6 +40,7 @@
#include "capture.h"
#include "ringbuffer.h"
#include "clopts_common.h"
#include "cmdarg_err.h"
void
capture_opts_init(capture_options *capture_opts, void *cfile)
......@@ -125,7 +126,7 @@ capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_optio
* in some fashion.
*/
static gboolean
set_autostop_criterion(capture_options *capture_opts, const char *appname, const char *autostoparg)
set_autostop_criterion(capture_options *capture_opts, const char *autostoparg)
{
gchar *p, *colonp;
......@@ -154,14 +155,14 @@ set_autostop_criterion(capture_options *capture_opts, const char *appname, const
}
if (strcmp(autostoparg,"duration") == 0) {
capture_opts->has_autostop_duration = TRUE;
capture_opts->autostop_duration = get_positive_int(appname, p,"autostop duration");
capture_opts->autostop_duration = get_positive_int(p,"autostop duration");
} else if (strcmp(autostoparg,"filesize") == 0) {
capture_opts->has_autostop_filesize = TRUE;
capture_opts->autostop_filesize = get_positive_int(appname, p,"autostop filesize");
capture_opts->autostop_filesize = get_positive_int(p,"autostop filesize");
} else if (strcmp(autostoparg,"files") == 0) {
capture_opts->multi_files_on = TRUE;
capture_opts->has_autostop_files = TRUE;
capture_opts->autostop_files = get_positive_int(appname, p,"autostop files");
capture_opts->autostop_files = get_positive_int(p,"autostop files");
} else {
return FALSE;
}
......@@ -176,7 +177,7 @@ set_autostop_criterion(capture_options *capture_opts, const char *appname, const
* in some fashion.
*/
static gboolean
get_ring_arguments(capture_options *capture_opts, const char *appname, const char *arg)
get_ring_arguments(capture_options *capture_opts, const char *arg)
{
gchar *p = NULL, *colonp;
......@@ -206,13 +207,13 @@ get_ring_arguments(capture_options *capture_opts, const char *appname, const cha
if (strcmp(arg,"files") == 0) {
capture_opts->has_ring_num_files = TRUE;
capture_opts->ring_num_files = get_natural_int(appname, p, "number of ring buffer files");
capture_opts->ring_num_files = get_natural_int(p, "number of ring buffer files");
} else if (strcmp(arg,"filesize") == 0) {
capture_opts->has_autostop_filesize = TRUE;
capture_opts->autostop_filesize = get_positive_int(appname, p, "ring buffer filesize");
capture_opts->autostop_filesize = get_positive_int(p, "ring buffer filesize");
} else if (strcmp(arg,"duration") == 0) {
capture_opts->has_file_duration = TRUE;
capture_opts->file_duration = get_positive_int(appname, p, "ring buffer duration");
capture_opts->file_duration = get_positive_int(p, "ring buffer duration");
}
*colonp = ':'; /* put the colon back */
......@@ -228,7 +229,7 @@ get_ring_arguments(capture_options *capture_opts, const char *appname, const cha
* in some fashion.
*/
static gboolean
get_pipe_arguments(capture_options *capture_opts, const char *appname, const char *arg)
get_pipe_arguments(capture_options *capture_opts, const char *arg)
{
gchar *p = NULL, *colonp;
int pipe_fd;
......@@ -260,16 +261,16 @@ get_pipe_arguments(capture_options *capture_opts, const char *appname, const cha
if (strcmp(arg,"sync") == 0) {
/* associate stdout with sync pipe */
pipe_fd = get_natural_int(appname, p, "sync pipe file descriptor");
pipe_fd = get_natural_int(p, "sync pipe file descriptor");
if (dup2(pipe_fd, 1) < 0) {
fprintf(stderr, "%s: Unable to dup sync pipe handle\n", appname);
cmdarg_err("Unable to dup sync pipe handle");
return FALSE;
}
} else if (strcmp(arg,"signal") == 0) {
/* associate stdin with signal pipe */
pipe_fd = get_natural_int(appname, p, "signal pipe file descriptor");
pipe_fd = get_natural_int(p, "signal pipe file descriptor");
if (dup2(pipe_fd, 0) < 0) {
fprintf(stderr, "%s: Unable to dup signal pipe handle\n", appname);
cmdarg_err("Unable to dup signal pipe handle");
return FALSE;
}
}
......@@ -281,30 +282,30 @@ get_pipe_arguments(capture_options *capture_opts, const char *appname, const cha
void
capture_opts_add_opt(capture_options *capture_opts, const char *appname, int opt, const char *optarg, gboolean *start_capture)
capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg, gboolean *start_capture)
{
switch(opt) {
case 'a': /* autostop criteria */
if (set_autostop_criterion(capture_opts, appname, optarg) == FALSE) {
fprintf(stderr, "%s: Invalid or unknown -a flag \"%s\"\n", appname, optarg);
if (set_autostop_criterion(capture_opts, optarg) == FALSE) {
cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg);
exit(1);
}
break;
case 'b': /* Ringbuffer option */
capture_opts->multi_files_on = TRUE;
if (get_ring_arguments(capture_opts, appname, optarg) == FALSE) {
fprintf(stderr, "%s: Invalid or unknown -b arg \"%s\"\n", appname, optarg);
if (get_ring_arguments(capture_opts, optarg) == FALSE) {
cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg);
exit(1);
}
break;
#ifdef _WIN32
case 'B': /* Buffer size */
capture_opts->buffer_size = get_positive_int(appname, optarg, "buffer size");
capture_opts->buffer_size = get_positive_int(optarg, "buffer size");
break;
#endif
case 'c': /* Capture xxx packets */
capture_opts->has_autostop_packets = TRUE;
capture_opts->autostop_packets = get_positive_int(appname, optarg, "packet count");
capture_opts->autostop_packets = get_positive_int(optarg, "packet count");
break;
case 'f': /* capture filter */
if (capture_opts->cfilter)
......@@ -330,7 +331,7 @@ capture_opts_add_opt(capture_options *capture_opts, const char *appname, int opt
break;
case 's': /* Set the snapshot (capture) length */
capture_opts->has_snaplen = TRUE;
capture_opts->snaplen = get_positive_int(appname, optarg, "snapshot length");
capture_opts->snaplen = get_positive_int(optarg, "snapshot length");
break;
case 'S': /* "Real-Time" mode: used for following file ala tail -f */
capture_opts->real_time_mode = TRUE;
......@@ -342,20 +343,20 @@ capture_opts_add_opt(capture_options *capture_opts, const char *appname, int opt
#ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
capture_opts->linktype = pcap_datalink_name_to_val(optarg);
if (capture_opts->linktype == -1) {
fprintf(stderr, "%s: The specified data link type \"%s\" isn't valid\n",
appname, optarg);
cmdarg_err("The specified data link type \"%s\" isn't valid",
optarg);
exit(1);
}
#else /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
/* XXX - just treat it as a number */
capture_opts->linktype = get_natural_int(appname, optarg, "data link type");
capture_opts->linktype = get_natural_int(optarg, "data link type");
#endif /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
break;
#ifdef _WIN32
/* Hidden option supporting Sync mode */
case 'Z': /* Write to pipe FD XXX */
if (get_pipe_arguments(capture_opts, appname, optarg) == FALSE) {
fprintf(stderr, "%s: Invalid or unknown -Z flag \"%s\"\n", appname, optarg);
if (get_pipe_arguments(capture_opts, optarg) == FALSE) {
cmdarg_err("Invalid or unknown -Z flag \"%s\"", optarg);
exit(1);
}
break;
......
......@@ -34,70 +34,26 @@
#include <epan/prefs.h>
#include "clopts_common.h"
/*
* Handle the "-G" option, to cause protocol field, etc. information
* to be printed.
*/
void
handle_dashG_option(int argc, char **argv, const char *progname)
{
char *gpf_path, *pf_path;
int gpf_open_errno, gpf_read_errno;
int pf_open_errno, pf_read_errno;
if (argc >= 2 && strcmp(argv[1], "-G") == 0) {
if (argc == 2)
proto_registrar_dump_fields(1);
else {
if (strcmp(argv[2], "fields") == 0)
proto_registrar_dump_fields(1);
else if (strcmp(argv[2], "fields2") == 0)
proto_registrar_dump_fields(2);
else if (strcmp(argv[2], "fields3") == 0)
proto_registrar_dump_fields(3);
else if (strcmp(argv[2], "protocols") == 0)
proto_registrar_dump_protocols();
else if (strcmp(argv[2], "values") == 0)
proto_registrar_dump_values();
else if (strcmp(argv[2], "decodes") == 0)
dissector_dump_decodes();
else if (strcmp(argv[2], "defaultprefs") == 0)
write_prefs(NULL);
else if (strcmp(argv[2], "currentprefs") == 0) {
read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
&pf_open_errno, &pf_read_errno, &pf_path);
write_prefs(NULL);
} else {
fprintf(stderr, "%s: Invalid \"%s\" option for -G flag\n", progname,
argv[2]);
exit(1);
}
}
exit(0);
}
}
#include "cmdarg_err.h"
int
get_natural_int(const char *appname, const char *string, const char *name)
get_natural_int(const char *string, const char *name)
{
long number;
char *p;
number = strtol(string, &p, 10);
if (p == string || *p != '\0') {
fprintf(stderr, "%s: The specified %s \"%s\" isn't a decimal number\n",
appname, name, string);
cmdarg_err("The specified %s \"%s\" isn't a decimal number", name, string);
exit(1);
}
if (number < 0) {
fprintf(stderr, "%s: The specified %s \"%s\" is a negative number\n",
appname, name, string);
cmdarg_err("The specified %s \"%s\" is a negative number", name, string);
exit(1);
}
if (number > INT_MAX) {
fprintf(stderr, "%s: The specified %s \"%s\" is too large (greater than %d)\n",
appname, name, string, INT_MAX);
cmdarg_err("The specified %s \"%s\" is too large (greater than %d)",
name, string, INT_MAX);
exit(1);
}
return number;
......@@ -105,15 +61,14 @@ get_natural_int(const char *appname, const char *string, const char *name)
int
get_positive_int(const char *appname, const char *string, const char *name)
get_positive_int(const char *string, const char *name)
{
long number;
number = get_natural_int(appname, string, name);
number = get_natural_int(string, name);
if (number == 0) {
fprintf(stderr, "%s: The specified %s is zero\n",
appname, name);
cmdarg_err("The specified %s is zero", name);
exit(1);
}
......
......@@ -29,15 +29,9 @@
extern "C" {
#endif /* __cplusplus */
/*
* Handle the "-G" option, to cause protocol field, etc. information
* to be printed.
*/
void handle_dashG_option(int argc, char **argv, const char *progname);
int get_natural_int(const char *appname, const char *string, const char *name);
int get_natural_int(const char *string, const char *name);
int get_positive_int(const char *appname, const char *string, const char *name);
int get_positive_int(const char *string, const char *name);
#ifdef __cplusplus
}
......
/* cmdarg_err.h
* Declarations of routines to report command-line errors.
*
* $Id$
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#ifndef __CMDARG_ERR_H__
#define __CMDARG_ERR_H__
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/*
* Report an error in command-line arguments.
*/
extern void cmdarg_err(const char *fmt, ...);
/*
* Report additional information for an error in command-line arguments.
*/
extern void cmdarg_err_cont(const char *fmt, ...);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __CMDARG_ERR_H__ */
This diff is collapsed.
......@@ -75,6 +75,7 @@
#include <epan/addr_resolv.h>
#include "util.h"
#include "clopts_common.h"
#include "cmdarg_err.h"
#include "version_info.h"
#include <epan/conversation.h>
#include <epan/plugins.h>
......@@ -689,9 +690,38 @@ main(int argc, char *argv[])
for backwards compatibility we dump out a glossary of display
filter symbols.
We do this here to mirror what happens in the GTK+ version, although
it's not necessary here. */
handle_dashG_option(argc, argv, "tethereal");
XXX - we do this here, for now, to support "-G" with no arguments.
If none of our build or other processes uses "-G" with no arguments,
we can just process it with the other arguments. */
if (argc >= 2 && strcmp(argv[1], "-G") == 0) {
if (argc == 2)
proto_registrar_dump_fields(1);
else {
if (strcmp(argv[2], "fields") == 0)
proto_registrar_dump_fields(1);
else if (strcmp(argv[2], "fields2") == 0)
proto_registrar_dump_fields(2);
else if (strcmp(argv[2], "fields3") == 0)
proto_registrar_dump_fields(3);
else if (strcmp(argv[2], "protocols") == 0)
proto_registrar_dump_protocols();
else if (strcmp(argv[2], "values") == 0)
proto_registrar_dump_values();
else if (strcmp(argv[2], "decodes") == 0)
dissector_dump_decodes();
else if (strcmp(argv[2], "defaultprefs") == 0)
write_prefs(NULL);
else if (strcmp(argv[2], "currentprefs") == 0) {
read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path,
&pf_open_errno, &pf_read_errno, &pf_path);
write_prefs(NULL);
} else {
cmdarg_err("Invalid \"%s\" option for -G flag", argv[2]);
exit(1);
}
}
exit(0);
}
/* Set the C-language locale to the native environment. */
setlocale(LC_ALL, "");
......@@ -783,7 +813,7 @@ main(int argc, char *argv[])
case 's': /* Set the snapshot (capture) length */
case 'y': /* Set the pcap data link type */
#ifdef HAVE_LIBPCAP
capture_opts_add_opt(&capture_opts, "tethereal", opt, optarg, &start_capture);
capture_opts_add_opt(&capture_opts, opt, optarg, &start_capture);
#else
capture_option_specified = TRUE;
arg_error = TRUE;
......@@ -3355,3 +3385,32 @@ read_failure_message(const char *filename, int err)
fprintf(stderr, "tethereal: An error occurred while reading from the file \"%s\": %s.\n",
filename, strerror(err));
}
/*
* Report an error in command-line arguments.
*/
void
cmdarg_err(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "tethereal: ");
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, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
va_end(ap);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment