Skip to content

Commit c8135ef

Browse files
author
Peter Kosyh
committed
load/save and restart
1 parent 1d6d66b commit c8135ef

File tree

2 files changed

+106
-22
lines changed

2 files changed

+106
-22
lines changed

main.c

Lines changed: 101 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <stdlib.h>
33
#include <string.h>
44
#include <errno.h>
5+
#include <unistd.h>
6+
#include <locale.h>
57
#ifdef _WIN32
68
#include <windows.h>
79
#endif
@@ -12,10 +14,39 @@
1214
static int opt_log = 0;
1315
static int opt_debug = 0;
1416
static int opt_width = WIDTH;
17+
static char *opt_autoload = NULL;
18+
19+
static int need_restart = 0;
20+
static int need_load = 0;
21+
static int need_save = 0;
22+
static int parser_mode = 0;
23+
24+
static int luaB_menu(lua_State *L)
25+
{
26+
const char *menu = luaL_optstring(L, 1, NULL);
27+
if (!menu)
28+
return 0;
29+
need_save = !strcmp(menu, "save");
30+
need_load = !strcmp(menu, "load");
31+
return 0;
32+
}
33+
34+
static int luaB_restart(lua_State *L)
35+
{
36+
need_restart = !lua_isboolean(L, 1) || lua_toboolean(L, 1);
37+
return 0;
38+
}
39+
40+
static const luaL_Reg tiny_funcs[] = {
41+
{ "instead_restart", luaB_restart },
42+
{ "instead_menu", luaB_menu },
43+
{ NULL, NULL }
44+
};
1545

1646
static int tiny_init(void)
1747
{
1848
int rc;
49+
instead_api_register(tiny_funcs);
1950
rc = instead_loadfile(STEAD_PATH"tiny.lua");
2051
if (rc)
2152
return rc;
@@ -77,7 +108,8 @@ static void fmt(const char *str, int width)
77108
static char *trim(char *str)
78109
{
79110
char *eptr = str + strlen(str);
80-
while ((*eptr == '\n' || *eptr == 0) && eptr != str) *eptr-- = 0;
111+
while ((*eptr == '\n' || *eptr == 0 || *eptr == ' ') && eptr != str) *eptr-- = 0;
112+
str += strspn(str, " \t\n\r");
81113
return str;
82114
}
83115

@@ -104,11 +136,24 @@ static void reopen_stderr(const char *fname)
104136
}
105137
}
106138

139+
static char *get_input(void)
140+
{
141+
static char input[256], *p;
142+
input[0] = 0;
143+
p = fgets(input, sizeof(input), stdin);
144+
if (p && *p) {
145+
p[strcspn(p, "\n\r")] = 0;
146+
}
147+
return p;
148+
}
149+
107150
int main(int argc, const char **argv)
108151
{
109152
int rc, i;
110153
char *str;
111154
const char *game = NULL;
155+
char cmd[256 + 64];
156+
setlocale(LC_ALL, "");
112157
#ifdef _WIN32
113158
SetConsoleOutputCP(1251);
114159
SetConsoleCP(1251);
@@ -146,7 +191,7 @@ int main(int argc, const char **argv)
146191
fclose(stderr);
147192

148193
instead_set_debug(opt_debug);
149-
194+
restart:
150195
if (instead_init(game)) {
151196
fprintf(stdout, "Can not init game: %s\n", game);
152197
exit(1);
@@ -155,29 +200,32 @@ int main(int argc, const char **argv)
155200
fprintf(stdout, "Can not load game: %s\n", instead_err());
156201
exit(1);
157202
}
158-
#if 0 /* no autoload */
159-
str = instead_cmd("load autosave", &rc);
160-
#else
161-
str = instead_cmd("look", &rc);
162-
#endif
163-
if (!rc) {
164-
trim(str);
165-
fmt(str, opt_width);
203+
if (opt_autoload) {
204+
snprintf(cmd, sizeof(cmd), "load %s", opt_autoload);
205+
printf("%s\n", cmd);
206+
str = instead_cmd(cmd, &rc);
207+
} else
208+
str = instead_cmd("look", &rc);
209+
if (!rc && str) {
210+
fmt(trim(str), opt_width);
166211
fflush(stdout);
167212
}
168213
free(str);
169-
214+
if (opt_autoload) {
215+
free(opt_autoload);
216+
opt_autoload = NULL;
217+
}
218+
need_restart = need_load = need_save = 0;
170219
while (1) {
171-
char input[256], *p, cmd[256 + 64];
220+
char *p;
172221
printf("> "); fflush(stdout);
173-
p = fgets(input, sizeof(input), stdin);
222+
p = get_input();
174223
if (!p)
175224
break;
176-
p[strcspn(p, "\n\r")] = 0;
177-
if (!strcmp(p, "quit"))
225+
if (!strcmp(p, "/quit"))
178226
break;
179227

180-
if (!strncmp(p, "load ", 5) || !strncmp(p, "save ", 5)) {
228+
if (!strncmp(p, "/load ", 6) || !strncmp(p, "/save ", 6)) {
181229
rc = 1; str = NULL;
182230
} else {
183231
snprintf(cmd, sizeof(cmd), "use %s", p); /* try use */
@@ -194,19 +242,53 @@ int main(int argc, const char **argv)
194242
str = instead_cmd(cmd, &rc);
195243
}
196244
if (rc && !str) { /* parser? */
245+
parser_mode = 1;
197246
snprintf(cmd, sizeof(cmd), "@metaparser \"%s\"", p);
198247
str = instead_cmd(cmd, NULL);
199248
}
200249
if (str) {
201-
trim(str);
202-
fmt(str, opt_width);
250+
fmt(trim(str), opt_width);
203251
fflush(stdout);
204252
}
205253
free(str);
206-
if (!rc) /* no parser */
254+
if (!rc && !parser_mode) /* no parser */
207255
footer();
208256
if (opt_log)
209257
fprintf(stderr, "%s\n", p);
258+
if (need_restart) {
259+
instead_done();
260+
goto restart;
261+
}
262+
if (need_save) {
263+
puts("?(autosave)"); fflush(stdout);
264+
p = get_input();
265+
if (p && *p)
266+
snprintf(cmd, sizeof(cmd), "save %s", p);
267+
else
268+
snprintf(cmd, sizeof(cmd), "save autosave");
269+
printf("%s\n", cmd);
270+
str = instead_cmd(cmd, NULL);
271+
if (str)
272+
free(str);
273+
need_save = 0;
274+
} else if (need_load) {
275+
puts("?(autosave)"); fflush(stdout);
276+
p = get_input();
277+
if (opt_autoload)
278+
free(opt_autoload);
279+
if (p && *p)
280+
opt_autoload = strdup(p);
281+
else
282+
opt_autoload = strdup("autosave");
283+
if (!access(opt_autoload, R_OK)) {
284+
instead_done();
285+
goto restart;
286+
} else {
287+
need_load = 0;
288+
printf("No file\n");
289+
free(opt_autoload); opt_autoload = NULL;
290+
}
291+
}
210292
}
211293
instead_cmd("save autosave", NULL);
212294
instead_done();

tiny.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
if API == 'stead3' then
88
require 'tiny3'
9-
return
9+
local instead = std '@instead'
10+
instead.restart = instead_restart
11+
instead.menu = instead_menu
12+
else
13+
require 'tiny2'
1014
end
11-
12-
require 'tiny2'

0 commit comments

Comments
 (0)