Debian Codegraph @

src/xml_format.c

raw ◀ ▶
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 #include <libxml/globals.h>
46
47 #include "xmlstar.h"
48
49 /*
50 * TODO: 1. Attribute formatting options (as every attribute on a new line)
51 * 2. exit values on errors
52 */
53
54 typedef struct _foOptions {
55 int indent; /* indent output */
56 int indent_tab; /* indent output with tab */
57 int indent_spaces; /* num spaces for indentation */
58 int omit_decl; /* omit xml declaration */
59 int recovery; /* try to recover what is parsable */
60 int dropdtd; /* remove the DOCTYPE of the input docs */
61 int options; /* global parsing flags */
62 #ifdef LIBXML_HTML_ENABLED
63 int html; /* inputs are in HTML format */
64 #endif
65 int quiet; /* quiet mode */
66 } foOptions;
67
68 typedef foOptions *foOptionsPtr;
69
70 const char *encoding = NULL;
71 static char *spaces = NULL;
72
73 /**
74 * Print small help for command line options
75 */
76 void
77 foUsage(int argc, char **argv, exit_status status)
78 {
79 extern void fprint_format_usage(FILE* o, const char* argv0);
80 extern const char more_info[];
81 FILE *o = (status == EXIT_SUCCESS)? stdout : stderr;
82 fprint_format_usage(o, argv[0]);
83 fprintf(o, "%s", more_info);
84 exit(status);
85 }
86
87 /**
88 * Initialize global command line options
89 */
90 void
91 foInitOptions(foOptionsPtr ops)
92 {
93 ops->indent = 1;
94 ops->indent_tab = 0;
95 ops->indent_spaces = 2;
96 ops->omit_decl = 0;
97 ops->recovery = 0;
98 ops->dropdtd = 0;
99 ops->options = XML_PARSE_NONET;
100 #ifdef LIBXML_HTML_ENABLED
101 ops->html = 0;
102 #endif
103 ops->quiet = globalOptions.quiet;
104 }
105
106 /**
107 * Initialize LibXML
108 */
109 void
110 foInitLibXml(foOptionsPtr ops)
111 {
112 /*
113 * Initialize library memory
114 */
115 xmlInitMemory();
116
117 LIBXML_TEST_VERSION
118
119 /*
120 * Store line numbers in the document tree
121 */
122 xmlLineNumbersDefault(1);
123
124 xmlSubstituteEntitiesDefault(1);
125 xmlKeepBlanksDefault(0);
126 xmlPedanticParserDefault(0);
127
128 xmlGetWarningsDefaultValue = 1;
129 xmlDoValidityCheckingDefaultValue = 0;
130 xmlLoadExtDtdDefaultValue = 0;
131
132 xmlTreeIndentString = NULL;
133 if (ops->indent)
134 {
135 xmlIndentTreeOutput = 1;
136 if (ops->indent_tab)
137 {
138 xmlTreeIndentString = "\t";
139 }
140 else if (ops->indent_spaces > 0)
141 {
142 spaces = xmlMalloc(ops->indent_spaces + 1);
143 xmlTreeIndentString = spaces;
144 memset(spaces, ' ', ops->indent_spaces);
145 spaces[ops->indent_spaces] = '\0';
146 }
147 }
148 else
149 xmlIndentTreeOutput = 0;
150 }
151
152 /**
153 * Parse global command line options
154 */
155 int
156 foParseOptions(foOptionsPtr ops, int argc, char **argv)
157 {
158 int i;
159
160 i = 2;
161 while(i < argc)
162 {
163 if (!strcmp(argv[i], "--noindent") || !strcmp(argv[i], "-n"))
164 {
165 ops->indent = 0;
166 i++;
167 }
168 else if (!strcmp(argv[i], "--encode") || !strcmp(argv[i], "-e"))
169 {
170 i++;
171 encoding = argv[i];
172 i++;
173 }
174 else if (!strcmp(argv[i], "--indent-tab") || !strcmp(argv[i], "-t"))
175 {
176 ops->indent_tab = 1;
177 i++;
178 }
179 else if (!strcmp(argv[i], "--omit-decl") || !strcmp(argv[i], "-o"))
180 {
181 ops->omit_decl = 1;
182 i++;
183 }
184 else if (!strcmp(argv[i], "--dropdtd") || !strcmp(argv[i], "-D"))
185 {
186 ops->dropdtd = 1;
187 i++;
188 }
189 else if (!strcmp(argv[i], "--recover") || !strcmp(argv[i], "-R"))
190 {
191 ops->recovery = 1;
192 ops->options |= XML_PARSE_RECOVER;
193 i++;
194 }
195 else if (!strcmp(argv[i], "--nocdata") || !strcmp(argv[i], "-C"))
196 {
197 ops->options |= XML_PARSE_NOCDATA;
198 i++;
199 }
200 else if (!strcmp(argv[i], "--nsclean") || !strcmp(argv[i], "-N"))
201 {
202 ops->options |= XML_PARSE_NSCLEAN;
203 i++;
204 }
205 else if (!strcmp(argv[i], "--indent-spaces") || !strcmp(argv[i], "-s"))
206 {
207 int value;
208 i++;
209 if (i >= argc) foUsage(argc, argv, EXIT_BAD_ARGS);
210 if (sscanf(argv[i], "%d", &value) == 1)
211 {
212 if (value > 0) ops->indent_spaces = value;
213 }
214 else
215 {
216 foUsage(argc, argv, EXIT_BAD_ARGS);
217 }
218 ops->indent_tab = 0;
219 i++;
220 }
221 else if (!strcmp(argv[i], "--quiet") || !strcmp(argv[i], "-Q"))
222 {
223 ops->quiet = 1;
224 i++;
225 }
226 #ifdef LIBXML_HTML_ENABLED
227 else if (!strcmp(argv[i], "--html") || !strcmp(argv[i], "-H"))
228 {
229 ops->html = 1;
230 i++;
231 }
232 #endif
233 else if (!strcmp(argv[i], "--net"))
234 {
235 ops->options &= ~XML_PARSE_NONET;
236 i++;
237 }
238 else if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
239 {
240 foUsage(argc, argv, EXIT_SUCCESS);
241 }
242 else if (!strcmp(argv[i], "-"))
243 {
244 i++;
245 break;
246 }
247 else if (argv[i][0] == '-')
248 {
249 foUsage(argc, argv, EXIT_BAD_ARGS);
250 }
251 else
252 {
253 i++;
254 break;
255 }
256 }
257
258 return i-1;
259 }
260
261 /**
262 * 'process' xml document(s)
263 */
264 int
265 foProcess(foOptionsPtr ops, int start, int argc, char **argv)
266 {
267 int ret = 0;
268 xmlDocPtr doc = NULL;
269 char *fileName = "-";
270
271 if ((start > 1) && (start < argc) && (argv[start][0] != '-') &&
272 strcmp(argv[start-1], "--indent-spaces") &&
273 strcmp(argv[start-1], "-s"))
274 {
275 fileName = argv[start];
276 }
277 /*
278 if (ops->recovery)
279 {
280 doc = xmlRecoverFile(fileName);
281 }
282 else
283 */
284 if (ops->quiet)
285 suppressErrors();
286
287 #ifdef LIBXML_HTML_ENABLED
288 if (ops->html)
289 {
290 doc = htmlReadFile(fileName, NULL, ops->options);
291 }
292 else
293 #endif
294 doc = xmlReadFile(fileName, NULL, ops->options);
295
296 if (doc == NULL)
297 {
298 /*fprintf(stderr, "%s:: error: XML parse error\n", fileName);*/
299 return 2;
300 }
301
302 /*
303 * Remove DOCTYPE nodes
304 */
305 if (ops->dropdtd) {
306 xmlDtdPtr dtd;
307
308 dtd = xmlGetIntSubset(doc);
309 if (dtd != NULL) {
310 xmlUnlinkNode((xmlNodePtr)dtd);
311 xmlFreeDtd(dtd);
312 }
313 }
314
315 if (!ops->omit_decl)
316 {
317 if (encoding != NULL)
318 {
319 xmlSaveFormatFileEnc("-", doc, encoding, 1);
320 }
321 else
322 {
323 xmlSaveFormatFile("-", doc, 1);
324 }
325 }
326 else
327 {
328 int format = 1;
329 xmlOutputBufferPtr buf = NULL;
330 xmlCharEncodingHandlerPtr handler = NULL;
331 buf = xmlOutputBufferCreateFile(stdout, handler);
332
333 if (doc->children != NULL)
334 {
335 xmlNodePtr child = doc->children;
336 while (child != NULL)
337 {
338 xmlNodeDumpOutput(buf, doc, child, 0, format, encoding);
339 xmlOutputBufferWriteString(buf, "\n");
340 child = child->next;
341 }
342 }
343 ret = xmlOutputBufferClose(buf);
344 }
345
346 xmlFreeDoc(doc);
347 return ret;
348 }
349
350 /**
351 * Cleanup memory
352 */
353 void
354 foCleanup()
355 {
356 free(spaces);
357 spaces = NULL;
358 xmlCleanupParser();
359 #if 0
360 xmlMemoryDump();
361 #endif
362 }
363
364 /**
365 * This is the main function for 'format' option
366 */
367 int
368 foMain(int argc, char **argv)
369 {
370 int ret = 0;
371 int start;
372 static foOptions ops;
373
374 if (argc <=1) foUsage(argc, argv, EXIT_BAD_ARGS);
375 foInitOptions(&ops);
376 start = foParseOptions(&ops, argc, argv);
377 if (argc-start > 1) foUsage(argc, argv, EXIT_BAD_ARGS);
378 foInitLibXml(&ops);
379 ret = foProcess(&ops, start, argc, argv);
380 foCleanup();
381
382 return ret;
383 }