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