Subversion Repositories SvarDOS

Rev

Rev 1892 | 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"
1931 mateusz.vi 12
#include "svarlang.lib\svarlang.h"
262 mateuszvis 13
 
219 mateuszvis 14
#include "loadconf.h"
15
 
16
 
225 mateuszvis 17
void freeconf(struct customdirs **dirlist) {
219 mateuszvis 18
  struct customdirs *curpos;
19
  /* free the linked list of custom dirs */
20
  while (*dirlist != NULL) {
21
    curpos = *dirlist;
22
    *dirlist = (*dirlist)->next;
23
    free(curpos);
24
  }
25
}
26
 
27
 
238 mateuszvis 28
static int checkfordoubledirlist(const struct customdirs *dirlist) {
219 mateuszvis 29
  struct customdirs *curpos;
30
  for (; dirlist != NULL; dirlist = dirlist->next) {
31
    for (curpos = dirlist->next; curpos != NULL; curpos = curpos->next) {
32
      if (strcasecmp(curpos->name, dirlist->name) == 0) {
613 mateuszvis 33
        kitten_printf(7, 0, curpos->name); /* "ERROR: custom dir '%s' is listed twice!" */
219 mateuszvis 34
        puts("");
35
        return(-1);
36
      }
37
    }
38
  }
39
  return(0);
40
}
41
 
42
 
