This commit is contained in:
2025-09-28 22:06:39 -04:00
parent 90fadf3980
commit d847c2fcb6
20 changed files with 636 additions and 450 deletions

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "gcklib"]
path = gcklib
url = https://github.com/gck-org/gcklib

View File

@@ -1,11 +1,11 @@
PREFIX = /usr/bin/ PREFIX = /usr/bin/
YAIT_SRCS := $(wildcard src/*.c) YAIT_SRCS := $(wildcard src/*.c) $(wildcard lib/*.c)
YAIT_OBJS := $(patsubst src/%.c,build/obj/%.o,$(YAIT_SRCS)) YAIT_OBJS := $(patsubst src/%.c,build/obj/%.o,$(YAIT_SRCS))
YAIT := bin/yait YAIT := bin/yait
ALLOWED_DIRS = doc include man src tools ALLOWED_DIRS = doc include man src tools lib
DISTDIRS := $(sort $(shell find . -maxdepth 1 -type d -not -name '.' -printf '%f\n')) DISTDIRS := $(sort $(shell find . -maxdepth 1 -type d -not -name '.' -printf '%f\n'))
-include config.mak -include config.mak
@@ -23,10 +23,10 @@ build:
mkdir -p build/obj mkdir -p build/obj
build/obj/%.o: src/%.c config.mak build/obj/%.o: src/%.c config.mak
$(CC) $(CFLAGS) -DCOMMIT=$(shell git rev-list --count --all) -Iinclude -c $< -o $@ $(CC) $(CFLAGS) -DCOMMIT=$(shell git rev-list --count --all) -Iinclude -Ilib -c $< -o $@
$(YAIT): $(YAIT_OBJS) $(YAIT): $(YAIT_OBJS)
$(CC) $(CFLAGS) -Iinclude -DCOMMIT=$(shell git rev-list --count --all) $^ -o $@ $(CC) $(CFLAGS) -Iinclude -Ilib -DCOMMIT=$(shell git rev-list --count --all) $^ -o $@
endif endif

2
TODO
View File

@@ -3,7 +3,7 @@ GCK yait - TODO
Todo: Todo:
* Refactor to more GNU style main * Refactor to more GNU style main
* Implement the extra option * Factor out str_dup
* Build tarball with Makefile * Build tarball with Makefile
* Polish for 1.0 * Polish for 1.0

1
gcklib Submodule

Submodule gcklib added at 432788eb5f

View File

@@ -9,25 +9,10 @@
#ifndef YAIT_H #ifndef YAIT_H
#define YAIT_H #define YAIT_H
typedef enum { MIT, GPL, BSD, UNL, LCOUNT } licence_t; typedef enum { MIT, GPL, BSD } licence_t;
typedef enum { MAKE, CMAKE, AUTOTOOLS, BARE, BCOUNT } built_t;
typedef struct { typedef struct {
licence_t licence; licence_t licence;
built_t build;
bool lib;
bool git;
bool flat;
bool open_editor;
struct {
bool build_nob;
bool tools_format;
bool tools_Cleanup;
} extra;
char *project; char *project;
char *author; char *author;
char *editor; char *editor;

151
lib/err.c Normal file
View File

@@ -0,0 +1,151 @@
/* Copyright (C) GCK
*
* This file is part of gcklib
*
* This project and file is licensed under the BSD-3-Clause license.
* <https://opensource.org/licenses/BSD-3-Clause>
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "err.h"
#define RESET "\e[0m"
#define ERROR "\e[1;91m"
#define WARN "\e[1;95m"
#define NOTE "\e[1;94m"
#define HINT "\e[38;5;166m"
static bool __check(void)
{
static int done = 0;
static bool cached = false;
if (!done) {
const char *term = getenv("TERM");
cached = isatty(STDOUT_FILENO) && term != NULL &&
(strstr(term, "color") != NULL ||
strstr(term, "ansi") != NULL ||
strstr(term, "xterm") != NULL);
done = 1;
}
return cached;
}
void errorf(const char *format, ...)
{
va_list args;
va_start(args, format);
if (__check()) {
fprintf(stderr, "%serror%s: ", ERROR, RESET);
} else {
fputs("error: ", stderr);
}
vfprintf(stderr, format, args);
fputc('\n', stderr);
va_end(args);
}
void fatalf(const char *format, ...)
{
va_list args;
va_start(args, format);
if (__check()) {
fprintf(stderr, "%sfatal%s: ", ERROR, RESET);
} else {
fputs("fatal: ", stderr);
}
vfprintf(stderr, format, args);
fputc('\n', stderr);
va_end(args);
exit(EXIT_FAILURE);
}
void warnf(const char *format, ...)
{
va_list args;
va_start(args, format);
if (__check()) {
fprintf(stderr, "%swarning%s: ", WARN, RESET);
} else {
fputs("warning: ", stderr);
}
vfprintf(stderr, format, args);
fputc('\n', stderr);
va_end(args);
}
void notef(const char *format, ...)
{
va_list args;
va_start(args, format);
if (__check()) {
fprintf(stderr, "%snote%s: ", NOTE, RESET);
} else {
fputs("note: ", stderr);
}
vfprintf(stderr, format, args);
fputc('\n', stderr);
va_end(args);
}
void hintf(const char *format, ...)
{
va_list args;
va_start(args, format);
if (__check()) {
fprintf(stderr, "%shint: ", HINT);
} else {
fputs("hint: ", stderr);
}
vfprintf(stderr, format, args);
if (__check()) {
fprintf(stderr, "%s\n", RESET);
} else {
fputc('\n', stderr);
}
va_end(args);
}
void errorfa(int code)
{
errorf(strerror(code));
}
void fatalfa(int code)
{
fatalf(strerror(code));
}
void notefa(int code)
{
notef(strerror(code));
}
void warnfa(int code)
{
warnf(strerror(code));
}
void hintfa(int code)
{
hintf(strerror(code));
}

24
lib/err.h Normal file
View File

@@ -0,0 +1,24 @@
/* Copyright (C) GCK
*
* This file is part of gcklib
*
* This project and file is licensed under the BSD-3-Clause licence.
* <https://opensource.org/licence/bsd-3-clause>
*/
#ifndef ERR_H
#define ERR_H
void errorf(const char *format, ...);
void fatalf(const char *format, ...);
void notef(const char *format, ...);
void warnf(const char *format, ...);
void hintf(const char *format, ...);
void errorfa(int code);
void fatalfa(int code);
void notefa(int code);
void warnfa(int code);
void hintfa(int code);
#endif

52
lib/proginfo.c Normal file
View File

@@ -0,0 +1,52 @@
/* Copyright (C) GCK
*
* This file is part of gcklib
*
* This project and file is licensed under the BSD-3-Clause licence.
* <https://opensource.org/licence/bsd-3-clause>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../config.h"
#include "proginfo.h"
const char *prog_name = "";
void set_prog_name(const char *name)
{
prog_name = prog_name ? name : "";
}
void emit_try_help()
{
printf("Try '%s --help' for more information\n", prog_name);
}
void emit_version()
{
printf("\
%s %s %d\n\
Copyright (C) %d GCK.\n\
This is free software: you are free to change and redistribute it.\n\
There is NO WARRNTY, to the extent permitted by law.\n\
",
prog_name, VERSION, COMMIT, YEAR);
}
int parse_standard_options(int argc, char **argv, void (*usage)(int),
void (*version)())
{
for (int i = 0; i < argc; ++i) {
if (!strcmp(argv[i], "--help")) {
usage(0);
exit(EXIT_SUCCESS);
} else if (!strcmp(argv[i], "--version")) {
emit_version();
exit(EXIT_SUCCESS);
}
}
return 0;
}

22
lib/proginfo.h Normal file
View File

@@ -0,0 +1,22 @@
/* Copyright (C) GCK
*
* This file is part of gcklib
*
* This project and file is licensed under the BSD-3-Clause licence.
* <https://opensource.org/licence/bsd-3-clause>
*/
#ifndef proginfo_H
#define proginfo_H
extern const char *prog_name;
void set_prog_name(const char *name);
void emit_try_help();
void emit_version();
int parse_standard_options(int argc, char **argv, void (*usage)(int),
void (*version)());
#endif

12
lib/str_dup.c Normal file
View File

@@ -0,0 +1,12 @@
#include <stdlib.h>
#include <string.h>
#include "xmem.h"
#include "str_dup.h"
char *str_dup(char *s)
{
char *new = xmalloc(strlen(s) + 1);
strcpy(new, s);
return new;
}

6
lib/str_dup.h Normal file
View File

@@ -0,0 +1,6 @@
#ifndef STR_DUP_H
#define STR_DUP_H
char *str_dup(char *s);
#endif

36
lib/xmem.c Normal file
View File

@@ -0,0 +1,36 @@
/* Copyright (C) GCK
*
* This file is part of gcklib
*
* This project and file is licensed under the BSD-3-Clause licence.
* <https://opensource.org/licence/bsd-3-clause>
*/
#include <stdio.h>
#include <stdlib.h>
#include "err.h"
#include "xmem.h"
void *ensure_nonnull(void *ptr)
{
if (ptr == NULL)
fatalf("memory exhausted");
return ptr;
}
void *xmalloc(size_t size)
{
return ensure_nonnull(malloc(size));
}
void *xrealloc(void *ptr, size_t size)
{
return ensure_nonnull(realloc(ptr, size));
}
void *xcalloc(size_t nmemb, size_t size)
{
return ensure_nonnull(calloc(nmemb, size));
}

18
lib/xmem.h Normal file
View File

@@ -0,0 +1,18 @@
/* Copyright (C) GCK
*
* This file is part of gcklib
*
* This project and file is licensed under the BSD-3-Clause licence.
* <https://opensource.org/licence/bsd-3-clause>
*/
#ifndef xmem_H
#define xmem_H
#include <stdlib.h>
void *xmalloc(size_t size);
void *xrealloc(void *ptr, size_t size);
void *xcalloc(size_t nmemb, size_t size);
#endif

View File

@@ -17,166 +17,166 @@
#include "licences/licences.h" #include "licences/licences.h"
#include "util.h" #include "util.h"
int create_project(manifest_t manifest) // int create_project(manifest_t manifest)
{ // {
int status; // int status;
char buffer[BUFSIZ], *main_source; // char buffer[BUFSIZ], *main_source;
//
status = mkdir_p(manifest.project); // status = mkdir_p(manifest.project);
if (status) // if (status)
return 1; // return 1;
//
status = chdir(manifest.project); // status = chdir(manifest.project);
if (status) // if (status)
return 1; // return 1;
//
cfprintf( // cfprintf(
"README", // "README",
"%s ( short description )\n\nThis cool project actions adverbly.\n", // "%s ( short description )\n\nThis cool project actions adverbly.\n",
manifest.project); // manifest.project);
//
if (manifest.build != BARE) { // if (manifest.build != BARE) {
main_source = manifest.flat ? "main.c" : "src/main.c"; // main_source = manifest.flat ? "main.c" : "src/main.c";
cfprintf(main_source, "#include <stdio.h>\n" // cfprintf(main_source, "#include <stdio.h>\n"
"\n" // "\n"
"int main()\n" // "int main()\n"
"{\n" // "{\n"
"\tputs(\"Hello, World!\");\n" // "\tputs(\"Hello, World!\");\n"
"\treturn 0;\n" // "\treturn 0;\n"
"}\n"); // "}\n");
} // }
char *upr_name = tostrupr(manifest.project); // char *upr_name = tostrupr(manifest.project);
switch (manifest.build) { // switch (manifest.build) {
case MAKE: // case MAKE:
cfprintf( // cfprintf(
"Makefile", // "Makefile",
"PREFIX = /usr/bin\n" // "PREFIX = /usr/bin\n"
"\n" // "\n"
"%s_SRCS := $(wildcard src/*.c)\n" // "%s_SRCS := $(wildcard src/*.c)\n"
"%s_OBJS := $(patsubst src/%.c,build/obj/%.o,$(%s_SRCS))" // "%s_OBJS := $(patsubst src/%.c,build/obj/%.o,$(%s_SRCS))"
"\n" // "\n"
"%s := bin/%s" // "%s := bin/%s"
"\n" // "\n"
"-include config.mak\n" // "-include config.mak\n"
"\n" // "\n"
"ifeq ($(wildcard config.mak),)\n", // "ifeq ($(wildcard config.mak),)\n",
upr_name, upr_name, upr_name, upr_name, // upr_name, upr_name, upr_name, upr_name,
manifest.project); // manifest.project);
break; // break;
case CMAKE: // case CMAKE:
cfprintf("CMakeLists.txt", // cfprintf("CMakeLists.txt",
"cmake_minimum_required(VERSION 3.16)\n" // "cmake_minimum_required(VERSION 3.16)\n"
"\n" // "\n"
"project(%s\n" // "project(%s\n"
" VERSION 0.1.0\n" // " VERSION 0.1.0\n"
" LANGUAGES C)\n" // " LANGUAGES C)\n"
"\n" // "\n"
"set(CMAKE_C_STANDARD 23)\n" // "set(CMAKE_C_STANDARD 23)\n"
"set(CMAKE_C_STANDARD_REQUIRED ON)\n" // "set(CMAKE_C_STANDARD_REQUIRED ON)\n"
"set(CMAKE_C_EXTENSIONS OFF)\n" // "set(CMAKE_C_EXTENSIONS OFF)\n"
"\n" // "\n"
"include(GNUInstallDirs)\n" // "include(GNUInstallDirs)\n"
"\n" // "\n"
"add_executable(%s\n" // "add_executable(%s\n"
" src/main.c\n" // " src/main.c\n"
")\n" // ")\n"
"\n" // "\n"
"install(TARGETS %s\n" // "install(TARGETS %s\n"
" RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}\n" // " RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}\n"
")\n", // ")\n",
manifest.project, manifest.project, manifest.project); // manifest.project, manifest.project, manifest.project);
break; // break;
case AUTOTOOLS: // case AUTOTOOLS:
cfprintf("configure.ac", // cfprintf("configure.ac",
"AC_PREREQ([2.69])\n" // "AC_PREREQ([2.69])\n"
"AC_INIT([%s], [0.1], [bug-report@exmaple.com])\n" // "AC_INIT([%s], [0.1], [bug-report@exmaple.com])\n"
"AM_INIT_AUTOMAKE([foreign -Wall])\n" // "AM_INIT_AUTOMAKE([foreign -Wall])\n"
"AC_CONFIG_SRCDIR([src/main.c])\n" // "AC_CONFIG_SRCDIR([src/main.c])\n"
"AC_CONFIG_HEADERS([config.h])" // "AC_CONFIG_HEADERS([config.h])"
"AC_PROG_CC\n" // "AC_PROG_CC\n"
"AC_CONFIG_FILES([Makefile src/Makefile])\n" // "AC_CONFIG_FILES([Makefile src/Makefile])\n"
"AC_OUTPUT\n", // "AC_OUTPUT\n",
manifest.project); // manifest.project);
cfprintf("Makefile.am", "SUBDIRS = src\n"); // cfprintf("Makefile.am", "SUBDIRS = src\n");
cfprintf("src/Makefile.am", // cfprintf("src/Makefile.am",
"bin_PROGRAMS = %s\n" // "bin_PROGRAMS = %s\n"
"%s_SOURCES = main.c\n", // "%s_SOURCES = main.c\n",
manifest.project, manifest.project); // manifest.project, manifest.project);
cfprintf("bootstrap", // cfprintf("bootstrap",
"#!/bin/sh\n" // "#!/bin/sh\n"
"set -e\n" // "set -e\n"
"\n" // "\n"
"autoreconf --install --verbose --force\n"); // "autoreconf --install --verbose --force\n");
break; // break;
case BARE: // case BARE:
snprintf(buffer, BUFSIZ, "%s.c", manifest.project); // snprintf(buffer, BUFSIZ, "%s.c", manifest.project);
cfprintf(buffer, ""); // cfprintf(buffer, "");
main_source = str_dup(buffer); // main_source = str_dup(buffer);
cfprintf("Makefile", // cfprintf("Makefile",
".POSIX:\n" // ".POSIX:\n"
"CC ::= gcc\n" // "CC ::= gcc\n"
"CFLAGS ::= -std=c23 -Wall -Wextra -Wpedantic\n" // "CFLAGS ::= -std=c23 -Wall -Wextra -Wpedantic\n"
"\n" // "\n"
"all: %s\n" // "all: %s\n"
"\n" // "\n"
"clean:\n" // "clean:\n"
"\t$(RM) %s", // "\t$(RM) %s",
manifest.project, manifest.project); // manifest.project, manifest.project);
break; // break;
case BCOUNT: // case BCOUNT:
default: // default:
abort(); // abort();
} // }
//
flast = true; // flast = true;
switch (manifest.licence) { // switch (manifest.licence) {
case MIT: // case MIT:
cfprintf("COPYING", "%s", MIT_txt); // cfprintf("COPYING", "%s", MIT_txt);
break; // break;
case GPL: // case GPL:
cfprintf("COPYING", "%s", GPL_3_0_txt); // cfprintf("COPYING", "%s", GPL_3_0_txt);
break; // break;
case BSD: // case BSD:
cfprintf("COPYING", "%s", BSD_3_Clause_txt); // cfprintf("COPYING", "%s", BSD_3_Clause_txt);
break; // break;
case UNL: // case UNL:
cfprintf("COPYING", "%s", UNLICENSE_txt); // cfprintf("COPYING", "%s", UNLICENSE_txt);
break; // break;
case LCOUNT: // case LCOUNT:
default: // default:
abort(); // abort();
} // }
//
if (!manifest.git) { // if (!manifest.git) {
fprintf(stderr, "Initializing git reposity"); // fprintf(stderr, "Initializing git reposity");
status = system("git init --quiet"); // status = system("git init --quiet");
if (status) // if (status)
fprintf(stderr, ", failed.\n"); // fprintf(stderr, ", failed.\n");
else // else
fprintf(stderr, ", done.\n"); // fprintf(stderr, ", done.\n");
} // }
//
if (manifest.build == AUTOTOOLS) { // if (manifest.build == AUTOTOOLS) {
fprintf(stderr, "Changing files permissions 1"); // fprintf(stderr, "Changing files permissions 1");
struct stat st; // struct stat st;
if (stat("bootstrap", &st) == -1) { // if (stat("bootstrap", &st) == -1) {
perror("stat failed"); // perror("stat failed");
return 1; // return 1;
} // }
mode_t new_mode = st.st_mode | S_IXUSR; // mode_t new_mode = st.st_mode | S_IXUSR;
if (chmod("bootstrap", new_mode) == -1) { // if (chmod("bootstrap", new_mode) == -1) {
perror("chmod failed"); // perror("chmod failed");
return 1; // return 1;
} // }
fprintf(stderr, ", done.\n"); // fprintf(stderr, ", done.\n");
} // }
//
if (manifest.open_editor) { // if (manifest.open_editor) {
snprintf(buffer, BUFSIZ, "$EDITOR %s", main_source); // snprintf(buffer, BUFSIZ, "$EDITOR %s", main_source);
status = system(buffer); // status = system(buffer);
if (status) // if (status)
fprintf(stderr, "Could not open editor"); // fprintf(stderr, "Could not open editor");
} // }
//
return 0; // return 0;
} // }

View File

@@ -8,23 +8,80 @@
// Usage: yait [OPTION]... <PROJECT> // Usage: yait [OPTION]... <PROJECT>
#define _POSIX_C_SOURCE 200809L // popen extention
#include <getopt.h> #include <getopt.h>
#include <pwd.h> #include <pwd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <limits.h> #include <limits.h>
#include <string.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <stdarg.h> #include <stdarg.h>
#include <string.h>
#include "../lib/proginfo.h"
#include "../lib/err.h"
#include "../include/yait.h" #include "../include/yait.h"
#include "standard.h"
#include "util.h"
#include "../config.h" #include "../config.h"
#include "name.h"
#define print_option(option, description) \ static struct option longopts[] = { { "author", required_argument, 0, 'a' },
printf(" %-20s %-20s\n", option, description) { "licence", required_argument, 0, 'l' },
{ 0, 0, 0, 0 } };
static int exit_status;
static void usage(int status);
int main(int argc, char **argv)
{
int optc;
int lose = 0;
set_prog_name(argv[0]);
exit_status = EXIT_SUCCESS;
manifest_t manifest = {
.author = get_name(),
.editor = NULL,
.licence = BSD,
.project = "Project",
};
parse_standard_options(argc, argv, usage, emit_version);
while ((optc = getopt_long(argc, argv, "a:l:E", longopts, NULL)) != -1)
switch (optc) {
case 'l':
if (!strcmp(optarg, "list")) {
printf("BSD\nGPL\nMIT\n");
exit(EXIT_SUCCESS);
}
if (!strcmp(optarg, "GPL"))
manifest.licence = GPL;
else if (!strcmp(optarg, "MIT"))
manifest.licence = MIT;
else {
printf("BSD\nGPL\nMIT\n");
exit(EXIT_FAILURE);
}
break;
case 'E':
manifest.editor = getenv("EDITOR");
break;
default:
lose = 1;
}
char *cwd = getcwd(NULL, 0);
if (!cwd) {
fatalfa(errno);
}
fprintf(stderr, "Created %s at\n %s\n", manifest.project, cwd);
free(cwd);
return exit_status;
}
static void usage(int status) static void usage(int status)
{ {
@@ -44,222 +101,8 @@ Generates an optionated C project.\n",
stdout); stdout);
puts(""); puts("");
fputs("\ fputs("\
--git Initialize git repository (default)\n\ -E Open $EDITOR after project creation (default false)\n\
--no-git Do not initialize git repository\n\ --author=NAME Set the program author (default git username)\n\
--lib Make this a library\n\ --licence=LICENCE Set the program licence (default BSD)\n",
-l <licence> Set licence. This list can be found by passing 'list'\n\
-E Open $EDITOR after project creation\n\
--autotools Use the autotools build system\n\
--cmake Use the cmake build system\n\
--make Use the GNU make build system (default)\n\
--bare Minimal C project structure\n\
--flat All files in project root.\n\
--extras=<arg1>,<arg2> Extra build options, Pass list to list out options.\n",
stdout); stdout);
} }
void print_lines(const char *first, ...)
{
va_list args;
const char *s;
va_start(args, first);
for (s = first; s != NULL; s = va_arg(args, const char *))
printf("%s\n", s);
va_end(args);
}
static inline int parse_extras_token(manifest_t *conf, const char *s)
{
if (!strcmp(s, "list")) {
print_lines("nob", "Cleanup", "format", NULL);
exit(EXIT_SUCCESS);
}
if (!strcmp(s, "nob"))
return conf->extra.build_nob = true, 0;
if (!strcmp(s, "Cleanup"))
return conf->extra.tools_Cleanup = true, 0;
if (!strcmp(s, "format"))
return conf->extra.tools_format = true, 0;
fprintf(stderr, "Option '%s' is not valid. See %s --extras=list\n", s,
PROGRAM);
return 1;
}
static int parse_arguments(manifest_t *conf, int argc, char **argv)
{
int opt, option_index;
// clang-format off
static struct option long_opts[] = {
{ "git", no_argument, 0, 'g' },
{ "no-git", no_argument, 0, 'G' },
{ "lib", no_argument, 0, 'L' },
{ "autotools", no_argument, 0, 'a' },
{ "cmake", no_argument, 0, 'c' },
{ "make", no_argument, 0, 'm' },
{ "bare", no_argument, 0, 'B' },
{ "flat", no_argument, 0, 'f' },
{ "author", required_argument, 0, 'A' },
{ "extras", required_argument, 0, 0 },
{ 0, 0, 0, 0 } };
// clang-format on
while ((opt = getopt_long(argc, argv, "gGLbacmBfAl:E", long_opts,
&option_index)) != -1) {
if (opt == 0 &&
strcmp(long_opts[option_index].name, "extras") == 0) {
int err;
char *arg = optarg;
char *token = strtok(arg, ",");
while (token) {
err = parse_extras_token(conf, token);
if (err)
exit(err);
token = strtok(NULL, ",");
}
}
switch (opt) {
case 'g':
conf->git = true;
break;
case 'G':
conf->git = false;
break;
case 'L':
conf->lib = true;
break;
case 'a':
conf->build = AUTOTOOLS;
break;
case 'c':
conf->build = CMAKE;
break;
case 'm':
conf->build = MAKE;
break;
case 'B':
conf->build = BARE;
break;
case 'f':
conf->flat = true;
break;
case 'A':
conf->author = optarg;
break;
case 'l':
if (!strcmp(optarg, "list")) {
print_lines("MIT", "BSD", "GPL", "UNL", NULL);
exit(EXIT_SUCCESS);
}
conf->licence = TOlicence(optarg);
break;
case 'E':
conf->open_editor = true;
conf->editor = getenv("EDITOR");
break;
default:
return 1;
}
}
if (optind >= argc) {
return HELP_REQUESTED;
}
conf->project = str_dup(argv[optind]);
return 0;
}
int get_name(char **output)
{
FILE *fp;
char buf[256];
char *res = NULL;
struct passwd *pw;
fp = popen("git config --get user.name", "r");
if (fp) {
if (fgets(buf, sizeof buf, fp)) {
size_t len = strlen(buf);
if (len > 0 && buf[len - 1] == '\n')
buf[len - 1] = '\0';
res = strdup(buf);
}
pclose(fp);
}
if (!res) {
pw = getpwuid(getuid());
if (!pw || !pw->pw_name)
return -1;
res = strdup(pw->pw_name);
}
if (!res)
return -1;
*output = res;
return 0;
}
int main(int argc, char **argv)
{
int status;
manifest_t manifest = {
.project = "Project",
.author = "author",
.editor = "vim",
.licence = UNL,
.git = true,
.build = MAKE,
.flat = false,
.open_editor = false,
.lib = false,
.extra.build_nob = false,
.extra.tools_format = false,
.extra.tools_Cleanup = false,
};
status = parse_standard_options(usage, argc, argv);
if (status != 0 && status != HELP_REQUESTED)
return fprintf(stderr, "error: %s\n", strerror(status)),
EXIT_FAILURE;
status = parse_arguments(&manifest, argc, argv);
if (status == HELP_REQUESTED) {
usage(0);
}
if (status) {
return EXIT_FAILURE;
}
get_name(&manifest.author);
status = create_project(manifest);
if (status) {
fprintf(stderr, "%s\n", strerror(status));
return EXIT_FAILURE;
}
char *cwd;
cwd = getcwd(NULL, 0);
if (cwd == NULL) {
perror("getcwd");
exit(EXIT_FAILURE);
}
fprintf(stderr, "Created %s at\n %s\n", manifest.project, cwd);
free(cwd);
return EXIT_SUCCESS;
}

