fix: correct clang-format and other files

This commit is contained in:
2025-10-28 21:31:26 -04:00
parent 6af34bfd0d
commit c5881e15bf
17 changed files with 795 additions and 651 deletions

108
.clang-format Normal file
View File

@@ -0,0 +1,108 @@
---
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: false
BreakBeforeTernaryOperators: false
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeComma
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: false
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 8
ContinuationIndentWidth: 8
Cpp11BracedListStyle: false
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: false
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '.*'
Priority: 1
IncludeIsMainRegex: '(Test)?$'
IndentCaseLabels: false
IndentGotoLabels: false
IndentPPDirectives: None
IndentWidth: 8
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 8
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 10
PenaltyBreakBeforeFirstCallParameter: 30
PenaltyBreakComment: 10
PenaltyBreakFirstLessLess: 0
PenaltyBreakString: 10
PenaltyExcessCharacter: 100
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: false
SortIncludes: false
SortUsingDeclarations: false
SpaceAfterCStyleCast: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatementsExceptForEachMacros
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: false
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp03
TabWidth: 8
UseTab: Always
...

7
.clangd Normal file
View File

@@ -0,0 +1,7 @@
CompileFlags:
Add: [-x, c, -std=c2x]
Diagnostics:
ClangTidy:
Add: [clang-diagnostic-*]
Remove: []

5
.gitignore vendored Normal file
View File

@@ -0,0 +1,5 @@
bin/
build/
*.o
config.mak
config.status

1
.usermap Normal file
View File

@@ -0,0 +1 @@
vx-clutch <https://github.com/vx-clutch>

View File

@@ -1,3 +0,0 @@
PREFIX=/usr/local
CFLAGS=-pedantic -O0 -ggdb -Wall -Wextra -Wpedantic -fno-omit-frame-pointer -DDEBUG
CC=gcc

View File

@@ -1,44 +0,0 @@
#!/bin/sh
# DO NOT MODIFY. THIS IS A AUTOGENERATED SCRIPT FROM 'configure'
#
# COMPILATION (Linux - POSIX):
# ./config.status
#
#
# LICENSE: BSD-3-Clause
#
# Copyright (c) 2025 GCK
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
printf "config.status: recreating config.mak... "
{
printf "PREFIX=%s\n" "/usr/local"
printf "CFLAGS=%s\n" "-pedantic -O0 -ggdb -Wall -Wextra -Wpedantic -fno-omit-frame-pointer -DDEBUG"
printf "CC=%s\n" "gcc"
} > config.status
printf "done\n"

234
lib/err.c
View File

