]> sjero.net Git - wget/blob - src/wget.h
6c41d0e229389e2d7f1152d9834382202e9887d8
[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
70 #define DO_NOTHING do {} while (0)
71
72 /* Print X if debugging is enabled; a no-op otherwise.  */
73 #ifdef DEBUG
74 # define DEBUGP(x) do { if (opt.debug) { debug_logprintf x; } } while (0)
75 #else  /* not DEBUG */
76 # define DEBUGP(x) DO_NOTHING
77 #endif /* not DEBUG */
78
79 /* Make gcc check for the format of logmsg() and debug_logmsg().  */
80 #ifdef __GNUC__
81 # define GCC_FORMAT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
82 #else  /* not __GNUC__ */
83 # define GCC_FORMAT_ATTR(a, b)
84 #endif /* not __GNUC__ */
85
86 /* These are from log.c, but they are used everywhere, so we declare
87    them here.  */
88 enum log_options { LOG_VERBOSE, LOG_NOTQUIET, LOG_NONVERBOSE, LOG_ALWAYS };
89
90 #ifdef HAVE_STDARG_H
91 void logprintf PARAMS ((enum log_options, const char *, ...))
92      GCC_FORMAT_ATTR (2, 3);
93 void debug_logprintf PARAMS ((const char *, ...)) GCC_FORMAT_ATTR (1, 2);
94 #else  /* not HAVE_STDARG_H */
95 void logprintf ();
96 void debug_logprintf ();
97 #endif /* not HAVE_STDARG_H */
98 void logputs PARAMS ((enum log_options, const char *));
99
100 /* Defined in `utils.c', but used literally everywhere.  */
101 void *xmalloc PARAMS ((size_t));
102 void xfree PARAMS ((void *));
103 void *xrealloc PARAMS ((void *, size_t));
104 char *xstrdup PARAMS ((const char *));
105
106 /* #### Find a better place for this.  */
107 /* The log file to which Wget writes to after HUP.  */
108 #define DEFAULT_LOGFILE "wget-log"
109
110 #define MD5_HASHLEN 16
111 \f
112 /* Useful macros used across the code: */
113
114 /* Is the string a hpyhen-only?  */
115 #define HYPHENP(x) (*(x) == '-' && !*((x) + 1))
116
117 /* The smaller value of the two.  */
118 #define MINVAL(x, y) ((x) < (y) ? (x) : (y))
119
120 /* ASCII char -> HEX digit */
121 #define ASC2HEXD(x) (((x) >= '0' && (x) <= '9') ?               \
122                      ((x) - '0') : (TOUPPER(x) - 'A' + 10))
123
124 /* HEX digit -> ASCII char */
125 #define HEXD2ASC(x) (((x) < 10) ? ((x) + '0') : ((x) - 10 + 'A'))
126
127 #define ARRAY_SIZE(array) (sizeof (array) / sizeof (*(array)))
128
129 /* Note that this much more elegant definition cannot be used:
130
131    #define STRDUP_ALLOCA(str) (strcpy ((char *)alloca (strlen (str) + 1), str))
132
133    This is because some compilers don't handle alloca() as argument to
134    function correctly.  Gcc under Intel has been reported to offend in
135    this case.  */
136
137 #define STRDUP_ALLOCA(ptr, str) do {            \
138   (ptr) = (char *)alloca (strlen (str) + 1);    \
139   strcpy (ptr, str);                            \
140 } while (0)
141
142 #define ALLOCA_ARRAY(type, len) ((type *) alloca ((len) * sizeof (type)))
143
144 #define XREALLOC_ARRAY(ptr, type, len)                                  \
145      ((void) (ptr = (type *) xrealloc (ptr, (len) * sizeof (type))))
146
147 /* Generally useful if you want to avoid arbitrary size limits but
148    don't need a full dynamic array.  Assumes that BASEVAR points to a
149    malloced array of TYPE objects (or possibly a NULL pointer, if
150    SIZEVAR is 0), with the total size stored in SIZEVAR.  This macro
151    will realloc BASEVAR as necessary so that it can hold at least
152    NEEDED_SIZE objects.  The reallocing is done by doubling, which
153    ensures constant amortized time per element.  */
154 #define DO_REALLOC(basevar, sizevar, needed_size, type) do      \
155 {                                                               \
156   /* Avoid side-effectualness.  */                              \
157   long do_realloc_needed_size = (needed_size);                  \
158   long do_realloc_newsize = 0;                                  \
159   while ((sizevar) < (do_realloc_needed_size)) {                \
160     do_realloc_newsize = 2*(sizevar);                           \
161     if (do_realloc_newsize < 32)                                \
162       do_realloc_newsize = 32;                                  \
163     (sizevar) = do_realloc_newsize;                             \
164   }                                                             \
165   if (do_realloc_newsize)                                       \
166     XREALLOC_ARRAY (basevar, type, do_realloc_newsize);         \
167 } while (0)
168
169 /* Use this for small stack-allocated memory chunks that might grow.
170    The initial array is created using alloca(), and this macro
171    requests it to grow.  If the needed size is larger than the array,
172    this macro will use malloc to allocate it to new size, and copy the
173    old contents.  After that, successive invocations behave just like
174    DO_REALLOC.  */
175 #define DO_REALLOC_FROM_ALLOCA(basevar, sizevar, needed_size, allocap, type) do \
176 {                                                                               \
177   /* Avoid side-effectualness.  */                                              \
178   long do_realloc_needed_size = (needed_size);                                  \
179   long do_realloc_newsize = 0;                                                  \
180   while ((sizevar) < (do_realloc_needed_size)) {                                \
181     do_realloc_newsize = 2*(sizevar);                                           \
182     if (do_realloc_newsize < 16)                                                \
183       do_realloc_newsize = 16;                                                  \
184     (sizevar) = do_realloc_newsize;                                             \
185   }                                                                             \
186   if (do_realloc_newsize)                                                       \
187     {                                                                           \
188       if (!allocap)                                                             \
189         XREALLOC_ARRAY (basevar, type, do_realloc_newsize);                     \
190       else                                                                      \
191         {                                                                       \
192           void *drfa_new_basevar = xmalloc (do_realloc_newsize);                \
193           memcpy (drfa_new_basevar, basevar, sizevar);                          \
194           (basevar) = drfa_new_basevar;                                         \
195           allocap = 0;                                                          \
196         }                                                                       \
197     }                                                                           \
198 } while (0)
199
200 /* Free FOO if it is non-NULL.  */
201 #define FREE_MAYBE(foo) do { if (foo) xfree (foo); } while (0)
202
203 /* #### Hack: OPTIONS_DEFINED_HERE is defined in main.c.  */
204 /* [Is this weird hack really necessary on any compilers?  No ANSI C compiler
205     should complain about "extern const char *exec_name;" followed by
206     "const char *exec_name;".  Are we doing this for K&R compilers, or...??
207     -- Dan Harkless <dan-wget@dilvish.speed.net>] */
208 #ifndef OPTIONS_DEFINED_HERE
209 extern const char *exec_name;
210 #endif
211
212 \f
213 /* Document type ("dt") flags */
214 enum
215 {
216   TEXTHTML             = 0x0001,        /* document is of type text/html */
217   RETROKF              = 0x0002,        /* retrieval was OK */
218   HEAD_ONLY            = 0x0004,        /* only send the HEAD request */
219   SEND_NOCACHE         = 0x0008,        /* send Pragma: no-cache directive */
220   ACCEPTRANGES         = 0x0010,        /* Accept-ranges header was found */
221   ADDED_HTML_EXTENSION = 0x0020         /* added ".html" extension due to -E */
222 };
223
224 /* Universal error type -- used almost everywhere.
225    This is, of course, utter crock.  */
226 typedef enum
227 {
228   NOCONERROR, HOSTERR, CONSOCKERR, CONERROR,
229   CONREFUSED, NEWLOCATION, NOTENOUGHMEM, CONPORTERR,
230   BINDERR, BINDOK, LISTENERR, ACCEPTERR, ACCEPTOK,
231   CONCLOSED, FTPOK, FTPLOGINC, FTPLOGREFUSED, FTPPORTERR,
232   FTPNSFOD, FTPRETROK, FTPUNKNOWNTYPE, FTPRERR,
233   FTPREXC, FTPSRVERR, FTPRETRINT, FTPRESTFAIL,
234   URLOK, URLHTTP, URLFTP, URLFILE, URLUNKNOWN, URLBADPORT,
235   URLBADHOST, FOPENERR, FWRITEERR, HOK, HLEXC, HEOF,
236   HERR, RETROK, RECLEVELEXC, FTPACCDENIED, WRONGCODE,
237   FTPINVPASV, FTPNOPASV,
238   RETRFINISHED, READERR, TRYLIMEXC, URLBADPATTERN,
239   FILEBADFILE, RANGEERR, RETRBADPATTERN, RETNOTSUP,
240   ROBOTSOK, NOROBOTS, PROXERR, AUTHFAILED, QUOTEXC, WRITEFAILED
241 } uerr_t;
242
243 typedef unsigned char  boolean;
244 #ifndef FALSE
245 #define FALSE 0
246 #endif
247 #ifndef TRUE
248 #define TRUE  1
249 #endif
250
251 /* So we can say strcmp(a, b) == EQ rather than strcmp(a, b) == 0 or
252    the really awful !strcmp(a, b). */
253 #define EQ 0
254
255 /* For most options, 0 means no limits, but with -p in the picture, that causes
256    a problem on the maximum recursion depth variable.  To retain backwards
257    compatibility we allow users to consider "0" to be synonymous with "inf" for
258    -l, but internally infinite recursion is specified by -1 and 0 means to only
259    retrieve the requisites of a single document. */
260 #define INFINITE_RECURSION -1
261
262 #endif /* WGET_H */