-
Notifications
You must be signed in to change notification settings - Fork 11
/
gtmisc.c
288 lines (256 loc) · 8.37 KB
/
gtmisc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* gtmisc.c: Miscellaneous functions
for GlkTerm, curses.h implementation of the Glk API.
Designed by Andrew Plotkin <erkyrath@eblong.com>
http://www.eblong.com/zarf/glk/index.html
*/
#include "gtoption.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curses.h>
#include "glk.h"
#include "glkterm.h"
static unsigned char char_tolower_table[256];
static unsigned char char_toupper_table[256];
unsigned char char_printable_table[256];
unsigned char char_typable_table[256];
static unsigned char char_A0_FF_typable[6*16] = OPT_AO_FF_TYPABLE;
#ifndef OPT_NATIVE_LATIN_1
static unsigned char char_A0_FF_output[6*16] = OPT_AO_FF_OUTPUT;
unsigned char char_from_native_table[256];
unsigned char char_to_native_table[256];
#endif /* OPT_NATIVE_LATIN_1 */
gidispatch_rock_t (*gli_register_obj)(void *obj, glui32 objclass) = NULL;
void (*gli_unregister_obj)(void *obj, glui32 objclass, gidispatch_rock_t objrock) = NULL;
gidispatch_rock_t (*gli_register_arr)(void *array, glui32 len, char *typecode) = NULL;
void (*gli_unregister_arr)(void *array, glui32 len, char *typecode,
gidispatch_rock_t objrock) = NULL;
static char *char_A0_FF_to_ascii[6*16] = {
" ", "!", "c", "Lb", NULL, "Y", "|", NULL,
NULL, "(C)", NULL, "<<", NULL, "-", "(R)", NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, "*",
NULL, NULL, NULL, ">>", "1/4", "1/2", "3/4", "?",
"A", "A", "A", "A", "A", "A", "AE", "C",
"E", "E", "E", "E", "I", "I", "I", "I",
NULL, "N", "O", "O", "O", "O", "O", "x",
"O", "U", "U", "U", "U", "Y", NULL, "ss",
"a", "a", "a", "a", "a", "a", "ae", "c",
"e", "e", "e", "e", "i", "i", "i", "i",
NULL, "n", "o", "o", "o", "o", "o", "/",
"o", "u", "u", "u", "u", "y", NULL, "y",
};
/* Set up things. This is called from main(). */
void gli_initialize_misc()
{
int ix;
/* Initialize the to-uppercase and to-lowercase tables. These should
*not* be localized to a platform-native character set! They are
intended to work on Latin-1 data, and the code below correctly
sets up the tables for that character set. */
for (ix=0; ix<256; ix++) {
char_toupper_table[ix] = ix;
char_tolower_table[ix] = ix;
}
for (ix=0; ix<256; ix++) {
int lower_equiv;
if (ix >= 'A' && ix <= 'Z') {
lower_equiv = ix + ('a' - 'A');
}
else if (ix >= 0xC0 && ix <= 0xDE && ix != 0xD7) {
lower_equiv = ix + 0x20;
}
else {
lower_equiv = 0;
}
if (lower_equiv) {
char_tolower_table[ix] = lower_equiv;
char_toupper_table[lower_equiv] = ix;
}
}
#ifndef OPT_NATIVE_LATIN_1
for (ix=0; ix<256; ix++) {
if (ix <= 0x7E)
char_from_native_table[ix] = ix;
else
char_from_native_table[ix] = 0;
}
#endif /* OPT_NATIVE_LATIN_1 */
for (ix=0; ix<256; ix++) {
unsigned char native_equiv;
int cantype, canprint;
native_equiv = ix;
if (ix < 0x20) {
/* Many control characters are untypable, for many reasons. */
if (ix == '\t' || ix == '\014' /* reserved by the input system */
#ifdef OPT_USE_SIGNALS
|| ix == '\003' || ix == '\032' /* interrupt/suspend signals */
#endif
|| ix == '\010' /* parsed as keycode_Delete */
|| ix == '\012' || ix == '\015' /* parsed as keycode_Return */
|| ix == '\033') /* parsed as keycode_Escape */
cantype = FALSE;
else
cantype = TRUE;
/* The newline is printable, but no other control characters. */
if (ix == '\012')
canprint = TRUE;
else
canprint = FALSE;
}
else if (ix <= 0x7E) {
cantype = TRUE;
canprint = TRUE;
}
else if (ix < 0xA0) {
cantype = FALSE;
canprint = FALSE;
}
else {
cantype = char_A0_FF_typable[ix - 0xA0];
#ifdef OPT_NATIVE_LATIN_1
canprint = TRUE;
#else /* OPT_NATIVE_LATIN_1 */
native_equiv = char_A0_FF_output[ix - 0xA0];
cantype = cantype && native_equiv; /* If it can't be printed exactly, it certainly
can't be typed. */
canprint = (native_equiv != 0);
#endif /* OPT_NATIVE_LATIN_1 */
}
char_typable_table[ix] = cantype;
char_printable_table[ix] = canprint;
#ifndef OPT_NATIVE_LATIN_1
char_to_native_table[ix] = native_equiv;
if (native_equiv)
char_from_native_table[native_equiv] = ix;
#endif /* OPT_NATIVE_LATIN_1 */
}
#ifndef OPT_NATIVE_LATIN_1
char_from_native_table[0] = '\0'; /* The little dance above misses this
entry, for dull reasons. */
#endif /* OPT_NATIVE_LATIN_1 */
}
void glk_exit()
{
gli_msgin_getchar("Hit any key to exit.", TRUE);
gli_streams_close_all();
endwin();
putchar('\n');
exit(0);
}
void glk_set_interrupt_handler(void (*func)(void))
{
gli_interrupt_handler = func;
}
void glk_tick()
{
/* Nothing to do here. */
}
void gidispatch_set_object_registry(
gidispatch_rock_t (*regi)(void *obj, glui32 objclass),
void (*unregi)(void *obj, glui32 objclass, gidispatch_rock_t objrock))
{
window_t *win;
stream_t *str;
fileref_t *fref;
gli_register_obj = regi;
gli_unregister_obj = unregi;
if (gli_register_obj) {
/* It's now necessary to go through all existing objects, and register
them. */
for (win = glk_window_iterate(NULL, NULL);
win;
win = glk_window_iterate(win, NULL)) {
win->disprock = (*gli_register_obj)(win, gidisp_Class_Window);
}
for (str = glk_stream_iterate(NULL, NULL);
str;
str = glk_stream_iterate(str, NULL)) {
str->disprock = (*gli_register_obj)(str, gidisp_Class_Stream);
}
for (fref = glk_fileref_iterate(NULL, NULL);
fref;
fref = glk_fileref_iterate(fref, NULL)) {
fref->disprock = (*gli_register_obj)(fref, gidisp_Class_Fileref);
}
}
}
void gidispatch_set_retained_registry(
gidispatch_rock_t (*regi)(void *array, glui32 len, char *typecode),
void (*unregi)(void *array, glui32 len, char *typecode,
gidispatch_rock_t objrock))
{
gli_register_arr = regi;
gli_unregister_arr = unregi;
}
gidispatch_rock_t gidispatch_get_objrock(void *obj, glui32 objclass)
{
switch (objclass) {
case gidisp_Class_Window:
return ((window_t *)obj)->disprock;
case gidisp_Class_Stream:
return ((stream_t *)obj)->disprock;
case gidisp_Class_Fileref:
return ((fileref_t *)obj)->disprock;
default: {
gidispatch_rock_t dummy;
dummy.num = 0;
return dummy;
}
}
}
void gidispatch_set_autorestore_registry(
long (*locatearr)(void *array, glui32 len, char *typecode,
gidispatch_rock_t objrock, int *elemsizeref),
gidispatch_rock_t (*restorearr)(long bufkey, glui32 len,
char *typecode, void **arrayref))
{
/* GlkTerm is not able to serialize its UI state. Therefore, it
does not have the capability of autosaving and autorestoring.
Therefore, it will never call these hooks. Therefore, we ignore
them and do nothing here. */
}
unsigned char glk_char_to_lower(unsigned char ch)
{
return char_tolower_table[ch];
}
unsigned char glk_char_to_upper(unsigned char ch)
{
return char_toupper_table[ch];
}
char *gli_ascii_equivalent(unsigned char ch)
{
static char buf[5];
if (ch >= 0xA0 && char_A0_FF_to_ascii[ch - 0xA0]) {
return char_A0_FF_to_ascii[ch - 0xA0];
}
buf[0] = '\\';
buf[1] = '0' + ((ch >> 6) & 7);
buf[2] = '0' + ((ch >> 3) & 7);
buf[3] = '0' + ((ch) & 7);
buf[4] = '\0';
return buf;
}
#ifdef NO_MEMMOVE
void *memmove(void *destp, void *srcp, int n)
{
char *dest = (char *)destp;
char *src = (char *)srcp;
if (dest < src) {
for (; n > 0; n--) {
*dest = *src;
dest++;
src++;
}
}
else if (dest > src) {
src += n;
dest += n;
for (; n > 0; n--) {
dest--;
src--;
*dest = *src;
}
}
return destp;
}
#endif /* NO_MEMMOVE */