forked from rmyorston/pdpmake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.c
1085 lines (993 loc) · 23.4 KB
/
input.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Parse a makefile
*/
#include "make.h"
#include <glob.h>
int lineno; // Physical line number in file
int dispno; // Line number for display purposes
/*
* Return a pointer to the next blank-delimited word or NULL if
* there are none left.
*/
static char *
gettok(char **ptr)
{
char *p;
while (isblank(**ptr)) // Skip blanks
(*ptr)++;
if (**ptr == '\0') // Nothing after blanks
return NULL;
p = *ptr; // Word starts here
while (**ptr != '\0' && !isblank(**ptr))
(*ptr)++; // Find end of word
// Terminate token and move on unless already at end of string
if (**ptr != '\0')
*(*ptr)++ = '\0';
return(p);
}
/*
* Skip over (possibly adjacent or nested) macro expansions.
*/
static char *
skip_macro(const char *s)
{
while (*s && s[0] == '$') {
if (s[1] == '(' || s[1] == '{') {
char end = *++s == '(' ? ')' : '}';
while (*s && *s != end)
s = skip_macro(s + 1);
if (*s == end)
++s;
} else if (s[1] != '\0') {
s += 2;
} else {
break;
}
}
return (char *)s;
}
#if !ENABLE_FEATURE_MAKE_POSIX_202X
# define modify_words(v, m, l, fp, rp, fs, rs) modify_words(v, m, l, fs, rs)
#endif
/*
* Process each whitespace-separated word in the input string:
*
* - replace paths with their directory or filename part
* - replace prefixes and suffixes
*
* Returns an allocated string or NULL if the input is unmodified.
*/
static char *
modify_words(const char *val, int modifier, size_t lenf,
const char *find_pref, const char *repl_pref,
const char *find_suff, const char *repl_suff)
{
char *s, *copy, *word, *sep, *newword, *buf = NULL;
#if ENABLE_FEATURE_MAKE_POSIX_202X
size_t find_pref_len = 0, find_suff_len = 0;
#endif
if (!modifier && !lenf)
return buf;
#if ENABLE_FEATURE_MAKE_POSIX_202X
if (find_pref) {
// get length of find prefix, e.g: src/
find_pref_len = strlen(find_pref);
// get length of find suffix, e.g: .c
find_suff_len = lenf - find_pref_len - 1;
}
#endif
s = copy = xstrdup(val);
while ((word = gettok(&s)) != NULL) {
newword = NULL;
if (modifier) {
sep = strrchr(word, '/');
if (modifier == 'D') {
if (!sep) {
word[0] = '.'; // no '/', return "."
sep = word + 1;
} else if (sep == word) {
// '/' at start of word, return "/"
sep = word + 1;
}
// else terminate at separator
*sep = '\0';
} else if (/* modifier == 'F' && */ sep) {
word = sep + 1;
}
}
if (lenf) {
size_t lenw = strlen(word);
#if ENABLE_FEATURE_MAKE_POSIX_202X
// This code implements pattern macro expansions:
// https://austingroupbugs.net/view.php?id=519
//
// find: <prefix>%<suffix>
// example: src/%.c
if (lenw >= lenf - 1 && find_pref) {
// If prefix and suffix of word match find_pref and
// find_suff, then do substitution.
if (strncmp(word, find_pref, find_pref_len) == 0 &&
strcmp(word + lenw - find_suff_len, find_suff) == 0) {
// replace: <prefix>[%<suffix>]
// example: build/%.o or build/all.o (notice no %)
// If repl_suff is NULL, replace whole word with repl_pref.
if (!repl_suff) {
word = newword = xstrdup(repl_pref);
} else {
word[lenw - find_suff_len] = '\0';
word = newword = xconcat3(repl_pref,
word + find_pref_len, repl_suff);
}
}
} else
#endif
if (lenw >= lenf && strcmp(word + lenw - lenf, find_suff) == 0) {
word[lenw - lenf] = '\0';
word = newword = xconcat3(word, repl_suff, "");
}
}
buf = xappendword(buf, word);
free(newword);
}
free(copy);
return buf;
}
/*
* Return a pointer to the next instance of a given character. Macro
* expansions are skipped so the ':' and '=' in $(VAR:.s1=.s2) aren't
* detected as separators for rules or macro definitions.
*/
static char *
find_char(const char *str, int c)
{
const char *s;
for (s = skip_macro(str); *s; s = skip_macro(s + 1)) {
if (*s == c)
return (char *)s;
}
return NULL;
}
/*
* Recursively expand any macros in str to an allocated string.
*/
char *
expand_macros(const char *str, int except_dollar)
{
char *exp, *newexp, *s, *t, *p, *q, *name;
char *find, *replace, *modified;
char *expval, *expfind, *find_suff, *repl_suff;
#if ENABLE_FEATURE_MAKE_POSIX_202X
char *find_pref = NULL, *repl_pref = NULL;
#endif
size_t lenf;
char modifier;
struct macro *mp;
exp = xstrdup(str);
for (t = exp; *t; t++) {
if (*t == '$') {
if (t[1] == '\0') {
break;
}
#if ENABLE_FEATURE_MAKE_POSIX_202X
if (t[1] == '$' && except_dollar) {
t++;
continue;
}
#endif
// Need to expand a macro. Find its extent (s to t inclusive)
// and take a copy of its content.
s = t;
t++;
if (*t == '{' || *t == '(') {
t = find_char(t, *t == '{' ? '}' : ')');
if (t == NULL)
error("unterminated variable '%s'", s);
name = xstrndup(s + 2, t - s - 2);
} else {
name = xmalloc(2);
name[0] = *t;
name[1] = '\0';
}
// Only do suffix replacement or pattern macro expansion
// if both ':' and '=' are found. This is indicated by
// lenf != 0.
expfind = NULL;
find_suff = repl_suff = NULL;
lenf = 0;
if ((find = find_char(name, ':'))) {
*find++ = '\0';
expfind = expand_macros(find, FALSE);
if ((replace = find_char(expfind, '='))) {
*replace++ = '\0';
lenf = strlen(expfind);
#if ENABLE_FEATURE_MAKE_POSIX_202X
if (!POSIX_2017 && (find_suff = strchr(expfind, '%'))) {
find_pref = expfind;
repl_pref = replace;
*find_suff++ = '\0';
if ((repl_suff = strchr(replace, '%')))
*repl_suff++ = '\0';
} else
#endif
{
find_suff = expfind;
repl_suff = replace;
}
}
}
p = q = name;
#if ENABLE_FEATURE_MAKE_EXTENSIONS
// If not in POSIX mode expand macros in the name.
if (!posix) {
char *expname = expand_macros(name, FALSE);
free(name);
name = expname;
} else
#endif
// Skip over nested expansions in name
do {
*q++ = *p;
} while ((p = skip_macro(p + 1)) && *p);
// The internal macros support 'D' and 'F' modifiers
modifier = '\0';
switch (name[0]) {
#if ENABLE_FEATURE_MAKE_POSIX_202X
case '^':
if (POSIX_2017)
break;
// fall through
#endif
case '@': case '%': case '?': case '<': case '*':
if ((name[1] == 'D' || name[1] == 'F') && name[2] == '\0') {
modifier = name[1];
name[1] = '\0';
}
break;
}
modified = NULL;
if ((mp = getmp(name))) {
// Recursive expansion
if (mp->m_flag)
error("recursive macro %s", name);
#if ENABLE_FEATURE_MAKE_POSIX_202X
// Note if we've expanded $(MAKE)
if (strcmp(name, "MAKE") == 0)
opts |= OPT_make;
#endif
mp->m_flag = TRUE;
expval = expand_macros(mp->m_val, FALSE);
mp->m_flag = FALSE;
modified = modify_words(expval, modifier, lenf,
find_pref, repl_pref, find_suff, repl_suff);
if (modified)
free(expval);
else
modified = expval;
}
free(name);
free(expfind);
if (modified && *modified) {
// The text to be replaced by the macro expansion is
// from s to t inclusive.
*s = '\0';
newexp = xconcat3(exp, modified, t + 1);
t = newexp + (s - exp) + strlen(modified) - 1;
free(exp);
exp = newexp;
} else {
// Macro wasn't expanded or expanded to nothing.
// Close the space occupied by the macro reference.
q = t + 1;
t = s - 1;
while ((*s++ = *q++))
continue;
}
free(modified);
}
}
return exp;
}
/*
* Process a non-command line
*/
static char *
process_line(char *s)
{
char *r, *t;
// Skip leading blanks
while (isblank(*s))
s++;
r = s;
// Strip comment
t = strchr(s, '#');
if (t)
*t = '\0';
// Replace escaped newline and any leading white space on the
// following line with a single space. Stop processing at a
// non-escaped newline.
for (t = s; *s && *s != '\n'; ) {
if (s[0] == '\\' && s[1] == '\n') {
s += 2;
while (isspace(*s))
++s;
*t++ = ' ';
} else {
*t++ = *s++;
}
}
*t = '\0';
return r;
}
#if ENABLE_FEATURE_MAKE_EXTENSIONS
enum {
INITIAL = 0,
SKIP_LINE = 1 << 0,
EXPECT_ELSE = 1 << 1,
GOT_MATCH = 1 << 2
};
#define IF_MAX 10
static uint8_t clevel = 0;
static uint8_t cstate[IF_MAX + 1] = {INITIAL};
/*
* Process conditional directives and return TRUE if the current line
* should be skipped.
*/
static int
skip_line(const char *str1)
{
char *copy, *q, *token, *next_token;
bool new_level = TRUE;
// Default is to return skip flag for current level
int ret = cstate[clevel] & SKIP_LINE;
if (*str1 == '\t')
return ret;
copy = xstrdup(str1);
q = process_line(copy);
if ((token = gettok(&q)) != NULL) {
next_token = gettok(&q);
if (strcmp(token, "endif") == 0) {
if (next_token != NULL)
error_unexpected("text");
if (clevel == 0)
error_unexpected(token);
--clevel;
ret = TRUE;
goto end;
} else if (strcmp(token, "else") == 0) {
if (!(cstate[clevel] & EXPECT_ELSE))
error_unexpected(token);
// If an earlier condition matched we'll now skip lines.
// If not we don't, though an 'else if' may override this.
if ((cstate[clevel] & GOT_MATCH))
cstate[clevel] |= SKIP_LINE;
else
cstate[clevel] &= ~SKIP_LINE;
if (next_token == NULL) {
// Simple else with no conditional directive
cstate[clevel] &= ~EXPECT_ELSE;
ret = TRUE;
goto end;
} else {
// A conditional directive is now required ('else if').
token = next_token;
next_token = gettok(&q);
new_level = FALSE;
}
}
if (strcmp(token, "ifdef") == 0 || strcmp(token, "ifndef") == 0) {
if (next_token != NULL && gettok(&q) == NULL) {
if (new_level) {
// Start a new level.
if (clevel == IF_MAX)
error("nesting too deep");
++clevel;
cstate[clevel] = EXPECT_ELSE | SKIP_LINE;
// If we were skipping lines at the previous level
// we need to continue doing that unconditionally
// at the new level.
if ((cstate[clevel - 1] & SKIP_LINE))
cstate[clevel] |= GOT_MATCH;
}
if (!(cstate[clevel] & GOT_MATCH)) {
char *t = expand_macros(next_token, FALSE);
struct macro *mp = getmp(t);
int match = mp != NULL && mp->m_val[0] != '\0';
if (token[2] == 'n')
match = !match;
if (match) {
cstate[clevel] &= ~SKIP_LINE;
cstate[clevel] |= GOT_MATCH;
}
free(t);
}
} else {
error("invalid condition");
}
ret = TRUE;
} else if (!new_level) {
error("missing conditional");
}
}
end:
free(copy);
return ret;
}
#endif
/*
* If fd is NULL read the built-in rules. Otherwise read from the
* specified file descriptor.
*/
static char *
make_fgets(char *s, int size, FILE *fd)
{
return fd ? fgets(s, size, fd) : getrules(s, size);
}
/*
* Read a newline-terminated line into an allocated string.
* Backslash-escaped newlines don't terminate the line.
* Ignore comment lines. Return NULL on EOF.
*/
static char *
readline(FILE *fd)
{
char *p, *str;
int pos = 0;
int len = 256;
str = xmalloc(len);
for (;;) {
if (make_fgets(str + pos, len - pos, fd) == NULL) {
if (pos)
return str;
free(str);
return NULL; // EOF
}
if ((p = strchr(str + pos, '\n')) == NULL) {
// Need more room
pos = len - 1;
len += 256;
str = xrealloc(str, len);
continue;
}
lineno++;
// Remove CR before LF
if (p != str && p[-1] == '\r') {
p[-1] = '\n';
*p-- = '\0';
}
// Keep going if newline has been escaped
if (p != str && p[-1] == '\\') {
pos = p - str + 1;
continue;
}
dispno = lineno;
// Check for comment lines and lines that are conditionally skipped.
p = str;
while (isblank(*p))
p++;
if (*p != '\n' && *str != '#'
IF_FEATURE_MAKE_EXTENSIONS(&& (posix || !skip_line(str)))
) {
return str;
}
pos = 0;
}
}
/*
* Return TRUE if the argument is a known suffix.
*/
static int
is_suffix(const char *s)
{
struct name *np;
struct rule *rp;
struct depend *dp;
np = newname(".SUFFIXES");
for (rp = np->n_rule; rp; rp = rp->r_next) {
for (dp = rp->r_dep; dp; dp = dp->d_next) {
if (strcmp(s, dp->d_name->n_name) == 0) {
return TRUE;
}
}
}
return FALSE;
}
#define T_NORMAL 0
#define T_SPECIAL 1
#define T_INFERENCE 2
/*
* Determine if the argument is a special target and return a set
* of flags indicating its properties.
*/
static int
target_type(char *s)
{
char *sfx;
int ret;
static const char *s_name[] = {
".DEFAULT",
".POSIX",
".IGNORE",
".PRECIOUS",
".SILENT",
".SUFFIXES",
#if ENABLE_FEATURE_MAKE_POSIX_202X
".PHONY",
#endif
};
if (*s != '.')
return T_NORMAL;
// Check for one of the known special targets
for (ret = 0; ret < sizeof(s_name)/sizeof(s_name[0]); ret++)
if (strcmp(s_name[ret], s) == 0)
return T_SPECIAL;
// Check for an inference rule
ret = T_NORMAL;
sfx = suffix(s);
if (is_suffix(sfx)) {
if (s == sfx) { // Single suffix rule
ret = T_INFERENCE;
} else {
// Suffix is valid, check that prefix is too
*sfx = '\0';
if (is_suffix(s))
ret = T_INFERENCE;
*sfx = '.';
}
}
return ret;
}
#if ENABLE_FEATURE_MAKE_EXTENSIONS
static int
ends_with_bracket(const char *s)
{
const char *t = strrchr(s, ')');
return t && t[1] == '\0';
}
#endif
/*
* Process a command line
*/
static char *
process_command(char *s)
{
char *t, *u;
// Remove tab following escaped newline. Stop processing at a
// non-escaped newline.
for (t = u = s; *u && *u != '\n'; u++) {
if (u[0] == '\\' && u[1] == '\n' && u[2] == '\t') {
*t++ = *u++;
*t++ = *u++;
} else {
*t++ = *u;
}
}
*t = '\0';
return s;
}
#if ENABLE_FEATURE_MAKE_EXTENSIONS
static char *
run_command(const char *cmd)
{
FILE *fd;
char *s, *val = NULL;
char buf[256];
size_t len = 0, nread;
if ((fd = popen(cmd, "r")) == NULL)
return val;
for (;;) {
nread = fread(buf, 1, sizeof(buf), fd);
if (nread == 0)
break;
val = xrealloc(val, len + nread + 1);
memcpy(val + len, buf, nread);
len += nread;
val[len] = '\0';
}
pclose(fd);
if (val) {
// Remove one newline from the end (BSD compatibility)
if (val[len - 1] == '\n')
val[len - 1] = '\0';
// Other newlines are changed to spaces
for (s = val; *s; ++s) {
if (*s == '\n')
*s = ' ';
}
}
return val;
}
/*
* Check for an unescaped wildcard character
*/
static int wildchar(const char *p)
{
while (*p) {
switch (*p) {
case '?':
case '*':
case '[':
return 1;
case '\\':
if (p[1] != '\0')
++p;
break;
}
++p;
}
return 0;
}
/*
* Expand any wildcards in a pattern. Return TRUE if a match is
* found, in which case the caller should call globfree() on the
* glob_t structure.
*/
static int
wildcard(char *p, glob_t *gd)
{
int ret;
char *s;
// Don't call glob() if there are no wildcards.
if (!wildchar(p)) {
nomatch:
// Remove backslashes from the name.
for (s = p; *p; ++p) {
if (*p == '\\' && p[1] != '\0')
continue;
*s++ = *p;
}
*s = '\0';
return 0;
}
memset(gd, 0, sizeof(*gd));
ret = glob(p, GLOB_NOSORT, NULL, gd);
if (ret == GLOB_NOMATCH) {
globfree(gd);
goto nomatch;
} else if (ret != 0) {
error("glob error for '%s'", p);
}
return 1;
}
#endif
/*
* Parse input from the makefile and construct a tree structure of it.
*/
void
input(FILE *fd, int ilevel)
{
char *p, *q, *s, *a, *str, *expanded, *copy;
char *str1, *str2;
struct name *np;
struct depend *dp;
struct cmd *cp;
int startno, count;
bool semicolon_cmd, seen_inference;
#if ENABLE_FEATURE_MAKE_EXTENSIONS
uint8_t old_clevel = clevel;
bool dbl;
char *lib = NULL;
glob_t gd;
int nfile, i;
char **files;
#else
const bool dbl = FALSE;
#endif
#if ENABLE_FEATURE_MAKE_POSIX_202X
bool minus;
#else
const bool minus = FALSE;
#endif
lineno = 0;
str1 = readline(fd);
while (str1) {
str2 = NULL;
if (*str1 == '\t') // Command without target
error("command not allowed here");
// Newlines and comments are handled differently in command lines
// and other types of line. Take a copy of the current line before
// processing it as a non-command line in case it contains a
// rule with a command line. That is, a line of the form:
//
// target: prereq; command
//
copy = xstrdup(str1);
str = process_line(str1);
// Check for an include line
#if ENABLE_FEATURE_MAKE_POSIX_202X
minus = !POSIX_2017 && *str == '-';
#endif
p = str + minus;
if (strncmp(p, "include", 7) == 0 && isblank(p[7])) {
const char *old_makefile = makefile;
int old_lineno = lineno;
if (ilevel > 16)
error("too many includes");
q = expanded = expand_macros(p + 7, FALSE);
while ((p = gettok(&q)) != NULL) {
FILE *ifd;
#if ENABLE_FEATURE_MAKE_POSIX_202X
if (!POSIX_2017) {
// Try to create include file or bring it up-to-date
opts |= OPT_include;
make(newname(p), 1);
opts &= ~OPT_include;
}
#endif
if ((ifd = fopen(p, "r")) == NULL) {
if (!minus)
error("can't open include file '%s'", p);
} else {
makefile = p;
input(ifd, ilevel + 1);
fclose(ifd);
}
#if ENABLE_FEATURE_MAKE_EXTENSIONS
if (posix)
break;
#endif
}
#if ENABLE_FEATURE_MAKE_EXTENSIONS
if (posix && (p == NULL || gettok(&q)))
error("one include file per line");
#endif
makefile = old_makefile;
lineno = old_lineno;
goto end_loop;
}
// Check for a macro definition
q = find_char(str, '=');
if (q != NULL) {
int level = (useenv || fd == NULL) ? 4 : 3;
#if ENABLE_FEATURE_MAKE_EXTENSIONS || ENABLE_FEATURE_MAKE_POSIX_202X
char *newq = NULL;
char eq = '\0';
if (q - 1 > str) {
switch (q[-1]) {
case ':':
# if ENABLE_FEATURE_MAKE_POSIX_202X
// '::=' and ':::=' are from POSIX 202X.
if (!POSIX_2017 && q - 2 > str && q[-2] == ':') {
if (q - 3 > str && q[-3] == ':') {
eq = 'B'; // BSD-style ':='
q[-3] = '\0';
} else {
eq = ':'; // GNU-style ':='
q[-2] = '\0';
}
break;
}
# endif
# if ENABLE_FEATURE_MAKE_EXTENSIONS
case '!':
// ':=' and '!=' are non-POSIX extensions.
if (posix)
break;
IF_FEATURE_MAKE_POSIX_202X(goto set_eq;)
# else
break;
# endif
# if ENABLE_FEATURE_MAKE_POSIX_202X
case '+':
case '?':
// '+=' and '?=' are from POSIX 202X.
if (POSIX_2017)
break;
IF_FEATURE_MAKE_EXTENSIONS(set_eq:)
# endif
eq = q[-1];
q[-1] = '\0';
break;
}
}
#endif
*q++ = '\0'; // Separate name and value
while (isblank(*q))
q++;
if ((p = strrchr(q, '\n')) != NULL)
*p = '\0';
// Expand left-hand side of assignment
p = expanded = expand_macros(str, FALSE);
if ((a = gettok(&p)) == NULL || gettok(&p))
error("invalid macro assignment");
#if ENABLE_FEATURE_MAKE_EXTENSIONS || ENABLE_FEATURE_MAKE_POSIX_202X
if (eq == ':') {
// GNU-style ':='. Expand right-hand side of assignment.
// Macro is of type immediate-expansion.
q = newq = expand_macros(q, FALSE);
level |= M_IMMEDIATE;
}
# if ENABLE_FEATURE_MAKE_POSIX_202X
else if (eq == 'B') {
// BSD-style ':='. Expand right-hand side of assignment,
// though not '$$'. Macro is of type delayed-expansion.
q = newq = expand_macros(q, TRUE);
} else if (eq == '?' && getmp(a) != NULL) {
// Skip assignment if macro is already set
goto end_loop;
} else if (eq == '+') {
// Append to current value
struct macro *mp = getmp(a);
char *rhs;
newq = mp && mp->m_val[0] ? xstrdup(mp->m_val) : NULL;
if (mp && mp->m_immediate) {
// Expand right-hand side of assignment (GNU make
// compatibility)
rhs = expand_macros(q, FALSE);
level |= M_IMMEDIATE;
} else {
rhs = q;
}
newq = xappendword(newq, rhs);
if (rhs != q)
free(rhs);
q = newq;
}
# endif
# if ENABLE_FEATURE_MAKE_EXTENSIONS
else if (eq == '!') {
char *cmd = expand_macros(q, FALSE);
q = newq = run_command(cmd);
free(cmd);
}
# endif
#endif
setmacro(a, q, level);
#if ENABLE_FEATURE_MAKE_EXTENSIONS || ENABLE_FEATURE_MAKE_POSIX_202X
free(newq);
#endif
goto end_loop;
}
// If we get here it must be a target rule
p = expanded = expand_macros(str, FALSE);
// Look for colon separator
q = find_char(p, ':');
if (q == NULL)
error("expected separator");
*q++ = '\0'; // Separate targets and prerequisites
#if ENABLE_FEATURE_MAKE_EXTENSIONS
// Double colon
dbl = !posix && *q == ':';
if (dbl)
q++;
#endif
// Look for semicolon separator
cp = NULL;
s = strchr(q, ';');
if (s) {
*s = '\0';
// Retrieve command from copy of line
if ((p = find_char(copy, ':')) && (p = strchr(p, ';')))
cp = newcmd(process_command(p + 1), cp);
}
semicolon_cmd = cp != NULL;
// Create list of prerequisites
dp = NULL;
while (((p = gettok(&q)) != NULL)) {
#if !ENABLE_FEATURE_MAKE_EXTENSIONS
np = newname(p);
dp = newdep(np, dp);
#else
char *newp = NULL;
if (!posix) {
// Allow prerequisites of form library(member1 member2).
// Leading and trailing spaces in the brackets are skipped.
if (!lib) {
s = strchr(p, '(');
if (s && !ends_with_bracket(s) && strchr(q, ')')) {
// Looks like an unterminated archive member
// with a terminator later on the line.
lib = p;
if (s[1] != '\0') {
p = newp = xconcat3(lib, ")", "");
s[1] = '\0';
} else {
continue;
}
}
} else if (ends_with_bracket(p)) {
if (*p != ')')
p = newp = xconcat3(lib, p, "");
lib = NULL;
if (newp == NULL)
continue;
} else {
p = newp = xconcat3(lib, p, ")");
}
}
// If not in POSIX mode expand wildcards in the name.
nfile = 1;
files = &p;
if (!posix && wildcard(p, &gd)) {
nfile = gd.gl_pathc;
files = gd.gl_pathv;
}
for (i = 0; i < nfile; ++i) {
np = newname(files[i]);
dp = newdep(np, dp);
}
if (files != &p)
globfree(&gd);
free(newp);
#endif /* ENABLE_FEATURE_MAKE_EXTENSIONS */
}