home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / GNU / MAK358AS.ZIP / VARIABLE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-02-22  |  20.7 KB  |  789 lines

  1. /* Internals of variables for GNU Make.
  2. Copyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 1, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. /*
  20.  * MS-DOS port (c) 1990 by Thorsten Ohl <ohl@gnu.ai.mit.edu>
  21.  *
  22.  * To this port, the same copying conditions apply as to the
  23.  * original release.
  24.  *
  25.  * IMPORTANT:
  26.  * This file is not identical to the original GNU release!
  27.  * You should have received this code as patch to the official
  28.  * GNU release.
  29.  *
  30.  * MORE IMPORTANT:
  31.  * This port comes with ABSOLUTELY NO WARRANTY.
  32.  *
  33.  * $Header: e:/gnu/make/RCS/variable.c'v 3.58.0.3 90/07/20 11:27:29 tho Exp $
  34.  */
  35.  
  36. #include "make.h"
  37. #include "commands.h"
  38. #include "variable.h"
  39. #include "dep.h"
  40. #include "file.h"
  41.  
  42. #ifdef MSDOS
  43. #define DEFAULT_SHELL "command"
  44. #else /* not MSDOS */
  45. #define DEFAULT_SHELL "/bin/sh"
  46. #endif /* not MSDOS */
  47.  
  48. #ifndef MSDOS
  49. #ifdef    __GNUC__
  50. #define    max(a, b) \
  51.   ({ register int __a = (a), __b = (b); __a > __b ? __a : __b; })
  52. #else
  53. #define max(a, b) ((a) > (b) ? (a) : (b))
  54. #endif
  55. #endif /* not MSDOS */
  56.  
  57.  
  58. /* Hash table of all global variable definitions.  */
  59.  
  60. #ifndef    VARIABLE_BUCKETS
  61. #define VARIABLE_BUCKETS        523
  62. #endif
  63. #ifndef    PERFILE_VARIABLE_BUCKETS
  64. #define    PERFILE_VARIABLE_BUCKETS    23
  65. #endif
  66. #ifndef    SMALL_SCOPE_VARIABLE_BUCKETS
  67. #define    SMALL_SCOPE_VARIABLE_BUCKETS    13
  68. #endif
  69. static struct variable *variable_table[VARIABLE_BUCKETS];
  70. static struct variable_set global_variable_set
  71.   = { variable_table, VARIABLE_BUCKETS };
  72. static struct variable_set_list global_setlist
  73.   = { 0, &global_variable_set };
  74. struct variable_set_list *current_variable_set_list = &global_setlist;
  75.  
  76. /* The next two describe the variable output buffer.
  77.    This buffer is used to hold the variable-expansion of a line of the
  78.    makefile.  It is made bigger with realloc whenever it is too small.
  79.    variable_buffer_length is the size currently allocated.
  80.    variable_buffer is the address of the buffer.  */
  81.  
  82. static unsigned int variable_buffer_length;
  83. static char *variable_buffer;
  84.  
  85. #ifdef MSDOS
  86. static struct variable *define_variable_in_set (char *name,
  87.         unsigned int length, char *value, enum variable_origin origin,
  88.         int recursive, struct variable_set *set);
  89. static void print_variable (struct variable *v, char *prefix);
  90. static void print_variable_set (struct variable_set *set, char *prefix);
  91. static void merge_variable_sets (struct variable_set *set0,
  92.                  struct variable_set *set1);
  93. #endif /* MSDOS */
  94.  
  95. /* Implement variables.  */
  96.  
  97. /* Define variable named NAME with value VALUE in SET.  VALUE is copied.
  98.    LENGTH is the length of NAME, which does not need to be null-terminated.
  99.    ORIGIN specifies the origin of the variable (makefile, command line
  100.    or environment).
  101.    If RECURSIVE is nonzero a flag is set in the variable saying
  102.    that it should be recursively re-expanded.  */
  103.  
  104. static struct variable *
  105. define_variable_in_set (name, length, value, origin, recursive, set)
  106.      char *name;
  107.      unsigned int length;
  108.      char *value;
  109.      enum variable_origin origin;
  110.      int recursive;
  111.      struct variable_set *set;
  112. {
  113.   register unsigned int i;
  114.   register unsigned int hashval;
  115.   register struct variable *v;
  116.  
  117.   hashval = 0;
  118.   for (i = 0; i < length; ++i)
  119.     HASH (hashval, name[i]);
  120.   hashval %= set->buckets;
  121.  
  122.   for (v = set->table[hashval]; v != 0; v = v->next)
  123.     if (*v->name == *name
  124.     && !strncmp (v->name + 1, name + 1, length - 1)
  125.     && v->name[length] == '\0')
  126.       break;
  127.  
  128.   if (env_overrides && origin == o_env)
  129.     origin = o_env_override;
  130.  
  131.   if (v != 0)
  132.     {
  133.       if (env_overrides && v->origin == o_env)
  134.     v->origin = o_env_override;
  135.       /* A variable of this name is already defined.
  136.      If the old definition is from a stronger source
  137.      than this one, don't redefine it.  */
  138.       if ((int) origin >= (int) v->origin)
  139.     {
  140.       v->value = savestring (value, strlen (value));
  141.       v->origin = origin;
  142.       v->recursive = recursive;
  143.     }
  144.       return v;
  145.     }
  146.  
  147.   /* Create a new variable definition and add it to the hash table.  */
  148.  
  149.   v = (struct variable *) xmalloc (sizeof (struct variable));
  150.   v->name = savestring (name, length);
  151.   v->value = savestring (value, strlen (value));
  152.   v->origin = origin;
  153.   v->recursive = recursive;
  154.   v->expanding = 0;
  155.   v->next = set->table[hashval];
  156.   set->table[hashval] = v;
  157.   return v;
  158. }
  159.  
  160. /* Define a variable in the current variable set.  */
  161.  
  162. struct variable *
  163. define_variable (name, length, value, origin, recursive)
  164.      char *name;
  165.      unsigned int length;
  166.      char *value;
  167.      enum variable_origin origin;
  168.      int recursive;
  169. {
  170.   return define_variable_in_set (name, length, value, origin, recursive,
  171.                  current_variable_set_list->set);
  172. }
  173.  
  174. /* Define a variable in FILE's variable set.  */
  175.  
  176. struct variable *
  177. define_variable_for_file (name, length, value, origin, recursive, file)
  178.      char *name;
  179.      unsigned int length;
  180.      char *value;
  181.      enum variable_origin origin;
  182.      int recursive;
  183.      struct file *file;
  184. {
  185.   return define_variable_in_set (name, length, value, origin, recursive,
  186.                  file->variables->set);
  187. }
  188.  
  189. /* Lookup a variable whose name is a string starting at NAME
  190.    and with LENGTH chars.  NAME need not be null-terminated.
  191.    Returns address of the `struct variable' containing all info
  192.    on the variable, or nil if no such variable is defined.  */
  193.  
  194. struct variable *
  195. lookup_variable (name, length)
  196.      char *name;
  197.      unsigned int length;
  198. {
  199.   register struct variable_set_list *setlist;
  200.  
  201.   register unsigned int i;
  202.   register unsigned int rawhash = 0;
  203.  
  204.   for (i = 0; i < length; ++i)
  205.     HASH (rawhash, name[i]);
  206.  
  207.   for (setlist = current_variable_set_list;
  208.        setlist != 0; setlist = setlist->next)
  209.     {
  210.       register struct variable_set *set = setlist->set;
  211.       register unsigned int hashval = rawhash % set->buckets;
  212.       register struct variable *v;
  213.  
  214.       for (v = set->table[hashval]; v != 0; v = v->next)
  215.     if (*v->name == *name
  216.         && !strncmp (v->name + 1, name + 1, length - 1)
  217.         && v->name[length] == 0)
  218.       return v;
  219.     }
  220.  
  221.   return 0;
  222. }
  223.  
  224. /* Initialize FILE's variable set list.  If FILE already has a variable set
  225.    list, the topmost variable set is left intact, but the the rest of the
  226.    chain is replaced with FILE->parent's setlist.  */
  227.  
  228. void
  229. initialize_file_variables (file)
  230.      struct file *file;
  231. {
  232.   register struct variable_set_list *l = file->variables;
  233.   if (l == 0)
  234.     {
  235.       l = (struct variable_set_list *)
  236.     xmalloc (sizeof (struct variable_set_list));
  237.       l->set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
  238.       l->set->buckets = PERFILE_VARIABLE_BUCKETS;
  239.       l->set->table = (struct variable **)
  240.     xmalloc (l->set->buckets * sizeof (struct variable *));
  241.       bzero ((char *) l->set->table,
  242.          l->set->buckets * sizeof (struct variable *));
  243.       file->variables = l;
  244.     }
  245.  
  246.   if (file->parent == 0)
  247.     l->next = &global_setlist;
  248.   else
  249.     {
  250.       if (file->parent->variables == 0)
  251.     initialize_file_variables (file->parent);
  252.       l->next = file->parent->variables;
  253.     }
  254. }
  255.  
  256. /* Pop the top set off the current variable set list,
  257.    and free all its storage.  */
  258.  
  259. void
  260. pop_variable_scope ()
  261. {
  262.   register struct variable_set_list *setlist = current_variable_set_list;
  263.   register struct variable_set *set = setlist->set;
  264.   register unsigned int i;
  265.  
  266.   current_variable_set_list = setlist->next;
  267.   free ((char *) setlist);
  268.  
  269.   for (i = 0; i < set->buckets; ++i)
  270.     {
  271.       register struct variable *next = set->table[i];
  272.       while (next != 0)
  273.     {
  274.       register struct variable *v = next;
  275.       next = v->next;
  276.  
  277.       free (v->name);
  278.       free ((char *) v);
  279.     }
  280.     }
  281.   free ((char *) set->table);
  282.   free ((char *) set);
  283. }
  284.  
  285. /* Create a new variable set and push it on the current setlist.  */
  286.  
  287. void
  288. push_new_variable_scope ()
  289. {
  290.   register struct variable_set_list *setlist;
  291.   register struct variable_set *set;
  292.  
  293.   set = (struct variable_set *) xmalloc (sizeof (struct variable_set));
  294.   set->buckets = SMALL_SCOPE_VARIABLE_BUCKETS;
  295.   set->table = (struct variable **)
  296.     xmalloc (set->buckets * sizeof (struct variable *));
  297.   bzero ((char *) set->table, set->buckets * sizeof (struct variable *));
  298.  
  299.   setlist = (struct variable_set_list *)
  300.     xmalloc (sizeof (struct variable_set_list));
  301.   setlist->set = set;
  302.   setlist->next = current_variable_set_list;
  303.   current_variable_set_list = setlist;
  304. }
  305.  
  306. /* Merge SET1 into SET0, freeing unused storage in SET1.  */
  307.  
  308. static void
  309. merge_variable_sets (set0, set1)
  310.      struct variable_set *set0, *set1;
  311. {
  312.   register unsigned int bucket1;
  313.  
  314.   for (bucket1 = 0; bucket1 < set1->buckets; ++bucket1)
  315.     {
  316.       register struct variable *v1 = set1->table[bucket1];
  317.       while (v1 != 0)
  318.     {
  319.       struct variable *next = v1->next;
  320.       unsigned int bucket0;
  321.       register struct variable *v0;
  322.  
  323.       if (set1->buckets >= set0->buckets)
  324.         bucket0 = bucket1;
  325.       else
  326.         {
  327.           register char *n;
  328.           bucket0 = 0;
  329.           for (n = v1->name; *n != '\0'; ++n)
  330.         HASH (bucket0, *n);
  331.         }
  332.       bucket0 %= set0->buckets;
  333.  
  334.       for (v0 = set0->table[bucket0]; v0 != 0; v0 = v0->next)
  335.         if (streq (v0->name, v1->name))
  336.           break;
  337.  
  338.       if (v0 == 0)
  339.         {
  340.           /* There is no variable in SET0 with the same name.  */
  341.           v1->next = set0->table[bucket0];
  342.           set0->table[bucket0] = v1;
  343.         }
  344.       else
  345.         {
  346.           /* The same variable exists in both sets.
  347.          SET0 takes precedence.  */
  348.           free (v1->value);
  349.           free ((char *) v1);
  350.         }
  351.  
  352.       v1 = next;
  353.     }
  354.     }
  355. }
  356.  
  357. /* Merge SETLIST1 into SETLIST0, freeing unused storage in SETLIST1.  */
  358.  
  359. void
  360. merge_variable_set_lists (setlist0, setlist1)
  361.      struct variable_set_list **setlist0, *setlist1;
  362. {
  363.   register struct variable_set_list *list0 = *setlist0;
  364.   struct variable_set_list *last0 = 0;
  365.  
  366.   while (setlist1 != 0 && list0 != 0)
  367.     {
  368.       struct variable_set_list *next = setlist1;
  369.       setlist1 = setlist1->next;
  370.  
  371.       merge_variable_sets (list0->set, next->set);
  372.  
  373.       free ((char *) next);
  374.  
  375.       last0 = list0;
  376.       list0 = list0->next;
  377.     }
  378.  
  379.   if (setlist1 != 0)
  380.     {
  381.       if (last0 == 0)
  382.     *setlist0 = setlist1;
  383.       else
  384.     last0->next = setlist1;
  385.     }
  386. }
  387.  
  388. /* Define the automatic variables, and record the addresses
  389.    of their structures so we can change their values quickly.  */
  390.  
  391. void
  392. define_automatic_variables ()
  393. {
  394.   register struct variable *v;
  395.   char buf[100];
  396.  
  397.   sprintf (buf, "%u", makelevel);
  398.   (void) define_variable ("MAKELEVEL", 9, buf, o_env, 0);
  399.  
  400.   /* This won't override any definition, but it
  401.      will provide one if there isn't one there.  */
  402.   v = define_variable ("SHELL", 5, DEFAULT_SHELL, o_default, 0);
  403.  
  404.   /* Don't let SHELL come from the environment
  405.      if MAKELEVEL is 0.  Also, SHELL must not be empty.  */
  406.   if (*v->value == '\0' || (v->origin == o_env && makelevel == 0))
  407.     {
  408.       v->origin = o_file;
  409.       v->value = savestring (DEFAULT_SHELL, 7);
  410.     }
  411.  
  412. }
  413.  
  414. /* Subroutine of variable_expand and friends:
  415.    The text to add is LENGTH chars starting at STRING to the variable_buffer.
  416.    The text is added to the buffer at PTR, and the updated pointer into
  417.    the buffer is returned as the value.  Thus, the value returned by
  418.    each call to variable_buffer_output should be the first argument to
  419.    the following call.  */
  420.  
  421. char *
  422. variable_buffer_output (ptr, string, length)
  423.      char *ptr, *string;
  424.      unsigned int length;
  425. {
  426.   register unsigned int newlen = length + (ptr - variable_buffer);
  427.  
  428.   if (newlen > variable_buffer_length)
  429.     {
  430.       unsigned int offset = ptr - variable_buffer;
  431.       variable_buffer_length = max (2 * variable_buffer_length, newlen + 100);
  432.       variable_buffer = (char *) xrealloc (variable_buffer,
  433.                        variable_buffer_length);
  434.       ptr = variable_buffer + offset;
  435.     }
  436.  
  437.   bcopy (string, ptr, length);
  438.   return ptr + length;
  439. }
  440.  
  441. /* Return a pointer to the beginning of the variable buffer.  */
  442.  
  443. char *
  444. initialize_variable_output ()
  445. {
  446.   /* If we don't have a variable output buffer yet, get one.  */
  447.  
  448.   if (variable_buffer == 0)
  449.     {
  450.       variable_buffer_length = 200;
  451.       variable_buffer = (char *) xmalloc (variable_buffer_length);
  452.     }
  453.  
  454.   return variable_buffer;
  455. }
  456.  
  457. /* Create a new environment for FILE's commands.
  458.    The child's MAKELEVEL variable is incremented.  */
  459.  
  460. char **
  461. target_environment (file)
  462.      struct file *file;
  463. {
  464.   register struct variable_set_list *s;
  465.   struct variable_bucket
  466.     {
  467.       struct variable_bucket *next;
  468.       struct variable *variable;
  469.     };
  470.   struct variable_bucket **table;
  471.   unsigned int buckets;
  472.   register unsigned int i;
  473.   register unsigned nvariables;
  474.   char **result;
  475.  
  476.   /* Find the lowest number of buckets in any set in the list.  */
  477.   s = file->variables;
  478.   buckets = s->set->buckets;
  479.   for (s = s->next; s != 0; s = s->next)
  480.     if (s->set->buckets < buckets)
  481.       buckets = s->set->buckets;
  482.  
  483.   /* Temporarily allocate a table with that many buckets.  */
  484.   table = (struct variable_bucket **)
  485.     alloca (buckets * sizeof (struct variable_bucket *));
  486.   bzero ((char *) table, buckets * sizeof (struct variable_bucket *));
  487.  
  488.   /* Run through all the variable sets in the list,
  489.      accumulating variables in TABLE.  */
  490.   nvariables = 0;
  491.   for (s = file->variables; s != 0; s = s->next)
  492.     {
  493.       register struct variable_set *set = s->set;
  494.       for (i = 0; i < set->buckets; ++i)
  495.     {
  496.       register struct variable *v;
  497.       for (v = set->table[i]; v != 0; v = v->next)
  498.         {
  499.           unsigned int j = i % buckets;
  500.           register struct variable_bucket *ov;
  501.           register char *p = v->name;
  502.  
  503.           if (v->origin == o_default
  504.           || streq (p, "MAKELEVEL"))
  505.         continue;
  506.  
  507.           if (*p != '_' && (*p < 'A' || *p > 'Z')
  508.           && (*p < 'a' || *p > 'z'))
  509.         continue;
  510.           for (++p; *p != '\0'; ++p)
  511.         if (*p != '_' && (*p < 'a' || *p > 'z')
  512.             && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9'))
  513.           break;
  514.           if (*p != '\0')
  515.         continue;
  516.  
  517.           for (ov = table[j]; ov != 0; ov = ov->next)
  518.         if (streq (v->name, ov->variable->name))
  519.           break;
  520.           if (ov == 0)
  521.         {
  522.           register struct variable_bucket *entry;
  523.           entry = (struct variable_bucket *)
  524.             alloca (sizeof (struct variable_bucket));
  525.           entry->next = table[j];
  526.           entry->variable = v;
  527.           table[j] = entry;
  528.           ++nvariables;
  529.         }
  530.         }
  531.     }
  532.     }
  533.  
  534.   result = (char **) xmalloc ((nvariables + 2) * sizeof (char *));
  535.   nvariables = 0;
  536.   for (i = 0; i < buckets; ++i)
  537.     {
  538.       register struct variable_bucket *b;
  539.       for (b = table[i]; b != 0; b = b->next)
  540.     {
  541.       register struct variable *v = b->variable;
  542.       result[nvariables++] = concat (v->name, "=", v->value);
  543.     }
  544.     }
  545.   result[nvariables] = (char *) xmalloc (100);
  546.   (void) sprintf (result[nvariables], "MAKELEVEL=%u", makelevel + 1);
  547.   result[++nvariables] = 0;
  548.  
  549.   return result;
  550. }
  551.  
  552. /* Try to interpret LINE (a null-terminated string)
  553.    as a variable definition.  If it is one, define the
  554.    variable and return 1.  Otherwise return 0.
  555.  
  556.    ORIGIN may be o_file, o_override, o_env, o_env_override,
  557.    or o_command specifying that the variable definition comes
  558.    from a makefile, an override directive, the environment with
  559.    or without the -e switch, or the command line.
  560.  
  561.    A variable definition has the form "name = value" or "name := value".
  562.    Any whitespace around the "=" or ":=" is removed.  The first form
  563.    defines a variable that is recursively re-evaluated.  The second form
  564.    defines a variable whose value is variable-expanded at the time of
  565.    definition and then is evaluated only once at the time of expansion.  */
  566.  
  567. int
  568. try_variable_definition (line, origin)
  569.      char *line;
  570.      enum variable_origin origin;
  571. {
  572.   register int c;
  573.   register char *p = line;
  574.   register char *beg;
  575.   register char *end;
  576.   register int recursive;
  577.  
  578.   if (*p == '\t')
  579.     return 0;
  580.   while (1)
  581.     {
  582.       c = *p++;
  583.       if (c == '\0' || c == '#')
  584.     return 0;
  585.       if (c == '=')
  586.     {
  587.       recursive = 1;
  588.       break;
  589.     }
  590.       else if (c == ':')
  591.     if (*p == '=')
  592.       {
  593.         ++p;
  594.         recursive = 0;
  595.         break;
  596.       }
  597.     else
  598.       return 0;
  599.     }
  600.  
  601.   beg = next_token (line);
  602.   end = p - 1;
  603.   if (!recursive)
  604.     --end;
  605.   while (end[-1] == ' ' || end[-1] == '\t')
  606.     --end;
  607.   p = next_token (p);
  608.  
  609.   (void) define_variable (beg, end - beg, recursive ? p : variable_expand (p),
  610.               origin, recursive);
  611.  
  612.   return 1;
  613. }
  614.  
  615. /* Print information for variable V, prefixing it with PREFIX.  */
  616.  
  617. static void
  618. print_variable (v, prefix)
  619.      register struct variable *v;
  620.      char *prefix;
  621. {
  622.   char *origin;
  623.  
  624.   switch (v->origin)
  625.     {
  626.     case o_default:
  627.       origin = "default";
  628.       break;
  629.     case o_env:
  630.       origin = "environment";
  631.       break;
  632.     case o_file:
  633.       origin = "makefile";
  634.       break;
  635.     case o_env_override:
  636.       origin = "environment under -e";
  637.       break;
  638.     case o_command:
  639.       origin = "command line";
  640.       break;
  641.     case o_override:
  642.       origin = "`override' directive";
  643.       break;
  644.     case o_automatic:
  645.       origin = "automatic";
  646.       break;
  647.     case o_invalid:
  648.     default:
  649.       abort ();
  650.       break;
  651.     }
  652.   printf ("# %s\n", origin);
  653.  
  654.   fputs (prefix, stdout);
  655.  
  656.   /* Is this a `define'?  */
  657.   if (v->recursive && index (v->value, '\n') != 0)
  658.     printf ("define %s\n%sendef\n", v->name, v->value);
  659.   else
  660.     {
  661.       register char *p;
  662.  
  663.       printf ("%s %s= ", v->name, v->recursive ? "" : ":");
  664.  
  665.       /* Check if the value is just whitespace.  */
  666.       p = next_token (v->value);
  667.       if (p != v->value && *p == '\0')
  668.     /* All whitespace.  */
  669.     printf ("$(subst ,,%s)", v->value);
  670.       else if (v->recursive)
  671.     fputs (v->value, stdout);
  672.       else
  673.     /* Double up dollar signs.  */
  674.     for (p = v->value; *p != '\0'; ++p)
  675.       {
  676.         if (*p == '$')
  677.           putchar ('$');
  678.         putchar (*p);
  679.       }
  680.       putchar ('\n');
  681.     }
  682. }
  683.  
  684.  
  685. /* Print all the variables in SET.  PREFIX is printed before
  686.    the actual variable definitions (everything else is comments).  */
  687.  
  688. static void
  689. print_variable_set (set, prefix)
  690.      register struct variable_set *set;
  691.      char *prefix;
  692. {
  693.   register unsigned int i, nvariables, per_bucket;
  694.   register struct variable *v;
  695.  
  696.   per_bucket = nvariables = 0;
  697.   for (i = 0; i < set->buckets; ++i)
  698.     {
  699.       register unsigned int this_bucket = 0;
  700.  
  701.       for (v = set->table[i]; v != 0; v = v->next)
  702.     {
  703.       ++this_bucket;
  704.       print_variable (v, prefix);
  705.     }
  706.  
  707.       nvariables += this_bucket;
  708.       if (this_bucket > per_bucket)
  709.     per_bucket = this_bucket;
  710.     }
  711.  
  712.   if (nvariables == 0)
  713.     puts ("# No variables.");
  714.   else
  715.     {
  716.       printf ("# %u variables in %u hash buckets.\n",
  717.           nvariables, set->buckets);
  718. #ifndef    NO_FLOAT
  719.       printf ("# average of %.1f variables per bucket, \
  720. max %u in one bucket.\n",
  721.           ((double) nvariables) * 100.0 / (double) set->buckets,
  722.           per_bucket);
  723. #endif
  724.     }
  725. }
  726.  
  727.  
  728. /* Print the data base of variables.  */
  729.  
  730. void
  731. print_variable_data_base ()
  732. {
  733.   puts ("\n# Variables\n");
  734.  
  735.   print_variable_set (&global_variable_set, "");
  736. }
  737.  
  738.  
  739. /* Print all the local variables of FILE.  */
  740.  
  741. void
  742. print_file_variables (file)
  743.      struct file *file;
  744. {
  745.   if (file->variables != 0)
  746.     print_variable_set (file->variables->set, "# ");
  747. }
  748.  
  749. struct output_state
  750.   {
  751.     char *buffer;
  752.     unsigned int length;
  753.   };
  754.  
  755. /* Save the current variable output state and return a pointer
  756.    to storage describing it.  Then reset the output state.  */
  757.  
  758. char *
  759. save_variable_output ()
  760. {
  761.   struct output_state *state;
  762.  
  763.   state = (struct output_state *) xmalloc (sizeof (struct output_state));
  764.   state->buffer = variable_buffer;
  765.   state->length = variable_buffer_length;
  766.  
  767.   variable_buffer = 0;
  768.   variable_buffer_length = 0;
  769.  
  770.   return (char *) state;
  771. }
  772.  
  773. /* Restore the variable output state saved in SAVE.  */
  774.  
  775. void
  776. restore_variable_output (save)
  777.      char *save;
  778. {
  779.   register struct output_state *state = (struct output_state *) save;
  780.  
  781.   if (variable_buffer != 0)
  782.     free (variable_buffer);
  783.  
  784.   variable_buffer = state->buffer;
  785.   variable_buffer_length = state->length;
  786.  
  787.   free ((char *) state);
  788. }
  789.