Subversion Repositories SvarDOS

Rev

Rev 219 | Rev 227 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 219 Rev 225
Line 18... Line 18...
18
#include "loadconf.h"
18
#include "loadconf.h"
19
#include "parsecmd.h"
19
#include "parsecmd.h"
20
#include "version.h"
20
#include "version.h"
21
 
21
 
22
 
22
 
23
void freeconf(char **repolist, int repscount, struct customdirs **dirlist) {
23
void freeconf(struct customdirs **dirlist) {
24
  int x;
-
 
25
  struct customdirs *curpos;
24
  struct customdirs *curpos;
26
  /* free repolist */
-
 
27
  for (x = 0; x < repscount; x++) free(repolist[x]);
-
 
28
  /* free the linked list of custom dirs */
25
  /* free the linked list of custom dirs */
29
  while (*dirlist != NULL) {
26
  while (*dirlist != NULL) {
30
    curpos = *dirlist;
27
    curpos = *dirlist;
31
    if (curpos->name != NULL) free(curpos->name);
28
    if (curpos->name != NULL) free(curpos->name);
32
    if (curpos->location != NULL) free(curpos->location);
29
    if (curpos->location != NULL) free(curpos->location);
Line 35... Line 32...
35
  }
32
  }
36
  *dirlist = NULL;
33
  *dirlist = NULL;
37
}
34
}
38
 
35
 
39
 
36
 
40
static int checkfordoubledrepos(char **repolist, int repocount) {
-
 
41
  int x, y;
-
 
42
  for (x = 0; x < (repocount - 1); x++) {
-
 
43
    for (y = x + 1; y < repocount; y++) {
-
 
44
      if (strcmp(repolist[x], repolist[y]) == 0) {
-
 
45
        kitten_printf(7, 14, "Error: repository '%s' is listed twice!", repolist[x]);
-
 
46
        puts("");
-
 
47
        return(-1);
-
 
48
      }
-
 
49
    }
-
 
50
  }
-
 
51
  return(0);
-
 
52
}
-
 
53
 
-
 
54
 
-
 
