|
| 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 | +} |
0 commit comments