File: include.c

package info (click to toggle)
xmake 1.05-2
  • links: PTS
  • area: main
  • in suites: potato, woody
  • size: 104 kB
  • ctags: 119
  • sloc: ansic: 1,371; makefile: 57
file content (704 lines) | stat: -rw-r--r-- 13,203 bytes parent folder | download | duplicates (2)
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

/*
 * INCLUDE.C
 *
 */

#include "defs.h"

Prototype void IncludeFile(const char *fileName);
Prototype INode	*CurINode;

Prototype const char *getToken(INode *in, int expand);
Prototype const char *expToken(Depend *dep, INode *in, const char *ptr, int mode);

char *extcolon(char **pp, char exdelim, char *pwe);

INode	*CurINode;

void
IncludeFile(const char *fileName)
{
    INode *in = MakeNode(fileName, sizeof(INode), 0);
    int vslen = VNodeStrSize(ExtDefs);
    char *path = malloc(strlen(fileName) + sizeof(CPPCMD) + vslen + 8);

    in->in_Parent = CurINode;
    in->in_Node.no_INode = in;
    in->in_Node.no_ILine = 0;
    in->in_LastNl = 1;
    InitList(&in->in_EList);

    CurINode = in;

    {
	int l;
	Node *node;

	strcpy(path, CPPCMD);
	l = strlen(path);
	for (node = GetHead(&ExtDefs->vn_List); node; node = GetNext(node)) {
	    sprintf(path + l, " %s", node->no_Name);
	    l += strlen(path + l);
	}
	sprintf(path + l, " %s", fileName);
    }

    if ((in->in_Fi = popen(path, "r")) != NULL) {
	const char *s;

	while ((s = getToken(in, EXP_FORCE)) != NULL) {
	    if (*s == '\n') {
		;
	    } else if (s[0] == '.' && s[1] != '.' && s[1] != '/') {
		if (strcmp(s, ".set") == 0) {
		    /*
		     * get variable name
		     */
		    VNode *vnode;

		    if ((s = getToken(in, EXP_FORCE)) == NULL) {
			fatal(&in->in_Node, ".set - unexpected EOF\n");
			/* not reached */
		    }
		    vnode = MakeVNode(&VarList, s);
		    while ((s = getToken(in, EXP_NORMAL)) != NULL && *s != '\n') {
			AddTail(&vnode->vn_List, &MakeDNode(s)->dn_Node);
		    }
		    CommitVNode(vnode);
		} else {
		    fatal(&in->in_Node, "Unknown directive: %s\n", s);
		    /* not reached */
		}
	    } else {
		/*
		 * Parse a dependancy:
		 *
		 * d1 d2 ... dn : s1 s2 ... sn [ : a1 a2 ... an ]
		 * d1 : s1 s2 ... sn
		 */
		List *lhs = malloc(sizeof(List));
		List *rhs = malloc(sizeof(List));
		List *ahs = malloc(sizeof(List));
		List *xlist = malloc(sizeof(List));
		List *clist = lhs;

		InitList(lhs);
		InitList(rhs);
		InitList(ahs);
		InitList(xlist);

		/*
		 * Dependancies
		 */

		while (s && *s != '\n') {
		    if (*s == ':') {
			if (clist == lhs)
			    clist = rhs;
			else if (clist == rhs)
			    clist = ahs;
			else
			    fatal(&in->in_Node, "Too many colons in dependancy line\n");
		    } else {
			AddTail(clist, &MakeDNode(s)->dn_Node);
		    }
		    if (clist == lhs)
			s = getToken(in, EXP_FORCE);
		    else
			s = getToken(in, EXP_FORCE);
		}
		if (s == NULL)
		    fatal(&in->in_Node, "Unexpected EOF\n");

		/*
		 * s1 : d1 d2 d3 ... dn
		 */

		if (lhs->li_Count == 1 && ahs->li_Count == 0) {
		    List *t = ahs;
		    ahs = rhs;
		    rhs = t;
		}

		if (lhs->li_Count != rhs->li_Count && rhs->li_Count > 1) {
		    fatal(
			&in->in_Node, 
			"lhs/rhs count mismatch for rule %s (%d,%d,%d)\n", 
			((lhs->li_Count) ? lhs->li_Node.no_Next->no_Name : "?"),
			lhs->li_Count,
			rhs->li_Count, 
			ahs->li_Count
		    );
		}

		/*
		 * Body
		 */

		{
		    int bcnt = 0;
		    int c;

		    while ((c = getc(in->in_Fi)) != EOF) {
			char buf[MAXLINE];

			++in->in_Node.no_ILine;

			/*
			 * Handle end of command sequencing
			 */

			if (c == '\n' && bcnt == 0)
			    break;

			/*
			 * Handle . directive
			 */

			if (c == '.') {
			    ungetc(c, in->in_Fi);
			    s = getToken(in, 0);

			    if (strcmp(s, ".set") == 0) {
				/*
				 * get variable name
				 */
				VNode *vnode;

				if ((s = getToken(in, 0)) == NULL) {
				    fatal(&in->in_Node, ".set - unexpected EOF\n");
				    /* not reached */
				}
				vnode = MakeVNode(xlist, s);
				while ((s = getToken(in, 0)) != NULL && *s != '\n') {
				    AddTail(&vnode->vn_List, &MakeDNode(s)->dn_Node);
				}
				CommitVNode(vnode);
			    } else if (strcmp(s, ".beg") == 0) {
				++bcnt;
			    } else if (strcmp(s, ".end") == 0) {
				if (--bcnt < 0)
				    fatal(&in->in_Node, "too many .end directives!\n");
			    } else {
				fatal(&in->in_Node, "Unknown directive in command sequence: %s\n", s);
			    }
			    while (s && *s != '\n')
				s = getToken(in, 0);
			    continue;
			}

			/*
			 * Handle command line to execute
			 */

			buf[0] = c;
			if (fgets(buf + 1, sizeof(buf) - 1, in->in_Fi) == NULL) {
			    fatal(&in->in_Node, "Unexpected EOF\n");
			}

			if (buf[0] != '#') {
			    Node *node;
			    char *s = buf;
			    int l = strlen(buf);

			    if (l && buf[l-1] == '\n')
				buf[--l] = 0;
			    while (*s && (*s == ' ' || *s == '\t'))
				++s;

			    node = MakeNode(s, sizeof(Node), 0);
			    node->no_Type = TYPE_EXEC;
			    AddTail(xlist, node);
			}
		    }
		}
		in->in_LastNl = 1;

		/*
		 * Create dependancy list
		 */

		{
		    Node *lnode;

		    for (lnode = GetHead(lhs); lnode; lnode = GetNext(lnode)) {
			Node *rnode;

			if ((rnode = GetHead(rhs)) != NULL)
			    DelNode(rhs, rnode);
			MakeDepend(lnode->no_Name, rnode, ahs, xlist);
		    }
		}
	    }
	}
	pclose(in->in_Fi);
	in->in_Fi = NULL;
    } else {
	fatal(&in->in_Node, "popen failed: %s\n", path);
	/* not reached */
    }
    CurINode = in->in_Parent;
    free(path);
}

