]>
git.wh0rd.org - fontconfig.git/blob - doc/edit-sgml.c
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.
30 typedef enum { False
, True
} Bool
;
38 #define STRING_INIT 128
43 void *m
= malloc (size
);
50 Reallocate (void *p
, int size
)
52 void *r
= realloc (p
, size
);
70 s
= New (sizeof (String
));
71 s
->buf
= New (STRING_INIT
);
72 s
->size
= STRING_INIT
- 1;
79 StringAdd (String
*s
, char c
)
81 if (s
->len
== s
->size
)
82 s
->buf
= Reallocate (s
->buf
, (s
->size
*= 2) + 1);
84 s
->buf
[s
->len
] = '\0';
88 StringAddString (String
*s
, char *buf
)
91 StringAdd (s
, *buf
++);
95 StringMake (char *buf
)
97 String
*s
= StringNew ();
98 StringAddString (s
, buf
);
103 StringDel (String
*s
)
106 s
->buf
[--s
->len
] = '\0';
110 StringPut (FILE *f
, String
*s
)
118 #define StringLast(s) ((s)->len ? (s)->buf[(s)->len - 1] : '\0')
121 StringDispose (String
*s
)
135 Replace
*r
= New (sizeof (Replace
));
136 r
->tag
= StringNew ();
137 r
->text
= StringNew ();
142 ReplaceDispose (Replace
*r
)
144 StringDispose (r
->tag
);
145 StringDispose (r
->text
);
150 Bail (char *format
, char *arg
)
152 fprintf (stderr
, "fatal: ");
153 fprintf (stderr
, format
, arg
);
154 fprintf (stderr
, "\n");
159 ReplaceRead (FILE *f
)
164 while ((c
= getc (f
)) != '@')
170 while ((c
= getc (f
)) != '@')
178 Bail ("invalid character after tag %s", r
->tag
->buf
);
179 StringAdd (r
->tag
, c
);
181 if (r
->tag
->buf
[0] == '\0')
186 while (isspace ((c
= getc (f
))))
189 while ((c
= getc (f
)) != '@' && c
!= EOF
)
190 StringAdd (r
->text
, c
);
193 while (isspace (StringLast (r
->text
)))
198 typedef struct _replaceList
{
199 struct _replaceList
*next
;
204 ReplaceListNew (Replace
*r
, ReplaceList
*next
)
206 ReplaceList
*l
= New (sizeof (ReplaceList
));
213 ReplaceListDispose (ReplaceList
*l
)
217 ReplaceListDispose (l
->next
);
218 ReplaceDispose (l
->r
);
230 ReplaceSet
*s
= New (sizeof (ReplaceSet
));
236 ReplaceSetDispose (ReplaceSet
*s
)
238 ReplaceListDispose (s
->head
);
243 ReplaceSetAdd (ReplaceSet
*s
, Replace
*r
)
245 s
->head
= ReplaceListNew (r
, s
->head
);
249 ReplaceSetFind (ReplaceSet
*s
, char *tag
)
253 for (l
= s
->head
; l
; l
= l
->next
)
254 if (!strcmp (tag
, l
->r
->tag
->buf
))
260 ReplaceSetRead (FILE *f
)
262 ReplaceSet
*s
= ReplaceSetNew ();
265 while ((r
= ReplaceRead (f
)))
267 while (ReplaceSetFind (s
, r
->tag
->buf
))
268 StringAdd (r
->tag
, '+');
269 ReplaceSetAdd (s
, r
);
273 ReplaceSetDispose (s
);
279 typedef struct _skipStack
{
280 struct _skipStack
*prev
;
285 SkipStackPush (SkipStack
*prev
, int skipping
)
287 SkipStack
*ss
= New (sizeof (SkipStack
));
289 ss
->skipping
= skipping
;
294 SkipStackPop (SkipStack
*prev
)
296 SkipStack
*ss
= prev
->prev
;
301 typedef struct _loopStack
{
302 struct _loopStack
*prev
;
309 LoopStackPush (LoopStack
*prev
, FILE *f
, char *tag
)
311 LoopStack
*ls
= New (sizeof (LoopStack
));
313 ls
->tag
= StringMake (tag
);
314 ls
->extra
= StringNew ();
320 LoopStackLoop (ReplaceSet
*rs
, LoopStack
*ls
, FILE *f
)
322 String
*s
= StringMake (ls
->tag
->buf
);
326 StringAdd (ls
->extra
, '+');
327 StringAddString (s
, ls
->extra
->buf
);
328 loop
= ReplaceSetFind (rs
, s
->buf
) != 0;
331 fseek (f
, ls
->pos
, SEEK_SET
);
335 StringDispose (ls
->tag
);
336 StringDispose (ls
->extra
);
347 while ((c
= getc (f
)) == '\n')
353 DoReplace (FILE *f
, ReplaceSet
*s
)
362 while ((c
= getc (f
)) != EOF
)
367 while ((c
= getc (f
)) != '@')
374 StringAddString (tag
, ls
->extra
->buf
);
375 switch (tag
->buf
[0]) {
377 ss
= SkipStackPush (ss
, skipping
);
378 if (!ReplaceSetFind (s
, tag
->buf
+ 1))
385 if (ss
->skipping
== skipping
)
392 skipping
= ss
->skipping
;
393 ss
= SkipStackPop (ss
);
397 ls
= LoopStackPush (ls
, f
, tag
->buf
+ 1);
401 ls
= LoopStackLoop (s
, ls
, f
);
405 r
= ReplaceSetFind (s
, tag
->buf
);
407 StringPut (stdout
, r
->text
);
418 main (int argc
, char **argv
)
424 Bail ("usage: %s <template.sgml>", argv
[0]);
425 f
= fopen (argv
[1], "r");
428 Bail ("can't open file %s", argv
[1]);
431 while ((s
= ReplaceSetRead (stdin
)))
434 ReplaceSetDispose (s
);
438 Bail ("%s", "error writing output");