lib

morphux C utility library
Log | Files | Refs | Submodules | README | LICENSE | git clone https://git.ne02ptzero.me/git/lib

commit d7b63055f6f43680ffe9f83f6895e67346fe5d96
parent 4e4034bfbbf660ac9bb2a2b2ef55371b41239b4d
Author: Ne02ptzero <louis@ne02ptzero.me>
Date:   Tue, 17 Jan 2017 18:16:12 +0100

Add(Args support): Now support advanced use of arguments

Diffstat:
MMakefile | 2+-
Ainc/m_args.h | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Minc/m_types.h | 2+-
Minc/morphux.h | 1+
Asrc/m_args.c | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
5 files changed, 171 insertions(+), 2 deletions(-)

diff --git a/Makefile b/Makefile @@ -17,7 +17,7 @@ NAME= libmorphux.a CC = gcc LIB = ar -CFLAGS = -Wall -Wextra -Werror -O3 -I inc/ +CFLAGS = -Wall -Wextra -Werror -I inc/ -std=c99 LFLAGS = -cq SRCS = $(wildcard src/*.c) OBJS = $(SRCS:%.c=%.o) diff --git a/inc/m_args.h b/inc/m_args.h @@ -0,0 +1,58 @@ +/*********************************** LICENSE **********************************\ +* Copyright 2017 Morphux * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); * +* you may not use this file except in compliance with the License. * +* You may obtain a copy of the License at * +* * +* http://www.apache.org/licenses/LICENSE-2.0 * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +\******************************************************************************/ + +#ifndef M_ARGS_H +# define M_ARGS_H + +# include <stdbool.h> +# include <m_types.h> +# include <m_print.h> + +typedef struct s_args { + /** + * Single letter option + * Example: -f, -s + */ + char opt; + + /** + * Full string option + * Example: --force, --skip + */ + char *s_opt; + + /** + * Description of the option. + * Used for the help + */ + char *desc; + + /** + * Boolean that describe if the option must take an argument + */ + bool take_arg; + + /** + * Callback of the option + */ + void (*callback)(char *); +} margs_t; + + +u32_t read_opt(const int ac, const char **av, const margs_t *args); +static void opt_help(const margs_t *args); + +#endif /* M_ARGS_H */ diff --git a/inc/m_types.h b/inc/m_types.h @@ -24,7 +24,7 @@ typedef signed short s16_t; typedef signed int s32_t; typedef signed long long s64_t; -typedef unsigned char us8_t; +typedef unsigned char u8_t; typedef unsigned short u16_t; typedef unsigned int u32_t; typedef unsigned long long u64_t; diff --git a/inc/morphux.h b/inc/morphux.h @@ -19,5 +19,6 @@ # include <m_types.h> # include <m_print.h> +# include <m_args.h> #endif /* MORPHUX_H */ diff --git a/src/m_args.c b/src/m_args.c @@ -0,0 +1,110 @@ +/*********************************** LICENSE **********************************\ +* Copyright 2017 Morphux * +* * +* Licensed under the Apache License, Version 2.0 (the "License"); * +* you may not use this file except in compliance with the License. * +* You may obtain a copy of the License at * +* * +* http://www.apache.org/licenses/LICENSE-2.0 * +* * +* Unless required by applicable law or agreed to in writing, software * +* distributed under the License is distributed on an "AS IS" BASIS, * +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * +* See the License for the specific language governing permissions and * +* limitations under the License. * +\******************************************************************************/ + +#include <m_args.h> + +/*! + * \brief Read the options given by the program + * \param ac Number of argument in av + * \param av Array of string, containing the arguments + * \param args Array of margs_t, containing the preset options. Must end with + * an empty structure. + * \return Number of options read + * + * The read_opt function reads a given list of arguments, and parse the options + * in it. The options are read from the args array. + * If an option is not known, the function calls the help and quit. + * If the option is -h | --help is passed, the function call the help and quit. + * + * \note Only the arguments beginning with - are parsed. + */ +u32_t read_opt(const int ac, const char **av, const margs_t *args) { + u32_t ret = 0, it; + u8_t n_dash; + + for (u32_t i = 1, j = 0; i < (u32_t)ac; i++) { + + if (av[i] == NULL || strlen(av[i]) == 0) + continue ; + + n_dash = 0; + for (j = 0; av[i][j] != '\0' && av[i][j] == '-'; j++) + n_dash++; + + /* Argument have more than two '-' */ + if (n_dash > 2) { + m_error("Malformed option: %s\n", av[i]); + continue ; + } + + /* Single letter option */ + if (n_dash == 1) { + + if (strlen(av[i]) < 2) { + m_error("Dash without option. Ignoring...\n"); + continue ; + } + + /* Help option */ + if (av[i][1] == 'h') + opt_help(args); + + /* Search the option in the args array */ + for (it = 0; args[it].opt != 0 && args[it].opt != av[i][1]; it++) + ; + + /* Can't find the option */ + if (args[it].opt == 0) { + m_error("Unknow option -%s\n", &(av[i][1])); + opt_help(args); + } else { + args[it].callback(NULL); + } + + /* Word option */ + } else if (n_dash == 2) { + if (strlen(av[i]) < 3) { + m_error("Double dash without option. Ignoring...\n"); + continue ; + } + + if (strcmp(&(av[i][2]), "help") == 0) + opt_help(args); + + /* Search the option in the args array */ + for (it = 0; args[it].opt != 0 && (args[it].s_opt != NULL && + strcmp(args[it].s_opt, &(av[i][2])) != 0); it++) + ; + + /* Can't find the option */ + if (args[it].opt == 0) { + m_error("Unknown option %s\n", av[i]); + opt_help(args); + } else { + args[it].callback(NULL); + } + } + } + return ret; +} + +static void opt_help(const margs_t *args) { + m_info("Help:\n"); + for (u32_t i = 0; args[i].opt != 0; i++) { + m_info("\t-%c | --%s : %s\n", args[i].opt, args[i].s_opt, args[i].desc); + } + _exit(1); +}