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
38 # define NDEBUG /* To kill off assertions */
39 #endif /* not DEBUG */
41 /* Define this if you want primitive but extensive malloc debugging.
42 It will make Wget extremely slow, so only do it in development
48 # define PARAMS(args) args
50 # define PARAMS(args) ()
54 /* `gettext (FOO)' is long to write, so we use `_(FOO)'. If NLS is
55 unavailable, _(STRING) simply returns STRING. */
57 # define _(string) gettext (string)
58 # ifdef HAVE_LIBINTL_H
60 # endif /* HAVE_LIBINTL_H */
61 #else /* not HAVE_NLS */
62 # define _(string) string
63 #endif /* not HAVE_NLS */
65 /* I18N NOTE: You will notice that none of the DEBUG messages are
66 marked as translatable. This is intentional, for a few reasons:
68 1) The debug messages are not meant for the users to look at, but
69 for the developers; as such, they should be considered more like
70 source comments than real program output.
72 2) The messages are numerous, and yet they are random and frivolous
73 ("double yuck!" and such). There would be a lot of work with no
76 3) Finally, the debug messages are meant to be a clue for me to
77 debug problems with Wget. If I get them in a language I don't
78 understand, debugging will become a new challenge of its own! */
81 /* Include these, so random files need not include them. */
84 /* locale independent replacement for ctype.h */
85 #include "safe-ctype.h"
87 #define DO_NOTHING do {} while (0)
89 /* Print X if debugging is enabled; a no-op otherwise. */
91 # define DEBUGP(x) do { if (opt.debug) { debug_logprintf x; } } while (0)
93 # define DEBUGP(x) DO_NOTHING
94 #endif /* not DEBUG */
96 /* Make gcc check for the format of logmsg() and debug_logmsg(). */
98 # define GCC_FORMAT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
99 #else /* not __GNUC__ */
100 # define GCC_FORMAT_ATTR(a, b)
101 #endif /* not __GNUC__ */
103 /* These are from log.c, but they are used everywhere, so we declare
105 enum log_options { LOG_VERBOSE, LOG_NOTQUIET, LOG_NONVERBOSE, LOG_ALWAYS };
108 void logprintf PARAMS ((enum log_options, const char *, ...))
109 GCC_FORMAT_ATTR (2, 3);
110 void debug_logprintf PARAMS ((const char *, ...)) GCC_FORMAT_ATTR (1, 2);
111 #else /* not HAVE_STDARG_H */
113 void debug_logprintf ();
114 #endif /* not HAVE_STDARG_H */
115 void logputs PARAMS ((enum log_options, const char *));
116 void logflush PARAMS ((void));
117 void log_set_flush PARAMS ((int));
118 int log_set_save_context PARAMS ((int));
120 /* Defined in `utils.c', but used literally everywhere. */
123 #define xmalloc xmalloc_real
124 #define xrealloc xrealloc_real
125 #define xstrdup xstrdup_real
128 void *xmalloc_real PARAMS ((size_t));
129 void *xrealloc_real PARAMS ((void *, size_t));
130 char *xstrdup_real PARAMS ((const char *));
132 #else /* DEBUG_MALLOC */
134 #define xmalloc(s) xmalloc_debug (s, __FILE__, __LINE__)
135 #define xfree(p) xfree_debug (p, __FILE__, __LINE__)
136 #define xrealloc(p, s) xrealloc_debug (p, s, __FILE__, __LINE__)
137 #define xstrdup(p) xstrdup_debug (p, __FILE__, __LINE__)
139 void *xmalloc_debug PARAMS ((size_t, const char *, int));
140 void xfree_debug PARAMS ((void *, const char *, int));
141 void *xrealloc_debug PARAMS ((void *, size_t, const char *, int));
142 char *xstrdup_debug PARAMS ((const char *, const char *, int));
144 #endif /* DEBUG_MALLOC */
146 /* #### Find a better place for this. */
147 /* The log file to which Wget writes to after HUP. */
148 #define DEFAULT_LOGFILE "wget-log"
150 #define MD5_HASHLEN 16
152 /* Useful macros used across the code: */
154 /* Is the string a hpyhen-only? */
155 #define HYPHENP(x) (*(x) == '-' && !*((x) + 1))
157 /* The smaller value of the two. */
158 #define MINVAL(x, y) ((x) < (y) ? (x) : (y))
160 /* Convert the ASCII character X to a hex-digit. X should be between
161 '0' and '9', or between 'A' and 'F', or between 'a' and 'f'. The
162 result is a number between 0 and 15. If X is not a hexadecimal
163 digit character, the result is undefined. */
164 #define XCHAR_TO_XDIGIT(x) \
165 (((x) >= '0' && (x) <= '9') ? \
166 ((x) - '0') : (TOUPPER(x) - 'A' + 10))
168 /* The reverse of the above: convert a HEX digit in the [0, 15] range
169 to an ASCII character representing it. The A-F characters are
170 always in upper case. */
171 #define XDIGIT_TO_XCHAR(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'A'))
173 /* Like XDIGIT_TO_XCHAR, but produce a lower-case char. */
174 #define XDIGIT_TO_xchar(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'a'))
176 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (*(array)))
178 /* Copy the data delimited with BEG and END to alloca-allocated
179 storage, and zero-terminate it. BEG and END are evaluated only
180 once, in that order. */
181 #define BOUNDED_TO_ALLOCA(beg, end, place) do { \
182 const char *DTA_beg = (beg); \
183 int DTA_len = (end) - DTA_beg; \
184 place = alloca (DTA_len + 1); \
185 memcpy (place, DTA_beg, DTA_len); \
186 place[DTA_len] = '\0'; \
189 /* Return non-zero if string bounded between BEG and END is equal to
190 STRING_LITERAL. The comparison is case-sensitive. */
191 #define BOUNDED_EQUAL(beg, end, string_literal) \
192 ((end) - (beg) == sizeof (string_literal) - 1 \
193 && !memcmp ((beg), (string_literal), \
194 sizeof (string_literal) - 1))
196 /* The same as above, except the comparison is case-insensitive. */
197 #define BOUNDED_EQUAL_NO_CASE(beg, end, string_literal) \
198 ((end) - (beg) == sizeof (string_literal) - 1 \
199 && !strncasecmp ((beg), (string_literal), \
200 sizeof (string_literal) - 1))
202 /* Note that this much more elegant definition cannot be used:
204 #define STRDUP_ALLOCA(str) (strcpy ((char *)alloca (strlen (str) + 1), str))
206 This is because some compilers don't handle alloca() as argument to
207 function correctly. Gcc under Intel has been reported to offend in
210 #define STRDUP_ALLOCA(ptr, str) do { \
211 (ptr) = (char *)alloca (strlen (str) + 1); \
215 #define ALLOCA_ARRAY(type, len) ((type *) alloca ((len) * sizeof (type)))
217 #define XREALLOC_ARRAY(ptr, type, len) \
218 ((void) (ptr = (type *) xrealloc (ptr, (len) * sizeof (type))))
220 /* Generally useful if you want to avoid arbitrary size limits but
221 don't need a full dynamic array. Assumes that BASEVAR points to a
222 malloced array of TYPE objects (or possibly a NULL pointer, if
223 SIZEVAR is 0), with the total size stored in SIZEVAR. This macro
224 will realloc BASEVAR as necessary so that it can hold at least
225 NEEDED_SIZE objects. The reallocing is done by doubling, which
226 ensures constant amortized time per element. */
227 #define DO_REALLOC(basevar, sizevar, needed_size, type) do \
229 /* Avoid side-effectualness. */ \
230 long do_realloc_needed_size = (needed_size); \
231 long do_realloc_newsize = 0; \
232 while ((sizevar) < (do_realloc_needed_size)) { \
233 do_realloc_newsize = 2*(sizevar); \
234 if (do_realloc_newsize < 32) \
235 do_realloc_newsize = 32; \
236 (sizevar) = do_realloc_newsize; \
238 if (do_realloc_newsize) \
239 XREALLOC_ARRAY (basevar, type, do_realloc_newsize); \
242 /* Use this for small stack-allocated memory chunks that might grow.
243 The initial array is created using alloca(), and this macro
244 requests it to grow. If the needed size is larger than the array,
245 this macro will use malloc to allocate it to new size, and copy the
246 old contents. After that, successive invocations behave just like
248 #define DO_REALLOC_FROM_ALLOCA(basevar, sizevar, needed_size, allocap, type) do \
250 /* Avoid side-effectualness. */ \
251 long do_realloc_needed_size = (needed_size); \
252 long do_realloc_newsize = (sizevar); \
253 while (do_realloc_newsize < do_realloc_needed_size) { \
254 do_realloc_newsize <<= 1; \
255 if (do_realloc_newsize < 16) \
256 do_realloc_newsize = 16; \
258 if (do_realloc_newsize != (sizevar)) \
261 XREALLOC_ARRAY (basevar, type, do_realloc_newsize); \
264 void *drfa_new_basevar = \
265 xmalloc (do_realloc_newsize * sizeof (type)); \
266 memcpy (drfa_new_basevar, basevar, (sizevar) * sizeof (type)); \
267 (basevar) = drfa_new_basevar; \
270 (sizevar) = do_realloc_newsize; \
274 /* Free FOO if it is non-NULL. */
275 #define FREE_MAYBE(foo) do { if (foo) xfree (foo); } while (0)
277 extern const char *exec_name;
279 /* Document type ("dt") flags */
282 TEXTHTML = 0x0001, /* document is of type text/html */
283 RETROKF = 0x0002, /* retrieval was OK */
284 HEAD_ONLY = 0x0004, /* only send the HEAD request */
285 SEND_NOCACHE = 0x0008, /* send Pragma: no-cache directive */
286 ACCEPTRANGES = 0x0010, /* Accept-ranges header was found */
287 ADDED_HTML_EXTENSION = 0x0020 /* added ".html" extension due to -E */
290 /* Universal error type -- used almost everywhere.
291 This is, of course, utter crock. */
294 NOCONERROR, HOSTERR, CONSOCKERR, CONERROR, CONSSLERR,
295 CONREFUSED, NEWLOCATION, NOTENOUGHMEM, CONPORTERR,
296 BINDERR, BINDOK, LISTENERR, ACCEPTERR, ACCEPTOK,
297 CONCLOSED, FTPOK, FTPLOGINC, FTPLOGREFUSED, FTPPORTERR,
298 FTPNSFOD, FTPRETROK, FTPUNKNOWNTYPE, FTPRERR,
299 FTPREXC, FTPSRVERR, FTPRETRINT, FTPRESTFAIL, URLERROR,
300 FOPENERR, FWRITEERR, HOK, HLEXC, HEOF,
301 HERR, RETROK, RECLEVELEXC, FTPACCDENIED, WRONGCODE,
302 FTPINVPASV, FTPNOPASV,
303 CONTNOTSUPPORTED, RETRUNNEEDED, RETRFINISHED, READERR, TRYLIMEXC,
304 URLBADPATTERN, FILEBADFILE, RANGEERR, RETRBADPATTERN,
305 RETNOTSUP, ROBOTSOK, NOROBOTS, PROXERR, AUTHFAILED,
306 QUOTEXC, WRITEFAILED,
307 SSLERRCERTFILE,SSLERRCERTKEY,SSLERRCTXCREATE
310 typedef unsigned char boolean;
318 /* So we can say strcmp(a, b) == EQ rather than strcmp(a, b) == 0 or
319 the really awful !strcmp(a, b). */
322 /* For most options, 0 means no limits, but with -p in the picture, that causes
323 a problem on the maximum recursion depth variable. To retain backwards
324 compatibility we allow users to consider "0" to be synonymous with "inf" for
325 -l, but internally infinite recursion is specified by -1 and 0 means to only
326 retrieve the requisites of a single document. */
327 #define INFINITE_RECURSION -1
329 #define CONNECT_ERROR(x) ((x) == ECONNREFUSED && !opt.retry_connrefused \
330 ? CONREFUSED : CONERROR)