summaryrefslogtreecommitdiffstats
path: root/usr.bin/sgmls/sgmlsasp/replace.c
blob: a37086be31292e8b5a8251bde6b0ff5dc8af3d81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/* replace.c
   Parse ASP style replacement file.

   Written by James Clark (jjc@jclark.com). */

#include "sgmlsasp.h"
#include "replace.h"

#define TABLE_SIZE 251

struct table_entry {
  enum event_type type;
  char *gi;
  struct replacement replacement;
  struct table_entry *next;
};

struct replacement_table {
  struct table_entry *table[TABLE_SIZE];
};

struct buffer {
  char *s;
  unsigned len;
  unsigned size;
};

/* Tokens returned by get_token(). */

#define STRING 1
#define STAGO 2
#define ETAGO 3
#define PLUS 4

static int get P((void));
static int peek P((void));
static int get_token P((void));
static void scan_name P((struct buffer *, int));
static struct replacement *define_replacement
  P((struct replacement_table *, enum event_type, char *));
static struct replacement_item **parse_string
  P((struct replacement_item **, int));
static UNIV xmalloc P((unsigned));
static UNIV xrealloc P((UNIV, unsigned));
static struct replacement_item **add_replacement_data
  P((struct replacement_item **, char *, unsigned));
static struct replacement_item **add_replacement_attr
  P((struct replacement_item **, char *));
static int hash P((enum event_type, char *));
static NO_RETURN void parse_error VP((char *,...));
static VOID buffer_init P((struct buffer *));
static VOID buffer_append P((struct buffer *, int));
static char *buffer_extract P((struct buffer *));
#if 0
static VOID buffer_free P((struct buffer *));
#endif

#define buffer_length(buf) ((buf)->len)

#define NEW(type) ((type *)xmalloc(sizeof(type)))

static int current_lineno;
static char *current_file;
static FILE *fp;

struct replacement_table *make_replacement_table()
{
  int i;
  struct replacement_table *tablep;

  tablep = NEW(struct replacement_table);
  for (i = 0; i < TABLE_SIZE; i++)
    tablep->table[i] = 0;
  return tablep;
}

void load_replacement_file(tablep, file)
     struct replacement_table *tablep;
     char *file;
{
  int tok;
  struct buffer name;

  buffer_init(&name);
  errno = 0;
  fp = fopen(file, "r");
  if (!fp) {
    if (errno)
      error("can't open `%s': %s", file, strerror(errno));
    else
      error("can't open `%s'", file);
  }

  current_lineno = 1;
  current_file = file;
  tok = get_token();
  while (tok != EOF) {
    struct replacement *p;
    struct replacement_item **tail;
    enum event_type type;

    if (tok != STAGO && tok != ETAGO)
      parse_error("syntax error");
    type = tok == STAGO ? START_ELEMENT : END_ELEMENT;
    scan_name(&name, '>');
    p = define_replacement(tablep, type, buffer_extract(&name));
    tok = get_token();
    if (tok == PLUS) {
      if (p)
	p->flags |= NEWLINE_BEGIN;
      tok = get_token();
    }
    tail = p ? &p->items : 0;
    while (tok == STRING) {
      tail = parse_string(tail, type == START_ELEMENT);
      tok = get_token();
    }
    if (tok == PLUS) {
      if (p)
	p->flags |= NEWLINE_END;
      tok = get_token();
    }
  }
  fclose(fp);
}

static
struct replacement_item **parse_string(tail, recog_attr)
     struct replacement_item **tail;
     int recog_attr;
{
  struct buffer buf;
  unsigned len;

  buffer_init(&buf);
  for (;;) {
    int c = get();
    if (c == '\"')
      break;
    if (recog_attr && c == '[') {
      if (buffer_length(&buf)) {
	len = buffer_length(&buf);
	tail = add_replacement_data(tail, buffer_extract(&buf), len);
      }
      scan_name(&buf, ']');
      tail = add_replacement_attr(tail, buffer_extract(&buf));
    }
    else {
      if (c == '\\') {
	c = get();
	switch (c) {
	case EOF:
	  parse_error("unfinished string at end of file");
	case 's':
	  buffer_append(&buf, ' ');
	  break;
	case 'n':
	  buffer_append(&buf, '\n');
	  break;
	case 't':
	  buffer_append(&buf, '\t');
	  break;
	case 'r':
	  buffer_append(&buf, '\r');
	  break;
	case 'f':
	  buffer_append(&buf, '\f');
	  break;
	case '0':
	case '1':
	case '2':
	case '3':
	case '4':
	case '5':
	case '6':
	case '7':
	  {
	    int val = c - '0';
	    c = peek();
	    if ('0' <= c && c <= '7') {
	      (void)get();
	      val = val*8 + (c - '0');
	      c = peek();
	      if ('0' <= c && c <= '7') {
		(void)get();
		val = val*8 + (c - '0');
	      }
	    }
	    buffer_append(&buf, val);
	    break;
	  }
	default:
	  buffer_append(&buf, c);
	  break;
	}
      }
      else
	buffer_append(&buf, c);
    }
  }
  len = buffer_length(&buf);
  if (len > 0)
    tail = add_replacement_data(tail, buffer_extract(&buf), len);
  return tail;
}

