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: sgmlsasp.c /main/3 1996/06/19 17:18:11 drk $ */
25 Translate sgmls output using ASP replacement file.
27 Written by James Clark (jjc@jclark.com). */
34 /* Non-zero if general (non-entity) names should be folded to upper case. */
35 int fold_general_names = 1;
37 static char *program_name;
38 static char last_char = '\n';
40 static void output_begin_line P((void));
41 static void output_data P((struct sgmls_data *, int));
42 static void output_pi P((char *, unsigned));
43 static void output_token P((char *));
44 static void output_attribute P((struct sgmls_attribute *));
45 static void output_data_char P((int));
46 static void output_replacement
47 P((struct replacement *, struct sgmls_attribute *));
48 static void do_file P((FILE *, struct replacement_table *));
49 static void usage P((void));
50 static void input_error P((int, char *, unsigned long));
52 #define output_char(c) (last_char = (c), putchar(c))
58 struct replacement_table *tablep;
61 program_name = argv[0];
63 while ((opt = getopt(argc, argv, "n")) != EOF)
66 fold_general_names = 0;
73 if (argc - optind <= 0)
75 tablep = make_replacement_table();
76 for (i = optind; i < argc; i++)
77 load_replacement_file(tablep, argv[i]);
78 (void)sgmls_set_errhandler(input_error);
79 do_file(stdin, tablep);
86 fprintf(stderr, "usage: %s [-n] replacement_file...\n", program_name);
91 void input_error(num, str, lineno)
96 error("Error at input line %lu: %s", lineno, str);
100 void do_file(fp, tablep)
102 struct replacement_table *tablep;
105 struct sgmls_event e;
107 sp = sgmls_create(fp);
108 while (sgmls_next(sp, &e))
110 case SGMLS_EVENT_DATA:
111 output_data(e.u.data.v, e.u.data.n);
113 case SGMLS_EVENT_ENTITY:
114 /* XXX what should we do here? */
117 output_pi(e.u.pi.s, e.u.pi.len);
119 case SGMLS_EVENT_START:
120 output_replacement(lookup_replacement(tablep,
121 START_ELEMENT, e.u.start.gi),
122 e.u.start.attributes);
123 sgmls_free_attributes(e.u.start.attributes);
125 case SGMLS_EVENT_END:
126 output_replacement(lookup_replacement(tablep, END_ELEMENT, e.u.end.gi),
129 case SGMLS_EVENT_SUBSTART:
131 case SGMLS_EVENT_SUBEND:
133 case SGMLS_EVENT_APPINFO:
135 case SGMLS_EVENT_CONFORMING:
144 void output_data(v, n)
145 struct sgmls_data *v;
150 for (i = 0; i < n; i++) {
153 for (; len > 0; len--, s++)
154 output_data_char(*s);
159 void output_pi(s, len)
163 for (; len > 0; len--, s++)
164 output_data_char(*s);
168 void output_replacement(repl, attributes)
169 struct replacement *repl;
170 struct sgmls_attribute *attributes;
172 struct replacement_item *p;
173 struct sgmls_attribute *a;
178 if (repl->flags & NEWLINE_BEGIN)
181 for (p = repl->items; p; p = p->next)
184 for (i = 0; i < p->u.data.n; i++)
185 output_char(p->u.data.s[i]);
188 for (a = attributes; a; a = a->next)
189 if (strcmp(a->name, p->u.attr) == 0) {
198 if (repl->flags & NEWLINE_END)
203 void output_attribute(p)
204 struct sgmls_attribute *p;
207 case SGMLS_ATTR_IMPLIED:
209 case SGMLS_ATTR_CDATA:
210 output_data(p->value.data.v, p->value.data.n);
212 case SGMLS_ATTR_TOKEN:
214 char **token = p->value.token.v;
215 int n = p->value.token.n;
219 output_token(token[0]);
220 for (i = 1; i < n; i++) {
222 output_token(token[i]);
227 case SGMLS_ATTR_ENTITY:
229 struct sgmls_entity **v = p->value.entity.v;
230 int n = p->value.entity.n;
233 for (i = 0; i < n; i++) {
236 output_token(v[i]->is_internal
237 ? v[i]->u.internal.name
238 : v[i]->u.external.name);
242 case SGMLS_ATTR_NOTATION:
243 if (p->value.notation)
244 output_token(p->value.notation->name);
260 void output_data_char(c)
271 void output_begin_line()
273 if (last_char != '\n')
279 void error(va_alist) va_dcl
281 void error(char *message,...)
289 fprintf(stderr, "%s: ", program_name);
292 message = va_arg(ap, char *);
294 va_start(ap, message);
296 vfprintf(stderr, message, ap);