1 /* Collect URLs from CSS source.
2 Copyright (C) 1998, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 This file is part of GNU Wget.
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.
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.
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.
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. */
31 Note that this is not an actual CSS parser, but just a lexical
32 scanner with a tiny bit more smarts bolted on top. A full parser
33 is somewhat overkill for this job. The only things we're interested
34 in are @import rules and url() tokens, so it's easy enough to
35 grab those without truly understanding the input. The only downside
36 to this is that we might be coerced into downloading files that
37 a browser would ignore. That might merit some more investigation.
57 #include "css-tokens.h"
62 typedef struct yy_buffer_state *YY_BUFFER_STATE;
63 extern YY_BUFFER_STATE yy_scan_bytes (const char *bytes,int len );
64 extern int yylex (void);
67 const char *token_names[] = {
102 Given a detected URI token, get only the URI specified within.
103 Also adjust the starting position and length of the string.
105 A URI can be specified with or without quotes, and the quotes
106 can be single or double quotes. In addition there can be
107 whitespace after the opening parenthesis and before the closing
111 get_uri_string (const char *at, int *pos, int *length)
115 strncpy(buf,at + *pos, *length);
117 DEBUGP (("get_uri_string: \"%s\"\n", buf));*/
119 if (0 != strncasecmp (at + *pos, "url(", 4))
123 *length -= 5; /* url() */
124 /* skip leading space */
125 while (isspace (at[*pos]))
130 /* skip trailing space */
131 while (isspace (at[*pos + *length - 1]))
135 /* trim off quotes */
136 if (at[*pos] == '\'' || at[*pos] == '"')
142 uri = xmalloc (*length + 1);
145 strncpy (uri, at + *pos, *length);
153 get_urls_css (struct map_context *ctx, int offset, int buf_length)
162 strncpy(tmp,ctx->text + offset, buf_length);
163 tmp[buf_length] = '\0';
164 DEBUGP (("get_urls_css: \"%s\"\n", tmp));
167 /* tell flex to scan from this buffer */
168 yy_scan_bytes (ctx->text + offset, buf_length);
170 while((token = yylex()) != CSSEOF)
172 /*DEBUGP (("%s ", token_names[token]));*/
174 or @import url(foo.css)
176 if(token == IMPORT_SYM)
179 buffer_pos += yyleng;
180 } while((token = yylex()) == S);
182 /*DEBUGP (("%s ", token_names[token]));*/
184 if (token == STRING || token == URI)
186 /*DEBUGP (("Got URI "));*/
187 pos = buffer_pos + offset;
192 uri = get_uri_string (ctx->text, &pos, &length);
196 /* cut out quote characters */
199 uri = xmalloc (length + 1);
200 strncpy (uri, yytext + 1, length);
206 struct urlpos *up = append_url (uri, pos, length, ctx);
207 DEBUGP (("Found @import: [%s] at %d [%s]\n", yytext, buffer_pos, uri));
211 up->link_inline_p = 1;
213 up->link_expect_css = 1;
220 /* background-image: url(foo.png)
221 note that we don't care what
222 property this is actually on.
224 else if(token == URI)
226 pos = buffer_pos + offset;
228 uri = get_uri_string (ctx->text, &pos, &length);
232 struct urlpos *up = append_url (uri, pos, length, ctx);
233 DEBUGP (("Found URI: [%s] at %d [%s]\n", yytext, buffer_pos, uri));
236 up->link_inline_p = 1;
243 buffer_pos += yyleng;
249 get_urls_css_file (const char *file, const char *url)
251 struct file_memory *fm;
252 struct map_context ctx;
255 fm = read_file (file);
258 logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
261 DEBUGP (("Loaded %s (size %s).\n", file, number_to_static_string (fm->length)));
263 ctx.text = fm->content;
264 ctx.head = ctx.tail = NULL;
266 ctx.parent_base = url ? url : opt.base_href;
267 ctx.document_file = file;
270 get_urls_css (&ctx, 0, fm->length);