]> sjero.net Git - wget/blob - src/wget.h
[svn] Implemented breadth-first retrieval.
[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
109 /* Defined in `utils.c', but used literally everywhere.  */
110 #ifndef DEBUG_MALLOC
111
112 #define xmalloc  xmalloc_real
113 #define xrealloc xrealloc_real
114 #define xstrdup  xstrdup_real
115 #define xfree    free
116
117 void *xmalloc_real PARAMS ((size_t));
118 void *xrealloc_real PARAMS ((void *, size_t));
119 char *xstrdup_real PARAMS ((const char *));
120
121 #else  /* DEBUG_MALLOC */
122
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__)
127
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));
132
133 #endif /* DEBUG_MALLOC */
134
135 /* #### Find a better place for this.  */
136 /* The log file to which Wget writes to after HUP.  */
137 #define DEFAULT_LOGFILE "wget-log"
138
139 #define MD5_HASHLEN 16
140 \f
141 /* Useful macros used across the code: */
142
143 /* Is the string a hpyhen-only?  */
144 #define HYPHENP(x) (*(x) == '-' && !*((x) + 1))
145
146 /* The smaller value of the two.  */
147 #define MINVAL(x, y) ((x) < (y) ? (x) : (y))
148
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))
156
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'))
161
162 /* Like XDIGIT_TO_XCHAR, but produce a lower-case char. */
163 #define XDIGIT_TO_xchar(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'a'))
164
165 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (*(array)))
166
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';                        \
176 } while (0)
177
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))
184
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))
190
191 /* Note that this much more elegant definition cannot be used:
192
193    #define STRDUP_ALLOCA(str) (strcpy ((char *)alloca (strlen (str) + 1), str))
194
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
197    this case.  */
198
199 #define STRDUP_ALLOCA(ptr, str) do {            \
200   (ptr) = (char *)alloca (strlen (str) + 1);    \
201   strcpy (ptr, str);                            \
202 } while (0)
203
204 #define ALLOCA_ARRAY(type, len) ((type *) alloca ((len) * sizeof (type)))
205
206 #define XREALLOC_ARRAY(ptr, type, len)                                  \
207      ((void) (ptr = (type *) xrealloc (ptr, (len) * sizeof (type))))
208
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      \
217 {                                                               \
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;                             \
226   }                                                             \
227   if (do_realloc_newsize)                                       \
228     XREALLOC_ARRAY (basevar, type, do_realloc_newsize);         \
229 } while (0)
230
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
236    DO_REALLOC.  */
237 #define DO_REALLOC_FROM_ALLOCA(basevar, sizevar, needed_size, allocap, type) do \
238 {                                                                               \
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;                                                  \
246   }                                                                             \
247   if (do_realloc_newsize != (sizevar))                                          \
248     {                                                                           \
249       if (!allocap)                                                             \
250         XREALLOC_ARRAY (basevar, type, do_realloc_newsize);                     \
251       else                                                                      \
252         {                                                                       \
253           void *drfa_new_basevar = xmalloc (do_realloc_newsize);                \
254           memcpy (drfa_new_basevar, basevar, (sizevar));                        \
255           (basevar) = drfa_new_basevar;                                         \
256           allocap = 0;                                                          \
257         }                                                                       \
258       (sizevar) = do_realloc_newsize;                                           \
259     }                                                                           \
260 } while (0)
261
262 /* Free FOO if it is non-NULL.  */
263 #define FREE_MAYBE(foo) do { if (foo) xfree (foo); } while (0)
264
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;
272 #endif
273
274 \f
275 /* Document type ("dt") flags */
276 enum
277 {
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 */
284 };
285
286 /* Universal error type -- used almost everywhere.
287    This is, of course, utter crock.  */
288 typedef enum
289 {
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
304 } uerr_t;
305
306 typedef unsigned char  boolean;
307 #ifndef FALSE
308 #define FALSE 0
309 #endif
310 #ifndef TRUE
311 #define TRUE  1
312 #endif
313
314 /* So we can say strcmp(a, b) == EQ rather than strcmp(a, b) == 0 or
315    the really awful !strcmp(a, b). */
316 #define EQ 0
317
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
324
325 #endif /* WGET_H */