~ubuntu-branches/ubuntu/feisty/mp4h/feisty

« back to all changes in this revision

Viewing changes to src/freeze.c

  • Committer: Bazaar Package Importer
  • Author(s): Denis Barbier
  • Date: 2003-11-06 20:34:24 UTC
  • Revision ID: james.westby@ubuntu.com-20031106203424-nqx14tkfeirx01r3
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* mp4h -- A macro processor for HTML documents
 
2
   Copyright 2000-2003, Denis Barbier
 
3
   All rights reserved.
 
4
 
 
5
   This program is free software; you can redistribute it and/or modify
 
6
   it under the terms of the GNU General Public License as published by
 
7
   the Free Software Foundation; either version 2, or (at your option)
 
8
   any later version.
 
9
 
 
10
   This program is a work based on GNU m4 version 1.4n. Below is the
 
11
   original copyright.
 
12
*/
 
13
/* GNU m4 -- A simple macro processor
 
14
   Copyright (C) 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
 
15
  
 
16
   This program is free software; you can redistribute it and/or modify
 
17
   it under the terms of the GNU General Public License as published by
 
18
   the Free Software Foundation; either version 2, or (at your option)
 
19
   any later version.
 
20
  
 
21
   This program is distributed in the hope that it will be useful,
 
22
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
23
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
24
   GNU General Public License for more details.
 
25
  
 
26
   You should have received a copy of the GNU General Public License
 
27
   along with this program; if not, write to the Free Software
 
28
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
29
*/
 
30
 
 
31
/* This module handles frozen files.  */
 
32
 
 
33
#include "mp4h.h"
 
34
 
 
35
static int lineno;
 
36
 
 
37
/*-------------------------------------------------------------------.
 
38
| Destructively reverse a symbol list and return the reversed list.  |
 
39
`-------------------------------------------------------------------*/
 
40
 
 
41
static symbol *
 
42
reverse_symbol_list (symbol *sym)
 
43
{
 
44
  symbol *result;
 
45
  symbol *next;
 
46
 
 
47
  result = NULL;
 
48
  while (sym)
 
49
    {
 
50
      next = SYMBOL_NEXT (sym);
 
51
      SYMBOL_NEXT (sym) = result;
 
52
      result = sym;
 
53
      sym = next;
 
54
    }
 
55
  return result;
 
56
}
 
57
 
 
58
/*------------------------------------------------.
 
59
| Produce a frozen state to the given file NAME.  |
 
60
`------------------------------------------------*/
 
61
 
 
62
void
 
63
produce_frozen_state (const char *name)
 