@@ -51,143 +51,165 @@
#define NOTE "\x1B[1;94m" #define NOTE "\x1B[1;94m"
#define HINT "\x1B[38;5;166m" #define HINT "\x1B[38;5;166m"
static bool err_support_color(void) { static bool err_support_color(void)
{
#ifdef NOCOLOR #ifdef NOCOLOR
return false; return false;
#else #else
static int cached = -1; static int cached = -1;
if (cached != -1) if (cached != -1)
return cached; return cached;
const char *term, *colorterm, *force, *nocolor; const char *term, *colorterm, *force, *nocolor;
term = getenv("TERM"); term = getenv("TERM");
colorterm = getenv("COLORTERM"); colorterm = getenv("COLORTERM");
force = getenv("FORCE_COLOR"); force = getenv("FORCE_COLOR");
nocolor = getenv("NO_COLOR"); nocolor = getenv("NO_COLOR");
if (nocolor && *nocolor && (!force || !*force)) { if (nocolor && *nocolor && (!force || !*force)) {
cached = 0; cached = 0;
return false; return false;
} }
if (force && *force && strcmp(force, "0") != 0) { if (force && *force && strcmp(force, "0") != 0) {
cached = 1; cached = 1;
return true; return true;
} }
// if (!isatty(fileno(stdout))) { // if (!isatty(fileno(stdout))) {
// cached = 0; // cached = 0;
// return false; // return false;
// } // }
if (colorterm && *colorterm) { if (colorterm && *colorterm) {
cached = 1; cached = 1;
return true; return true;
} }
if (!term || !*term) { if (!term || !*term) {
cached = 0; cached = 0;
return false; return false;
} }
if (strstr(term, "color") || strstr(term, "xterm") || if (strstr(term, "color") || strstr(term, "xterm") ||
strstr(term, "screen") || strstr(term, "vt100") || strstr(term, "rxvt") || strstr(term, "screen") || strstr(term, "vt100") ||
strstr(term, "ansi") || strstr(term, "linux") || strstr(term, "rxvt") || strstr(term, "ansi") ||
strstr(term, "konsole") || strstr(term, "vte") || strstr(term, "kitty") || strstr(term, "linux") || strstr(term, "konsole") ||
strstr(term, "wezterm") || strstr(term, "gnome")) { strstr(term, "vte") || strstr(term, "kitty") ||
cached = 1; strstr(term, "wezterm") || strstr(term, "gnome")) {
return true; cached = 1;
} return true;
cached = 0; }
return false; cached = 0;
return false;
#endif #endif
} }
void errorf(const char *format, ...) { void errorf(const char *format, ...)
va_list args; {
va_start(args, format); va_list args;
va_start(args, format);
if (err_support_color()) { if (err_support_color()) {
fprintf(stderr, "%serror%s: ", ERROR, RESET); fprintf(stderr, "%serror%s: ", ERROR, RESET);
} else { } else {
fputs("error: ", stderr); fputs("error: ", stderr);
} }
vfprintf(stderr, format, args); vfprintf(stderr, format, args);
fputc('\n', stderr); fputc('\n', stderr);
va_end(args); va_end(args);
} }
void fatalf(const char *format, ...) { void fatalf(const char *format, ...)
va_list args; {
va_start(args, format); va_list args;
va_start(args, format);
if (err_support_color()) { if (err_support_color()) {
fprintf(stderr, "%sfatal error%s: ", ERROR, RESET); fprintf(stderr, "%sfatal error%s: ", ERROR, RESET);
} else { } else {
fputs("fatal error: ", stderr); fputs("fatal error: ", stderr);
} }
vfprintf(stderr, format, args); vfprintf(stderr, format, args);
fputc('\n', stderr); fputc('\n', stderr);
va_end(args); va_end(args);
fputs("program terminated.\n", stderr); fputs("program terminated.\n", stderr);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
void warnf(const char *format, ...) { void warnf(const char *format, ...)
va_list args; {
va_start(args, format); va_list args;
va_start(args, format);
if (err_support_color()) { if (err_support_color()) {
fprintf(stderr, "%swarning%s: ", WARN, RESET); fprintf(stderr, "%swarning%s: ", WARN, RESET);
} else { } else {
fputs("warning: ", stderr); fputs("warning: ", stderr);
} }
vfprintf(stderr, format, args); vfprintf(stderr, format, args);
fputc('\n', stderr); fputc('\n', stderr);
va_end(args); va_end(args);
} }
void notef(const char *format, ...) { void notef(const char *format, ...)
va_list args; {
va_start(args, format); va_list args;
va_start(args, format);
if (err_support_color()) { if (err_support_color()) {
fprintf(stderr, "%snote%s: ", NOTE, RESET); fprintf(stderr, "%snote%s: ", NOTE, RESET);
} else { } else {
fputs("note: ", stderr); fputs("note: ", stderr);
} }
vfprintf(stderr, format, args); vfprintf(stderr, format, args);
fputc('\n', stderr); fputc('\n', stderr);
va_end(args); va_end(args);
} }
void hintf(const char *format, ...) { void hintf(const char *format, ...)
va_list args; {
va_start(args, format); va_list args;
va_start(args, format);
if (err_support_color()) { if (err_support_color()) {
fprintf(stderr, "%shint: ", HINT); fprintf(stderr, "%shint: ", HINT);
} else { } else {
fputs("hint: ", stderr); fputs("hint: ", stderr);
} }
vfprintf(stderr, format, args); vfprintf(stderr, format, args);
if (err_support_color()) { if (err_support_color()) {
fprintf(stderr, "%s\n", RESET); fprintf(stderr, "%s\n", RESET);
} else { } else {
fputc('\n', stderr); fputc('\n', stderr);
} }
va_end(args); va_end(args);
} }
void errorfa(int code) { errorf(strerror(code)); } void errorfa(int code)
{
errorf(strerror(code));
}
void fatalfa(int code) { fatalf(strerror(code)); } void fatalfa(int code)
{
fatalf(strerror(code));
}
void notefa(int code) { notef(strerror(code)); } void notefa(int code)
{
notef(strerror(code));
}
void warnfa(int code) { warnf(strerror(code)); } void warnfa(int code)
{
warnf(strerror(code));
}
void hintfa(int code) { hintf(strerror(code)); } void hintfa(int code)
{
hintf(strerror(code));
}
/* end of file err.c */ /* end of file err.c */

View File

@@ -55,16 +55,16 @@ void warnfa(int code);
void hintfa(int code); void hintfa(int code);
#if defined(SHOW_TRACE) #if defined(SHOW_TRACE)
#define errorf(fmt, ...) \ #define errorf(fmt, ...) \
do { \ do { \
errorf("%s:%s:%d: " fmt, __FILE__, __func__, \ errorf("%s:%s:%d: " fmt, __FILE__, __func__, \
__LINE__ __VA_OPT__(, ) __VA_ARGS__); \ __LINE__ __VA_OPT__(, ) __VA_ARGS__); \
} while (0) } while (0)
#define fatalf(fmt, ...) \ #define fatalf(fmt, ...) \
do { \ do { \
fatalf("%s:%s:%d: " fmt, __FILE__, __func__, \ fatalf("%s:%s:%d: " fmt, __FILE__, __func__, \
__LINE__ __VA_OPT__(, ) __VA_ARGS__); \ __LINE__ __VA_OPT__(, ) __VA_ARGS__); \
} while (0) } while (0)
#endif #endif
#endif #endif

View File

@@ -43,78 +43,83 @@
static char *nextchar; static char *nextchar;
int getopt_long(int argc, char *const argv[], const char *optstring, int getopt_long(int argc, char *const argv[], const char *optstring,
const struct option *longopts, int *longindex) { const struct option *longopts, int *longindex)
if (nextchar == NULL || *nextchar == '\0') { {
if (optind >= argc) if (nextchar == NULL || *nextchar == '\0') {
return -1; if (optind >= argc)
if (argv[optind][0] != '-' || argv[optind][1] == '\0') return -1;
return -1; if (argv[optind][0] != '-' || argv[optind][1] == '\0')
if (argv[optind][1] == '-' && argv[optind][2] == '\0') { return -1;
optind++; if (argv[optind][1] == '-' && argv[optind][2] == '\0') {
return -1; optind++;
} return -1;
if (argv[optind][1] == '-') { }
const char *arg = argv[optind] + 2; if (argv[optind][1] == '-') {
char *eq = strchr(arg, '='); const char *arg = argv[optind] + 2;
size_t len = eq ? (size_t)(eq - arg) : strlen(arg); char *eq = strchr(arg, '=');
for (int i = 0; longopts[i].name; i++) { size_t len = eq ? (size_t)(eq - arg) : strlen(arg);
if (strncmp(arg, longopts[i].name, len) == 0 && for (int i = 0; longopts[i].name; i++) {
strlen(longopts[i].name) == len) { if (strncmp(arg, longopts[i].name, len) == 0 &&
if (longindex) strlen(longopts[i].name) == len) {
*longindex = i; if (longindex)
if (longopts[i].has_arg == required_argument) { *longindex = i;
if (eq) if (longopts[i].has_arg ==
optarg = (char *)eq + 1; required_argument) {
else if (optind + 1 < argc) if (eq)
optarg = argv[++optind]; optarg = (char *)eq + 1;
else else if (optind + 1 < argc)
return '?'; optarg = argv[++optind];
} else if (longopts[i].has_arg == optional_argument) else
optarg = eq ? (char *)eq + 1 : NULL; return '?';
else } else if (longopts[i].has_arg ==
optarg = NULL; optional_argument)
optind++; optarg = eq ? (char *)eq + 1 :
if (longopts[i].flag) { NULL;
*longopts[i].flag = longopts[i].val; else
return 0; optarg = NULL;
} optind++;
return longopts[i].val; if (longopts[i].flag) {
} *longopts[i].flag =
} longopts[i].val;
optind++; return 0;
return '?'; }
} return longopts[i].val;
nextchar = argv[optind] + 1; }
} }
char c = *nextchar++; optind++;
const char *pos = strchr(optstring, c); return '?';
if (!pos) { }
optopt = c; nextchar = argv[optind] + 1;
if (*nextchar == '\0') }
optind++; char c = *nextchar++;
return '?'; const char *pos = strchr(optstring, c);
} if (!pos) {
if (pos[1] == ':') { optopt = c;
if (*nextchar != '\0') { if (*nextchar == '\0')
optarg = nextchar; optind++;
optind++; return '?';
nextchar = NULL; }
} else if (optind + 1 < argc) { if (pos[1] == ':') {
optarg = argv[++optind]; if (*nextchar != '\0') {
optind++; optarg = nextchar;
nextchar = NULL; optind++;
} else { nextchar = NULL;
optopt = c; } else if (optind + 1 < argc) {
return '?'; optarg = argv[++optind];
} optind++;
} else { nextchar = NULL;
optarg = NULL; } else {
if (*nextchar == '\0') { optopt = c;
optind++; return '?';
nextchar = NULL; }
} } else {
} optarg = NULL;
return c; if (*nextchar == '\0') {
optind++;
nextchar = NULL;
}
}
return c;
} }
/* end of file flag.c */ /* end of file flag.c */

View File

@@ -37,7 +37,7 @@
#define FLAG_H #define FLAG_H
int getopt_long(int argc, char *const argv[], const char *optstring, int getopt_long(int argc, char *const argv[], const char *optstring,
const struct option *longopts, int *longindex); const struct option *longopts, int *longindex);
#endif #endif

223
lib/fs.c
View File

@@ -53,150 +53,159 @@
#include "xmem.h" #include "xmem.h"
#if defined(FS_ERROR_ON) #if defined(FS_ERROR_ON)
#define RETURN(code) \ #define RETURN(code) \
errorfa(code); \ errorfa(code); \
return code return code
#elif defined(FS_FATAL_OFF) #elif defined(FS_FATAL_OFF)
#define RETURN(code) return code #define RETURN(code) return code
#else #else
#define RETURN(code) fatalfa(code) #define RETURN(code) fatalfa(code)
#endif #endif
char *fs_read(const char *path) { char *fs_read(const char *path)
FILE *fptr = fopen(path, "r"); {
if (!fptr) { FILE *fptr = fopen(path, "r");
if (!fptr) {
#if defined(FS_ERROR_ON) #if defined(FS_ERROR_ON)
errorfa(errno); errorfa(errno);
#elif defined(FS_FATAL_ON) #elif defined(FS_FATAL_ON)
fatalfa(errno); fatalfa(errno);
#endif #endif
return NULL; return NULL;
} }
size_t cap = 1024; size_t cap = 1024;
size_t len = 0; size_t len = 0;
char *buf = xmalloc(cap); char *buf = xmalloc(cap);
int c; int c;
while ((c = fgetc(fptr)) != EOF) { while ((c = fgetc(fptr)) != EOF) {
if (len + 1 >= cap) { if (len + 1 >= cap) {
cap *= 2; cap *= 2;
char *tmp = realloc(buf, cap); char *tmp = realloc(buf, cap);
if (!tmp) { if (!tmp) {
free(buf); free(buf);
fclose(fptr); fclose(fptr);
return NULL; return NULL;
} }
buf = tmp; buf = tmp;
} }
buf[len++] = (char)c; buf[len++] = (char)c;
} }
buf[len] = '\0'; buf[len] = '\0';
fclose(fptr); fclose(fptr);
return buf; return buf;
} }
bool fs_exists(const char *path) { bool fs_exists(const char *path)
FILE *fptr; {
bool exists; FILE *fptr;
bool exists;
fptr = fopen(path, "r"); fptr = fopen(path, "r");
if (fptr) { if (fptr) {
exists = true; exists = true;
} else { } else {
exists = false; exists = false;
} }
fclose(fptr); fclose(fptr);
return exists; return exists;
} }
int fs_append(const char *path, const char *format, ...) { int fs_append(const char *path, const char *format, ...)
FILE *fp = fopen(path, "a"); {
if (!fp) FILE *fp = fopen(path, "a");
RETURN(errno); if (!fp)
RETURN(errno);
va_list ap; va_list ap;
va_start(ap, format); va_start(ap, format);
int ret = vfprintf(fp, format, ap); int ret = vfprintf(fp, format, ap);
va_end(ap); va_end(ap);
if (ret < 0) { if (ret < 0) {
fclose(fp); fclose(fp);
RETURN(errno); RETURN(errno);
} }
if (fclose(fp) != 0) if (fclose(fp) != 0)
RETURN(errno); RETURN(errno);
return ret; return ret;
} }
int fs_del(const char *path) { RETURN(remove(path)); } int fs_del(const char *path)
{
int fs_new(const char *path) { RETURN(remove(path));
size_t len;
int fd;
if (path == NULL) {
errno = EINVAL;
RETURN(-1);
}
len = strlen(path);
if (len == 0) {
errno = EINVAL;
RETURN(-1);
}
if (path[len - 1] == '/') {
if (mkdir(path, 0777) == -1)
RETURN(-1);
} else {
fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666);
if (fd == -1)
RETURN(-1);
close(fd);
}
return 0;
} }
int fs_write(const char *path, const char *format, ...) { int fs_new(const char *path)
FILE *fptr = fopen(path, "w"); {
if (!fptr) size_t len;
RETURN(-1); int fd;
va_list ap; if (path == NULL) {
va_start(ap, format); errno = EINVAL;
int ret = vfprintf(fptr, format, ap); RETURN(-1);
va_end(ap); }
if (ret < 0) { len = strlen(path);
fclose(fptr); if (len == 0) {
RETURN(-1); errno = EINVAL;
} RETURN(-1);
}
if (fclose(fptr) != 0) if (path[len - 1] == '/') {
RETURN(-1); if (mkdir(path, 0777) == -1)
RETURN(-1);
} else {
fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666);
if (fd == -1)
RETURN(-1);
close(fd);
}
return ret; return 0;
} }
FILE *fs_temp() { int fs_write(const char *path, const char *format, ...)
FILE *fptr = tmpfile(); {
FILE *fptr = fopen(path, "w");
if (!fptr)
RETURN(-1);
if (!fptr) { va_list ap;
va_start(ap, format);
int ret = vfprintf(fptr, format, ap);
va_end(ap);
if (ret < 0) {
fclose(fptr);
RETURN(-1);
}
if (fclose(fptr) != 0)
RETURN(-1);
return ret;
}
FILE *fs_temp()
{
FILE *fptr = tmpfile();
if (!fptr) {
#if defined(FS_ERROR_ON) #if defined(FS_ERROR_ON)
errorf("tmp failed"); errorf("tmp failed");
#elif defined(FS_FATAL_ON) #elif defined(FS_FATAL_ON)
fatalf("tmp failed"); fatalf("tmp failed");
#endif #endif
return NULL; return NULL;
} }
return fptr; return fptr;
} }
/* end of file fs.c */ /* end of file fs.c */

