๐ŸŽ€ Toy Gemini Client Written in C99
cli retro gemini gemtext terminal tls lightweight minimal c99 gemini-protocol
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

chore(flag): add as dep

Fuwn 8186f33f 8fb64d7b

+10 -402
+3
.gitmodules
··· 1 + [submodule "deps/flag"] 2 + path = deps/flag 3 + url = https://github.com/Fuwn/flag.h
+3 -1
CMakeLists.txt
··· 32 32 C 33 33 VERSION 34 34 0.1.0 35 + DESCRIPTION 36 + "๐ŸŽ€ toy gemini client in ansi c (c99)" 35 37 ) 36 38 37 39 # Packages ··· 46 48 list(APPEND viv_SOURCES 47 49 "viv/cli.c" 48 50 "viv/dynamic_array.c" 49 - "viv/flag.c" 50 51 "viv/gemini.c" 51 52 "viv/log.c" 52 53 "viv/ssl.c" ··· 86 87 87 88 target_include_directories(viv PRIVATE 88 89 "include/" 90 + "deps/" 89 91 ) 90 92 91 93 if(CURSES_FOUND AND CURSES_HAVE_NCURSES_H) # curses
+1 -1
cmake.toml
··· 20 20 [target.viv] 21 21 type = "executable" 22 22 sources = ["viv/*.c"] 23 - private-include-directories = ["include/"] 23 + private-include-directories = ["include/", "deps/"] 24 24 properties = { CMAKE_C_STANDARD_REQUIRED = true, CMAKE_C_STANDARD = "99", CMAKE_C_EXTENSIONS = false, CURSES_NEED_NCURSES = true } 25 25 compile-options = ["-std=c99", "-Wall", "-Wextra", "-Werror=pedantic", "-Wno-unused-function", "-Wpedantic", "-pedantic-errors", "-march=native", "-D_XOPEN_SOURCE=500"] # -03, -fsanitize=address 26 26 link-options = ["-fsanitize=address"]
-393
include/viv/flag.h
··· 1 - // flag.h -- command-line flag parsing 2 - // 3 - // Inspired by Go's flag module: https://pkg.go.dev/flag 4 - // 5 - #ifndef FLAG_H_ 6 - #define FLAG_H_ 7 - 8 - #define static_assert _Static_assert 9 - 10 - #include <assert.h> 11 - #include <stdio.h> 12 - #include <stdlib.h> 13 - #include <stdbool.h> 14 - #include <stdint.h> 15 - #include <stddef.h> 16 - #include <inttypes.h> 17 - #include <limits.h> 18 - #include <string.h> 19 - #include <errno.h> 20 - 21 - // TODO: add support for -flag=x syntax 22 - // TODO: *_var function variants 23 - // void flag_bool_var(bool *var, const char *name, bool def, const char *desc); 24 - // void flag_bool_uint64(uint64_t *var, const char *name, bool def, const char *desc); 25 - // etc. 26 - // WARNING! *_var functions may break the flag_name() functionality 27 - 28 - char *flag_name(void *val); 29 - bool *flag_bool(const char *name, bool def, const char *desc); 30 - uint64_t *flag_uint64(const char *name, uint64_t def, const char *desc); 31 - size_t *flag_size(const char *name, uint64_t def, const char *desc); 32 - char **flag_str(const char *name, const char *def, const char *desc); 33 - bool flag_parse(int argc, char **argv); 34 - int flag_rest_argc(void); 35 - char **flag_rest_argv(void); 36 - void flag_print_error(FILE *stream); 37 - void flag_print_options(FILE *stream); 38 - 39 - #endif // FLAG_H_ 40 - 41 - ////////////////////////////// 42 - 43 - #ifdef FLAG_IMPLEMENTATION 44 - 45 - typedef enum { 46 - FLAG_BOOL = 0, 47 - FLAG_UINT64, 48 - FLAG_SIZE, 49 - FLAG_STR, 50 - COUNT_FLAG_TYPES, 51 - } Flag_Type; 52 - 53 - static_assert(COUNT_FLAG_TYPES == 4, "Exhaustive Flag_Value definition"); 54 - typedef union { 55 - char *as_str; 56 - uint64_t as_uint64; 57 - bool as_bool; 58 - size_t as_size; 59 - } Flag_Value; 60 - 61 - typedef enum { 62 - FLAG_NO_ERROR = 0, 63 - FLAG_ERROR_UNKNOWN, 64 - FLAG_ERROR_NO_VALUE, 65 - FLAG_ERROR_INVALID_NUMBER, 66 - FLAG_ERROR_INTEGER_OVERFLOW, 67 - FLAG_ERROR_INVALID_SIZE_SUFFIX, 68 - COUNT_FLAG_ERRORS, 69 - } Flag_Error; 70 - 71 - typedef struct { 72 - Flag_Type type; 73 - char *name; 74 - char *desc; 75 - Flag_Value val; 76 - Flag_Value def; 77 - } Flag; 78 - 79 - #ifndef FLAGS_CAP 80 - #define FLAGS_CAP 256 81 - #endif 82 - 83 - typedef struct { 84 - Flag flags[FLAGS_CAP]; 85 - size_t flags_count; 86 - 87 - Flag_Error flag_error; 88 - char *flag_error_name; 89 - 90 - int rest_argc; 91 - char **rest_argv; 92 - } Flag_Context; 93 - 94 - static Flag_Context flag_global_context; 95 - 96 - Flag *flag_new(Flag_Type type, const char *name, const char *desc) 97 - { 98 - Flag_Context *c = &flag_global_context; 99 - 100 - assert(c->flags_count < FLAGS_CAP); 101 - Flag *flag = &c->flags[c->flags_count++]; 102 - memset(flag, 0, sizeof(*flag)); 103 - flag->type = type; 104 - // NOTE: I won't touch them I promise Kappa 105 - flag->name = (char*) name; 106 - flag->desc = (char*) desc; 107 - return flag; 108 - } 109 - 110 - char *flag_name(void *val) 111 - { 112 - Flag *flag = (Flag*) ((char*) val - offsetof(Flag, val)); 113 - return flag->name; 114 - } 115 - 116 - bool *flag_bool(const char *name, bool def, const char *desc) 117 - { 118 - Flag *flag = flag_new(FLAG_BOOL, name, desc); 119 - flag->def.as_bool = def; 120 - flag->val.as_bool = def; 121 - return &flag->val.as_bool; 122 - } 123 - 124 - uint64_t *flag_uint64(const char *name, uint64_t def, const char *desc) 125 - { 126 - Flag *flag = flag_new(FLAG_UINT64, name, desc); 127 - flag->val.as_uint64 = def; 128 - flag->def.as_uint64 = def; 129 - return &flag->val.as_uint64; 130 - } 131 - 132 - size_t *flag_size(const char *name, uint64_t def, const char *desc) 133 - { 134 - Flag *flag = flag_new(FLAG_SIZE, name, desc); 135 - flag->val.as_size = def; 136 - flag->def.as_size = def; 137 - return &flag->val.as_size; 138 - } 139 - 140 - char **flag_str(const char *name, const char *def, const char *desc) 141 - { 142 - Flag *flag = flag_new(FLAG_STR, name, desc); 143 - flag->val.as_str = (char*) def; 144 - flag->def.as_str = (char*) def; 145 - return &flag->val.as_str; 146 - } 147 - 148 - static char *flag_shift_args(int *argc, char ***argv) 149 - { 150 - assert(*argc > 0); 151 - char *result = **argv; 152 - *argv += 1; 153 - *argc -= 1; 154 - return result; 155 - } 156 - 157 - int flag_rest_argc(void) 158 - { 159 - return flag_global_context.rest_argc; 160 - } 161 - 162 - char **flag_rest_argv(void) 163 - { 164 - return flag_global_context.rest_argv; 165 - } 166 - 167 - bool flag_parse(int argc, char **argv) 168 - { 169 - Flag_Context *c = &flag_global_context; 170 - 171 - flag_shift_args(&argc, &argv); 172 - 173 - while (argc > 0) { 174 - char *flag = flag_shift_args(&argc, &argv); 175 - 176 - if (*flag != '-') { 177 - // NOTE: pushing flag back into args 178 - c->rest_argc = argc + 1; 179 - c->rest_argv = argv - 1; 180 - return true; 181 - } 182 - 183 - if (strcmp(flag, "--") == 0) { 184 - // NOTE: but if it's the terminator we don't need to push it back 185 - c->rest_argc = argc; 186 - c->rest_argv = argv; 187 - return true; 188 - } 189 - 190 - // NOTE: remove the dash 191 - flag += 1; 192 - 193 - bool found = false; 194 - for (size_t i = 0; i < c->flags_count; ++i) { 195 - if (strcmp(c->flags[i].name, flag) == 0) { 196 - static_assert(COUNT_FLAG_TYPES == 4, "Exhaustive flag type parsing"); 197 - switch (c->flags[i].type) { 198 - case FLAG_BOOL: { 199 - c->flags[i].val.as_bool = true; 200 - } 201 - break; 202 - 203 - case FLAG_STR: { 204 - if (argc == 0) { 205 - c->flag_error = FLAG_ERROR_NO_VALUE; 206 - c->flag_error_name = flag; 207 - return false; 208 - } 209 - char *arg = flag_shift_args(&argc, &argv); 210 - c->flags[i].val.as_str = arg; 211 - } 212 - break; 213 - 214 - case FLAG_UINT64: { 215 - if (argc == 0) { 216 - c->flag_error = FLAG_ERROR_NO_VALUE; 217 - c->flag_error_name = flag; 218 - return false; 219 - } 220 - char *arg = flag_shift_args(&argc, &argv); 221 - 222 - static_assert(sizeof(unsigned long long int) == sizeof(uint64_t), "The original author designed this for x86_64 machine with the compiler that expects unsigned long long int and uint64_t to be the same thing, so they could use strtoull() function to parse it. Please adjust this code for your case and maybe even send the patch to upstream to make it work on a wider range of environments."); 223 - char *endptr; 224 - // TODO: replace strtoull with a custom solution 225 - // That way we can get rid of the dependency on errno and static_assert 226 - unsigned long long int result = strtoull(arg, &endptr, 10); 227 - 228 - if (*endptr != '\0') { 229 - c->flag_error = FLAG_ERROR_INVALID_NUMBER; 230 - c->flag_error_name = flag; 231 - return false; 232 - } 233 - 234 - if (result == ULLONG_MAX && errno == ERANGE) { 235 - c->flag_error = FLAG_ERROR_INTEGER_OVERFLOW; 236 - c->flag_error_name = flag; 237 - return false; 238 - } 239 - 240 - c->flags[i].val.as_uint64 = result; 241 - } 242 - break; 243 - 244 - case FLAG_SIZE: { 245 - if (argc == 0) { 246 - c->flag_error = FLAG_ERROR_NO_VALUE; 247 - c->flag_error_name = flag; 248 - return false; 249 - } 250 - char *arg = flag_shift_args(&argc, &argv); 251 - 252 - static_assert(sizeof(unsigned long long int) == sizeof(size_t), "The original author designed this for x86_64 machine with the compiler that expects unsigned long long int and size_t to be the same thing, so they could use strtoull() function to parse it. Please adjust this code for your case and maybe even send the patch to upstream to make it work on a wider range of environments."); 253 - char *endptr; 254 - // TODO: replace strtoull with a custom solution 255 - // That way we can get rid of the dependency on errno and static_assert 256 - unsigned long long int result = strtoull(arg, &endptr, 10); 257 - 258 - // TODO: handle more multiplicative suffixes like in dd(1). From the dd(1) man page: 259 - // > N and BYTES may be followed by the following 260 - // > multiplicative suffixes: c =1, w =2, b =512, kB =1000, K 261 - // > =1024, MB =1000*1000, M =1024*1024, xM =M, GB 262 - // > =1000*1000*1000, G =1024*1024*1024, and so on for T, P, 263 - // > E, Z, Y. 264 - if (strcmp(endptr, "K") == 0) { 265 - result *= 1024; 266 - } else if (strcmp(endptr, "M") == 0) { 267 - result *= 1024*1024; 268 - } else if (strcmp(endptr, "G") == 0) { 269 - result *= 1024*1024*1024; 270 - } else if (strcmp(endptr, "") != 0) { 271 - c->flag_error = FLAG_ERROR_INVALID_SIZE_SUFFIX; 272 - c->flag_error_name = flag; 273 - // TODO: capability to report what exactly is the wrong suffix 274 - return false; 275 - } 276 - 277 - if (result == ULLONG_MAX && errno == ERANGE) { 278 - c->flag_error = FLAG_ERROR_INTEGER_OVERFLOW; 279 - c->flag_error_name = flag; 280 - return false; 281 - } 282 - 283 - c->flags[i].val.as_size = result; 284 - } 285 - break; 286 - 287 - case COUNT_FLAG_TYPES: 288 - default: { 289 - assert(0 && "unreachable"); 290 - exit(69); 291 - } 292 - } 293 - 294 - found = true; 295 - } 296 - } 297 - 298 - if (!found) { 299 - c->flag_error = FLAG_ERROR_UNKNOWN; 300 - c->flag_error_name = flag; 301 - return false; 302 - } 303 - } 304 - 305 - c->rest_argc = argc; 306 - c->rest_argv = argv; 307 - return true; 308 - } 309 - 310 - void flag_print_options(FILE *stream) 311 - { 312 - Flag_Context *c = &flag_global_context; 313 - for (size_t i = 0; i < c->flags_count; ++i) { 314 - Flag *flag = &c->flags[i]; 315 - 316 - fprintf(stream, " -%s\n", flag->name); 317 - fprintf(stream, " %s\n", flag->desc); 318 - static_assert(COUNT_FLAG_TYPES == 4, "Exhaustive flag type defaults printing"); 319 - switch (c->flags[i].type) { 320 - case FLAG_BOOL: 321 - if (flag->def.as_bool) { 322 - fprintf(stream, " Default: %s\n", flag->def.as_bool ? "true" : "false"); 323 - } 324 - break; 325 - case FLAG_UINT64: 326 - fprintf(stream, " Default: %" PRIu64 "\n", flag->def.as_uint64); 327 - break; 328 - case FLAG_SIZE: 329 - fprintf(stream, " Default: %zu\n", flag->def.as_size); 330 - break; 331 - case FLAG_STR: 332 - if (flag->def.as_str) { 333 - fprintf(stream, " Default: %s\n", flag->def.as_str); 334 - } 335 - break; 336 - default: 337 - assert(0 && "unreachable"); 338 - exit(69); 339 - } 340 - } 341 - } 342 - 343 - void flag_print_error(FILE *stream) 344 - { 345 - Flag_Context *c = &flag_global_context; 346 - static_assert(COUNT_FLAG_ERRORS == 6, "Exhaustive flag error printing"); 347 - switch (c->flag_error) { 348 - case FLAG_NO_ERROR: 349 - // NOTE: don't call flag_print_error() if flag_parse() didn't return false, okay? ._. 350 - fprintf(stream, "Operation Failed Successfully! Please tell the developer of this software that they don't know what they are doing! :)"); 351 - break; 352 - case FLAG_ERROR_UNKNOWN: 353 - fprintf(stream, "ERROR: -%s: unknown flag\n", c->flag_error_name); 354 - break; 355 - case FLAG_ERROR_NO_VALUE: 356 - fprintf(stream, "ERROR: -%s: no value provided\n", c->flag_error_name); 357 - break; 358 - case FLAG_ERROR_INVALID_NUMBER: 359 - fprintf(stream, "ERROR: -%s: invalid number\n", c->flag_error_name); 360 - break; 361 - case FLAG_ERROR_INTEGER_OVERFLOW: 362 - fprintf(stream, "ERROR: -%s: integer overflow\n", c->flag_error_name); 363 - break; 364 - case FLAG_ERROR_INVALID_SIZE_SUFFIX: 365 - fprintf(stream, "ERROR: -%s: invalid size suffix\n", c->flag_error_name); 366 - break; 367 - case COUNT_FLAG_ERRORS: 368 - default: 369 - assert(0 && "unreachable"); 370 - exit(69); 371 - } 372 - } 373 - 374 - #endif 375 - // Copyright 2021 Alexey Kutepov <reximkut@gmail.com> 376 - // 377 - // Permission is hereby granted, free of charge, to any person obtaining a copy 378 - // of this software and associated documentation files (the "Software"), to 379 - // deal in the Software without restriction, including without limitation the 380 - // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 381 - // sell copies of the Software, and to permit persons to whom the Software is 382 - // furnished to do so, subject to the following conditions: 383 - // 384 - // The above copyright notice and this permission notice shall be included in 385 - // all copies or substantial portions of the Software. 386 - // 387 - // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 388 - // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 389 - // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 390 - // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 391 - // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 392 - // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 393 - // IN THE SOFTWARE.
+3 -1
viv/cli.c
··· 5 5 6 6 #include <stdlib.h> 7 7 8 - #include <viv/flag.h> 9 8 #include <viv/gemini.h> 10 9 #include <viv/log.h> 11 10 #include <viv/viv.h> 11 + 12 + #define FLAG_IMPLEMENTATION 13 + #include <flag/flag.h> 12 14 13 15 CLI_handle CLI_cli(int argc, const char *argv[]) { 14 16 CLI_handle cli_handle;
-6
viv/flag.c
··· 1 - /* Copyright (C) 2021-2021 Fuwn 2 - * SPDX-License-Identifier: GPL-3.0-only */ 3 - 4 - #define FLAG_IMPLEMENTATION 5 - 6 - #include <viv/flag.h>