]> sjero.net Git - wget/blob - src/wget.h
[svn] Define the IF_DEBUG macro.
[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 /* `gettext (FOO)' is long to write, so we use `_(FOO)'.  If NLS is
44    unavailable, _(STRING) simply returns STRING.  */
45 #ifdef HAVE_NLS
46 # define _(string) gettext (string)
47 # ifdef HAVE_LIBINTL_H
48 #  include <libintl.h>
49 # else  /* not HAVE_LIBINTL_H */
50    const char *gettext ();
51 # endif /* not HAVE_LIBINTL_H */
52 #else  /* not HAVE_NLS */
53 # define _(string) (string)
54 #endif /* not HAVE_NLS */
55
56 /* A pseudo function call that serves as a marker for the automated
57    extraction of messages, but does not call gettext().  The run-time
58    translation is done at a different place in the code.  The purpose
59    of the N_("...") call is to make the message snarfer aware that the
60    "..." string needs to be translated.  STRING should be a string
61    literal.  Concatenated strings and other string expressions won't
62    work.  The macro's expansion is not parenthesized, so that it is
63    suitable as initializer for static 'char[]' or 'const char[]'
64    variables.  -- explanation partly taken from GNU make.  */
65 #define N_(string) string
66
67 /* I18N NOTE: You will notice that none of the DEBUGP messages are
68    marked as translatable.  This is intentional, for a few reasons:
69
70    1) The debug messages are not meant for the users to look at, but
71    for the developers; as such, they should be considered more like
72    source comments than real program output.
73
74    2) The messages are numerous, and yet they are random and frivolous
75    ("double yuck!" and such).  There would be a lot of work with no
76    gain.
77
78    3) Finally, the debug messages are meant to be a clue for me to
79    debug problems with Wget.  If I get them in a language I don't
80    understand, debugging will become a new challenge of its own!  */
81
82
83 /* Include these, so random files need not include them.  */
84 #include "sysdep.h"
85 /* locale independent replacement for ctype.h */
86 #include "safe-ctype.h"
87
88 /* Conditionalize the use of GCC's __attribute__((format)) and
89    __builtin_expect features using macros.  */
90
91 #if defined(__GNUC__) && __GNUC__ >= 3
92 # define GCC_FORMAT_ATTR(a, b) __attribute__ ((format (printf, a, b)))
93 # define LIKELY(exp)   __builtin_expect (!!(exp), 1)
94 # define UNLIKELY(exp) __builtin_expect ((exp), 0)
95 #else
96 # define GCC_FORMAT_ATTR(a, b)
97 # define LIKELY(exp)   (exp)
98 # define UNLIKELY(exp) (exp)
99 #endif
100
101 /* Execute the following statement if debugging is both enabled at
102    compile-time and requested at run-time; a no-op otherwise.  */
103
104 #ifdef ENABLE_DEBUG
105 # define IF_DEBUG if (UNLIKELY (opt.debug))
106 #else
107 # define IF_DEBUG if (0)
108 #endif
109
110 #define DEBUGP(x) do { IF_DEBUG { debug_logprintf x; } } while (0)
111
112 /* Define an integer type that works for file sizes, content lengths,
113    and such.  Normally we could just use off_t, but off_t is always
114    32-bit on Windows.  */
115
116 #ifndef WINDOWS
117 typedef off_t wgint;
118 # define SIZEOF_WGINT SIZEOF_OFF_T
119 #endif
120
121 /* Define a strtol/strtoll clone that works with wgint.  */
122 #ifndef str_to_wgint            /* mswindows.h defines its own alias */
123 # if SIZEOF_WGINT == SIZEOF_LONG
124 #  define str_to_wgint strtol
125 #  define WGINT_MAX LONG_MAX
126 # else
127 #  define str_to_wgint strtoll
128 #  define WGINT_MAX LLONG_MAX
129 # endif
130 #endif
131
132 #include "options.h"
133
134 /* Everything uses this, so include them here directly.  */
135 #include "xmalloc.h"
136
137 /* Likewise for logging functions.  */
138 #include "log.h"
139 \f
140 /* Useful macros used across the code: */
141
142 /* The number of elements in an array.  For example:
143    static char a[] = "foo";     -- countof(a) == 4 (note terminating \0)
144    int a[5] = {1, 2};           -- countof(a) == 5
145    char *a[] = {                -- countof(a) == 3
146      "foo", "bar", "baz"
147    }; */
148 #define countof(array) (sizeof (array) / sizeof ((array)[0]))
149
150 /* Zero out a value.  */
151 #define xzero(x) memset (&(x), '\0', sizeof (x))
152
153 /* Convert an ASCII hex digit to the corresponding number between 0
154    and 15.  H should be a hexadecimal digit that satisfies isxdigit;
155    otherwise, the result is undefined.  */
156 #define XDIGIT_TO_NUM(h) ((h) < 'A' ? (h) - '0' : TOUPPER (h) - 'A' + 10)
157 #define X2DIGITS_TO_NUM(h1, h2) ((XDIGIT_TO_NUM (h1) << 4) + XDIGIT_TO_NUM (h2))
158
159 /* The reverse of the above: convert a number in the [0, 16) range to
160    the ASCII representation of the corresponding hexadecimal digit.
161    `+ 0' is there so you can't accidentally use it as an lvalue.  */
162 #define XNUM_TO_DIGIT(x) ("0123456789ABCDEF"[x] + 0)
163 #define XNUM_TO_digit(x) ("0123456789abcdef"[x] + 0)
164
165 /* Copy the data delimited with BEG and END to alloca-allocated
166    storage, and zero-terminate it.  Arguments are evaluated only once,
167    in the order BEG, END, PLACE.  */
168 #define BOUNDED_TO_ALLOCA(beg, end, place) do { \
169   const char *BTA_beg = (beg);                  \
170   int BTA_len = (end) - BTA_beg;                \
171   char **BTA_dest = &(place);                   \
172   *BTA_dest = alloca (BTA_len + 1);             \
173   memcpy (*BTA_dest, BTA_beg, BTA_len);         \
174   (*BTA_dest)[BTA_len] = '\0';                  \
175 } while (0)
176
177 /* Return non-zero if string bounded between BEG and END is equal to
178    STRING_LITERAL.  The comparison is case-sensitive.  */
179 #define BOUNDED_EQUAL(beg, end, string_literal)                         \
180   ((end) - (beg) == sizeof (string_literal) - 1                         \
181    && !memcmp (beg, string_literal, sizeof (string_literal) - 1))
182
183 /* The same as above, except the comparison is case-insensitive. */
184 #define BOUNDED_EQUAL_NO_CASE(beg, end, string_literal)                 \
185   ((end) - (beg) == sizeof (string_literal) - 1                         \
186    && !strncasecmp (beg, string_literal, sizeof (string_literal) - 1))
187
188 /* Like ptr=strdup(str), but allocates the space for PTR on the stack.
189    This cannot be an expression because this is not portable:
190      #define STRDUP_ALLOCA(str) (strcpy (alloca (strlen (str) + 1), str))
191    The problem is that some compilers can't handle alloca() being an
192    argument to a function.  */
193
194 #define STRDUP_ALLOCA(ptr, str) do {                    \
195   char **SA_dest = &(ptr);                              \
196   const char *SA_src = (str);                           \
197   *SA_dest = (char *)alloca (strlen (SA_src) + 1);      \
198   strcpy (*SA_dest, SA_src);                            \
199 } while (0)
200
201 /* Generally useful if you want to avoid arbitrary size limits but
202    don't need a full dynamic array.  Assumes that BASEVAR points to a
203    malloced array of TYPE objects (or possibly a NULL pointer, if
204    SIZEVAR is 0), with the total size stored in SIZEVAR.  This macro
205    will realloc BASEVAR as necessary so that it can hold at least
206    NEEDED_SIZE objects.  The reallocing is done by doubling, which
207    ensures constant amortized time per element.  */
208
209 #define DO_REALLOC(basevar, sizevar, needed_size, type) do {            \
210   long DR_needed_size = (needed_size);                                  \
211   long DR_newsize = 0;                                                  \
212   while ((sizevar) < (DR_needed_size)) {                                \
213     DR_newsize = sizevar << 1;                                          \
214     if (DR_newsize < 16)                                                \
215       DR_newsize = 16;                                                  \
216     (sizevar) = DR_newsize;                                             \
217   }                                                                     \
218   if (DR_newsize)                                                       \
219     basevar = xrealloc (basevar, DR_newsize * sizeof (type));           \
220 } while (0)
221
222 /* Used to print pointers (usually for debugging).  Print pointers
223    using printf ("%0*lx", PTR_FORMAT (p)).  (%p is too unpredictable;
224    some implementations prepend 0x, while some don't, and most don't
225    0-pad the address.)  */
226 #define PTR_FORMAT(p) 2 * sizeof (void *), (unsigned long) (p)
227
228 extern const char *exec_name;
229 \f
230 /* Document type ("dt") flags */
231 enum
232 {
233   TEXTHTML             = 0x0001,        /* document is of type text/html
234                                            or application/xhtml+xml */
235   RETROKF              = 0x0002,        /* retrieval was OK */
236   HEAD_ONLY            = 0x0004,        /* only send the HEAD request */
237   SEND_NOCACHE         = 0x0008,        /* send Pragma: no-cache directive */
238   ACCEPTRANGES         = 0x0010,        /* Accept-ranges header was found */
239   ADDED_HTML_EXTENSION = 0x0020         /* added ".html" extension due to -E */
240 };
241
242 /* Universal error type -- used almost everywhere.  Error reporting of
243    this detail is not generally used or needed and should be
244    simplified.  */
245 typedef enum
246 {
247   NOCONERROR, HOSTERR, CONSOCKERR, CONERROR, CONSSLERR,
248   CONIMPOSSIBLE, NEWLOCATION, NOTENOUGHMEM, CONPORTERR,
249   CONCLOSED, FTPOK, FTPLOGINC, FTPLOGREFUSED, FTPPORTERR, FTPSYSERR,
250   FTPNSFOD, FTPRETROK, FTPUNKNOWNTYPE, FTPRERR,
251   FTPREXC, FTPSRVERR, FTPRETRINT, FTPRESTFAIL, URLERROR,
252   FOPENERR, FOPEN_EXCL_ERR, FWRITEERR, HOK, HLEXC, HEOF,
253   HERR, RETROK, RECLEVELEXC, FTPACCDENIED, WRONGCODE,
254   FTPINVPASV, FTPNOPASV,
255   CONTNOTSUPPORTED, RETRUNNEEDED, RETRFINISHED, READERR, TRYLIMEXC,
256   URLBADPATTERN, FILEBADFILE, RANGEERR, RETRBADPATTERN,
257   RETNOTSUP, ROBOTSOK, NOROBOTS, PROXERR, AUTHFAILED,
258   QUOTEXC, WRITEFAILED, SSLINITFAILED
259 } uerr_t;
260
261 #endif /* WGET_H */