61
src/name.c Normal file
View File

@@ -0,0 +1,61 @@
/* Copyright (C) GCK
*
* This file is part of yait
*
* This project and file is licenced under the BSD-3-Clause licence.
* <https://opensource.org/licence/bsd-3-clause>
*/
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <pwd.h>
#include "../lib/str_dup.h"
#include "name.h"
char *get_name()
{
int fds[2];
if (pipe(fds) == -1)
goto sysuser;
pid_t pid = fork();
if (pid == -1) {
close(fds[0]);
close(fds[1]);
goto sysuser;
}
if (pid == 0) {
dup2(fds[1], STDOUT_FILENO);
close(fds[0]);
close(fds[1]);
execlp("git", "git", "config", "--get", "user.name",
(char *)NULL);
_exit(127);
}
close(fds[1]);
char buf[256];
ssize_t n = read(fds[0], buf, sizeof buf - 1);
close(fds[0]);
int status;
waitpid(pid, &status, 0);
if (n > 0 && WIFEXITED(status) && WEXITSTATUS(status) == 0) {
buf[n] = 0;
buf[strcspn(buf, "\n")] = 0;
return str_dup(buf);
}
sysuser: {
char *name = getlogin();
if (name)
return str_dup(name);
struct passwd *pw = getpwuid(getuid());
if (pw && pw->pw_name)
return str_dup(pw->pw_name);
}
return "author";
}

