remove all calls to exit() from the capture_opts.c and replace them by status return values (0,1 or 2).

This way makes it much easier to debug, e.g. to set a common breakpoint if a command line parameter fails.

svn path=/trunk/; revision=17240
This commit is contained in:
Ulf Lamping 2006-02-10 02:05:30 +00:00
parent 0d9c276e90
commit e4e8df5e1d
5 changed files with 67 additions and 39 deletions

View File

@ -48,7 +48,7 @@
#include <wiretap/file_util.h>
static gboolean capture_opts_output_to_pipe(const char *save_file);
static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe);
void
@ -292,7 +292,7 @@ get_pipe_arguments(capture_options *capture_opts, const char *arg)
#endif
void
static int
capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg)
{
long adapter_index;
@ -316,16 +316,16 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg)
if (p != NULL && *p == '\0') {
if (adapter_index < 0) {
cmdarg_err("The specified adapter index is a negative number");
exit(1);
return 1;
}
if (adapter_index > INT_MAX) {
cmdarg_err("The specified adapter index is too large (greater than %d)",
INT_MAX);
exit(1);
return 1;
}
if (adapter_index == 0) {
cmdarg_err("there is no interface with that adapter index");
exit(1);
return 1;
}
if_list = get_interface_list(&err, err_str);
if (if_list == NULL) {
@ -342,35 +342,39 @@ capture_opts_add_iface_opt(capture_options *capture_opts, const char *optarg)
cmdarg_err("There are no interfaces on which a capture can be done");
break;
}
exit(2);
return 2;
}
if_info = g_list_nth_data(if_list, adapter_index - 1);
if (if_info == NULL) {
cmdarg_err("there is no interface with that adapter index");
exit(1);
return 1;
}
capture_opts->iface = g_strdup(if_info->name);
free_interface_list(if_list);
} else {
capture_opts->iface = g_strdup(optarg);
}
return 0;
}
void
int
capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg, gboolean *start_capture)
{
int status;
switch(opt) {
case 'a': /* autostop criteria */
if (set_autostop_criterion(capture_opts, optarg) == FALSE) {
cmdarg_err("Invalid or unknown -a flag \"%s\"", optarg);
exit(1);
return 1;
}
break;
case 'b': /* Ringbuffer option */
capture_opts->multi_files_on = TRUE;
if (get_ring_arguments(capture_opts, optarg) == FALSE) {
cmdarg_err("Invalid or unknown -b arg \"%s\"", optarg);
exit(1);
return 1;
}
break;
#ifdef _WIN32
@ -391,7 +395,10 @@ capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg,
capture_opts->show_info = FALSE;
break;
case 'i': /* Use interface xxx */
capture_opts_add_iface_opt(capture_opts, optarg);
status = capture_opts_add_iface_opt(capture_opts, optarg);
if(status != 0) {
return status;
}
break;
case 'k': /* Start capture immediately */
*start_capture = TRUE;
@ -418,7 +425,8 @@ capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg,
#else
capture_opts->save_file = g_strdup(optarg);
#endif
capture_opts->output_to_pipe = capture_opts_output_to_pipe(capture_opts->save_file);
status = capture_opts_output_to_pipe(capture_opts->save_file, &capture_opts->output_to_pipe);
return status;
break;
case 'y': /* Set the pcap data link type */
#ifdef HAVE_PCAP_DATALINK_NAME_TO_VAL
@ -426,7 +434,7 @@ capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg,
if (capture_opts->linktype == -1) {
cmdarg_err("The specified data link type \"%s\" isn't valid",
optarg);
exit(1);
return 1;
}
#else /* HAVE_PCAP_DATALINK_NAME_TO_VAL */
/* XXX - just treat it as a number */
@ -438,7 +446,7 @@ capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg,
case 'Z': /* Write to pipe FD XXX */
if (get_pipe_arguments(capture_opts, optarg) == FALSE) {
cmdarg_err("Invalid or unknown -Z flag \"%s\"", optarg);
exit(1);
return 1;
}
break;
#endif /* _WIN32 */
@ -446,10 +454,12 @@ capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg,
/* the caller is responsible to send us only the right opt's */
g_assert_not_reached();
}
return 0;
}
void capture_opts_list_link_layer_types(capture_options *capture_opts)
int capture_opts_list_link_layer_types(capture_options *capture_opts)
{
gchar err_str[PCAP_ERRBUF_SIZE];
GList *lt_list, *lt_entry;
@ -464,7 +474,7 @@ void capture_opts_list_link_layer_types(capture_options *capture_opts)
"you have the proper interface or pipe specified.\n", err_str);
} else
cmdarg_err("The capture device has no data link types.");
exit(2);
return 2;
}
cmdarg_err_cont("Data link types (use option -y to set):");
for (lt_entry = lt_list; lt_entry != NULL;
@ -478,10 +488,12 @@ void capture_opts_list_link_layer_types(capture_options *capture_opts)
putchar('\n');
}
free_pcap_linktype_list(lt_list);
return 0;
}
void capture_opts_list_interfaces()
int capture_opts_list_interfaces()
{
GList *if_list;
GList *if_entry;
@ -505,7 +517,7 @@ void capture_opts_list_interfaces()
cmdarg_err("There are no interfaces on which a capture can be done");
break;
}
exit(2);
return 2;
}
i = 1; /* Interface id number */
@ -518,6 +530,8 @@ void capture_opts_list_interfaces()
printf("\n");
}
free_interface_list(if_list);
return 0;
}
@ -607,7 +621,7 @@ static int capture_opts_test_for_fifo(const char *path)
return 0;
}
static gboolean capture_opts_output_to_pipe(const char *save_file)
static gboolean capture_opts_output_to_pipe(const char *save_file, gboolean *is_pipe)
{
int err;
@ -619,7 +633,7 @@ static gboolean capture_opts_output_to_pipe(const char *save_file)
silly to do "-w - >output_file" rather than "-w output_file",
but by not checking we might be violating the Principle Of
Least Astonishment. */
return TRUE;
*is_pipe = TRUE;
} else {
/* not a capture file, test for a FIFO (aka named pipe) */
err = capture_opts_test_for_fifo(save_file);
@ -631,18 +645,20 @@ static gboolean capture_opts_output_to_pipe(const char *save_file)
break;
case ESPIPE: /* it is a FIFO */
return TRUE;
*is_pipe = TRUE;
break;
default: /* couldn't stat it */
cmdarg_err("Error testing whether capture file is a pipe: %s",
strerror(errno));
exit(2);
return 2;
}
}
}
return FALSE;
*is_pipe = FALSE;
return 0;
}
#endif /* HAVE_LIBPCAP */

