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