| 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <unistd.h> |
| 4 | |
| 5 | #include "src/user.h" |
| 6 | #include "src/os.h" |
| 7 | #if defined(__linux__) || defined(__sun) |
| 8 | #include "src/distro.h" |
| 9 | #endif |
| 10 | #if !defined(__HAIKU__) |
| 11 | #include "src/host.h" |
| 12 | #endif |
| 13 | #include "src/uptime.h" |
| 14 | #if defined(__OpenBSD__) |
| 15 | #include "src/recording.h" |
| 16 | #endif |
| 17 | #include "src/packages.h" |
| 18 | #include "src/resolution.h" |
| 19 | #include "src/wm.h" |
| 20 | #include "src/shell.h" |
| 21 | #include "src/libc.h" |
| 22 | #include "src/cpu.h" |
| 23 | #include "src/gpu.h" |
| 24 | #include "src/memory.h" |
| 25 | #include "src/storage.h" |
| 26 | |
| 27 | #include "src/config.h" |
| 28 | #include "src/common.h" |
| 29 | |
| 30 | const char *sofname = "farfetch"; |
| 31 | const char *version = "0.3.0"; |
| 32 | #if defined(__linux__) || defined(__sun) |
| 33 | const char *avalopt = "lrs"; |
| 34 | #elif defined(__HAIKU__) |
| 35 | const char *avalopt = "s"; |
| 36 | #else |
| 37 | const char *avalopt = "rs"; |
| 38 | #endif |
| 39 | |
| 40 | int opt; |
| 41 | #if defined(__linux__) || defined(__sun) |
| 42 | int islogo = 0; |
| 43 | #endif |
| 44 | int issmall = 0; |
| 45 | int iserr = 0; |
| 46 | |
| 47 | void usage() { |
| 48 | #if defined(__linux__) || defined(__sun) |
| 49 | printf("%s-%s\nusage: %s [-rs] [-l distro]\n", sofname, version, sofname); |
| 50 | #else |
| 51 | printf("%s-%s\nusage: %s [-%s]\n", sofname, version, sofname, avalopt); |
| 52 | #endif |
| 53 | } |
| 54 | |
| 55 | void flags(int opt) { |
| 56 | switch (opt) { |
| 57 | #if defined(__linux__) || defined(__sunos) |
| 58 | case 'l': |
| 59 | islogo = 1; |
| 60 | islogodistro = 0; |
| 61 | islogocustom = 0; |
| 62 | break; |
| 63 | #endif |
| 64 | #if !defined(__HAIKU__) |
| 65 | case 'r': |
| 66 | delete_cache(); |
| 67 | break; |
| 68 | #endif |
| 69 | case 's': |
| 70 | issmall = 1; |
| 71 | break; |
| 72 | default: |
| 73 | iserr = 1; |
| 74 | usage(); |
| 75 | break; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | int main(int argc, char *argv[]) { |
| 80 | int lc = 0; |
| 81 | while ((opt = getopt(argc, argv, avalopt)) != -1) { |
| 82 | flags(opt); |
| 83 | } |
| 84 | |
| 85 | if (iserr == 1) { |
| 86 | return 1; |
| 87 | } |
| 88 | |
| 89 | getconf(); |
| 90 | |
| 91 | #if defined(__OpenBSD__) |
| 92 | #include "src/logo/openbsd.h" |
| 93 | getOS(); |
| 94 | #elif defined(__NetBSD__) |
| 95 | #include "src/logo/netbsd.h" |
| 96 | getOS(); |
| 97 | #elif defined(__FreeBSD__) |
| 98 | #include "src/logo/freebsd.h" |
| 99 | getOS(); |
| 100 | #elif defined(__linux__) |
| 101 | get_distro(); |
| 102 | #include "src/logo/linux.h" |
| 103 | getDistro(distroname); |
| 104 | #elif defined(__sun) |
| 105 | get_distro(); |
| 106 | #include "src/logo/sunos.h" |
| 107 | getDistro(distroname); |
| 108 | #elif defined(__APPLE__) |
| 109 | #include "src/logo/macos.h" |
| 110 | getOS(); |
| 111 | #elif defined(__HAIKU__) |
| 112 | #include "src/logo/haiku.h" |
| 113 | getOS(); |
| 114 | #else |
| 115 | #include "src/logo/colors.h" |
| 116 | const char *color = MAGENTA; |
| 117 | const char *titlecolor = MAGENTA; |
| 118 | size_t logosize = 11; |
| 119 | char *LOGO[] = { |
| 120 | " ⢀⣀⣀⡀⠀⠀⠀⠀⠀⠀⠀⣠⠾⠛⠶⣄⢀⣠⣤⠴⢦⡀ ⠀⠀⠀⠀", |
| 121 | "⠀⠀⠀⢠⡿⠉⠉⠉⠛⠶⠶⠖⠒⠒⣾⠋⠀⢀⣀⣙⣯⡁⠀⠀⠀⣿⠀⠀⠀ ⠀", |
| 122 | "⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⢸⡏⠀⠀⢯⣼⠋⠉⠙⢶⠞⠛⠻⣆ ⠀⠀⠀", |
| 123 | "⠀⠀⠀⢸⣧⠆⠀⠀⠀⠀⠀⠀⠀⠀⠻⣦⣤⡤⢿⡀⠀⢀⣼⣷⠀⠀⣽ ⠀⠀⠀", |
| 124 | "⠀⠀⠀⣼⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠙⢏⡉⠁⣠⡾⣇⠀⠀⠀ ", |
| 125 | "⠀⠀⢰⡏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠋⠉⠀⢻⡀ ⠀⠀", |
| 126 | "⣀⣠⣼⣧⣤⠀⠀⠀⣀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⡀⠀⠀⠐⠖⢻⡟⠓⠒ ", |
| 127 | "⠀⠀⠈⣷⣀⡀⠀⠘⠿⠇⠀⠀⠀⢀⣀⣀⠀⠀⠀⠀⠿⠟⠀⠀⠀⠲⣾⠦⢤ ⠀", |
| 128 | "⠀⠀⠋⠙⣧⣀⡀⠀⠀⠀⠀⠀⠀⠘⠦⠼⠃⠀⠀⠀⠀⠀⠀⠀⢤⣼⣏ ⠀⠀⠀", |
| 129 | "⠀⠀⢀⠴⠚⠻⢧⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣀⣤⠞⠉⠉⠓⠀⠀ ", |
| 130 | "⠀⠀⠀⠀⠀⠀⠀⠈⠉⠛⠛⠶⠶⠶⣶⣤⣴⡶⠶⠶⠟⠛⠉⠀⠀⠀⠀⠀⠀⠀ " |
| 131 | }; |
| 132 | char *LOGO_SMALL[] = { |
| 133 | "|\____/| ", |
| 134 | "| | ", |
| 135 | "| 0 0 | ", |
| 136 | "| ω | ", |
| 137 | "\_____/ " |
| 138 | }; |
| 139 | #endif |
| 140 | |
| 141 | int minsize = MIN_SIZE; |
| 142 | |
| 143 | if (isresolution) { |
| 144 | const char *res = display_resolution(); |
| 145 | if (!res) minsize--; |
| 146 | else free((void *)res); |
| 147 | } else minsize--; |
| 148 | if (iswm) { |
| 149 | const char *winman = display_wm(); |
| 150 | if (!winman) minsize--; |
| 151 | #if !defined(__APPLE__) && !defined(__HAIKU__) |
| 152 | else free((void *)winman); |
| 153 | #endif |
| 154 | } else minsize--; |
| 155 | if (islibc) { |
| 156 | const char *clang = display_libc(); |
| 157 | if (clang) minsize++; |
| 158 | } |
| 159 | if (isstorage) { |
| 160 | const char *store = display_storage(); |
| 161 | if (!store) minsize--; |
| 162 | else free((void *)store); |
| 163 | } else minsize--; |
| 164 | if (isgpu) { |
| 165 | const char *graph = display_gpu(); |
| 166 | if (!graph) minsize--; |
| 167 | else free((void *)graph); |
| 168 | } else minsize--; |
| 169 | |
| 170 | const char *reset = RESET; |
| 171 | size_t ls = logosize <= (size_t)minsize ? (size_t)minsize : logosize; |
| 172 | #if defined(__linux__) || defined(__sun) |
| 173 | if (islogo) { |
| 174 | logoname = argv[2]; |
| 175 | getDistro(logoname); |
| 176 | } else if (islogodistro) { |
| 177 | logoname = distrostring; |
| 178 | getDistro(logoname); |
| 179 | free(distrostring); |
| 180 | } |
| 181 | #endif |
| 182 | |
| 183 | bool smol = false; |
| 184 | if (issmall && !islogos) smol = true; |
| 185 | else if (!issmall && islogos) smol = true; |
| 186 | |
| 187 | if (smol) { |
| 188 | size_t ne = sizeof(LOGO_SMALL) / sizeof(LOGO_SMALL[0]); |
| 189 | for (size_t i = 0; i < ne; i++) { |
| 190 | LOGO[i] = LOGO_SMALL[i]; |
| 191 | } |
| 192 | ls = ne >= (size_t)minsize ? (size_t)minsize : ne; |
| 193 | } |
| 194 | |
| 195 | const char *name = display_user_name(); |
| 196 | const char *host = display_user_host(); |
| 197 | if (name || host) { |
| 198 | printf("%s ", LOGO[lc]); |
| 199 | if (name) printf("%s%s%s",titlecolor, name, reset); |
| 200 | if (name && host) printf("@"); |
| 201 | if (host) printf("%s%s%s", titlecolor, host, reset); |
| 202 | printf("\n"); |
| 203 | if (name) free((void *)name); |
| 204 | if (host) free((void *)host); |
| 205 | lc++; |
| 206 | } |
| 207 | |
| 208 | printf("%s ", LOGO[lc]); |
| 209 | printf("------------------\n"); |
| 210 | lc++; |
| 211 | |
| 212 | if (isos) { |
| 213 | const char *os = display_os(); |
| 214 | if (os) { |
| 215 | printf("%s ", LOGO[lc]); |
| 216 | printf("%sOS%s: %s\n", color, reset, os); |
| 217 | free((void *)os); |
| 218 | lc++; |
| 219 | } |
| 220 | } else minsize--; |
| 221 | |
| 222 | #if defined(__linux__) || defined(__sun) |
| 223 | if (isdistro) { |
| 224 | const char *distroo = display_distro(); |
| 225 | if (distroo) { |
| 226 | printf("%s ", LOGO[lc]); |
| 227 | printf("%sDistro%s: %s\n", color, reset, distroo); |
| 228 | lc++; |
| 229 | free((void *)distroo); |
| 230 | } |
| 231 | } else minsize--; |
| 232 | #endif |
| 233 | |
| 234 | #if !defined(__HAIKU__) |
| 235 | const char *machine = display_host(); |
| 236 | if (ishost) { |
| 237 | printf("%s ", LOGO[lc]); |
| 238 | printf("%sHost%s: %s\n", color, reset, machine); |
| 239 | lc++; |
| 240 | } else minsize--; |
| 241 | #endif |
| 242 | |
| 243 | if (isuptime) { |
| 244 | const char *days = display_days(); |
| 245 | const char *time = display_time(); |
| 246 | if (days || time) { |
| 247 | printf("%s ", LOGO[lc]); |
| 248 | printf("%s%s%s%s", color, "Uptime", reset, ": "); |
| 249 | if (days) { |
| 250 | printf("%s", days); |
| 251 | if (time) printf(" "); |
| 252 | } |
| 253 | if (time) { |
| 254 | printf("%s", time); |
| 255 | } |
| 256 | printf("\n"); |
| 257 | if (days) free((void *)days); |
| 258 | if (time) free((void *)time); |
| 259 | lc++; |
| 260 | } |
| 261 | } else minsize--; |
| 262 | |
| 263 | #if defined(__OpenBSD__) |
| 264 | if (isrecording) { |
| 265 | const char *audio = display_recording_audio(); |
| 266 | const char *video = display_recording_video(); |
| 267 | if (audio || video) { |
| 268 | printf("%s ", LOGO[lc]); |
| 269 | printf("%sRecording%s: ", color, reset); |
| 270 | if (audio) { |
| 271 | printf("audio = %s", audio); |
| 272 | if (video) printf(", "); |
| 273 | } |
| 274 | if (video) { |
| 275 | printf("video = %s", video); |
| 276 | } |
| 277 | printf("\n"); |
| 278 | if (audio) free((void *)audio); |
| 279 | if (video) free((void *)video); |
| 280 | lc++; |
| 281 | } |
| 282 | } else minsize--; |
| 283 | #endif |
| 284 | |
| 285 | if (ispackages) { |
| 286 | const char *packages = display_packages(); |
| 287 | if (packages) { |
| 288 | printf("%s ", LOGO[lc]); |
| 289 | printf("%sPackages%s: %s\n", color, reset, packages); |
| 290 | lc++; |
| 291 | free((void *)packages); |
| 292 | } |
| 293 | } else minsize--; |
| 294 | |
| 295 | if (isresolution) { |
| 296 | const char *resolution = display_resolution(); |
| 297 | if (resolution) { |
| 298 | printf("%s ", LOGO[lc]); |
| 299 | printf("%sResolution%s: %s\n", color, reset, resolution); |
| 300 | lc++; |
| 301 | free((void *)resolution); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | if (iswm) { |
| 306 | const char *wm = display_wm(); |
| 307 | if (wm) { |
| 308 | printf("%s ", LOGO[lc]); |
| 309 | printf("%sWM%s: %s\n", color, reset, wm); |
| 310 | #if !defined(__APPLE__) && !defined(__HAIKU__) |
| 311 | free((void *)wm); |
| 312 | #endif |
| 313 | lc++; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if (isshell) { |
| 318 | const char *shell = display_shell(); |
| 319 | if (shell) { |
| 320 | printf("%s ", LOGO[lc]); |
| 321 | printf("%sShell%s: %s\n", color, reset, shell); |
| 322 | free((void *)shell); |
| 323 | lc++; |
| 324 | } |
| 325 | } else minsize--; |
| 326 | |
| 327 | if (islibc) { |
| 328 | const char *libc = display_libc(); |
| 329 | if (libc) { |
| 330 | printf("%s ", LOGO[lc]); |
| 331 | printf("%slibc%s: %s\n", color, reset, libc); |
| 332 | lc++; |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | if (iscpu) { |
| 337 | const char *cpu = display_cpu(); |
| 338 | if (cpu) { |
| 339 | printf("%s ", LOGO[lc]); |
| 340 | printf("%sCPU%s: %s\n", color, reset, cpu); |
| 341 | lc++; |
| 342 | free((void *)cpu); |
| 343 | } |
| 344 | } else minsize--; |
| 345 | |
| 346 | if (isgpu) { |
| 347 | const char *gpu = display_gpu(); |
| 348 | if (gpu) { |
| 349 | printf("%s ", LOGO[lc]); |
| 350 | printf("%sGPU%s: %s\n", color, reset, gpu); |
| 351 | lc++; |
| 352 | free((void *)gpu); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | if (ismemory) { |
| 357 | printf("%s ", LOGO[lc]); |
| 358 | printf("%s%s%s%s", color, "Memory", reset, ": "); |
| 359 | display_memory(); |
| 360 | printf("\n"); |
| 361 | lc++; |
| 362 | } else minsize--; |
| 363 | |
| 364 | if (isstorage) { |
| 365 | const char *storage = display_storage(); |
| 366 | if (storage) { |
| 367 | printf("%s ", LOGO[lc]); |
| 368 | printf("%sStorage%s: %s\n", color, reset, storage); |
| 369 | lc++; |
| 370 | free((void *)storage); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | for (size_t i = lc; i < ls; i++) { |
| 375 | printf("%s\n", LOGO[i]); |
| 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | } |