]> git.wh0rd.org - fontconfig.git/blame - doc/edit-sgml.c
Add space between type and formal in devel man pages (bug 8935)
[fontconfig.git] / doc / edit-sgml.c
CommitLineData
5e1f56b5
KP
1/*
2 * $Id$
3 *
46b51147 4 * Copyright © 2003 Keith Packard
5e1f56b5
KP
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and its
7 * documentation for any purpose is hereby granted without fee, provided that
8 * the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of Keith Packard not be used in
11 * advertising or publicity pertaining to distribution of the software without
12 * specific, written prior permission. Keith Packard makes no
13 * representations about the suitability of this software for any purpose. It
14 * is provided "as is" without express or implied warranty.
15 *
16 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22 * PERFORMANCE OF THIS SOFTWARE.
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
12d49d3c 27#include <string.h>
5e1f56b5
KP
28#include <ctype.h>
29
34cd0514
CW
30static void *
31New (int size);
32
33static void *
34Reallocate (void *p, int size);
35
36static void
37Dispose (void *p);
38
5e1f56b5
KP
39typedef enum { False, True } Bool;
40
41typedef struct {
42 char *buf;
43 int size;
44 int len;
45} String;
46
34cd0514
CW
47static String *
48StringNew (void);
49
50static void
51StringAdd (String *s, char c);
52
53static void
54StringAddString (String *s, char *buf);
55
56static String *
57StringMake (char *buf);
58
59static void
60StringDel (String *s);
61
62static void
63StringPut (FILE *f, String *s);
64
65static void
66StringDispose (String *s);
67
68typedef struct {
69 String *tag;
70 String *text;
71} Replace;
72
73static Replace *
74ReplaceNew (void);
75
76static void
77ReplaceDispose (Replace *r);
78
79static void
67accef4 80Bail (const char *format, const char *arg);
34cd0514
CW
81
82static Replace *
83ReplaceRead (FILE *f);
84
85typedef struct _replaceList {
86 struct _replaceList *next;
87 Replace *r;
88} ReplaceList;
89
90static ReplaceList *
91ReplaceListNew (Replace *r, ReplaceList *next);
92
93static void
94ReplaceListDispose (ReplaceList *l);
95
96typedef struct {
97 ReplaceList *head;
98} ReplaceSet;
99
100static ReplaceSet *
101ReplaceSetNew (void);
102
103static void
104ReplaceSetDispose (ReplaceSet *s);
105
106static void
107ReplaceSetAdd (ReplaceSet *s, Replace *r);
108
109static Replace *
110ReplaceSetFind (ReplaceSet *s, char *tag);
111
112static ReplaceSet *
113ReplaceSetRead (FILE *f);
114
115typedef struct _skipStack {
116 struct _skipStack *prev;
117 int skipping;
118} SkipStack;
119
120static SkipStack *
121SkipStackPush (SkipStack *prev, int skipping);
122
123static SkipStack *
124SkipStackPop (SkipStack *prev);
125
126typedef struct _loopStack {
127 struct _loopStack *prev;
128 String *tag;
129 String *extra;
130 long pos;
131} LoopStack;
132
133static LoopStack *
134LoopStackPush (LoopStack *prev, FILE *f, char *tag);
135
136static LoopStack *
137LoopStackLoop (ReplaceSet *rs, LoopStack *ls, FILE *f);
138
139static void
140LineSkip (FILE *f);
141
142static void
143DoReplace (FILE *f, ReplaceSet *s);
144
5e1f56b5
KP
145#define STRING_INIT 128
146
34cd0514 147static void *
5e1f56b5
KP
148New (int size)
149{
150 void *m = malloc (size);
151 if (!m)
152 abort ();
153 return m;
154}
155
34cd0514 156static void *
5e1f56b5
KP
157Reallocate (void *p, int size)
158{
159 void *r = realloc (p, size);
160
161 if (!r)
162 abort ();
163 return r;
164}
165
34cd0514 166static void
5e1f56b5
KP
167Dispose (void *p)
168{
169 free (p);
170}
171
34cd0514 172static String *
5e1f56b5
KP
173StringNew (void)
174{
175 String *s;
176
177 s = New (sizeof (String));
178 s->buf = New (STRING_INIT);
179 s->size = STRING_INIT - 1;
180 s->buf[0] = '\0';
181 s->len = 0;
182 return s;
183}
184
34cd0514 185static void
5e1f56b5
KP
186StringAdd (String *s, char c)
187{
188 if (s->len == s->size)
189 s->buf = Reallocate (s->buf, (s->size *= 2) + 1);
190 s->buf[s->len++] = c;
191 s->buf[s->len] = '\0';
192}
193
34cd0514 194static void
5e1f56b5
KP
195StringAddString (String *s, char *buf)
196{
197 while (*buf)
198 StringAdd (s, *buf++);
199}
200
34cd0514 201static String *
5e1f56b5
KP
202StringMake (char *buf)
203{
204 String *s = StringNew ();
205 StringAddString (s, buf);
206 return s;
207}
208
34cd0514 209static void
5e1f56b5
KP
210StringDel (String *s)
211{
212 if (s->len)
213 s->buf[--s->len] = '\0';
214}
215
34cd0514 216static void
5e1f56b5
KP
217StringPut (FILE *f, String *s)
218{
219 char *b = s->buf;
220
221 while (*b)
222 putc (*b++, f);
223}
224
225#define StringLast(s) ((s)->len ? (s)->buf[(s)->len - 1] : '\0')
226
34cd0514 227static void
5e1f56b5
KP
228StringDispose (String *s)
229{
230 Dispose (s->buf);
231 Dispose (s);
232}
233
34cd0514 234static Replace *
5e1f56b5
KP
235ReplaceNew (void)
236{
237 Replace *r = New (sizeof (Replace));
238 r->tag = StringNew ();
239 r->text = StringNew ();
240 return r;
241}
242
34cd0514 243static void
5e1f56b5
KP
244ReplaceDispose (Replace *r)
245{
246 StringDispose (r->tag);
247 StringDispose (r->text);
248 Dispose (r);
249}
250
34cd0514 251static void
67accef4 252Bail (const char *format, const char *arg)
39381776
KP
253{
254 fprintf (stderr, "fatal: ");
255 fprintf (stderr, format, arg);
256 fprintf (stderr, "\n");
257 exit (1);
258}
259
34cd0514 260static Replace *
5e1f56b5
KP
261ReplaceRead (FILE *f)
262{
263 int c;
264 Replace *r;
265
266 while ((c = getc (f)) != '@')
267 {
268 if (c == EOF)
269 return 0;
270 }
271 r = ReplaceNew();
272 while ((c = getc (f)) != '@')
273 {
274 if (c == EOF)
275 {
276 ReplaceDispose (r);
277 return 0;
278 }
39381776
KP
279 if (isspace (c))
280 Bail ("invalid character after tag %s", r->tag->buf);
5e1f56b5
KP
281 StringAdd (r->tag, c);
282 }
283 if (r->tag->buf[0] == '\0')
284 {
285 ReplaceDispose (r);
286 return 0;
287 }
288 while (isspace ((c = getc (f))))
289 ;
290 ungetc (c, f);
291 while ((c = getc (f)) != '@' && c != EOF)
292 StringAdd (r->text, c);
293 if (c == '@')
294 ungetc (c, f);
b219ac6b 295 while (isspace (StringLast (r->text)))
5e1f56b5 296 StringDel (r->text);
61895ed1
KP
297 if (StringLast(r->text) == '%')
298 {
299 StringDel (r->text);
300 StringAdd (r->text, ' ');
301 }
5e1f56b5
KP
302 return r;
303}
304
34cd0514 305static ReplaceList *
5e1f56b5
KP
306ReplaceListNew (Replace *r, ReplaceList *next)
307{
308 ReplaceList *l = New (sizeof (ReplaceList));
309 l->r = r;
310 l->next = next;
311 return l;
312}
313
34cd0514 314static void
5e1f56b5
KP
315ReplaceListDispose (ReplaceList *l)
316{
317 if (l)
318 {
319 ReplaceListDispose (l->next);
320 ReplaceDispose (l->r);
321 Dispose (l);
322 }
323}
324
34cd0514 325static ReplaceSet *
5e1f56b5
KP
326ReplaceSetNew (void)
327{
328 ReplaceSet *s = New (sizeof (ReplaceSet));
329 s->head = 0;
330 return s;
331}
332
34cd0514 333static void
5e1f56b5
KP
334ReplaceSetDispose (ReplaceSet *s)
335{
336 ReplaceListDispose (s->head);
337 Dispose (s);
338}
339
34cd0514 340static void
5e1f56b5
KP
341ReplaceSetAdd (ReplaceSet *s, Replace *r)
342{
343 s->head = ReplaceListNew (r, s->head);
344}
345
34cd0514 346static Replace *
5e1f56b5
KP
347ReplaceSetFind (ReplaceSet *s, char *tag)
348{
349 ReplaceList *l;
350
351 for (l = s->head; l; l = l->next)
352 if (!strcmp (tag, l->r->tag->buf))
353 return l->r;
354 return 0;
355}
356
34cd0514 357static ReplaceSet *
5e1f56b5
KP
358ReplaceSetRead (FILE *f)
359{
360 ReplaceSet *s = ReplaceSetNew ();
361 Replace *r;
362
363 while ((r = ReplaceRead (f)))
364 {
365 while (ReplaceSetFind (s, r->tag->buf))
366 StringAdd (r->tag, '+');
367 ReplaceSetAdd (s, r);
368 }
369 if (!s->head)
370 {
371 ReplaceSetDispose (s);
372 s = 0;
373 }
374 return s;
375}
376
34cd0514 377static SkipStack *
5e1f56b5
KP
378SkipStackPush (SkipStack *prev, int skipping)
379{
380 SkipStack *ss = New (sizeof (SkipStack));
381 ss->prev = prev;
382 ss->skipping = skipping;
383 return ss;
384}
385
34cd0514 386static SkipStack *
5e1f56b5
KP
387SkipStackPop (SkipStack *prev)
388{
389 SkipStack *ss = prev->prev;
390 Dispose (prev);
391 return ss;
392}
393
34cd0514 394static LoopStack *
5e1f56b5
KP
395LoopStackPush (LoopStack *prev, FILE *f, char *tag)
396{
397 LoopStack *ls = New (sizeof (LoopStack));
398 ls->prev = prev;
399 ls->tag = StringMake (tag);
400 ls->extra = StringNew ();
401 ls->pos = ftell (f);
402 return ls;
403}
404
34cd0514 405static LoopStack *
5e1f56b5
KP
406LoopStackLoop (ReplaceSet *rs, LoopStack *ls, FILE *f)
407{
408 String *s = StringMake (ls->tag->buf);
409 LoopStack *ret = ls;
410 Bool loop;
411
412 StringAdd (ls->extra, '+');
413 StringAddString (s, ls->extra->buf);
414 loop = ReplaceSetFind (rs, s->buf) != 0;
415 StringDispose (s);
416 if (loop)
417 fseek (f, ls->pos, SEEK_SET);
418 else
419 {
420 ret = ls->prev;
421 StringDispose (ls->tag);
422 StringDispose (ls->extra);
423 Dispose (ls);
424 }
425 return ret;
426}
427
34cd0514 428static void
5e1f56b5
KP
429LineSkip (FILE *f)
430{
431 int c;
432
433 while ((c = getc (f)) == '\n')
434 ;
435 ungetc (c, f);
436}
437
34cd0514 438static void
5e1f56b5
KP
439DoReplace (FILE *f, ReplaceSet *s)
440{
441 int c;
442 String *tag;
443 Replace *r;
444 SkipStack *ss = 0;
445 LoopStack *ls = 0;
446 int skipping = 0;
447
448 while ((c = getc (f)) != EOF)
449 {
450 if (c == '@')
451 {
452 tag = StringNew ();
453 while ((c = getc (f)) != '@')
454 {
455 if (c == EOF)
456 abort ();
457 StringAdd (tag, c);
458 }
459 if (ls)
460 StringAddString (tag, ls->extra->buf);
461 switch (tag->buf[0]) {
462 case '?':
463 ss = SkipStackPush (ss, skipping);
464 if (!ReplaceSetFind (s, tag->buf + 1))
465 skipping++;
466 LineSkip (f);
467 break;
468 case ':':
469 if (!ss)
470 abort ();
471 if (ss->skipping == skipping)
472 ++skipping;
473 else
474 --skipping;
475 LineSkip (f);
476 break;
477 case ';':
478 skipping = ss->skipping;
479 ss = SkipStackPop (ss);
480 LineSkip (f);
481 break;
482 case '{':
483 ls = LoopStackPush (ls, f, tag->buf + 1);
484 LineSkip (f);
485 break;
486 case '}':
487 ls = LoopStackLoop (s, ls, f);
488 LineSkip (f);
489 break;
490 default:
491 r = ReplaceSetFind (s, tag->buf);
492 if (r && !skipping)
493 StringPut (stdout, r->text);
494 break;
495 }
496 StringDispose (tag);
497 }
498 else if (!skipping)
499 putchar (c);
500 }
501}
502
503int
504main (int argc, char **argv)
505{
506 FILE *f;
507 ReplaceSet *s;
508
39381776
KP
509 if (!argv[1])
510 Bail ("usage: %s <template.sgml>", argv[0]);
5e1f56b5
KP
511 f = fopen (argv[1], "r");
512 if (!f)
513 {
39381776 514 Bail ("can't open file %s", argv[1]);
5e1f56b5
KP
515 exit (1);
516 }
517 while ((s = ReplaceSetRead (stdin)))
518 {
519 DoReplace (f, s);
520 ReplaceSetDispose (s);
521 rewind (f);
522 }
523 if (ferror (stdout))
39381776 524 Bail ("%s", "error writing output");
5e1f56b5
KP
525 exit (0);
526}