Subversion Repositories SvarDOS

Rev

Rev 1965 | 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()... */
2235 bernd.boec 7
#include <string.h>
8
#include <strings.h> /* strcasecmp() */
219 mateuszvis 9
#include <stdlib.h> /* malloc(), free() */
10
 
11
#include "helpers.h" /* slash2backslash(), removeDoubleBackslashes()... */
2235 bernd.boec 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) {
1965 mateusz.vi 33
        char msg[256];
34
        sprintf(msg, svarlang_str(7, 0), curpos->name); /* "ERROR: custom dir '%s' is listed twice!" */
35
        output(msg);
36
        outputnl(" (PKG.CFG)");
219 mateuszvis 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:\')*/
1965 mateusz.vi 49
    if (strlen(dirlist->location) < 3) goto INVALID_CUSTOM_DIR;
50
 
219 mateuszvis 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')))) {
1965 mateusz.vi 55
      goto INVALID_CUSTOM_DIR;
219 mateuszvis 56
    }
1965 mateusz.vi 57
 
219 mateuszvis 58
    /* check for forbidden names */
59
    if ((strcasecmp(dirlist->name, "appinfo") == 0) ||
60
        (strcasecmp(dirlist->name, "doc") == 0) ||
61
        (strcasecmp(dirlist->name, "help") == 0) ||
62
        (strcasecmp(dirlist->name, "nls") == 0) ||
63
        (strcasecmp(dirlist->name, "packages") == 0)) {
1965 mateusz.vi 64
      goto INVALID_CUSTOM_DIR;
219 mateuszvis 65
    }
66
  }
67
  return(0);
1965 mateusz.vi 68
 
69
  INVALID_CUSTOM_DIR:
70
  output(svarlang_str(7, 15)); /* "ERROR: invalid custom dir:" */
71
  output(" ");
72
  output(dirlist->name);
73
  outputnl(" (PKG.CFG)");
74
  return(-1);
219 mateuszvis 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
 
1892 mateusz.vi 93
int loadconf(const char *dosdir, struct customdirs **dirlist, char *bootdrive) {
219 mateuszvis 94
  FILE *fd;
248 mateuszvis 95
  char *value = NULL;
96
  char token[512];
97
  int nline = 0;
1892 mateusz.vi 98
  const char *PKG_CFG = " (PKG.CFG)";
1965 mateusz.vi 99
  char msg[256];
219 mateuszvis 100
 
1931 mateusz.vi 101
  /* load config file from %DOSDIR%\PKG.CFG */
102
  snprintf(token, sizeof(token), "%s\\pkg.cfg", dosdir);
248 mateuszvis 103
  fd = fopen(token, "r");
1931 mateusz.vi 104
  /* if not found then try the legacy location at %DOSDIR%\CFG\PKG.CFG */
219 mateuszvis 105
  if (fd == NULL) {
1931 mateusz.vi 106
    snprintf(token, sizeof(token), "%s\\cfg\\pkg.cfg", dosdir);
107
    fd = fopen(token, "r");
108
    if (fd == NULL) {
1965 mateusz.vi 109
      sprintf(msg, svarlang_str(7, 1), "%DOSDIR%\\PKG.CFG"); /* "ERROR: Could not open config file (%s)!" */
110
      outputnl(msg);
1931 mateusz.vi 111
    } else {
1965 mateusz.vi 112
      sprintf(msg, svarlang_str(7, 17), token);  /* "ERROR: PKG.CFG found at %s */
113
      outputnl(msg);
1963 mateusz.vi 114
      outputnl(svarlang_str(7, 18));    /* Please move it to %DOSDIR%\PKG.CFG */
1931 mateusz.vi 115
    }
219 mateuszvis 116
    return(-1);
117
  }
118
 
260 mateuszvis 119
  *dirlist = NULL;
1892 mateusz.vi 120
  *bootdrive = 'C'; /* default */
260 mateuszvis 121
 
219 mateuszvis 122
  /* read the config file line by line */
248 mateuszvis 123
  while (freadtokval(fd, token, sizeof(token), &value, ' ') == 0) {
124
    nline++;
125
 
126
    /* skip comments and empty lines */
127
    if ((token[0] == '#') || (token[0] == 0)) continue;
128
 
129
    if ((value == NULL) || (value[0] == 0)) {
1965 mateusz.vi 130
      sprintf(msg, svarlang_str(7, 4), nline); /* "Warning: token with empty value on line #%d" */
131
      output(msg);
1963 mateusz.vi 132
      outputnl(PKG_CFG);
248 mateuszvis 133
      continue;
134
    }
135
 
136
    /* printf("token='%s' ; value = '%s'\n", token, value); */
260 mateuszvis 137
    if (strcasecmp(token, "DIR") == 0) { /* custom directory entry */
262 mateuszvis 138
      char *location = NULL;
139
      int i;
140
      /* find nearer space */
141
      for (i = 0; (value[i] != ' ') && (value[i] != 0); i++);
142
      if (value[i] == 0) {
1965 mateusz.vi 143
        sprintf(msg, svarlang_str(7, 11), nline); /* "Warning: Invalid 'DIR' directive found at line #%d" */
144
        output(msg);
1963 mateusz.vi 145
        outputnl(PKG_CFG);
262 mateuszvis 146
        continue;
248 mateuszvis 147
      }
262 mateuszvis 148
      value[i] = 0;
149
      location = value + i + 1;
150
 
248 mateuszvis 151
      /* add the entry to the list */
262 mateuszvis 152
      slash2backslash(location);
153
      removeDoubleBackslashes(location);
154
      if (location[strlen(location) - 1] != '\\') strcat(location, "\\"); /* make sure to end dirs with a backslash */
155
      if (addnewdir(dirlist, value, location) != 0) {
1965 mateusz.vi 156
        outputnl(svarlang_str(2, 14)); /* "Out of memory!" */
248 mateuszvis 157
        freeconf(dirlist);
158
        fclose(fd);
159
        return(-1);
160
      }
1892 mateusz.vi 161
    } else if (strcasecmp(token, "BOOTDRIVE") == 0) { /* boot drive (used for kernel and command.com installation */
162
      *bootdrive = value[0];
163
      *bootdrive &= 0xDF; /* upcase it */
164
      if ((*bootdrive < 'A') || (*bootdrive > 'Z')) {
1965 mateusz.vi 165
        sprintf(msg, svarlang_str(7, 5), nline); /* Warning: Invalid bootdrive at line #%d */
166
        output(msg);
1963 mateusz.vi 167
        outputnl(PKG_CFG);
1892 mateusz.vi 168
        *bootdrive = 'C';
169
      }
248 mateuszvis 170
    } else { /* unknown token */
1965 mateusz.vi 171
      sprintf(msg, svarlang_str(7, 8), token, nline); /* "Warning: Unknown token '%s' at line #%d" */
172
      output(msg);
173
      outputnl(PKG_CFG);
219 mateuszvis 174
    }
248 mateuszvis 175
  }
219 mateuszvis 176
  fclose(fd);
177
 
225 mateuszvis 178
  /* perform some validations */
248 mateuszvis 179
  if ((checkfordoubledirlist(*dirlist) != 0) || (validatedirlist(*dirlist) != 0)) {
180
    freeconf(dirlist);
181
    return(-1);
182
  }
219 mateuszvis 183
 
225 mateuszvis 184
  return(0);
219 mateuszvis 185
}