View File

@ -103,7 +103,7 @@ extern void
capture_opts_init(capture_options *capture_opts, void *cfile);
/* set a command line option value */
extern void
extern int
capture_opts_add_opt(capture_options *capture_opts, int opt, const char *optarg, gboolean *start_capture);
/* log content of capture_opts */
@ -111,11 +111,11 @@ extern void
capture_opts_log(const char *log_domain, GLogLevelFlags log_level, capture_options *capture_opts);
/* list link layer types */
extern void
extern int
capture_opts_list_link_layer_types(capture_options *capture_opts);
/* list interfaces */
extern void
extern int
capture_opts_list_interfaces(void);
/* trim the snaplen entry */

View File

@ -252,6 +252,7 @@ main(int argc, char *argv[])
struct pcap_stat stats;
GLogLevelFlags log_flags;
gboolean list_link_layer_types = FALSE;
int status;
#define OPTSTRING_INIT "a:b:c:Df:hi:Lps:vw:y:"
@ -358,13 +359,16 @@ main(int argc, char *argv[])
/* Hidden option supporting Sync mode */
case 'Z': /* Write to pipe FD x */
#endif /* _WIN32 */
capture_opts_add_opt(capture_opts, opt, optarg, &start_capture);
status = capture_opts_add_opt(capture_opts, opt, optarg, &start_capture);
if(status != 0) {
exit_main(status);
}
break;
/*** all non capture option specific ***/
case 'D': /* Print a list of capture devices and exit */
capture_opts_list_interfaces();
exit_main(0);
status = capture_opts_list_interfaces();
exit_main(status);
break;
case 'L': /* Print list of link-layer types and exit */
list_link_layer_types = TRUE;
@ -441,8 +445,8 @@ main(int argc, char *argv[])
}
if (list_link_layer_types) {
capture_opts_list_link_layer_types(capture_opts);
exit_main(0);
status = capture_opts_list_link_layer_types(capture_opts);
exit_main(status);
}
capture_opts_trim_snaplen(capture_opts, MIN_PACKET_SIZE);

View File

@ -1897,6 +1897,7 @@ main(int argc, char *argv[])
GLogLevelFlags log_flags;
guint go_to_packet = 0;
int optind_initial;
int status;
#define OPTSTRING_INIT "a:b:c:Df:g:Hhi:klLm:nN:o:pQr:R:Ss:t:vw:X:y:z:"
@ -2295,7 +2296,10 @@ main(int argc, char *argv[])
case 'Z': /* Write to pipe FD XXX */
#endif /* _WIN32 */
#ifdef HAVE_LIBPCAP
capture_opts_add_opt(capture_opts, opt, optarg, &start_capture);
status = capture_opts_add_opt(capture_opts, opt, optarg, &start_capture);
if(status != 0) {
exit(status);
}
#else
capture_option_specified = TRUE;
arg_error = TRUE;
@ -2553,8 +2557,8 @@ main(int argc, char *argv[])
}
if (list_link_layer_types) {
capture_opts_list_link_layer_types(capture_opts);
exit(0);
status = capture_opts_list_link_layer_types(capture_opts);
exit(status);
}
capture_opts_trim_snaplen(capture_opts, MIN_PACKET_SIZE);

View File

@ -683,6 +683,7 @@ main(int argc, char *argv[])
e_prefs *prefs;
char badopt;
GLogLevelFlags log_flags;
int status;
#define OPTSTRING_INIT "a:b:c:d:Df:F:hi:lLnN:o:pqr:R:s:St:T:vVw:xX:y:z:"
#ifdef HAVE_LIBPCAP
@ -885,7 +886,10 @@ main(int argc, char *argv[])
case 'B': /* Buffer size */
#endif /* _WIN32 */
#ifdef HAVE_LIBPCAP
capture_opts_add_opt(&capture_opts, opt, optarg, &start_capture);
status = capture_opts_add_opt(&capture_opts, opt, optarg, &start_capture);
if(status != 0) {
exit(status);
}
#else
capture_option_specified = TRUE;
arg_error = TRUE;
@ -897,8 +901,8 @@ main(int argc, char *argv[])
break;
case 'D': /* Print a list of capture devices and exit */
#ifdef HAVE_LIBPCAP
capture_opts_list_interfaces();
exit(0);
status = capture_opts_list_interfaces();
exit(status);
#else
capture_option_specified = TRUE;
arg_error = TRUE;
@ -1386,8 +1390,8 @@ main(int argc, char *argv[])
/* if requested, list the link layer types and exit */
if (list_link_layer_types) {
capture_opts_list_link_layer_types(&capture_opts);
exit(0);
status = capture_opts_list_link_layer_types(&capture_opts);
exit(status);
}
if (!quiet) {