View File

@@ -44,34 +44,40 @@
/* TODO(vx-clutch): default this to argv[0] */ /* TODO(vx-clutch): default this to argv[0] */
const char *prog_name = ""; const char *prog_name = "";
void set_prog_name(char *name) { prog_name = prog_name ? basename(name) : ""; } void set_prog_name(char *name)
{
void emit_try_help() { prog_name = prog_name ? basename(name) : "";
printf("Try '%s --help' for more information\n", prog_name);
} }
void emit_version() { void emit_try_help()
printf("\ {
printf("Try '%s --help' for more information\n", prog_name);
}
void emit_version()
{
printf("\
%s %s %d\n\ %s %s %d\n\
Copyright (C) %d GCK.\n\ Copyright (C) %d GCK.\n\
This is free software: you are free to change and redistribute it.\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\ There is NO WARRNTY, to the extent permitted by law.\n\
", ",
prog_name, VERSION, COMMIT, YEAR); prog_name, VERSION, COMMIT, YEAR);
} }
int parse_standard_options(int argc, char **argv, void (*print_help)(), int parse_standard_options(int argc, char **argv, void (*print_help)(),
void (*print_version)()) { void (*print_version)())
for (int i = 0; i < argc; ++i) { {
if (!strcmp(argv[i], "--help")) { for (int i = 0; i < argc; ++i) {
print_help(); if (!strcmp(argv[i], "--help")) {
exit(EXIT_SUCCESS); print_help();
} else if (!strcmp(argv[i], "--version")) { exit(EXIT_SUCCESS);
emit_version(); } else if (!strcmp(argv[i], "--version")) {
exit(EXIT_SUCCESS); emit_version();
} exit(EXIT_SUCCESS);
} }
return 0; }
return 0;
} }
/* end of file proginfo.c */ /* end of file proginfo.c */

