1 /* GNU m4 -- A simple macro processor
3 Copyright (C) 1989, 1990, 1991, 1992, 1993, 1994, 2004, 2005, 2006,
4 2007 Free Software Foundation, Inc.
6 This file is part of GNU M4.
8 GNU M4 is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU M4 is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #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
; /* in-memory 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 length in characters */
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 discarded. */
90 static m4_diversion
*output_diversion
;
92 /* Values of some output_diversion fields, cached out for speed. */
93 static FILE *output_file
; /* current value of (file) */
94 static char *output_cursor
; /* current value of (buffer + used) */
95 static int output_unused
; /* current value of (size - used) */
97 /* Number of input line we are generating output for. */
98 int output_current_line
;
100 /* Temporary directory holding all spilled diversion files. */
101 static m4_temp_dir
*output_temp_dir
;
105 /*------------------------.
106 | Output initialization. |
107 `------------------------*/
109 /* Callback for comparing list elements ELT1 and ELT2 for order in
112 cmp_diversion_CB (const void *elt1
, const void *elt2
)
114 const m4_diversion
*d1
= (const m4_diversion
*) elt1
;
115 const m4_diversion
*d2
= (const m4_diversion
*) elt2
;
116 /* No need to worry about overflow, since we don't create diversions
117 with negative divnum. */
118 return d1
->divnum
- d2
->divnum
;
121 /* Callback for comparing list element ELT against THRESHOLD. */
123 threshold_diversion_CB (const void *elt
, const void *threshold
)
125 const m4_diversion
*div
= (const m4_diversion
*) elt
;
126 /* No need to worry about overflow, since we don't create diversions
127 with negative divnum. */
128 return div
->divnum
>= *(const int *) threshold
;
134 diversion_table
= gl_oset_create_empty (GL_AVLTREE_OSET
, cmp_diversion_CB
,
136 div0
.u
.file
= stdout
;
137 output_diversion
= &div0
;
138 output_file
= stdout
;
139 obstack_init (&diversion_storage
);
145 /* Order is important, since we may have registered cleanup_tmpfile
146 as an atexit handler, and it must not traverse stale memory. */
147 gl_oset_t table
= diversion_table
;
148 diversion_table
= NULL
;
149 gl_oset_free (table
);
150 obstack_free (&diversion_storage
, NULL
);
153 /* Clean up any temporary directory. Designed for use as an atexit
154 handler, where it is not safe to call exit() recursively; so this
155 calls _exit if a problem is encountered. */
157 cleanup_tmpfile (void)
159 /* Close any open diversions. */
165 gl_oset_iterator_t iter
= gl_oset_iterator (diversion_table
);
166 while (gl_oset_iterator_next (&iter
, &elt
))
168 m4_diversion
*diversion
= (m4_diversion
*) elt
;
169 if (!diversion
->size
&& diversion
->u
.file
170 && close_stream_temp (diversion
->u
.file
) != 0)
173 "cannot clean temporary file for diversion"));
177 gl_oset_iterator_free (&iter
);
180 /* Clean up the temporary directory. */
181 if (cleanup_temp_dir (output_temp_dir
) != 0)
184 _exit (exit_failure
);
187 /* Convert DIVNUM into a temporary file name for use in m4_tmp*. */
189 m4_tmpname (int divnum
)
195 tail
= xasprintf ("%s/m4-%d", output_temp_dir
->dir_name
, INT_MAX
);
196 buffer
= obstack_copy0 (&diversion_storage
, tail
, strlen (tail
));
198 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; when finally done with
208 the file, close it with m4_tmpremove. Exits on failure, so the
209 return value is always an open file. */
211 m4_tmpfile (int divnum
)
216 if (output_temp_dir
== NULL
)
218 output_temp_dir
= create_temp_dir ("m4-", NULL
, true);
219 if (output_temp_dir
== NULL
)
220 M4ERROR ((EXIT_FAILURE
, errno
,
221 "cannot create temporary file for diversion"));
222 atexit (cleanup_tmpfile
);
224 name
= m4_tmpname (divnum
);
225 register_temp_file (output_temp_dir
, name
);
226 file
= fopen_temp (name
, O_BINARY
? "wb+" : "w+");
229 unregister_temp_file (output_temp_dir
, name
);
230 M4ERROR ((EXIT_FAILURE
, errno
,
231 "cannot create temporary file for diversion"));
233 else if (set_cloexec_flag (fileno (file
), true) != 0)
234 M4ERROR ((warning_status
, errno
,
235 "Warning: cannot protect diversion across forks"));
239 /* Reopen a temporary file for diversion DIVNUM for reading and
240 writing in a secure temp directory. Exits on failure, so the
241 return value is always an open file. */
243 m4_tmpopen (int divnum
)
245 const char *name
= m4_tmpname (divnum
);
248 file
= fopen_temp (name
, O_BINARY
? "ab+" : "a+");
250 M4ERROR ((EXIT_FAILURE
, errno
,
251 "cannot create temporary file for diversion"));
252 else if (set_cloexec_flag (fileno (file
), true) != 0)
253 M4ERROR ((warning_status
, errno
,
254 "Warning: cannot protect diversion across forks"));
255 /* POSIX states that it is undefined whether an append stream starts
256 at offset 0 or at the end. We want the beginning. */
257 else if (fseeko (file
, 0, SEEK_SET
) != 0)
258 M4ERROR ((EXIT_FAILURE
, errno
,
259 "cannot seek to beginning of diversion"));
263 /* Close, but don't delete, a temporary FILE. */
265 m4_tmpclose (FILE *file
)
267 return close_stream_temp (file
);
270 /* Delete a closed temporary FILE for diversion DIVNUM. */
272 m4_tmpremove (int divnum
)
274 return cleanup_temp_file (output_temp_dir
, m4_tmpname (divnum
));
277 /*-----------------------------------------------------------------------.
278 | Reorganize in-memory diversion buffers so the current diversion can |
279 | accomodate LENGTH more characters without further reorganization. The |
280 | current diversion buffer is made bigger if possible. But to make room |
281 | for a bigger buffer, one of the in-memory diversion buffers might have |
282 | to be flushed to a newly created temporary file. This flushed buffer |
283 | might well be the current one. |
284 `-----------------------------------------------------------------------*/
287 make_room_for (int length
)
290 m4_diversion
*selected_diversion
= NULL
;
292 /* Compute needed size for in-memory buffer. Diversions in-memory
293 buffers start at 0 bytes, then 512, then keep doubling until it is
294 decided to flush them to disk. */
296 output_diversion
->used
= output_diversion
->size
- output_unused
;
298 for (wanted_size
= output_diversion
->size
;
299 wanted_size
< output_diversion
->used
+ length
;
300 wanted_size
= wanted_size
== 0 ? INITIAL_BUFFER_SIZE
: wanted_size
* 2)
303 /* Check if we are exceeding the maximum amount of buffer memory. */
305 if (total_buffer_size
- output_diversion
->size
+ wanted_size
306 > MAXIMUM_TOTAL_SIZE
)
309 char *selected_buffer
;
310 m4_diversion
*diversion
;
312 gl_oset_iterator_t iter
;
315 /* Find out the buffer having most data, in view of flushing it to
316 disk. Fake the current buffer as having already received the
317 projected data, while making the selection. So, if it is
318 selected indeed, we will flush it smaller, before it grows. */
320 selected_diversion
= output_diversion
;
321 selected_used
= output_diversion
->used
+ length
;
323 iter
= gl_oset_iterator (diversion_table
);
324 while (gl_oset_iterator_next (&iter
, &elt
))
326 diversion
= (m4_diversion
*) elt
;
327 if (diversion
->used
> selected_used
)
329 selected_diversion
= diversion
;
330 selected_used
= diversion
->used
;
333 gl_oset_iterator_free (&iter
);
335 /* Create a temporary file, write the in-memory buffer of the
336 diversion to this file, then release the buffer. Zero the
337 diversion before doing anything that can exit () (including
338 m4_tmpfile), so that the atexit handler doesn't try to close
339 a garbage pointer as a file. */
341 selected_buffer
= selected_diversion
->u
.buffer
;
342 total_buffer_size
-= selected_diversion
->size
;
343 selected_diversion
->size
= 0;
344 selected_diversion
->u
.file
= NULL
;
345 selected_diversion
->u
.file
= m4_tmpfile (selected_diversion
->divnum
);
347 if (selected_diversion
->used
> 0)
349 count
= fwrite (selected_buffer
, (size_t) selected_diversion
->used
,
350 1, selected_diversion
->u
.file
);
352 M4ERROR ((EXIT_FAILURE
, errno
,
353 "ERROR: cannot flush diversion to temporary file"));
356 /* Reclaim the buffer space for other diversions. */
358 free (selected_buffer
);
359 selected_diversion
->used
= 1;
362 /* Reload output_file, just in case the flushed diversion was current. */
364 if (output_diversion
== selected_diversion
)
366 /* The flushed diversion was current indeed. */
368 output_file
= output_diversion
->u
.file
;
369 output_cursor
= NULL
;
374 /* Close any selected file since it is not the current diversion. */
375 if (selected_diversion
)
377 FILE *file
= selected_diversion
->u
.file
;
378 selected_diversion
->u
.file
= NULL
;
379 if (m4_tmpclose (file
) != 0)
380 M4ERROR ((0, errno
, "cannot close temporary file for diversion"));
383 /* The current buffer may be safely reallocated. */
384 output_diversion
->u
.buffer
385 = xrealloc (output_diversion
->u
.buffer
, (size_t) wanted_size
);
387 total_buffer_size
+= wanted_size
- output_diversion
->size
;
388 output_diversion
->size
= wanted_size
;
390 output_cursor
= output_diversion
->u
.buffer
+ output_diversion
->used
;
391 output_unused
= wanted_size
- output_diversion
->used
;
395 /*------------------------------------------------------------------------.
396 | Output one character CHAR, when it is known that it goes to a diversion |
397 | file or an in-memory diversion buffer. |
398 `------------------------------------------------------------------------*/
400 #define OUTPUT_CHARACTER(Char) \
402 putc ((Char), output_file); \
403 else if (output_unused == 0) \
404 output_character_helper ((Char)); \
406 (output_unused--, *output_cursor++ = (Char))
409 output_character_helper (int character
)
414 putc (character
, output_file
);
417 *output_cursor
++ = character
;
422 /*------------------------------------------------------------------------.
423 | Output one TEXT having LENGTH characters, when it is known that it goes |
424 | to a diversion file or an in-memory diversion buffer. |
425 `------------------------------------------------------------------------*/
428 output_text (const char *text
, int length
)
432 if (!output_diversion
|| !length
)
435 if (!output_file
&& length
> output_unused
)
436 make_room_for (length
);
440 count
= fwrite (text
, length
, 1, output_file
);
442 M4ERROR ((EXIT_FAILURE
, errno
, "ERROR: copying inserted file"));
446 memcpy (output_cursor
, text
, (size_t) length
);
447 output_cursor
+= length
;
448 output_unused
-= length
;
452 /*--------------------------------------------------------------------.
453 | Add some text into an obstack OBS, taken from TEXT, having LENGTH |
454 | characters. If OBS is NULL, output the text to an external file |
455 | or an in-memory diversion buffer instead. If OBS is NULL, and |
456 | there is no output file, the text is discarded. LINE is the line |
457 | where the token starts (not necessarily current_line, in the case |
458 | of multiline tokens). |
460 | If we are generating sync lines, the output has to be examined, |
461 | because we need to know how much output each input line generates. |
462 | In general, sync lines are output whenever a single input lines |
463 | generates several output lines, or when several input lines do not |
464 | generate any output. |
465 `--------------------------------------------------------------------*/
468 shipout_text (struct obstack
*obs
, const char *text
, int length
, int line
)
470 static bool start_of_output_line
= true;
474 /* If output goes to an obstack, merely add TEXT to it. */
478 obstack_grow (obs
, text
, length
);
482 /* Do nothing if TEXT should be discarded. */
484 if (output_diversion
== NULL
)
487 /* Output TEXT to a file, or in-memory diversion buffer. */
493 /* In-line short texts. */
495 case 8: OUTPUT_CHARACTER (*text
); text
++;
496 case 7: OUTPUT_CHARACTER (*text
); text
++;
497 case 6: OUTPUT_CHARACTER (*text
); text
++;
498 case 5: OUTPUT_CHARACTER (*text
); text
++;
499 case 4: OUTPUT_CHARACTER (*text
); text
++;
500 case 3: OUTPUT_CHARACTER (*text
); text
++;
501 case 2: OUTPUT_CHARACTER (*text
); text
++;
502 case 1: OUTPUT_CHARACTER (*text
);
506 /* Optimize longer texts. */
509 output_text (text
, length
);
513 /* Check for syncline only at the start of a token. Multiline
514 tokens, and tokens that are out of sync but in the middle of
515 the line, must wait until the next raw newline triggers a
517 if (start_of_output_line
)
519 start_of_output_line
= false;
520 output_current_line
++;
522 fprintf (stderr
, "DEBUG: line %d, cur %d, cur out %d\n",
523 line
, current_line
, output_current_line
);
526 /* Output a `#line NUM' synchronization directive if needed.
527 If output_current_line was previously given a negative
528 value (invalidated), output `#line NUM "FILE"' instead. */
530 if (output_current_line
!= line
)
532 sprintf (linebuf
, "#line %d", line
);
533 for (cursor
= linebuf
; *cursor
; cursor
++)
534 OUTPUT_CHARACTER (*cursor
);
535 if (output_current_line
< 1 && current_file
[0] != '\0')
537 OUTPUT_CHARACTER (' ');
538 OUTPUT_CHARACTER ('"');
539 for (cursor
= current_file
; *cursor
; cursor
++)
540 OUTPUT_CHARACTER (*cursor
);
541 OUTPUT_CHARACTER ('"');
543 OUTPUT_CHARACTER ('\n');
544 output_current_line
= line
;
548 /* Output the token, and track embedded newlines. */
549 for (; length
-- > 0; text
++)
551 if (start_of_output_line
)
553 start_of_output_line
= false;
554 output_current_line
++;
556 fprintf (stderr
, "DEBUG: line %d, cur %d, cur out %d\n",
557 line
, current_line
, output_current_line
);
560 OUTPUT_CHARACTER (*text
);
562 start_of_output_line
= true;
567 /* Functions for use by diversions. */
569 /*--------------------------------------------------------------------------.
570 | Make a file for diversion DIVNUM, and install it in the diversion table. |
571 | Grow the size of the diversion table as needed. |
572 `--------------------------------------------------------------------------*/
574 /* The number of possible diversions is limited only by memory and
575 available file descriptors (each overflowing diversion uses one). */
578 make_diversion (int divnum
)
580 m4_diversion
*diversion
= NULL
;
582 if (current_diversion
== divnum
)
585 if (output_diversion
)
587 if (!output_diversion
->size
&& !output_diversion
->u
.file
)
589 if (!gl_oset_remove (diversion_table
, output_diversion
))
590 error (EXIT_FAILURE
, 0, "INTERNAL ERROR: make_diversion failed");
591 output_diversion
->u
.next
= free_list
;
592 output_diversion
->used
= 0;
593 free_list
= output_diversion
;
595 else if (output_diversion
->size
)
596 output_diversion
->used
= output_diversion
->size
- output_unused
;
597 else if (output_diversion
->used
)
599 FILE *file
= output_diversion
->u
.file
;
600 output_diversion
->u
.file
= NULL
;
601 if (m4_tmpclose (file
) != 0)
602 M4ERROR ((0, errno
, "cannot close temporary file for diversion"));
604 output_diversion
= NULL
;
606 output_cursor
= NULL
;
610 current_diversion
= divnum
;
620 if (gl_oset_search_atleast (diversion_table
, threshold_diversion_CB
,
623 m4_diversion
*temp
= (m4_diversion
*) elt
;
624 if (temp
->divnum
== divnum
)
628 if (diversion
== NULL
)
630 /* First time visiting this diversion. */
633 diversion
= free_list
;
634 free_list
= diversion
->u
.next
;
638 diversion
= (m4_diversion
*) obstack_alloc (&diversion_storage
,
643 diversion
->u
.file
= NULL
;
644 diversion
->divnum
= divnum
;
645 gl_oset_add (diversion_table
, diversion
);
648 output_diversion
= diversion
;
649 if (output_diversion
->size
)
651 output_cursor
= output_diversion
->u
.buffer
+ output_diversion
->used
;
652 output_unused
= output_diversion
->size
- output_diversion
->used
;
656 if (!output_diversion
->u
.file
&& output_diversion
->used
)
657 output_diversion
->u
.file
= m4_tmpopen (output_diversion
->divnum
);
658 output_file
= output_diversion
->u
.file
;
660 output_current_line
= -1;
663 /*-------------------------------------------------------------------.
664 | Insert a FILE into the current output file, in the same manner |
665 | diversions are handled. This allows files to be included, without |
666 | having them rescanned by m4. |
667 `-------------------------------------------------------------------*/
670 insert_file (FILE *file
)
672 char buffer
[COPY_BUFFER_SIZE
];
675 /* Optimize out inserting into a sink. */
677 if (!output_diversion
)
680 /* Insert output by big chunks. */
684 length
= fread (buffer
, 1, COPY_BUFFER_SIZE
, file
);
686 M4ERROR ((EXIT_FAILURE
, errno
, "ERROR: reading inserted file"));
689 output_text (buffer
, length
);
693 /*-------------------------------------------------------------------.
694 | Insert DIVERSION (but not div0) into the current output file. The |
695 | diversion is NOT placed on the expansion obstack, because it must |
696 | not be rescanned. When the file is closed, it is deleted by the |
698 `-------------------------------------------------------------------*/
701 insert_diversion_helper (m4_diversion
*diversion
)
703 /* Effectively undivert only if an output stream is active. */
704 if (output_diversion
)
707 output_text (diversion
->u
.buffer
, diversion
->used
);
710 if (!diversion
->u
.file
)
711 diversion
->u
.file
= m4_tmpopen (diversion
->divnum
);
712 insert_file (diversion
->u
.file
);
715 output_current_line
= -1;
718 /* Return all space used by the diversion. */
721 free (diversion
->u
.buffer
);
727 if (diversion
->u
.file
)
729 FILE *file
= diversion
->u
.file
;
730 diversion
->u
.file
= NULL
;
732 if (m4_tmpclose (file
) != 0)
733 M4ERROR ((0, errno
, "cannot clean temporary file for diversion"));
735 if (m4_tmpremove (diversion
->divnum
) != 0)
736 M4ERROR ((0, errno
, "cannot clean temporary file for diversion"));
738 gl_oset_remove (diversion_table
, diversion
);
739 diversion
->u
.next
= free_list
;
740 free_list
= diversion
;
743 /*-------------------------------------------------------------------------.
744 | Insert diversion number DIVNUM into the current output file. The |
745 | diversion is NOT placed on the expansion obstack, because it must not be |
746 | rescanned. When the file is closed, it is deleted by the system. |
747 `-------------------------------------------------------------------------*/
750 insert_diversion (int divnum
)
754 /* Do not care about nonexistent diversions, and undiverting stdout
755 or self is a no-op. */
756 if (divnum
<= 0 || current_diversion
== divnum
)
758 if (gl_oset_search_atleast (diversion_table
, threshold_diversion_CB
,
761 m4_diversion
*diversion
= (m4_diversion
*) elt
;
762 if (diversion
->divnum
== divnum
)
763 insert_diversion_helper (diversion
);
767 /*-------------------------------------------------------------------------.
768 | Get back all diversions. This is done just before exiting from main (), |
769 | and from m4_undivert (), if called without arguments. |
770 `-------------------------------------------------------------------------*/
776 gl_oset_iterator_t iter
= gl_oset_iterator (diversion_table
);
777 while (gl_oset_iterator_next (&iter
, &elt
))
779 m4_diversion
*diversion
= (m4_diversion
*) elt
;
780 if (diversion
->divnum
!= current_diversion
)
781 insert_diversion_helper (diversion
);
783 gl_oset_iterator_free (&iter
);
786 /*-------------------------------------------------------------.
787 | Produce all diversion information in frozen format on FILE. |
788 `-------------------------------------------------------------*/
791 freeze_diversions (FILE *file
)
795 gl_oset_iterator_t iter
;
798 saved_number
= current_diversion
;
801 output_file
= file
; /* kludge in the frozen file */
803 iter
= gl_oset_iterator (diversion_table
);
804 while (gl_oset_iterator_next (&iter
, &elt
))
806 m4_diversion
*diversion
= (m4_diversion
*) elt
;;
807 if (diversion
->size
|| diversion
->used
)
810 fprintf (file
, "D%d,%d\n", diversion
->divnum
, diversion
->used
);
813 struct stat file_stat
;
814 diversion
->u
.file
= m4_tmpopen (diversion
->divnum
);
815 if (fstat (fileno (diversion
->u
.file
), &file_stat
) < 0)
816 M4ERROR ((EXIT_FAILURE
, errno
, "cannot stat diversion"));
817 if (file_stat
.st_size
< 0
818 || file_stat
.st_size
!= (unsigned long int) file_stat
.st_size
)
819 M4ERROR ((EXIT_FAILURE
, 0, "diversion too large"));
820 fprintf (file
, "D%d,%lu\n", diversion
->divnum
,
821 (unsigned long int) file_stat
.st_size
);
824 insert_diversion_helper (diversion
);
827 last_inserted
= diversion
->divnum
;
830 gl_oset_iterator_free (&iter
);
832 /* Save the active diversion number, if not already. */
834 if (saved_number
!= last_inserted
)
835 fprintf (file
, "D%d,0\n\n", saved_number
);