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