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

View File

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

View File

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

View File

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

223
lib/fs.c
View File

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

View File

@@ -44,34 +44,40 @@
/* TODO(vx-clutch): default this to argv[0] */
const char *prog_name = "";
void set_prog_name(char *name) { prog_name = prog_name ? basename(name) : ""; }
void emit_try_help() {
printf("Try '%s --help' for more information\n", prog_name);
void set_prog_name(char *name)
{
prog_name = prog_name ? basename(name) : "";
}
void emit_version() {
printf("\
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);
prog_name, VERSION, COMMIT, YEAR);
}
int parse_standard_options(int argc, char **argv, void (*print_help)(),
void (*print_version)()) {
for (int i = 0; i < argc; ++i) {
if (!strcmp(argv[i], "--help")) {
print_help();
exit(EXIT_SUCCESS);
} else if (!strcmp(argv[i], "--version")) {
emit_version();
exit(EXIT_SUCCESS);
}
}
return 0;
void (*print_version)())
{
for (int i = 0; i < argc; ++i) {
if (!strcmp(argv[i], "--help")) {
print_help();
exit(EXIT_SUCCESS);
} else if (!strcmp(argv[i], "--version")) {
emit_version();
exit(EXIT_SUCCESS);
}
}
return 0;
}
/* end of file proginfo.c */

View File

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

View File

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

View File

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

View File

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

View File

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