Add main_menubar.c
[obnox/wireshark/wip.git] / capture_ui_utils.c
index 551b78947cd0d6859540c08afdb9ff5de918a0c0..1ebe978c6831282c653c364e982c1a3e234637c7 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <ctype.h>
 #include <glib.h>
 
-#include <epan/prefs.h>
-#include "capture-pcap-util.h"
+#include "epan/prefs.h"
+#include "epan/ex-opt.h"
+#include "capture_ifinfo.h"
 #include "capture_ui_utils.h"
 
 /*
@@ -130,12 +132,12 @@ capture_dev_user_linktype_find(const gchar *if_name)
 /*
  * Return as descriptive a name for an interface as we can get.
  * If the user has specified a comment, use that.  Otherwise,
- * if get_interface_list() supplies a description, use that,
+ * if capture_interface_list() supplies a description, use that,
  * otherwise use the interface name.
  *
  * The result must be g_free()'d when you're done with it.
  *
- * Note: given that this calls get_interface_list(), which attempts to
+ * Note: given that this calls capture_interface_list(), which attempts to
  * open all adapters it finds in order to check whether they can be
  * captured on, this is an expensive routine to call, so don't call it
  * frequently.
@@ -154,12 +156,23 @@ get_interface_descriptive_name(const char *if_name)
   if (descr != NULL) {
     /* Yes - make a copy of that. */
     descr = g_strdup(descr);
