yait

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit b7e67b144038e3a98284b0abfa38073b139fdb44
parent dd5673009a7a6203779df25460795793bd72236b
Author: vx-clutch <[email protected]>
Date:   Tue,  5 Aug 2025 20:53:07 -0400

save

Diffstat:
Dyait/debug.h | 35-----------------------------------
Myait/main.c | 332++++++++++++++++++++++++++++++++++++++++++++-----------------------------------
Ryait/format.h -> yait/manifest.h | 0
3 files changed, 187 insertions(+), 180 deletions(-)

diff --git a/yait/debug.h b/yait/debug.h @@ -1,35 +0,0 @@ -#ifndef DEBUG_H -#define DEBUG_H - -#ifdef DEBUG -#define on_error(msg, code) \ - if (err) \ - { \ - fprintf (stderr, "\n"); \ - printfn (msg ": %s", strerror (err)); \ - return code; \ - } -#else -#define on_error(msg, code) \ - if (code) \ - { \ - printfn (msg ": %s", strerror (code)); \ - return code; \ - } -#endif - -#ifdef DEBUG -#define debug(fmt, ...) \ - fprintf (stderr, "\e[0;32m[%8d]\e[0;33m " fmt "\e[0m\n", __LINE__, \ - ##__VA_ARGS__) -#define debugc(fmt, ...) \ - fprintf (stderr, "\e[0;32m[%8d]\e[0;33m " fmt "... \e[0m", __LINE__, \ - ##__VA_ARGS__) -#define done fprintf (stderr, "done.\n") -#else -#define debug(fmt, ...) ((void)0) -#define debugc(fmt, ...) ((void)0) -#define done ((void)0) -#endif - -#endif // DEBUG_H diff --git a/yait/main.c b/yait/main.c @@ -22,8 +22,7 @@ #include "../core/print.h" #include "../core/standard.h" #include "contents.h" -#include "debug.h" -#include "format.h" +#include "manifest.h" #define DEFAULT_USER_NAME "unknown" #define DEFAULT_PROJECT_NAME "Project" @@ -62,6 +61,59 @@ usage (int status) } // clang-format on +static int parse_arguments(manifest_t *conf, int argc, char **argv) +{ + static struct option long_options[] = { + { "GNU", no_argument, 0, 'g' }, + { "use-cpp", no_argument, 0, 'c' }, + { "git", no_argument, 0, 'i' }, + { "licence", required_argument, 0, 'l' }, + { "lib", required_argument, 0, 'L' }, + { "help", no_argument, 0, 'h' }, + { 0, 0, 0, 0 } + }; + + int opt; + while ((opt = getopt_long(argc, argv, "gcil:L:h", long_options, + NULL)) != -1) { + switch (opt) { + case 'g': + conf->flag.GNU = true; + break; + case 'c': + conf->flag.use_cpp = true; + break; + case 'i': + conf->flag.git = true; + break; + case 'l': + if (strcmp(optarg, "bsd") == 0) + conf->licence = BSD3; + else if (strcmp(optarg, "gpl") == 0) + conf->licence = GPLv3; + else if (strcmp(optarg, "mit") == 0) + conf->licence = MIT; + break; + case 'L': + ADD_LIBRARY(conf->libraries, TOLibrary(optarg)); + break; + case 'h': + usage(0); + exit(0); + default: + usage(1); + exit(1); + } + } + + /* now handle positional args */ + if (optind < argc) + conf->project = argv[optind++]; + if (optind < argc) + conf->name = argv[optind++]; + return 0; +} + /* This macro exist purely because I like how it looks. This should be called in every function that creates file to ensure they are being created in right place. */ @@ -77,18 +129,41 @@ static int reset_path_() return 0; } +int program_exists(const char *prog) +{ + char *path = getenv("PATH"); + if (!path) + return 1; + + char *copy = strdup(path); + if (!copy) + return 1; + + char *dir = strtok(copy, ":"); + while (dir) { + char buf[4096]; + snprintf(buf, sizeof(buf), "%s/%s", dir, prog); + if (access(buf, X_OK) == 0) { + free(copy); + return 0; + } + dir = strtok(NULL, ":"); + } + + free(copy); + return 1; +} + // TODO(vx-clutch): sanitize the alpha-numeric range +// clang-format off static int sanitize(manifest_t *m) { - if (!m->project) - m->project = DEFAULT_PROJECT_NAME; - if (!m->name) - m->name = DEFAULT_USER_NAME; - if (!(m->licence == UNLICENCE)) - m->licence = DEFAULT_LICENCE; + if (!m->project) m->project = DEFAULT_PROJECT_NAME; + if (!m->name) m->name = DEFAULT_USER_NAME; + if (!(m->licence == UNLICENCE)) m->licence = DEFAULT_LICENCE; + m->flag.git = m->flag.git ? true : DEFAULT_GIT_INIT; - m->flag.clang_format = m->flag.clang_format ? true : - DEFAULT_CLANG_FORMAT; + m->flag.clang_format = m->flag.clang_format ? true : DEFAULT_CLANG_FORMAT; m->flag.GNU = m->flag.GNU ? true : DEFAULT_GNU; if (!m->name) { @@ -98,8 +173,8 @@ static int sanitize(manifest_t *m) return 0; } +// clang-format on -#define get(url) status = system("git submodule add -q " url) static int create_libraries(manifest_t manifest) { int status = 0; @@ -107,18 +182,28 @@ static int create_libraries(manifest_t manifest) if (!manifest.libraries) { return status; } - reset_path; + + /* reset_path; */ + while (depth != 0) { + if (chdir("..") != 0) + return errno; + else + --depth; + } for (int i = 0; i < LIB_COUNT_; ++i) { - if HAS_LIBRARY (manifest.libraries, LIB_RAYLIB) { + if (HAS_LIBRARY(manifest.libraries, LIB_RAYLIB)) { REMOVE_LIBRARY(manifest.libraries, LIB_RAYLIB); - get("https://github.com/raysan5/raylib"); - } else if HAS_LIBRARY (manifest.libraries, LIB_NCURSES) { + status = system( + "git submodule add -q https://github.com/raysan5/raylib"); + } else if (HAS_LIBRARY(manifest.libraries, LIB_NCURSES)) { REMOVE_LIBRARY(manifest.libraries, LIB_NCURSES); - get("https://github.com/mirror/ncurses"); - } else if HAS_LIBRARY (manifest.libraries, LIB_CURL) { + status = system( + "git submodule add -q https://github.com/mirror/ncurses"); + } else if (HAS_LIBRARY(manifest.libraries, LIB_CURL)) { REMOVE_LIBRARY(manifest.libraries, LIB_CURL); - get("https://github.com/raysan5/raylib"); + status = system( + "git submodule add -q https://github.com/raysan5/raylib"); } } return status; @@ -129,27 +214,29 @@ static int create_licence(manifest_t manifest, char **licence_line_buffer) if (manifest.licence == UNLICENCE) return 0; - reset_path; - /* TODO(vx-clutch): Run better checks on licence_line_buffer to ensure we have enough - space. This could be done through a multitude of ways; that is for you to - figure out. */ - assert(licence_line_buffer != NULL); + /* reset_path; */ + while (depth != 0) { + if (chdir("..") != 0) + return errno; + else + --depth; + } -// TODO(vx-clutch): Remove this and actually implement the features. -#define TODO() \ - printfn("Not impl"); \ - assert(1 == 2) + assert(licence_line_buffer != NULL); switch (manifest.licence) { case BSD3: *licence_line_buffer = "Bsd"; - TODO(); + printfn("Not impl"); + assert(1 == 2); break; case GPLv3: - TODO(); + printfn("Not impl"); + assert(1 == 2); break; case MIT: - TODO(); + printfn("Not impl"); + assert(1 == 2); break; case UNLICENCE: default: @@ -165,7 +252,13 @@ static int maybe_create_clang_format(manifest_t manifest) if (!manifest.flag.clang_format) return 0; - reset_path; + /* reset_path; */ + while (depth != 0) { + if (chdir("..") != 0) + return errno; + else + --depth; + } status = create_file_with_content(".clang-format", clang_format_template); @@ -178,13 +271,19 @@ static int setup_git(manifest_t manifest) if (!manifest.flag.git) { return 0; } - reset_path; + + /* reset_path; */ + while (depth != 0) { + if (chdir("..") != 0) + return errno; + else + --depth; + } int status = system("git init --quiet"); if (status) { printfn("failed on git initialize: %s", strerror(status)); } - return status; } @@ -234,21 +333,21 @@ static int generate_source_code(manifest_t manifest) { int status; - debug("take %s/%s", manifest.project, manifest.project); status = create_and_enter_directory(manifest.project); - on_error("failed to create or enter directory", status); + if (status) { + printfn("failed to create or enter directory: %s", + strerror(status)); + return status; + } ++depth; if (manifest.flag.GNU) { - debug("GNU flag source branch"); - create_file_with_content("main.c", main_c_gnu_template, manifest.project, manifest.name); goto atexit_clean; } - debug("default sourcebranch"); create_file_with_content("main.c", main_c_template, manifest.project, manifest.name); @@ -257,129 +356,76 @@ atexit_clean: return 0; } -static int parse_arguments(manifest_t *conf, int argc, char **argv) -{ - static struct option long_options[] = { - { "GNU", no_argument, 0, 'g' }, - { "use-cpp", no_argument, 0, 'c' }, - { "git", no_argument, 0, 'i' }, - { "licence", required_argument, 0, 'l' }, - { "lib", required_argument, 0, 'L' }, - { "help", no_argument, 0, 'h' }, - { 0, 0, 0, 0 } - }; - - int opt; - while ((opt = getopt_long(argc, argv, "gcil:L:h", long_options, - NULL)) != -1) { - switch (opt) { - case 'g': - conf->flag.GNU = true; - break; - case 'c': - conf->flag.use_cpp = true; - break; - case 'i': - conf->flag.git = true; - break; - case 'l': - if (strcmp(optarg, "bsd") == 0) - conf->licence = BSD3; - else if (strcmp(optarg, "gpl") == 0) - conf->licence = GPLv3; - else if (strcmp(optarg, "mit") == 0) - conf->licence = MIT; - break; - case 'L': - ADD_LIBRARY(conf->libraries, TOLibrary(optarg)); - break; - case 'h': - usage(0); - exit(0); - default: - usage(1); - exit(1); - } - } - - /* now handle positional args */ - if (optind < argc) - conf->project = argv[optind++]; - if (optind < argc) - conf->name = argv[optind++]; - return 0; -} - static int create_project(manifest_t manifest) { int status; - debugc("sanitize"); status = sanitize(&manifest); - on_error("failed to sanitize format", status); - done; + if (status) { + printfn("failed to sanitize format: %s", strerror(status)); + return status; + } - debugc("take %s", manifest.project); status = create_and_enter_directory(manifest.project); depth = 0; - on_error("failed to create or enter directory", status); - done; + if (status) { + printfn("failed to create or enter directory: %s", + strerror(status)); + return status; + } - debugc("create makefile"); status = create_makefile(manifest); - on_error("failed to create Makefile", status); - done; + if (status) { + printfn("failed to create Makefile: %s", strerror(status)); + return status; + } + + status = create_configure(manifest); + if (status) { + printfn("failed to create configure: %s", strerror(status)); + return status; + } - debug("setup git"); status = setup_git(manifest); if (status) { printfn("warning: git initialization failed: %s", strerror(status)); } - debug("create .clang-format"); status = maybe_create_clang_format(manifest); if (status) { printfn("warning: clang-format setup failed: %s", strerror(status)); } - debugc("generate source code"); + // TODO(vx-clutch): make this smarter--or not ( macro ). + char *licence = malloc(sizeof(char) * 1024); + if (!licence) { + printfn("failed to create memory for licence line: %s", + strerror(status)); + return status; + } + status = create_licence(manifest, &licence); + if (status) { + printfn("failed to get libraries: %s", strerror(status)); + return status; + } + + // TODO(vx-clutch): Take in licence line and put it into standard.c status = generate_source_code(manifest); - on_error("failed to generate source code", status); - done; + if (status) { + printfn("failed to generate source code: %s", strerror(status)); + return status; + } + free(licence); - debugc("get libraries"); status = create_libraries(manifest); - on_error("failed to get libraries", status); - done; - - return 0; -} - -int program_exists(const char *prog) -{ - char *path = getenv("PATH"); - if (!path) - return 1; - - char *copy = strdup(path); - if (!copy) - return 1; - - char *dir = strtok(copy, ":"); - while (dir) { - char buf[4096]; - snprintf(buf, sizeof(buf), "%s/%s", dir, prog); - if (access(buf, X_OK) == 0) { - free(copy); - return 0; - } - dir = strtok(NULL, ":"); + if (status) { + printfn("failed to get libraries: %s", strerror(status)); + return status; } - free(copy); - return 1; + return 0; } int main(int argc, char **argv) @@ -388,23 +434,19 @@ int main(int argc, char **argv) manifest_t manifest = { 0 }; status = parse_standard_options(usage, argc, argv); - if (status && status != HELP_REQUESTED) { - printfn("error: %s", strerror(status)); - return status; + if (status != 0 && status != HELP_REQUESTED) { + fprintf(stderr, "error: %s\n", strerror(status)); + return EXIT_FAILURE; } status = program_exists("git"); - on_error("git bin not present", status); + if (status != 0) { + fprintf(stderr, "git binary not present\n"); + return EXIT_FAILURE; + } parse_arguments(&manifest, argc, argv); status = create_project(manifest); -#ifdef DEBUG - if (!status) - debug("project made successfully"); - else - debug("something when wrong"); -#endif - - return status; + return status == 0 ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/yait/format.h b/yait/manifest.h