1 /* GNU m4 -- A simple macro processor
3 Copyright (C) 1989-1994, 2004-2011 Free Software Foundation, Inc.
5 This file is part of GNU M4.
7 GNU M4 is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU M4 is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include "gl_avltree_oset.h"
29 /* Size of initial in-memory buffer size for diversions. Small diversions
30 would usually fit in. */
31 #define INITIAL_BUFFER_SIZE 512
33 /* Maximum value for the total of all in-memory buffer sizes for
35 #define MAXIMUM_TOTAL_SIZE (512 * 1024)
37 /* Size of buffer size to use while copying files. */
38 #define COPY_BUFFER_SIZE (32 * 512)
40 /* Output functions. Most of the complexity is for handling cpp like
43 This code is fairly entangled with the code in input.c, and maybe it
46 typedef struct temp_dir m4_temp_dir;
48 /* When part of diversion_table, each struct m4_diversion either
49 represents an open file (zero size, non-NULL u.file), an in-memory
50 buffer (non-zero size, non-NULL u.buffer), or an unused placeholder
51 diversion (zero size, u is NULL, non-zero used indicates that a
52 file has been created). When not part of diversion_table, u.next
53 is a pointer to the free_list chain. */
55 typedef struct m4_diversion m4_diversion;
61 FILE *file; /* Diversion file on disk. */
62 char *buffer; /* Malloc'd diversion buffer. */
63 m4_diversion *next; /* Free-list pointer */
65 int divnum; /* Which diversion this represents. */
66 int size; /* Usable size before reallocation. */
67 int used; /* Used buffer length, or tmp file exists. */
70 /* Table of diversions 1 through INT_MAX. */
71 static gl_oset_t diversion_table;
73 /* Diversion 0 (not part of diversion_table). */
74 static m4_diversion div0;
76 /* Linked list of reclaimed diversion storage. */
77 static m4_diversion *free_list;
79 /* Obstack from which diversion storage is allocated. */
80 static struct obstack diversion_storage;
82 /* Total size of all in-memory buffer sizes. */
83 static int total_buffer_size;
85 /* The number of the currently active diversion. This variable is
86 maintained for the `divnum' builtin function. */
87 int current_diversion;
89 /* Current output diversion, NULL if output is being currently
90 discarded. output_diversion->u is guaranteed non-NULL except when
91 the diversion has never been used; use size to determine if it is a
92 malloc'd buffer or a FILE. output_diversion->used is 0 if u.file
93 is stdout, and non-zero if this is a malloc'd buffer or a temporary
95 static m4_diversion *output_diversion;
97 /* Cache of output_diversion->u.file, only valid when
98 output_diversion->size is 0. */
99 static FILE *output_file;
101 /* Cache of output_diversion->u.buffer + output_diversion->used, only
102 valid when output_diversion->size is non-zero. */
103 static char *output_cursor;
105 /* Cache of output_diversion->size - output_diversion->used, only
106 valid when output_diversion->size is non-zero. */
107 static int output_unused;
109 /* Number of input line we are generating output for. */
110 int output_current_line;
112 /* Temporary directory holding all spilled diversion files. */
113 static m4_temp_dir *output_temp_dir;
115 /* Cache of most recently used spilled diversion files. */
116 static FILE *tmp_file1;
117 static FILE *tmp_file2;
119 /* Diversions that own tmp_file, or 0. */
120 static int tmp_file1_owner;
121 static int tmp_file2_owner;
123 /* True if tmp_file2 is more recently used. */
124 static bool tmp_file2_recent;
127 /* Internal routines. */
129 /* Callback for comparing list elements ELT1 and ELT2 for order in
132 cmp_diversion_CB (const void *elt1, const void *elt2)
134 const m4_diversion *d1 = (const m4_diversion *) elt1;
135 const m4_diversion *d2 = (const m4_diversion *) elt2;
136 /* No need to worry about overflow, since we don't create diversions
137 with negative divnum. */
138 return d1->divnum - d2->divnum;
141 /* Callback for comparing list element ELT against THRESHOLD. */
143 threshold_diversion_CB (const void *elt, const void *threshold)
145 const m4_diversion *diversion = (const m4_diversion *) elt;
146 /* No need to worry about overflow, since we don't create diversions
147 with negative divnum. */
148 return diversion->divnum >= *(const int *) threshold;
151 /* Clean up any temporary directory. Designed for use as an atexit
152 handler, where it is not safe to call exit() recursively; so this
153 calls _exit if a problem is encountered. */
155 cleanup_tmpfile (void)
157 /* Close any open diversions. */
163 gl_oset_iterator_t iter = gl_oset_iterator (diversion_table);
164 while (gl_oset_iterator_next (&iter, &elt))
166 m4_diversion *diversion = (m4_diversion *) elt;
167 if (!diversion->size && diversion->u.file
168 && close_stream_temp (diversion->u.file) != 0)
171 "cannot clean temporary file for diversion"));
175 gl_oset_iterator_free (&iter);
178 /* Clean up the temporary directory. */
179 if (cleanup_temp_dir (output_temp_dir) != 0)
182 _exit (exit_failure);
185 /* Convert DIVNUM into a temporary file name for use in m4_tmp*. */
187 m4_tmpname (int divnum)
193 tail = xasprintf ("%s/m4-%d", output_temp_dir->dir_name, INT_MAX);
194 buffer = (char *) obstack_copy0 (&diversion_storage, tail,
197 tail = strrchr (buffer, '-') + 1;
200 sprintf (tail, "%d", divnum);
204 /* Create a temporary file for diversion DIVNUM open for reading and
205 writing in a secure temp directory. The file will be automatically
206 closed and deleted on a fatal signal. The file can be closed and
207 reopened with m4_tmpclose and m4_tmpopen, or moved with
208 m4_tmprename; when finally done with the file, close it with
209 m4_tmpremove. Exits on failure, so the return value is always an
212 m4_tmpfile (int divnum)
217 if (output_temp_dir == NULL)
219 output_temp_dir = create_temp_dir ("m4-", NULL, true);
220 if (output_temp_dir == NULL)
221 M4ERROR ((EXIT_FAILURE, errno,
222 "cannot create temporary file for diversion"));
223 atexit (cleanup_tmpfile);
225 name = m4_tmpname (divnum);
226 register_temp_file (output_temp_dir, name);
227 file = fopen_temp (name, O_BINARY ? "wb+" : "w+");
230 unregister_temp_file (output_temp_dir, name);
231 M4ERROR ((EXIT_FAILURE, errno,
232 "cannot create temporary file for diversion"));
234 else if (set_cloexec_flag (fileno (file), true) != 0)
235 M4ERROR ((warning_status, errno,
236 "Warning: cannot protect diversion across forks"));
240 /* Reopen a temporary file for diversion DIVNUM for reading and
241 writing in a secure temp directory. If REREAD, the file is
242 positioned at offset 0, otherwise the file is positioned at the
243 end. Exits on failure, so the return value is always an open
246 m4_tmpopen (int divnum, bool reread)
251 if (tmp_file1_owner == divnum)
253 if (reread && fseeko (tmp_file1, 0, SEEK_SET) != 0)
254 m4_error (EXIT_FAILURE, errno,
255 _("cannot seek within diversion"));
256 tmp_file2_recent = false;
259 else if (tmp_file2_owner == divnum)
261 if (reread && fseeko (tmp_file2, 0, SEEK_SET) != 0)
262 m4_error (EXIT_FAILURE, errno,
263 _("cannot seek within diversion"));
264 tmp_file2_recent = true;
267 name = m4_tmpname (divnum);
268 /* We need update mode, to avoid truncation. */
269 file = fopen_temp (name, O_BINARY ? "rb+" : "r+");
271 M4ERROR ((EXIT_FAILURE, errno,
272 "cannot create temporary file for diversion"));
273 else if (set_cloexec_flag (fileno (file), true) != 0)
274 m4_error (0, errno, _("cannot protect diversion across forks"));
275 /* Update mode starts at the beginning of the stream, but sometimes
277 else if (!reread && fseeko (file, 0, SEEK_END) != 0)
278 m4_error (EXIT_FAILURE, errno,
279 _("cannot seek within diversion"));
283 /* Close, but don't delete, a temporary FILE for diversion DIVNUM. To
284 reduce the I/O overhead of repeatedly opening and closing the same
285 file, this implementation caches the most recent spilled diversion.
286 On the other hand, keeping every spilled diversion open would run
287 into EMFILE limits. */
289 m4_tmpclose (FILE *file, int divnum)
292 if (divnum != tmp_file1_owner && divnum != tmp_file2_owner)
294 if (tmp_file2_recent)
297 result = close_stream_temp (tmp_file1);
299 tmp_file1_owner = divnum;
304 result = close_stream_temp (tmp_file2);
306 tmp_file2_owner = divnum;
312 /* Delete a closed temporary FILE for diversion DIVNUM. */
314 m4_tmpremove (int divnum)
316 if (divnum == tmp_file1_owner)
318 int result = close_stream_temp (tmp_file1);
323 else if (divnum == tmp_file2_owner)
325 int result = close_stream_temp (tmp_file2);
330 return cleanup_temp_file (output_temp_dir, m4_tmpname (divnum));
333 /* Transfer the temporary file for diversion OLDNUM to the previously
334 unused diversion NEWNUM. Return an open stream visiting the new
335 temporary file, positioned at the end, or exit on failure. */
337 m4_tmprename (int oldnum, int newnum)
339 /* m4_tmpname reuses its return buffer. */
340 char *oldname = xstrdup (m4_tmpname (oldnum));
341 const char *newname = m4_tmpname (newnum);
342 register_temp_file (output_temp_dir, newname);
343 if (oldnum == tmp_file1_owner)
345 /* Be careful of mingw, which can't rename an open file. */
346 if (RENAME_OPEN_FILE_WORKS)
347 tmp_file1_owner = newnum;
350 if (close_stream_temp (tmp_file1))
351 m4_error (EXIT_FAILURE, errno,
352 _("cannot close temporary file for diversion"));
356 else if (oldnum == tmp_file2_owner)
358 /* Be careful of mingw, which can't rename an open file. */
359 if (RENAME_OPEN_FILE_WORKS)
360 tmp_file2_owner = newnum;
363 if (close_stream_temp (tmp_file2))
364 m4_error (EXIT_FAILURE, errno,
365 _("cannot close temporary file for diversion"));
369 /* Either it is safe to rename an open file, or no one should have
370 oldname open at this point. */
371 if (rename (oldname, newname))
372 m4_error (EXIT_FAILURE, errno,
373 _("cannot create temporary file for diversion"));
374 unregister_temp_file (output_temp_dir, oldname);
376 return m4_tmpopen (newnum, false);
380 /*------------------------.
381 | Output initialization. |
382 `------------------------*/
387 diversion_table = gl_oset_create_empty (GL_AVLTREE_OSET, cmp_diversion_CB,
389 div0.u.file = stdout;
390 output_diversion = &div0;
391 output_file = stdout;
392 obstack_init (&diversion_storage);
398 /* Order is important, since we may have registered cleanup_tmpfile
399 as an atexit handler, and it must not traverse stale memory. */
400 gl_oset_t table = diversion_table;
402 m4_tmpremove (tmp_file1_owner);
404 m4_tmpremove (tmp_file2_owner);
405 diversion_table = NULL;
406 gl_oset_free (table);
407 obstack_free (&diversion_storage, NULL);
410 /*----------------------------------------------------------------.
411 | Reorganize in-memory diversion buffers so the current diversion |
412 | can accomodate LENGTH more characters without further |
413 | reorganization. The current diversion buffer is made bigger if |
414 | possible. But to make room for a bigger buffer, one of the |
415 | in-memory diversion buffers might have to be flushed to a newly |
416 | created temporary file. This flushed buffer might well be the |
418 `----------------------------------------------------------------*/
421 make_room_for (int length)
424 m4_diversion *selected_diversion = NULL;
426 /* Compute needed size for in-memory buffer. Diversions in-memory
427 buffers start at 0 bytes, then 512, then keep doubling until it is
428 decided to flush them to disk. */
430 output_diversion->used = output_diversion->size - output_unused;
432 for (wanted_size = output_diversion->size;
433 wanted_size < output_diversion->used + length;
434 wanted_size = wanted_size == 0 ? INITIAL_BUFFER_SIZE : wanted_size * 2)
437 /* Check if we are exceeding the maximum amount of buffer memory. */
439 if (total_buffer_size - output_diversion->size + wanted_size
440 > MAXIMUM_TOTAL_SIZE)
443 char *selected_buffer;
444 m4_diversion *diversion;
446 gl_oset_iterator_t iter;
449 /* Find out the buffer having most data, in view of flushing it to
450 disk. Fake the current buffer as having already received the
451 projected data, while making the selection. So, if it is
452 selected indeed, we will flush it smaller, before it grows. */
454 selected_diversion = output_diversion;
455 selected_used = output_diversion->used + length;
457 iter = gl_oset_iterator (diversion_table);
458 while (gl_oset_iterator_next (&iter, &elt))
460 diversion = (m4_diversion *) elt;
461 if (diversion->used > selected_used)
463 selected_diversion = diversion;
464 selected_used = diversion->used;
467 gl_oset_iterator_free (&iter);
469 /* Create a temporary file, write the in-memory buffer of the
470 diversion to this file, then release the buffer. Zero the
471 diversion before doing anything that can exit () (including
472 m4_tmpfile), so that the atexit handler doesn't try to close
473 a garbage pointer as a file. */
475 selected_buffer = selected_diversion->u.buffer;
476 total_buffer_size -= selected_diversion->size;
477 selected_diversion->size = 0;
478 selected_diversion->u.file = NULL;
479 selected_diversion->u.file = m4_tmpfile (selected_diversion->divnum);
481 if (selected_diversion->used > 0)
483 count = fwrite (selected_buffer, (size_t) selected_diversion->used,
484 1, selected_diversion->u.file);
486 M4ERROR ((EXIT_FAILURE, errno,
487 "ERROR: cannot flush diversion to temporary file"));
490 /* Reclaim the buffer space for other diversions. */
492 free (selected_buffer);
493 selected_diversion->used = 1;
496 /* Reload output_file, just in case the flushed diversion was current. */
498 if (output_diversion == selected_diversion)
500 /* The flushed diversion was current indeed. */
502 output_file = output_diversion->u.file;
503 output_cursor = NULL;
508 /* Close any selected file since it is not the current diversion. */
509 if (selected_diversion)
511 FILE *file = selected_diversion->u.file;
512 selected_diversion->u.file = NULL;
513 if (m4_tmpclose (file, selected_diversion->divnum) != 0)
515 _("cannot close temporary file for diversion"));
518 /* The current buffer may be safely reallocated. */
520 char *buffer = output_diversion->u.buffer;
521 output_diversion->u.buffer = xcharalloc ((size_t) wanted_size);
522 memcpy (output_diversion->u.buffer, buffer, output_diversion->used);
526 total_buffer_size += wanted_size - output_diversion->size;
527 output_diversion->size = wanted_size;
529 output_cursor = output_diversion->u.buffer + output_diversion->used;
530 output_unused = wanted_size - output_diversion->used;
534 /*--------------------------------------------------------------.
535 | Output one character CHAR, when it is known that it goes to a |
536 | diversion file or an in-memory diversion buffer. |
537 `--------------------------------------------------------------*/
539 #define OUTPUT_CHARACTER(Char) \
541 putc ((Char), output_file); \
542 else if (output_unused == 0) \
543 output_character_helper ((Char)); \
545 (output_unused--, *output_cursor++ = (Char))
548 output_character_helper (int character)
553 putc (character, output_file);
556 *output_cursor++ = character;
561 /*-------------------------------------------------------------------.
562 | Output one TEXT having LENGTH characters, when it is known that it |
563 | goes to a diversion file or an in-memory diversion buffer. |
564 `-------------------------------------------------------------------*/
567 output_text (const char *text, int length)
571 if (!output_diversion || !length)
574 if (!output_file && length > output_unused)
575 make_room_for (length);
579 count = fwrite (text, length, 1, output_file);
581 M4ERROR ((EXIT_FAILURE, errno, "ERROR: copying inserted file"));
585 memcpy (output_cursor, text, (size_t) length);
586 output_cursor += length;
587 output_unused -= length;
591 /*--------------------------------------------------------------------.
592 | Add some text into an obstack OBS, taken from TEXT, having LENGTH |
593 | characters. If OBS is NULL, output the text to an external file |
594 | or an in-memory diversion buffer instead. If OBS is NULL, and |
595 | there is no output file, the text is discarded. LINE is the line |
596 | where the token starts (not necessarily current_line, in the case |
597 | of multiline tokens). |
599 | If we are generating sync lines, the output has to be examined, |
600 | because we need to know how much output each input line generates. |
601 | In general, sync lines are output whenever a single input lines |
602 | generates several output lines, or when several input lines do not |
603 | generate any output. |
604 `--------------------------------------------------------------------*/
607 shipout_text (struct obstack *obs, const char *text, int length, int line)
609 static bool start_of_output_line = true;
612 /* If output goes to an obstack, merely add TEXT to it. */
616 obstack_grow (obs, text, length);
620 /* Do nothing if TEXT should be discarded. */
622 if (output_diversion == NULL)
625 /* Output TEXT to a file, or in-memory diversion buffer. */
631 /* In-line short texts. */
633 case 8: OUTPUT_CHARACTER (*text); text++;
634 case 7: OUTPUT_CHARACTER (*text); text++;
635 case 6: OUTPUT_CHARACTER (*text); text++;
636 case 5: OUTPUT_CHARACTER (*text); text++;
637 case 4: OUTPUT_CHARACTER (*text); text++;
638 case 3: OUTPUT_CHARACTER (*text); text++;
639 case 2: OUTPUT_CHARACTER (*text); text++;
640 case 1: OUTPUT_CHARACTER (*text);
644 /* Optimize longer texts. */
647 output_text (text, length);
651 /* Check for syncline only at the start of a token. Multiline
652 tokens, and tokens that are out of sync but in the middle of
653 the line, must wait until the next raw newline triggers a
655 if (start_of_output_line)
657 start_of_output_line = false;
658 output_current_line++;
660 xfprintf (stderr, "DEBUG: line %d, cur %d, cur out %d\n",
661 line, current_line, output_current_line);
664 /* Output a `#line NUM' synchronization directive if needed.
665 If output_current_line was previously given a negative
666 value (invalidated), output `#line NUM "FILE"' instead. */
668 if (output_current_line != line)
670 OUTPUT_CHARACTER ('#');
671 OUTPUT_CHARACTER ('l');
672 OUTPUT_CHARACTER ('i');
673 OUTPUT_CHARACTER ('n');
674 OUTPUT_CHARACTER ('e');
675 OUTPUT_CHARACTER (' ');
676 for (cursor = ntoa (line, 10); *cursor; cursor++)
677 OUTPUT_CHARACTER (*cursor);
678 if (output_current_line < 1 && current_file[0] != '\0')
680 OUTPUT_CHARACTER (' ');
681 OUTPUT_CHARACTER ('"');
682 for (cursor = current_file; *cursor; cursor++)
683 OUTPUT_CHARACTER (*cursor);
684 OUTPUT_CHARACTER ('"');
686 OUTPUT_CHARACTER ('\n');
687 output_current_line = line;
691 /* Output the token, and track embedded newlines. */
692 for (; length-- > 0; text++)
694 if (start_of_output_line)
696 start_of_output_line = false;
697 output_current_line++;
699 xfprintf (stderr, "DEBUG: line %d, cur %d, cur out %d\n",
700 line, current_line, output_current_line);
703 OUTPUT_CHARACTER (*text);
705 start_of_output_line = true;
710 /* Functions for use by diversions. */
712 /*------------------------------------------------------------------.
713 | Make a file for diversion DIVNUM, and install it in the diversion |
714 | table. Grow the size of the diversion table as needed. |
715 `------------------------------------------------------------------*/
717 /* The number of possible diversions is limited only by memory and
718 available file descriptors (each overflowing diversion uses one). */
721 make_diversion (int divnum)
723 m4_diversion *diversion = NULL;
725 if (current_diversion == divnum)
728 if (output_diversion)
730 if (!output_diversion->size && !output_diversion->u.file)
732 assert (!output_diversion->used);
733 if (!gl_oset_remove (diversion_table, output_diversion))
735 output_diversion->u.next = free_list;
736 free_list = output_diversion;
738 else if (output_diversion->size)
739 output_diversion->used = output_diversion->size - output_unused;
740 else if (output_diversion->used)
742 FILE *file = output_diversion->u.file;
743 output_diversion->u.file = NULL;
744 if (m4_tmpclose (file, output_diversion->divnum) != 0)
746 _("cannot close temporary file for diversion"));
748 output_diversion = NULL;
750 output_cursor = NULL;
754 current_diversion = divnum;
764 if (gl_oset_search_atleast (diversion_table, threshold_diversion_CB,
767 m4_diversion *temp = (m4_diversion *) elt;
768 if (temp->divnum == divnum)
772 if (diversion == NULL)
774 /* First time visiting this diversion. */
777 diversion = free_list;
778 free_list = diversion->u.next;
782 diversion = (m4_diversion *) obstack_alloc (&diversion_storage,
787 diversion->u.file = NULL;
788 diversion->divnum = divnum;
789 gl_oset_add (diversion_table, diversion);
792 output_diversion = diversion;
793 if (output_diversion->size)
795 output_cursor = output_diversion->u.buffer + output_diversion->used;
796 output_unused = output_diversion->size - output_diversion->used;
800 if (!output_diversion->u.file && output_diversion->used)
801 output_diversion->u.file = m4_tmpopen (output_diversion->divnum,
803 output_file = output_diversion->u.file;
805 output_current_line = -1;
808 /*-------------------------------------------------------------------.
809 | Insert a FILE into the current output file, in the same manner |
810 | diversions are handled. This allows files to be included, without |
811 | having them rescanned by m4. |
812 `-------------------------------------------------------------------*/
815 insert_file (FILE *file)
817 static char buffer[COPY_BUFFER_SIZE];
820 /* Optimize out inserting into a sink. */
821 if (!output_diversion)
824 /* Insert output by big chunks. */
827 length = fread (buffer, 1, sizeof buffer, file);
829 M4ERROR ((EXIT_FAILURE, errno, "error reading inserted file"));
832 output_text (buffer, length);
836 /*-------------------------------------------------------------------.
837 | Insert DIVERSION (but not div0) into the current output file. The |
838 | diversion is NOT placed on the expansion obstack, because it must |
839 | not be rescanned. When the file is closed, it is deleted by the |
841 `-------------------------------------------------------------------*/
844 insert_diversion_helper (m4_diversion *diversion)
846 /* Effectively undivert only if an output stream is active. */
847 if (output_diversion)
851 if (!output_diversion->u.file)
853 /* Transferring diversion metadata is faster than
855 assert (!output_diversion->used && output_diversion != &div0
857 output_diversion->u.buffer = diversion->u.buffer;
858 output_diversion->size = diversion->size;
859 output_cursor = diversion->u.buffer + diversion->used;
860 output_unused = diversion->size - diversion->used;
861 diversion->u.buffer = NULL;
865 /* Avoid double-charging the total in-memory size when
866 transferring from one in-memory diversion to
868 total_buffer_size -= diversion->size;
869 output_text (diversion->u.buffer, diversion->used);
872 else if (!output_diversion->u.file)
874 /* Transferring diversion metadata is faster than copying
876 assert (!output_diversion->used && output_diversion != &div0
878 output_diversion->u.file = m4_tmprename (diversion->divnum,
879 output_diversion->divnum);
880 output_diversion->used = 1;
881 output_file = output_diversion->u.file;
882 diversion->u.file = NULL;
887 if (!diversion->u.file)
888 diversion->u.file = m4_tmpopen (diversion->divnum, true);
889 insert_file (diversion->u.file);
892 output_current_line = -1;
895 /* Return all space used by the diversion. */
898 if (!output_diversion)
899 total_buffer_size -= diversion->size;
900 free (diversion->u.buffer);
905 if (diversion->u.file)
907 FILE *file = diversion->u.file;
908 diversion->u.file = NULL;
909 if (m4_tmpclose (file, diversion->divnum) != 0)
911 _("cannot clean temporary file for diversion"));
913 if (m4_tmpremove (diversion->divnum) != 0)
914 M4ERROR ((0, errno, "cannot clean temporary file for diversion"));
917 gl_oset_remove (diversion_table, diversion);
918 diversion->u.next = free_list;
919 free_list = diversion;
922 /*------------------------------------------------------------------.
923 | Insert diversion number DIVNUM into the current output file. The |
924 | diversion is NOT placed on the expansion obstack, because it must |
925 | not be rescanned. When the file is closed, it is deleted by the |
927 `------------------------------------------------------------------*/
930 insert_diversion (int divnum)
934 /* Do not care about nonexistent diversions, and undiverting stdout
935 or self is a no-op. */
936 if (divnum <= 0 || current_diversion == divnum)
938 if (gl_oset_search_atleast (diversion_table, threshold_diversion_CB,
941 m4_diversion *diversion = (m4_diversion *) elt;
942 if (diversion->divnum == divnum)
943 insert_diversion_helper (diversion);
947 /*----------------------------------------------------------------.
948 | Get back all diversions. This is done just before exiting from |
949 | main, and from m4_undivert (), if called without arguments. |
950 `----------------------------------------------------------------*/
956 gl_oset_iterator_t iter = gl_oset_iterator (diversion_table);
957 while (gl_oset_iterator_next (&iter, &elt))
959 m4_diversion *diversion = (m4_diversion *) elt;
960 if (diversion->divnum != current_diversion)
961 insert_diversion_helper (diversion);
963 gl_oset_iterator_free (&iter);
966 /*-------------------------------------------------------------.
967 | Produce all diversion information in frozen format on FILE. |
968 `-------------------------------------------------------------*/
971 freeze_diversions (FILE *file)
975 gl_oset_iterator_t iter;
978 saved_number = current_diversion;
981 output_file = file; /* kludge in the frozen file */
983 iter = gl_oset_iterator (diversion_table);
984 while (gl_oset_iterator_next (&iter, &elt))
986 m4_diversion *diversion = (m4_diversion *) elt;
987 if (diversion->size || diversion->used)
990 xfprintf (file, "D%d,%d\n", diversion->divnum, diversion->used);
993 struct stat file_stat;
994 diversion->u.file = m4_tmpopen (diversion->divnum, true);
995 if (fstat (fileno (diversion->u.file), &file_stat) < 0)
996 M4ERROR ((EXIT_FAILURE, errno, "cannot stat diversion"));
997 if (file_stat.st_size < 0
998 || (file_stat.st_size + 0UL
999 != (unsigned long int) file_stat.st_size))
1000 M4ERROR ((EXIT_FAILURE, 0, "diversion too large"));
1001 xfprintf (file, "D%d,%lu\n", diversion->divnum,
1002 (unsigned long int) file_stat.st_size);
1005 insert_diversion_helper (diversion);
1008 last_inserted = diversion->divnum;
1011 gl_oset_iterator_free (&iter);
1013 /* Save the active diversion number, if not already. */
1015 if (saved_number != last_inserted)
1016 xfprintf (file, "D%d,0\n\n", saved_number);