]> sjero.net Git - wget/blob - src/css-url.c
Ran update-copyright.
[wget] / src / css-url.c
1 /* Collect URLs from CSS source.
2    Copyright (C) 1998, 2000, 2001, 2002, 2003, 2009 Free Software
3    Foundation, Inc.
4
5 This file is part of GNU Wget.
6
7 GNU Wget is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11
12 GNU Wget is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Wget; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21 In addition, as a special exception, the Free Software Foundation
22 gives permission to link the code of its release of Wget with the
23 OpenSSL project's "OpenSSL" library (or with modified versions of it
24 that use the same license as the "OpenSSL" library), and distribute
25 the linked executables.  You must obey the GNU General Public License
26 in all respects for all of the code used other than "OpenSSL".  If you
27 modify this file, you may extend this exception to your version of the
28 file, but you are not obligated to do so.  If you do not wish to do
29 so, delete this exception statement from your version.  */
30
31 /*
32   Note that this is not an actual CSS parser, but just a lexical
33   scanner with a tiny bit more smarts bolted on top.  A full parser
34   is somewhat overkill for this job.  The only things we're interested
35   in are @import rules and url() tokens, so it's easy enough to
36   grab those without truly understanding the input.  The only downside
37   to this is that we might be coerced into downloading files that
38   a browser would ignore.  That might merit some more investigation.
39  */
40
41 #include <wget.h>
42
43 #include <stdio.h>
44 #ifdef HAVE_STRING_H
45 # include <string.h>
46 #else
47 # include <strings.h>
48 #endif
49 #include <stdlib.h>
50 #include <ctype.h>
51 #include <errno.h>
52 #include <assert.h>
53
54 #include "wget.h"
55 #include "utils.h"
56 #include "convert.h"
57 #include "html-url.h"
58 #include "css-tokens.h"
59
60 /* from lex.yy.c */
61 extern char *yytext;
62 extern int yyleng;
63 typedef struct yy_buffer_state *YY_BUFFER_STATE;
64 extern YY_BUFFER_STATE yy_scan_bytes (const char *bytes,int len  );
65 extern int yylex (void);
66
67 #if 1
68 const char *token_names[] = {
69   "CSSEOF",
70   "S",
71   "CDO",
72   "CDC",
73   "INCLUDES",
74   "DASHMATCH",
75   "LBRACE",
76   "PLUS",
77   "GREATER",
78   "COMMA",
79   "STRING",
80   "INVALID",
81   "IDENT",
82   "HASH",
83   "IMPORT_SYM",
84   "PAGE_SYM",
85   "MEDIA_SYM",
86   "CHARSET_SYM",
87   "IMPORTANT_SYM",
88   "EMS",
89   "EXS",
90   "LENGTH",
91   "ANGLE",
92   "TIME",
93   "FREQ",
94   "DIMENSION",
95   "PERCENTAGE",
96   "NUMBER",
97   "URI",
98   "FUNCTION"
99 };
100 #endif
101
102 /*
103   Given a detected URI token, get only the URI specified within.
104   Also adjust the starting position and length of the string.
105
106   A URI can be specified with or without quotes, and the quotes
107   can be single or double quotes.  In addition there can be
108   whitespace after the opening parenthesis and before the closing
109   parenthesis.
110 */
111 char *
112 get_uri_string (const char *at, int *pos, int *length)
113 {
114   char *uri;
115   /*char buf[1024];
116   strncpy(buf,at + *pos, *length);
117   buf[*length] = '\0';
118   DEBUGP (("get_uri_string: \"%s\"\n", buf));*/
119
120   if (0 != strncasecmp (at + *pos, "url(", 4))
121     return NULL;
122
123   *pos += 4;
124   *length -= 5; /* url() */
125   /* skip leading space */
126   while (isspace (at[*pos]))
127     {
128     (*pos)++;
129     (*length)--;
130     }
131   /* skip trailing space */
132   while (isspace (at[*pos + *length - 1]))
133     {
134       (*length)--;
135     }
136   /* trim off quotes */
137   if (at[*pos] == '\'' || at[*pos] == '"')
138     {
139       (*pos)++;
140       *length -= 2;
141     }
142
143   uri = xmalloc (*length + 1);
144   if (uri)
145     {
146       strncpy (uri, at + *pos, *length);
147       uri[*length] = '\0';      
148     }
149
150   return uri;
151 }
152
153 void
154 get_urls_css (struct map_context *ctx, int offset, int buf_length)
155 {
156   int token;
157   /*char tmp[2048];*/
158   int buffer_pos = 0;
159   int pos, length;
160   char *uri;
161
162   /*
163   strncpy(tmp,ctx->text + offset, buf_length);
164   tmp[buf_length] = '\0';
165   DEBUGP (("get_urls_css: \"%s\"\n", tmp));
166   */
167
168   /* tell flex to scan from this buffer */
169   yy_scan_bytes (ctx->text + offset, buf_length);
170
171   while((token = yylex()) != CSSEOF)
172     {
173       /*DEBUGP (("%s ", token_names[token]));*/
174       /* @import "foo.css"
175          or @import url(foo.css)
176       */
177       if(token == IMPORT_SYM)
178         {
179           do {
180             buffer_pos += yyleng;
181           } while((token = yylex()) == S);
182
183           /*DEBUGP (("%s ", token_names[token]));*/
184
185           if (token == STRING || token == URI)
186             {
187               /*DEBUGP (("Got URI "));*/
188               pos = buffer_pos + offset;
189               length = yyleng;
190
191               if (token == URI)
192                 {
193                   uri = get_uri_string (ctx->text, &pos, &length);
194                 }
195               else
196                 {
197                   /* cut out quote characters */
198                   pos++;
199                   length -= 2;
200                   uri = xmalloc (length + 1);
201                   strncpy (uri, yytext + 1, length);
202                   uri[length] = '\0';
203                 }
204
205               if (uri)
206                 {
207                   struct urlpos *up = append_url (uri, pos, length, ctx);
208                   DEBUGP (("Found @import: [%s] at %d [%s]\n", yytext, buffer_pos, uri));
209
210                   if (up)
211                     {
212                       up->link_inline_p = 1;
213                       up->link_css_p = 1;
214                       up->link_expect_css = 1;
215                     }
216
217                   xfree(uri);
218                 }
219             }
220         }
221       /* background-image: url(foo.png)
222          note that we don't care what
223          property this is actually on.
224       */
225       else if(token == URI)
226         {
227           pos = buffer_pos + offset;
228           length = yyleng;
229           uri = get_uri_string (ctx->text, &pos, &length);
230
231           if (uri)
232             {
233               struct urlpos *up = append_url (uri, pos, length, ctx);
234               DEBUGP (("Found URI: [%s] at %d [%s]\n", yytext, buffer_pos, uri));
235               if (up)
236                 {
237                   up->link_inline_p = 1;
238                   up->link_css_p = 1;
239                 }
240
241               xfree (uri);
242             }
243         }
244       buffer_pos += yyleng;
245     }
246   DEBUGP (("\n"));
247 }
248
249 struct urlpos *
250 get_urls_css_file (const char *file, const char *url)
251 {
252   struct file_memory *fm;
253   struct map_context ctx;
254
255   /* Load the file. */
256   fm = read_file (file);
257   if (!fm)
258     {
259       logprintf (LOG_NOTQUIET, "%s: %s\n", file, strerror (errno));
260       return NULL;
261     }
262   DEBUGP (("Loaded %s (size %s).\n", file, number_to_static_string (fm->length)));
263
264   ctx.text = fm->content;
265   ctx.head = ctx.tail = NULL;
266   ctx.base = NULL;
267   ctx.parent_base = url ? url : opt.base_href;
268   ctx.document_file = file;
269   ctx.nofollow = 0;
270
271   get_urls_css (&ctx, 0, fm->length);
272   read_file_free (fm);
273   return ctx.head;
274 }