1 /* Miscellaneous declarations.
2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation,
6 This file is part of GNU Wget.
8 GNU Wget is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 GNU Wget is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with Wget. If not, see <http://www.gnu.org/licenses/>.
21 Additional permission under GNU GPL version 3 section 7
23 If you modify this program, or any covered work, by linking or
24 combining it with the OpenSSL project's OpenSSL library (or a
25 modified version of that library), containing parts covered by the
26 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
27 grants you additional permission to convey the resulting work.
28 Corresponding Source for a non-source form of such a combination
29 shall include the source code for the parts of OpenSSL used as well
30 as that of the covered work. */
32 /* This file contains declarations that are universally useful and
33 those that don't fit elsewhere. It also includes sysdep.h which
34 includes some often-needed system includes, like the obnoxious
35 <time.h> inclusion. */
42 #if ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__)
46 /* Include these, so random files need not include them. */
49 /* Disable assertions when debug support is not compiled in. */
54 /* Is OpenSSL or GNUTLS available? */
55 #if defined HAVE_LIBSSL || defined HAVE_LIBSSL32 || defined HAVE_LIBGNUTLS
59 /* `gettext (FOO)' is long to write, so we use `_(FOO)'. If NLS is
60 unavailable, _(STRING) simply returns STRING. */
62 #define _(string) gettext (string)
64 /* A pseudo function call that serves as a marker for the automated
65 extraction of messages, but does not call gettext(). The run-time
66 translation is done at a different place in the code. The purpose
67 of the N_("...") call is to make the message snarfer aware that the
68 "..." string needs to be translated. STRING should be a string
69 literal. Concatenated strings and other string expressions won't
70 work. The macro's expansion is not parenthesized, so that it is
71 suitable as initializer for static 'char[]' or 'const char[]'
72 variables. -- explanation partly taken from GNU make. */
73 #define N_(string) string
79 #endif /* not ENABLE_NLS */
81 #if HAVE_WCWIDTH && HAVE_MBTOWC
82 # define USE_NLS_PROGRESS_BAR 1
84 /* Just to be a little paranoid about it. */
85 # undef USE_NLS_PROGRESS_BAR
88 /* I18N NOTE: You will notice that none of the DEBUGP messages are
89 marked as translatable. This is intentional, for a few reasons:
91 1) The debug messages are not meant for the users to look at, but
92 for the developers; as such, they should be considered more like
93 source comments than real program output.
95 2) The messages are numerous, and yet they are random and frivolous
96 ("double yuck!" and such). There would be a lot of work with no
99 3) Finally, the debug messages are meant to be a clue for me to
100 debug problems with Wget. If I get them in a language I don't
101 understand, debugging will become a new challenge of its own! */
103 /* locale independent replacement for ctype.h */
106 /* Conditionalize the use of GCC's __attribute__((format)) and
107 __builtin_expect features using macros. */
109 #if defined(__GNUC__) && __GNUC__ >= 3
110 # define GCC_FORMAT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
111 # define LIKELY(exp) __builtin_expect (!!(exp), 1)
112 # define UNLIKELY(exp) __builtin_expect ((exp), 0)
114 # define GCC_FORMAT_ATTR(a, b)
115 # define LIKELY(exp) (exp)
116 # define UNLIKELY(exp) (exp)
119 /* Execute the following statement if debugging is both enabled at
120 compile-time and requested at run-time; a no-op otherwise. */
123 # define IF_DEBUG if (UNLIKELY (opt.debug))
125 # define IF_DEBUG if (0)
128 /* Print ARGS if debugging is enabled and requested, otherwise do
129 nothing. This must be called with an extra level of parentheses
130 because it's not possible to pass a variable number of arguments to
131 a macro (in portable C89). ARGS are like arguments to printf. */
133 #define DEBUGP(args) do { IF_DEBUG { debug_logprintf args; } } while (0)
135 /* Pick an integer type large enough for file sizes, content lengths,
136 and such. Because today's files can be very large, it should be a
137 signed integer at least 64 bits wide. This can't be typedeffed to
138 off_t because: a) off_t is always 32-bit on Windows, and b) we
139 don't necessarily want to tie having a 64-bit type for internal
140 calculations to having LFS support. */
143 /* nothing to do, see mswindows.h */
144 #elif SIZEOF_LONG >= 8
145 /* long is large enough, so use it. */
147 # define SIZEOF_WGINT SIZEOF_LONG
148 #elif SIZEOF_LONG_LONG >= 8
149 /* long long is large enough and available, use that */
150 typedef long long wgint;
151 # define SIZEOF_WGINT SIZEOF_LONG_LONG
153 typedef int64_t wgint;
154 # define SIZEOF_WGINT 8
155 #elif SIZEOF_OFF_T >= 8
156 /* In case off_t is typedeffed to a large non-standard type that our
159 # define SIZEOF_WGINT SIZEOF_OFF_T
161 /* Fall back to using long, which is always available and in most
162 cases large enough. */
164 # define SIZEOF_WGINT SIZEOF_LONG
167 /* Pick a strtol-compatible function that will work with wgint. The
168 choices are strtol, strtoll, or our own implementation of strtoll
169 in cmpt.c, activated with NEED_STRTOLL. */
172 /* nothing to do, see mswindows.h */
173 #elif SIZEOF_WGINT == SIZEOF_LONG
174 # define str_to_wgint strtol
175 #elif SIZEOF_WGINT == SIZEOF_LONG_LONG
176 # define str_to_wgint strtoll
177 # ifndef HAVE_STRTOLL
178 # define NEED_STRTOLL
179 # define strtoll_type long long
182 /* wgint has a strange size; synthesize strtoll and use it. */
183 # define str_to_wgint strtoll
184 # define NEED_STRTOLL
185 # define strtoll_type wgint
188 #define WGINT_MAX TYPE_MAXIMUM (wgint)
190 /* Declare our strtoll replacement. */
192 strtoll_type strtoll (const char *, char **, int);
195 /* Now define a large numeric type useful for storing sizes of *sums*
196 of downloads, such as the value of the --quota option. This should
197 be a type able to hold 2G+ values even on systems without large
198 file support. (It is useful to limit Wget's download quota to say
199 10G even if a single file cannot be that large.)
201 To make sure we get the largest size possible, we use `double' on
202 systems without a 64-bit integral type. (Since it is used in very
203 few places in Wget, this is acceptable.) */
205 #if SIZEOF_WGINT >= 8
207 typedef wgint SUM_SIZE_INT;
209 /* On systems without LFS, use double, which buys us integers up to 2^53. */
210 typedef double SUM_SIZE_INT;
215 /* Everything uses this, so include them here directly. */
219 /* Likewise for logging functions. */
222 /* Likewise for quoting functions. */
224 #include "quotearg.h"
226 /* Likewise for struct iri definition */
229 /* Useful macros used across the code: */
231 /* The number of elements in an array. For example:
232 static char a[] = "foo"; -- countof(a) == 4 (note terminating \0)
233 int a[5] = {1, 2}; -- countof(a) == 5
234 char *a[] = { -- countof(a) == 3
237 #define countof(array) (sizeof (array) / sizeof ((array)[0]))
239 /* Zero out a value. */
240 #define xzero(x) memset (&(x), '\0', sizeof (x))
242 /* Convert an ASCII hex digit to the corresponding number between 0
243 and 15. H should be a hexadecimal digit that satisfies isxdigit;
244 otherwise, the result is undefined. */
245 #define XDIGIT_TO_NUM(h) ((h) < 'A' ? (h) - '0' : c_toupper (h) - 'A' + 10)
246 #define X2DIGITS_TO_NUM(h1, h2) ((XDIGIT_TO_NUM (h1) << 4) + XDIGIT_TO_NUM (h2))
248 /* The reverse of the above: convert a number in the [0, 16) range to
249 the ASCII representation of the corresponding hexadecimal digit.
250 `+ 0' is there so you can't accidentally use it as an lvalue. */
251 #define XNUM_TO_DIGIT(x) ("0123456789ABCDEF"[x] + 0)
252 #define XNUM_TO_digit(x) ("0123456789abcdef"[x] + 0)
254 /* Copy the data delimited with BEG and END to alloca-allocated
255 storage, and zero-terminate it. Arguments are evaluated only once,
256 in the order BEG, END, PLACE. */
257 #define BOUNDED_TO_ALLOCA(beg, end, place) do { \
258 const char *BTA_beg = (beg); \
259 int BTA_len = (end) - BTA_beg; \
260 char **BTA_dest = &(place); \
261 *BTA_dest = alloca (BTA_len + 1); \
262 memcpy (*BTA_dest, BTA_beg, BTA_len); \
263 (*BTA_dest)[BTA_len] = '\0'; \
266 /* Return non-zero if string bounded between BEG and END is equal to
267 STRING_LITERAL. The comparison is case-sensitive. */
268 #define BOUNDED_EQUAL(beg, end, string_literal) \
269 ((end) - (beg) == sizeof (string_literal) - 1 \
270 && !memcmp (beg, string_literal, sizeof (string_literal) - 1))
272 /* The same as above, except the comparison is case-insensitive. */
273 #define BOUNDED_EQUAL_NO_CASE(beg, end, string_literal) \
274 ((end) - (beg) == sizeof (string_literal) - 1 \
275 && !strncasecmp (beg, string_literal, sizeof (string_literal) - 1))
277 /* Like ptr=strdup(str), but allocates the space for PTR on the stack.
278 This cannot be an expression because this is not portable:
279 #define STRDUP_ALLOCA(str) (strcpy (alloca (strlen (str) + 1), str))
280 The problem is that some compilers can't handle alloca() being an
281 argument to a function. */
283 #define STRDUP_ALLOCA(ptr, str) do { \
284 char **SA_dest = &(ptr); \
285 const char *SA_src = (str); \
286 *SA_dest = (char *)alloca (strlen (SA_src) + 1); \
287 strcpy (*SA_dest, SA_src); \
290 /* Generally useful if you want to avoid arbitrary size limits but
291 don't need a full dynamic array. Assumes that BASEVAR points to a
292 malloced array of TYPE objects (or possibly a NULL pointer, if
293 SIZEVAR is 0), with the total size stored in SIZEVAR. This macro
294 will realloc BASEVAR as necessary so that it can hold at least
295 NEEDED_SIZE objects. The reallocing is done by doubling, which
296 ensures constant amortized time per element. */
298 #define DO_REALLOC(basevar, sizevar, needed_size, type) do { \
299 long DR_needed_size = (needed_size); \
300 long DR_newsize = 0; \
301 while ((sizevar) < (DR_needed_size)) { \
302 DR_newsize = sizevar << 1; \
303 if (DR_newsize < 16) \
305 (sizevar) = DR_newsize; \
308 basevar = xrealloc (basevar, DR_newsize * sizeof (type)); \
311 /* Used to print pointers (usually for debugging). Print pointers
312 using printf ("0x%0*lx", PTR_FORMAT (p)). (%p is too unpredictable;
313 some implementations prepend 0x, while some don't, and most don't
314 0-pad the address.) */
315 #define PTR_FORMAT(p) (int) (2 * sizeof (void *)), (unsigned long) (p)
317 extern const char *exec_name;
319 /* Document type ("dt") flags */
322 TEXTHTML = 0x0001, /* document is of type text/html
323 or application/xhtml+xml */
324 RETROKF = 0x0002, /* retrieval was OK */
325 HEAD_ONLY = 0x0004, /* only send the HEAD request */
326 SEND_NOCACHE = 0x0008, /* send Pragma: no-cache directive */
327 ACCEPTRANGES = 0x0010, /* Accept-ranges header was found */
328 ADDED_HTML_EXTENSION = 0x0020, /* added ".html" extension due to -E */
329 TEXTCSS = 0x0040 /* document is of type text/css */
332 /* Universal error type -- used almost everywhere. Error reporting of
333 this detail is not generally used or needed and should be
338 NOCONERROR, HOSTERR, CONSOCKERR, CONERROR, CONSSLERR,
339 CONIMPOSSIBLE, NEWLOCATION, NOTENOUGHMEM /* ! */,
340 CONPORTERR /* ! */, CONCLOSED /* ! */,
342 FTPOK, FTPLOGINC, FTPLOGREFUSED, FTPPORTERR, FTPSYSERR,
343 FTPNSFOD, FTPRETROK /* ! */, FTPUNKNOWNTYPE, FTPRERR, FTPREXC /* ! */,
345 FTPSRVERR, FTPRETRINT, FTPRESTFAIL, URLERROR, FOPENERR,
346 FOPEN_EXCL_ERR, FWRITEERR, HOK /* ! */, HLEXC /* ! */, HEOF,
348 HERR, RETROK, RECLEVELEXC, FTPACCDENIED /* ! */, WRONGCODE,
349 FTPINVPASV, FTPNOPASV, CONTNOTSUPPORTED, RETRUNNEEDED, RETRFINISHED,
351 READERR, TRYLIMEXC, URLBADPATTERN /* ! */, FILEBADFILE /* ! */, RANGEERR,
352 RETRBADPATTERN, RETNOTSUP /* ! */, ROBOTSOK /* ! */, NOROBOTS /* ! */,
355 AUTHFAILED, QUOTEXC, WRITEFAILED, SSLINITFAILED, VERIFCERTERR,
356 UNLINKERR, NEWLOCATION_KEEP_POST
360 Select an appropriate "orig" suffix and a separator character for
361 adding a unique suffix to a file name.
363 A VMS ODS2 file system can not tolerate multiple dots. An ODS5 file
364 system can, but even there not all dots are equal, and heroic effort
365 would be needed to get ".html^.orig" rather than (the less desirable)
366 "^.html.orig". It's more satisfactory always to use "_orig" on VMS
367 (rather than including "vms.h", testing "ods5_dest", and acting
370 Note that code in various places assumes that this string is five
374 # define ORIG_SFX "_orig"
375 # else /* def __VMS */
376 # define ORIG_SFX ".orig"
377 # endif /* def __VMS [else] */
379 /* ".NNN" unique-ifying suffix separator character for unique_name() in
380 url.c (and anywhere else). Note that on VMS, the file system's
381 version numbers solve the problem that unique_name() is designed to
382 handle, obviating this whole exercise. Other systems may specify a
383 character different from "." here, if desired.
386 # define UNIQ_SEP '.'
387 # endif /* ndef __VMS */