Subversion Repositories SvarDOS

Rev

Rev 241 | Rev 258 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 241 Rev 248
1
/*
1
/*
2
 * This file is part of pkginst (SvarDOS).
2
 * This file is part of pkginst (SvarDOS).
3
 *
3
 *
4
 * Loads the list of repositories from a config file.
4
 * Loads the list of repositories from a config file.
5
 *
5
 *
6
 * Copyright (C) 2012-2021 Mateusz Viste
6
 * Copyright (C) 2012-2021 Mateusz Viste
7
 */
7
 */
8
 
8
 
9
#include <stdio.h>  /* printf(), fclose(), fopen()... */
9
#include <stdio.h>  /* printf(), fclose(), fopen()... */
10
#include <string.h> /* strcasecmp() */
10
#include <string.h> /* strcasecmp() */
11
#include <stdlib.h> /* malloc(), free() */
11
#include <stdlib.h> /* malloc(), free() */
12
 
12
 
13
#include "fdnpkg.h" /* PKGINST_SKIPLINKS... */
13
#include "fdnpkg.h" /* PKGINST_SKIPLINKS... */
14
#include "helpers.h" /* slash2backslash(), removeDoubleBackslashes()... */
14
#include "helpers.h" /* slash2backslash(), removeDoubleBackslashes()... */
15
#include "kprintf.h"
15
#include "kprintf.h"
16
#include "loadconf.h"
16
#include "loadconf.h"
17
#include "parsecmd.h"
17
#include "parsecmd.h"
18
 
18
 
19
 
19
 
20
void freeconf(struct customdirs **dirlist) {
20
void freeconf(struct customdirs **dirlist) {
21
  struct customdirs *curpos;
21
  struct customdirs *curpos;
22
  /* free the linked list of custom dirs */
22
  /* free the linked list of custom dirs */
23
  while (*dirlist != NULL) {
23
  while (*dirlist != NULL) {
24
    curpos = *dirlist;
24
    curpos = *dirlist;
25
    *dirlist = (*dirlist)->next;
25
    *dirlist = (*dirlist)->next;
26
    free(curpos);
26
    free(curpos);
27
  }
27
  }
28
}
28
}
29
 
29
 
30
 
30
 
31
static int checkfordoubledirlist(const struct customdirs *dirlist) {
31
static int checkfordoubledirlist(const struct customdirs *dirlist) {
32
  struct customdirs *curpos;
32
  struct customdirs *curpos;
33
  for (; dirlist != NULL; dirlist = dirlist->next) {
33
  for (; dirlist != NULL; dirlist = dirlist->next) {
34
    for (curpos = dirlist->next; curpos != NULL; curpos = curpos->next) {
34
    for (curpos = dirlist->next; curpos != NULL; curpos = curpos->next) {
35
      if (strcasecmp(curpos->name, dirlist->name) == 0) {
35
      if (strcasecmp(curpos->name, dirlist->name) == 0) {
36
        kitten_printf(7, 0, "Error: custom dir '%s' is listed twice!", curpos->name);
36
        kitten_printf(7, 0, "Error: custom dir '%s' is listed twice!", curpos->name);
37
        puts("");
37
        puts("");
38
        return(-1);
38
        return(-1);
39
      }
39
      }
40
    }
40
    }
41
  }
41
  }
42
  return(0);
42
  return(0);
43
}
43
}
44
 
44
 
45
 
45
 