64
{
 
65
  FILE *file;
 
66
  int h;
 
67
  symbol *sym;
 
68
  const builtin *bp;
 
69
  int number[2];
 
70
 
 
71
  if (file = fopen (name, "w"), !file)
 
72
    {
 
73
      MP4HERROR ((warning_status, errno, name));
 
74
      return;
 
75
    }
 
76
 
 
77
  /* Write a recognizable header.  */
 
78
 
 
79
  fprintf (file, "# This is a frozen state file generated by %s %s\n",
 
80
           PACKAGE, VERSION);
 
81
  fprintf (file, "V1\n");
 
82
 
 
83
  /* Dump comment delimiters.  */
 
84
  if (strcmp (eolcomm.string, DEF_EOLCOMM))
 
85
    {
 
86
      fprintf (file, "C%d\n", (int) eolcomm.length);
 
87
      fputs (eolcomm.string, file);
 
88
      fputc ('\n', file);
 
89
    }
 
90
 
 
91
 
 
92
  /* Dump all symbols.  */
 
93
 
 
94
  for (h = 0; h < hash_table_size; h++)
 
95
    {
 
96
 
 
97
      /* Process all entries in one bucket, from the last to the first.
 
98
         This order ensures that, at reload time, pushdef's will be
 
99
         executed with the oldest definitions first.  */
 
100
 
 
101
      symtab[h] = reverse_symbol_list (symtab[h]);
 
102
      for (sym = symtab[h]; sym; sym = SYMBOL_NEXT (sym))
 
103
        {
 
104
          switch (SYMBOL_TYPE (sym))
 
105
            {
 
106
            case TOKEN_TEXT:
 
107
              if (*SYMBOL_NAME (sym) == '<')
 
108
                {
 
109
                  fprintf (file, "A%d,%d\n",
 
110
                       (int) strlen (SYMBOL_NAME (sym)) - 1,
 
111
                       (int) strlen (SYMBOL_TEXT (sym)));
 
112
                  fputs (SYMBOL_NAME (sym) + 1, file);
 
113
                }
 
114
              else
 
115
                {
 
116
                  fprintf (file, "T%d,%d\n",
 
117
                       (int) strlen (SYMBOL_NAME (sym)),
 
118
                       (int) strlen (SYMBOL_TEXT (sym)));
 
119
                  fputs (SYMBOL_NAME (sym), file);
 
120
                }
 
121
              fputs (SYMBOL_TEXT (sym), file);
 
122
              fputc ('\n', file);
 
123
              if (*SYMBOL_NAME (sym) != '<')
 
124
                {
 
125
                  fputc (SYMBOL_CONTAINER (sym) ? '1' : '0', file);
 
126
                  fputc (SYMBOL_EXPAND_ARGS (sym) ? '1' : '0', file);
 
127
                  fputc ('\n', file);
 
128
                  number[0] = (SYMBOL_HOOK_BEGIN (sym) ?
 
129
                          strlen (SYMBOL_HOOK_BEGIN (sym)) : 0);
 
130
                  number[1] = (SYMBOL_HOOK_END (sym) ?
 
131
                          strlen (SYMBOL_HOOK_END (sym)) : 0);
 
132
                  fprintf (file, "%d,%d\n", number[0], number[1]);
 
133
                  if (SYMBOL_HOOK_BEGIN (sym))
 
134
                    fputs (SYMBOL_HOOK_BEGIN (sym), file);
 
135
                  if (SYMBOL_HOOK_END (sym))
 
136
                    fputs (SYMBOL_HOOK_END (sym), file);
 
137
                  fputc ('\n', file);
 
138
                }
 
139
              break;
 
140
 
 
141
            case TOKEN_FUNC:
 
142
              bp = find_builtin_by_addr (SYMBOL_FUNC (sym));
 
143
              if (bp == NULL)
 
144
                {
 
145
                  MP4HERROR ((warning_status, 0, "\
 
146
INTERNAL ERROR: Built-in not found in builtin table!"));
 
147
                  exit (1);
 
148
                }
 
149
              fprintf (file, "F%d,%d\n",
 
150
                       (int) strlen (SYMBOL_NAME (sym)),
 
151
                       (int) strlen (bp->name));
 
152
              fputs (SYMBOL_NAME (sym), file);
 
153
              fputs (bp->name, file);
 
154
              fputc ('\n', file);
 
155
              break;
 
156
 
 
157
            default:
 
158
              MP4HERROR ((warning_status, 0, "\
 
159
INTERNAL ERROR: Bad token data type in freeze_one_symbol ()"));
 
160
              exit (1);
 
161
              break;
 
162
            }
 
163
        }
 
164
 
 
165
      /* Reverse the bucket once more, putting it back as it was.  */
 
166
 
 
167
      symtab[h] = reverse_symbol_list (symtab[h]);
 
168
    }
 
169
 
 
170
  /* All done.  */
 
171
 
 
172
  fputs ("# End of frozen state file\n", file);
 
173
  fclose (file);
 
174
}
 
175
 
 
176
/*----------------------------------------------------------------------.
 
177
| Issue a message saying that some character is an EXPECTED character.  |
 
178
`----------------------------------------------------------------------*/
 
179
 
 
180
static void
 
181
issue_expect_message (int expected)
 
182
{
 
183
  if (expected == '\n')
 
184
    MP4HERROR ((EXIT_FAILURE, 0, _("%d: Expecting line feed in frozen file"),
 
185
                lineno));
 
186
  else
 
187
    MP4HERROR ((EXIT_FAILURE, 0, _("%d: Expecting character `%c' in frozen file"),
 
188
                lineno, expected));
 
189
}
 
190
 
 
191
/*-------------------------------------------------.
 
192
| Reload a frozen state from the given file NAME.  |
 
193
`-------------------------------------------------*/
 
194
 
 
195
/* We are seeking speed, here.  */
 
196
 
 
197
#define GET_CHARACTER \
 
198
  (character = getc (file))
 
199
 
 
200
#define GET_NUMBER(Number) \
 
201
  do                                                            \
 
202
    {                                                           \
 
203
      (Number) = 0;                                             \
 
204
      while (isdigit (character))                               \
 
205
        {                                                       \
 
206
          (Number) = 10 * (Number) + character - '0';           \
 
207
          GET_CHARACTER;                                        \
 
208
        }                                                       \
 
209
    }                                                           \
 
210
  while (0)
 
211
 
 
212
#define VALIDATE(Expected) \
 
213
  do                                                            \
 
214
    {                                                           \
 
215
      if (character != (Expected))                              \
 
216
        issue_expect_message ((Expected));                      \
 
217
    }                                                           \
 
218
  while (0)
 
219
 
 
220
void
 
221
reload_frozen_state (const char *name)
 
222
{
 
223
  FILE *file;
 
224
  int character;
 
225
  int operation;
 
226
  char *string[2];
 
227
  int allocated[2];
 
228
  int number[2];
 
229
  const builtin *bp;
 
230
  symbol *sym, *var;
 
231
  boolean container, expand_args;
 
232
 
 
233
  file = path_search (name, (char **)NULL);
 
234
  if (file == NULL)
 
235
    MP4HERROR ((EXIT_FAILURE, errno, _("Cannot open %s"), name));
 
236
  lineno = 1;
 
237
 
 
238
  allocated[0] = 100;
 
239
  string[0] = xmalloc ((size_t) allocated[0]);
 
240
  allocated[1] = 100;
 
241
  string[1] = xmalloc ((size_t) allocated[1]);
 
242
 
 
243
  while (GET_CHARACTER, character != EOF)
 
244
    {
 
245
    switch (character)
 
246
      {
 
247
      default:
 
248
        MP4HERROR ((EXIT_FAILURE, 0, _("Ill-formated frozen file")));
 
249
 
 
250
      case '\n':
 
251
 
 
252
        /* Skip empty lines.  */
 
253
 
 
254
        lineno++;
 
255
        break;
 
256
 
 
257
      case '#':
 
258
 
 
259
        /* Comments are introduced by `#' at beginning of line, and are
 
260
           ignored.  */
 
261
 
 
262
        while (character != EOF && character != '\n')
 
263
          GET_CHARACTER;
 
264
        VALIDATE ('\n');
 
265
        lineno++;
 
266
        break;
 
267
 
 
268
      case 'C':
 
269
 
 
270
        /* Change comment strings.  */
 
271
 
 
272
        GET_CHARACTER;
 
273
        GET_NUMBER (number[0]);
 
274
        GET_CHARACTER;
 
275
        VALIDATE ('\n');
 
276
        lineno++;
 
277
        if (number[0] + 1 > allocated[0])
 
278
          {
 
279
            free (string[0]);
 
280
            allocated[0] = number[0] + 1;
 
281
            string[0] = xmalloc ((size_t) allocated[0]);
 
282
          }
 
283
 
 
284
        if (number[0] > 0)
 
285
          if (!fread (string[0], (size_t) number[0], 1, file))
 
286
            MP4HERROR ((EXIT_FAILURE, 0, _("Premature end of frozen file")));
 
287
 
 
288
        string[0][number[0]] = '\0';
 
289
        GET_CHARACTER;
 
290
        VALIDATE ('\n');
 
291
        lineno++;
 
292
 
 
293
        eolcomm.string = string[0];
 
294
        eolcomm.length = strlen (eolcomm.string);
 
295
        break;
 
296
 
 
297
      case 'A':
 
298
      case 'F':
 
299
      case 'T':
 
300
        operation = character;
 
301
        GET_CHARACTER;
 
302
 
 
303
        /* Get string lengths.  Accept a negative diversion number.  */
 
304
 
 
305
        number[1] = 0;
 
306
        GET_NUMBER (number[0]);
 
307
        VALIDATE (',');
 
308
        GET_CHARACTER;
 
309
        GET_NUMBER (number[1]);
 
310
        VALIDATE ('\n');
 
311
        lineno++;
 
312
 
 
313
        /* Get first string contents.  */
 
314
 
 
315
        if (number[0] + 1 > allocated[0])
 
316
          {
 
317
            free (string[0]);
 
318
            allocated[0] = number[0] + 1;
 
319
            string[0] = xmalloc ((size_t) allocated[0]);
 
320
          }
 
321
 
 
322
        if (number[0] > 0)
 
323
          if (!fread (string[0], (size_t) number[0], 1, file))
 
324
            MP4HERROR ((EXIT_FAILURE, 0, _("Premature end of frozen file")));
 
325
 
 
326
        string[0][number[0]] = '\0';
 
327
 
 
328
        /* Get second string contents.  */
 
329
 
 
330
        if (number[1] + 1 > allocated[1])
 
331
          {
 
332
            free (string[1]);
 
333
            allocated[1] = number[1] + 1;
 
334
            string[1] = xmalloc ((size_t) allocated[1]);
 
335
          }
 
336
 
 
337
        if (number[1] > 0)
 
338
          if (!fread (string[1], (size_t) number[1], 1, file))
 
339
            MP4HERROR ((EXIT_FAILURE, 0, _("Premature end of frozen file")));
 
340
 
 
341
        string[1][number[1]] = '\0';
 
342
 
 
343
        GET_CHARACTER;
 
344
        VALIDATE ('\n');
 
345
        lineno++;
 
346
      
 
347
        /* Act according to operation letter.  */
 
348
 
 
349
        switch (operation)
 
350
          {
 
351
          case 'A':
 
352
 
 
353
            /* Define a variable.  */
 
354
 
 
355
            var = lookup_variable (string[0], SYMBOL_INSERT);
 
356
            SYMBOL_TYPE (var) = TOKEN_TEXT;
 
357
            SYMBOL_TEXT (var) = xstrdup (string[0]);
 
358
            break;
 
359
 
 
360
          case 'F':
 
361
 
 
362
            /* Enter a macro having a builtin function as a definition.  */
 
363
 
 
364
            bp = find_builtin_by_name (string[1]);
 
365
            if (bp)
 
366
              define_builtin (string[0], bp, 0);
 
367
            else
 
368
              MP4HERROR ((warning_status, 0, _("\
 
369
`%s' from frozen file not found in builtin table!"),
 
370
                        string[0]));
 
371
            break;
 
372
 
 
373
          case 'T':
 
374
 
 
375
            GET_CHARACTER;
 
376
            container = (character == '1');
 
377
            GET_CHARACTER;
 
378
            expand_args = (character == '1');
 
379
            GET_CHARACTER;
 
380
            VALIDATE ('\n');
 
381
            lineno++;
 
382
 
 
383
            /* Enter a macro having an expansion text as a definition.  */
 
384
 
 
385
            define_user_macro (string[0], string[1], SYMBOL_INSERT,
 
386
                    container, expand_args, FALSE);
 
387
 
 
388
            sym = lookup_symbol (string[0], SYMBOL_LOOKUP);
 
389
 
 
390
            /* Add hooks.  */
 
391
 
 
392
            GET_CHARACTER;
 
393
            GET_NUMBER (number[0]);
 
394
            VALIDATE (',');
 
395
            GET_CHARACTER;
 
396
            GET_NUMBER (number[1]);
 
397
            VALIDATE ('\n');
 
398
            lineno++;
 
399
 
 
400
            if (number[0] > 0)
 
401
              {
 
402
                if (number[0] + 1 > allocated[0])
 
403
                  {
 
404
                    free (string[0]);
 
405
                    allocated[0] = number[0] + 1;
 
406
                    string[0] = xmalloc ((size_t) allocated[0]);
 
407
                  }
 
408
                if (!fread (string[0], (size_t) number[0], 1, file))
 
409
                  MP4HERROR ((EXIT_FAILURE, 0, _("Premature end of frozen file")));
 
410
 
 
411
                string[0][number[0]] = '\0';
 
412
 
 
413
                SYMBOL_HOOK_BEGIN (sym) = xstrdup (string[0]);
 
414
 
 
415
              }
 
416
 
 
417
            if (number[1] > 0)
 
418
              {
 
419
                if (number[1] + 1 > allocated[1])
 
420
                  {
 
421
                    free (string[1]);
 
422
                    allocated[1] = number[1] + 1;
 
423
                    string[1] = xmalloc ((size_t) allocated[1]);
 
424
                  }
 
425
                if (!fread (string[1], (size_t) number[1], 1, file))
 
426
                  MP4HERROR ((EXIT_FAILURE, 0, _("Premature end of frozen file")));
 
427
 
 
428
                string[1][number[1]] = '\0';
 
429
 
 
430
                SYMBOL_HOOK_END (sym) = xstrdup (string[1]);
 
431
 
 
432
              }
 
433
            GET_CHARACTER;
 
434
            VALIDATE ('\n');
 
435
            lineno++;
 
436
 
 
437
            break;
 
438
 
 
439
          default:
 
440
 
 
441
            /* Cannot happen.  */
 
442
 
 
443
            break;
 
444
          }
 
445
        break;
 
446
        
 
447
      case 'V':
 
448
 
 
449
        /* Validate format version.  Only `1' is acceptable for now.  */
 
450
 
 
451
        GET_CHARACTER;
 
452
        VALIDATE ('1');
 
453
        GET_CHARACTER;
 
454
        VALIDATE ('\n');
 
455
        lineno++;
 
456
        break;
 
457
 
 
458
      }
 
459
    }
 
460
 
 
461
  free (string[0]);
 
462
  free (string[1]);
 
463
  fclose (file);
 
464
 
 
465
#undef GET_CHARACTER
 
466
#undef GET_NUMBER
 
467
#undef VALIDATE
 
468
}