-
Notifications
You must be signed in to change notification settings - Fork 151
/
mamake.c
2375 lines (2183 loc) · 46.9 KB
/
mamake.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
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1990-2011 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.eclipse.org/org/documents/epl-v10.html *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
/*
* mamake -- MAM make
*
* coded for portability
*/
static char id[] = "\n@(#)$Id: mamake (AT&T Research) 2011-08-31 $\0\n";
#if _PACKAGE_ast
#include <ast.h>
#include <error.h>
static const char usage[] =
"[-?\n@(#)$Id: mamake (AT&T Research) 2011-08-31 $\n]"
USAGE_LICENSE
"[+NAME?mamake - make abstract machine make]"
"[+DESCRIPTION?\bmamake\b reads \amake abstract machine\a target and"
" prerequisite file descriptions from a mamfile (see \b-f\b) and executes"
" actions to update targets that are older than their prerequisites."
" Mamfiles are generated by the \b--mam\b option of \bnmake\b(1) and"
" \bgmake\b(1) and are portable to environments that only have"
" \bsh\b(1) and \bcc\b(1).]"
"[+?In practice \bmamake\b is used to bootstrap build \bnmake\b(1) and"
" \bksh\b(1) in new environments. Mamfiles are used rather than"
" old-\bmake\b makefiles because some features are not reliably supported"
" across all \bmake\b variants:]{"
" [+action execution?Multi-line actions are executed as a"
" unit by \b$SHELL\b. There are some shell constructs"
" that cannot be expressed in an old-\bmake\b makefile.]"
" [+viewpathing?\bVPATH\b is properly interpreted. This allows"
" source to be separate from generated files.]"
" [+recursion?Ordered subdirectory recursion over unrelated"
" makefiles.]"
" }"
"[+?\bmamprobe\b(1) is called to probe and generate system specific variable"
" definitions. The probe information is regenerated when it is older"
" than the \bmamprobe\b command.]"
"[+?For compatibility with \bnmake\b(1) the \b-K\b option and the"
" \brecurse\b and \bcc-*\b command line targets are ignored.]"
"[e:?Explain reason for triggering action. Ignored if -F is on.]"
"[f:?Read \afile\a instead of the default.]:[file:=Mamfile]"
"[i:?Ignore action errors.]"
"[k:?Continue after error with sibling prerequisites.]"
"[n:?Print actions but do not execute. Recursion actions (see \b-r\b) are still"
" executed. Use \b-N\b to disable recursion actions too.]"
"[r:?Recursively make leaf directories matching \apattern\a. Only leaf"
" directories containing a makefile named \bNmakefile\b, \bnmakefile\b,"
" \bMakefile\b or \bmakefile\b are considered. The first makefile"
" found in each leaf directory is scanned for leaf directory"
" prerequisites; the recusion order is determined by a topological sort"
" of these prerequisites.]:[pattern]"
"[C:?Do all work in \adirectory\a. All messages will mention"
" \adirectory\a.]:[directory]"
"[D:?Set the debug trace level to \alevel\a. Higher levels produce more"
" output.]#[level]"
"[F:?Force all targets to be out of date.]"
"[K:?Ignored.]"
"[N:?Like \b-n\b but recursion actions (see \b-r\b) are also disabled.]"
"[V:?Print the program version and exit.]"
"[G:debug-symbols?Compile and link with debugging symbol options enabled.]"
"[S:strip-symbols?Strip link-time static symbols from executables.]"
"\n"
"\n[ target ... ] [ name=value ... ]\n"
"\n"
"[+SEE ALSO?\bgmake\b(1), \bmake\b(1), \bmamprobe\b(1),"
" \bnmake\b(1), \bsh\b(1)]"
;
#else
#define elementsof(x) (sizeof(x)/sizeof(x[0]))
#define newof(p,t,n,x) ((p)?(t*)realloc((char*)(p),sizeof(t)*(n)+(x)):(t*)calloc(1,sizeof(t)*(n)+(x)))
#define NiL ((char*)0)
#endif
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#if !_PACKAGE_ast && defined(__STDC__)
#include <stdlib.h>
#include <string.h>
#endif
#define delimiter(c) ((c)==' '||(c)=='\t'||(c)=='\n'||(c)==';'||(c)=='('||(c)==')'||(c)=='`'||(c)=='|'||(c)=='&'||(c)=='=')
#define add(b,c) (((b)->nxt >= (b)->end) ? append(b, "") : NiL, *(b)->nxt++ = (c))
#define get(b) ((b)->nxt-(b)->buf)
#define set(b,o) ((b)->nxt=(b)->buf+(o))
#define use(b) (*(b)->nxt=0,(b)->nxt=(b)->buf)
#define CHUNK 1024
#define KEY(a,b,c,d) ((((unsigned long)(a))<<15)|(((unsigned long)(b))<<10)|(((unsigned long)(c))<<5)|(((unsigned long)(d))))
#define NOW ((unsigned long)time((time_t*)0))
#define ROTATE(p,l,r,t) ((t)=(p)->l,(p)->l=(t)->r,(t)->r=(p),(p)=(t))
#define RULE_active 0x0001 /* active target */
#define RULE_dontcare 0x0002 /* ok if not found */
#define RULE_error 0x0004 /* not found or not generated */
#define RULE_exists 0x0008 /* target file exists */
#define RULE_generated 0x0010 /* generated target */
#define RULE_ignore 0x0020 /* ignore time */
#define RULE_implicit 0x0040 /* implicit prerequisite */
#define RULE_made 0x0080 /* already made */
#define RULE_virtual 0x0100 /* not a file */
#define STREAM_KEEP 0x0001 /* don't fclose() on pop() */
#define STREAM_MUST 0x0002 /* push() file must exist */
#define STREAM_PIPE 0x0004 /* pclose() on pop() */
#ifndef S_IXUSR
#define S_IXUSR 0100 /* owner execute permission */
#endif
#ifndef S_IXGRP
#define S_IXGRP 0010 /* group execute permission */
#endif
#ifndef S_IXOTH
#define S_IXOTH 0001 /* other execute permission */
#endif
struct Rule_s;
typedef struct stat Stat_t;
typedef FILE Stdio_t;
typedef struct Buf_s /* buffer stream */
{
struct Buf_s* old; /* next dropped buffer */
char* end; /* 1 past end of buffer */
char* nxt; /* next char to add */
char* buf; /* buffer space */
} Buf_t;
typedef struct Dict_item_s /* dictionary item */
{
struct Dict_item_s* left; /* left child */
struct Dict_item_s* right; /* right child */
void* value; /* user defined value */
char name[1];/* 0 terminated name */
} Dict_item_t;
typedef struct Dict_s /* dictionary handle */
{
Dict_item_t* root; /* root item */
} Dict_t;
typedef struct List_s /* Rule_t list */
{
struct List_s* next; /* next in list */
struct Rule_s* rule; /* list item */
} List_t;
typedef struct Rule_s /* rule item */
{
char* name; /* unbound name */
char* path; /* bound path */
List_t* prereqs; /* prerequisites */
struct Rule_s* leaf; /* recursion leaf alias */
int flags; /* RULE_* flags */
int making; /* currently make()ing */
unsigned long time; /* modification time */
} Rule_t;
typedef struct Stream_s /* input file stream stack */
{
Stdio_t* fp; /* read stream */
char* file; /* stream path */
unsigned long line; /* stream line */
int flags; /* stream flags */
} Stream_t;
typedef struct View_s /* viewpath level */
{
struct View_s* next; /* next level in viewpath */
int node; /* viewpath node path length */
char dir[1]; /* viewpath level dir prefix */
} View_t;
static struct /* program state */
{
Buf_t* buf; /* work buffer */
Buf_t* old; /* dropped buffers */
Buf_t* opt; /* option buffer */
Dict_t* leaf; /* recursion leaf dictionary */
Dict_t* libs; /* library dictionary */
Dict_t* rules; /* rule dictionary */
Dict_t* vars; /* variable dictionary */
View_t* view; /* viewpath levels */
char* directory; /* work in this directory */
char* id; /* command name */
char* file; /* first input file */
char* pwd; /* current directory */
char* recurse; /* recursion pattern */
char* shell; /* ${SHELL} */
int active; /* targets currently active */
int debug; /* negative of debug level */
int errors; /* some error(s) occurred */
int exec; /* execute actions */
int explain; /* explain actions */
int force; /* all targets out of date */
int ignore; /* ignore command errors */
int indent; /* debug indent */
int keepgoing; /* do siblings on error */
int never; /* never execute */
int peek; /* next line already in input */
int probed; /* probe already done */
int verified; /* don't bother with verify() */
Stream_t streams[4]; /* input file stream stack */
Stream_t* sp; /* input stream stack pointer */
char input[8*CHUNK]; /* input buffer */
} state;
static unsigned long make(Rule_t*);
static char mamfile[] = "Mamfile";
static char sh[] = "/bin/sh";
extern char** environ;
#if !_PACKAGE_ast
#if defined(NeXT) || defined(__NeXT)
#define getcwd(a,b) getwd(a)
#endif
/*
* emit usage message and exit
*/
static void
usage()
{
fprintf(stderr, "Usage: %s [-iknFKNV] [-f mamfile] [-r pattern] [-C directory] [-D level] [target ...] [name=value ...]\n", state.id);
exit(2);
}
#endif
/*
* output error message identification
*/
static void
identify(Stdio_t* sp)
{
if (state.directory)
fprintf(sp, "%s [%s]: ", state.id, state.directory);
else
fprintf(sp, "%s: ", state.id);
}
/*
* emit error message
* level:
* <0 debug
* 0 info
* 1 warning
* 2 error
* >2 exit(level-2)
*/
static void
report(int level, char* text, char* item, unsigned long stamp)
{
int i;
if (level >= state.debug)
{
if (level)
identify(stderr);
if (level < 0)
{
fprintf(stderr, "debug%d: ", level);
for (i = 1; i < state.indent; i++)
fprintf(stderr, " ");
}
else
{
if (state.sp && state.sp->line)
{
if (state.sp->file)
fprintf(stderr, "%s: ", state.sp->file);
fprintf(stderr, "%ld: ", state.sp->line);
}
if (level == 1)
fprintf(stderr, "warning: ");
else if (level > 1)
state.errors = 1;
}
if (item)
fprintf(stderr, "%s: ", item);
fprintf(stderr, "%s", text);
if (stamp && state.debug <= -2)
fprintf(stderr, " %10lu", stamp);
fprintf(stderr, "\n");
if (level > 2)
exit(level - 2);
}
}
/*
* don't know how to make or exit code making
*/
static void
dont(Rule_t* r, int code, int keepgoing)
{
identify(stderr);
if (!code)
fprintf(stderr, "don't know how to make %s\n", r->name);
else
{
fprintf(stderr, "*** exit code %d making %s%s\n", code, r->name, state.ignore ? " ignored" : "");
unlink(r->name);
if (state.ignore)
return;
}
if (!keepgoing)
exit(1);
state.errors++;
r->flags |= RULE_error;
}
/*
* local strrchr()
*/
static char*
last(register char* s, register int c)
{
register char* r = 0;
for (r = 0; *s; s++)
if (*s == c)
r = s;
return r;
}
/*
* open a buffer stream
*/
static Buf_t*
buffer(void)
{
register Buf_t* buf;
if (buf = state.old)
state.old = state.old->old;
else if (!(buf = newof(0, Buf_t, 1, 0)) || !(buf->buf = newof(0, char, CHUNK, 0)))
report(3, "out of space [buffer]", NiL, (unsigned long)0);
buf->end = buf->buf + CHUNK;
buf->nxt = buf->buf;
return buf;
}
/*
* close a buffer stream
*/
static void
drop(Buf_t* buf)
{
buf->old = state.old;
state.old = buf;
}
/*
* append str length n to buffer and return the buffer base
*/
static char*
appendn(Buf_t* buf, char* str, int n)
{
int m;
int i;
if ((n + 1) >= (buf->end - buf->nxt))
{
i = buf->nxt - buf->buf;
m = (((buf->end - buf->buf) + n + CHUNK + 1) / CHUNK) * CHUNK;
if (!(buf->buf = newof(buf->buf, char, m, 0)))
report(3, "out of space [buffer resize]", NiL, (unsigned long)0);
buf->end = buf->buf + m;
buf->nxt = buf->buf + i;
}
memcpy(buf->nxt, str, n + 1);
buf->nxt += n;
return buf->buf;
}
/*
* append str to buffer and return the buffer base
* if str==0 then next pointer reset to base
*/
static char*
append(Buf_t* buf, char* str)
{
if (str)
return appendn(buf, str, strlen(str));
buf->nxt = buf->buf;
return buf->buf;
}
/*
* allocate space for s and return the copy
*/
static char*
duplicate(char* s)
{
char* t;
int n;
n = strlen(s);
if (!(t = newof(0, char, n, 1)))
report(3, "out of space [duplicate]", s, (unsigned long)0);
strcpy(t, s);
return t;
}
/*
* open a new dictionary
*/
static Dict_t*
dictionary(void)
{
Dict_t* dict;
if (!(dict = newof(0, Dict_t, 1, 0)))
report(3, "out of space [dictionary]", NiL, (unsigned long)0);
return dict;
}
/*
* return the value for item name in dictionary dict
* if value!=0 then name entry value is created if necessary and set
* uses top-down splaying (ala Tarjan and Sleator)
*/
static void*
search(register Dict_t* dict, char* name, void* value)
{
register int cmp;
register Dict_item_t* root;
register Dict_item_t* t;
register Dict_item_t* left;
register Dict_item_t* right;
register Dict_item_t* lroot;
register Dict_item_t* rroot;
root = dict->root;
left = right = lroot = rroot = 0;
while (root)
{
if (!(cmp = strcmp(name, root->name)))
break;
else if (cmp < 0)
{
if (root->left && (cmp = strcmp(name, root->left->name)) <= 0)
{
ROTATE(root, left, right, t);
if (!cmp)
break;
}
if (right)
right->left = root;
else
rroot = root;
right = root;
root = root->left;
right->left = 0;
}
else
{
if (root->right && (cmp = strcmp(name, root->right->name)) >= 0)
{
ROTATE(root, right, left, t);
if (!cmp)
break;
}
if (left)
left->right = root;
else
lroot = root;
left = root;
root = root->right;
left->right = 0;
}
}
if (root)
{
if (right)
right->left = root->right;
else
rroot = root->right;
if (left)
left->right = root->left;
else
lroot = root->left;
}
else if (value)
{
if (!(root = newof(0, Dict_item_t, 1, strlen(name))))
report(3, "out of space [dictionary]", name, (unsigned long)0);
strcpy(root->name, name);
}
if (root)
{
if (value)
root->value = value;
root->left = lroot;
root->right = rroot;
dict->root = root;
return value ? (void*)root->name : root->value;
}
if (left)
{
left->right = rroot;
dict->root = lroot;
}
else if (right)
{
right->left = lroot;
dict->root = rroot;
}
return 0;
}
/*
* low level for walk()
*/
static int
apply(Dict_t* dict, Dict_item_t* item, int (*func)(Dict_item_t*, void*), void* handle)
{
register Dict_item_t* right;
do
{
right = item->right;
if (item->left && apply(dict, item->left, func, handle))
return -1;
if ((*func)(item, handle))
return -1;
} while (item = right);
return 0;
}
/*
* apply func to each dictionary item
*/
static int
walk(Dict_t* dict, int (*func)(Dict_item_t*, void*), void* handle)
{
return dict->root ? apply(dict, dict->root, func, handle) : 0;
}
/*
* return a rule pointer for name
*/
static Rule_t*
rule(char* name)
{
Rule_t* r;
if (!(r = (Rule_t*)search(state.rules, name, NiL)))
{
if (!(r = newof(0, Rule_t, 1, 0)))
report(3, "out of space [rule]", name, (unsigned long)0);
r->name = (char*)search(state.rules, name, (void*)r);
}
return r;
}
/*
* prepend p onto rule r prereqs
*/
static void
cons(Rule_t* r, Rule_t* p)
{
register List_t* x;
for (x = r->prereqs; x && x->rule != p; x = x->next);
if (!x)
{
if (!(x = newof(0, List_t, 1, 0)))
report(3, "out of space [list]", r->name, (unsigned long)0);
x->rule = p;
x->next = r->prereqs;
r->prereqs = x;
}
}
/*
* initialize the viewpath
*/
static void
view(void)
{
register char* s;
register char* t;
register char* p;
register View_t* vp;
View_t* zp;
int c;
int n;
Stat_t st;
Stat_t ts;
char buf[CHUNK];
if (stat(".", &st))
report(3, "cannot stat", ".", (unsigned long)0);
if ((s = (char*)search(state.vars, "PWD", NiL)) && !stat(s, &ts) &&
ts.st_dev == st.st_dev && ts.st_ino == st.st_ino)
state.pwd = s;
if (!state.pwd)
{
if (!getcwd(buf, sizeof(buf) - 1))
report(3, "cannot determine PWD", NiL, (unsigned long)0);
state.pwd = duplicate(buf);
search(state.vars, "PWD", state.pwd);
}
if ((s = (char*)search(state.vars, "VPATH", NiL)) && *s)
{
zp = 0;
for (;;)
{
for (t = s; *t && *t != ':'; t++);
if (c = *t)
*t = 0;
if (!state.view)
{
/*
* determine the viewpath offset
*/
if (stat(s, &st))
report(3, "cannot stat top view", s, (unsigned long)0);
if (stat(state.pwd, &ts))
report(3, "cannot stat", state.pwd, (unsigned long)0);
if (ts.st_dev == st.st_dev && ts.st_ino == st.st_ino)
p = ".";
else
{
p = state.pwd + strlen(state.pwd);
while (p > state.pwd)
if (*--p == '/')
{
if (p == state.pwd)
report(3, ". not under VPATH", s, (unsigned long)0);
*p = 0;
if (stat(state.pwd, &ts))
report(3, "cannot stat", state.pwd, (unsigned long)0);
*p = '/';
if (ts.st_dev == st.st_dev && ts.st_ino == st.st_ino)
{
p++;
break;
}
}
if (p <= state.pwd)
report(3, "cannot determine viewpath offset", s, (unsigned long)0);
}
}
n = strlen(s);
if (!(vp = newof(0, View_t, 1, strlen(p) + n + 1)))
report(3, "out of space [view]", s, (unsigned long)0);
vp->node = n + 1;
strcpy(vp->dir, s);
*(vp->dir + n) = '/';
strcpy(vp->dir + n + 1, p);
report(-4, vp->dir, "view", (unsigned long)0);
if (!state.view)
state.view = zp = vp;
else
zp = zp->next = vp;
if (!c)
break;
*t++ = c;
s = t;
}
}
}
/*
* return next '?' or '}' in nested '}'
*/
static char*
cond(register char* s)
{
register int n;
if (*s == '?')
s++;
n = 0;
for (;;)
{
switch (*s++)
{
case 0:
break;
case '{':
n++;
continue;
case '}':
if (!n--)
break;
continue;
case '?':
if (!n)
break;
continue;
default:
continue;
}
break;
}
return s - 1;
}
/*
* expand var refs from s into buf
*/
static void
substitute(Buf_t* buf, register char* s)
{
register char* t;
register char* v;
register char* q;
register char* b;
register int c;
register int n;
int a = 0;
int i;
while (c = *s++)
{
if (c == '$' && *s == '{')
{
b = s - 1;
i = 1;
for (n = *(t = ++s) == '-' ? 0 : '-'; (c = *s) && c != '?' && c != '+' && c != n && c != ':' && c != '=' && c != '[' && c != '}'; s++)
if (!isalnum(c) && c != '_')
i = 0;
*s = 0;
if (c == '[')
{
append(buf, b);
*s = c;
continue;
}
v = (char*)search(state.vars, t, NiL);
if ((c == ':' || c == '=') && (!v || c == ':' && !*v))
{
append(buf, b);
*s = c;
continue;
}
if (t[0] == 'A' && t[1] == 'R' && t[2] == 0)
a = 1;
*s = c;
if (c && c != '}')
{
n = 1;
for (t = ++s; *s; s++)
if (*s == '{')
n++;
else if (*s == '}' && !--n)
break;
}
switch (c)
{
case '?':
q = cond(t - 1);
if (v)
{
if (((q - t) != 1 || *t != '*') && strncmp(v, t, q - t))
v = 0;
}
else if (q == t)
v = s;
t = cond(q);
if (v)
{
if (t > q)
{
c = *t;
*t = 0;
substitute(buf, q + 1);
*t = c;
}
}
else
{
q = cond(t);
if (q > t)
{
c = *q;
*q = 0;
substitute(buf, t + 1);
*q = c;
}
}
break;
case '+':
case '-':
if ((v == 0 || *v == 0) == (c == '-'))
{
c = *s;
*s = 0;
substitute(buf, t);
*s = c;
break;
}
if (c != '-')
break;
/*FALLTHROUGH*/
case 0:
case '=':
case '}':
if (v)
{
if (a && t[0] == 'm' && t[1] == 'a' && t[2] == 'm' && t[3] == '_' && t[4] == 'l' && t[5] == 'i' && t[6] == 'b')
{
for (t = v; *t == ' '; t++);
for (; *t && *t != ' '; t++);
if (*t)
*t = 0;
else
t = 0;
substitute(buf, v);
if (t)
*t = ' ';
}
else
substitute(buf, v);
}
else if (i)
{
c = *s;
*s = 0;
append(buf, b);
*s = c;
continue;
}
break;
}
if (*s)
s++;
}
else
add(buf, c);
}
}
/*
* expand var refs from s into buf and return buf base
*/
static char*
expand(Buf_t* buf, char* s)
{
substitute(buf, s);
return use(buf);
}
/*
* stat() with .exe check
*/
static char*
status(Buf_t* buf, int off, char* path, struct stat* st)
{
int r;
char* s;
Buf_t* tmp;
if (!stat(path, st))
return path;
if (!(tmp = buf))
{
tmp = buffer();
off = 0;
}
if (off)
set(tmp, off);
else
append(tmp, path);
append(tmp, ".exe");
s = use(tmp);
r = stat(s, st);
if (!buf)
{
drop(tmp);
s = path;
}
if (r)
{
if (off)
s[off] = 0;
s = 0;
}
return s;
}
/*
* return path to file
*/
static char*
find(Buf_t* buf, char* file, struct stat* st)
{
char* s;
View_t* vp;
int node;
int c;
int o;
if (s = status(buf, 0, file, st))
{
report(-3, s, "find", (unsigned long)0);
return s;
}
if (vp = state.view)
{
node = 0;
if (*file == '/')
{
do
{
if (!strncmp(file, vp->dir, vp->node))
{
file += vp->node;
node = 2;
break;
}
} while (vp = vp->next);
}
else
vp = vp->next;
if (vp)
do
{
if (node)
{
c = vp->dir[vp->node];
vp->dir[vp->node] = 0;
append(buf, vp->dir);
vp->dir[vp->node] = c;
}
else
{
append(buf, vp->dir);