commit-inetutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[SCM] GNU Inetutils branch, master, updated. inetutils-1_7-33-g1c98914


From: Sergey Poznyakoff
Subject: [SCM] GNU Inetutils branch, master, updated. inetutils-1_7-33-g1c98914
Date: Mon, 08 Feb 2010 09:42:40 +0000

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU Inetutils ".

The branch, master has been updated
       via  1c9891407d45e3214d8a47273f28c33856f7ad62 (commit)
      from  c77421ed35e4a512154ee9b219e105f5e067e856 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://git.savannah.gnu.org/cgit/inetutils.git/commit/?id=1c9891407d45e3214d8a47273f28c33856f7ad62


commit 1c9891407d45e3214d8a47273f28c33856f7ad62
Author: Sergey Poznyakoff <address@hidden>
Date:   Mon Feb 8 11:40:05 2010 +0200

    Ifconfig: read format from files; implement the -s option.
    
    * ifconfig/flags.c (if_format_flags): Rename to if_list_flags.
    (if_format_flags): New function.
    (print_if_flags): Fix spelling of the 3rd argument.
    * ifconfig/flags.h (IF_FORMAT_FLAGS_BUFSIZE): New define.
    (if_format_flags): Rename to if_list_flags.
    (if_format_flags): New prototype.
    * ifconfig/ifconfig.h: Include obstack.h
    * ifconfig/options.c (formats)<netstat>: New format.
    (argp_options): New option: --short (-s).
    (parse_opt_set_default_format): Optimize.
    (parse_opt_set_default_format_from_file): New function.
    (parse_opt): Handle -s option.
    If argument to --format begins with a '@', it specifies a
    file to read format from.
    (parse_cmdline): Order interfaces alphabetically.
    * ifconfig/printif.c (put_flags_short): New function.
    (fh_flags): ${flags}{short} prints flags in short notation.

diff --git a/ChangeLog b/ChangeLog
index f5b45b0..7d3d83b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,25 @@
+2010-02-08  Sergey Poznyakoff  <address@hidden>
+
+       Ifconfig: read format from files and implement the -s option.
+
+       * ifconfig/flags.c (if_format_flags): Rename to if_list_flags.
+       (if_format_flags): New function.
+       (print_if_flags): Fix spelling of the 3rd argument.
+       * ifconfig/flags.h (IF_FORMAT_FLAGS_BUFSIZE): New define.
+       (if_format_flags): Rename to if_list_flags.
+       (if_format_flags): New prototype.
+       * ifconfig/ifconfig.h: Include obstack.h
+       * ifconfig/options.c (formats)<netstat>: New format.
+       (argp_options): New option: --short (-s).
+       (parse_opt_set_default_format): Optimize.
+       (parse_opt_set_default_format_from_file): New function.
+       (parse_opt): Handle -s option.
+       If argument to --format begins with a '@', it specifies a
+       file to read format from.
+       (parse_cmdline): Order interfaces alphabetically.
+       * ifconfig/printif.c (put_flags_short): New function.
+       (fh_flags): ${flags}{short} prints flags in short notation.
+
 2010-02-04  Alfred M. Szmidt  <address@hidden>
 
        New options --active and --passive.
diff --git a/NEWS b/NEWS
index 55f55c0..db0416e 100644
--- a/NEWS
+++ b/NEWS
@@ -26,6 +26,18 @@ now uses -a instead.
 
 New commands (multicast, allmulti) to set multicast flags.
 
