Subversion Repositories SvarDOS

Rev

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

Rev 1376 Rev 1377
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
/* if WITHSTDIO is enabled, then remap file operations to use the standard
25
/* if WITHSTDIO is enabled, then remap file operations to use the standard
26
 * stdio amenities */
26
 * stdio amenities */
27
#ifdef WITHSTDIO
27
#ifdef WITHSTDIO
28
 
28
 
29
#include <stdio.h>   /* FILE, fopen(), fseek(), etc */
29
#include <stdio.h>   /* FILE, fopen(), fseek(), etc */
30
typedef FILE* FHANDLE;
30
typedef FILE* FHANDLE;
31
#define FOPEN(x) fopen(x, "rb")
31
#define FOPEN(x) fopen(x, "rb")
32
#define FCLOSE(x) fclose(x)
32
#define FCLOSE(x) fclose(x)
33
#define FSEEK(f,b) fseek(f,b,SEEK_CUR)
33
#define FSEEK(f,b) fseek(f,b,SEEK_CUR)
34
#define FREAD(f,t,b) fread(t, 1, b, f)
34
#define FREAD(f,t,b) fread(t, 1, b, f)
35
 
35
 
36
#else
36
#else
37
 
37
 
38
#include <i86.h>  /* FP_SEG, FP_OFF */
38
#include <i86.h>  /* FP_SEG, FP_OFF */
39
typedef unsigned short FHANDLE;
39
typedef unsigned short FHANDLE;
40
 
40
 
41
#endif
41
#endif
42
 
42
 
43
 
43
 
44
#include "svarlang.h"
44
#include "svarlang.h"
45
 
45
 
46
 
46
 
47
/* supplied through DEFLANG.C */
47
/* supplied through DEFLANG.C */
48
extern char svarlang_mem[];
48
extern char svarlang_mem[];
49
extern unsigned short svarlang_dict[];
49
extern unsigned short svarlang_dict[];
50
extern const unsigned short svarlang_memsz;
50
extern const unsigned short svarlang_memsz;
51
extern const unsigned short svarlang_string_count;
51
extern const unsigned short svarlang_string_count;
52
 
52
 
53
 
53
 
54
const char *svarlang_strid(unsigned short id) {
54
const char *svarlang_strid(unsigned short id) {
55
  unsigned short left = 0, right = svarlang_string_count - 1, x;
55
  unsigned short left = 0, right = svarlang_string_count - 1, x;
56
  unsigned short v;
56
  unsigned short v;
57
 
57
 
58
  if (svarlang_string_count == 0) return("");
58
  if (svarlang_string_count == 0) return("");
59
 
59
 
60
  while (left <= right) {
60
  while (left <= right) {
61
    x = left + ((right - left ) >> 2);
61
    x = left + ((right - left ) >> 2);
62
    v = svarlang_dict[x * 2];
62
    v = svarlang_dict[x * 2];
63
 
63
 
64
    if (id == v) return(svarlang_mem + svarlang_dict[x * 2 + 1]);
64
    if (id == v) return(svarlang_mem + svarlang_dict[x * 2 + 1]);
65
 
65
 
66
    if (id > v) {
66
    if (id > v) {
67
      left = x + 1;
67
      left = x + 1;
68
    } else {
68
    } else {
69
      right = x - 1;
69
      right = x - 1;
70
    }
70
    }
71
  }
71
  }
72
 
72
 
73
  return("");
73
  return("");
74
}
74
}
75
 
75
 
76
 
76
 
77
/* routines below are simplified (dos-based) versions of the libc FILE-related
77
/* routines below are simplified (dos-based) versions of the libc FILE-related
78
 * functions. Using them avoids a dependency on FILE, hence makes the binary
78
 * functions. Using them avoids a dependency on FILE, hence makes the binary
79
 * smaller if the application does not need to pull fopen() and friends */
79
 * smaller if the application does not need to pull fopen() and friends */
