1 /* Conversion of links to local files.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
5 This file is part of GNU Wget.
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 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with Wget. If not, see <http://www.gnu.org/licenses/>.
20 Additional permission under GNU GPL version 3 section 7
22 If you modify this program, or any covered work, by linking or
23 combining it with the OpenSSL project's OpenSSL library (or a
24 modified version of that library), containing parts covered by the
25 terms of the OpenSSL or SSLeay licenses, the Free Software Foundation
26 grants you additional permission to convey the resulting work.
27 Corresponding Source for a non-source form of such a combination
28 shall include the source code for the parts of OpenSSL used as well
29 as that of the covered work. */
50 static struct hash_table *dl_file_url_map;
51 struct hash_table *dl_url_file_map;
53 /* Set of HTML/CSS files downloaded in this Wget run, used for link
54 conversion after Wget is done. */
55 struct hash_table *downloaded_html_set;
56 struct hash_table *downloaded_css_set;
58 static void convert_links (const char *, struct urlpos *);
62 convert_links_in_hashtable (struct hash_table *downloaded_set,
73 cnt = hash_table_count (downloaded_set);
76 file_array = alloca_array (char *, cnt);
77 string_set_to_array (downloaded_set, file_array);
79 for (i = 0; i < cnt; i++)
81 struct urlpos *urls, *cur_url;
83 char *file = file_array[i];
85 /* Determine the URL of the file. get_urls_{html,css} will need
87 url = hash_table_get (dl_file_url_map, file);
90 DEBUGP (("Apparently %s has been removed.\n", file));
94 DEBUGP (("Scanning %s (from %s)\n", file, url));
96 /* Parse the file... */
97 urls = is_css ? get_urls_css_file (file, url) :
98 get_urls_html (file, url, NULL, NULL);
100 /* We don't respect meta_disallow_follow here because, even if
101 the file is not followed, we might still want to convert the
102 links that have been followed from other files. */
104 for (cur_url = urls; cur_url; cur_url = cur_url->next)
110 if (cur_url->link_base_p)
112 /* Base references have been resolved by our parser, so
113 we turn the base URL into an empty string. (Perhaps
114 we should remove the tag entirely?) */
115 cur_url->convert = CO_NULLIFY_BASE;
119 /* We decide the direction of conversion according to whether
120 a URL was downloaded. Downloaded URLs will be converted
121 ABS2REL, whereas non-downloaded will be converted REL2ABS. */
124 set_uri_encoding (pi, opt.locale, true);
126 u = url_parse (cur_url->url->url, NULL, pi, true);
130 local_name = hash_table_get (dl_url_file_map, u->url);
132 /* Decide on the conversion type. */
135 /* We've downloaded this URL. Convert it to relative
136 form. We do this even if the URL already is in
137 relative form, because our directory structure may
138 not be identical to that on the server (think `-nd',
139 `--cut-dirs', etc.) */
140 cur_url->convert = CO_CONVERT_TO_RELATIVE;
141 cur_url->local_name = xstrdup (local_name);
142 DEBUGP (("will convert url %s to local %s\n", u->url, local_name));
146 /* We haven't downloaded this URL. If it's not already
147 complete (including a full host name), convert it to
148 that form, so it can be reached while browsing this
150 if (!cur_url->link_complete_p)
151 cur_url->convert = CO_CONVERT_TO_COMPLETE;
152 cur_url->local_name = NULL;
153 DEBUGP (("will convert url %s to complete\n", u->url));
160 /* Convert the links in the file. */
161 convert_links (file, urls);
169 /* This function is called when the retrieval is done to convert the
170 links that have been downloaded. It has to be called at the end of
171 the retrieval, because only then does Wget know conclusively which
172 URLs have been downloaded, and which not, so it can tell which
173 direction to convert to.
175 The "direction" means that the URLs to the files that have been
176 downloaded get converted to the relative URL which will point to
177 that file. And the other URLs get converted to the remote URL on
180 All the downloaded HTMLs are kept in downloaded_html_files, and
181 downloaded URLs in urls_downloaded. All the information is
182 extracted from these two lists. */
185 convert_all_links (void)
190 struct ptimer *timer = ptimer_new ();
192 convert_links_in_hashtable (downloaded_html_set, 0, &file_count);
193 convert_links_in_hashtable (downloaded_css_set, 1, &file_count);
195 secs = ptimer_measure (timer);
196 logprintf (LOG_VERBOSE, _("Converted %d files in %s seconds.\n"),
197 file_count, print_decimal (secs));
199 ptimer_destroy (timer);
202 static void write_backup_file (const char *, downloaded_file_t);
203 static const char *replace_plain (const char*, int, FILE*, const char *);
204 static const char *replace_attr (const char *, int, FILE *, const char *);
205 static const char *replace_attr_refresh_hack (const char *, int, FILE *,
207 static char *local_quote_string (const char *, bool);
208 static char *construct_relative (const char *, const char *);
210 /* Change the links in one file. LINKS is a list of links in the
211 document, along with their positions and the desired direction of
214 convert_links (const char *file, struct urlpos *links)
216 struct file_memory *fm;
219 downloaded_file_t downloaded_file_return;
222 int to_url_count = 0, to_file_count = 0;
224 logprintf (LOG_VERBOSE, _("Converting %s... "), file);
227 /* First we do a "dry run": go through the list L and see whether
228 any URL needs to be converted in the first place. If not, just
229 leave the file alone. */
232 for (dry = links; dry; dry = dry->next)
233 if (dry->convert != CO_NOCONVERT)
237 logputs (LOG_VERBOSE, _("nothing to do.\n"));
242 fm = wget_read_file (file);
245 logprintf (LOG_NOTQUIET, _("Cannot convert links in %s: %s\n"),
246 file, strerror (errno));
250 downloaded_file_return = downloaded_file (CHECK_FOR_FILE, file);
251 if (opt.backup_converted && downloaded_file_return)
252 write_backup_file (file, downloaded_file_return);
254 /* Before opening the file for writing, unlink the file. This is
255 important if the data in FM is mmaped. In such case, nulling the
256 file, which is what fopen() below does, would make us read all
257 zeroes from the mmaped region. */
258 if (unlink (file) < 0 && errno != ENOENT)
260 logprintf (LOG_NOTQUIET, _("Unable to delete %s: %s\n"),
261 quote (file), strerror (errno));
262 wget_read_file_free (fm);
265 /* Now open the file for writing. */
266 fp = fopen (file, "wb");
269 logprintf (LOG_NOTQUIET, _("Cannot convert links in %s: %s\n"),
270 file, strerror (errno));
271 wget_read_file_free (fm);
275 /* Here we loop through all the URLs in file, replacing those of
276 them that are downloaded with relative references. */
278 for (link = links; link; link = link->next)
280 char *url_start = fm->content + link->pos;
282 if (link->pos >= fm->length)
284 DEBUGP (("Something strange is going on. Please investigate."));
287 /* If the URL is not to be converted, skip it. */
288 if (link->convert == CO_NOCONVERT)
290 DEBUGP (("Skipping %s at position %d.\n", link->url->url, link->pos));
294 /* Echo the file contents, up to the offending URL's opening
295 quote, to the outfile. */
296 fwrite (p, 1, url_start - p, fp);
299 switch (link->convert)
301 case CO_CONVERT_TO_RELATIVE:
302 /* Convert absolute URL to relative. */
304 char *newname = construct_relative (file, link->local_name);
305 char *quoted_newname = local_quote_string (newname,
308 if (link->link_css_p)
309 p = replace_plain (p, link->size, fp, quoted_newname);
310 else if (!link->link_refresh_p)
311 p = replace_attr (p, link->size, fp, quoted_newname);
313 p = replace_attr_refresh_hack (p, link->size, fp, quoted_newname,
314 link->refresh_timeout);
316 DEBUGP (("TO_RELATIVE: %s to %s at position %d in %s.\n",
317 link->url->url, newname, link->pos, file));
319 xfree (quoted_newname);
323 case CO_CONVERT_TO_COMPLETE:
324 /* Convert the link to absolute URL. */
326 char *newlink = link->url->url;
327 char *quoted_newlink = html_quote_string (newlink);
329 if (link->link_css_p)
330 p = replace_plain (p, link->size, fp, newlink);
331 else if (!link->link_refresh_p)
332 p = replace_attr (p, link->size, fp, quoted_newlink);
334 p = replace_attr_refresh_hack (p, link->size, fp, quoted_newlink,
335 link->refresh_timeout);
337 DEBUGP (("TO_COMPLETE: <something> to %s at position %d in %s.\n",
338 newlink, link->pos, file));
339 xfree (quoted_newlink);
343 case CO_NULLIFY_BASE:
344 /* Change the base href to "". */
345 p = replace_attr (p, link->size, fp, "");
353 /* Output the rest of the file. */
354 if (p - fm->content < fm->length)
355 fwrite (p, 1, fm->length - (p - fm->content), fp);
357 wget_read_file_free (fm);
359 logprintf (LOG_VERBOSE, "%d-%d\n", to_file_count, to_url_count);
362 /* Construct and return a link that points from BASEFILE to LINKFILE.
363 Both files should be local file names, BASEFILE of the referrering
364 file, and LINKFILE of the referred file.
368 cr("foo", "bar") -> "bar"
369 cr("A/foo", "A/bar") -> "bar"
370 cr("A/foo", "A/B/bar") -> "B/bar"
371 cr("A/X/foo", "A/Y/bar") -> "../Y/bar"
372 cr("X/", "Y/bar") -> "../Y/bar" (trailing slash does matter in BASE)
374 Both files should be absolute or relative, otherwise strange
375 results might ensue. The function makes no special efforts to
376 handle "." and ".." in links, so make sure they're not there
377 (e.g. using path_simplify). */
380 construct_relative (const char *basefile, const char *linkfile)
387 /* First, skip the initial directory components common to both
390 for (b = basefile, l = linkfile; *b == *l && *b != '\0'; ++b, ++l)
393 start = (b - basefile) + 1;
398 /* With common directories out of the way, the situation we have is
400 b - b1/b2/[...]/bfile
401 l - l1/l2/[...]/lfile
403 The link we're constructing needs to be:
404 lnk - ../../l1/l2/[...]/lfile
406 Where the number of ".."'s equals the number of bN directory
409 /* Count the directory components in B. */
411 for (b = basefile; *b; b++)
417 /* Construct LINK as explained above. */
418 link = xmalloc (3 * basedirs + strlen (linkfile) + 1);
419 for (i = 0; i < basedirs; i++)
420 memcpy (link + 3 * i, "../", 3);
421 strcpy (link + 3 * i, linkfile);
425 /* Used by write_backup_file to remember which files have been
427 static struct hash_table *converted_files;
430 write_backup_file (const char *file, downloaded_file_t downloaded_file_return)
432 /* Rather than just writing over the original .html file with the
433 converted version, save the former to *.orig. Note we only do
434 this for files we've _successfully_ downloaded, so we don't
435 clobber .orig files sitting around from previous invocations.
436 On VMS, use "_orig" instead of ".orig". See "wget.h". */
438 /* Construct the backup filename as the original name plus ".orig". */
439 size_t filename_len = strlen (file);
440 char* filename_plus_orig_suffix;
442 /* TODO: hack this to work with css files */
443 if (downloaded_file_return == FILE_DOWNLOADED_AND_HTML_EXTENSION_ADDED)
445 /* Just write "orig" over "html". We need to do it this way
446 because when we're checking to see if we've downloaded the
447 file before (to see if we can skip downloading it), we don't
448 know if it's a text/html file. Therefore we don't know yet
449 at that stage that -E is going to cause us to tack on
450 ".html", so we need to compare vs. the original URL plus
451 ".orig", not the original URL plus ".html.orig". */
452 filename_plus_orig_suffix = alloca (filename_len + 1);
453 strcpy (filename_plus_orig_suffix, file);
454 strcpy ((filename_plus_orig_suffix + filename_len) - 4, "orig");
456 else /* downloaded_file_return == FILE_DOWNLOADED_NORMALLY */
458 /* Append ".orig" to the name. */
459 filename_plus_orig_suffix = alloca (filename_len + sizeof (ORIG_SFX));
460 strcpy (filename_plus_orig_suffix, file);
461 strcpy (filename_plus_orig_suffix + filename_len, ORIG_SFX);
464 if (!converted_files)
465 converted_files = make_string_hash_table (0);
467 /* We can get called twice on the same URL thanks to the
468 convert_all_links() call in main(). If we write the .orig file
469 each time in such a case, it'll end up containing the first-pass
470 conversion, not the original file. So, see if we've already been
471 called on this file. */
472 if (!string_set_contains (converted_files, file))
474 /* Rename <file> to <file>.orig before former gets written over. */
475 if (rename (file, filename_plus_orig_suffix) != 0)
476 logprintf (LOG_NOTQUIET, _("Cannot back up %s as %s: %s\n"),
477 file, filename_plus_orig_suffix, strerror (errno));
479 /* Remember that we've already written a .orig backup for this file.
480 Note that we never free this memory since we need it till the
481 convert_all_links() call, which is one of the last things the
482 program does before terminating. BTW, I'm not sure if it would be
483 safe to just set 'converted_file_ptr->string' to 'file' below,
484 rather than making a copy of the string... Another note is that I
485 thought I could just add a field to the urlpos structure saying
486 that we'd written a .orig file for this URL, but that didn't work,
487 so I had to make this separate list.
488 -- Dan Harkless <wget@harkless.org>
490 This [adding a field to the urlpos structure] didn't work
491 because convert_file() is called from convert_all_links at
492 the end of the retrieval with a freshly built new urlpos
494 -- Hrvoje Niksic <hniksic@xemacs.org>
496 string_set_add (converted_files, file);
500 static bool find_fragment (const char *, int, const char **, const char **);
502 /* Replace a string with NEW_TEXT. Ignore quoting. */
504 replace_plain (const char *p, int size, FILE *fp, const char *new_text)
506 fputs (new_text, fp);
511 /* Replace an attribute's original text with NEW_TEXT. */
514 replace_attr (const char *p, int size, FILE *fp, const char *new_text)
516 bool quote_flag = false;
517 char quote_char = '\"'; /* use "..." for quoting, unless the
518 original value is quoted, in which
519 case reuse its quoting char. */
520 const char *frag_beg, *frag_end;
522 /* Structure of our string is:
524 <--- size ---> (with quotes)
527 <--- size --> (no quotes) */
529 if (*p == '\"' || *p == '\'')
534 size -= 2; /* disregard opening and closing quote */
536 putc (quote_char, fp);
537 fputs (new_text, fp);
539 /* Look for fragment identifier, if any. */
540 if (find_fragment (p, size, &frag_beg, &frag_end))
541 fwrite (frag_beg, 1, frag_end - frag_beg, fp);
545 putc (quote_char, fp);
550 /* The same as REPLACE_ATTR, but used when replacing
551 <meta http-equiv=refresh content="new_text"> because we need to
552 append "timeout_value; URL=" before the next_text. */
555 replace_attr_refresh_hack (const char *p, int size, FILE *fp,
556 const char *new_text, int timeout)
559 char *new_with_timeout = (char *)alloca (numdigit (timeout)
563 sprintf (new_with_timeout, "%d; URL=%s", timeout, new_text);
565 return replace_attr (p, size, fp, new_with_timeout);
568 /* Find the first occurrence of '#' in [BEG, BEG+SIZE) that is not
569 preceded by '&'. If the character is not found, return zero. If
570 the character is found, return true and set BP and EP to point to
571 the beginning and end of the region.
573 This is used for finding the fragment indentifiers in URLs. */
576 find_fragment (const char *beg, int size, const char **bp, const char **ep)
578 const char *end = beg + size;
579 bool saw_amp = false;
580 for (; beg < end; beg++)
602 /* Quote FILE for use as local reference to an HTML file.
604 We quote ? as %3F to avoid passing part of the file name as the
605 parameter when browsing the converted file through HTTP. However,
606 it is safe to do this only when `--adjust-extension' is turned on.
607 This is because converting "index.html?foo=bar" to
608 "index.html%3Ffoo=bar" would break local browsing, as the latter
609 isn't even recognized as an HTML file! However, converting
610 "index.html?foo=bar.html" to "index.html%3Ffoo=bar.html" should be
611 safe for both local and HTTP-served browsing.
613 We always quote "#" as "%23", "%" as "%25" and ";" as "%3B"
614 because those characters have special meanings in URLs. */
617 local_quote_string (const char *file, bool no_html_quote)
622 char *any = strpbrk (file, "?#%;");
624 return no_html_quote ? strdup (file) : html_quote_string (file);
626 /* Allocate space assuming the worst-case scenario, each character
627 having to be quoted. */
628 to = newname = (char *)alloca (3 * strlen (file) + 1);
629 for (from = file; *from; from++)
648 if (opt.adjust_extension)
661 return no_html_quote ? strdup (newname) : html_quote_string (newname);
664 /* Book-keeping code for dl_file_url_map, dl_url_file_map,
665 downloaded_html_list, and downloaded_html_set. Other code calls
666 these functions to let us know that a file has been downloaded. */
668 #define ENSURE_TABLES_EXIST do { \
669 if (!dl_file_url_map) \
670 dl_file_url_map = make_string_hash_table (0); \
671 if (!dl_url_file_map) \
672 dl_url_file_map = make_string_hash_table (0); \
675 /* Return true if S1 and S2 are the same, except for "/index.html".
676 The three cases in which it returns one are (substitute any
677 substring for "foo"):
679 m("foo/index.html", "foo/") ==> 1
680 m("foo/", "foo/index.html") ==> 1
681 m("foo", "foo/index.html") ==> 1
682 m("foo", "foo/" ==> 1
683 m("foo", "foo") ==> 1 */
686 match_except_index (const char *s1, const char *s2)
691 /* Skip common substring. */
692 for (i = 0; *s1 && *s2 && *s1 == *s2; s1++, s2++, i++)
695 /* Strings differ at the very beginning -- bail out. We need to
696 check this explicitly to avoid `lng - 1' reading outside the
701 /* Both strings hit EOF -- strings are equal. */
704 /* Strings are randomly different, e.g. "/foo/bar" and "/foo/qux". */
707 /* S1 is the longer one. */
710 /* S2 is the longer one. */
714 /* foo/index.html */ /* or */ /* foo/index.html */
718 /* The right-hand case. */
721 if (*lng == '/' && *(lng + 1) == '\0')
726 return 0 == strcmp (lng, "/index.html");
730 dissociate_urls_from_file_mapper (void *key, void *value, void *arg)
732 char *mapping_url = (char *)key;
733 char *mapping_file = (char *)value;
734 char *file = (char *)arg;
736 if (0 == strcmp (mapping_file, file))
738 hash_table_remove (dl_url_file_map, mapping_url);
740 xfree (mapping_file);
743 /* Continue mapping. */
747 /* Remove all associations from various URLs to FILE from dl_url_file_map. */
750 dissociate_urls_from_file (const char *file)
752 /* Can't use hash_table_iter_* because the table mutates while mapping. */
753 hash_table_for_each (dl_url_file_map, dissociate_urls_from_file_mapper,
757 /* Register that URL has been successfully downloaded to FILE. This
758 is used by the link conversion code to convert references to URLs
759 to references to local files. It is also being used to check if a
760 URL has already been downloaded. */
763 register_download (const char *url, const char *file)
765 char *old_file, *old_url;
769 /* With some forms of retrieval, it is possible, although not likely
770 or particularly desirable. If both are downloaded, the second
771 download will override the first one. When that happens,
772 dissociate the old file name from the URL. */
774 if (hash_table_get_pair (dl_file_url_map, file, &old_file, &old_url))
776 if (0 == strcmp (url, old_url))
777 /* We have somehow managed to download the same URL twice.
781 if (match_except_index (url, old_url)
782 && !hash_table_contains (dl_url_file_map, url))
783 /* The two URLs differ only in the "index.html" ending. For
784 example, one is "http://www.server.com/", and the other is
785 "http://www.server.com/index.html". Don't remove the old
786 one, just add the new one as a non-canonical entry. */
789 hash_table_remove (dl_file_url_map, file);
793 /* Remove all the URLs that point to this file. Yes, there can
794 be more than one such URL, because we store redirections as
795 multiple entries in dl_url_file_map. For example, if URL1
796 redirects to URL2 which gets downloaded to FILE, we map both
797 URL1 and URL2 to FILE in dl_url_file_map. (dl_file_url_map
798 only points to URL2.) When another URL gets loaded to FILE,
799 we want both URL1 and URL2 dissociated from it.
801 This is a relatively expensive operation because it performs
802 a linear search of the whole hash table, but it should be
803 called very rarely, only when two URLs resolve to the same
804 file name, *and* the "<file>.1" extensions are turned off.
805 In other words, almost never. */
806 dissociate_urls_from_file (file);
809 hash_table_put (dl_file_url_map, xstrdup (file), xstrdup (url));
812 /* A URL->FILE mapping is not possible without a FILE->URL mapping.
813 If the latter were present, it should have been removed by the
814 above `if'. So we could write:
816 assert (!hash_table_contains (dl_url_file_map, url));
818 The above is correct when running in recursive mode where the
819 same URL always resolves to the same file. But if you do
824 then the first URL will resolve to "FILE", and the other to
825 "FILE.1". In that case, FILE.1 will not be found in
826 dl_file_url_map, but URL will still point to FILE in
828 if (hash_table_get_pair (dl_url_file_map, url, &old_url, &old_file))
830 hash_table_remove (dl_url_file_map, url);
835 hash_table_put (dl_url_file_map, xstrdup (url), xstrdup (file));
838 /* Register that FROM has been redirected to TO. This assumes that TO
839 is successfully downloaded and already registered using
840 register_download() above. */
843 register_redirection (const char *from, const char *to)
849 file = hash_table_get (dl_url_file_map, to);
850 assert (file != NULL);
851 if (!hash_table_contains (dl_url_file_map, from))
852 hash_table_put (dl_url_file_map, xstrdup (from), xstrdup (file));
855 /* Register that the file has been deleted. */
858 register_delete_file (const char *file)
860 char *old_url, *old_file;
864 if (!hash_table_get_pair (dl_file_url_map, file, &old_file, &old_url))
867 hash_table_remove (dl_file_url_map, file);
870 dissociate_urls_from_file (file);
873 /* Register that FILE is an HTML file that has been downloaded. */
876 register_html (const char *file)
878 if (!downloaded_html_set)
879 downloaded_html_set = make_string_hash_table (0);
880 string_set_add (downloaded_html_set, file);
883 /* Register that FILE is a CSS file that has been downloaded. */
886 register_css (const char *file)
888 if (!downloaded_css_set)
889 downloaded_css_set = make_string_hash_table (0);
890 string_set_add (downloaded_css_set, file);
893 static void downloaded_files_free (void);
895 /* Cleanup the data structures associated with this file. */
898 convert_cleanup (void)
902 free_keys_and_values (dl_file_url_map);
903 hash_table_destroy (dl_file_url_map);
904 dl_file_url_map = NULL;
908 free_keys_and_values (dl_url_file_map);
909 hash_table_destroy (dl_url_file_map);
910 dl_url_file_map = NULL;
912 if (downloaded_html_set)
913 string_set_free (downloaded_html_set);
914 downloaded_files_free ();
916 string_set_free (converted_files);
919 /* Book-keeping code for downloaded files that enables extension
922 /* This table should really be merged with dl_file_url_map and
923 downloaded_html_files. This was originally a list, but I changed
924 it to a hash table beause it was actually taking a lot of time to
925 find things in it. */
927 static struct hash_table *downloaded_files_hash;
929 /* We're storing "modes" of type downloaded_file_t in the hash table.
930 However, our hash tables only accept pointers for keys and values.
931 So when we need a pointer, we use the address of a
932 downloaded_file_t variable of static storage. */
934 static downloaded_file_t *
935 downloaded_mode_to_ptr (downloaded_file_t mode)
937 static downloaded_file_t
938 v1 = FILE_NOT_ALREADY_DOWNLOADED,
939 v2 = FILE_DOWNLOADED_NORMALLY,
940 v3 = FILE_DOWNLOADED_AND_HTML_EXTENSION_ADDED,
945 case FILE_NOT_ALREADY_DOWNLOADED:
947 case FILE_DOWNLOADED_NORMALLY:
949 case FILE_DOWNLOADED_AND_HTML_EXTENSION_ADDED:
957 /* Remembers which files have been downloaded. In the standard case,
958 should be called with mode == FILE_DOWNLOADED_NORMALLY for each
959 file we actually download successfully (i.e. not for ones we have
960 failures on or that we skip due to -N).
962 When we've downloaded a file and tacked on a ".html" extension due
963 to -E, call this function with
964 FILE_DOWNLOADED_AND_HTML_EXTENSION_ADDED rather than
965 FILE_DOWNLOADED_NORMALLY.
967 If you just want to check if a file has been previously added
968 without adding it, call with mode == CHECK_FOR_FILE. Please be
969 sure to call this function with local filenames, not remote
973 downloaded_file (downloaded_file_t mode, const char *file)
975 downloaded_file_t *ptr;
977 if (mode == CHECK_FOR_FILE)
979 if (!downloaded_files_hash)
980 return FILE_NOT_ALREADY_DOWNLOADED;
981 ptr = hash_table_get (downloaded_files_hash, file);
983 return FILE_NOT_ALREADY_DOWNLOADED;
987 if (!downloaded_files_hash)
988 downloaded_files_hash = make_string_hash_table (0);
990 ptr = hash_table_get (downloaded_files_hash, file);
994 ptr = downloaded_mode_to_ptr (mode);
995 hash_table_put (downloaded_files_hash, xstrdup (file), ptr);
997 return FILE_NOT_ALREADY_DOWNLOADED;
1001 downloaded_files_free (void)
1003 if (downloaded_files_hash)
1005 hash_table_iterator iter;
1006 for (hash_table_iterate (downloaded_files_hash, &iter);
1007 hash_table_iter_next (&iter);
1010 hash_table_destroy (downloaded_files_hash);
1011 downloaded_files_hash = NULL;
1015 /* The function returns the pointer to the malloc-ed quoted version of
1016 string s. It will recognize and quote numeric and special graphic
1017 entities, as per RFC1866:
1025 No other entities are recognized or replaced. */
1027 html_quote_string (const char *s)
1033 /* Pass through the string, and count the new size. */
1034 for (i = 0; *s; s++, i++)
1037 i += 4; /* `amp;' */
1038 else if (*s == '<' || *s == '>')
1039 i += 3; /* `lt;' and `gt;' */
1040 else if (*s == '\"')
1041 i += 5; /* `quot;' */
1045 res = xmalloc (i + 1);
1047 for (p = res; *s; s++)
1060 *p++ = (*s == '<' ? 'l' : 'g');