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: lineout.c /main/3 1996/06/19 17:15:55 drk $ */
25 Implements line-oriented output format.
27 Written by James Clark (jjc@jclark.com).
32 #include "entity.h" /* Templates for entity control blocks. */
33 #include "adl.h" /* Definitions for attribute list processing. */
34 #include "sgmlmain.h" /* Main interface to SGML services. */
38 static VOID flush_data P((void));
39 static VOID define_external_entity P((PNE));
40 static VOID define_entity P((UNCH *));
41 static VOID handle_attributes P((UNCH *, struct ad *));
42 static VOID handle_token_list P((UNCH *, struct ad *, int));
43 static VOID handle_single_token P((UNCH *, struct ad *, int));
44 static VOID output_notation P((UNCH *, UNCH *, UNCH *));
45 static VOID output_internal_entity P((UNCH *, int, UNCH *));
46 static VOID output_external_entity P((UNCH *, int, UNIV, UNCH *, UNCH *,
48 static VOID output_subdoc P((UNCH *, UNIV, UNCH *, UNCH *));
50 static VOID process_subdoc P((UNCH *, UNIV));
51 #endif /* SUPPORT_SUBDOC */
52 static VOID output_record_end P((void));
53 static VOID output_pcdata P((UNS, UNCH *));
54 static VOID output_cdata P((UNS, UNCH *));
55 static VOID output_sdata P((UNS, UNCH *));
56 static VOID output_entity_reference P((UNCH *));
57 static VOID output_start_tag P((UNCH *));
58 static VOID output_end_tag P((UNCH *));
59 static VOID output_processing_instruction P((UNS, UNCH *));
60 static VOID output_implied_attribute P((UNCH *, UNCH *));
61 static char *attribute_type_string P((int));
62 static VOID output_begin_attribute P((UNCH *, UNCH *, int));
63 static VOID output_attribute_token P((UNS, UNCH *));
64 static VOID output_end_attribute P((void));
65 static VOID print_data P((UNS, UNCH *, int));
66 static VOID print_string P((UNS, UNCH *, int));
67 static VOID print_id P((UNIV, UNCH *, UNCH *));
68 static VOID print_filename P((char *));
69 static VOID output_location P((void));
70 static VOID output_appinfo P((UNS, UNCH *));
72 static int have_data = 0;
73 static char *current_filename = 0;
74 static unsigned long current_lineno = 0;
76 VOID process_document(subdocsw)
81 struct rcbdata rcbdaf;
83 while ((rc = sgmlnext(&rcbdaf, &rcbtag)) != SGMLEOD) {
85 if (rc == SGMLDAF && !CONTERSW(rcbdaf) && NDESW(rcbdaf)
86 && NEXTYPE(NEPTR(rcbdaf)) == ESNSUB) {
87 if (!suppsw && !sgmlment(NEENAME(NEPTR(rcbdaf))))
88 define_external_entity(NEPTR(rcbdaf));
89 process_subdoc(NEENAME(NEPTR(rcbdaf)) + 1,
93 #endif /* SUPPORT_SUBDOC */
100 output_cdata(CDATALEN(rcbdaf), CDATA(rcbdaf));
101 else if (SDESW(rcbdaf))
102 output_sdata(CDATALEN(rcbdaf), CDATA(rcbdaf));
103 else if (NDESW(rcbdaf)) {
104 assert(NEXTYPE(NEPTR(rcbdaf)) != ESNSUB);
105 if (!sgmlment(NEENAME(NEPTR(rcbdaf))))
106 define_external_entity(NEPTR(rcbdaf));
107 output_entity_reference(NEENAME(NEPTR(rcbdaf)) + 1);
110 output_pcdata(CDATALEN(rcbdaf), CDATA(rcbdaf));
113 if (CONTERSW(rcbtag))
116 handle_attributes((UNCH *)NULL, ALPTR(rcbtag));
117 output_start_tag(CURGI(rcbtag));
120 if (CONTERSW(rcbtag))
122 output_end_tag(CURGI(rcbtag));
125 if (CONTERSW(rcbdaf))
127 output_processing_instruction(PDATALEN(rcbdaf),
131 if (CONTERSW(rcbdaf))
136 if (CONTERSW(rcbdaf))
139 output_appinfo(ADATALEN(rcbdaf), ADATA(rcbdaf));
147 /* Output an indication that the document was conforming. */
149 VOID output_conforming()
152 printf("%c\n", CONFORMING_CODE);
155 static VOID define_external_entity(p)
158 if (NEXTYPE(p) == ESNSUB)
159 output_subdoc(NEENAME(p) + 1, NEID(p), NEPUBID(p), NESYSID(p));
162 output_notation(NEDCN(p) + 1, NEDCNPUBID(p), NEDCNSYSID(p));
163 output_external_entity(NEENAME(p) + 1, NEXTYPE(p), NEID(p),
164 NEPUBID(p), NESYSID(p), NEDCN(p) + 1);
166 handle_attributes(NEENAME(p) + 1, NEAL(p));
170 static VOID define_entity(ename)
177 if (sgmlment(ename)) /* already defined it */
179 rc = sgmlgent(ename, &np, &tp);
182 define_external_entity(np);
186 output_internal_entity(ename + 1, rc == 3, tp);
191 /* ENT is the name of the entity with which these attributes are associated;
192 if it's NULL, they're associated with the next start tag. */
194 static VOID handle_attributes(ent, al)
200 for (aln = 1; aln <= ADN(al); aln++) {
201 if (GET(ADFLAGS(al, aln), AERROR))
203 else if (GET(ADFLAGS(al, aln), AINVALID))
205 else if (ADVAL(al, aln) == NULL)
206 output_implied_attribute(ent, ADNAME(al, aln));
207 else if (ADTYPE(al, aln) >= ATKNLIST)
208 handle_token_list(ent, al, aln);
210 handle_single_token(ent, al, aln);
211 if (BITON(ADFLAGS(al, aln), AGROUP))
212 aln += ADNUM(al, aln);
216 static VOID handle_token_list(ent, al, aln)
223 if (ADTYPE(al, aln) == AENTITYS) {
224 ptr = ADVAL(al, aln);
225 for (i = 0; i < ADNUM(al, aln); i++) {
226 /* Temporarily make token look like normal
227 name with length and EOS. */
228 UNCH c = ptr[*ptr + 1];
229 ptr[*ptr + 1] = '\0';
237 output_begin_attribute(ent, ADNAME(al, aln), ADTYPE(al, aln));
238 ptr = ADVAL(al, aln);
239 for (i = 0; i < ADNUM(al, aln); i++) {
240 /* The first byte is a length NOT including the length
241 byte; the tokens are not EOS terminated. */
242 output_attribute_token(*ptr, ptr + 1);
245 output_end_attribute();
248 static VOID handle_single_token(ent, al, aln)
253 if (ADTYPE(al, aln) == ANOTEGRP && !DCNMARK(ADDATA(al, aln).x))
254 output_notation(ADVAL(al, aln) + 1,
255 ADDATA(al, aln).x->pubid,
256 ADDATA(al, aln).x->sysid);
257 else if (ADTYPE(al, aln) == AENTITY)
258 define_entity(ADVAL(al, aln));
259 output_begin_attribute(ent, ADNAME(al, aln), ADTYPE(al, aln));
260 if (ADTYPE(al, aln) == ACHARS)
261 output_attribute_token(ustrlen(ADVAL(al, aln)), ADVAL(al, aln));
263 output_attribute_token(*ADVAL(al, aln) - 2, ADVAL(al, aln) + 1);
264 output_end_attribute();
267 static VOID output_notation(name, pubid, sysid)
272 print_id((UNIV)0, pubid, sysid);
273 printf("%c%s\n", DEFINE_NOTATION_CODE, name);
276 static VOID output_internal_entity(ename, is_sdata, text)
282 printf("%c%s %s ", DEFINE_INTERNAL_ENTITY_CODE, ename,
283 is_sdata ? "SDATA" : "CDATA");
284 print_string(text ? ustrlen(text) : 0, text, 0);
288 static VOID output_subdoc(nm, id, pubid, sysid)
294 print_id(id, pubid, sysid);
295 printf("%c%s\n", DEFINE_SUBDOC_ENTITY_CODE, nm);
298 #ifdef SUPPORT_SUBDOC
300 static VOID process_subdoc(nm, id)
307 printf("%c%s\n", START_SUBDOC_CODE, nm);
316 argv = make_argv(id);
317 ret = run_process(argv);
321 current_filename = 0;
328 appl_error(E_SUBDOC, nm);
332 printf("%c%s\n", END_SUBDOC_CODE, nm);
335 #endif /* SUPPORT_SUBDOC */
337 static VOID output_external_entity(nm, xtype, id, pubid, sysid, dcn)
347 print_id(id, pubid, sysid);
362 printf("%c%s %s %s\n", DEFINE_EXTERNAL_ENTITY_CODE, nm, type, dcn);
365 static VOID output_record_end()
367 static UNCH re = RECHAR;
368 print_data(1, &re, 0);
371 static VOID output_pcdata(n, s)
378 static VOID output_cdata(n, s)
385 static VOID output_sdata(n, s)
392 static VOID output_entity_reference(s)
397 printf("%c%s\n", REFERENCE_ENTITY_CODE, s);
400 static VOID output_start_tag(s)
405 printf("%c%s\n", START_CODE, s);
408 static VOID output_end_tag(s)
412 printf("%c%s\n", END_CODE, s);
415 static VOID output_processing_instruction(n, s)
422 print_string(n, s, 0);
426 static VOID output_appinfo(n, s)
432 putchar(APPINFO_CODE);
433 print_string(n, s, 0);
438 static VOID output_implied_attribute(ent, aname)
443 printf("%c%s %s IMPLIED\n", DATA_ATTRIBUTE_CODE, ent, aname);
445 printf("%c%s IMPLIED\n", ATTRIBUTE_CODE, aname);
448 static char *attribute_type_string(type)
474 fatal("invalid attribute type %d", type);
479 static VOID output_begin_attribute(ent, aname, type)
485 printf("%c%s %s %s", DATA_ATTRIBUTE_CODE, ent, aname,
486 attribute_type_string(type));
488 printf("%c%s %s", ATTRIBUTE_CODE, aname,
489 attribute_type_string(type));
493 static VOID output_attribute_token(vallen, val)
498 print_string(vallen, val, 0);
501 static VOID output_end_attribute()
506 static VOID print_data(n, s, is_sdata)
511 if (n > 0 || is_sdata) {
512 if (n == 1 && *s == RECHAR)
518 print_string(n, s, is_sdata);
523 static VOID flush_data()
531 static VOID output_location()
534 unsigned long lineno;
535 int filename_changed = 0;
539 if (!sgmlloc(&lineno, &filename))
541 if (!current_filename || strcmp(filename, current_filename) != 0)
542 filename_changed = 1;
543 else if (lineno == current_lineno)
546 printf("%c%lu", LOCATION_CODE, lineno);
547 current_lineno = lineno;
548 if (filename_changed) {
550 print_filename(filename);
551 current_filename = filename;
556 static VOID print_string(slen, s, is_sdata)
562 fputs("\\|", stdout);
566 if (ch == DELSDATA) {
568 ; /* I don't think this should happen */
570 fputs("\\|", stdout);
573 else if (ch == DELCDATA)
576 if (ch == DELNONCH) {
585 fputs("\\n", stdout);
588 fputs("\\\\", stdout);
591 if (ISASCII(ch) && isprint(ch))
594 printf("\\%03o", ch);
600 fputs("\\|", stdout);
604 static VOID print_id(id, pubid, sysid)
612 print_string(ustrlen(pubid), pubid, 0);
618 print_string(ustrlen(sysid), sysid, 0);
625 for (p = id; *p != '\0'; p++) {
630 fputs("\\\\", stdout);
633 fputs("\\n", stdout);
636 if (ISASCII(*p) && isprint((UNCH)*p))
639 printf("\\%03o", (UNCH)*p);
648 static VOID print_filename(s)
654 fputs("\\\\", stdout);
657 fputs("\\n", stdout);
660 if (ISASCII(*s) && isprint((UNCH)*s))
663 printf("\\%03o", (UNCH)*s);
671 c-continued-statement-offset: 5