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"

View File

@@ -51,7 +51,8 @@
#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
@@ -84,9 +85,10 @@ static bool err_support_color(void) {
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, "vte") || strstr(term, "kitty") ||
strstr(term, "wezterm") || strstr(term, "gnome")) { strstr(term, "wezterm") || strstr(term, "gnome")) {
cached = 1; cached = 1;
return true; return true;
@@ -96,7 +98,8 @@ static bool err_support_color(void) {
#endif #endif
} }
void errorf(const char *format, ...) { void errorf(const char *format, ...)
{
va_list args; va_list args;
va_start(args, format); va_start(args, format);
@@ -111,7 +114,8 @@ void errorf(const char *format, ...) {
va_end(args); va_end(args);
} }
void fatalf(const char *format, ...) { void fatalf(const char *format, ...)
{
va_list args; va_list args;
va_start(args, format); va_start(args, format);
@@ -129,7 +133,8 @@ void fatalf(const char *format, ...) {
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_list args;
va_start(args, format); va_start(args, format);
@@ -144,7 +149,8 @@ void warnf(const char *format, ...) {
va_end(args); va_end(args);
} }
void notef(const char *format, ...) { void notef(const char *format, ...)
{
va_list args; va_list args;
va_start(args, format); va_start(args, format);
@@ -159,7 +165,8 @@ void notef(const char *format, ...) {
va_end(args); va_end(args);
} }
void hintf(const char *format, ...) { void hintf(const char *format, ...)
{
va_list args; va_list args;
va_start(args, format); va_start(args, format);
@@ -180,14 +187,29 @@ void hintf(const char *format, ...) {
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

@@ -43,7 +43,8 @@
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 (nextchar == NULL || *nextchar == '\0') {
if (optind >= argc) if (optind >= argc)
return -1; return -1;
@@ -62,20 +63,24 @@ int getopt_long(int argc, char *const argv[], const char *optstring,
strlen(longopts[i].name) == len) { strlen(longopts[i].name) == len) {
if (longindex) if (longindex)
*longindex = i; *longindex = i;
if (longopts[i].has_arg == required_argument) { if (longopts[i].has_arg ==
required_argument) {
if (eq) if (eq)
optarg = (char *)eq + 1; optarg = (char *)eq + 1;
else if (optind + 1 < argc) else if (optind + 1 < argc)
optarg = argv[++optind]; optarg = argv[++optind];
else else
return '?'; return '?';
} else if (longopts[i].has_arg == optional_argument) } else if (longopts[i].has_arg ==
optarg = eq ? (char *)eq + 1 : NULL; optional_argument)
optarg = eq ? (char *)eq + 1 :
NULL;
else else
optarg = NULL; optarg = NULL;
optind++; optind++;
if (longopts[i].flag) { if (longopts[i].flag) {
*longopts[i].flag = longopts[i].val; *longopts[i].flag =
longopts[i].val;
return 0; return 0;
} }
return longopts[i].val; return longopts[i].val;

View File

@@ -62,7 +62,8 @@
#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"); FILE *fptr = fopen(path, "r");
if (!fptr) { if (!fptr) {
#if defined(FS_ERROR_ON) #if defined(FS_ERROR_ON)
@@ -97,7 +98,8 @@ char *fs_read(const char *path) {
return buf; return buf;
} }
bool fs_exists(const char *path) { bool fs_exists(const char *path)
{
FILE *fptr; FILE *fptr;
bool exists; bool exists;
@@ -112,7 +114,8 @@ bool fs_exists(const char *path) {
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"); FILE *fp = fopen(path, "a");
if (!fp) if (!fp)
RETURN(errno); RETURN(errno);
@@ -133,9 +136,13 @@ int fs_append(const char *path, const char *format, ...) {
return ret; return ret;
} }
int fs_del(const char *path) { RETURN(remove(path)); } int fs_del(const char *path)
{
RETURN(remove(path));
}
int fs_new(const char *path) { int fs_new(const char *path)
{
size_t len; size_t len;
int fd; int fd;
@@ -163,7 +170,8 @@ int fs_new(const char *path) {
return 0; return 0;
} }
int fs_write(const char *path, const char *format, ...) { int fs_write(const char *path, const char *format, ...)
{
FILE *fptr = fopen(path, "w"); FILE *fptr = fopen(path, "w");
if (!fptr) if (!fptr)
RETURN(-1); RETURN(-1);
@@ -184,7 +192,8 @@ int fs_write(const char *path, const char *format, ...) {
return ret; return ret;
} }
FILE *fs_temp() { FILE *fs_temp()
{
FILE *fptr = tmpfile(); FILE *fptr = tmpfile();
if (!fptr) { if (!fptr) {

View File

@@ -44,13 +44,18 @@
/* 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)
{
prog_name = prog_name ? basename(name) : "";
}
void emit_try_help() { void emit_try_help()
{
printf("Try '%s --help' for more information\n", prog_name); printf("Try '%s --help' for more information\n", prog_name);
} }
void emit_version() { void emit_version()
{
printf("\ printf("\
%s %s %d\n\ %s %s %d\n\
Copyright (C) %d GCK.\n\ Copyright (C) %d GCK.\n\
@@ -61,7 +66,8 @@ There is NO WARRNTY, to the extent permitted by law.\n\
} }
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) { for (int i = 0; i < argc; ++i) {
if (!strcmp(argv[i], "--help")) { if (!strcmp(argv[i], "--help")) {
print_help(); print_help();

View File

@@ -44,19 +44,22 @@
#include "xmem.h" #include "xmem.h"
void alert() { void alert()
{
fputs("\a", stderr); fputs("\a", stderr);
fflush(stderr); fflush(stderr);
return; 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; int total_width = strlen(fmt) + 1;
*result = (char *)xmalloc(total_width); *result = (char *)xmalloc(total_width);
return vsprintf(*result, fmt, ap); return vsprintf(*result, fmt, ap);
} }
int asprintf(char **buf, const char *fmt, ...) { int asprintf(char **buf, const char *fmt, ...)
{
int status; int status;
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
@@ -65,7 +68,8 @@ int asprintf(char **buf, const char *fmt, ...) {
return status; return status;
} }
int say(const char *restrict format, ...) { int say(const char *restrict format, ...)
{
struct winsize w; struct winsize w;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) { if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {
va_list args; va_list args;
@@ -93,7 +97,8 @@ int say(const char *restrict format, ...) {
return ret; return ret;
} }
_Noreturn void die(const char *msg) { _Noreturn void die(const char *msg)
{
fputs(msg, stderr); fputs(msg, stderr);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

View File

@@ -39,29 +39,36 @@
#include "textc.h" #include "textc.h"
char *str_dup(char *s) { char *str_dup(char *s)
{
char *new = xmalloc(strlen(s) + 1); char *new = xmalloc(strlen(s) + 1);
strcpy(new, s); strcpy(new, s);
return new; return new;
} }
char *tostrupr(char *s) { char *tostrupr(char *s)
{
char *new = str_dup(s); char *new = str_dup(s);
for (int i = 0; new[i] != '\0'; ++i) for (int i = 0; new[i] != '\0'; ++i)
new[i] = toupper((unsigned char)new[i]); new[i] = toupper((unsigned char)new[i]);
return new; return new;
} }
char *tostrlwr(char *s) { char *tostrlwr(char *s)
{
char *new = str_dup(s); char *new = str_dup(s);
for (int i = 0; new[i] != '\0'; ++i) for (int i = 0; new[i] != '\0'; ++i)
new[i] = tolower((unsigned char)new[i]); new[i] = tolower((unsigned char)new[i]);
return new; 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);

View File

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

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;
@@ -80,7 +82,8 @@ 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}}
@@ -94,7 +97,8 @@ static char *source_replace(const char *restrict template,
return NULL; return NULL;
} }
static char *get_name() { static char *get_name()
{
int fds[2]; int fds[2];
if (pipe(fds) == -1) if (pipe(fds) == -1)
goto sysuser; goto sysuser;
@@ -110,7 +114,8 @@ static char *get_name() {
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",
(char *)NULL);
_exit(127); _exit(127);
} }
@@ -137,14 +142,16 @@ sysuser: {
return str_dup("author"); return str_dup("author");
} }
static int get_year() { static int get_year()
{
time_t now = time(NULL); time_t now = time(NULL);
struct tm *t = localtime(&now); 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 optc;
int lose = 0; int lose = 0;
char *package; char *package;
@@ -160,7 +167,8 @@ int main(int argc, char **argv) {
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)) !=
-1)
switch (optc) { switch (optc) {
case 'a': case 'a':
if (optarg) { if (optarg) {
@@ -1029,7 +1037,8 @@ end of file TODO\
return exit_status; return exit_status;
} }
static void print_help() { static void print_help()
{
printf("Usage: %s [OPTION]... [project-name]...\n", PROGRAM); printf("Usage: %s [OPTION]... [project-name]...\n", PROGRAM);
fputs("\ fputs("\
Generates an opinionated C project.\n", Generates an opinionated C project.\n",
@@ -1050,7 +1059,8 @@ Generates an opinionated C project.\n",
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);