]> sjero.net Git - wget/blob - src/wget.h
[svn] Retired the `boolean' type. Renamed FREE_MAYBE to xfree_null and moved the
[wget] / src / wget.h
1 /* Miscellaneous declarations.
2    Copyright (C) 1995, 1996, 1997, 1998, 2003 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 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables.  You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL".  If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so.  If you do not wish to do
28 so, delete this exception statement from your version.  */
29
30 /* This file contains declarations that are universally useful and
31    those that don't fit elsewhere.  It also includes sysdep.h which
32    includes some often-needed system includes, like the obnoxious
33    <time.h> inclusion.  */
34
35 #ifndef WGET_H
36 #define WGET_H
37
38 /* Disable assertions when debug support is not compiled in. */
39 #ifndef ENABLE_DEBUG
40 # define NDEBUG
41 #endif
42
43 /* Define this if you want primitive but extensive malloc debugging.
44    It will make Wget extremely slow, so only do it in development
45    builds.  */
46 #undef DEBUG_MALLOC
47
48 #ifndef PARAMS
49 # if PROTOTYPES
50 #  define PARAMS(args) args
51 # else
52 #  define PARAMS(args) ()
53 # endif
54 #endif
55
56 /* `gettext (FOO)' is long to write, so we use `_(FOO)'.  If NLS is
57    unavailable, _(STRING) simply returns STRING.  */
58 #ifdef HAVE_NLS
59 # define _(string) gettext (string)
60 # ifdef HAVE_LIBINTL_H
61 #  include <libintl.h>
62 # endif /* HAVE_LIBINTL_H */
63 #else  /* not HAVE_NLS */
64 # define _(string) (string)
65 #endif /* not HAVE_NLS */
66
67 /* No-op version of gettext, used for constant strings. */
68 #define N_(string) (string)
69
70 /* I18N NOTE: You will notice that none of the DEBUGP messages are
71    marked as translatable.  This is intentional, for a few reasons:
72
73    1) The debug messages are not meant for the users to look at, but
74    for the developers; as such, they should be considered more like
75    source comments than real program output.
76
77    2) The messages are numerous, and yet they are random and frivolous
78    ("double yuck!" and such).  There would be a lot of work with no
79    gain.
80
81    3) Finally, the debug messages are meant to be a clue for me to
82    debug problems with Wget.  If I get them in a language I don't
83    understand, debugging will become a new challenge of its own!  */
84
85
86 /* Include these, so random files need not include them.  */
87 #include "sysdep.h"
88 #include "options.h"
89 /* locale independent replacement for ctype.h */
90 #include "safe-ctype.h"
91
92 #define DO_NOTHING do {} while (0)
93
94 /* Print X if debugging is enabled; a no-op otherwise.  */
95 #ifdef ENABLE_DEBUG
96 # define DEBUGP(x) do { if (opt.debug) { debug_logprintf x; } } while (0)
97 #else  /* not ENABLE_DEBUG */
98 # define DEBUGP(x) DO_NOTHING
99 #endif /* not ENABLE_DEBUG */
100
101 /* Everything uses this, so include them here directly.  */
102 #include "xmalloc.h"
103
104 /* Likewise for logging functions.  */
105 #include "log.h"
106 \f
107 /* Useful macros used across the code: */
108
109 /* The number of elements in an array.  For example:
110    static char a[] = "foo";     -- countof(a) == 4 (note terminating \0)
111    int a[5] = {1, 2};           -- countof(a) == 5
112    char *a[] = {                -- countof(a) == 3
113      "foo", "bar", "baz"
114    }; */
115 #define countof(array) (sizeof (array) / sizeof ((array)[0]))
116
117 /* Zero out a value.  */
118 #define xzero(x) memset (&(x), '\0', sizeof (x))
119
120 /* Convert an ASCII hex digit to the corresponding number between 0
121    and 15.  H should be a hexadecimal digit that satisfies isxdigit;
122    otherwise, the result is undefined.  */
123 #define XDIGIT_TO_NUM(h) ((h) < 'A' ? (h) - '0' : TOUPPER (h) - 'A' + 10)
124 #define X2DIGITS_TO_NUM(h1, h2) ((XDIGIT_TO_NUM (h1) << 4) + XDIGIT_TO_NUM (h2))
125
126 /* The reverse of the above: convert a number in the [0, 16) range to
127    the ASCII representation of the corresponding hexadecimal digit.
128    `+ 0' is there so you can't accidentally use it as an lvalue.  */
129 #define XNUM_TO_DIGIT(x) ("0123456789ABCDEF"[x] + 0)
130 #define XNUM_TO_digit(x) ("0123456789abcdef"[x] + 0)
131
132 /* Copy the data delimited with BEG and END to alloca-allocated
133    storage, and zero-terminate it.  Arguments are evaluated only once,
134    in the order BEG, END, PLACE.  */
135 #define BOUNDED_TO_ALLOCA(beg, end, place) do { \
136   const char *BTA_beg = (beg);                  \
137   int BTA_len = (end) - BTA_beg;                \
138   char **BTA_dest = &(place);                   \
139   *BTA_dest = alloca (BTA_len + 1);             \
140   memcpy (*BTA_dest, BTA_beg, BTA_len);         \
141   (*BTA_dest)[BTA_len] = '\0';                  \
142 } while (0)
143
144 /* Return non-zero if string bounded between BEG and END is equal to
145    STRING_LITERAL.  The comparison is case-sensitive.  */
146 #define BOUNDED_EQUAL(beg, end, string_literal)                         \
147   ((end) - (beg) == sizeof (string_literal) - 1                         \
148    && !memcmp (beg, string_literal, sizeof (string_literal) - 1))
149
150 /* The same as above, except the comparison is case-insensitive. */
151 #define BOUNDED_EQUAL_NO_CASE(beg, end, string_literal)                 \
152   ((end) - (beg) == sizeof (string_literal) - 1                         \
153    && !strncasecmp (beg, string_literal, sizeof (string_literal) - 1))
154
155 /* Like ptr=strdup(str), but allocates the space for PTR on the stack.
156    This cannot be an expression because this is not portable:
157      #define STRDUP_ALLOCA(str) (strcpy (alloca (strlen (str) + 1), str))
158    The problem is that some compilers can't handle alloca() being an
159    argument to a function.  */
160
161 #define STRDUP_ALLOCA(ptr, str) do {                    \
162   char **SA_dest = &(ptr);                              \
163   const char *SA_src = (str);                           \
164   *SA_dest = (char *)alloca (strlen (SA_src) + 1);      \
165   strcpy (*SA_dest, SA_src);                            \
166 } while (0)
167
168 /* Generally useful if you want to avoid arbitrary size limits but
169    don't need a full dynamic array.  Assumes that BASEVAR points to a
170    malloced array of TYPE objects (or possibly a NULL pointer, if
171    SIZEVAR is 0), with the total size stored in SIZEVAR.  This macro
172    will realloc BASEVAR as necessary so that it can hold at least
173    NEEDED_SIZE objects.  The reallocing is done by doubling, which
174    ensures constant amortized time per element.  */
175
176 #define DO_REALLOC(basevar, sizevar, needed_size, type) do {            \
177   long DR_needed_size = (needed_size);                                  \
178   long DR_newsize = 0;                                                  \
179   while ((sizevar) < (DR_needed_size)) {                                \
180     DR_newsize = sizevar << 1;                                          \
181     if (DR_newsize < 16)                                                \
182       DR_newsize = 16;                                                  \
183     (sizevar) = DR_newsize;                                             \
184   }                                                                     \
185   if (DR_newsize)                                                       \
186     basevar = (type *)xrealloc (basevar, DR_newsize * sizeof (type));   \
187 } while (0)
188
189 extern const char *exec_name;
190 \f
191 /* Document type ("dt") flags */
192 enum
193 {
194   TEXTHTML             = 0x0001,        /* document is of type text/html
195                                            or application/xhtml+xml */
196   RETROKF              = 0x0002,        /* retrieval was OK */
197   HEAD_ONLY            = 0x0004,        /* only send the HEAD request */
198   SEND_NOCACHE         = 0x0008,        /* send Pragma: no-cache directive */
199   ACCEPTRANGES         = 0x0010,        /* Accept-ranges header was found */
200   ADDED_HTML_EXTENSION = 0x0020         /* added ".html" extension due to -E */
201 };
202
203 /* Universal error type -- used almost everywhere.  Error reporting of
204    this detail is not generally used or needed and should be
205    simplified.  */
206 typedef enum
207 {
208   NOCONERROR, HOSTERR, CONSOCKERR, CONERROR, CONSSLERR,
209   CONIMPOSSIBLE, NEWLOCATION, NOTENOUGHMEM, CONPORTERR,
210   BINDERR, BINDOK, LISTENERR, ACCEPTERR, ACCEPTOK,
211   CONCLOSED, FTPOK, FTPLOGINC, FTPLOGREFUSED, FTPPORTERR,
212   FTPNSFOD, FTPRETROK, FTPUNKNOWNTYPE, FTPRERR,
213   FTPREXC, FTPSRVERR, FTPRETRINT, FTPRESTFAIL, URLERROR,
214   FOPENERR, FWRITEERR, HOK, HLEXC, HEOF,
215   HERR, RETROK, RECLEVELEXC, FTPACCDENIED, WRONGCODE,
216   FTPINVPASV, FTPNOPASV,
217   CONTNOTSUPPORTED, RETRUNNEEDED, RETRFINISHED, READERR, TRYLIMEXC,
218   URLBADPATTERN, FILEBADFILE, RANGEERR, RETRBADPATTERN,
219   RETNOTSUP, ROBOTSOK, NOROBOTS, PROXERR, AUTHFAILED,
220   QUOTEXC, WRITEFAILED,
221   SSLERRCERTFILE,SSLERRCERTKEY,SSLERRCTXCREATE
222 } uerr_t;
223
224 /* In case old systems don't have EAFNOSUPPORT, which we use below. */
225 #ifndef EAFNOSUPPORT
226 # define EAFNOSUPPORT EINVAL
227 #endif
228
229 #define CONNECT_ERROR(err) ((   (err) == EAFNOSUPPORT           \
230                              || (err) == EINVAL                 \
231                              || ((err) == ECONNREFUSED          \
232                                  && !opt.retry_connrefused))    \
233                             ? CONIMPOSSIBLE : CONERROR)
234
235 #endif /* WGET_H */