80
#ifndef WITHSTDIO
80
#ifndef WITHSTDIO
81
static unsigned short FOPEN(const char *s) {
81
static unsigned short FOPEN(const char *s) {
82
  unsigned short fname_seg = FP_SEG(s);
82
  unsigned short fname_seg = FP_SEG(s);
83
  unsigned short fname_off = FP_OFF(s);
83
  unsigned short fname_off = FP_OFF(s);
84
  unsigned short res = 0; /* fd 0 is already used by stdout so it's a good error value */
84
  unsigned short res = 0; /* fd 0 is already used by stdout so it's a good error value */
85
  _asm {
85
  _asm {
86
    push dx
86
    push dx
87
    push ds
87
    push ds
88
 
88
 
89
    mov ax, fname_seg
89
    mov ax, fname_seg
90
    mov dx, fname_off
90
    mov dx, fname_off
91
    mov ds, ax
91
    mov ds, ax
92
    mov ax, 0x3d00  /* open file, read-only (fname at DS:DX) */
92
    mov ax, 0x3d00  /* open file, read-only (fname at DS:DX) */
93
    int 0x21
93
    int 0x21
94
    pop ds
94
    pop ds
95
    jc ERR
95
    jc ERR
96
    mov res, ax
96
    mov res, ax
97
 
97
 
98
    ERR:
98
    ERR:
99
    pop dx
99
    pop dx
100
  }
100
  }
101
 
101
 
102
  return(res);
102
  return(res);
103
}
103
}
104
 
104
 
105
 
105
 
106
static void FCLOSE(unsigned short handle) {
106
static void FCLOSE(unsigned short handle) {
107
  _asm {
107
  _asm {
108
    push bx
-
 
109
 
-
 
110
    mov ah, 0x3e
108
    mov ah, 0x3e
111
    mov bx, handle
109
    mov bx, handle
112
    int 0x21
110
    int 0x21
113
 
-
 
114
    pop bx
-
 
115
  }
111
  }
116
}
112
}
117
 
113
 
118
 
114
 
119
static unsigned short FREAD(unsigned short handle, void *buff, unsigned short bytes) {
115
static unsigned short FREAD(unsigned short handle, void *buff, unsigned short bytes) {
120
  unsigned short buff_seg = FP_SEG(buff);
116
  unsigned short buff_seg = FP_SEG(buff);
121
  unsigned short buff_off = FP_OFF(buff);
117
  unsigned short buff_off = FP_OFF(buff);
122
  unsigned short res = 0;
118
  unsigned short res = 0;
123
 
119
 
124
  _asm {
120
  _asm {
125
    push bx
121
    push bx
126
    push cx
122
    push cx
127
    push dx
123
    push dx
128
 
124
 
129
    mov bx, handle
125
    mov bx, handle
130
    mov cx, bytes
126
    mov cx, bytes
131
    mov dx, buff_off
127
    mov dx, buff_off
132
    mov ax, buff_seg
128
    mov ax, buff_seg
133
    push ds
129
    push ds
134
    mov ds, ax
130
    mov ds, ax
135
    mov ah, 0x3f    /* read cx bytes from file handle bx to DS:DX */
131
    mov ah, 0x3f    /* read cx bytes from file handle bx to DS:DX */
136
    int 0x21
132
    int 0x21
137
    pop ds
133
    pop ds
138
    jc ERR
134
    jc ERR
139
 
135
 
140
    mov res, ax
136
    mov res, ax
141
    ERR:
137
    ERR:
142
 
138
 
143
    pop dx
139
    pop dx
144
    pop cx
140
    pop cx
145
    pop bx
141
    pop bx
146
  }
142
  }
147
 
143
 
148
  return(res);
144
  return(res);
149
}
145
}
150
 
146
 
151
 
147
 
152
static void FSEEK(unsigned short handle, unsigned short bytes) {
148
static void FSEEK(unsigned short handle, unsigned short bytes) {
153
  _asm {
149
  _asm {
154
    push bx
-
 
155
    push cx
-
 
156
    push dx
-
 
157
 
-
 
158
    mov ax, 0x4201  /* move file pointer from cur pos + CX:DX */
150
    mov ax, 0x4201  /* move file pointer from cur pos + CX:DX */
159
    mov bx, handle
151
    mov bx, handle
160
    xor cx, cx
152
    xor cx, cx
161
    mov dx, bytes
153
    mov dx, bytes
162
    int 0x21
154
    int 0x21
163
 
-
 
164
    pop dx
-
 
165
    pop cx
-
 
166
    pop bx
-
 
167
  }
155
  }
168
}
156
}
169
#endif
157
#endif
170
 
