bug-hurd
[Top][All Lists]
Advanced

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

Re: showtrans --active (hopefully: more compliant patch)


From: David Walter
Subject: Re: showtrans --active (hopefully: more compliant patch)
Date: Sat, 17 Aug 2002 22:02:44 -0400
User-agent: Gnus/5.090007 (Oort Gnus v0.07) XEmacs/21.4 (Honest Recruiter, i386-unknown-gnu0.2)

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 01:35:15 -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,35 +49,134 @@
 "\vA FILE argument of `-' prints the translator on the node"
 " attached to standard input.";
 
-/* ---------------------------------------------------------------- */
+
+error_t
+get_active_translator_info (const char *path, char **trans,
+                            int *trans_len)
+{
+   char *transinfo = NULL;
+   char *toptransinfo = NULL;
+
+   file_t trans_node;
+
+   int transinfolen = 0;
+
+   struct store *storeinfo = NULL;
+
+   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  */
+      file_t file = file_name_lookup (path, O_NOTRANS, 0); 
+      fsys_t fsys;
+      /* get the active translator if there is one.  */
+      error_t active_trans_rc = file_get_translator_cntl (file, &fsys); 
+      mach_port_deallocate (mach_task_self (), file);
+      if (active_trans_rc)
+         return EINVAL;
+   }
+   /*
+     The root partition (in particular) needs to use the storeio info.
+    
+     Others (other translated nodes) require only the information from
+     the translator.
+
+     If, it is actively translated, get the information re: the
+     translation
+   */
+
+   trans_node = file_name_lookup (path, 0, 0666);
+   int s_rc =
+   store_create (trans_node, STORE_INACTIVE | STORE_NO_FILEIO, 0,
+                 &storeinfo);
+   int m_rc = mach_port_deallocate (mach_task_self (), trans_node);
+
+
+   trans_node = file_name_lookup (path, 0, 0666);
+
+   int f_rc = file_get_fs_options (trans_node, &toptransinfo, &transinfolen);
+   m_rc = mach_port_deallocate (mach_task_self (), trans_node);
+
+   if (trans_node == MACH_PORT_NULL || f_rc != 0)
+   {           
+      if (toptransinfo != NULL)
+         munmap (toptransinfo, transinfolen);
+      return EINVAL;
+   }
+
+   char *options_start = toptransinfo + strlen (toptransinfo) + 1;
+
+   argz_stringify (toptransinfo, transinfolen, ' ');
+
+   *trans = toptransinfo;
+   *trans_len = transinfolen;
+
+   if (storeinfo)
+      store_free (storeinfo);
+
+
+   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);
+      REQUEST request = passive;
+      if (show_active && !show_passive)
+         request = active;
+      if (show_active && show_passive)
+         request = both;
 
-         switch (err)
+      switch (request)
            {
-           case 0:
-             /* Make the '\0's in TRANS printable.  */
+      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, &trans_len);
              argz_stringify (trans, trans_len, ' ');
-
+         if (!err)
+         {
              if (!silent)
                {
                  if (print_prefix)
@@ -83,28 +184,46 @@
                  else
                    printf ("%.*s\n", (int) trans_len, trans);
                }
-
              if (trans != buf)
                munmap (trans, trans_len);
-
              status = 0;
-
+         }
+         if (request != both)
              break;
+         /* *NOTICE* fall through if doing both */
+      case active:
+         /*
+           If we get a translator from the node, it was a good
+           translator
+         */
+         err = file_get_translator (node, &trans, &trans_len);
+         if (err) /* if not, have to do some special work */                 
+            err = get_active_translator_info (name, &trans, &trans_len);
+
+         if (!err)
+         {
+            argz_stringify (trans, trans_len, ' ');
+            trans[trans_len] = '\0';
+            if (!silent)
+            {
+               if (print_prefix)
+                  printf ("*%s: %.*s\n", name, (int) trans_len, trans);
+               else
+                  printf ("*%.*s\n", (int) trans_len, trans);
+            }
+            if (trans != buf)
+               munmap (trans, trans_len);
 
-           case EINVAL:
-             /* NODE just doesn't have a translator.  */
-             if (!silent && print_prefix && show_untrans)
-               puts (name);
+            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 +231,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]