-
Notifications
You must be signed in to change notification settings - Fork 2
/
isscrolls.c
338 lines (282 loc) · 7.67 KB
/
isscrolls.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
/*
* Copyright (c) 2021 Matthias Schmidt <xhr@giessen.ccc.de>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/stat.h>
#include <sys/types.h>
#include <limits.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "isscrolls.h"
static char prompt[MAX_PROMPT_LEN];
static char isscrolls_dir[_POSIX_PATH_MAX];
static int debug = 0;
static int color = 0;
static int banner = 1;
static int output = 1;
static int ironsworn = 1;
static int sundered_isles = 0;
static volatile sig_atomic_t sflag = 0;
static void
signal_handler(int signal)
{
switch (signal) {
case SIGINT:
case SIGTERM:
sflag = 1;
break;
}
}
void
show_banner(__attribute__((unused)) char *unused)
{
pm(GREEN, "\n██▓ ██████ ██████ ▄████▄ ██▀███ ▒█████ ██▓ ██▓ ██████\n");
pm(GREEN, "▓██▒▒██ ▒ ▒██ ▒ ▒██▀ ▀█ ▓██ ▒ ██▒▒██▒ ██▒▓██▒ ▓██▒ ▒██ ▒\n");
pm(GREEN, "▒██▒░ ▓██▄ ░ ▓██▄ ▒▓█ ▄ ▓██ ░▄█ ▒▒██░ ██▒▒██░ ▒██░ ░ ▓██▄\n");
pm(GREEN, "░██░ ▒ ██▒ ▒ ██▒▒▓▓▄ ▄██▒▒██▀▀█▄ ▒██ ██░▒██░ ▒██░ ▒ ██▒\n");
pm(GREEN, "░██░▒██████▒▒▒██████▒▒▒ ▓███▀ ░░██▓ ▒██▒░ ████▓▒░░██████▒░██████▒▒██████▒▒\n");
pm(GREEN, "░▓ ▒ ▒▓▒ ▒ ░▒ ▒▓▒ ▒ ░░ ░▒ ▒ ░░ ▒▓ ░▒▓░░ ▒░▒░▒░ ░ ▒░▓ ░░ ▒░▓ ░▒ ▒▓▒ ▒ ░\n");
pm(GREEN, " ▒ ░░ ░▒ ░ ░░ ░▒ ░ ░ ░ ▒ ░▒ ░ ▒░ ░ ▒ ▒░ ░ ░ ▒ ░░ ░ ▒ ░░ ░▒ ░ ░\n");
pm(GREEN, " ▒ ░░ ░ ░ ░ ░ ░ ░ ░░ ░ ░ ░ ░ ▒ ░ ░ ░ ░ ░ ░ ░\n");
pm(GREEN, " ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░ ░\n");
pm(GREEN, " ░\n");
printf(" Version %s\n\n", VERSION);
if (get_si()) {
printf("\n\tAhoy, matey. Sundered Isles mode active!\n\n");
}
printf("\tPlayer toolkit for the Ironsworn tabletop RPG\n");
printf("\tBy Matthias Schmidt - https://cybervillains.com/@_xhr_\n\n");
printf("Enter 'help' for available commands\n\n");
}
int
main(int argc, char **argv)
{
char *line, *res;
int ch;
/*
* Seed the PRNG with the current time of the day. This is not fine
* for a security critical application, for rolling dice it is
*/
srandom(time(NULL) ^ getpid());
while ((ch = getopt(argc, argv, "cdbs")) != -1) {
switch (ch) {
case 'b':
banner = 0;
break;
case 'c':
color = 1;
break;
case 'd':
debug = 1;
break;
case 's':
toggle_si();
toggle_ironsworn();
break;
}
}
argc -= optind;
argv += optind;
setup_base_dir();
initialize_readline(isscrolls_dir);
if (banner)
show_banner(NULL);
if (signal(SIGINT, signal_handler) == SIG_ERR)
log_errx(1, "signal");
if (signal(SIGTERM, signal_handler) == SIG_ERR)
log_errx(1, "signal");
sandbox(isscrolls_dir);
if (load_characters_list() == -1)
set_prompt("> ");
while (!sflag) {
line = readline(prompt);
if (line == NULL)
continue;
res = stripwhite(line);
if (*res) {
add_history(res);
execute_command(res);
}
free(line);
}
shutdown(0);
return 0;
}
void
set_prompt(const char *p)
{
if (p == NULL || strlen(p) == 0)
return;
if (color)
snprintf(prompt, sizeof(prompt), "%s%s%s", ANSI_COLOR_BOLD, p, ANSI_COLOR_RESET);
else
snprintf(prompt, sizeof(prompt), "%s", p);
}
#ifdef __OpenBSD__
void
sandbox(const char *dir)
{
if (unveil(PATH_SHARE_DIR, "r") == -1)
log_errx(1, "unveil");
if (unveil(dir, "rwc") == -1)
log_errx(1, "unveil");
if (unveil(NULL, NULL) == -1)
log_errx(1, "unveil");
if (pledge("stdio rpath wpath cpath tty", NULL) == -1)
log_errx(1, "pledge");
}
#else
void sandbox(__attribute__((unused)) const char *dir)
{
}
#endif /* __OpenBSD__ */
void
cmd_quit(__attribute__((unused)) char *unused)
{
shutdown(0);
}
void
shutdown(int exit_code)
{
char hist_path[_POSIX_PATH_MAX];
int ret;
save_current_character();
ret = snprintf(hist_path, sizeof(hist_path), "%s/history", isscrolls_dir);
if (ret < 0 || (size_t)ret >= sizeof(hist_path)) {
printf("Path truncation happened. Buffer to short to fit %s\n", hist_path);
}
log_debug("Writing history to %s\n", hist_path);
write_history(hist_path);
exit(exit_code);
}
void
setup_base_dir(void)
{
struct stat sb;
char *home, *xdg_home;
int ret;
if ((xdg_home = getenv("XDG_CONFIG_HOME")) != NULL) {
ret = snprintf(isscrolls_dir, sizeof(isscrolls_dir), "%s/isscrolls", xdg_home);
if (ret < 0 || (size_t)ret >= sizeof(isscrolls_dir)) {
log_errx(1, "Path truncation happened. Buffer to short to fit %s\n", isscrolls_dir);
}
} else if ((home = getenv("HOME")) != NULL) {
ret = snprintf(isscrolls_dir, sizeof(isscrolls_dir), "%s/.config/isscrolls", home);
if (ret < 0 || (size_t)ret >= sizeof(isscrolls_dir)) {
log_errx(1, "Path truncation happened. Buffer to short to fit %s\n", isscrolls_dir);
}
} else {
log_errx(1, "Neither $XDG_CONFIG_HOME nor $HOME is set!\n");
}
if (stat(isscrolls_dir, &sb) == 0 && S_ISDIR(sb.st_mode)) {
log_debug("%s already exists\n", isscrolls_dir);
} else {
log_debug("%s does not exists. Attempt to create it\n", isscrolls_dir);
if (mkdir(isscrolls_dir, 0755) == -1) {
log_errx(1, "Cannot create %s directory\n", isscrolls_dir);
}
}
}
void
log_debug(const char *fmt, ...)
{
va_list ap;
if (debug == 0)
return;
va_start(ap, fmt);
fprintf(stdout, "[*] ");
vfprintf(stdout, fmt, ap);
va_end(ap);
}
void
log_errx(int prio, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
shutdown(prio);
}
void
pm(int what, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
if (color) {
switch (what) {
case RED:
fprintf(stdout, ANSI_COLOR_RED);
break;
case YELLOW:
fprintf(stdout, ANSI_COLOR_YELLOW);
break;
case GREEN:
fprintf(stdout, ANSI_COLOR_GREEN);
break;
default:
break;
}
}
vfprintf(stdout, fmt, ap);
if (color)
fprintf(stdout, ANSI_COLOR_RESET);
va_end(ap);
}
const char*
get_isscrolls_dir(void)
{
return isscrolls_dir;
}
void
toggle_output(void)
{
output = !output;
}
int
get_output(void)
{
return output;
}
int
get_color(void)
{
return color;
}
int
get_ironsworn(void)
{
return ironsworn;
}
int
get_si(void)
{
return sundered_isles;
}
void
toggle_ironsworn(void)
{
ironsworn = !ironsworn;
}
void
toggle_si(void)
{
sundered_isles = !sundered_isles;
}