-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
500 lines (469 loc) · 10.7 KB
/
main.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
* Copyright 2009-2022 Peter Kosyh <p.kosyh at gmail.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <locale.h>
#ifdef _WIN32
#include <windows.h>
extern char *dirname(char *);
#else
#include <sys/wait.h>
#endif
#include "instead/src/instead/instead.h"
#include "instead/src/instead/util.h"
#define WIDTH 70
static int opt_log = 0;
static int opt_lua = 0;
static int opt_echo = 0;
static int opt_debug = 0;
static int opt_width = WIDTH;
static char *opt_autoload = NULL;
static const char *mmedia_bin = NULL;
static int opt_autosave = 0;
static int opt_mmedia = 0;
static int need_restart = 0;
static int need_load = 0;
static int need_save = 0;
#ifdef _WIN32
static int opt_codepage = 1251;
#endif
static int luaB_menu(lua_State *L)
{
const char *menu = luaL_optstring(L, 1, NULL);
if (!menu)
return 0;
need_save = !strcmp(menu, "save");
need_load = !strcmp(menu, "load");
return 0;
}
static int luaB_restart(lua_State *L)
{
need_restart = !lua_isboolean(L, 1) || lua_toboolean(L, 1);
return 0;
}
static const luaL_Reg tiny_funcs[] = {
{ "instead_restart", luaB_restart },
{ "instead_menu", luaB_menu },
{ NULL, NULL }
};
static int tiny_init(void)
{
int rc;
char path[1024];
instead_api_register(tiny_funcs);
snprintf(path, sizeof(path), "%s/tiny.lua", instead_lua_path(NULL));
rc = instead_loadfile(path);
if (rc)
return rc;
return 0;
}
static struct instead_ext ext = {
.init = tiny_init,
};
#define utf_cont(p) ((*(p) & 0xc0) == 0x80)
static int utf_ff(const char *s, const char *e)
{
int l = 0;
if (!s || !e)
return 0;
if (s > e)
return 0;
if ((*s & 0x80) == 0) /* ascii */
return 1;
l = 1;
while (s < e && utf_cont(s + 1)) {
s ++;
l ++;
}
return l;
}
static void fmt(const char *str, int width)
{
int l;
const char *ptr = str;
const char *eptr = ptr + strlen(str);
const char *s = str;
const char *last = NULL;
char c;
int w = 0;
while ((l = utf_ff(ptr, eptr))) {
c = *ptr;
ptr += l;
w ++;
if (c == ' ' || c == '\t' || c == '\n')
last = ptr;
if ((width > 0 && w >= width && last && s < last) || c == '\n') {
while(s != last) {
#ifdef _WIN32
int n = utf_ff(s, last);
write(1, s, n);
s += n;
#else
putc(*s++, stdout);
#endif
}
if (c != '\n')
putc('\n', stdout);
fflush(stdout);
w = 0;
last = s;
ptr = s;
continue;
}
}
while(s != eptr) {
#ifdef _WIN32
int n = utf_ff(s, eptr);
write(1, s, n);
s += n;
#else
putc(*s++, stdout);
#endif
}
putc('\n', stdout);
fflush(stdout);
}
static char *trim(char *str)
{
char *eptr = str + strlen(str);
while ((*eptr == '\n' || *eptr == 0 || *eptr == ' ') && eptr != str) *eptr-- = 0;
str += strspn(str, " \t\n\r");
return str;
}
static void footer(void)
{
char *p; int way = 0;
p = instead_cmd("way", NULL);
if (p && *p) {
printf("\n");
way = 1;
printf(">> "); fmt(trim(p), opt_width);
free(p);
}
p = instead_cmd("inv", NULL);
if (p && *p) {
if (!way)
printf("\n");
printf("** "); fmt(trim(p), opt_width);
free(p);
}
}
static void reopen_stderr(const char *fname)
{
if (*fname && freopen(fname, "w", stderr) != stderr) {
fprintf(stderr, "Error opening '%s': %s\n", fname, strerror(errno));
exit(1);
}
}
static void reopen_stdin(const char *fname)
{
if (!fname || !*fname)
fname = "autoscript";
if (freopen(fname, "r", stdin) != stdin) {
fprintf(stderr, "Error opening '%s': %s\n", fname, strerror(errno));
exit(1);
}
}
static char *get_input(const char *prompt)
{
static char input[256];
char *p;
printf("\n%s", prompt); fflush(stdout);
input[0] = 0;
p = fgets(input, sizeof(input), stdin);
if (p && *p) {
p[strcspn(p, "\n\r")] = 0;
}
if (opt_echo && p)
printf("%s\n", p);
return p;
}
static void show_err(void)
{
if (instead_err()) {
printf("Error: %s\n", instead_err());
instead_err_msg(NULL);
}
}
static char *media[] = { NULL, NULL };
static char *media_fn[] = {
"instead.get_picture",
"instead.get_music"
};
static void mmedia_run(const char *f)
{
#ifndef _WIN32
pid_t pid;
int status;
if (!mmedia_bin || !mmedia_bin[0])
return;
pid = fork();
if (pid == -1)
return;
else if (pid > 0) {
waitpid(pid, &status, 0);
} else {
execl(mmedia_bin, mmedia_bin, f, NULL);
exit(1);
}
#else
static char cmd[512];
if (!mmedia_bin || !mmedia_bin[0])
return;
snprintf(cmd, sizeof(cmd), "%s \"%s\"", mmedia_bin, f);
/* not safe! but it is windows... */
system(cmd);
#endif
}
static void mmedia(int t)
{
char *mm;
if (!opt_mmedia)
return;
instead_lock();
instead_function(media_fn[t], NULL);
mm = instead_retval(0);
instead_clear();
instead_unlock();
if (!mm && !media[t])
return;
if ((!media[t] && mm) || (media[t] && !mm) ||
strcmp(media[t], mm)) {
free(media[t]);
media[t] = mm;
if (mm && *mm) { /* changed */
printf("@ %s\n", mm);
if (t == 0) /* we do not support composed images */
mm[strcspn(mm, ";")] = 0;
mmedia_run(mm);
}
}
}
int main(int argc, const char **argv)
{
int rc, i;
char *str;
FILE *log_file = NULL;
const char *game = NULL;
char cmd[256 + 64];
int parser_mode = 0;
int menu_mode = 0;
int opt_args = 0;
#ifdef _WIN32
char cwd[1024];
char base[PATH_MAX];
GetModuleFileNameA(NULL, cwd, 1024);
unix_path(cwd);
snprintf(base, sizeof(base), "%s/%s", dirname(cwd), STEAD_PATH);
instead_lua_path(base);
#endif
setlocale(LC_ALL, "");
for (i = 1; i < argc; i++) {
if (!strncmp(argv[i], "-l", 2)) {
opt_log = 1;
log_file = fopen(argv[i] + 2, "wb");
#ifdef _WIN32
} else if (!strncmp(argv[i], "-cp", 3)) {
opt_codepage = atoi(argv[i] + 3);
#endif
} else if (!strcmp(argv[i], "-a")) {
opt_autoload = strdup("autosave");
opt_autosave = 1;
} else if (!strcmp(argv[i], "-x")) {
opt_lua = 1;
} else if (!strncmp(argv[i], "-m", 2)) {
opt_mmedia = 1;
mmedia_bin = argv[i] + 2;
} else if (!strcmp(argv[i], "-e")) {
opt_echo = 1;
} else if (!strncmp(argv[i], "-d", 2)) {
opt_debug = 1;
reopen_stderr(argv[i] + 2);
} else if (!strncmp(argv[i], "-w", 2)) {
opt_width = atoi(argv[i] + 2);
} else if (!strncmp(argv[i], "-i", 2)) {
reopen_stdin(argv[i] + 2);
} else if (!game) {
game = argv[i];
opt_args = i + 1;
}
}
#ifdef _WIN32
SetConsoleOutputCP(opt_codepage);
SetConsoleCP(opt_codepage);
#endif
if (!game) {
printf("instead-cli %s (by Peter Kosyh '2021-2022)\n", VERSION);
fprintf(stdout, "Usage: %s [-d<file>] [-w<width>] [-i<script>] [-l<log>] [-a] <game>\n", argv[0]);
exit(1);
}
if (instead_extension(&ext)) {
fprintf(stderr, "Can't register tiny extension\n");
exit(1);
}
if (!opt_debug)
fclose(stderr);
instead_set_debug(opt_debug);
if (opt_lua) {
rc = instead_init_lua(dirpath(game), 0);
show_err();
if (rc)
exit(1);
rc = instead_loadscript((char*)game, argc - opt_args, (char **)argv + opt_args, 1);
show_err();
instead_done();
exit(!!rc);
}
restart:
if (instead_init(game)) {
fprintf(stdout, "Can not init game: %s (%s)\n", game, instead_err());
exit(1);
}
#ifdef _WIN32
if (opt_codepage == 65001)
instead_set_encoding("UTF-8");
else {
snprintf(cmd, sizeof(cmd), "CP%d", opt_codepage);
instead_set_encoding(cmd);
}
#endif
if (instead_load(NULL)) {
fprintf(stdout, "Can not load game: %s\n", instead_err());
exit(1);
}
if (opt_autoload && !access(opt_autoload, R_OK)) {
snprintf(cmd, sizeof(cmd), "load %s", opt_autoload);
printf("/%s\n", cmd);
str = instead_cmd(cmd, &rc);
} else
str = instead_cmd("look", &rc);
mmedia(0); mmedia(1);
show_err();
if (!rc && str) {
fmt(trim(str), opt_width);
}
free(str);
if (opt_autoload) {
free(opt_autoload);
opt_autoload = NULL;
}
need_restart = need_load = need_save = 0;
while (1) {
char *p;
int cmd_mode = 0;
p = get_input("> ");
if (!p || !strcmp(p, "/quit")) {
printf("\n");
break;
}
rc = 1; str = NULL;
if (*p == '/') {
p++;
cmd_mode = 1;
snprintf(cmd, sizeof(cmd), "%s", p);
str = instead_cmd(cmd, &rc);
if (rc)
printf("Error!\n");
rc = 0; /* force success */
} else if (!parser_mode) {
snprintf(cmd, sizeof(cmd), "use %s", p); /* try use */
str = instead_cmd(cmd, &rc);
if (rc) { /* try go */
free(str);
snprintf(cmd, sizeof(cmd), "go %s", p);
str = instead_cmd(cmd, &rc);
}
if (!rc)
menu_mode = 1;
}
if (rc && !str && !menu_mode) { /* parser? */
snprintf(cmd, sizeof(cmd), "@metaparser \"%s\"", p);
str = instead_cmd(cmd, &rc);
if (!rc && p[0])
parser_mode = 1;
}
if (rc) { /* try act */
free(str);
snprintf(cmd, sizeof(cmd), "act %s", p);
str = instead_cmd(cmd, &rc);
}
if (str) {
mmedia(0); mmedia(1);
fmt(trim(str), opt_width);
free(str);
}
if (!parser_mode && !cmd_mode)
footer();
if (opt_log) {
if (log_file)
fprintf(log_file, "%s\n", p);
else
fprintf(stderr, "%s\n", p);
}
if (need_restart) {
instead_done();
goto restart;
}
if (need_save) {
puts("?(autosave)"); fflush(stdout);
p = get_input("> ");
if (p && *p)
snprintf(cmd, sizeof(cmd), "save %s", p);
else
snprintf(cmd, sizeof(cmd), "save autosave");
printf("/%s\n", cmd);
str = instead_cmd(cmd, NULL);
if (str)
free(str);
need_save = 0;
} else if (need_load) {
puts("?(autosave)"); fflush(stdout);
p = get_input("> ");
if (opt_autoload)
free(opt_autoload);
if (p && *p)
opt_autoload = strdup(p);
else
opt_autoload = strdup("autosave");
if (!access(opt_autoload, R_OK)) {
instead_done();
goto restart;
} else {
need_load = 0;
printf("No file\n");
free(opt_autoload); opt_autoload = NULL;
}
}
show_err();
}
if (opt_autosave)
free(instead_cmd("save autosave", NULL));
instead_done();
if (log_file)
fclose(log_file);
return 0;
}