Debian Codegraph @

src/xml_format.c

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /* $Id: xml_format.c,v 1.25 2005/01/07 02:33:40 mgrouch Exp $ */
2
3 /*
4
5 XMLStarlet: Command Line Toolkit to query/edit/check/transform XML documents
6
7 Copyright (c) 2002 Mikhail Grushinskiy. All Rights Reserved.
8
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26
27 */
28
29 #include <config.h>
30
31 #include <string.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34
35 #include <libxml/xmlmemory.h>
36 #include <libxml/debugXML.h>
37 #include <libxml/xmlIO.h>
38 #include <libxml/HTMLtree.h>
39 #include <libxml/xinclude.h>
40 #include <libxml/xpath.h>
41 #include <libxml/xpathInternals.h>
42 #include <libxml/xpointer.h>
43 #include <libxml/parserInternals.h>
44 #include <libxml/uri.h>
45
46 #include "xmlstar.h"
47
48 /*
49 * TODO: 1. Attribute formatting options (as every attribute on a new line)
50 * 2. exit values on errors
51 */
52
53 typedef struct _foOptions {
54 int indent; /* indent output */
55 int indent_tab; /* indent output with tab */
56 int indent_spaces; /* num spaces for indentation */
57 int omit_decl; /* omit xml declaration */
58 int recovery; /* try to recover what is parsable */
59 int dropdtd; /* remove the DOCTYPE of the input docs */
60 int options; /* global parsing flags */
61 #ifdef LIBXML_HTML_ENABLED
62 int html; /* inputs are in HTML format */
63 #endif
64 int quiet; /* quiet mode */
65 } foOptions;
66
67 typedef foOptions *foOptionsPtr;
68
69 const char *encoding = NULL;
70 static char *spaces = NULL;
71
72 /**
73 * Print small help for command line options
74 */
75 void
76 foUsage(int argc, char **argv, exit_status status)
77 {
78 extern void fprint_format_usage(FILE* o, const char* argv0);
79 extern const char more_info[];
80 FILE *o = (status == EXIT_SUCCESS)? stdout : stderr;
81 fprint_format_usage(o, argv[0]);
82 fprintf(o, "%s", more_info);
83 exit(status);
84 }
85
86 /**
87 * Initialize global command line options
88 */
89 void
90 foInitOptions(foOptionsPtr ops)
91 {
92 ops->indent = 1;
93 ops->indent_tab = 0;
94 ops->indent_spaces = 2;
95 ops->omit_decl = 0;
96 ops->recovery = 0;
97 ops->dropdtd = 0;
98 ops->options = XML_PARSE_NONET;
99 #ifdef LIBXML_HTML_ENABLED
100 ops->html = 0;
101 #endif
102 ops->quiet = globalOptions.quiet;
103 }
104
105 /**
106 * Initialize LibXML
107 */
108 void
109 foInitLibXml(foOptionsPtr ops)
110 {
111 /*
112 * Initialize library memory
113 */
114 xmlInitMemory();
115
116 LIBXML_TEST_VERSION
117
118 /*
119 * Store line numbers in the document tree
120 */
121 xmlLineNumbersDefault(1);
122
123 xmlSubstituteEntitiesDefault(1);
124 xmlKeepBlanksDefault(0);
125 xmlPedanticParserDefault(0);
126
127 xmlGetWarningsDefaultValue = 1;
128 xmlDoValidityCheckingDefaultValue = 0;
129 xmlLoadExtDtdDefaultValue = 0;
130
131 xmlTreeIndentString = NULL;
132 if (ops->indent)
133 {
134 xmlIndentTreeOutput = 1;
135 if (ops->indent_tab)
136 {
137 xmlTreeIndentString = "\t";
138 }
139 else if (ops->indent_spaces > 0)
140 {
141 spaces = xmlMalloc(ops->indent_spaces + 1);
142 xmlTreeIndentString = spaces;
143 memset(spaces, ' ', ops->indent_spaces);
144 spaces[ops->indent_spaces] = '\0';
145 }
146 }
147 else
148 xmlIndentTreeOutput = 0;
149 }
150
151 /**
152 * Parse global command line options
153 */
154 int
155 foParseOptions(foOptionsPtr ops, int argc, char **argv)
156 {
157 int i;
158
159 i = 2;
160 while(i < argc)
161 {
162 if (!strcmp(argv[i], "--noindent") || !strcmp(argv[i], "-n"))
163 {
164 ops->indent = 0;
165 i++;
166 }
167 else if (!strcmp(argv[i], "--encode") || !strcmp(argv[i], "-e"))
168 {
169 i++;
170 encoding = argv[i];
171 i++;
172 }
173 else if (!strcmp(argv[i], "--indent-tab") || !strcmp(argv[i], "-t"))
174 {
175 ops->indent_tab = 1;
176 i++;
177 }
178 else if (!strcmp(argv[i], "--omit-decl") || !strcmp(argv[i], "-o"))
179 {
180 ops->omit_decl = 1;
181 i++;
182 }
183 else if (!strcmp(argv[i], "--dropdtd") || !strcmp(argv[i], "-D"))
184 {
185 ops->dropdtd = 1;
186 i++;
187 }
188 else if (!strcmp(argv[i], "--recover") || !strcmp(argv[i], "-R"))
189 {
190 ops->recovery = 1;
191 ops->options |= XML_PARSE_RECOVER;
192 i++;
193 }
194 else if (!strcmp(argv[i], "--nocdata") || !strcmp(argv[i], "-C"))
195 {
196 ops->options |= XML_PARSE_NOCDATA;
197 i++;
198 }
199 else if (!strcmp(argv[i], "--nsclean") || !strcmp(argv[i], "-N"))
200 {
201 ops->options |= XML_PARSE_NSCLEAN;
202 i++;
203 }
204 else if (!strcmp(argv[i], "--indent-spaces") || !strcmp(argv[i], "-s"))
205 {
206 int value;
207 i++;
208 if (i >= argc) foUsage(argc, argv, EXIT_BAD_ARGS);
209 if (sscanf(argv[i], "%d", &value) == 1)
210 {
211 if (value > 0) ops->indent_spaces = value;
212 }
213 else
214 {
215 foUsage(argc, argv, EXIT_BAD_ARGS);
216 }
217 ops->indent_tab = 0;
218 i++;
219 }
220 else if (!strcmp(argv[i], "--quiet") || !strcmp(argv[i], "-Q"))
221 {
222 ops->quiet = 1;
223 i++;
224 }
225 #ifdef LIBXML_HTML_ENABLED
226 else if (!strcmp(argv[i], "--html") || !strcmp(argv[i], "-H"))
227 {
228 ops->html = 1;
229 i++;
230 }
231 #endif
232 else if (!strcmp(argv[i], "--net"))
233 {
234 ops->options &= ~XML_PARSE_NONET;
235 i++;
236 }
237 else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
238 {
239 foUsage(argc, argv, EXIT_SUCCESS);
240 }
241 else if (!strcmp(argv[i], "-"))
242 {
243 i++;
244 break;
245 }
246 else if (argv[i][0] == '-')
247 {
248 foUsage(argc, argv, EXIT_BAD_ARGS);
249 }
250 else
251 {
252 i++;
253 break;
254 }
255 }
256
257 return i-1;
258 }
259
260 /**
261 * 'process' xml document(s)
262 */
263 int
264 foProcess(foOptionsPtr ops, int start, int argc, char **argv)
265 {
266 int ret = 0;
267 xmlDocPtr doc = NULL;
268 char *fileName = "-";
269
270 if ((start > 1) && (start < argc) && (argv[start][0] != '-') &&
271 strcmp(argv[start-1], "--indent-spaces") &&
272 strcmp(argv[start-1], "-s"))
273 {
274 fileName = argv[start];
275 }
276 /*
277 if (ops->recovery)
278 {
279 doc = xmlRecoverFile(fileName);
280 }
281 else
282 */
283 if (ops->quiet)
284 suppressErrors();
285
286 #ifdef LIBXML_HTML_ENABLED
287 if (ops->html)
288 {
289 doc = htmlReadFile(fileName, NULL, ops->options);
290 }
291 else
292 #endif
293 doc = xmlReadFile(fileName, NULL, ops->options);
294
295 if (doc == NULL)
296 {
297 /*fprintf(stderr, "%s:: error: XML parse error\n", fileName);*/
298 return 2;
299 }
300
301 /*
302 * Remove DOCTYPE nodes
303 */
304 if (ops->dropdtd) {
305 xmlDtdPtr dtd;
306
307 dtd = xmlGetIntSubset(doc);
308 if (dtd != NULL) {
309 xmlUnlinkNode((xmlNodePtr)dtd);
310 xmlFreeDtd(dtd);
311 }
312 }
313
314 if (!ops->omit_decl)
315 {
316 if (encoding != NULL)
317 {
318 xmlSaveFormatFileEnc("-", doc, encoding, 1);
319 }
320 else
321 {
322 xmlSaveFormatFile("-", doc, 1);
323 }
324 }
325 else
326 {
327 int format = 1;
328 xmlOutputBufferPtr buf = NULL;
329 xmlCharEncodingHandlerPtr handler = NULL;
330 buf = xmlOutputBufferCreateFile(stdout, handler);
331
332 if (doc->children != NULL)
333 {
334 xmlNodePtr child = doc->children;
335 while (child != NULL)
336 {
337 xmlNodeDumpOutput(buf, doc, child, 0, format, encoding);
338 xmlOutputBufferWriteString(buf, "\n");
339 child = child->next;
340 }
341 }
342 ret = xmlOutputBufferClose(buf);
343 }
344
345 xmlFreeDoc(doc);
346 return ret;
347 }
348
349 /**
350 * Cleanup memory
351 */
352 void
353 foCleanup()
354 {
355 free(spaces);
356 spaces = NULL;
357 xmlCleanupParser();
358 #if 0
359 xmlMemoryDump();
360 #endif
361 }
362
363 /**
364 * This is the main function for 'format' option
365 */
366 int
367 foMain(int argc, char **argv)
368 {
369 int ret = 0;
370 int start;
371 static foOptions ops;
372
373 if (argc <=1) foUsage(argc, argv, EXIT_BAD_ARGS);
374 foInitOptions(&ops);
375 start = foParseOptions(&ops, argc, argv);
376 if (argc-start > 1) foUsage(argc, argv, EXIT_BAD_ARGS);
377 foInitLibXml(&ops);
378 ret = foProcess(&ops, start, argc, argv);
379 foCleanup();
380
381 return ret;
382 }