2000-11-19 15:50:10 -05:00
|
|
|
/* HTML parser for Wget.
|
2007-09-28 18:45:31 -04:00
|
|
|
Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
|
2015-03-09 11:32:01 -04:00
|
|
|
2007, 2008, 2009, 2010, 2011, 2015 Free Software Foundation, Inc.
|
2000-11-19 15:50:10 -05:00
|
|
|
|
2001-05-27 15:35:15 -04:00
|
|
|
This file is part of GNU Wget.
|
2000-11-19 15:50:10 -05:00
|
|
|
|
2001-05-27 15:35:15 -04:00
|
|
|
GNU Wget is free software; you can redistribute it and/or modify
|
2000-11-19 15:50:10 -05:00
|
|
|
it under the terms of the GNU General Public License as published by
|
2007-07-10 01:53:22 -04:00
|
|
|
the Free Software Foundation; either version 3 of the License, or (at
|
2000-11-19 15:50:10 -05:00
|
|
|
your option) any later version.
|
|
|
|
|
2001-05-27 15:35:15 -04:00
|
|
|
GNU Wget is distributed in the hope that it will be useful,
|
2000-11-19 15:50:10 -05:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2007-07-10 01:53:22 -04:00
|
|
|
along with Wget. If not, see <http://www.gnu.org/licenses/>.
|
2002-05-17 22:16:36 -04:00
|
|
|
|
2007-11-28 03:05:33 -05:00
|
|
|
Additional permission under GNU GPL version 3 section 7
|
|
|
|
|
|
|
|
If you modify this program, or any covered work, by linking or
|
|
|
|
combining it with the OpenSSL project's OpenSSL library (or a
|
|
|
|
modified version of that library), containing parts covered by the
|
|
|
|
terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
|
|
|
|
grants you additional permission to convey the resulting work.
|
|
|
|
Corresponding Source for a non-source form of such a combination
|
|
|
|
shall include the source code for the parts of OpenSSL used as well
|
|
|
|
as that of the covered work. */
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
/* The only entry point to this module is map_html_tags(), which see. */
|
|
|
|
|
|
|
|
/* TODO:
|
|
|
|
|
|
|
|
- Allow hooks for callers to process contents outside tags. This
|
|
|
|
is needed to implement handling <style> and <script>. The
|
|
|
|
taginfo structure already carries the information about where the
|
|
|
|
tags are, but this is not enough, because one would also want to
|
|
|
|
skip the comments. (The funny thing is that for <style> and
|
|
|
|
<script> you *don't* want to skip comments!)
|
|
|
|
|
|
|
|
- Create a test suite for regression testing. */
|
|
|
|
|
|
|
|
/* HISTORY:
|
|
|
|
|
|
|
|
This is the third HTML parser written for Wget. The first one was
|
|
|
|
written some time during the Geturl 1.0 beta cycle, and was very
|
|
|
|
inefficient and buggy. It also contained some very complex code to
|
|
|
|
remember a list of parser states, because it was supposed to be
|
2003-10-02 13:23:25 -04:00
|
|
|
reentrant.
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
The second HTML parser was written for Wget 1.4 (the first version
|
|
|
|
by the name `Wget'), and was a complete rewrite. Although the new
|
|
|
|
parser behaved much better and made no claims of reentrancy, it
|
|
|
|
still shared many of the fundamental flaws of the old version -- it
|
|
|
|
only regarded HTML in terms tag-attribute pairs, where the
|
|
|
|
attribute's value was a URL to be returned. Any other property of
|
|
|
|
HTML, such as <base href=...>, or strange way to specify a URL,
|
|
|
|
such as <meta http-equiv=Refresh content="0; URL=..."> had to be
|
|
|
|
crudely hacked in -- and the caller had to be aware of these hacks.
|
|
|
|
Like its predecessor, this parser did not support HTML comments.
|
|
|
|
|
|
|
|
After Wget 1.5.1 was released, I set out to write a third HTML
|
|
|
|
parser. The objectives of the new parser were to: (1) provide a
|
|
|
|
clean way to analyze HTML lexically, (2) separate interpretation of
|
|
|
|
the markup from the parsing process, (3) be as correct as possible,
|
|
|
|
e.g. correctly skipping comments and other SGML declarations, (4)
|
|
|
|
understand the most common errors in markup and skip them or be
|
|
|
|
relaxed towrds them, and (5) be reasonably efficient (no regexps,
|
|
|
|
minimum copying and minimum or no heap allocation).
|
|
|
|
|
|
|
|
I believe this parser meets all of the above goals. It is
|
|
|
|
reasonably well structured, and could be relatively easily
|
|
|
|
separated from Wget and used elsewhere. While some of its
|
|
|
|
intrinsic properties limit its value as a general-purpose HTML
|
|
|
|
parser, I believe that, with minimum modifications, it could serve
|
|
|
|
as a backend for one.
|
|
|
|
|
|
|
|
Due to time and other constraints, this parser was not integrated
|
2001-04-24 20:59:39 -04:00
|
|
|
into Wget until the version 1.7. */
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
/* DESCRIPTION:
|
|
|
|
|
|
|
|
The single entry point of this parser is map_html_tags(), which
|
|
|
|
works by calling a function you specify for each tag. The function
|
|
|
|
gets called with the pointer to a structure describing the tag and
|
|
|
|
its attributes. */
|
|
|
|
|
|
|
|
/* To test as standalone, compile with `-DSTANDALONE -I.'. You'll
|
|
|
|
still need Wget headers to compile. */
|
|
|
|
|
2007-10-18 23:50:40 -04:00
|
|
|
#include "wget.h"
|
2000-11-19 15:50:10 -05:00
|
|
|
|
2001-12-18 14:33:36 -05:00
|
|
|
#ifdef STANDALONE
|
|
|
|
# define I_REALLY_WANT_CTYPE_MACROS
|
|
|
|
#endif
|
|
|
|
|
2000-11-19 15:50:10 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2005-06-19 18:34:58 -04:00
|
|
|
#include <string.h>
|
2000-11-19 15:50:10 -05:00
|
|
|
#include <assert.h>
|
|
|
|
|
2008-04-18 19:21:24 -04:00
|
|
|
#include "utils.h"
|
2000-11-19 15:50:10 -05:00
|
|
|
#include "html-parse.h"
|
|
|
|
|
|
|
|
#ifdef STANDALONE
|
2003-10-02 13:23:25 -04:00
|
|
|
# undef xmalloc
|
|
|
|
# undef xrealloc
|
|
|
|
# undef xfree
|
2000-11-19 15:50:10 -05:00
|
|
|
# define xmalloc malloc
|
|
|
|
# define xrealloc realloc
|
2000-11-22 12:00:31 -05:00
|
|
|
# define xfree free
|
2001-12-18 14:33:36 -05:00
|
|
|
|
2007-10-14 17:46:24 -04:00
|
|
|
# undef c_isspace
|
|
|
|
# undef c_isdigit
|
|
|
|
# undef c_isxdigit
|
|
|
|
# undef c_isalpha
|
|
|
|
# undef c_isalnum
|
|
|
|
# undef c_tolower
|
|
|
|
# undef c_toupper
|
|
|
|
|
|
|
|
# define c_isspace(x) isspace (x)
|
|
|
|
# define c_isdigit(x) isdigit (x)
|
|
|
|
# define c_isxdigit(x) isxdigit (x)
|
|
|
|
# define c_isalpha(x) isalpha (x)
|
|
|
|
# define c_isalnum(x) isalnum (x)
|
|
|
|
# define c_tolower(x) tolower (x)
|
|
|
|
# define c_toupper(x) toupper (x)
|
2003-10-09 11:01:58 -04:00
|
|
|
|
|
|
|
struct hash_table {
|
|
|
|
int dummy;
|
|
|
|
};
|
|
|
|
static void *
|
|
|
|
hash_table_get (const struct hash_table *ht, void *ptr)
|
|
|
|
{
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
#else /* not STANDALONE */
|
|
|
|
# include "hash.h"
|
|
|
|
#endif
|
2000-11-19 15:50:10 -05:00
|
|
|
|
2001-12-18 14:33:36 -05:00
|
|
|
/* Pool support. A pool is a resizable chunk of memory. It is first
|
|
|
|
allocated on the stack, and moved to the heap if it needs to be
|
|
|
|
larger than originally expected. map_html_tags() uses it to store
|
|
|
|
the zero-terminated names and values of tags and attributes.
|
|
|
|
|
|
|
|
Thus taginfo->name, and attr->name and attr->value for each
|
|
|
|
attribute, do not point into separately allocated areas, but into
|
|
|
|
different parts of the pool, separated only by terminating zeros.
|
|
|
|
This ensures minimum amount of allocation and, for most tags, no
|
|
|
|
allocation because the entire pool is kept on the stack. */
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
struct pool {
|
2007-08-02 23:38:21 -04:00
|
|
|
char *contents; /* pointer to the contents. */
|
|
|
|
int size; /* size of the pool. */
|
|
|
|
int tail; /* next available position index. */
|
|
|
|
bool resized; /* whether the pool has been resized
|
|
|
|
using malloc. */
|
2003-10-02 18:20:44 -04:00
|
|
|
|
2007-08-02 23:38:21 -04:00
|
|
|
char *orig_contents; /* original pool contents, usually
|
2003-10-02 18:20:44 -04:00
|
|
|
stack-allocated. used by POOL_FREE
|
|
|
|
to restore the pool to the initial
|
|
|
|
state. */
|
2000-11-19 15:50:10 -05:00
|
|
|
int orig_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Initialize the pool to hold INITIAL_SIZE bytes of storage. */
|
|
|
|
|
2007-08-02 23:38:21 -04:00
|
|
|
#define POOL_INIT(p, initial_storage, initial_size) do { \
|
|
|
|
struct pool *P = (p); \
|
|
|
|
P->contents = (initial_storage); \
|
|
|
|
P->size = (initial_size); \
|
|
|
|
P->tail = 0; \
|
|
|
|
P->resized = false; \
|
|
|
|
P->orig_contents = P->contents; \
|
|
|
|
P->orig_size = P->size; \
|
2000-11-19 15:50:10 -05:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* Grow the pool to accomodate at least SIZE new bytes. If the pool
|
|
|
|
already has room to accomodate SIZE bytes of data, this is a no-op. */
|
|
|
|
|
2007-08-02 23:38:21 -04:00
|
|
|
#define POOL_GROW(p, increase) \
|
|
|
|
GROW_ARRAY ((p)->contents, (p)->size, (p)->tail + (increase), \
|
|
|
|
(p)->resized, char)
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
/* Append text in the range [beg, end) to POOL. No zero-termination
|
|
|
|
is done. */
|
|
|
|
|
2007-08-02 23:38:21 -04:00
|
|
|
#define POOL_APPEND(p, beg, end) do { \
|
|
|
|
const char *PA_beg = (beg); \
|
|
|
|
int PA_size = (end) - PA_beg; \
|
|
|
|
POOL_GROW (p, PA_size); \
|
|
|
|
memcpy ((p)->contents + (p)->tail, PA_beg, PA_size); \
|
|
|
|
(p)->tail += PA_size; \
|
2000-11-19 15:50:10 -05:00
|
|
|
} while (0)
|
|
|
|
|
2003-10-02 13:23:25 -04:00
|
|
|
/* Append one character to the pool. Can be used to zero-terminate
|
|
|
|
pool strings. */
|
2000-11-19 15:50:10 -05:00
|
|
|
|
2007-08-02 23:38:21 -04:00
|
|
|
#define POOL_APPEND_CHR(p, ch) do { \
|
|
|
|
char PAC_char = (ch); \
|
|
|
|
POOL_GROW (p, 1); \
|
|
|
|
(p)->contents[(p)->tail++] = PAC_char; \
|
2000-11-19 15:50:10 -05:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* Forget old pool contents. The allocated memory is not freed. */
|
2003-10-02 18:20:44 -04:00
|
|
|
#define POOL_REWIND(p) (p)->tail = 0
|
2000-11-19 15:50:10 -05:00
|
|
|
|
2000-11-22 11:58:28 -05:00
|
|
|
/* Free heap-allocated memory for contents of POOL. This calls
|
|
|
|
xfree() if the memory was allocated through malloc. It also
|
|
|
|
restores `contents' and `size' to their original, pre-malloc
|
|
|
|
values. That way after POOL_FREE, the pool is fully usable, just
|
|
|
|
as if it were freshly initialized with POOL_INIT. */
|
2000-11-19 15:50:10 -05:00
|
|
|
|
2007-08-02 23:38:21 -04:00
|
|
|
#define POOL_FREE(p) do { \
|
|
|
|
struct pool *P = p; \
|
|
|
|
if (P->resized) \
|
|
|
|
xfree (P->contents); \
|
|
|
|
P->contents = P->orig_contents; \
|
|
|
|
P->size = P->orig_size; \
|
|
|
|
P->tail = 0; \
|
|
|
|
P->resized = false; \
|
2000-11-19 15:50:10 -05:00
|
|
|
} while (0)
|
|
|
|
|
2003-10-02 18:20:44 -04:00
|
|
|
/* Used for small stack-allocated memory chunks that might grow. Like
|
|
|
|
DO_REALLOC, this macro grows BASEVAR as necessary to take
|
|
|
|
NEEDED_SIZE items of TYPE.
|
|
|
|
|
|
|
|
The difference is that on the first resize, it will use
|
|
|
|
malloc+memcpy rather than realloc. That way you can stack-allocate
|
|
|
|
the initial chunk, and only resort to heap allocation if you
|
|
|
|
stumble upon large data.
|
|
|
|
|
|
|
|
After the first resize, subsequent ones are performed with realloc,
|
|
|
|
just like DO_REALLOC. */
|
|
|
|
|
2007-08-02 23:38:21 -04:00
|
|
|
#define GROW_ARRAY(basevar, sizevar, needed_size, resized, type) do { \
|
|
|
|
long ga_needed_size = (needed_size); \
|
|
|
|
long ga_newsize = (sizevar); \
|
|
|
|
while (ga_newsize < ga_needed_size) \
|
|
|
|
ga_newsize <<= 1; \
|
|
|
|
if (ga_newsize != (sizevar)) \
|
|
|
|
{ \
|
|
|
|
if (resized) \
|
|
|
|
basevar = xrealloc (basevar, ga_newsize * sizeof (type)); \
|
|
|
|
else \
|
|
|
|
{ \
|
|
|
|
void *ga_new = xmalloc (ga_newsize * sizeof (type)); \
|
|
|
|
memcpy (ga_new, basevar, (sizevar) * sizeof (type)); \
|
|
|
|
(basevar) = ga_new; \
|
|
|
|
resized = true; \
|
|
|
|
} \
|
|
|
|
(sizevar) = ga_newsize; \
|
|
|
|
} \
|
2003-10-02 18:20:44 -04:00
|
|
|
} while (0)
|
2014-11-20 10:35:34 -05:00
|
|
|
|
2003-11-02 09:57:31 -05:00
|
|
|
/* Test whether n+1-sized entity name fits in P. We don't support
|
|
|
|
IE-style non-terminated entities, e.g. "<foo" -> "<foo".
|
|
|
|
However, "<foo" will work, as will "<!foo", "<", etc. In
|
|
|
|
other words an entity needs to be terminated by either a
|
|
|
|
non-alphanumeric or the end of string. */
|
2007-10-14 17:46:24 -04:00
|
|
|
#define FITS(p, n) (p + n == end || (p + n < end && !c_isalnum (p[n])))
|
2003-11-02 09:57:31 -05:00
|
|
|
|
|
|
|
/* Macros that test entity names by returning true if P is followed by
|
|
|
|
the specified characters. */
|
|
|
|
#define ENT1(p, c0) (FITS (p, 1) && p[0] == c0)
|
|
|
|
#define ENT2(p, c0, c1) (FITS (p, 2) && p[0] == c0 && p[1] == c1)
|
|
|
|
#define ENT3(p, c0, c1, c2) (FITS (p, 3) && p[0]==c0 && p[1]==c1 && p[2]==c2)
|
|
|
|
|
|
|
|
/* Increment P by INC chars. If P lands at a semicolon, increment it
|
|
|
|
past the semicolon. This ensures that e.g. "<foo" is converted
|
|
|
|
to "<foo", but "<,foo" to "<,foo". */
|
|
|
|
#define SKIP_SEMI(p, inc) (p += inc, p < end && *p == ';' ? ++p : p)
|
|
|
|
|
2008-04-22 03:15:48 -04:00
|
|
|
struct tagstack_item {
|
|
|
|
const char *tagname_begin;
|
|
|
|
const char *tagname_end;
|
|
|
|
const char *contents_begin;
|
|
|
|
struct tagstack_item *prev;
|
|
|
|
struct tagstack_item *next;
|
|
|
|
};
|
|
|
|
|
2012-05-05 09:24:35 -04:00
|
|
|
static struct tagstack_item *
|
2008-04-22 03:15:48 -04:00
|
|
|
tagstack_push (struct tagstack_item **head, struct tagstack_item **tail)
|
|
|
|
{
|
|
|
|
struct tagstack_item *ts = xmalloc(sizeof(struct tagstack_item));
|
|
|
|
if (*head == NULL)
|
|
|
|
{
|
|
|
|
*head = *tail = ts;
|
|
|
|
ts->prev = ts->next = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
(*tail)->next = ts;
|
|
|
|
ts->prev = *tail;
|
|
|
|
*tail = ts;
|
|
|
|
ts->next = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ts;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove ts and everything after it from the stack */
|
2012-05-05 09:24:35 -04:00
|
|
|
static void
|
2008-04-22 03:15:48 -04:00
|
|
|
tagstack_pop (struct tagstack_item **head, struct tagstack_item **tail,
|
|
|
|
struct tagstack_item *ts)
|
|
|
|
{
|
|
|
|
if (*head == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (ts == *tail)
|
|
|
|
{
|
|
|
|
if (ts == *head)
|
|
|
|
{
|
|
|
|
xfree (ts);
|
|
|
|
*head = *tail = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ts->prev->next = NULL;
|
|
|
|
*tail = ts->prev;
|
|
|
|
xfree (ts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (ts == *head)
|
|
|
|
{
|
|
|
|
*head = NULL;
|
|
|
|
}
|
|
|
|
*tail = ts->prev;
|
|
|
|
|
|
|
|
if (ts->prev)
|
|
|
|
{
|
|
|
|
ts->prev->next = NULL;
|
|
|
|
}
|
|
|
|
while (ts)
|
|
|
|
{
|
|
|
|
struct tagstack_item *p = ts->next;
|
|
|
|
xfree (ts);
|
|
|
|
ts = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-05 09:24:35 -04:00
|
|
|
static struct tagstack_item *
|
2008-04-22 03:15:48 -04:00
|
|
|
tagstack_find (struct tagstack_item *tail, const char *tagname_begin,
|
|
|
|
const char *tagname_end)
|
|
|
|
{
|
|
|
|
int len = tagname_end - tagname_begin;
|
|
|
|
while (tail)
|
|
|
|
{
|
|
|
|
if (len == (tail->tagname_end - tail->tagname_begin))
|
|
|
|
{
|
|
|
|
if (0 == strncasecmp (tail->tagname_begin, tagname_begin, len))
|
|
|
|
return tail;
|
|
|
|
}
|
|
|
|
tail = tail->prev;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2003-11-02 09:57:31 -05:00
|
|
|
/* Decode the HTML character entity at *PTR, considering END to be end
|
|
|
|
of buffer. It is assumed that the "&" character that marks the
|
|
|
|
beginning of the entity has been seen at *PTR-1. If a recognized
|
|
|
|
ASCII entity is seen, it is returned, and *PTR is moved to the end
|
|
|
|
of the entity. Otherwise, -1 is returned and *PTR left unmodified.
|
|
|
|
|
|
|
|
The recognized entities are: <, >, &, &apos, and ". */
|
|
|
|
|
|
|
|
static int
|
|
|
|
decode_entity (const char **ptr, const char *end)
|
|
|
|
{
|
|
|
|
const char *p = *ptr;
|
|
|
|
int value = -1;
|
|
|
|
|
|
|
|
if (++p == end)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
switch (*p++)
|
|
|
|
{
|
|
|
|
case '#':
|
|
|
|
/* Process numeric entities "&#DDD;" and "&#xHH;". */
|
|
|
|
{
|
2007-08-02 23:38:21 -04:00
|
|
|
int digits = 0;
|
|
|
|
value = 0;
|
|
|
|
if (*p == 'x')
|
2007-10-14 17:46:24 -04:00
|
|
|
for (++p; value < 256 && p < end && c_isxdigit (*p); p++, digits++)
|
2007-08-02 23:38:21 -04:00
|
|
|
value = (value << 4) + XDIGIT_TO_NUM (*p);
|
|
|
|
else
|
2007-10-14 17:46:24 -04:00
|
|
|
for (; value < 256 && p < end && c_isdigit (*p); p++, digits++)
|
2007-08-02 23:38:21 -04:00
|
|
|
value = (value * 10) + (*p - '0');
|
|
|
|
if (!digits)
|
|
|
|
return -1;
|
|
|
|
/* Don't interpret 128+ codes and NUL because we cannot
|
|
|
|
portably reinserted them into HTML. */
|
|
|
|
if (!value || (value & ~0x7f))
|
|
|
|
return -1;
|
|
|
|
*ptr = SKIP_SEMI (p, 0);
|
|
|
|
return value;
|
2003-11-02 09:57:31 -05:00
|
|
|
}
|
|
|
|
/* Process named ASCII entities. */
|
|
|
|
case 'g':
|
|
|
|
if (ENT1 (p, 't'))
|
2007-08-02 23:38:21 -04:00
|
|
|
value = '>', *ptr = SKIP_SEMI (p, 1);
|
2003-11-02 09:57:31 -05:00
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
if (ENT1 (p, 't'))
|
2007-08-02 23:38:21 -04:00
|
|
|
value = '<', *ptr = SKIP_SEMI (p, 1);
|
2003-11-02 09:57:31 -05:00
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
if (ENT2 (p, 'm', 'p'))
|
2007-08-02 23:38:21 -04:00
|
|
|
value = '&', *ptr = SKIP_SEMI (p, 2);
|
2003-11-02 09:57:31 -05:00
|
|
|
else if (ENT3 (p, 'p', 'o', 's'))
|
2007-08-02 23:38:21 -04:00
|
|
|
/* handle &apos for the sake of the XML/XHTML crowd. */
|
|
|
|
value = '\'', *ptr = SKIP_SEMI (p, 3);
|
2003-11-02 09:57:31 -05:00
|
|
|
break;
|
|
|
|
case 'q':
|
|
|
|
if (ENT3 (p, 'u', 'o', 't'))
|
2007-08-02 23:38:21 -04:00
|
|
|
value = '\"', *ptr = SKIP_SEMI (p, 3);
|
2003-11-02 09:57:31 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
#undef ENT1
|
|
|
|
#undef ENT2
|
|
|
|
#undef ENT3
|
2003-11-02 11:48:40 -05:00
|
|
|
#undef FITS
|
|
|
|
#undef SKIP_SEMI
|
2003-11-02 09:57:31 -05:00
|
|
|
|
|
|
|
enum {
|
2007-08-02 23:38:21 -04:00
|
|
|
AP_DOWNCASE = 1,
|
|
|
|
AP_DECODE_ENTITIES = 2,
|
|
|
|
AP_TRIM_BLANKS = 4
|
2003-11-02 09:57:31 -05:00
|
|
|
};
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
/* Copy the text in the range [BEG, END) to POOL, optionally
|
|
|
|
performing operations specified by FLAGS. FLAGS may be any
|
2003-11-02 09:57:31 -05:00
|
|
|
combination of AP_DOWNCASE, AP_DECODE_ENTITIES and AP_TRIM_BLANKS
|
2000-11-19 15:50:10 -05:00
|
|
|
with the following meaning:
|
|
|
|
|
|
|
|
* AP_DOWNCASE -- downcase all the letters;
|
|
|
|
|
2003-11-02 09:57:31 -05:00
|
|
|
* AP_DECODE_ENTITIES -- decode the named and numeric entities in
|
|
|
|
the ASCII range when copying the string.
|
2000-11-19 15:50:10 -05:00
|
|
|
|
2003-10-02 13:23:25 -04:00
|
|
|
* AP_TRIM_BLANKS -- ignore blanks at the beginning and at the end
|
2003-11-26 11:37:04 -05:00
|
|
|
of text, as well as embedded newlines. */
|
2003-10-02 13:23:25 -04:00
|
|
|
|
2000-11-19 15:50:10 -05:00
|
|
|
static void
|
|
|
|
convert_and_copy (struct pool *pool, const char *beg, const char *end, int flags)
|
|
|
|
{
|
2003-10-02 18:20:44 -04:00
|
|
|
int old_tail = pool->tail;
|
2000-11-19 15:50:10 -05:00
|
|
|
|
2003-11-26 11:37:04 -05:00
|
|
|
/* Skip blanks if required. We must do this before entities are
|
|
|
|
processed, so that blanks can still be inserted as, for instance,
|
|
|
|
` '. */
|
2003-10-02 13:23:25 -04:00
|
|
|
if (flags & AP_TRIM_BLANKS)
|
2000-11-19 15:50:10 -05:00
|
|
|
{
|
2007-10-14 17:46:24 -04:00
|
|
|
while (beg < end && c_isspace (*beg))
|
2007-08-02 23:38:21 -04:00
|
|
|
++beg;
|
2007-10-14 17:46:24 -04:00
|
|
|
while (end > beg && c_isspace (end[-1]))
|
2007-08-02 23:38:21 -04:00
|
|
|
--end;
|
2000-11-19 15:50:10 -05:00
|
|
|
}
|
|
|
|
|
2003-11-02 09:57:31 -05:00
|
|
|
if (flags & AP_DECODE_ENTITIES)
|
2000-11-19 15:50:10 -05:00
|
|
|
{
|
2003-10-02 13:23:25 -04:00
|
|
|
/* Grow the pool, then copy the text to the pool character by
|
2007-08-02 23:38:21 -04:00
|
|
|
character, processing the encountered entities as we go
|
|
|
|
along.
|
2003-10-02 13:23:25 -04:00
|
|
|
|
2007-08-02 23:38:21 -04:00
|
|
|
It's safe (and necessary) to grow the pool in advance because
|
|
|
|
processing the entities can only *shorten* the string, it can
|
|
|
|
never lengthen it. */
|
2000-11-19 15:50:10 -05:00
|
|
|
const char *from = beg;
|
2003-10-03 12:11:09 -04:00
|
|
|
char *to;
|
2005-06-22 15:38:10 -04:00
|
|
|
bool squash_newlines = !!(flags & AP_TRIM_BLANKS);
|
2003-10-03 12:11:09 -04:00
|
|
|
|
|
|
|
POOL_GROW (pool, end - beg);
|
|
|
|
to = pool->contents + pool->tail;
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
while (from < end)
|
2007-08-02 23:38:21 -04:00
|
|
|
{
|
|
|
|
if (*from == '&')
|
|
|
|
{
|
|
|
|
int entity = decode_entity (&from, end);
|
|
|
|
if (entity != -1)
|
|
|
|
*to++ = entity;
|
|
|
|
else
|
|
|
|
*to++ = *from++;
|
|
|
|
}
|
|
|
|
else if ((*from == '\n' || *from == '\r') && squash_newlines)
|
|
|
|
++from;
|
|
|
|
else
|
|
|
|
*to++ = *from++;
|
|
|
|
}
|
2003-10-02 13:23:25 -04:00
|
|
|
/* Verify that we haven't exceeded the original size. (It
|
2007-08-02 23:38:21 -04:00
|
|
|
shouldn't happen, hence the assert.) */
|
2003-10-02 18:20:44 -04:00
|
|
|
assert (to - (pool->contents + pool->tail) <= end - beg);
|
2003-10-02 13:23:25 -04:00
|
|
|
|
|
|
|
/* Make POOL's tail point to the position following the string
|
2007-08-02 23:38:21 -04:00
|
|
|
we've written. */
|
2003-10-02 18:20:44 -04:00
|
|
|
pool->tail = to - pool->contents;
|
|
|
|
POOL_APPEND_CHR (pool, '\0');
|
2000-11-19 15:50:10 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Just copy the text to the pool. */
|
2003-10-02 18:20:44 -04:00
|
|
|
POOL_APPEND (pool, beg, end);
|
|
|
|
POOL_APPEND_CHR (pool, '\0');
|
2000-11-19 15:50:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & AP_DOWNCASE)
|
|
|
|
{
|
2003-10-02 18:20:44 -04:00
|
|
|
char *p = pool->contents + old_tail;
|
2000-11-19 15:50:10 -05:00
|
|
|
for (; *p; p++)
|
2007-10-14 17:46:24 -04:00
|
|
|
*p = c_tolower (*p);
|
2000-11-19 15:50:10 -05:00
|
|
|
}
|
|
|
|
}
|
2014-11-20 10:35:34 -05:00
|
|
|
|
2003-09-18 20:33:22 -04:00
|
|
|
/* Originally we used to adhere to rfc 1866 here, and allowed only
|
2002-05-27 11:03:35 -04:00
|
|
|
letters, digits, periods, and hyphens as names (of tags or
|
|
|
|
attributes). However, this broke too many pages which used
|
2003-09-18 20:33:22 -04:00
|
|
|
proprietary or strange attributes, e.g. <img src="a.gif"
|
2002-05-27 11:03:35 -04:00
|
|
|
v:shapes="whatever">.
|
|
|
|
|
|
|
|
So now we allow any character except:
|
|
|
|
* whitespace
|
|
|
|
* 8-bit and control chars
|
|
|
|
* characters that clearly cannot be part of name:
|
2010-05-30 08:01:10 -04:00
|
|
|
'=', '<', '>', '/'.
|
2002-05-27 11:03:35 -04:00
|
|
|
|
|
|
|
This only affects attribute and tag names; attribute values allow
|
|
|
|
an even greater variety of characters. */
|
|
|
|
|
2007-08-02 23:38:21 -04:00
|
|
|
#define NAME_CHAR_P(x) ((x) > 32 && (x) < 127 \
|
2010-05-30 08:01:10 -04:00
|
|
|
&& (x) != '=' && (x) != '<' && (x) != '>' \
|
|
|
|
&& (x) != '/')
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
#ifdef STANDALONE
|
|
|
|
static int comment_backout_count;
|
|
|
|
#endif
|
|
|
|
|
2003-09-18 20:33:22 -04:00
|
|
|
/* Advance over an SGML declaration, such as <!DOCTYPE ...>. In
|
|
|
|
strict comments mode, this is used for skipping over comments as
|
|
|
|
well.
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
To recap: any SGML declaration may have comments associated with
|
|
|
|
it, e.g.
|
|
|
|
<!MY-DECL -- isn't this fun? -- foo bar>
|
|
|
|
|
|
|
|
An HTML comment is merely an empty declaration (<!>) with a comment
|
|
|
|
attached, like this:
|
|
|
|
<!-- some stuff here -->
|
|
|
|
|
|
|
|
Several comments may be embedded in one comment declaration:
|
|
|
|
<!-- have -- -- fun -->
|
|
|
|
|
|
|
|
Whitespace is allowed between and after the comments, but not
|
2003-09-18 20:33:22 -04:00
|
|
|
before the first comment. Additionally, this function attempts to
|
|
|
|
handle double quotes in SGML declarations correctly. */
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
static const char *
|
|
|
|
advance_declaration (const char *beg, const char *end)
|
|
|
|
{
|
|
|
|
const char *p = beg;
|
2007-08-02 23:38:21 -04:00
|
|
|
char quote_char = '\0'; /* shut up, gcc! */
|
2000-11-19 15:50:10 -05:00
|
|
|
char ch;
|
2003-09-18 20:33:22 -04:00
|
|
|
|
|
|
|
enum {
|
|
|
|
AC_S_DONE,
|
|
|
|
AC_S_BACKOUT,
|
|
|
|
AC_S_BANG,
|
|
|
|
AC_S_DEFAULT,
|
|
|
|
AC_S_DCLNAME,
|
|
|
|
AC_S_DASH1,
|
|
|
|
AC_S_DASH2,
|
|
|
|
AC_S_COMMENT,
|
|
|
|
AC_S_DASH3,
|
|
|
|
AC_S_DASH4,
|
|
|
|
AC_S_QUOTE1,
|
|
|
|
AC_S_IN_QUOTE,
|
2003-10-23 08:15:39 -04:00
|
|
|
AC_S_QUOTE2
|
2003-09-18 20:33:22 -04:00
|
|
|
} state = AC_S_BANG;
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
if (beg == end)
|
|
|
|
return beg;
|
|
|
|
ch = *p++;
|
|
|
|
|
|
|
|
/* It looked like a good idea to write this as a state machine, but
|
|
|
|
now I wonder... */
|
|
|
|
|
|
|
|
while (state != AC_S_DONE && state != AC_S_BACKOUT)
|
|
|
|
{
|
|
|
|
if (p == end)
|
2007-08-02 23:38:21 -04:00
|
|
|
state = AC_S_BACKOUT;
|
2000-11-19 15:50:10 -05:00
|
|
|
switch (state)
|
2007-08-02 23:38:21 -04:00
|
|
|
{
|
|
|
|
case AC_S_DONE:
|
|
|
|
case AC_S_BACKOUT:
|
|
|
|
break;
|
|
|
|
case AC_S_BANG:
|
|
|
|
if (ch == '!')
|
|
|
|
{
|
|
|
|
ch = *p++;
|
|
|
|
state = AC_S_DEFAULT;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
state = AC_S_BACKOUT;
|
|
|
|
break;
|
|
|
|
case AC_S_DEFAULT:
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case '-':
|
|
|
|
state = AC_S_DASH1;
|
|
|
|
break;
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\r':
|
|
|
|
case '\n':
|
|
|
|
ch = *p++;
|
|
|
|
break;
|
2010-05-30 08:01:10 -04:00
|
|
|
case '<':
|
2007-08-02 23:38:21 -04:00
|
|
|
case '>':
|
|
|
|
state = AC_S_DONE;
|
|
|
|
break;
|
|
|
|
case '\'':
|
|
|
|
case '\"':
|
|
|
|
state = AC_S_QUOTE1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (NAME_CHAR_P (ch))
|
|
|
|
state = AC_S_DCLNAME;
|
|
|
|
else
|
|
|
|
state = AC_S_BACKOUT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AC_S_DCLNAME:
|
|
|
|
if (ch == '-')
|
|
|
|
state = AC_S_DASH1;
|
|
|
|
else if (NAME_CHAR_P (ch))
|
|
|
|
ch = *p++;
|
|
|
|
else
|
|
|
|
state = AC_S_DEFAULT;
|
|
|
|
break;
|
|
|
|
case AC_S_QUOTE1:
|
|
|
|
/* We must use 0x22 because broken assert macros choke on
|
|
|
|
'"' and '\"'. */
|
|
|
|
assert (ch == '\'' || ch == 0x22);
|
|
|
|
quote_char = ch; /* cheating -- I really don't feel like
|
|
|
|
introducing more different states for
|
|
|
|
different quote characters. */
|
|
|
|
ch = *p++;
|
|
|
|
state = AC_S_IN_QUOTE;
|
|
|
|
break;
|
|
|
|
case AC_S_IN_QUOTE:
|
|
|
|
if (ch == quote_char)
|
|
|
|
state = AC_S_QUOTE2;
|
|
|
|
else
|
|
|
|
ch = *p++;
|
|
|
|
break;
|
|
|
|
case AC_S_QUOTE2:
|
|
|
|
assert (ch == quote_char);
|
|
|
|
ch = *p++;
|
|
|
|
state = AC_S_DEFAULT;
|
|
|
|
break;
|
|
|
|
case AC_S_DASH1:
|
|
|
|
assert (ch == '-');
|
|
|
|
ch = *p++;
|
|
|
|
state = AC_S_DASH2;
|
|
|
|
break;
|
|
|
|
case AC_S_DASH2:
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case '-':
|
|
|
|
ch = *p++;
|
|
|
|
state = AC_S_COMMENT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
state = AC_S_BACKOUT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AC_S_COMMENT:
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case '-':
|
|
|
|
state = AC_S_DASH3;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ch = *p++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AC_S_DASH3:
|
|
|
|
assert (ch == '-');
|
|
|
|
ch = *p++;
|
|
|
|
state = AC_S_DASH4;
|
|
|
|
break;
|
|
|
|
case AC_S_DASH4:
|
|
|
|
switch (ch)
|
|
|
|
{
|
|
|
|
case '-':
|
|
|
|
ch = *p++;
|
|
|
|
state = AC_S_DEFAULT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
state = AC_S_COMMENT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2000-11-19 15:50:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (state == AC_S_BACKOUT)
|
|
|
|
{
|
|
|
|
#ifdef STANDALONE
|
|
|
|
++comment_backout_count;
|
|
|
|
#endif
|
|
|
|
return beg + 1;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
2003-09-18 20:33:22 -04:00
|
|
|
|
|
|
|
/* Find the first occurrence of the substring "-->" in [BEG, END) and
|
|
|
|
return the pointer to the character after the substring. If the
|
|
|
|
substring is not found, return NULL. */
|
|
|
|
|
|
|
|
static const char *
|
|
|
|
find_comment_end (const char *beg, const char *end)
|
|
|
|
{
|
|
|
|
/* Open-coded Boyer-Moore search for "-->". Examine the third char;
|
|
|
|
if it's not '>' or '-', advance by three characters. Otherwise,
|
|
|
|
look at the preceding characters and try to find a match. */
|
|
|
|
|
|
|
|
const char *p = beg - 1;
|
|
|
|
|
|
|
|
while ((p += 3) < end)
|
|
|
|
switch (p[0])
|
|
|
|
{
|
|
|
|
case '>':
|
2007-08-02 23:38:21 -04:00
|
|
|
if (p[-1] == '-' && p[-2] == '-')
|
|
|
|
return p + 1;
|
|
|
|
break;
|
2003-09-18 20:33:22 -04:00
|
|
|
case '-':
|
|
|
|
at_dash:
|
2007-08-02 23:38:21 -04:00
|
|
|
if (p[-1] == '-')
|
|
|
|
{
|
|
|
|
at_dash_dash:
|
|
|
|
if (++p == end) return NULL;
|
|
|
|
switch (p[0])
|
|
|
|
{
|
|
|
|
case '>': return p + 1;
|
|
|
|
case '-': goto at_dash_dash;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ((p += 2) >= end) return NULL;
|
|
|
|
switch (p[0])
|
|
|
|
{
|
|
|
|
case '>':
|
|
|
|
if (p[-1] == '-')
|
|
|
|
return p + 1;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
goto at_dash;
|
|
|
|
}
|
|
|
|
}
|
2003-09-18 20:33:22 -04:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-11-20 10:35:34 -05:00
|
|
|
|
2005-06-22 15:38:10 -04:00
|
|
|
/* Return true if the string containing of characters inside [b, e) is
|
|
|
|
present in hash table HT. */
|
2003-10-09 11:01:58 -04:00
|
|
|
|
2005-06-22 15:38:10 -04:00
|
|
|
static bool
|
2003-10-09 11:01:58 -04:00
|
|
|
name_allowed (const struct hash_table *ht, const char *b, const char *e)
|
|
|
|
{
|
|
|
|
char *copy;
|
|
|
|
if (!ht)
|
2005-06-22 15:38:10 -04:00
|
|
|
return true;
|
2003-10-09 11:01:58 -04:00
|
|
|
BOUNDED_TO_ALLOCA (b, e, copy);
|
|
|
|
return hash_table_get (ht, copy) != NULL;
|
|
|
|
}
|
|
|
|
|
2000-11-19 15:50:10 -05:00
|
|
|
/* Advance P (a char pointer), with the explicit intent of being able
|
|
|
|
to read the next character. If this is not possible, go to finish. */
|
|
|
|
|
2007-08-02 23:38:21 -04:00
|
|
|
#define ADVANCE(p) do { \
|
|
|
|
++p; \
|
|
|
|
if (p >= end) \
|
|
|
|
goto finish; \
|
2000-11-19 15:50:10 -05:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* Skip whitespace, if any. */
|
|
|
|
|
2007-08-02 23:38:21 -04:00
|
|
|
#define SKIP_WS(p) do { \
|
2007-10-14 17:46:24 -04:00
|
|
|
while (c_isspace (*p)) { \
|
2007-08-02 23:38:21 -04:00
|
|
|
ADVANCE (p); \
|
|
|
|
} \
|
2000-11-19 15:50:10 -05:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/* Skip non-whitespace, if any. */
|
|
|
|
|
2007-08-02 23:38:21 -04:00
|
|
|
#define SKIP_NON_WS(p) do { \
|
2007-10-14 17:46:24 -04:00
|
|
|
while (!c_isspace (*p)) { \
|
2007-08-02 23:38:21 -04:00
|
|
|
ADVANCE (p); \
|
|
|
|
} \
|
2000-11-19 15:50:10 -05:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#ifdef STANDALONE
|
|
|
|
static int tag_backout_count;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Map MAPFUN over HTML tags in TEXT, which is SIZE characters long.
|
|
|
|
MAPFUN will be called with two arguments: pointer to an initialized
|
2003-10-08 12:17:33 -04:00
|
|
|
struct taginfo, and MAPARG.
|
2000-11-19 15:50:10 -05:00
|
|
|
|
2005-03-19 12:25:44 -05:00
|
|
|
ALLOWED_TAGS and ALLOWED_ATTRIBUTES are hash tables the keys of
|
|
|
|
which are the tags and attribute names that this function should
|
|
|
|
use. If ALLOWED_TAGS is NULL, all tags are processed; if
|
|
|
|
ALLOWED_ATTRIBUTES is NULL, all attributes are returned.
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
(Obviously, the caller can filter out unwanted tags and attributes
|
|
|
|
just as well, but this is just an optimization designed to avoid
|
2005-03-19 12:25:44 -05:00
|
|
|
unnecessary copying of tags/attributes which the caller doesn't
|
|
|
|
care about.) */
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
void
|
|
|
|
map_html_tags (const char *text, int size,
|
2007-08-02 23:38:21 -04:00
|
|
|
void (*mapfun) (struct taginfo *, void *), void *maparg,
|
|
|
|
int flags,
|
|
|
|
const struct hash_table *allowed_tags,
|
|
|
|
const struct hash_table *allowed_attributes)
|
2000-11-19 15:50:10 -05:00
|
|
|
{
|
2003-10-02 18:20:44 -04:00
|
|
|
/* storage for strings passed to MAPFUN callback; if 256 bytes is
|
|
|
|
too little, POOL_APPEND allocates more with malloc. */
|
|
|
|
char pool_initial_storage[256];
|
|
|
|
struct pool pool;
|
|
|
|
|
2000-11-19 15:50:10 -05:00
|
|
|
const char *p = text;
|
|
|
|
const char *end = text + size;
|
|
|
|
|
2003-10-02 18:20:44 -04:00
|
|
|
struct attr_pair attr_pair_initial_storage[8];
|
|
|
|
int attr_pair_size = countof (attr_pair_initial_storage);
|
2005-06-22 15:38:10 -04:00
|
|
|
bool attr_pair_resized = false;
|
2003-10-02 18:20:44 -04:00
|
|
|
struct attr_pair *pairs = attr_pair_initial_storage;
|
2000-11-19 15:50:10 -05:00
|
|
|
|
2008-04-22 03:15:48 -04:00
|
|
|
struct tagstack_item *head = NULL;
|
|
|
|
struct tagstack_item *tail = NULL;
|
|
|
|
|
2000-11-19 15:50:10 -05:00
|
|
|
if (!size)
|
|
|
|
return;
|
|
|
|
|
2003-10-02 18:20:44 -04:00
|
|
|
POOL_INIT (&pool, pool_initial_storage, countof (pool_initial_storage));
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
int nattrs, end_tag;
|
|
|
|
const char *tag_name_begin, *tag_name_end;
|
|
|
|
const char *tag_start_position;
|
2005-06-22 15:38:10 -04:00
|
|
|
bool uninteresting_tag;
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
look_for_tag:
|
2003-10-02 18:20:44 -04:00
|
|
|
POOL_REWIND (&pool);
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
nattrs = 0;
|
|
|
|
end_tag = 0;
|
|
|
|
|
|
|
|
/* Find beginning of tag. We use memchr() instead of the usual
|
|
|
|
looping with ADVANCE() for speed. */
|
|
|
|
p = memchr (p, '<', end - p);
|
|
|
|
if (!p)
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
tag_start_position = p;
|
|
|
|
ADVANCE (p);
|
|
|
|
|
|
|
|
/* Establish the type of the tag (start-tag, end-tag or
|
|
|
|
declaration). */
|
|
|
|
if (*p == '!')
|
|
|
|
{
|
2007-08-02 23:38:21 -04:00
|
|
|
if (!(flags & MHT_STRICT_COMMENTS)
|
2014-11-01 21:01:56 -04:00
|
|
|
&& p + 3 < end && p[1] == '-' && p[2] == '-')
|
2007-08-02 23:38:21 -04:00
|
|
|
{
|
|
|
|
/* If strict comments are not enforced and if we know
|
|
|
|
we're looking at a comment, simply look for the
|
|
|
|
terminating "-->". Non-strict is the default because
|
|
|
|
it works in other browsers and most HTML writers can't
|
|
|
|
be bothered with getting the comments right. */
|
|
|
|
const char *comment_end = find_comment_end (p + 3, end);
|
|
|
|
if (comment_end)
|
|
|
|
p = comment_end;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Either in strict comment mode or looking at a non-empty
|
|
|
|
declaration. Real declarations are much less likely to
|
|
|
|
be misused the way comments are, so advance over them
|
|
|
|
properly regardless of strictness. */
|
|
|
|
p = advance_declaration (p, end);
|
|
|
|
}
|
|
|
|
if (p == end)
|
|
|
|
goto finish;
|
|
|
|
goto look_for_tag;
|
2000-11-19 15:50:10 -05:00
|
|
|
}
|
|
|
|
else if (*p == '/')
|
|
|
|
{
|
2007-08-02 23:38:21 -04:00
|
|
|
end_tag = 1;
|
|
|
|
ADVANCE (p);
|
2000-11-19 15:50:10 -05:00
|
|
|
}
|
|
|
|
tag_name_begin = p;
|
|
|
|
while (NAME_CHAR_P (*p))
|
|
|
|
ADVANCE (p);
|
|
|
|
if (p == tag_name_begin)
|
|
|
|
goto look_for_tag;
|
|
|
|
tag_name_end = p;
|
|
|
|
SKIP_WS (p);
|
2008-04-22 03:15:48 -04:00
|
|
|
|
|
|
|
if (!end_tag)
|
|
|
|
{
|
|
|
|
struct tagstack_item *ts = tagstack_push (&head, &tail);
|
|
|
|
if (ts)
|
|
|
|
{
|
|
|
|
ts->tagname_begin = tag_name_begin;
|
|
|
|
ts->tagname_end = tag_name_end;
|
|
|
|
ts->contents_begin = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-30 08:01:10 -04:00
|
|
|
if (end_tag && *p != '>' && *p != '<')
|
2000-11-19 15:50:10 -05:00
|
|
|
goto backout_tag;
|
|
|
|
|
2003-10-09 11:01:58 -04:00
|
|
|
if (!name_allowed (allowed_tags, tag_name_begin, tag_name_end))
|
2000-11-19 15:50:10 -05:00
|
|
|
/* We can't just say "goto look_for_tag" here because we need
|
|
|
|
the loop below to properly advance over the tag's attributes. */
|
2005-06-22 15:38:10 -04:00
|
|
|
uninteresting_tag = true;
|
2000-11-19 15:50:10 -05:00
|
|
|
else
|
|
|
|
{
|
2007-08-02 23:38:21 -04:00
|
|
|
uninteresting_tag = false;
|
|
|
|
convert_and_copy (&pool, tag_name_begin, tag_name_end, AP_DOWNCASE);
|
2000-11-19 15:50:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the attributes. */
|
|
|
|
while (1)
|
|
|
|
{
|
2007-08-02 23:38:21 -04:00
|
|
|
const char *attr_name_begin, *attr_name_end;
|
|
|
|
const char *attr_value_begin, *attr_value_end;
|
|
|
|
const char *attr_raw_value_begin, *attr_raw_value_end;
|
|
|
|
int operation = AP_DOWNCASE; /* stupid compiler. */
|
|
|
|
|
|
|
|
SKIP_WS (p);
|
|
|
|
|
|
|
|
if (*p == '/')
|
|
|
|
{
|
|
|
|
/* A slash at this point means the tag is about to be
|
|
|
|
closed. This is legal in XML and has been popularized
|
|
|
|
in HTML via XHTML. */
|
|
|
|
/* <foo a=b c=d /> */
|
|
|
|
/* ^ */
|
|
|
|
ADVANCE (p);
|
|
|
|
SKIP_WS (p);
|
2010-05-31 11:29:05 -04:00
|
|
|
if (*p != '<' && *p != '>')
|
2007-08-02 23:38:21 -04:00
|
|
|
goto backout_tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for end of tag definition. */
|
2010-05-30 08:01:10 -04:00
|
|
|
if (*p == '<' || *p == '>')
|
2007-08-02 23:38:21 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
/* Establish bounds of attribute name. */
|
|
|
|
attr_name_begin = p; /* <foo bar ...> */
|
|
|
|
/* ^ */
|
|
|
|
while (NAME_CHAR_P (*p))
|
|
|
|
ADVANCE (p);
|
|
|
|
attr_name_end = p; /* <foo bar ...> */
|
|
|
|
/* ^ */
|
|
|
|
if (attr_name_begin == attr_name_end)
|
|
|
|
goto backout_tag;
|
|
|
|
|
|
|
|
/* Establish bounds of attribute value. */
|
|
|
|
SKIP_WS (p);
|
2010-05-30 08:01:10 -04:00
|
|
|
|
|
|
|
if (NAME_CHAR_P (*p) || *p == '/' || *p == '<' || *p == '>')
|
2007-08-02 23:38:21 -04:00
|
|
|
{
|
|
|
|
/* Minimized attribute syntax allows `=' to be omitted.
|
2000-11-19 15:50:10 -05:00
|
|
|
For example, <UL COMPACT> is a valid shorthand for <UL
|
|
|
|
COMPACT="compact">. Even if such attributes are not
|
|
|
|
useful to Wget, we need to support them, so that the
|
|
|
|
tags containing them can be parsed correctly. */
|
2007-08-02 23:38:21 -04:00
|
|
|
attr_raw_value_begin = attr_value_begin = attr_name_begin;
|
|
|
|
attr_raw_value_end = attr_value_end = attr_name_end;
|
|
|
|
}
|
|
|
|
else if (*p == '=')
|
|
|
|
{
|
|
|
|
ADVANCE (p);
|
|
|
|
SKIP_WS (p);
|
|
|
|
if (*p == '\"' || *p == '\'')
|
|
|
|
{
|
|
|
|
bool newline_seen = false;
|
|
|
|
char quote_char = *p;
|
|
|
|
attr_raw_value_begin = p;
|
|
|
|
ADVANCE (p);
|
|
|
|
attr_value_begin = p; /* <foo bar="baz"> */
|
|
|
|
/* ^ */
|
|
|
|
while (*p != quote_char)
|
|
|
|
{
|
|
|
|
if (!newline_seen && *p == '\n')
|
|
|
|
{
|
|
|
|
/* If a newline is seen within the quotes, it
|
|
|
|
is most likely that someone forgot to close
|
|
|
|
the quote. In that case, we back out to
|
|
|
|
the value beginning, and terminate the tag
|
|
|
|
at either `>' or the delimiter, whichever
|
|
|
|
comes first. Such a tag terminated at `>'
|
|
|
|
is discarded. */
|
|
|
|
p = attr_value_begin;
|
|
|
|
newline_seen = true;
|
|
|
|
continue;
|
|
|
|
}
|
2010-05-30 08:01:10 -04:00
|
|
|
else if (newline_seen && (*p == '<' || *p == '>'))
|
2007-08-02 23:38:21 -04:00
|
|
|
break;
|
|
|
|
ADVANCE (p);
|
|
|
|
}
|
|
|
|
attr_value_end = p; /* <foo bar="baz"> */
|
|
|
|
/* ^ */
|
|
|
|
if (*p == quote_char)
|
|
|
|
ADVANCE (p);
|
|
|
|
else
|
|
|
|
goto look_for_tag;
|
|
|
|
attr_raw_value_end = p; /* <foo bar="baz"> */
|
|
|
|
/* ^ */
|
|
|
|
operation = AP_DECODE_ENTITIES;
|
|
|
|
if (flags & MHT_TRIM_VALUES)
|
|
|
|
operation |= AP_TRIM_BLANKS;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
attr_value_begin = p; /* <foo bar=baz> */
|
|
|
|
/* ^ */
|
|
|
|
/* According to SGML, a name token should consist only
|
|
|
|
of alphanumerics, . and -. However, this is often
|
|
|
|
violated by, for instance, `%' in `width=75%'.
|
|
|
|
We'll be liberal and allow just about anything as
|
|
|
|
an attribute value. */
|
2010-05-30 08:01:10 -04:00
|
|
|
while (!c_isspace (*p) && *p != '<' && *p != '>')
|
2007-08-02 23:38:21 -04:00
|
|
|
ADVANCE (p);
|
|
|
|
attr_value_end = p; /* <foo bar=baz qux=quix> */
|
|
|
|
/* ^ */
|
|
|
|
if (attr_value_begin == attr_value_end)
|
|
|
|
/* <foo bar=> */
|
|
|
|
/* ^ */
|
|
|
|
goto backout_tag;
|
|
|
|
attr_raw_value_begin = attr_value_begin;
|
|
|
|
attr_raw_value_end = attr_value_end;
|
|
|
|
operation = AP_DECODE_ENTITIES;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We skipped the whitespace and found something that is
|
|
|
|
neither `=' nor the beginning of the next attribute's
|
|
|
|
name. Back out. */
|
|
|
|
goto backout_tag; /* <foo bar [... */
|
|
|
|
/* ^ */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we're not interested in the tag, don't bother with any
|
2000-11-19 15:50:10 -05:00
|
|
|
of the attributes. */
|
2007-08-02 23:38:21 -04:00
|
|
|
if (uninteresting_tag)
|
|
|
|
continue;
|
2000-11-19 15:50:10 -05:00
|
|
|
|
2007-08-02 23:38:21 -04:00
|
|
|
/* If we aren't interested in the attribute, skip it. We
|
2000-11-19 15:50:10 -05:00
|
|
|
cannot do this test any sooner, because our text pointer
|
|
|
|
needs to correctly advance over the attribute. */
|
2007-08-02 23:38:21 -04:00
|
|
|
if (!name_allowed (allowed_attributes, attr_name_begin, attr_name_end))
|
|
|
|
continue;
|
2000-11-19 15:50:10 -05:00
|
|
|
|
2007-08-02 23:38:21 -04:00
|
|
|
GROW_ARRAY (pairs, attr_pair_size, nattrs + 1, attr_pair_resized,
|
|
|
|
struct attr_pair);
|
2000-11-19 15:50:10 -05:00
|
|
|
|
2007-08-02 23:38:21 -04:00
|
|
|
pairs[nattrs].name_pool_index = pool.tail;
|
|
|
|
convert_and_copy (&pool, attr_name_begin, attr_name_end, AP_DOWNCASE);
|
2000-11-19 15:50:10 -05:00
|
|
|
|
2007-08-02 23:38:21 -04:00
|
|
|
pairs[nattrs].value_pool_index = pool.tail;
|
|
|
|
convert_and_copy (&pool, attr_value_begin, attr_value_end, operation);
|
|
|
|
pairs[nattrs].value_raw_beginning = attr_raw_value_begin;
|
|
|
|
pairs[nattrs].value_raw_size = (attr_raw_value_end
|
|
|
|
- attr_raw_value_begin);
|
|
|
|
++nattrs;
|
2000-11-19 15:50:10 -05:00
|
|
|
}
|
|
|
|
|
2008-04-22 03:15:48 -04:00
|
|
|
if (!end_tag && tail && (tail->tagname_begin == tag_name_begin))
|
|
|
|
{
|
|
|
|
tail->contents_begin = p+1;
|
|
|
|
}
|
|
|
|
|
2000-11-19 15:50:10 -05:00
|
|
|
if (uninteresting_tag)
|
|
|
|
{
|
2007-08-02 23:38:21 -04:00
|
|
|
ADVANCE (p);
|
|
|
|
goto look_for_tag;
|
2000-11-19 15:50:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* By now, we have a valid tag with a name and zero or more
|
|
|
|
attributes. Fill in the data and call the mapper function. */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct taginfo taginfo;
|
2008-04-22 03:15:48 -04:00
|
|
|
struct tagstack_item *ts = NULL;
|
2000-11-19 15:50:10 -05:00
|
|
|
|
|
|
|
taginfo.name = pool.contents;
|
|
|
|
taginfo.end_tag_p = end_tag;
|
|
|
|
taginfo.nattrs = nattrs;
|
|
|
|
/* We fill in the char pointers only now, when pool can no
|
2007-08-02 23:38:21 -04:00
|
|
|
longer get realloc'ed. If we did that above, we could get
|
|
|
|
hosed by reallocation. Obviously, after this point, the pool
|
|
|
|
may no longer be grown. */
|
2000-11-19 15:50:10 -05:00
|
|
|
for (i = 0; i < nattrs; i++)
|
2007-08-02 23:38:21 -04:00
|
|
|
{
|
|
|
|
pairs[i].name = pool.contents + pairs[i].name_pool_index;
|
|
|
|
pairs[i].value = pool.contents + pairs[i].value_pool_index;
|
|
|
|
}
|
2000-11-19 15:50:10 -05:00
|
|
|
taginfo.attrs = pairs;
|
|
|
|
taginfo.start_position = tag_start_position;
|
|
|
|
taginfo.end_position = p + 1;
|
2008-04-22 03:15:48 -04:00
|
|
|
taginfo.contents_begin = NULL;
|
|
|
|
taginfo.contents_end = NULL;
|
|
|
|
|
|
|
|
if (end_tag)
|
|
|
|
{
|
|
|
|
ts = tagstack_find (tail, tag_name_begin, tag_name_end);
|
|
|
|
if (ts)
|
|
|
|
{
|
|
|
|
if (ts->contents_begin)
|
|
|
|
{
|
|
|
|
taginfo.contents_begin = ts->contents_begin;
|
|
|
|
taginfo.contents_end = tag_start_position;
|
|
|
|
}
|
|
|
|
tagstack_pop (&head, &tail, ts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-19 18:34:58 -04:00
|
|
|
mapfun (&taginfo, maparg);
|
2010-05-30 08:01:10 -04:00
|
|
|
if (*p != '<')
|
|
|
|
ADVANCE (p);
|
2000-11-19 15:50:10 -05:00
|
|
|
}
|
|
|
|
goto look_for_tag;
|
|
|
|
|
|
|
|
backout_tag:
|
|
|
|
#ifdef STANDALONE
|
|
|
|
++tag_backout_count;
|
|
|
|
#endif
|
|
|
|
/* The tag wasn't really a tag. Treat its contents as ordinary
|
|
|
|
data characters. */
|
|
|
|
p = tag_start_position + 1;
|
|
|
|
goto look_for_tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
finish:
|
2003-10-02 18:20:44 -04:00
|
|
|
POOL_FREE (&pool);
|
|
|
|
if (attr_pair_resized)
|
2000-11-22 11:58:28 -05:00
|
|
|
xfree (pairs);
|
2008-04-22 03:15:48 -04:00
|
|
|
/* pop any tag stack that's left */
|
|
|
|
tagstack_pop (&head, &tail, head);
|
2000-11-19 15:50:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#undef ADVANCE
|
|
|
|
#undef SKIP_WS
|
|
|
|
#undef SKIP_NON_WS
|
2014-11-20 10:35:34 -05:00
|
|
|
|
2000-11-19 15:50:10 -05:00
|
|
|
#ifdef STANDALONE
|
|
|
|
static void
|
|
|
|
test_mapper (struct taginfo *taginfo, void *arg)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf ("%s%s", taginfo->end_tag_p ? "/" : "", taginfo->name);
|
|
|
|
for (i = 0; i < taginfo->nattrs; i++)
|
|
|
|
printf (" %s=%s", taginfo->attrs[i].name, taginfo->attrs[i].value);
|
|
|
|
putchar ('\n');
|
|
|
|
++*(int *)arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main ()
|
|
|
|
{
|
|
|
|
int size = 256;
|
2005-06-19 19:03:27 -04:00
|
|
|
char *x = xmalloc (size);
|
2000-11-19 15:50:10 -05:00
|
|
|
int length = 0;
|
|
|
|
int read_count;
|
|
|
|
int tag_counter = 0;
|
|
|
|
|
2014-06-11 09:38:14 -04:00
|
|
|
#ifdef ENABLE_NLS
|
|
|
|
/* Set the current locale. */
|
|
|
|
setlocale (LC_ALL, "");
|
|
|
|
/* Set the text message domain. */
|
|
|
|
bindtextdomain ("wget", LOCALEDIR);
|
|
|
|
textdomain ("wget");
|
|
|
|
#endif /* ENABLE_NLS */
|
|
|
|
|
2000-11-19 15:50:10 -05:00
|
|
|
while ((read_count = fread (x + length, 1, size - length, stdin)))
|
|
|
|
{
|
|
|
|
length += read_count;
|
|
|
|
size <<= 1;
|
2005-06-19 19:03:27 -04:00
|
|
|
x = xrealloc (x, size);
|
2000-11-19 15:50:10 -05:00
|
|
|
}
|
|
|
|
|
2003-10-09 11:01:58 -04:00
|
|
|
map_html_tags (x, length, test_mapper, &tag_counter, 0, NULL, NULL);
|
2000-11-19 15:50:10 -05:00
|
|
|
printf ("TAGS: %d\n", tag_counter);
|
|
|
|
printf ("Tag backouts: %d\n", tag_backout_count);
|
|
|
|
printf ("Comment backouts: %d\n", comment_backout_count);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* STANDALONE */
|