summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/shared.c99
-rw-r--r--src/util/shared.h13
2 files changed, 112 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;
+}
+
diff --git a/src/util/shared.h b/src/util/shared.h
index 5e5f4a9..c0945c1 100644
--- a/src/util/shared.h
+++ b/src/util/shared.h
@@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
+#include <fcntl.h>
#define ANSCII "\x1b["
#define NEXT ";"
@@ -44,3 +45,15 @@ bool prefix(const char* pre, const char* str);
void print_file_size(size_t bytes, char buf[5]);
void print_date_time(time_t mills, char buf[13]);
+
+#define UNUSED(x) (void)(x)
+#define ARG_UNUSED 0
+#define ARG_USED 1
+#define ARG_IGNORE 2
+
+void check_arg (char* arg);
+void parse_help (int argc, char** argv, void (*help)(void));
+int parse_args (int argc, char** argv, void (*help)(void), int (*short_arg)(char, char*), int (*long_arg)(char*, char*));
+
+int get_tty();
+FILE* get_tty_stream(char* type);