]> sjero.net Git - wget/blob - src/wget.h
[svn] Fix memory leak in a rare case in url.c.
[wget] / src / wget.h
1 /* Miscellaneous declarations.
2    Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
3
4 This file is part of GNU Wget.
5
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.
10
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.
15
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.
19
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.  */
29
30 /* This file contains some declarations that don't fit anywhere else.
31    It also contains some useful includes, like the obnoxious TIME_H
32    inclusion.  */
33
34 #ifndef WGET_H
35 #define WGET_H
36
37 #ifndef DEBUG
38 # define NDEBUG /* To kill off assertions */
39 #endif /* not DEBUG */
40
41 /* Define this if you want primitive but extensive malloc debugging.
42    It will make Wget extremely slow, so only do it in development
43    builds.  */
44 #undef DEBUG_MALLOC
45
46 #ifndef PARAMS
47 # if PROTOTYPES
48 #  define PARAMS(args) args
49 # else
50 #  define PARAMS(args) ()
51 # endif
52 #endif
53
54 /* `gettext (FOO)' is long to write, so we use `_(FOO)'.  If NLS is
55    unavailable, _(STRING) simply returns STRING.  */
56 #ifdef HAVE_NLS
57 # define _(string) gettext (string)
58 # ifdef HAVE_LIBINTL_H
59 #  include <libintl.h>
60 # endif /* HAVE_LIBINTL_H */
61 #else  /* not HAVE_NLS */
62 # define _(string) string
63 #endif /* not HAVE_NLS */
64
65 /* No-op version of gettext, used for constant strings. */
66 #define N_(string) (string)
67
68 /* I18N NOTE: You will notice that none of the DEBUG messages are
69    marked as translatable.  This is intentional, for a few reasons:
70
71    1) The debug messages are not meant for the users to look at, but
72    for the developers; as such, they should be considered more like
73    source comments than real program output.
74
75    2) The messages are numerous, and yet they are random and frivolous
76    ("double yuck!" and such).  There would be a lot of work with no
77    gain.
78
79    3) Finally, the debug messages are meant to be a clue for me to
80    debug problems with Wget.  If I get them in a language I don't
81    understand, debugging will become a new challenge of its own!  */
82
83
84 /* Include these, so random files need not include them.  */
85 #include "sysdep.h"
86 #include "options.h"
87 /* locale independent replacement for ctype.h */
88 #include "safe-ctype.h"
89
90 #define DO_NOTHING do {} while (0)
91
92 /* Print X if debugging is enabled; a no-op otherwise.  */
93 #ifdef DEBUG
94 # define DEBUGP(x) do { if (opt.debug) { debug_logprintf x; } } while (0)
95 #else  /* not DEBUG */
96 # define DEBUGP(x) DO_NOTHING
97 #endif /* not DEBUG */
98
99 /* Make gcc check for the format of logmsg() and debug_logmsg().  */
100 #ifdef __GNUC__
101 # define GCC_FORMAT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
102 #else  /* not __GNUC__ */
103 # define GCC_FORMAT_ATTR(a, b)
104 #endif /* not __GNUC__ */
105
106 /* These are from log.c, but they are used everywhere, so we declare
107    them here.  */
108 enum log_options { LOG_VERBOSE, LOG_NOTQUIET, LOG_NONVERBOSE, LOG_ALWAYS };
109
110 #ifdef HAVE_STDARG_H
111 void logprintf PARAMS ((enum log_options, const char *, ...))
112      GCC_FORMAT_ATTR (2, 3);
113 void debug_logprintf PARAMS ((const char *, ...)) GCC_FORMAT_ATTR (1, 2);
114 #else  /* not HAVE_STDARG_H */
115 void logprintf ();
116 void debug_logprintf ();
117 #endif /* not HAVE_STDARG_H */
118 void logputs PARAMS ((enum log_options, const char *));
119 void logflush PARAMS ((void));
120 void log_set_flush PARAMS ((int));
121 int log_set_save_context PARAMS ((int));
122
123 /* Defined in `utils.c', but used literally everywhere.  */
124 #ifndef DEBUG_MALLOC
125
126 #define xmalloc  xmalloc_real
127 #define xrealloc xrealloc_real
128 #define xstrdup  xstrdup_real
129 #define xfree    free
130
131 void *xmalloc_real PARAMS ((size_t));
132 void *xrealloc_real PARAMS ((void *, size_t));
133 char *xstrdup_real PARAMS ((const char *));
134
135 #else  /* DEBUG_MALLOC */
136
137 #define xmalloc(s)     xmalloc_debug (s, __FILE__, __LINE__)
138 #define xfree(p)       xfree_debug (p, __FILE__, __LINE__)
139 #define xrealloc(p, s) xrealloc_debug (p, s, __FILE__, __LINE__)
140 #define xstrdup(p)     xstrdup_debug (p, __FILE__, __LINE__)
141
142 void *xmalloc_debug PARAMS ((size_t, const char *, int));
143 void xfree_debug PARAMS ((void *, const char *, int));
144 void *xrealloc_debug PARAMS ((void *, size_t, const char *, int));
145 char *xstrdup_debug PARAMS ((const char *, const char *, int));
146
147 #endif /* DEBUG_MALLOC */
148
149 /* #### Find a better place for this.  */
150 /* The log file to which Wget writes to after HUP.  */
151 #define DEFAULT_LOGFILE "wget-log"
152
153 #define MD5_HASHLEN 16
154 \f
155 /* Useful macros used across the code: */
156
157 /* Is the string a hpyhen-only?  */
158 #define HYPHENP(x) (*(x) == '-' && !*((x) + 1))
159
160 /* The smaller value of the two.  */
161 #define MINVAL(x, y) ((x) < (y) ? (x) : (y))
162
163 /* Convert an ASCII hex digit to the corresponding number between 0
164    and 15.  X should be a hexadecimal digit that satisfies isxdigit;
165    otherwise, the result is undefined.  */
166 #define XDIGIT_TO_NUM(x) ((x) < 'A' ? (x) - '0' : TOUPPER (x) - 'A' + 10)
167
168 /* Convert a sequence of ASCII hex digits X and Y to a number betewen
169    0 and 255.  Uses XDIGIT_TO_NUM for conversion of individual
170    digits.  */
171 #define X2DIGITS_TO_NUM(h1, h2) ((XDIGIT_TO_NUM (h1) << 4) + XDIGIT_TO_NUM (h2))
172
173 /* The reverse of the above: convert a number in the [0, 16) range to
174    its ASCII representation in hex.  The A-F characters are in upper
175    case.  */
176 #define XNUM_TO_DIGIT(x) ("0123456789ABCDEF"[x])
177
178 /* Like XNUM_TO_DIGIT, but generates lower-case characters. */
179 #define XNUM_TO_digit(x) ("0123456789abcdef"[x])
180
181 /* Returns the number of elements in an array with fixed
182    initialization.  For example:
183
184    static char a[] = "foo";     -- countof(a) == 4 (for terminating \0)
185
186    int a[5] = {1, 2};           -- countof(a) == 5
187
188    char *a[3] = {               -- countof(a) == 3
189      "foo", "bar", "baz"
190    };
191
192    And, most importantly, it works when the compiler counts the array
193    elements for you:
194
195    char *a[] = {                -- countof(a) == 4
196      "foo", "bar", "baz", "qux"
197    }  */
198 #define countof(array) (sizeof (array) / sizeof (*(array)))
199
200 /* Copy the data delimited with BEG and END to alloca-allocated
201    storage, and zero-terminate it.  Arguments are evaluated only once,
202    in the order BEG, END, PLACE.  */
203 #define BOUNDED_TO_ALLOCA(beg, end, place) do { \
204   const char *BTA_beg = (beg);                  \
205   int BTA_len = (end) - BTA_beg;                \
206   char **BTA_dest = &(place);                   \
207   *BTA_dest = alloca (BTA_len + 1);             \
208   memcpy (*BTA_dest, BTA_beg, BTA_len);         \
209   (*BTA_dest)[BTA_len] = '\0';                  \
210 } while (0)
211
212 /* Return non-zero if string bounded between BEG and END is equal to
213    STRING_LITERAL.  The comparison is case-sensitive.  */
214 #define BOUNDED_EQUAL(beg, end, string_literal) \
215   ((end) - (beg) == sizeof (string_literal) - 1 \
216    && !memcmp ((beg), (string_literal),         \
217                sizeof (string_literal) - 1))
218
219 /* The same as above, except the comparison is case-insensitive. */
220 #define BOUNDED_EQUAL_NO_CASE(beg, end, string_literal) \
221   ((end) - (beg) == sizeof (string_literal) - 1         \
222    && !strncasecmp ((beg), (string_literal),            \
223                     sizeof (string_literal) - 1))
224
225 /* Note that this much more elegant definition cannot be used:
226
227    #define STRDUP_ALLOCA(str) (strcpy ((char *)alloca (strlen (str) + 1), str))
228
229    This is because some compilers don't handle alloca() as argument to
230    function correctly.  Gcc under Intel has been reported to offend in
231    this case.  */
232
233 #define STRDUP_ALLOCA(ptr, str) do {            \
234   (ptr) = (char *)alloca (strlen (str) + 1);    \
235   strcpy (ptr, str);                            \
236 } while (0)
237
238 #define ALLOCA_ARRAY(type, len) ((type *) alloca ((len) * sizeof (type)))
239
240 #define XREALLOC_ARRAY(ptr, type, len)                                  \
241      ((void) (ptr = (type *) xrealloc (ptr, (len) * sizeof (type))))
242
243 /* Generally useful if you want to avoid arbitrary size limits but
244    don't need a full dynamic array.  Assumes that BASEVAR points to a
245    malloced array of TYPE objects (or possibly a NULL pointer, if
246    SIZEVAR is 0), with the total size stored in SIZEVAR.  This macro
247    will realloc BASEVAR as necessary so that it can hold at least
248    NEEDED_SIZE objects.  The reallocing is done by doubling, which
249    ensures constant amortized time per element.  */
250 #define DO_REALLOC(basevar, sizevar, needed_size, type) do      \
251 {                                                               \
252   /* Avoid side-effectualness.  */                              \
253   long do_realloc_needed_size = (needed_size);                  \
254   long do_realloc_newsize = 0;                                  \
255   while ((sizevar) < (do_realloc_needed_size)) {                \
256     do_realloc_newsize = 2*(sizevar);                           \
257     if (do_realloc_newsize < 32)                                \
258       do_realloc_newsize = 32;                                  \
259     (sizevar) = do_realloc_newsize;                             \
260   }                                                             \
261   if (do_realloc_newsize)                                       \
262     XREALLOC_ARRAY (basevar, type, do_realloc_newsize);         \
263 } while (0)
264
265 /* Use this for small stack-allocated memory chunks that might grow.
266    The initial array is created using alloca(), and this macro
267    requests it to grow.  If the needed size is larger than the array,
268    this macro will use malloc to allocate it to new size, and copy the
269    old contents.  After that, successive invocations behave just like
270    DO_REALLOC.  */
271 #define DO_REALLOC_FROM_ALLOCA(basevar, sizevar, needed_size, allocap, type) do \
272 {                                                                               \
273   /* Avoid side-effectualness.  */                                              \
274   long do_realloc_needed_size = (needed_size);                                  \
275   long do_realloc_newsize = (sizevar);                                          \
276   while (do_realloc_newsize < do_realloc_needed_size) {                         \
277     do_realloc_newsize <<= 1;                                                   \
278     if (do_realloc_newsize < 16)                                                \
279       do_realloc_newsize = 16;                                                  \
280   }                                                                             \
281   if (do_realloc_newsize != (sizevar))                                          \
282     {                                                                           \
283       if (!allocap)                                                             \
284         XREALLOC_ARRAY (basevar, type, do_realloc_newsize);                     \
285       else                                                                      \
286         {                                                                       \
287           void *drfa_new_basevar =                                              \
288                 xmalloc (do_realloc_newsize * sizeof (type));                   \
289           memcpy (drfa_new_basevar, basevar, (sizevar) * sizeof (type));        \
290           (basevar) = drfa_new_basevar;                                         \
291           allocap = 0;                                                          \
292         }                                                                       \
293       (sizevar) = do_realloc_newsize;                                           \
294     }                                                                           \
295 } while (0)
296
297 /* Free FOO if it is non-NULL.  */
298 #define FREE_MAYBE(foo) do { if (foo) xfree (foo); } while (0)
299
300 extern const char *exec_name;
301 \f
302 /* Document type ("dt") flags */
303 enum
304 {
305   TEXTHTML             = 0x0001,        /* document is of type text/html
306                                            or application/xhtml+xml */
307   RETROKF              = 0x0002,        /* retrieval was OK */
308   HEAD_ONLY            = 0x0004,        /* only send the HEAD request */
309   SEND_NOCACHE         = 0x0008,        /* send Pragma: no-cache directive */
310   ACCEPTRANGES         = 0x0010,        /* Accept-ranges header was found */
311   ADDED_HTML_EXTENSION = 0x0020         /* added ".html" extension due to -E */
312 };
313
314 /* Universal error type -- used almost everywhere.  Error reporting of
315    this detail is not generally used or needed and should be
316    simplified.  */
317 typedef enum
318 {
319   NOCONERROR, HOSTERR, CONSOCKERR, CONERROR, CONSSLERR,
320   CONREFUSED, NEWLOCATION, NOTENOUGHMEM, CONPORTERR,
321   BINDERR, BINDOK, LISTENERR, ACCEPTERR, ACCEPTOK,
322   CONCLOSED, FTPOK, FTPLOGINC, FTPLOGREFUSED, FTPPORTERR,
323   FTPNSFOD, FTPRETROK, FTPUNKNOWNTYPE, FTPRERR,
324   FTPREXC, FTPSRVERR, FTPRETRINT, FTPRESTFAIL, URLERROR,
325   FOPENERR, FWRITEERR, HOK, HLEXC, HEOF,
326   HERR, RETROK, RECLEVELEXC, FTPACCDENIED, WRONGCODE,
327   FTPINVPASV, FTPNOPASV,
328   CONTNOTSUPPORTED, RETRUNNEEDED, RETRFINISHED, READERR, TRYLIMEXC,
329   URLBADPATTERN, FILEBADFILE, RANGEERR, RETRBADPATTERN,
330   RETNOTSUP, ROBOTSOK, NOROBOTS, PROXERR, AUTHFAILED,
331   QUOTEXC, WRITEFAILED,
332   SSLERRCERTFILE,SSLERRCERTKEY,SSLERRCTXCREATE
333 } uerr_t;
334
335 typedef unsigned char  boolean;
336 #ifndef FALSE
337 #define FALSE 0
338 #endif
339 #ifndef TRUE
340 #define TRUE  1
341 #endif
342
343 /* So we can say strcmp(a, b) == EQ rather than strcmp(a, b) == 0 or
344    the really awful !strcmp(a, b). */
345 #define EQ 0
346
347 /* For most options, 0 means no limits, but with -p in the picture, that causes
348    a problem on the maximum recursion depth variable.  To retain backwards
349    compatibility we allow users to consider "0" to be synonymous with "inf" for
350    -l, but internally infinite recursion is specified by -1 and 0 means to only
351    retrieve the requisites of a single document. */
352 #define INFINITE_RECURSION -1
353
354 #define CONNECT_ERROR(x) ((x) == ECONNREFUSED && !opt.retry_connrefused \
355                           ? CONREFUSED : CONERROR)
356
357 #endif /* WGET_H */