File: include.c

package info (click to toggle)
wily 0.13.33-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 1,500 kB
  • ctags: 1,720
  • sloc: ansic: 12,830; sh: 245; makefile: 188
file content (93 lines) | stat: -rw-r--r-- 1,781 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
#include "wily.h"

static char *	pathfind (const char *paths, const char *file);
static Bool	is_includebrackets(char left, char right);

/*
 * The user has selected 'r' in 'v'.
 * If possible, open an appropriate include file, return a View
 * representing its body.
 *
 * If no include file is appropriate, return 0.
 */
View*
openinclude(View *v, Range r) {
	Range	expanded;
	Path		buf, pbuf;
	int		len;
	Text		*t;
	char		*s;
	
	t = view_text(v);
	
	expanded = text_expand(t, r, notinclude);
	len = RLEN(expanded);
	if( len > (MAXPATH*UTFmax) || len < 2)
		return false;
	len = text_copyutf(t, expanded, buf);
	
	if (!is_includebrackets(buf[0], buf[len-1]))
		return false;
	
	buf[len-1] = 0;
	s = pathfind(getenv("INCLUDES"),  buf+1);
	if(!s) {
		sprintf(pbuf, "/usr/include/%s", buf+1);
		s = pbuf;
	}
	return openlabel(s, false);
}

/**********************************************************
	static functions
**********************************************************/

static Bool
is_includebrackets(char left, char right) {
	return (left == '"' && right == '"') ||
		(left == '<' && right == '>');
}

static const char *
nextstr (const char *p, const char *c, int *n){
	int i;

	if (!p || !*p)
		return 0;

	*n = i = strcspn (p, c);	/* XXX - utf */
	if (p[i])
		i += 1;					/* strspn (p+i, c); ? */
	return p+i;
}

static char *
pathfind (const char *paths, const char *file)
{
	const char *p,*ptmp;
	int flen;
	int plen;

	if (!paths || !file)
		return 0;

	flen = strlen(file);
	p = paths;
	while((ptmp = nextstr(p, ":", &plen))!= 0) {
		int fd;
		char *tmp = malloc(plen+1+flen+1);

		if (tmp) {
			sprintf(tmp, "%.*s/%s", plen, p, file);
			if ((fd = open(tmp, 0)) < 0) {
				free(tmp);
			} else {
				close(fd);
				return tmp;
			}
		}
		p = ptmp;
	}
	return 0;
}