This commit is contained in:
2025-07-18 15:44:30 -04:00
parent cf26cdc4b9
commit 50e964d3a0
6 changed files with 71 additions and 65 deletions

View File

@@ -1,14 +0,0 @@
#include "e.h"
#include "print.h"
#include <stdlib.h>
error_t
unwrap (error_t err)
{
if (!err.null)
{
printfn ("error: %s", err.src);
exit (err.status);
}
return err;
}

View File

@@ -1,14 +0,0 @@
#ifndef ERROR_H
#define ERROR_H
#include <stdbool.h>
typedef struct {
bool null;
int status;
const char *src;
} error_t;
error_t unwrap(error_t);
#endif

View File

@@ -1,25 +1,17 @@
#include "file.h" #include "file.h"
#include "e.h"
#include <errno.h> #include <errno.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
error_t int
touch (char *path, char *format, ...) touch (char *path, char *format, ...)
{ {
error_t err = { 0 };
err.null = true;
FILE *fp = fopen (path, "w"); FILE *fp = fopen (path, "w");
if (!fp) if (!fp)
{ {
err.null = false; return 0;
err.status = errno;
err.src = strerror (errno);
return err;
} }
else else
@@ -31,15 +23,12 @@ touch (char *path, char *format, ...)
} }
fclose (fp); fclose (fp);
return err; return 0;
} }
error_t int
dir (char *format, ...) dir (char *format, ...)
{ {
error_t err = { 0 };
err.null = true; // success by default
va_list args; va_list args;
va_start (args, format); va_start (args, format);
@@ -50,28 +39,23 @@ dir (char *format, ...)
if (mkdir (path, 0777) < 0) if (mkdir (path, 0777) < 0)
{ {
err.null = false; return errno;
err.status = errno;
err.src = strerror (errno);
} }
return err; return 0;
} }
error_t int
take (const char *dirname) take (const char *dirname)
{ {
error_t err = dir ("%s", dirname); int err = dir ("%s", dirname);
if (!err.null) if (err)
{ {
return err; return err;
} }
if (chdir (dirname) != 0) if (chdir (dirname) != 0)
{ {
err.null = false; return errno;
err.status = errno;
err.src = strerror (errno);
return err;
} }
return err; return 0;
} }

View File

@@ -1,12 +1,11 @@
#ifndef FILE_H #ifndef FILE_H
#define FILE_H #define FILE_H
#include "e.h"
#include <unistd.h> #include <unistd.h>
error_t take(const char *dirname); int take(const char *dirname);
error_t touch(char *, char *, ...); int touch(char *, char *, ...);
error_t dir(char *, ...); int dir(char *, ...);
#endif #endif

View File