46
/* validates dirlist entries: check that they are absolute paths and are not using restricted names */
46
/* validates dirlist entries: check that they are absolute paths and are not using restricted names */
47
static int validatedirlist(const struct customdirs *dirlist) {
47
static int validatedirlist(const struct customdirs *dirlist) {
48
  for (; dirlist != NULL; dirlist = dirlist->next) {
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:\')*/
49
    /* the location must be at least 3 characters long to be a valid absolute path (like 'c:\')*/
50
    if (strlen(dirlist->location) < 3) {
50
    if (strlen(dirlist->location) < 3) {
51
      kitten_printf(7, 15, "Error: custom dir '%s' is not a valid absolute path!", dirlist->name);
51
      kitten_printf(7, 15, "Error: custom dir '%s' is not a valid absolute path!", dirlist->name);
52
      puts("");
52
      puts("");
53
      return(-1);
53
      return(-1);
54
    }
54
    }
55
    /* is it a valid absolute path? should start with [a..Z]:\ */
55
    /* is it a valid absolute path? should start with [a..Z]:\ */
56
    if ((dirlist->location[1] != ':') ||
56
    if ((dirlist->location[1] != ':') ||
57
       ((dirlist->location[2] != '/') && (dirlist->location[2] != '\\')) ||
57
       ((dirlist->location[2] != '/') && (dirlist->location[2] != '\\')) ||
58
       (((dirlist->location[0] < 'a') || (dirlist->location[0] > 'z')) && ((dirlist->location[0] < 'A') || (dirlist->location[0] > 'Z')))) {
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);
59
      kitten_printf(7, 15, "Error: custom dir '%s' is not a valid absolute path!", dirlist->name);
60
      puts("");
60
      puts("");
61
      return(-1);
61
      return(-1);
62
    }
62
    }
63
    /* check for forbidden names */
63
    /* check for forbidden names */
64
    if ((strcasecmp(dirlist->name, "appinfo") == 0) ||
64
    if ((strcasecmp(dirlist->name, "appinfo") == 0) ||
65
        (strcasecmp(dirlist->name, "bin") == 0) ||
65
        (strcasecmp(dirlist->name, "bin") == 0) ||
66
        (strcasecmp(dirlist->name, "doc") == 0) ||
66
        (strcasecmp(dirlist->name, "doc") == 0) ||
67
        (strcasecmp(dirlist->name, "help") == 0) ||
67
        (strcasecmp(dirlist->name, "help") == 0) ||
68
        (strcasecmp(dirlist->name, "nls") == 0) ||
68
        (strcasecmp(dirlist->name, "nls") == 0) ||
69
        (strcasecmp(dirlist->name, "packages") == 0)) {
69
        (strcasecmp(dirlist->name, "packages") == 0)) {
70
      kitten_printf(7, 16, "Error: custom dir '%s' is a reserved name!", dirlist->name);
70
      kitten_printf(7, 16, "Error: custom dir '%s' is a reserved name!", dirlist->name);
71
      puts("");
71
      puts("");
72
      return(-1);
72
      return(-1);
73
    }
73
    }
74
  }
74
  }
75
  return(0);
75
  return(0);
76
}
76
}
77
 
77
 
78
 
78
 
79
/* add (and allocates) a new custom dir entry to dirlist. Returns 0 on success,
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). */
80
   or non-zero on failure (failures happen on out of memory events). */
81
static int addnewdir(struct customdirs **dirlist, const char *name, const char *location) {
81
static int addnewdir(struct customdirs **dirlist, const char *name, const char *location) {
82
  struct customdirs *newentry;
82
  struct customdirs *newentry;
83
  if (strlen(name) >= sizeof(newentry->name)) return(-2);
83
  if (strlen(name) >= sizeof(newentry->name)) return(-2);
84
  newentry = malloc(sizeof(struct customdirs) + strlen(location) + 1);
84
  newentry = malloc(sizeof(struct customdirs) + strlen(location) + 1);
85
  if (newentry == NULL) return(-1);
85
  if (newentry == NULL) return(-1);
86
  strcpy(newentry->name, name);
86
  strcpy(newentry->name, name);
87
  strcpy(newentry->location, location);
87
  strcpy(newentry->location, location);
88
  newentry->next = *dirlist;
88
  newentry->next = *dirlist;
89
  *dirlist = newentry;
89
  *dirlist = newentry;
90
  return(0);
90
  return(0);
91
}
91
}
92
 
92
 
93
 
93
 
94
int loadconf(const char *dosdir, struct customdirs **dirlist, int *flags) {
94
int loadconf(const char *dosdir, struct customdirs **dirlist, int *flags) {
95
  int bytebuff, parserstate = 0;
-
 
96
  FILE *fd;
95
  FILE *fd;
97
  #define maxtok 16
-
 
98
  char token[maxtok];
-
 
99
  #define maxval 256
-
 
100
  char value[maxval];
96
  char *value = NULL;
101
  char cfgfile[256];
97
  char token[512];
102
  int curtok = 0, curval = 0, nline = 1;
98
  int nline = 0;
103
 
99
 
104
  snprintf(cfgfile, sizeof(cfgfile), "%s\\cfg\\pkginst.cfg", dosdir);
100
  snprintf(token, sizeof(token), "%s\\cfg\\pkginst.cfg", dosdir);
105
  fd = fopen(cfgfile, "r");
101
  fd = fopen(token, "r");
106
  if (fd == NULL) {
102
  if (fd == NULL) {
107
    kitten_printf(7, 1, "Error: Could not open config file (%s)!", cfgfile);
103
    kitten_printf(7, 1, "Error: Could not open config file (%s)!", token);
108
    puts("");
104
    puts("");
109
    return(-1);
105
    return(-1);
110
  }
106
  }
111
 
107
 
112
  /* read the config file line by line */
108
  /* 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;
109
  while (freadtokval(fd, token, sizeof(token), &value, ' ') == 0) {
119
          if (bytebuff == '\n') {
-
 
120
            nline += 1;
-
 
121
            break;
-
 
122
          }
110
    nline++;
123
          if (bytebuff == '#') {
-
 
124
              parserstate = 9;
-
 
125
            } else {
-
 
126
              token[0] = bytebuff;
-
 
127
              curtok = 1;
-
 
128
              parserstate = 1;
-
 
129
          }
111
 
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;
112
    /* skip comments and empty lines */
136
            parserstate = 0;
-
 
137
            break;
-
 
138
          }
-
 
139
          if ((bytebuff == ' ') || (bytebuff == '\t')) {
113
    if ((token[0] == '#') || (token[0] == 0)) continue;
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
          }
114
 
151
          break;
-
 
152
        case 2: /* Looking for value start */
-
 
153
          if ((bytebuff == EOF) || (bytebuff == '\n')) {
115
    if ((value == NULL) || (value[0] == 0)) {
154
            kitten_printf(7, 4, "Warning: token with empty value on line #%d", nline);
116
      kitten_printf(7, 4, "Warning: token with empty value on line #%d", nline);
155
            puts("");
117
      puts("");
156
            if (bytebuff == '\n') nline += 1;
-
 
157
            parserstate = 0;
-
 
158
            break;
118
      continue;
159
          }
119
    }
160
          if ((bytebuff != ' ') && (bytebuff != '\t')) {
-
 
161
            value[0] = bytebuff;
-
 
162
            curval = 1;
-
 
163
            parserstate = 3;
-
 
164
          }
120
 
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); */
121
    /* printf("token='%s' ; value = '%s'\n", token, value); */
