1 /* Reading/parsing the initialization file.
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of Wget.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
24 #include <sys/types.h>
52 #define CMD_DECLARE(func) static int func \
53 PARAMS ((const char *, const char *, void *))
55 CMD_DECLARE (cmd_boolean);
56 CMD_DECLARE (cmd_boolean);
57 CMD_DECLARE (cmd_number);
58 CMD_DECLARE (cmd_number_inf);
59 CMD_DECLARE (cmd_string);
60 CMD_DECLARE (cmd_vector);
61 CMD_DECLARE (cmd_directory_vector);
62 CMD_DECLARE (cmd_bytes);
63 CMD_DECLARE (cmd_time);
65 CMD_DECLARE (cmd_spec_dirstruct);
66 CMD_DECLARE (cmd_spec_dotstyle);
67 CMD_DECLARE (cmd_spec_header);
68 CMD_DECLARE (cmd_spec_htmlify);
69 CMD_DECLARE (cmd_spec_mirror);
70 CMD_DECLARE (cmd_spec_outputdocument);
71 CMD_DECLARE (cmd_spec_recursive);
72 CMD_DECLARE (cmd_spec_useragent);
74 /* List of recognized commands, each consisting of name, closure and
75 function. When adding a new command, simply add it to the list,
76 but be sure to keep the list sorted alphabetically, as comind()
81 int (*action) PARAMS ((const char *, const char *, void *));
83 { "accept", &opt.accepts, cmd_vector },
84 { "addhostdir", &opt.add_hostdir, cmd_boolean },
85 { "alwaysrest", &opt.always_rest, cmd_boolean }, /* deprecated */
86 { "background", &opt.background, cmd_boolean },
87 { "backupconverted", &opt.backup_converted, cmd_boolean },
88 { "backups", &opt.backups, cmd_number },
89 { "base", &opt.base_href, cmd_string },
90 { "cache", &opt.proxy_cache, cmd_boolean },
91 { "continue", &opt.always_rest, cmd_boolean },
92 { "convertlinks", &opt.convert_links, cmd_boolean },
93 { "cutdirs", &opt.cut_dirs, cmd_number },
95 { "debug", &opt.debug, cmd_boolean },
97 { "deleteafter", &opt.delete_after, cmd_boolean },
98 { "dirprefix", &opt.dir_prefix, cmd_string },
99 { "dirstruct", NULL, cmd_spec_dirstruct },
100 { "domains", &opt.domains, cmd_vector },
101 { "dotbytes", &opt.dot_bytes, cmd_bytes },
102 { "dotsinline", &opt.dots_in_line, cmd_number },
103 { "dotspacing", &opt.dot_spacing, cmd_number },
104 { "dotstyle", NULL, cmd_spec_dotstyle },
105 { "excludedirectories", &opt.excludes, cmd_directory_vector },
106 { "excludedomains", &opt.exclude_domains, cmd_vector },
107 { "followftp", &opt.follow_ftp, cmd_boolean },
108 { "forcehtml", &opt.force_html, cmd_boolean },
109 { "ftpproxy", &opt.ftp_proxy, cmd_string },
110 { "glob", &opt.ftp_glob, cmd_boolean },
111 { "header", NULL, cmd_spec_header },
112 { "htmlify", NULL, cmd_spec_htmlify },
113 { "httppasswd", &opt.http_passwd, cmd_string },
114 { "httpproxy", &opt.http_proxy, cmd_string },
115 { "httpuser", &opt.http_user, cmd_string },
116 { "ignorelength", &opt.ignore_length, cmd_boolean },
117 { "includedirectories", &opt.includes, cmd_directory_vector },
118 { "input", &opt.input_filename, cmd_string },
119 { "killlonger", &opt.kill_longer, cmd_boolean },
120 { "logfile", &opt.lfilename, cmd_string },
121 { "login", &opt.ftp_acc, cmd_string },
122 { "mirror", NULL, cmd_spec_mirror },
123 { "netrc", &opt.netrc, cmd_boolean },
124 { "noclobber", &opt.noclobber, cmd_boolean },
125 { "noparent", &opt.no_parent, cmd_boolean },
126 { "noproxy", &opt.no_proxy, cmd_vector },
127 { "numtries", &opt.ntry, cmd_number_inf }, /* deprecated */
128 { "outputdocument", NULL, cmd_spec_outputdocument },
129 { "passiveftp", &opt.ftp_pasv, cmd_boolean },
130 { "passwd", &opt.ftp_pass, cmd_string },
131 { "proxypasswd", &opt.proxy_passwd, cmd_string },
132 { "proxyuser", &opt.proxy_user, cmd_string },
133 { "quiet", &opt.quiet, cmd_boolean },
134 { "quota", &opt.quota, cmd_bytes },
135 { "reclevel", &opt.reclevel, cmd_number_inf },
136 { "recursive", NULL, cmd_spec_recursive },
137 { "reject", &opt.rejects, cmd_vector },
138 { "relativeonly", &opt.relative_only, cmd_boolean },
139 { "removelisting", &opt.remove_listing, cmd_boolean },
140 { "retrsymlinks", &opt.retr_symlinks, cmd_boolean },
141 { "robots", &opt.use_robots, cmd_boolean },
142 { "saveheaders", &opt.save_headers, cmd_boolean },
143 { "serverresponse", &opt.server_response, cmd_boolean },
144 { "simplehostcheck", &opt.simple_check, cmd_boolean },
145 { "spanhosts", &opt.spanhost, cmd_boolean },
146 { "spider", &opt.spider, cmd_boolean },
147 { "timeout", &opt.timeout, cmd_time },
148 { "timestamping", &opt.timestamping, cmd_boolean },
149 { "tries", &opt.ntry, cmd_number_inf },
150 { "useproxy", &opt.use_proxy, cmd_boolean },
151 { "useragent", NULL, cmd_spec_useragent },
152 { "verbose", &opt.verbose, cmd_boolean },
153 { "wait", &opt.wait, cmd_time }
156 /* Return index of COM if it is a valid command, or -1 otherwise. COM
157 is looked up in `commands' using binary search algorithm. */
159 comind (const char *com)
161 int min = 0, max = ARRAY_SIZE (commands);
165 int i = (min + max) / 2;
166 int cmp = strcasecmp (com, commands[i].name);
178 /* Reset the variables to default values. */
184 /* Most of the default values are 0. Just reset everything, and
185 fill in the non-zero values. Note that initializing pointers to
186 NULL this way is technically illegal, but porting Wget to a
187 machine where NULL is not all-zero bit pattern will be the least
188 of the implementors' worries. */
189 memset (&opt, 0, sizeof (opt));
192 opt.dir_prefix = xstrdup (".");
196 opt.ftp_acc = xstrdup ("anonymous");
197 /*opt.ftp_pass = xstrdup (ftp_getaddress ());*/
202 tmp = getenv ("no_proxy");
204 opt.no_proxy = sepstring (tmp);
212 opt.remove_listing = 1;
214 opt.dot_bytes = 1024;
215 opt.dot_spacing = 10;
216 opt.dots_in_line = 50;
219 /* Return the user's home directory (strdup-ed), or NULL if none is
224 char *home = getenv ("HOME");
229 /* If HOME is not defined, try getting it from the password
231 struct passwd *pwd = getpwuid (getuid ());
232 if (!pwd || !pwd->pw_dir)
237 /* #### Maybe I should grab home_dir from registry, but the best
238 that I could get from there is user's Start menu. It sucks! */
242 return home ? xstrdup (home) : NULL;
245 /* Return the path to the user's .wgetrc. This is either the value of
246 `WGETRC' environment variable, or `$HOME/.wgetrc'.
248 If the `WGETRC' variable exists but the file does not exist, the
249 function will exit(). */
251 wgetrc_file_name (void)
256 /* Try the environment. */
257 env = getenv ("WGETRC");
260 if (!file_exists_p (env))
262 fprintf (stderr, "%s: %s: %s.\n", exec_name, file, strerror (errno));
265 return xstrdup (env);
269 /* If that failed, try $HOME/.wgetrc. */
273 file = (char *)xmalloc (strlen (home) + 1 + strlen (".wgetrc") + 1);
274 sprintf (file, "%s/.wgetrc", home);
277 /* Under Windows, "home" is (for the purposes of this function) the
278 directory where `wget.exe' resides, and `wget.ini' will be used
279 as file name. SYSTEM_WGETRC should not be defined under WINDOWS.
281 It is not as trivial as I assumed, because on 95 argv[0] is full
282 path, but on NT you get what you typed in command line. --dbudor */
286 file = (char *)xmalloc (strlen (home) + strlen ("wget.ini") + 1);
287 sprintf (file, "%swget.ini", home);
294 if (!file_exists_p (file))
302 /* Initialize variables from a wgetrc file */
304 run_wgetrc (const char *file)
310 fp = fopen (file, "rb");
313 fprintf (stderr, _("%s: Cannot read %s (%s).\n"), exec_name,
314 file, strerror (errno));
317 /* Reset line number. */
319 while ((line = read_whole_line (fp)))
323 int length = strlen (line);
325 if (length && line[length - 1] == '\r')
326 line[length - 1] = '\0';
327 /* Parse the line. */
328 status = parse_line (line, &com, &val);
330 /* If everything is OK, set the value. */
333 if (!setval (com, val))
334 fprintf (stderr, _("%s: Error in %s at line %d.\n"), exec_name,
339 else if (status == 0)
340 fprintf (stderr, _("%s: Error in %s at line %d.\n"), exec_name,
347 /* Initialize the defaults and run the system wgetrc and user's own
354 /* Load the hard-coded defaults. */
357 /* If SYSTEM_WGETRC is defined, use it. */
359 if (file_exists_p (SYSTEM_WGETRC))
360 run_wgetrc (SYSTEM_WGETRC);
362 /* Override it with your own, if one exists. */
363 file = wgetrc_file_name ();
366 /* #### We should somehow canonicalize `file' and SYSTEM_WGETRC,
369 if (!strcmp (file, SYSTEM_WGETRC))
371 fprintf (stderr, _("\
372 %s: Warning: Both system and user wgetrc point to `%s'.\n"),
382 /* Parse the line pointed by line, with the syntax:
383 <sp>* command <sp>* = <sp>* value <newline>
384 Uses malloc to allocate space for command and value.
385 If the line is invalid, data is freed and 0 is returned.
392 parse_line (const char *line, char **com, char **val)
394 const char *p = line;
395 const char *orig_comptr, *end;
399 while (*p == ' ' || *p == '\t')
402 /* Don't process empty lines. */
403 if (!*p || *p == '\n' || *p == '#')
406 for (orig_comptr = p; ISALPHA (*p) || *p == '_' || *p == '-'; p++)
408 /* The next char should be space or '='. */
409 if (!ISSPACE (*p) && (*p != '='))
411 *com = (char *)xmalloc (p - orig_comptr + 1);
412 for (new_comptr = *com; orig_comptr < p; orig_comptr++)
414 if (*orig_comptr == '_' || *orig_comptr == '-')
416 *new_comptr++ = *orig_comptr;
419 /* If the command is invalid, exit now. */
420 if (comind (*com) == -1)
426 /* Skip spaces before '='. */
427 for (; ISSPACE (*p); p++);
428 /* If '=' not found, bail out. */
434 /* Skip spaces after '='. */
435 for (++p; ISSPACE (*p); p++);
436 /* Get the ending position. */
437 for (end = p; *end && *end != '\n'; end++);
438 /* Allocate *val, and copy from line. */
439 *val = strdupdelim (p, end);
443 /* Set COM to VAL. This is the meat behind processing `.wgetrc'. No
444 fatals -- error signal prints a warning and resets to default
445 value. All error messages are printed to stderr, *not* to
446 opt.lfile, since opt.lfile wasn't even generated yet. */
448 setval (const char *com, const char *val)
457 /* #### Should I just abort()? */
459 fprintf (stderr, _("%s: BUG: unknown command `%s', value `%s'.\n"),
460 exec_name, com, val);
464 return ((*commands[ind].action) (com, val, commands[ind].closure));
467 /* Generic helper functions, for use with `commands'. */
469 static int myatoi PARAMS ((const char *s));
471 /* Store the boolean value from VAL to CLOSURE. COM is ignored,
472 except for error messages. */
474 cmd_boolean (const char *com, const char *val, void *closure)
478 if (!strcasecmp (val, "on")
479 || (*val == '1' && !*(val + 1)))
481 else if (!strcasecmp (val, "off")
482 || (*val == '0' && !*(val + 1)))
486 fprintf (stderr, _("%s: %s: Please specify on or off.\n"),
491 *(int *)closure = bool_value;
495 /* Set the non-negative integer value from VAL to CLOSURE. With
496 incorrect specification, the number remains unchanged. */
498 cmd_number (const char *com, const char *val, void *closure)
500 int num = myatoi (val);
504 fprintf (stderr, _("%s: %s: Invalid specification `%s'.\n"),
505 exec_name, com, val);
508 *(int *)closure = num;
512 /* Similar to cmd_number(), only accepts `inf' as a synonym for 0. */
514 cmd_number_inf (const char *com, const char *val, void *closure)
516 if (!strcasecmp (val, "inf"))
521 return cmd_number (com, val, closure);
524 /* Copy (strdup) the string at COM to a new location and place a
525 pointer to *CLOSURE. */
527 cmd_string (const char *com, const char *val, void *closure)
529 char **pstring = (char **)closure;
531 FREE_MAYBE (*pstring);
532 *pstring = xstrdup (val);
536 /* Merge the vector (array of strings separated with `,') in COM with
537 the vector (NULL-terminated array of strings) pointed to by
540 cmd_vector (const char *com, const char *val, void *closure)
542 char ***pvec = (char ***)closure;
545 *pvec = merge_vecs (*pvec, sepstring (val));
555 cmd_directory_vector (const char *com, const char *val, void *closure)
557 char ***pvec = (char ***)closure;
561 /* Strip the trailing slashes from directories. */
564 seps = sepstring (val);
565 for (t = seps; t && *t; t++)
567 int len = strlen (*t);
568 /* Skip degenerate case of root directory. */
571 if ((*t)[len - 1] == '/')
572 (*t)[len - 1] = '\0';
575 *pvec = merge_vecs (*pvec, seps);
585 /* Set the value stored in VAL to CLOSURE (which should point to a
586 long int), allowing several postfixes, with the following syntax
590 [0-9]+[kK] -> bytes * 1024
591 [0-9]+[mM] -> bytes * 1024 * 1024
594 Anything else is flagged as incorrect, and CLOSURE is unchanged. */
596 cmd_bytes (const char *com, const char *val, void *closure)
599 long *out = (long *)closure;
604 /* Check for "inf". */
605 if (p[0] == 'i' && p[1] == 'n' && p[2] == 'f' && p[3] == '\0')
610 /* Search for digits and construct result. */
611 for (; *p && ISDIGIT (*p); p++)
612 result = (10 * result) + (*p - '0');
613 /* If no digits were found, or more than one character is following
615 if (p == val || (*p != '\0' && *(p + 1) != '\0'))
617 printf (_("%s: Invalid specification `%s'\n"), com, val);
620 /* Search for a designator. */
621 switch (tolower (*p))
632 result *= (long)1024 * 1024;
636 result *= (long)1024 * 1024 * 1024;
639 printf (_("%s: Invalid specification `%s'\n"), com, val);
646 /* Store the value of VAL to *OUT, allowing suffixes for minutes and
649 cmd_time (const char *com, const char *val, void *closure)
654 /* Search for digits and construct result. */
655 for (; *p && ISDIGIT (*p); p++)
656 result = (10 * result) + (*p - '0');
657 /* If no digits were found, or more than one character is following
659 if (p == val || (*p != '\0' && *(p + 1) != '\0'))
661 printf (_("%s: Invalid specification `%s'\n"), com, val);
664 /* Search for a suffix. */
665 switch (tolower (*p))
679 /* Days (overflow on 16bit machines) */
687 printf (_("%s: Invalid specification `%s'\n"), com, val);
690 *(long *)closure = result;
694 /* Specialized helper functions, used by `commands' to handle some
695 options specially. */
697 static int check_user_specified_header PARAMS ((const char *));
700 cmd_spec_dirstruct (const char *com, const char *val, void *closure)
702 if (!cmd_boolean (com, val, &opt.dirstruct))
704 /* Since dirstruct behaviour is explicitly changed, no_dirstruct
705 must be affected inversely. */
707 opt.no_dirstruct = 0;
709 opt.no_dirstruct = 1;
714 cmd_spec_dotstyle (const char *com, const char *val, void *closure)
716 /* Retrieval styles. */
717 if (!strcasecmp (val, "default"))
719 /* Default style: 1K dots, 10 dots in a cluster, 50 dots in a
721 opt.dot_bytes = 1024;
722 opt.dot_spacing = 10;
723 opt.dots_in_line = 50;
725 else if (!strcasecmp (val, "binary"))
727 /* "Binary" retrieval: 8K dots, 16 dots in a cluster, 48 dots
729 opt.dot_bytes = 8192;
730 opt.dot_spacing = 16;
731 opt.dots_in_line = 48;
733 else if (!strcasecmp (val, "mega"))
735 /* "Mega" retrieval, for retrieving very long files; each dot is
736 64K, 8 dots in a cluster, 6 clusters (3M) in a line. */
737 opt.dot_bytes = 65536L;
739 opt.dots_in_line = 48;
741 else if (!strcasecmp (val, "giga"))
743 /* "Giga" retrieval, for retrieving very very *very* long files;
744 each dot is 1M, 8 dots in a cluster, 4 clusters (32M) in a
746 opt.dot_bytes = (1L << 20);
748 opt.dots_in_line = 32;
750 else if (!strcasecmp (val, "micro"))
752 /* "Micro" retrieval, for retrieving very small files (and/or
753 slow connections); each dot is 128 bytes, 8 dots in a
754 cluster, 6 clusters (6K) in a line. */
757 opt.dots_in_line = 48;
761 fprintf (stderr, _("%s: %s: Invalid specification `%s'.\n"),
762 exec_name, com, val);
769 cmd_spec_header (const char *com, const char *val, void *closure)
773 /* Empty header means reset headers. */
774 FREE_MAYBE (opt.user_header);
775 opt.user_header = NULL;
781 if (!check_user_specified_header (val))
783 fprintf (stderr, _("%s: %s: Invalid specification `%s'.\n"),
784 exec_name, com, val);
787 i = opt.user_header ? strlen (opt.user_header) : 0;
788 opt.user_header = (char *)xrealloc (opt.user_header, i + strlen (val)
790 strcpy (opt.user_header + i, val);
792 opt.user_header[i++] = '\r';
793 opt.user_header[i++] = '\n';
794 opt.user_header[i] = '\0';
800 cmd_spec_htmlify (const char *com, const char *val, void *closure)
802 int flag = cmd_boolean (com, val, &opt.htmlify);
803 if (flag && !opt.htmlify)
804 opt.remove_listing = 0;
809 cmd_spec_mirror (const char *com, const char *val, void *closure)
813 if (!cmd_boolean (com, val, &mirror))
818 if (!opt.no_dirstruct)
820 opt.timestamping = 1;
822 opt.remove_listing = 0;
828 cmd_spec_outputdocument (const char *com, const char *val, void *closure)
830 FREE_MAYBE (opt.output_document);
831 opt.output_document = xstrdup (val);
837 cmd_spec_recursive (const char *com, const char *val, void *closure)
839 if (!cmd_boolean (com, val, &opt.recursive))
843 if (opt.recursive && !opt.no_dirstruct)
850 cmd_spec_useragent (const char *com, const char *val, void *closure)
852 /* Just check for empty string and newline, so we don't throw total
853 junk to the server. */
854 if (!*val || strchr (val, '\n'))
856 fprintf (stderr, _("%s: %s: Invalid specification `%s'.\n"),
857 exec_name, com, val);
860 opt.useragent = xstrdup (val);
864 /* Miscellaneous useful routines. */
866 /* Return the integer value of a positive integer written in S, or -1
867 if an error was encountered. */
869 myatoi (const char *s)
872 const char *orig = s;
874 for (res = 0; *s && ISDIGIT (*s); s++)
875 res = 10 * res + (*s - '0');
882 #define ISODIGIT(x) ((x) >= '0' && (x) <= '7')
885 check_user_specified_header (const char *s)
889 for (p = s; *p && *p != ':' && !ISSPACE (*p); p++);
890 /* The header MUST contain `:' preceded by at least one
891 non-whitespace character. */
892 if (*p != ':' || p == s)
894 /* The header MUST NOT contain newlines. */
895 if (strchr (s, '\n'))
900 /* Free the memory allocated by global variables. */
904 extern acc_t *netrc_list;
906 recursive_cleanup ();
908 free_netrc (netrc_list);
911 FREE_MAYBE (opt.lfilename);
912 free (opt.dir_prefix);
913 FREE_MAYBE (opt.input_filename);
914 FREE_MAYBE (opt.output_document);
915 free_vec (opt.accepts);
916 free_vec (opt.rejects);
917 free_vec (opt.excludes);
918 free_vec (opt.includes);
919 free_vec (opt.domains);
922 FREE_MAYBE (opt.ftp_proxy);
923 FREE_MAYBE (opt.http_proxy);
924 free_vec (opt.no_proxy);
925 FREE_MAYBE (opt.useragent);
926 FREE_MAYBE (opt.http_user);
927 FREE_MAYBE (opt.http_passwd);
928 FREE_MAYBE (opt.user_header);