+New option -s displays a short list of interfaces (similar to
+netstat -i).
+
+The --format option can be used to read format from a file.  To do so,
+prefix the file name with a '@' sign, e.g.:
+
+          ifconfig address@hidden
+
+The contents of this file is read literally, except that the lines
+beginning with a `#' sign are ignored (and can thus be used to introduce
+comments).
+
 * Man pages for the daemons are now in section 8 instead of 1.
 
 * There is now a man page for ping6 too.
diff --git a/ifconfig/flags.c b/ifconfig/flags.c
index c948cea..05371b2 100644
--- a/ifconfig/flags.c
+++ b/ifconfig/flags.c
@@ -233,7 +233,7 @@ cmpname (const void *a, const void *b)
 }
 
 char *
-if_format_flags (const char *prefix)
+if_list_flags (const char *prefix)
 {
   size_t len = 0;
   struct if_flag *fp;
@@ -377,11 +377,62 @@ if_nameztoflag (const char *name, int *prev)
   return if_nametoflag (name, strlen (name), prev);
 }
 
+struct if_flag_char
+{
+  int mask;
+  int ch;
+};
+
+/* Interface flag bits and the corresponding letters for short output.
+   Notice that the entries must be ordered alphabetically, by the letter name.
+   There are two lamentable exceptions:
+
+   1. The 'd' is misplaced.
+   2. The 'P' letter is ambiguous. Depending on its position in the output
+      line it may stand for IFF_PROMISC or IFF_POINTOPOINT.
+
+   That's the way netstat does it.
+*/
+static struct if_flag_char flag_char_tab[] = {
+  { IFF_ALLMULTI,    'A' },
+  { IFF_BROADCAST,   'B' },
+  { IFF_DEBUG,       'D' },
+  { IFF_LOOPBACK,    'L' },
+  { IFF_MULTICAST,   'M' },
+#ifdef HAVE_DYNAMIC
+  { IFF_DYNAMIC,     'd' },
+#endif
+  { IFF_PROMISC,     'P' },
+  { IFF_NOTRAILERS,  'N' },
+  { IFF_NOARP,       'O' },
+  { IFF_POINTOPOINT, 'P' },
+  { IFF_SLAVE,       's' },
+  { IFF_MASTER,      'm' },
+  { IFF_RUNNING,     'R' },
+  { IFF_UP,          'U' },
+  { 0 }
+};
+
+void
+if_format_flags (int flags, char *buf, size_t size)
+{
+  struct if_flag_char *fp;
+  size--;
+  for (fp = flag_char_tab; size && fp->mask; fp++)
+    if (fp->mask & flags)
+      {
+       *buf++ = fp->ch;
+       size--;
+      }
+  *buf = 0;
+}
+
+
 /* Print the flags in FLAGS, using AVOID as in if_flagtoname, and
-   SEPERATOR between individual flags.  Returns the number of
+   SEPARATOR between individual flags.  Returns the number of
    characters printed.  */
 int
-print_if_flags (int flags, const char *avoid, char seperator)
+print_if_flags (int flags, const char *avoid, char separator)
 {
   int f = 1;
   const char *name;
@@ -397,7 +448,7 @@ print_if_flags (int flags, const char *avoid, char 
seperator)
            {
              if (!first)
                {
-                 putchar (seperator);
+                 putchar (separator);
                  length++;
                }
              length += printf ("%s", name);
@@ -411,7 +462,7 @@ print_if_flags (int flags, const char *avoid, char 
seperator)
     {
       if (!first)
        {
-         putchar (seperator);
+         putchar (separator);
          length++;
        }
       length += printf ("%#x", flags);
diff --git a/ifconfig/flags.h b/ifconfig/flags.h
index 713cf1c..f216fcd 100644
--- a/ifconfig/flags.h
+++ b/ifconfig/flags.h
@@ -52,6 +52,10 @@ int if_nameztoflag (const char *name, int *prev);
    characters printed.  */
 int print_if_flags (int flags, const char *avoid, char seperator);
 
-char *if_format_flags (const char *prefix);
+char *if_list_flags (const char *prefix);
+
+/* Size of the buffer for the if_format_flags call */
+#define IF_FORMAT_FLAGS_BUFSIZE 15
+void if_format_flags (int flags, char *buf, size_t size);
 
 #endif
diff --git a/ifconfig/ifconfig.h b/ifconfig/ifconfig.h
index 30a7081..c5c4b8c 100644
--- a/ifconfig/ifconfig.h
+++ b/ifconfig/ifconfig.h
@@ -30,6 +30,9 @@
 # include <progname.h>
 # include <error.h>
 # include <argp.h>
+# define obstack_chunk_alloc malloc
+# define obstack_chunk_free free
+# include <obstack.h>
 # include <libinetutils.h>
 
 /* XXX */
diff --git a/ifconfig/options.c b/ifconfig/options.c
index 106e79a..273f1c8 100644
--- a/ifconfig/options.c
+++ b/ifconfig/options.c
@@ -121,6 +121,17 @@ struct format formats[] = {
    "${newline}"
    "}"
   },
+  {"netstat",
+   "${first?}{Iface    MTU Met    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR 
TX-DRP TX-OVR Flg${newline}}"
+   "${format}{check-existence}"
+   "${name}${tab}{6}${mtu}{%6d} ${metric}{%3d}"
+   "${ifstat?}{"
+   " ${rxpackets}{%8lu} ${rxerrors}{%6lu} ${rxdropped}{%6lu} 
${rxfifoerr}{%6lu}"
+   " ${txpackets}{%8lu} ${txerrors}{%6lu} ${txdropped}{%6lu} 
${txfifoerr}{%6lu}"
+   "}{   - no statistics available -}"
+   "${tab}{76} ${flags?}{${flags}{short}}{[NO FLAGS]}"
+   "${newline}"
+  },
   /* Resembles the output of ifconfig shipped with unix systems like
      Solaris 2.7 or HPUX 10.20.  */
   {"unix",
@@ -198,6 +209,8 @@ static struct argp_option argp_options[] = {
     "shut the interface down" },
   { "flags", 'F', "FLAG[,FLAG...]", 0,
     "set interface flags" },
+  { "short", 's', NULL, 0,
+    "short output format" },
   { NULL }
 };
 
@@ -322,28 +335,75 @@ parse_opt_set_point_to_point (struct ifconfig *ifp, char 
*addr)
 }
 
 void
-parse_opt_set_default_format (const char *new_format)
+parse_opt_set_default_format (const char *format)
 {
-  struct format *frm = formats;
-  const char *format = new_format;
+  struct format *frm;
 
   if (!format)
     format = system_default_format ? system_default_format : "default";
 
-  while (frm->name)
+  for (frm = formats; frm->name; frm++)
     {
-      if (!strcmp (format ? format : "default", frm->name))
-       break;
-      frm++;
+      if (!strcmp (format, frm->name))
+       {
+         default_format = frm->templ;
+         return;
+       }
     }
 
-  /* If we didn't find the format, set the user specified one.  */
-  if (frm->name)
-    default_format = frm->templ;
-  else if (format)
-    default_format = format;
-  else
-    default_format = "default";
+  default_format = format;
+}
+
+static int
+is_comment_line (const char *p, size_t len)
+{
+  while (len--)
+    {
+      int c = *p++;
+      switch (c)
+       {
+       case ' ':
+       case '\t':
+         continue;
+
+       case '#':
+         return 1;
+
+       default:
+         return 0;
+       }
+    }
+  return 0;
+}
+
+void
+parse_opt_set_default_format_from_file (const char *file)
+{
+  static struct obstack stk;
+  FILE *fp;
+  char *buf = NULL;
+  size_t size = 0;
+
+  fp = fopen (file, "r");
+  if (!fp)
+    error (EXIT_FAILURE, errno, "cannot open format file %s", file);
+
+  obstack_init (&stk);
+  while (getline (&buf, &size, fp) > 0)
+    {
+      size_t len = strlen (buf);
+
+      if (len >= 1 && buf[len-1] == '\n')
+       len--;
+
+      if (len == 0 || is_comment_line (buf, len))
+       continue;
+      obstack_grow (&stk, buf, len);
+    }
+  free (buf);
+  fclose (fp);
+  obstack_1grow (&stk, 0);
+  default_format = obstack_finish (&stk);
 }
 
 /* Must be reentrant!  */
@@ -404,6 +464,10 @@ parse_opt (int key, char *arg, struct argp_state *state)
       parse_opt_set_mtu (ifp, arg);
       break;
 
+    case 's':
+      parse_opt_set_default_format ("netstat");
+      break;
+
     case 'v':
       verbose++;
       break;
@@ -413,7 +477,10 @@ parse_opt (int key, char *arg, struct argp_state *state)
       break;
 
     case FORMAT_OPTION:                /* Output format.  */
-      parse_opt_set_default_format (arg);
+      if (arg && arg[0] == '@')
+       parse_opt_set_default_format_from_file (arg + 1);
+      else
+       parse_opt_set_default_format (arg);
       break;
 
     case UP_OPTION:
@@ -443,7 +510,7 @@ default_help_filter (int key, const char *text, void *input)
       break;
 
     case ARGP_KEY_HELP_EXTRA:
-      s = if_format_flags ("Known flags are: ");
+      s = if_list_flags ("Known flags are: ");
     }
   return s;
 }
@@ -459,6 +526,14 @@ static struct argp argp =
     default_help_filter
   };
 
+static int
+cmp_if_name (const void *a, const void *b)
+{
+  const struct ifconfig *ifa = a;
+  const struct ifconfig *ifb = b;
+
+  return strcmp (ifa->name, ifb->name);
+}
 
 void
 parse_cmdline (int argc, char *argv[])
@@ -506,5 +581,7 @@ parse_cmdline (int argc, char *argv[])
       /* XXX: We never do if_freenameindex (ifnx), because we are
         keeping the names for later instead using strdup
         (if->if_name) here.  */
+
+      qsort (ifs, nifs, sizeof (ifs[0]), cmp_if_name);
     }
 }
diff --git a/ifconfig/printif.c b/ifconfig/printif.c
index 67a4b28..f283d1e 100644
--- a/ifconfig/printif.c
+++ b/ifconfig/printif.c
@@ -334,6 +334,14 @@ put_flags (format_data_t form, int argc, char *argv[], 
short flags)
     }
 }
 
+void
+put_flags_short (format_data_t form, int argc, char *argv[], short flags)
+{
+  char buf[IF_FORMAT_FLAGS_BUFSIZE];
+  if_format_flags (flags, buf, sizeof buf);
+  put_string (form, buf);
+}
+
 /* Format handler can mangle form->format, so update it after calling
    here.  */
 void
@@ -757,6 +765,8 @@ fh_flags (format_data_t form, int argc, char *argv[])
        {
          if (!strcmp (argv[0], "number"))
            put_int (form, argc - 1, &argv[1], form->ifr->ifr_flags);
+         else if (!strcmp (argv[0], "short"))
+           put_flags_short (form, argc - 1, &argv[1], form->ifr->ifr_flags);
          else if (!strcmp (argv[0], "string"))
            put_flags (form, argc - 1, &argv[1], form->ifr->ifr_flags);
        }

-----------------------------------------------------------------------

Summary of changes:
 ChangeLog           |   22 ++++++++++
 NEWS                |   12 ++++++
 ifconfig/flags.c    |   61 ++++++++++++++++++++++++++--
 ifconfig/flags.h    |    6 ++-
 ifconfig/ifconfig.h |    3 +
 ifconfig/options.c  |  109 +++++++++++++++++++++++++++++++++++++++++++-------
 ifconfig/printif.c  |   10 +++++
 7 files changed, 201 insertions(+), 22 deletions(-)


hooks/post-receive
-- 
GNU Inetutils 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]