For getopt() arg processing:

- Use exit(0) for -h option as per convention;

For g_option...() arg processing (when USE_GOPTION is set):
 - Fix bug: 'capinfos x' doesn't print any info about x;
   (bug introduced in SVN #48481);
 - Use stderr for error messages related to cmd-line arg parsing;


svn path=/trunk/; revision=54555
This commit is contained in:
Bill Meier 2014-01-02 15:41:45 +00:00
parent be849dcaad
commit 8ef3e2fad3
1 changed files with 98 additions and 93 deletions

View File

@ -118,11 +118,11 @@ static gboolean continue_after_wtap_open_offline_failure = TRUE;
* table report variables * table report variables
*/ */
static gboolean long_report = TRUE; /* By default generate long report */ static gboolean long_report = TRUE; /* By default generate long report */
static gchar table_report_header = TRUE; /* Generate column header by default */ static gchar table_report_header = TRUE; /* Generate column header by default */
static gchar field_separator = '\t'; /* Use TAB as field separator by default */ static gchar field_separator = '\t'; /* Use TAB as field separator by default */
static gchar quote_char = '\0'; /* Do NOT quote fields by default */ static gchar quote_char = '\0'; /* Do NOT quote fields by default */
static gboolean machine_readable = FALSE; /* Display machine-readable numbers */ static gboolean machine_readable = FALSE; /* Display machine-readable numbers */
/* /*
* capinfos has the ability to report on a number of * capinfos has the ability to report on a number of
@ -135,33 +135,33 @@ static gboolean machine_readable = FALSE; /* Display machine-readable numbers
* individual options. * individual options.
*/ */
static gboolean report_all_infos = TRUE; /* Report all infos */ static gboolean report_all_infos = TRUE; /* Report all infos */
static gboolean cap_file_type = TRUE; /* Report capture type */ static gboolean cap_file_type = TRUE; /* Report capture type */
static gboolean cap_file_encap = TRUE; /* Report encapsulation */ static gboolean cap_file_encap = TRUE; /* Report encapsulation */
static gboolean cap_snaplen = TRUE; /* Packet size limit (snaplen)*/ static gboolean cap_snaplen = TRUE; /* Packet size limit (snaplen)*/
static gboolean cap_packet_count = TRUE; /* Report packet count */ static gboolean cap_packet_count = TRUE; /* Report packet count */
static gboolean cap_file_size = TRUE; /* Report file size */ static gboolean cap_file_size = TRUE; /* Report file size */
static gboolean cap_comment = TRUE; /* Display the capture comment */ static gboolean cap_comment = TRUE; /* Display the capture comment */
static gboolean cap_data_size = TRUE; /* Report packet byte size */ static gboolean cap_data_size = TRUE; /* Report packet byte size */
static gboolean cap_duration = TRUE; /* Report capture duration */ static gboolean cap_duration = TRUE; /* Report capture duration */
static gboolean cap_start_time = TRUE; /* Report capture start time */ static gboolean cap_start_time = TRUE; /* Report capture start time */
static gboolean cap_end_time = TRUE; /* Report capture end time */ static gboolean cap_end_time = TRUE; /* Report capture end time */
static gboolean time_as_secs = FALSE; /* Report time values as raw seconds */ static gboolean time_as_secs = FALSE; /* Report time values as raw seconds */
static gboolean cap_data_rate_byte = TRUE; /* Report data rate bytes/sec */ static gboolean cap_data_rate_byte = TRUE; /* Report data rate bytes/sec */
static gboolean cap_data_rate_bit = TRUE; /* Report data rate bites/sec */ static gboolean cap_data_rate_bit = TRUE; /* Report data rate bites/sec */
static gboolean cap_packet_size = TRUE; /* Report average packet size */ static gboolean cap_packet_size = TRUE; /* Report average packet size */
static gboolean cap_packet_rate = TRUE; /* Report average packet rate */ static gboolean cap_packet_rate = TRUE; /* Report average packet rate */
static gboolean cap_order = TRUE; /* Report if packets are in chronological order (True/False) */ static gboolean cap_order = TRUE; /* Report if packets are in chronological order (True/False) */
#ifdef HAVE_LIBGCRYPT #ifdef HAVE_LIBGCRYPT
static gboolean cap_file_hashes = TRUE; /* Calculate file hashes */ static gboolean cap_file_hashes = TRUE; /* Calculate file hashes */
#endif #endif
#ifdef USE_GOPTION #ifdef USE_GOPTION
static gboolean cap_help = FALSE; static gboolean cap_help = FALSE;
static gboolean table_report = FALSE; static gboolean table_report = FALSE;
static GOptionEntry general_entries[] = static GOptionEntry general_entries[] =
@ -258,9 +258,9 @@ GError *parse_err = NULL;
#endif /* USE_GOPTION */ #endif /* USE_GOPTION */
#ifdef HAVE_LIBGCRYPT #ifdef HAVE_LIBGCRYPT
#define HASH_SIZE_SHA1 20 #define HASH_SIZE_SHA1 20
#define HASH_SIZE_RMD160 20 #define HASH_SIZE_RMD160 20
#define HASH_SIZE_MD5 16 #define HASH_SIZE_MD5 16
#define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */ #define HASH_STR_SIZE (41) /* Max hash size * 2 + '\0' */
#define HASH_BUF_SIZE (1024 * 1024) #define HASH_BUF_SIZE (1024 * 1024)
@ -293,60 +293,60 @@ typedef enum {
typedef struct _capture_info { typedef struct _capture_info {
const char *filename; const char *filename;
guint16 file_type; guint16 file_type;
gboolean iscompressed; gboolean iscompressed;
int file_encap; int file_encap;
gint64 filesize; gint64 filesize;
gchar *comment; gchar *comment;
guint64 packet_bytes; guint64 packet_bytes;
gboolean times_known; gboolean times_known;
double start_time; double start_time;
double stop_time; double stop_time;
guint32 packet_count; guint32 packet_count;
gboolean snap_set; /* If set in capture file header */ gboolean snap_set; /* If set in capture file header */
guint32 snaplen; /* value from the capture file header */ guint32 snaplen; /* value from the capture file header */
guint32 snaplen_min_inferred; /* If caplen < len for 1 or more rcds */ guint32 snaplen_min_inferred; /* If caplen < len for 1 or more rcds */
guint32 snaplen_max_inferred; /* ... */ guint32 snaplen_max_inferred; /* ... */
gboolean drops_known; gboolean drops_known;
guint32 drop_count; guint32 drop_count;
double duration; double duration;
double packet_rate; double packet_rate;
double packet_size; double packet_size;
double data_rate; /* in bytes */ double data_rate; /* in bytes */
gboolean know_order; gboolean know_order;
order_t order; order_t order;
int *encap_counts; /* array of per_packet encap counts; array has one entry per wtap_encap type */ int *encap_counts; /* array of per_packet encap counts; array has one entry per wtap_encap type */
} capture_info; } capture_info;
static void static void
enable_all_infos(void) enable_all_infos(void)
{ {
report_all_infos = TRUE; report_all_infos = TRUE;
cap_file_type = TRUE; cap_file_type = TRUE;
cap_file_encap = TRUE; cap_file_encap = TRUE;
cap_snaplen = TRUE; cap_snaplen = TRUE;
cap_packet_count = TRUE; cap_packet_count = TRUE;
cap_file_size = TRUE; cap_file_size = TRUE;
cap_comment = TRUE; cap_comment = TRUE;
cap_data_size = TRUE; cap_data_size = TRUE;
cap_duration = TRUE; cap_duration = TRUE;
cap_start_time = TRUE; cap_start_time = TRUE;
cap_end_time = TRUE; cap_end_time = TRUE;
cap_order = TRUE; cap_order = TRUE;
cap_data_rate_byte = TRUE; cap_data_rate_byte = TRUE;
cap_data_rate_bit = TRUE; cap_data_rate_bit = TRUE;
cap_packet_size = TRUE; cap_packet_size = TRUE;
cap_packet_rate = TRUE; cap_packet_rate = TRUE;
#ifdef HAVE_LIBGCRYPT #ifdef HAVE_LIBGCRYPT
cap_file_hashes = TRUE; cap_file_hashes = TRUE;
#endif /* HAVE_LIBGCRYPT */ #endif /* HAVE_LIBGCRYPT */
} }
@ -374,7 +374,7 @@ disable_all_infos(void)
cap_packet_rate = FALSE; cap_packet_rate = FALSE;
#ifdef HAVE_LIBGCRYPT #ifdef HAVE_LIBGCRYPT
cap_file_hashes = FALSE; cap_file_hashes = FALSE;
#endif /* HAVE_LIBGCRYPT */ #endif /* HAVE_LIBGCRYPT */
} }
@ -400,9 +400,9 @@ order_string(order_t order)
static gchar * static gchar *
time_string(time_t timer, capture_info *cf_info, gboolean want_lf) time_string(time_t timer, capture_info *cf_info, gboolean want_lf)
{ {
const gchar *lf = want_lf ? "\n" : ""; const gchar *lf = want_lf ? "\n" : "";
static gchar time_string_buf[20]; static gchar time_string_buf[20];
char *time_string_ctime; char *time_string_ctime;
if (cf_info->times_known && cf_info->packet_count > 0) { if (cf_info->times_known && cf_info->packet_count > 0) {
if (time_as_secs) { if (time_as_secs) {
@ -450,8 +450,8 @@ static void
print_stats(const gchar *filename, capture_info *cf_info) print_stats(const gchar *filename, capture_info *cf_info)
{ {
const gchar *file_type_string, *file_encap_string; const gchar *file_type_string, *file_encap_string;
time_t start_time_t; time_t start_time_t;
time_t stop_time_t; time_t stop_time_t;
gchar *size_string; gchar *size_string;
/* Build printable strings for various stats */ /* Build printable strings for various stats */
@ -474,7 +474,7 @@ print_stats(const gchar *filename, capture_info *cf_info)
} }
if (cap_snaplen && cf_info->snap_set) if (cap_snaplen && cf_info->snap_set)
printf ("Packet size limit: file hdr: %u bytes\n", cf_info->snaplen); printf ("Packet size limit: file hdr: %u bytes\n", cf_info->snaplen);
else if(cap_snaplen && !cf_info->snap_set) else if (cap_snaplen && !cf_info->snap_set)
printf ("Packet size limit: file hdr: (not set)\n"); printf ("Packet size limit: file hdr: (not set)\n");
if (cf_info->snaplen_max_inferred > 0) { if (cf_info->snaplen_max_inferred > 0) {
if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred) if (cf_info->snaplen_min_inferred == cf_info->snaplen_max_inferred)
@ -626,8 +626,8 @@ static void
print_stats_table(const gchar *filename, capture_info *cf_info) print_stats_table(const gchar *filename, capture_info *cf_info)
{ {
const gchar *file_type_string, *file_encap_string; const gchar *file_type_string, *file_encap_string;
time_t start_time_t; time_t start_time_t;
time_t stop_time_t; time_t stop_time_t;
/* Build printable strings for various stats */ /* Build printable strings for various stats */
file_type_string = wtap_file_type_subtype_string(cf_info->file_type); file_type_string = wtap_file_type_subtype_string(cf_info->file_type);
@ -663,7 +663,7 @@ print_stats_table(const gchar *filename, capture_info *cf_info)
if (cap_snaplen) { if (cap_snaplen) {
putsep(); putsep();
putquote(); putquote();
if(cf_info->snap_set) if (cf_info->snap_set)
printf("%u", cf_info->snaplen); printf("%u", cf_info->snaplen);
else else
printf("(not set)"); printf("(not set)");
@ -843,10 +843,10 @@ process_cap_file(wtap *wth, const char *filename)
if (phdr->presence_flags & WTAP_HAS_TS) { if (phdr->presence_flags & WTAP_HAS_TS) {
prev_time = cur_time; prev_time = cur_time;
cur_time = nstime_to_sec(&phdr->ts); cur_time = nstime_to_sec(&phdr->ts);
if(packet==0) { if (packet == 0) {
start_time = cur_time; start_time = cur_time;
stop_time = cur_time; stop_time = cur_time;
prev_time = cur_time; prev_time = cur_time;
} }
if (cur_time < prev_time) { if (cur_time < prev_time) {
order = NOT_IN_ORDER; order = NOT_IN_ORDER;
@ -935,7 +935,7 @@ process_cap_file(wtap *wth, const char *filename)
/* Packet size limit (snaplen) */ /* Packet size limit (snaplen) */
cf_info.snaplen = wtap_snapshot_length(wth); cf_info.snaplen = wtap_snapshot_length(wth);
if(cf_info.snaplen > 0) if (cf_info.snaplen > 0)
cf_info.snap_set = TRUE; cf_info.snap_set = TRUE;
else else
cf_info.snap_set = FALSE; cf_info.snap_set = FALSE;
@ -981,13 +981,13 @@ process_cap_file(wtap *wth, const char *filename)
we replace linefeeds with spaces */ we replace linefeeds with spaces */
p = cf_info.comment; p = cf_info.comment;
while (*p != '\0') { while (*p != '\0') {
if (*p=='\n') if (*p == '\n')
*p=' '; *p = ' ';
p++; p++;
} }
} }
if(long_report) { if (long_report) {
print_stats(filename, &cf_info); print_stats(filename, &cf_info);
} else { } else {
print_stats_table(filename, &cf_info); print_stats_table(filename, &cf_info);
@ -1143,7 +1143,7 @@ main(int argc, char *argv[])
/* Register all the plugin types we have. */ /* Register all the plugin types we have. */
wtap_register_plugin_types(); /* Types known to libwiretap */ wtap_register_plugin_types(); /* Types known to libwiretap */
init_report_err(failure_message,NULL,NULL,NULL); init_report_err(failure_message, NULL, NULL, NULL);
/* Scan for plugins. This does *not* call their registration routines; /* Scan for plugins. This does *not* call their registration routines;
that's done later. */ that's done later. */
@ -1192,18 +1192,23 @@ main(int argc, char *argv[])
argv[0] = (char *)"capinfos"; argv[0] = (char *)"capinfos";
/* if we have at least one cmdline option, we disable printing all infos */ /* if we have at least one cmdline option, we disable printing all infos */
if (argc>=2 && report_all_infos) if (argc > 2 && report_all_infos)
disable_all_infos(); disable_all_infos();
if( !g_option_context_parse(ctx, &argc, &argv, &parse_err) ) { if ( !g_option_context_parse(ctx, &argc, &argv, &parse_err) ) {
if(parse_err) g_print ("option parsing failed: %s\n", parse_err->message); if (parse_err)
g_print("%s", g_option_context_get_help (ctx, TRUE, NULL)); g_printerr ("option parsing failed: %s\n", parse_err->message);
g_printerr("%s", g_option_context_get_help (ctx, TRUE, NULL));
exit(1); exit(1);
} }
if( cap_help || (argc < 2) ) { if ( cap_help ) {
g_print("%s", g_option_context_get_help (ctx, FALSE, NULL)); g_print("%s", g_option_context_get_help (ctx, FALSE, NULL));
exit(0); exit(0);
} }
if ( argc < 2 ) {
g_printerr("%s", g_option_context_get_help (ctx, FALSE, NULL));
exit(1);
}
g_option_context_free(ctx); g_option_context_free(ctx);
#endif /* USE_GOPTION */ #endif /* USE_GOPTION */
@ -1351,7 +1356,7 @@ main(int argc, char *argv[])
case 'h': case 'h':
usage(FALSE); usage(FALSE);
exit(1); exit(0);
break; break;
case '?': /* Bad flag - print usage message */ case '?': /* Bad flag - print usage message */
@ -1369,7 +1374,7 @@ main(int argc, char *argv[])
exit(1); exit(1);
} }
if(!long_report && table_report_header) { if (!long_report && table_report_header) {
print_stats_table_header(); print_stats_table_header();
} }
@ -1425,11 +1430,11 @@ main(int argc, char *argv[])
break; break;
} }
overall_error_status = 1; /* remember that an error has occurred */ overall_error_status = 1; /* remember that an error has occurred */
if(!continue_after_wtap_open_offline_failure) if (!continue_after_wtap_open_offline_failure)
exit(1); /* error status */ exit(1); /* error status */
} }
if(wth) { if (wth) {
if ((opt > optind) && (long_report)) if ((opt > optind) && (long_report))
printf("\n"); printf("\n");
status = process_cap_file(wth, argv[opt]); status = process_cap_file(wth, argv[opt]);
@ -1448,10 +1453,10 @@ main(int argc, char *argv[])
* *
* Local variables: * Local variables:
* c-basic-offset: 2 * c-basic-offset: 2
* tab-width: 2 * tab-width: 8
* indent-tabs-mode: nil * indent-tabs-mode: nil
* End: * End:
* *
* vi: set shiftwidth=2 tabstop=2 expandtab: * vi: set shiftwidth=2 tabstop=8 expandtab:
* :indentSize=2:tabSize=2:noTabs=true: * :indentSize=2:tabSize=8:noTabs=true:
*/ */