Subversion Repositories SvarDOS

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

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