Subversion Repositories SvarDOS

Rev

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

Rev 241 Rev 248
Line 90... Line 90...
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
}