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