55
static int checkfordoubledirlist(struct customdirs *dirlist) {
37
static int checkfordoubledirlist(struct customdirs *dirlist) {
56
  struct customdirs *curpos;
38
  struct customdirs *curpos;
57
  for (; dirlist != NULL; dirlist = dirlist->next) {
39
  for (; dirlist != NULL; dirlist = dirlist->next) {
58
    for (curpos = dirlist->next; curpos != NULL; curpos = curpos->next) {
40
    for (curpos = dirlist->next; curpos != NULL; curpos = curpos->next) {
59
      if (strcasecmp(curpos->name, dirlist->name) == 0) {
41
      if (strcasecmp(curpos->name, dirlist->name) == 0) {
Line 123... Line 105...
123
  *dirlist = newentry;
105
  *dirlist = newentry;
124
  return(0);
106
  return(0);
125
}
107
}
126
 
108
 
127
 
109
 
128
int loadconf(char *cfgfile, char **repolist, int maxreps, unsigned long *crc32val, long *maxcachetime, struct customdirs **dirlist, int *flags, char **proxy, int *proxyport, char **mapdrv) {
110
int loadconf(char *cfgfile, struct customdirs **dirlist, int *flags) {
129
  int bytebuff, parserstate = 0;
111
  int bytebuff, parserstate = 0;
130
  FILE *fd;
112
  FILE *fd;
131
  #define BUFFSIZE 1024
-
 
132
  unsigned char *fbuff;
-
 
133
  #define maxtok 16
113
  #define maxtok 16
134
  char token[maxtok];
114
  char token[maxtok];
135
  #define maxval 1024
115
  #define maxval 1024
136
  char value[maxval];
116
  char value[maxval];
137
  int curtok = 0, curval = 0, nline = 1;
117
  int curtok = 0, curval = 0, nline = 1;
138
  int repocount = 0;
-
 
139
  int buffread;
-
 
140
 
118
 
141
  fd = fopen(cfgfile, "r");
119
  fd = fopen(cfgfile, "r");
142
  if (fd == NULL) {
120
  if (fd == NULL) {
143
    kitten_printf(7, 1, "Error: Could not open config file '%s'!", cfgfile);
121
    kitten_printf(7, 1, "Error: Could not open config file '%s'!", cfgfile);
144
    puts("");
122
    puts("");
145
    return(-1);
123
    return(-1);
146
  }
124
  }
147
 
125
 
148
  /* compute the CRC32 of the configuration file (if crc32val not NULL) */
-
 
149
  if (crc32val != NULL) {
-
 
150
    fbuff = malloc(BUFFSIZE);
-
 
151
    if (fbuff == NULL) {
-
 
152
      fclose(fd);
-
 
153
      kitten_printf(2, 14, "Out of memory! (%s)", "fbuff malloc");
-
 
154
      puts("");
-
 
155
      puts("");
-
 
156
      return(-1);
-
 
157
    }
-
 
158
    *crc32val = crc32_init();
-
 
159
    while ((buffread = fread(fbuff, sizeof(char), BUFFSIZE, fd)) > 0) {
-
 
160
      if (buffread > 0) crc32_feed(crc32val, fbuff, buffread);
-
 
161
    }
-
 
162
    crc32_finish(crc32val);
-
 
163
    free(fbuff);
-
 
164
  }
-
 
165
  /* rewind the file, to start reading it again */
-
 
166
  rewind(fd);
-
 
167
 
-
 
168
  /* read the config file line by line */
126
  /* read the config file line by line */
169
  do {
127
  do {
170
    bytebuff = fgetc(fd);
128
    bytebuff = fgetc(fd);
171
    if (bytebuff != '\r') {
129
    if (bytebuff != '\r') {
172
      switch (parserstate) {
130
      switch (parserstate) {
Line 228... Line 186...
228
                puts("");
186
                puts("");
229
                while ((value[curval - 1] == ' ') || (value[curval - 1] == '\t')) value[--curval] = 0;
187
                while ((value[curval - 1] == ' ') || (value[curval - 1] == '\t')) value[--curval] = 0;
230
              }
188
              }
231
              /* Interpret the token/value pair now! */
189
              /* Interpret the token/value pair now! */
232
              /* printf("token='%s' ; value = '%s'\n", token, value); */
190
              /* printf("token='%s' ; value = '%s'\n", token, value); */
233
              if (strcasecmp(token, "REPO") == 0) { /* Repository declaration */
-
 
234
                  if (maxreps == 0) {
-
 
235
                      /* simply ignore if the app explicitely wishes to load no repositories */
-
 
236
                    } else if (repocount >= maxreps) {
-
 
237
                      kitten_printf(7, 6, "Dropped a repository: too many configured (max=%d)", maxreps);
-
 
238
                      puts("");
-
 
239
                    } else {
-
 
240
                      char pathdelimchar;
-
 
241
                      /* add a trailing path delimiter (slash or backslash) to the url if not there already */
-
 
242
                      if (detect_localpath(value) != 0) {
-
 
243
                          pathdelimchar = '\\';
-
 
244
                        } else {
-
 
245
                          pathdelimchar = '/';
-
 
246
                      }
-
 
247
                      if ((value[curval - 1] != '/') && (value[curval - 1] != '\\')) {
-
 
248
                        value[curval++] = pathdelimchar;
-
 
249
                        value[curval] = 0;
-
 
250
                      }
-
 
251
                      /* copy the value into the repository list */
-
 
252
                      repolist[repocount] = strdup(value);
-
 
253
                      if (repolist[repocount] == NULL) {
-
 
254
                        kitten_printf(2, 14, "Out of memory! (%s)", "repolist malloc");
-
 
255
                        puts("");
-
 
256
                        freeconf(repolist, repocount, dirlist);
-
 
257
                        fclose(fd);
-
 
258
                        return(-1);
-
 
259
                      }
-
 
260
                      repocount += 1;
-
 
261
                  }
-
 
262
                } else if (strcasecmp(token, "MAPDRIVES") == 0) {
-
 
263
                  *mapdrv = strdup(value);
-
 
264
                  if ((*mapdrv != NULL) && strlen(*mapdrv) & 1) {
-
 
265
                    free(*mapdrv);
-
 
266
                    *mapdrv = NULL;
-
 
267
                  }
-
 
268
                  if (*mapdrv == NULL) *mapdrv = "";
-
 
269
                } else if (strcasecmp(token, "MAXCACHETIME") == 0) {
-
 
270
                  long tmpint = atol(value);
-
 
271
                  if ((tmpint >= 0) && (tmpint < 1209600l)) { /* min 0s / max 2 weeks */
-
 
272
                      if (maxcachetime != NULL) *maxcachetime = tmpint;
-
 
273
                    } else {
-
 
274
                      kitten_printf(7, 10, "Warning: Ignored an illegal '%s' value at line #%d", "maxcachetime", nline);
-
 
275
                      puts("");
-
 
276
                  }
-
 
277
                } else if (strcasecmp(token, "INSTALLSOURCES") == 0) {
-
 
278
                  int tmpint = atoi(value); /* must be 0/1 */
-
 
279
                  if (tmpint == 0) {
-
 
280
                    *flags |= PKGINST_NOSOURCE;
-
 
281
                  } else if (tmpint == 1) {
-
 
282
                    /* do nothing */
-
 
283
                  } else {
-
 
284
                    kitten_printf(7, 10, "Warning: Ignored an illegal '%s' value at line #%d", "installsources", nline);
-
 
285
                    puts("");
-
 
286
                  }
-
 
287
                } else if (strcasecmp(token, "SKIPLINKS") == 0) {
191
              if (strcasecmp(token, "SKIPLINKS") == 0) {
288
                  int tmpint = atoi(value); /* must be 0/1 */
192
                  int tmpint = atoi(value); /* must be 0/1 */
289
                  if (tmpint == 0) {
193
                  if (tmpint == 0) {
290
                    /* do nothing */
194
                    /* do nothing */
291
                  } else if (tmpint == 1) {
195
                  } else if (tmpint == 1) {
292
                    *flags |= PKGINST_SKIPLINKS;
196
                    *flags |= PKGINST_SKIPLINKS;
293
                  } else {
197
                  } else {
294
                    kitten_printf(7, 10, "Warning: Ignored an illegal '%s' value at line #%d", "skiplinks", nline);
198
                    kitten_printf(7, 10, "Warning: Ignored an illegal '%s' value at line #%d", "skiplinks", nline);
295
                    puts("");
199
                    puts("");
296
                  }
200
                  }
297
                } else if (strcasecmp(token, "HTTP_PROXY") == 0) {
-
 
298
                  if (value[0] != 0) {
-
 
299
                      if (proxy != NULL) *proxy = strdup(value);
-
 
300
                    } else {
-
 
301
                      kitten_printf(7, 10, "Warning: Ignored an illegal '%s' value at line #%d", "http_proxy", nline);
-
 
302
                      puts("");
-
 
303
                  }
-
 
304
                } else if (strcasecmp(token, "HTTP_PROXYPORT") == 0) {
-
 
305
                  int tmpint = atoi(value);
-
 
306
                  if (tmpint != 0) {
-
 
307
                      if (proxyport != NULL) *proxyport = tmpint;
-
 
308
                    } else {
-
 
309
                      kitten_printf(7, 10, "Warning: Ignored an illegal '%s' value at line #%d", "http_proxyport", nline);
-
 
310
                      puts("");
-
 
311
                  }
-
 
312
                } else if (strcasecmp(token, "DIR") == 0) { /* custom repository entry */
201
                } else if (strcasecmp(token, "DIR") == 0) { /* custom repository entry */
313
                  char *argv[2], *evar, *evar_content, *realLocation;
202
                  char *argv[2], *evar, *evar_content, *realLocation;
314
                  #define realLocation_len 512
203
                  #define realLocation_len 512
315
                  int x, y;
204
                  int x, y;
316
                  if (parsecmd(value, argv, 2) != 2) {
205
                  if (parsecmd(value, argv, 2) != 2) {
Line 319... Line 208...
319
                  }
208
                  }
320
                  realLocation = malloc(realLocation_len);
209
                  realLocation = malloc(realLocation_len);
321
                  if (realLocation == NULL) {
210
                  if (realLocation == NULL) {
322
                    kitten_printf(2, 14, "Out of memory! (%s)", "malloc realLocation");
211
                    kitten_printf(2, 14, "Out of memory! (%s)", "malloc realLocation");
323
                    puts("");
212
                    puts("");
324
                    freeconf(repolist, repocount, dirlist);
213
                    freeconf(dirlist);
325
                    fclose(fd);
214
                    fclose(fd);
326
                    return(-1);
215
                    return(-1);
327
                  }
216
                  }
328
                  realLocation[0] = 0; /* force it to be empty, since we might use strcat() on this later! */
217
                  realLocation[0] = 0; /* force it to be empty, since we might use strcat() on this later! */
329
                  /* resolve possible env variables */
218
                  /* resolve possible env variables */
Line 335... Line 224...
335
                            evar = &argv[1][x+1];
224
                            evar = &argv[1][x+1];
336
                          } else {
225
                          } else {
337
                            if (y + 1 > realLocation_len) {
226
                            if (y + 1 > realLocation_len) {
338
                              kitten_printf(7, 12, "Error: DIR path too long at line #%d", nline);
227
                              kitten_printf(7, 12, "Error: DIR path too long at line #%d", nline);
339
                              puts("");
228
                              puts("");
340
                              freeconf(repolist, repocount, dirlist);
229
                              freeconf(dirlist);
341
                              free(realLocation);
230
                              free(realLocation);
342
                              fclose(fd);
231
                              fclose(fd);
343
                              return(-1);
232
                              return(-1);
344
                            }
233
                            }
345
                            realLocation[y] = argv[1][x]; /* copy over */
234
                            realLocation[y] = argv[1][x]; /* copy over */
Line 351... Line 240...
351
                          argv[1][x] = 0;
240
                          argv[1][x] = 0;
352
                          evar_content = getenv(evar);
241
                          evar_content = getenv(evar);
353
                          if (evar_content == NULL) {
242
                          if (evar_content == NULL) {
354
                            kitten_printf(7, 13, "Error: Found inexisting environnement variable '%s' at line #%d", evar, nline);
243
                            kitten_printf(7, 13, "Error: Found inexisting environnement variable '%s' at line #%d", evar, nline);
355
                            puts("");
244
                            puts("");
356
                            freeconf(repolist, repocount, dirlist);
245
                            freeconf(dirlist);
357
                            free(realLocation);
246
                            free(realLocation);
358
                            fclose(fd);
247
                            fclose(fd);
359
                            return(-1);
248
                            return(-1);
360
                          }
249
                          }
361
                          if (strlen(evar_content) + y + 1 > realLocation_len) {
250
                          if (strlen(evar_content) + y + 1 > realLocation_len) {
362
                            kitten_printf(7, 12, "Error: DIR path too long at line #%d", nline);
251
                            kitten_printf(7, 12, "Error: DIR path too long at line #%d", nline);
363
                            puts("");
252
                            puts("");
364
                            freeconf(repolist, repocount, dirlist);
253
                            freeconf(dirlist);
365
                            free(realLocation);
254
                            free(realLocation);
366
                            fclose(fd);
255
                            fclose(fd);
367
                            return(-1);
256
                            return(-1);
368
                          }
257
                          }
369
                          strcat(realLocation, evar_content);
258
                          strcat(realLocation, evar_content);
Line 377... Line 266...
377
                  removeDoubleBackslashes(realLocation);
266
                  removeDoubleBackslashes(realLocation);
378
                  if (realLocation[strlen(realLocation) - 1] != '\\') strcat(realLocation, "\\"); /* make sure to end dirs with a backslash */
267
                  if (realLocation[strlen(realLocation) - 1] != '\\') strcat(realLocation, "\\"); /* make sure to end dirs with a backslash */
379
                  if (addnewdir(dirlist, argv[0], realLocation) != 0) {
268
                  if (addnewdir(dirlist, argv[0], realLocation) != 0) {
380
                    kitten_printf(2, 14, "Out of memory! (%s)", "addnewdir");
269
                    kitten_printf(2, 14, "Out of memory! (%s)", "addnewdir");
381
                    puts("");
270
                    puts("");
382
                    freeconf(repolist, repocount, dirlist);
271
                    freeconf(dirlist);
383
                    free(realLocation);
272
                    free(realLocation);
384
                    fclose(fd);
273
                    fclose(fd);
385
                    return(-1);
274
                    return(-1);
386
                  }
275
                  }
387
                  free(realLocation);
276
                  free(realLocation);
Line 411... Line 300...
411
      }
300
      }
412
    }
301
    }
413
  } while (bytebuff != EOF);
302
  } while (bytebuff != EOF);
414
  fclose(fd);
303
  fclose(fd);
415
 
304
 
416
  /* Look out for doubled repositories */
305
  /* perform some validations */
417
  if (checkfordoubledrepos(repolist, repocount) != 0) return(-1);
-
 
418
  if (checkfordoubledirlist(*dirlist) != 0) return(-1);
306
  if (checkfordoubledirlist(*dirlist) != 0) return(-1);
419
  if (validatedirlist(*dirlist) != 0) return(-1);
307
  if (validatedirlist(*dirlist) != 0) return(-1);
420
 
308
 
421
  return(repocount);
309
  return(0);
422
}
310
}