2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these librararies and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $XConsortium: rast.c /main/3 1996/06/19 17:16:53 drk $ */
25 Translate sgmls output to RAST result format.
27 Written by James Clark (jjc@jclark.com). */
35 #define P(parms) parms
41 #define NO_RETURN volatile
43 #define NO_RETURN /* as nothing */
49 #define VP(parms) P(parms)
53 #define ISASCII(c) isascii(c)
55 #define ISASCII(c) (1)
58 NO_RETURN void error VP((char *,...));
60 static void input_error P((int, char *, unsigned long));
61 static int do_file P((FILE *));
62 static void usage P((void));
64 static void output_processing_instruction P((char *, unsigned));
65 static void output_data P((struct sgmls_data *, int));
66 static void output_data_lines P((char *, unsigned));
67 static void output_internal_sdata P((char *, unsigned));
68 static void output_external_entity P((struct sgmls_external_entity *));
69 static void output_external_entity_info P((struct sgmls_external_entity *));
70 static void output_element_start P((char *, struct sgmls_attribute *));
71 static void output_element_end P((char *));
72 static void output_attribute P((struct sgmls_attribute *));
73 static void output_tokens P((char **, int));
74 static void output_markup_chars P((char *, unsigned));
75 static void output_markup_string P((char *));
76 static void output_char P((int, int));
77 static void output_flush P((int));
78 static void output_external_id P((char *, char *));
79 static void output_entity P((struct sgmls_entity *));
80 static void output_external_entity_info P((struct sgmls_external_entity *));
81 static void output_internal_entity P((struct sgmls_internal_entity *));
83 #define output_flush_markup() output_flush('!')
84 #define output_flush_data() output_flush('|')
87 static int char_count = 0;
88 static char *program_name;
96 char *output_file = 0;
98 program_name = argv[0];
100 while ((opt = getopt(argc, argv, "o:")) != EOF)
103 output_file = optarg;
113 outfp = fopen(output_file, "w");
115 error("couldn't open `%s' for output: %s", strerror(errno));
120 error("couldn't create temporary file: %s", strerror(errno));
123 if (argc - optind > 1)
126 if (argc - optind == 1) {
127 if (!freopen(argv[optind], "r", stdin))
128 error("couldn't open `%s' for input: %s", argv[optind], strerror(errno));
131 (void)sgmls_set_errhandler(input_error);
133 if (!do_file(stdin)) {
136 if (!freopen(output_file, "w", stdout))
137 error("couldn't reopen `%s' for output: %s", strerror(errno));
139 fputs("#ERROR\n", stdout);
145 if (fclose(outfp) == EOF)
146 error("error closing `%s': %s", output_file, strerror(errno));
150 if (fseek(outfp, 0L, SEEK_SET))
151 error("couldn't rewind temporary file: %s", strerror(errno));
152 while ((c = getc(outfp)) != EOF)
153 if (putchar(c) == EOF)
154 error("error writing standard output: %s", strerror(errno));
162 fprintf(stderr, "usage: %s [-o output_file] [input_file]\n", program_name);
171 struct sgmls_event e;
174 sp = sgmls_create(fp);
175 while (sgmls_next(sp, &e))
177 case SGMLS_EVENT_DATA:
178 output_data(e.u.data.v, e.u.data.n);
180 case SGMLS_EVENT_ENTITY:
181 output_external_entity(e.u.entity);
184 output_processing_instruction(e.u.pi.s, e.u.pi.len);
186 case SGMLS_EVENT_START:
187 output_element_start(e.u.start.gi, e.u.start.attributes);
188 sgmls_free_attributes(e.u.start.attributes);
190 case SGMLS_EVENT_END:
191 output_element_end(e.u.end.gi);
193 case SGMLS_EVENT_SUBSTART:
196 output_external_entity(e.u.entity);
198 if (!sgmls_next(sp, &e))
201 case SGMLS_EVENT_SUBSTART:
204 case SGMLS_EVENT_SUBEND:
207 case SGMLS_EVENT_START:
208 sgmls_free_attributes(e.u.start.attributes);
211 /* prevent compiler warnings */
217 case SGMLS_EVENT_APPINFO:
219 case SGMLS_EVENT_CONFORMING:
230 void output_processing_instruction(s, len)
237 output_data_lines(s, len);
244 void output_data(v, n)
245 struct sgmls_data *v;
249 for (i = 0; i < n; i++) {
251 output_internal_sdata(v[i].s, v[i].len);
252 else if (v[i].len > 0)
253 output_data_lines(v[i].s, v[i].len);
258 void output_data_lines(s, n)
264 output_char((unsigned char)*s++, '|');
269 void output_internal_sdata(s, n)
273 fputs("#SDATA-TEXT\n", outfp);
274 output_markup_chars(s, n);
275 output_flush_markup();
276 fputs("#END-SDATA\n", outfp);
280 void output_external_entity(e)
281 struct sgmls_external_entity *e;
283 fprintf(outfp, "[&%s\n", e->name);
284 output_external_entity_info(e);
289 void output_element_start(gi, att)
291 struct sgmls_attribute *att;
293 fprintf(outfp, "[%s", gi);
295 struct sgmls_attribute *p;
297 for (p = att; p; p = p->next)
304 void output_element_end(gi)
307 fprintf(outfp, "[/%s]\n", gi);
311 void output_attribute(p)
312 struct sgmls_attribute *p;
314 fprintf(outfp, "%s=\n", p->name);
316 case SGMLS_ATTR_IMPLIED:
317 fputs("#IMPLIED\n", outfp);
319 case SGMLS_ATTR_CDATA:
321 struct sgmls_data *v = p->value.data.v;
322 int n = p->value.data.n;
324 for (i = 0; i < n; i++)
326 output_internal_sdata(v[i].s, v[i].len);
328 output_markup_chars(v[i].s, v[i].len);
329 output_flush_markup();
333 case SGMLS_ATTR_TOKEN:
334 output_tokens(p->value.token.v, p->value.token.n);
336 case SGMLS_ATTR_ENTITY:
339 for (i = 0; i < p->value.entity.n; i++) {
340 struct sgmls_entity *e = p->value.entity.v[i];
344 name = e->u.internal.name;
346 name = e->u.external.name;
348 output_markup_string(" ");
349 output_markup_string(name);
351 output_flush_markup();
352 for (i = 0; i < p->value.entity.n; i++)
353 output_entity(p->value.entity.v[i]);
356 case SGMLS_ATTR_NOTATION:
357 output_tokens(&p->value.notation->name, 1);
358 output_external_id(p->value.notation->pubid, p->value.notation->sysid);
363 static void output_tokens(v, n)
369 output_markup_string(v[0]);
370 for (i = 1; i < n; i++) {
371 output_markup_string(" ");
372 output_markup_string(v[i]);
374 output_flush_markup();
378 void output_markup_chars(s, n)
383 output_char((unsigned char)*s++, '!');
387 void output_markup_string(s)
391 output_char((unsigned char)*s++, '!');
395 void output_char(c, delim)
399 if (ISASCII(c) && isprint(c)) {
404 if (char_count == 60) {
414 fputs("#RE\n", outfp);
417 fputs("#RS\n", outfp);
420 fputs("#TAB\n", outfp);
423 fprintf(outfp, "#%d\n", c);
429 void output_flush(delim)
432 if (char_count > 0) {
440 void output_external_id(pubid, sysid)
444 if (!pubid && !sysid)
445 fputs("#SYSTEM\n#NONE\n", outfp);
448 fputs("#PUBLIC\n", outfp);
450 output_markup_string(pubid);
451 output_flush_markup();
454 fputs("#EMPTY\n", outfp);
457 fputs("#SYSTEM\n", outfp);
459 output_markup_string(sysid);
460 output_flush_markup();
463 fputs("#EMPTY\n", outfp);
469 void output_entity(e)
470 struct sgmls_entity *e;
473 output_internal_entity(&e->u.internal);
475 output_external_entity_info(&e->u.external);
476 fputs("#END-ENTITY", outfp);
483 void output_external_entity_info(e)
484 struct sgmls_external_entity *e;
487 case SGMLS_ENTITY_CDATA:
488 fputs("#CDATA-EXTERNAL", outfp);
490 case SGMLS_ENTITY_SDATA:
491 fputs("#SDATA-EXTERNAL", outfp);
493 case SGMLS_ENTITY_NDATA:
494 fputs("#NDATA-EXTERNAL", outfp);
496 case SGMLS_ENTITY_SUBDOC:
497 fputs("#SUBDOC", outfp);
501 output_external_id(e->pubid, e->sysid);
502 if (e->type != SGMLS_ENTITY_SUBDOC) {
503 struct sgmls_attribute *p;
504 fprintf(outfp, "#NOTATION=%s\n", e->notation->name);
505 output_external_id(e->notation->pubid, e->notation->sysid);
506 for (p = e->attributes; p; p = p->next)
512 void output_internal_entity(e)
513 struct sgmls_internal_entity *e;
515 if (e->data.is_sdata)
516 fputs("#SDATA-INTERNAL", outfp);
518 fputs("#CDATA-INTERNAL", outfp);
520 output_markup_chars(e->data.s, e->data.len);
521 output_flush_markup();
525 void input_error(num, str, lineno)
528 unsigned long lineno;
530 error("Error at input line %lu: %s", lineno, str);
535 void error(va_alist) va_dcl
537 void error(char *message,...)
545 fprintf(stderr, "%s: ", program_name);
548 message = va_arg(ap, char *);
550 va_start(ap, message);
552 vfprintf(stderr, message, ap);