]> sjero.net Git - wget/blob - src/wget.h
Improved exit status handling.
[wget] / src / wget.h
1 /* Miscellaneous declarations.
2    Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
3    2004, 2005, 2006, 2007, 2008 Free Software 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 /* This file contains declarations that are universally useful and
32    those that don't fit elsewhere.  It also includes sysdep.h which
33    includes some often-needed system includes, like the obnoxious
34    <time.h> inclusion.  */
35
36 #ifndef WGET_H
37 #define WGET_H
38
39 #include "config.h"
40
41 /* Include these, so random files need not include them.  */
42 #include "sysdep.h"
43
44 /* Disable assertions when debug support is not compiled in. */
45 #ifndef ENABLE_DEBUG
46 # define NDEBUG
47 #endif
48
49 /* Is OpenSSL or GNUTLS available? */
50 #if defined HAVE_LIBSSL || defined HAVE_LIBGNUTLS
51 # define HAVE_SSL
52 #endif
53
54 /* `gettext (FOO)' is long to write, so we use `_(FOO)'.  If NLS is
55    unavailable, _(STRING) simply returns STRING.  */
56 #include "gettext.h"
57 #define _(string)   gettext (string)
58
59 /* A pseudo function call that serves as a marker for the automated
60    extraction of messages, but does not call gettext().  The run-time
61    translation is done at a different place in the code.  The purpose
62    of the N_("...") call is to make the message snarfer aware that the
63    "..." string needs to be translated.  STRING should be a string
64    literal.  Concatenated strings and other string expressions won't
65    work.  The macro's expansion is not parenthesized, so that it is
66    suitable as initializer for static 'char[]' or 'const char[]'
67    variables.  -- explanation partly taken from GNU make.  */
68 #define N_(string) string
69
70 #if ! ENABLE_NLS
71 # undef HAVE_WCHAR_H
72 # undef HAVE_WCWIDTH
73 # undef HAVE_MBTOWC
74 #endif /* not ENABLE_NLS */
75
76 #if HAVE_WCWIDTH && HAVE_MBTOWC
77 # define USE_NLS_PROGRESS_BAR 1
78 #else
79 /* Just to be a little paranoid about it. */
80 # undef  USE_NLS_PROGRESS_BAR
81 #endif
82
83 /* I18N NOTE: You will notice that none of the DEBUGP messages are
84    marked as translatable.  This is intentional, for a few reasons:
85
86    1) The debug messages are not meant for the users to look at, but
87    for the developers; as such, they should be considered more like
88    source comments than real program output.
89
90    2) The messages are numerous, and yet they are random and frivolous
91    ("double yuck!" and such).  There would be a lot of work with no
92    gain.
93
94    3) Finally, the debug messages are meant to be a clue for me to
95    debug problems with Wget.  If I get them in a language I don't
96    understand, debugging will become a new challenge of its own!  */
97
98 /* locale independent replacement for ctype.h */
99 #include "c-ctype.h"
100
101 /* Conditionalize the use of GCC's __attribute__((format)) and
102    __builtin_expect features using macros.  */
103
104 #if defined(__GNUC__) && __GNUC__ >= 3
105 # define GCC_FORMAT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
106 # define LIKELY(exp)   __builtin_expect (!!(exp), 1)
107 # define UNLIKELY(exp) __builtin_expect ((exp), 0)
108 #else
109 # define GCC_FORMAT_ATTR(a, b)
110 # define LIKELY(exp)   (exp)
111 # define UNLIKELY(exp) (exp)
112 #endif
113
114 /* Execute the following statement if debugging is both enabled at
115    compile-time and requested at run-time; a no-op otherwise.  */
116
117 #ifdef ENABLE_DEBUG
118 # define IF_DEBUG if (UNLIKELY (opt.debug))
119 #else
120 # define IF_DEBUG if (0)
121 #endif
122
123 /* Print ARGS if debugging is enabled and requested, otherwise do
124    nothing.  This must be called with an extra level of parentheses
125    because it's not possible to pass a variable number of arguments to
126    a macro (in portable C89).  ARGS are like arguments to printf.  */
127
128 #define DEBUGP(args) do { IF_DEBUG { debug_logprintf args; } } while (0)
129
130 /* Pick an integer type large enough for file sizes, content lengths,
131    and such.  Because today's files can be very large, it should be a
132    signed integer at least 64 bits wide.  This can't be typedeffed to
133    off_t because: a) off_t is always 32-bit on Windows, and b) we
134    don't necessarily want to tie having a 64-bit type for internal
135    calculations to having LFS support.  */
136
137 #ifdef WINDOWS
138   /* nothing to do, see mswindows.h */
139 #elif SIZEOF_LONG >= 8
140   /* long is large enough, so use it. */
141   typedef long wgint;
142 # define SIZEOF_WGINT SIZEOF_LONG
143 #elif SIZEOF_LONG_LONG >= 8
144   /* long long is large enough and available, use that */
145   typedef long long wgint;
146 # define SIZEOF_WGINT SIZEOF_LONG_LONG
147 #elif HAVE_INT64_T
148   typedef int64_t wgint;
149 # define SIZEOF_WGINT 8
150 #elif SIZEOF_OFF_T >= 8
151   /* In case off_t is typedeffed to a large non-standard type that our
152      tests don't find. */
153   typedef off_t wgint;
154 # define SIZEOF_WGINT SIZEOF_OFF_T
155 #else
156   /* Fall back to using long, which is always available and in most
157      cases large enough. */
158   typedef long wgint;
159 # define SIZEOF_WGINT SIZEOF_LONG
160 #endif
161
162 /* Pick a strtol-compatible function that will work with wgint.  The
163    choices are strtol, strtoll, or our own implementation of strtoll
164    in cmpt.c, activated with NEED_STRTOLL.  */
165
166 #ifdef WINDOWS
167   /* nothing to do, see mswindows.h */
168 #elif SIZEOF_WGINT == SIZEOF_LONG
169 # define str_to_wgint strtol
170 #elif SIZEOF_WGINT == SIZEOF_LONG_LONG
171 # define str_to_wgint strtoll
172 # ifndef HAVE_STRTOLL
173 #  define NEED_STRTOLL
174 #  define strtoll_type long long
175 # endif
176 #else
177   /* wgint has a strange size; synthesize strtoll and use it. */
178 # define str_to_wgint strtoll
179 # define NEED_STRTOLL
180 # define strtoll_type wgint
181 #endif
182
183 #define WGINT_MAX TYPE_MAXIMUM (wgint)
184
185 /* Declare our strtoll replacement. */
186 #ifdef NEED_STRTOLL
187 strtoll_type strtoll (const char *, char **, int);
188 #endif
189
190 /* Now define a large numeric type useful for storing sizes of *sums*
191    of downloads, such as the value of the --quota option.  This should
192    be a type able to hold 2G+ values even on systems without large
193    file support.  (It is useful to limit Wget's download quota to say
194    10G even if a single file cannot be that large.)
195
196    To make sure we get the largest size possible, we use `double' on
197    systems without a 64-bit integral type.  (Since it is used in very
198    few places in Wget, this is acceptable.)  */
199
200 #if SIZEOF_WGINT >= 8
201 /* just use wgint */
202 typedef wgint SUM_SIZE_INT;
203 #else
204 /* On systems without LFS, use double, which buys us integers up to 2^53. */
205 typedef double SUM_SIZE_INT;
206 #endif
207
208 #include "options.h"
209
210 /* Everything uses this, so include them here directly.  */
211 #include <alloca.h>
212 #include "xalloc.h"
213
214 /* Likewise for logging functions.  */
215 #include "log.h"
216
217 /* Likewise for quoting functions.  */
218 #include "quote.h"
219 #include "quotearg.h"
220
221 /* Likewise for struct iri definition */
222 #include "iri.h"
223
224 /* Useful macros used across the code: */
225
226 /* The number of elements in an array.  For example:
227    static char a[] = "foo";     -- countof(a) == 4 (note terminating \0)
228    int a[5] = {1, 2};           -- countof(a) == 5
229    char *a[] = {                -- countof(a) == 3
230      "foo", "bar", "baz"
231    }; */
232 #define countof(array) (sizeof (array) / sizeof ((array)[0]))
233
234 /* Zero out a value.  */
235 #define xzero(x) memset (&(x), '\0', sizeof (x))
236
237 /* Convert an ASCII hex digit to the corresponding number between 0
238    and 15.  H should be a hexadecimal digit that satisfies isxdigit;
239    otherwise, the result is undefined.  */
240 #define XDIGIT_TO_NUM(h) ((h) < 'A' ? (h) - '0' : c_toupper (h) - 'A' + 10)
241 #define X2DIGITS_TO_NUM(h1, h2) ((XDIGIT_TO_NUM (h1) << 4) + XDIGIT_TO_NUM (h2))
242
243 /* The reverse of the above: convert a number in the [0, 16) range to
244    the ASCII representation of the corresponding hexadecimal digit.
245    `+ 0' is there so you can't accidentally use it as an lvalue.  */
246 #define XNUM_TO_DIGIT(x) ("0123456789ABCDEF"[x] + 0)
247 #define XNUM_TO_digit(x) ("0123456789abcdef"[x] + 0)
248
249 /* Copy the data delimited with BEG and END to alloca-allocated
250    storage, and zero-terminate it.  Arguments are evaluated only once,
251    in the order BEG, END, PLACE.  */
252 #define BOUNDED_TO_ALLOCA(beg, end, place) do { \
253   const char *BTA_beg = (beg);                  \
254   int BTA_len = (end) - BTA_beg;                \
255   char **BTA_dest = &(place);                   \
256   *BTA_dest = alloca (BTA_len + 1);             \
257   memcpy (*BTA_dest, BTA_beg, BTA_len);         \
258   (*BTA_dest)[BTA_len] = '\0';                  \
259 } while (0)
260
261 /* Return non-zero if string bounded between BEG and END is equal to
262    STRING_LITERAL.  The comparison is case-sensitive.  */
263 #define BOUNDED_EQUAL(beg, end, string_literal)                         \
264   ((end) - (beg) == sizeof (string_literal) - 1                         \
265    && !memcmp (beg, string_literal, sizeof (string_literal) - 1))
266
267 /* The same as above, except the comparison is case-insensitive. */
268 #define BOUNDED_EQUAL_NO_CASE(beg, end, string_literal)                 \
269   ((end) - (beg) == sizeof (string_literal) - 1                         \
270    && !strncasecmp (beg, string_literal, sizeof (string_literal) - 1))
271
272 /* Like ptr=strdup(str), but allocates the space for PTR on the stack.
273    This cannot be an expression because this is not portable:
274      #define STRDUP_ALLOCA(str) (strcpy (alloca (strlen (str) + 1), str))
275    The problem is that some compilers can't handle alloca() being an
276    argument to a function.  */
277
278 #define STRDUP_ALLOCA(ptr, str) do {                    \
279   char **SA_dest = &(ptr);                              \
280   const char *SA_src = (str);                           \
281   *SA_dest = (char *)alloca (strlen (SA_src) + 1);      \
282   strcpy (*SA_dest, SA_src);                            \
283 } while (0)
284
285 /* Generally useful if you want to avoid arbitrary size limits but
286    don't need a full dynamic array.  Assumes that BASEVAR points to a
287    malloced array of TYPE objects (or possibly a NULL pointer, if
288    SIZEVAR is 0), with the total size stored in SIZEVAR.  This macro
289    will realloc BASEVAR as necessary so that it can hold at least
290    NEEDED_SIZE objects.  The reallocing is done by doubling, which
291    ensures constant amortized time per element.  */
292
293 #define DO_REALLOC(basevar, sizevar, needed_size, type) do {            \
294   long DR_needed_size = (needed_size);                                  \
295   long DR_newsize = 0;                                                  \
296   while ((sizevar) < (DR_needed_size)) {                                \
297     DR_newsize = sizevar << 1;                                          \
298     if (DR_newsize < 16)                                                \
299       DR_newsize = 16;                                                  \
300     (sizevar) = DR_newsize;                                             \
301   }                                                                     \
302   if (DR_newsize)                                                       \
303     basevar = xrealloc (basevar, DR_newsize * sizeof (type));           \
304 } while (0)
305
306 /* Used to print pointers (usually for debugging).  Print pointers
307    using printf ("0x%0*lx", PTR_FORMAT (p)).  (%p is too unpredictable;
308    some implementations prepend 0x, while some don't, and most don't
309    0-pad the address.)  */
310 #define PTR_FORMAT(p) (int) (2 * sizeof (void *)), (unsigned long) (p)
311
312 extern const char *exec_name;
313 \f
314 /* Document type ("dt") flags */
315 enum
316 {
317   TEXTHTML             = 0x0001,        /* document is of type text/html
318                                            or application/xhtml+xml */
319   RETROKF              = 0x0002,        /* retrieval was OK */
320   HEAD_ONLY            = 0x0004,        /* only send the HEAD request */
321   SEND_NOCACHE         = 0x0008,        /* send Pragma: no-cache directive */
322   ACCEPTRANGES         = 0x0010,        /* Accept-ranges header was found */
323   ADDED_HTML_EXTENSION = 0x0020,        /* added ".html" extension due to -E */
324   TEXTCSS              = 0x0040         /* document is of type text/css */
325 };
326
327 /* Universal error type -- used almost everywhere.  Error reporting of
328    this detail is not generally used or needed and should be
329    simplified.  */
330 typedef enum
331 {
332   /*  0  */
333   NOCONERROR, HOSTERR, CONSOCKERR, CONERROR, CONSSLERR,
334   CONIMPOSSIBLE, NEWLOCATION, NOTENOUGHMEM /* ! */,
335   CONPORTERR /* ! */, CONCLOSED /* ! */, 
336   /* 10  */
337   FTPOK, FTPLOGINC, FTPLOGREFUSED, FTPPORTERR, FTPSYSERR,
338   FTPNSFOD, FTPRETROK /* ! */, FTPUNKNOWNTYPE, FTPRERR, FTPREXC /* ! */, 
339   /* 20  */
340   FTPSRVERR, FTPRETRINT, FTPRESTFAIL, URLERROR, FOPENERR, 
341   FOPEN_EXCL_ERR, FWRITEERR, HOK /* ! */, HLEXC /* ! */, HEOF,
342   /* 30  */
343   HERR, RETROK, RECLEVELEXC, FTPACCDENIED /* ! */, WRONGCODE,
344   FTPINVPASV, FTPNOPASV, CONTNOTSUPPORTED, RETRUNNEEDED, RETRFINISHED, 
345   /* 40  */
346   READERR, TRYLIMEXC, URLBADPATTERN /* ! */, FILEBADFILE /* ! */, RANGEERR, 
347   RETRBADPATTERN, RETNOTSUP /* ! */, ROBOTSOK /* ! */, NOROBOTS /* ! */,
348   PROXERR, 
349   /* 50  */
350   AUTHFAILED, QUOTEXC, WRITEFAILED, SSLINITFAILED, VERIFCERTERR
351 } uerr_t;
352
353 /* 2005-02-19 SMS.
354    Select an appropriate "orig" suffix and a separator character for
355    adding a unique suffix to a file name.
356
357    A VMS ODS2 file system can not tolerate multiple dots.  An ODS5 file
358    system can, but even there not all dots are equal, and heroic effort
359    would be needed to get ".html^.orig" rather than (the less desirable)
360    "^.html.orig".  It's more satisfactory always to use "_orig" on VMS
361    (rather than including "vms.h", testing "ods5_dest", and acting
362    accordingly).
363
364    Note that code in various places assumes that this string is five
365    characters long.
366 */
367 # ifdef __VMS
368 #  define ORIG_SFX "_orig"
369 # else /* def __VMS */
370 #  define ORIG_SFX ".orig"
371 # endif /* def __VMS [else] */
372
373 /* ".NNN" unique-ifying suffix separator character for unique_name() in
374    url.c (and anywhere else).  Note that on VMS, the file system's
375    version numbers solve the problem that unique_name() is designed to
376    handle, obviating this whole exercise.  Other systems may specify a
377    character different from "." here, if desired.
378 */
379 # ifndef __VMS
380 #  define UNIQ_SEP '.'
381 # endif /* ndef __VMS */
382
383 #endif /* WGET_H */