forked from SWI-Prolog/packages-sgml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsgml.c
455 lines (378 loc) · 9.53 KB
/
sgml.c
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/* $Id$
Part of SWI-Prolog
Author: Jan Wielemaker
E-mail: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 1985-2006, University of Amsterdam
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _ISOC99_SOURCE 1 /* fwprintf(), etc prototypes */
#include <stdio.h>
#include "dtd.h"
#include "util.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <wctype.h>
#include <wchar.h>
#include <locale.h>
#define streq(s1, s2) (strcmp(s1, s2) == 0)
char *program;
int nerrors = 0;
int nwarnings = 0;
int style_messages = FALSE;
static void
usage(void)
{ fprintf(stderr,
"Usage: %s [-xml] [-s] [-nodefs] [file.dtd] [file]\n\n", program);
fprintf(stderr,
"\t-xml\tForce XML mode\n"
"\t-s\tSilent: only report errors and warnings\n"
"\t-style\tWarn about correct but dubious input\n"
"\t-nodefs\tDo not include defaulted attributes\n");
exit(EXIT_FAILURE);
}
static int
wputc(int c, FILE *f)
{ char buf[MB_CUR_MAX];
int i, len = wctomb(buf, c);
for(i=0; i<len; i++)
putc(buf[i], f);
return c;
}
static void
print_word(dtd_parser * p, char c, /* preceding character */
ichar const *s, /* where to start */
ichar const *e) /* where to end (at NUL if e is NULL) */
{ FILE *f = stdout;
ichar x;
wputc(c, f);
if (p->dtd->case_sensitive)
{ if (e != 0)
while (s != e)
wputc(*s++, f);
else
while ((x = *s++) != (ichar) 0)
wputc(x, f);
} else
{ if (e != 0)
while (s != e)
wputc(towupper((wint_t)*s++), f);
else
while ((x = *s++) != (ichar) 0)
wputc(towupper(x), f);
}
}
static void
wprint_escaped(FILE *f, const wchar_t *s, int len)
{ const wchar_t *e = &s[len];
while ( s < e )
{ wint_t x = *s++;
if (x >= ' ')
{ if (x == '\\') /* \ --> \\ */
wputc(x, f);
wputc(x, f);
} else if (x == '\t')
{ wputc(x, f); /* \t */
} else if (x == '\n')
{ fprintf(f, "\\n"); /* \n */
} else
{ fprintf(f, "\\%03o", (unsigned int)x);
}
}
}
static void
print_cdata(char c, sgml_attribute *a)
{ wputc(c, stdout);
wprint_escaped(stdout, a->value.textW, a->value.number);
wputc('\n', stdout);
}
static int
print_close(dtd_parser * p, dtd_element * e)
{ print_word(p, ')', e->name->name, 0);
putchar('\n');
return TRUE;
}
typedef struct atdef
{ attrtype type; /* AT_* */
char const *name; /* name */
int islist; /* list-type */
} atdef;
static atdef attrs[] = {
{AT_CDATA, "CDATA", FALSE},
{AT_ENTITY, "ENTITY", FALSE},
{AT_ENTITIES, "ENTITY", TRUE},
{AT_ID, "ID", FALSE},
{AT_IDREF, "IDREF", FALSE},
{AT_IDREFS, "IDREF", TRUE},
{AT_NAME, "NAME", FALSE},
{AT_NAMES, "NAME", TRUE},
{AT_NMTOKEN, "NMTOKEN", FALSE},
{AT_NMTOKENS, "NMTOKEN", TRUE},
{AT_NUMBER, "NUMBER", FALSE},
{AT_NUMBERS, "NUMBER", TRUE},
{AT_NUTOKEN, "NUTOKEN", FALSE},
{AT_NUTOKENS, "NUTOKEN", TRUE},
{AT_NOTATION, "NOTATION", FALSE},
{AT_CDATA, (char *) 0, FALSE}
};
static atdef *
find_attrdef(attrtype type)
{ atdef *ad;
for (ad = attrs; ad->name != (char *) 0; ad++)
{ if (ad->type == type)
return ad;
}
assert(0);
return (atdef *) 0;
}
static ichar *
istrblank(ichar const *s)
{ for (; *s; s++)
{ if (iswspace(*s))
return (ichar *) s;
}
return (ichar *) 0;
}
static int
print_open(dtd_parser * p, dtd_element * e, int argc, sgml_attribute *argv)
{ int i;
for (i = 0; i < argc; i++)
{ print_word(p, 'A', argv[i].definition->name->name, 0);
switch (argv[i].definition->type)
{ case AT_CDATA:
printf(" CDATA");
print_cdata(' ', &argv[i]);
continue; /* so we don't get two line breaks */
case AT_NUMBER:
printf(" NUMBER ");
if (argv[i].value.textW)
print_word(p, ' ', argv[i].value.textW, 0);
else
printf("%ld", argv[i].value.number);
break;
case AT_NAMEOF:
printf(" NAME");
print_word(p, ' ', argv[i].value.textW, 0);
break;
default:
{ atdef *ad = find_attrdef(argv[i].definition->type);
ichar const *val = argv[i].value.textW;
printf(" %s", ad->name);
if (ad->islist)
{ ichar const *n;
while ((n = istrblank(val)) != 0)
{ if (n != val)
print_word(p, ' ', val, n);
val = n + 1;
}
}
print_word(p, ' ', val, 0);
}
break;
}
putchar('\n');
}
print_word(p, '(', e->name->name, 0);
putchar('\n');
return TRUE;
}
static int
print_data(dtd_parser * p, data_type type, int len, const wchar_t *data)
{ char c;
switch (type)
{ case EC_CDATA:
c = '-';
break;
case EC_NDATA:
c = 'N';
break;
case EC_SDATA:
c = 'S';
break;
default:
assert(0);
}
wputc(c, stdout);
wprint_escaped(stdout, data, len);
wputc('\n', stdout);
return TRUE;
}
static int
on_entity(dtd_parser *p, dtd_entity *e, int chr)
{ if (e == 0)
printf("&#%d;\n", chr);
else
wprintf(L"&%s;\n", e->name->name);
return TRUE;
}
static int
on_pi(dtd_parser *p, ichar const *pi)
{ wputc('?', stdout);
wprint_escaped(stdout, pi, wcslen(pi));
return TRUE;
}
static dtd_srcloc *
file_location(dtd_srcloc *l)
{ while (l->parent && l->type != IN_FILE)
l = l->parent;
return l;
}
static int
on_error(dtd_parser * p, dtd_error * error)
{ char const *severity;
char const *dialect;
dtd_srcloc *l = file_location(error->location);
switch (p->dtd->dialect)
{ case DL_SGML:
dialect = "sgml";
break;
case DL_XML:
dialect = "xml";
break;
case DL_XMLNS:
default: /* make compiler happy */
dialect = "xmlns";
break;
}
switch (error->severity)
{ case ERS_STYLE:
severity = "Style";
if ( !style_messages )
return TRUE;
break;
case ERS_WARNING:
severity = "Warning";
nwarnings++;
break;
case ERS_ERROR:
default: /* make compiler happy */
severity = "Error";
nerrors++;
break;
}
if ( l->name.file )
{ fwprintf(stderr, L"%s: (%s mode) %s: %ls:%d:%d %ls\n",
program, dialect, severity,
l->name.entity, l->line, l->linepos,
error->plain_message);
} else
{ fwprintf(stderr, L"%s: (%s mode) %s: %d:%d %ls\n",
program, dialect, severity,
error->plain_message);
}
return TRUE;
}
static void
set_functions(dtd_parser * p, int output)
{ if (output)
{ p->on_end_element = print_close;
p->on_begin_element = print_open;
p->on_data = print_data;
p->on_entity = on_entity;
p->on_pi = on_pi;
}
p->on_error = on_error;
}
static wchar_t *
mb2wc(const char *s)
{ int wl = mbstowcs(NULL, s, 0);
if ( wl > 0 )
{ wchar_t *ws = malloc((wl+1)*sizeof(wchar_t));
mbstowcs(ws, s, wl+1);
return ws;
}
perror("mbstowcs");
exit(1);
}
#define shift (argc--, argv++)
#define strcaseeq(x, y) istrcaseeq((ichar const *)(x), (ichar const *)(y))
static ichar const *no_dtd = (ichar const *) NULL;
int
main(int argc, char **argv)
{ dtd_parser *p = NULL;
char *s;
int xml = FALSE;
int output = TRUE;
int nodefs = FALSE; /* include defaulted attributes */
setlocale(LC_CTYPE, "");
s = strchr(argv[0], '/');
program = s == NULL ? argv[0] : s + 1;
if (streq(program, "xml"))
xml = TRUE;
shift;
while (argc > 0 && argv[0][0] == '-')
{ if (streq(argv[0], "-xml"))
{ xml = TRUE;
} else if (streq(argv[0], "-s"))
{ output = FALSE;
} else if (streq(argv[0], "-nodefs"))
{ nodefs = TRUE;
} else if (streq(argv[0], "-style"))
{ style_messages = TRUE;
} else
{ usage();
}
shift;
}
if (argc > 0)
{ char *slash = strchr(argv[0], '/');
char *dot = strchr(argv[0], '.');
char *ext = dot == 0 || (slash != 0 && slash > dot) ? "." : dot;
if (strcaseeq(ext, ".dtd"))
{ char doctype[256];
strncpy(doctype, argv[0], ext - argv[0]);
doctype[ext - argv[0]] = '\0';
p = new_dtd_parser(new_dtd(mb2wc(doctype)));
load_dtd_from_file(p, mb2wc(argv[0]));
shift;
} else if (strcaseeq(ext, ".html") || strcaseeq(ext, ".htm"))
{ p = new_dtd_parser(new_dtd((ichar const *) "html"));
load_dtd_from_file(p, L"html.dtd");
} else if (xml || strcaseeq(ext, ".xml"))
{ dtd *dtd = new_dtd(no_dtd);
set_dialect_dtd(dtd, DL_XML);
p = new_dtd_parser(dtd);
} else
{ p = new_dtd_parser(new_dtd(no_dtd));
}
} else
{ p = new_dtd_parser(new_dtd(no_dtd));
}
if (nodefs)
p->flags |= SGML_PARSER_NODEFS;
switch (argc)
{ case 1:
{ set_functions(p, output);
sgml_process_file(p, mb2wc(argv[0]), 0);
free_dtd_parser(p);
if (output && nerrors == 0)
printf("C\n");
return 0;
}
case 0:
{ set_functions(p, output);
set_file_dtd_parser(p, IN_FILE, L"stdin");
set_mode_dtd_parser(p, DM_DATA);
sgml_process_stream(p, stdin, 0);
free_dtd_parser(p);
if (output && nerrors == 0 && nwarnings == 0)
printf("C\n");
return 0;
}
default:
{ usage();
return EXIT_FAILURE;
}
}
}