#include "config.h" #include #include #include #include #include #include #include #include namespace gassyterm { namespace { const char kDefaultFile[] = "# gassyterm configuration.\n" "# Read once, when the window opens; changes apply the next time you\n" "# start it. Format: key = value. '#' starts a comment.\n" "\n" "# Font pattern as understood by fontconfig, including the size.\n" "font = monospace:size=11\n" "\n" "# Colours are #rrggbb. The cursor block follows the foreground unless\n" "# you give it its own colour.\n" "foreground = #cccccc\n" "background = #0b0b0b\n" "cursor = #cccccc\n" "\n" "# Initial window size, in character cells.\n" "columns = 80\n" "rows = 24\n" "\n" "# Optional overrides for the 16 ANSI base colours (color0 = black ...).\n" "# color0 = #000000\n" "# color1 = #cd3131\n" "# color2 = #0dbc79\n" "# color3 = #e5e510\n" "# color4 = #2472c8\n" "# color5 = #bc3fbc\n" "# color6 = #11a8cd\n" "# color7 = #e5e5e5\n" "# color8 = #666666\n" "# color9 = #f14c4c\n" "# color10 = #23d18b\n" "# color11 = #f5f543\n" "# color12 = #3b8eea\n" "# color13 = #d670d6\n" "# color14 = #29b8db\n" "# color15 = #ffffff\n"; std::string trim(const std::string& text) { size_t begin = text.find_first_not_of(" \t\r\n"); if (begin == std::string::npos) return std::string(); size_t end = text.find_last_not_of(" \t\r\n"); return text.substr(begin, end - begin + 1); } std::string unquote(const std::string& text) { if (text.size() >= 2 && (text.front() == '"' || text.front() == '\'') && text.back() == text.front()) { return text.substr(1, text.size() - 2); } return text; } std::string lowercase(const std::string& text) { std::string result = text; for (char& c : result) { c = static_cast(std::tolower(static_cast(c))); } return result; } int hexValue(char c) { if (c >= '0' && c <= '9') return c - '0'; if (c >= 'a' && c <= 'f') return c - 'a' + 10; if (c >= 'A' && c <= 'F') return c - 'A' + 10; return -1; } std::string parentDirectory(const std::string& file) { size_t slash = file.find_last_of('/'); if (slash == std::string::npos) return std::string("."); if (slash == 0) return std::string("/"); return file.substr(0, slash); } // mkdir -p, so ~/.config/gassyterm exists even on a bare home directory. bool makeDirectories(const std::string& dir) { std::string partial; size_t pos = 0; while (pos <= dir.size()) { size_t next = dir.find('/', pos); if (next == std::string::npos) next = dir.size(); partial = dir.substr(0, next); if (!partial.empty() && mkdir(partial.c_str(), 0755) != 0 && errno != EEXIST) { return false; } pos = next + 1; } return true; } void writeDefaultFile(const std::string& file) { const std::string dir = parentDirectory(file); if (!makeDirectories(dir)) { std::fprintf(stderr, "gassyterm: cannot create %s\n", dir.c_str()); return; } std::ofstream out(file); if (!out) { std::fprintf(stderr, "gassyterm: cannot write %s\n", file.c_str()); return; } out << kDefaultFile; } bool readInteger(const std::string& text, int minimum, int* out) { if (text.empty()) return false; const char* begin = text.c_str(); char* end = nullptr; long value = std::strtol(begin, &end, 10); if (end == begin || *end != '\0') return false; if (value < minimum || value > 4096) return false; *out = static_cast(value); return true; } } // namespace bool parseColor(const std::string& text, int* r, int* g, int* b) { std::string hex = text; if (!hex.empty() && hex[0] == '#') hex.erase(0, 1); if (hex.size() != 6) return false; int values[3] = {0, 0, 0}; for (int i = 0; i < 3; ++i) { int high = hexValue(hex[static_cast(i) * 2]); int low = hexValue(hex[static_cast(i) * 2 + 1]); if (high < 0 || low < 0) return false; values[i] = high * 16 + low; } *r = values[0]; *g = values[1]; *b = values[2]; return true; } std::string Config::path() { const char* xdg = std::getenv("XDG_CONFIG_HOME"); if (xdg != nullptr && xdg[0] != '\0') { return std::string(xdg) + "/gassyterm/config"; } const char* home = std::getenv("HOME"); if (home != nullptr && home[0] != '\0') { return std::string(home) + "/.config/gassyterm/config"; } return std::string(".gassyterm-config"); } bool Config::loadFile(const std::string& file, Config* out) { std::ifstream in(file); if (!in) return false; std::string line; int number = 0; while (std::getline(in, line)) { ++number; const std::string text = trim(line); if (text.empty() || text[0] == '#') continue; size_t equals = text.find('='); if (equals == std::string::npos) { std::fprintf(stderr, "gassyterm: %s:%d: expected key = value\n", file.c_str(), number); continue; } const std::string key = lowercase(trim(text.substr(0, equals))); const std::string value = unquote(trim(text.substr(equals + 1))); if (key == "font") { if (value.empty()) { std::fprintf(stderr, "gassyterm: %s:%d: empty font\n", file.c_str(), number); } else { out->font = value; } } else if (key == "foreground") { int r = 0, g = 0, b = 0; if (parseColor(value, &r, &g, &b)) { out->foreground = value; } else { std::fprintf(stderr, "gassyterm: %s:%d: bad colour \"%s\"\n", file.c_str(), number, value.c_str()); } } else if (key == "background") { int r = 0, g = 0, b = 0; if (parseColor(value, &r, &g, &b)) { out->background = value; } else { std::fprintf(stderr, "gassyterm: %s:%d: bad colour \"%s\"\n", file.c_str(), number, value.c_str()); } } else if (key == "cursor") { int r = 0, g = 0, b = 0; if (value.empty()) { out->cursor.clear(); } else if (parseColor(value, &r, &g, &b)) { out->cursor = value; } else { std::fprintf(stderr, "gassyterm: %s:%d: bad colour \"%s\"\n", file.c_str(), number, value.c_str()); } } else if (key == "columns") { if (!readInteger(value, 1, &out->columns)) { std::fprintf(stderr, "gassyterm: %s:%d: bad columns \"%s\"\n", file.c_str(), number, value.c_str()); } } else if (key == "rows") { if (!readInteger(value, 1, &out->rows)) { std::fprintf(stderr, "gassyterm: %s:%d: bad rows \"%s\"\n", file.c_str(), number, value.c_str()); } } else if (key.size() == 6 && key.compare(0, 5, "color") == 0 && key[5] >= '0' && key[5] <= '9') { int index = key[5] - '0'; int r = 0, g = 0, b = 0; if (parseColor(value, &r, &g, &b)) { out->colors[index] = value; } else { std::fprintf(stderr, "gassyterm: %s:%d: bad colour \"%s\"\n", file.c_str(), number, value.c_str()); } } else if (key.size() == 7 && key.compare(0, 5, "color") == 0 && key[5] >= '1' && key[5] <= '5' && key[6] >= '0' && key[6] <= '9') { int index = (key[5] - '0') * 10 + (key[6] - '0'); if (index < 16) { int r = 0, g = 0, b = 0; if (parseColor(value, &r, &g, &b)) { out->colors[index] = value; } else { std::fprintf(stderr, "gassyterm: %s:%d: bad colour \"%s\"\n", file.c_str(), number, value.c_str()); } } else { std::fprintf(stderr, "gassyterm: %s:%d: unknown key \"%s\"\n", file.c_str(), number, key.c_str()); } } else { std::fprintf(stderr, "gassyterm: %s:%d: unknown key \"%s\"\n", file.c_str(), number, key.c_str()); } } return true; } Config Config::load() { Config config; const std::string file = path(); struct stat info {}; if (stat(file.c_str(), &info) != 0) writeDefaultFile(file); loadFile(file, &config); if (config.cursor.empty()) config.cursor = config.foreground; return config; } } // namespace gassyterm