View File

@@ -44,7 +44,7 @@ void emit_try_help();
void emit_version(); void emit_version();
int parse_standard_options(int argc, char **argv, void (*print_help)(), int parse_standard_options(int argc, char **argv, void (*print_help)(),
void (*print_version)()); void (*print_version)());
#endif #endif

View File

@@ -44,58 +44,63 @@
#include "xmem.h" #include "xmem.h"
void alert() { void alert()
fputs("\a", stderr); {
fflush(stderr); fputs("\a", stderr);
return; fflush(stderr);
return;
} }
int vasprintf(char **result, const char *fmt, va_list ap) { int vasprintf(char **result, const char *fmt, va_list ap)
int total_width = strlen(fmt) + 1; {
*result = (char *)xmalloc(total_width); int total_width = strlen(fmt) + 1;
return vsprintf(*result, fmt, ap); *result = (char *)xmalloc(total_width);
return vsprintf(*result, fmt, ap);
} }
int asprintf(char **buf, const char *fmt, ...) { int asprintf(char **buf, const char *fmt, ...)
int status; {
va_list ap; int status;
va_start(ap, fmt); va_list ap;
status = vasprintf(buf, fmt, ap); va_start(ap, fmt);
va_end(ap); status = vasprintf(buf, fmt, ap);
return status; va_end(ap);
return status;
} }
int say(const char *restrict format, ...) { int say(const char *restrict format, ...)
struct winsize w; {
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) { struct winsize w;
va_list args; if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {
va_start(args, format); va_list args;
int ret = vprintf(format, args); va_start(args, format);
va_end(args); int ret = vprintf(format, args);
putchar('\n'); va_end(args);
fflush(stdout); putchar('\n');
return ret; fflush(stdout);
} return ret;
}
printf("\0337"); // save cursor (ESC 7) printf("\0337"); // save cursor (ESC 7)
printf("\033[%d;1H", w.ws_row); // move to last row printf("\033[%d;1H", w.ws_row); // move to last row
printf("\033[2K"); // clear entire line printf("\033[2K"); // clear entire line
va_list args; va_list args;
va_start(args, format); va_start(args, format);
int ret = vprintf(format, args); // print formatted message int ret = vprintf(format, args); // print formatted message
va_end(args); va_end(args);
fflush(stdout); // ensure it's visible immediately fflush(stdout); // ensure it's visible immediately
printf("\0338"); // restore cursor (ESC 8) printf("\0338"); // restore cursor (ESC 8)
fflush(stdout); fflush(stdout);
return ret; return ret;
} }
_Noreturn void die(const char *msg) { _Noreturn void die(const char *msg)
fputs(msg, stderr); {
exit(EXIT_FAILURE); fputs(msg, stderr);
exit(EXIT_FAILURE);
} }
/* end of file say.c */ /* end of file say.c */

