Subversion Repositories SvarDOS

Rev

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

Rev 1298 Rev 1299
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>
38
#include <i86.h>
39
typedef unsigned short FHANDLE;
39
typedef unsigned short FHANDLE;
40
 
40
 
41
#endif
41
#endif
42
 
42
 
43
 
43
 
44
#include <string.h>  /* strcpy() */
-
 
45
 
-
 
46
#include "svarlang.h"
44
#include "svarlang.h"
47
 
45
 
48
 
46
 
49
/* supplied through DEFLANG.C */
47
/* supplied through DEFLANG.C */
50
extern char svarlang_mem[];
48
extern char svarlang_mem[];
51
extern unsigned short svarlang_dict[];
49
extern unsigned short svarlang_dict[];
52
extern const unsigned short svarlang_memsz;
50
extern const unsigned short svarlang_memsz;
53
extern const unsigned short svarlang_string_count;
51
extern const unsigned short svarlang_string_count;
54
 
52
 
55
 
53
 
56
const char *svarlang_strid(unsigned short id) {
54
const char *svarlang_strid(unsigned short id) {
57
  size_t left = 0, right = svarlang_string_count - 1, x;
55
  unsigned short left = 0, right = svarlang_string_count - 1, x;
58
  unsigned short v;
56
  unsigned short v;
59
 
57
 
60
  if (svarlang_string_count == 0) return("");
58
  if (svarlang_string_count == 0) return("");
61
 
59
 
62
  while (left <= right) {
60
  while (left <= right) {
63
    x = left + ( (right - left ) >> 2 );
61
    x = left + ((right - left ) >> 2);
64
    v = svarlang_dict[x * 2];
62
    v = svarlang_dict[x * 2];
65
 
63
 
66
    if (id == v) return(svarlang_mem + svarlang_dict[x * 2 + 1]);
64
    if (id == v) return(svarlang_mem + svarlang_dict[x * 2 + 1]);
67
 
65
 
68
    if (id > v) {
66
    if (id > v) {
69
      left = x + 1;
67
      left = x + 1;
70
    } else {
68
    } else {
71
      right = x - 1;
69
      right = x - 1;
72
    }
70
    }
73
  }
71
  }
74
 
72
 
75
  return("");
73
  return("");
76
}
74
}
77
 
75
 
78
 
76
 
79
/* 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
80
 * 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
81
 * 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 */
82
#ifndef WITHSTDIO
80
#ifndef WITHSTDIO
83
static unsigned short FOPEN(const char *s) {
81
static unsigned short FOPEN(const char *s) {
84
  unsigned short fname_seg = FP_SEG(s);
82
  unsigned short fname_seg = FP_SEG(s);
85
  unsigned short fname_off = FP_OFF(s);
83
  unsigned short fname_off = FP_OFF(s);
86
  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 */
87
  _asm {
85
  _asm {
88
    push dx
86
    push dx
89
    push ds
87
    push ds
90
 
88
 
91
    mov ax, fname_seg
89
    mov ax, fname_seg
92
    mov dx, fname_off
90
    mov dx, fname_off
93
    mov ds, ax
91
    mov ds, ax
94
    mov ax, 0x3d00  /* open file, read-only (fname at DS:DX) */
92
    mov ax, 0x3d00  /* open file, read-only (fname at DS:DX) */
95
    int 0x21
93
    int 0x21
96
    pop ds
94
    pop ds
97
    jc ERR
95
    jc ERR
98
    mov res, ax
96
    mov res, ax
99
 
97
 
100
    ERR:
98
    ERR:
101
    pop dx
99
    pop dx
102
  }
100
  }
103
 
101
 
104
  return(res);
102
  return(res);
105
}
103
}
106
 
104
 
107
 
105
 
108
static void FCLOSE(unsigned short handle) {
106
static void FCLOSE(unsigned short handle) {
109
  _asm {
107
  _asm {
110
    push bx
108
    push bx
111
 
109
 
112
    mov ah, 0x3e
110
    mov ah, 0x3e
113
    mov bx, handle
111
    mov bx, handle
114
    int 0x21
112
    int 0x21
115
 
113
 
116
    pop bx
114
    pop bx
117
  }
115
  }
118
}
116
}
119
 
117
 
120
 
118
 
121
static unsigned short FREAD(unsigned short handle, void *buff, unsigned short bytes) {
119
static unsigned short FREAD(unsigned short handle, void *buff, unsigned short bytes) {
122
  unsigned short buff_seg = FP_SEG(buff);
120
  unsigned short buff_seg = FP_SEG(buff);
123
  unsigned short buff_off = FP_OFF(buff);
121
  unsigned short buff_off = FP_OFF(buff);
124
  unsigned short res = 0;
122
  unsigned short res = 0;
125
 
123
 
126
  _asm {
124
  _asm {
127
    push bx
125
    push bx
128
    push cx
126
    push cx
129
    push dx
127
    push dx
130
 
128
 
131
    mov bx, handle
129
    mov bx, handle
132
    mov cx, bytes
130
    mov cx, bytes
133
    mov dx, buff_off
131
    mov dx, buff_off
134
    mov ax, buff_seg
132
    mov ax, buff_seg
135
    push ds
133
    push ds
136
    mov ds, ax
134
    mov ds, ax
137
    mov ah, 0x3f    /* read cx bytes from file handle bx to DS:DX */
135
    mov ah, 0x3f    /* read cx bytes from file handle bx to DS:DX */
138
    int 0x21
136
    int 0x21
139
    pop ds
137
    pop ds
140
    jc ERR
138
    jc ERR
141
 
139
 
142
    mov res, ax
140
    mov res, ax
143
    ERR:
141
    ERR:
144
 
142
 
145
    pop dx
143
    pop dx
146
    pop cx
144
    pop cx
147
    pop bx
145
    pop bx
148
  }
146
  }
149
 
147
 
150
  return(res);
148
  return(res);
151
}
149
}
152
 
