From 62542734b50acd0b5c3b48ea54d5e0d9339d0331 Mon Sep 17 00:00:00 2001 From: zegonix Date: Mon, 22 Jun 2026 23:16:45 +0200 Subject: [PATCH] started argument parsing using `argp` --- Makefile | 3 +- inc/arguments.h | 24 +++++++++++++ src/arguments.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 22 ++++++++---- 4 files changed, 131 insertions(+), 7 deletions(-) create mode 100644 inc/arguments.h create mode 100644 src/arguments.c diff --git a/Makefile b/Makefile index a3a3d64..a1ec6e7 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,8 @@ WCC := -Wconversion \ C_DEF := C_SRC := main.c \ - log.c + log.c \ + arguments.c C_FLAGS := -std=c11 \ $(WG) \ diff --git a/inc/arguments.h b/inc/arguments.h new file mode 100644 index 0000000..21ac8d7 --- /dev/null +++ b/inc/arguments.h @@ -0,0 +1,24 @@ +/* -------------------------------------------------------- + * -- project: SCAN + * -- description: handle commandline arguments + * -------------------------------------------------------- + */ + +#ifndef __ARGUMENTS_H +#define __ARGUMENTS_H + +#include +#include + +typedef struct { + bool show_all; + bool show_almost_all; + bool group_directories; + bool human_readable; + bool long_view; + bool tree_view; +} arguments_t; + +error_t parse_args( int argc, char** argv, arguments_t* arguments ); + +#endif diff --git a/src/arguments.c b/src/arguments.c new file mode 100644 index 0000000..ad00e4c --- /dev/null +++ b/src/arguments.c @@ -0,0 +1,89 @@ +#include +#include "arguments.h" + +// function prototypes +static error_t arg_parser( int key, char* arg, struct argp_state* state ); + +// global variable definitions +const char* argp_program_version = "0.1"; +static char description[] = "`scan` is an alternative to `ls` with better colour support and builtin tree view."; +static char argument_description[] = "ARGUMENTS:"; + +static struct argp_option options[] = +{ + { + "all", + 'a', + NULL, + OPTION_ARG_OPTIONAL, + "show all items (including hidden ones)", + 0 + }, + { + "almost-all", + 'A', + NULL, + OPTION_ARG_OPTIONAL, + "same --all, excluding . and ..", + 0 + }, + { + "group-directories", + 'g', + NULL, + OPTION_ARG_OPTIONAL, + "group directories at the top", + 0 + }, + { + "human-readable", + 'h', + NULL, + OPTION_ARG_OPTIONAL, + "show sizes in human readable format", + 0 + }, + { + "long", + 'l', + NULL, + OPTION_ARG_OPTIONAL, + "use long listing format", + 0 + }, + { + "tree", + 'T', + NULL, + OPTION_ARG_OPTIONAL, + "use tree listing format", + 0 + } +}; + +static struct argp parser = { + options, + arg_parser, + argument_description, + description, + NULL, + NULL, + NULL +}; + + +// module logic +error_t parse_args( int argc, char** argv, arguments_t* arguments ) +{ + // TODO: revisit ARG_INDEX for handling unknown arguments + return argp_parse(&parser, argc, argv, 0, NULL, arguments); +} + +static error_t arg_parser( int key, char* arg, struct argp_state* state ) +{ + ( void )key; + ( void )arg; + ( void )state; + + return 0; +} diff --git a/src/main.c b/src/main.c index 4410dd1..411ebe1 100644 --- a/src/main.c +++ b/src/main.c @@ -1,16 +1,22 @@ +// external dependencies #include #include #include +#include #include +#include #include + +// internal dependencies #include "log.h" +#include "arguments.h" -typedef struct dirent dirent; - +// program logic int main( int argc, char** argv ) { - ( void )argc; - ( void )argv; + arguments_t args; + + parse_args( argc, argv, &args ); if ( 2 < argc ) return -1; @@ -27,11 +33,15 @@ int main( int argc, char** argv ) } DIR* directory = opendir( argument_directory ); - dirent* entry; + struct dirent* entry; if ( !directory ) { - log_error( "failed to open directory `%s`", argument_directory ); + if ( ENOENT == errno ) + log_error( "no directory `%s`", argument_directory ); + else + log_error( "failed to open directory `%s`", argument_directory ); + return -1; }