Subversion Repositories SvarDOS

Rev

Rev 237 | Rev 241 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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