Change make license
[platform/upstream/make.git] / src / expand.c
1 /* Variable expansion functions for GNU Make.
2 Copyright (C) 1988-2020 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 it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
8 version.
9
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along with
15 this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 #include "makeint.h"
18
19 #include <assert.h>
20
21 #include "filedef.h"
22 #include "job.h"
23 #include "commands.h"
24 #include "variable.h"
25 #include "rule.h"
26
27 /* Initially, any errors reported when expanding strings will be reported
28    against the file where the error appears.  */
29 const floc **expanding_var = &reading_file;
30
31 /* The next two describe the variable output buffer.
32    This buffer is used to hold the variable-expansion of a line of the
33    makefile.  It is made bigger with realloc whenever it is too small.
34    variable_buffer_length is the size currently allocated.
35    variable_buffer is the address of the buffer.
36
37    For efficiency, it's guaranteed that the buffer will always have
38    VARIABLE_BUFFER_ZONE extra bytes allocated.  This allows you to add a few
39    extra chars without having to call a function.  Note you should never use
40    these bytes unless you're _sure_ you have room (you know when the buffer
41    length was last checked.  */
42
43 #define VARIABLE_BUFFER_ZONE    5
44
45 static size_t variable_buffer_length;
46 char *variable_buffer;
47
48 /* Subroutine of variable_expand and friends:
49    The text to add is LENGTH chars starting at STRING to the variable_buffer.
50    The text is added to the buffer at PTR, and the updated pointer into
51    the buffer is returned as the value.  Thus, the value returned by
52    each call to variable_buffer_output should be the first argument to
53    the following call.  */
54
55 char *
56 variable_buffer_output (char *ptr, const char *string, size_t length)
57 {
58   size_t newlen = length + (ptr - variable_buffer);
59
60   if ((newlen + VARIABLE_BUFFER_ZONE) > variable_buffer_length)
61     {
62       size_t offset = ptr - variable_buffer;
63       variable_buffer_length = (newlen + 100 > 2 * variable_buffer_length
64                                 ? newlen + 100
65                                 : 2 * variable_buffer_length);
66       variable_buffer = xrealloc (variable_buffer, variable_buffer_length);
67       ptr = variable_buffer + offset;
68     }
69
70   memcpy (ptr, string, length);
71   return ptr + length;
72 }
73
74 /* Return a pointer to the beginning of the variable buffer.  */
75
76 static char *
77 initialize_variable_output (void)
78 {
79   /* If we don't have a variable output buffer yet, get one.  */
80
81   if (variable_buffer == 0)
82     {
83       variable_buffer_length = 200;
84       variable_buffer = xmalloc (variable_buffer_length);
85       variable_buffer[0] = '\0';
86     }
87
88   return variable_buffer;
89 }
90 \f
91 /* Recursively expand V.  The returned string is malloc'd.  */
92
93 static char *allocated_variable_append (const struct variable *v);
94
95 char *
96 recursively_expand_for_file (struct variable *v, struct file *file)
97 {
98   char *value;
99   const floc *this_var;
100   const floc **saved_varp;
101   struct variable_set_list *save = 0;
102   int set_reading = 0;
103
104   /* Don't install a new location if this location is empty.
105      This can happen for command-line variables, builtin variables, etc.  */
106   saved_varp = expanding_var;
107   if (v->fileinfo.filenm)
108     {
109       this_var = &v->fileinfo;
110       expanding_var = &this_var;
111     }
112
113   /* If we have no other file-reading context, use the variable's context. */
114   if (!reading_file)
115     {
116       set_reading = 1;
117       reading_file = &v->fileinfo;
118     }
119
120   if (v->expanding)
121     {
122       if (!v->exp_count)
123         /* Expanding V causes infinite recursion.  Lose.  */
124         OS (fatal, *expanding_var,
125             _("Recursive variable '%s' references itself (eventually)"),
126             v->name);
127       --v->exp_count;
128     }
129
130   if (file)
131     {
132       save = current_variable_set_list;
133       current_variable_set_list = file->variables;
134     }
135
136   v->expanding = 1;
137   if (v->append)
138     value = allocated_variable_append (v);
139   else
140     value = allocated_variable_expand (v->value);
141   v->expanding = 0;
142
143   if (set_reading)
144     reading_file = 0;
145
146   if (file)
147     current_variable_set_list = save;
148
149   expanding_var = saved_varp;
150
151   return value;
152 }
153
154 /* Expand a simple reference to variable NAME, which is LENGTH chars long.  */
155
156 #ifdef __GNUC__
157 __inline
158 #endif
159 static char *
160 reference_variable (char *o, const char *name, size_t length)
161 {
162   struct variable *v;
163   char *value;
164
165   v = lookup_variable (name, length);
166
167   if (v == 0)
168     warn_undefined (name, length);
169
170   /* If there's no variable by that name or it has no value, stop now.  */
171   if (v == 0 || (*v->value == '\0' && !v->append))
172     return o;
173
174   value = (v->recursive ? recursively_expand (v) : v->value);
175
176   o = variable_buffer_output (o, value, strlen (value));
177
178   if (v->recursive)
179     free (value);
180
181   return o;
182 }
183 \f
184 /* Scan STRING for variable references and expansion-function calls.  Only
185    LENGTH bytes of STRING are actually scanned.  If LENGTH is -1, scan until
186    a null byte is found.
187
188    Write the results to LINE, which must point into 'variable_buffer'.  If
189    LINE is NULL, start at the beginning of the buffer.
190    Return a pointer to LINE, or to the beginning of the buffer if LINE is
191    NULL.
192  */
193 char *
194 variable_expand_string (char *line, const char *string, size_t length)
195 {
196   struct variable *v;
197   const char *p, *p1;
198   char *save;
199   char *o;
200   size_t line_offset;
201
202   if (!line)
203     line = initialize_variable_output ();
204   o = line;
205   line_offset = line - variable_buffer;
206
207   if (length == 0)
208     {
209       variable_buffer_output (o, "", 1);
210       return (variable_buffer);
211     }
212
213   /* We need a copy of STRING: due to eval, it's possible that it will get
214      freed as we process it (it might be the value of a variable that's reset
215      for example).  Also having a nil-terminated string is handy.  */
216   save = length == SIZE_MAX ? xstrdup (string) : xstrndup (string, length);
217   p = save;
218
219   while (1)
220     {
221       /* Copy all following uninteresting chars all at once to the
222          variable output buffer, and skip them.  Uninteresting chars end
223          at the next $ or the end of the input.  */
224
225       p1 = strchr (p, '$');
226
227       o = variable_buffer_output (o, p, p1 != 0 ? (size_t) (p1 - p) : strlen (p) + 1);
228
229       if (p1 == 0)
230         break;
231       p = p1 + 1;
232
233       /* Dispatch on the char that follows the $.  */
234
235       switch (*p)
236         {
237         case '$':
238         case '\0':
239           /* $$ or $ at the end of the string means output one $ to the
240              variable output buffer.  */
241           o = variable_buffer_output (o, p1, 1);
242           break;
243
244         case '(':
245         case '{':
246           /* $(...) or ${...} is the general case of substitution.  */
247           {
248             char openparen = *p;
249             char closeparen = (openparen == '(') ? ')' : '}';
250             const char *begp;
251             const char *beg = p + 1;
252             char *op;
253             char *abeg = NULL;
254             const char *end, *colon;
255
256             op = o;
257             begp = p;
258             if (handle_function (&op, &begp))
259               {
260                 o = op;
261                 p = begp;
262                 break;
263               }
264
265             /* Is there a variable reference inside the parens or braces?
266                If so, expand it before expanding the entire reference.  */
267
268             end = strchr (beg, closeparen);
269             if (end == 0)
270               /* Unterminated variable reference.  */
271               O (fatal, *expanding_var, _("unterminated variable reference"));
272             p1 = lindex (beg, end, '$');
273             if (p1 != 0)
274               {
275                 /* BEG now points past the opening paren or brace.
276                    Count parens or braces until it is matched.  */
277                 int count = 0;
278                 for (p = beg; *p != '\0'; ++p)
279                   {
280                     if (*p == openparen)
281                       ++count;
282                     else if (*p == closeparen && --count < 0)
283                       break;
284                   }
285                 /* If COUNT is >= 0, there were unmatched opening parens
286                    or braces, so we go to the simple case of a variable name
287                    such as '$($(a)'.  */
288                 if (count < 0)
289                   {
290                     abeg = expand_argument (beg, p); /* Expand the name.  */
291                     beg = abeg;
292                     end = strchr (beg, '\0');
293                   }
294               }
295             else
296               /* Advance P to the end of this reference.  After we are
297                  finished expanding this one, P will be incremented to
298                  continue the scan.  */
299               p = end;
300
301             /* This is not a reference to a built-in function and
302                any variable references inside are now expanded.
303                Is the resultant text a substitution reference?  */
304
305             colon = lindex (beg, end, ':');
306             if (colon)
307               {
308                 /* This looks like a substitution reference: $(FOO:A=B).  */
309                 const char *subst_beg = colon + 1;
310                 const char *subst_end = lindex (subst_beg, end, '=');
311                 if (subst_end == 0)
312                   /* There is no = in sight.  Punt on the substitution
313                      reference and treat this as a variable name containing
314                      a colon, in the code below.  */
315                   colon = 0;
316                 else
317                   {
318                     const char *replace_beg = subst_end + 1;
319                     const char *replace_end = end;
320
321                     /* Extract the variable name before the colon
322                        and look up that variable.  */
323                     v = lookup_variable (beg, colon - beg);
324                     if (v == 0)
325                       warn_undefined (beg, colon - beg);
326
327                     /* If the variable is not empty, perform the
328                        substitution.  */
329                     if (v != 0 && *v->value != '\0')
330                       {
331                         char *pattern, *replace, *ppercent, *rpercent;
332                         char *value = (v->recursive
333                                        ? recursively_expand (v)
334                                        : v->value);
335
336                         /* Copy the pattern and the replacement.  Add in an
337                            extra % at the beginning to use in case there
338                            isn't one in the pattern.  */
339                         pattern = alloca (subst_end - subst_beg + 2);
340                         *(pattern++) = '%';
341                         memcpy (pattern, subst_beg, subst_end - subst_beg);
342                         pattern[subst_end - subst_beg] = '\0';
343
344                         replace = alloca (replace_end - replace_beg + 2);
345                         *(replace++) = '%';
346                         memcpy (replace, replace_beg,
347                                replace_end - replace_beg);
348                         replace[replace_end - replace_beg] = '\0';
349
350                         /* Look for %.  Set the percent pointers properly
351                            based on whether we find one or not.  */
352                         ppercent = find_percent (pattern);
353                         if (ppercent)
354                           {
355                             ++ppercent;
356                             rpercent = find_percent (replace);
357                             if (rpercent)
358                               ++rpercent;
359                           }
360                         else
361                           {
362                             ppercent = pattern;
363                             rpercent = replace;
364                             --pattern;
365                             --replace;
366                           }
367
368                         o = patsubst_expand_pat (o, value, pattern, replace,
369                                                  ppercent, rpercent);
370
371                         if (v->recursive)
372                           free (value);
373                       }
374                   }
375               }
376
377             if (colon == 0)
378               /* This is an ordinary variable reference.
379                  Look up the value of the variable.  */
380                 o = reference_variable (o, beg, end - beg);
381
382             free (abeg);
383           }
384           break;
385
386         default:
387           if (ISSPACE (p[-1]))
388             break;
389
390           /* A $ followed by a random char is a variable reference:
391              $a is equivalent to $(a).  */
392           o = reference_variable (o, p, 1);
393
394           break;
395         }
396
397       if (*p == '\0')
398         break;
399
400       ++p;
401     }
402
403   free (save);
404
405   variable_buffer_output (o, "", 1);
406   return (variable_buffer + line_offset);
407 }
408 \f
409 /* Scan LINE for variable references and expansion-function calls.
410    Build in 'variable_buffer' the result of expanding the references and calls.
411    Return the address of the resulting string, which is null-terminated
412    and is valid only until the next time this function is called.  */
413
414 char *
415 variable_expand (const char *line)
416 {
417   return variable_expand_string (NULL, line, SIZE_MAX);
418 }
419 \f
420 /* Expand an argument for an expansion function.
421    The text starting at STR and ending at END is variable-expanded
422    into a null-terminated string that is returned as the value.
423    This is done without clobbering 'variable_buffer' or the current
424    variable-expansion that is in progress.  */
425
426 char *
427 expand_argument (const char *str, const char *end)
428 {
429   char *tmp, *alloc = NULL;
430   char *r;
431
432   if (str == end)
433     return xstrdup ("");
434
435   if (!end || *end == '\0')
436     return allocated_variable_expand (str);
437
438   if (end - str + 1 > 1000)
439     tmp = alloc = xmalloc (end - str + 1);
440   else
441     tmp = alloca (end - str + 1);
442
443   memcpy (tmp, str, end - str);
444   tmp[end - str] = '\0';
445
446   r = allocated_variable_expand (tmp);
447
448   free (alloc);
449
450   return r;
451 }
452 \f
453 /* Expand LINE for FILE.  Error messages refer to the file and line where
454    FILE's commands were found.  Expansion uses FILE's variable set list.  */
455
456 char *
457 variable_expand_for_file (const char *line, struct file *file)
458 {
459   char *result;
460   struct variable_set_list *savev;
461   const floc *savef;
462
463   if (file == 0)
464     return variable_expand (line);
465
466   savev = current_variable_set_list;
467   current_variable_set_list = file->variables;
468
469   savef = reading_file;
470   if (file->cmds && file->cmds->fileinfo.filenm)
471     reading_file = &file->cmds->fileinfo;
472   else
473     reading_file = 0;
474
475   result = variable_expand (line);
476
477   current_variable_set_list = savev;
478   reading_file = savef;
479
480   return result;
481 }
482 \f
483 /* Like allocated_variable_expand, but for += target-specific variables.
484    First recursively construct the variable value from its appended parts in
485    any upper variable sets.  Then expand the resulting value.  */
486
487 static char *
488 variable_append (const char *name, size_t length,
489                  const struct variable_set_list *set, int local)
490 {
491   const struct variable *v;
492   char *buf = 0;
493   int nextlocal;
494
495   /* If there's nothing left to check, return the empty buffer.  */
496   if (!set)
497     return initialize_variable_output ();
498
499   /* If this set is local and the next is not a parent, then next is local.  */
500   nextlocal = local && set->next_is_parent == 0;
501
502   /* Try to find the variable in this variable set.  */
503   v = lookup_variable_in_set (name, length, set->set);
504
505   /* If there isn't one, or this one is private, try the set above us.  */
506   if (!v || (!local && v->private_var))
507     return variable_append (name, length, set->next, nextlocal);
508
509   /* If this variable type is append, first get any upper values.
510      If not, initialize the buffer.  */
511   if (v->append)
512     buf = variable_append (name, length, set->next, nextlocal);
513   else
514     buf = initialize_variable_output ();
515
516   /* Append this value to the buffer, and return it.
517      If we already have a value, first add a space.  */
518   if (buf > variable_buffer)
519     buf = variable_buffer_output (buf, " ", 1);
520
521   /* Either expand it or copy it, depending.  */
522   if (! v->recursive)
523     return variable_buffer_output (buf, v->value, strlen (v->value));
524
525   buf = variable_expand_string (buf, v->value, strlen (v->value));
526   return (buf + strlen (buf));
527 }
528
529
530 static char *
531 allocated_variable_append (const struct variable *v)
532 {
533   char *val;
534
535   /* Construct the appended variable value.  */
536
537   char *obuf = variable_buffer;
538   size_t olen = variable_buffer_length;
539
540   variable_buffer = 0;
541
542   val = variable_append (v->name, strlen (v->name),
543                          current_variable_set_list, 1);
544   variable_buffer_output (val, "", 1);
545   val = variable_buffer;
546
547   variable_buffer = obuf;
548   variable_buffer_length = olen;
549
550   return val;
551 }
552
553 /* Like variable_expand_for_file, but the returned string is malloc'd.
554    This function is called a lot.  It wants to be efficient.  */
555
556 char *
557 allocated_variable_expand_for_file (const char *line, struct file *file)
558 {
559   char *value;
560
561   char *obuf = variable_buffer;
562   size_t olen = variable_buffer_length;
563
564   variable_buffer = 0;
565
566   value = variable_expand_for_file (line, file);
567
568   variable_buffer = obuf;
569   variable_buffer_length = olen;
570
571   return value;
572 }
573
574 /* Install a new variable_buffer context, returning the current one for
575    safe-keeping.  */
576
577 void
578 install_variable_buffer (char **bufp, size_t *lenp)
579 {
580   *bufp = variable_buffer;
581   *lenp = variable_buffer_length;
582
583   variable_buffer = 0;
584   initialize_variable_output ();
585 }
586
587 /* Restore a previously-saved variable_buffer setting (free the current one).
588  */
589
590 void
591 restore_variable_buffer (char *buf, size_t len)
592 {
593   free (variable_buffer);
594
595   variable_buffer = buf;
596   variable_buffer_length = len;
597 }