177
              if (strcasecmp(token, "SKIPLINKS") == 0) {
122
    if (strcasecmp(token, "SKIPLINKS") == 0) {
178
                  int tmpint = atoi(value); /* must be 0/1 */
123
      int tmpint = atoi(value); /* must be 0/1 */
179
                  if (tmpint == 0) {
124
      if (tmpint == 0) {
180
                    /* do nothing */
125
        /* do nothing */
181
                  } else if (tmpint == 1) {
126
      } else if (tmpint == 1) {
182
                    *flags |= PKGINST_SKIPLINKS;
127
        *flags |= PKGINST_SKIPLINKS;
183
                  } else {
128
      } else {
184
                    kitten_printf(7, 10, "Warning: Ignored an illegal '%s' value at line #%d", "skiplinks", nline);
129
        kitten_printf(7, 10, "Warning: Ignored an illegal '%s' value at line #%d", "skiplinks", nline);
185
                    puts("");
130
        puts("");
186
                  }
131
      }
187
                } else if (strcasecmp(token, "DIR") == 0) { /* custom repository entry */
132
    } else if (strcasecmp(token, "DIR") == 0) { /* custom directory entry */
188
                  char *argv[2], *evar, *evar_content, *realLocation;
133
      char *argv[2];
189
                  #define realLocation_len 512
134
      #define realLocation_len 512
190
                  int x, y;
-
 
191
                  if (parsecmd(value, argv, 2) != 2) {
135
      if (parsecmd(value, argv, 2) != 2) {
192
                    kitten_printf(7, 11, "Warning: Invalid 'DIR' directive found at line #%d", nline);
136
        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("");
-
 
199
                    freeconf(dirlist);
-
 
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("");
-
 
215
                              freeconf(dirlist);
-
 
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("");
-
 
231
                            freeconf(dirlist);
-
 
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("");
-
 
239
                            freeconf(dirlist);
-
 
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("");
-
 
257
                    freeconf(dirlist);
-
 
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("");
137
        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
      }
138
      }
-
 
139
      /* add the entry to the list */
-
 
140
      slash2backslash(argv[1]);
-
 
141
      removeDoubleBackslashes(argv[1]);
-
 
142
      if (argv[1][strlen(argv[1]) - 1] != '\\') strcat(argv[1], "\\"); /* make sure to end dirs with a backslash */
-
 
143
      if (addnewdir(dirlist, argv[0], argv[1]) != 0) {
-
 
144
        kitten_printf(2, 14, "Out of memory! (%s)", "addnewdir");
-
 
145
        puts("");
-
 
146
        freeconf(dirlist);
-
 
147
        fclose(fd);
-
 
148
        return(-1);
-
 
149
      }
-
 
150
    } else { /* unknown token */
-
 
151
      kitten_printf(7, 8, "Warning: Unknown token '%s' at line #%d", token, nline);
-
 
152
      puts("");
287
    }
153
    }
288
  } while (bytebuff != EOF);
154
  }
289
  fclose(fd);
155
  fclose(fd);
290
 
156
 
291
  /* perform some validations */
157
  /* perform some validations */
292
  if (checkfordoubledirlist(*dirlist) != 0) return(-1);
158
  if ((checkfordoubledirlist(*dirlist) != 0) || (validatedirlist(*dirlist) != 0)) {
293
  if (validatedirlist(*dirlist) != 0) return(-1);
159
    freeconf(dirlist);
-
 
160
    return(-1);
-
 
161
  }
294
 
162
 
295
  return(0);
163
  return(0);
296
}
164
}
297
 
165