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