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