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