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