bug-hurd
[Top][All Lists]
Advanced

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

Re: showtrans --active


From: David Walter
Subject: Re: showtrans --active
Date: Sun, 18 Aug 2002 01:13:43 -0400
User-agent: Gnus/5.090007 (Oort Gnus v0.07) XEmacs/21.4 (Honest Recruiter, i386-unknown-gnu0.2)

Marcus Brinkmann <Marcus.Brinkmann@ruhr-uni-bochum.de> writes:

> Hi David,
>
> first, please submit patches following the GNU coding standards.  Read the
> GNU coding standards and have a look at the Hurd sources for the details.
> It makes the code so much easier to read for us.

I hope the following is in better form.

> ajax:/tmp# settrans -ac foo /hurd/hello
> ajax:/tmp# settrans -pk foo /hurd/null
> ajax:/tmp# cat foo
> Hello, world!
> ajax:/tmp# showtrans foo
> /hurd/null
> ajax:/tmp# fsysopts foo
> /hurd/hello --contents=Hello, world!

Thanks, I had considered but not tested this specific scenario, so
didn't see that the only translator that was being found was the
passive, when both existed.

> Please study this case carefully, and test your showtrans
> modification on it.  Does your code do what you expect it to do?

I believe that now it does, and more cleanly, with no redundancies.
./showtrans --active --passive foo /mnt/* /opt /home /var/run/mtab /

foo: /hurd/null
*foo: /hurd/hello --contents=Hello, world!

*/mnt/12: /hurd/ext2fs --writable /dev/hd0s12
*/mnt/16: /hurd/ext2fs --writable /dev/hd0s16
*/mnt/17: /hurd/ext2fs --writable /dev/hd0s17
/mnt/5: /hurd/ext2fs.static /dev/hd0s5
/opt: /hurd/ext2fs /dev/hd0s13
*/opt: /hurd/ext2fs --writable /dev/hd0s13
/var/run/mtab: /hurd/mtab
*/var/run/mtab: /hurd/mtab
*/: /hurd/ext2fs.static --writable --store-type=typed device:hd0s15


Index: showtrans.c
===================================================================
RCS file: /cvsroot/hurd/hurd/utils/showtrans.c,v
retrieving revision 1.17
diff --unified -w -i -r1.17 showtrans.c
--- showtrans.c 28 May 2002 23:56:34 -0000      1.17
+++ showtrans.c 18 Aug 2002 05:03:32 -0000
@@ -26,18 +26,20 @@
 #include <unistd.h>
 #include <version.h>
 #include <sys/mman.h>
-
 #include <error.h>
 #include <argz.h>
-
+#include <hurd/hurd_types.h>
+#include <hurd/store.h>
 const char *argp_program_version = STANDARD_HURD_VERSION (showtrans);
 
