From ca397aa5b00e320e2bae7a6e3ed000eaa240e635 Mon Sep 17 00:00:00 2001 From: Julien Pichon Date: Sat, 13 Jun 2009 16:45:09 -0700 Subject: [PATCH] Tilde expansion without globals --- src/ChangeLog | 6 +++++ src/init.c | 74 +++++++++++++++++++++++++++------------------------ 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 58e44a87..e519a21c 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,9 @@ +2009-06-13 Julien Pichon + + * init.c: Handle tilde-expansion in wgetrc commands, without + resorting to setting/unsetting globals to change behavior in one + call location. + 2009-06-12 Micah Cowan * host.c: Include before . Not diff --git a/src/init.c b/src/init.c index 5bed28f6..da900cce 100644 --- a/src/init.c +++ b/src/init.c @@ -58,11 +58,6 @@ as that of the covered work. */ #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 @@ enum parse_line { 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 @@ run_wgetrc (const char *file) 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 @@ run_wgetrc (const char *file) { 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 @@ run_wgetrc (const char *file) xfree (line); ++ln; } - enable_tilde_expansion = false; fclose (fp); return errcnt == 0; @@ -668,6 +662,12 @@ parse_line (const char *line, char **com, char **val, int *comind) 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 @@ -678,6 +678,36 @@ setval_internal (int comind, const char *com, const char *val) 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. @@ -813,11 +843,6 @@ cmd_string (const char *com, const char *val, void *place) 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 @@ -831,28 +856,7 @@ cmd_file (const char *com, const char *val, void *place) /* #### 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 "/". */ -- 2.39.2