/*
* $Id$
*
- * Copyright © 2003 Keith Packard
+ * Copyright © 2003 Keith Packard
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <ctype.h>
+static void *
+New (int size);
+
+static void *
+Reallocate (void *p, int size);
+
+static void
+Dispose (void *p);
+
typedef enum { False, True } Bool;
typedef struct {
int len;
} String;
+static String *
+StringNew (void);
+
+static void
+StringAdd (String *s, char c);
+
+static void
+StringAddString (String *s, char *buf);
+
+static String *
+StringMake (char *buf);
+
+static void
+StringDel (String *s);
+
+static void
+StringPut (FILE *f, String *s);
+
+static void
+StringDispose (String *s);
+
+typedef struct {
+ String *tag;
+ String *text;
+} Replace;
+
+static Replace *
+ReplaceNew (void);
+
+static void
+ReplaceDispose (Replace *r);
+
+static void
+Bail (const char *format, const char *arg);
+
+static Replace *
+ReplaceRead (FILE *f);
+
+typedef struct _replaceList {
+ struct _replaceList *next;
+ Replace *r;
+} ReplaceList;
+
+static ReplaceList *
+ReplaceListNew (Replace *r, ReplaceList *next);
+
+static void
+ReplaceListDispose (ReplaceList *l);
+
+typedef struct {
+ ReplaceList *head;
+} ReplaceSet;
+
+static ReplaceSet *
+ReplaceSetNew (void);
+
+static void
+ReplaceSetDispose (ReplaceSet *s);
+
+static void
+ReplaceSetAdd (ReplaceSet *s, Replace *r);
+
+static Replace *
+ReplaceSetFind (ReplaceSet *s, char *tag);
+
+static ReplaceSet *
+ReplaceSetRead (FILE *f);
+
+typedef struct _skipStack {
+ struct _skipStack *prev;
+ int skipping;
+} SkipStack;
+
+static SkipStack *
+SkipStackPush (SkipStack *prev, int skipping);
+
+static SkipStack *
+SkipStackPop (SkipStack *prev);
+
+typedef struct _loopStack {
+ struct _loopStack *prev;
+ String *tag;
+ String *extra;
+ long pos;
+} LoopStack;
+
+static LoopStack *
+LoopStackPush (LoopStack *prev, FILE *f, char *tag);
+
+static LoopStack *
+LoopStackLoop (ReplaceSet *rs, LoopStack *ls, FILE *f);
+
+static void
+LineSkip (FILE *f);
+
+static void
+DoReplace (FILE *f, ReplaceSet *s);
+
#define STRING_INIT 128
-void *
+static void *
New (int size)
{
void *m = malloc (size);
return m;
}
-void *
+static void *
Reallocate (void *p, int size)
{
void *r = realloc (p, size);
return r;
}
-void
+static void
Dispose (void *p)
{
free (p);
}
-String *
+static String *
StringNew (void)
{
String *s;
return s;
}
-void
+static void
StringAdd (String *s, char c)
{
if (s->len == s->size)
s->buf[s->len] = '\0';
}
-void
+static void
StringAddString (String *s, char *buf)
{
while (*buf)
StringAdd (s, *buf++);
}
-String *
+static String *
StringMake (char *buf)
{
String *s = StringNew ();
return s;
}
-void
+static void
StringDel (String *s)
{
if (s->len)
s->buf[--s->len] = '\0';
}
-void
+static void
StringPut (FILE *f, String *s)
{
char *b = s->buf;
#define StringLast(s) ((s)->len ? (s)->buf[(s)->len - 1] : '\0')
-void
+static void
StringDispose (String *s)
{
Dispose (s->buf);
Dispose (s);
}
-typedef struct {
- String *tag;
- String *text;
-} Replace;
-
-Replace *
+static Replace *
ReplaceNew (void)
{
Replace *r = New (sizeof (Replace));
return r;
}
-void
+static void
ReplaceDispose (Replace *r)
{
StringDispose (r->tag);
Dispose (r);
}
-void
-Bail (char *format, char *arg)
+static void
+Bail (const char *format, const char *arg)
{
fprintf (stderr, "fatal: ");
fprintf (stderr, format, arg);
exit (1);
}
-Replace *
+static Replace *
ReplaceRead (FILE *f)
{
int c;
return r;
}
-typedef struct _replaceList {
- struct _replaceList *next;
- Replace *r;
-} ReplaceList;
-
-ReplaceList *
+static ReplaceList *
ReplaceListNew (Replace *r, ReplaceList *next)
{
ReplaceList *l = New (sizeof (ReplaceList));
return l;
}
-void
+static void
ReplaceListDispose (ReplaceList *l)
{
if (l)
}
}
-typedef struct {
- ReplaceList *head;
-} ReplaceSet;
-
-ReplaceSet *
+static ReplaceSet *
ReplaceSetNew (void)
{
ReplaceSet *s = New (sizeof (ReplaceSet));
return s;
}
-void
+static void
ReplaceSetDispose (ReplaceSet *s)
{
ReplaceListDispose (s->head);
Dispose (s);
}
-void
+static void
ReplaceSetAdd (ReplaceSet *s, Replace *r)
{
s->head = ReplaceListNew (r, s->head);
}
-Replace *
+static Replace *
ReplaceSetFind (ReplaceSet *s, char *tag)
{
ReplaceList *l;
return 0;
}
-ReplaceSet *
+static ReplaceSet *
ReplaceSetRead (FILE *f)
{
ReplaceSet *s = ReplaceSetNew ();
return s;
}
-typedef struct _skipStack {
- struct _skipStack *prev;
- int skipping;
-} SkipStack;
-
-SkipStack *
+static SkipStack *
SkipStackPush (SkipStack *prev, int skipping)
{
SkipStack *ss = New (sizeof (SkipStack));
return ss;
}
-SkipStack *
+static SkipStack *
SkipStackPop (SkipStack *prev)
{
SkipStack *ss = prev->prev;
return ss;
}
-typedef struct _loopStack {
- struct _loopStack *prev;
- String *tag;
- String *extra;
- long pos;
-} LoopStack;
-
-LoopStack *
+static LoopStack *
LoopStackPush (LoopStack *prev, FILE *f, char *tag)
{
LoopStack *ls = New (sizeof (LoopStack));
return ls;
}
-LoopStack *
+static LoopStack *
LoopStackLoop (ReplaceSet *rs, LoopStack *ls, FILE *f)
{
String *s = StringMake (ls->tag->buf);
return ret;
}
-void
+static void
LineSkip (FILE *f)
{
int c;
ungetc (c, f);
}
-void
+static void
DoReplace (FILE *f, ReplaceSet *s)
{
int c;