forked from freebsd/freebsd-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
3140 lines (2874 loc) · 80.1 KB
/
parse.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
/* $NetBSD: parse.c,v 1.188 2013/03/22 16:07:59 sjg Exp $ */
/*
* Copyright (c) 1988, 1989, 1990, 1993
* The Regents of the University of California. 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.
*/
/*
* 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. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. 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.
*/
#ifndef MAKE_NATIVE
static char rcsid[] = "$NetBSD: parse.c,v 1.188 2013/03/22 16:07:59 sjg Exp $";
#else
#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#)parse.c 8.3 (Berkeley) 3/19/94";
#else
__RCSID("$NetBSD: parse.c,v 1.188 2013/03/22 16:07:59 sjg Exp $");
#endif
#endif /* not lint */
#endif
/*-
* parse.c --
* Functions to parse a makefile.
*
* One function, Parse_Init, must be called before any functions
* in this module are used. After that, the function Parse_File is the
* main entry point and controls most of the other functions in this
* module.
*
* Most important structures are kept in Lsts. Directories for
* the .include "..." function are kept in the 'parseIncPath' Lst, while
* those for the .include <...> are kept in the 'sysIncPath' Lst. The
* targets currently being defined are kept in the 'targets' Lst.
*
* The variables 'fname' and 'lineno' are used to track the name
* of the current file and the line number in that file so that error
* messages can be more meaningful.
*
* Interface:
* Parse_Init Initialization function which must be
* called before anything else in this module
* is used.
*
* Parse_End Cleanup the module
*
* Parse_File Function used to parse a makefile. It must
* be given the name of the file, which should
* already have been opened, and a function
* to call to read a character from the file.
*
* Parse_IsVar Returns TRUE if the given line is a
* variable assignment. Used by MainParseArgs
* to determine if an argument is a target
* or a variable assignment. Used internally
* for pretty much the same thing...
*
* Parse_Error Function called when an error occurs in
* parsing. Used by the variable and
* conditional modules.
* Parse_MainName Returns a Lst of the main target to create.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include "make.h"
#include "hash.h"
#include "dir.h"
#include "job.h"
#include "buf.h"
#include "pathnames.h"
#ifdef HAVE_MMAP
#include <sys/mman.h>
#ifndef MAP_COPY
#define MAP_COPY MAP_PRIVATE
#endif
#ifndef MAP_FILE
#define MAP_FILE 0
#endif
#endif
////////////////////////////////////////////////////////////
// types and constants
/*
* Structure for a file being read ("included file")
*/
typedef struct IFile {
const char *fname; /* name of file */
int lineno; /* current line number in file */
int first_lineno; /* line number of start of text */
int cond_depth; /* 'if' nesting when file opened */
char *P_str; /* point to base of string buffer */
char *P_ptr; /* point to next char of string buffer */
char *P_end; /* point to the end of string buffer */
char *(*nextbuf)(void *, size_t *); /* Function to get more data */
void *nextbuf_arg; /* Opaque arg for nextbuf() */
struct loadedfile *lf; /* loadedfile object, if any */
} IFile;
/*
* These values are returned by ParseEOF to tell Parse_File whether to
* CONTINUE parsing, i.e. it had only reached the end of an include file,
* or if it's DONE.
*/
#define CONTINUE 1
#define DONE 0
/*
* Tokens for target attributes
*/
typedef enum {
Begin, /* .BEGIN */
Default, /* .DEFAULT */
End, /* .END */
dotError, /* .ERROR */
Ignore, /* .IGNORE */
Includes, /* .INCLUDES */
Interrupt, /* .INTERRUPT */
Libs, /* .LIBS */
Meta, /* .META */
MFlags, /* .MFLAGS or .MAKEFLAGS */
Main, /* .MAIN and we don't have anything user-specified to
* make */
NoExport, /* .NOEXPORT */
NoMeta, /* .NOMETA */
NoMetaCmp, /* .NOMETA_CMP */
NoPath, /* .NOPATH */
Not, /* Not special */
NotParallel, /* .NOTPARALLEL */
Null, /* .NULL */
ExObjdir, /* .OBJDIR */
Order, /* .ORDER */
Parallel, /* .PARALLEL */
ExPath, /* .PATH */
Phony, /* .PHONY */
#ifdef POSIX
Posix, /* .POSIX */
#endif
Precious, /* .PRECIOUS */
ExShell, /* .SHELL */
Silent, /* .SILENT */
SingleShell, /* .SINGLESHELL */
Stale, /* .STALE */
Suffixes, /* .SUFFIXES */
Wait, /* .WAIT */
Attribute /* Generic attribute */
} ParseSpecial;
/*
* Other tokens
*/
#define LPAREN '('
#define RPAREN ')'
////////////////////////////////////////////////////////////
// result data
/*
* The main target to create. This is the first target on the first
* dependency line in the first makefile.
*/
static GNode *mainNode;
////////////////////////////////////////////////////////////
// eval state
/* targets we're working on */
static Lst targets;
#ifdef CLEANUP
/* command lines for targets */
static Lst targCmds;
#endif
/*
* specType contains the SPECial TYPE of the current target. It is
* Not if the target is unspecial. If it *is* special, however, the children
* are linked as children of the parent but not vice versa. This variable is
* set in ParseDoDependency
*/
static ParseSpecial specType;
/*
* Predecessor node for handling .ORDER. Initialized to NULL when .ORDER
* seen, then set to each successive source on the line.
*/
static GNode *predecessor;
////////////////////////////////////////////////////////////
// parser state
/* true if currently in a dependency line or its commands */
static Boolean inLine;
/* number of fatal errors */
static int fatals = 0;
/*
* Variables for doing includes
*/
/* current file being read */
static IFile *curFile;
/* stack of IFiles generated by .includes */
static Lst includes;
/* include paths (lists of directories) */
Lst parseIncPath; /* dirs for "..." includes */
Lst sysIncPath; /* dirs for <...> includes */
Lst defIncPath; /* default for sysIncPath */
////////////////////////////////////////////////////////////
// parser tables
/*
* The parseKeywords table is searched using binary search when deciding
* if a target or source is special. The 'spec' field is the ParseSpecial
* type of the keyword ("Not" if the keyword isn't special as a target) while
* the 'op' field is the operator to apply to the list of targets if the
* keyword is used as a source ("0" if the keyword isn't special as a source)
*/
static const struct {
const char *name; /* Name of keyword */
ParseSpecial spec; /* Type when used as a target */
int op; /* Operator when used as a source */
} parseKeywords[] = {
{ ".BEGIN", Begin, 0 },
{ ".DEFAULT", Default, 0 },
{ ".END", End, 0 },
{ ".ERROR", dotError, 0 },
{ ".EXEC", Attribute, OP_EXEC },
{ ".IGNORE", Ignore, OP_IGNORE },
{ ".INCLUDES", Includes, 0 },
{ ".INTERRUPT", Interrupt, 0 },
{ ".INVISIBLE", Attribute, OP_INVISIBLE },
{ ".JOIN", Attribute, OP_JOIN },
{ ".LIBS", Libs, 0 },
{ ".MADE", Attribute, OP_MADE },
{ ".MAIN", Main, 0 },
{ ".MAKE", Attribute, OP_MAKE },
{ ".MAKEFLAGS", MFlags, 0 },
{ ".META", Meta, OP_META },
{ ".MFLAGS", MFlags, 0 },
{ ".NOMETA", NoMeta, OP_NOMETA },
{ ".NOMETA_CMP", NoMetaCmp, OP_NOMETA_CMP },
{ ".NOPATH", NoPath, OP_NOPATH },
{ ".NOTMAIN", Attribute, OP_NOTMAIN },
{ ".NOTPARALLEL", NotParallel, 0 },
{ ".NO_PARALLEL", NotParallel, 0 },
{ ".NULL", Null, 0 },
{ ".OBJDIR", ExObjdir, 0 },
{ ".OPTIONAL", Attribute, OP_OPTIONAL },
{ ".ORDER", Order, 0 },
{ ".PARALLEL", Parallel, 0 },
{ ".PATH", ExPath, 0 },
{ ".PHONY", Phony, OP_PHONY },
#ifdef POSIX
{ ".POSIX", Posix, 0 },
#endif
{ ".PRECIOUS", Precious, OP_PRECIOUS },
{ ".RECURSIVE", Attribute, OP_MAKE },
{ ".SHELL", ExShell, 0 },
{ ".SILENT", Silent, OP_SILENT },
{ ".SINGLESHELL", SingleShell, 0 },
{ ".STALE", Stale, 0 },
{ ".SUFFIXES", Suffixes, 0 },
{ ".USE", Attribute, OP_USE },
{ ".USEBEFORE", Attribute, OP_USEBEFORE },
{ ".WAIT", Wait, 0 },
};
////////////////////////////////////////////////////////////
// local functions
static int ParseIsEscaped(const char *, const char *);
static void ParseErrorInternal(const char *, size_t, int, const char *, ...)
MAKE_ATTR_PRINTFLIKE(4,5);
static void ParseVErrorInternal(FILE *, const char *, size_t, int, const char *, va_list)
MAKE_ATTR_PRINTFLIKE(5, 0);
static int ParseFindKeyword(const char *);
static int ParseLinkSrc(void *, void *);
static int ParseDoOp(void *, void *);
static void ParseDoSrc(int, const char *);
static int ParseFindMain(void *, void *);
static int ParseAddDir(void *, void *);
static int ParseClearPath(void *, void *);
static void ParseDoDependency(char *);
static int ParseAddCmd(void *, void *);
static void ParseHasCommands(void *);
static void ParseDoInclude(char *);
static void ParseSetParseFile(const char *);
#ifdef SYSVINCLUDE
static void ParseTraditionalInclude(char *);
#endif
#ifdef GMAKEEXPORT
static void ParseGmakeExport(char *);
#endif
static int ParseEOF(void);
static char *ParseReadLine(void);
static void ParseFinishLine(void);
static void ParseMark(GNode *);
////////////////////////////////////////////////////////////
// file loader
struct loadedfile {
const char *path; /* name, for error reports */
char *buf; /* contents buffer */
size_t len; /* length of contents */
size_t maplen; /* length of mmap area, or 0 */
Boolean used; /* XXX: have we used the data yet */
};
/*
* Constructor/destructor for loadedfile
*/
static struct loadedfile *
loadedfile_create(const char *path)
{
struct loadedfile *lf;
lf = bmake_malloc(sizeof(*lf));
lf->path = (path == NULL ? "(stdin)" : path);
lf->buf = NULL;
lf->len = 0;
lf->maplen = 0;
lf->used = FALSE;
return lf;
}
static void
loadedfile_destroy(struct loadedfile *lf)
{
if (lf->buf != NULL) {
if (lf->maplen > 0) {
#ifdef HAVE_MMAP
munmap(lf->buf, lf->maplen);
#endif
} else {
free(lf->buf);
}
}
free(lf);
}
/*
* nextbuf() operation for loadedfile, as needed by the weird and twisted
* logic below. Once that's cleaned up, we can get rid of lf->used...
*/
static char *
loadedfile_nextbuf(void *x, size_t *len)
{
struct loadedfile *lf = x;
if (lf->used) {
return NULL;
}
lf->used = TRUE;
*len = lf->len;
return lf->buf;
}
/*
* Try to get the size of a file.
*/
static ReturnStatus
load_getsize(int fd, size_t *ret)
{
struct stat st;
if (fstat(fd, &st) < 0) {
return FAILURE;
}
if (!S_ISREG(st.st_mode)) {
return FAILURE;
}
/*
* st_size is an off_t, which is 64 bits signed; *ret is
* size_t, which might be 32 bits unsigned or 64 bits
* unsigned. Rather than being elaborate, just punt on
* files that are more than 2^31 bytes. We should never
* see a makefile that size in practice...
*
* While we're at it reject negative sizes too, just in case.
*/
if (st.st_size < 0 || st.st_size > 0x7fffffff) {
return FAILURE;
}
*ret = (size_t) st.st_size;
return SUCCESS;
}
/*
* Read in a file.
*
* Until the path search logic can be moved under here instead of
* being in the caller in another source file, we need to have the fd
* passed in already open. Bleh.
*
* If the path is NULL use stdin and (to insure against fd leaks)
* assert that the caller passed in -1.
*/
static struct loadedfile *
loadfile(const char *path, int fd)
{
struct loadedfile *lf;
#ifdef HAVE_MMAP
long pagesize;
#endif
ssize_t result;
size_t bufpos;
lf = loadedfile_create(path);
if (path == NULL) {
assert(fd == -1);
fd = STDIN_FILENO;
} else {
#if 0 /* notyet */
fd = open(path, O_RDONLY);
if (fd < 0) {
...
Error("%s: %s", path, strerror(errno));
exit(1);
}
#endif
}
#ifdef HAVE_MMAP
if (load_getsize(fd, &lf->len) == SUCCESS) {
/* found a size, try mmap */
pagesize = sysconf(_SC_PAGESIZE);
if (pagesize <= 0) {
pagesize = 0x1000;
}
/* round size up to a page */
lf->maplen = pagesize * ((lf->len + pagesize - 1)/pagesize);
/*
* XXX hack for dealing with empty files; remove when
* we're no longer limited by interfacing to the old
* logic elsewhere in this file.
*/
if (lf->maplen == 0) {
lf->maplen = pagesize;
}
/*
* FUTURE: remove PROT_WRITE when the parser no longer
* needs to scribble on the input.
*/
lf->buf = mmap(NULL, lf->maplen, PROT_READ|PROT_WRITE,
MAP_FILE|MAP_COPY, fd, 0);
if (lf->buf != MAP_FAILED) {
/* succeeded */
if (lf->len == lf->maplen && lf->buf[lf->len - 1] != '\n') {
char *b = malloc(lf->len + 1);
b[lf->len] = '\n';
memcpy(b, lf->buf, lf->len++);
munmap(lf->buf, lf->maplen);
lf->maplen = 0;
lf->buf = b;
}
goto done;
}
}
#endif
/* cannot mmap; load the traditional way */
lf->maplen = 0;
lf->len = 1024;
lf->buf = bmake_malloc(lf->len);
bufpos = 0;
while (1) {
assert(bufpos <= lf->len);
if (bufpos == lf->len) {
lf->len *= 2;
lf->buf = bmake_realloc(lf->buf, lf->len);
}
result = read(fd, lf->buf + bufpos, lf->len - bufpos);
if (result < 0) {
Error("%s: read error: %s", path, strerror(errno));
exit(1);
}
if (result == 0) {
break;
}
bufpos += result;
}
assert(bufpos <= lf->len);
lf->len = bufpos;
/* truncate malloc region to actual length (maybe not useful) */
if (lf->len > 0) {
lf->buf = bmake_realloc(lf->buf, lf->len);
}
#ifdef HAVE_MMAP
done:
#endif
if (path != NULL) {
close(fd);
}
return lf;
}
////////////////////////////////////////////////////////////
// old code
/*-
*----------------------------------------------------------------------
* ParseIsEscaped --
* Check if the current character is escaped on the current line
*
* Results:
* 0 if the character is not backslash escaped, 1 otherwise
*
* Side Effects:
* None
*----------------------------------------------------------------------
*/
static int
ParseIsEscaped(const char *line, const char *c)
{
int active = 0;
for (;;) {
if (line == c)
return active;
if (*--c != '\\')
return active;
active = !active;
}
}
/*-
*----------------------------------------------------------------------
* ParseFindKeyword --
* Look in the table of keywords for one matching the given string.
*
* Input:
* str String to find
*
* Results:
* The index of the keyword, or -1 if it isn't there.
*
* Side Effects:
* None
*----------------------------------------------------------------------
*/
static int
ParseFindKeyword(const char *str)
{
int start, end, cur;
int diff;
start = 0;
end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1;
do {
cur = start + ((end - start) / 2);
diff = strcmp(str, parseKeywords[cur].name);
if (diff == 0) {
return (cur);
} else if (diff < 0) {
end = cur - 1;
} else {
start = cur + 1;
}
} while (start <= end);
return (-1);
}
/*-
* ParseVErrorInternal --
* Error message abort function for parsing. Prints out the context
* of the error (line number and file) as well as the message with
* two optional arguments.
*
* Results:
* None
*
* Side Effects:
* "fatals" is incremented if the level is PARSE_FATAL.
*/
/* VARARGS */
static void
ParseVErrorInternal(FILE *f, const char *cfname, size_t clineno, int type,
const char *fmt, va_list ap)
{
static Boolean fatal_warning_error_printed = FALSE;
(void)fprintf(f, "%s: ", progname);
if (cfname != NULL) {
(void)fprintf(f, "\"");
if (*cfname != '/' && strcmp(cfname, "(stdin)") != 0) {
char *cp;
const char *dir;
/*
* Nothing is more annoying than not knowing
* which Makefile is the culprit.
*/
dir = Var_Value(".PARSEDIR", VAR_GLOBAL, &cp);
if (dir == NULL || *dir == '\0' ||
(*dir == '.' && dir[1] == '\0'))
dir = Var_Value(".CURDIR", VAR_GLOBAL, &cp);
if (dir == NULL)
dir = ".";
(void)fprintf(f, "%s/%s", dir, cfname);
} else
(void)fprintf(f, "%s", cfname);
(void)fprintf(f, "\" line %d: ", (int)clineno);
}
if (type == PARSE_WARNING)
(void)fprintf(f, "warning: ");
(void)vfprintf(f, fmt, ap);
(void)fprintf(f, "\n");
(void)fflush(f);
if (type == PARSE_FATAL || parseWarnFatal)
fatals += 1;
if (parseWarnFatal && !fatal_warning_error_printed) {
Error("parsing warnings being treated as errors");
fatal_warning_error_printed = TRUE;
}
}
/*-
* ParseErrorInternal --
* Error function
*
* Results:
* None
*
* Side Effects:
* None
*/
/* VARARGS */
static void
ParseErrorInternal(const char *cfname, size_t clineno, int type,
const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
(void)fflush(stdout);
ParseVErrorInternal(stderr, cfname, clineno, type, fmt, ap);
va_end(ap);
if (debug_file != stderr && debug_file != stdout) {
va_start(ap, fmt);
ParseVErrorInternal(debug_file, cfname, clineno, type, fmt, ap);
va_end(ap);
}
}
/*-
* Parse_Error --
* External interface to ParseErrorInternal; uses the default filename
* Line number.
*
* Results:
* None
*
* Side Effects:
* None
*/
/* VARARGS */
void
Parse_Error(int type, const char *fmt, ...)
{
va_list ap;
const char *fname;
size_t lineno;
if (curFile == NULL) {
fname = NULL;
lineno = 0;
} else {
fname = curFile->fname;
lineno = curFile->lineno;
}
va_start(ap, fmt);
(void)fflush(stdout);
ParseVErrorInternal(stderr, fname, lineno, type, fmt, ap);
va_end(ap);
if (debug_file != stderr && debug_file != stdout) {
va_start(ap, fmt);
ParseVErrorInternal(debug_file, fname, lineno, type, fmt, ap);
va_end(ap);
}
}
/*
* ParseMessage
* Parse a .info .warning or .error directive
*
* The input is the line minus the ".". We substitute
* variables, print the message and exit(1) (for .error) or just print
* a warning if the directive is malformed.
*/
static Boolean
ParseMessage(char *line)
{
int mtype;
switch(*line) {
case 'i':
mtype = 0;
break;
case 'w':
mtype = PARSE_WARNING;
break;
case 'e':
mtype = PARSE_FATAL;
break;
default:
Parse_Error(PARSE_WARNING, "invalid syntax: \".%s\"", line);
return FALSE;
}
while (isalpha((u_char)*line))
line++;
if (!isspace((u_char)*line))
return FALSE; /* not for us */
while (isspace((u_char)*line))
line++;
line = Var_Subst(NULL, line, VAR_CMD, 0);
Parse_Error(mtype, "%s", line);
free(line);
if (mtype == PARSE_FATAL) {
/* Terminate immediately. */
exit(1);
}
return TRUE;
}
/*-
*---------------------------------------------------------------------
* ParseLinkSrc --
* Link the parent node to its new child. Used in a Lst_ForEach by
* ParseDoDependency. If the specType isn't 'Not', the parent
* isn't linked as a parent of the child.
*
* Input:
* pgnp The parent node
* cgpn The child node
*
* Results:
* Always = 0
*
* Side Effects:
* New elements are added to the parents list of cgn and the
* children list of cgn. the unmade field of pgn is updated
* to reflect the additional child.
*---------------------------------------------------------------------
*/
static int
ParseLinkSrc(void *pgnp, void *cgnp)
{
GNode *pgn = (GNode *)pgnp;
GNode *cgn = (GNode *)cgnp;
if ((pgn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (pgn->cohorts))
pgn = (GNode *)Lst_Datum(Lst_Last(pgn->cohorts));
(void)Lst_AtEnd(pgn->children, cgn);
if (specType == Not)
(void)Lst_AtEnd(cgn->parents, pgn);
pgn->unmade += 1;
if (DEBUG(PARSE)) {
fprintf(debug_file, "# ParseLinkSrc: added child %s - %s\n", pgn->name, cgn->name);
Targ_PrintNode(pgn, 0);
Targ_PrintNode(cgn, 0);
}
return (0);
}
/*-
*---------------------------------------------------------------------
* ParseDoOp --
* Apply the parsed operator to the given target node. Used in a
* Lst_ForEach call by ParseDoDependency once all targets have
* been found and their operator parsed. If the previous and new
* operators are incompatible, a major error is taken.
*
* Input:
* gnp The node to which the operator is to be applied
* opp The operator to apply
*
* Results:
* Always 0
*
* Side Effects:
* The type field of the node is altered to reflect any new bits in
* the op.
*---------------------------------------------------------------------
*/
static int
ParseDoOp(void *gnp, void *opp)
{
GNode *gn = (GNode *)gnp;
int op = *(int *)opp;
/*
* If the dependency mask of the operator and the node don't match and
* the node has actually had an operator applied to it before, and
* the operator actually has some dependency information in it, complain.
*/
if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
!OP_NOP(gn->type) && !OP_NOP(op))
{
Parse_Error(PARSE_FATAL, "Inconsistent operator for %s", gn->name);
return (1);
}
if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
/*
* If the node was the object of a :: operator, we need to create a
* new instance of it for the children and commands on this dependency
* line. The new instance is placed on the 'cohorts' list of the
* initial one (note the initial one is not on its own cohorts list)
* and the new instance is linked to all parents of the initial
* instance.
*/
GNode *cohort;
/*
* Propagate copied bits to the initial node. They'll be propagated
* back to the rest of the cohorts later.
*/
gn->type |= op & ~OP_OPMASK;
cohort = Targ_FindNode(gn->name, TARG_NOHASH);
if (doing_depend)
ParseMark(cohort);
/*
* Make the cohort invisible as well to avoid duplicating it into
* other variables. True, parents of this target won't tend to do
* anything with their local variables, but better safe than
* sorry. (I think this is pointless now, since the relevant list
* traversals will no longer see this node anyway. -mycroft)
*/
cohort->type = op | OP_INVISIBLE;
(void)Lst_AtEnd(gn->cohorts, cohort);
cohort->centurion = gn;
gn->unmade_cohorts += 1;
snprintf(cohort->cohort_num, sizeof cohort->cohort_num, "#%d",
gn->unmade_cohorts);
} else {
/*
* We don't want to nuke any previous flags (whatever they were) so we
* just OR the new operator into the old
*/
gn->type |= op;
}
return (0);
}
/*-
*---------------------------------------------------------------------
* ParseDoSrc --
* Given the name of a source, figure out if it is an attribute
* and apply it to the targets if it is. Else decide if there is
* some attribute which should be applied *to* the source because
* of some special target and apply it if so. Otherwise, make the
* source be a child of the targets in the list 'targets'
*
* Input:
* tOp operator (if any) from special targets
* src name of the source to handle
*
* Results:
* None
*
* Side Effects:
* Operator bits may be added to the list of targets or to the source.
* The targets may have a new source added to their lists of children.
*---------------------------------------------------------------------
*/
static void
ParseDoSrc(int tOp, const char *src)
{
GNode *gn = NULL;
static int wait_number = 0;
char wait_src[16];
if (*src == '.' && isupper ((unsigned char)src[1])) {
int keywd = ParseFindKeyword(src);
if (keywd != -1) {
int op = parseKeywords[keywd].op;
if (op != 0) {
Lst_ForEach(targets, ParseDoOp, &op);
return;
}
if (parseKeywords[keywd].spec == Wait) {
/*
* We add a .WAIT node in the dependency list.
* After any dynamic dependencies (and filename globbing)
* have happened, it is given a dependency on the each
* previous child back to and previous .WAIT node.
* The next child won't be scheduled until the .WAIT node
* is built.
* We give each .WAIT node a unique name (mainly for diag).
*/
snprintf(wait_src, sizeof wait_src, ".WAIT_%u", ++wait_number);
gn = Targ_FindNode(wait_src, TARG_NOHASH);
if (doing_depend)
ParseMark(gn);
gn->type = OP_WAIT | OP_PHONY | OP_DEPENDS | OP_NOTMAIN;
Lst_ForEach(targets, ParseLinkSrc, gn);
return;
}
}