]> sjero.net Git - wget/blob - src/wget.h
[svn] (DO_REALLOC_FROM_ALLOCA): Multiply with sizeof(type) when calling
[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 /* This file contains some declarations that don't fit anywhere else.
21    It also contains some useful includes, like the obnoxious TIME_H
22    inclusion.  */
23
24 #ifndef WGET_H
25 #define WGET_H
26
27 #ifndef DEBUG
28 # define NDEBUG /* To kill off assertions */
29 #endif /* not DEBUG */
30
31 /* Define this if you want primitive but extensive malloc debugging.
32    It will make Wget extremely slow, so only do it in development
33    builds.  */
34 #undef DEBUG_MALLOC
35
36 #ifndef PARAMS
37 # if PROTOTYPES
38 #  define PARAMS(args) args
39 # else
40 #  define PARAMS(args) ()
41 # endif
42 #endif
43
44 /* `gettext (FOO)' is long to write, so we use `_(FOO)'.  If NLS is
45    unavailable, _(STRING) simply returns STRING.  */
46 #ifdef HAVE_NLS
47 # define _(string) gettext (string)
48 # ifdef HAVE_LIBINTL_H
49 #  include <libintl.h>
50 # endif /* HAVE_LIBINTL_H */
51 #else  /* not HAVE_NLS */
52 # define _(string) string
53 #endif /* not HAVE_NLS */
54
55 /* I18N NOTE: You will notice that none of the DEBUG messages are
56    marked as translatable.  This is intentional, for a few reasons:
57
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.
61
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
64    gain.
65
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!  */
69
70
71 /* Include these, so random files need not include them.  */
72 #include "sysdep.h"
73 #include "options.h"
74 /* locale independent replacement for ctype.h */
75 #include "safe-ctype.h"
76
77 #define DO_NOTHING do {} while (0)
78
79 /* Print X if debugging is enabled; a no-op otherwise.  */
80 #ifdef DEBUG
81 # define DEBUGP(x) do { if (opt.debug) { debug_logprintf x; } } while (0)
82 #else  /* not DEBUG */
83 # define DEBUGP(x) DO_NOTHING
84 #endif /* not DEBUG */
85
86 /* Make gcc check for the format of logmsg() and debug_logmsg().  */
87 #ifdef __GNUC__
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__ */
92
93 /* These are from log.c, but they are used everywhere, so we declare
94    them here.  */
95 enum log_options { LOG_VERBOSE, LOG_NOTQUIET, LOG_NONVERBOSE, LOG_ALWAYS };
96
97 #ifdef HAVE_STDARG_H
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 */
102 void logprintf ();
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));
108 int log_set_save_context PARAMS ((int));
109
110 /* Defined in `utils.c', but used literally everywhere.  */
111 #ifndef DEBUG_MALLOC
112
113 #define xmalloc  xmalloc_real
114 #define xrealloc xrealloc_real
115 #define xstrdup  xstrdup_real
116 #define xfree    free
117
118 void *xmalloc_real PARAMS ((size_t));
119 void *xrealloc_real PARAMS ((void *, size_t));
120 char *xstrdup_real PARAMS ((const char *));
121
122 #else  /* DEBUG_MALLOC */
123
124 #define xmalloc(s)     xmalloc_debug (s, __FILE__, __LINE__)
125 #define xfree(p)       xfree_debug (p, __FILE__, __LINE__)
126 #define xrealloc(p, s) xrealloc_debug (p, s, __FILE__, __LINE__)
127 #define xstrdup(p)     xstrdup_debug (p, __FILE__, __LINE__)
128
129 void *xmalloc_debug PARAMS ((size_t, const char *, int));
130 void xfree_debug PARAMS ((void *, const char *, int));
131 void *xrealloc_debug PARAMS ((void *, size_t, const char *, int));
132 char *xstrdup_debug PARAMS ((const char *, const char *, int));
133
134 #endif /* DEBUG_MALLOC */
135
136 /* #### Find a better place for this.  */
137 /* The log file to which Wget writes to after HUP.  */
138 #define DEFAULT_LOGFILE "wget-log"
139
140 #define MD5_HASHLEN 16
141 \f
142 /* Useful macros used across the code: */
143
144 /* Is the string a hpyhen-only?  */
145 #define HYPHENP(x) (*(x) == '-' && !*((x) + 1))
146
147 /* The smaller value of the two.  */
148 #define MINVAL(x, y) ((x) < (y) ? (x) : (y))
149
150 /* Convert the ASCII character X to a hex-digit.  X should be between
151    '0' and '9', or between 'A' and 'F', or between 'a' and 'f'.  The
152    result is a number between 0 and 15.  If X is not a hexadecimal
153    digit character, the result is undefined.  */
154 #define XCHAR_TO_XDIGIT(x)                      \
155   (((x) >= '0' && (x) <= '9') ?                 \
156    ((x) - '0') : (TOUPPER(x) - 'A' + 10))
157
158 /* The reverse of the above: convert a HEX digit in the [0, 15] range
159    to an ASCII character representing it.  The A-F characters are
160    always in upper case.  */
161 #define XDIGIT_TO_XCHAR(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'A'))
162
163 /* Like XDIGIT_TO_XCHAR, but produce a lower-case char. */
164 #define XDIGIT_TO_xchar(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'a'))
165
166 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (*(array)))
167
168 /* Copy the data delimited with BEG and END to alloca-allocated
169    storage, and zero-terminate it.  BEG and END are evaluated only
170    once, in that order.  */
171 #define BOUNDED_TO_ALLOCA(beg, end, place) do { \
172   const char *DTA_beg = (beg);                  \
173   int DTA_len = (end) - DTA_beg;                \
174   place = alloca (DTA_len + 1);                 \
175   memcpy (place, DTA_beg, DTA_len);             \
176   place[DTA_len] = '\0';                        \
177 } while (0)
178
179 /* Return non-zero if string bounded between BEG and END is equal to
180    STRING_LITERAL.  The comparison is case-sensitive.  */
181 #define BOUNDED_EQUAL(beg, end, string_literal) \
182   ((end) - (beg) == sizeof (string_literal) - 1 \
183    && !memcmp ((beg), (string_literal),         \
184                sizeof (string_literal) - 1))
185
186 /* The same as above, except the comparison is case-insensitive. */
187 #define BOUNDED_EQUAL_NO_CASE(beg, end, string_literal) \
188   ((end) - (beg) == sizeof (string_literal) - 1         \
189    && !strncasecmp ((beg), (string_literal),            \
190                     sizeof (string_literal) - 1))
191
192 /* Note that this much more elegant definition cannot be used:
193
194    #define STRDUP_ALLOCA(str) (strcpy ((char *)alloca (strlen (str) + 1), str))
195
196    This is because some compilers don't handle alloca() as argument to
197    function correctly.  Gcc under Intel has been reported to offend in
198    this case.  */
199
200 #define STRDUP_ALLOCA(ptr, str) do {            \
201   (ptr) = (char *)alloca (strlen (str) + 1);    \
202   strcpy (ptr, str);                            \
203 } while (0)
204
205 #define ALLOCA_ARRAY(type, len) ((type *) alloca ((len) * sizeof (type)))
206
207 #define XREALLOC_ARRAY(ptr, type, len)                                  \
208      ((void) (ptr = (type *) xrealloc (ptr, (len) * sizeof (type))))
209
210 /* Generally useful if you want to avoid arbitrary size limits but
211    don't need a full dynamic array.  Assumes that BASEVAR points to a
212    malloced array of TYPE objects (or possibly a NULL pointer, if
213    SIZEVAR is 0), with the total size stored in SIZEVAR.  This macro
214    will realloc BASEVAR as necessary so that it can hold at least
215    NEEDED_SIZE objects.  The reallocing is done by doubling, which
216    ensures constant amortized time per element.  */
217 #define DO_REALLOC(basevar, sizevar, needed_size, type) do      \
218 {                                                               \
219   /* Avoid side-effectualness.  */                              \
220   long do_realloc_needed_size = (needed_size);                  \
221   long do_realloc_newsize = 0;                                  \
222   while ((sizevar) < (do_realloc_needed_size)) {                \
223     do_realloc_newsize = 2*(sizevar);                           \
224     if (do_realloc_newsize < 32)                                \
225       do_realloc_newsize = 32;                                  \
226     (sizevar) = do_realloc_newsize;                             \
227   }                                                             \
228   if (do_realloc_newsize)                                       \
229     XREALLOC_ARRAY (basevar, type, do_realloc_newsize);         \
230 } while (0)
231
232 /* Use this for small stack-allocated memory chunks that might grow.
233    The initial array is created using alloca(), and this macro
234    requests it to grow.  If the needed size is larger than the array,
235    this macro will use malloc to allocate it to new size, and copy the
236    old contents.  After that, successive invocations behave just like
237    DO_REALLOC.  */
238 #define DO_REALLOC_FROM_ALLOCA(basevar, sizevar, needed_size, allocap, type) do \
239 {                                                                               \
240   /* Avoid side-effectualness.  */                                              \
241   long do_realloc_needed_size = (needed_size);                                  \
242   long do_realloc_newsize = (sizevar);                                          \
243   while (do_realloc_newsize < do_realloc_needed_size) {                         \
244     do_realloc_newsize <<= 1;                                                   \
245     if (do_realloc_newsize < 16)                                                \
246       do_realloc_newsize = 16;                                                  \
247   }                                                                             \
248   if (do_realloc_newsize != (sizevar))                                          \
249     {                                                                           \
250       if (!allocap)                                                             \
251         XREALLOC_ARRAY (basevar, type, do_realloc_newsize);                     \
252       else                                                                      \
253         {                                                                       \
254           void *drfa_new_basevar =                                              \
255                 xmalloc (do_realloc_newsize * sizeof (type));                   \
256           memcpy (drfa_new_basevar, basevar, (sizevar) * sizeof (type));        \
257           (basevar) = drfa_new_basevar;                                         \
258           allocap = 0;                                                          \
259         }                                                                       \
260       (sizevar) = do_realloc_newsize;                                           \
261     }                                                                           \
262 } while (0)
263
264 /* Free FOO if it is non-NULL.  */
265 #define FREE_MAYBE(foo) do { if (foo) xfree (foo); } while (0)
266
267 /* #### Hack: OPTIONS_DEFINED_HERE is defined in main.c.  */
268 /* [Is this weird hack really necessary on any compilers?  No ANSI C compiler
269     should complain about "extern const char *exec_name;" followed by
270     "const char *exec_name;".  Are we doing this for K&R compilers, or...??
271     -- Dan Harkless <wget@harkless.org>] */
272 #ifndef OPTIONS_DEFINED_HERE
273 extern const char *exec_name;
274 #endif
275
276 \f
277 /* Document type ("dt") flags */
278 enum
279 {
280   TEXTHTML             = 0x0001,        /* document is of type text/html */
281   RETROKF              = 0x0002,        /* retrieval was OK */
282   HEAD_ONLY            = 0x0004,        /* only send the HEAD request */
283   SEND_NOCACHE         = 0x0008,        /* send Pragma: no-cache directive */
284   ACCEPTRANGES         = 0x0010,        /* Accept-ranges header was found */
285   ADDED_HTML_EXTENSION = 0x0020         /* added ".html" extension due to -E */
286 };
287
288 /* Universal error type -- used almost everywhere.
289    This is, of course, utter crock.  */
290 typedef enum
291 {
292   NOCONERROR, HOSTERR, CONSOCKERR, CONERROR, CONSSLERR,
293   CONREFUSED, NEWLOCATION, NOTENOUGHMEM, CONPORTERR,
294   BINDERR, BINDOK, LISTENERR, ACCEPTERR, ACCEPTOK,
295   CONCLOSED, FTPOK, FTPLOGINC, FTPLOGREFUSED, FTPPORTERR,
296   FTPNSFOD, FTPRETROK, FTPUNKNOWNTYPE, FTPRERR,
297   FTPREXC, FTPSRVERR, FTPRETRINT, FTPRESTFAIL, URLERROR,
298   FOPENERR, FWRITEERR, HOK, HLEXC, HEOF,
299   HERR, RETROK, RECLEVELEXC, FTPACCDENIED, WRONGCODE,
300   FTPINVPASV, FTPNOPASV,
301   CONTNOTSUPPORTED, RETRUNNEEDED, RETRFINISHED, READERR, TRYLIMEXC,
302   URLBADPATTERN, FILEBADFILE, RANGEERR, RETRBADPATTERN,
303   RETNOTSUP, ROBOTSOK, NOROBOTS, PROXERR, AUTHFAILED,
304   QUOTEXC, WRITEFAILED,
305   SSLERRCERTFILE,SSLERRCERTKEY,SSLERRCTXCREATE
306 } uerr_t;
307
308 typedef unsigned char  boolean;
309 #ifndef FALSE
310 #define FALSE 0
311 #endif
312 #ifndef TRUE
313 #define TRUE  1
314 #endif
315
316 /* So we can say strcmp(a, b) == EQ rather than strcmp(a, b) == 0 or
317    the really awful !strcmp(a, b). */
318 #define EQ 0
319
320 /* For most options, 0 means no limits, but with -p in the picture, that causes
321    a problem on the maximum recursion depth variable.  To retain backwards
322    compatibility we allow users to consider "0" to be synonymous with "inf" for
323    -l, but internally infinite recursion is specified by -1 and 0 means to only
324    retrieve the requisites of a single document. */
325 #define INFINITE_RECURSION -1
326
327 #endif /* WGET_H */