bug-wget
[Top][All Lists]
Advanced

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

[Bug-wget] Bug #20388


From: jpichon
Subject: [Bug-wget] Bug #20388
Date: Fri, 27 Feb 2009 20:08:35 +0100
User-agent: Thunderbird 2.0.0.19 (X11/20090105)

Hi all,

I suggest a patch in order to correct the bug #20388
(https://savannah.gnu.org/bugs/?func=detailitem&item_id=20388#options),
concerning tilde expansion.


The code lay-out looks like:

static bool enable_tilde_expansion;

static const struct {
   const char *name;
   void *place;
   bool (*action) (const char *, const char *, void *)
} commands [] = {
   {"cacertificate",    &opt.ca_cert    cmd_file}
};

static bool cmd_file(const char *com, const char *val, void *place)
{
   /* Put the right value (tilde expanded or not in place) */
}

static bool cmd_directory(const char *com, const char *val, void *place)
{
   cmd_file(com, val, place);
   /* do something */
}
static bool setval_internal(int comind, const char *val, const char *place)
{
return commands[comind].action(com, val, commands[comind].place); /* call cmd_file sometime */
}

static bool run_wgetrc(const char *file)
{
   enable_tilde_expansion = true;
   /* Parse the line and find the proper type of parameter */
   while(parse_line(comind, com, val))
    {
       setval_internal(comind, com, val)
    }
   enable_tilde_expansion = false;
}


int main()
{
initialize(); /* Calls the run_wgetrc and so the cmd_file with tilde expansion */

   /* Parse line argument an options */
   while(getopt_long())
    {
setoptval(); /* calls setval_internal ans so cmd_file without tilde expansion */
    }
}



We see that we can't change the function signature of cmd_file because it complies with the bool (*action) (const char *, const char *, void *) type which is widely used.

So I've made a wrapper called setval_internal_wrapper that the run_wgetrc called instead of setval_internal_wrapper. This new function called setval_wrapper and performs the tilde
expansion.

Note: I've changed the ISSEP macro place in order to use it.

--- init.orig.c 2009-02-27 18:44:27.000000000 +0100
+++ init.c      2009-02-27 19:44:08.000000000 +0100
@@ -58,11 +58,6 @@
 #include "test.h"
 #endif
 
-/* We want tilde expansion enabled only when reading `.wgetrc' lines;
-   otherwise, it will be performed by the shell.  This variable will
-   be set by the wgetrc-reading function.  */
-
-static bool enable_tilde_expansion;
 
 
 #define CMD_DECLARE(func) static bool func (const char *, const char *, void *)
@@ -473,6 +468,7 @@
 
 static enum parse_line parse_line (const char *, char **, char **, int *);
 static bool setval_internal (int, const char *, const char *);
+static bool setval_internal_wrapper (int, const char *, const char *);
 
 /* Initialize variables from a wgetrc file.  Returns zero (failure) if
    there were errors in the file.  */
@@ -492,7 +488,6 @@
                file, strerror (errno));
       return true;                      /* not a fatal error */
     }
-  enable_tilde_expansion = true;
   ln = 1;
   while ((line = read_whole_line (fp)) != NULL)
     {
@@ -504,7 +499,7 @@
         {
         case line_ok:
           /* If everything is OK, set the value.  */
-          if (!setval_internal (comind, com, val))
+          if (!setval_internal_wrapper (comind, com, val))
             {
               fprintf (stderr, _("%s: Error in %s at line %d.\n"),
                        exec_name, file, ln);
@@ -531,7 +526,6 @@
       xfree (line);
       ++ln;
     }
-  enable_tilde_expansion = false;
   fclose (fp);
 
   return errcnt == 0;
@@ -663,6 +657,12 @@
   return line_ok;
 }
 
+#if defined(WINDOWS) || defined(MSDOS)
+# define ISSEP(c) ((c) == '/' || (c) == '\\')
+#else
+# define ISSEP(c) ((c) == '/')
+#endif
+
 /* Run commands[comind].action. */
 
 static bool
@@ -673,6 +673,36 @@
   return commands[comind].action (com, val, commands[comind].place);
 }
 
+static bool
+setval_internal_wrapper (int comind, const char *com, const char *val)
+{
+  bool ret;
+  int homelen;
+  char *home;
+  char **pstring;
+  ret = setval_internal (comind, com, val);
+
+  /* We make tilde expansion for cmd_file and cmd_directory */
+  if (((commands[comind].action == cmd_file) ||
+       (commands[comind].action == cmd_directory)) && ret)
+    {
+      pstring = commands[comind].place;
+      home = home_dir();
+      if (home)
+       {
+         homelen = strlen(home);
+         while (homelen && ISSEP(home[homelen - 1]))
+            home[--homelen] = '\0';
+
+         /* Skip the leading "~/". */
+         for (++val; ISSEP(*val); val++)
+           ;
+         *pstring = concat_strings (home, "/", val, (char *)0);
+       }
+    }
+  return ret;
+}
+
 /* Run command COM with value VAL.  If running the command produces an
    error, report the error and exit.
 
@@ -808,11 +838,6 @@
   return true;
 }
 
-#if defined(WINDOWS) || defined(MSDOS)
-# define ISSEP(c) ((c) == '/' || (c) == '\\')
-#else
-# define ISSEP(c) ((c) == '/')
-#endif
 
 /* Like the above, but handles tilde-expansion when reading a user's
    `.wgetrc'.  In that case, and if VAL begins with `~', the tilde
@@ -826,28 +851,7 @@
 
   /* #### If VAL is empty, perhaps should set *PLACE to NULL.  */
 
-  if (!enable_tilde_expansion || !(*val == '~' && ISSEP (val[1])))
-    {
-    noexpand:
-      *pstring = xstrdup (val);
-    }
-  else
-    {
-      int homelen;
-      char *home = home_dir ();
-      if (!home)
-        goto noexpand;
-
-      homelen = strlen (home);
-      while (homelen && ISSEP (home[homelen - 1]))
-        home[--homelen] = '\0';
-
-      /* Skip the leading "~/". */
-      for (++val; ISSEP (*val); val++)
-        ;
-
-      *pstring = concat_strings (home, "/", val, (char *) 0);
-    }
+  *pstring = xstrdup (val);
 
 #if defined(WINDOWS) || defined(MSDOS)
   /* Convert "\" to "/". */

reply via email to

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