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