From d943d6db8cb8dd08f5e1f920df35edf61baeaa70 Mon Sep 17 00:00:00 2001 From: vx-clutch Date: Thu, 17 Jul 2025 18:22:10 -0400 Subject: [PATCH] feat: standard argument parsing --- config.h | 12 +++++++ core/file.c | 85 ++++++++++++++++++++++++++++--------------------- core/standard.c | 29 +++++++++++++++++ core/standard.h | 6 ++++ yait/format.h | 5 +-- yait/main.c | 42 ++++++++++++++++++++---- 6 files changed, 134 insertions(+), 45 deletions(-) create mode 100644 config.h create mode 100644 core/standard.c create mode 100644 core/standard.h diff --git a/config.h b/config.h new file mode 100644 index 0000000..d88c949 --- /dev/null +++ b/config.h @@ -0,0 +1,12 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#define PROGRAM "yait" + +#define ORGANIZATION "vx_clutch" +#define VERSION "pre-alpha" +#define YEAR 2025 + +#define LICENCE_LINE "License BSD-3-Clause: BSD-3-Clause " + +#endif diff --git a/core/file.c b/core/file.c index 591f36b..7180ea4 100644 --- a/core/file.c +++ b/core/file.c @@ -7,60 +7,71 @@ #include #include -error_t touch(char *path, char *format, ...) { - error_t err = {0}; +error_t +touch (char *path, char *format, ...) +{ + error_t err = { 0 }; err.null = true; - FILE *fp = fopen(path, "w"); - if (!fp) { - err.null = false; - err.status = errno; - err.src = strerror(errno); - return err; - } + FILE *fp = fopen (path, "w"); + if (!fp) + { + err.null = false; + err.status = errno; + err.src = strerror (errno); + return err; + } - else { - va_list args; - va_start(args, format); - vfprintf(fp, format, args); - va_end(args); - } + else + { + va_list args; + va_start (args, format); + vfprintf (fp, format, args); + va_end (args); + } - fclose(fp); + fclose (fp); return err; } -error_t dir(char *format, ...) { - error_t err = {0}; +error_t +dir (char *format, ...) +{ + error_t err = { 0 }; err.null = true; // success by default va_list args; - va_start(args, format); + va_start (args, format); char path[1024]; - vsnprintf(path, sizeof(path), format, args); + vsnprintf (path, sizeof (path), format, args); - va_end(args); + va_end (args); - if (mkdir(path, 0777) < 0) { - err.null = false; - err.status = errno; - err.src = strerror(errno); - } + if (mkdir (path, 0777) < 0) + { + err.null = false; + err.status = errno; + err.src = strerror (errno); + } return err; } -error_t take(const char *dirname) { - error_t err = dir("%s", dirname); - if (!err.null) { - return err; - } - if (chdir(dirname) != 0) { - err.null = false; - err.status = errno; - err.src = strerror(errno); - return err; - } +error_t +take (const char *dirname) +{ + error_t err = dir ("%s", dirname); + if (!err.null) + { + return err; + } + if (chdir (dirname) != 0) + { + err.null = false; + err.status = errno; + err.src = strerror (errno); + return err; + } return err; } diff --git a/core/standard.c b/core/standard.c new file mode 100644 index 0000000..48f708f --- /dev/null +++ b/core/standard.c @@ -0,0 +1,29 @@ +#include "standard.h" +#include "../config.h" +#include "print.h" +#include +#include + +int +parse_standard_options (void (*usage) (), int argc, char **argv) +{ + for (int i = 1; i < argc; ++i) + { + if (strcmp (argv[i], "--help") == 0) + { + usage (); + exit (0); + } + else if (strcmp (argv[i], "--version") == 0) + { + printfn ("%s (%s) %s\nCopyright (C) %d %s.\n%s\n" + "This is free software: you are free to change " + "and redistribute it.\nThere is NO WARRANTY, to the extent " + "permitted by law.", + PROGRAM, ORGANIZATION, VERSION, YEAR, ORGANIZATION, + LICENCE_LINE); + exit (0); + } + } + return -1; +} diff --git a/core/standard.h b/core/standard.h new file mode 100644 index 0000000..f419887 --- /dev/null +++ b/core/standard.h @@ -0,0 +1,6 @@ +#ifndef STANDARD_H +#define STANDARD_H + +int parse_standard_options(void (*)(), int argc, char **argv); + +#endif diff --git a/yait/format.h b/yait/format.h index a81c8bc..78c509e 100644 --- a/yait/format.h +++ b/yait/format.h @@ -4,7 +4,7 @@ #include typedef enum { - OPENBSD3, + BSD3, GPLv3, MIT, UNLICENCE, @@ -17,7 +17,8 @@ typedef enum { } lib_t; typedef struct { - bool nogit; + bool git; + bool clang_format; licence_t licence; char *name; } format_t; diff --git a/yait/main.c b/yait/main.c index 94762b1..58735cc 100644 --- a/yait/main.c +++ b/yait/main.c @@ -1,9 +1,16 @@ #include "../core/file.h" #include "../core/print.h" +#include "../core/standard.h" #include "format.h" +#include +#include + +#define AUTHORS vx_clutch int create (format_t); +void usage () {}; + int main (int argc, char **argv) { @@ -12,9 +19,17 @@ main (int argc, char **argv) printfn ("error: not enough arguments."); return 1; } + int status = parse_standard_options (usage, argc, argv); + if (status) + { + printfn ("error: %s", strerror (status)); + return 1; + } format_t conf; conf.name = argv[1]; - conf.nogit = false; + conf.git = true; + conf.clang_format = true; + conf.licence = BSD3; create (conf); return 0; } @@ -22,11 +37,14 @@ main (int argc, char **argv) int create (format_t fmt) { - error_t err = take(fmt.name); - if (!err.null) { - printfn("failed to create or enter directory: %s", err.src); - return 1; - } + error_t err = take (fmt.name); + if (!err.null) + { + printfn ("failed to create or enter directory: %s", err.src); + return 1; + } + if (fmt.git) + system ("git init --quiet"); touch ("README", "%s ( concise description )\n\n" "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do " @@ -99,5 +117,17 @@ create (format_t fmt) "printf \"LDFLAGS=%%s\\n\" \"$LDFLAGS\" >> config.mak\n" "printf \"CC=%%s\\n\" \"$CC\" >> config.mak\n" "printf \"done\\n\"\n"); + if (fmt.clang_format) + touch (".clang-format", "Language: Cpp\nBasedOnStyle: GNU\n"); + switch (fmt.licence) + { + case BSD3: + touch ("COPYING", + "https://raw.githubusercontent.com/teamdigitale/licenses/" + "refs/heads/master/BSD-3-Clause"); + break; + default: + break; + } return 0; }