1 /* Miscellaneous declarations.
2 Copyright (C) 1996-2006 Free Software Foundation, Inc.
4 This file is part of GNU Wget.
6 GNU Wget 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 3 of the License, or
9 (at your option) any later version.
11 GNU Wget 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 Wget. If not, see <http://www.gnu.org/licenses/>.
19 In addition, as a special exception, the Free Software Foundation
20 gives permission to link the code of its release of Wget with the
21 OpenSSL project's "OpenSSL" library (or with modified versions of it
22 that use the same license as the "OpenSSL" library), and distribute
23 the linked executables. You must obey the GNU General Public License
24 in all respects for all of the code used other than "OpenSSL". If you
25 modify this file, you may extend this exception to your version of the
26 file, but you are not obligated to do so. If you do not wish to do
27 so, delete this exception statement from your version. */
29 /* This file contains declarations that are universally useful and
30 those that don't fit elsewhere. It also includes sysdep.h which
31 includes some often-needed system includes, like the obnoxious
32 <time.h> inclusion. */
37 /* Disable assertions when debug support is not compiled in. */
42 /* Is OpenSSL or GNUTLS available? */
43 #if defined HAVE_LIBSSL || defined HAVE_LIBGNUTLS
47 /* `gettext (FOO)' is long to write, so we use `_(FOO)'. If NLS is
48 unavailable, _(STRING) simply returns STRING. */
50 # define _(string) gettext (string)
51 # ifdef HAVE_LIBINTL_H
53 # else /* not HAVE_LIBINTL_H */
54 const char *gettext ();
55 # endif /* not HAVE_LIBINTL_H */
56 #else /* not HAVE_NLS */
57 # define _(string) (string)
58 #endif /* not HAVE_NLS */
60 /* A pseudo function call that serves as a marker for the automated
61 extraction of messages, but does not call gettext(). The run-time
62 translation is done at a different place in the code. The purpose
63 of the N_("...") call is to make the message snarfer aware that the
64 "..." string needs to be translated. STRING should be a string
65 literal. Concatenated strings and other string expressions won't
66 work. The macro's expansion is not parenthesized, so that it is
67 suitable as initializer for static 'char[]' or 'const char[]'
68 variables. -- explanation partly taken from GNU make. */
69 #define N_(string) string
71 /* I18N NOTE: You will notice that none of the DEBUGP messages are
72 marked as translatable. This is intentional, for a few reasons:
74 1) The debug messages are not meant for the users to look at, but
75 for the developers; as such, they should be considered more like
76 source comments than real program output.
78 2) The messages are numerous, and yet they are random and frivolous
79 ("double yuck!" and such). There would be a lot of work with no
82 3) Finally, the debug messages are meant to be a clue for me to
83 debug problems with Wget. If I get them in a language I don't
84 understand, debugging will become a new challenge of its own! */
87 /* Include these, so random files need not include them. */
89 /* locale independent replacement for ctype.h */
90 #include "safe-ctype.h"
92 /* Conditionalize the use of GCC's __attribute__((format)) and
93 __builtin_expect features using macros. */
95 #if defined(__GNUC__) && __GNUC__ >= 3
96 # define GCC_FORMAT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
97 # define LIKELY(exp) __builtin_expect (!!(exp), 1)
98 # define UNLIKELY(exp) __builtin_expect ((exp), 0)
100 # define GCC_FORMAT_ATTR(a, b)
101 # define LIKELY(exp) (exp)
102 # define UNLIKELY(exp) (exp)
105 /* Execute the following statement if debugging is both enabled at
106 compile-time and requested at run-time; a no-op otherwise. */
109 # define IF_DEBUG if (UNLIKELY (opt.debug))
111 # define IF_DEBUG if (0)
114 /* Print ARGS if debugging is enabled and requested, otherwise do
115 nothing. This must be called with an extra level of parentheses
116 because it's not possible to pass a variable number of arguments to
117 a macro (in portable C89). ARGS are like arguments to printf. */
119 #define DEBUGP(args) do { IF_DEBUG { debug_logprintf args; } } while (0)
121 /* Pick an integer type large enough for file sizes, content lengths,
122 and such. Because today's files can be very large, it should be a
123 signed integer at least 64 bits wide. This can't be typedeffed to
124 off_t because: a) off_t is always 32-bit on Windows, and b) we
125 don't necessarily want to tie having a 64-bit type for internal
126 calculations to having LFS support. */
129 /* nothing to do, see mswindows.h */
130 #elif SIZEOF_LONG >= 8
131 /* long is large enough, so use it. */
133 # define SIZEOF_WGINT SIZEOF_LONG
134 #elif SIZEOF_LONG_LONG >= 8
135 /* long long is large enough and available, use that */
136 typedef long long wgint;
137 # define SIZEOF_WGINT SIZEOF_LONG_LONG
139 typedef int64_t wgint;
140 # define SIZEOF_WGINT 8
141 #elif SIZEOF_OFF_T >= 8
142 /* In case off_t is typedeffed to a large non-standard type that our
145 # define SIZEOF_WGINT SIZEOF_OFF_T
147 /* Fall back to using long, which is always available and in most
148 cases large enough. */
150 # define SIZEOF_WGINT SIZEOF_LONG
153 /* Pick a strtol-compatible function that will work with wgint. The
154 choices are strtol, strtoll, or our own implementation of strtoll
155 in cmpt.c, activated with NEED_STRTOLL. */
158 /* nothing to do, see mswindows.h */
159 #elif SIZEOF_WGINT == SIZEOF_LONG
160 # define str_to_wgint strtol
161 #elif SIZEOF_WGINT == SIZEOF_LONG_LONG
162 # define str_to_wgint strtoll
163 # ifndef HAVE_STRTOLL
164 # define NEED_STRTOLL
165 # define strtoll_type long long
168 /* wgint has a strange size; synthesize strtoll and use it. */
169 # define str_to_wgint strtoll
170 # define NEED_STRTOLL
171 # define strtoll_type wgint
174 #define WGINT_MAX TYPE_MAXIMUM (wgint)
176 /* Declare our strtoll replacement. */
178 strtoll_type strtoll (const char *, char **, int);
181 /* Now define a large numeric type useful for storing sizes of *sums*
182 of downloads, such as the value of the --quota option. This should
183 be a type able to hold 2G+ values even on systems without large
184 file support. (It is useful to limit Wget's download quota to say
185 10G even if a single file cannot be that large.)
187 To make sure we get the largest size possible, we use `double' on
188 systems without a 64-bit integral type. (Since it is used in very
189 few places in Wget, this is acceptable.) */
191 #if SIZEOF_WGINT >= 8
193 typedef wgint SUM_SIZE_INT;
195 /* On systems without LFS, use double, which buys us integers up to 2^53. */
196 typedef double SUM_SIZE_INT;
201 /* Everything uses this, so include them here directly. */
204 /* Likewise for logging functions. */
207 /* Useful macros used across the code: */
209 /* The number of elements in an array. For example:
210 static char a[] = "foo"; -- countof(a) == 4 (note terminating \0)
211 int a[5] = {1, 2}; -- countof(a) == 5
212 char *a[] = { -- countof(a) == 3
215 #define countof(array) (sizeof (array) / sizeof ((array)[0]))
217 /* Zero out a value. */
218 #define xzero(x) memset (&(x), '\0', sizeof (x))
220 /* Convert an ASCII hex digit to the corresponding number between 0
221 and 15. H should be a hexadecimal digit that satisfies isxdigit;
222 otherwise, the result is undefined. */
223 #define XDIGIT_TO_NUM(h) ((h) < 'A' ? (h) - '0' : TOUPPER (h) - 'A' + 10)
224 #define X2DIGITS_TO_NUM(h1, h2) ((XDIGIT_TO_NUM (h1) << 4) + XDIGIT_TO_NUM (h2))
226 /* The reverse of the above: convert a number in the [0, 16) range to
227 the ASCII representation of the corresponding hexadecimal digit.
228 `+ 0' is there so you can't accidentally use it as an lvalue. */
229 #define XNUM_TO_DIGIT(x) ("0123456789ABCDEF"[x] + 0)
230 #define XNUM_TO_digit(x) ("0123456789abcdef"[x] + 0)
232 /* Copy the data delimited with BEG and END to alloca-allocated
233 storage, and zero-terminate it. Arguments are evaluated only once,
234 in the order BEG, END, PLACE. */
235 #define BOUNDED_TO_ALLOCA(beg, end, place) do { \
236 const char *BTA_beg = (beg); \
237 int BTA_len = (end) - BTA_beg; \
238 char **BTA_dest = &(place); \
239 *BTA_dest = alloca (BTA_len + 1); \
240 memcpy (*BTA_dest, BTA_beg, BTA_len); \
241 (*BTA_dest)[BTA_len] = '\0'; \
244 /* Return non-zero if string bounded between BEG and END is equal to
245 STRING_LITERAL. The comparison is case-sensitive. */
246 #define BOUNDED_EQUAL(beg, end, string_literal) \
247 ((end) - (beg) == sizeof (string_literal) - 1 \
248 && !memcmp (beg, string_literal, sizeof (string_literal) - 1))
250 /* The same as above, except the comparison is case-insensitive. */
251 #define BOUNDED_EQUAL_NO_CASE(beg, end, string_literal) \
252 ((end) - (beg) == sizeof (string_literal) - 1 \
253 && !strncasecmp (beg, string_literal, sizeof (string_literal) - 1))
255 /* Like ptr=strdup(str), but allocates the space for PTR on the stack.
256 This cannot be an expression because this is not portable:
257 #define STRDUP_ALLOCA(str) (strcpy (alloca (strlen (str) + 1), str))
258 The problem is that some compilers can't handle alloca() being an
259 argument to a function. */
261 #define STRDUP_ALLOCA(ptr, str) do { \
262 char **SA_dest = &(ptr); \
263 const char *SA_src = (str); \
264 *SA_dest = (char *)alloca (strlen (SA_src) + 1); \
265 strcpy (*SA_dest, SA_src); \
268 /* Generally useful if you want to avoid arbitrary size limits but
269 don't need a full dynamic array. Assumes that BASEVAR points to a
270 malloced array of TYPE objects (or possibly a NULL pointer, if
271 SIZEVAR is 0), with the total size stored in SIZEVAR. This macro
272 will realloc BASEVAR as necessary so that it can hold at least
273 NEEDED_SIZE objects. The reallocing is done by doubling, which
274 ensures constant amortized time per element. */
276 #define DO_REALLOC(basevar, sizevar, needed_size, type) do { \
277 long DR_needed_size = (needed_size); \
278 long DR_newsize = 0; \
279 while ((sizevar) < (DR_needed_size)) { \
280 DR_newsize = sizevar << 1; \
281 if (DR_newsize < 16) \
283 (sizevar) = DR_newsize; \
286 basevar = xrealloc (basevar, DR_newsize * sizeof (type)); \
289 /* Used to print pointers (usually for debugging). Print pointers
290 using printf ("0x%0*lx", PTR_FORMAT (p)). (%p is too unpredictable;
291 some implementations prepend 0x, while some don't, and most don't
292 0-pad the address.) */
293 #define PTR_FORMAT(p) (int) (2 * sizeof (void *)), (unsigned long) (p)
295 extern const char *exec_name;
297 /* Document type ("dt") flags */
300 TEXTHTML = 0x0001, /* document is of type text/html
301 or application/xhtml+xml */
302 RETROKF = 0x0002, /* retrieval was OK */
303 HEAD_ONLY = 0x0004, /* only send the HEAD request */
304 SEND_NOCACHE = 0x0008, /* send Pragma: no-cache directive */
305 ACCEPTRANGES = 0x0010, /* Accept-ranges header was found */
306 ADDED_HTML_EXTENSION = 0x0020 /* added ".html" extension due to -E */
309 /* Universal error type -- used almost everywhere. Error reporting of
310 this detail is not generally used or needed and should be
315 NOCONERROR, HOSTERR, CONSOCKERR, CONERROR, CONSSLERR,
316 CONIMPOSSIBLE, NEWLOCATION, NOTENOUGHMEM, CONPORTERR, CONCLOSED,
318 FTPOK, FTPLOGINC, FTPLOGREFUSED, FTPPORTERR, FTPSYSERR,
319 FTPNSFOD, FTPRETROK, FTPUNKNOWNTYPE, FTPRERR, FTPREXC,
321 FTPSRVERR, FTPRETRINT, FTPRESTFAIL, URLERROR, FOPENERR,
322 FOPEN_EXCL_ERR, FWRITEERR, HOK, HLEXC, HEOF,
324 HERR, RETROK, RECLEVELEXC, FTPACCDENIED, WRONGCODE,
325 FTPINVPASV, FTPNOPASV, CONTNOTSUPPORTED, RETRUNNEEDED, RETRFINISHED,
327 READERR, TRYLIMEXC, URLBADPATTERN, FILEBADFILE, RANGEERR,
328 RETRBADPATTERN, RETNOTSUP, ROBOTSOK, NOROBOTS, PROXERR,
330 AUTHFAILED, QUOTEXC, WRITEFAILED, SSLINITFAILED