Debian Codegraph @

conf.c

raw symbols SCIP
1 #include <ctype.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <stdarg.h>
6 #include "conf.h"
7
8 static int count_ch(const char *str, int ch) {
9 int count = 0;
10
11 for (; str && *str; ++str)
12 if (*str == ch)
13 ++count;
14 return count;
15 }
16
17 struct odpath_t *load_conf(const char *file, struct odpath_t *root) {
18 char *buffer = malloc(2048), *section = malloc(16384);
19 struct odpath_t *path = root, *current = NULL;
20 FILE *fp;
21 int line = 0, sec_size = 0;
22
23 if (path) while (path->next) path = path->next;
24
25 if (!(fp = fopen(file, "r"))) {
26 free(buffer);
27 free(section);
28 return root;
29 }
30
31 while (fgets(buffer, 2048, fp)) {
32 int line_size = strlen(buffer);
33
34 ++line;
35 /* empty lines and comments are ignored */
36 if (!strchr("#\n\r", buffer[0]) && line_size) {
37 /* continuation... */
38 if (isspace(buffer[0])) {
39 char *sol = buffer;
40
41 if (!current) {
42 error("%s:%i: data outside section", file, line);
43 continue;
44 }
45 if (sec_size + line_size >= MAX_SECTION_SIZE)
46 fatal("section is greater than MAX_SECTION_SIZE (%i)", MAX_SECTION_SIZE);
47 /* strip leading spaces */
48 while (isspace(*sol)) ++sol;
49 strcat(section, sol);
50 } else {
51 const char *home = getenv("HOME");
52 char *tok;
53 char **paths;
54 int npaths, pathi;
55
56 /* new section... */
57 if (current) {
58 current->content = strdup(section);
59 if (path) {
60 path->next = current;
61 path = current;
62 } else {
63 path = current;
64 if (!root) root = path;
65 }
66 }
67 if (!(current = calloc(1, sizeof(struct odpath_t))))
68 fatal("malloc failed");
69
70 memset(section, 0, MAX_SECTION_SIZE);
71
72 /* read stuff */
73 tok = strtok(buffer, " \t");
74 if (!tok)
75 fatal("%s:%i: unexpected end of line", file, line);
76
77 if (!strcmp(tok, "final")) {
78 current->final = 1;
79 tok = strtok(NULL, " \t");
80 }
81
82 if (strcmp(tok, "enter") && strcmp(tok, "leave"))
83 fatal("%s:%i: line should start with final, enter or leave", file, line);
84
85 if (!strcmp(tok, "enter"))
86 current->type = PT_ENTER;
87 else
88 if (!strcmp(tok, "leave"))
89 current->type = PT_LEAVE;
90
91 /* count :'s */
92 tok = strtok(NULL, "\n");
93 npaths = count_ch(tok, ':') + 1;
94
95 paths = calloc(npaths, sizeof(char*));
96
97 for (pathi = 0, tok = strtok(tok, ":"); pathi < npaths; ++pathi, tok = strtok(NULL, ":")) {
98 if (home && tok[0] == '~') {
99 char tmppath[strlen(tok) + strlen(home) + 1];
100
101 strcpy(tmppath, home);
102 strcat(tmppath, tok + 1);
103 paths[pathi] = expand_envars(tmppath);
104 } else
105 paths[pathi] = expand_envars(tok);
106 }
107 current->paths = (const char**)paths;
108 current->npaths = npaths;
109 }
110 }
111 }
112 if (current) {
113 current->content = strdup(section);
114 if (path) {
115 path->next = current;
116 path = current;
117 } else {
118 path = current;
119 if (!root) root = path;
120 }
121 }
122 fclose(fp);
123 free(section);
124 free(buffer);
125 return root;
126 }
127
128 void fatal(const char *fmt, ...) {
129 va_list args;
130
131 fprintf(stderr, "fatal: ");
132 va_start(args, fmt);
133 vfprintf(stderr, fmt, args);
134 fprintf(stderr, "\n");
135 va_end(args);
136 exit(1);
137 }
138
139 void error(const char *fmt, ...) {
140 va_list args;
141
142 fprintf(stderr, "error: ");
143 va_start(args, fmt);
144 vfprintf(stderr, fmt, args);
145 fprintf(stderr, "\n");
146 va_end(args);
147 }
148
149 void warning(const char *fmt, ...) {
150 va_list args;
151
152 printf("warning: ");
153 va_start(args, fmt);
154 vprintf(fmt, args);
155 va_end(args);
156 }
157
158 void info(const char *fmt, ...) {
159 va_list args;
160
161 printf("ondir: ");
162 va_start(args, fmt);
163 vprintf(fmt, args);
164 va_end(args);
165 }
166
167 /* Expand internal ONDIR environment variables */
168 char *expand_envars(const char *in) {
169 int inlen = strlen(in), outmax = inlen * 2, outlen = 0;
170 char *out = calloc(1, outmax);
171
172 while (*in) {
173 const char *mark = in;
174
175 if (*in == '$') {
176 int bracketed = 0, varlen = 0;
177 const char *varstart;
178
179 ++in;
180 if (*in == '{') { ++in; bracketed = 1; }
181 varstart = in;
182 while (in[varlen] && (isalnum(in[varlen]) || in[varlen] == '_'))
183 ++varlen;
184 if (bracketed && in[varlen] != '}')
185 in = mark;
186 else {
187 char var[varlen + 1];
188 const char *val;
189
190 strncpy(var, varstart, varlen);
191 var[varlen] = 0;
192
193 if (strcmp(var, "ONDIRWD") && (var[0] < '0' || var[0] > '9')) {
194 in = mark;
195 } else {
196 val = getenv(var);
197 if (val) {
198 int vallen = strlen(val);
199
200 if (outlen + vallen >= outmax) {
201 outmax = outmax * 2 + vallen;
202 out = realloc(out, outmax);
203 }
204 strcat(out, val);
205 outlen += vallen;
206 in = mark + varlen + bracketed * 2 + 1;
207 continue;
208 } else {
209 in = mark;
210 out[outlen] = 0;
211 }
212 }
213 }
214 }
215
216 /* Add another character */
217 if (outlen + 1 >= outmax) {
218 outmax *= 2;
219 out = realloc(out, outmax);
220 }
221
222 out[outlen++] = *in++;
223 out[outlen] = 0;
224 }
225 out[outlen] = 0;
226 return out;
227 }