Subversion Repositories SvarDOS

Rev

Rev 1782 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1782 Rev 1783
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-2024 Mateusz Viste
4
 * Copyright (C) 2021-2024 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
 * I use pragma aux directives for more compact size. open-watcom only. */
80
 * I use pragma aux directives for more compact size. open-watcom only. */
81
#ifndef WITHSTDIO
81
#ifndef WITHSTDIO
82
 
82
 
83
static unsigned short FOPEN(const char far *s);
83
static unsigned short FOPEN(const char far *s);
84
 
84
 
85
#pragma aux FOPEN = \
85
#pragma aux FOPEN = \
86
"push ds" \
86
"push ds" \
87
"push es" \
87
"push es" \
88
"pop ds" \
88
"pop ds" \
89
"mov ax, 0x3D00" /* open file, read-only (fname at DS:DX) */ \
89
"mov ax, 0x3D00" /* open file, read-only (fname at DS:DX) */ \
90
"int 0x21" \
90
"int 0x21" \
91
"jnc DONE" \
91
"jnc DONE" \
92
"xor ax, ax" \
92
"xor ax, ax"     /* return 0 on error */ \
93
"DONE:" \
93
"DONE:" \
94
"pop ds" \
94
"pop ds" \
95
parm [es dx] \
95
parm [es dx] \
96
value [ax];
96
value [ax];
97
 
97
 
98
 
98
 
99
static void FCLOSE(unsigned short handle);
99
static void FCLOSE(unsigned short handle);
100
 
100
 
101
#pragma aux FCLOSE = \
101
#pragma aux FCLOSE = \
102
"mov ah, 0x3E" \
102
"mov ah, 0x3E" \
103
"int 0x21" \
103
"int 0x21" \
104
modify [ax]  /* AX might contain an error code on failure */ \
104
modify [ax]  /* AX might contain an error code on failure */ \
105
parm [bx]
105
parm [bx]
106
 
106
 
107
 
107
 
108
static unsigned short FREAD(unsigned short handle, void far *buff, unsigned short bytes);
108
static unsigned short FREAD(unsigned short handle, void far *buff, unsigned short bytes);
109
 
109
 
110
#pragma aux FREAD = \
110
#pragma aux FREAD = \
111
"push ds" \
111
"push ds" \
112
"push es" \
112
"push es" \
113
"pop ds" \
113
"pop ds" \
114
"mov ah, 0x3F"    /* read cx bytes from file handle bx to DS:DX */ \
114
"mov ah, 0x3F"    /* read cx bytes from file handle bx to DS:DX */ \
115
"int 0x21" \
115
"int 0x21" \
116
"jnc ERR" \
116
"jnc ERR" \
117
"xor ax, ax"      /* return 0 on error */ \
117
"xor ax, ax"      /* return 0 on error */ \
118
"ERR:" \
118
"ERR:" \
119
"pop ds" \
119
"pop ds" \
120
parm [bx] [es dx] [cx] \
120
parm [bx] [es dx] [cx] \
121
value [ax]
121
value [ax]
122
 
122
 
123
 
123
 
124
static void FSEEK(unsigned short handle, unsigned short bytes);
124
static void FSEEK(unsigned short handle, unsigned short bytes);
125
 
125
 
126
#pragma aux FSEEK = \
126
#pragma aux FSEEK = \
127
"mov ax, 0x4201"  /* move file pointer from cur pos + CX:DX */ \
127
"mov ax, 0x4201"  /* move file pointer from cur pos + CX:DX */ \
128
"xor cx, cx" \
128
"xor cx, cx" \
129
"int 0x21" \
129
"int 0x21" \
130
parm [bx] [dx] \
130
parm [bx] [dx] \
131
modify [ax cx dx]
131
modify [ax cx dx]
132
 
132
 
133
#endif
133
#endif
134
 
134
 
135
 
135
 
