Fix typo that broke non-Windows builds.
[bpt/emacs.git] / src / xml.c
1 /* Interface to libxml2.
2 Copyright (C) 2010-2012 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs 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 3 of the License, or
9 (at your option) any later version.
10
11 GNU Emacs 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.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
18
19 #include <config.h>
20
21 #ifdef HAVE_LIBXML2
22
23 #include <setjmp.h>
24 #include <libxml/tree.h>
25 #include <libxml/parser.h>
26 #include <libxml/HTMLparser.h>
27
28 #include "lisp.h"
29 #include "buffer.h"
30
31 \f
32 static Lisp_Object Qlibxml2_dll;
33
34 #ifdef WINDOWSNT
35
36 #include <windows.h>
37 #include "w32.h"
38
39 /* Macro for defining functions that will be loaded from the libxml2 DLL. */
40 #define DEF_XML2_FN(rettype,func,args) static rettype (FAR CDECL *fn_##func)args
41
42 /* Macro for loading libxml2 functions from the library. */
43 #define LOAD_XML2_FN(lib,func) { \
44 fn_##func = (void *) GetProcAddress (lib, #func); \
45 if (!fn_##func) goto bad_library; \
46 }
47
48 DEF_XML2_FN (htmlDocPtr, htmlReadMemory,
49 (const char *, int, const char *, const char *, int));
50 DEF_XML2_FN (xmlDocPtr, xmlReadMemory,
51 (const char *, int, const char *, const char *, int));
52 DEF_XML2_FN (xmlNodePtr, xmlDocGetRootElement, (xmlDocPtr));
53 DEF_XML2_FN (void, xmlFreeDoc, (xmlDocPtr));
54 DEF_XML2_FN (void, xmlCleanupParser, (void));
55 DEF_XML2_FN (void, xmlCheckVersion, (int));
56
57 static int
58 libxml2_loaded_p (void)
59 {
60 Lisp_Object found = Fassq (Qlibxml2_dll, Vlibrary_cache);
61
62 if (CONSP (found))
63 return EQ (XCDR (found), Qt) ? 1 : 0;
64 return 0;
65 }
66
67 #else /* !WINDOWSNT */
68
69 #define fn_htmlReadMemory htmlReadMemory
70 #define fn_xmlReadMemory xmlReadMemory
71 #define fn_xmlDocGetRootElement xmlDocGetRootElement
72 #define fn_xmlFreeDoc xmlFreeDoc
73 #define fn_xmlCleanupParser xmlCleanupParser
74 #define fn_xmlCheckVersion xmlCheckVersion
75
76 static inline int
77 libxml2_loaded_p (void)
78 {
79 return 1;
80 }
81
82 #endif /* !WINDOWSNT */
83
84 static int
85 init_libxml2_functions (Lisp_Object libraries)
86 {
87 #ifdef WINDOWSNT
88 Lisp_Object found = Fassq (Qlibxml2_dll, Vlibrary_cache);
89
90 if (libxml2_loaded_p ())
91 return 1;
92 else
93 {
94 HMODULE library;
95
96 if (!(library = w32_delayed_load (libraries, Qlibxml2_dll)))
97 {
98 message ("%s", "libxml2 library not found");
99 return 0;
100 }
101
102 /* LOAD_XML2_FN jumps to bad_library if it fails to find the
103 named function. */
104 LOAD_XML2_FN (library, htmlReadMemory);
105 LOAD_XML2_FN (library, xmlReadMemory);
106 LOAD_XML2_FN (library, xmlDocGetRootElement);
107 LOAD_XML2_FN (library, xmlFreeDoc);
108 LOAD_XML2_FN (library, xmlCleanupParser);
109 LOAD_XML2_FN (library, xmlCheckVersion);
110
111 Vlibrary_cache = Fcons (Fcons (Qlibxml2_dll, Qt), Vlibrary_cache);
112 return 1;
113 }
114
115 bad_library:
116 Vlibrary_cache = Fcons (Fcons (Qlibxml2_dll, Qnil), Vlibrary_cache);
117
118 return 0;
119 #else /* !WINDOWSNT */
120 return 1;
121 #endif /* !WINDOWSNT */
122 }
123
124 static Lisp_Object
125 make_dom (xmlNode *node)
126 {
127 if (node->type == XML_ELEMENT_NODE)
128 {
129 Lisp_Object result = Fcons (intern ((char *) node->name), Qnil);
130 xmlNode *child;
131 xmlAttr *property;
132 Lisp_Object plist = Qnil;
133
134 /* First add the attributes. */
135 property = node->properties;
136 while (property != NULL)
137 {
138 if (property->children &&
139 property->children->content)
140 {
141 char *content = (char *) property->children->content;
142 plist = Fcons (Fcons (intern ((char *) property->name),
143 build_string (content)),
144 plist);
145 }
146 property = property->next;
147 }
148 result = Fcons (Fnreverse (plist), result);
149
150 /* Then add the children of the node. */
151 child = node->children;
152 while (child != NULL)
153 {
154 result = Fcons (make_dom (child), result);
155 child = child->next;
156 }
157
158 return Fnreverse (result);
159 }
160 else if (node->type == XML_TEXT_NODE || node->type == XML_CDATA_SECTION_NODE)
161 {
162 if (node->content)
163 return build_string ((char *) node->content);
164 else
165 return Qnil;
166 }
167 else if (node->type == XML_COMMENT_NODE)
168 {
169 if (node->content)
170 return list3 (intern ("comment"), Qnil,
171 build_string ((char *) node->content));
172 else
173 return Qnil;
174 }
175 else
176 return Qnil;
177 }
178
179 static Lisp_Object
180 parse_region (Lisp_Object start, Lisp_Object end, Lisp_Object base_url, int htmlp)
181 {
182 xmlDoc *doc;
183 Lisp_Object result = Qnil;
184 const char *burl = "";
185 EMACS_INT bytes;
186 EMACS_INT istart, iend;
187
188 fn_xmlCheckVersion (LIBXML_VERSION);
189
190 validate_region (&start, &end);
191
192 istart = XINT (start);
193 iend = XINT (end);
194
195 if (istart < GPT && GPT < iend)
196 move_gap (iend);
197
198 if (! NILP (base_url))
199 {
200 CHECK_STRING (base_url);
201 burl = SSDATA (base_url);
202 }
203
204 bytes = CHAR_TO_BYTE (iend) - CHAR_TO_BYTE (istart);
205
206 if (htmlp)
207 doc = fn_htmlReadMemory ((char *) BYTE_POS_ADDR (CHAR_TO_BYTE (istart)),
208 bytes, burl, "utf-8",
209 HTML_PARSE_RECOVER|HTML_PARSE_NONET|
210 HTML_PARSE_NOWARNING|HTML_PARSE_NOERROR|
211 HTML_PARSE_NOBLANKS);
212 else
213 doc = fn_xmlReadMemory ((char *) BYTE_POS_ADDR (CHAR_TO_BYTE (istart)),
214 bytes, burl, "utf-8",
215 XML_PARSE_NONET|XML_PARSE_NOWARNING|
216 XML_PARSE_NOBLANKS |XML_PARSE_NOERROR);
217
218 if (doc != NULL)
219 {
220 /* If the document is just comments, then this should get us the
221 nodes anyway. */
222 xmlNode *n = doc->children->next;
223 Lisp_Object r = Qnil;
224
225 while (n) {
226 if (!NILP (r))
227 result = Fcons (r, result);
228 r = make_dom (n);
229 n = n->next;
230 }
231
232 if (NILP (result)) {
233 /* The document isn't just comments, so get the tree the
234 proper way. */
235 xmlNode *node = fn_xmlDocGetRootElement (doc);
236 if (node != NULL)
237 result = make_dom (node);
238 } else
239 result = Fcons (intern ("top"),
240 Fcons (Qnil, Fnreverse (Fcons (r, result))));
241
242 fn_xmlFreeDoc (doc);
243 }
244
245 return result;
246 }
247
248 void
249 xml_cleanup_parser (void)
250 {
251 if (libxml2_loaded_p ())
252 fn_xmlCleanupParser ();
253 }
254
255 DEFUN ("libxml-parse-html-region", Flibxml_parse_html_region,
256 Slibxml_parse_html_region,
257 2, 3, 0,
258 doc: /* Parse the region as an HTML document and return the parse tree.
259 If BASE-URL is non-nil, it is used to expand relative URLs. */)
260 (Lisp_Object start, Lisp_Object end, Lisp_Object base_url)
261 {
262 if (init_libxml2_functions (Vdynamic_library_alist))
263 return parse_region (start, end, base_url, 1);
264 return Qnil;
265 }
266
267 DEFUN ("libxml-parse-xml-region", Flibxml_parse_xml_region,
268 Slibxml_parse_xml_region,
269 2, 3, 0,
270 doc: /* Parse the region as an XML document and return the parse tree.
271 If BASE-URL is non-nil, it is used to expand relative URLs. */)
272 (Lisp_Object start, Lisp_Object end, Lisp_Object base_url)
273 {
274 if (init_libxml2_functions (Vdynamic_library_alist))
275 return parse_region (start, end, base_url, 0);
276 return Qnil;
277 }
278
279 \f
280 /***********************************************************************
281 Initialization
282 ***********************************************************************/
283 void
284 syms_of_xml (void)
285 {
286 defsubr (&Slibxml_parse_html_region);
287 defsubr (&Slibxml_parse_xml_region);
288
289 DEFSYM (Qlibxml2_dll, "libxml2");
290 }
291
292 #endif /* HAVE_LIBXML2 */