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