#include <config.h>
#include <stdio.h>
-#include <ctype.h>
#include <sys/types.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include "host.h"
#include "recur.h"
#include "netrc.h"
+#include "cookies.h" /* for cookies_cleanup */
#ifndef errno
extern int errno;
#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 int enable_tilde_expansion;
+
#define CMD_DECLARE(func) static int func \
PARAMS ((const char *, const char *, void *))
CMD_DECLARE (cmd_number);
CMD_DECLARE (cmd_number_inf);
CMD_DECLARE (cmd_string);
+CMD_DECLARE (cmd_file);
CMD_DECLARE (cmd_time);
CMD_DECLARE (cmd_vector);
{ "cache", &opt.proxy_cache, cmd_boolean },
{ "continue", &opt.always_rest, cmd_boolean },
{ "convertlinks", &opt.convert_links, cmd_boolean },
+ { "cookies", &opt.cookies, cmd_boolean },
{ "cutdirs", &opt.cut_dirs, cmd_number },
#ifdef DEBUG
{ "debug", &opt.debug, cmd_boolean },
#endif
{ "deleteafter", &opt.delete_after, cmd_boolean },
- { "dirprefix", &opt.dir_prefix, cmd_string },
+ { "dirprefix", &opt.dir_prefix, cmd_file },
{ "dirstruct", NULL, cmd_spec_dirstruct },
{ "domains", &opt.domains, cmd_vector },
{ "dotbytes", &opt.dot_bytes, cmd_bytes },
{ "header", NULL, cmd_spec_header },
{ "htmlextension", &opt.html_extension, cmd_boolean },
{ "htmlify", NULL, cmd_spec_htmlify },
+ { "httpkeepalive", &opt.http_keep_alive, cmd_boolean },
{ "httppasswd", &opt.http_passwd, cmd_string },
{ "httpproxy", &opt.http_proxy, cmd_string },
+ { "httpsproxy", &opt.https_proxy, cmd_string },
{ "httpuser", &opt.http_user, cmd_string },
{ "ignorelength", &opt.ignore_length, cmd_boolean },
{ "ignoretags", &opt.ignore_tags, cmd_vector },
{ "includedirectories", &opt.includes, cmd_directory_vector },
- { "input", &opt.input_filename, cmd_string },
+ { "input", &opt.input_filename, cmd_file },
{ "killlonger", &opt.kill_longer, cmd_boolean },
- { "logfile", &opt.lfilename, cmd_string },
+ { "loadcookies", &opt.cookies_input, cmd_file },
+ { "logfile", &opt.lfilename, cmd_file },
{ "login", &opt.ftp_acc, cmd_string },
{ "mirror", NULL, cmd_spec_mirror },
{ "netrc", &opt.netrc, cmd_boolean },
{ "noparent", &opt.no_parent, cmd_boolean },
{ "noproxy", &opt.no_proxy, cmd_vector },
{ "numtries", &opt.ntry, cmd_number_inf },/* deprecated*/
- { "outputdocument", &opt.output_document, cmd_string },
+ { "outputdocument", &opt.output_document, cmd_file },
{ "pagerequisites", &opt.page_requisites, cmd_boolean },
{ "passiveftp", &opt.ftp_pasv, cmd_lockable_boolean },
{ "passwd", &opt.ftp_pass, cmd_string },
{ "removelisting", &opt.remove_listing, cmd_boolean },
{ "retrsymlinks", &opt.retr_symlinks, cmd_boolean },
{ "robots", &opt.use_robots, cmd_boolean },
+ { "savecookies", &opt.cookies_output, cmd_file },
{ "saveheaders", &opt.save_headers, cmd_boolean },
{ "serverresponse", &opt.server_response, cmd_boolean },
{ "simplehostcheck", &opt.simple_check, cmd_boolean },
{ "spanhosts", &opt.spanhost, cmd_boolean },
{ "spider", &opt.spider, cmd_boolean },
+#ifdef HAVE_SSL
+ { "sslcertfile", &opt.sslcertfile, cmd_file },
+ { "sslcertkey", &opt.sslcertkey, cmd_file },
+#endif /* HAVE_SSL */
{ "timeout", &opt.timeout, cmd_time },
{ "timestamping", &opt.timestamping, cmd_boolean },
{ "tries", &opt.ntry, cmd_number_inf },
of the implementors' worries. */
memset (&opt, 0, sizeof (opt));
+ opt.cookies = 1;
+
opt.verbose = -1;
opt.dir_prefix = xstrdup (".");
opt.ntry = 20;
opt.netrc = 1;
opt.ftp_glob = 1;
opt.htmlify = 1;
+ opt.http_keep_alive = 1;
opt.use_proxy = 1;
tmp = getenv ("no_proxy");
if (tmp)
return NULL;
if (!file_exists_p (file))
{
- free (file);
+ xfree (file);
return NULL;
}
return file;
file, strerror (errno));
return;
}
- /* Reset line number. */
+ enable_tilde_expansion = 1;
ln = 1;
while ((line = read_whole_line (fp)))
{
/* Parse the line. */
status = parse_line (line, &com, &val);
- free (line);
+ xfree (line);
/* If everything is OK, set the value. */
if (status == 1)
{
if (!setval (com, val))
fprintf (stderr, _("%s: Error in %s at line %d.\n"), exec_name,
file, ln);
- free (com);
- free (val);
+ xfree (com);
+ xfree (val);
}
else if (status == 0)
fprintf (stderr, _("%s: Error in %s at line %d.\n"), exec_name,
file, ln);
++ln;
}
+ enable_tilde_expansion = 0;
fclose (fp);
}
else
#endif
run_wgetrc (file);
- free (file);
+ xfree (file);
return;
}
\f
/* If the command is invalid, exit now. */
if (comind (*com) == -1)
{
- free (*com);
+ xfree (*com);
return 0;
}
/* If '=' not found, bail out. */
if (*p != '=')
{
- free (*com);
+ xfree (*com);
return 0;
}
/* Skip spaces after '='. */
static int myatoi PARAMS ((const char *s));
-/* Store the address (specified as hostname or dotted-quad IP address) from VAL
- to CLOSURE. COM is ignored, except for error messages. */
+/* Interpret VAL as an Internet address (a hostname or a dotted-quad
+ IP address), and write it (in network order) to a malloc-allocated
+ address. That address gets stored to the memory pointed to by
+ CLOSURE. COM is ignored, except for error messages.
+
+ #### IMHO it's a mistake to do this kind of work so early in the
+ process (before any download even started!) opt.bind_address
+ should simply remember the provided value as a string. Another
+ function should do the lookup, when needed, and cache the
+ result. --hniksic */
static int
cmd_address (const char *com, const char *val, void *closure)
{
- struct sockaddr_in *sin;
-
- sin = (struct sockaddr_in *) malloc(sizeof *sin);
- if (sin == NULL)
- {
- fprintf (stderr, _("%s: Out of memory.\n"), exec_name);
- return 0;
- }
-
- if (!store_hostaddress ((unsigned char *)&sin->sin_addr, val))
- {
- fprintf (stderr, _("%s: %s: Cannot convert `%s' to an IP address.\n"),
- exec_name, com, val);
- return 0;
- }
-
- sin->sin_family = AF_INET;
- sin->sin_port = 0;
-
- * (struct sockaddr_in **) closure = sin;
-
- return 1;
+ struct sockaddr_in sin;
+ struct sockaddr_in **target = (struct sockaddr_in **)closure;
+
+ if (!store_hostaddress ((unsigned char *)&sin.sin_addr, val))
+ {
+ fprintf (stderr, _("%s: %s: Cannot convert `%s' to an IP address.\n"),
+ exec_name, com, val);
+ return 0;
+ }
+
+ sin.sin_family = AF_INET;
+ sin.sin_port = 0;
+
+ FREE_MAYBE (*target);
+
+ *target = xmalloc (sizeof (sin));
+ memcpy (*target, &sin, sizeof (sin));
+
+ return 1;
}
/* Store the boolean value from VAL to CLOSURE. COM is ignored,
return 1;
}
+/* Like the above, but handles tilde-expansion when reading a user's
+ `.wgetrc'. In that case, and if VAL begins with `~', the tilde
+ gets expanded to the user's home directory. */
+static int
+cmd_file (const char *com, const char *val, void *closure)
+{
+ char **pstring = (char **)closure;
+
+ FREE_MAYBE (*pstring);
+ if (!enable_tilde_expansion || !(*val == '~' && *(val + 1) == '/'))
+ {
+ noexpand:
+ *pstring = xstrdup (val);
+ }
+ else
+ {
+ char *result;
+ int homelen;
+ char *home = home_dir ();
+ if (!home)
+ goto noexpand;
+
+ homelen = strlen (home);
+ while (homelen && home[homelen - 1] == '/')
+ home[--homelen] = '\0';
+
+ /* Skip the leading "~/". */
+ for (++val; *val == '/'; val++)
+ ;
+
+ result = xmalloc (homelen + 1 + strlen (val));
+ memcpy (result, home, homelen);
+ result[homelen] = '/';
+ strcpy (result + homelen + 1, val);
+
+ *pstring = result;
+ }
+ return 1;
+}
+
/* Merge the vector (array of strings separated with `,') in COM with
the vector (NULL-terminated array of strings) pointed to by
CLOSURE. */
return 1;
}
\f
+void cleanup_html_url PARAMS ((void));
+void downloaded_files_free PARAMS ((void));
+
+
/* Free the memory allocated by global variables. */
void
cleanup (void)
free_netrc (netrc_list);
if (opt.dfp)
fclose (opt.dfp);
+ cleanup_html_url ();
+ downloaded_files_free ();
+ cookies_cleanup ();
FREE_MAYBE (opt.lfilename);
- free (opt.dir_prefix);
+ xfree (opt.dir_prefix);
FREE_MAYBE (opt.input_filename);
FREE_MAYBE (opt.output_document);
free_vec (opt.accepts);
free_vec (opt.domains);
free_vec (opt.follow_tags);
free_vec (opt.ignore_tags);
- free (opt.ftp_acc);
- free (opt.ftp_pass);
+ xfree (opt.ftp_acc);
+ FREE_MAYBE (opt.ftp_pass);
FREE_MAYBE (opt.ftp_proxy);
+ FREE_MAYBE (opt.https_proxy);
FREE_MAYBE (opt.http_proxy);
free_vec (opt.no_proxy);
FREE_MAYBE (opt.useragent);
FREE_MAYBE (opt.http_user);
FREE_MAYBE (opt.http_passwd);
FREE_MAYBE (opt.user_header);
+#ifdef HAVE_SSL
+ FREE_MAYBE (opt.sslcertkey);
+ FREE_MAYBE (opt.sslcertfile);
+#endif /* HAVE_SSL */
+ FREE_MAYBE (opt.bind_address);
+ FREE_MAYBE (opt.cookies_input);
+ FREE_MAYBE (opt.cookies_output);
}