/*
 * getToken() - get token from FILE and possibly expand it
 */

const char *
getToken(INode *in, int expand)
{
    Node *node;
    static char Buf[256];
    static char *LBuf;
    char *ptr;

top:
    if (LBuf) {
	free(LBuf);
	LBuf = NULL;
    }

    if ((node = GetHead(&in->in_EList)) != NULL) {
	DelNode(&in->in_EList, node);
	ptr = node->no_Name;
	/* cannot set LBuf here, node's no_Name may be reused XXX */
    } else {
	int c;
	int i = 0;
	int maxi = sizeof(Buf) - 1;
	int bqu = 0;
	int squ = 0;
	int dqu = 0;
	int paren = 0;

	ptr = Buf;

	while ((c = getc(in->in_Fi)) != EOF) {
	    if (c == '#') {
		if (in->in_LastNl) {
		    while ((c = getc(in->in_Fi)) != EOF && c != '\n')
			;
		    ++in->in_Node.no_ILine;
		    if (c == '\n')
			c = getc(in->in_Fi);
		}
	    }

	    if (c == '\n') {
		/*
		 * break on newline
		 */
		if (paren + bqu + squ + dqu != 0)
		    fatal(&in->in_Node, "unterminated string\n");
		if (i == 0) {
		    ptr[i++] = c;
		    c = getc(in->in_Fi);
		    in->in_LastNl = 1;
		}
		break;
	    }
	    in->in_LastNl = 0;

	    if ((c == ' ' || c == '\t') && (paren + bqu + squ + dqu == 0)) {
		/*
		 * skip leading white space
		 */
		if (i == 0)
		    continue;
		/*
		 * break on separator
		 */
		break;
	    }

	    if (c == '`')
		bqu = 1 - bqu;
	    if (c == '\"')
		dqu = 1 - dqu;
	    if (c == '\'' )
		squ = 1 - squ;
	    if (c == '(' )
		++paren;
	    if (c == '{' )
		++paren;
	    if (c == ')' )
		--paren;
	    if (c == '}' )
		--paren;

	    if (c == '\\') {
		c = getc(in->in_Fi);
		if (c == '\n') {
		    /*
		     * eat both
		     */
		    continue;
		}
		ungetc(c, in->in_Fi);
		c = '\\';
	    }

	    /*
	     * store c
	     */

	    if (i == maxi) {
		if (ptr == Buf) {
		    ptr = malloc(i * 2);
		    memmove(ptr, Buf, i);
		} else {
		    ptr = realloc(ptr, i * 2);
		}
		LBuf = ptr;
		maxi = i * 2 - 1;
	    }

	    /*
	     * make : a delimeter as well
	     */

	    if (paren + bqu + squ + dqu == 0) {
		if (c == ':' && i != 0)
		    break;

		if (i && ptr[0] == ':')
		    break;
	    }

	    ptr[i++] = c;
	}
	if (c != EOF)
	    ungetc(c, in->in_Fi);
	ptr[i] = 0;
	if (i == 0)
	    ptr = NULL;
    }

    if (expand && ptr) {
	char *p;
	int use = 0;

	for (p = ptr; *p; ++p) {
	    if ((((p[0] == '$' || p[0] == '%') && 
		(p[1] == '(' || (expand == EXP_FORCE && p[1] == '{')))) || 
		p[0] == '`'
	    ) {
		use = expand;
		break;
	    }
	}
	if (use) {
	    expToken(NULL, in, ptr, use);
	    goto top;
	}
    }

    return(ptr);
}

