]>
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.
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
, int line
, const char *arg
);
83 ReplaceRead (FILE *f
, int *linep
);
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
, int *linep
);
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
);
140 LineSkip (FILE *f
, int *linep
);
143 DoReplace (FILE *f
, int *linep
, 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
, int line
, const char *arg
)
254 fprintf (stderr
, "fatal: ");
255 fprintf (stderr
, format
, line
, arg
);
256 fprintf (stderr
, "\n");
261 Getc (FILE *f
, int *linep
)
269 Ungetc (int c
, FILE *f
, int *linep
)
277 ReplaceRead (FILE *f
, int *linep
)
282 while ((c
= Getc (f
, linep
)) != '@')
288 while ((c
= Getc (f
, linep
)) != '@')
296 Bail ("%d: invalid character after tag %s", *linep
, r
->tag
->buf
);
297 StringAdd (r
->tag
, c
);
299 if (r
->tag
->buf
[0] == '\0')
304 while (isspace ((c
= Getc (f
, linep
))))
306 Ungetc (c
, f
, linep
);
307 while ((c
= Getc (f
, linep
)) != '@' && c
!= EOF
)
308 StringAdd (r
->text
, c
);
310 Ungetc (c
, f
, linep
);
311 while (isspace (StringLast (r
->text
)))
313 if (StringLast(r
->text
) == '%')
316 StringAdd (r
->text
, ' ');
322 ReplaceListNew (Replace
*r
, ReplaceList
*next
)
324 ReplaceList
*l
= New (sizeof (ReplaceList
));
331 ReplaceListDispose (ReplaceList
*l
)
335 ReplaceListDispose (l
->next
);
336 ReplaceDispose (l
->r
);
344 ReplaceSet
*s
= New (sizeof (ReplaceSet
));
350 ReplaceSetDispose (ReplaceSet
*s
)
352 ReplaceListDispose (s
->head
);
357 ReplaceSetAdd (ReplaceSet
*s
, Replace
*r
)
359 s
->head
= ReplaceListNew (r
, s
->head
);
363 ReplaceSetFind (ReplaceSet
*s
, char *tag
)
367 for (l
= s
->head
; l
; l
= l
->next
)
368 if (!strcmp (tag
, l
->r
->tag
->buf
))
374 ReplaceSetRead (FILE *f
, int *linep
)
376 ReplaceSet
*s
= ReplaceSetNew ();
379 while ((r
= ReplaceRead (f
, linep
)))
381 while (ReplaceSetFind (s
, r
->tag
->buf
))
382 StringAdd (r
->tag
, '+');
383 ReplaceSetAdd (s
, r
);
387 ReplaceSetDispose (s
);
394 SkipStackPush (SkipStack
*prev
, int skipping
)
396 SkipStack
*ss
= New (sizeof (SkipStack
));
398 ss
->skipping
= skipping
;
403 SkipStackPop (SkipStack
*prev
)
405 SkipStack
*ss
= prev
->prev
;
411 LoopStackPush (LoopStack
*prev
, FILE *f
, char *tag
)
413 LoopStack
*ls
= New (sizeof (LoopStack
));
415 ls
->tag
= StringMake (tag
);
416 ls
->extra
= StringNew ();
422 LoopStackLoop (ReplaceSet
*rs
, LoopStack
*ls
, FILE *f
)
424 String
*s
= StringMake (ls
->tag
->buf
);
428 StringAdd (ls
->extra
, '+');
429 StringAddString (s
, ls
->extra
->buf
);
430 loop
= ReplaceSetFind (rs
, s
->buf
) != 0;
433 fseek (f
, ls
->pos
, SEEK_SET
);
437 StringDispose (ls
->tag
);
438 StringDispose (ls
->extra
);
445 LineSkip (FILE *f
, int *linep
)
449 while ((c
= Getc (f
, linep
)) == '\n')
451 Ungetc (c
, f
, linep
);
455 DoReplace (FILE *f
, int *linep
, ReplaceSet
*s
)
464 while ((c
= Getc (f
, linep
)) != EOF
)
469 while ((c
= Getc (f
, linep
)) != '@')
476 StringAddString (tag
, ls
->extra
->buf
);
477 switch (tag
->buf
[0]) {
479 ss
= SkipStackPush (ss
, skipping
);
480 if (!ReplaceSetFind (s
, tag
->buf
+ 1))
487 if (ss
->skipping
== skipping
)
494 skipping
= ss
->skipping
;
495 ss
= SkipStackPop (ss
);
499 ls
= LoopStackPush (ls
, f
, tag
->buf
+ 1);
503 ls
= LoopStackLoop (s
, ls
, f
);
507 r
= ReplaceSetFind (s
, tag
->buf
);
509 StringPut (stdout
, r
->text
);
520 main (int argc
, char **argv
)
527 Bail ("usage: %s <template.sgml>", 0, argv
[0]);
528 f
= fopen (argv
[1], "r");
531 Bail ("can't open file %s", 0, argv
[1]);
535 while ((s
= ReplaceSetRead (stdin
, &iline
)))
538 DoReplace (f
, &oline
, s
);
539 ReplaceSetDispose (s
);
543 Bail ("%s", 0, "error writing output");