1 /* Reading/parsing the initialization file.
2 Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Wget.
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Wget; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 In addition, as a special exception, the Free Software Foundation
22 gives permission to link the code of its release of Wget with the
23 OpenSSL project's "OpenSSL" library (or with modified versions of it
24 that use the same license as the "OpenSSL" library), and distribute
25 the linked executables. You must obey the GNU General Public License
26 in all respects for all of the code used other than "OpenSSL". If you
27 modify this file, you may extend this exception to your version of the
28 file, but you are not obligated to do so. If you do not wish to do
29 so, delete this exception statement from your version. */
34 #include <sys/types.h>
49 # include <sys/socket.h>
50 # include <netinet/in.h>
52 # include <arpa/inet.h>
67 #include "cookies.h" /* for cookie_jar_delete */
74 extern struct cookie_jar *wget_cookie_jar;
76 /* We want tilde expansion enabled only when reading `.wgetrc' lines;
77 otherwise, it will be performed by the shell. This variable will
78 be set by the wgetrc-reading function. */
80 static int enable_tilde_expansion;
83 #define CMD_DECLARE(func) static int func \
84 PARAMS ((const char *, const char *, void *))
86 CMD_DECLARE (cmd_boolean);
87 CMD_DECLARE (cmd_bytes);
88 CMD_DECLARE (cmd_directory_vector);
89 CMD_DECLARE (cmd_lockable_boolean);
90 CMD_DECLARE (cmd_number);
91 CMD_DECLARE (cmd_number_inf);
92 CMD_DECLARE (cmd_string);
93 CMD_DECLARE (cmd_file);
94 CMD_DECLARE (cmd_directory);
95 CMD_DECLARE (cmd_time);
96 CMD_DECLARE (cmd_vector);
98 CMD_DECLARE (cmd_spec_dirstruct);
99 CMD_DECLARE (cmd_spec_header);
100 CMD_DECLARE (cmd_spec_htmlify);
101 CMD_DECLARE (cmd_spec_mirror);
102 CMD_DECLARE (cmd_spec_progress);
103 CMD_DECLARE (cmd_spec_recursive);
104 CMD_DECLARE (cmd_spec_restrict_file_names);
105 CMD_DECLARE (cmd_spec_useragent);
107 /* List of recognized commands, each consisting of name, closure and function.
108 When adding a new command, simply add it to the list, but be sure to keep the
109 list sorted alphabetically, as findcmd() depends on it. Also, be sure to add
110 any entries that allocate memory (e.g. cmd_string and cmd_vector guys) to the
111 cleanup() function below. */
115 int (*action) PARAMS ((const char *, const char *, void *));
117 { "accept", &opt.accepts, cmd_vector },
118 { "addhostdir", &opt.add_hostdir, cmd_boolean },
119 { "alwaysrest", &opt.always_rest, cmd_boolean }, /* deprecated */
120 { "background", &opt.background, cmd_boolean },
121 { "backupconverted", &opt.backup_converted, cmd_boolean },
122 { "backups", &opt.backups, cmd_number },
123 { "base", &opt.base_href, cmd_string },
124 { "bindaddress", &opt.bind_address, cmd_string },
125 { "cache", &opt.allow_cache, cmd_boolean },
126 { "continue", &opt.always_rest, cmd_boolean },
127 { "convertlinks", &opt.convert_links, cmd_boolean },
128 { "cookies", &opt.cookies, cmd_boolean },
129 { "cutdirs", &opt.cut_dirs, cmd_number },
131 { "debug", &opt.debug, cmd_boolean },
133 { "deleteafter", &opt.delete_after, cmd_boolean },
134 { "dirprefix", &opt.dir_prefix, cmd_directory },
135 { "dirstruct", NULL, cmd_spec_dirstruct },
136 { "dnscache", &opt.dns_cache, cmd_boolean },
137 { "domains", &opt.domains, cmd_vector },
138 { "dotbytes", &opt.dot_bytes, cmd_bytes },
139 { "dotsinline", &opt.dots_in_line, cmd_number },
140 { "dotspacing", &opt.dot_spacing, cmd_number },
141 { "dotstyle", &opt.dot_style, cmd_string },
143 { "egdfile", &opt.sslegdsock, cmd_file },
145 { "excludedirectories", &opt.excludes, cmd_directory_vector },
146 { "excludedomains", &opt.exclude_domains, cmd_vector },
147 { "followftp", &opt.follow_ftp, cmd_boolean },
148 { "followtags", &opt.follow_tags, cmd_vector },
149 { "forcehtml", &opt.force_html, cmd_boolean },
150 { "ftpproxy", &opt.ftp_proxy, cmd_string },
151 { "glob", &opt.ftp_glob, cmd_boolean },
152 { "header", NULL, cmd_spec_header },
153 { "htmlextension", &opt.html_extension, cmd_boolean },
154 { "htmlify", NULL, cmd_spec_htmlify },
155 { "httpkeepalive", &opt.http_keep_alive, cmd_boolean },
156 { "httppasswd", &opt.http_passwd, cmd_string },
157 { "httpproxy", &opt.http_proxy, cmd_string },
158 { "httpsproxy", &opt.https_proxy, cmd_string },
159 { "httpuser", &opt.http_user, cmd_string },
160 { "ignorelength", &opt.ignore_length, cmd_boolean },
161 { "ignoretags", &opt.ignore_tags, cmd_vector },
162 { "includedirectories", &opt.includes, cmd_directory_vector },
163 { "input", &opt.input_filename, cmd_file },
164 { "killlonger", &opt.kill_longer, cmd_boolean },
165 { "limitrate", &opt.limit_rate, cmd_bytes },
166 { "loadcookies", &opt.cookies_input, cmd_file },
167 { "logfile", &opt.lfilename, cmd_file },
168 { "login", &opt.ftp_acc, cmd_string },
169 { "mirror", NULL, cmd_spec_mirror },
170 { "netrc", &opt.netrc, cmd_boolean },
171 { "noclobber", &opt.noclobber, cmd_boolean },
172 { "noparent", &opt.no_parent, cmd_boolean },
173 { "noproxy", &opt.no_proxy, cmd_vector },
174 { "numtries", &opt.ntry, cmd_number_inf },/* deprecated*/
175 { "outputdocument", &opt.output_document, cmd_file },
176 { "pagerequisites", &opt.page_requisites, cmd_boolean },
177 { "passiveftp", &opt.ftp_pasv, cmd_lockable_boolean },
178 { "passwd", &opt.ftp_pass, cmd_string },
179 { "postdata", &opt.post_data, cmd_string },
180 { "postfile", &opt.post_file_name, cmd_file },
181 { "progress", &opt.progress_type, cmd_spec_progress },
182 { "proxypasswd", &opt.proxy_passwd, cmd_string },
183 { "proxyuser", &opt.proxy_user, cmd_string },
184 { "quiet", &opt.quiet, cmd_boolean },
185 { "quota", &opt.quota, cmd_bytes },
186 { "randomwait", &opt.random_wait, cmd_boolean },
187 { "reclevel", &opt.reclevel, cmd_number_inf },
188 { "recursive", NULL, cmd_spec_recursive },
189 { "referer", &opt.referer, cmd_string },
190 { "reject", &opt.rejects, cmd_vector },
191 { "relativeonly", &opt.relative_only, cmd_boolean },
192 { "removelisting", &opt.remove_listing, cmd_boolean },
193 { "restrictfilenames", NULL, cmd_spec_restrict_file_names },
194 { "retrsymlinks", &opt.retr_symlinks, cmd_boolean },
195 { "retryconnrefused", &opt.retry_connrefused, cmd_boolean },
196 { "robots", &opt.use_robots, cmd_boolean },
197 { "savecookies", &opt.cookies_output, cmd_file },
198 { "saveheaders", &opt.save_headers, cmd_boolean },
199 { "serverresponse", &opt.server_response, cmd_boolean },
200 { "spanhosts", &opt.spanhost, cmd_boolean },
201 { "spider", &opt.spider, cmd_boolean },
203 { "sslcadir", &opt.sslcadir, cmd_directory },
204 { "sslcafile", &opt.sslcafile, cmd_file },
205 { "sslcertfile", &opt.sslcertfile, cmd_file },
206 { "sslcertkey", &opt.sslcertkey, cmd_file },
207 { "sslcerttype", &opt.sslcerttype, cmd_number },
208 { "sslcheckcert", &opt.sslcheckcert, cmd_number },
209 { "sslprotocol", &opt.sslprotocol, cmd_number },
210 #endif /* HAVE_SSL */
211 { "strictcomments", &opt.strict_comments, cmd_boolean },
212 { "timeout", &opt.timeout, cmd_time },
213 { "timestamping", &opt.timestamping, cmd_boolean },
214 { "tries", &opt.ntry, cmd_number_inf },
215 { "useproxy", &opt.use_proxy, cmd_boolean },
216 { "useragent", NULL, cmd_spec_useragent },
217 { "verbose", &opt.verbose, cmd_boolean },
218 { "wait", &opt.wait, cmd_time },
219 { "waitretry", &opt.waitretry, cmd_time }
222 /* Look up COM in the commands[] array and return its index. If COM
223 is not found, -1 is returned. This function uses binary search. */
226 findcmd (const char *com)
228 int lo = 0, hi = countof (commands) - 1;
232 int mid = (lo + hi) >> 1;
233 int cmp = strcasecmp (com, commands[mid].name);
244 /* Reset the variables to default values. */
250 /* Most of the default values are 0. Just reset everything, and
251 fill in the non-zero values. Note that initializing pointers to
252 NULL this way is technically illegal, but porting Wget to a
253 machine where NULL is not all-zero bit pattern will be the least
254 of the implementors' worries. */
255 memset (&opt, 0, sizeof (opt));
263 opt.ftp_acc = xstrdup ("anonymous");
264 opt.ftp_pass = xstrdup ("-wget@");
268 opt.http_keep_alive = 1;
270 tmp = getenv ("no_proxy");
272 opt.no_proxy = sepstring (tmp);
280 opt.remove_listing = 1;
282 opt.dot_bytes = 1024;
283 opt.dot_spacing = 10;
284 opt.dots_in_line = 50;
288 /* The default for file name restriction defaults to the OS type. */
289 #if !defined(WINDOWS) && !defined(__CYGWIN__)
290 opt.restrict_files_os = restrict_unix;
292 opt.restrict_files_os = restrict_windows;
294 opt.restrict_files_ctrl = 1;
297 /* Return the user's home directory (strdup-ed), or NULL if none is
302 char *home = getenv ("HOME");
307 /* If HOME is not defined, try getting it from the password
309 struct passwd *pwd = getpwuid (getuid ());
310 if (!pwd || !pwd->pw_dir)
315 /* #### Maybe I should grab home_dir from registry, but the best
316 that I could get from there is user's Start menu. It sucks! */
320 return home ? xstrdup (home) : NULL;
323 /* Return the path to the user's .wgetrc. This is either the value of
324 `WGETRC' environment variable, or `$HOME/.wgetrc'.
326 If the `WGETRC' variable exists but the file does not exist, the
327 function will exit(). */
329 wgetrc_file_name (void)
334 /* Try the environment. */
335 env = getenv ("WGETRC");
338 if (!file_exists_p (env))
340 fprintf (stderr, "%s: %s: %s.\n", exec_name, env, strerror (errno));
343 return xstrdup (env);
347 /* If that failed, try $HOME/.wgetrc. */
351 file = (char *)xmalloc (strlen (home) + 1 + strlen (".wgetrc") + 1);
352 sprintf (file, "%s/.wgetrc", home);
356 /* Under Windows, "home" is (for the purposes of this function) the
357 directory where `wget.exe' resides, and `wget.ini' will be used
358 as file name. SYSTEM_WGETRC should not be defined under WINDOWS.
360 It is not as trivial as I assumed, because on 95 argv[0] is full
361 path, but on NT you get what you typed in command line. --dbudor */
365 file = (char *)xmalloc (strlen (home) + strlen ("wget.ini") + 1);
366 sprintf (file, "%swget.ini", home);
372 if (!file_exists_p (file))
380 static int parse_line PARAMS ((const char *, char **, char **, int *));
381 static int setval_internal PARAMS ((int, const char *, const char *));
383 /* Initialize variables from a wgetrc file. */
386 run_wgetrc (const char *file)
392 fp = fopen (file, "rb");
395 fprintf (stderr, _("%s: Cannot read %s (%s).\n"), exec_name,
396 file, strerror (errno));
399 enable_tilde_expansion = 1;
401 while ((line = read_whole_line (fp)))
406 /* Parse the line. */
407 status = parse_line (line, &com, &val, &comind);
409 /* If everything is OK, set the value. */
412 if (!setval_internal (comind, com, val))
413 fprintf (stderr, _("%s: Error in %s at line %d.\n"), exec_name,
418 else if (status == 0)
419 fprintf (stderr, _("%s: Error in %s at line %d.\n"), exec_name,
423 enable_tilde_expansion = 0;
427 /* Initialize the defaults and run the system wgetrc and user's own
434 /* Load the hard-coded defaults. */
437 /* If SYSTEM_WGETRC is defined, use it. */
439 if (file_exists_p (SYSTEM_WGETRC))
440 run_wgetrc (SYSTEM_WGETRC);
442 /* Override it with your own, if one exists. */
443 file = wgetrc_file_name ();
446 /* #### We should canonicalize `file' and SYSTEM_WGETRC with
447 something like realpath() before comparing them with `strcmp' */
449 if (!strcmp (file, SYSTEM_WGETRC))
451 fprintf (stderr, _("\
452 %s: Warning: Both system and user wgetrc point to `%s'.\n"),
462 /* Remove dashes and underscores from S, modifying S in the
468 char *t = s; /* t - tortoise */
469 char *h = s; /* h - hare */
471 if (*h == '_' || *h == '-')
478 /* Parse the line pointed by line, with the syntax:
479 <sp>* command <sp>* = <sp>* value <newline>
480 Uses malloc to allocate space for command and value.
481 If the line is invalid, data is freed and 0 is returned.
489 parse_line (const char *line, char **com, char **val, int *comind)
492 const char *end = line + strlen (line);
493 const char *cmdstart, *cmdend;
494 const char *valstart, *valend;
499 /* Skip leading and trailing whitespace. */
500 while (*line && ISSPACE (*line))
502 while (end > line && ISSPACE (end[-1]))
505 /* Skip empty lines and comments. */
506 if (!*line || *line == '#')
512 while (p < end && (ISALPHA (*p) || *p == '_' || *p == '-'))
516 /* Skip '=', as well as any space before or after it. */
517 while (p < end && ISSPACE (*p))
519 if (p == end || *p != '=')
522 while (p < end && ISSPACE (*p))
528 /* The line now known to be syntactically correct. Check whether
529 the command is valid. */
530 BOUNDED_TO_ALLOCA (cmdstart, cmdend, cmdcopy);
532 ind = findcmd (cmdcopy);
536 /* The command is valid. Now fill in the values and report success
539 *com = strdupdelim (cmdstart, cmdend);
540 *val = strdupdelim (valstart, valend);
544 /* Run commands[comind].action. */
547 setval_internal (int comind, const char *com, const char *val)
549 assert (0 <= comind && comind < countof (commands));
550 return ((*commands[comind].action) (com, val, commands[comind].closure));
553 /* Run command COM with value VAL. If running the command produces an
554 error, report the error and exit.
556 This is intended to be called from main() with commands not
557 provided by the user, therefore it aborts when an unknown command
558 is encountered. Once the COMIND's are exported to init.h, this
559 function will be changed to accept COMIND directly. */
562 setoptval (const char *com, const char *val)
564 int comind = findcmd (com);
565 assert (comind != -1);
566 if (!setval_internal (comind, com, val))
571 run_command (const char *opt)
575 int status = parse_line (opt, &com, &val, &comind);
578 if (!setval_internal (comind, com, val))
583 else if (status == 0)
585 fprintf (stderr, "Invalid command `%s'\n", opt);
590 /* Generic helper functions, for use with `commands'. */
592 static int myatoi PARAMS ((const char *s));
594 /* Store the boolean value from VAL to CLOSURE. COM is ignored,
595 except for error messages. */
597 cmd_boolean (const char *com, const char *val, void *closure)
601 #define LC(x) TOLOWER(x)
603 if ((LC(v[0]) == 'o' && LC(v[1]) == 'n' && !v[2])
605 (LC(v[0]) == 'y' && LC(v[1]) == 'e' && LC(v[2]) == 's' && !v[3])
607 (v[0] == '1' && !v[1]))
608 /* "on", "yes" and "1" mean true. */
610 else if ((LC(v[0]) == 'o' && LC(v[1]) == 'f' && LC(v[2]) == 'f' && !v[3])
612 (LC(v[0]) == 'n' && LC(v[1]) == 'o' && !v[2])
614 (v[0] == '0' && !v[1]))
615 /* "off", "no" and "0" mean false. */
619 fprintf (stderr, _("%s: %s: Please specify on or off.\n"),
624 *(int *)closure = bool_value;
628 /* Store the lockable_boolean {2, 1, 0, -1} value from VAL to CLOSURE. COM is
629 ignored, except for error messages. Values 2 and -1 indicate that once
630 defined, the value may not be changed by successive wgetrc files or
631 command-line arguments.
633 Values: 2 - Enable a particular option for good ("always")
634 1 - Enable an option ("on")
635 0 - Disable an option ("off")
636 -1 - Disable an option for good ("never") */
638 cmd_lockable_boolean (const char *com, const char *val, void *closure)
640 int lockable_boolean_value;
643 * If a config file said "always" or "never", don't allow command line
644 * arguments to override the config file.
646 if (*(int *)closure == -1 || *(int *)closure == 2)
649 if (!strcasecmp (val, "always") || !strcmp (val, "2"))
650 lockable_boolean_value = 2;
651 else if (!strcasecmp (val, "on")
652 || !strcasecmp (val, "yes")
653 || !strcmp (val, "1"))
654 lockable_boolean_value = 1;
655 else if (!strcasecmp (val, "off")
656 || !strcasecmp (val, "no")
657 || !strcmp (val, "0"))
658 lockable_boolean_value = 0;
659 else if (!strcasecmp (val, "never") || !strcmp (val, "-1"))
660 lockable_boolean_value = -1;
663 fprintf (stderr, _("%s: %s: Please specify always, on, off, "
669 *(int *)closure = lockable_boolean_value;
673 /* Set the non-negative integer value from VAL to CLOSURE. With
674 incorrect specification, the number remains unchanged. */
676 cmd_number (const char *com, const char *val, void *closure)
678 int num = myatoi (val);
682 fprintf (stderr, _("%s: %s: Invalid specification `%s'.\n"),
683 exec_name, com, val);
686 *(int *)closure = num;
690 /* Similar to cmd_number(), only accepts `inf' as a synonym for 0. */
692 cmd_number_inf (const char *com, const char *val, void *closure)
694 if (!strcasecmp (val, "inf"))
699 return cmd_number (com, val, closure);
702 /* Copy (strdup) the string at COM to a new location and place a
703 pointer to *CLOSURE. */
705 cmd_string (const char *com, const char *val, void *closure)
707 char **pstring = (char **)closure;
709 FREE_MAYBE (*pstring);
710 *pstring = xstrdup (val);
715 # define ISSEP(c) ((c) == '/')
717 # define ISSEP(c) ((c) == '/' || (c) == '\\')
720 /* Like the above, but handles tilde-expansion when reading a user's
721 `.wgetrc'. In that case, and if VAL begins with `~', the tilde
722 gets expanded to the user's home directory. */
724 cmd_file (const char *com, const char *val, void *closure)
726 char **pstring = (char **)closure;
728 FREE_MAYBE (*pstring);
730 /* #### If VAL is empty, perhaps should set *CLOSURE to NULL. */
732 if (!enable_tilde_expansion || !(*val == '~' && ISSEP (val[1])))
735 *pstring = xstrdup (val);
741 char *home = home_dir ();
745 homelen = strlen (home);
746 while (homelen && ISSEP (home[homelen - 1]))
747 home[--homelen] = '\0';
749 /* Skip the leading "~/". */
750 for (++val; ISSEP (*val); val++)
753 result = xmalloc (homelen + 1 + strlen (val) + 1);
754 memcpy (result, home, homelen);
755 result[homelen] = '/';
756 strcpy (result + homelen + 1, val);
762 /* Convert "\" to "/". */
765 for (s = *pstring; *s; s++)
773 /* Like cmd_file, but strips trailing '/' characters. */
775 cmd_directory (const char *com, const char *val, void *closure)
779 /* Call cmd_file() for tilde expansion and separator
780 canonicalization (backslash -> slash under Windows). These
781 things should perhaps be in a separate function. */
782 if (!cmd_file (com, val, closure))
785 s = *(char **)closure;
787 while (t > s && *--t == '/')
793 /* Merge the vector (array of strings separated with `,') in COM with
794 the vector (NULL-terminated array of strings) pointed to by
797 cmd_vector (const char *com, const char *val, void *closure)
799 char ***pvec = (char ***)closure;
802 *pvec = merge_vecs (*pvec, sepstring (val));
812 cmd_directory_vector (const char *com, const char *val, void *closure)
814 char ***pvec = (char ***)closure;
818 /* Strip the trailing slashes from directories. */
821 seps = sepstring (val);
822 for (t = seps; t && *t; t++)
824 int len = strlen (*t);
825 /* Skip degenerate case of root directory. */
828 if ((*t)[len - 1] == '/')
829 (*t)[len - 1] = '\0';
832 *pvec = merge_vecs (*pvec, seps);
842 /* Poor man's atof: handles only <digits>.<digits>. Returns 1 on
843 success, 0 on failure. In case of success, stores its result to
847 simple_atof (const char *beg, const char *end, double *dest)
863 result = (10 * result) + (ch - '0');
865 result += (ch - '0') / (divider *= 10);
883 /* Parse VAL as a number and set its value to CLOSURE (which should
884 point to a long int).
886 By default, the value is assumed to be in bytes. If "K", "M", or
887 "G" are appended, the value is multiplied with 1<<10, 1<<20, or
888 1<<30, respectively. Floating point values are allowed and are
889 cast to integer before use. The idea is to be able to use things
890 like 1.5k instead of "1536".
892 The string "inf" is returned as 0.
894 In case of error, 0 is returned and memory pointed to by CLOSURE
895 remains unmodified. */
898 cmd_bytes (const char *com, const char *val, void *closure)
902 const char *end = val + strlen (val);
904 /* Check for "inf". */
905 if (0 == strcmp (val, "inf"))
907 *(long *)closure = 0;
911 /* Strip trailing whitespace. */
912 while (val < end && ISSPACE (end[-1]))
918 fprintf (stderr, _("%s: Invalid byte value `%s'\n"), com, val);
922 switch (TOLOWER (end[-1]))
925 --end, mult = 1L<<10;
928 --end, mult = 1L<<20;
931 --end, mult = 1L<<30;
934 /* Not a recognized suffix: assume it belongs to the number.
935 (If not, atof simple_atof will raise an error.) */
939 /* Skip leading and trailing whitespace. */
940 while (val < end && ISSPACE (*val))
942 while (val < end && ISSPACE (end[-1]))
947 if (!simple_atof (val, end, &number))
950 *(long *)closure = (long)(number * mult);
954 /* Store the value of VAL to *OUT. The value is a time period, by
955 default expressed in seconds, but also accepting suffixes "m", "h",
956 "d", and "w" for minutes, hours, days, and weeks respectively. */
959 cmd_time (const char *com, const char *val, void *closure)
962 const char *end = val + strlen (val);
964 /* Strip trailing whitespace. */
965 while (val < end && ISSPACE (end[-1]))
971 fprintf (stderr, _("%s: Invalid time specification `%s'\n"), com, val);
975 switch (TOLOWER (end[-1]))
978 --end, mult = 1; /* seconds */
981 --end, mult = 60; /* minutes */
984 --end, mult = 3600; /* hours */
987 --end, mult = 86400.0; /* days */
990 --end, mult = 604800.0; /* weeks */
993 /* Not a recognized suffix: assume it belongs to the number.
994 (If not, atof simple_atof will raise an error.) */
998 /* Skip leading and trailing whitespace. */
999 while (val < end && ISSPACE (*val))
1001 while (val < end && ISSPACE (end[-1]))
1006 if (!simple_atof (val, end, &number))
1009 *(double *)closure = number * mult;
1013 /* Specialized helper functions, used by `commands' to handle some
1014 options specially. */
1016 static int check_user_specified_header PARAMS ((const char *));
1019 cmd_spec_dirstruct (const char *com, const char *val, void *closure)
1021 if (!cmd_boolean (com, val, &opt.dirstruct))
1023 /* Since dirstruct behaviour is explicitly changed, no_dirstruct
1024 must be affected inversely. */
1026 opt.no_dirstruct = 0;
1028 opt.no_dirstruct = 1;
1033 cmd_spec_header (const char *com, const char *val, void *closure)
1037 /* Empty header means reset headers. */
1038 FREE_MAYBE (opt.user_header);
1039 opt.user_header = NULL;
1045 if (!check_user_specified_header (val))
1047 fprintf (stderr, _("%s: %s: Invalid specification `%s'.\n"),
1048 exec_name, com, val);
1051 i = opt.user_header ? strlen (opt.user_header) : 0;
1052 opt.user_header = (char *)xrealloc (opt.user_header, i + strlen (val)
1054 strcpy (opt.user_header + i, val);
1056 opt.user_header[i++] = '\r';
1057 opt.user_header[i++] = '\n';
1058 opt.user_header[i] = '\0';
1064 cmd_spec_htmlify (const char *com, const char *val, void *closure)
1066 int flag = cmd_boolean (com, val, &opt.htmlify);
1067 if (flag && !opt.htmlify)
1068 opt.remove_listing = 0;
1073 cmd_spec_mirror (const char *com, const char *val, void *closure)
1077 if (!cmd_boolean (com, val, &mirror))
1082 if (!opt.no_dirstruct)
1084 opt.timestamping = 1;
1085 opt.reclevel = INFINITE_RECURSION;
1086 opt.remove_listing = 0;
1092 cmd_spec_progress (const char *com, const char *val, void *closure)
1094 if (!valid_progress_implementation_p (val))
1096 fprintf (stderr, _("%s: %s: Invalid progress type `%s'.\n"),
1097 exec_name, com, val);
1100 FREE_MAYBE (opt.progress_type);
1102 /* Don't call set_progress_implementation here. It will be called
1103 in main() when it becomes clear what the log output is. */
1104 opt.progress_type = xstrdup (val);
1109 cmd_spec_recursive (const char *com, const char *val, void *closure)
1111 if (!cmd_boolean (com, val, &opt.recursive))
1115 if (opt.recursive && !opt.no_dirstruct)
1122 cmd_spec_restrict_file_names (const char *com, const char *val, void *closure)
1124 int restrict_os = opt.restrict_files_os;
1125 int restrict_ctrl = opt.restrict_files_ctrl;
1127 const char *end = strchr (val, ',');
1129 end = val + strlen (val);
1131 #define VAL_IS(string_literal) BOUNDED_EQUAL (val, end, string_literal)
1133 if (VAL_IS ("unix"))
1134 restrict_os = restrict_unix;
1135 else if (VAL_IS ("windows"))
1136 restrict_os = restrict_windows;
1137 else if (VAL_IS ("nocontrol"))
1142 fprintf (stderr, _("%s: %s: Invalid specification `%s'.\n"),
1143 exec_name, com, val);
1151 if (!strcmp (end + 1, "nocontrol"))
1157 opt.restrict_files_os = restrict_os;
1158 opt.restrict_files_ctrl = restrict_ctrl;
1163 cmd_spec_useragent (const char *com, const char *val, void *closure)
1165 /* Just check for empty string and newline, so we don't throw total
1166 junk to the server. */
1167 if (!*val || strchr (val, '\n'))
1169 fprintf (stderr, _("%s: %s: Invalid specification `%s'.\n"),
1170 exec_name, com, val);
1173 opt.useragent = xstrdup (val);
1177 /* Miscellaneous useful routines. */
1179 /* Return the integer value of a positive integer written in S, or -1
1180 if an error was encountered. */
1182 myatoi (const char *s)
1185 const char *orig = s;
1187 for (res = 0; *s && ISDIGIT (*s); s++)
1188 res = 10 * res + (*s - '0');
1189 if (*s || orig == s)
1196 check_user_specified_header (const char *s)
1200 for (p = s; *p && *p != ':' && !ISSPACE (*p); p++);
1201 /* The header MUST contain `:' preceded by at least one
1202 non-whitespace character. */
1203 if (*p != ':' || p == s)
1205 /* The header MUST NOT contain newlines. */
1206 if (strchr (s, '\n'))
1211 void cleanup_html_url PARAMS ((void));
1212 void res_cleanup PARAMS ((void));
1213 void downloaded_files_free PARAMS ((void));
1214 void http_cleanup PARAMS ((void));
1217 /* Free the memory allocated by global variables. */
1221 /* Free external resources, close files, etc. */
1226 /* We're exiting anyway so there's no real need to call free()
1227 hundreds of times. Skipping the frees will make Wget exit
1230 However, when detecting leaks, it's crucial to free() everything
1231 because then you can find the real leaks, i.e. the allocated
1232 memory which grows with the size of the program. */
1235 recursive_cleanup ();
1238 cleanup_html_url ();
1239 downloaded_files_free ();
1241 cookie_jar_delete (wget_cookie_jar);
1244 extern acc_t *netrc_list;
1245 free_netrc (netrc_list);
1247 FREE_MAYBE (opt.lfilename);
1248 FREE_MAYBE (opt.dir_prefix);
1249 FREE_MAYBE (opt.input_filename);
1250 FREE_MAYBE (opt.output_document);
1251 free_vec (opt.accepts);
1252 free_vec (opt.rejects);
1253 free_vec (opt.excludes);
1254 free_vec (opt.includes);
1255 free_vec (opt.domains);
1256 free_vec (opt.follow_tags);
1257 free_vec (opt.ignore_tags);
1258 FREE_MAYBE (opt.progress_type);
1259 xfree (opt.ftp_acc);
1260 FREE_MAYBE (opt.ftp_pass);
1261 FREE_MAYBE (opt.ftp_proxy);
1262 FREE_MAYBE (opt.https_proxy);
1263 FREE_MAYBE (opt.http_proxy);
1264 free_vec (opt.no_proxy);
1265 FREE_MAYBE (opt.useragent);
1266 FREE_MAYBE (opt.referer);
1267 FREE_MAYBE (opt.http_user);
1268 FREE_MAYBE (opt.http_passwd);
1269 FREE_MAYBE (opt.user_header);
1271 FREE_MAYBE (opt.sslcertkey);
1272 FREE_MAYBE (opt.sslcertfile);
1273 #endif /* HAVE_SSL */
1274 FREE_MAYBE (opt.bind_address);
1275 FREE_MAYBE (opt.cookies_input);
1276 FREE_MAYBE (opt.cookies_output);