home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / m4-1.4-src.tgz / tar.out / fsf / m4 / src / output.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  17KB  |  591 lines

  1. /* GNU m4 -- A simple macro processor
  2.    Copyright (C) 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
  3.   
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.   
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.   
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18.  
  19. #include "m4.h"
  20.  
  21. #include <sys/stat.h>
  22.  
  23. /* Size of initial in-memory buffer size for diversions.  Small diversions
  24.    would usually fit in.  */
  25. #define INITIAL_BUFFER_SIZE 512
  26.  
  27. /* Maximum value for the total of all in-memory buffer sizes for
  28.    diversions.  */
  29. #define MAXIMUM_TOTAL_SIZE (512 * 1024)
  30.  
  31. /* Size of buffer size to use while copying files.  */
  32. #define COPY_BUFFER_SIZE (32 * 512)
  33.  
  34. #ifdef HAVE_TMPFILE
  35. extern FILE *tmpfile ();
  36. #endif
  37.  
  38. /* Output functions.  Most of the complexity is for handling cpp like
  39.    sync lines.
  40.   
  41.    This code is fairly entangled with the code in input.c, and maybe it
  42.    belongs there?  */
  43.  
  44. /* In a struct diversion, only one of file or buffer be may non-NULL,
  45.    depending on the fact output is diverted to a file or in memory
  46.    buffer.  Further, if buffer is NULL, then pointer is NULL, size and
  47.    unused are zero.  */
  48.  
  49. struct diversion
  50.   {
  51.     FILE *file;            /* diversion file on disk */
  52.     char *buffer;        /* in-memory diversion buffer */
  53.     int size;            /* usable size before reallocation */
  54.     int used;            /* used length in characters */
  55.   };
  56.  
  57. /* Table of diversions.  */
  58. static struct diversion *diversion_table;
  59.  
  60. /* Number of entries in diversion table.  */
  61. static int diversions;
  62.  
  63. /* Total size of all in-memory buffer sizes.  */
  64. static int total_buffer_size;
  65.  
  66. /* The number of the currently active diversion.  This variable is
  67.    maintained for the `divnum' builtin function.  */
  68. int current_diversion;
  69.  
  70. /* Current output diversion, NULL if output is being currently discarded.  */
  71. static struct diversion *output_diversion;
  72.  
  73. /* Values of some output_diversion fields, cached out for speed.  */
  74. static FILE *output_file;    /* current value of (file) */
  75. static char *output_cursor;    /* current value of (buffer + used) */
  76. static int output_unused;    /* current value of (size - used) */
  77.  
  78. /* Number of input line we are generating output for.  */
  79. int output_current_line;
  80.  
  81. /*------------------------.
  82. | Output initialisation.  |
  83. `------------------------*/
  84.  
  85. void
  86. output_init (void)
  87. {
  88.   diversion_table = (struct diversion *) xmalloc (sizeof (struct diversion));
  89.   diversions = 1;
  90.   diversion_table[0].file = stdout;
  91.   diversion_table[0].buffer = NULL;
  92.   diversion_table[0].size = 0;
  93.   diversion_table[0].used = 0;
  94.  
  95.   total_buffer_size = 0;
  96.   current_diversion = 0;
  97.   output_diversion = diversion_table;
  98.   output_file = stdout;
  99.   output_cursor = NULL;
  100.   output_unused = 0;
  101. }
  102.  
  103. #ifndef HAVE_TMPFILE
  104.  
  105. #ifndef HAVE_MKSTEMP
  106.  
  107. /* This implementation of mkstemp(3) does not avoid any races, but its
  108.    there.  */
  109.  
  110. #include <fcntl.h>
  111.  
  112. static int
  113. mkstemp (const char *tmpl)
  114. {
  115.   mktemp (tmpl);
  116.   return open (tmpl, O_RDWR | O_TRUNC | O_CREAT, 0600);
  117. }
  118.  
  119. #endif /* not HAVE_MKSTEMP */
  120.  
  121. /* Implement tmpfile(3) for non-USG systems.  */
  122.  
  123. static FILE *
  124. tmpfile (void)
  125. {
  126.   char buf[32];
  127.   int fd;
  128.  
  129.   strcpy (buf, "/tmp/m4XXXXXX");
  130.   fd = mkstemp (buf);
  131.   if (fd < 0)
  132.     return NULL;
  133.  
  134.   unlink (buf);
  135.   return fdopen (fd, "w+");
  136. }
  137.  
  138. #endif /* not HAVE_TMPFILE */
  139.  
  140. /*-----------------------------------------------------------------------.
  141. | Reorganize in-memory diversion buffers so the current diversion can     |
  142. | accomodate LENGTH more characters without further reorganization.  The |
  143. | current diversion buffer is made bigger if possible.  But to make room |
  144. | for a bigger buffer, one of the in-memory diversion buffers might have |
  145. | to be flushed to a newly created temporary file.  This flushed buffer     |
  146. | might well be the current one.                     |
  147. `-----------------------------------------------------------------------*/
  148.  
  149. static void
  150. make_room_for (int length)
  151. {
  152.   int wanted_size;
  153.  
  154.   /* Compute needed size for in-memory buffer.  Diversions in-memory
  155.      buffers start at 0 bytes, then 512, then keep doubling until it is
  156.      decided to flush them to disk.  */
  157.  
  158.   output_diversion->used = output_diversion->size - output_unused;
  159.  
  160.   for (wanted_size = output_diversion->size;
  161.        wanted_size < output_diversion->used + length;
  162.        wanted_size = wanted_size == 0 ? INITIAL_BUFFER_SIZE : wanted_size * 2)
  163.     ;
  164.  
  165.   /* Check if we are exceeding the maximum amount of buffer memory.  */
  166.  
  167.   if (total_buffer_size - output_diversion->size + wanted_size
  168.       > MAXIMUM_TOTAL_SIZE)
  169.     {
  170.       struct diversion *selected_diversion;
  171.       int selected_used;
  172.       struct diversion *diversion;
  173.       int count;
  174.  
  175.       /* Find out the buffer having most data, in view of flushing it to
  176.      disk.  Fake the current buffer as having already received the
  177.      projected data, while making the selection.  So, if it is
  178.      selected indeed, we will flush it smaller, before it grows.  */
  179.  
  180.       selected_diversion = output_diversion;
  181.       selected_used = output_diversion->used + length;
  182.  
  183.       for (diversion = diversion_table + 1;
  184.        diversion < diversion_table + diversions;
  185.        diversion++)
  186.     if (diversion->used > selected_used)
  187.       {
  188.         selected_diversion = diversion;
  189.         selected_used = diversion->used;
  190.       }
  191.  
  192.       /* Create a temporary file, write the in-memory buffer of the
  193.      diversion to this file, then release the buffer.  */
  194.  
  195.       selected_diversion->file = tmpfile ();
  196.       if (selected_diversion->file == NULL)
  197.     M4ERROR ((EXIT_FAILURE, errno,
  198.           "ERROR: Cannot create temporary file for diversion"));
  199.  
  200.       if (selected_diversion->used > 0)
  201.     {
  202.       count = fwrite (selected_diversion->buffer,
  203.               (size_t) selected_diversion->used,
  204.               1,
  205.               selected_diversion->file);
  206.       if (count != 1)
  207.         M4ERROR ((EXIT_FAILURE, errno,
  208.               "ERROR: Cannot flush diversion to temporary file"));
  209.     }
  210.  
  211.       /* Reclaim the buffer space for other diversions.  */
  212.  
  213.       free (selected_diversion->buffer);
  214.       total_buffer_size -= selected_diversion->size;
  215.  
  216.       selected_diversion->buffer = NULL;
  217.       selected_diversion->size = 0;
  218.       selected_diversion->used = 0;
  219.     }
  220.  
  221.   /* Reload output_file, just in case the flushed diversion was current.  */
  222.  
  223.   output_file = output_diversion->file;
  224.   if (output_file)
  225.     {
  226.  
  227.       /* The flushed diversion was current indeed.  */
  228.  
  229.       output_cursor = NULL;
  230.       output_unused = 0;
  231.     }
  232.   else
  233.     {
  234.  
  235.       /* The buffer may be safely reallocated.  */
  236.  
  237.       output_diversion->buffer
  238.     = xrealloc (output_diversion->buffer, (size_t) wanted_size);
  239.  
  240.       total_buffer_size += wanted_size - output_diversion->size;
  241.       output_diversion->size = wanted_size;
  242.  
  243.       output_cursor = output_diversion->buffer + output_diversion->used;
  244.       output_unused = wanted_size - output_diversion->used;
  245.     }
  246. }
  247.  
  248. /*------------------------------------------------------------------------.
  249. | Output one character CHAR, when it is known that it goes to a diversion |
  250. | file or an in-memory diversion buffer.                  |
  251. `------------------------------------------------------------------------*/
  252.  
  253. #define OUTPUT_CHARACTER(Char) \
  254.   if (output_file)                            \
  255.     putc ((Char), output_file);                        \
  256.   else if (output_unused == 0)                        \
  257.     output_character_helper ((Char));                    \
  258.   else                                    \
  259.     (output_unused--, *output_cursor++ = (Char))
  260.  
  261. static void
  262. output_character_helper (int character)
  263. {
  264.   make_room_for (1);
  265.  
  266.   if (output_file)
  267.     putc (character, output_file);
  268.   else
  269.     {
  270.       *output_cursor++ = character;
  271.       output_unused--;
  272.     }
  273. }
  274.  
  275. /*------------------------------------------------------------------------.
  276. | Output one TEXT having LENGTH characters, when it is known that it goes |
  277. | to a diversion file or an in-memory diversion buffer.              |
  278. `------------------------------------------------------------------------*/
  279.  
  280. static void
  281. output_text (const char *text, int length)
  282. {
  283.   int count;
  284.  
  285.   if (!output_file && length > output_unused)
  286.     make_room_for (length);
  287.  
  288.   if (output_file)
  289.     {
  290.       count = fwrite (text, length, 1, output_file);
  291.       if (count != 1)
  292.     M4ERROR ((EXIT_FAILURE, errno, "ERROR: Copying inserted file"));
  293.     }
  294.   else
  295.     {
  296.       memcpy (output_cursor, text, (size_t) length);
  297.       output_cursor += length;
  298.       output_unused -= length;
  299.     }
  300. }
  301.  
  302. /*-------------------------------------------------------------------------.
  303. | Add some text into an obstack OBS, taken from TEXT, having LENGTH       |
  304. | characters.  If OBS is NULL, rather output the text to an external file  |
  305. | or an in-memory diversion buffer.  If OBS is NULL, and there is no       |
  306. | output file, the text is discarded.                       |
  307. |                                        |
  308. | If we are generating sync lines, the output have to be examined, because |
  309. | we need to know how much output each input line generates.  In general,  |
  310. | sync lines are output whenever a single input lines generates several       |
  311. | output lines, or when several input lines does not generate any output.  |
  312. `-------------------------------------------------------------------------*/
  313.  
  314. void
  315. shipout_text (struct obstack *obs, const char *text, int length)
  316. {
  317.   static boolean start_of_output_line = TRUE;
  318.   char line[20];
  319.   const char *cursor;
  320.  
  321.   /* If output goes to an obstack, merely add TEXT to it.  */
  322.  
  323.   if (obs != NULL)
  324.     {
  325.       obstack_grow (obs, text, length);
  326.       return;
  327.     }
  328.  
  329.   /* Do nothing if TEXT should be discarded.  */
  330.  
  331.   if (output_diversion == NULL)
  332.     return;
  333.  
  334.   /* Output TEXT to a file, or in-memory diversion buffer.  */
  335.  
  336.   if (!sync_output)
  337.     switch (length)
  338.       {
  339.  
  340.     /* In-line short texts.  */
  341.  
  342.       case 8: OUTPUT_CHARACTER (*text); text++;
  343.       case 7: OUTPUT_CHARACTER (*text); text++;
  344.       case 6: OUTPUT_CHARACTER (*text); text++;
  345.       case 5: OUTPUT_CHARACTER (*text); text++;
  346.       case 4: OUTPUT_CHARACTER (*text); text++;
  347.       case 3: OUTPUT_CHARACTER (*text); text++;
  348.       case 2: OUTPUT_CHARACTER (*text); text++;
  349.       case 1: OUTPUT_CHARACTER (*text);
  350.       case 0:
  351.     return;
  352.  
  353.     /* Optimize longer texts.  */
  354.  
  355.       default:
  356.     output_text (text, length);
  357.       }
  358.   else
  359.     for (; length-- > 0; text++)
  360.       {
  361.     if (start_of_output_line)
  362.       {
  363.         start_of_output_line = FALSE;
  364.         output_current_line++;
  365.  
  366. #ifdef DEBUG_OUTPUT
  367.         printf ("DEBUG: cur %d, cur out %d\n",
  368.             current_line, output_current_line);
  369. #endif
  370.  
  371.         /* Output a `#line NUM' synchronisation directive if needed.
  372.            If output_current_line was previously given a negative
  373.            value (invalidated), rather output `#line NUM "FILE"'.  */
  374.  
  375.         if (output_current_line != current_line)
  376.           {
  377.         sprintf (line, "#line %d", current_line);
  378.         for (cursor = line; *cursor; cursor++)
  379.           OUTPUT_CHARACTER (*cursor);
  380.         if (output_current_line < 1)
  381.           {
  382.             OUTPUT_CHARACTER (' ');
  383.             OUTPUT_CHARACTER ('"');
  384.             for (cursor = current_file; *cursor; cursor++)
  385.               OUTPUT_CHARACTER (*cursor);
  386.             OUTPUT_CHARACTER ('"');
  387.           }
  388.         OUTPUT_CHARACTER ('\n');
  389.         output_current_line = current_line;
  390.           }
  391.       }
  392.     OUTPUT_CHARACTER (*text);
  393.     if (*text == '\n')
  394.       start_of_output_line = TRUE;
  395.       }
  396. }
  397.  
  398. /* Functions for use by diversions.  */
  399.  
  400. /*--------------------------------------------------------------------------.
  401. | Make a file for diversion DIVNUM, and install it in the diversion table.  |
  402. | Grow the size of the diversion table as needed.                |
  403. `--------------------------------------------------------------------------*/
  404.  
  405. /* The number of possible diversions is limited only by memory and
  406.    available file descriptors (each overflowing diversion uses one).  */
  407.  
  408. void
  409. make_diversion (int divnum)
  410. {
  411.   struct diversion *diversion;
  412.  
  413.   if (output_diversion)
  414.     {
  415.       output_diversion->file = output_file;
  416.       output_diversion->used = output_diversion->size - output_unused;
  417.       output_diversion = NULL;
  418.       output_file = NULL;
  419.       output_cursor = NULL;
  420.       output_unused = 0;
  421.     }
  422.  
  423.   current_diversion = divnum;
  424.  
  425.   if (divnum < 0)
  426.     return;
  427.  
  428.   if (divnum >= diversions)
  429.     {
  430.       diversion_table = (struct diversion *)
  431.     xrealloc (diversion_table, (divnum + 1) * sizeof (struct diversion));
  432.       for (diversion = diversion_table + diversions;
  433.        diversion <= diversion_table + divnum;
  434.        diversion++)
  435.     {
  436.       diversion->file = NULL;
  437.       diversion->buffer = NULL;
  438.       diversion->size = 0;
  439.       diversion->used = 0;
  440.     }
  441.       diversions = divnum + 1;
  442.     }
  443.  
  444.   output_diversion = diversion_table + divnum;
  445.   output_file = output_diversion->file;
  446.   output_cursor = output_diversion->buffer + output_diversion->used;
  447.   output_unused = output_diversion->size - output_diversion->used;
  448.   output_current_line = -1;
  449. }
  450.  
  451. /*-------------------------------------------------------------------.
  452. | Insert a FILE into the current output file, in the same manner     |
  453. | diversions are handled.  This allows files to be included, without |
  454. | having them rescanned by m4.                         |
  455. `-------------------------------------------------------------------*/
  456.  
  457. void
  458. insert_file (FILE *file)
  459. {
  460.   char buffer[COPY_BUFFER_SIZE];
  461.   size_t length;
  462.  
  463.   /* Optimize out inserting into a sink.  */
  464.  
  465.   if (!output_diversion)
  466.     return;
  467.  
  468.   /* Insert output by big chunks.  */
  469.  
  470.   while (length = read (fileno (file), buffer, COPY_BUFFER_SIZE),
  471.      length != 0)
  472.     if (length == (size_t) -1)
  473.       M4ERROR ((EXIT_FAILURE, errno, "ERROR: Reading inserted file"));
  474.     else
  475.       output_text (buffer, length);
  476. }
  477.  
  478. /*-------------------------------------------------------------------------.
  479. | Insert diversion number DIVNUM into the current output file.  The       |
  480. | diversion is NOT placed on the expansion obstack, because it must not be |
  481. | rescanned.  When the file is closed, it is deleted by the system.       |
  482. `-------------------------------------------------------------------------*/
  483.  
  484. void
  485. insert_diversion (int divnum)
  486. {
  487.   struct diversion *diversion;
  488.  
  489.   /* Do not care about unexisting diversions.  */
  490.  
  491.   if (divnum < 0 || divnum >= diversions)
  492.     return;
  493.  
  494.   /* Also avoid undiverting into self.  */
  495.  
  496.   diversion = diversion_table + divnum;
  497.   if (diversion == output_diversion)
  498.     return;
  499.  
  500.   /* Effectively undivert only if an output stream is active.  */
  501.  
  502.   if (output_diversion)
  503.     {
  504.       if (diversion->file)
  505.     {
  506.       rewind (diversion->file);
  507.       insert_file (diversion->file);
  508.     }
  509.       else if (diversion->buffer)
  510.     output_text (diversion->buffer, diversion->used);
  511.  
  512.       output_current_line = -1;
  513.     }
  514.  
  515.   /* Return all space used by the diversion.  */
  516.  
  517.   if (diversion->file)
  518.     {
  519.       fclose (diversion->file);
  520.       diversion->file = NULL;
  521.     }
  522.   else if (diversion->buffer)
  523.     {
  524.       free (diversion->buffer);
  525.       diversion->buffer = NULL;
  526.       diversion->size = 0;
  527.       diversion->used = 0;
  528.     }
  529. }
  530.  
  531. /*-------------------------------------------------------------------------.
  532. | Get back all diversions.  This is done just before exiting from main (), |
  533. | and from m4_undivert (), if called without arguments.               |
  534. `-------------------------------------------------------------------------*/
  535.  
  536. void
  537. undivert_all (void)
  538. {
  539.   int divnum;
  540.  
  541.   for (divnum = 1; divnum < diversions; divnum++)
  542.     insert_diversion (divnum);
  543. }
  544.  
  545. /*-------------------------------------------------------------.
  546. | Produce all diversion information in frozen format on FILE.  |
  547. `-------------------------------------------------------------*/
  548.  
  549. void
  550. freeze_diversions (FILE *file)
  551. {
  552.   int saved_number;
  553.   int last_inserted;
  554.   int divnum;
  555.   struct diversion *diversion;
  556.   struct stat file_stat;
  557.   
  558.   saved_number = current_diversion;
  559.   last_inserted = 0;
  560.   make_diversion (0);
  561.   output_file = file;        /* kludge in the frozen file */
  562.  
  563.   for (divnum = 1; divnum < diversions; divnum++)
  564.     {
  565.       diversion = diversion_table + divnum;
  566.       if (diversion->file || diversion->buffer)
  567.     {
  568.       if (diversion->file)
  569.         {
  570.           fflush (diversion->file);
  571.           if (fstat (fileno (diversion->file), &file_stat) < 0)
  572.         M4ERROR ((EXIT_FAILURE, errno, "Cannot stat diversion"));
  573.           fprintf (file, "D%d,%d", divnum, (int) file_stat.st_size);
  574.         }
  575.       else
  576.         fprintf (file, "D%d,%d\n", divnum, diversion->used);
  577.  
  578.       insert_diversion (divnum);
  579.       putc ('\n', file);
  580.  
  581.       last_inserted = divnum;
  582.     }
  583.     }
  584.  
  585.   /* Save the active diversion number, if not already.  */
  586.  
  587.   if (saved_number != last_inserted)
  588.     fprintf (file, "D%d,0\n\n", saved_number);
  589. }
  590.  
  591.