Subversion Repositories SvarDOS

Rev

Rev 1251 | Rev 1281 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1251 Rev 1277
1
/* This file is part of the svarlang project and is published under the terms
1
/* This file is part of the svarlang project and is published under the terms
2
 * of the MIT license.
2
 * of the MIT license.
3
 *
3
 *
4
 * Copyright (C) 2021-2023 Mateusz Viste
4
 * Copyright (C) 2021-2023 Mateusz Viste
5
 *
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * Permission is hereby granted, free of charge, to any person obtaining a
7
 * copy of this software and associated documentation files (the "Software"),
7
 * copy of this software and associated documentation files (the "Software"),
8
 * to deal in the Software without restriction, including without limitation
8
 * to deal in the Software without restriction, including without limitation
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10
 * and/or sell copies of the Software, and to permit persons to whom the
10
 * and/or sell copies of the Software, and to permit persons to whom the
11
 * Software is furnished to do so, subject to the following conditions:
11
 * Software is furnished to do so, subject to the following conditions:
12
 *
12
 *
13
 * The above copyright notice and this permission notice shall be included in
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
14
 * all copies or substantial portions of the Software.
15
 *
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22
 * DEALINGS IN THE SOFTWARE.
22
 * DEALINGS IN THE SOFTWARE.
23
 */
23
 */
24
 
24
 
25
#include <i86.h>
25
#include <i86.h>
26
#include <stdlib.h>  /* NULL */
26
#include <stdlib.h>  /* NULL */
27
#include <string.h>  /* memcmp(), strcpy() */
27
#include <string.h>  /* memcmp(), strcpy() */
28
 
28
 
29
#include "svarlang.h"
29
#include "svarlang.h"
30
 
30
 
31
 
31
 
32
/* supplied through DEFLANG.C */
32
/* supplied through DEFLANG.C */
33
extern char svarlang_mem[];
33
extern char svarlang_mem[];
34
extern const unsigned short svarlang_memsz;
34
extern const unsigned short svarlang_memsz;
35
 
35
 
36
 
36
 
37
const char *svarlang_strid(unsigned short id) {
37
const char *svarlang_strid(unsigned short id) {
38
  const char *ptr = svarlang_mem;
38
  const char *ptr = svarlang_mem;
39
  /* find the string id in langblock memory */
39
  /* find the string id in langblock memory */
40
  for (;;) {
40
  for (;;) {
41
    if (((unsigned short *)ptr)[0] == id) return(ptr + 4);
41
    if (((unsigned short *)ptr)[0] == id) return(ptr + 4);
42
    if (((unsigned short *)ptr)[1] == 0) return(ptr + 2); /* end of strings - return an empty string */
42
    if (((unsigned short *)ptr)[1] == 0) return(ptr + 2); /* end of strings - return an empty string */
43
    ptr += ((unsigned short *)ptr)[1] + 4;
43
    ptr += ((unsigned short *)ptr)[1] + 4;
44
  }
44
  }
45
}
45
}
46
 
46
 
47
 
47
 
48
static unsigned short FOPEN(const char *s) {
48
static unsigned short FOPEN(const char *s) {
49
  unsigned short fname_seg = FP_SEG(s);
49
  unsigned short fname_seg = FP_SEG(s);
50
  unsigned short fname_off = FP_OFF(s);
50
  unsigned short fname_off = FP_OFF(s);
51
  unsigned short res = 0xffff;
51
  unsigned short res = 0xffff;
52
  _asm {
52
  _asm {
53
    push dx
53
    push dx
54
    push ds
54
    push ds
55
 
55
 
56
    mov ax, fname_seg
56
    mov ax, fname_seg
57
    mov dx, fname_off
57
    mov dx, fname_off
58
    mov ds, ax
58
    mov ds, ax
59
    mov ax, 0x3d00  /* open file, read-only (fname at DS:DX) */
59
    mov ax, 0x3d00  /* open file, read-only (fname at DS:DX) */
60
    int 0x21
60
    int 0x21
61
    pop ds
61
    pop ds
62
    jc ERR
62
    jc ERR
63
    mov res, ax
63
    mov res, ax
64
 
64
 
65
    ERR:
65
    ERR:
66
    pop dx
66
    pop dx
67
  }
67
  }
68
 
68
 
69
  return(res);
69
  return(res);
70
}
70
}
71
 