150
 
153
 
151
 
154
static void FSEEK(unsigned short handle, unsigned short bytes) {
152
static void FSEEK(unsigned short handle, unsigned short bytes) {
155
  _asm {
153
  _asm {
156
    push bx
154
    push bx
157
    push cx
155
    push cx
158
    push dx
156
    push dx
159
 
157
 
160
    mov ax, 0x4201  /* move file pointer from cur pos + CX:DX */
158
    mov ax, 0x4201  /* move file pointer from cur pos + CX:DX */
161
    mov bx, handle
159
    mov bx, handle
162
    xor cx, cx
160
    xor cx, cx
163
    mov dx, bytes
161
    mov dx, bytes
164
    int 0x21
162
    int 0x21
165
 
163
 
166
    pop dx
164
    pop dx
167
    pop cx
165
    pop cx
168
    pop bx
166
    pop bx
169
  }
167
  }
170
}
168
}
171
#endif
169
#endif
172
 
170
 
173
 
171
 
174
int svarlang_load(const char *fname, const char *lang) {
172
int svarlang_load(const char *fname, const char *lang) {
175
  unsigned short langid;
173
  unsigned short langid;
176
  unsigned long hdr;
174
  unsigned long hdr;
177
  unsigned short buff16[2];
175
  unsigned short buff16[2];
178
  FHANDLE fd;
176
  FHANDLE fd;
179
  unsigned short string_count;
177
  unsigned short string_count;
180
 
178
 
181
  langid = *((unsigned short *)lang);
179
  langid = *((unsigned short *)lang);
182
  langid &= 0xDFDF; /* make sure lang is upcase */
180
  langid &= 0xDFDF; /* make sure lang is upcase */
183
 
181
 
184
  fd = FOPEN(fname);
182
  fd = FOPEN(fname);
185
  if (!fd) return(-1);
183
  if (!fd) return(-1);
186
 
184
 
187
  /* read hdr, should be "SvL\x1a" */
185
  /* read hdr, should be "SvL\x1a" */
188
  if ((FREAD(fd, &hdr, 4) != 4) || (hdr != 0x1a4c7653L)) { /* 0x1a4c7653 = 'SvL\x1a' */
186
  if ((FREAD(fd, &hdr, 4) != 4) || (hdr != 0x1a4c7653L)) { /* 0x1a4c7653 = 'SvL\x1a' */
189
    FCLOSE(fd);
187
    FCLOSE(fd);
190
    return(-3);
188
    return(-3);
191
  }
189
  }
192
 
190
 
193
  /* read string count */
191
  /* read string count */
194
  if ((FREAD(fd, &string_count, 2) != 2) || (string_count != svarlang_string_count)) {
192
  if ((FREAD(fd, &string_count, 2) != 2) || (string_count != svarlang_string_count)) {
195
    FCLOSE(fd);
193
    FCLOSE(fd);
196
    return(-6);
194
    return(-6);
197
  }
195
  }
198
 
196
 
199
  /* read next lang id and string table size in file */
197
  /* read next lang id and string table size in file */
200
  while (FREAD(fd, buff16, 4) == 4) {
198
  while (FREAD(fd, buff16, 4) == 4) {
201
 
199
 
202
    /* is it the lang I am looking for? */
200
    /* is it the lang I am looking for? */
203
    if (buff16[0] != langid) { /* skip to next lang */
201
    if (buff16[0] != langid) { /* skip to next lang */
204
      FSEEK(fd, svarlang_string_count * 4);
202
      FSEEK(fd, svarlang_string_count * 4);
205
      FSEEK(fd, buff16[1]);
203
      FSEEK(fd, buff16[1]);
206
      continue;
204
      continue;
207
    }
205
    }
208
 
206
 
209
    /* found - but do I have enough memory space? */
207
    /* found - but do I have enough memory space? */
210
    if (buff16[1] >= svarlang_memsz) {
208
    if (buff16[1] >= svarlang_memsz) {
211
      FCLOSE(fd);
209
      FCLOSE(fd);
212
      return(-4);
210
      return(-4);
213
    }
211
    }
214
 
212
 
215
    /* load dictionary & strings */
213
    /* load dictionary & strings */
216
    if ((FREAD(fd, svarlang_dict, svarlang_string_count * 4) != svarlang_string_count * 4) ||
214
    if ((FREAD(fd, svarlang_dict, svarlang_string_count * 4) != svarlang_string_count * 4) ||
217
       (FREAD(fd, svarlang_mem, buff16[1]) != buff16[1])) {
215
       (FREAD(fd, svarlang_mem, buff16[1]) != buff16[1])) {
218
      FCLOSE(fd);
216
      FCLOSE(fd);
219
      return -7;
217
      return -7;
220
    }
218
    }
221
    FCLOSE(fd);
219
    FCLOSE(fd);
222
    return(0);
220
    return(0);
223
  }
221
  }
224
 
222
 
225
  FCLOSE(fd);
223
  FCLOSE(fd);
226
  return(-5);
224
  return(-5);
227
}
225
}
228
 
226