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