]> sjero.net Git - wget/blob - src/wget.h
1236ff14368ecb0cc585fc06b4bbdea960520f9c
[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 Wget.
5
6 This program 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 This program 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 this program; 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 /* ASCII char -> HEX digit */
143 #define ASC2HEXD(x) (((x) >= '0' && (x) <= '9') ?               \
144                      ((x) - '0') : (TOUPPER(x) - 'A' + 10))
145
146 /* HEX digit -> ASCII char */
147 #define HEXD2ASC(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'A'))
148
149 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (*(array)))
150
151 /* Copy the data delimited with BEG and END to alloca-allocated
152    storage, and zero-terminate it.  BEG and END are evaluated only
153    once, in that order.  */
154 #define BOUNDED_TO_ALLOCA(beg, end, place) do { \
155   const char *DTA_beg = (beg);                  \
156   int DTA_len = (end) - DTA_beg;                \
157   place = alloca (DTA_len + 1);                 \
158   memcpy (place, DTA_beg, DTA_len);             \
159   place[DTA_len] = '\0';                        \
160 } while (0)
161
162 /* Return non-zero if string bounded between BEG and END is equal to
163    STRING_LITERAL.  The comparison is case-sensitive.  */
164 #define BOUNDED_EQUAL(beg, end, string_literal) \
165   ((end) - (beg) == sizeof (string_literal) - 1 \
166    && !memcmp ((beg), (string_literal),         \
167                sizeof (string_literal) - 1))
168
169 /* The same as above, except the comparison is case-insensitive. */
170 #define BOUNDED_EQUAL_NO_CASE(beg, end, string_literal) \
171   ((end) - (beg) == sizeof (string_literal) - 1         \
172    && !strncasecmp ((beg), (string_literal),            \
173                     sizeof (string_literal) - 1))
174
175 /* Note that this much more elegant definition cannot be used:
176
177    #define STRDUP_ALLOCA(str) (strcpy ((char *)alloca (strlen (str) + 1), str))
178
179    This is because some compilers don't handle alloca() as argument to
180    function correctly.  Gcc under Intel has been reported to offend in
181    this case.  */
182
183 #define STRDUP_ALLOCA(ptr, str) do {            \
184   (ptr) = (char *)alloca (strlen (str) + 1);    \
185   strcpy (ptr, str);                            \
186 } while (0)
187
188 #define ALLOCA_ARRAY(type, len) ((type *) alloca ((len) * sizeof (type)))
189
190 #define XREALLOC_ARRAY(ptr, type, len)                                  \
191      ((void) (ptr = (type *) xrealloc (ptr, (len) * sizeof (type))))
192
193 /* Generally useful if you want to avoid arbitrary size limits but
194    don't need a full dynamic array.  Assumes that BASEVAR points to a
195    malloced array of TYPE objects (or possibly a NULL pointer, if
196    SIZEVAR is 0), with the total size stored in SIZEVAR.  This macro
197    will realloc BASEVAR as necessary so that it can hold at least
198    NEEDED_SIZE objects.  The reallocing is done by doubling, which
199    ensures constant amortized time per element.  */
200 #define DO_REALLOC(basevar, sizevar, needed_size, type) do      \
201 {                                                               \
202   /* Avoid side-effectualness.  */                              \
203   long do_realloc_needed_size = (needed_size);                  \
204   long do_realloc_newsize = 0;                                  \
205   while ((sizevar) < (do_realloc_needed_size)) {                \
206     do_realloc_newsize = 2*(sizevar);                           \
207     if (do_realloc_newsize < 32)                                \
208       do_realloc_newsize = 32;                                  \
209     (sizevar) = do_realloc_newsize;                             \
210   }                                                             \
211   if (do_realloc_newsize)                                       \
212     XREALLOC_ARRAY (basevar, type, do_realloc_newsize);         \
213 } while (0)
214
215 /* Use this for small stack-allocated memory chunks that might grow.
216    The initial array is created using alloca(), and this macro
217    requests it to grow.  If the needed size is larger than the array,
218    this macro will use malloc to allocate it to new size, and copy the
219    old contents.  After that, successive invocations behave just like
220    DO_REALLOC.  */
221 #define DO_REALLOC_FROM_ALLOCA(basevar, sizevar, needed_size, allocap, type) do \
222 {                                                                               \
223   /* Avoid side-effectualness.  */                                              \
224   long do_realloc_needed_size = (needed_size);                                  \
225   long do_realloc_newsize = 0;                                                  \
226   while ((sizevar) < (do_realloc_needed_size)) {                                \
227     do_realloc_newsize = 2*(sizevar);                                           \
228     if (do_realloc_newsize < 16)                                                \
229       do_realloc_newsize = 16;                                                  \
230     (sizevar) = do_realloc_newsize;                                             \
231   }                                                                             \
232   if (do_realloc_newsize)                                                       \
233     {                                                                           \
234       if (!allocap)                                                             \
235         XREALLOC_ARRAY (basevar, type, do_realloc_newsize);                     \
236       else                                                                      \
237         {                                                                       \
238           void *drfa_new_basevar = xmalloc (do_realloc_newsize);                \
239           memcpy (drfa_new_basevar, basevar, sizevar);                          \
240           (basevar) = drfa_new_basevar;                                         \
241           allocap = 0;                                                          \
242         }                                                                       \
243     }                                                                           \
244 } while (0)
245
246 /* Free FOO if it is non-NULL.  */
247 #define FREE_MAYBE(foo) do { if (foo) xfree (foo); } while (0)
248
249 /* #### Hack: OPTIONS_DEFINED_HERE is defined in main.c.  */
250 /* [Is this weird hack really necessary on any compilers?  No ANSI C compiler
251     should complain about "extern const char *exec_name;" followed by
252     "const char *exec_name;".  Are we doing this for K&R compilers, or...??
253     -- Dan Harkless <wget@harkless.org>] */
254 #ifndef OPTIONS_DEFINED_HERE
255 extern const char *exec_name;
256 #endif
257
258 \f
259 /* Document type ("dt") flags */
260 enum
261 {
262   TEXTHTML             = 0x0001,        /* document is of type text/html */
263   RETROKF              = 0x0002,        /* retrieval was OK */
264   HEAD_ONLY            = 0x0004,        /* only send the HEAD request */
265   SEND_NOCACHE         = 0x0008,        /* send Pragma: no-cache directive */
266   ACCEPTRANGES         = 0x0010,        /* Accept-ranges header was found */
267   ADDED_HTML_EXTENSION = 0x0020         /* added ".html" extension due to -E */
268 };
269
270 /* Universal error type -- used almost everywhere.
271    This is, of course, utter crock.  */
272 typedef enum
273 {
274   NOCONERROR, HOSTERR, CONSOCKERR, CONERROR, CONSSLERR,
275   CONREFUSED, NEWLOCATION, NOTENOUGHMEM, CONPORTERR,
276   BINDERR, BINDOK, LISTENERR, ACCEPTERR, ACCEPTOK,
277   CONCLOSED, FTPOK, FTPLOGINC, FTPLOGREFUSED, FTPPORTERR,
278   FTPNSFOD, FTPRETROK, FTPUNKNOWNTYPE, FTPRERR,
279   FTPREXC, FTPSRVERR, FTPRETRINT, FTPRESTFAIL, URLHTTPS,
280   URLOK, URLHTTP, URLFTP, URLFILE, URLUNKNOWN, URLBADPORT,
281   URLBADHOST, FOPENERR, FWRITEERR, HOK, HLEXC, HEOF,
282   HERR, RETROK, RECLEVELEXC, FTPACCDENIED, WRONGCODE,
283   FTPINVPASV, FTPNOPASV,
284   CONTNOTSUPPORTED, RETRFINISHED, READERR, TRYLIMEXC,
285   URLBADPATTERN, FILEBADFILE, RANGEERR, RETRBADPATTERN,
286   RETNOTSUP, ROBOTSOK, NOROBOTS, PROXERR, AUTHFAILED,
287   QUOTEXC, WRITEFAILED,
288   SSLERRCERTFILE,SSLERRCERTKEY,SSLERRCTXCREATE
289 } uerr_t;
290
291 typedef unsigned char  boolean;
292 #ifndef FALSE
293 #define FALSE 0
294 #endif
295 #ifndef TRUE
296 #define TRUE  1
297 #endif
298
299 /* So we can say strcmp(a, b) == EQ rather than strcmp(a, b) == 0 or
300    the really awful !strcmp(a, b). */
301 #define EQ 0
302
303 /* For most options, 0 means no limits, but with -p in the picture, that causes
304    a problem on the maximum recursion depth variable.  To retain backwards
305    compatibility we allow users to consider "0" to be synonymous with "inf" for
306    -l, but internally infinite recursion is specified by -1 and 0 means to only
307    retrieve the requisites of a single document. */
308 #define INFINITE_RECURSION -1
309
310 #endif /* WGET_H */