Debian Codegraph @

src/init.c

raw
1 /* Reading/parsing the initialization file.
2 Copyright (C) 1996-2012, 2014-2015, 2018-2024 Free Software
3 Foundation, Inc.
4
5 This file is part of GNU Wget.
6
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 3 of the License, or
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with Wget. If not, see <http://www.gnu.org/licenses/>.
19
20 Additional permission under GNU GPL version 3 section 7
21
22 If you modify this program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a
24 modified version of that library), containing parts covered by the
25 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
26 grants you additional permission to convey the resulting work.
27 Corresponding Source for a non-source form of such a combination
28 shall include the source code for the parts of OpenSSL used as well
29 as that of the covered work. */
30
31 #include "wget.h"
32 #include "exits.h"
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <stdbool.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <errno.h>
40 #include <limits.h>
41 /* not all systems provide PATH_MAX in limits.h */
42 #ifndef PATH_MAX
43 # include <sys/param.h>
44 # ifndef PATH_MAX
45 # define PATH_MAX MAXPATHLEN
46 # endif
47 #endif
48
49 #include <regex.h>
50
51 #ifdef HAVE_PWD_H
52 # include <pwd.h>
53 #endif
54 #include <assert.h>
55
56 #include "utils.h"
57 #include "init.h"
58 #include "host.h"
59 #include "netrc.h"
60 #include "progress.h"
61 #include "connect.h" /* for connect_cleanup */
62 #include "ssl.h" /* for ssl_cleanup */
63 #include "recur.h" /* for INFINITE_RECURSION */
64 #include "convert.h" /* for convert_cleanup */
65 #include "res.h" /* for res_cleanup */
66 #include "http.h" /* for http_cleanup */
67 #include "retr.h" /* for output_stream */
68 #include "warc.h" /* for warc_close */
69 #include "spider.h" /* for spider_cleanup */
70 #include "html-url.h" /* for cleanup_html_url */
71 #include "ptimer.h" /* for ptimer_destroy */
72 #include "c-strcase.h"
73
74 #ifdef TESTING
75 #include "../tests/unit-tests.h"
76 #endif
77
78
79
80 #define CMD_DECLARE(func) static bool func (const char *, const char *, void *)
81
82 CMD_DECLARE (cmd_boolean);
83 CMD_DECLARE (cmd_bytes);
84 CMD_DECLARE (cmd_bytes_sum);
85 #ifdef HAVE_SSL
86 CMD_DECLARE (cmd_cert_type);
87 #endif
88 CMD_DECLARE (cmd_directory_vector);
89 CMD_DECLARE (cmd_number);
90 CMD_DECLARE (cmd_number_inf);
91 CMD_DECLARE (cmd_string);
92 CMD_DECLARE (cmd_string_uppercase);
93 CMD_DECLARE (cmd_file);
94 CMD_DECLARE (cmd_file_once);
95 CMD_DECLARE (cmd_directory);
96 CMD_DECLARE (cmd_time);
97 CMD_DECLARE (cmd_vector);
98
99 CMD_DECLARE (cmd_use_askpass);
100
101 #ifdef HAVE_LIBZ
102 CMD_DECLARE (cmd_spec_compression);
103 #endif
104 CMD_DECLARE (cmd_spec_dirstruct);
105 CMD_DECLARE (cmd_spec_header);
106 CMD_DECLARE (cmd_spec_warc_header);
107 CMD_DECLARE (cmd_spec_htmlify);
108 CMD_DECLARE (cmd_spec_mirror);
109 CMD_DECLARE (cmd_spec_prefer_family);
110 CMD_DECLARE (cmd_spec_progress);
111 CMD_DECLARE (cmd_spec_progressdisp);
112 CMD_DECLARE (cmd_spec_recursive);
113 CMD_DECLARE (cmd_spec_regex_type);
114 CMD_DECLARE (cmd_spec_restrict_file_names);
115 CMD_DECLARE (cmd_spec_report_speed);
116 #ifdef HAVE_SSL
117 CMD_DECLARE (cmd_spec_secure_protocol);
118 #endif
119 CMD_DECLARE (cmd_spec_timeout);
120 CMD_DECLARE (cmd_spec_useragent);
121 CMD_DECLARE (cmd_spec_verbose);
122 CMD_DECLARE (cmd_check_cert);
123
124 /* List of recognized commands, each consisting of name, place and
125 function. When adding a new command, simply add it to the list,
126 but be sure to keep the list sorted alphabetically, as
127 command_by_name's binary search depends on it. Also, be sure to
128 add any entries that allocate memory (e.g. cmd_string and
129 cmd_vector) to the cleanup() function below. */
130
131 static const struct {
132 const char *name;
133 void *place;
134 bool (*action) (const char *, const char *, void *);
135 } commands[] = {
136 /* KEEP THIS LIST ALPHABETICALLY SORTED */
137 { "accept", &opt.accepts, cmd_vector },
138 { "acceptregex", &opt.acceptregex_s, cmd_string },
139 { "addhostdir", &opt.add_hostdir, cmd_boolean },
140 { "adjustextension", &opt.adjust_extension, cmd_boolean },
141 { "alwaysrest", &opt.always_rest, cmd_boolean }, /* deprecated */
142 { "askpassword", &opt.ask_passwd, cmd_boolean },
143 { "authnochallenge", &opt.auth_without_challenge,
144 cmd_boolean },
145 { "background", &opt.background, cmd_boolean },
146 { "backupconverted", &opt.backup_converted, cmd_boolean },
147 { "backups", &opt.backups, cmd_number },
148 { "base", &opt.base_href, cmd_string },
149 { "bindaddress", &opt.bind_address, cmd_string },
150 #ifdef HAVE_LIBCARES
151 { "binddnsaddress", &opt.bind_dns_address, cmd_string },
152 #endif
153 { "bodydata", &opt.body_data, cmd_string },
154 { "bodyfile", &opt.body_file, cmd_string },
155 #ifdef HAVE_SSL
156 { "cacertificate", &opt.ca_cert, cmd_file },
157 #endif
158 { "cache", &opt.allow_cache, cmd_boolean },
159 #ifdef HAVE_SSL
160 { "cadirectory", &opt.ca_directory, cmd_directory },
161 { "certificate", &opt.cert_file, cmd_file },
162 { "certificatetype", &opt.cert_type, cmd_cert_type },
163 { "checkcertificate", &opt.check_cert, cmd_check_cert },
164 #endif
165 { "chooseconfig", &opt.choose_config, cmd_file },
166 #ifdef HAVE_SSL
167 { "ciphers", &opt.tls_ciphers_string, cmd_string },
168 #endif
169 #ifdef HAVE_LIBZ
170 { "compression", &opt.compression, cmd_spec_compression },
171 #endif
172 { "connecttimeout", &opt.connect_timeout, cmd_time },
173 { "contentdisposition", &opt.content_disposition, cmd_boolean },
174 { "contentonerror", &opt.content_on_error, cmd_boolean },
175 { "continue", &opt.always_rest, cmd_boolean },
176 { "convertfileonly", &opt.convert_file_only, cmd_boolean },
177 { "convertlinks", &opt.convert_links, cmd_boolean },
178 { "cookies", &opt.cookies, cmd_boolean },
179 #ifdef HAVE_SSL
180 { "crlfile", &opt.crl_file, cmd_file_once },
181 #endif
182 { "cutdirs", &opt.cut_dirs, cmd_number },
183 { "debug", &opt.debug, cmd_boolean },
184 { "defaultpage", &opt.default_page, cmd_string },
185 { "deleteafter", &opt.delete_after, cmd_boolean },
186 { "dirprefix", &opt.dir_prefix, cmd_directory },
187 { "dirstruct", NULL, cmd_spec_dirstruct },
188 { "dnscache", &opt.dns_cache, cmd_boolean },
189 #ifdef HAVE_LIBCARES
190 { "dnsservers", &opt.dns_servers, cmd_string },
191 #endif
192 { "dnstimeout", &opt.dns_timeout, cmd_time },
193 { "domains", &opt.domains, cmd_vector },
194 { "dotbytes", &opt.dot_bytes, cmd_bytes },
195 { "dotsinline", &opt.dots_in_line, cmd_number },
196 { "dotspacing", &opt.dot_spacing, cmd_number },
197 { "dotstyle", &opt.dot_style, cmd_string }, /* deprecated */
198 #ifdef HAVE_SSL
199 { "egdfile", &opt.egd_file, cmd_file },
200 #endif
201 { "excludedirectories", &opt.excludes, cmd_directory_vector },
202 { "excludedomains", &opt.exclude_domains, cmd_vector },
203 { "followftp", &opt.follow_ftp, cmd_boolean },
204 { "followtags", &opt.follow_tags, cmd_vector },
205 { "forcehtml", &opt.force_html, cmd_boolean },
206 { "ftppasswd", &opt.ftp_passwd, cmd_string }, /* deprecated */
207 { "ftppassword", &opt.ftp_passwd, cmd_string },
208 { "ftpproxy", &opt.ftp_proxy, cmd_string },
209 #ifdef HAVE_SSL
210 { "ftpscleardataconnection", &opt.ftps_clear_data_connection, cmd_boolean },
211 { "ftpsfallbacktoftp", &opt.ftps_fallback_to_ftp, cmd_boolean },
212 { "ftpsimplicit", &opt.ftps_implicit, cmd_boolean },
213 { "ftpsresumessl", &opt.ftps_resume_ssl, cmd_boolean },
214 #endif
215 #ifdef __VMS
216 { "ftpstmlf", &opt.ftp_stmlf, cmd_boolean },
217 #endif /* def __VMS */
218 { "ftpuser", &opt.ftp_user, cmd_string },
219 { "glob", &opt.ftp_glob, cmd_boolean },
220 { "header", NULL, cmd_spec_header },
221 #ifdef HAVE_HSTS
222 { "hsts", &opt.hsts, cmd_boolean },
223 { "hstsfile", &opt.hsts_file, cmd_file },
224 #endif
225 { "htmlextension", &opt.adjust_extension, cmd_boolean }, /* deprecated */
226 { "htmlify", NULL, cmd_spec_htmlify },
227 { "httpkeepalive", &opt.http_keep_alive, cmd_boolean },
228 { "httppasswd", &opt.http_passwd, cmd_string }, /* deprecated */
229 { "httppassword", &opt.http_passwd, cmd_string },
230 { "httpproxy", &opt.http_proxy, cmd_string },
231 #ifdef HAVE_SSL
232 { "httpsonly", &opt.https_only, cmd_boolean },
233 #endif
234 { "httpsproxy", &opt.https_proxy, cmd_string },
235 { "httpuser", &opt.http_user, cmd_string },
236 { "ifmodifiedsince", &opt.if_modified_since, cmd_boolean },
237 { "ignorecase", &opt.ignore_case, cmd_boolean },
238 { "ignorelength", &opt.ignore_length, cmd_boolean },
239 { "ignoretags", &opt.ignore_tags, cmd_vector },
240 { "includedirectories", &opt.includes, cmd_directory_vector },
241 #ifdef ENABLE_IPV6
242 { "inet4only", &opt.ipv4_only, cmd_boolean },
243 { "inet6only", &opt.ipv6_only, cmd_boolean },
244 #endif
245 { "input", &opt.input_filename, cmd_file },
246 #ifdef HAVE_METALINK
247 { "inputmetalink", &opt.input_metalink, cmd_file },
248 #endif
249 { "iri", &opt.enable_iri, cmd_boolean },
250 { "keepbadhash", &opt.keep_badhash, cmd_boolean },
251 { "keepsessioncookies", &opt.keep_session_cookies, cmd_boolean },
252 { "limitrate", &opt.limit_rate, cmd_bytes },
253 { "loadcookies", &opt.cookies_input, cmd_file },
254 { "localencoding", &opt.locale, cmd_string },
255 { "logfile", &opt.lfilename, cmd_file },
256 { "login", &opt.ftp_user, cmd_string },/* deprecated*/
257 { "maxredirect", &opt.max_redirect, cmd_number },
258 #ifdef HAVE_METALINK
259 { "metalinkindex", &opt.metalink_index, cmd_number_inf },
260 { "metalinkoverhttp", &opt.metalink_over_http, cmd_boolean },
261 #endif
262 { "method", &opt.method, cmd_string_uppercase },
263 { "mirror", NULL, cmd_spec_mirror },
264 { "netrc", &opt.netrc, cmd_boolean },
265 { "noclobber", &opt.noclobber, cmd_boolean },
266 { "noconfig", &opt.noconfig, cmd_boolean },
267 { "noparent", &opt.no_parent, cmd_boolean },
268 { "noproxy", &opt.no_proxy, cmd_vector },
269 { "numtries", &opt.ntry, cmd_number_inf },/* deprecated*/
270 { "outputdocument", &opt.output_document, cmd_file },
271 { "pagerequisites", &opt.page_requisites, cmd_boolean },
272 { "passiveftp", &opt.ftp_pasv, cmd_boolean },
273 { "passwd", &opt.ftp_passwd, cmd_string },/* deprecated*/
274 { "password", &opt.passwd, cmd_string },
275 #ifdef HAVE_SSL
276 { "pinnedpubkey", &opt.pinnedpubkey, cmd_string },
277 #endif
278 { "postdata", &opt.post_data, cmd_string },
279 { "postfile", &opt.post_file_name, cmd_file },
280 { "preferfamily", NULL, cmd_spec_prefer_family },
281 #ifdef HAVE_METALINK
282 { "preferredlocation", &opt.preferred_location, cmd_string },
283 #endif
284 { "preservepermissions", &opt.preserve_perm, cmd_boolean },
285 #ifdef HAVE_SSL
286 { "privatekey", &opt.private_key, cmd_file },
287 { "privatekeytype", &opt.private_key_type, cmd_cert_type },
288 #endif
289 { "progress", &opt.progress_type, cmd_spec_progress },
290 { "protocoldirectories", &opt.protocol_directories, cmd_boolean },
291 { "proxypasswd", &opt.proxy_passwd, cmd_string }, /* deprecated */
292 { "proxypassword", &opt.proxy_passwd, cmd_string },
293 { "proxyuser", &opt.proxy_user, cmd_string },
294 { "quiet", &opt.quiet, cmd_boolean },
295 { "quota", &opt.quota, cmd_bytes_sum },
296 #ifdef HAVE_SSL
297 { "randomfile", &opt.random_file, cmd_file },
298 #endif
299 { "randomwait", &opt.random_wait, cmd_boolean },
300 { "readtimeout", &opt.read_timeout, cmd_time },
301 { "reclevel", &opt.reclevel, cmd_number_inf },
302 { "recursive", NULL, cmd_spec_recursive },
303 { "referer", &opt.referer, cmd_string },
304 { "regextype", &opt.regex_type, cmd_spec_regex_type },
305 { "reject", &opt.rejects, cmd_vector },
306 { "rejectedlog", &opt.rejected_log, cmd_file },
307 { "rejectregex", &opt.rejectregex_s, cmd_string },
308 { "relativeonly", &opt.relative_only, cmd_boolean },
309 { "remoteencoding", &opt.encoding_remote, cmd_string },
310 { "removelisting", &opt.remove_listing, cmd_boolean },
311 { "reportspeed", &opt.report_bps, cmd_spec_report_speed},
312 { "restrictfilenames", NULL, cmd_spec_restrict_file_names },
313 { "retrsymlinks", &opt.retr_symlinks, cmd_boolean },
314 { "retryconnrefused", &opt.retry_connrefused, cmd_boolean },
315 { "retryonhosterror", &opt.retry_on_host_error, cmd_boolean },
316 { "retryonhttperror", &opt.retry_on_http_error, cmd_string },
317 { "robots", &opt.use_robots, cmd_boolean },
318 { "savecookies", &opt.cookies_output, cmd_file },
319 { "saveheaders", &opt.save_headers, cmd_boolean },
320 #ifdef HAVE_SSL
321 { "secureprotocol", &opt.secure_protocol, cmd_spec_secure_protocol },
322 #endif
323 { "serverresponse", &opt.server_response, cmd_boolean },
324 { "showalldnsentries", &opt.show_all_dns_entries, cmd_boolean },
325 { "showprogress", &opt.show_progress, cmd_spec_progressdisp },
326 { "spanhosts", &opt.spanhost, cmd_boolean },
327 { "spider", &opt.spider, cmd_boolean },
328 { "startpos", &opt.start_pos, cmd_bytes },
329 { "strictcomments", &opt.strict_comments, cmd_boolean },
330 { "timeout", NULL, cmd_spec_timeout },
331 { "timestamping", &opt.timestamping, cmd_boolean },
332 { "tries", &opt.ntry, cmd_number_inf },
333 { "trustservernames", &opt.trustservernames, cmd_boolean },
334 { "unlink", &opt.unlink_requested, cmd_boolean },
335 #ifndef __VMS
336 { "useaskpass" , &opt.use_askpass, cmd_use_askpass },
337 #endif
338 { "useproxy", &opt.use_proxy, cmd_boolean },
339 { "user", &opt.user, cmd_string },
340 { "useragent", NULL, cmd_spec_useragent },
341 { "useservertimestamps", &opt.useservertimestamps, cmd_boolean },
342 { "verbose", NULL, cmd_spec_verbose },
343 { "wait", &opt.wait, cmd_time },
344 { "waitretry", &opt.waitretry, cmd_time },
345 { "warccdx", &opt.warc_cdx_enabled, cmd_boolean },
346 { "warccdxdedup", &opt.warc_cdx_dedup_filename, cmd_file },
347 #ifdef HAVE_LIBZ
348 { "warccompression", &opt.warc_compression_enabled, cmd_boolean },
349 #endif
350 { "warcdigests", &opt.warc_digests_enabled, cmd_boolean },
351 { "warcfile", &opt.warc_filename, cmd_file },
352 { "warcheader", NULL, cmd_spec_warc_header },
353 { "warckeeplog", &opt.warc_keep_log, cmd_boolean },
354 { "warcmaxsize", &opt.warc_maxsize, cmd_bytes },
355 { "warctempdir", &opt.warc_tempdir, cmd_directory },
356 #ifdef USE_WATT32
357 { "wdebug", &opt.wdebug, cmd_boolean },
358 #endif
359 #ifdef ENABLE_XATTR
360 { "xattr", &opt.enable_xattr, cmd_boolean },
361 #endif
362 };
363
364 /* Look up CMDNAME in the commands[] and return its position in the
365 array. If CMDNAME is not found, return -1. */
366
367 static int
368 command_by_name (const char *cmdname)
369 {
370 /* Use binary search for speed. Wget has ~100 commands, which
371 guarantees a worst case performance of 7 string comparisons. */
372 int lo = 0, hi = countof (commands) - 1;
373
374 while (lo <= hi)
375 {
376 int mid = (lo + hi) >> 1;
377 int cmp = c_strcasecmp (cmdname, commands[mid].name);
378 if (cmp < 0)
379 hi = mid - 1;
380 else if (cmp > 0)
381 lo = mid + 1;
382 else
383 return mid;
384 }
385 return -1;
386 }
387
388 /* Reset the variables to default values. */
389 void
390 defaults (void)
391 {
392 char *tmp;
393
394 /* Most of the default values are 0 (and 0.0, NULL, and false).
395 Just reset everything, and fill in the non-zero values. Note
396 that initializing pointers to NULL this way is technically
397 illegal, but porting Wget to a machine where NULL is not all-zero
398 bit pattern will be the least of the implementors' worries. */
399 xzero (opt);
400
401 #ifdef HAVE_METALINK
402 opt.metalink_index = -1;
403 #endif
404
405 opt.cookies = true;
406 opt.verbose = -1;
407 opt.ntry = 20;
408 opt.reclevel = 5;
409 opt.add_hostdir = true;
410 opt.netrc = true;
411 opt.ftp_glob = true;
412 opt.htmlify = true;
413 opt.http_keep_alive = true;
414 opt.use_proxy = true;
415 opt.convert_file_only = false;
416 tmp = getenv ("no_proxy");
417 if (tmp)
418 opt.no_proxy = sepstring (tmp);
419 opt.prefer_family = prefer_none;
420 opt.allow_cache = true;
421 opt.if_modified_since = true;
422
423 opt.read_timeout = 900;
424 opt.use_robots = true;
425
426 opt.remove_listing = true;
427
428 opt.dot_bytes = 1024;
429 opt.dot_spacing = 10;
430 opt.dots_in_line = 50;
431
432 opt.dns_cache = true;
433 opt.ftp_pasv = true;
434 /* 2014-09-07 Darshit Shah <[email protected]>
435 * opt.retr_symlinks is set to true by default. Creating symbolic links on the
436 * local file system pose a security threat by malicious FTP Servers that
437 * server a specially crafted .listing file akin to this:
438 *
439 * lrwxrwxrwx 1 root root 33 Dec 25 2012 JoCxl6d8rFU -> /
440 * drwxrwxr-x 15 1024 106 4096 Aug 28 02:02 JoCxl6d8rFU
441 *
442 * A .listing file in this fashion makes Wget susceptiple to a symlink attack
443 * wherein the attacker is able to create arbitrary files, directories and
444 * symbolic links on the target system and even set permissions.
445 *
446 * Hence, by default Wget attempts to retrieve the pointed-to files and does
447 * not create the symbolic links locally.
448 */
449 opt.retr_symlinks = true;
450
451 #ifdef HAVE_SSL
452 opt.check_cert = CHECK_CERT_ON;
453 opt.ftps_resume_ssl = true;
454 opt.ftps_fallback_to_ftp = false;
455 opt.ftps_implicit = false;
456 opt.ftps_clear_data_connection = false;
457 #endif
458
459 #ifdef HAVE_LIBZ
460 opt.compression = compression_none;
461 #endif
462
463 /* The default for file name restriction defaults to the OS type. */
464 #if defined(WINDOWS) || defined(MSDOS) || defined(__CYGWIN__)
465 opt.restrict_files_os = restrict_windows;
466 #elif defined(__VMS)
467 opt.restrict_files_os = restrict_vms;
468 #else
469 opt.restrict_files_os = restrict_unix;
470 #endif
471 opt.restrict_files_ctrl = true;
472 opt.restrict_files_nonascii = false;
473 opt.restrict_files_case = restrict_no_case_restriction;
474
475 opt.regex_type = regex_type_posix;
476
477 opt.max_redirect = 20;
478
479 opt.waitretry = 10;
480
481 #ifdef ENABLE_IRI
482 opt.enable_iri = true;
483 #else
484 opt.enable_iri = false;
485 #endif
486 opt.locale = NULL;
487 opt.encoding_remote = NULL;
488
489 opt.useservertimestamps = true;
490 opt.show_all_dns_entries = false;
491
492 opt.warc_maxsize = 0; /* 1024 * 1024 * 1024; */
493 #ifdef HAVE_LIBZ
494 opt.warc_compression_enabled = true;
495 #else
496 opt.warc_compression_enabled = false;
497 #endif
498 opt.warc_digests_enabled = true;
499 opt.warc_cdx_enabled = false;
500 opt.warc_cdx_dedup_filename = NULL;
501 opt.warc_tempdir = NULL;
502 opt.warc_keep_log = true;
503
504 /* Use a negative value to mark the absence of --start-pos option */
505 opt.start_pos = -1;
506 opt.show_progress = -1;
507 opt.noscroll = false;
508
509 #ifdef HAVE_HSTS
510 /* HSTS is enabled by default */
511 opt.hsts = true;
512 #endif
513
514 opt.enable_xattr = false;
515 }
516
517 /* Return the user's home directory (strdup-ed), or NULL if none is
518 found. */
519 char *
520 home_dir (void)
521 {
522 static char *buf = NULL;
523 static char *home, *ret;
524
525 if (!home)
526 {
527 home = getenv ("HOME");
528 if (!home)
529 {
530 #if defined(MSDOS)
531 int len;
532
533 /* Under MSDOS, if $HOME isn't defined, use the directory where
534 `wget.exe' resides. */
535 const char *_w32_get_argv0 (void); /* in libwatt.a/pcconfig.c */
536 char *p;
537
538 buff = _w32_get_argv0 ();
539
540 p = strrchr (buf, '/'); /* djgpp */
541 if (!p)
542 p = strrchr (buf, '\\'); /* others */
543 assert (p);
544
545 len = p - buff + 1;
546 buff = strdup (_w32_get_argv0 ());
547
548 home = buf;
549 #elif !defined(WINDOWS)
550 /* If HOME is not defined, try getting it from the password
551 file. */
552 struct passwd *pwd = getpwuid (getuid ());
553 if (!pwd || !pwd->pw_dir)
554 return NULL;
555 home = pwd->pw_dir;
556 #else /* !WINDOWS */
557 /* Under Windows, if $HOME isn't defined, use the directory where
558 `wget.exe' resides. */
559 home = ws_mypath ();
560 #endif /* WINDOWS */
561 }
562 }
563
564 ret = home ? xstrdup (home) : NULL;
565 xfree (buf);
566
567 return ret;
568 }
569
570 /* Check the 'WGETRC' environment variable and return the file name
571 if 'WGETRC' is set and is a valid file.
572 If the `WGETRC' variable exists but the file does not exist, the
573 function will exit(). */
574 char *
575 wgetrc_env_file_name (void)
576 {
577 char *env = getenv ("WGETRC");
578 if (env && *env)
579 {
580 file_stats_t flstat;
581 if (!file_exists_p (env, &flstat))
582 {
583 fprintf (stderr, _("%s: WGETRC points to %s, which couldn't be accessed because of error: %s.\n"),
584 exec_name, env, strerror(flstat.access_err));
585 exit (WGET_EXIT_GENERIC_ERROR);
586 }
587 return xstrdup (env);
588 }
589 return NULL;
590 }
591
592 /* Append file name to (locally appropriate) directory spec.
593 Return pointer to allocated storage. */
594 char *
595 ajoin_dir_file (const char *dir, const char *file)
596 {
597 char *dir_file;
598 #ifdef __VMS
599 /* No separator: "dev:[dir]" + "name.type" */
600 dir_file = aprintf ("%s%s", dir, file);
601 #else /* def __VMS */
602 /* Slash separator: "/a/b" + "/" + "name.type" */
603 dir_file = aprintf ("%s/%s", dir, file);
604 #endif /* def __VMS [else] */
605 return dir_file;
606 }
607
608 /* Check for the existence of '$HOME/.wgetrc' and return its path
609 if it exists and is set. */
610 char *
611 wgetrc_user_file_name (void)
612 {
613 char *file = NULL;
614
615 /* Join opt.homedir ($HOME) and ".wgetrc" */
616 if (opt.homedir) {
617 file = ajoin_dir_file(opt.homedir, ".wgetrc");
618 }
619
620 if (!file)
621 return NULL;
622 #ifndef FUZZING
623 if (!file_exists_p (file, NULL))
624 {
625 xfree (file);
626 return NULL;
627 }
628 #endif
629 return file;
630 }
631
632 /* Return the path to the user's .wgetrc. This is either the value of
633 `WGETRC' environment variable, or `$HOME/.wgetrc'.
634
635 Additionally, for windows, look in the directory where wget.exe
636 resides. */
637 char *
638 wgetrc_file_name (void)
639 {
640 char *file = wgetrc_env_file_name ();
641 if (file && *file)
642 return file;
643
644 file = wgetrc_user_file_name ();
645
646 #ifdef WINDOWS
647 /* Under Windows, if we still haven't found .wgetrc, look for the file
648 `wget.ini' in the directory where `wget.exe' resides; we do this for
649 backward compatibility with previous versions of Wget.
650 SYSTEM_WGETRC should not be defined under WINDOWS. */
651 if (!file)
652 {
653 const char *home = ws_mypath ();
654 if (home)
655 {
656 file = aprintf ("%s/wget.ini", home);
657 if (!file_exists_p (file, NULL))
658 {
659 xfree (file);
660 }
661 }
662 }
663 #endif /* WINDOWS */
664
665 return file;
666 }
667
668 /* Return values of parse_line. */
669 enum parse_line {
670 line_ok,
671 line_empty,
672 line_syntax_error,
673 line_unknown_command
674 };
675
676 static enum parse_line parse_line (const char *, char **, char **, int *);
677 static bool setval_internal (int, const char *, const char *);
678 static bool setval_internal_tilde (int, const char *, const char *);
679
680 /* Initialize variables from a wgetrc file. Returns zero (failure) if
681 there were errors in the file. */
682
683 bool
684 run_wgetrc (const char *file, file_stats_t *flstats)
685 {
686 FILE *fp;
687 char *line = NULL;
688 size_t bufsize = 0;
689 int ln;
690 int errcnt = 0;
691
692 fp = fopen_stat (file, "r", flstats);
693 if (!fp)
694 {
695 fprintf (stderr, _("%s: Cannot read %s (%s).\n"), exec_name,
696 file, strerror (errno));
697 return true; /* not a fatal error */
698 }
699 ln = 1;
700 while (getline (&line, &bufsize, fp) > 0)
701 {
702 char *com = NULL, *val = NULL;
703 int comind;
704
705 /* Parse the line. */
706 switch (parse_line (line, &com, &val, &comind))
707 {
708 case line_ok:
709 /* If everything is OK, set the value. */
710 if (!setval_internal_tilde (comind, com, val))
711 {
712 fprintf (stderr, _("%s: Error in %s at line %d.\n"),
713 exec_name, file, ln);
714 ++errcnt;
715 }
716 break;
717 case line_syntax_error:
718 fprintf (stderr, _("%s: Syntax error in %s at line %d.\n"),
719 exec_name, file, ln);
720 ++errcnt;
721 break;
722 case line_unknown_command:
723 fprintf (stderr, _("%s: Unknown command %s in %s at line %d.\n"),
724 exec_name, quote (com), file, ln);
725 ++errcnt;
726 break;
727 case line_empty:
728 break;
729 default:
730 abort ();
731 }
732 xfree (com);
733 xfree (val);
734 ++ln;
735 }
736 xfree (line);
737 fclose (fp);
738
739 return errcnt == 0;
740 }
741
742 /* Initialize the defaults and run the system wgetrc and user's own
743 wgetrc. */
744 int
745 initialize (void)
746 {
747 char *env_sysrc;
748 file_stats_t flstats;
749 bool ok = true;
750
751 memset(&flstats, 0, sizeof(flstats));
752 /* Run a non-standard system rc file when the according environment
753 variable has been set. For internal testing purposes only! */
754 env_sysrc = getenv ("SYSTEM_WGETRC");
755 if (env_sysrc && file_exists_p (env_sysrc, &flstats))
756 {
757 ok &= run_wgetrc (env_sysrc, &flstats);
758 /* If there are any problems parsing the system wgetrc file, tell
759 the user and exit */
760 if (! ok)
761 {
762 fprintf (stderr, _("\
763 Parsing system wgetrc file (env SYSTEM_WGETRC) failed. Please check\n\
764 '%s',\n\
765 or specify a different file using --config.\n"), env_sysrc);
766 return WGET_EXIT_PARSE_ERROR;
767 }
768 }
769 /* Otherwise, if SYSTEM_WGETRC is defined, use it. */
770 #ifdef SYSTEM_WGETRC
771 else if (file_exists_p (SYSTEM_WGETRC, &flstats))
772 ok &= run_wgetrc (SYSTEM_WGETRC, &flstats);
773 /* If there are any problems parsing the system wgetrc file, tell
774 the user and exit */
775 if (! ok)
776 {
777 fprintf (stderr, _("\
778 Parsing system wgetrc file failed. Please check\n\
779 '%s',\n\
780 or specify a different file using --config.\n"), SYSTEM_WGETRC);
781 return WGET_EXIT_PARSE_ERROR;
782 }
783 #endif
784 /* Override it with your own, if one exists. */
785 opt.wgetrcfile = wgetrc_file_name ();
786 if (!opt.wgetrcfile)
787 return 0;
788 /* #### We should canonicalize `file' and SYSTEM_WGETRC with
789 something like realpath() before comparing them with `strcmp' */
790 #ifdef SYSTEM_WGETRC
791 if (!strcmp (opt.wgetrcfile, SYSTEM_WGETRC))
792 {
793 fprintf (stderr, _("\
794 %s: Warning: Both system and user wgetrc point to %s.\n"),
795 exec_name, quote (opt.wgetrcfile));
796 }
797 else
798 #endif
799 #ifndef FUZZING
800 if (file_exists_p (opt.wgetrcfile, &flstats))
801 #endif
802 ok &= run_wgetrc (opt.wgetrcfile, &flstats);
803
804 xfree (opt.wgetrcfile);
805
806 /* If there were errors processing either `.wgetrc', abort. */
807 if (!ok)
808 return WGET_EXIT_PARSE_ERROR;
809
810 return 0;
811 }
812
813 /* Remove dashes and underscores from S, modifying S in the
814 process. */
815
816 static void
817 dehyphen (char *s)
818 {
819 char *t = s; /* t - tortoise */
820 char *h = s; /* h - hare */
821 while (*h)
822 if (*h == '_' || *h == '-')
823 ++h;
824 else
825 *t++ = *h++;
826 *t = '\0';
827 }
828
829 /* Parse the line pointed by line, with the syntax:
830 <sp>* command <sp>* = <sp>* value <sp>*
831 Uses malloc to allocate space for command and value.
832
833 Returns one of line_ok, line_empty, line_syntax_error, or
834 line_unknown_command.
835
836 In case of line_ok, *COM and *VAL point to freshly allocated
837 strings, and *COMIND points to com's index. In case of error or
838 empty line, their values are unmodified. */
839
840 static enum parse_line
841 parse_line (const char *line, char **com, char **val, int *comind)
842 {
843 const char *p;
844 const char *end = line + strlen (line);
845 const char *cmdstart, *cmdend;
846 const char *valstart, *valend;
847 char buf[1024];
848 size_t len;
849
850 char *cmdcopy;
851 int ind;
852
853 /* Skip leading and trailing whitespace. */
854 while (*line && c_isspace (*line))
855 ++line;
856 while (end > line && c_isspace (end[-1]))
857 --end;
858
859 /* Skip empty lines and comments. */
860 if (!*line || *line == '#')
861 return line_empty;
862
863 p = line;
864
865 cmdstart = p;
866 while (p < end && (c_isalnum (*p) || *p == '_' || *p == '-'))
867 ++p;
868 cmdend = p;
869
870 /* Skip '=', as well as any space before or after it. */
871 while (p < end && c_isspace (*p))
872 ++p;
873 if (p == end || *p != '=')
874 return line_syntax_error;
875 ++p;
876 while (p < end && c_isspace (*p))
877 ++p;
878
879 valstart = p;
880 valend = end;
881
882 /* The syntax is valid (even though the command might not be). Fill
883 in the command name and value. */
884 *com = strdupdelim (cmdstart, cmdend);
885 *val = strdupdelim (valstart, valend);
886
887 /* The line now known to be syntactically correct. Check whether
888 the command is valid. */
889 len = cmdend - cmdstart;
890 if (len < sizeof (buf))
891 cmdcopy = buf;
892 else
893 cmdcopy = xmalloc (len + 1);
894 memcpy (cmdcopy, cmdstart, len);
895 cmdcopy[len] = 0;
896
897 dehyphen (cmdcopy);
898 ind = command_by_name (cmdcopy);
899 if (cmdcopy != buf)
900 xfree (cmdcopy);
901 if (ind == -1)
902 return line_unknown_command;
903
904 /* Report success to the caller. */
905 *comind = ind;
906 return line_ok;
907 }
908
909 #if defined(WINDOWS) || defined(MSDOS)
910 # define ISSEP(c) ((c) == '/' || (c) == '\\')
911 # define SEPSTRING "/\\"
912 #else
913 # define ISSEP(c) ((c) == '/')
914 # define SEPSTRING "/"
915 #endif
916
917 /* Run commands[comind].action. */
918
919 static bool
920 setval_internal (int comind, const char *com, const char *val)
921 {
922 assert (0 <= comind && ((size_t) comind) < countof (commands));
923
924 if ((unsigned) comind >= countof (commands))
925 return NULL;
926
927 DEBUGP (("Setting %s (%s) to %s\n", com, commands[comind].name, val));
928 return commands[comind].action (com, val, commands[comind].place);
929 }
930
931 static bool
932 setval_internal_tilde (int comind, const char *com, const char *val)
933 {
934 bool ret;
935 int homelen;
936 char **pstring;
937 ret = setval_internal (comind, com, val);
938
939 /* We make tilde expansion for cmd_file and cmd_directory */
940 if (ret && ((commands[comind].action == cmd_file) ||
941 (commands[comind].action == cmd_directory))
942 && (*val == '~' && ISSEP (val[1])))
943 {
944 pstring = commands[comind].place;
945 if (opt.homedir)
946 {
947 char *home = xstrdup(opt.homedir);
948 homelen = strlen (home);
949 while (homelen && ISSEP (home[homelen - 1]))
950 home[--homelen] = '\0';
951
952 xfree (*pstring);
953
954 /* Skip the leading "~/". */
955 val += strspn(val + 1, SEPSTRING) + 1;
956 *pstring = concat_strings (home, "/", val, (char *)0);
957 xfree (home);
958 }
959 }
960 return ret;
961 }
962
963 /* Run command COM with value VAL. If running the command produces an
964 error, report the error and exit.
965
966 This is intended to be called from main to modify Wget's behavior
967 through command-line switches. Since COM is hard-coded in main,
968 it is not canonicalized, and this aborts when COM is not found.
969
970 If COMIND's are exported to init.h, this function will be changed
971 to accept COMIND directly. */
972
973 void
974 setoptval (const char *com, const char *val, const char *optname)
975 {
976 /* Prepend "--" to OPTNAME. */
977 char dd_optname[2 + MAX_LONGOPTION + 1];
978
979 if ((unsigned) snprintf(dd_optname, sizeof (dd_optname), "--%s", optname) > sizeof (dd_optname))
980 exit (WGET_EXIT_PARSE_ERROR);
981
982 assert (val != NULL);
983
984 if (!setval_internal (command_by_name (com), dd_optname, val))
985 exit (WGET_EXIT_PARSE_ERROR);
986 }
987
988 /* Parse OPT into command and value and run it. For example,
989 run_command("foo=bar") is equivalent to setoptval("foo", "bar").
990 This is used by the `--execute' flag in main.c. */
991
992 void
993 run_command (const char *cmdopt)
994 {
995 char *com, *val;
996 int comind;
997 switch (parse_line (cmdopt, &com, &val, &comind))
998 {
999 case line_ok:
1000 if (!setval_internal (comind, com, val))
1001 exit (WGET_EXIT_PARSE_ERROR);
1002 xfree (com);
1003 xfree (val);
1004 break;
1005 default:
1006 fprintf (stderr, _("%s: Invalid --execute command %s\n"),
1007 exec_name, quote (cmdopt));
1008 exit (WGET_EXIT_PARSE_ERROR);
1009 }
1010 }
1011
1012 /* Generic helper functions, for use with `commands'. */
1013
1014 /* Forward declarations: */
1015 struct decode_item {
1016 const char *name;
1017 int code;
1018 };
1019 static bool decode_string (const char *, const struct decode_item *, int, int *);
1020 static bool simple_atof (const char *, const char *, double *);
1021
1022 #define CMP1(p, c0) (c_tolower((p)[0]) == (c0) && (p)[1] == '\0')
1023
1024 #define CMP2(p, c0, c1) (c_tolower((p)[0]) == (c0) \
1025 && c_tolower((p)[1]) == (c1) \
1026 && (p)[2] == '\0')
1027
1028 #define CMP3(p, c0, c1, c2) (c_tolower((p)[0]) == (c0) \
1029 && c_tolower((p)[1]) == (c1) \
1030 && c_tolower((p)[2]) == (c2) \
1031 && (p)[3] == '\0')
1032
1033
1034 static int
1035 cmd_boolean_internal (const char *com _GL_UNUSED, const char *val, void *place _GL_UNUSED)
1036 {
1037 if (CMP2 (val, 'o', 'n') || CMP3 (val, 'y', 'e', 's') || CMP1 (val, '1'))
1038 /* "on", "yes" and "1" mean true. */
1039 return 1;
1040 else if (CMP3 (val, 'o', 'f', 'f') || CMP2 (val, 'n', 'o') || CMP1 (val, '0'))
1041 /* "off", "no" and "0" mean false. */
1042 return 0;
1043 return -1;
1044 }
1045
1046 /* Store the boolean value from VAL to PLACE. COM is ignored,
1047 except for error messages. */
1048 static bool
1049 cmd_boolean (const char *com, const char *val, void *place)
1050 {
1051 bool value;
1052
1053 switch (cmd_boolean_internal (com, val, place))
1054 {
1055 case 0:
1056 value = false;
1057 break;
1058
1059 case 1:
1060 value = true;
1061 break;
1062
1063 default:
1064 {
1065 fprintf (stderr,
1066 _("%s: %s: Invalid boolean %s; use `on' or `off'.\n"),
1067 exec_name, com, quote (val));
1068 return false;
1069 }
1070 }
1071 *(bool *) place = value;
1072 return true;
1073 }
1074
1075 /* Store the check_cert value from VAL to PLACE. COM is ignored,
1076 except for error messages. */
1077 static bool
1078 cmd_check_cert (const char *com, const char *val, void *place)
1079 {
1080 int value;
1081
1082 switch (cmd_boolean_internal (com, val, place))
1083 {
1084 case 0:
1085 value = CHECK_CERT_OFF;
1086 break;
1087
1088 case 1:
1089 value = CHECK_CERT_ON;
1090 break;
1091
1092 default:
1093 {
1094 if (!c_strcasecmp (val, "quiet"))
1095 value = CHECK_CERT_QUIET;
1096 else
1097 {
1098 fprintf (stderr,
1099 _("%s: %s: Invalid %s; use `on', `off' or `quiet'.\n"),
1100 exec_name, com, quote (val));
1101 return false;
1102 }
1103 }
1104 }
1105 *(int *) place = value;
1106 return true;
1107 }
1108
1109 /* Set the non-negative integer value from VAL to PLACE. With
1110 incorrect specification, the number remains unchanged. */
1111 static bool
1112 cmd_number (const char *com, const char *val, void *place)
1113 {
1114 long l = strtol(val, NULL, 10);
1115
1116 if (((l == LONG_MIN || l == LONG_MAX) && errno == ERANGE)
1117 || l < 0 || l > INT_MAX)
1118 {
1119 fprintf (stderr, _("%s: %s: Invalid number %s.\n"),
1120 exec_name, com, quote (val));
1121 return false;
1122 }
1123 *(int *) place = (int) l;
1124 return true;
1125 }
1126
1127 /* Similar to cmd_number(), only accepts `inf' as a synonym for 0. */
1128 static bool
1129 cmd_number_inf (const char *com, const char *val, void *place)
1130 {
1131 if (!c_strcasecmp (val, "inf"))
1132 {
1133 *(int *) place = 0;
1134 return true;
1135 }
1136 return cmd_number (com, val, place);
1137 }
1138
1139 /* Copy (strdup) the string at COM to a new location and place a
1140 pointer to *PLACE. */
1141 static bool
1142 cmd_string (const char *com _GL_UNUSED, const char *val, void *place)
1143 {
1144 char **pstring = (char **)place;
1145
1146 xfree (*pstring);
1147 *pstring = xstrdup (val);
1148 return true;
1149 }
1150
1151 /* Like cmd_string but ensure the string is upper case. */
1152 static bool
1153 cmd_string_uppercase (const char *com _GL_UNUSED, const char *val, void *place)
1154 {
1155 char *q, **pstring;
1156 pstring = (char **)place;
1157 xfree (*pstring);
1158
1159 *pstring = xmalloc (strlen (val) + 1);
1160
1161 for (q = *pstring; *val; val++, q++)
1162 *q = c_toupper (*val);
1163
1164 *q = '\0';
1165 return true;
1166 }
1167
1168
1169 /* Like cmd_string, but handles tilde-expansion when reading a user's
1170 `.wgetrc'. In that case, and if VAL begins with `~', the tilde
1171 gets expanded to the user's home directory. */
1172 static bool
1173 cmd_file (const char *com _GL_UNUSED, const char *val, void *place)
1174 {
1175 char **pstring = (char **)place;
1176
1177 xfree (*pstring);
1178
1179 /* #### If VAL is empty, perhaps should set *PLACE to NULL. */
1180
1181 *pstring = xstrdup (val);
1182
1183 #if defined(WINDOWS) || defined(MSDOS)
1184 /* Convert "\" to "/". */
1185 {
1186 char *s;
1187 for (s = *pstring; *s; s++)
1188 if (*s == '\\')
1189 *s = '/';
1190 }
1191 #endif
1192 return true;
1193 }
1194
1195 /* like cmd_file, but insist on just a single option usage */
1196 static bool
1197 cmd_file_once (const char *com _GL_UNUSED, const char *val, void *place)
1198 {
1199 if (*(char **)place)
1200 {
1201 fprintf (stderr, _("%s: %s must only be used once\n"),
1202 exec_name, com);
1203 return false;
1204 }
1205
1206 return cmd_file(com, val, place);
1207 }
1208
1209 /* Like cmd_file, but strips trailing '/' characters. */
1210 static bool
1211 cmd_directory (const char *com, const char *val, void *place)
1212 {
1213 char *s, *t;
1214
1215 /* Call cmd_file() for tilde expansion and separator
1216 canonicalization (backslash -> slash under Windows). These
1217 things should perhaps be in a separate function. */
1218 if (!cmd_file (com, val, place))
1219 return false;
1220
1221 s = *(char **)place;
1222 t = s + strlen (s);
1223 while (t > s && *--t == '/')
1224 *t = '\0';
1225
1226 return true;
1227 }
1228
1229 /* Split VAL by space to a vector of values, and append those values
1230 to vector pointed to by the PLACE argument. If VAL is empty, the
1231 PLACE vector is cleared instead. */
1232
1233 static bool
1234 cmd_vector (const char *com _GL_UNUSED, const char *val, void *place)
1235 {
1236 char ***pvec = (char ***)place;
1237
1238 if (*val)
1239 *pvec = merge_vecs (*pvec, sepstring (val));
1240 else
1241 {
1242 free_vec (*pvec);
1243 *pvec = NULL;
1244 }
1245 return true;
1246 }
1247
1248 static bool
1249 cmd_directory_vector (const char *com _GL_UNUSED, const char *val, void *place)
1250 {
1251 char ***pvec = (char ***)place;
1252
1253 if (*val)
1254 {
1255 /* Strip the trailing slashes from directories. */
1256 char **t, **seps;
1257
1258 seps = sepstring (val);
1259 for (t = seps; t && *t; t++)
1260 {
1261 int len = strlen (*t);
1262 /* Skip degenerate case of root directory. */
1263 if (len > 1)
1264 {
1265 if ((*t)[len - 1] == '/')
1266 (*t)[len - 1] = '\0';
1267 }
1268 }
1269 *pvec = merge_vecs (*pvec, seps);
1270 }
1271 else
1272 {
1273 free_vec (*pvec);
1274 *pvec = NULL;
1275 }
1276 return true;
1277 }
1278
1279 /* Engine for cmd_bytes and cmd_bytes_sum: converts a string such as
1280 "100k" or "2.5G" to a floating point number. */
1281
1282 static bool
1283 parse_bytes_helper (const char *val, double *result)
1284 {
1285 double number, mult;
1286 const char *end = val + strlen (val);
1287
1288 /* Check for "inf". */
1289 if (0 == strcmp (val, "inf"))
1290 {
1291 *result = 0;
1292 return true;
1293 }
1294
1295 /* Strip trailing whitespace. */
1296 while (val < end && c_isspace (end[-1]))
1297 --end;
1298 if (val == end)
1299 return false;
1300
1301 switch (c_tolower (end[-1]))
1302 {
1303 case 'k':
1304 --end, mult = 1024.0;
1305 break;
1306 case 'm':
1307 --end, mult = 1048576.0;
1308 break;
1309 case 'g':
1310 --end, mult = 1073741824.0;
1311 break;
1312 case 't':
1313 --end, mult = 1099511627776.0;
1314 break;
1315 default:
1316 /* Not a recognized suffix: assume it's a digit. (If not,
1317 simple_atof will raise an error.) */
1318 mult = 1;
1319 }
1320
1321 /* Skip leading and trailing whitespace. */
1322 while (val < end && c_isspace (*val))
1323 ++val;
1324 while (val < end && c_isspace (end[-1]))
1325 --end;
1326 if (val == end)
1327 return false;
1328
1329 if (!simple_atof (val, end, &number) || number < 0)
1330 return false;
1331
1332 *result = number * mult;
1333 return true;
1334 }
1335
1336 /* Parse VAL as a number and set its value to PLACE (which should
1337 point to a wgint).
1338
1339 By default, the value is assumed to be in bytes. If "K", "M", or
1340 "G" are appended, the value is multiplied with 1<<10, 1<<20, or
1341 1<<30, respectively. Floating point values are allowed and are
1342 cast to integer before use. The idea is to be able to use things
1343 like 1.5k instead of "1536".
1344
1345 The string "inf" is returned as 0.
1346
1347 In case of error, false is returned and memory pointed to by PLACE
1348 remains unmodified. */
1349
1350 static bool
1351 cmd_bytes (const char *com, const char *val, void *place)
1352 {
1353 double byte_value;
1354 if (!parse_bytes_helper (val, &byte_value))
1355 {
1356 fprintf (stderr, _("%s: %s: Invalid byte value %s\n"),
1357 exec_name, com, quote (val));
1358 return false;
1359 }
1360 *(wgint *)place = (wgint)byte_value;
1361 return true;
1362 }
1363
1364 /* Like cmd_bytes, but PLACE is interpreted as a pointer to
1365 SIZE_SUM. It works by converting the string to double, therefore
1366 working with values up to 2^53-1 without loss of precision. This
1367 value (8192 TB) is large enough to serve for a while. */
1368
1369 static bool
1370 cmd_bytes_sum (const char *com, const char *val, void *place)
1371 {
1372 double byte_value;
1373
1374 if (!parse_bytes_helper (val, &byte_value)
1375 || byte_value < WGINT_MIN || byte_value > WGINT_MAX)
1376 {
1377 fprintf (stderr, _("%s: %s: Invalid byte value %s\n"),
1378 exec_name, com, quote (val));
1379 return false;
1380 }
1381 *(wgint *) place = (wgint) byte_value;
1382 return true;
1383 }
1384
1385 /* Store the value of VAL to *OUT. The value is a time period, by
1386 default expressed in seconds, but also accepting suffixes "m", "h",
1387 "d", and "w" for minutes, hours, days, and weeks respectively. */
1388
1389 static bool
1390 cmd_time (const char *com, const char *val, void *place)
1391 {
1392 double number, mult;
1393 const char *end = val + strlen (val);
1394
1395 /* Strip trailing whitespace. */
1396 while (val < end && c_isspace (end[-1]))
1397 --end;
1398
1399 if (val == end)
1400 {
1401 err:
1402 fprintf (stderr, _("%s: %s: Invalid time period %s\n"),
1403 exec_name, com, quote (val));
1404 return false;
1405 }
1406
1407 switch (c_tolower (end[-1]))
1408 {
1409 case 's':
1410 --end, mult = 1; /* seconds */
1411 break;
1412 case 'm':
1413 --end, mult = 60; /* minutes */
1414 break;
1415 case 'h':
1416 --end, mult = 3600; /* hours */
1417 break;
1418 case 'd':
1419 --end, mult = 86400.0; /* days */
1420 break;
1421 case 'w':
1422 --end, mult = 604800.0; /* weeks */
1423 break;
1424 default:
1425 /* Not a recognized suffix: assume it belongs to the number.
1426 (If not, simple_atof will raise an error.) */
1427 mult = 1;
1428 }
1429
1430 /* Skip leading and trailing whitespace. */
1431 while (val < end && c_isspace (*val))
1432 ++val;
1433 while (val < end && c_isspace (end[-1]))
1434 --end;
1435 if (val == end)
1436 goto err;
1437
1438 if (!simple_atof (val, end, &number))
1439 goto err;
1440
1441 if (number < 0)
1442 {
1443 fprintf (stderr, _("%s: %s: Negative time period %s\n"),
1444 exec_name, com, quote (val));
1445 return false;
1446 }
1447
1448 *(double *)place = number * mult;
1449 return true;
1450 }
1451
1452
1453 static bool
1454 cmd_use_askpass (const char *com _GL_UNUSED, const char *val, void *place)
1455 {
1456 const char *env_name = "WGET_ASKPASS";
1457 const char *env;
1458
1459 if (val && *val)
1460 return cmd_string (com, val, place);
1461
1462 env = getenv (env_name);
1463 if (!(env && *env))
1464 {
1465 env_name = "SSH_ASKPASS";
1466 env = getenv (env_name);
1467 }
1468
1469 if (!(env && *env))
1470 {
1471 fprintf (stderr, _("use-askpass requires a string or either environment variable WGET_ASKPASS or SSH_ASKPASS to be set.\n"));
1472 return false;
1473 }
1474
1475 return cmd_string (com, env, place);
1476 }
1477
1478 #ifdef HAVE_SSL
1479 static bool
1480 cmd_cert_type (const char *com, const char *val, void *place)
1481 {
1482 static const struct decode_item choices[] = {
1483 { "pem", keyfile_pem },
1484 { "der", keyfile_asn1 },
1485 { "asn1", keyfile_asn1 },
1486 };
1487 int ok = decode_string (val, choices, countof (choices), place);
1488 if (!ok)
1489 fprintf (stderr, _("%s: %s: Invalid value %s.\n"), exec_name, com, quote (val));
1490 return ok;
1491 }
1492 #endif
1493
1494 /* Specialized helper functions, used by `commands' to handle some
1495 options specially. */
1496
1497 static bool check_user_specified_header (const char *);
1498
1499 #ifdef HAVE_LIBZ
1500 static bool
1501 cmd_spec_compression (const char *com, const char *val, void *place)
1502 {
1503 static const struct decode_item choices[] = {
1504 { "auto", compression_auto },
1505 { "gzip", compression_gzip },
1506 { "none", compression_none },
1507 };
1508 int ok = decode_string (val, choices, countof (choices), place);
1509 if (!ok)
1510 {
1511 fprintf (stderr, _("%s: %s: Invalid value %s.\n"), exec_name, com,
1512 quote (val));
1513 }
1514 return ok;
1515 }
1516 #endif
1517
1518 static bool
1519 cmd_spec_dirstruct (const char *com, const char *val, void *place_ignored _GL_UNUSED)
1520 {
1521 if (!cmd_boolean (com, val, &opt.dirstruct))
1522 return false;
1523 /* Since dirstruct behaviour is explicitly changed, no_dirstruct
1524 must be affected inversely. */
1525 if (opt.dirstruct)
1526 opt.no_dirstruct = false;
1527 else
1528 opt.no_dirstruct = true;
1529 return true;
1530 }
1531
1532 static bool
1533 cmd_spec_header (const char *com, const char *val, void *place_ignored _GL_UNUSED)
1534 {
1535 /* Empty value means reset the list of headers. */
1536 if (*val == '\0')
1537 {
1538 free_vec (opt.user_headers);
1539 opt.user_headers = NULL;
1540 return true;
1541 }
1542
1543 if (!check_user_specified_header (val))
1544 {
1545 fprintf (stderr, _("%s: %s: Invalid header %s.\n"),
1546 exec_name, com, quote (val));
1547 return false;
1548 }
1549 opt.user_headers = vec_append (opt.user_headers, val);
1550 return true;
1551 }
1552
1553 static bool
1554 cmd_spec_warc_header (const char *com, const char *val, void *place_ignored _GL_UNUSED)
1555 {
1556 /* Empty value means reset the list of headers. */
1557 if (*val == '\0')
1558 {
1559 free_vec (opt.warc_user_headers);
1560 opt.warc_user_headers = NULL;
1561 return true;
1562 }
1563
1564 if (!check_user_specified_header (val))
1565 {
1566 fprintf (stderr, _("%s: %s: Invalid WARC header %s.\n"),
1567 exec_name, com, quote (val));
1568 return false;
1569 }
1570 opt.warc_user_headers = vec_append (opt.warc_user_headers, val);
1571 return true;
1572 }
1573
1574 static bool
1575 cmd_spec_htmlify (const char *com, const char *val, void *place_ignored _GL_UNUSED)
1576 {
1577 int flag = cmd_boolean (com, val, &opt.htmlify);
1578 if (flag && !opt.htmlify)
1579 opt.remove_listing = false;
1580 return flag;
1581 }
1582
1583 /* Set the "mirror" mode. It means: recursive download, timestamping,
1584 no limit on max. recursion depth, and don't remove listings. */
1585
1586 static bool
1587 cmd_spec_mirror (const char *com, const char *val, void *place_ignored _GL_UNUSED)
1588 {
1589 bool mirror;
1590
1591 if (!cmd_boolean (com, val, &mirror))
1592 return false;
1593 if (mirror)
1594 {
1595 opt.recursive = true;
1596 if (!opt.no_dirstruct)
1597 opt.dirstruct = true;
1598 opt.timestamping = true;
1599 opt.reclevel = INFINITE_RECURSION;
1600 opt.remove_listing = false;
1601 }
1602 return true;
1603 }
1604
1605 /* Validate --prefer-family and set the choice. Allowed values are
1606 "IPv4", "IPv6", and "none". */
1607
1608 static bool
1609 cmd_spec_prefer_family (const char *com, const char *val, void *place_ignored _GL_UNUSED)
1610 {
1611 static const struct decode_item choices[] = {
1612 { "IPv4", prefer_ipv4 },
1613 { "IPv6", prefer_ipv6 },
1614 { "none", prefer_none },
1615 };
1616 int prefer_family = prefer_none;
1617 int ok = decode_string (val, choices, countof (choices), &prefer_family);
1618 if (!ok)
1619 fprintf (stderr, _("%s: %s: Invalid value %s.\n"), exec_name, com, quote (val));
1620 opt.prefer_family = prefer_family;
1621 return ok;
1622 }
1623
1624 /* Set progress.type to VAL, but verify that it's a valid progress
1625 implementation before that. */
1626
1627 static bool
1628 cmd_spec_progress (const char *com, const char *val, void *place_ignored _GL_UNUSED)
1629 {
1630 if (!valid_progress_implementation_p (val))
1631 {
1632 fprintf (stderr, _("%s: %s: Invalid progress type %s.\n"),
1633 exec_name, com, quote (val));
1634 return false;
1635 }
1636 xfree (opt.progress_type);
1637
1638 /* Don't call set_progress_implementation here. It will be called
1639 in main when it becomes clear what the log output is. */
1640 opt.progress_type = xstrdup (val);
1641 return true;
1642 }
1643
1644 /* Set opt.recursive to VAL as with cmd_boolean. If opt.recursive is
1645 set to true, also set opt.dirstruct to true, unless opt.no_dirstruct
1646 is specified. */
1647
1648 static bool
1649 cmd_spec_recursive (const char *com, const char *val, void *place_ignored _GL_UNUSED)
1650 {
1651 if (!cmd_boolean (com, val, &opt.recursive))
1652 return false;
1653 else
1654 {
1655 if (opt.recursive && !opt.no_dirstruct)
1656 opt.dirstruct = true;
1657 }
1658 return true;
1659 }
1660
1661 /* Validate --regex-type and set the choice. */
1662
1663 static bool
1664 cmd_spec_regex_type (const char *com, const char *val, void *place_ignored _GL_UNUSED)
1665 {
1666 static const struct decode_item choices[] = {
1667 { "posix", regex_type_posix },
1668 #if defined HAVE_LIBPCRE || defined HAVE_LIBPCRE2
1669 { "pcre", regex_type_pcre },
1670 #endif
1671 };
1672 int regex_type = regex_type_posix;
1673 int ok = decode_string (val, choices, countof (choices), &regex_type);
1674 if (!ok)
1675 fprintf (stderr, _("%s: %s: Invalid value %s.\n"), exec_name, com, quote (val));
1676 opt.regex_type = regex_type;
1677 return ok;
1678 }
1679
1680 static bool
1681 cmd_spec_restrict_file_names (const char *com, const char *val, void *place_ignored _GL_UNUSED)
1682 {
1683 int restrict_os = opt.restrict_files_os;
1684 int restrict_ctrl = opt.restrict_files_ctrl;
1685 int restrict_case = opt.restrict_files_case;
1686 int restrict_nonascii = opt.restrict_files_nonascii;
1687
1688 const char *end;
1689
1690 #define VAL_IS(string_literal) BOUNDED_EQUAL (val, end, string_literal)
1691
1692 do
1693 {
1694 end = strchr (val, ',');
1695 if (!end)
1696 end = val + strlen (val);
1697
1698 if (VAL_IS ("unix"))
1699 restrict_os = restrict_unix;
1700 else if (VAL_IS ("vms"))
1701 restrict_os = restrict_vms;
1702 else if (VAL_IS ("windows"))
1703 restrict_os = restrict_windows;
1704 else if (VAL_IS ("lowercase"))
1705 restrict_case = restrict_lowercase;
1706 else if (VAL_IS ("uppercase"))
1707 restrict_case = restrict_uppercase;
1708 else if (VAL_IS ("nocontrol"))
1709 restrict_ctrl = false;
1710 else if (VAL_IS ("ascii"))
1711 restrict_nonascii = true;
1712 else
1713 {
1714 fprintf (stderr, _("\
1715 %s: %s: Invalid restriction %s,\n\
1716 use [unix|vms|windows],[lowercase|uppercase],[nocontrol],[ascii].\n"),
1717 exec_name, com, quote (val));
1718 return false;
1719 }
1720
1721 if (*end)
1722 val = end + 1;
1723 }
1724 while (*val && *end);
1725
1726 #undef VAL_IS
1727
1728 opt.restrict_files_os = restrict_os;
1729 opt.restrict_files_ctrl = restrict_ctrl;
1730 opt.restrict_files_case = restrict_case;
1731 opt.restrict_files_nonascii = restrict_nonascii;
1732
1733 return true;
1734 }
1735
1736 static bool
1737 cmd_spec_report_speed (const char *com, const char *val, void *place_ignored _GL_UNUSED)
1738 {
1739 opt.report_bps = c_strcasecmp (val, "bits") == 0;
1740 if (!opt.report_bps)
1741 fprintf (stderr, _("%s: %s: Invalid value %s.\n"), exec_name, com, quote (val));
1742 return opt.report_bps;
1743 }
1744
1745 #ifdef HAVE_SSL
1746 static bool
1747 cmd_spec_secure_protocol (const char *com, const char *val, void *place)
1748 {
1749 static const struct decode_item choices[] = {
1750 { "auto", secure_protocol_auto },
1751 { "sslv2", secure_protocol_sslv2 },
1752 { "sslv3", secure_protocol_sslv3 },
1753 { "tlsv1", secure_protocol_tlsv1 },
1754 { "tlsv1_1", secure_protocol_tlsv1_1 },
1755 { "tlsv1_2", secure_protocol_tlsv1_2 },
1756 { "tlsv1_3", secure_protocol_tlsv1_3 },
1757 { "pfs", secure_protocol_pfs },
1758 };
1759 snprintf (opt.secure_protocol_name, sizeof (opt.secure_protocol_name), "%s", val);
1760 int ok = decode_string (val, choices, countof (choices), place);
1761 if (!ok)
1762 fprintf (stderr, _("%s: %s: Invalid value %s.\n"), exec_name, com, quote (val));
1763 return ok;
1764 }
1765 #endif
1766
1767 /* Set all three timeout values. */
1768
1769 static bool
1770 cmd_spec_timeout (const char *com, const char *val, void *place_ignored _GL_UNUSED)
1771 {
1772 double value;
1773 if (!cmd_time (com, val, &value))
1774 return false;
1775 opt.read_timeout = value;
1776 opt.connect_timeout = value;
1777 opt.dns_timeout = value;
1778 return true;
1779 }
1780
1781 static bool
1782 cmd_spec_useragent (const char *com, const char *val, void *place_ignored _GL_UNUSED)
1783 {
1784 /* Disallow embedded newlines. */
1785 if (strchr (val, '\n'))
1786 {
1787 fprintf (stderr, _("%s: %s: Invalid value %s.\n"),
1788 exec_name, com, quote (val));
1789 return false;
1790 }
1791 xfree (opt.useragent);
1792 opt.useragent = xstrdup (val);
1793 return true;
1794 }
1795
1796 /* The --show-progress option is not a cmd_boolean since we need to keep track
1797 * of whether the user explicitly requested the option or not. -1 means
1798 * uninitialized. */
1799 static bool
1800 cmd_spec_progressdisp (const char *com, const char *val, void *place _GL_UNUSED)
1801 {
1802 bool flag;
1803 if (cmd_boolean (com, val, &flag))
1804 {
1805 opt.show_progress = flag;
1806 return true;
1807 }
1808 return false;
1809 }
1810
1811
1812 /* The "verbose" option cannot be cmd_boolean because the variable is
1813 not bool -- it's of type int (-1 means uninitialized because of
1814 some random hackery for disallowing -q -v). */
1815
1816 static bool
1817 cmd_spec_verbose (const char *com, const char *val, void *place_ignored _GL_UNUSED)
1818 {
1819 bool flag;
1820 if (cmd_boolean (com, val, &flag))
1821 {
1822 opt.verbose = flag;
1823 opt.show_progress = -1;
1824 return true;
1825 }
1826 return false;
1827 }
1828
1829 /* Miscellaneous useful routines. */
1830
1831 /* Trivial atof, with error reporting. Handles "<digits>[.<digits>]",
1832 doesn't handle exponential notation. Returns true on success,
1833 false on failure. In case of success, stores its result to
1834 *DEST. */
1835
1836 static bool
1837 simple_atof (const char *beg, const char *end, double *dest)
1838 {
1839 double result = 0;
1840
1841 bool negative = false;
1842 bool seen_dot = false;
1843 bool seen_digit = false;
1844 double divider = 1;
1845
1846 const char *p = beg;
1847
1848 while (p < end && c_isspace (*p))
1849 ++p;
1850 if (p < end && (*p == '-' || *p == '+'))
1851 {
1852 negative = (*p == '-');
1853 ++p;
1854 }
1855
1856 for (; p < end; p++)
1857 {
1858 char ch = *p;
1859 if (c_isdigit (ch))
1860 {
1861 if (!seen_dot)
1862 result = (10 * result) + (ch - '0');
1863 else
1864 result += (ch - '0') / (divider *= 10);
1865 seen_digit = true;
1866 }
1867 else if (ch == '.')
1868 {
1869 if (!seen_dot)
1870 seen_dot = true;
1871 else
1872 return false;
1873 }
1874 else
1875 return false;
1876 }
1877 if (!seen_digit)
1878 return false;
1879 if (negative)
1880 result = -result;
1881
1882 *dest = result;
1883 return true;
1884 }
1885
1886 /* Verify that the user-specified header in S is valid. It must
1887 contain a colon preceded by non-white-space characters and must not
1888 contain newlines. */
1889
1890 static bool
1891 check_user_specified_header (const char *s)
1892 {
1893 const char *p;
1894
1895 for (p = s; *p && *p != ':' && !c_isspace (*p); p++)
1896 ;
1897 /* The header MUST contain `:' preceded by at least one
1898 non-whitespace character. */
1899 if (*p != ':' || p == s)
1900 return false;
1901 /* The header MUST NOT contain newlines. */
1902 if (strchr (s, '\n'))
1903 return false;
1904 return true;
1905 }
1906
1907 /* Decode VAL into a number, according to ITEMS. */
1908
1909 static bool
1910 decode_string (const char *val, const struct decode_item *items, int itemcount,
1911 int *place)
1912 {
1913 int i;
1914 for (i = 0; i < itemcount; i++)
1915 if (0 == c_strcasecmp (val, items[i].name))
1916 {
1917 *place = items[i].code;
1918 return true;
1919 }
1920 return false;
1921 }
1922
1923 extern struct ptimer *timer;
1924 extern int cleaned_up;
1925
1926 /* Free the memory allocated by global variables. */
1927 void
1928 cleanup (void)
1929 {
1930 /* Free external resources, close files, etc. */
1931
1932 if (cleaned_up++)
1933 return; /* cleanup() must not be called twice */
1934
1935 /* Close WARC file. */
1936 if (opt.warc_filename != 0)
1937 warc_close ();
1938
1939 log_close ();
1940
1941 if (output_stream && output_stream != stderr && output_stream != stdout)
1942 {
1943 FILE *fp = output_stream;
1944 output_stream = NULL;
1945 if (fclose (fp) == EOF)
1946 inform_exit_status (CLOSEFAILED);
1947 }
1948
1949 /* No need to check for error because Wget flushes its output (and
1950 checks for errors) after any data arrives. */
1951
1952 /* We're exiting anyway so there's no real need to call free()
1953 hundreds of times. Skipping the frees will make Wget exit
1954 faster.
1955 *
1956 However, when detecting leaks, it's crucial to free() everything
1957 because then you can find the real leaks, i.e. the allocated
1958 memory which grows with the size of the program. */
1959
1960 #if defined DEBUG_MALLOC || defined TESTING
1961 convert_cleanup ();
1962 res_cleanup ();
1963 http_cleanup ();
1964 cleanup_html_url ();
1965 spider_cleanup ();
1966 host_cleanup ();
1967 log_cleanup ();
1968 netrc_cleanup ();
1969 #ifdef HAVE_SSL
1970 ssl_cleanup ();
1971 #endif
1972 connect_cleanup ();
1973
1974 xfree (opt.choose_config);
1975 xfree (opt.lfilename);
1976 xfree (opt.dir_prefix);
1977 xfree (opt.input_filename);
1978 #ifdef HAVE_METALINK
1979 xfree (opt.input_metalink);
1980 xfree (opt.preferred_location);
1981 #endif
1982 xfree (opt.output_document);
1983 xfree (opt.default_page);
1984 if (opt.regex_type == regex_type_posix)
1985 {
1986 if (opt.acceptregex)
1987 regfree (opt.acceptregex);
1988 if (opt.rejectregex)
1989 regfree (opt.rejectregex);
1990 }
1991 xfree (opt.acceptregex);
1992 xfree (opt.rejectregex);
1993 xfree (opt.acceptregex_s);
1994 xfree (opt.rejectregex_s);
1995 free_vec (opt.accepts);
1996 free_vec (opt.rejects);
1997 free_vec ((char **)opt.excludes);
1998 free_vec ((char **)opt.includes);
1999 free_vec (opt.domains);
2000 free_vec (opt.exclude_domains);
2001 free_vec (opt.follow_tags);
2002 free_vec (opt.ignore_tags);
2003 xfree (opt.progress_type);
2004 xfree (opt.warc_filename);
2005 xfree (opt.warc_tempdir);
2006 xfree (opt.warc_cdx_dedup_filename);
2007 xfree (opt.ftp_user);
2008 xfree (opt.ftp_passwd);
2009 xfree (opt.ftp_proxy);
2010 xfree (opt.https_proxy);
2011 xfree (opt.http_proxy);
2012 free_vec (opt.no_proxy);
2013 xfree (opt.proxy_user);
2014 xfree (opt.proxy_passwd);
2015 xfree (opt.useragent);
2016 xfree (opt.referer);
2017 xfree (opt.http_user);
2018 xfree (opt.http_passwd);
2019 xfree (opt.dot_style);
2020 free_vec (opt.user_headers);
2021 free_vec (opt.warc_user_headers);
2022 # ifdef HAVE_SSL
2023 xfree (opt.cert_file);
2024 xfree (opt.private_key);
2025 xfree (opt.ca_directory);
2026 xfree (opt.ca_cert);
2027 xfree (opt.crl_file);
2028 xfree (opt.pinnedpubkey);
2029 xfree (opt.random_file);
2030 xfree (opt.egd_file);
2031 # endif
2032 xfree (opt.bind_address);
2033 xfree (opt.cookies_input);
2034 xfree (opt.cookies_output);
2035 xfree (opt.user);
2036 xfree (opt.passwd);
2037 xfree (opt.base_href);
2038 xfree (opt.method);
2039 xfree (opt.post_file_name);
2040 xfree (opt.post_data);
2041 xfree (opt.body_data);
2042 xfree (opt.body_file);
2043 xfree (opt.rejected_log);
2044 xfree (opt.use_askpass);
2045 xfree (opt.retry_on_http_error);
2046
2047 xfree (opt.encoding_remote);
2048 xfree (opt.locale);
2049 #ifdef HAVE_HSTS
2050 xfree (opt.hsts_file);
2051 #endif
2052
2053 xfree (opt.wgetrcfile);
2054 xfree (opt.homedir);
2055 xfree (exec_name);
2056 xfree (program_argstring);
2057 ptimer_destroy (timer); timer = NULL;
2058
2059 #ifdef HAVE_LIBCARES
2060 #include <ares.h>
2061 {
2062 extern ares_channel ares;
2063
2064 xfree (opt.bind_dns_address);
2065 xfree (opt.dns_servers);
2066 ares_destroy (ares);
2067 ares_library_cleanup ();
2068 }
2069 #endif
2070
2071 quotearg_free ();
2072
2073 #endif /* DEBUG_MALLOC || TESTING */
2074 }
2075
2076 /* Unit testing routines. */
2077
2078 #ifdef TESTING
2079
2080 const char *
2081 test_commands_sorted(void)
2082 {
2083 unsigned i;
2084
2085 for (i = 1; i < countof(commands); ++i)
2086 {
2087 if (c_strcasecmp (commands[i - 1].name, commands[i].name) > 0)
2088 {
2089 mu_assert ("FAILED", false);
2090 break;
2091 }
2092 }
2093 return NULL;
2094 }
2095
2096 const char *
2097 test_cmd_spec_restrict_file_names(void)
2098 {
2099 unsigned i;
2100 static const struct {
2101 const char *val;
2102 int expected_restrict_files_os;
2103 bool expected_restrict_files_ctrl;
2104 int expected_restrict_files_case;
2105 bool result;
2106 } test_array[] = {
2107 { "windows", restrict_windows, true, restrict_no_case_restriction, true },
2108 { "windows,", restrict_windows, true, restrict_no_case_restriction, true },
2109 { "windows,lowercase", restrict_windows, true, restrict_lowercase, true },
2110 { "unix,nocontrol,lowercase,", restrict_unix, false, restrict_lowercase, true },
2111 };
2112
2113 for (i = 0; i < countof(test_array); ++i)
2114 {
2115 bool res;
2116
2117 defaults();
2118 res = cmd_spec_restrict_file_names ("dummy", test_array[i].val, NULL);
2119
2120 /*
2121 fprintf (stderr, "test_cmd_spec_restrict_file_names: TEST %d\n", i); fflush (stderr);
2122 fprintf (stderr, "opt.restrict_files_os: %d\n", opt.restrict_files_os); fflush (stderr);
2123 fprintf (stderr, "opt.restrict_files_ctrl: %d\n", opt.restrict_files_ctrl); fflush (stderr);
2124 fprintf (stderr, "opt.restrict_files_case: %d\n", opt.restrict_files_case); fflush (stderr);
2125 */
2126 mu_assert ("test_cmd_spec_restrict_file_names: wrong result",
2127 res == test_array[i].result
2128 && (int) opt.restrict_files_os == test_array[i].expected_restrict_files_os
2129 && opt.restrict_files_ctrl == test_array[i].expected_restrict_files_ctrl
2130 && (int) opt.restrict_files_case == test_array[i].expected_restrict_files_case);
2131 }
2132
2133 return NULL;
2134 }
2135
2136 #endif /* TESTING */