// 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 #endif #include #include #include #include "config.h" #include "path.h" static void usage(char const *name) { printf("Usage: %s [-c ] [-d] [-S ] [-v[v[v]]]\n" " -c Carts directory.\n" " -d Print additional debug statistics.\n" " -m Memory to allocate for PICO-8 in MB.\n" " -S 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); }