static
struct replacement_item **add_replacement_data(tail, buf, n)
     struct replacement_item **tail;
     char *buf;
     unsigned n;
{
  if (!tail)
    free(buf);
  else {
    *tail = NEW(struct replacement_item);
    (*tail)->type = DATA_REPL;
    (*tail)->u.data.n = n;
    (*tail)->next = 0;
    (*tail)->u.data.s = buf;
    tail = &(*tail)->next;
  }
  return tail;
}

static
struct replacement_item **add_replacement_attr(tail, name)
     struct replacement_item **tail;
     char *name;
{
  if (!tail)
    free(name);
  else {
    *tail = NEW(struct replacement_item);
    (*tail)->type = ATTR_REPL;
    (*tail)->next = 0;
    (*tail)->u.attr = name;
    tail = &(*tail)->next;
  }
  return tail;
}

static
int get_token()
{
  int c;

  for (;;) {
    c = get();
    while (isspace(c))
      c = get();
    if (c != '%')
      break;
    do {
      c = get();
      if (c == EOF)
	return EOF;
    } while (c != '\n');
  }
  switch (c) {
  case '+':
    return PLUS;
  case '<':
    c = peek();
    if (c == '/') {
      (void)get();
      return ETAGO;
    }
    return STAGO;
  case '"':
    return STRING;
  case EOF:
    return EOF;
  default:
    parse_error("bad input character `%c'", c);
  }
  return EOF;
}

static
void scan_name(buf, term)
     struct buffer *buf;
     int term;
{
  int c;
  for (;;) {
    c = get();
    if (c == term)
      break;
    if (c == '\n' || c == EOF)
      parse_error("missing `%c'", term);
    if (fold_general_names) {
      if (islower((unsigned char)c))
	c = toupper((unsigned char)c);
    }
    buffer_append(buf, c);
  }
  if (buffer_length(buf) == 0)
    parse_error("empty name");
  buffer_append(buf, '\0');
}

static
int get()
{
  int c = getc(fp);
  if (c == '\n')
    current_lineno++;
  return c;
}

static
int peek()
{
  int c = getc(fp);
  if (c != EOF)
    ungetc(c, fp);
  return c;
}

struct replacement *lookup_replacement(tablep, type, name)
     struct replacement_table *tablep;
     enum event_type type;
     char *name;
{
  int h = hash(type, name);
  struct table_entry *p;

  for (p = tablep->table[h]; p; p = p->next)
    if (strcmp(name, p->gi) == 0 && type == p->type)
      return &p->replacement;
  return 0;
}

/* Return 0 if already defined. */

static
struct replacement *define_replacement(tablep, type, name)
     struct replacement_table *tablep;
     enum event_type type;
     char *name;
{
  int h = hash(type, name);
  struct table_entry *p;

  for (p = tablep->table[h]; p; p = p->next)
    if (strcmp(name, p->gi) == 0 && type == p->type)
      return 0;
  p = NEW(struct table_entry);
  p->next = tablep->table[h];
  tablep->table[h] = p;
  p->type = type;
  p->gi = name;
  p->replacement.flags = 0;
  p->replacement.items = 0;
  return &p->replacement;
}

static
VOID buffer_init(buf)
     struct buffer *buf;
{
  buf->size = buf->len = 0;
  buf->s = 0;
}

static
char *buffer_extract(buf)
     struct buffer *buf;
{
  char *s = buf->s;
  buf->s = 0;
  buf->len = 0;
  buf->size = 0;
  return s;
}

#if 0
static
VOID buffer_free(buf)
     struct buffer *buf;
{
  if (buf->s) {
    free((UNIV)buf->s);
    buf->s = 0;
    buf->size = buf->size = 0;
  }
}
#endif

static
VOID buffer_append(buf, c)
     struct buffer *buf;
     int c;
{
  if (buf->len >= buf->size) {
    if (!buf->size)
      buf->s = (char *)xmalloc(buf->size = 10);
    else
      buf->s = (char *)xrealloc((UNIV)buf->s, buf->size *= 2);
  }
  buf->s[buf->len] = c;
  buf->len += 1;
}

static
int hash(type, s)
     enum event_type type;
     char *s;
{
  unsigned long h = 0, g;

  while (*s != 0) {
    h <<= 4;
    h += *s++;
    if ((g = h & 0xf0000000) != 0) {
      h ^= g >> 24;
      h ^= g;
    }
  }
  h ^= (int)type;
  return (int)(h % TABLE_SIZE);
}

static
UNIV xmalloc(n)
     unsigned n;
{
  UNIV p = (UNIV)malloc(n);
  if (!p)
    parse_error("out of memory");
  return p;
}

static
UNIV xrealloc(p, size)
     UNIV p;
     unsigned size;
{
  p = (UNIV)realloc(p, size);
  if (!p)
    parse_error("out of memory");
  return p;
}

static NO_RETURN
#ifdef VARARGS
void parse_error(va_alist) va_dcl
#else
void parse_error(char *message,...)
#endif
{
  char buf[512];
#ifdef VARARGS
  char *message;
#endif
  va_list ap;

#ifdef VARARGS
  va_start(ap);
  message = va_arg(ap, char *);
#else
  va_start(ap, message);
#endif
  vsprintf(buf, message, ap);
  va_end(ap);
  error("%s:%d: %s", current_file, current_lineno, buf);
}
OpenPOWER on IntegriCloud