136
int svarlang_load(const char *fname, const char *lang) {
136
int svarlang_load(const char *fname, const char *lang) {
137
  unsigned short langid;
137
  unsigned short langid;
138
  unsigned short buff16[2];
138
  unsigned short buff16[2];
139
  FHANDLE fd;
139
  FHANDLE fd;
140
  signed char exitcode = 0;
140
  signed char exitcode = 0;
141
  struct {
141
  struct {
142
    unsigned long sig;
142
    unsigned long sig;
143
    unsigned short string_count;
143
    unsigned short string_count;
144
  } hdr;
144
  } hdr;
145
 
145
 
146
  langid = *((unsigned short *)lang);
146
  langid = *((unsigned short *)lang);
147
  langid &= 0xDFDF; /* make sure lang is upcase */
147
  langid &= 0xDFDF; /* make sure lang is upcase */
148
 
148
 
149
  fd = FOPEN(fname);
149
  fd = FOPEN(fname);
150
  if (!fd) return(-1);
150
  if (!fd) return(-1);
151
 
151
 
152
  /* read hdr, sig should be "SvL\x1a" (0x1a4c7653) */
152
  /* read hdr, sig should be "SvL\x1a" (0x1a4c7653) */
153
  if ((FREAD(fd, &hdr, 6) != 6) || (hdr.sig != 0x1a4c7653L) || (hdr.string_count != svarlang_string_count)) {
153
  if ((FREAD(fd, &hdr, 6) != 6) || (hdr.sig != 0x1a4c7653L) || (hdr.string_count != svarlang_string_count)) {
154
    exitcode = -2;
154
    exitcode = -2;
155
    goto FCLOSE_AND_EXIT;
155
    goto FCLOSE_AND_EXIT;
156
  }
156
  }
157
 
157
 
158
  for (;;) {
158
  for (;;) {
159
    /* read next lang id and string table size in file */
159
    /* read next lang id and string table size in file */
160
    if (FREAD(fd, buff16, 4) != 4) {
160
    if (FREAD(fd, buff16, 4) != 4) {
161
      exitcode = -3;
161
      exitcode = -3;
162
      goto FCLOSE_AND_EXIT;
162
      goto FCLOSE_AND_EXIT;
163
    }
163
    }
164
 
164
 
165
    /* is it the lang I am looking for? */
165
    /* is it the lang I am looking for? */
166
    if (buff16[0] == langid) break;
166
    if (buff16[0] == langid) break;
167
 
167
 
168
    /* skip to next lang (in two steps to avoid a potential uint16 overflow) */
168
    /* skip to next lang (in two steps to avoid a potential uint16 overflow) */
169
    FSEEK(fd, svarlang_string_count * 4);
169
    FSEEK(fd, svarlang_string_count * 4);
170
    FSEEK(fd, buff16[1]);
170
    FSEEK(fd, buff16[1]);
171
  }
171
  }
172
 
172
 
173
  /* load dictionary & strings, but only if I have enough memory space */
173
  /* load dictionary & strings, but only if I have enough memory space */
174
  if ((buff16[1] >= svarlang_memsz)
174
  if ((buff16[1] >= svarlang_memsz)
175
   || (FREAD(fd, svarlang_dict, svarlang_string_count * 4) != svarlang_string_count * 4)
175
   || (FREAD(fd, svarlang_dict, svarlang_string_count * 4) != svarlang_string_count * 4)
176
   || (FREAD(fd, svarlang_mem, buff16[1]) != buff16[1])) {
176
   || (FREAD(fd, svarlang_mem, buff16[1]) != buff16[1])) {
177
    exitcode = -4;
177
    exitcode = -4;
178
  }
178
  }
179
 
179
 
180
  FCLOSE_AND_EXIT:
180
  FCLOSE_AND_EXIT:
181
 
181
 
182
  FCLOSE(fd);
182
  FCLOSE(fd);
183
  return(exitcode);
183
  return(exitcode);
184
}
184
}
185
 
185