aboutsummaryrefslogtreecommitdiff
path: root/src/options.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/options.c')
-rw-r--r--src/options.c118
1 files changed, 118 insertions, 0 deletions
diff --git a/src/options.c b/src/options.c
new file mode 100644
index 0000000..214a72c
--- /dev/null
+++ b/src/options.c
@@ -0,0 +1,118 @@
+/* SPDX-License-Identifier: Apache-2.0 */
+
+/* Copyright 2026 Thorsten Töpper
+ *
+ * vim:ts=4:sw=4:expandtab
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <getopt.h>
+#include <stdbool.h>
+#include <ctype.h>
+
+#include "options.h"
+#include "trace_macros.h"
+
+
+/* === GLOBAL VARIABLES === */
+struct option long_options[] = {
+ { "help", no_argument, 0, 0 },
+ { "quiet", no_argument, 0, 0 },
+ { "show-hidden-entries", no_argument, 0, 0 },
+ { 0, 0, 0, 0 }
+};
+
+bool option_quiet = false;
+bool option_show_hidden_entries = false;
+
+char *exec_name;
+
+/* === IMPLEMENTATION === */
+
+void usage(char *executable) {
+ fprintf(stderr, "Call: %s OPTIONS path_to_open\n", executable);
+ fprintf(stderr, "\nOPTIONS are\n");
+ /* long name, short name, optional argument, explanation */
+ fprintf(stderr, " %-25s %2s %10s - %s\n", "--help", "-h", "",
+ "Show this message and exit");
+ fprintf(stderr, " %-25s %2s %10s - %s\n", "--quiet", "-q", "",
+ "Don't print error messages or warnings");
+ fprintf(stderr, " %-25s %2s %10s - %s\n", "--show-hidden-entries", "-a", "",
+ "Show hidden entries in the directory");
+}
+
+
+void set_option(const char *option_name, char *option_argument) {
+
+ DBGTRC("DEBUG: called with option_name '%s' and option_argument '%s'\n",
+ option_name, option_argument);
+
+ if (option_name == NULL)
+ return;
+
+ /* options WITHOUT arguments */
+ if (strcmp("help", option_name) == 0) {
+ usage(exec_name);
+ exit(EXIT_SUCCESS);
+ }
+
+ if (strcmp("quiet", option_name) == 0) {
+ option_quiet = true;
+ return;
+ }
+
+ if (strcmp("show-hidden-entries", option_name) == 0) {
+ option_show_hidden_entries = true;
+ return;
+ }
+
+ /* options WITH arguments */
+ if (option_argument == NULL || option_argument[0] == '\0') {
+ LOGERR("ERROR: option_name %s with missing option_argument\n",
+ option_name);
+ exit(EXIT_FAILURE);
+ }
+
+ LOGERR("ERROR: Option '%s' not recognized\n.", option_name);
+}
+
+
+int parse_arguments(int argc, char **argv) {
+ int c = 0, index;
+ /* exec_name is a file internal global variable for --help in set_option() */
+ exec_name = argv[0];
+
+ while(1) {
+ index = 0;
+ c = getopt_long(argc, argv, "hqs", long_options, &index);
+
+ if (c == -1) {
+ break;
+ }
+
+ switch (c) {
+ case 0:
+ set_option(long_options[index].name, optarg);
+ break;
+ case 'h':
+ usage(exec_name);
+ exit(EXIT_SUCCESS);
+ case 'q':
+ option_quiet = true;
+ break;
+ case 's':
+ option_show_hidden_entries = true;
+ break;
+ case '?':
+ break;
+ default:
+ LOGERR("ERROR: unrecognized option 0x%02X '%c'\n", c, c);
+ usage(exec_name);
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ return optind;
+}
+