summaryrefslogtreecommitdiff
path: root/src/freeze.c
blob: 8298317baea1d320a6015cd35c9e91d988776628 (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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* GNU m4 -- A simple macro processor
   Copyright (C) 1989-1994, 2004-2010, 2013-2014 Free Software
   Foundation, Inc.

   This file is part of GNU M4.

   GNU M4 is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   GNU M4 is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/* This module handles frozen files.  */

#include <config.h>

#include "m4.h"

#include "binary-io.h"
#include "close-stream.h"
#include "quotearg.h"
#include "verify.h"
#include "xmemdup0.h"

static  void  produce_mem_dump          (FILE *, const char *, size_t);
static  void  produce_resyntax_dump     (m4 *, FILE *);
static  void  produce_syntax_dump       (FILE *, m4_syntax_table *, char);
static  void  produce_module_dump       (m4 *, FILE *, m4_module *);
static  void  produce_symbol_dump       (m4 *, FILE *, m4_symbol_table *);
static  void *dump_symbol_CB            (m4_symbol_table *, const char *,
                                         size_t, m4_symbol *, void *);
static  void  issue_expect_message      (m4 *, int);
static  int   decode_char               (m4 *, FILE *, bool *);


/* Dump an ASCII-encoded representation of LEN bytes at MEM to FILE.
   MEM may contain embedded NUL characters.  */
static void
produce_mem_dump (FILE *file, const char *mem, size_t len)
{
  char *quoted = quotearg_style_mem (escape_quoting_style, mem, len);
  /* Any errors will be detected by ferror later.  */
  fwrite (quoted, strlen (quoted), 1, file);
}


/* Produce the 'R14\nPOSIX_EXTENDED\n' frozen file dump of the current
   default regular expression syntax.  Note that it would be a little
   faster to use the encoded syntax in this format as used by re_compile(),
   but the representation of RE_SYNTAX_POSIX_EXTENDED may change in
   future (or alternative) implementations of re_compile, so we use an
   unencoded representation here.  */

static void
produce_resyntax_dump (m4 *context, FILE *file)
{
  int code = m4_get_regexp_syntax_opt (context);

  /* Don't dump default syntax code (`0' for GNU_EMACS).  */
  if (code)
    {
      const char *resyntax = m4_regexp_syntax_decode (code);

      if (!resyntax)
        m4_error (context, EXIT_FAILURE, 0, NULL,
                  _("invalid regexp syntax code `%d'"), code);

      /* No need to use produce_mem_dump, since we know all resyntax
         names are already ASCII-encoded.  */
      xfprintf (file, "R%zu\n%s\n", strlen (resyntax), resyntax);
    }
}

static void
produce_syntax_dump (FILE *file, m4_syntax_table *syntax, char ch)
{
  char buf[UCHAR_MAX + 1];
  int code = m4_syntax_code (ch);
  int count = 0;
  int i;

  for (i = 0; i < UCHAR_MAX + 1; ++i)
    if (m4_has_syntax (syntax, i, code) && code != syntax->orig[i])
      buf[count++] = i;

  /* If code falls in M4_SYNTAX_MASKS, then we must treat it
     specially, since it will not be found in syntax->orig.  */
  if (count == 1
      && ((code == M4_SYNTAX_RQUOTE && *buf == *DEF_RQUOTE)
          || (code == M4_SYNTAX_ECOMM && *buf == *DEF_ECOMM)))
    return;

  if (count || (code & M4_SYNTAX_MASKS))
    {
      xfprintf (file, "S%c%d\n", ch, count);
      produce_mem_dump (file, buf, count);
      fputc ('\n', file);
    }
}

/* Store the debug mode in textual format.  */
static void
produce_debugmode_state (FILE *file, int flags)
{
  /* This code tracks the number of bits in M4_DEBUG_TRACE_VERBOSE.  */
  char str[15];
  int offset = 0;
  verify ((1 << (sizeof str - 1)) - 1 == M4_DEBUG_TRACE_VERBOSE);
  if (flags & M4_DEBUG_TRACE_ARGS)
    str[offset++] = 'a';
  if (flags & M4_DEBUG_TRACE_EXPANSION)
    str[offset++] = 'e';
  if (flags & M4_DEBUG_TRACE_QUOTE)
    str[offset++] = 'q';
  if (flags & M4_DEBUG_TRACE_ALL)
    str[offset++] = 't';
  if (flags & M4_DEBUG_TRACE_LINE)
    str[offset++] = 'l';
  if (flags & M4_DEBUG_TRACE_FILE)
    str[offset++] = 'f';
  if (flags & M4_DEBUG_TRACE_PATH)
    str[offset++] = 'p';
  if (flags & M4_DEBUG_TRACE_CALL)
    str[offset++] = 'c';
  if (flags & M4_DEBUG_TRACE_INPUT)
    str[offset++] = 'i';
  if (flags & M4_DEBUG_TRACE_CALLID)
    str[offset++] = 'x';
  if (flags & M4_DEBUG_TRACE_MODULE)
    str[offset++] = 'm';
  if (flags & M4_DEBUG_TRACE_STACK)
    str[offset++] = 's';
  if (flags & M4_DEBUG_TRACE_DEREF)
    str[offset++] = 'd';
  if (flags & M4_DEBUG_TRACE_OUTPUT_DUMPDEF)
    str[offset++] = 'o';
  str[offset] = '\0';
  if (offset)
    xfprintf (file, "d%d\n%s\n", offset, str);
}

/* The modules must be dumped in the order in which they will be
   reloaded from the frozen file.  We store handles in a push
   down stack, so we need to dump them in the reverse order to that.  */
static void
produce_module_dump (m4 *context, FILE *file, m4_module *module)
{
  const char *name = m4_get_module_name (module);
  size_t len = strlen (name);

  module = m4_module_next (context, module);
  if (module)
    produce_module_dump (context, file, module);

  xfprintf (file, "M%zu\n", len);
  produce_mem_dump (file, name, len);
  fputc ('\n', file);
}

/* Process all entries in one bucket, from the last to the first.
   This order ensures that, at reload time, pushdef's will be
   executed with the oldest definitions first.  */
static void
produce_symbol_dump (m4 *context, FILE *file, m4_symbol_table *symtab)
{
  if (m4_symtab_apply (symtab, true, dump_symbol_CB, file))
    assert (false);
}

/* Given a stack of symbol values starting with VALUE, destructively
   reverse the stack and return the pointer to what was previously the
   last value in the stack.  VALUE may be NULL.  The symbol table that
   owns the value stack should not be modified or consulted until this
   is called again to undo the effect.  */
static m4_symbol_value *
reverse_symbol_value_stack (m4_symbol_value *value)
{
  m4_symbol_value *result = NULL;
  m4_symbol_value *next;
  while (value)
    {
      next = VALUE_NEXT (value);
      VALUE_NEXT (value) = result;
      result = value;
      value = next;
    }
  return result;
}

/* Dump the stack of values for SYMBOL, with name SYMBOL_NAME and
   length LEN, located in SYMTAB.  USERDATA is interpreted as the
   FILE* to dump to.  */
static void *
dump_symbol_CB (m4_symbol_table *symtab, const char *symbol_name, size_t len,
                m4_symbol *symbol, void *userdata)
{
  FILE *file = (FILE *) userdata;
  m4_symbol_value *value;
  m4_symbol_value *last;

  last = value = reverse_symbol_value_stack (m4_get_symbol_value (symbol));
  while (value)
    {
      m4_module *module = VALUE_MODULE (value);
      const char *module_name = module ? m4_get_module_name (module) : NULL;
      size_t module_len = module_name ? strlen (module_name) : 0;

      if (m4_is_symbol_value_text (value))
        {
          const char *text = m4_get_symbol_value_text (value);
          size_t text_len = m4_get_symbol_value_len (value);
          xfprintf (file, "T%zu,%zu", len, text_len);
          if (module)
            xfprintf (file, ",%zu", module_len);
          fputc ('\n', file);

          produce_mem_dump (file, symbol_name, len);
          fputc ('\n', file);
          produce_mem_dump (file, text, text_len);
          fputc ('\n', file);
          if (module)
            {
              produce_mem_dump (file, module_name, module_len);
              fputc ('\n', file);
            }
        }
      else if (m4_is_symbol_value_func (value))
        {
          const m4_builtin *bp = m4_get_symbol_value_builtin (value);
          size_t bp_len;
          if (bp == NULL)
            assert (!"INTERNAL ERROR: builtin not found in builtin table!");
          bp_len = strlen (bp->name);

          xfprintf (file, "F%zu,%zu", len, bp_len);
          if (module)
            xfprintf (file, ",%zu", module_len);
          fputc ('\n', file);

          produce_mem_dump (file, symbol_name, len);
          fputc ('\n', file);
          produce_mem_dump (file, bp->name, bp_len);
          fputc ('\n', file);
          if (module)
            {
              produce_mem_dump (file, module_name, module_len);
              fputc ('\n', file);
            }
        }
      else if (m4_is_symbol_value_placeholder (value))
        ; /* Nothing to do for a builtin we couldn't reload earlier.  */
      else
        assert (!"dump_symbol_CB");
      value = VALUE_NEXT (value);
    }
  reverse_symbol_value_stack (last);
  if (m4_get_symbol_traced (symbol))
    xfprintf (file, "t%zu\n%s\n", len, symbol_name);
  return NULL;
}

/* Produce a frozen state to the given file NAME. */
void
produce_frozen_state (m4 *context, const char *name)
{
  FILE *file = fopen (name, O_BINARY ? "wb" : "w");
  const char *str;
  const m4_string_pair *pair;

  if (!file)
    {
      m4_error (context, 0, errno, NULL, _("cannot open %s"),
                quotearg_style (locale_quoting_style, name));
      return;
    }

  /* Write a recognizable header.  */

  xfprintf (file, "# This is a frozen state file generated by GNU %s %s\n",
            PACKAGE, VERSION);
  fputs ("V2\n", file);

  /* Dump quote delimiters.  */
  pair = m4_get_syntax_quotes (M4SYNTAX);
  if (STRNEQ (pair->str1, DEF_LQUOTE) || STRNEQ (pair->str2, DEF_RQUOTE))
    {
      xfprintf (file, "Q%zu,%zu\n", pair->len1, pair->len2);
      produce_mem_dump (file, pair->str1, pair->len1);
      fputc ('\n', file);
      produce_mem_dump (file, pair->str2, pair->len2);
      fputc ('\n', file);
    }

  /* Dump comment delimiters.  */
  pair = m4_get_syntax_comments (M4SYNTAX);
  if (STRNEQ (pair->str1, DEF_BCOMM) || STRNEQ (pair->str2, DEF_ECOMM))
    {
      xfprintf (file, "C%zu,%zu\n", pair->len1, pair->len2);
      produce_mem_dump (file, pair->str1, pair->len1);
      fputc ('\n', file);
      produce_mem_dump (file, pair->str2, pair->len2);
      fputc ('\n', file);
    }

  /* Dump regular expression syntax.  */
  produce_resyntax_dump (context, file);

  /* Dump syntax table.  */
  str = "I@WLBOD${}SA(),RE";
  while (*str)
    produce_syntax_dump (file, M4SYNTAX, *str++);

  /* Dump debugmode state.  */
  produce_debugmode_state (file, m4_get_debug_level_opt (context));

  /* Dump all loaded modules.  */
  produce_module_dump (context, file, m4_module_next (context, NULL));

  /* Dump all symbols.  */
  produce_symbol_dump (context, file, M4SYMTAB);

  /* Let diversions be issued from output.c module, its cleaner to have this
     piece of code there.  */
  m4_freeze_diversions (context, file);

  /* All done.  */

  fputs ("# End of frozen state file\n", file);
  if (close_stream (file) != 0)
    m4_error (context, EXIT_FAILURE, errno, NULL,
              _("unable to create frozen state"));
}

/* Issue a message saying that some character is an EXPECTED character. */
static void
issue_expect_message (m4 *context, int expected)
{
  if (expected == '\n')
    m4_error (context, EXIT_FAILURE, 0, NULL,
              _("expecting line feed in frozen file"));
  else
    m4_error (context, EXIT_FAILURE, 0, NULL,
              _("expecting character `%c' in frozen file"), expected);
}


/* Reload frozen state.  */

/* Read the next character from the IN stream.  Various escape
   sequences are converted, and returned.  EOF is returned if the end
   of file is reached whilst reading the character, or on an
   unrecognized escape sequence.  */

static int
decode_char (m4 *context, FILE *in, bool *advance_line)
{
  int ch = getc (in);
  int next;
  int value = 0;

  if (*advance_line)
    {
      m4_set_current_line (context, m4_get_current_line (context) + 1);
      *advance_line = false;
    }

  while (ch == '\\')
    {
      ch = getc (in);
      switch (ch)
        {
        case 'a': return '\a';
        case 'b': return '\b';
        case 'f': return '\f';
        case 'n': return '\n';
        case 'r': return '\r';
        case 't': return '\t';
        case 'v': return '\v';
        case '\\': return '\\';

        case '\n':
          ch = getc (in);
          m4_set_current_line (context, m4_get_current_line (context) + 1);
          continue;

        case 'x': case 'X':
          next = getc (in);
          if (next >= '0' && next <= '9')
            ch = (next - '0') * 16;
          else if (next >= 'a' && next <= 'f')
            ch = (next - 'a' + 10) * 16;
          else if (next >= 'A' && next <= 'F')
            ch = (next - 'A' + 10) * 16;
          else
            return EOF;
          next = getc (in);
          if (next >= '0' && next <= '9')
            ch += next - '0';
          else if (next >= 'a' && next <= 'f')
            ch += next - 'a' + 10;
          else if (next >= 'A' && next <= 'F')
            ch += next - 'A' + 10;
          else
            return EOF;
          return ch;
        case '0': case '1': case '2': case '3':
          value = ch - '0';
          ch = getc (in);
          /* fall through */
        case '4': case '5': case '6': case '7':
          if (ch >= '0' && ch <= '7')
            {
              value = value * 8 + ch - '0';
              ch = getc (in);
            }
          else
            {
              ungetc (ch, in);
              return value;
            }
          if (ch >= '0' && ch <= '7')
            value = value * 8 + ch - '0';
          else
            ungetc (ch, in);
          return value;

        default:
          return EOF;
        }
    }

  if (ch == '\n')
    *advance_line = true;
  return ch;
}


/*  Reload state from the given file NAME.  We are seeking speed,
    here.  */

void
reload_frozen_state (m4 *context, const char *name)
{
  FILE *file = NULL;
  char *filepath;
  int version;
  int character;
  int operation;
  char syntax;
  char *string[3];
  size_t allocated[3];
  int number[3] = {0};
  bool advance_line = true;

#define GET_CHARACTER                                                   \
  do                                                                    \
    {                                                                   \
      if (advance_line)                                                 \
        {                                                               \
          m4_set_current_line (context,                                 \
                               m4_get_current_line (context) + 1);      \
          advance_line = false;                                         \
        }                                                               \
      character = getc (file);                                          \
      if (character == '\n')                                            \
        advance_line = true;                                            \
    }                                                                   \
  while (0)

#define GET_NUMBER(Number, AllowNeg)                            \
  do                                                            \
    {                                                           \
      unsigned int n = 0;                                       \
      while (isdigit (character) && n <= INT_MAX / 10)          \
        {                                                       \
          n = 10 * n + character - '0';                         \
          GET_CHARACTER;                                        \
        }                                                       \
      if (((AllowNeg) ? INT_MIN: INT_MAX) < n                   \
          || isdigit (character))                               \
        m4_error (context, EXIT_FAILURE, 0, NULL,               \
                  _("integer overflow in frozen file"));        \
      (Number) = n;                                             \
    }                                                           \
  while (0)

#define GET_STRING(File, Buf, BufSize, StrLen, UseChar)         \
  do                                                            \
    {                                                           \
      size_t len = (StrLen);                                    \
      char *p;                                                  \
      int ch;                                                   \
      if (UseChar)                                              \
        {                                                       \
          ungetc (character, File);                             \
          if (advance_line)                                     \
            {                                                   \
              assert (character == '\n');                       \
              advance_line = false;                             \
            }                                                   \
        }                                                       \
      CHECK_ALLOCATION ((Buf), (BufSize), len);                 \
      p = (Buf);                                                \
      while (len-- > 0)                                         \
        {                                                       \
          ch = (version > 1                                     \
                ? decode_char (context, File, &advance_line)    \
                : getc (File));                                 \
          if (ch == EOF)                                        \
            m4_error (context, EXIT_FAILURE, 0, NULL,           \
                      _("premature end of frozen file"));       \
          *p++ = ch;                                            \
        }                                                       \
      *p = '\0';                                                \
      GET_CHARACTER;                                            \
      while (version > 1 && character == '\\')                  \
        {                                                       \
          GET_CHARACTER;                                        \
          VALIDATE ('\n');                                      \
          GET_CHARACTER;                                        \
        }                                                       \
    }                                                           \
  while (0)

#define VALIDATE(Expected)                                      \
  do                                                            \
    {                                                           \
      if (character != (Expected))                              \
        issue_expect_message (context, (Expected));             \
    }                                                           \
  while (0)

#define CHECK_ALLOCATION(Where, Allocated, Needed)              \
  do                                                            \
    {                                                           \
      if ((Needed) + 1 > (Allocated))                           \
        {                                                       \
          free (Where);                                         \
          (Allocated) = (Needed) + 1;                           \
          (Where) = xcharalloc (Allocated);                     \
        }                                                       \
    }                                                           \
  while (0)

  /* Skip comments (`#' at beginning of line) and blank lines, setting
     character to the next directive or to EOF.  */

#define GET_DIRECTIVE                                           \
  do                                                            \
    {                                                           \
      GET_CHARACTER;                                            \
      if (character == '#')                                     \
        {                                                       \
          while (character != EOF && character != '\n')         \
            GET_CHARACTER;                                      \
          VALIDATE ('\n');                                      \
        }                                                       \
    }                                                           \
  while (character == '\n')

  filepath = m4_path_search (context, name, NULL);
  file = m4_fopen (context, filepath, "r");
  if (file == NULL)
    m4_error (context, EXIT_FAILURE, errno, NULL, _("cannot open %s"),
              quotearg_style (locale_quoting_style, name));
  m4_set_current_file (context, name);

  allocated[0] = 100;
  string[0] = xcharalloc (allocated[0]);
  allocated[1] = 100;
  string[1] = xcharalloc (allocated[1]);
  allocated[2] = 100;
  string[2] = xcharalloc (allocated[2]);

  /* Validate format version.  Accept both `1' (m4 1.3 and 1.4.x) and
     `2' (m4 2.0).  */
  GET_DIRECTIVE;
  VALIDATE ('V');
  GET_CHARACTER;
  GET_NUMBER (version, false);
  switch (version)
    {
    case 2:
      break;
    case 1:
      m4__module_open (context, "m4", NULL);
      if (m4_get_posixly_correct_opt (context))
        m4__module_open (context, "traditional", NULL);
      else
        m4__module_open (context, "gnu", NULL);
      /* Disable { and } categories, since ${11} was not supported in
         1.4.x.  */
      m4_set_syntax (M4SYNTAX, 'O', '+', "{}", 2);
      break;
    default:
      if (version > 2)
        m4_error (context, EXIT_MISMATCH, 0, NULL,
                  _("frozen file version %d greater than max supported of 2"),
                  version);
      else
        m4_error (context, EXIT_FAILURE, 0, NULL,
                  _("ill-formed frozen file, version directive expected"));
    }
  VALIDATE ('\n');

  GET_DIRECTIVE;
  while (character != EOF)
    {
      switch (character)
        {
        default:
          m4_error (context, EXIT_FAILURE, 0, NULL,
                    _("ill-formed frozen file, unknown directive %c"),
                    character);

        case 'd':
          /* Set debugmode flags.  */
          if (version < 2)
            {
              /* 'd' operator is not supported in format version 1. */
              m4_error (context, EXIT_FAILURE, 0, NULL, _("\
ill-formed frozen file, version 2 directive `%c' encountered"), 'd');
            }

          GET_CHARACTER;
          GET_NUMBER (number[0], false);
          VALIDATE ('\n');
          GET_STRING (file, string[0], allocated[0], number[0], false);
          VALIDATE ('\n');

          if (m4_debug_decode (context, string[0], number[0]) < 0)
            m4_error (context, EXIT_FAILURE, 0, NULL,
                      _("unknown debug mode %s"),
                      quotearg_style_mem (locale_quoting_style, string[0],
                                          number[0]));
          break;

        case 'F':
          GET_CHARACTER;

          /* Get string lengths. */

          GET_NUMBER (number[0], false);
          VALIDATE (',');
          GET_CHARACTER;
          GET_NUMBER (number[1], false);

          if (character == ',')
            {
              if (version > 1)
                {
                  /* 'F' operator accepts an optional third argument for
                     format versions 2 or later.  */
                  GET_CHARACTER;
                  GET_NUMBER (number[2], false);
                }
              else
                /* 3 argument 'F' operations are invalid for format
                   version 1.  */
                m4_error (context, EXIT_FAILURE, 0, NULL, _("\
ill-formed frozen file, version 2 directive `%c' encountered"), 'F');
            }
          else
            {
              number[2] = 0;
            }

          VALIDATE ('\n');


          /* Get string contents.  */

          GET_STRING (file, string[0], allocated[0], number[0], false);
          if (version > 1)
            {
              VALIDATE ('\n');
              GET_CHARACTER;
            }
          GET_STRING (file, string[1], allocated[1], number[1], true);
          if (version > 1 && number[2])
            {
              VALIDATE ('\n');
              GET_CHARACTER;
            }
          GET_STRING (file, string[2], allocated[2], number[2], true);
          VALIDATE ('\n');

          /* Enter a macro having a builtin function as a definition.  */
          {
            m4_module *module = NULL;
            m4_symbol_value *token;

            // Builtins cannot contain a NUL byte.
            if (strlen (string[1]) < number[1])
              m4_error (context, EXIT_FAILURE, 0, NULL, _("\
ill-formed frozen file, invalid builtin %s encountered"),
                        quotearg_style_mem (locale_quoting_style, string[1],
                                            number[1]));
            if (number[2] > 0)
              {
                if (strlen (string[2]) < number[2])
                  m4_error (context, EXIT_FAILURE, 0, NULL, _("\
ill-formed frozen file, invalid module %s encountered"),
                            quotearg_style_mem (locale_quoting_style,
                                                string[2], number[2]));
                module = m4__module_find (context, string[2]);
              }
            token = m4_builtin_find_by_name (context, module, string[1]);

            if (token == NULL)
              {
                token = (m4_symbol_value *) xzalloc (sizeof *token);
                m4_set_symbol_value_placeholder (token, xstrdup (string[1]));
                VALUE_MODULE (token) = module;
                VALUE_MIN_ARGS (token) = 0;
                VALUE_MAX_ARGS (token) = -1;
              }
            m4_symbol_pushdef (M4SYMTAB, string[0], number[0], token);
          }
          break;

        case 'M':

          /* Load a module, but *without* perturbing the symbol table.
             Note that any expansion from loading the module which would
             have been seen when loading it originally is discarded
             when loading it from a frozen file. */

          if (version < 2)
            {
              /* 'M' operator is not supported in format version 1. */
              m4_error (context, EXIT_FAILURE, 0, NULL, _("\
ill-formed frozen file, version 2 directive `%c' encountered"), 'M');
            }

          GET_CHARACTER;
          GET_NUMBER (number[0], false);
          VALIDATE ('\n');
          GET_STRING (file, string[0], allocated[0], number[0], false);
          VALIDATE ('\n');

          if (strlen (string[0]) < number[0])
            m4_error (context, EXIT_FAILURE, 0, NULL, _("\
ill-formed frozen file, invalid module %s encountered"),
                      quotearg_style_mem (locale_quoting_style,
                                          string[0], number[0]));
          m4__module_open (context, string[0], NULL);

          break;

        case 'R':

          if (version < 2)
            {
              /* 'R' operator is not supported in format version 1. */
              m4_error (context, EXIT_FAILURE, 0, NULL, _("\
ill-formed frozen file, version 2 directive `%c' encountered"), 'R');
            }

          GET_CHARACTER;
          GET_NUMBER (number[0], false);
          VALIDATE ('\n');
          GET_STRING (file, string[0], allocated[0], number[0], false);
          VALIDATE ('\n');

          m4_set_regexp_syntax_opt (context,
                                    m4_regexp_syntax_encode (string[0]));
          if (m4_get_regexp_syntax_opt (context) < 0
              || strlen (string[0]) < number[0])
            {
              m4_error (context, EXIT_FAILURE, 0, NULL,
                        _("bad syntax-spec %s"),
                        quotearg_style_mem (locale_quoting_style, string[0],
                                            number[0]));
            }

          break;

        case 'S':

          if (version < 2)
            {
              /* 'S' operator is not supported in format version 1. */
              m4_error (context, EXIT_FAILURE, 0, NULL, _("\
ill-formed frozen file, version 2 directive `%c' encountered"), 'S');
            }

          GET_CHARACTER;
          syntax = character;
          GET_CHARACTER;
          GET_NUMBER (number[0], false);
          VALIDATE ('\n');
          GET_STRING (file, string[0], allocated[0], number[0], false);

          /* Syntax under M4_SYNTAX_MASKS is handled specially; all
             other characters are additive.  */
          if ((m4_set_syntax (M4SYNTAX, syntax,
                              (m4_syntax_code (syntax) & M4_SYNTAX_MASKS
                               ? '=' : '+'), string[0], number[0]) < 0)
              && (syntax != '\0'))
            {
              m4_error (context, 0, 0, NULL,
                        _("undefined syntax code %c"), syntax);
            }
          break;

        case 't':
          /* Trace a macro name.  */
          if (version < 2)
            {
              /* 't' operator is not supported in format version 1. */
              m4_error (context, EXIT_FAILURE, 0, NULL, _("\
ill-formed frozen file, version 2 directive `%c' encountered"), 't');
            }

          GET_CHARACTER;
          GET_NUMBER (number[0], false);
          VALIDATE ('\n');
          GET_STRING (file, string[0], allocated[0], number[0], false);
          VALIDATE ('\n');

          m4_set_symbol_name_traced (M4SYMTAB, string[0], number[0], true);

          break;

        case 'C':
        case 'D':
        case 'Q':
          operation = character;
          GET_CHARACTER;

          /* Get string lengths. */

          if (operation == 'D' && character == '-')
            {
              /* Accept a negative diversion number.  */
              GET_CHARACTER;
              GET_NUMBER (number[0], true);
              number[0] = -number[0];
            }
          else
            GET_NUMBER (number[0], false);
          VALIDATE (',');
          GET_CHARACTER;
          GET_NUMBER (number[1], false);
          VALIDATE ('\n');

          /* Get string contents.  */
          if (operation != 'D')
            {
              GET_STRING (file, string[0], allocated[0], number[0], false);
              if (version > 1)
                {
                  VALIDATE ('\n');
                  GET_CHARACTER;
                }
            }
          else
            GET_CHARACTER;
          GET_STRING (file, string[1], allocated[1], number[1], true);
          VALIDATE ('\n');

          /* Act according to operation letter.  */

          switch (operation)
            {
            case 'C':

              /* Change comment strings.  */

              m4_set_comment (M4SYNTAX, string[0], number[0], string[1],
                              number[1]);
              break;

            case 'D':

              /* Select a diversion and add a string to it.  */

              m4_make_diversion (context, number[0]);
              if (number[1] > 0)
                m4_output_text (context, string[1], number[1]);
              break;

            case 'Q':

              /* Change quote strings.  */

              m4_set_quotes (M4SYNTAX, string[0], number[0], string[1],
                             number[1]);
              break;

            default:

              /* Cannot happen.  */

              break;
            }
          break;

        case 'T':
          GET_CHARACTER;

          /* Get string lengths. */

          GET_NUMBER (number[0], false);
          VALIDATE (',');
          GET_CHARACTER;
          GET_NUMBER (number[1], false);

          if (character == ',')
            {
              if (version > 1)
                {
                  /* 'T' operator accepts an optional third argument for
                     format versions 2 or later.  */
                  GET_CHARACTER;
                  GET_NUMBER (number[2], false);
                }
              else
                {
                  /* 3 argument 'T' operations are invalid for format
                     version 1.  */
                  m4_error (context, EXIT_FAILURE, 0, NULL, _("\
ill-formed frozen file, version 2 directive `%c' encountered"), 'T');
                }
            }
          else
            number[2] = 0;

          VALIDATE ('\n');

          /* Get string contents.  */
          GET_STRING (file, string[0], allocated[0], number[0], false);
          if (version > 1)
            {
              VALIDATE ('\n');
              GET_CHARACTER;
            }
          GET_STRING (file, string[1], allocated[1], number[1], true);
          if (version > 1 && number[2])
            {
              VALIDATE ('\n');
              GET_CHARACTER;
            }
          GET_STRING (file, string[2], allocated[2], number[2], true);
          VALIDATE ('\n');

          /* Enter a macro having an expansion text as a definition.  */
          {
            m4_symbol_value *token;
            m4_module *module = NULL;

            token = (m4_symbol_value *) xzalloc (sizeof *token);
            if (number[2] > 0)
              {
                if (strlen (string[2]) < number[2])
                  m4_error (context, EXIT_FAILURE, 0, NULL, _("\
ill-formed frozen file, invalid module %s encountered"),
                            quotearg_style_mem (locale_quoting_style,
                                                string[2], number[2]));
                module = m4__module_find (context, string[2]);
              }

            m4_set_symbol_value_text (token, xmemdup0 (string[1], number[1]),
                                      number[1], 0);
            VALUE_MODULE (token) = module;
            VALUE_MAX_ARGS (token) = -1;

            m4_symbol_pushdef (M4SYMTAB, string[0], number[0], token);
          }
          break;

        }
      GET_DIRECTIVE;
    }

  free (string[0]);
  free (string[1]);
  free (string[2]);
  if (close_stream (file) != 0)
    m4_error (context, EXIT_FAILURE, errno, NULL,
              _("unable to read frozen state"));
  m4_set_current_file (context, NULL);
  m4_set_current_line (context, 0);

#undef GET_STRING
#undef GET_CHARACTER
#undef GET_NUMBER
#undef VALIDATE
#undef CHECK_ALLOCATION
#undef GET_DIRECTIVE
}