Subversion Repositories SvarDOS

Rev

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

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