Skip to content

Commit 8463fc6

Browse files
author
peter
committed
first commit
0 parents  commit 8463fc6

File tree

16 files changed

+1236
-0
lines changed

16 files changed

+1236
-0
lines changed

.gitmodules

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[submodule "instead"]
2+
path = instead
3+
url = https://github.com/instead-hub/instead.git
4+
[submodule "metaparser"]
5+
path = metaparser
6+
url = https://github.com/instead-hub/metaparser.git

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
include Makefile.common
2+
3+
LUA_CFLAGS=$(shell pkg-config --cflags luajit)
4+
LUA_LFLAGS=$(shell pkg-config --libs luajit)
5+
6+
CFLAGS += -O2 -Wall -Dunix -D_HAVE_ICONV
7+
8+
EXE=
9+
PLATFORM=unix.c
10+
RM=rm

Makefile.common

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
include Makefile.defs
2+
3+
CFLAGS += $(LUA_CFLAGS) $(EXTRA_CFLAGS) -DSTEAD_PATH=\"${STEADPATH}/\" -DVERSION=\"$(VERSION)\"
4+
5+
LDFLAGS += $(LUA_LFLAGS) $(EXTRA_LDFLAGS)
6+
7+
INSTEAD_SRC := instead.c util.c list.c cache.c idf.c tinymt32.c lfs.c
8+
9+
SRC := $(INSTEAD_SRC)
10+
OBJ := $(patsubst %.c, %.o, $(SRC))
11+
12+
all: instead-cli$(EXE)
13+
14+
$(OBJ): %.o : instead/src/instead/%.c
15+
$(CC) -c $(<) $(I) $(CFLAGS) $(CPPFLAGS) -o $(@)
16+
17+
instead-cli$(EXE): main.c $(OBJ)
18+
$(CC) $(CFLAGS) $(^) $(LDFLAGS) -o $(@)
19+
20+
clean:
21+
$(RM) -f *.o instead-cli$(EXE)
22+
23+
install: all
24+
$(INSTALLD) $(DESTDIR)/$(BIN)
25+
$(INSTALLB) instead-cli$(EXE) $(DESTDIR)/$(BIN)/instead-cli$(EXE)
26+
$(INSTALLD) $(DESTDIR)/$(STEADPATH)
27+
for d in stead2 stead3 stead3/ext; do \
28+
$(INSTALLD) $(DESTDIR)/$(STEADPATH)/$$d; \
29+
$(INSTALL) instead/stead/$$d/*.lua $(DESTDIR)/$(STEADPATH)/$$d/; \
30+
done
31+
32+
for d in morph parser; do \
33+
$(INSTALLD) $(DESTDIR)/$(STEADPATH)/stead3/$$d; \
34+
$(INSTALL) metaparser/$$d/* $(DESTDIR)/$(STEADPATH)/stead3/$$d/; \
35+
done
36+
37+
$(INSTALL) instead/src/tiny/tiny2.lua $(DESTDIR)/$(STEADPATH)/stead2/
38+
$(INSTALL) instead/src/tiny/tiny3.lua $(DESTDIR)/$(STEADPATH)/stead3/
39+
$(INSTALL) instead/src/tiny/tiny.lua $(DESTDIR)/$(STEADPATH)/

Makefile.defs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
VERSION := 3.3.4
2+
3+
PREFIX=./
4+
DESTDIR=
5+
6+
BIN=$(PREFIX)
7+
STEADPATH=$(PREFIX)stead
8+
9+
INSTALLD=install -d -m 0755
10+
INSTALLB=install -m 0755
11+
INSTALL=install -m 0644

Makefile.mingw

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
include Makefile.common
2+
3+
WINDOWS=yes
4+
5+
LUA_CFLAGS=-I./windows/
6+
LUA_LFLAGS=-llua5.1 -L./windows/
7+
8+
CFLAGS += -Wall -mconsole -D_HAVE_ICONV
9+
LDFLAGS += -liconv
10+
11+
CC=i686-w64-mingw32-gcc
12+
13+
RM=rm
14+
EXE=.exe
15+
16+
INSTALLD=install -d -m 0755
17+
INSTALLB=install -m 0755
18+
INSTALL=install -m 0644
19+
20+
PLATFORM=windows.c

instead

Submodule instead added at a6e9287

main.c

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#ifdef _WIN32
5+
#include <windows.h>
6+
#endif
7+
#include "instead/src/instead/instead.h"
8+
9+
#define WIDTH 70
10+
11+
static int log_opt = 0;
12+
static int opt_debug = 0;
13+
static int opt_width = WIDTH;
14+
15+
static int tiny_init(void)
16+
{
17+
int rc;
18+
rc = instead_loadfile(STEAD_PATH"tiny.lua");
19+
if (rc)
20+
return rc;
21+
return 0;
22+
}
23+
static struct instead_ext ext = {
24+
.init = tiny_init,
25+
};
26+
27+
#define utf_cont(p) ((*(p) & 0xc0) == 0x80)
28+
29+
static int utf_ff(const char *s, const char *e)
30+
{
31+
int l = 0;
32+
if (!s || !e)
33+
return 0;
34+
if (s > e)
35+
return 0;
36+
if ((*s & 0x80) == 0) /* ascii */
37+
return 1;
38+
l = 1;
39+
while (s < e && utf_cont(s + 1)) {
40+
s ++;
41+
l ++;
42+
}
43+
return l;
44+
}
45+
46+
static void fmt(const char *str, int width)
47+
{
48+
int l;
49+
const char *ptr = str;
50+
const char *eptr = ptr + strlen(str);
51+
const char *s = str;
52+
const char *last = NULL;
53+
char c;
54+
int w = 0;
55+
while ((l = utf_ff(ptr, eptr))) {
56+
c = *ptr;
57+
ptr += l;
58+
w ++;
59+
if (c == ' ' || c == '\t' || c == '\n')
60+
last = ptr;
61+
if ((width > 0 && w > width && last) || c == '\n') {
62+
while(s != last)
63+
putc(*s++, stdout);
64+
if (c != '\n')
65+
putc('\n', stdout);
66+
w = 0;
67+
last = s;
68+
continue;
69+
}
70+
}
71+
while(s != eptr)
72+
putc(*s++, stdout);
73+
putc('\n', stdout);
74+
}
75+
76+
static char *trim(char *str)
77+
{
78+
char *eptr = str + strlen(str);
79+
while ((*eptr == '\n' || *eptr == 0) && eptr != str) *eptr-- = 0;
80+
return str;
81+
}
82+
83+
static void footer(void)
84+
{
85+
char *p;
86+
p = instead_cmd("way", NULL);
87+
if (p && *p) {
88+
puts(">> "); fmt(trim(p), opt_width);
89+
free(p);
90+
}
91+
p = instead_cmd("inv", NULL);
92+
if (p && *p) {
93+
puts("** "); fmt(trim(p), opt_width);
94+
free(p);
95+
}
96+
}
97+
98+
int main(int argc, const char **argv)
99+
{
100+
int rc, i;
101+
char *str;
102+
const char *game = NULL;
103+
104+
#ifdef _WIN32
105+
SetConsoleOutputCP(1251);
106+
SetConsoleCP(1251);
107+
#endif
108+
for (i = 1; i < argc; i++) {
109+
if (!strcmp(argv[i], "-d"))
110+
opt_debug = 1;
111+
else if (!strncmp(argv[i], "-w", 2))
112+
opt_width = atoi(argv[i] + 2);
113+
else if (!game)
114+
game = argv[i];
115+
}
116+
117+
if (!game) {
118+
fprintf(stderr, "Usage: %s [-d] [-w<width>] <game>\n", argv[0]);
119+
exit(1);
120+
}
121+
122+
if (instead_extension(&ext)) {
123+
fprintf(stderr, "Can't register tiny extension\n");
124+
exit(1);
125+
}
126+
127+
instead_set_debug(opt_debug);
128+
129+
if (instead_init(game)) {
130+
fprintf(stderr, "Can not init game: %s\n", game);
131+
exit(1);
132+
}
133+
if (instead_load(NULL)) {
134+
fprintf(stderr, "Can not load game: %s\n", instead_err());
135+
exit(1);
136+
}
137+
#if 0 /* no autoload */
138+
str = instead_cmd("load autosave", &rc);
139+
#else
140+
str = instead_cmd("look", &rc);
141+
#endif
142+
if (!rc) {
143+
trim(str);
144+
fmt(str, opt_width);
145+
fflush(stdout);
146+
}
147+
free(str);
148+
149+
while (1) {
150+
char input[256], *p, cmd[256 + 64];
151+
printf("> "); fflush(stdout);
152+
p = fgets(input, sizeof(input), stdin);
153+
if (!p)
154+
break;
155+
p[strcspn(p, "\n\r")] = 0;
156+
if (!strcmp(p, "quit"))
157+
break;
158+
if (!strcmp(p, "log")) {
159+
log_opt = 1;
160+
continue;
161+
}
162+
163+
if (!strncmp(p, "load ", 5) || !strncmp(p, "save ", 5)) {
164+
rc = 1; str = NULL;
165+
} else {
166+
snprintf(cmd, sizeof(cmd), "use %s", p); /* try use */
167+
str = instead_cmd(cmd, &rc);
168+
if (rc) { /* try go */
169+
free(str);
170+
snprintf(cmd, sizeof(cmd), "go %s", p);
171+
str = instead_cmd(cmd, &rc);
172+
}
173+
}
174+
if (rc) { /* try act */
175+
free(str);
176+
snprintf(cmd, sizeof(cmd), "%s", p);
177+
str = instead_cmd(cmd, &rc);
178+
}
179+
if (rc && !str) { /* parser? */
180+
snprintf(cmd, sizeof(cmd), "@metaparser \"%s\"", p);
181+
str = instead_cmd(cmd, NULL);
182+
}
183+
if (str) {
184+
trim(str);
185+
fmt(str, opt_width);
186+
fflush(stdout);
187+
}
188+
free(str);
189+
if (!rc) /* no parser */
190+
footer();
191+
if (log_opt)
192+
fprintf(stderr, "%s\n", p);
193+
}
194+
instead_cmd("save autosave", NULL);
195+
instead_done();
196+
exit(0);
197+
}

metaparser

Submodule metaparser added at 96eefd3

0 commit comments

Comments
 (0)