+  } else if (strcmp(if_name, "-") == 0) {
+    /*
+     * Strictly speaking, -X (extension) options are for modules, e.g. Lua
+     * and using one here stretches that definition. However, this doesn't
+     * waste a single-letter option on something that might be rarely used
+     * and is backward-compatible to 1.0.
+     */
+    descr = g_strdup(ex_opt_get_nth("stdin_descr", 0));
+    if (!descr) {
+      descr = g_strdup("Standard input");
+    }
   } else {
     /* No, we don't have a user-supplied description; did we get
        one from the OS or libpcap? */
     descr = NULL;
-    if_list = get_interface_list(&err, NULL);
-    if (if_list != NULL && if_name != NULL) {
+    if_list = capture_interface_list(&err, NULL);
+    if (if_list != NULL) {
       if_entry = if_list;
       do {
         if_info = if_entry->data;
@@ -190,19 +203,19 @@ get_interface_descriptive_name(const char *if_name)
 static if_info_t *
 search_info(GList *if_list, gchar *if_name)
 {
-    GList *if_entry;
-    if_info_t *if_info;
+  GList *if_entry;
+  if_info_t *if_info;
 
 
-    for (if_entry = if_list; if_entry != NULL; if_entry = g_list_next(if_entry)) {
-        if_info = if_entry->data;
+  for (if_entry = if_list; if_entry != NULL; if_entry = g_list_next(if_entry)) {
+    if_info = if_entry->data;
 
-        if(strcmp(if_name, if_info->name) == 0) {
-            return if_info;
-        }
+    if(strcmp(if_name, if_info->name) == 0) {
+      return if_info;
     }
+  }
 
-    return NULL;
+  return NULL;
 }
 
 
@@ -210,32 +223,31 @@ search_info(GList *if_list, gchar *if_name)
 char *
 build_capture_combo_name(GList *if_list, gchar *if_name)
 {
-    gchar *descr;
-    char *if_string;
-    if_info_t *if_info;
-
-
-       /* Do we have a user-supplied description? */
-       descr = capture_dev_user_descr_find(if_name);
-       if (descr != NULL) {
-         /* Yes, we have a user-supplied description; use it. */
-         if_string = g_strdup_printf("%s: %s", descr, if_name);
-         g_free(descr);
-       } else {
-         /* No, we don't have a user-supplied description; did we get
-            one from the OS or libpcap? */
-      if_info = search_info(if_list, if_name);
-         if (if_info && if_info->description != NULL) {
-           /* Yes - use it. */
-           if_string = g_strdup_printf("%s: %s", if_info->description,
-                                       if_info->name);
-         } else {
-           /* No. */
-           if_string = g_strdup(if_name);
-         }
-       }
+  gchar *descr;
+  char *if_string;
+  if_info_t *if_info;
+
+  /* Do we have a user-supplied description? */
+  descr = capture_dev_user_descr_find(if_name);
+  if (descr != NULL) {
+    /* Yes, we have a user-supplied description; use it. */
+    if_string = g_strdup_printf("%s: %s", descr, if_name);
+    g_free(descr);
+  } else {
+    /* No, we don't have a user-supplied description; did we get
+     one from the OS or libpcap? */
+    if_info = search_info(if_list, if_name);
+    if (if_info != NULL && if_info->description != NULL) {
+      /* Yes - use it. */
+      if_string = g_strdup_printf("%s: %s", if_info->description,
+                                  if_info->name);
+    } else {
+      /* No. */
+      if_string = g_strdup(if_name);
+    }
+  }
 
-    return if_string;
+  return if_string;
 }
 
 
@@ -258,29 +270,29 @@ build_capture_combo_list(GList *if_list, gboolean do_hide)
       /* Is this interface hidden and, if so, should we include it
          anyway? */
       if (!prefs_is_capture_device_hidden(if_info->name) || !do_hide) {
-       /* It's not hidden, or it is but we should include it in the list. */
-
-       /* Do we have a user-supplied description? */
-       descr = capture_dev_user_descr_find(if_info->name);
-       if (descr != NULL) {
-         /* Yes, we have a user-supplied description; use it. */
-         if_string = g_strdup_printf("%s: %s", descr, if_info->name);
-         g_free(descr);
-       } else {
-         /* No, we don't have a user-supplied description; did we get
-            one from the OS or libpcap? */
-         if (if_info->description != NULL) {
-           /* Yes - use it. */
-           if_string = g_strdup_printf("%s: %s", if_info->description,
+        /* It's not hidden, or it is but we should include it in the list. */
+
+        /* Do we have a user-supplied description? */
+        descr = capture_dev_user_descr_find(if_info->name);
+        if (descr != NULL) {
+             /* Yes, we have a user-supplied description; use it. */
+             if_string = g_strdup_printf("%s: %s", descr, if_info->name);
+          g_free(descr);
+        } else {
+          /* No, we don't have a user-supplied description; did we get
+                one from the OS or libpcap? */
+          if (if_info->description != NULL) {
+            /* Yes - use it. */
+               if_string = g_strdup_printf("%s: %s", if_info->description,
                                        if_info->name);
-         } else {
-           /* No. */
-           if_string = g_strdup(if_info->name);
-         }
-       }
-       combo_list = g_list_append(combo_list, if_string);
+          } else {
+            /* No. */
+            if_string = g_strdup(if_info->name);
+          }
+        }
+        combo_list = g_list_append(combo_list, if_string);
       }
-    }
+    }/*for*/
   }
   return combo_list;
 }
@@ -383,17 +395,26 @@ get_if_name(const char *if_text)
   return if_name;
 }
 
-/*  Return capture_opts->iface_descr (after setting it if it is not set)
- *  This is necessary because capture_opts.c can't set iface_descr (at least
+/*  Return interface_opts->descr (after setting it if it is not set)
+ *  This is necessary because capture_opts.c can't set descr (at least
  *  not without adding significant dependencies there).
  */
 const char *
-get_iface_description(capture_options *capture_opts)
+get_iface_description_for_interface(capture_options *capture_opts, guint i)
 {
-       if (!capture_opts->iface_descr && capture_opts->iface)
-               capture_opts->iface_descr = get_interface_descriptive_name(capture_opts->iface);
-
-       return(capture_opts->iface_descr);
-
+  interface_options interface_opts;
+
+  if (i < capture_opts->ifaces->len) {
+    interface_opts = g_array_index(capture_opts->ifaces, interface_options, i);
+    if (!interface_opts.descr && interface_opts.name) {
+      interface_opts.descr = get_interface_descriptive_name(interface_opts.name);
+      capture_opts->ifaces = g_array_remove_index(capture_opts->ifaces, i);
+      g_array_insert_val(capture_opts->ifaces, i, interface_opts);
+    }
+    return (interface_opts.descr);
+  } else {
+    return (NULL);
+  }
 }
+
 #endif /* HAVE_LIBPCAP */