]>
git.wh0rd.org - fontconfig.git/blob - doc/edit-sgml.c
3f3be53aaafd64a6e8e1d5bfb26781414ba67a8f
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
)))
297 if (StringLast(r
->text
) == '%')
300 StringAdd (r
->text
, ' ');
306 ReplaceListNew (Replace
*r
, ReplaceList
*next
)
308 ReplaceList
*l
= New (sizeof (ReplaceList
));
315 ReplaceListDispose (ReplaceList
*l
)
319 ReplaceListDispose (l
->next
);
320 ReplaceDispose (l
->r
);
328 ReplaceSet
*s
= New (sizeof (ReplaceSet
));
334 ReplaceSetDispose (ReplaceSet
*s
)
336 ReplaceListDispose (s
->head
);
341 ReplaceSetAdd (ReplaceSet
*s
, Replace
*r
)
343 s
->head
= ReplaceListNew (r
, s
->head
);
347 ReplaceSetFind (ReplaceSet
*s
, char *tag
)
351 for (l
= s
->head
; l
; l
= l
->next
)
352 if (!strcmp (tag
, l
->r
->tag
->buf
))
358 ReplaceSetRead (FILE *f
)
360 ReplaceSet
*s
= ReplaceSetNew ();
363 while ((r
= ReplaceRead (f
)))
365 while (ReplaceSetFind (s
, r
->tag
->buf
))
366 StringAdd (r
->tag
, '+');
367 ReplaceSetAdd (s
, r
);
371 ReplaceSetDispose (s
);
378 SkipStackPush (SkipStack
*prev
, int skipping
)
380 SkipStack
*ss
= New (sizeof (SkipStack
));
382 ss
->skipping
= skipping
;
387 SkipStackPop (SkipStack
*prev
)
389 SkipStack
*ss
= prev
->prev
;
395 LoopStackPush (LoopStack
*prev
, FILE *f
, char *tag
)
397 LoopStack
*ls
= New (sizeof (LoopStack
));
399 ls
->tag
= StringMake (tag
);
400 ls
->extra
= StringNew ();
406 LoopStackLoop (ReplaceSet
*rs
, LoopStack
*ls
, FILE *f
)
408 String
*s
= StringMake (ls
->tag
->buf
);
412 StringAdd (ls
->extra
, '+');
413 StringAddString (s
, ls
->extra
->buf
);
414 loop
= ReplaceSetFind (rs
, s
->buf
) != 0;
417 fseek (f
, ls
->pos
, SEEK_SET
);
421 StringDispose (ls
->tag
);
422 StringDispose (ls
->extra
);
433 while ((c
= getc (f
)) == '\n')
439 DoReplace (FILE *f
, ReplaceSet
*s
)
448 while ((c
= getc (f
)) != EOF
)
453 while ((c
= getc (f
)) != '@')
460 StringAddString (tag
, ls
->extra
->buf
);
461 switch (tag
->buf
[0]) {
463 ss
= SkipStackPush (ss
, skipping
);
464 if (!ReplaceSetFind (s
, tag
->buf
+ 1))
471 if (ss
->skipping
== skipping
)
478 skipping
= ss
->skipping
;
479 ss
= SkipStackPop (ss
);
483 ls
= LoopStackPush (ls
, f
, tag
->buf
+ 1);
487 ls
= LoopStackLoop (s
, ls
, f
);
491 r
= ReplaceSetFind (s
, tag
->buf
);
493 StringPut (stdout
, r
->text
);
504 main (int argc
, char **argv
)
510 Bail ("usage: %s <template.sgml>", argv
[0]);
511 f
= fopen (argv
[1], "r");
514 Bail ("can't open file %s", argv
[1]);
517 while ((s
= ReplaceSetRead (stdin
)))
520 ReplaceSetDispose (s
);
524 Bail ("%s", "error writing output");