1 /* Miscellaneous declarations.
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of GNU Wget.
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.
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.
16 You should have received a copy of the GNU General Public License
17 along with Wget; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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. */
30 /* This file contains some declarations that don't fit anywhere else.
31 It also contains some useful includes, like the obnoxious TIME_H
37 /* Disable assertions when debug support is not compiled in. */
42 /* Define this if you want primitive but extensive malloc debugging.
43 It will make Wget extremely slow, so only do it in development
49 # define PARAMS(args) args
51 # define PARAMS(args) ()
55 /* `gettext (FOO)' is long to write, so we use `_(FOO)'. If NLS is
56 unavailable, _(STRING) simply returns STRING. */
58 # define _(string) gettext (string)
59 # ifdef HAVE_LIBINTL_H
61 # endif /* HAVE_LIBINTL_H */
62 #else /* not HAVE_NLS */
63 # define _(string) string
64 #endif /* not HAVE_NLS */
66 /* No-op version of gettext, used for constant strings. */
67 #define N_(string) (string)
69 /* I18N NOTE: You will notice that none of the DEBUGP messages are
70 marked as translatable. This is intentional, for a few reasons:
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.
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
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! */
85 /* Include these, so random files need not include them. */
88 /* locale independent replacement for ctype.h */
89 #include "safe-ctype.h"
91 #define DO_NOTHING do {} while (0)
93 /* Print X if debugging is enabled; a no-op otherwise. */
95 # define DEBUGP(x) do { if (opt.debug) { debug_logprintf x; } } while (0)
96 #else /* not ENABLE_DEBUG */
97 # define DEBUGP(x) DO_NOTHING
98 #endif /* not ENABLE_DEBUG */
100 /* Make gcc check for the format of logmsg() and debug_logmsg(). */
102 # define GCC_FORMAT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
103 #else /* not __GNUC__ */
104 # define GCC_FORMAT_ATTR(a, b)
105 #endif /* not __GNUC__ */
107 /* These are from log.c, but they are used everywhere, so we declare
109 enum log_options { LOG_VERBOSE, LOG_NOTQUIET, LOG_NONVERBOSE, LOG_ALWAYS };
112 void logprintf PARAMS ((enum log_options, const char *, ...))
113 GCC_FORMAT_ATTR (2, 3);
114 void debug_logprintf PARAMS ((const char *, ...)) GCC_FORMAT_ATTR (1, 2);
115 #else /* not HAVE_STDARG_H */
117 void debug_logprintf ();
118 #endif /* not HAVE_STDARG_H */
119 void logputs PARAMS ((enum log_options, const char *));
120 void logflush PARAMS ((void));
121 void log_set_flush PARAMS ((int));
122 int log_set_save_context PARAMS ((int));
124 /* Defined in `utils.c', but used literally everywhere. */
127 #define xmalloc xmalloc_real
128 #define xrealloc xrealloc_real
129 #define xstrdup xstrdup_real
132 void *xmalloc_real PARAMS ((size_t));
133 void *xrealloc_real PARAMS ((void *, size_t));
134 char *xstrdup_real PARAMS ((const char *));
136 #else /* DEBUG_MALLOC */
138 #define xmalloc(s) xmalloc_debug (s, __FILE__, __LINE__)
139 #define xfree(p) xfree_debug (p, __FILE__, __LINE__)
140 #define xrealloc(p, s) xrealloc_debug (p, s, __FILE__, __LINE__)
141 #define xstrdup(p) xstrdup_debug (p, __FILE__, __LINE__)
143 void *xmalloc_debug PARAMS ((size_t, const char *, int));
144 void xfree_debug PARAMS ((void *, const char *, int));
145 void *xrealloc_debug PARAMS ((void *, size_t, const char *, int));
146 char *xstrdup_debug PARAMS ((const char *, const char *, int));
148 #endif /* DEBUG_MALLOC */
150 /* #### Find a better place for this. */
151 /* The log file to which Wget writes to after HUP. */
152 #define DEFAULT_LOGFILE "wget-log"
154 #define MD5_HASHLEN 16
156 /* Useful macros used across the code: */
158 /* Is the string a hpyhen-only? */
159 #define HYPHENP(x) (*(x) == '-' && !*((x) + 1))
161 /* The smaller value of the two. */
162 #define MINVAL(x, y) ((x) < (y) ? (x) : (y))
164 /* Convert an ASCII hex digit to the corresponding number between 0
165 and 15. X should be a hexadecimal digit that satisfies isxdigit;
166 otherwise, the result is undefined. */
167 #define XDIGIT_TO_NUM(x) ((x) < 'A' ? (x) - '0' : TOUPPER (x) - 'A' + 10)
169 /* Convert a sequence of ASCII hex digits X and Y to a number betewen
170 0 and 255. Uses XDIGIT_TO_NUM for conversion of individual
172 #define X2DIGITS_TO_NUM(h1, h2) ((XDIGIT_TO_NUM (h1) << 4) + XDIGIT_TO_NUM (h2))
174 /* The reverse of the above: convert a number in the [0, 16) range to
175 its ASCII representation in hex. The A-F characters are in upper
177 #define XNUM_TO_DIGIT(x) ("0123456789ABCDEF"[x])
179 /* Like XNUM_TO_DIGIT, but generates lower-case characters. */
180 #define XNUM_TO_digit(x) ("0123456789abcdef"[x])
182 /* Returns the number of elements in an array with fixed
183 initialization. For example:
185 static char a[] = "foo"; -- countof(a) == 4 (for terminating \0)
187 int a[5] = {1, 2}; -- countof(a) == 5
189 char *a[] = { -- countof(a) == 3
192 #define countof(array) (sizeof (array) / sizeof (*(array)))
194 #define alloca_array(type, size) ((type *) alloca ((size) * sizeof (type)))
196 /* Copy the data delimited with BEG and END to alloca-allocated
197 storage, and zero-terminate it. Arguments are evaluated only once,
198 in the order BEG, END, PLACE. */
199 #define BOUNDED_TO_ALLOCA(beg, end, place) do { \
200 const char *BTA_beg = (beg); \
201 int BTA_len = (end) - BTA_beg; \
202 char **BTA_dest = &(place); \
203 *BTA_dest = alloca (BTA_len + 1); \
204 memcpy (*BTA_dest, BTA_beg, BTA_len); \
205 (*BTA_dest)[BTA_len] = '\0'; \
208 /* Return non-zero if string bounded between BEG and END is equal to
209 STRING_LITERAL. The comparison is case-sensitive. */
210 #define BOUNDED_EQUAL(beg, end, string_literal) \
211 ((end) - (beg) == sizeof (string_literal) - 1 \
212 && !memcmp ((beg), (string_literal), \
213 sizeof (string_literal) - 1))
215 /* The same as above, except the comparison is case-insensitive. */
216 #define BOUNDED_EQUAL_NO_CASE(beg, end, string_literal) \
217 ((end) - (beg) == sizeof (string_literal) - 1 \
218 && !strncasecmp ((beg), (string_literal), \
219 sizeof (string_literal) - 1))
221 /* Note that this much more elegant definition cannot be used:
223 #define STRDUP_ALLOCA(str) (strcpy ((char *)alloca (strlen (str) + 1), str))
225 This is because some compilers don't handle alloca() as argument to
226 function correctly. Gcc under Intel has been reported to offend in
229 #define STRDUP_ALLOCA(ptr, str) do { \
230 (ptr) = (char *)alloca (strlen (str) + 1); \
231 strcpy ((ptr), (str)); \
234 /* Generally useful if you want to avoid arbitrary size limits but
235 don't need a full dynamic array. Assumes that BASEVAR points to a
236 malloced array of TYPE objects (or possibly a NULL pointer, if
237 SIZEVAR is 0), with the total size stored in SIZEVAR. This macro
238 will realloc BASEVAR as necessary so that it can hold at least
239 NEEDED_SIZE objects. The reallocing is done by doubling, which
240 ensures constant amortized time per element. */
241 #define DO_REALLOC(basevar, sizevar, needed_size, type) do \
243 /* Avoid side-effectualness. */ \
244 long do_realloc_needed_size = (needed_size); \
245 long do_realloc_newsize = 0; \
246 while ((sizevar) < (do_realloc_needed_size)) { \
247 do_realloc_newsize = 2*(sizevar); \
248 if (do_realloc_newsize < 32) \
249 do_realloc_newsize = 32; \
250 (sizevar) = do_realloc_newsize; \
252 if (do_realloc_newsize) \
253 basevar = (type *)xrealloc (basevar, do_realloc_newsize * sizeof (type)); \
256 /* Free FOO if it is non-NULL. */
257 #define FREE_MAYBE(foo) do { if (foo) xfree (foo); } while (0)
259 extern const char *exec_name;
261 /* Document type ("dt") flags */
264 TEXTHTML = 0x0001, /* document is of type text/html
265 or application/xhtml+xml */
266 RETROKF = 0x0002, /* retrieval was OK */
267 HEAD_ONLY = 0x0004, /* only send the HEAD request */
268 SEND_NOCACHE = 0x0008, /* send Pragma: no-cache directive */
269 ACCEPTRANGES = 0x0010, /* Accept-ranges header was found */
270 ADDED_HTML_EXTENSION = 0x0020 /* added ".html" extension due to -E */
273 /* Universal error type -- used almost everywhere. Error reporting of
274 this detail is not generally used or needed and should be
278 NOCONERROR, HOSTERR, CONSOCKERR, CONERROR, CONSSLERR,
279 CONIMPOSSIBLE, NEWLOCATION, NOTENOUGHMEM, CONPORTERR,
280 BINDERR, BINDOK, LISTENERR, ACCEPTERR, ACCEPTOK,
281 CONCLOSED, FTPOK, FTPLOGINC, FTPLOGREFUSED, FTPPORTERR,
282 FTPNSFOD, FTPRETROK, FTPUNKNOWNTYPE, FTPRERR,
283 FTPREXC, FTPSRVERR, FTPRETRINT, FTPRESTFAIL, URLERROR,
284 FOPENERR, FWRITEERR, HOK, HLEXC, HEOF,
285 HERR, RETROK, RECLEVELEXC, FTPACCDENIED, WRONGCODE,
286 FTPINVPASV, FTPNOPASV,
287 CONTNOTSUPPORTED, RETRUNNEEDED, RETRFINISHED, READERR, TRYLIMEXC,
288 URLBADPATTERN, FILEBADFILE, RANGEERR, RETRBADPATTERN,
289 RETNOTSUP, ROBOTSOK, NOROBOTS, PROXERR, AUTHFAILED,
290 QUOTEXC, WRITEFAILED,
291 SSLERRCERTFILE,SSLERRCERTKEY,SSLERRCTXCREATE
294 typedef unsigned char boolean;
302 /* So we can say strcmp(a, b) == EQ rather than strcmp(a, b) == 0 or
303 the really awful !strcmp(a, b). */
306 /* For most options, 0 means no limits, but with -p in the picture, that causes
307 a problem on the maximum recursion depth variable. To retain backwards
308 compatibility we allow users to consider "0" to be synonymous with "inf" for
309 -l, but internally infinite recursion is specified by -1 and 0 means to only
310 retrieve the requisites of a single document. */
311 #define INFINITE_RECURSION -1
313 /* In case old systems don't have EAFNOSUPPORT, which we use below. */
315 # define EAFNOSUPPORT EINVAL
318 #define CONNECT_ERROR(err) (( (err) == EAFNOSUPPORT \
320 || ((err) == ECONNREFUSED \
321 && !opt.retry_connrefused)) \
322 ? CONIMPOSSIBLE : CONERROR)