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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
|
/* $OpenBSD: var.c,v 1.98 2014/05/12 19:11:19 espie Exp $ */
/* $NetBSD: var.c,v 1.18 1997/03/18 19:24:46 christos Exp $ */
/*
* Copyright (c) 1999,2000,2007 Marc Espie.
*
* Extensive code modifications for the OpenBSD project.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
* PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Copyright (c) 1988, 1989, 1990, 1993
* The Regents of the University of California. All rights reserved.
* Copyright (c) 1989 by Berkeley Softworks
* All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Adam de Boor.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ohash.h>
#include "config.h"
#include "defines.h"
#include "buf.h"
#include "stats.h"
#include "pathnames.h"
#include "varmodifiers.h"
#include "var.h"
#include "varname.h"
#include "error.h"
#include "str.h"
#include "var_int.h"
#include "memory.h"
#include "symtable.h"
#include "gnode.h"
#include "dump.h"
#include "lowparse.h"
/*
* This is a harmless return value for Var_Parse that can be used by Var_Subst
* to determine if there was an error in parsing -- easier than returning
* a flag, as things outside this module don't give a hoot.
*/
char var_Error[] = "";
GNode *current_node = NULL;
/*
* Similar to var_Error, but returned when the 'err' flag for Var_Parse is
* set false. Why not just use a constant? Well, gcc likes to condense
* identical string instances...
*/
static char varNoError[] = "";
bool errorIsOkay;
static bool checkEnvFirst; /* true if environment should be searched for
* variables before the global context */
void
Var_setCheckEnvFirst(bool yes)
{
checkEnvFirst = yes;
}
/*
* The rules for variable look-up are complicated.
*
* - Dynamic variables like $@ and $* are special. They always pertain to
* a given variable. In this implementation of make, it is an error to
* try to affect them manually. They are stored in a local symtable directly
* inside the gnode.
*
* Global variables can be obtained:
* - from the command line
* - from the environment
* - from the Makefile proper.
* All of these are stored in a hash global_variables.
*
* Variables set on the command line override Makefile contents, are
* passed to submakes (see Var_AddCmdLine), and are also exported to the
* environment.
*
* Without -e (!checkEnvFirst), make will see variables set in the
* Makefile, and default to the environment otherwise.
*
* With -e (checkEnvFirst), make will see the environment first, and that
* will override anything that's set in the Makefile (but not set on
* the command line).
*
* The SHELL variable is very special: it is never obtained from the
* environment, and never passed to the environment.
*/
/* definitions pertaining to dynamic variables */
/* full names of dynamic variables */
static char *varnames[] = {
TARGET,
PREFIX,
ARCHIVE,
MEMBER,
OODATE,
ALLSRC,
IMPSRC,
FTARGET,
DTARGET,
FPREFIX,
DPREFIX,
FARCHIVE,
DARCHIVE,
FMEMBER,
DMEMBER
};
static bool xtlist[] = {
false, /* GLOBAL_INDEX */
true, /* $@ */
false, /* $* */
false, /* $! */
true, /* $% */
false, /* $? */
false, /* $> */
true, /* $< */
true, /* ${@F} */
true, /* ${@D} */
false, /* ${*F} */
false, /* ${*D} */
false, /* ${!F} */
false, /* ${!D} */
true, /* ${%F} */
true, /* ${%D} */
};
/* so that we can access tlist[-1] */
static bool *tlist = xtlist+1;
/* hashed names of dynamic variables */
#include "varhashconsts.h"
/* extended indices for System V stuff */
#define FTARGET_INDEX 7
#define DTARGET_INDEX 8
#define FPREFIX_INDEX 9
#define DPREFIX_INDEX 10
#define FARCHIVE_INDEX 11
#define DARCHIVE_INDEX 12
#define FMEMBER_INDEX 13
#define DMEMBER_INDEX 14
#define GLOBAL_INDEX -1
#define EXTENDED2SIMPLE(i) (((i)-LOCAL_SIZE)/2)
#define IS_EXTENDED_F(i) ((i)%2 == 1)
static struct ohash global_variables;
typedef struct Var_ {
BUFFER val; /* the variable value */
unsigned int flags; /* miscellaneous status flags */
#define VAR_IN_USE 1 /* Variable's value currently being used. */
/* (Used to avoid recursion) */
#define VAR_DUMMY 2 /* Variable is currently just a name */
/* In particular: BUFFER is invalid */
#define VAR_FROM_CMD 4 /* Special source: command line */
#define VAR_FROM_ENV 8 /* Special source: environment */
#define VAR_SEEN_ENV 16 /* No need to go look up environment again */
#define VAR_SHELL 32 /* Magic behavior */
#define POISONS (POISON_NORMAL | POISON_EMPTY | POISON_NOT_DEFINED)
/* Defined in var.h */
char name[1]; /* the variable's name */
} Var;
static struct ohash_info var_info = {
offsetof(Var, name),
NULL,
hash_calloc, hash_free, element_alloc
};
static int classify_var(const char *, const char **, uint32_t *);
static Var *find_global_var(const char *, const char *, uint32_t);
static Var *find_global_var_without_env(const char *, const char *, uint32_t);
static void fill_from_env(Var *);
static Var *create_var(const char *, const char *);
static void var_set_initial_value(Var *, const char *);
static void var_set_value(Var *, const char *);
#define var_get_value(v) Buf_Retrieve(&((v)->val))
static void var_append_value(Var *, const char *);
static void poison_check(Var *);
static void var_set_append(const char *, const char *, const char *, int, bool);
static void set_magic_shell_variable(void);
static void delete_var(Var *);
static void print_var(Var *);
static const char *find_rparen(const char *);
static const char *find_ket(const char *);
typedef const char * (*find_t)(const char *);
static find_t find_pos(int);
static void push_used(Var *);
static void pop_used(Var *);
static char *get_expanded_value(const char *, const char *, int, uint32_t,
SymTable *, bool, bool *);
static bool parse_base_variable_name(const char **, struct Name *, SymTable *);
/* Variable lookup function: return idx for dynamic variable, or
* GLOBAL_INDEX if name is not dynamic. Set up *pk for further use.
*/
static int
classify_var(const char *name, const char **enamePtr, uint32_t *pk)
{
size_t len;
*pk = ohash_interval(name, enamePtr);
len = *enamePtr - name;
/* substitute short version for long local name */
switch (*pk % MAGICSLOTS1) { /* MAGICSLOTS should be the */
case K_LONGALLSRC % MAGICSLOTS1:/* smallest constant yielding */
/* distinct case values */
if (*pk == K_LONGALLSRC && len == strlen(LONGALLSRC) &&
strncmp(name, LONGALLSRC, len) == 0)
return ALLSRC_INDEX;
break;
case K_LONGARCHIVE % MAGICSLOTS1:
if (*pk == K_LONGARCHIVE && len == strlen(LONGARCHIVE) &&
strncmp(name, LONGARCHIVE, len) == 0)
return ARCHIVE_INDEX;
break;
case K_LONGIMPSRC % MAGICSLOTS1:
if (*pk == K_LONGIMPSRC && len == strlen(LONGIMPSRC) &&
strncmp(name, LONGIMPSRC, len) == 0)
return IMPSRC_INDEX;
break;
case K_LONGMEMBER % MAGICSLOTS1:
if (*pk == K_LONGMEMBER && len == strlen(LONGMEMBER) &&
strncmp(name, LONGMEMBER, len) == 0)
return MEMBER_INDEX;
break;
case K_LONGOODATE % MAGICSLOTS1:
if (*pk == K_LONGOODATE && len == strlen(LONGOODATE) &&
strncmp(name, LONGOODATE, len) == 0)
return OODATE_INDEX;
break;
case K_LONGPREFIX % MAGICSLOTS1:
if (*pk == K_LONGPREFIX && len == strlen(LONGPREFIX) &&
strncmp(name, LONGPREFIX, len) == 0)
return PREFIX_INDEX;
break;
case K_LONGTARGET % MAGICSLOTS1:
if (*pk == K_LONGTARGET && len == strlen(LONGTARGET) &&
strncmp(name, LONGTARGET, len) == 0)
return TARGET_INDEX;
break;
case K_TARGET % MAGICSLOTS1:
if (name[0] == TARGET[0] && len == 1)
return TARGET_INDEX;
break;
case K_OODATE % MAGICSLOTS1:
if (name[0] == OODATE[0] && len == 1)
return OODATE_INDEX;
break;
case K_ALLSRC % MAGICSLOTS1:
if (name[0] == ALLSRC[0] && len == 1)
return ALLSRC_INDEX;
break;
case K_IMPSRC % MAGICSLOTS1:
if (name[0] == IMPSRC[0] && len == 1)
return IMPSRC_INDEX;
break;
case K_PREFIX % MAGICSLOTS1:
if (name[0] == PREFIX[0] && len == 1)
return PREFIX_INDEX;
break;
case K_ARCHIVE % MAGICSLOTS1:
if (name[0] == ARCHIVE[0] && len == 1)
return ARCHIVE_INDEX;
break;
case K_MEMBER % MAGICSLOTS1:
if (name[0] == MEMBER[0] && len == 1)
return MEMBER_INDEX;
break;
case K_FTARGET % MAGICSLOTS1:
if (name[0] == FTARGET[0] && name[1] == FTARGET[1] && len == 2)
return FTARGET_INDEX;
break;
case K_DTARGET % MAGICSLOTS1:
if (name[0] == DTARGET[0] && name[1] == DTARGET[1] && len == 2)
return DTARGET_INDEX;
break;
case K_FPREFIX % MAGICSLOTS1:
if (name[0] == FPREFIX[0] && name[1] == FPREFIX[1] && len == 2)
return FPREFIX_INDEX;
break;
case K_DPREFIX % MAGICSLOTS1:
if (name[0] == DPREFIX[0] && name[1] == DPREFIX[1] && len == 2)
return DPREFIX_INDEX;
break;
case K_FARCHIVE % MAGICSLOTS1:
if (name[0] == FARCHIVE[0] && name[1] == FARCHIVE[1] &&
len == 2)
return FARCHIVE_INDEX;
break;
case K_DARCHIVE % MAGICSLOTS1:
if (name[0] == DARCHIVE[0] && name[1] == DARCHIVE[1] &&
len == 2)
return DARCHIVE_INDEX;
break;
case K_FMEMBER % MAGICSLOTS1:
if (name[0] == FMEMBER[0] && name[1] == FMEMBER[1] && len == 2)
return FMEMBER_INDEX;
break;
case K_DMEMBER % MAGICSLOTS1:
if (name[0] == DMEMBER[0] && name[1] == DMEMBER[1] && len == 2)
return DMEMBER_INDEX;
break;
default:
break;
}
return GLOBAL_INDEX;
}
/***
*** Internal handling of variables.
***/
/* Create a new variable, does not initialize anything except the name.
* in particular, buffer is invalid, and flag value is invalid. Accordingly,
* must either:
* - set flags to VAR_DUMMY
* - set flags to !VAR_DUMMY, and initialize buffer, for instance with
* var_set_initial_value().
*/
static Var *
create_var(const char *name, const char *ename)
{
return ohash_create_entry(&var_info, name, &ename);
}
/* Initial version of var_set_value(), to be called after create_var().
*/
static void
var_set_initial_value(Var *v, const char *val)
{
size_t len;
len = strlen(val);
Buf_Init(&(v->val), len+1);
Buf_AddChars(&(v->val), len, val);
}
/* Normal version of var_set_value(), to be called after variable is fully
* initialized.
*/
static void
var_set_value(Var *v, const char *val)
{
if ((v->flags & VAR_DUMMY) == 0) {
Buf_Reset(&(v->val));
Buf_AddString(&(v->val), val);
} else {
var_set_initial_value(v, val);
v->flags &= ~VAR_DUMMY;
}
}
/* Add to a variable, insert a separating space if the variable was already
* defined.
*/
static void
var_append_value(Var *v, const char *val)
{
if ((v->flags & VAR_DUMMY) == 0) {
Buf_AddSpace(&(v->val));
Buf_AddString(&(v->val), val);
} else {
var_set_initial_value(v, val);
v->flags &= ~VAR_DUMMY;
}
}
/* Delete a variable and all the space associated with it.
*/
static void
delete_var(Var *v)
{
if ((v->flags & VAR_DUMMY) == 0)
Buf_Destroy(&(v->val));
free(v);
}
/***
*** Dynamic variable handling.
***/
/* create empty symtable.
* XXX: to save space, dynamic variables may be NULL pointers.
*/
void
SymTable_Init(SymTable *ctxt)
{
static SymTable sym_template;
memcpy(ctxt, &sym_template, sizeof(*ctxt));
}
/***
*** Global variable handling.
***/
/* Create a new global var if necessary, and set it up correctly.
* Do not take environment into account.
*/
static Var *
find_global_var_without_env(const char *name, const char *ename, uint32_t k)
{
unsigned int slot;
Var *v;
slot = ohash_lookup_interval(&global_variables, name, ename, k);
v = ohash_find(&global_variables, slot);
if (v == NULL) {
v = create_var(name, ename);
v->flags = VAR_DUMMY;
ohash_insert(&global_variables, slot, v);
}
return v;
}
/* Helper for find_global_var(): grab environment value if needed.
*/
static void
fill_from_env(Var *v)
{
char *env;
env = getenv(v->name);
if (env == NULL)
v->flags |= VAR_SEEN_ENV;
else {
var_set_value(v, env);
v->flags |= VAR_FROM_ENV | VAR_SEEN_ENV;
}
#ifdef STATS_VAR_LOOKUP
STAT_VAR_FROM_ENV++;
#endif
}
/* Find global var, and obtain its value from the environment if needed.
*/
static Var *
find_global_var(const char *name, const char *ename, uint32_t k)
{
Var *v;
v = find_global_var_without_env(name, ename, k);
if ((v->flags & VAR_SEEN_ENV) == 0)
if ((checkEnvFirst && (v->flags & VAR_FROM_CMD) == 0) ||
(v->flags & VAR_DUMMY) != 0)
fill_from_env(v);
return v;
}
/* mark variable as poisoned, in a given setup.
*/
void
Var_MarkPoisoned(const char *name, const char *ename, unsigned int type)
{
Var *v;
uint32_t k;
int idx;
idx = classify_var(name, &ename, &k);
if (idx != GLOBAL_INDEX) {
Parse_Error(PARSE_FATAL,
"Trying to poison dynamic variable $%s",
varnames[idx]);
return;
}
v = find_global_var(name, ename, k);
v->flags |= type;
/* POISON_NORMAL is not lazy: if the variable already exists in
* the Makefile, then it's a mistake.
*/
if (v->flags & POISON_NORMAL) {
if (v->flags & VAR_DUMMY)
return;
if (v->flags & VAR_FROM_ENV)
return;
Parse_Error(PARSE_FATAL,
"Poisoned variable %s is already set\n", v->name);
}
}
/* Check if there's any reason not to use the variable in this context.
*/
static void
poison_check(Var *v)
{
if (v->flags & POISON_NORMAL) {
Parse_Error(PARSE_FATAL,
"Poisoned variable %s has been referenced\n", v->name);
return;
}
if (v->flags & VAR_DUMMY) {
Parse_Error(PARSE_FATAL,
"Poisoned variable %s is not defined\n", v->name);
return;
}
if (v->flags & POISON_EMPTY)
if (strcmp(var_get_value(v), "") == 0)
Parse_Error(PARSE_FATAL,
"Poisoned variable %s is empty\n", v->name);
}
/* Delete global variable.
*/
void
Var_Deletei(const char *name, const char *ename)
{
Var *v;
uint32_t k;
unsigned int slot;
int idx;
idx = classify_var(name, &ename, &k);
if (idx != GLOBAL_INDEX) {
Parse_Error(PARSE_FATAL,
"Trying to delete dynamic variable $%s", varnames[idx]);
return;
}
slot = ohash_lookup_interval(&global_variables, name, ename, k);
v = ohash_find(&global_variables, slot);
if (v == NULL)
return;
if (checkEnvFirst && (v->flags & VAR_FROM_ENV))
return;
if (v->flags & VAR_FROM_CMD)
return;
ohash_remove(&global_variables, slot);
delete_var(v);
}
/* Set or add a global variable, in VAR_CMD or VAR_GLOBAL context.
*/
static void
var_set_append(const char *name, const char *ename, const char *val, int ctxt,
bool append)
{
Var *v;
uint32_t k;
int idx;
idx = classify_var(name, &ename, &k);
if (idx != GLOBAL_INDEX) {
Parse_Error(PARSE_FATAL, "Trying to %s dynamic variable $%s",
append ? "append to" : "set", varnames[idx]);
return;
}
v = find_global_var(name, ename, k);
if (v->flags & POISON_NORMAL)
Parse_Error(PARSE_FATAL, "Trying to %s poisoned variable %s\n",
append ? "append to" : "set", v->name);
/* so can we write to it ? */
if (ctxt == VAR_CMD) { /* always for command line */
(append ? var_append_value : var_set_value)(v, val);
v->flags |= VAR_FROM_CMD;
if ((v->flags & VAR_SHELL) == 0) {
/* Any variables given on the command line are
* automatically exported to the environment,
* except for SHELL (as per POSIX standard).
*/
esetenv(v->name, val);
}
if (DEBUG(VAR))
printf("command:%s = %s\n", v->name, var_get_value(v));
} else if ((v->flags & VAR_FROM_CMD) == 0 &&
(!checkEnvFirst || (v->flags & VAR_FROM_ENV) == 0)) {
(append ? var_append_value : var_set_value)(v, val);
if (DEBUG(VAR))
printf("global:%s = %s\n", v->name, var_get_value(v));
} else if (DEBUG(VAR))
printf("overridden:%s = %s\n", v->name, var_get_value(v));
}
void
Var_Seti_with_ctxt(const char *name, const char *ename, const char *val,
int ctxt)
{
var_set_append(name, ename, val, ctxt, false);
}
void
Var_Appendi_with_ctxt(const char *name, const char *ename, const char *val,
int ctxt)
{
var_set_append(name, ename, val, ctxt, true);
}
/* XXX different semantics for Var_Valuei() and Var_Definedi():
* references to poisoned value variables will error out in Var_Valuei(),
* but not in Var_Definedi(), so the following construct works:
* .poison BINDIR
* BINDIR ?= /usr/bin
*/
char *
Var_Valuei(const char *name, const char *ename)
{
Var *v;
uint32_t k;
int idx;
idx = classify_var(name, &ename, &k);
if (idx != GLOBAL_INDEX) {
Parse_Error(PARSE_FATAL,
"Trying to get value of dynamic variable $%s",
varnames[idx]);
return NULL;
}
v = find_global_var(name, ename, k);
if (v->flags & POISONS)
poison_check(v);
if ((v->flags & VAR_DUMMY) == 0)
return var_get_value(v);
else
return NULL;
}
bool
Var_Definedi(const char *name, const char *ename)
{
Var *v;
uint32_t k;
int idx;
idx = classify_var(name, &ename, &k);
/* We don't bother writing an error message for dynamic variables,
* these will be caught when getting set later, usually.
*/
if (idx == GLOBAL_INDEX) {
v = find_global_var(name, ename, k);
if (v->flags & POISON_NORMAL)
poison_check(v);
if ((v->flags & VAR_DUMMY) == 0)
return true;
}
return false;
}
/***
*** Substitution functions, handling both global and dynamic variables.
***/
/* All the scanning functions needed to account for all the forms of
* variable names that exist:
* $A, ${AB}, $(ABC), ${A:mod}, $(A:mod)
*/
static const char *
find_rparen(const char *p)
{
while (*p != '$' && *p != '\0' && *p != ')' && *p != ':')
p++;
return p;
}
static const char *
find_ket(const char *p)
{
while (*p != '$' && *p != '\0' && *p != '}' && *p != ':')
p++;
return p;
}
/* Figure out what kind of name we're looking for from a start character.
*/
static find_t
find_pos(int c)
{
switch(c) {
case '(':
return find_rparen;
case '{':
return find_ket;
default:
Parse_Error(PARSE_FATAL,
"Wrong character in variable spec %c (can't happen)");
return find_rparen;
}
}
static bool
parse_base_variable_name(const char **pstr, struct Name *name, SymTable *ctxt)
{
const char *str = *pstr;
const char *tstr;
bool has_modifier = false;
switch(str[1]) {
case '(':
case '{':
/* Find eventual modifiers in the variable */
tstr = VarName_Get(str+2, name, ctxt, false, find_pos(str[1]));
if (*tstr == '\0')
Parse_Error(PARSE_FATAL, "Unterminated variable spec in %s", *pstr);
else if (*tstr == ':')
has_modifier = true;
else
tstr++;
break;
default:
name->s = str+1;
name->e = str+2;
name->tofree = false;
tstr = str + 2;
break;
}
*pstr = tstr;
return has_modifier;
}
bool
Var_ParseSkip(const char **pstr, SymTable *ctxt)
{
const char *str = *pstr;
struct Name name;
bool result;
bool has_modifier;
const char *tstr = str;
if (str[1] == 0) {
*pstr = str+1;
return false;
}
has_modifier = parse_base_variable_name(&tstr, &name, ctxt);
VarName_Free(&name);
result = true;
if (has_modifier) {
bool freePtr = false;
char *s = VarModifiers_Apply(NULL, NULL, ctxt, true, &freePtr,
&tstr, str[1]);
if (s == var_Error)
result = false;
if (freePtr)
free(s);
}
*pstr = tstr;
return result;
}
/* As of now, Var_ParseBuffer is just a wrapper around Var_Parse. For
* speed, it may be better to revisit the implementation to do things
* directly. */
bool
Var_ParseBuffer(Buffer buf, const char *str, SymTable *ctxt, bool err,
size_t *lengthPtr)
{
char *result;
bool freeIt;
result = Var_Parse(str, ctxt, err, lengthPtr, &freeIt);
if (result == var_Error)
return false;
Buf_AddString(buf, result);
if (freeIt)
free(result);
return true;
}
/* Helper function for Var_Parse: still recursive, but we tag what variables
* we expand for better error messages.
*/
#define MAX_DEPTH 350
static Var *call_trace[MAX_DEPTH];
static int current_depth = 0;
static void
push_used(Var *v)
{
if (v->flags & VAR_IN_USE) {
int i;
fprintf(stderr, "Problem with variable expansion chain: ");
for (i = 0;
i < (current_depth > MAX_DEPTH ? MAX_DEPTH : current_depth);
i++)
fprintf(stderr, "%s -> ", call_trace[i]->name);
fprintf(stderr, "%s\n", v->name);
Fatal("\tVariable %s is recursive.", v->name);
/*NOTREACHED*/
}
v->flags |= VAR_IN_USE;
if (current_depth < MAX_DEPTH)
call_trace[current_depth] = v;
current_depth++;
}
static void
pop_used(Var *v)
{
v->flags &= ~VAR_IN_USE;
current_depth--;
}
static char *
get_expanded_value(const char *name, const char *ename, int idx, uint32_t k,
SymTable *ctxt, bool err, bool *freePtr)
{
char *val;
/* Before doing any modification, we have to make sure the
* value has been fully expanded. If it looks like recursion
* might be necessary (there's a dollar sign somewhere in
* the variable's value) we just call Var_Subst to do any
* other substitutions that are necessary. Note that the
* value returned by Var_Subst will have been dynamically
* allocated, so it will need freeing when we return.
*/
if (idx == GLOBAL_INDEX) {
Var *v = find_global_var(name, ename, k);
if (v == NULL)
return NULL;
if ((v->flags & POISONS) != 0)
poison_check(v);
if ((v->flags & VAR_DUMMY) != 0)
return NULL;
val = var_get_value(v);
if (strchr(val, '$') != NULL) {
push_used(v);
val = Var_Subst(val, ctxt, err);
pop_used(v);
*freePtr = true;
}
} else {
if (ctxt != NULL) {
if (idx < LOCAL_SIZE)
val = ctxt->locals[idx];
else
val = ctxt->locals[EXTENDED2SIMPLE(idx)];
} else
val = NULL;
if (val == NULL)
return NULL;
if (idx >= LOCAL_SIZE) {
if (IS_EXTENDED_F(idx))
val = Var_GetTail(val);
else
val = Var_GetHead(val);
*freePtr = true;
}
}
return val;
}
#define ERRMSG1 "Using $< in a non-suffix rule context is a GNUmake idiom "
#define ERRMSG2 "Using undefined dynamic variable $%s "
static void
bad_dynamic_variable(int idx)
{
Location origin;
Parse_FillLocation(&origin);
if (idx >= LOCAL_SIZE)
idx = EXTENDED2SIMPLE(idx);
switch(idx) {
case IMPSRC_INDEX:
if (origin.fname)
Fatal(ERRMSG1 "(%s:%lu)",
origin.fname, origin.lineno);
else if (current_node)
Fatal(ERRMSG1 "(prereq of %s)", current_node->name);
else
Fatal(ERRMSG1 "(?)");
break;
default:
if (origin.fname)
Error(ERRMSG2 "(%s:%lu)", varnames[idx],
origin.fname, origin.lineno);
else if (current_node)
Error(ERRMSG2 "(prereq of %s)", varnames[idx],
current_node->name);
else
Error(ERRMSG2 "(?)", varnames[idx]);
break;
}
}
char *
Var_Parse(const char *str, /* The string to parse */
SymTable *ctxt, /* The context for the variable */
bool err, /* true if undefined variables are an error */
size_t *lengthPtr, /* OUT: The length of the specification */
bool *freePtr) /* OUT: true if caller should free result */
{
const char *tstr;
struct Name name;
char *val;
uint32_t k;
int idx;
bool has_modifier;
*freePtr = false;
tstr = str;
if (str[1] == 0) {
*lengthPtr = 1;
*freePtr = false;
return err ? var_Error : varNoError;
}
has_modifier = parse_base_variable_name(&tstr, &name, ctxt);
idx = classify_var(name.s, &name.e, &k);
val = get_expanded_value(name.s, name.e, idx, k, ctxt, err, freePtr);
if (has_modifier) {
val = VarModifiers_Apply(val, &name, ctxt, err, freePtr,
&tstr, str[1]);
}
if (val == NULL) {
val = err ? var_Error : varNoError;
/* If it comes from a dynamic source, and it doesn't have
* a context, copy the spec instead.
* Specifically, this make allows constructs like:
* target.o: $*.c
* Absence of a context means "parsing". But these can't
* be expanded during parsing, to be consistent with the
* way .SUFFIXES work.
* .SUFFIXES may be added/reset/removed during parsing,
* but in the end, the final list is what's considered for
* handling targets. So those dynamic variables must be
* handled lazily too.
*/
if (idx != GLOBAL_INDEX) {
if (ctxt == NULL) {
*freePtr = true;
val = Str_dupi(str, tstr);
} else {
bad_dynamic_variable(idx);
}
}
}
VarName_Free(&name);
*lengthPtr = tstr - str;
return val;
}
char *
Var_Subst(const char *str, /* the string in which to substitute */
SymTable *ctxt, /* the context wherein to find variables */
bool undefErr) /* true if undefineds are an error */
{
BUFFER buf; /* Buffer for forming things */
static bool errorReported;
Buf_Init(&buf, MAKE_BSIZE);
errorReported = false;
for (;;) {
char *val; /* Value to substitute for a variable */
size_t length; /* Length of the variable invocation */
bool doFree; /* Set true if val should be freed */
const char *cp;
/* copy uninteresting stuff */
for (cp = str; *str != '\0' && *str != '$'; str++)
;
Buf_Addi(&buf, cp, str);
if (*str == '\0')
break;
if (str[1] == '$') {
/* A $ may be escaped with another $. */
Buf_AddChar(&buf, '$');
str += 2;
continue;
}
val = Var_Parse(str, ctxt, undefErr, &length, &doFree);
/* When we come down here, val should either point to the
* value of this variable, suitably modified, or be NULL.
* Length should be the total length of the potential
* variable invocation (from $ to end character...) */
if (val == var_Error || val == varNoError) {
/* If errors are not an issue, skip over the variable
* and continue with the substitution. Otherwise, store
* the dollar sign and advance str so we continue with
* the string... */
if (errorIsOkay)
str += length;
else if (undefErr) {
/* If variable is undefined, complain and
* skip the variable name. The complaint
* will stop us from doing anything when
* the file is parsed. */
if (!errorReported)
Parse_Error(PARSE_FATAL,
"Undefined variable \"%.*s\"",
length, str);
str += length;
errorReported = true;
} else {
Buf_AddChar(&buf, *str);
str++;
}
} else {
/* We've now got a variable structure to store in.
* But first, advance the string pointer. */
str += length;
/* Copy all the characters from the variable value
* straight into the new string. */
Buf_AddString(&buf, val);
if (doFree)
free(val);
}
}
return Buf_Retrieve(&buf);
}
/* Very quick version of the variable scanner that just looks for target
* variables, and never ever errors out
*/
bool
Var_Check_for_target(const char *str)
{
bool seen_target = false;
for (;;) {
const char *tstr;
uint32_t k;
int idx;
bool has_modifier;
struct Name name;
/* skip over uninteresting stuff */
for (; *str != '\0' && *str != '$'; str++)
;
if (*str == '\0')
break;
if (str[1] == '$') {
/* A $ may be escaped with another $. */
str += 2;
continue;
}
tstr = str;
has_modifier = parse_base_variable_name(&tstr, &name, NULL);
idx = classify_var(name.s, &name.e, &k);
if (has_modifier) {
bool doFree = false;
char *val = VarModifiers_Apply(NULL, NULL, NULL, false,
&doFree, &tstr, str[1]);
if (doFree)
free(val);
}
if (tlist[idx])
seen_target = true;
VarName_Free(&name);
str = tstr;
}
return seen_target;
}
static BUFFER subst_buffer;
/* we would like to subst on intervals, but it's complicated, so we cheat
* by storing the interval in a static buffer.
*/
char *
Var_Substi(const char *str, const char *estr, SymTable *ctxt, bool undefErr)
{
/* delimited string: no need to copy */
if (estr == NULL || *estr == '\0')
return Var_Subst(str, ctxt, undefErr);
Buf_Reset(&subst_buffer);
Buf_Addi(&subst_buffer, str, estr);
return Var_Subst(Buf_Retrieve(&subst_buffer), ctxt, undefErr);
}
/***
*** Supplementary support for .for loops.
***/
struct LoopVar
{
Var old; /* keep old variable value (before the loop) */
Var *me; /* the variable we're dealing with */
};
struct LoopVar *
Var_NewLoopVar(const char *name, const char *ename)
{
struct LoopVar *l;
uint32_t k;
l = emalloc(sizeof(struct LoopVar));
/* we obtain a new variable quickly, make a snapshot of its old
* value, and make sure the environment cannot touch us.
*/
/* XXX: should we avoid dynamic variables ? */
k = ohash_interval(name, &ename);
l->me = find_global_var_without_env(name, ename, k);
l->old = *(l->me);
l->me->flags = VAR_SEEN_ENV | VAR_DUMMY;
return l;
}
char *
Var_LoopVarName(struct LoopVar *v)
{
return v->me->name;
}
void
Var_DeleteLoopVar(struct LoopVar *l)
{
if ((l->me->flags & VAR_DUMMY) == 0)
Buf_Destroy(&(l->me->val));
*(l->me) = l->old;
free(l);
}
void
Var_SubstVar(Buffer buf, /* To store result */
const char *str, /* The string in which to substitute */
struct LoopVar *l, /* Handle */
const char *val) /* Its value */
{
const char *var = l->me->name;
var_set_value(l->me, val);
for (;;) {
const char *start;
/* Copy uninteresting stuff */
for (start = str; *str != '\0' && *str != '$'; str++)
;
Buf_Addi(buf, start, str);
start = str;
if (*str++ == '\0')
break;
str++;
/* and escaped dollars */
if (start[1] == '$') {
Buf_Addi(buf, start, start+2);
continue;
}
/* Simple variable, if it's not us, copy. */
if (start[1] != '(' && start[1] != '{') {
if (start[1] != *var || var[1] != '\0') {
Buf_AddChars(buf, 2, start);
continue;
}
} else {
const char *p;
char paren = start[1];
/* Find the end of the variable specification. */
p = find_pos(paren)(str);
/* A variable inside the variable. We don't know how to
* expand the external variable at this point, so we
* try again with the nested variable. */
if (*p == '$') {
Buf_Addi(buf, start, p);
str = p;
continue;
}
if (strncmp(var, str, p - str) != 0 ||
var[p - str] != '\0') {
/* Not the variable we want to expand. */
Buf_Addi(buf, start, p);
str = p;
continue;
}
if (*p == ':') {
bool doFree; /* should val be freed ? */
char *newval;
struct Name name;
doFree = false;
name.s = var;
name.e = var + (p-str);
/* val won't be freed since !doFree, but
* VarModifiers_Apply doesn't know that,
* hence the cast. */
newval = VarModifiers_Apply((char *)val,
&name, NULL, false, &doFree, &p, paren);
Buf_AddString(buf, newval);
if (doFree)
free(newval);
str = p;
continue;
} else
str = p+1;
}
Buf_AddString(buf, val);
}
}
/***
*** Odds and ends
***/
static void
set_magic_shell_variable()
{
const char *name = "SHELL";
const char *ename = NULL;
uint32_t k;
Var *v;
k = ohash_interval(name, &ename);
v = find_global_var_without_env(name, ename, k);
var_set_value(v, _PATH_BSHELL);
/* XXX the environment shall never affect it */
v->flags = VAR_SHELL | VAR_SEEN_ENV;
}
/*
* Var_Init
* Initialize the module
*/
void
Var_Init(void)
{
ohash_init(&global_variables, 10, &var_info);
set_magic_shell_variable();
errorIsOkay = true;
Var_setCheckEnvFirst(false);
VarModifiers_Init();
Buf_Init(&subst_buffer, MAKE_BSIZE);
}
static const char *interpret(int);
static const char *
interpret(int f)
{
if (f & VAR_DUMMY)
return "(D)";
return "";
}
static void
print_var(Var *v)
{
printf("%-16s%s = %s\n", v->name, interpret(v->flags),
(v->flags & VAR_DUMMY) == 0 ? var_get_value(v) : "(none)");
}
void
Var_Dump(void)
{
Var **t;
unsigned int i;
const char *banner;
bool first = true;
t = sort_ohash_by_name(&global_variables);
/* somewhat dirty, but does the trick */
#define LOOP(mask, value, do_stuff) \
for (i = 0; t[i] != NULL; i++) \
if ((t[i]->flags & (mask)) == (value)) { \
if (banner) { \
if (first) \
first = false; \
else \
putchar('\n'); \
fputs(banner, stdout); \
banner = NULL; \
} \
do_stuff; \
}
banner = "#variables from command line:\n";
LOOP(VAR_FROM_CMD | VAR_DUMMY, VAR_FROM_CMD, print_var(t[i]));
banner = "#global variables:\n";
LOOP(VAR_FROM_ENV| VAR_FROM_CMD | VAR_DUMMY, 0, print_var(t[i]));
banner = "#variables from env:\n";
LOOP(VAR_FROM_ENV|VAR_DUMMY, VAR_FROM_ENV, print_var(t[i]));
banner = "#variable name seen, but not defined:";
LOOP(VAR_DUMMY|POISONS, VAR_DUMMY, printf(" %s", t[i]->name));
#undef LOOP
printf("\n\n");
for (i = 0; t[i] != NULL; i++)
switch(t[i]->flags & POISONS) {
case POISON_NORMAL:
printf(".poison %s\n", t[i]->name);
break;
case POISON_EMPTY:
printf(".poison empty(%s)\n", t[i]->name);
break;
case POISON_NOT_DEFINED:
printf(".poison !defined(%s)\n", t[i]->name);
break;
default:
break;
}
free(t);
printf("\n");
}
static const char *quotable = " \t\n\\'\"";
/* POSIX says that variable assignments passed on the command line should be
* propagated to sub makes through MAKEFLAGS.
*/
void
Var_AddCmdline(const char *name)
{
Var *v;
unsigned int i;
BUFFER buf;
char *s;
Buf_Init(&buf, MAKE_BSIZE);
for (v = ohash_first(&global_variables, &i); v != NULL;
v = ohash_next(&global_variables, &i)) {
/* This is not as expensive as it looks: this function is
* called before parsing Makefiles, so there are just a
* few non cmdling variables in there.
*/
if (!(v->flags & VAR_FROM_CMD)) {
continue;
}
/* We assume variable names don't need quoting */
Buf_AddString(&buf, v->name);
Buf_AddChar(&buf, '=');
for (s = var_get_value(v); *s != '\0'; s++) {
if (strchr(quotable, *s))
Buf_AddChar(&buf, '\\');
Buf_AddChar(&buf, *s);
}
Buf_AddSpace(&buf);
}
Var_Append(name, Buf_Retrieve(&buf));
Buf_Destroy(&buf);
}
|