14
src/name.h Normal file
View File

@@ -0,0 +1,14 @@
/* Copyright (C) GCK
*
* This file is part of yait
*
* This project and file is licenced under the BSD-3-Clause licence.
* <https://opensource.org/licence/bsd-3-clause>
*/
#ifndef NAME_H
#define NAME_H
char *get_name();
#endif

View File

@@ -1,34 +0,0 @@
/* Copyright (C) GCK
*
* This file is part of yait
*
* This project and file is licenced under the BSD-3-Clause licence.
* <https://opensource.org/license/bsd-3-clause>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../config.h"
#include "standard.h"
int parse_standard_options(void (*usage)(int), int argc, char **argv)
{
for (int i = 0; i < argc; ++i) {
if (strcmp(argv[i], "--help") == 0) {
usage(0);
exit(EXIT_SUCCESS);
} else if (strcmp(argv[i], "--version") == 0) {
printf("%s %s %d\nCopyright (C) %d %s.\n%s\nThis is "
"free software: "
"you are free to change and redistribute "
"it.\nThere is NO "
"WARRNTY, to the extent permitted by law.\n",
PROGRAM, VERSION, COMMIT, YEAR, AUTHORS,
LICENSE_LINE);
exit(EXIT_SUCCESS);
}
}
return HELP_REQUESTED;
}

View File

@@ -1,8 +0,0 @@
#ifndef STANDARD_H
#define STANDARD_H
#define HELP_REQUESTED 2
int parse_standard_options(void (*usage)(int), int argc, char **argv);
#endif

View File

@@ -28,7 +28,7 @@ licence_t TOlicence(char *src)
if (!strcmp(s, "BSD")) if (!strcmp(s, "BSD"))
return BSD; return BSD;
free(s); free(s);
return UNL; return BSD;
} }
char *str_dup(const char *s) char *str_dup(const char *s)