View File

@@ -39,35 +39,42 @@
#include "textc.h" #include "textc.h"
char *str_dup(char *s) { char *str_dup(char *s)
char *new = xmalloc(strlen(s) + 1); {
strcpy(new, s); char *new = xmalloc(strlen(s) + 1);
return new; strcpy(new, s);
return new;
} }
char *tostrupr(char *s) { char *tostrupr(char *s)
char *new = str_dup(s); {
for (int i = 0; new[i] != '\0'; ++i) char *new = str_dup(s);
new[i] = toupper((unsigned char)new[i]); for (int i = 0; new[i] != '\0'; ++i)
return new; new[i] = toupper((unsigned char)new[i]);
return new;
} }
char *tostrlwr(char *s) { char *tostrlwr(char *s)
char *new = str_dup(s); {
for (int i = 0; new[i] != '\0'; ++i) char *new = str_dup(s);
new[i] = tolower((unsigned char)new[i]); for (int i = 0; new[i] != '\0'; ++i)
return new; new[i] = tolower((unsigned char)new[i]);
return new;
} }
char *textc_trim(char *s) { return NULL; } char *textc_trim(char *s)
{
return NULL;
}
char *textc_pad_left(int count, char *s, char pad) { char *textc_pad_left(int count, char *s, char pad)
char *buffer = xmalloc(strlen(s) + 1); {
char *buffer = xmalloc(strlen(s) + 1);
free(buffer); free(buffer);
buffer = "NOT IMPLEMENTED"; buffer = "NOT IMPLEMENTED";
return buffer; return buffer;
} }
/* end of file textc.c */ /* end of file textc.c */

View File

@@ -40,20 +40,26 @@
#include "xmem.h" #include "xmem.h"
void *ensure_nonnull(void *ptr) { void *ensure_nonnull(void *ptr)
if (ptr == NULL) {
fatalf("memory exhausted"); if (ptr == NULL)
return ptr; fatalf("memory exhausted");
return ptr;
} }
void *xmalloc(size_t size) { return ensure_nonnull(malloc(size)); } void *xmalloc(size_t size)
{
void *xrealloc(void *ptr, size_t size) { return ensure_nonnull(malloc(size));
return ensure_nonnull(realloc(ptr, size));
} }
void *xcalloc(size_t nmemb, size_t size) { void *xrealloc(void *ptr, size_t size)
return ensure_nonnull(calloc(nmemb, size)); {
return ensure_nonnull(realloc(ptr, size));
}
void *xcalloc(size_t nmemb, size_t size)
{
return ensure_nonnull(calloc(nmemb, size));
} }
/* end of file xmem.c */ /* end of file xmem.c */

View File

