waitui/app/src/main.c
2022-01-20 23:49:46 +01:00

163 lines
6.8 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "waitui/version.h"
#include <waitui/log.h>
#include <errno.h>
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
// -----------------------------------------------------------------------------
// Local defines
// -----------------------------------------------------------------------------
#define WAITUI_SUCCESS 0
#define WAITUI_FAILURE 1
#define WAITUI_MEMORY_ERROR 2
// -----------------------------------------------------------------------------
// Local variables
// -----------------------------------------------------------------------------
// clang-format off
static const char * waitui_shortOptions = "vhqd::";
static const struct option waitui_longOptions[] = {
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, 'h'},
{"quiet", no_argument, 0, 'q'},
{"debug", optional_argument, 0,'d'},
0
};
// clang-format on
// -----------------------------------------------------------------------------
// Local functions
// -----------------------------------------------------------------------------
// clang-format off
#define waitui_printLogo(s) \
do { \
fprintf((s), "\n"); \
fprintf((s), "\n"); \
fprintf((s), " ___ __ \n"); \
fprintf((s), " (_ ( . ) )__ -.- '. \\ : / .' \n"); \
fprintf((s), " '(___(_____) -.- '. \\ : / .' \n"); \
fprintf((s), " '. \\ : / .' \n"); \
fprintf((s), " __ -----____ _ _____----- \n"); \
fprintf((s), "_________________/. _\\________________________________(_)______________________\n"); \
fprintf((s), " .--.|/_/__ \n"); \
fprintf((s), " ''.--o/___ \\ ~ ~ ,-----------------------, \n"); \
fprintf((s), " ~ /.'o|_o '.| ~ | | \n"); \
fprintf((s), " |/ |_| ~ | Waitui | \n"); \
fprintf((s), " ~ ' |_| ~ | by Rick | \n"); \
fprintf((s), " __|_|-;.___.;--.,___ | | \n"); \
fprintf((s), " ~ _,,-\"\" |_| \"\"-,,_ `------------------------' \n"); \
fprintf((s), " .-'´'´ |_| ``-. ~ \n"); \
fprintf((s), " \", |_| ,\" ~ \n"); \
fprintf((s), " \"-_ _-\" ~ \n"); \
fprintf((s), " ~ ``----..,_ , __,,..---'´ ~ ~ \n"); \
fprintf((s), " ```'''''' ~ ~ \n"); \
} \
while (0)
// clang-format on
/**
* @brief Print version.
* @param[in] stream Where the version should be printed
* @param logo Whether to print the logo
*/
void waitui_printVersion(FILE *stream, int logo) {
fprintf(stream, "%s version %s", WAITUI_VERSION_NAME,
WAITUI_VERSION_STRING);
if (logo) { waitui_printLogo(stream); }
}
/**
* @brief Print help.
* @param[in] stream Where the help should be printed
*/
void waitui_printHelp(FILE *stream) {
waitui_printVersion(stream, 0);
fprintf(stream, "\n\n");
fprintf(stream, "Usage: %s[-hvq] [-d level]\n", WAITUI_VERSION_NAME);
fprintf(stream, "\t--help, -h \t\tDisplays this help\n");
fprintf(stream, "\t--version, -v \t\tDisplays the version\n");
fprintf(stream, "\t--quiet, -q \t\tDisplays the version\n");
fprintf(stream, "\t--debug=level, -d level\t\tDisplays the version\n");
}
// -----------------------------------------------------------------------------
// Main function
// -----------------------------------------------------------------------------
int main(int argc, char **argv) {
int result = WAITUI_SUCCESS;
bool quite = false;
long loglevel = WAITUI_LOG_DEBUG;
waitui_log_setQuiet(quite);
waitui_log_setLevel(loglevel);
while (1) {
int index = -1;
struct option *opt = NULL;
int optRes = getopt_long(argc, argv, waitui_shortOptions,
waitui_longOptions, &index);
if (optRes == -1) break; /* end of list */
switch (optRes) {
case 'v':
waitui_printVersion(stdout, 1);
return WAITUI_SUCCESS;
case 'h':
waitui_printHelp(stdout);
return WAITUI_SUCCESS;
case 'q':
quite = true;
break;
case 'd':
if (optarg) {
char *endPtr = NULL;
errno = 0;
loglevel = strtol(optarg, &endPtr, 10);
if (errno != 0 || endPtr == optarg ||
loglevel < WAITUI_LOG_TRACE ||
loglevel >= WAITUI_LOG_MAX) {
waitui_log_warn(
"no valid log level given or out of range "
"(%d - %d): %s",
WAITUI_LOG_TRACE, WAITUI_LOG_MAX - 1, optarg);
}
}
break;
case 0:
/* all parameter that do not appear in the option string */
opt = (struct option *) &(waitui_longOptions[index]);
printf("'%s' was specified.", opt->name);
if (opt->has_arg == required_argument)
printf("Arg: <%s>", optarg);
printf("\n");
break;
default: /* unknown */
break;
}
}
/* print all others parameters */
while (optind < argc) { printf("other parameter: <%s>\n", argv[optind++]); }
waitui_log_setQuiet(quite);
waitui_log_setLevel(loglevel);
waitui_log_debug("waitui start execution");
waitui_log_debug("waitui execution done");
return result;
}