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