@@ -67,11 +67,13 @@
typedef enum { MIT, GPL, BSD, UNL } licence_t; typedef enum { MIT, GPL, BSD, UNL } licence_t;
static const struct option longopts[] = {{"author", required_argument, 0, 'a'}, static const struct option longopts[] = {
{"licence", required_argument, 0, 'l'}, { "author", required_argument, 0, 'a' },
{"quiet", no_argument, 0, 'q'}, { "licence", required_argument, 0, 'l' },
{"force", no_argument, 0, 'f'}, { "quiet", no_argument, 0, 'q' },
{0, 0, 0, 0}}; { "force", no_argument, 0, 'f' },
{ 0, 0, 0, 0 }
};
static int exit_status; static int exit_status;
@@ -79,9 +81,10 @@ static void print_help();
static void print_version(); static void print_version();
static char *source_replace(const char *restrict template, static char *source_replace(const char *restrict template,
const char *restrict package, const char *restrict package,
const char *restrict author) { const char *restrict author)
/* {
/*
* XXX(vx-clutch): * XXX(vx-clutch):
* - package token : {{PACKAGE}} * - package token : {{PACKAGE}}
* - author token : {{AUTHOR}} * - author token : {{AUTHOR}}
@@ -90,135 +93,140 @@ static char *source_replace(const char *restrict template,
* - replace all packages with package and authors with author * - replace all packages with package and authors with author
* - return * - return
*/ */
char *buffer = xmalloc(99999999999); char *buffer = xmalloc(99999999999);
return NULL; return NULL;
} }
static char *get_name() { static char *get_name()
int fds[2]; {
if (pipe(fds) == -1) int fds[2];
goto sysuser; if (pipe(fds) == -1)
goto sysuser;
pid_t pid = fork(); pid_t pid = fork();
if (pid == -1) { if (pid == -1) {
close(fds[0]); close(fds[0]);
close(fds[1]); close(fds[1]);
goto sysuser; goto sysuser;
} }
if (pid == 0) { if (pid == 0) {
dup2(fds[1], STDOUT_FILENO); dup2(fds[1], STDOUT_FILENO);
close(fds[0]); close(fds[0]);
close(fds[1]); close(fds[1]);
execlp("git", "git", "config", "--get", "user.name", (char *)NULL); execlp("git", "git", "config", "--get", "user.name",
_exit(127); (char *)NULL);
} _exit(127);
}
close(fds[1]); close(fds[1]);
char buf[256]; char buf[256];
ssize_t n = read(fds[0], buf, sizeof buf - 1); ssize_t n = read(fds[0], buf, sizeof buf - 1);
close(fds[0]); close(fds[0]);
int status; int status;
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
if (n > 0 && WIFEXITED(status) && WEXITSTATUS(status) == 0) { if (n > 0 && WIFEXITED(status) && WEXITSTATUS(status) == 0) {
buf[n] = 0; buf[n] = 0;
buf[strcspn(buf, "\n")] = 0; buf[strcspn(buf, "\n")] = 0;
return str_dup(buf); return str_dup(buf);
} }
sysuser: { sysuser: {
char *name = getlogin(); char *name = getlogin();
if (name) if (name)
return str_dup(name); return str_dup(name);
struct passwd *pw = getpwuid(getuid()); struct passwd *pw = getpwuid(getuid());
if (pw && pw->pw_name) if (pw && pw->pw_name)
return str_dup(pw->pw_name); return str_dup(pw->pw_name);
} }
return str_dup("author"); return str_dup("author");
} }
static int get_year() { static int get_year()
time_t now = time(NULL); {
struct tm *t = localtime(&now); time_t now = time(NULL);
struct tm *t = localtime(&now);
return t->tm_year + 1900; return t->tm_year + 1900;
} }
int main(int argc, char **argv) { int main(int argc, char **argv)
int optc; {
int lose = 0; int optc;
char *package; int lose = 0;
bool quiet = false; char *package;
bool force = false; bool quiet = false;
bool editor = false; bool force = false;
bool shell = false; bool editor = false;
char *author = get_name(); bool shell = false;
exit_status = EXIT_SUCCESS; char *author = get_name();
int year = get_year(); exit_status = EXIT_SUCCESS;
licence_t licence = BSD; int year = get_year();
set_prog_name(argv[0]); licence_t licence = BSD;
set_prog_name(argv[0]);
parse_standard_options(argc, argv, print_help, print_version); parse_standard_options(argc, argv, print_help, print_version);
while ((optc = getopt_long(argc, argv, "a:l:EqfS", longopts, NULL)) != -1) while ((optc = getopt_long(argc, argv, "a:l:EqfS", longopts, NULL)) !=
switch (optc) { -1)
case 'a': switch (optc) {
if (optarg) { case 'a':
if (author) if (optarg) {
free(author); if (author)
author = str_dup(optarg); free(author);
} author = str_dup(optarg);
break; }
case 'l': break;
if (!strcmp(optarg, "list")) { case 'l':
puts("BSD\nGPL\nMIT\nUNL"); if (!strcmp(optarg, "list")) {
exit(EXIT_SUCCESS); puts("BSD\nGPL\nMIT\nUNL");
} exit(EXIT_SUCCESS);
if (!strcmp(optarg, "GPL")) }
licence = GPL; if (!strcmp(optarg, "GPL"))
else if (!strcmp(optarg, "MIT")) licence = GPL;
licence = MIT; else if (!strcmp(optarg, "MIT"))
else if (!strcmp(optarg, "BSD")) licence = MIT;
licence = BSD; else if (!strcmp(optarg, "BSD"))
else if (!strcmp(optarg, "UNL")) licence = BSD;
licence = UNL; else if (!strcmp(optarg, "UNL"))
else { licence = UNL;
puts("BSD\nGPL\nMIT\nUNL"); else {
exit(EXIT_FAILURE); puts("BSD\nGPL\nMIT\nUNL");
} exit(EXIT_FAILURE);
break; }
case 'E': break;
editor = true; case 'E':
break; editor = true;
case 'q': break;
quiet = true; case 'q':
break; quiet = true;
case 'f': break;
force = true; case 'f':
break; force = true;
case 'S': break;
shell = true; case 'S':
break; shell = true;
default: break;
lose = 1; default:
} lose = 1;
if (lose) { }
emit_try_help(); if (lose) {
} emit_try_help();
}
if (optind >= argc) { if (optind >= argc) {
fatalf("no input name"); fatalf("no input name");
} }
if (optind + 1 < argc) { if (optind + 1 < argc) {
errorf("extra operand: %s", argv[optind + 1]); errorf("extra operand: %s", argv[optind + 1]);
emit_try_help(); emit_try_help();
} }
package = str_dup(argv[optind]); package = str_dup(argv[optind]);
if (shell) { if (shell) {
fs_write(package, "\ fs_write(package, "\
#!/bin/sh\n\ #!/bin/sh\n\
\n\ \n\
# Usage: $0 [options]...\n\ # Usage: $0 [options]...\n\
@@ -263,44 +271,44 @@ while [ $# -gt 0 ]; do\n\
shift\n\ shift\n\
done\n\ done\n\
", ",
year, author); year, author);
struct stat st; struct stat st;
if (stat(package, &st) != 0) { if (stat(package, &st) != 0) {
fatalfa(errno); fatalfa(errno);
} }
mode_t mode = st.st_mode | S_IXUSR; mode_t mode = st.st_mode | S_IXUSR;
if (chmod(package, mode) != 0) { if (chmod(package, mode) != 0) {
fatalfa(errno); fatalfa(errno);
} }
return exit_status; return exit_status;
} }
char *pdir; char *pdir;
asprintf(&pdir, "%s/", package); asprintf(&pdir, "%s/", package);
fs_new(pdir); fs_new(pdir);
if (chdir(pdir)) if (chdir(pdir))
fatalfa(errno); fatalfa(errno);
fs_new("doc/"); fs_new("doc/");
fs_new("src/"); fs_new("src/");
fs_new("tools/"); fs_new("tools/");
fs_new("lib/"); fs_new("lib/");
fs_write("doc/version.texi", "\ fs_write("doc/version.texi", "\
@set UPDATED %s\ @set UPDATED %s\
@set UPDATED-MONTH %s\ @set UPDATED-MONTH %s\
@set EDITION 1\ @set EDITION 1\
@set VERSION alpha\ @set VERSION alpha\
", ",
"1 January 1970", "January 2025"); "1 January 1970", "January 2025");
char *texi_buffer; char *texi_buffer;
asprintf(&texi_buffer, "doc/%s.texi", package); asprintf(&texi_buffer, "doc/%s.texi", package);
fs_write(texi_buffer, "\ fs_write(texi_buffer, "\
\\input texinfo @c -*-texinfo-*-\n\ \\input texinfo @c -*-texinfo-*-\n\
@c %**start of header\n\ @c %**start of header\n\
@setfilename foo.info\n\ @setfilename foo.info\n\
@@ -469,14 +477,14 @@ a @file{ChangeLog} entry.\n\
\n\ \n\
@bye\ @bye\
", ",
author, author, author, author, author, author, author, author, author, author, author, author, author, author, author, author,
author, author, author, author, author, author); author, author, author, author, author, author);
free(texi_buffer); free(texi_buffer);
char *src_path; char *src_path;
asprintf(&src_path, "src/%s.c", package); asprintf(&src_path, "src/%s.c", package);
fs_write(src_path, "typedef int x;"); fs_write(src_path, "typedef int x;");
// fs_write(src_path, "\ // fs_write(src_path, "\
// /* Copyright (C) %s\n\ // /* Copyright (C) %s\n\
// *\n\ // *\n\
// * This file is part of %s\n\ // * This file is part of %s\n\
@@ -537,10 +545,10 @@ a @file{ChangeLog} entry.\n\
// exit(exit_status);\n\ // exit(exit_status);\n\
// }\ // }\
// ", // ",
// author, package, package, package, author); // author, package, package, package, author);
free(src_path); free(src_path);
fs_write("tools/Cleanup", "\ fs_write("tools/Cleanup", "\
#!/bin/sh\n\ #!/bin/sh\n\
# Usage: ./Cleanup\n\ # Usage: ./Cleanup\n\
\n\ \n\
@@ -562,7 +570,7 @@ run make distclean\n\
\n\ \n\
echo \"done.\"\ echo \"done.\"\
"); ");
fs_write("tools/format", "\ fs_write("tools/format", "\
#!/bin/sh\n\ #!/bin/sh\n\
\n\ \n\
# Usage ./format\n\ # Usage ./format\n\
@@ -571,7 +579,7 @@ find . -name \"*.c\" -exec clang-format -i --verbose {} \\;\n\
find . -name \"*.h\" -exec clang-format -i --verbose {} \\;\ find . -name \"*.h\" -exec clang-format -i --verbose {} \\;\
"); ");
fs_write(".clang-format", "\ fs_write(".clang-format", "\
---\n\ ---\n\
AccessModifierOffset: -4\n\ AccessModifierOffset: -4\n\
AlignAfterOpenBracket: Align\n\ AlignAfterOpenBracket: Align\n\
@@ -682,7 +690,7 @@ UseTab: Always\n\
...\ ...\
"); ");
fs_write(".clangd", "\ fs_write(".clangd", "\
CompileFlags:\n\ CompileFlags:\n\
Add: [-x, c, -std=c23, -Ilib, -I.]\n\ Add: [-x, c, -std=c23, -Ilib, -I.]\n\
\n\ \n\
@@ -692,7 +700,7 @@ Diagnostics:\n\
Remove: []\ Remove: []\
"); ");
fs_write("README", "\ fs_write("README", "\
This is the README for the GCK %s distribution.\n\ This is the README for the GCK %s distribution.\n\
%s does a thing.\n\ %s does a thing.\n\
\n\ \n\
@@ -723,9 +731,9 @@ implementation, etc., would still be very much appreciated.\n\
\n\ \n\
GCK %s is free software. See the file COPYING for copying conditions.\n\ GCK %s is free software. See the file COPYING for copying conditions.\n\
\n", \n",
package, package, year, package); package, package, year, package);
fs_write("INSTALL", "\ fs_write("INSTALL", "\
Installation Instructions\n\ Installation Instructions\n\
*************************\n\ *************************\n\
\n\ \n\
@@ -781,9 +789,9 @@ Documentation and other data files still use the regular prefix.\n\
`configure` also accepts some other options. Run `configure --help` for more\n\ `configure` also accepts some other options. Run `configure --help` for more\n\
details\n\ details\n\
", ",
year); year);
fs_write("AUTHORS", "\ fs_write("AUTHORS", "\
Authors of %s %s.\n\ Authors of %s %s.\n\
\n\ \n\
Copyright (C) %d %s.\n\ Copyright (C) %d %s.\n\
@@ -796,9 +804,9 @@ Also see the THANKS files.\n\
\n\ \n\
%s\n\ %s\n\
", ",
author, package, year, package, author); author, package, year, package, author);
fs_write("THANKS", "\ fs_write("THANKS", "\
Additional contributors to %s %s.\n\ Additional contributors to %s %s.\n\
\n\ \n\
Copyright (C) %d %s.\n\ Copyright (C) %d %s.\n\
@@ -813,9 +821,9 @@ Thanks to:\n\
\n\ \n\
See also the AUTHORS file.\n\ See also the AUTHORS file.\n\
", ",
author, package, year, author); author, package, year, author);
fs_write("config.h", "\ fs_write("config.h", "\
#ifndef CONFIG_H\n\ #ifndef CONFIG_H\n\
#define CONFIG_H\n\ #define CONFIG_H\n\
\n\ \n\
@@ -827,9 +835,9 @@ See also the AUTHORS file.\n\
\n\ \n\
#endif\ #endif\
", ",
package, author, year); package, author, year);
fs_write("configure", "\ fs_write("configure", "\
#!/bin/sh\n\ #!/bin/sh\n\
\n\ \n\
usage() {\n\ usage() {\n\
@@ -922,7 +930,7 @@ printf \"CC=%%s\n\" \"$CC\"\n\
printf \"done\n\"\ printf \"done\n\"\
"); ");
fs_write("Makefile", "\ fs_write("Makefile", "\
PACKAGE := %s\n\ PACKAGE := %s\n\
\n\ \n\
SRCS := $(wildcard src/*.c) $(wildcard lib/*.c)\n\ SRCS := $(wildcard src/*.c) $(wildcard lib/*.c)\n\
@@ -978,9 +986,9 @@ release: clean all\n\
\n\ \n\
.PHONY: all clean distclean install uninstall build release\ .PHONY: all clean distclean install uninstall build release\
", ",
package); package);
fs_write("TODO", "\ fs_write("TODO", "\
%s %s- TODO\n\ %s %s- TODO\n\
\n\ \n\
Todo:\n\ Todo:\n\
@@ -989,75 +997,77 @@ Todo:\n\
\n\ \n\
end of file TODO\ end of file TODO\
", ",
author, package, package); author, package, package);
switch (licence) { switch (licence) {
case BSD: case BSD:
fs_write("COPYING", fBSD, year, author); fs_write("COPYING", fBSD, year, author);
break; break;
case MIT: case MIT:
fs_write("COPYING", fMIT, year, author); fs_write("COPYING", fMIT, year, author);
break; break;
case GPL: case GPL:
fs_write("COPYING", fGPL); fs_write("COPYING", fGPL);
break; break;
case UNL: case UNL:
fs_write("COPYING", fUNL); fs_write("COPYING", fUNL);
break; break;
default: default:
fatalf("illegal state"); fatalf("illegal state");
} }
struct stat st; struct stat st;
if (stat("configure", &st) != 0) { if (stat("configure", &st) != 0) {
fatalfa(errno); fatalfa(errno);
} }
mode_t mode = st.st_mode | S_IXUSR; mode_t mode = st.st_mode | S_IXUSR;
if (chmod("configure", mode) != 0) { if (chmod("configure", mode) != 0) {
fatalfa(errno); fatalfa(errno);
} }
if (chmod("tools/format", mode) != 0) { if (chmod("tools/format", mode) != 0) {
fatalfa(errno); fatalfa(errno);
} }
if (chmod("tools/Cleanup", mode) != 0) { if (chmod("tools/Cleanup", mode) != 0) {
fatalfa(errno); fatalfa(errno);
} }
return exit_status; return exit_status;
} }
static void print_help() { static void print_help()
printf("Usage: %s [OPTION]... [project-name]...\n", PROGRAM); {
fputs("\ printf("Usage: %s [OPTION]... [project-name]...\n", PROGRAM);
fputs("\
Generates an opinionated C project.\n", Generates an opinionated C project.\n",
stdout); stdout);
puts(""); puts("");
fputs("\ fputs("\
--help display this help and exit\n\ --help display this help and exit\n\
--version display version information and exit\n", --version display version information and exit\n",
stdout); stdout);
puts(""); puts("");
fputs("\ fputs("\
-E Open $EDITOR after project creation\n\ -E Open $EDITOR after project creation\n\
-q, --quiet Only print required messages\n\ -q, --quiet Only print required messages\n\
-f, --force Overwrite existing files\n\ -f, --force Overwrite existing files\n\
--author=NAME Set the program author (default git username|system username)\n\ --author=NAME Set the program author (default git username|system username)\n\
--licence=LICENCE Set the program licence (default BSD)\n", --licence=LICENCE Set the program licence (default BSD)\n",
stdout); stdout);
exit(exit_status); exit(exit_status);
} }
static void print_version() { static void print_version()
printf("%s %s %d\n", prog_name, VERSION, COMMIT); {
printf("%s %s %d\n", prog_name, VERSION, COMMIT);
printf("Copyright (C) %d GCK.\n", YEAR); printf("Copyright (C) %d GCK.\n", YEAR);
puts("This is free software: you are free to change and redistribute it."); puts("This is free software: you are free to change and redistribute it.");
puts("There is NO WARRANTY, to the extent permitted by law."); puts("There is NO WARRANTY, to the extent permitted by law.");
exit(exit_status); exit(exit_status);
} }
/* end of file yait.c */ /* end of file yait.c */