| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | #include <string.h> |
| 5 | #include <stdbool.h> |
| 6 | #include <ctype.h> |
| 7 | |
| 8 | #include "config.h" |
| 9 | #include "logo/colors.h" |
| 10 | #if defined(__OpenBSD__) |
| 11 | #include "logo/openbsd.h" |
| 12 | #elif defined(__NetBSD__) |
| 13 | #include "logo/netbsd.h" |
| 14 | #elif defined(__FreeBSD__) |
| 15 | #include "logo/freebsd.h" |
| 16 | #elif defined(__linux__) |
| 17 | #include "logo/linux.h" |
| 18 | #include "distro.h" |
| 19 | #elif defined(__sun) |
| 20 | #include "logo/sunos.h" |
| 21 | #elif defined(__APPLE__) |
| 22 | #include "logo/macos.h" |
| 23 | #elif defined(__HAIKU__) |
| 24 | #include "logo/haiku.h" |
| 25 | #endif |
| 26 | |
| 27 | bool islogob = true; |
| 28 | bool islogos = false; |
| 29 | bool islogod = true; |
| 30 | bool islogocustom = false; |
| 31 | #if defined(__linux__) || defined(__sunos) |
| 32 | bool islogodistro = false; |
| 33 | char *distrostring; |
| 34 | #endif |
| 35 | bool isos = true; |
| 36 | bool ishost = true; |
| 37 | #if defined(__linux__) || defined(__sunos) |
| 38 | bool isdistro = true; |
| 39 | #endif |
| 40 | bool isuptime = true; |
| 41 | #if defined(__OpenBSD__) |
| 42 | bool isrecording = true; |
| 43 | #endif |
| 44 | bool ispackages = true; |
| 45 | bool islibc = true; |
| 46 | bool isresolution = true; |
| 47 | bool iswm = true; |
| 48 | bool isshell = true; |
| 49 | bool iscpu = true; |
| 50 | bool isgpu = true; |
| 51 | bool ismemory = true; |
| 52 | bool isstorage = true; |
| 53 | bool isbiglogo = false; |
| 54 | bool issmalllogo = false; |
| 55 | bool mkbiglogo = false; |
| 56 | bool mksmalllogo = false; |
| 57 | size_t biglogoi = 0; |
| 58 | size_t smalllogoi = 0; |
| 59 | const char *customcolor; |
| 60 | const char *customtitlecolor; |
| 61 | const char *customlogobig; |
| 62 | const char *customlogosmall; |
| 63 | |
| 64 | void rmstr(char *str, const char *sub) { |
| 65 | char *pos; |
| 66 | size_t len = strlen(sub); |
| 67 | |
| 68 | while ((pos = strstr(str, sub)) != NULL) { |
| 69 | memmove(pos, pos + len, strlen(pos + len) + 1); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | const char *applycolor(const char *color) { |
| 74 | if (strncmp(color, "grey", 4) == 0) return GREY; |
| 75 | else if (strncmp(color, "red", 3) == 0) return RED; |
| 76 | else if (strncmp(color, "green", 5) == 0) return GREEN; |
| 77 | else if (strncmp(color, "yellow", 6) == 0) return YELLOW; |
| 78 | else if (strncmp(color, "blue", 4) == 0) return BLUE; |
| 79 | else if (strncmp(color, "magenta", 7) == 0) return MAGENTA; |
| 80 | else if (strncmp(color, "cyan", 4) == 0) return CYAN; |
| 81 | else if (strncmp(color, "white", 5) == 0) return WHITE; |
| 82 | return RESET; |
| 83 | } |
| 84 | |
| 85 | char *colrep(const char *str, const char *old, const char *new) { |
| 86 | char *res; |
| 87 | int i, count = 0; |
| 88 | int nlen = strlen(new); |
| 89 | int olen = strlen(old); |
| 90 | |
| 91 | for (i = 0; str[i] != '\0'; i++) { |
| 92 | if (strstr(&str[i], old) == &str[i]) { |
| 93 | count++; |
| 94 | i += olen - 1; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | res = (char *)malloc(i + count * (nlen - olen) + 1); |
| 99 | if (!res) { |
| 100 | return NULL; |
| 101 | } |
| 102 | |
| 103 | i = 0; |
| 104 | while (*str) { |
| 105 | if (strstr(str, old) == str) { |
| 106 | memcpy(&res[i], new, strlen(new)); |
| 107 | i += nlen; |
| 108 | str += olen; |
| 109 | } else { |
| 110 | res[i++] = *str++; |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | res[i] = '\0'; |
| 115 | return res; |
| 116 | } |
| 117 | |
| 118 | bool containvocab(const char *line, const char *word) { |
| 119 | const char *p = line; |
| 120 | size_t len = strlen(word); |
| 121 | while ((p = strstr(p, word)) != NULL) { |
| 122 | if ( |
| 123 | (p == line || !isalnum((unsigned char)p[-1])) && |
| 124 | !isalnum((unsigned char)p[len]) |
| 125 | ) return true; |
| 126 | |
| 127 | p += len; |
| 128 | } |
| 129 | |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | void getconf() { |
| 134 | char *homedir = getenv("HOME"); |
| 135 | if (homedir == NULL) { |
| 136 | perror("ホームディレクトリを受取に失敗"); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | #if defined(__HAIKU__) |
| 141 | char *basedir = "/config/settings/"; |
| 142 | #else |
| 143 | char *basedir = "/.config/"; |
| 144 | #endif |
| 145 | |
| 146 | int alllen = snprintf(NULL, 0, "%s%s%s", homedir, basedir, "farfetch.conf") + 1; |
| 147 | char *cnfpath = malloc(alllen); |
| 148 | if (cnfpath == NULL) { |
| 149 | perror("メモリを割当に失敗"); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | snprintf(cnfpath, alllen, "%s%s%s", homedir, basedir, "farfetch.conf"); |
| 154 | |
| 155 | if (access(cnfpath, F_OK) != 0) { |
| 156 | free(cnfpath); |
| 157 | return; |
| 158 | } |
| 159 | |
| 160 | FILE *file = fopen(cnfpath, "r"); |
| 161 | free(cnfpath); |
| 162 | if (!file) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | char line[256]; |
| 167 | while (fgets(line, sizeof(line), file)) { |
| 168 | if (line[0] == '#' || line[0] == '\n') continue; |
| 169 | if (strstr(line, "hide ") != NULL) { |
| 170 | if (containvocab(line, "logo")) islogod = false; |
| 171 | if (containvocab(line, "os")) isos = false; |
| 172 | if (containvocab(line, "host")) ishost = false; |
| 173 | #if defined(__linux__) || defined(__sunos) |
| 174 | if (containvocab(line, "distro")) isdistro = false; |
| 175 | #endif |
| 176 | if (containvocab(line, "uptime")) isuptime = false; |
| 177 | #if defined (__OpenBSD__) |
| 178 | if (containvocab(line, "recording")) isrecording = false; |
| 179 | #endif |
| 180 | if (containvocab(line, "packages")) ispackages = false; |
| 181 | if (containvocab(line, "libc")) islibc = false; |
| 182 | if (containvocab(line, "resolution")) isresolution = false; |
| 183 | if (containvocab(line, "wm")) iswm = false; |
| 184 | if (containvocab(line, "shell")) isshell = false; |
| 185 | if (containvocab(line, "cpu")) iscpu = false; |
| 186 | if (containvocab(line, "gpu")) isgpu = false; |
| 187 | if (containvocab(line, "memory")) ismemory = false; |
| 188 | if (containvocab(line, "storage")) isstorage = false; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | if (strstr(line, "set color") != NULL) { |
| 193 | char color[10]; |
| 194 | sscanf(line, "set color %s", color); |
| 195 | customcolor = applycolor(color); |
| 196 | } |
| 197 | if (strstr(line, "set titlecolor") != NULL) { |
| 198 | char color[10]; |
| 199 | sscanf(line, "set titlecolor %s", color); |
| 200 | customtitlecolor = applycolor(color); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | if (strstr(line, "show logo") != NULL) { |
| 205 | if (containvocab(line, "small")) { |
| 206 | islogob = false; |
| 207 | islogos = true; |
| 208 | } else { |
| 209 | islogob = true; |
| 210 | islogos = false; |
| 211 | } |
| 212 | |
| 213 | if (containvocab(line, "custom")) { |
| 214 | islogocustom = true; |
| 215 | #if defined(__linux__) || defined(__sunos) |
| 216 | } else { |
| 217 | distrostring = strdup(line); |
| 218 | if (!distrostring) { |
| 219 | perror("メモリの役割に失敗"); |
| 220 | return; |
| 221 | } |
| 222 | rmstr(distrostring, "show logo"); |
| 223 | if (containvocab(line, "small")) rmstr(distrostring, "small"); |
| 224 | else rmstr(distrostring, "big"); |
| 225 | rmstr(distrostring, "\n"); |
| 226 | rmstr(distrostring, " "); |
| 227 | |
| 228 | if (is_distro(distrostring)) islogodistro = true; |
| 229 | else distrostring = NULL; |
| 230 | #endif |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | |
| 235 | if (strstr(line, "define custom big logo:") != NULL) { |
| 236 | mkbiglogo = true; |
| 237 | } |
| 238 | if (strstr(line, "define custom small logo:") != NULL) { |
| 239 | mksmalllogo = true; |
| 240 | } |
| 241 | |
| 242 | if (mkbiglogo && islogocustom && islogob) { |
| 243 | isbiglogo = true; |
| 244 | if (strstr(line, "define custom big logo:") != NULL) { |
| 245 | continue; |
| 246 | } else if (strstr(line, "EOL") != NULL) { |
| 247 | mkbiglogo = false; |
| 248 | } else if (biglogoi < logosize) { |
| 249 | LOGO[biglogoi] = strdup(line); |
| 250 | LOGO[biglogoi] = colrep(LOGO[biglogoi], "[GREY]", GREY); |
| 251 | LOGO[biglogoi] = colrep(LOGO[biglogoi], "[RED]", RED); |
| 252 | LOGO[biglogoi] = colrep(LOGO[biglogoi], "[GREEN]", GREEN); |
| 253 | LOGO[biglogoi] = colrep(LOGO[biglogoi], "[YELLOW]", YELLOW); |
| 254 | LOGO[biglogoi] = colrep(LOGO[biglogoi], "[BLUE]", BLUE); |
| 255 | LOGO[biglogoi] = colrep(LOGO[biglogoi], "[MAGENTA]", MAGENTA); |
| 256 | LOGO[biglogoi] = colrep(LOGO[biglogoi], "[CYAN]", CYAN); |
| 257 | LOGO[biglogoi] = colrep(LOGO[biglogoi], "[WHITE]", WHITE); |
| 258 | LOGO[biglogoi] = colrep(LOGO[biglogoi], "[RESET]", RESET); |
| 259 | size_t len = strlen(LOGO[biglogoi]); |
| 260 | if (len > 0 && LOGO[biglogoi][len - 1] == '\n') { |
| 261 | LOGO[biglogoi][len - 1] = '\0'; |
| 262 | } |
| 263 | biglogoi++; |
| 264 | free(LOGO[biglogoi]); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (mksmalllogo && islogocustom && islogos) { |
| 269 | issmalllogo = true; |
| 270 | if (strstr(line, "define custom small logo:") != NULL) { |
| 271 | continue; |
| 272 | } else if (strstr(line, "EOL") != NULL) { |
| 273 | mksmalllogo = false; |
| 274 | } else if (smalllogoi < MIN_SIZE) { |
| 275 | LOGO_SMALL[smalllogoi] = strdup(line); |
| 276 | LOGO_SMALL[smalllogoi] = colrep(LOGO_SMALL[smalllogoi], "[GREY]", GREY); |
| 277 | LOGO_SMALL[smalllogoi] = colrep(LOGO_SMALL[smalllogoi], "[RED]", RED); |
| 278 | LOGO_SMALL[smalllogoi] = colrep(LOGO_SMALL[smalllogoi], "[GREEN]", GREEN); |
| 279 | LOGO_SMALL[smalllogoi] = colrep(LOGO_SMALL[smalllogoi], "[YELLOW]", YELLOW); |
| 280 | LOGO_SMALL[smalllogoi] = colrep(LOGO_SMALL[smalllogoi], "[BLUE]", BLUE); |
| 281 | LOGO_SMALL[smalllogoi] = colrep(LOGO_SMALL[smalllogoi], "[MAGENTA]", MAGENTA); |
| 282 | LOGO_SMALL[smalllogoi] = colrep(LOGO_SMALL[smalllogoi], "[CYAN]", CYAN); |
| 283 | LOGO_SMALL[smalllogoi] = colrep(LOGO_SMALL[smalllogoi], "[WHITE]", WHITE); |
| 284 | LOGO_SMALL[smalllogoi] = colrep(LOGO_SMALL[smalllogoi], "[RESET]", RESET); |
| 285 | size_t len = strlen(LOGO_SMALL[smalllogoi]); |
| 286 | if (len > 0 && LOGO_SMALL[smalllogoi][len - 1] == '\n') { |
| 287 | LOGO_SMALL[smalllogoi][len - 1] = '\0'; |
| 288 | } |
| 289 | smalllogoi++; |
| 290 | free(LOGO[smalllogoi]); |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | fclose(file); |
| 296 | return; |
| 297 | } |