4 * Copyright © 2003 Keith Packard
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.
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.
34 Reallocate (void *p, int size);
39 typedef enum { False, True } Bool;
51 StringAdd (String *s, char c);
54 StringAddString (String *s, char *buf);
57 StringMake (char *buf);
60 StringDel (String *s);
63 StringPut (FILE *f, String *s);
66 StringDispose (String *s);
77 ReplaceDispose (Replace *r);
80 Bail (const char *format, const char *arg);
83 ReplaceRead (FILE *f);
85 typedef struct _replaceList {
86 struct _replaceList *next;
91 ReplaceListNew (Replace *r, ReplaceList *next);
94 ReplaceListDispose (ReplaceList *l);
101 ReplaceSetNew (void);
104 ReplaceSetDispose (ReplaceSet *s);
107 ReplaceSetAdd (ReplaceSet *s, Replace *r);
110 ReplaceSetFind (ReplaceSet *s, char *tag);
113 ReplaceSetRead (FILE *f);
115 typedef struct _skipStack {
116 struct _skipStack *prev;
121 SkipStackPush (SkipStack *prev, int skipping);
124 SkipStackPop (SkipStack *prev);
126 typedef struct _loopStack {
127 struct _loopStack *prev;
134 LoopStackPush (LoopStack *prev, FILE *f, char *tag);
137 LoopStackLoop (ReplaceSet *rs, LoopStack *ls, FILE *f);
143 DoReplace (FILE *f, ReplaceSet *s);
145 #define STRING_INIT 128
150 void *m = malloc (size);
157 Reallocate (void *p, int size)
159 void *r = realloc (p, size);
177 s = New (sizeof (String));
178 s->buf = New (STRING_INIT);
179 s->size = STRING_INIT - 1;
186 StringAdd (String *s, char c)
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';
195 StringAddString (String *s, char *buf)
198 StringAdd (s, *buf++);
202 StringMake (char *buf)
204 String *s = StringNew ();
205 StringAddString (s, buf);
210 StringDel (String *s)
213 s->buf[--s->len] = '\0';
217 StringPut (FILE *f, String *s)
225 #define StringLast(s) ((s)->len ? (s)->buf[(s)->len - 1] : '\0')
228 StringDispose (String *s)
237 Replace *r = New (sizeof (Replace));
238 r->tag = StringNew ();
239 r->text = StringNew ();
244 ReplaceDispose (Replace *r)
246 StringDispose (r->tag);
247 StringDispose (r->text);
252 Bail (const char *format, const char *arg)
254 fprintf (stderr, "fatal: ");
255 fprintf (stderr, format, arg);
256 fprintf (stderr, "\n");
261 ReplaceRead (FILE *f)
266 while ((c = getc (f)) != '@')
272 while ((c = getc (f)) != '@')
280 Bail ("invalid character after tag %s", r->tag->buf);
281 StringAdd (r->tag, c);
283 if (r->tag->buf[0] == '\0')
288 while (isspace ((c = getc (f))))
291 while ((c = getc (f)) != '@' && c != EOF)
292 StringAdd (r->text, c);
295 while (isspace (StringLast (r->text)))
301 ReplaceListNew (Replace *r, ReplaceList *next)
303 ReplaceList *l = New (sizeof (ReplaceList));
310 ReplaceListDispose (ReplaceList *l)
314 ReplaceListDispose (l->next);
315 ReplaceDispose (l->r);
323 ReplaceSet *s = New (sizeof (ReplaceSet));
329 ReplaceSetDispose (ReplaceSet *s)
331 ReplaceListDispose (s->head);
336 ReplaceSetAdd (ReplaceSet *s, Replace *r)
338 s->head = ReplaceListNew (r, s->head);
342 ReplaceSetFind (ReplaceSet *s, char *tag)
346 for (l = s->head; l; l = l->next)
347 if (!strcmp (tag, l->r->tag->buf))
353 ReplaceSetRead (FILE *f)
355 ReplaceSet *s = ReplaceSetNew ();
358 while ((r = ReplaceRead (f)))
360 while (ReplaceSetFind (s, r->tag->buf))
361 StringAdd (r->tag, '+');
362 ReplaceSetAdd (s, r);
366 ReplaceSetDispose (s);
373 SkipStackPush (SkipStack *prev, int skipping)
375 SkipStack *ss = New (sizeof (SkipStack));
377 ss->skipping = skipping;
382 SkipStackPop (SkipStack *prev)
384 SkipStack *ss = prev->prev;
390 LoopStackPush (LoopStack *prev, FILE *f, char *tag)
392 LoopStack *ls = New (sizeof (LoopStack));
394 ls->tag = StringMake (tag);
395 ls->extra = StringNew ();
401 LoopStackLoop (ReplaceSet *rs, LoopStack *ls, FILE *f)
403 String *s = StringMake (ls->tag->buf);
407 StringAdd (ls->extra, '+');
408 StringAddString (s, ls->extra->buf);
409 loop = ReplaceSetFind (rs, s->buf) != 0;
412 fseek (f, ls->pos, SEEK_SET);
416 StringDispose (ls->tag);
417 StringDispose (ls->extra);
428 while ((c = getc (f)) == '\n')
434 DoReplace (FILE *f, ReplaceSet *s)
443 while ((c = getc (f)) != EOF)
448 while ((c = getc (f)) != '@')
455 StringAddString (tag, ls->extra->buf);
456 switch (tag->buf[0]) {
458 ss = SkipStackPush (ss, skipping);
459 if (!ReplaceSetFind (s, tag->buf + 1))
466 if (ss->skipping == skipping)
473 skipping = ss->skipping;
474 ss = SkipStackPop (ss);
478 ls = LoopStackPush (ls, f, tag->buf + 1);
482 ls = LoopStackLoop (s, ls, f);
486 r = ReplaceSetFind (s, tag->buf);
488 StringPut (stdout, r->text);
499 main (int argc, char **argv)
505 Bail ("usage: %s <template.sgml>", argv[0]);
506 f = fopen (argv[1], "r");
509 Bail ("can't open file %s", argv[1]);
512 while ((s = ReplaceSetRead (stdin)))
515 ReplaceSetDispose (s);
519 Bail ("%s", "error writing output");