-static struct argp_option options[] =
-{
+static struct argp_option options[] = {
   {"prefix",    'p', 0, 0, "Always display `FILENAME: ' before translators"},
   {"no-prefix", 'P', 0, 0, "Never display `FILENAME: ' before translators"},
   {"silent",    's', 0, 0, "No output; useful when checking error status"},
   {"quiet",     'q', 0, OPTION_ALIAS | OPTION_HIDDEN},
+   {"active", 'a', 0, 0, "show active translators. Denoted by an * "},
+   {"passive", 'd', 0, 0,
+    "(d)efaults to passive, but allow the user to force printing both."},
   {"translated",'t', 0, 0, "Only display files that have translators"},
   {0, 0}
 };
@@ -47,64 +49,145 @@
 "\vA FILE argument of `-' prints the translator on the node"
 " attached to standard input.";
 
-/* ---------------------------------------------------------------- */
+
+error_t
+get_active_translator_info (const char *path, char **translator, int *length)
+{
+
+   file_t node;
+
+   errno = 0;
+   /*
+     step 1. is there an active translator on this path? 
+     determine if this is an (actively) translated path or not.
+     
+     But, skip if root, special case for root.
+   */
+   if (strcmp (path, "/") != 0)
+   {
+      /*
+        get the file w/o the translator  */
+      
+      node = file_name_lookup (path, O_NOTRANS, 0666); 
+
+      /*
+        test: is there an active translator? */
+
+      fsys_t fsys;
+      error_t no_active_trans = file_get_translator_cntl (node, &fsys);
+      if(node != MACH_PORT_NULL)
+         mach_port_deallocate (mach_task_self (), node);
+      if (no_active_trans)
+         return EINVAL;
+   }
+
+   node = file_name_lookup (path, 0, 0666); 
+   /*
+     get the active translator information.
+   */
+   if( file_get_fs_options (node, translator, length) != 0)
+      if (node == MACH_PORT_NULL)
+         return EINVAL;
+
+   mach_port_deallocate (mach_task_self (), node);
+   argz_stringify (*translator, *length, ' ');
+
+   return 0;
+}
+
 
 int
 main (int argc, char *argv[])
 {
-  /* The default exit status -- changed to 0 if we find any translators.  */
+   /* The default exit status -- changed to 0 if we find any
+    * translators.  */
   int status = 1;
   /* Some option flags.  -1 for PRINT_PREFIX means use the default.  */
   int print_prefix = -1, silent = 0, show_untrans = 1;
 
-  /* If NODE is MACH_PORT_NULL, prints an error message and exits, otherwise
-     prints the translator on NODE, possibly prefixed by `NAME:', and
-     deallocates NODE.  */
+   boolean_t show_active = FALSE;
+   boolean_t show_passive = FALSE;
+
+   typedef enum REQUEST
+   {
+      passive,           /* default */
+      active,            /* only */
+      both,              /* print two lines if active and passive */
+   }
+   REQUEST;
+
+   /* If NODE is MACH_PORT_NULL, prints an error message and exits,
+      otherwise prints the translator on NODE, possibly prefixed by
+      `NAME:', and deallocates NODE.  */
   void print_node_trans (file_t node, char *name)
     {
+      error_t err = 0;
+      typedef mach_port_t fsys_t;
+
       if (node == MACH_PORT_NULL)
-       error (0, errno, "%s", name);
-      else
        {
+         error (0, errno, "%s", name);
+         return;
+      }
+      /* Can't get here unless node is okay. */
          char buf[1024], *trans = buf;
-         size_t trans_len = sizeof (buf);
-         error_t err = file_get_translator (node, &trans, &trans_len);
-
-         switch (err)
+      size_t length = sizeof (buf);
+      REQUEST request = passive;
+      if (show_active && !show_passive)
+         request = active;
+      if (show_active && show_passive)
+         request = both;
+
+      switch (request)
+      {
+      case both:
+      case passive:
+         /* get the file w/o the translator */
+         node = file_name_lookup (name, O_NOTRANS, 0);   
+         err = file_get_translator (node, &trans, &length);
+         argz_stringify (trans, length, ' ');
+         if (!err)
            {
-           case 0:
-             /* Make the '\0's in TRANS printable.  */
-             argz_stringify (trans, trans_len, ' ');
-
              if (!silent)
                {
                  if (print_prefix)
-                   printf ("%s: %.*s\n", name, (int) trans_len, trans);
+                  printf ("%s: %.*s\n", name, (int) length, trans);
                  else
-                   printf ("%.*s\n", (int) trans_len, trans);
+                  printf ("%.*s\n", (int) length, trans);
                }
-
              if (trans != buf)
-               munmap (trans, trans_len);
-
+               munmap (trans, length);
              status = 0;
-
+         }
+         if (request != both)
              break;
+         /* *NOTICE* fall through if doing both */
+      case active:
 
-           case EINVAL:
-             /* NODE just doesn't have a translator.  */
-             if (!silent && print_prefix && show_untrans)
-               puts (name);
+         if (get_active_translator_info (name, &trans, &length) == 0)
+         {
+            argz_stringify (trans, length, ' ');
+            trans[length] = '\0';
+            if (!silent)
+            {
+               if (print_prefix)
+                  printf ("*%s: %.*s\n", name, (int) length, trans);
+               else
+                  printf ("*%.*s\n", (int) length, trans);
+            }
+            if (trans != buf)
+               munmap (trans, length);
+
+            status = 0;
+         }
              break;
 
            default:
-             error (0, err, "%s", name);
+         break;
            }
-
          mach_port_deallocate (mach_task_self (), node);
+      return;
        }
-    }
-
   /* Parse a command line option.  */
   error_t parse_opt (int key, char *arg, struct argp_state *state)
     {
@@ -112,21 +195,26 @@
        {
        case ARGP_KEY_ARG:      /* A FILE argument */
          if (print_prefix < 0)
-           /* By default, only print a prefix if there are multiple files. */
+            /* By default, only print a prefix if there are
+             * multiple files. */
            print_prefix = state->next < state->argc;
 
          if (strcmp (arg, "-") != 0)
+         {
            print_node_trans (file_name_lookup (arg, O_NOTRANS, 0), arg);
+         }
          else
            print_node_trans (getdport (0), "-");
          break;
 
          /* Options. */
+         /* Options. */
        case 'p': print_prefix = 1; break;
        case 'P': print_prefix = 0; break;
        case 's': case 'q': silent = 1; break;
        case 't': show_untrans = 0; break;
-
+      case 'a': show_active = 1; break;
+      case 'd': show_passive = 1; break;
        case ARGP_KEY_NO_ARGS:
          argp_usage (state);   /* exits */
 
-- 
/^\
\ /     ASCII RIBBON CAMPAIGN
 X        AGAINST HTML MAIL
/ \




reply via email to

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