Remove the "-I" flag from dumpcap, and add a "-M" flag used to specify
[obnox/wireshark/wip.git] / dumpcap.c
index bf0eea77646f1b5cbdd9da6fce751bf382a46c86..2c054c492ad59d0eab0639ea04869f0c468ed257 100644 (file)
--- a/dumpcap.c
+++ b/dumpcap.c
@@ -115,6 +115,7 @@ print_usage(gboolean print_ver) {
   fprintf(output, "  -y <link type>           link layer type (def: first appropriate)\n");
   fprintf(output, "  -D                       print list of interfaces and exit\n");
   fprintf(output, "  -L                       print list of link-layer types of iface and exit\n");
+  fprintf(output, "  -M                       for -D and -L, produce machine-readable output\n");
   fprintf(output, "\n");
   fprintf(output, "Stop conditions:\n");
   fprintf(output, "  -c <packet count>        stop after n packets (def: infinite)\n");
@@ -161,7 +162,10 @@ cmdarg_err(const char *fmt, ...)
   va_list ap;
 
   if(capture_child) {
-    /* XXX - convert to g_log */
+    /* Print a bare error */
+    va_start(ap, fmt);
+    vfprintf(stderr, fmt, ap);
+    va_end(ap);
   } else {
     va_start(ap, fmt);
     fprintf(stderr, "dumpcap: ");
@@ -242,10 +246,12 @@ main(int argc, char *argv[])
   gboolean             stats_known;
   struct pcap_stat     stats;
   GLogLevelFlags       log_flags;
+  gboolean             list_interfaces = FALSE;
   gboolean             list_link_layer_types = FALSE;
+  gboolean             machine_readable = FALSE;
   int                  status;
 
-#define OPTSTRING_INIT "a:b:c:Df:hi:Lps:vw:y:Z"
+#define OPTSTRING_INIT "a:b:c:Df:hi:LMps:vw:y:Z"
 
 #ifdef _WIN32
 #define OPTSTRING_WIN32 "B:"
@@ -275,7 +281,7 @@ main(int argc, char *argv[])
   /* the default_log_handler will use stdout, which makes trouble in */
   /* capture child mode, as it uses stdout for it's sync_pipe */
   /* so do the filtering in the console_log_handler and not here */
-  log_flags = 
+  log_flags =
                    G_LOG_LEVEL_ERROR|
                    G_LOG_LEVEL_CRITICAL|
                    G_LOG_LEVEL_WARNING|
@@ -297,7 +303,7 @@ main(int argc, char *argv[])
                    log_flags,
             console_log_handler, NULL /* user_data */);
 
-  /* Set the initial values in the capture_opts. This might be overwritten 
+  /* Set the initial values in the capture_opts. This might be overwritten
      by the command line parameters. */
   capture_opts_init(capture_opts, NULL);
 
@@ -326,7 +332,7 @@ main(int argc, char *argv[])
 
         /* Assemble the run-time version information string */
         runtime_info_str = g_string_new("Running ");
-        get_runtime_version_info(runtime_info_str, NULL);              
+        get_runtime_version_info(runtime_info_str, NULL);
         show_version(comp_info_str, runtime_info_str);
         g_string_free(comp_info_str, TRUE);
         g_string_free(runtime_info_str, TRUE);
@@ -362,12 +368,14 @@ main(int argc, char *argv[])
 
       /*** all non capture option specific ***/
       case 'D':        /* Print a list of capture devices and exit */
-        status = capture_opts_list_interfaces();
-        exit_main(status);
+        list_interfaces = TRUE;
         break;
       case 'L':        /* Print list of link-layer types and exit */
         list_link_layer_types = TRUE;
         break;
+      case 'M':        /* For -D and -L, print machine-readable output */
+        machine_readable = TRUE;
+        break;
       default:
       case '?':        /* Bad flag - print usage message */
         cmdarg_err("Invalid Option: %s", argv[optind-1]);
@@ -398,7 +406,10 @@ main(int argc, char *argv[])
     exit_main(1);
   }
 
-  if (list_link_layer_types) {
+  if (list_interfaces && list_link_layer_types) {
+    cmdarg_err("Only one of -D or -L may be supplied.");
+    exit_main(1);
+  } else if (list_link_layer_types) {
     /* We're supposed to list the link-layer types for an interface;
        did the user also specify a capture file to be read? */
     /* No - did they specify a ring buffer option? */
@@ -435,8 +446,11 @@ main(int argc, char *argv[])
   /* get_interface_descriptive_name() is not available! */
   g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Interface: %s\n", capture_opts->iface);
 
-  if (list_link_layer_types) {
-    status = capture_opts_list_link_layer_types(capture_opts);
+  if (list_interfaces) {
+    status = capture_opts_list_interfaces(machine_readable);
+    exit_main(status);
+  } else if (list_link_layer_types) {
+    status = capture_opts_list_link_layer_types(capture_opts, machine_readable);
     exit_main(status);
   }
 
@@ -473,7 +487,7 @@ console_log_handler(const char *log_domain, GLogLevelFlags log_level,
 
   /* create a "timestamp" */
   time(&curr);
-  today = localtime(&curr);    
+  today = localtime(&curr);
 
   switch(log_level & G_LOG_LEVEL_MASK) {
   case G_LOG_LEVEL_ERROR:
@@ -558,7 +572,7 @@ report_cfilter_error(const char *cfilter, const char *errmsg)
         g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, "Capture filter error: %s", errmsg);
         pipe_write_block(1, SP_BAD_FILTER, errmsg);
     } else {
-        fprintf(stderr, 
+        fprintf(stderr,
           "Invalid capture filter: \"%s\"!\n"
           "\n"
           "That string isn't a valid capture filter (%s).\n"
@@ -571,9 +585,9 @@ void
 report_capture_error(const char *error_msg, const char *secondary_error_msg)
 {
     if(capture_child) {
-        g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, 
+        g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
             "Primary Error: %s", error_msg);
-        g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG, 
+        g_log(LOG_DOMAIN_CAPTURE_CHILD, G_LOG_LEVEL_DEBUG,
             "Secondary Error: %s", secondary_error_msg);
        sync_pipe_errmsg_to_parent(error_msg, secondary_error_msg);
     } else {