219 |
mateuszvis |
1 |
/*
|
|
|
2 |
* This file is part of FDNPKG.
|
|
|
3 |
*
|
|
|
4 |
* Loads the list of repositories from the config file specified in %FDNPKG%.
|
|
|
5 |
* Returns the amount of repositories found (and loaded) on success, or -1 on failure.
|
|
|
6 |
*
|
|
|
7 |
* Copyright (C) 2012-2016 Mateusz Viste
|
|
|
8 |
*/
|
|
|
9 |
|
|
|
10 |
#include <stdio.h> /* printf(), fclose(), fopen()... */
|
|
|
11 |
#include <string.h> /* strcasecmp() */
|
|
|
12 |
#include <stdlib.h> /* malloc(), free() */
|
|
|
13 |
|
227 |
mateuszvis |
14 |
#include "fdnpkg.h" /* PKGINST_SKIPLINKS... */
|
219 |
mateuszvis |
15 |
#include "helpers.h" /* slash2backslash(), removeDoubleBackslashes()... */
|
|
|
16 |
#include "kprintf.h"
|
|
|
17 |
#include "loadconf.h"
|
|
|
18 |
#include "parsecmd.h"
|
|
|
19 |
#include "version.h"
|
|
|
20 |
|
|
|
21 |
|
225 |
mateuszvis |
22 |
void freeconf(struct customdirs **dirlist) {
|
219 |
mateuszvis |
23 |
struct customdirs *curpos;
|
|
|
24 |
/* free the linked list of custom dirs */
|
|
|
25 |
while (*dirlist != NULL) {
|
|
|
26 |
curpos = *dirlist;
|
|
|
27 |
if (curpos->name != NULL) free(curpos->name);
|
|
|
28 |
if (curpos->location != NULL) free(curpos->location);
|
|
|
29 |
*dirlist = (*dirlist)->next;
|
|
|
30 |
free(curpos);
|
|
|
31 |
}
|
|
|
32 |
*dirlist = NULL;
|
|
|
33 |
}
|
|
|
34 |
|
|
|
35 |
|
|
|
36 |
static int checkfordoubledirlist(struct customdirs *dirlist) {
|
|
|
37 |
struct customdirs *curpos;
|
|
|
38 |
for (; dirlist != NULL; dirlist = dirlist->next) {
|
|
|
39 |
for (curpos = dirlist->next; curpos != NULL; curpos = curpos->next) {
|
|
|
40 |
if (strcasecmp(curpos->name, dirlist->name) == 0) {
|
|
|
41 |
kitten_printf(7, 0, "Error: custom dir '%s' is listed twice!", curpos->name);
|
|
|
42 |
puts("");
|
|
|
43 |
return(-1);
|
|
|
44 |
}
|
|
|
45 |
}
|
|
|
46 |
}
|
|
|
47 |
return(0);
|
|
|
48 |
}
|
|
|
49 |
|
|
|
50 |
|
|
|
51 |
/* validates dirlist entries: check that they are absolute paths and are not using restricted names */
|
|
|
52 |
static int validatedirlist(struct customdirs *dirlist) {
|
|
|
53 |
for (; dirlist != NULL; dirlist = dirlist->next) {
|
|
|
54 |
/* the location must be at least 3 characters long to be a valid absolute path (like 'c:\')*/
|
|
|
55 |
if (strlen(dirlist->location) < 3) {
|
|
|
56 |
kitten_printf(7, 15, "Error: custom dir '%s' is not a valid absolute path!", dirlist->name);
|
|
|
57 |
puts("");
|
|
|
58 |
return(-1);
|
|
|
59 |
}
|
|
|
60 |
/* is it a valid absolute path? should start with [a..Z]:\ */
|
|
|
61 |
if ((dirlist->location[1] != ':') ||
|
|
|
62 |
((dirlist->location[2] != '/') && (dirlist->location[2] != '\\')) ||
|
|
|
63 |
(((dirlist->location[0] < 'a') || (dirlist->location[0] > 'z')) && ((dirlist->location[0] < 'A') || (dirlist->location[0] > 'Z')))) {
|
|
|
64 |
kitten_printf(7, 15, "Error: custom dir '%s' is not a valid absolute path!", dirlist->name);
|
|
|
65 |
puts("");
|
|
|
66 |
return(-1);
|
|
|
67 |
}
|
|
|
68 |
/* check for forbidden names */
|
|
|
69 |
if ((strcasecmp(dirlist->name, "appinfo") == 0) ||
|
|
|
70 |
(strcasecmp(dirlist->name, "bin") == 0) ||
|
|
|
71 |
(strcasecmp(dirlist->name, "doc") == 0) ||
|
|
|
72 |
(strcasecmp(dirlist->name, "help") == 0) ||
|
|
|
73 |
(strcasecmp(dirlist->name, "nls") == 0) ||
|
|
|
74 |
(strcasecmp(dirlist->name, "packages") == 0)) {
|
|
|
75 |
kitten_printf(7, 16, "Error: custom dir '%s' is a reserved name!", dirlist->name);
|
|
|
76 |
puts("");
|
|
|
77 |
return(-1);
|
|
|
78 |
}
|
|
|
79 |
}
|
|
|
80 |
return(0);
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
|
|
|
84 |
/* add (and allocates) a new custom dir entry to dirlist. Returns 0 on success,
|
|
|
85 |
or non-zero on failure (failures happen on out of memory events). */
|
|
|
86 |
static int addnewdir(struct customdirs **dirlist, char *name, char *location) {
|
|
|
87 |
struct customdirs *newentry;
|
|
|
88 |
newentry = malloc(sizeof(struct customdirs));
|
|
|
89 |
if (newentry == NULL) return(-1);
|
|
|
90 |
newentry->name = malloc(strlen(name) + 1);
|
|
|
91 |
if (newentry->name == NULL) {
|
|
|
92 |
free(newentry);
|
|
|
93 |
return(-1);
|
|
|
94 |
}
|
|
|
95 |
newentry->location = malloc(strlen(location) + 1);
|
|
|
96 |
if (newentry->location == NULL) {
|
|
|
97 |
free(newentry->name);
|
|
|
98 |
free(newentry);
|
|
|
99 |
return(-1);
|
|
|
100 |
}
|
|
|
101 |
strcpy(newentry->name, name);
|
|
|
102 |
strcpy(newentry->location, location);
|
|
|
103 |
newentry->next = *dirlist;
|
|
|
104 |
*dirlist = newentry;
|
|
|
105 |
return(0);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
|
237 |
mateuszvis |
109 |
int loadconf(const char *dosdir, struct customdirs **dirlist, int *flags) {
|
219 |
mateuszvis |
110 |
int bytebuff, parserstate = 0;
|
|
|
111 |
FILE *fd;
|
|
|
112 |
#define maxtok 16
|
|
|
113 |
char token[maxtok];
|
237 |
mateuszvis |
114 |
#define maxval 256
|
219 |
mateuszvis |
115 |
char value[maxval];
|
237 |
mateuszvis |
116 |
char cfgfile[256];
|
219 |
mateuszvis |
117 |
int curtok = 0, curval = 0, nline = 1;
|
|
|
118 |
|
237 |
mateuszvis |
119 |
snprintf(cfgfile, sizeof(cfgfile), "%s\\cfg\\pkg.cfg", dosdir);
|
219 |
mateuszvis |
120 |
fd = fopen(cfgfile, "r");
|
|
|
121 |
if (fd == NULL) {
|
237 |
mateuszvis |
122 |
kitten_printf(7, 1, "Error: Could not open config file (%s)!", cfgfile);
|
219 |
mateuszvis |
123 |
puts("");
|
|
|
124 |
return(-1);
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
/* read the config file line by line */
|
|
|
128 |
do {
|
|
|
129 |
bytebuff = fgetc(fd);
|
|
|
130 |
if (bytebuff != '\r') {
|
|
|
131 |
switch (parserstate) {
|
|
|
132 |
case 0: /* Looking for start of line */
|
|
|
133 |
if ((bytebuff == EOF) || (bytebuff == ' ') || (bytebuff == '\t')) break;
|
|
|
134 |
if (bytebuff == '\n') {
|
|
|
135 |
nline += 1;
|
|
|
136 |
break;
|
|
|
137 |
}
|
|
|
138 |
if (bytebuff == '#') {
|
|
|
139 |
parserstate = 9;
|
|
|
140 |
} else {
|
|
|
141 |
token[0] = bytebuff;
|
|
|
142 |
curtok = 1;
|
|
|
143 |
parserstate = 1;
|
|
|
144 |
}
|
|
|
145 |
break;
|
|
|
146 |
case 1: /* Looking for token end */
|
|
|
147 |
if ((bytebuff == EOF) || (bytebuff == '\n')) {
|
|
|
148 |
kitten_printf(7, 2, "Warning: token without value on line #%d", nline);
|
|
|
149 |
puts("");
|
|
|
150 |
if (bytebuff == '\n') nline += 1;
|
|
|
151 |
parserstate = 0;
|
|
|
152 |
break;
|
|
|
153 |
}
|
|
|
154 |
if ((bytebuff == ' ') || (bytebuff == '\t')) {
|
|
|
155 |
token[curtok] = 0;
|
|
|
156 |
parserstate = 2;
|
|
|
157 |
} else {
|
|
|
158 |
token[curtok] = bytebuff;
|
|
|
159 |
curtok += 1;
|
|
|
160 |
if (curtok >= maxtok) {
|
|
|
161 |
parserstate = 9; /* ignore the line */
|
|
|
162 |
kitten_printf(7, 3, "Warning: Config file token overflow on line #%d", nline);
|
|
|
163 |
puts("");
|
|
|
164 |
}
|
|
|
165 |
}
|
|
|
166 |
break;
|
|
|
167 |
case 2: /* Looking for value start */
|
|
|
168 |
if ((bytebuff == EOF) || (bytebuff == '\n')) {
|
|
|
169 |
kitten_printf(7, 4, "Warning: token with empty value on line #%d", nline);
|
|
|
170 |
puts("");
|
|
|
171 |
if (bytebuff == '\n') nline += 1;
|
|
|
172 |
parserstate = 0;
|
|
|
173 |
break;
|
|
|
174 |
}
|
|
|
175 |
if ((bytebuff != ' ') && (bytebuff != '\t')) {
|
|
|
176 |
value[0] = bytebuff;
|
|
|
177 |
curval = 1;
|
|
|
178 |
parserstate = 3;
|
|
|
179 |
}
|
|
|
180 |
break;
|
|
|
181 |
case 3: /* Looking for value end */
|
|
|
182 |
if ((bytebuff == EOF) || (bytebuff == '\n')) {
|
|
|
183 |
parserstate = 0;
|
|
|
184 |
value[curval] = 0;
|
|
|
185 |
if ((value[curval - 1] == ' ') || (value[curval - 1] == '\t')) {
|
|
|
186 |
kitten_printf(7, 5, "Warning: trailing white-space(s) after value on line #%d", nline);
|
|
|
187 |
puts("");
|
|
|
188 |
while ((value[curval - 1] == ' ') || (value[curval - 1] == '\t')) value[--curval] = 0;
|
|
|
189 |
}
|
|
|
190 |
/* Interpret the token/value pair now! */
|
|
|
191 |
/* printf("token='%s' ; value = '%s'\n", token, value); */
|
225 |
mateuszvis |
192 |
if (strcasecmp(token, "SKIPLINKS") == 0) {
|
219 |
mateuszvis |
193 |
int tmpint = atoi(value); /* must be 0/1 */
|
|
|
194 |
if (tmpint == 0) {
|
|
|
195 |
/* do nothing */
|
|
|
196 |
} else if (tmpint == 1) {
|
|
|
197 |
*flags |= PKGINST_SKIPLINKS;
|
|
|
198 |
} else {
|
|
|
199 |
kitten_printf(7, 10, "Warning: Ignored an illegal '%s' value at line #%d", "skiplinks", nline);
|
|
|
200 |
puts("");
|
|
|
201 |
}
|
|
|
202 |
} else if (strcasecmp(token, "DIR") == 0) { /* custom repository entry */
|
|
|
203 |
char *argv[2], *evar, *evar_content, *realLocation;
|
|
|
204 |
#define realLocation_len 512
|
|
|
205 |
int x, y;
|
|
|
206 |
if (parsecmd(value, argv, 2) != 2) {
|
|
|
207 |
kitten_printf(7, 11, "Warning: Invalid 'DIR' directive found at line #%d", nline);
|
|
|
208 |
puts("");
|
|
|
209 |
}
|
|
|
210 |
realLocation = malloc(realLocation_len);
|
|
|
211 |
if (realLocation == NULL) {
|
|
|
212 |
kitten_printf(2, 14, "Out of memory! (%s)", "malloc realLocation");
|
|
|
213 |
puts("");
|
225 |
mateuszvis |
214 |
freeconf(dirlist);
|
219 |
mateuszvis |
215 |
fclose(fd);
|
|
|
216 |
return(-1);
|
|
|
217 |
}
|
|
|
218 |
realLocation[0] = 0; /* force it to be empty, since we might use strcat() on this later! */
|
|
|
219 |
/* resolve possible env variables */
|
|
|
220 |
evar = NULL;
|
|
|
221 |
y = 0;
|
|
|
222 |
for (x = 0; argv[1][x] != 0; x++) {
|
|
|
223 |
if (evar == NULL) { /* searching for % and copying */
|
|
|
224 |
if (argv[1][x] == '%') {
|
|
|
225 |
evar = &argv[1][x+1];
|
|
|
226 |
} else {
|
|
|
227 |
if (y + 1 > realLocation_len) {
|
|
|
228 |
kitten_printf(7, 12, "Error: DIR path too long at line #%d", nline);
|
|
|
229 |
puts("");
|
225 |
mateuszvis |
230 |
freeconf(dirlist);
|
219 |
mateuszvis |
231 |
free(realLocation);
|
|
|
232 |
fclose(fd);
|
|
|
233 |
return(-1);
|
|
|
234 |
}
|
|
|
235 |
realLocation[y] = argv[1][x]; /* copy over */
|
|
|
236 |
y++;
|
|
|
237 |
realLocation[y] = 0; /* make sure to terminate the string at any time */
|
|
|
238 |
}
|
|
|
239 |
} else { /* reading a % variable */
|
|
|
240 |
if (argv[1][x] == '%') {
|
|
|
241 |
argv[1][x] = 0;
|
|
|
242 |
evar_content = getenv(evar);
|
|
|
243 |
if (evar_content == NULL) {
|
|
|
244 |
kitten_printf(7, 13, "Error: Found inexisting environnement variable '%s' at line #%d", evar, nline);
|
|
|
245 |
puts("");
|
225 |
mateuszvis |
246 |
freeconf(dirlist);
|
219 |
mateuszvis |
247 |
free(realLocation);
|
|
|
248 |
fclose(fd);
|
|
|
249 |
return(-1);
|
|
|
250 |
}
|
|
|
251 |
if (strlen(evar_content) + y + 1 > realLocation_len) {
|
|
|
252 |
kitten_printf(7, 12, "Error: DIR path too long at line #%d", nline);
|
|
|
253 |
puts("");
|
225 |
mateuszvis |
254 |
freeconf(dirlist);
|
219 |
mateuszvis |
255 |
free(realLocation);
|
|
|
256 |
fclose(fd);
|
|
|
257 |
return(-1);
|
|
|
258 |
}
|
|
|
259 |
strcat(realLocation, evar_content);
|
|
|
260 |
y += strlen(evar_content);
|
|
|
261 |
evar = NULL;
|
|
|
262 |
}
|
|
|
263 |
}
|
|
|
264 |
}
|
|
|
265 |
/* add the entry to the list */
|
|
|
266 |
slash2backslash(realLocation);
|
|
|
267 |
removeDoubleBackslashes(realLocation);
|
|
|
268 |
if (realLocation[strlen(realLocation) - 1] != '\\') strcat(realLocation, "\\"); /* make sure to end dirs with a backslash */
|
|
|
269 |
if (addnewdir(dirlist, argv[0], realLocation) != 0) {
|
|
|
270 |
kitten_printf(2, 14, "Out of memory! (%s)", "addnewdir");
|
|
|
271 |
puts("");
|
225 |
mateuszvis |
272 |
freeconf(dirlist);
|
219 |
mateuszvis |
273 |
free(realLocation);
|
|
|
274 |
fclose(fd);
|
|
|
275 |
return(-1);
|
|
|
276 |
}
|
|
|
277 |
free(realLocation);
|
|
|
278 |
} else { /* unknown token */
|
|
|
279 |
kitten_printf(7, 8, "Warning: Unknown token '%s' at line #%d", token, nline);
|
|
|
280 |
puts("");
|
|
|
281 |
}
|
|
|
282 |
/* interpretation done */
|
|
|
283 |
if (bytebuff == '\n') nline += 1;
|
|
|
284 |
} else {
|
|
|
285 |
value[curval] = bytebuff;
|
|
|
286 |
curval += 1;
|
|
|
287 |
if ((curval + 1) >= maxval) {
|
|
|
288 |
parserstate = 9; /* ignore the line */
|
|
|
289 |
kitten_printf(7, 9, "Warning: Config file value overflow on line #%d", nline);
|
|
|
290 |
puts("");
|
|
|
291 |
}
|
|
|
292 |
}
|
|
|
293 |
break;
|
|
|
294 |
case 9: /* Got comment, ignoring the rest of line */
|
|
|
295 |
if (bytebuff == EOF) break;
|
|
|
296 |
if (bytebuff == '\n') {
|
|
|
297 |
nline += 1;
|
|
|
298 |
parserstate = 0;
|
|
|
299 |
}
|
|
|
300 |
break;
|
|
|
301 |
}
|
|
|
302 |
}
|
|
|
303 |
} while (bytebuff != EOF);
|
|
|
304 |
fclose(fd);
|
|
|
305 |
|
225 |
mateuszvis |
306 |
/* perform some validations */
|
219 |
mateuszvis |
307 |
if (checkfordoubledirlist(*dirlist) != 0) return(-1);
|
|
|
308 |
if (validatedirlist(*dirlist) != 0) return(-1);
|
|
|
309 |
|
225 |
mateuszvis |
310 |
return(0);
|
219 |
mateuszvis |
311 |
}
|