summaryrefslogtreecommitdiffstats
path: root/lib/libmytinfo/buildpath.c
blob: cecba422e2c8958bfb462a5f5963a27559c6ccda (plain)
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
/*
 * buildpath.c
 *
 * By Ross Ridge
 * Public Domain
 * 92/02/01 07:29:42
 *
 * _buildpath builds a list of file names and terminal descriprions extracted
 * from its arguments. It returns a pointer to a structure that is used by
 * other routines as the list of file names to search for terminal
 * descriptions.  It is passed a variable number of arguments consisting
 * of file name and type pairs. The file name can actually be a list of
 * file names seperated by spaces and any environment variables specified
 * by a dollar sign ($) followed by its name are substituted in. A type
 * of 1 indicates that the file name may actually be termcap description
 * and a type of 2 indicates it may be a terminfo description. A type of 0
 * indicates that the file name can only be a file name (or list of them).
 *
 */

#include "defs.h"

#include <ctype.h>

#ifdef USE_SCCS_IDS
static const char SCCSid[] = "@(#) mytinfo buildpath.c 3.2 92/02/01 public domain, By Ross Ridge";
#endif

/* more memory is allocated for file names every HUNK file names */
#define HUNK 32

/* characters that seperate file names in a list */
#define SEPERATORS " :"

static struct term_path *path = NULL;	/* the list of files */
static int files = 0;			/* # of files in the list */
static int size = 0;			/* # of files there is space for */

/* add a file name, type pair to the list */
static int
addfile(file, type)
char *file;
int type; {
	int l;
	char *s;

	if (file == NULL) {
		if (type != -1)
			return -1;
	} else if (file[0] == '\0')
		return -1;

#ifdef DEBUG
	if (file != NULL)
		printf("addfile: %s\n", file);
#endif

	if (files >= size) {
		size += HUNK;
		if (path == NULL)
			path = (struct term_path *)
				malloc(size * sizeof(struct term_path));
		else
			path = (struct term_path *)
				realloc((anyptr) path,
					size * sizeof(struct term_path));
		if (path == NULL)
			return 0;
	}
	if (file == NULL) {
		path[files].file = file;
	} else {
		l = strlen(file) + 1;
		s = (char *) malloc(l * sizeof(char));
		if (s == NULL)
			return 0;
		path[files].file = strcpy(s, file);
	}
	path[files].type = type;

	return ++files;
}

/* deallocate space used by the path list */
void
_delpath(ppath)
struct term_path *ppath; {
	struct term_path *p;

	p = ppath;
	while(p->file != NULL) {
		free((anyptr)p->file);
		p++;
	}

	free((anyptr)ppath);
}

/* build a list of paths. see above */
#ifdef lint
/*VARARGS2*/
struct term_path *
_buildpath(file, type)
char *file;
int type;
#else
#ifdef USE_STDARG
#ifdef USE_PROTOTYPES
struct term_path *_buildpath(char *file, int type, ...)
#else
struct term_path *_buildpath(file, type)
char *file;
int type;
#endif /* USE_PROTOTYPES */
#else /* USE_STDARG */
struct term_path *_buildpath(va_alist)
va_dcl
#endif /* USE_STDARG */
#endif /* lint */
{
#ifndef lint
#ifndef USE_STDARG
	char *file;
	int type;
#endif
#endif
	va_list ap;
	register char *s, *d, *e;
	char *p;
	char line[MAX_BUF+1];
	char name[MAX_NAME+1];
	int i,j;

	size = 0;
	files = 0;
	path = NULL;

#ifdef lint
	ap = NULL;
#else
#ifdef USE_STDARG
	va_start(ap, type);
#else
	va_start(ap);
	file = va_arg(ap, char *);
	type = va_arg(ap, int);
#endif
#endif

	while (type >= 0 && type <= 2) {
		s = file;
		d = line;
		i = 0;
		while(*s != '\0') {
			if (*s == '$') {
				s++;
				j = 0;
				while(*s != '\0' && (*s == '_' || isalnum(*s)))
					if (j < MAX_NAME) {
						name[j] = *s++;
						j++;
					} else
						break;
				name[j] = '\0';
				e = getenv(name);
				if (e != NULL) {
					while(*e != '\0') {
						if (i < MAX_BUF) {
							*d++ = *e++;
							i++;
						} else
							break;
					}
				} else if (*s == '/')
					s++;
			} else {
				if (i < MAX_BUF) {
					*d++ = *s++;
					i++;
				} else
					break;
			}
		}
		*d = '\0';
		if (type == 0 || line[0] == '/') {
			p = line;
			while ((s = strsep(&p, SEPERATORS)) != NULL && *s == '\0')
				;
			while(s != NULL) {
				if (addfile(s, 0) == 0)
					return NULL;
				while ((s = strsep(&p, SEPERATORS)) != NULL && *s == '\0')
					;
			}
		} else
			if (addfile(line, type) == 0)
				return NULL;
		file = va_arg(ap, char *);
		type = va_arg(ap, int);
	}
	addfile(NULL, -1);
	return path;
}
OpenPOWER on IntegriCloud