43
/* validates dirlist entries: check that they are absolute paths and are not using restricted names */
238 mateuszvis 44
static int validatedirlist(const struct customdirs *dirlist) {
219 mateuszvis 45
  for (; dirlist != NULL; dirlist = dirlist->next) {
46
    /* the location must be at least 3 characters long to be a valid absolute path (like 'c:\')*/
47
    if (strlen(dirlist->location) < 3) {
613 mateuszvis 48
      kitten_printf(7, 15, dirlist->name); /* "ERROR: custom dir '%s' is not a valid absolute path!" */
219 mateuszvis 49
      puts("");
50
      return(-1);
51
    }
52
    /* is it a valid absolute path? should start with [a..Z]:\ */
53
    if ((dirlist->location[1] != ':') ||
54
       ((dirlist->location[2] != '/') && (dirlist->location[2] != '\\')) ||
55
       (((dirlist->location[0] < 'a') || (dirlist->location[0] > 'z')) && ((dirlist->location[0] < 'A') || (dirlist->location[0] > 'Z')))) {
613 mateuszvis 56
      kitten_printf(7, 15, dirlist->name); /* "ERROR: custom dir '%s' is not a valid absolute path!" */
219 mateuszvis 57
      puts("");
58
      return(-1);
59
    }
60
    /* check for forbidden names */
61
    if ((strcasecmp(dirlist->name, "appinfo") == 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)) {
613 mateuszvis 66
      kitten_printf(7, 16, dirlist->name); /* "ERROR: custom dir '%s' is a reserved name!" */
219 mateuszvis 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
 
1892 mateusz.vi 90
int loadconf(const char *dosdir, struct customdirs **dirlist, char *bootdrive) {
219 mateuszvis 91
  FILE *fd;
248 mateuszvis 92
  char *value = NULL;
93
  char token[512];
94
  int nline = 0;
1892 mateusz.vi 95
  const char *PKG_CFG = " (PKG.CFG)";
219 mateuszvis 96
 
1931 mateusz.vi 97
  /* load config file from %DOSDIR%\PKG.CFG */
98
  snprintf(token, sizeof(token), "%s\\pkg.cfg", dosdir);
248 mateuszvis 99
  fd = fopen(token, "r");
1931 mateusz.vi 100
  /* if not found then try the legacy location at %DOSDIR%\CFG\PKG.CFG */
219 mateuszvis 101
  if (fd == NULL) {
1931 mateusz.vi 102
    snprintf(token, sizeof(token), "%s\\cfg\\pkg.cfg", dosdir);
103
    fd = fopen(token, "r");
104
    if (fd == NULL) {
105
      kitten_printf(7, 1, "%DOSDIR%\\PKG.CFG"); /* "ERROR: Could not open config file (%s)!" */
106
    } else {
107
      kitten_printf(7, 17, token);  /* "ERROR: PKG.CFG found at %s */
108
      puts("");
109
      puts(svarlang_str(7, 18));    /* Please move it to %DOSDIR%\PKG.CFG */
110
    }
219 mateuszvis 111
    puts("");
112
    return(-1);
113
  }
114
 
260 mateuszvis 115
  *dirlist = NULL;
1892 mateusz.vi 116
  *bootdrive = 'C'; /* default */
260 mateuszvis 117
 
219 mateuszvis 118
  /* read the config file line by line */
248 mateuszvis 119
  while (freadtokval(fd, token, sizeof(token), &value, ' ') == 0) {
120
    nline++;
121
 
122
    /* skip comments and empty lines */
123
    if ((token[0] == '#') || (token[0] == 0)) continue;
124
 
125
    if ((value == NULL) || (value[0] == 0)) {
613 mateuszvis 126
      kitten_printf(7, 4, nline); /* "Warning: token with empty value on line #%d" */
1892 mateusz.vi 127
      puts(PKG_CFG);
248 mateuszvis 128
      continue;
129
    }
130
 
131
    /* printf("token='%s' ; value = '%s'\n", token, value); */
260 mateuszvis 132
    if (strcasecmp(token, "DIR") == 0) { /* custom directory entry */
262 mateuszvis 133
      char *location = NULL;
134
      int i;
135
      /* find nearer space */
136
      for (i = 0; (value[i] != ' ') && (value[i] != 0); i++);
137
      if (value[i] == 0) {
613 mateuszvis 138
        kitten_printf(7, 11, nline); /* "Warning: Invalid 'DIR' directive found at line #%d" */
1892 mateusz.vi 139
        puts(PKG_CFG);
262 mateuszvis 140
        continue;
248 mateuszvis 141
      }
262 mateuszvis 142
      value[i] = 0;
143
      location = value + i + 1;
144
 
248 mateuszvis 145
      /* add the entry to the list */
262 mateuszvis 146
      slash2backslash(location);
147
      removeDoubleBackslashes(location);
148
      if (location[strlen(location) - 1] != '\\') strcat(location, "\\"); /* make sure to end dirs with a backslash */
149
      if (addnewdir(dirlist, value, location) != 0) {
613 mateuszvis 150
        kitten_printf(2, 14, "addnewdir"); /* "Out of memory! (%s)" */
248 mateuszvis 151
        puts("");
152
        freeconf(dirlist);
153
        fclose(fd);
154
        return(-1);
155
      }
1892 mateusz.vi 156
    } else if (strcasecmp(token, "BOOTDRIVE") == 0) { /* boot drive (used for kernel and command.com installation */
157
      *bootdrive = value[0];
158
      *bootdrive &= 0xDF; /* upcase it */
159
      if ((*bootdrive < 'A') || (*bootdrive > 'Z')) {
160
        kitten_printf(7, 5, nline); /* Warning: Invalid bootdrive at line #%d */
161
        puts(PKG_CFG);
162
        *bootdrive = 'C';
163
      }
248 mateuszvis 164
    } else { /* unknown token */
613 mateuszvis 165
      kitten_printf(7, 8, token, nline); /* "Warning: Unknown token '%s' at line #%d" */
248 mateuszvis 166
      puts("");
219 mateuszvis 167
    }
248 mateuszvis 168
  }
219 mateuszvis 169
  fclose(fd);
170
 
225 mateuszvis 171
  /* perform some validations */
248 mateuszvis 172
  if ((checkfordoubledirlist(*dirlist) != 0) || (validatedirlist(*dirlist) != 0)) {
173
    freeconf(dirlist);
174
    return(-1);
175
  }
219 mateuszvis 176
 
225 mateuszvis 177
  return(0);
219 mateuszvis 178
}