71
 
72
 
72
 
73
static void FCLOSE(unsigned short handle) {
73
static void FCLOSE(unsigned short handle) {
74
  _asm {
74
  _asm {
75
    push bx
75
    push bx
76
 
76
 
77
    mov ah, 0x3e
77
    mov ah, 0x3e
78
    mov bx, handle
78
    mov bx, handle
79
    int 0x21
79
    int 0x21
80
 
80
 
81
    pop bx
81
    pop bx
82
  }
82
  }
83
}
83
}
84
 
84
 
85
 
85
 
86
static unsigned short FREAD(unsigned short handle, void *buff, unsigned short bytes) {
86
static unsigned short FREAD(unsigned short handle, void *buff, unsigned short bytes) {
87
  unsigned short buff_seg = FP_SEG(buff);
87
  unsigned short buff_seg = FP_SEG(buff);
88
  unsigned short buff_off = FP_OFF(buff);
88
  unsigned short buff_off = FP_OFF(buff);
89
  unsigned short res = 0;
89
  unsigned short res = 0;
90
 
90
 
91
  _asm {
91
  _asm {
92
    push bx
92
    push bx
93
    push cx
93
    push cx
94
    push dx
94
    push dx
95
 
95
 
96
    mov bx, handle
96
    mov bx, handle
97
    mov cx, bytes
97
    mov cx, bytes
98
    mov dx, buff_off
98
    mov dx, buff_off
99
    mov ax, buff_seg
99
    mov ax, buff_seg
100
    push ds
100
    push ds
101
    mov ds, ax
101
    mov ds, ax
102
    mov ah, 0x3f    /* read cx bytes from file handle bx to DS:DX */
102
    mov ah, 0x3f    /* read cx bytes from file handle bx to DS:DX */
103
    int 0x21
103
    int 0x21
104
    pop ds
104
    pop ds
105
    jc ERR
105
    jc ERR
106
 
106
 
107
    mov res, ax
107
    mov res, ax
108
    ERR:
108
    ERR:
109
 
109
 
110
    pop dx
110
    pop dx
111
    pop cx
111
    pop cx
112
    pop bx
112
    pop bx
113
  }
113
  }
114
 
114
 
115
  return(res);
115
  return(res);
116
}
116
}
117
 
117
 
118
 
118
 
119
static void FJUMP(unsigned short handle, unsigned short bytes) {
119
static void FJUMP(unsigned short handle, unsigned short bytes) {
120
  _asm {
120
  _asm {
121
    push bx
121
    push bx
122
    push cx
122
    push cx
123
    push dx
123
    push dx
124
 
124
 
125
    mov ax, 0x4201  /* move file pointer from cur pos + CX:DX */
125
    mov ax, 0x4201  /* move file pointer from cur pos + CX:DX */
126
    mov bx, handle
126
    mov bx, handle
127
    xor cx, cx
127
    xor cx, cx
128
    mov dx, bytes
128
    mov dx, bytes
129
    int 0x21
129
    int 0x21
130
 
130
 
131
    pop dx
131
    pop dx
132
    pop cx
132
    pop cx
133
    pop bx
133
    pop bx
134
  }
134
  }
135
}
135
}
136
 
136
 
137
 
137
 
