diff --git a/include/yait.h b/include/yait.h index 9622745..c195301 100644 --- a/include/yait.h +++ b/include/yait.h @@ -9,18 +9,16 @@ #ifndef YAIT_H #define YAIT_H -typedef enum { MIT, GPL, BSD, UNL, _LICENCE_COUNT_ } licence_t; +typedef enum { MIT, GPL, BSD, UNL, LCOUNT } licence_t; +typedef enum { MAKE, CMAKE, AUTOTOOLS, BARE, BCOUNT } built_t; typedef struct { licence_t licence; + built_t build; bool lib; bool git; - bool autotools; - bool cmake; - bool make; - bool bare; bool flat; bool open_editor; diff --git a/src/create_project.c b/src/create_project.c index cc84da9..c6a2b38 100644 --- a/src/create_project.c +++ b/src/create_project.c @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -29,7 +30,7 @@ int create_project(manifest_t manifest) if (status) return 1; - if (manifest.bare) { + if (manifest.build == BARE) { cfprintf("main.c", ""); cfprintf( "Makefile", @@ -48,7 +49,8 @@ int create_project(manifest_t manifest) "\treturn 0;\n" "}\n"); char *upr_name = tostrupr(manifest.project); - if (manifest.make) { + switch (manifest.build) { + case MAKE: cfprintf( "Makefile", "PREFIX = /usr/bin\n" @@ -63,6 +65,52 @@ int create_project(manifest_t manifest) "ifeq ($(wildcard config.mak),)\n", upr_name, upr_name, upr_name, upr_name, manifest.project); + break; + case CMAKE: + cfprintf("CMakeLists.txt", + "cmake_minimum_required(VERSION 3.16)\n" + "\n" + "project(%s\n" + " VERSION 0.1.0\n" + " LANGUAGES C)\n" + "\n" + "set(CMAKE_C_STANDARD 23)\n" + "set(CMAKE_C_STANDARD_REQUIRED ON)\n" + "set(CMAKE_C_EXTENSIONS OFF)\n" + "\n" + "include(GNUInstallDirs)\n" + "\n" + "add_executable(%s\n" + " src/main.c\n" + ")\n" + "\n" + "install(TARGETS %s\n" + " RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}\n" + ")\n", + manifest.project, manifest.project, manifest.project); + break; + case AUTOTOOLS: + cfprintf("configure.ac", + "AC_PREREQ([2.69])\n" + "AC_INIT([%s], [0.1], [bug-report@exmaple.com])\n" + "AM_INIT_AUTOMAKE([foreign -Wall])\n" + "AC_CONFIG_SRCDIR([src/main.c])\n" + "AC_CONFIG_HEADERS([config.h])" + "AC_PROG_CC\n" + "AC_CONFIG_FILES([Makefile src/Makefile])\n" + "AC_OUTPUT\n", + manifest.project); + cfprintf("Makefile.am", "SUBDIRS = src\n"); + cfprintf("src/Makefile.am", + "bin_PROGRAMS = %s\n" + "%s_SOURCES = main.c\n", + manifest.project, manifest.project); + cfprintf("bootstrap", + "#!/bin/sh\n" + "set -e\n" + "\n" + "autoreconf --install --verbose --force\n"); + break; } bare_skip: @@ -90,6 +138,21 @@ bare_skip: fprintf(stderr, ", done.\n"); } + if (manifest.build == AUTOTOOLS) { + fprintf(stderr, "Changing files permissions 1"); + struct stat st; + if (stat("bootstrap", &st) == -1) { + perror("stat failed"); + return 1; + } + mode_t new_mode = st.st_mode | S_IXUSR; + if (chmod("bootstrap", new_mode) == -1) { + perror("chmod failed"); + return 1; + } + fprintf(stderr, ", done.\n"); + } + if (manifest.open_editor) { snprintf(buffer, BUFSIZ, "nvim %s", main_source); system(buffer); diff --git a/src/file.c b/src/file.c index 5358de1..bc96864 100644 --- a/src/file.c +++ b/src/file.c @@ -49,7 +49,7 @@ int mkdir_p(const char *path) int cfprintf(const char *path, const char *format, ...) { - int lines = atoi(getenv("LINES")); + // int lines = atoi(getenv("LINES")); char *dirpath; const char *slash = strrchr(path, '/'); if (slash) { diff --git a/src/main.c b/src/main.c index ec44739..f586f26 100644 --- a/src/main.c +++ b/src/main.c @@ -122,22 +122,16 @@ static int parse_arguments(manifest_t *conf, int argc, char **argv) conf->lib = true; break; case 'a': - conf->autotools = true; - conf->cmake = false; - conf->make = false; + conf->build = AUTOTOOLS; break; case 'c': - conf->cmake = true; - conf->autotools = false; - conf->make = false; + conf->build = CMAKE; break; case 'm': - conf->make = true; - conf->autotools = false; - conf->cmake = false; + conf->build = MAKE; break; case 'B': - conf->bare = true; + conf->build = BARE; break; case 'f': conf->flat = true; @@ -214,10 +208,7 @@ int main(int argc, char **argv) .licence = UNL, .git = true, - .autotools = false, - .cmake = false, - .make = true, - .bare = false, + .build = MAKE, .flat = false, .open_editor = false, .lib = false, diff --git a/tools/tostr b/tools/tostr new file mode 100755 index 0000000..7facf32 --- /dev/null +++ b/tools/tostr @@ -0,0 +1,44 @@ +#!/bin/sh + +awk ' +BEGIN { + printf "\"" +} +{ + for (i = 1; i <= length($0); i++) { + c = substr($0, i, 1) + if (c == "\\") { + printf "\\\\" + } else if (c == "\"") { + printf "\\\"" + } else if (c == "\t") { + printf "\\t" + } else if (c == "\r") { + printf "\\r" + } else if (c == "\n") { + printf "\\n" + } else if (c == "\f") { + printf "\\f" + } else if (c == "\b") { + printf "\\b" + } else if (c == "\a") { + printf "\\a" + } else if (c == "\v") { + printf "\\v" + } else if (c ~ /[[:cntrl:]]/) { + printf "\\x%02x", ord(c) + } else { + printf "%s", c + } + } + printf "\\n" +} +END { + printf "\"\n" +} +function ord(str, l, r) { + l = sprintf("%c", 255) + r = sprintf("%c", 0) + return index(l str r, str) - 1 +} +'