Subversion Repositories SvarDOS

Rev

Rev 1634 | Rev 1931 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/*
268 mateuszvis 2
 * This file is part of pkg (SvarDOS).
1634 mateusz.vi 3
 * Copyright (C) 2012-2024 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) {
613 mateuszvis 32
        kitten_printf(7, 0, curpos->name); /* "ERROR: custom dir '%s' is listed twice!" */
219 mateuszvis 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) {
613 mateuszvis 47
      kitten_printf(7, 15, dirlist->name); /* "ERROR: custom dir '%s' is not a valid absolute path!" */
219 mateuszvis 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')))) {
613 mateuszvis 55
      kitten_printf(7, 15, dirlist->name); /* "ERROR: custom dir '%s' is not a valid absolute path!" */
219 mateuszvis 56
      puts("");
57
      return(-1);
58
    }
59
    /* check for forbidden names */
60
    if ((strcasecmp(dirlist->name, "appinfo") == 0) ||
61
        (strcasecmp(dirlist->name, "doc") == 0) ||
62
        (strcasecmp(dirlist->name, "help") == 0) ||
63
        (strcasecmp(dirlist->name, "nls") == 0) ||
64
        (strcasecmp(dirlist->name, "packages") == 0)) {
613 mateuszvis 65
      kitten_printf(7, 16, dirlist->name); /* "ERROR: custom dir '%s' is a reserved name!" */
219 mateuszvis 66
      puts("");
67
      return(-1);
68
    }
69
  }
70
  return(0);
71
}
72
 
73
 
74
/* add (and allocates) a new custom dir entry to dirlist. Returns 0 on success,
75
   or non-zero on failure (failures happen on out of memory events). */
238 mateuszvis 76
static int addnewdir(struct customdirs **dirlist, const char *name, const char *location) {
219 mateuszvis 77
  struct customdirs *newentry;
238 mateuszvis 78
  if (strlen(name) >= sizeof(newentry->name)) return(-2);
79
  newentry = malloc(sizeof(struct customdirs) + strlen(location) + 1);
219 mateuszvis 80
  if (newentry == NULL) return(-1);
81
  strcpy(newentry->name, name);
82
  strcpy(newentry->location, location);
83
  newentry->next = *dirlist;
84
  *dirlist = newentry;
85
  return(0);
86
}
87
 
88
 
1892 mateusz.vi 89
int loadconf(const char *dosdir, struct customdirs **dirlist, char *bootdrive) {
219 mateuszvis 90
  FILE *fd;
248 mateuszvis 91
  char *value = NULL;
92
  char token[512];
93
  int nline = 0;
1892 mateusz.vi 94
  const char *PKG_CFG = " (PKG.CFG)";
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) {
613 mateuszvis 99
    kitten_printf(7, 1, token); /* "ERROR: Could not open config file (%s)!" */
219 mateuszvis 100
    puts("");
101
    return(-1);
102
  }
103
 
260 mateuszvis 104
  *dirlist = NULL;
1892 mateusz.vi 105
  *bootdrive = 'C'; /* default */
260 mateuszvis 106
 
219 mateuszvis 107
  /* read the config file line by line */
248 mateuszvis 108
  while (freadtokval(fd, token, sizeof(token), &value, ' ') == 0) {
109
    nline++;
110
 
111
    /* skip comments and empty lines */
112
    if ((token[0] == '#') || (token[0] == 0)) continue;
113
 
114
    if ((value == NULL) || (value[0] == 0)) {
613 mateuszvis 115
      kitten_printf(7, 4, nline); /* "Warning: token with empty value on line #%d" */
1892 mateusz.vi 116
      puts(PKG_CFG);
248 mateuszvis 117
      continue;
118
    }
119
 
120
    /* printf("token='%s' ; value = '%s'\n", token, value); */
260 mateuszvis 121
    if (strcasecmp(token, "DIR") == 0) { /* custom directory entry */
262 mateuszvis 122
      char *location = NULL;
123
      int i;
124
      /* find nearer space */
125
      for (i = 0; (value[i] != ' ') && (value[i] != 0); i++);
126
      if (value[i] == 0) {
613 mateuszvis 127
        kitten_printf(7, 11, nline); /* "Warning: Invalid 'DIR' directive found at line #%d" */
1892 mateusz.vi 128
        puts(PKG_CFG);
262 mateuszvis 129
        continue;
248 mateuszvis 130
      }
262 mateuszvis 131
      value[i] = 0;
132
      location = value + i + 1;
133
 
248 mateuszvis 134
      /* add the entry to the list */
262 mateuszvis 135
      slash2backslash(location);
136
      removeDoubleBackslashes(location);
137
      if (location[strlen(location) - 1] != '\\') strcat(location, "\\"); /* make sure to end dirs with a backslash */
138
      if (addnewdir(dirlist, value, location) != 0) {
613 mateuszvis 139
        kitten_printf(2, 14, "addnewdir"); /* "Out of memory! (%s)" */
248 mateuszvis 140
        puts("");
141
        freeconf(dirlist);
142
        fclose(fd);
143
        return(-1);
144
      }
1892 mateusz.vi 145
    } else if (strcasecmp(token, "BOOTDRIVE") == 0) { /* boot drive (used for kernel and command.com installation */
146
      *bootdrive = value[0];
147
      *bootdrive &= 0xDF; /* upcase it */
148
      if ((*bootdrive < 'A') || (*bootdrive > 'Z')) {
149
        kitten_printf(7, 5, nline); /* Warning: Invalid bootdrive at line #%d */
150
        puts(PKG_CFG);
151
        *bootdrive = 'C';
152
      }
248 mateuszvis 153
    } else { /* unknown token */
613 mateuszvis 154
      kitten_printf(7, 8, token, nline); /* "Warning: Unknown token '%s' at line #%d" */
248 mateuszvis 155
      puts("");
219 mateuszvis 156
    }
248 mateuszvis 157
  }
219 mateuszvis 158
  fclose(fd);
159
 
225 mateuszvis 160
  /* perform some validations */
248 mateuszvis 161
  if ((checkfordoubledirlist(*dirlist) != 0) || (validatedirlist(*dirlist) != 0)) {
162
    freeconf(dirlist);
163
    return(-1);
164
  }
219 mateuszvis 165
 
225 mateuszvis 166
  return(0);
219 mateuszvis 167
}