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)
172 m4_warn (errno
, NULL
,
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 if (sprintf (tail
, "%d", divnum
) < 0)
201 m4_error (EXIT_FAILURE
, errno
, NULL
,
202 _("cannot create temporary file for diversion"));
206 /* Create a temporary file for diversion DIVNUM open for reading and
207 writing in a secure temp directory. The file will be automatically
208 closed and deleted on a fatal signal. The file can be closed and
209 reopened with m4_tmpclose and m4_tmpopen; when finally done with
210 the file, close it with m4_tmpremove. Exits on failure, so the
211 return value is always an open file. */
213 m4_tmpfile (int divnum
)
218 if (output_temp_dir
== NULL
)
220 output_temp_dir
= create_temp_dir ("m4-", NULL
, true);
221 if (output_temp_dir
== NULL
)
222 m4_error (EXIT_FAILURE
, errno
, NULL
,
223 _("cannot create temporary file for diversion"));
224 atexit (cleanup_tmpfile
);
226 name
= m4_tmpname (divnum
);
227 register_temp_file (output_temp_dir
, name
);
228 file
= fopen_temp (name
, O_BINARY
? "wb+" : "w+");
231 unregister_temp_file (output_temp_dir
, name
);
232 m4_error (EXIT_FAILURE
, errno
, NULL
,
233 _("cannot create temporary file for diversion"));
235 else if (set_cloexec_flag (fileno (file
), true) != 0)
236 m4_warn (errno
, NULL
, _("cannot protect diversion across forks"));
240 /* Reopen a temporary file for diversion DIVNUM for reading and
241 writing in a secure temp directory. Exits on failure, so the
242 return value is always an open file. */
244 m4_tmpopen (int divnum
)
246 const char *name
= m4_tmpname (divnum
);
249 file
= fopen_temp (name
, O_BINARY
? "ab+" : "a+");
251 m4_error (EXIT_FAILURE
, errno
, NULL
,
252 _("cannot create temporary file for diversion"));
253 else if (set_cloexec_flag (fileno (file
), true) != 0)
254 m4_warn (errno
, NULL
, _("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 m4_error (EXIT_FAILURE
, errno
, NULL
,
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 m4_error (EXIT_FAILURE
, errno
, NULL
,
353 _("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 m4_warn (errno
, NULL
,
381 _("cannot close temporary file for diversion"));
384 /* The current buffer may be safely reallocated. */
385 output_diversion
->u
.buffer
386 = xrealloc (output_diversion
->u
.buffer
, (size_t) wanted_size
);
388 total_buffer_size
+= wanted_size
- output_diversion
->size
;
389 output_diversion
->size
= wanted_size
;
391 output_cursor
= output_diversion
->u
.buffer
+ output_diversion
->used
;
392 output_unused
= wanted_size
- output_diversion
->used
;
396 /*------------------------------------------------------------------------.
397 | Output one character CHAR, when it is known that it goes to a diversion |
398 | file or an in-memory diversion buffer. |
399 `------------------------------------------------------------------------*/
401 #define OUTPUT_CHARACTER(Char) \
403 putc ((Char), output_file); \
404 else if (output_unused == 0) \
405 output_character_helper ((Char)); \
407 (output_unused--, *output_cursor++ = (Char))
410 output_character_helper (int character
)
415 putc (character
, output_file
);
418 *output_cursor
++ = character
;
423 /*------------------------------------------------------------------------.
424 | Output one TEXT having LENGTH characters, when it is known that it goes |
425 | to a diversion file or an in-memory diversion buffer. |
426 `------------------------------------------------------------------------*/
429 output_text (const char *text
, int length
)
433 if (!output_diversion
|| !length
)
436 if (!output_file
&& length
> output_unused
)
437 make_room_for (length
);
441 count
= fwrite (text
, length
, 1, output_file
);
443 m4_error (EXIT_FAILURE
, errno
, NULL
, _("error copying inserted file"));
447 memcpy (output_cursor
, text
, (size_t) length
);
448 output_cursor
+= length
;
449 output_unused
-= length
;
453 /*--------------------------------------------------------------------.
454 | Add some text into an obstack OBS, taken from TEXT, having LENGTH |
455 | characters. If OBS is NULL, output the text to an external file |
456 | or an in-memory diversion buffer instead. If OBS is NULL, and |
457 | there is no output file, the text is discarded. LINE is the line |
458 | where the token starts (not necessarily current_line, in the case |
459 | of multiline tokens). |
461 | If we are generating sync lines, the output has to be examined, |
462 | because we need to know how much output each input line generates. |
463 | In general, sync lines are output whenever a single input lines |
464 | generates several output lines, or when several input lines do not |
465 | generate any output. |
466 `--------------------------------------------------------------------*/
469 shipout_text (struct obstack
*obs
, const char *text
, int length
, int line
)
471 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 xfprintf (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 OUTPUT_CHARACTER ('#');
533 OUTPUT_CHARACTER ('l');
534 OUTPUT_CHARACTER ('i');
535 OUTPUT_CHARACTER ('n');
536 OUTPUT_CHARACTER ('e');
537 OUTPUT_CHARACTER (' ');
538 for (cursor
= ntoa (line
, 10); *cursor
; cursor
++)
539 OUTPUT_CHARACTER (*cursor
);
540 if (output_current_line
< 1 && current_file
[0] != '\0')
542 OUTPUT_CHARACTER (' ');
543 OUTPUT_CHARACTER ('"');
544 for (cursor
= current_file
; *cursor
; cursor
++)
545 OUTPUT_CHARACTER (*cursor
);
546 OUTPUT_CHARACTER ('"');
548 OUTPUT_CHARACTER ('\n');
549 output_current_line
= line
;
553 /* Output the token, and track embedded newlines. */
554 for (; length
-- > 0; text
++)
556 if (start_of_output_line
)
558 start_of_output_line
= false;
559 output_current_line
++;
561 xfprintf (stderr
, "DEBUG: line %d, cur %d, cur out %d\n",
562 line
, current_line
, output_current_line
);
565 OUTPUT_CHARACTER (*text
);
567 start_of_output_line
= true;
572 /* Functions for use by diversions. */
574 /*--------------------------------------------------------------------------.
575 | Make a file for diversion DIVNUM, and install it in the diversion table. |
576 | Grow the size of the diversion table as needed. |
577 `--------------------------------------------------------------------------*/
579 /* The number of possible diversions is limited only by memory and
580 available file descriptors (each overflowing diversion uses one). */
583 make_diversion (int divnum
)
585 m4_diversion
*diversion
= NULL
;
587 if (current_diversion
== divnum
)
590 if (output_diversion
)
592 if (!output_diversion
->size
&& !output_diversion
->u
.file
)
594 if (!gl_oset_remove (diversion_table
, output_diversion
))
596 output_diversion
->u
.next
= free_list
;
597 output_diversion
->used
= 0;
598 free_list
= output_diversion
;
600 else if (output_diversion
->size
)
601 output_diversion
->used
= output_diversion
->size
- output_unused
;
602 else if (output_diversion
->used
)
604 FILE *file
= output_diversion
->u
.file
;
605 output_diversion
->u
.file
= NULL
;
606 if (m4_tmpclose (file
) != 0)
607 m4_warn (errno
, NULL
,
608 _("cannot close temporary file for diversion"));
610 output_diversion
= NULL
;
612 output_cursor
= NULL
;
616 current_diversion
= divnum
;
626 if (gl_oset_search_atleast (diversion_table
, threshold_diversion_CB
,
629 m4_diversion
*temp
= (m4_diversion
*) elt
;
630 if (temp
->divnum
== divnum
)
634 if (diversion
== NULL
)
636 /* First time visiting this diversion. */
639 diversion
= free_list
;
640 free_list
= diversion
->u
.next
;
644 diversion
= (m4_diversion
*) obstack_alloc (&diversion_storage
,
649 diversion
->u
.file
= NULL
;
650 diversion
->divnum
= divnum
;
651 gl_oset_add (diversion_table
, diversion
);
654 output_diversion
= diversion
;
655 if (output_diversion
->size
)
657 output_cursor
= output_diversion
->u
.buffer
+ output_diversion
->used
;
658 output_unused
= output_diversion
->size
- output_diversion
->used
;
662 if (!output_diversion
->u
.file
&& output_diversion
->used
)
663 output_diversion
->u
.file
= m4_tmpopen (output_diversion
->divnum
);
664 output_file
= output_diversion
->u
.file
;
666 output_current_line
= -1;
669 /*-------------------------------------------------------------------.
670 | Insert a FILE into the current output file, in the same manner |
671 | diversions are handled. This allows files to be included, without |
672 | having them rescanned by m4. |
673 `-------------------------------------------------------------------*/
676 insert_file (FILE *file
)
678 char buffer
[COPY_BUFFER_SIZE
];
681 /* Optimize out inserting into a sink. */
683 if (!output_diversion
)
686 /* Insert output by big chunks. */
690 length
= fread (buffer
, 1, COPY_BUFFER_SIZE
, file
);
692 m4_error (EXIT_FAILURE
, errno
, NULL
, _("error reading inserted file"));
695 output_text (buffer
, length
);
699 /*-------------------------------------------------------------------.
700 | Insert DIVERSION (but not div0) into the current output file. The |
701 | diversion is NOT placed on the expansion obstack, because it must |
702 | not be rescanned. When the file is closed, it is deleted by the |
704 `-------------------------------------------------------------------*/
707 insert_diversion_helper (m4_diversion
*diversion
)
709 /* Effectively undivert only if an output stream is active. */
710 if (output_diversion
)
713 output_text (diversion
->u
.buffer
, diversion
->used
);
716 if (!diversion
->u
.file
)
717 diversion
->u
.file
= m4_tmpopen (diversion
->divnum
);
718 insert_file (diversion
->u
.file
);
721 output_current_line
= -1;
724 /* Return all space used by the diversion. */
727 free (diversion
->u
.buffer
);
733 if (diversion
->u
.file
)
735 FILE *file
= diversion
->u
.file
;
736 diversion
->u
.file
= NULL
;
738 if (m4_tmpclose (file
) != 0)
739 m4_warn (errno
, NULL
,
740 _("cannot clean temporary file for diversion"));
742 if (m4_tmpremove (diversion
->divnum
) != 0)
743 m4_warn (errno
, NULL
, _("cannot clean temporary file for diversion"));
745 gl_oset_remove (diversion_table
, diversion
);
746 diversion
->u
.next
= free_list
;
747 free_list
= diversion
;
750 /*-------------------------------------------------------------------------.
751 | Insert diversion number DIVNUM into the current output file. The |
752 | diversion is NOT placed on the expansion obstack, because it must not be |
753 | rescanned. When the file is closed, it is deleted by the system. |
754 `-------------------------------------------------------------------------*/
757 insert_diversion (int divnum
)
761 /* Do not care about nonexistent diversions, and undiverting stdout
762 or self is a no-op. */
763 if (divnum
<= 0 || current_diversion
== divnum
)
765 if (gl_oset_search_atleast (diversion_table
, threshold_diversion_CB
,
768 m4_diversion
*diversion
= (m4_diversion
*) elt
;
769 if (diversion
->divnum
== divnum
)
770 insert_diversion_helper (diversion
);
774 /*-------------------------------------------------------------------------.
775 | Get back all diversions. This is done just before exiting from main (), |
776 | and from m4_undivert (), if called without arguments. |
777 `-------------------------------------------------------------------------*/
783 gl_oset_iterator_t iter
= gl_oset_iterator (diversion_table
);
784 while (gl_oset_iterator_next (&iter
, &elt
))
786 m4_diversion
*diversion
= (m4_diversion
*) elt
;
787 if (diversion
->divnum
!= current_diversion
)
788 insert_diversion_helper (diversion
);
790 gl_oset_iterator_free (&iter
);
793 /*-------------------------------------------------------------.
794 | Produce all diversion information in frozen format on FILE. |
795 `-------------------------------------------------------------*/
798 freeze_diversions (FILE *file
)
802 gl_oset_iterator_t iter
;
805 saved_number
= current_diversion
;
808 output_file
= file
; /* kludge in the frozen file */
810 iter
= gl_oset_iterator (diversion_table
);
811 while (gl_oset_iterator_next (&iter
, &elt
))
813 m4_diversion
*diversion
= (m4_diversion
*) elt
;;
814 if (diversion
->size
|| diversion
->used
)
817 xfprintf (file
, "D%d,%d\n", diversion
->divnum
, diversion
->used
);
820 struct stat file_stat
;
821 diversion
->u
.file
= m4_tmpopen (diversion
->divnum
);
822 if (fstat (fileno (diversion
->u
.file
), &file_stat
) < 0)
823 m4_error (EXIT_FAILURE
, errno
, NULL
,
824 _("cannot stat diversion"));
825 if (file_stat
.st_size
< 0
826 || file_stat
.st_size
!= (unsigned long int) file_stat
.st_size
)
827 m4_error (EXIT_FAILURE
, 0, NULL
, _("diversion too large"));
828 xfprintf (file
, "D%d,%lu\n", diversion
->divnum
,
829 (unsigned long int) file_stat
.st_size
);
832 insert_diversion_helper (diversion
);
835 last_inserted
= diversion
->divnum
;
838 gl_oset_iterator_free (&iter
);
840 /* Save the active diversion number, if not already. */
842 if (saved_number
!= last_inserted
)
843 xfprintf (file
, "D%d,0\n\n", saved_number
);