/*
 * expToken() - expand token, pushing the result on in_EList
 */

const char *
expToken(Depend *dep, INode *in, const char *id, int mode)
{
    static char Buf[1024];
    static char *LBuf;
    char *ptr = Buf;
    int i = 0;
    int bqi = -1;
    int vcount = 0;
    int vstart[MAXVARIND];
    int maxi = sizeof(Buf) - 1;

    if (LBuf) {
	free(LBuf);
	LBuf = NULL;
    }

    while (*id) {
	/*
	 * $(VAR)			immediate expansion
	 * $(VAR:search:replace)
	 *
	 * ${VAR}			delayed expansion
	 * ${VAR:search:replace}
	 */

	if ((id[0] == '$' || id[0] == '%') && 
	    (id[1] == '(' || (mode == EXP_FORCE && id[1] == '{'))
	) {
	    vstart[vcount++] = i;
	}
	if ((id[0] == ')' || (mode == EXP_FORCE && id[0] == '}')) && vcount) {
	    /*
	     * Expand the variable
	     */
	    int vqi = vstart[--vcount];
	    char type = ptr[vqi];
	    char we = 0;
	    char *vname;
	    char *wsrc = NULL;
	    char *wdst = NULL;
	    List *list;
	    Node *node;

	    /*
	     * Terminate the variable expression
	     */

	    ptr[i] = 0;

	    /*
	     * Extract the name, source, and destination wildcards
	     */

	    {
		char *p = ptr + vqi + 2;
		char dummy;

		vname = extcolon(&p, '?', &we);
		if (*p == ':') {
		    wsrc = extcolon(&p, 0, &dummy);
		    if (*p == ':')
			wdst = extcolon(&p, 0, &dummy);
		    else
			wdst = wsrc;
		}
	    }
		
	    /*
	     * Locate the variable.
	     */

	    list = FindVar(type, dep, vname);

#ifdef NOTDEF
	    if (we) {
		char *p = wdst;
		int plen;

		if (list) {
		    p = wsrc;
		}
		i = vqi;
		plen = strlen(p);
		if (i + plen >= maxi) {
		    maxi = (i + plen) * 2;

		    if (ptr == Buf) {
			ptr = malloc(maxi);
			memmove(ptr, Buf, i);
		    } else {
			ptr = realloc(ptr, maxi);
		    }
		}
		memmove(ptr + i, p, plen);
		i += plen;
		++id;
		continue;
	    }
#endif

	    if (list == NULL && we != '?')
		fatal(&in->in_Node, "Variable(%s) not found\n", vname);

	    /*
	     * Wildcard transformation
	     */

	    for ((node= (list)?GetHead(list):NULL); node; node=GetNext(node)) {
		char *p = node->no_Name;
		char *dbuf = NULL;

		if (wsrc != NULL) {
		    if (WildConvert(node->no_Name, &dbuf, wsrc, wdst) < 0) {
			p = "";
		    } else {
			p = dbuf;
		    }
		}

		if (*p) {
		    /*
		     * replace buffer's end (@ i) with string @ vqi and commit.
		     * This means that we vectorize multi-entry variables:
		     *
		     * A=a b c
		     * B=d e f
		     *
		     * $(A)$(B) results in ad ae af bd be bf		REQUEUED
		     * $(A)$(B) results in a b cd e f			IN-PLACE
		     *
		     * if in is NULL, we are doing an in-place replacement  (e.g.
		     * for a shell command line)
		     * 
		     * if in is not NULL, we are doing a requeued replacement.
		     */
		    int plen = strlen(p);

		    if (in == NULL) {
			int l = vqi + plen + 1;

			if (vqi + l >= maxi) {
			    maxi = (vqi + l) * 2;
			    if (ptr == Buf) {
				ptr = malloc(maxi + 1);
				memmove(ptr, Buf, vqi);
			    } else {
				ptr = realloc(ptr, maxi + 1);
			    }
			}
			if (node != list->li_Node.no_Next)
			    ptr[vqi++] = ' ';
			memmove(ptr + vqi, p, plen);
			vqi = vqi + plen;
		    } else {
			char xbuf[1024];
			char *cpy = xbuf;
			Node *n2;
			int idlen = strlen(id + 1);
			int l = vqi + plen + idlen;

			if (l >= sizeof(xbuf)) {
			    cpy = malloc(l + 1);
			}
			memmove(cpy, ptr, vqi);
			memmove(cpy + vqi, p, plen);
			memmove(cpy + vqi + plen, id + 1, idlen);
			cpy[vqi+plen+idlen] = 0;

			n2 = MakeNode(cpy, sizeof(Node), 0);
			AddTail(&in->in_EList, n2);

			if (cpy != xbuf)
			    free(cpy);
		    }
		}
		if (dbuf)
		    free(dbuf);
	    }
	    if (in != NULL) {
		/*
		 * break out, because we have requeued the deepest
		 * variable expansion level.
		 */
		i = 0;
		break;
	    }
	    i = vqi;
	    ++id;
	} 

	if (*id == '`') {
	    if (bqi >= 0) {
		/*
		 * execute and replace, parse the result 
		 * as a string of tokens
		 */
		INode *tin;

		ptr[i] = 0;
		tin = MakeNode(ptr + bqi, sizeof(INode), 0);
		tin->in_Parent = CurINode;
		tin->in_Node.no_INode = tin;
		tin->in_Node.no_ILine = 0;
		InitList(&tin->in_EList);

		if ((tin->in_Fi = popen(ptr + bqi, "r")) != NULL) {
		    const char *s;

		    while ((s = getToken(tin, 1)) != NULL) {
			Node *node;

			if (*s == '\n')
			    continue;

			node = MakeNode(s, sizeof(Node), 0);
			AddTail(&in->in_EList, node);
		    }
		    pclose(tin->in_Fi);
		} else {
		    fatal(&in->in_Node, "popen('%s') failed\n", ptr + bqi);
		}
		i = 0;
		bqi = -1;
		++id;
		continue;
	    }
	    bqi = i + 1;
	    if (i != 0) {
		fatal(&in->in_Node, "I can't deal with embedded backticks!\n");
	    }
	    /* fall through */
	}
	if (i == maxi) {
	    if (ptr == Buf) {
		ptr = malloc(i * 2);
		memmove(ptr, Buf, i);
	    } else {
		ptr = realloc(ptr, i * 2);
	    }
	    maxi = i * 2 - 1;
	}
	ptr[i++] = *id;
	++id;
    }
    ptr[i] = 0;
    if (i && in) {
	Node *node = MakeNode(ptr, sizeof(Node), 0);
	AddTail(&in->in_EList, node);
    }
    if (in == NULL) {
	if (ptr != Buf)
	    LBuf = ptr;
	return(ptr);
    } else {
	if (ptr != Buf)
	    free(ptr);
	return(NULL);
    }
}

char *
extcolon(char **pp, char exdelim, char *pwe)
{
    char *p = *pp;
    char *r;
    int i;
    int quo = 0;

    *pwe = 0;

    if (*p == ':')
	++p;

    if (*p == '\"') {
	++p;
	quo = 1;
    }
    for (i = 0; p[i] && (quo || (p[i] != ':' && p[i] != exdelim)); ++i) {
	if (p[i] == '\"') {
	    quo = 0;
	    break;
	}
    }
    r = malloc(i + 1);
    memmove(r, p, i);
    r[i] = 0;

    if (p[i] == '\"')
	++i;

    if (exdelim && p[i] == exdelim) {
	*pwe = exdelim;
	++i;
    }
    *pp = p + i;

    return(r);
}