File: conf.c

package info (click to toggle)
ondir 0.2.3%2Bgit0.55279f03-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, stretch
  • size: 188 kB
  • ctags: 53
  • sloc: ansic: 450; makefile: 70; sh: 13; csh: 2
file content (227 lines) | stat: -rw-r--r-- 4,828 bytes parent folder | download
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
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include "conf.h"

static int count_ch(const char *str, int ch) {
int count = 0;

	for (; str && *str; ++str)
		if (*str == ch)
			++count;
	return count;
}

struct odpath_t *load_conf(const char *file, struct odpath_t *root) {
char *buffer = malloc(2048), *section = malloc(16384);
struct odpath_t *path = root, *current = NULL;
FILE *fp;
int line = 0, sec_size = 0;

	if (path) while (path->next) path = path->next;

	if (!(fp = fopen(file, "r"))) {
		free(buffer);
		free(section);
		return root;
	}

	while (fgets(buffer, 2048, fp)) {
	int line_size = strlen(buffer);

		++line;
		/* empty lines and comments are ignored */
		if (!strchr("#\n\r", buffer[0]) && line_size) {
			/* continuation... */
			if (isspace(buffer[0])) {
			char *sol = buffer;

				if (!current) {
					error("%s:%i: data outside section", file, line);
					continue;
				}
				if (sec_size + line_size >= MAX_SECTION_SIZE)
					fatal("section is greater than MAX_SECTION_SIZE (%i)", MAX_SECTION_SIZE);
				/* strip leading spaces */
				while (isspace(*sol)) ++sol;
				strcat(section, sol);
			} else {
			const char *home = getenv("HOME");
			char *tok;
			char **paths;
			int npaths, pathi;

				/* new section... */
				if (current) {
					current->content = strdup(section);
					if (path) {
						path->next = current;
						path = current;
					} else {
						path = current;
						if (!root) root = path;
					}
				}
				if (!(current = calloc(1, sizeof(struct odpath_t))))
					fatal("malloc failed");

				memset(section, 0, MAX_SECTION_SIZE);

				/* read stuff */
				tok = strtok(buffer, " \t");
				if (!tok)
					fatal("%s:%i: unexpected end of line", file, line);

				if (!strcmp(tok, "final")) {
					current->final = 1;
					tok = strtok(NULL, " \t");
				}

				if (strcmp(tok, "enter") && strcmp(tok, "leave"))
					fatal("%s:%i: line should start with final, enter or leave", file, line);

				if (!strcmp(tok, "enter"))
					current->type = PT_ENTER;
				else
				if (!strcmp(tok, "leave"))
					current->type = PT_LEAVE;

				/* count :'s */
				tok = strtok(NULL, "\n");
				npaths = count_ch(tok, ':') + 1;

				paths = calloc(npaths, sizeof(char*));

				for (pathi = 0, tok = strtok(tok, ":"); pathi < npaths; ++pathi, tok = strtok(NULL, ":")) {
					if (home && tok[0] == '~') {
					char tmppath[strlen(tok) + strlen(home) + 1];

						strcpy(tmppath, home);
						strcat(tmppath, tok + 1);
						paths[pathi] = expand_envars(tmppath);
					} else
						paths[pathi] = expand_envars(tok);
				}
				current->paths = (const char**)paths;
				current->npaths = npaths;
			}
		}
	}
	if (current) {
		current->content = strdup(section);
		if (path) {
			path->next = current;
			path = current;
		} else {
			path = current;
			if (!root) root = path;
		}
	}
	fclose(fp);
	free(section);
	free(buffer);
	return root;
}

void fatal(const char *fmt, ...) {
va_list args;

	fprintf(stderr, "fatal: ");
	va_start(args, fmt);
	vfprintf(stderr, fmt, args);
	fprintf(stderr, "\n");
	va_end(args);
	exit(1);
}

void error(const char *fmt, ...) {
va_list args;

	fprintf(stderr, "error: ");
	va_start(args, fmt);
	vfprintf(stderr, fmt, args);
	fprintf(stderr, "\n");
	va_end(args);
}

void warning(const char *fmt, ...) {
va_list args;

	printf("warning: ");
	va_start(args, fmt);
	vprintf(fmt, args);
	va_end(args);
}

void info(const char *fmt, ...) {
va_list args;

	printf("ondir: ");
	va_start(args, fmt);
	vprintf(fmt, args);
	va_end(args);
}

/* Expand internal ONDIR environment variables */
char *expand_envars(const char *in) {
int inlen = strlen(in), outmax = inlen * 2, outlen = 0;
char *out = calloc(1, outmax);

	while (*in) {
	const char *mark = in;

		if (*in == '$') {
		int bracketed = 0, varlen = 0;
		const char *varstart;

			++in;
			if (*in == '{') { ++in; bracketed = 1; }
			varstart = in;
			while (in[varlen] && (isalnum(in[varlen]) || in[varlen] == '_'))
				++varlen;
			if (bracketed && in[varlen] != '}')
				in = mark;
			else {
			char var[varlen + 1];
			const char *val;

				strncpy(var, varstart, varlen);
				var[varlen] = 0;

				if (strcmp(var, "ONDIRWD") && (var[0] < '0' || var[0] > '9')) {
					in = mark;
				} else {
					val = getenv(var);
					if (val) {
					int vallen = strlen(val);

						if (outlen + vallen >= outmax) {
							outmax = outmax * 2 + vallen;
							out = realloc(out, outmax);
						}
						strcat(out, val);
						outlen += vallen;
						in = mark + varlen + bracketed * 2 + 1;
						continue;
					} else {
						in = mark;
						out[outlen] = 0;
					}
				}
			}
		}

		/* Add another character */
		if (outlen + 1 >= outmax) {
			outmax *= 2;
			out = realloc(out, outmax);
		}

		out[outlen++] = *in++;
		out[outlen] = 0;
	}
	out[outlen] = 0;
	return out;
}