diff options
author | Tyler Murphy <tylerm@tylerm.dev> | 2023-05-01 18:43:32 -0400 |
---|---|---|
committer | Tyler Murphy <tylerm@tylerm.dev> | 2023-05-01 18:43:32 -0400 |
commit | 82e55dde69dc89fdf06e751b75449f35836282a1 (patch) | |
tree | 29e49ae19cba7f6a7c2987c381ebe4b253edc053 /src/util/shared.c | |
parent | move back to c99 (diff) | |
download | lazysphere-82e55dde69dc89fdf06e751b75449f35836282a1.tar.gz lazysphere-82e55dde69dc89fdf06e751b75449f35836282a1.tar.bz2 lazysphere-82e55dde69dc89fdf06e751b75449f35836282a1.zip |
refactor and xargs
Diffstat (limited to '')
-rw-r--r-- | src/util/shared.c | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/util/shared.c b/src/util/shared.c index d512972..e207c6d 100644 --- a/src/util/shared.c +++ b/src/util/shared.c @@ -2,10 +2,12 @@ #include <errno.h> #include <stdarg.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> #include <sys/stat.h> +#include <paths.h> void error(const char* format, ...) { va_list list; @@ -132,3 +134,100 @@ void print_date_time(time_t mills, char buf[13]) { snprintf(buf + n, 13 - n, "%d %02d:%02d ", info->tm_mday, info->tm_hour, info->tm_sec); } + +#ifndef MAJOR +#define MAJOR 0 +#endif + +#ifndef MINOR +#define MINOR 0 +#endif + +#ifndef PATCH +#define PATCH 0 +#endif + +void check_arg (char* arg) { + if (arg == NULL) { + error("error: expected another argument after option"); + } +} + +static void global_help(void (*help)(void)) { + printf("LazySphere v%d.%d.%d multi-call binary.\n\n", MAJOR, MINOR, PATCH); + help(); + exit(EXIT_SUCCESS); +} + +void parse_help(int argc, char** argv, void (*help)(void)) { + if (argc < 1) return; + for (int i = 0; i < argc; i++) { + if (!prefix("-", argv[i]) || streql("-", argv[i])) break; + if (help != NULL && streql("--help", argv[i])) global_help(help); + } +} + +int parse_args(int argc, char** argv, void (*help)(void), int (*short_arg)(char, char*), int (*long_arg)(char*, char*)) { + if (argc < 1) return 0; + + int start = 0; + for (int i = 0; i < argc; i++) { + if (!prefix("-", argv[i]) || streql("-", argv[i])) break; + if (help != NULL && streql("--help", argv[i])) global_help(help); + + char* next_arg; + if (i + 1 == argc) { + next_arg = NULL; + } else { + next_arg = argv[i+1]; + } + + int current = i; + + if (prefix("--", argv[i])) { + if (long_arg == NULL) { + goto exit; + } + int r = long_arg(argv[current], next_arg); + if (r == ARG_USED) { + i++; + start++; + } else if (r == ARG_IGNORE) { + goto exit; + } + } else { + if (short_arg == NULL) { + goto exit; + } + for (size_t j = 1; j < strlen(argv[current]); j++) { + int r = short_arg(argv[current][j], next_arg); + if (r == ARG_USED) { + i++; + start++; + } else if (r == ARG_IGNORE) { + goto exit; + } + } + } + + start++; + } +exit: + return start; +} + +int get_tty() { + int fd = open(_PATH_TTY, O_RDONLY); + if (fd < 0) error("error: failed to get tty: %s", strerror(errno)); + return fd; +} + +FILE* get_tty_stream(char* type) { + int fd = get_tty(); + FILE* file = fdopen(fd, type); + if (file == NULL) { + error("error: failed to open tty stream: %s", strerror(errno)); + } + return file; +} + |