aboutsummaryrefslogtreecommitdiff
path: root/src/options.c
blob: 214a72c5e91430b2a95c78a6499b067b41a2debf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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;
}