2022-12-31 08:35:58 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2023-01-08 16:12:22 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <cfloat>
|
2023-01-04 16:40:35 +00:00
|
|
|
#include <sys/stat.h>
|
2022-12-31 08:35:58 +00:00
|
|
|
#include <string>
|
2023-01-04 16:40:35 +00:00
|
|
|
#include <memory>
|
2022-12-31 08:35:58 +00:00
|
|
|
|
2023-01-05 10:27:14 +00:00
|
|
|
#include "log.h"
|
2023-01-16 04:05:25 +00:00
|
|
|
#include "misc.h"
|
2022-12-31 08:35:58 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2023-01-04 16:40:35 +00:00
|
|
|
static FILE* fopen_or_raise(const char* path, const char* mode, bool ignore_enoent) {
|
|
|
|
FILE* file = fopen(path, mode);
|
2023-01-17 15:35:08 +00:00
|
|
|
if (!file && !(ignore_enoent && errno == ENOENT)) {
|
|
|
|
throw_system_error(std::string("fopen(") + quote(path) + ')');
|
2023-01-04 16:40:35 +00:00
|
|
|
}
|
2023-01-17 15:35:08 +00:00
|
|
|
return file;
|
2023-01-04 16:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void fclose_and_log(FILE* file) {
|
|
|
|
if (!fclose(file)) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-17 15:35:08 +00:00
|
|
|
log(std::string("Failed to close a file: fclose(): ") + strerror(errno));
|
2023-01-04 16:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool write(const std::string& ptr, FILE* file) {
|
|
|
|
return fwrite(ptr.data(), sizeof(char), ptr.size(), file) == ptr.size() * sizeof(char);
|
|
|
|
}
|
|
|
|
static bool write(const char* ptr, FILE* file) {
|
|
|
|
return fwrite(ptr, 1, strlen(ptr), file) == strlen(ptr);
|
|
|
|
}
|
|
|
|
|
2023-01-08 16:12:22 +00:00
|
|
|
static inline float to_float(const char* str) {
|
|
|
|
char* endptr;
|
|
|
|
|
|
|
|
while (isspace(str[0])) {
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
if (str[0] == '\0') {
|
|
|
|
throw std::invalid_argument("float string is empty");
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
|
|
|
float res = strtof(str, &endptr);
|
|
|
|
if (res == HUGE_VALF && errno == ERANGE) {
|
|
|
|
throw std::overflow_error(std::string(str) + " is bigger than HUGE_VALF");
|
|
|
|
} else if (res == FLT_MIN && errno == ERANGE) {
|
|
|
|
throw std::underflow_error(std::string(str) + " is smaller than FLT_MIN");
|
|
|
|
} else if (endptr[0] != '\0') {
|
|
|
|
throw std::invalid_argument(std::string(str) + " has trailing text");
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool validate_font_size_or_log(float size) {
|
|
|
|
if (size <= 0.0f) {
|
|
|
|
log(std::string("Font size ") + std::to_string(size) + " is lower or equal to 0");
|
|
|
|
return false;
|
|
|
|
} else if (std::isnan(size)) {
|
|
|
|
log(std::string("Font size ") + std::to_string(size) + " is NaN");
|
|
|
|
return false;
|
|
|
|
} else if (std::isinf(size)) {
|
|
|
|
log(std::string("Font size ") + std::to_string(size) + " is infinity or negative infinity");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:40:35 +00:00
|
|
|
|
|
|
|
std::string get_config_folder() {
|
2022-12-31 08:35:58 +00:00
|
|
|
const char* path;
|
|
|
|
|
|
|
|
path = getenv("XDG_CONFIG_HOME");
|
|
|
|
if (path) {
|
2023-01-04 16:40:35 +00:00
|
|
|
return std::string(path) + "/logmeow";
|
2022-12-31 08:35:58 +00:00
|
|
|
}
|
|
|
|
path = getenv("HOME");
|
|
|
|
if (path) {
|
2023-01-04 16:40:35 +00:00
|
|
|
return std::string(path) + "/.config/logmeow";
|
2022-12-31 08:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw std::runtime_error("cannot find suitable config folder, please set XDG_CONFIG_HOME or HOME");
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:40:35 +00:00
|
|
|
std::string get_config_file_path() {
|
|
|
|
return get_config_folder() + "/config";
|
|
|
|
}
|
|
|
|
|
|
|
|
// tries to create a directory with an empty string if it's just "/" but who cares
|
|
|
|
void create_config_folders_if_necessary() {
|
2023-01-05 10:27:14 +00:00
|
|
|
std::string full_path = get_config_folder();
|
|
|
|
std::string path;
|
2023-01-04 16:40:35 +00:00
|
|
|
size_t pos = 0;
|
|
|
|
|
|
|
|
while (pos != std::string::npos) {
|
2023-01-05 10:27:14 +00:00
|
|
|
pos = full_path.find('/', pos);
|
2023-01-04 16:40:35 +00:00
|
|
|
if (pos != std::string::npos) {
|
|
|
|
pos++;
|
|
|
|
}
|
2023-01-05 10:27:14 +00:00
|
|
|
path = full_path.substr(0, pos);
|
|
|
|
if (!mkdir(path.c_str(), 0600)) {
|
2023-01-04 16:40:35 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
continue;
|
|
|
|
}
|
2023-01-17 15:35:08 +00:00
|
|
|
throw_system_error(std::string("mkdir(") + quote(path) + ')');
|
2023-01-04 16:40:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-01-08 16:12:22 +00:00
|
|
|
static inline void parse_config_line(Config& config, char* line) {
|
|
|
|
if (strncmp(line, "logcat_command=", 15 * sizeof(char)) == 0) {
|
|
|
|
config.logcat_command = &line[15];
|
|
|
|
} else if (strncmp(line, "normal_font_size=", 17 * sizeof(char)) == 0) {
|
|
|
|
float size = to_float(&line[17]);
|
|
|
|
if (validate_font_size_or_log(size)) {
|
|
|
|
config.normal_font_size = size;
|
|
|
|
}
|
|
|
|
} else if (strncmp(line, "monospace_font_size=", 20 * sizeof(char)) == 0) {
|
|
|
|
float size = to_float(&line[20]);
|
|
|
|
if (validate_font_size_or_log(size)) {
|
|
|
|
config.monospace_font_size = size;
|
|
|
|
}
|
|
|
|
} else if (line[0] != '\0') {
|
|
|
|
throw std::invalid_argument(std::string("unknown config line: ") + line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:40:35 +00:00
|
|
|
static inline Config load_config(FILE* file) {
|
2022-12-31 08:35:58 +00:00
|
|
|
size_t line_capacity_size = 128 * sizeof(char);
|
|
|
|
char* line = static_cast<char*>(malloc(line_capacity_size));
|
|
|
|
if (line == nullptr) {
|
|
|
|
throw std::bad_alloc();
|
|
|
|
}
|
|
|
|
Config config;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
errno = 0;
|
|
|
|
if (getline(&line, &line_capacity_size, file) < 0) {
|
2023-01-17 15:35:08 +00:00
|
|
|
// https://stackoverflow.com/questions/30569981/does-free-set-errno
|
2022-12-31 08:35:58 +00:00
|
|
|
int errsv = errno;
|
|
|
|
free(line);
|
2023-01-17 15:35:08 +00:00
|
|
|
if (errsv) {
|
|
|
|
throw_system_error(errsv, "getline()");
|
2022-12-31 08:35:58 +00:00
|
|
|
}
|
2023-01-17 15:35:08 +00:00
|
|
|
break;
|
2022-12-31 08:35:58 +00:00
|
|
|
}
|
|
|
|
if (line_capacity_size == 0 || line[0] == '\0' || line[0] == '#') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (char* newline = strchr(line, '\n')) {
|
|
|
|
*newline = '\0';
|
|
|
|
}
|
|
|
|
|
2023-01-08 16:12:22 +00:00
|
|
|
try {
|
|
|
|
parse_config_line(config, line);
|
|
|
|
} catch (const std::exception& e) {
|
2022-12-31 08:35:58 +00:00
|
|
|
free(line);
|
2023-01-08 16:12:22 +00:00
|
|
|
throw;
|
2022-12-31 08:35:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:40:35 +00:00
|
|
|
Config load_config() {
|
|
|
|
std::string config_file_path = get_config_file_path();
|
|
|
|
std::unique_ptr<FILE, decltype(&fclose_and_log)> config_file(fopen_or_raise(config_file_path.c_str(), "r", true), fclose_and_log);
|
|
|
|
|
|
|
|
if (!config_file) {
|
|
|
|
return Config();
|
|
|
|
}
|
|
|
|
|
|
|
|
return load_config(config_file.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline void write_config(FILE* file, const Config& config) {
|
|
|
|
if (!write("# This is an auto-generated file, modifications will be lost\n"
|
|
|
|
"# This is a poor man's config file \"format\", there are only three legal lines:\n"
|
|
|
|
"# # a comment, only available at the start of a line\n"
|
|
|
|
"# (an empty line, no whitespace)\n"
|
|
|
|
"# key=value pairs, no spaces around the delimiter, and no unknown keys\n\n", file)) {
|
2023-01-05 10:27:14 +00:00
|
|
|
throw std::runtime_error("Failed to write info comment");
|
2023-01-04 16:40:35 +00:00
|
|
|
}
|
|
|
|
if (!write("logcat_command=", file)) {
|
2023-01-05 10:27:14 +00:00
|
|
|
throw std::runtime_error("Failed to write logcat command key");
|
2023-01-04 16:40:35 +00:00
|
|
|
}
|
|
|
|
if (!write(config.logcat_command, file)) {
|
2023-01-05 10:27:14 +00:00
|
|
|
throw std::runtime_error("Failed to write logcat command value");
|
2023-01-04 16:40:35 +00:00
|
|
|
}
|
2023-01-08 16:12:22 +00:00
|
|
|
|
|
|
|
if (!write("\nnormal_font_size=", file)) {
|
|
|
|
throw std::runtime_error("Failed to write normal font size key");
|
|
|
|
}
|
|
|
|
if (!write(std::to_string(config.normal_font_size), file)) {
|
|
|
|
throw std::runtime_error("Failed to write normal font size value");
|
|
|
|
}
|
|
|
|
if (!write("\nmonospace_font_size=", file)) {
|
|
|
|
throw std::runtime_error("Failed to write monospace font size key");
|
|
|
|
}
|
|
|
|
if (!write(std::to_string(config.monospace_font_size), file)) {
|
|
|
|
throw std::runtime_error("Failed to write monospace font size value");
|
|
|
|
}
|
2023-01-04 16:40:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void write_config(const Config& config) {
|
|
|
|
std::string config_file_path = get_config_file_path();
|
|
|
|
std::string tmp_config_file_path = config_file_path + ".tmp";
|
|
|
|
|
|
|
|
std::unique_ptr<FILE, decltype(&fclose_and_log)> config_file(fopen_or_raise(tmp_config_file_path.c_str(), "w", false), fclose_and_log);
|
|
|
|
write_config(config_file.get(), config);
|
|
|
|
config_file.reset();
|
|
|
|
|
|
|
|
if (!rename(tmp_config_file_path.c_str(), config_file_path.c_str())) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-17 15:35:08 +00:00
|
|
|
throw_system_error(std::string("rename(") + quote(tmp_config_file_path) + ", " + quote(config_file_path) + ')');
|
2022-12-31 08:35:58 +00:00
|
|
|
}
|