Subversion Repositories SvarDOS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/*
268 mateuszvis 2
 * This file is part of pkg (SvarDOS).
238 mateuszvis 3
 * Copyright (C) 2012-2021 Mateusz Viste
219 mateuszvis 4
 */
5
 
6
#include <stdio.h>  /* printf(), fclose(), fopen()... */
7
#include <string.h> /* strcasecmp() */
8
#include <stdlib.h> /* malloc(), free() */
9
 
10
#include "helpers.h" /* slash2backslash(), removeDoubleBackslashes()... */
11
#include "kprintf.h"
262 mateuszvis 12
 
219 mateuszvis 13
#include "loadconf.h"
14
 
15
 
225 mateuszvis 16
void freeconf(struct customdirs **dirlist) {
219 mateuszvis 17
  struct customdirs *curpos;
18
  /* free the linked list of custom dirs */
19
  while (*dirlist != NULL) {
20
    curpos = *dirlist;
21
    *dirlist = (*dirlist)->next;
22
    free(curpos);
23
  }
24
}
25
 
26
 
238 mateuszvis 27
static int checkfordoubledirlist(const struct customdirs *dirlist) {
219 mateuszvis 28
  struct customdirs *curpos;
29
  for (; dirlist != NULL; dirlist = dirlist->next) {
30
    for (curpos = dirlist->next; curpos != NULL; curpos = curpos->next) {
31
      if (strcasecmp(curpos->name, dirlist->name) == 0) {
32
        kitten_printf(7, 0, "Error: custom dir '%s' is listed twice!", curpos->name);
33
        puts("");
34
        return(-1);
35
      }
36
    }
37
  }
38
  return(0);
39
}
40
 
41
 
42
/* validates dirlist entries: check that they are absolute paths and are not using restricted names */
238 mateuszvis 43
static int validatedirlist(const struct customdirs *dirlist) {
219 mateuszvis 44
  for (; dirlist != NULL; dirlist = dirlist->next) {
45
    /* the location must be at least 3 characters long to be a valid absolute path (like 'c:\')*/
46
    if (strlen(dirlist->location) < 3) {
47
      kitten_printf(7, 15, "Error: custom dir '%s' is not a valid absolute path!", dirlist->name);
48
      puts("");
49
      return(-1);
50
    }
51
    /* is it a valid absolute path? should start with [a..Z]:\ */
52
    if ((dirlist->location[1] != ':') ||
53
       ((dirlist->location[2] != '/') && (dirlist->location[2] != '\\')) ||
54
       (((dirlist->location[0] < 'a') || (dirlist->location[0] > 'z')) && ((dirlist->location[0] < 'A') || (dirlist->location[0] > 'Z')))) {
55
      kitten_printf(7, 15, "Error: custom dir '%s' is not a valid absolute path!", dirlist->name);
56
      puts("");
57
      return(-1);
58
    }
59
    /* check for forbidden names */
60
    if ((strcasecmp(dirlist->name, "appinfo") == 0) ||
61
        (strcasecmp(dirlist->name, "bin") == 0) ||
62
        (strcasecmp(dirlist->name, "doc") == 0) ||
63
        (strcasecmp(dirlist->name, "help") == 0) ||
64
        (strcasecmp(dirlist->name, "nls") == 0) ||
65
        (strcasecmp(dirlist->name, "packages") == 0)) {
66
      kitten_printf(7, 16, "Error: custom dir '%s' is a reserved name!", dirlist->name);
67
      puts("");
68
      return(-1);
69
    }
70
  }
71
  return(0);
72
}
73
 
74
 
75
/* add (and allocates) a new custom dir entry to dirlist. Returns 0 on success,
76
   or non-zero on failure (failures happen on out of memory events). */
238 mateuszvis 77
static int addnewdir(struct customdirs **dirlist, const char *name, const char *location) {
219 mateuszvis 78
  struct customdirs *newentry;
238 mateuszvis 79
  if (strlen(name) >= sizeof(newentry->name)) return(-2);
80
  newentry = malloc(sizeof(struct customdirs) + strlen(location) + 1);
219 mateuszvis 81
  if (newentry == NULL) return(-1);
82
  strcpy(newentry->name, name);
83
  strcpy(newentry->location, location);
84
  newentry->next = *dirlist;
85
  *dirlist = newentry;
86
  return(0);
87
}
88
 
89
 
260 mateuszvis 90
int loadconf(const char *dosdir, struct customdirs **dirlist) {
219 mateuszvis 91
  FILE *fd;
248 mateuszvis 92
  char *value = NULL;
93
  char token[512];
94
  int nline = 0;
219 mateuszvis 95
 
268 mateuszvis 96
  snprintf(token, sizeof(token), "%s\\cfg\\pkg.cfg", dosdir);
248 mateuszvis 97
  fd = fopen(token, "r");
219 mateuszvis 98
  if (fd == NULL) {
248 mateuszvis 99
    kitten_printf(7, 1, "Error: Could not open config file (%s)!", token);
219 mateuszvis 100
    puts("");
101
    return(-1);
102
  }
103
 
260 mateuszvis 104
  *dirlist = NULL;
105
 
219 mateuszvis 106
  /* read the config file line by line */
248 mateuszvis 107
  while (freadtokval(fd, token, sizeof(token), &value, ' ') == 0) {
108
    nline++;
109
 
110
    /* skip comments and empty lines */
111
    if ((token[0] == '#') || (token[0] == 0)) continue;
112
 
113
    if ((value == NULL) || (value[0] == 0)) {
114
      kitten_printf(7, 4, "Warning: token with empty value on line #%d", nline);
115
      puts("");
116
      continue;
117
    }
118
 
119
    /* printf("token='%s' ; value = '%s'\n", token, value); */
260 mateuszvis 120
    if (strcasecmp(token, "DIR") == 0) { /* custom directory entry */
262 mateuszvis 121
      char *location = NULL;
122
      int i;
123
      /* find nearer space */
124
      for (i = 0; (value[i] != ' ') && (value[i] != 0); i++);
125
      if (value[i] == 0) {
248 mateuszvis 126
        kitten_printf(7, 11, "Warning: Invalid 'DIR' directive found at line #%d", nline);
127
        puts("");
262 mateuszvis 128
        continue;
248 mateuszvis 129
      }
262 mateuszvis 130
      value[i] = 0;
131
      location = value + i + 1;
132
 
248 mateuszvis 133
      /* add the entry to the list */
262 mateuszvis 134
      slash2backslash(location);
135
      removeDoubleBackslashes(location);
136
      if (location[strlen(location) - 1] != '\\') strcat(location, "\\"); /* make sure to end dirs with a backslash */
137
      if (addnewdir(dirlist, value, location) != 0) {
248 mateuszvis 138
        kitten_printf(2, 14, "Out of memory! (%s)", "addnewdir");
139
        puts("");
140
        freeconf(dirlist);
141
        fclose(fd);
142
        return(-1);
143
      }
144
    } else { /* unknown token */
145
      kitten_printf(7, 8, "Warning: Unknown token '%s' at line #%d", token, nline);
146
      puts("");
219 mateuszvis 147
    }
248 mateuszvis 148
  }
219 mateuszvis 149
  fclose(fd);
150
 
225 mateuszvis 151
  /* perform some validations */
248 mateuszvis 152
  if ((checkfordoubledirlist(*dirlist) != 0) || (validatedirlist(*dirlist) != 0)) {
153
    freeconf(dirlist);
154
    return(-1);
155
  }
219 mateuszvis 156
 
225 mateuszvis 157
  return(0);
219 mateuszvis 158
}