@@ -5,13 +5,13 @@
#include <string.h> #include <string.h>
int int
parse_standard_options (void (*usage) (), int argc, char **argv) parse_standard_options (void (*usage) (int), int argc, char **argv)
{ {
for (int i = 1; i < argc; ++i) for (int i = 1; i < argc; ++i)
{ {
if (strcmp (argv[i], "--help") == 0) if (strcmp (argv[i], "--help") == 0)
{ {
usage (); usage (0);
exit (0); exit (0);
} }
else if (strcmp (argv[i], "--version") == 0) else if (strcmp (argv[i], "--version") == 0)

View File

@@ -3,14 +3,42 @@
#include "../core/print.h" #include "../core/print.h"
#include "../core/standard.h" #include "../core/standard.h"
#include "format.h" #include "format.h"
#include <ctype.h>
#include <pwd.h> #include <pwd.h>
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
int create (format_t); int create (format_t);
void usage () {}; #define print_option(left, right) \
printf (" %-20s %-20s" \
"\n", \
left, right)
void
usage (int status)
{
if (status != 0)
fprintf (stderr, "Try 'yait --help' for more information.\n");
else
{
printf ("Usage: yait [OPTION]... [PROJECT] (NAME)\n");
printf ("Creates a C project with opinionated defaults.\nWhen only "
"given first argument it will detect your name\n\n");
printf ("Mandatory arguments to long options are mandatory for short "
"options too\n");
print_option ("-l, --license=NAME",
"Set license (gpl, mit, bsd) [default: gpl]");
print_option ("--use-cpp", "Uses the CPP language instead of C");
print_option ("--git", "Initialize git repository");
print_option ("--GNU",
"Adds stand GNU argument parsing to your project");
printf (" --help\tdisplay the help text and exit\n");
printf (" --version\toutput version information and exit\n");
}
}
int int
main (int argc, char **argv) main (int argc, char **argv)
@@ -45,10 +73,10 @@ main (int argc, char **argv)
int int
create (format_t fmt) create (format_t fmt)
{ {
error_t err = take (fmt.project); int err = take (fmt.project);
if (!err.null) if (err)
{ {
printfn ("failed to create or enter directory: %s", err.src); printfn ("failed to create or enter directory: %s", strerror (err));
return 1; return 1;
} }
if (fmt.git) if (fmt.git)
@@ -125,6 +153,24 @@ create (format_t fmt)
"printf \"LDFLAGS=%%s\\n\" \"$LDFLAGS\" >> config.mak\n" "printf \"LDFLAGS=%%s\\n\" \"$LDFLAGS\" >> config.mak\n"
"printf \"CC=%%s\\n\" \"$CC\" >> config.mak\n" "printf \"CC=%%s\\n\" \"$CC\" >> config.mak\n"
"printf \"done\\n\"\n"); "printf \"done\\n\"\n");
char *mkfile_name;
strcpy(mkfile_name, fmt.project);
for (char *p = mkfile_name; *p; ++p) *p = toupper(*p);
touch ("Makefile",
"prefix = /usr/bin\n\n%s_SRCS := $(wildcard yait/*.c) $(wildcard "
"core/*.c)\n%s_OBJS := $(patsubst "
"yait/%.c,c-out/obj/%.o,$(%s_SRCS))\n\n%s := "
"c-out/bin/yait\n\n-include config.mak\n\nifeq ($(wildcard "
"config.mak),)\nall:\n\t@echo \"File config.mak not found, run "
"configure\"\n\t@exit 1\nelse\n\nall: build $(%s) "
"$(%s_DOC)\n\nbuild:\n\tmkdir -p c-out/bin\n\tmkdir -p "
"c-out/obj\n\nc-out/obj/%.o: yait/%.c\n\t$(CC) $(CFLAGS) -c $< -o "
"$@\n\n$(%s): $(%s_OBJS)\n\t$(CC) $(CFLAGS) -DCOMMIT=$(shell git "
"rev-list --count --all) $^ -o $@\n\n\nendif\n\ninstall:\n\t@echo "
"\"NOT IMPL\"\n\texit 1\n\nuninstall:\n\t@echo \"NOT IMPL\"\n\texit "
"1\n\nclean:\n\trm -rf c-out\n\ndist-clean: clean\n\trm -f "
"config.mak\n\n.PHONY: all clean dist-clean install uninstall build "
"format\n", mkfile_name, mkfile_name, mkfile_name, mkfile_name, mkfile_name, mkfile_name);
if (fmt.clang_format) if (fmt.clang_format)
touch (".clang-format", "Language: Cpp\nBasedOnStyle: GNU\n"); touch (".clang-format", "Language: Cpp\nBasedOnStyle: GNU\n");
switch (fmt.licence) switch (fmt.licence)
@@ -162,5 +208,10 @@ create (format_t fmt)
default: default:
break; break;
} }
take (fmt.project);
touch ("main.c",
"#include <stdio.h>\n\nint main(void) {\n printf(\"%s: Hello "
"%s!\\n\");\nreturn 0;\n}",
fmt.project, fmt.name);
return 0; return 0;
} }