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
|
// We don't have `getopt` on all platforms (notably, it is not available on
// Windows), so detect whether or not it is there and avoid command line
// parsing if it is not available.
#if !defined(PW_NO_CLI) && (defined(__unix__) || defined(__APPLE__))
#define _POSIX_C_SOURCE 200809L
#include <unistd.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <SDL3/SDL_filesystem.h>
#include "config.h"
#include "path.h"
static void usage(char const *name) {
printf("Usage: %s [-c <carts>] [-d] [-S <mode>] [-v[v[v]]]\n"
" -c <carts> Carts directory.\n"
" -d Print additional debug statistics.\n"
" -m <mb> Memory to allocate for PICO-8 in MB.\n"
" -S <mode> Scale mode: 'pp' (default), 'fill' or 'stretch'.\n"
" -v[v[v]] Increasingly verbose logging.\n",
pw_basename(name));
}
static void print_err(char const *name, char const *err) {
if (err && *err) {
fprintf(stderr, "%s: %s\n", pw_basename(name), err);
}
}
struct named_value {
char const *k;
int v;
};
static bool parse_named_value(struct named_value *values, size_t size,
char const *s, int *v) {
for (size_t i = 0; i < size; ++i) {
if (!SDL_strcmp(s, values[i].k)) {
*v = values[i].v;
return true;
}
}
return false;
}
static bool parse_scale_mode(char const *s, enum pw_scale_mode *m) {
struct named_value values[] = {
{"pp", PW_SCALE_MODE_PIXEL_PERFECT},
{"fill", PW_SCALE_MODE_FILL},
{"stretch", PW_SCALE_MODE_STRETCH},
};
int v;
if (!parse_named_value(values, SDL_arraysize(values), s, &v)) {
SDL_SetError("invalid scaling mode '%s'", s);
return false;
}
*m = (enum pw_scale_mode)v;
return true;
}
static bool parse_size(char const *s, size_t *m) {
errno = 0;
char *end = NULL;
long long v = SDL_strtoll(s, &end, 10);
if (end == s || *end != '\0') {
return SDL_SetError("invalid digit %c", *end);
}
if (errno == ERANGE || v < 0 ||
(unsigned long long)v > (unsigned long long)SIZE_MAX) {
return SDL_SetError("size out of range");
}
*m = (size_t)v;
return true;
}
static bool parse_log_priority(char const *s, SDL_LogPriority *l) {
struct named_value values[] = {
{"trace", SDL_LOG_PRIORITY_TRACE},
{"verbose", SDL_LOG_PRIORITY_VERBOSE},
{"debug", SDL_LOG_PRIORITY_DEBUG},
{"info", SDL_LOG_PRIORITY_INFO},
{"warn", SDL_LOG_PRIORITY_WARN},
{"error", SDL_LOG_PRIORITY_ERROR},
{"critical", SDL_LOG_PRIORITY_CRITICAL},
};
int v;
if (!parse_named_value(values, SDL_arraysize(values), s, &v)) {
SDL_SetError("invalid verbosity '%s'", s);
return false;
}
*l = (SDL_LogPriority)v;
return true;
}
static bool parse_bool(char const *s, bool *b) {
struct named_value values[] = {
{"no", false}, {"false", false}, {"0", false},
{"yes", true}, {"true", true}, {"1", true},
};
int v;
if (!parse_named_value(values, SDL_arraysize(values), s, &v)) {
SDL_SetError("invalid boolean flag '%s'", s);
return false;
}
*b = (bool)v;
return true;
}
static void parse_config_ini(struct pw_config *config) {
char ini[1024];
if (!pw_pathjoin(ini, sizeof(ini), config->carts, "config.ini", NULL)) {
goto error;
}
size_t len;
char *data = SDL_LoadFile(ini, &len);
if (!data) {
goto error;
}
char *pos = data;
char line[1024], key[32], value[992];
int n;
while (pos < data + len) {
if (SDL_sscanf(pos++, "%1023[^\n]%n", line, &n) != 1) {
// Skip empty lines.
continue;
}
pos += n;
if (*line == '#') {
continue;
}
if (SDL_sscanf(line, " %31[^= \t] = %991[^\r\n]", key, value) != 2) {
SDL_LogWarn(0, "invalid configuration line '%s'", line);
continue;
}
if (!SDL_strcmp(key, "debug")) {
if (!parse_bool(value, &config->debug)) {
SDL_LogWarn(0, "%s", SDL_GetError());
SDL_ClearError();
}
} else if (!SDL_strcmp(key, "carts")) {
if (config->carts) {
SDL_free(config->carts);
}
config->carts = SDL_strdup(value);
} else if (!SDL_strcmp(key, "mode")) {
if (!parse_scale_mode(value, &config->mode)) {
SDL_LogWarn(0, "%s", SDL_GetError());
SDL_ClearError();
}
} else if (!SDL_strcmp(key, "memory")) {
if (!parse_size(value, &config->memory)) {
SDL_LogWarn(0, "%s", SDL_GetError());
SDL_ClearError();
}
} else if (!SDL_strcmp(key, "verbosity")) {
if (!parse_log_priority(value, &config->logp)) {
SDL_LogWarn(0, "%s", SDL_GetError());
SDL_ClearError();
}
}
}
SDL_free(data);
return;
error:
// We are very permissive with configuration loading, where we just log a
// warning and continue without propagating an error.
SDL_LogWarn(0, "failed to read config.ini: %s", SDL_GetError());
SDL_ClearError();
return;
}
struct pw_config *pw_config_load(int argc, char **argv) {
if (!argv) {
SDL_InvalidParamError("argv");
return NULL;
}
char const *name = "picow";
if (argc) {
name = argv[0];
}
struct pw_config *config = SDL_malloc(sizeof(*config));
if (!config) {
return NULL;
}
config->carts = SDL_GetPrefPath("PICO-w", "picow");
if (!config->carts) {
goto cleanup;
}
config->mode = PW_SCALE_MODE_PIXEL_PERFECT;
config->memory = 128;
config->logp = SDL_GetLogPriority(0);
config->debug = false;
// Read default values from the `config.ini` file before parsing command line
// arguments, as they take priority.
parse_config_ini(config);
#if defined(_POSIX_VERSION)
int c;
optind = 1;
while ((c = getopt(argc, argv, ":hc:dm:S:v")) != -1) {
switch (c) {
case 'h':
usage(name);
SDL_ClearError();
goto cleanup;
case 'c':
if (config->carts) {
SDL_free(config->carts);
}
config->carts = SDL_strdup(optarg);
break;
case 'd':
config->debug = true;
break;
case 'm':
if (!parse_size(optarg, &config->memory)) {
goto cleanup;
}
break;
case 'S':
if (!parse_scale_mode(optarg, &config->mode)) {
goto cleanup;
}
break;
case 'v':
config->logp--;
if (config->logp == SDL_LOG_PRIORITY_INVALID) {
SDL_SetError("specified -v too many times");
goto cleanup;
}
break;
case ':':
SDL_SetError("missing value for -%c", optopt);
goto cleanup;
case '?':
default:
SDL_SetError("unknown option -%c", optopt);
goto cleanup;
}
}
#else
(void)usage;
#endif
if (!config->carts) {
goto cleanup;
}
return config;
cleanup:
print_err(name, SDL_GetError());
pw_config_free(config);
return NULL;
}
void pw_config_free(struct pw_config *config) {
if (!config) {
SDL_InvalidParamError("config");
return;
}
if (config->carts) {
SDL_free(config->carts);
}
SDL_free(config);
}
|