138
int svarlang_load(const char *progname, const char *lang, const char *nlspath) {
138
int svarlang_load(const char *fname, const char *lang) {
139
  unsigned short langid;
139
  unsigned short langid;
140
  unsigned short fd;
140
  unsigned short fd;
141
  char buff[128];
141
  char hdr[4];
142
  unsigned short buff16[2];
142
  unsigned short buff16[2];
143
  unsigned short i;
-
 
144
 
-
 
145
  if (lang == NULL) return(-1);
-
 
146
  if (nlspath == NULL) nlspath = ""; /* nlspath can be NULL, treat is as empty */
-
 
147
 
143
 
148
  langid = *((unsigned short *)lang);
144
  langid = *((unsigned short *)lang);
149
  langid &= 0xDFDF; /* make sure lang is upcase */
145
  langid &= 0xDFDF; /* make sure lang is upcase */
150
 
146
 
151
  TRYNEXTPATH:
-
 
152
 
-
 
153
  /* skip any leading ';' separators */
-
 
154
  while (*nlspath == ';') nlspath++;
-
 
155
 
-
 
156
  /* copy nlspath to buff and remember len */
-
 
157
  for (i = 0; (nlspath[i] != 0) && (nlspath[i] != ';'); i++) buff[i] = nlspath[i];
-
 
158
  nlspath += i;
-
 
159
 
-
 
160
  /* add a trailing backslash if there is none (non-empty paths empty) */
-
 
161
  if ((i > 0) && (buff[i - 1] != '\\')) buff[i++] = '\\';
-
 
162
 
-
 
163
  strcpy(buff + i, progname);
-
 
164
  strcat(buff + i, ".lng");
-
 
165
 
-
 
166
  fd = FOPEN(buff);
147
  fd = FOPEN(fname);
167
  if (fd == 0xffff) { /* failed to open file - either abort or try next path */
-
 
168
    if (*nlspath == 0) return(-2);
148
  if (fd == 0xffff) return(-1);
169
    goto TRYNEXTPATH;
-
 
170
  }
-
 
171
 
149
 
172
  /* read hdr, should be "SvL\33" */
150
  /* read hdr, should be "SvL\33" */
173
  if ((FREAD(fd, buff, 4) != 4) || (memcmp(buff, "SvL\33", 4) != 0)) {
151
  if ((FREAD(fd, hdr, 4) != 4) || (memcmp(hdr, "SvL\33", 4) != 0)) {
174
    FCLOSE(fd);
152
    FCLOSE(fd);
175
    return(-3);
153
    return(-3);
176
  }
154
  }
177
 
155
 
178
  /* read next lang id in file */
156
  /* read next lang id in file */
179
  while (FREAD(fd, buff16, 4) == 4) {
157
  while (FREAD(fd, buff16, 4) == 4) {
180
 
158
 
181
    /* is it the lang I am looking for? */
159
    /* is it the lang I am looking for? */
182
    if (buff16[0] != langid) { /* skip to next lang */
160
    if (buff16[0] != langid) { /* skip to next lang */
183
      FJUMP(fd, buff16[1]);
161
      FJUMP(fd, buff16[1]);
184
      continue;
162
      continue;
185
    }
163
    }
186
 
164
 
187
    /* found - but do I have enough memory space? */
165
    /* found - but do I have enough memory space? */
188
    if (buff16[1] >= svarlang_memsz) {
166
    if (buff16[1] >= svarlang_memsz) {
189
      FCLOSE(fd);
167
      FCLOSE(fd);
190
      return(-4);
168
      return(-4);
191
    }
169
    }
192
 
170
 
193
    /* load strings */
171
    /* load strings */
194
    if (FREAD(fd, svarlang_mem, buff16[1]) != buff16[1]) break;
172
    if (FREAD(fd, svarlang_mem, buff16[1]) != buff16[1]) break;
195
    FCLOSE(fd);
173
    FCLOSE(fd);
196
    return(0);
174
    return(0);
197
  }
175
  }
198
 
176
 
199
  FCLOSE(fd);
177
  FCLOSE(fd);
200
  return(-5);
178
  return(-5);
201
}
179
}
202
 
180