]> sjero.net Git - wget/blobdiff - src/html-parse.c
[svn] Convert URLs in <form action=...>.
[wget] / src / html-parse.c
index b5efa7f21247b1a85d8c5a1ecd0997a54526e8f9..4ad331d52458852843a1c1759650146ed31d7926 100644 (file)
@@ -1,20 +1,20 @@
 /* HTML parser for Wget.
    Copyright (C) 1998, 2000 Free Software Foundation, Inc.
 
-This file is part of Wget.
+This file is part of GNU Wget.
 
-This program is free software; you can redistribute it and/or modify
+GNU Wget is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or (at
 your option) any later version.
 
-This program is distributed in the hope that it will be useful,
+GNU Wget is distributed in the hope that it will be useful,
 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
-along with this program; if not, write to the Free Software
+along with Wget; if not, write to the Free Software
 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 /* The only entry point to this module is map_html_tags(), which see.  */
@@ -69,7 +69,7 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
    as a backend for one.
 
    Due to time and other constraints, this parser was not integrated
-   into Wget until the version ???. */
+   into Wget until the version 1.7. */
 
 /* DESCRIPTION:
 
@@ -83,9 +83,12 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 
 #include <config.h>
 
+#ifdef STANDALONE
+# define I_REALLY_WANT_CTYPE_MACROS
+#endif
+
 #include <stdio.h>
 #include <stdlib.h>
-#include <ctype.h>
 #ifdef HAVE_STRING_H
 # include <string.h>
 #else
@@ -99,12 +102,25 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
 #ifdef STANDALONE
 # define xmalloc malloc
 # define xrealloc realloc
+# define xfree free
+
+# define ISSPACE(x) isspace (x)
+# define ISDIGIT(x) isdigit (x)
+# define ISALPHA(x) isalpha (x)
+# define ISALNUM(x) isalnum (x)
+# define TOLOWER(x) tolower (x)
 #endif /* STANDALONE */
 
-/* Pool support.  For efficiency, map_html_tags() stores temporary
-   string data to a single stack-allocated pool.  If the pool proves
-   too small, additional memory is allocated/resized with
-   malloc()/realloc().  */
+/* 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.  */
 
 struct pool {
   char *contents;              /* pointer to the contents. */
@@ -166,15 +182,15 @@ struct pool {
 /* Forget old pool contents.  The allocated memory is not freed. */
 #define POOL_REWIND(pool) pool.index = 0
 
-/* Free heap-allocated memory for contents of POOL.  This calls free()
-   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.  */
+/* 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.  */
 
 #define POOL_FREE(pool) do {                   \
   if (!(pool).alloca_p)                                \
-    free ((pool).contents);                    \
+    xfree ((pool).contents);                   \
   (pool).contents = (pool).orig_contents;      \
   (pool).size = (pool).orig_size;              \
   (pool).index = 0;                            \
@@ -432,7 +448,9 @@ advance_declaration (const char *beg, const char *end)
            state = AC_S_DEFAULT;
          break;
        case AC_S_QUOTE1:
-         assert (ch == '\'' || ch == '\"');
+         /* 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. */
@@ -638,6 +656,19 @@ map_html_tags (const char *text, int size,
 
        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);
+           if (*p != '>')
+             goto backout_tag;
+         }
+
        /* Check for end of tag definition. */
        if (*p == '>')
          break;
@@ -654,7 +685,7 @@ map_html_tags (const char *text, int size,
 
        /* Establish bounds of attribute value. */
        SKIP_WS (p);
-       if (NAME_CHAR_P (*p) || *p == '>')
+       if (NAME_CHAR_P (*p) || *p == '/' || *p == '>')
          {
            /* Minimized attribute syntax allows `=' to be omitted.
                For example, <UL COMPACT> is a valid shorthand for <UL
@@ -735,7 +766,7 @@ map_html_tags (const char *text, int size,
            /* 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 /... */
+           goto backout_tag;   /* <foo bar [... */
                                /*          ^    */
          }
 
@@ -812,7 +843,7 @@ map_html_tags (const char *text, int size,
  finish:
   POOL_FREE (pool);
   if (!attr_pair_alloca_p)
-    free (pairs);
+    xfree (pairs);
 }
 
 #undef ADVANCE