Subversion Repositories SvarDOS

Rev

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

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