add packaging
[platform/upstream/m4.git] / src / output.c
1 /* GNU m4 -- A simple macro processor
2
3    Copyright (C) 1989-1994, 2004-2013 Free Software Foundation, Inc.
4
5    This file is part of GNU M4.
6
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.
11
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.
16
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/>.
19 */
20
21 #include "m4.h"
22
23 #include <limits.h>
24 #include <sys/stat.h>
25
26 #include "gl_avltree_oset.h"
27 #include "gl_xoset.h"
28
29 /* Size of initial in-memory buffer size for diversions.  Small diversions
30    would usually fit in.  */
31 #define INITIAL_BUFFER_SIZE 512
32
33 /* Maximum value for the total of all in-memory buffer sizes for
34    diversions.  */
35 #define MAXIMUM_TOTAL_SIZE (512 * 1024)
36
37 /* Size of buffer size to use while copying files.  */
38 #define COPY_BUFFER_SIZE (32 * 512)
39
40 /* Output functions.  Most of the complexity is for handling cpp like
41    sync lines.
42
43    This code is fairly entangled with the code in input.c, and maybe it
44    belongs there?  */
45
46 typedef struct temp_dir m4_temp_dir;
47
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.  */
54
55 typedef struct m4_diversion m4_diversion;
56
57 struct m4_diversion
58   {
59     union
60       {
61         FILE *file;             /* Diversion file on disk.  */
62         char *buffer;           /* Malloc'd diversion buffer.  */
63         m4_diversion *next;     /* Free-list pointer */
64       } u;
65     int divnum;                 /* Which diversion this represents.  */
66     int size;                   /* Usable size before reallocation.  */
67     int used;                   /* Used buffer length, or tmp file exists.  */
68   };
69
70 /* Table of diversions 1 through INT_MAX.  */
71 static gl_oset_t diversion_table;
72
73 /* Diversion 0 (not part of diversion_table).  */
74 static m4_diversion div0;
75
76 /* Linked list of reclaimed diversion storage.  */
77 static m4_diversion *free_list;
78
79 /* Obstack from which diversion storage is allocated.  */
80 static struct obstack diversion_storage;
81
82 /* Total size of all in-memory buffer sizes.  */
83 static int total_buffer_size;
84
85 /* The number of the currently active diversion.  This variable is
86    maintained for the `divnum' builtin function.  */
87 int current_diversion;
88
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
94    diversion file.  */
95 static m4_diversion *output_diversion;
96
97 /* Cache of output_diversion->u.file, only valid when
98    output_diversion->size is 0.  */
99 static FILE *output_file;
100
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;
104
105 /* Cache of output_diversion->size - output_diversion->used, only
106    valid when output_diversion->size is non-zero.  */
107 static int output_unused;
108
109 /* Number of input line we are generating output for.  */
110 int output_current_line;
111
112 /* Temporary directory holding all spilled diversion files.  */
113 static m4_temp_dir *output_temp_dir;
114
115 /* Cache of most recently used spilled diversion files.  */
116 static FILE *tmp_file1;
117 static FILE *tmp_file2;
118
119 /* Diversions that own tmp_file, or 0.  */
120 static int tmp_file1_owner;
121 static int tmp_file2_owner;
122
123 /* True if tmp_file2 is more recently used.  */
124 static bool tmp_file2_recent;
125
126 \f
127 /* Internal routines.  */
128
129 /* Callback for comparing list elements ELT1 and ELT2 for order in
130    diversion_table.  */
131 static int
132 cmp_diversion_CB (const void *elt1, const void *elt2)
133 {
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;
139 }
140
141 /* Callback for comparing list element ELT against THRESHOLD.  */
142 static bool
143 threshold_diversion_CB (const void *elt, const void *threshold)
144 {
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;
149 }
150
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.  */
154 static void
155 cleanup_tmpfile (void)
156 {
157   /* Close any open diversions.  */
158   bool fail = false;
159
160   if (diversion_table)
161     {
162       const void *elt;
163       gl_oset_iterator_t iter = gl_oset_iterator (diversion_table);
164       while (gl_oset_iterator_next (&iter, &elt))
165         {
166           m4_diversion *diversion = (m4_diversion *) elt;
167           if (!diversion->size && diversion->u.file
168               && close_stream_temp (diversion->u.file) != 0)
169             {
170               M4ERROR ((0, errno,
171                         "cannot clean temporary file for diversion"));
172               fail = true;
173             }
174         }
175       gl_oset_iterator_free (&iter);
176     }
177
178   /* Clean up the temporary directory.  */
179   if (cleanup_temp_dir (output_temp_dir) != 0)
180     fail = true;
181   if (fail)
182     _exit (exit_failure);
183 }
184
185 /* Convert DIVNUM into a temporary file name for use in m4_tmp*.  */
186 static const char *
187 m4_tmpname (int divnum)
188 {
189   static char *buffer;
190   static char *tail;
191   if (buffer == NULL)
192     {
193       tail = xasprintf ("%s/m4-%d", output_temp_dir->dir_name, INT_MAX);
194       buffer = (char *) obstack_copy0 (&diversion_storage, tail,
195                                        strlen (tail));
196       free (tail);
197       tail = strrchr (buffer, '-') + 1;
198     }
199   assert (0 < divnum);
200   sprintf (tail, "%d", divnum);
201   return buffer;
202 }
203
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
210    open file.  */
211 static FILE *
212 m4_tmpfile (int divnum)
213 {
214   const char *name;
215   FILE *file;
216
217   if (output_temp_dir == NULL)
218     {
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);
224     }
225   name = m4_tmpname (divnum);
226   register_temp_file (output_temp_dir, name);
227   file = fopen_temp (name, O_BINARY ? "wb+" : "w+");
228   if (file == NULL)
229     {
230       unregister_temp_file (output_temp_dir, name);
231       M4ERROR ((EXIT_FAILURE, errno,
232                 "cannot create temporary file for diversion"));
233     }
234   else if (set_cloexec_flag (fileno (file), true) != 0)
235     M4ERROR ((warning_status, errno,
236               "Warning: cannot protect diversion across forks"));
237   return file;
238 }
239
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
244    file.  */
245 static FILE *
246 m4_tmpopen (int divnum, bool reread)
247 {
248   const char *name;
249   FILE *file;
250
251   if (tmp_file1_owner == divnum)
252     {
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;
257       return tmp_file1;
258     }
259   else if (tmp_file2_owner == divnum)
260     {
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;
265       return tmp_file2;
266     }
267   name = m4_tmpname (divnum);
268   /* We need update mode, to avoid truncation.  */
269   file = fopen_temp (name, O_BINARY ? "rb+" : "r+");
270   if (file == NULL)
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
276      we want the end.  */
277   else if (!reread && fseeko (file, 0, SEEK_END) != 0)
278     m4_error (EXIT_FAILURE, errno,
279               _("cannot seek within diversion"));
280   return file;
281 }
282
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.  */
288 static int
289 m4_tmpclose (FILE *file, int divnum)
290 {
291   int result = 0;
292   if (divnum != tmp_file1_owner && divnum != tmp_file2_owner)
293     {
294       if (tmp_file2_recent)
295         {
296           if (tmp_file1_owner)
297             result = close_stream_temp (tmp_file1);
298           tmp_file1 = file;
299           tmp_file1_owner = divnum;
300         }
301       else
302         {
303           if (tmp_file2_owner)
304             result = close_stream_temp (tmp_file2);
305           tmp_file2 = file;
306           tmp_file2_owner = divnum;
307         }
308     }
309   return result;
310 }
311
312 /* Delete a closed temporary FILE for diversion DIVNUM.  */
313 static int
314 m4_tmpremove (int divnum)
315 {
316   if (divnum == tmp_file1_owner)
317     {
318       int result = close_stream_temp (tmp_file1);
319       if (result)
320         return result;
321       tmp_file1_owner = 0;
322     }
323   else if (divnum == tmp_file2_owner)
324     {
325       int result = close_stream_temp (tmp_file2);
326       if (result)
327         return result;
328       tmp_file2_owner = 0;
329     }
330   return cleanup_temp_file (output_temp_dir, m4_tmpname (divnum));
331 }
332
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.  */
336 static FILE*
337 m4_tmprename (int oldnum, int newnum)
338 {
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)
344     {
345       /* Be careful of mingw, which can't rename an open file.  */
346       if (RENAME_OPEN_FILE_WORKS)
347         tmp_file1_owner = newnum;
348       else
349         {
350           if (close_stream_temp (tmp_file1))
351             m4_error (EXIT_FAILURE, errno,
352                       _("cannot close temporary file for diversion"));
353           tmp_file1_owner = 0;
354         }
355     }
356   else if (oldnum == tmp_file2_owner)
357     {
358       /* Be careful of mingw, which can't rename an open file.  */
359       if (RENAME_OPEN_FILE_WORKS)
360         tmp_file2_owner = newnum;
361       else
362         {
363           if (close_stream_temp (tmp_file2))
364             m4_error (EXIT_FAILURE, errno,
365                       _("cannot close temporary file for diversion"));
366           tmp_file2_owner = 0;
367         }
368     }
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);
375   free (oldname);
376   return m4_tmpopen (newnum, false);
377 }
378
379 \f
380 /*------------------------.
381 | Output initialization.  |
382 `------------------------*/
383
384 void
385 output_init (void)
386 {
387   diversion_table = gl_oset_create_empty (GL_AVLTREE_OSET, cmp_diversion_CB,
388                                           NULL);
389   div0.u.file = stdout;
390   output_diversion = &div0;
391   output_file = stdout;
392   obstack_init (&diversion_storage);
393 }
394
395 void
396 output_exit (void)
397 {
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;
401   if (tmp_file1_owner)
402     m4_tmpremove (tmp_file1_owner);
403   if (tmp_file2_owner)
404     m4_tmpremove (tmp_file2_owner);
405   diversion_table = NULL;
406   gl_oset_free (table);
407   obstack_free (&diversion_storage, NULL);
408 }
409
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  |
417 | current one.                                                    |
418 `----------------------------------------------------------------*/
419
420 static void
421 make_room_for (int length)
422 {
423   int wanted_size;
424   m4_diversion *selected_diversion = NULL;
425
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.  */
429
430   output_diversion->used = output_diversion->size - output_unused;
431
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)
435     ;
436
437   /* Check if we are exceeding the maximum amount of buffer memory.  */
438
439   if (total_buffer_size - output_diversion->size + wanted_size
440       > MAXIMUM_TOTAL_SIZE)
441     {
442       int selected_used;
443       char *selected_buffer;
444       m4_diversion *diversion;
445       int count;
446       gl_oset_iterator_t iter;
447       const void *elt;
448
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.  */
453
454       selected_diversion = output_diversion;
455       selected_used = output_diversion->used + length;
456
457       iter = gl_oset_iterator (diversion_table);
458       while (gl_oset_iterator_next (&iter, &elt))
459         {
460           diversion = (m4_diversion *) elt;
461           if (diversion->used > selected_used)
462             {
463               selected_diversion = diversion;
464               selected_used = diversion->used;
465             }
466         }
467       gl_oset_iterator_free (&iter);
468
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.  */
474
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);
480
481       if (selected_diversion->used > 0)
482         {
483           count = fwrite (selected_buffer, (size_t) selected_diversion->used,
484                           1, selected_diversion->u.file);
485           if (count != 1)
486             M4ERROR ((EXIT_FAILURE, errno,
487                       "ERROR: cannot flush diversion to temporary file"));
488         }
489
490       /* Reclaim the buffer space for other diversions.  */
491
492       free (selected_buffer);
493       selected_diversion->used = 1;
494     }
495
496   /* Reload output_file, just in case the flushed diversion was current.  */
497
498   if (output_diversion == selected_diversion)
499     {
500       /* The flushed diversion was current indeed.  */
501
502       output_file = output_diversion->u.file;
503       output_cursor = NULL;
504       output_unused = 0;
505     }
506   else
507     {
508       /* Close any selected file since it is not the current diversion.  */
509       if (selected_diversion)
510         {
511           FILE *file = selected_diversion->u.file;
512           selected_diversion->u.file = NULL;
513           if (m4_tmpclose (file, selected_diversion->divnum) != 0)
514             m4_error (0, errno,
515                       _("cannot close temporary file for diversion"));
516         }
517
518       /* The current buffer may be safely reallocated.  */
519       {
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);
523         free (buffer);
524       }
525
526       total_buffer_size += wanted_size - output_diversion->size;
527       output_diversion->size = wanted_size;
528
529       output_cursor = output_diversion->u.buffer + output_diversion->used;
530       output_unused = wanted_size - output_diversion->used;
531     }
532 }
533
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 `--------------------------------------------------------------*/
538
539 #define OUTPUT_CHARACTER(Char) \
540   if (output_file)                                                      \
541     putc ((Char), output_file);                                         \
542   else if (output_unused == 0)                                          \
543     output_character_helper ((Char));                                   \
544   else                                                                  \
545     (output_unused--, *output_cursor++ = (Char))
546
547 static void
548 output_character_helper (int character)
549 {
550   make_room_for (1);
551
552   if (output_file)
553     putc (character, output_file);
554   else
555     {
556       *output_cursor++ = character;
557       output_unused--;
558     }
559 }
560
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 `-------------------------------------------------------------------*/
565
566 void
567 output_text (const char *text, int length)
568 {
569   int count;
570
571   if (!output_diversion || !length)
572     return;
573
574   if (!output_file && length > output_unused)
575     make_room_for (length);
576
577   if (output_file)
578     {
579       count = fwrite (text, length, 1, output_file);
580       if (count != 1)
581         M4ERROR ((EXIT_FAILURE, errno, "ERROR: copying inserted file"));
582     }
583   else
584     {
585       memcpy (output_cursor, text, (size_t) length);
586       output_cursor += length;
587       output_unused -= length;
588     }
589 }
590
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).                                               |
598 |                                                                     |
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 `--------------------------------------------------------------------*/
605
606 void
607 shipout_text (struct obstack *obs, const char *text, int length, int line)
608 {
609   static bool start_of_output_line = true;
610   const char *cursor;
611
612   /* If output goes to an obstack, merely add TEXT to it.  */
613
614   if (obs != NULL)
615     {
616       obstack_grow (obs, text, length);
617       return;
618     }
619
620   /* Do nothing if TEXT should be discarded.  */
621
622   if (output_diversion == NULL)
623     return;
624
625   /* Output TEXT to a file, or in-memory diversion buffer.  */
626
627   if (!sync_output)
628     switch (length)
629       {
630
631         /* In-line short texts.  */
632
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);
641       case 0:
642         return;
643
644         /* Optimize longer texts.  */
645
646       default:
647         output_text (text, length);
648       }
649   else
650     {
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
654          syncline.  */
655       if (start_of_output_line)
656         {
657           start_of_output_line = false;
658           output_current_line++;
659 #ifdef DEBUG_OUTPUT
660           xfprintf (stderr, "DEBUG: line %d, cur %d, cur out %d\n",
661                    line, current_line, output_current_line);
662 #endif
663
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.  */
667
668           if (output_current_line != line)
669             {
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')
679                 {
680                   OUTPUT_CHARACTER (' ');
681                   OUTPUT_CHARACTER ('"');
682                   for (cursor = current_file; *cursor; cursor++)
683                     OUTPUT_CHARACTER (*cursor);
684                   OUTPUT_CHARACTER ('"');
685                 }
686               OUTPUT_CHARACTER ('\n');
687               output_current_line = line;
688             }
689         }
690
691       /* Output the token, and track embedded newlines.  */
692       for (; length-- > 0; text++)
693         {
694           if (start_of_output_line)
695             {
696               start_of_output_line = false;
697               output_current_line++;
698 #ifdef DEBUG_OUTPUT
699               xfprintf (stderr, "DEBUG: line %d, cur %d, cur out %d\n",
700                        line, current_line, output_current_line);
701 #endif
702             }
703           OUTPUT_CHARACTER (*text);
704           if (*text == '\n')
705             start_of_output_line = true;
706         }
707     }
708 }
709 \f
710 /* Functions for use by diversions.  */
711
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 `------------------------------------------------------------------*/
716
717 /* The number of possible diversions is limited only by memory and
718    available file descriptors (each overflowing diversion uses one).  */
719
720 void
721 make_diversion (int divnum)
722 {
723   m4_diversion *diversion = NULL;
724
725   if (current_diversion == divnum)
726     return;
727
728   if (output_diversion)
729     {
730       if (!output_diversion->size && !output_diversion->u.file)
731         {
732           assert (!output_diversion->used);
733           if (!gl_oset_remove (diversion_table, output_diversion))
734             assert (false);
735           output_diversion->u.next = free_list;
736           free_list = output_diversion;
737         }
738       else if (output_diversion->size)
739         output_diversion->used = output_diversion->size - output_unused;
740       else if (output_diversion->used)
741         {
742           FILE *file = output_diversion->u.file;
743           output_diversion->u.file = NULL;
744           if (m4_tmpclose (file, output_diversion->divnum) != 0)
745             m4_error (0, errno,
746                       _("cannot close temporary file for diversion"));
747         }
748       output_diversion = NULL;
749       output_file = NULL;
750       output_cursor = NULL;
751       output_unused = 0;
752     }
753
754   current_diversion = divnum;
755
756   if (divnum < 0)
757     return;
758
759   if (divnum == 0)
760     diversion = &div0;
761   else
762     {
763       const void *elt;
764       if (gl_oset_search_atleast (diversion_table, threshold_diversion_CB,
765                                   &divnum, &elt))
766         {
767           m4_diversion *temp = (m4_diversion *) elt;
768           if (temp->divnum == divnum)
769             diversion = temp;
770         }
771     }
772   if (diversion == NULL)
773     {
774       /* First time visiting this diversion.  */
775       if (free_list)
776         {
777           diversion = free_list;
778           free_list = diversion->u.next;
779         }
780       else
781         {
782           diversion = (m4_diversion *) obstack_alloc (&diversion_storage,
783                                                       sizeof *diversion);
784           diversion->size = 0;
785           diversion->used = 0;
786         }
787       diversion->u.file = NULL;
788       diversion->divnum = divnum;
789       gl_oset_add (diversion_table, diversion);
790     }
791
792   output_diversion = diversion;
793   if (output_diversion->size)
794     {
795       output_cursor = output_diversion->u.buffer + output_diversion->used;
796       output_unused = output_diversion->size - output_diversion->used;
797     }
798   else
799     {
800       if (!output_diversion->u.file && output_diversion->used)
801         output_diversion->u.file = m4_tmpopen (output_diversion->divnum,
802                                                false);
803       output_file = output_diversion->u.file;
804     }
805   output_current_line = -1;
806 }
807
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 `-------------------------------------------------------------------*/
813
814 void
815 insert_file (FILE *file)
816 {
817   static char buffer[COPY_BUFFER_SIZE];
818   size_t length;
819
820   /* Optimize out inserting into a sink.  */
821   if (!output_diversion)
822     return;
823
824   /* Insert output by big chunks.  */
825   while (1)
826     {
827       length = fread (buffer, 1, sizeof buffer, file);
828       if (ferror (file))
829         M4ERROR ((EXIT_FAILURE, errno, "error reading inserted file"));
830       if (length == 0)
831         break;
832       output_text (buffer, length);
833     }
834 }
835
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   |
840 | system.                                                            |
841 `-------------------------------------------------------------------*/
842
843 static void
844 insert_diversion_helper (m4_diversion *diversion)
845 {
846   /* Effectively undivert only if an output stream is active.  */
847   if (output_diversion)
848     {
849       if (diversion->size)
850         {
851           if (!output_diversion->u.file)
852             {
853               /* Transferring diversion metadata is faster than
854                  copying contents.  */
855               assert (!output_diversion->used && output_diversion != &div0
856                       && !output_file);
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;
862             }
863           else
864             {
865               /* Avoid double-charging the total in-memory size when
866                  transferring from one in-memory diversion to
867                  another.  */
868               total_buffer_size -= diversion->size;
869               output_text (diversion->u.buffer, diversion->used);
870             }
871         }
872       else if (!output_diversion->u.file)
873         {
874           /* Transferring diversion metadata is faster than copying
875              contents.  */
876           assert (!output_diversion->used && output_diversion != &div0
877                   && !output_file);
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;
883           diversion->size = 1;
884         }
885       else
886         {
887           if (!diversion->u.file)
888             diversion->u.file = m4_tmpopen (diversion->divnum, true);
889           insert_file (diversion->u.file);
890         }
891
892       output_current_line = -1;
893     }
894
895   /* Return all space used by the diversion.  */
896   if (diversion->size)
897     {
898       if (!output_diversion)
899         total_buffer_size -= diversion->size;
900       free (diversion->u.buffer);
901       diversion->size = 0;
902     }
903   else
904     {
905       if (diversion->u.file)
906         {
907           FILE *file = diversion->u.file;
908           diversion->u.file = NULL;
909           if (m4_tmpclose (file, diversion->divnum) != 0)
910             m4_error (0, errno,
911                       _("cannot clean temporary file for diversion"));
912         }
913       if (m4_tmpremove (diversion->divnum) != 0)
914         M4ERROR ((0, errno, "cannot clean temporary file for diversion"));
915     }
916   diversion->used = 0;
917   gl_oset_remove (diversion_table, diversion);
918   diversion->u.next = free_list;
919   free_list = diversion;
920 }
921
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  |
926 | system.                                                           |
927 `------------------------------------------------------------------*/
928
929 void
930 insert_diversion (int divnum)
931 {
932   const void *elt;
933
934   /* Do not care about nonexistent diversions, and undiverting stdout
935      or self is a no-op.  */
936   if (divnum <= 0 || current_diversion == divnum)
937     return;
938   if (gl_oset_search_atleast (diversion_table, threshold_diversion_CB,
939                               &divnum, &elt))
940     {
941       m4_diversion *diversion = (m4_diversion *) elt;
942       if (diversion->divnum == divnum)
943         insert_diversion_helper (diversion);
944     }
945 }
946
947 /*----------------------------------------------------------------.
948 | Get back all diversions.  This is done just before exiting from |
949 | main, and from m4_undivert (), if called without arguments.     |
950 `----------------------------------------------------------------*/
951
952 void
953 undivert_all (void)
954 {
955   const void *elt;
956   gl_oset_iterator_t iter = gl_oset_iterator (diversion_table);
957   while (gl_oset_iterator_next (&iter, &elt))
958     {
959       m4_diversion *diversion = (m4_diversion *) elt;
960       if (diversion->divnum != current_diversion)
961         insert_diversion_helper (diversion);
962     }
963   gl_oset_iterator_free (&iter);
964 }
965
966 /*-------------------------------------------------------------.
967 | Produce all diversion information in frozen format on FILE.  |
968 `-------------------------------------------------------------*/
969
970 void
971 freeze_diversions (FILE *file)
972 {
973   int saved_number;
974   int last_inserted;
975   gl_oset_iterator_t iter;
976   const void *elt;
977
978   saved_number = current_diversion;
979   last_inserted = 0;
980   make_diversion (0);
981   output_file = file; /* kludge in the frozen file */
982
983   iter = gl_oset_iterator (diversion_table);
984   while (gl_oset_iterator_next (&iter, &elt))
985     {
986       m4_diversion *diversion = (m4_diversion *) elt;
987       if (diversion->size || diversion->used)
988         {
989           if (diversion->size)
990             xfprintf (file, "D%d,%d\n", diversion->divnum, diversion->used);
991           else
992             {
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);
1003             }
1004
1005           insert_diversion_helper (diversion);
1006           putc ('\n', file);
1007
1008           last_inserted = diversion->divnum;
1009         }
1010     }
1011   gl_oset_iterator_free (&iter);
1012
1013   /* Save the active diversion number, if not already.  */
1014
1015   if (saved_number != last_inserted)
1016     xfprintf (file, "D%d,0\n\n", saved_number);
1017 }