Subversion Repositories SvarDOS

Rev

Rev 1963 | 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()... */
1931 mateusz.vi 11
#include "svarlang.lib\svarlang.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) {
1965 mateusz.vi 32
        char msg[256];
33
        sprintf(msg, svarlang_str(7, 0), curpos->name); /* "ERROR: custom dir '%s' is listed twice!" */
34
        output(msg);
35
        outputnl(" (PKG.CFG)");
219 mateuszvis 36
        return(-1);
37
      }
38
    }
39
  }
40
  return(0);
41
}
42
 
43
 
44
/* validates dirlist entries: check that they are absolute paths and are not using restricted names */
238 mateuszvis 45
static int validatedirlist(const struct customdirs *dirlist) {
219 mateuszvis 46
  for (; dirlist != NULL; dirlist = dirlist->next) {
47
    /* the location must be at least 3 characters long to be a valid absolute path (like 'c:\')*/
1965 mateusz.vi 48
    if (strlen(dirlist->location) < 3) goto INVALID_CUSTOM_DIR;
49
 
219 mateuszvis 50
    /* is it a valid absolute path? should start with [a..Z]:\ */
51
    if ((dirlist->location[1] != ':') ||
52
       ((dirlist->location[2] != '/') && (dirlist->location[2] != '\\')) ||
53
       (((dirlist->location[0] < 'a') || (dirlist->location[0] > 'z')) && ((dirlist->location[0] < 'A') || (dirlist->location[0] > 'Z')))) {
1965 mateusz.vi 54
      goto INVALID_CUSTOM_DIR;
219 mateuszvis 55
    }
1965 mateusz.vi 56
 
219 mateuszvis 57
    /* check for forbidden names */
58
    if ((strcasecmp(dirlist->name, "appinfo") == 0) ||
59
        (strcasecmp(dirlist->name, "doc") == 0) ||
60
        (strcasecmp(dirlist->name, "help") == 0) ||
61
        (strcasecmp(dirlist->name, "nls") == 0) ||
62
        (strcasecmp(dirlist->name, "packages") == 0)) {
1965 mateusz.vi 63
      goto INVALID_CUSTOM_DIR;
219 mateuszvis 64
    }
65
  }
66
  return(0);
1965 mateusz.vi 67
 
68
  INVALID_CUSTOM_DIR:
69
  output(svarlang_str(7, 15)); /* "ERROR: invalid custom dir:" */
70
  output(" ");
71
  output(dirlist->name);
72
  outputnl(" (PKG.CFG)");
73
  return(-1);
219 mateuszvis 74
}
75
 
76
 
77
/* add (and allocates) a new custom dir entry to dirlist. Returns 0 on success,
78
   or non-zero on failure (failures happen on out of memory events). */
238 mateuszvis 79
static int addnewdir(struct customdirs **dirlist, const char *name, const char *location) {
219 mateuszvis 80
  struct customdirs *newentry;
238 mateuszvis 81
  if (strlen(name) >= sizeof(newentry->name)) return(-2);
82
  newentry = malloc(sizeof(struct customdirs) + strlen(location) + 1);
219 mateuszvis 83
  if (newentry == NULL) return(-1);
84
  strcpy(newentry->name, name);
85
  strcpy(newentry->location, location);
86
  newentry->next = *dirlist;
87
  *dirlist = newentry;
88
  return(0);
89
}
90
 
91
 