158
 
171
 
159
 
172
int svarlang_load(const char *fname, const char *lang) {
160
int svarlang_load(const char *fname, const char *lang) {
173
  unsigned short langid;
161
  unsigned short langid;
174
  unsigned short buff16[2];
162
  unsigned short buff16[2];
175
  FHANDLE fd;
163
  FHANDLE fd;
176
  signed char exitcode = 0;
164
  signed char exitcode = 0;
177
  struct {
165
  struct {
178
    unsigned long sig;
166
    unsigned long sig;
179
    unsigned short string_count;
167
    unsigned short string_count;
180
  } hdr;
168
  } hdr;
181
 
169
 
182
  langid = *((unsigned short *)lang);
170
  langid = *((unsigned short *)lang);
183
  langid &= 0xDFDF; /* make sure lang is upcase */
171
  langid &= 0xDFDF; /* make sure lang is upcase */
184
 
172
 
185
  fd = FOPEN(fname);
173
  fd = FOPEN(fname);
186
  if (!fd) return(-1);
174
  if (!fd) return(-1);
187
 
175
 
188
  /* read hdr, sig should be "SvL\x1a" (0x1a4c7653) */
176
  /* read hdr, sig should be "SvL\x1a" (0x1a4c7653) */
189
  if ((FREAD(fd, &hdr, 6) != 6) || (hdr.sig != 0x1a4c7653L) || (hdr.string_count != svarlang_string_count)) {
177
  if ((FREAD(fd, &hdr, 6) != 6) || (hdr.sig != 0x1a4c7653L) || (hdr.string_count != svarlang_string_count)) {
190
    exitcode = -2;
178
    exitcode = -2;
191
    goto FCLOSE_AND_EXIT;
179
    goto FCLOSE_AND_EXIT;
192
  }
180
  }
193
 
181
 
194
  for (;;) {
182
  for (;;) {
195
    /* read next lang id and string table size in file */
183
    /* read next lang id and string table size in file */
196
    if (FREAD(fd, buff16, 4) != 4) {
184
    if (FREAD(fd, buff16, 4) != 4) {
197
      exitcode = -3;
185
      exitcode = -3;
198
      goto FCLOSE_AND_EXIT;
186
      goto FCLOSE_AND_EXIT;
199
    }
187
    }
200
 
188
 
201
    /* is it the lang I am looking for? */
189
    /* is it the lang I am looking for? */
202
    if (buff16[0] == langid) break;
190
    if (buff16[0] == langid) break;
203
 
191
 
204
    /* skip to next lang (in two steps to avoid a potential uint16 overflow) */
192
    /* skip to next lang (in two steps to avoid a potential uint16 overflow) */
205
    FSEEK(fd, svarlang_string_count * 4);
193
    FSEEK(fd, svarlang_string_count * 4);
206
    FSEEK(fd, buff16[1]);
194
    FSEEK(fd, buff16[1]);
207
  }
195
  }
208
 
196
 
209
  /* load dictionary & strings, but only if I have enough memory space */
197
  /* load dictionary & strings, but only if I have enough memory space */
210
  if ((buff16[1] >= svarlang_memsz)
198
  if ((buff16[1] >= svarlang_memsz)
211
   || (FREAD(fd, svarlang_dict, svarlang_string_count * 4) != svarlang_string_count * 4)
199
   || (FREAD(fd, svarlang_dict, svarlang_string_count * 4) != svarlang_string_count * 4)
212
   || (FREAD(fd, svarlang_mem, buff16[1]) != buff16[1])) {
200
   || (FREAD(fd, svarlang_mem, buff16[1]) != buff16[1])) {
213
    exitcode = -4;
201
    exitcode = -4;
214
  }
202
  }
215
 
203
 
216
  FCLOSE_AND_EXIT:
204
  FCLOSE_AND_EXIT:
217
 
205
 
218
  FCLOSE(fd);
206
  FCLOSE(fd);
219
  return(exitcode);
207
  return(exitcode);
220
}
208
}
221
 
209