1/* Miscellaneous declarations. 2 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 3 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, 4 Inc. 5 6This file is part of GNU Wget. 7 8GNU Wget is free software; you can redistribute it and/or modify 9it under the terms of the GNU General Public License as published by 10the Free Software Foundation; either version 3 of the License, or 11(at your option) any later version. 12 13GNU Wget is distributed in the hope that it will be useful, 14but WITHOUT ANY WARRANTY; without even the implied warranty of 15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16GNU General Public License for more details. 17 18You should have received a copy of the GNU General Public License 19along with Wget. If not, see <http://www.gnu.org/licenses/>. 20 21Additional permission under GNU GPL version 3 section 7 22 23If you modify this program, or any covered work, by linking or 24combining it with the OpenSSL project's OpenSSL library (or a 25modified version of that library), containing parts covered by the 26terms of the OpenSSL or SSLeay licenses, the Free Software Foundation 27grants you additional permission to convey the resulting work. 28Corresponding Source for a non-source form of such a combination 29shall include the source code for the parts of OpenSSL used as well 30as that of the covered work. */ 31 32/* This file contains declarations that are universally useful and 33 those that don't fit elsewhere. It also includes sysdep.h which 34 includes some often-needed system includes, like the obnoxious 35 <time.h> inclusion. */ 36 37#ifndef WGET_H 38#define WGET_H 39 40#include "config.h" 41 42#if ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) 43# define WINDOWS 44#endif 45 46/* Include these, so random files need not include them. */ 47#include "sysdep.h" 48 49/* Disable assertions when debug support is not compiled in. */ 50#ifndef ENABLE_DEBUG 51# define NDEBUG 52#endif 53 54/* Is OpenSSL or GNUTLS available? */ 55#if defined HAVE_LIBSSL || defined HAVE_LIBSSL32 || defined HAVE_LIBGNUTLS 56# define HAVE_SSL 57#endif 58 59/* `gettext (FOO)' is long to write, so we use `_(FOO)'. If NLS is 60 unavailable, _(STRING) simply returns STRING. */ 61#include "gettext.h" 62#define _(string) gettext (string) 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#if HAVE_WCWIDTH && HAVE_MBTOWC 76# define USE_NLS_PROGRESS_BAR 1 77#else 78/* Just to be a little paranoid about it. */ 79# undef USE_NLS_PROGRESS_BAR 80#endif 81 82/* I18N NOTE: You will notice that none of the DEBUGP messages are 83 marked as translatable. This is intentional, for a few reasons: 84 85 1) The debug messages are not meant for the users to look at, but 86 for the developers; as such, they should be considered more like 87 source comments than real program output. 88 89 2) The messages are numerous, and yet they are random and frivolous 90 ("double yuck!" and such). There would be a lot of work with no 91 gain. 92 93 3) Finally, the debug messages are meant to be a clue for me to 94 debug problems with Wget. If I get them in a language I don't 95 understand, debugging will become a new challenge of its own! */ 96 97/* locale independent replacement for ctype.h */ 98#include "c-ctype.h" 99 100/* Conditionalize the use of GCC's __attribute__((format)) and 101 __builtin_expect features using macros. */ 102 103#if defined(__GNUC__) && __GNUC__ >= 3 104# define GCC_FORMAT_ATTR(a, b) __attribute__ ((format (printf, a, b))) 105# define LIKELY(exp) __builtin_expect (!!(exp), 1) 106# define UNLIKELY(exp) __builtin_expect ((exp), 0) 107#else 108# define GCC_FORMAT_ATTR(a, b) 109# define LIKELY(exp) (exp) 110# define UNLIKELY(exp) (exp) 111#endif 112 113/* Execute the following statement if debugging is both enabled at 114 compile-time and requested at run-time; a no-op otherwise. */ 115 116#ifdef ENABLE_DEBUG 117# define IF_DEBUG if (UNLIKELY (opt.debug)) 118#else 119# define IF_DEBUG if (0) 120#endif 121 122/* Print ARGS if debugging is enabled and requested, otherwise do 123 nothing. This must be called with an extra level of parentheses 124 because it's not possible to pass a variable number of arguments to 125 a macro (in portable C89). ARGS are like arguments to printf. */ 126 127#define DEBUGP(args) do { IF_DEBUG { debug_logprintf args; } } while (0) 128 129/* Pick an integer type large enough for file sizes, content lengths, 130 and such. Because today's files can be very large, it should be a 131 signed integer at least 64 bits wide. This can't be typedeffed to 132 off_t because: a) off_t is always 32-bit on Windows, and b) we 133 don't necessarily want to tie having a 64-bit type for internal 134 calculations to having LFS support. */ 135 136#ifdef WINDOWS 137 /* nothing to do, see mswindows.h */ 138#elif SIZEOF_LONG >= 8 139 /* long is large enough, so use it. */ 140 typedef long wgint; 141# define SIZEOF_WGINT SIZEOF_LONG 142#elif SIZEOF_LONG_LONG >= 8 143 /* long long is large enough and available, use that */ 144 typedef long long wgint; 145# define SIZEOF_WGINT SIZEOF_LONG_LONG 146#elif HAVE_INT64_T 147 typedef int64_t wgint; 148# define SIZEOF_WGINT 8 149#elif SIZEOF_OFF_T >= 8 150 /* In case off_t is typedeffed to a large non-standard type that our 151 tests don't find. */ 152 typedef off_t wgint; 153# define SIZEOF_WGINT SIZEOF_OFF_T 154#else 155 /* Fall back to using long, which is always available and in most 156 cases large enough. */ 157 typedef long wgint; 158# define SIZEOF_WGINT SIZEOF_LONG 159#endif 160 161/* Pick a strtol-compatible function that will work with wgint. The 162 choices are strtol, strtoll, or our own implementation of strtoll 163 in cmpt.c, activated with NEED_STRTOLL. */ 164 165#ifdef WINDOWS 166 /* nothing to do, see mswindows.h */ 167#elif SIZEOF_WGINT == SIZEOF_LONG 168# define str_to_wgint strtol 169#elif SIZEOF_WGINT == SIZEOF_LONG_LONG 170# define str_to_wgint strtoll 171# ifndef HAVE_STRTOLL 172# define NEED_STRTOLL 173# define strtoll_type long long 174# endif 175#else 176 /* wgint has a strange size; synthesize strtoll and use it. */ 177# define str_to_wgint strtoll 178# define NEED_STRTOLL 179# define strtoll_type wgint 180#endif 181 182#define WGINT_MAX TYPE_MAXIMUM (wgint) 183 184/* Declare our strtoll replacement. */ 185#ifdef NEED_STRTOLL 186strtoll_type strtoll (const char *, char **, int); 187#endif 188 189/* Now define a large numeric type useful for storing sizes of *sums* 190 of downloads, such as the value of the --quota option. This should 191 be a type able to hold 2G+ values even on systems without large 192 file support. (It is useful to limit Wget's download quota to say 193 10G even if a single file cannot be that large.) 194 195 To make sure we get the largest size possible, we use `double' on 196 systems without a 64-bit integral type. (Since it is used in very 197 few places in Wget, this is acceptable.) */ 198 199#if SIZEOF_WGINT >= 8 200/* just use wgint */ 201typedef wgint SUM_SIZE_INT; 202#else 203/* On systems without LFS, use double, which buys us integers up to 2^53. */ 204typedef double SUM_SIZE_INT; 205#endif 206 207#include "options.h" 208 209/* Everything uses this, so include them here directly. */ 210#include <alloca.h> 211#include "xalloc.h" 212 213/* Likewise for logging functions. */ 214#include "log.h" 215 216/* Likewise for quoting functions. */ 217#include "quote.h" 218#include "quotearg.h" 219 220/* Likewise for struct iri definition */ 221#include "iri.h" 222 223/* Useful macros used across the code: */ 224 225/* The number of elements in an array. For example: 226 static char a[] = "foo"; -- countof(a) == 4 (note terminating \0) 227 int a[5] = {1, 2}; -- countof(a) == 5 228 char *a[] = { -- countof(a) == 3 229 "foo", "bar", "baz" 230 }; */ 231#define countof(array) (sizeof (array) / sizeof ((array)[0])) 232 233/* Zero out a value. */ 234#define xzero(x) memset (&(x), '\0', sizeof (x)) 235 236/* Convert an ASCII hex digit to the corresponding number between 0 237 and 15. H should be a hexadecimal digit that satisfies isxdigit; 238 otherwise, the result is undefined. */ 239#define XDIGIT_TO_NUM(h) ((h) < 'A' ? (h) - '0' : c_toupper (h) - 'A' + 10) 240#define X2DIGITS_TO_NUM(h1, h2) ((XDIGIT_TO_NUM (h1) << 4) + XDIGIT_TO_NUM (h2)) 241 242/* The reverse of the above: convert a number in the [0, 16) range to 243 the ASCII representation of the corresponding hexadecimal digit. 244 `+ 0' is there so you can't accidentally use it as an lvalue. */ 245#define XNUM_TO_DIGIT(x) ("0123456789ABCDEF"[x] + 0) 246#define XNUM_TO_digit(x) ("0123456789abcdef"[x] + 0) 247 248/* Copy the data delimited with BEG and END to alloca-allocated 249 storage, and zero-terminate it. Arguments are evaluated only once, 250 in the order BEG, END, PLACE. */ 251#define BOUNDED_TO_ALLOCA(beg, end, place) do { \ 252 const char *BTA_beg = (beg); \ 253 int BTA_len = (end) - BTA_beg; \ 254 char **BTA_dest = &(place); \ 255 *BTA_dest = alloca (BTA_len + 1); \ 256 memcpy (*BTA_dest, BTA_beg, BTA_len); \ 257 (*BTA_dest)[BTA_len] = '\0'; \ 258} while (0) 259 260/* Return non-zero if string bounded between BEG and END is equal to 261 STRING_LITERAL. The comparison is case-sensitive. */ 262#define BOUNDED_EQUAL(beg, end, string_literal) \ 263 ((end) - (beg) == sizeof (string_literal) - 1 \ 264 && !memcmp (beg, string_literal, sizeof (string_literal) - 1)) 265 266/* The same as above, except the comparison is case-insensitive. */ 267#define BOUNDED_EQUAL_NO_CASE(beg, end, string_literal) \ 268 ((end) - (beg) == sizeof (string_literal) - 1 \ 269 && !strncasecmp (beg, string_literal, sizeof (string_literal) - 1)) 270 271/* Like ptr=strdup(str), but allocates the space for PTR on the stack. 272 This cannot be an expression because this is not portable: 273 #define STRDUP_ALLOCA(str) (strcpy (alloca (strlen (str) + 1), str)) 274 The problem is that some compilers can't handle alloca() being an 275 argument to a function. */ 276 277#define STRDUP_ALLOCA(ptr, str) do { \ 278 char **SA_dest = &(ptr); \ 279 const char *SA_src = (str); \ 280 *SA_dest = (char *)alloca (strlen (SA_src) + 1); \ 281 strcpy (*SA_dest, SA_src); \ 282} while (0) 283 284/* Generally useful if you want to avoid arbitrary size limits but 285 don't need a full dynamic array. Assumes that BASEVAR points to a 286 malloced array of TYPE objects (or possibly a NULL pointer, if 287 SIZEVAR is 0), with the total size stored in SIZEVAR. This macro 288 will realloc BASEVAR as necessary so that it can hold at least 289 NEEDED_SIZE objects. The reallocing is done by doubling, which 290 ensures constant amortized time per element. */ 291 292#define DO_REALLOC(basevar, sizevar, needed_size, type) do { \ 293 long DR_needed_size = (needed_size); \ 294 long DR_newsize = 0; \ 295 while ((sizevar) < (DR_needed_size)) { \ 296 DR_newsize = sizevar << 1; \ 297 if (DR_newsize < 16) \ 298 DR_newsize = 16; \ 299 (sizevar) = DR_newsize; \ 300 } \ 301 if (DR_newsize) \ 302 basevar = xrealloc (basevar, DR_newsize * sizeof (type)); \ 303} while (0) 304 305/* Used to print pointers (usually for debugging). Print pointers 306 using printf ("0x%0*lx", PTR_FORMAT (p)). (%p is too unpredictable; 307 some implementations prepend 0x, while some don't, and most don't 308 0-pad the address.) */ 309#define PTR_FORMAT(p) (int) (2 * sizeof (void *)), (unsigned long) (p) 310 311/* Find the maximum buffer length needed to print an integer of type `x' 312 in base 10. 24082 / 10000 = 8*log_{10}(2). */ 313#define MAX_INT_TO_STRING_LEN(x) ((sizeof(x) * 24082 / 10000) + 2) 314 315extern const char *exec_name; 316 317/* Document type ("dt") flags */ 318enum 319{ 320 TEXTHTML = 0x0001, /* document is of type text/html 321 or application/xhtml+xml */ 322 RETROKF = 0x0002, /* retrieval was OK */ 323 HEAD_ONLY = 0x0004, /* only send the HEAD request */ 324 SEND_NOCACHE = 0x0008, /* send Pragma: no-cache directive */ 325 ACCEPTRANGES = 0x0010, /* Accept-ranges header was found */ 326 ADDED_HTML_EXTENSION = 0x0020, /* added ".html" extension due to -E */ 327 TEXTCSS = 0x0040 /* document is of type text/css */ 328}; 329 330/* Universal error type -- used almost everywhere. Error reporting of 331 this detail is not generally used or needed and should be 332 simplified. */ 333typedef enum 334{ 335 NOCONERROR, HOSTERR, CONSOCKERR, CONERROR, CONSSLERR, 336 CONIMPOSSIBLE, NEWLOCATION, 337 FTPOK, FTPLOGINC, FTPLOGREFUSED, FTPPORTERR, FTPSYSERR, 338 FTPNSFOD, FTPUNKNOWNTYPE, FTPRERR, 339 FTPSRVERR, FTPRETRINT, FTPRESTFAIL, URLERROR, FOPENERR, 340 FOPEN_EXCL_ERR, FWRITEERR, HEOF, 341 HERR, RETROK, RECLEVELEXC, WRONGCODE, 342 FTPINVPASV, FTPNOPASV, CONTNOTSUPPORTED, RETRUNNEEDED, RETRFINISHED, 343 READERR, TRYLIMEXC, FILEBADFILE, RANGEERR, 344 RETRBADPATTERN, PROXERR, 345 AUTHFAILED, QUOTEXC, WRITEFAILED, SSLINITFAILED, VERIFCERTERR, 346 UNLINKERR, NEWLOCATION_KEEP_POST, CLOSEFAILED, ATTRMISSING, UNKNOWNATTR, 347 WARC_ERR, WARC_TMP_FOPENERR, WARC_TMP_FWRITEERR 348} uerr_t; 349 350/* 2005-02-19 SMS. 351 Select an appropriate "orig" suffix and a separator character for 352 adding a unique suffix to a file name. 353 354 A VMS ODS2 file system can't tolerate multiple dots. An ODS5 file 355 system can, but even there not all dots are equal, and heroic effort 356 would be needed to get ".html^.orig" rather than (the less desirable) 357 "^.html.orig". It's more satisfactory always to use "_orig" on VMS 358 (rather than including "vms.h", testing "ods5_dest", and acting 359 accordingly). 360 361 Note that code in various places assumes that this string is five 362 characters long. 363*/ 364# ifdef __VMS 365# define ORIG_SFX "_orig" 366# else /* def __VMS */ 367# define ORIG_SFX ".orig" 368# endif /* def __VMS [else] */ 369 370/* ".NNN" unique-ifying suffix separator character for unique_name() in 371 url.c (and anywhere else). Note that on VMS, the file system's 372 version numbers solve the problem that unique_name() is designed to 373 handle, obviating this whole exercise. Other systems may specify a 374 character different from "." here, if desired. 375*/ 376# ifndef __VMS 377# define UNIQ_SEP '.' 378# endif /* ndef __VMS */ 379 380#endif /* WGET_H */ 381