1892 mateusz.vi 92
int loadconf(const char *dosdir, struct customdirs **dirlist, char *bootdrive) {
219 mateuszvis 93
  FILE *fd;
248 mateuszvis 94
  char *value = NULL;
95
  char token[512];
96
  int nline = 0;
1892 mateusz.vi 97
  const char *PKG_CFG = " (PKG.CFG)";
1965 mateusz.vi 98
  char msg[256];
219 mateuszvis 99
 
1931 mateusz.vi 100
  /* load config file from %DOSDIR%\PKG.CFG */
101
  snprintf(token, sizeof(token), "%s\\pkg.cfg", dosdir);
248 mateuszvis 102
  fd = fopen(token, "r");
1931 mateusz.vi 103
  /* if not found then try the legacy location at %DOSDIR%\CFG\PKG.CFG */
219 mateuszvis 104
  if (fd == NULL) {
1931 mateusz.vi 105
    snprintf(token, sizeof(token), "%s\\cfg\\pkg.cfg", dosdir);
106
    fd = fopen(token, "r");
107
    if (fd == NULL) {
1965 mateusz.vi 108
      sprintf(msg, svarlang_str(7, 1), "%DOSDIR%\\PKG.CFG"); /* "ERROR: Could not open config file (%s)!" */
109
      outputnl(msg);
1931 mateusz.vi 110
    } else {
1965 mateusz.vi 111
      sprintf(msg, svarlang_str(7, 17), token);  /* "ERROR: PKG.CFG found at %s */
112
      outputnl(msg);
1963 mateusz.vi 113
      outputnl(svarlang_str(7, 18));    /* Please move it to %DOSDIR%\PKG.CFG */
1931 mateusz.vi 114
    }
219 mateuszvis 115
    return(-1);
116
  }
117
 
260 mateuszvis 118
  *dirlist = NULL;
1892 mateusz.vi 119
  *bootdrive = 'C'; /* default */
260 mateuszvis 120
 
219 mateuszvis 121
  /* read the config file line by line */
248 mateuszvis 122
  while (freadtokval(fd, token, sizeof(token), &value, ' ') == 0) {
123
    nline++;
124
 
125
    /* skip comments and empty lines */
126
    if ((token[0] == '#') || (token[0] == 0)) continue;
127
 
128
    if ((value == NULL) || (value[0] == 0)) {
1965 mateusz.vi 129
      sprintf(msg, svarlang_str(7, 4), nline); /* "Warning: token with empty value on line #%d" */
130
      output(msg);
1963 mateusz.vi 131
      outputnl(PKG_CFG);
248 mateuszvis 132
      continue;
133
    }
134
 
135
    /* printf("token='%s' ; value = '%s'\n", token, value); */
260 mateuszvis 136
    if (strcasecmp(token, "DIR") == 0) { /* custom directory entry */
262 mateuszvis 137
      char *location = NULL;
138
      int i;
139
      /* find nearer space */
140
      for (i = 0; (value[i] != ' ') && (value[i] != 0); i++);
141
      if (value[i] == 0) {
1965 mateusz.vi 142
        sprintf(msg, svarlang_str(7, 11), nline); /* "Warning: Invalid 'DIR' directive found at line #%d" */
143
        output(msg);
1963 mateusz.vi 144
        outputnl(PKG_CFG);
262 mateuszvis 145
        continue;
248 mateuszvis 146
      }
262 mateuszvis 147
      value[i] = 0;
148
      location = value + i + 1;
149
 
248 mateuszvis 150
      /* add the entry to the list */
262 mateuszvis 151
      slash2backslash(location);
152
      removeDoubleBackslashes(location);
153
      if (location[strlen(location) - 1] != '\\') strcat(location, "\\"); /* make sure to end dirs with a backslash */
154
      if (addnewdir(dirlist, value, location) != 0) {
1965 mateusz.vi 155
        outputnl(svarlang_str(2, 14)); /* "Out of memory!" */
248 mateuszvis 156
        freeconf(dirlist);
157
        fclose(fd);
158
        return(-1);
159
      }
1892 mateusz.vi 160
    } else if (strcasecmp(token, "BOOTDRIVE") == 0) { /* boot drive (used for kernel and command.com installation */
161
      *bootdrive = value[0];
162
      *bootdrive &= 0xDF; /* upcase it */
163
      if ((*bootdrive < 'A') || (*bootdrive > 'Z')) {
1965 mateusz.vi 164
        sprintf(msg, svarlang_str(7, 5), nline); /* Warning: Invalid bootdrive at line #%d */
165
        output(msg);
1963 mateusz.vi 166
        outputnl(PKG_CFG);
1892 mateusz.vi 167
        *bootdrive = 'C';
168
      }
248 mateuszvis 169
    } else { /* unknown token */
1965 mateusz.vi 170
      sprintf(msg, svarlang_str(7, 8), token, nline); /* "Warning: Unknown token '%s' at line #%d" */
171
      output(msg);
172
      outputnl(PKG_CFG);
219 mateuszvis 173
    }
248 mateuszvis 174
  }
219 mateuszvis 175
  fclose(fd);
176
 
225 mateuszvis 177
  /* perform some validations */
248 mateuszvis 178
  if ((checkfordoubledirlist(*dirlist) != 0) || (validatedirlist(*dirlist) != 0)) {
179
    freeconf(dirlist);
180
    return(-1);
181
  }
219 mateuszvis 182
 
225 mateuszvis 183
  return(0);
219 mateuszvis 184
}