summaryrefslogtreecommitdiff
path: root/src/xml_trans.c
blob: b5f8355900a32e2bf7438c58c51d6af4b92b2b5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*  $Id: xml_trans.c,v 1.38 2005/01/07 02:40:59 mgrouch Exp $  */

/*

XMLStarlet: Command Line Toolkit to query/edit/check/transform XML documents

Copyright (c) 2002-2004 Mikhail Grushinskiy.  All Rights Reserved.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

#include <config.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#include "xmlstar.h"
#include "trans.h"

/*
 *  TODO:
 *        1. proper command line arguments handling
 *        2. review and clean up all code (free memory)
 *        3. check embedded stylesheet support
 *        4. exit values on errors
 */

/**
 *  Display usage syntax
 */
void
trUsage(const char *argv0, exit_status status)
{
    extern void fprint_trans_usage(FILE* o, const char* argv0);
    extern const char more_info[];
    extern const char libxslt_more_info[];
    FILE *o = (status == EXIT_SUCCESS)? stdout : stderr;
    fprint_trans_usage(o, argv0);
    fprintf(o, "%s", more_info);
    fprintf(o, "%s", libxslt_more_info);
    exit(status);
}

/**
 *  Parse global command line options
 */
int
trParseOptions(xsltOptionsPtr ops, int argc, char **argv)
{
    int i;

    if (argc <= 2) return argc;
    for (i=2; i<argc; i++)
    {
        if (argv[i][0] == '-')
        {
            if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
            {
                trUsage(argv[0], EXIT_SUCCESS);
            }
            else if (!strcmp(argv[i], "--show-ext"))
            {
                ops->show_extensions = 1;
            }
            else if (!strcmp(argv[i], "--val"))
            {
                ops->noval = 0;
            }
            else if (!strcmp(argv[i], "--net"))
            {
                ops->nonet = 0;
            }
            else if (!strcmp(argv[i], "-E") || !strcmp(argv[i], "--embed"))
            {
                ops->embed = 1;
            }
            else if (!strcmp(argv[i], "--omit-decl"))
            {
                ops->omit_decl = 1;
            }
            else if (!strcmp(argv[i], "--maxdepth"))
            {
                int value;
                i++;
                if (i >= argc) trUsage(argv[0], EXIT_BAD_ARGS);
                if (sscanf(argv[i], "%d", &value) == 1)
                    if (value > 0) xsltMaxDepth = value;
            }
#ifdef LIBXML_XINCLUDE_ENABLED
            else if (!strcmp(argv[i], "--xinclude"))
            {
                ops->xinclude = 1;
            }
#endif
#ifdef LIBXML_HTML_ENABLED
            else if (!strcmp(argv[i], "--html"))
            {
                ops->html = 1;
            }
#endif
        }
        else
            break;
    }

    return i;
}

/**
 *  Cleanup memory
 */
void
trCleanup()
{
    xsltCleanupGlobals();
    xmlCleanupParser();
#if 0
    xmlMemoryDump();
#endif
}

/**
 *  Parse command line for XSLT parameters
 */
int
trParseParams(const char** params, int* plen,
              int count, char **argv)
{
    int i;
    *plen = 0;
    params[0] = 0;

    for (i=0; i<count; i++)
    {
        if (argv[i][0] == '-')
        {
            if (!strcmp(argv[i], "-p"))
            {
                int j;
                xmlChar *name, *value;
                
                i++;
                if (i >= count) trUsage(argv[0], EXIT_BAD_ARGS);

                for(j=0; argv[i][j] && (argv[i][j] != '='); j++);
                if (argv[i][j] != '=') trUsage(argv[0], EXIT_BAD_ARGS);
                
                name = xmlStrndup((const xmlChar *) argv[i], j);
                value = xmlStrdup((const xmlChar *) argv[i]+j+1);

                if (*plen >= MAX_PARAMETERS)
                {
                    fprintf(stderr, "too many params increase MAX_PARAMETERS\n");
                    exit(EXIT_INTERNAL_ERROR);
                }

                params[*plen] = (char *)name;
                (*plen)++;
                params[*plen] = (char *)value;
                (*plen)++;                
                params[*plen] = 0;
            }
            else if (!strcmp(argv[i], "-s"))
            {
                int j;
                const xmlChar *string;
                xmlChar *name, *value;

                i++;
                if (i >= count) trUsage(argv[0], EXIT_BAD_ARGS);

                for(j=0; argv[i][j] && (argv[i][j] != '='); j++);
                if (argv[i][j] != '=') trUsage(argv[0], EXIT_BAD_ARGS);

                name = xmlStrndup((const xmlChar *)argv[i], j);
                string = (const xmlChar *)(argv[i]+j+1);

                if (xmlStrchr(string, '"'))
                {
                    if (xmlStrchr(string, '\''))
                    {
                        fprintf(stderr,
                            "string parameter contains both quote and double-quotes\n");
                        exit(EXIT_INTERNAL_ERROR);
                    }
                    value = xmlStrdup((const xmlChar *)"'");
                    value = xmlStrcat(value, string);
                    value = xmlStrcat(value, (const xmlChar *)"'");
                }
                else
                {
                    value = xmlStrdup((const xmlChar *)"\"");
                    value = xmlStrcat(value, string);
                    value = xmlStrcat(value, (const xmlChar *)"\"");
                }

                if (*plen >= MAX_PARAMETERS)
                {
                    fprintf(stderr, "too many params increase MAX_PARAMETERS\n");
                    exit(EXIT_INTERNAL_ERROR);
                }

                params[*plen] = (char *)name;
                (*plen)++;
                params[*plen] = (char *)value;
                (*plen)++;
                params[*plen] = 0;
            }
        }
        else
            break;
    }

    return i;    
}

/**
 *  Cleanup memory allocated by XSLT parameters
 */
void
trCleanupParams(const char **xsltParams)
{
    const char **p = xsltParams;

    while (*p)
    {
        xmlFree((char *)*p);
        p++;
    }
}

/**
 *  This is the main function for 'tr' option
 */
int
trMain(int argc, char **argv)
{
    static xsltOptions ops;
    static const char *xsltParams[2 * MAX_PARAMETERS + 1];

    int errorno = 0;
    int start, xslt_ind;
    int pCount;
    
    if (argc <= 2) trUsage(argv[0], EXIT_BAD_ARGS);

    xsltInitOptions(&ops);
    start = trParseOptions(&ops, argc, argv);
    xslt_ind = start;
    xsltInitLibXml(&ops);

    /* set parameters */
    start += trParseParams(xsltParams, &pCount, argc-start-1, argv+start+1);
    
    /* run transformation */
    errorno = xsltRun(&ops, argv[xslt_ind], xsltParams,
                      argc-start-1, argv+start+1);

    /* free resources */
    trCleanupParams(xsltParams);
    trCleanup();
    
    return errorno;                                                
}