home *** CD-ROM | disk | FTP | other *** search
/ OpenStep (Enterprise) / OpenStepENTCD.toast / OEDEV / GNUSRC.Z / expand.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-01  |  12.0 KB  |  495 lines

  1. /* Variable expansion functions for GNU Make.
  2. Copyright (C) 1988, 89, 91, 92, 93, 95 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 2, 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. #include "make.h"
  20. #include "commands.h"
  21. #include "file.h"
  22. #include "variable.h"
  23.  
  24. /* The next two describe the variable output buffer.
  25.    This buffer is used to hold the variable-expansion of a line of the
  26.    makefile.  It is made bigger with realloc whenever it is too small.
  27.    variable_buffer_length is the size currently allocated.
  28.    variable_buffer is the address of the buffer.  */
  29.  
  30. static unsigned int variable_buffer_length;
  31. static char *variable_buffer;
  32.  
  33. /* Subroutine of variable_expand and friends:
  34.    The text to add is LENGTH chars starting at STRING to the variable_buffer.
  35.    The text is added to the buffer at PTR, and the updated pointer into
  36.    the buffer is returned as the value.  Thus, the value returned by
  37.    each call to variable_buffer_output should be the first argument to
  38.    the following call.  */
  39.  
  40. char *
  41. variable_buffer_output (ptr, string, length)
  42.      char *ptr, *string;
  43.      unsigned int length;
  44. {
  45.   register unsigned int newlen = length + (ptr - variable_buffer);
  46.  
  47.   if (newlen > variable_buffer_length)
  48.     {
  49.       unsigned int offset = ptr - variable_buffer;
  50.       variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
  51.                 ? newlen + 100
  52.                 : 2 * variable_buffer_length);
  53.       variable_buffer = (char *) xrealloc (variable_buffer,
  54.                        variable_buffer_length);
  55.       ptr = variable_buffer + offset;
  56.     }
  57.  
  58.   bcopy (string, ptr, length);
  59.   return ptr + length;
  60. }
  61.  
  62. /* Return a pointer to the beginning of the variable buffer.  */
  63.  
  64. #if NeXT || NeXT_PDO
  65. char *
  66. #else
  67. static char *
  68. #endif
  69. initialize_variable_output ()
  70. {
  71.   /* If we don't have a variable output buffer yet, get one.  */
  72.  
  73.   if (variable_buffer == 0)
  74.     {
  75.       variable_buffer_length = 200;
  76.       variable_buffer = (char *) xmalloc (variable_buffer_length);
  77.       variable_buffer[0] = '\0';
  78.     }
  79.  
  80.   return variable_buffer;
  81. }
  82.  
  83. #if NeXT || NeXT_PDO
  84.  
  85. char *
  86. save_variable_output(savelenp)
  87.     int *savelenp;
  88. {
  89.   char *save;
  90.  
  91.   save = variable_buffer;
  92.   *savelenp = variable_buffer_length;
  93.   
  94.   variable_buffer = 0;
  95.   variable_buffer_length = 0;
  96.  
  97.   return (save);
  98. }
  99.  
  100.              
  101. void
  102. restore_variable_output (save, savelen)
  103.     char *save;
  104.     int savelen;
  105. {
  106.  
  107.   if (variable_buffer != 0)
  108.     free (variable_buffer);
  109.  
  110.   variable_buffer = save;
  111.   variable_buffer_length = savelen;
  112.  
  113. }
  114.  
  115. #endif /* NeXT || NeXT_PDO */
  116.  
  117. /* Recursively expand V.  The returned string is malloc'd.  */
  118.  
  119. char *
  120. recursively_expand (v)
  121.      register struct variable *v;
  122. {
  123.   char *value;
  124.  
  125.   if (v->expanding)
  126.     {
  127.       /* Expanding V causes infinite recursion.  Lose.  */
  128.       if (reading_filename == 0)
  129.     fatal ("Recursive variable `%s' references itself (eventually)",
  130.            v->name);
  131.       else
  132.     makefile_fatal
  133.       (reading_filename, *reading_lineno_ptr, 
  134.        "Recursive variable `%s' references itself (eventually)",
  135.        v->name);
  136.     }
  137.  
  138.   v->expanding = 1;
  139.   value = allocated_variable_expand (v->value);
  140.   v->expanding = 0;
  141.  
  142.   return value;
  143. }
  144.  
  145. /* Warn that NAME is an undefined variable.  */
  146.  
  147. #ifdef __GNUC__
  148. __inline
  149. #endif
  150. static void
  151. warn_undefined (name, length)
  152.      char *name;
  153.      unsigned int length;
  154. {
  155.   if (warn_undefined_variables_flag)
  156.     {
  157.       static const char warnmsg[] = "warning: undefined variable `%.*s'";
  158.       if (reading_filename != 0)
  159.     makefile_error (reading_filename, *reading_lineno_ptr,
  160.             warnmsg, length, name);
  161.       else
  162.     error (warnmsg, length, name);
  163.     }
  164. }
  165.  
  166. /* Expand a simple reference to variable NAME, which is LENGTH chars long.  */
  167.  
  168. #ifdef __GNUC__
  169. __inline
  170. #endif
  171. static char *
  172. reference_variable (o, name, length)
  173.      char *o;
  174.      char *name;
  175.      unsigned int length;
  176. {
  177.   register struct variable *v = lookup_variable (name, length);
  178.  
  179.   if (v == 0)
  180.     warn_undefined (name, length);
  181.  
  182.   if (v != 0 && *v->value != '\0')
  183.     {
  184.       char *value = (v->recursive ? recursively_expand (v) : v->value);
  185.       o = variable_buffer_output (o, value, strlen (value));
  186.       if (v->recursive)
  187.     free (value);
  188.     }
  189.  
  190.   return o;
  191. }
  192.  
  193. /* Scan LINE for variable references and expansion-function calls.
  194.    Build in `variable_buffer' the result of expanding the references and calls.
  195.    Return the address of the resulting string, which is null-terminated
  196.    and is valid only until the next time this function is called.  */
  197.  
  198. char *
  199. variable_expand (line)
  200.      register char *line;
  201. {
  202.   register struct variable *v;
  203.   register char *p, *o, *p1;
  204.  
  205.   p = line;
  206.   o = initialize_variable_output ();
  207.  
  208.   while (1)
  209.     {
  210.       /* Copy all following uninteresting chars all at once to the
  211.          variable output buffer, and skip them.  Uninteresting chars end
  212.      at the next $ or the end of the input.  */
  213.  
  214.       p1 = index (p, '$');
  215.  
  216.       o = variable_buffer_output (o, p, p1 != 0 ? p1 - p : strlen (p) + 1);
  217.  
  218.       if (p1 == 0)
  219.     break;
  220.       p = p1 + 1;
  221.  
  222.       /* Dispatch on the char that follows the $.  */
  223.  
  224.       switch (*p)
  225.     {
  226.     case '$':
  227.       /* $$ seen means output one $ to the variable output buffer.  */
  228.       o = variable_buffer_output (o, p, 1);
  229.       break;
  230.  
  231.     case '(':
  232.     case '{':
  233.       /* $(...) or ${...} is the general case of substitution.  */
  234.       {
  235.         char openparen = *p;
  236.         char closeparen = (openparen == '(') ? ')' : '}';
  237.         register char *beg = p + 1;
  238.         int free_beg = 0;
  239.         char *op, *begp;
  240.         char *end, *colon;
  241.  
  242.         op = o;
  243.         begp = p;
  244.         if (handle_function (&op, &begp))
  245.           {
  246.         o = op;
  247.         p = begp;
  248.         break;
  249.           }
  250.  
  251.         /* Is there a variable reference inside the parens or braces?
  252.            If so, expand it before expanding the entire reference.  */
  253.  
  254.         end = index (beg, closeparen);
  255.         if (end == 0)
  256.           {
  257.         /* Unterminated variable reference.  */
  258.         if (reading_filename != 0)
  259.           makefile_fatal (reading_filename, *reading_lineno_ptr,
  260.                   "unterminated variable reference");
  261.         else
  262.           fatal ("unterminated variable reference");
  263.           }
  264.         p1 = lindex (beg, end, '$');
  265.         if (p1 != 0)
  266.           {
  267.         /* BEG now points past the opening paren or brace.
  268.            Count parens or braces until it is matched.  */
  269.         int count = 0;
  270.         for (p = beg; *p != '\0'; ++p)
  271.           {
  272.             if (*p == openparen)
  273.               ++count;
  274.             else if (*p == closeparen && --count < 0)
  275.               break;
  276.           }
  277.         /* If COUNT is >= 0, there were unmatched opening parens
  278.            or braces, so we go to the simple case of a variable name
  279.            such as `$($(a)'.  */
  280.         if (count < 0)
  281.           {
  282.             beg = expand_argument (beg, p); /* Expand the name.  */
  283.             free_beg = 1; /* Remember to free BEG when finished.  */
  284.             end = index (beg, '\0');
  285.           }
  286.           }
  287.         else
  288.           /* Advance P to the end of this reference.  After we are
  289.                  finished expanding this one, P will be incremented to
  290.                  continue the scan.  */
  291.           p = end;
  292.  
  293.         /* This is not a reference to a built-in function and
  294.            any variable references inside are now expanded.
  295.            Is the resultant text a substitution reference?  */
  296.  
  297.         colon = lindex (beg, end, ':');
  298.         if (colon != 0)
  299.           {
  300.         /* This looks like a substitution reference: $(FOO:A=B).  */
  301.         char *subst_beg, *subst_end, *replace_beg, *replace_end;
  302.  
  303.         subst_beg = colon + 1;
  304.         subst_end = index (subst_beg, '=');
  305.         if (subst_end == 0)
  306.           /* There is no = in sight.  Punt on the substitution
  307.              reference and treat this as a variable name containing
  308.              a colon, in the code below.  */
  309.           colon = 0;
  310.         else
  311.           {
  312.             replace_beg = subst_end + 1;
  313.             replace_end = end;
  314.  
  315.             /* Extract the variable name before the colon
  316.                and look up that variable.  */
  317.             v = lookup_variable (beg, colon - beg);
  318.             if (v == 0)
  319.               warn_undefined (beg, colon - beg);
  320.  
  321.             if (v != 0 && *v->value != '\0')
  322.               {
  323.             char *value = (v->recursive ? recursively_expand (v)
  324.                        : v->value);
  325.             char *pattern, *percent;
  326.             if (free_beg)
  327.               {
  328.                 *subst_end = '\0';
  329.                 pattern = subst_beg;
  330.               }
  331.             else
  332.               {
  333.                 pattern = (char *) alloca (subst_end - subst_beg
  334.                                + 1);
  335.                 bcopy (subst_beg, pattern, subst_end - subst_beg);
  336.                 pattern[subst_end - subst_beg] = '\0';
  337.               }
  338.             percent = find_percent (pattern);
  339.             if (percent != 0)
  340.               {
  341.                 char *replace;
  342.                 if (free_beg)
  343.                   {
  344.                 *replace_end = '\0';
  345.                 replace = replace_beg;
  346.                   }
  347.                 else
  348.                   {
  349.                 replace = (char *) alloca (replace_end
  350.                                - replace_beg
  351.                                + 1);
  352.                 bcopy (replace_beg, replace,
  353.                        replace_end - replace_beg);
  354.                 replace[replace_end - replace_beg] = '\0';
  355.                   }
  356.                 
  357.                 o = patsubst_expand (o, value, pattern, replace,
  358.                          percent, (char *) 0);
  359.               }
  360.             else
  361.               o = subst_expand (o, value,
  362.                         pattern, replace_beg,
  363.                         strlen (pattern),
  364.                         end - replace_beg,
  365.                         0, 1);
  366.             if (v->recursive)
  367.               free (value);
  368.               }
  369.           }
  370.           }
  371.  
  372.         if (colon == 0)
  373.           /* This is an ordinary variable reference.
  374.          Look up the value of the variable.  */
  375.         o = reference_variable (o, beg, end - beg);
  376.  
  377.       if (free_beg)
  378.         free (beg);
  379.       }
  380.       break;
  381.  
  382.     case '\0':
  383.       break;
  384.  
  385.     default:
  386.       if (isblank (p[-1]))
  387.         break;
  388.  
  389.       /* A $ followed by a random char is a variable reference:
  390.          $a is equivalent to $(a).  */
  391.       {
  392.         /* We could do the expanding here, but this way
  393.            avoids code repetition at a small performance cost.  */
  394.         char name[5];
  395.         name[0] = '$';
  396.         name[1] = '(';
  397.         name[2] = *p;
  398.         name[3] = ')';
  399.         name[4] = '\0';
  400.         p1 = allocated_variable_expand (name);
  401.         o = variable_buffer_output (o, p1, strlen (p1));
  402.         free (p1);
  403.       }
  404.  
  405.       break;
  406.     }      
  407.  
  408.       if (*p == '\0')
  409.     break;
  410.       else
  411.     ++p;
  412.     }
  413.  
  414.   (void) variable_buffer_output (o, "", 1);
  415.   return initialize_variable_output ();
  416. }
  417.  
  418. /* Expand an argument for an expansion function.
  419.    The text starting at STR and ending at END is variable-expanded
  420.    into a null-terminated string that is returned as the value.
  421.    This is done without clobbering `variable_buffer' or the current
  422.    variable-expansion that is in progress.  */
  423.  
  424. char *
  425. expand_argument (str, end)
  426.      char *str, *end;
  427. {
  428.   char *tmp;
  429.  
  430.   if (*end == '\0')
  431.     tmp = str;
  432.   else
  433.     {
  434.       tmp = (char *) alloca (end - str + 1);
  435.       bcopy (str, tmp, end - str);
  436.       tmp[end - str] = '\0';
  437.     }
  438.  
  439.   return allocated_variable_expand (tmp);
  440. }
  441.  
  442. /* Expand LINE for FILE.  Error messages refer to the file and line where
  443.    FILE's commands were found.  Expansion uses FILE's variable set list.  */
  444.  
  445. char *
  446. variable_expand_for_file (line, file)
  447.      char *line;
  448.      register struct file *file;
  449. {
  450.   char *result;
  451.   struct variable_set_list *save;
  452.  
  453.   if (file == 0)
  454.     return variable_expand (line);
  455.  
  456.   save = current_variable_set_list;
  457.   current_variable_set_list = file->variables;
  458.   reading_filename = file->cmds->filename;
  459.   reading_lineno_ptr = &file->cmds->lineno;
  460.   result = variable_expand (line);
  461.   current_variable_set_list = save;
  462.   reading_filename = 0;
  463.   reading_lineno_ptr = 0;
  464.  
  465.   return result;
  466. }
  467.  
  468. /* Like variable_expand_for_file, but the returned string is malloc'd.
  469.    This function is called a lot.  It wants to be efficient.  */
  470.  
  471. char *
  472. allocated_variable_expand_for_file (line, file)
  473.      char *line;
  474.      struct file *file;
  475. {
  476.   char *value;
  477.  
  478.   char *obuf = variable_buffer;
  479.   unsigned int olen = variable_buffer_length;
  480.  
  481.   variable_buffer = 0;
  482.  
  483.   value = variable_expand_for_file (line, file);
  484.  
  485. #if 0
  486.   /* Waste a little memory and save time.  */
  487.   value = xrealloc (value, strlen (value))
  488. #endif
  489.  
  490.   variable_buffer = obuf;
  491.   variable_buffer_length = olen;
  492.  
  493.   return value;
  494. }
  495.