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.
29 typedef enum { False, True } Bool;
37 #define STRING_INIT 128
42 void *m = malloc (size);
49 Reallocate (void *p, int size)
51 void *r = realloc (p, size);
69 s = New (sizeof (String));
70 s->buf = New (STRING_INIT);
71 s->size = STRING_INIT - 1;
78 StringAdd (String *s, char c)
80 if (s->len == s->size)
81 s->buf = Reallocate (s->buf, (s->size *= 2) + 1);
83 s->buf[s->len] = '\0';
87 StringAddString (String *s, char *buf)
90 StringAdd (s, *buf++);
94 StringMake (char *buf)
96 String *s = StringNew ();
97 StringAddString (s, buf);
102 StringDel (String *s)
105 s->buf[--s->len] = '\0';
109 StringPut (FILE *f, String *s)
117 #define StringLast(s) ((s)->len ? (s)->buf[(s)->len - 1] : '\0')
120 StringDispose (String *s)
134 Replace *r = New (sizeof (Replace));
135 r->tag = StringNew ();
136 r->text = StringNew ();
141 ReplaceDispose (Replace *r)
143 StringDispose (r->tag);
144 StringDispose (r->text);
149 ReplaceRead (FILE *f)
154 while ((c = getc (f)) != '@')
160 while ((c = getc (f)) != '@')
167 StringAdd (r->tag, c);
169 if (r->tag->buf[0] == '\0')
174 while (isspace ((c = getc (f))))
177 while ((c = getc (f)) != '@' && c != EOF)
178 StringAdd (r->text, c);
181 while (StringLast (r->text) == '\n')
186 typedef struct _replaceList {
187 struct _replaceList *next;
192 ReplaceListNew (Replace *r, ReplaceList *next)
194 ReplaceList *l = New (sizeof (ReplaceList));
201 ReplaceListDispose (ReplaceList *l)
205 ReplaceListDispose (l->next);
206 ReplaceDispose (l->r);
218 ReplaceSet *s = New (sizeof (ReplaceSet));
224 ReplaceSetDispose (ReplaceSet *s)
226 ReplaceListDispose (s->head);
231 ReplaceSetAdd (ReplaceSet *s, Replace *r)
233 s->head = ReplaceListNew (r, s->head);
237 ReplaceSetFind (ReplaceSet *s, char *tag)
241 for (l = s->head; l; l = l->next)
242 if (!strcmp (tag, l->r->tag->buf))
248 ReplaceSetRead (FILE *f)
250 ReplaceSet *s = ReplaceSetNew ();
253 while ((r = ReplaceRead (f)))
255 while (ReplaceSetFind (s, r->tag->buf))
256 StringAdd (r->tag, '+');
257 ReplaceSetAdd (s, r);
261 ReplaceSetDispose (s);
267 typedef struct _skipStack {
268 struct _skipStack *prev;
273 SkipStackPush (SkipStack *prev, int skipping)
275 SkipStack *ss = New (sizeof (SkipStack));
277 ss->skipping = skipping;
282 SkipStackPop (SkipStack *prev)
284 SkipStack *ss = prev->prev;
289 typedef struct _loopStack {
290 struct _loopStack *prev;
297 LoopStackPush (LoopStack *prev, FILE *f, char *tag)
299 LoopStack *ls = New (sizeof (LoopStack));
301 ls->tag = StringMake (tag);
302 ls->extra = StringNew ();
308 LoopStackLoop (ReplaceSet *rs, LoopStack *ls, FILE *f)
310 String *s = StringMake (ls->tag->buf);
314 StringAdd (ls->extra, '+');
315 StringAddString (s, ls->extra->buf);
316 loop = ReplaceSetFind (rs, s->buf) != 0;
319 fseek (f, ls->pos, SEEK_SET);
323 StringDispose (ls->tag);
324 StringDispose (ls->extra);
335 while ((c = getc (f)) == '\n')
341 DoReplace (FILE *f, ReplaceSet *s)
350 while ((c = getc (f)) != EOF)
355 while ((c = getc (f)) != '@')
362 StringAddString (tag, ls->extra->buf);
363 switch (tag->buf[0]) {
365 ss = SkipStackPush (ss, skipping);
366 if (!ReplaceSetFind (s, tag->buf + 1))
373 if (ss->skipping == skipping)
380 skipping = ss->skipping;
381 ss = SkipStackPop (ss);
385 ls = LoopStackPush (ls, f, tag->buf + 1);
389 ls = LoopStackLoop (s, ls, f);
393 r = ReplaceSetFind (s, tag->buf);
395 StringPut (stdout, r->text);
406 main (int argc, char **argv)
411 f = fopen (argv[1], "r");
417 while ((s = ReplaceSetRead (stdin)))
420 ReplaceSetDispose (s);