From 1f6130c7a513555fe25e0e254579b7f7920bfe16 Mon Sep 17 00:00:00 2001 From: vx-clutch Date: Wed, 22 Oct 2025 19:22:43 -0400 Subject: [PATCH] save --- gcklib | 2 +- lib/err.c | 122 +++++++++++++++++++++++++++++++++++++------------ lib/err.h | 55 +++++++++++++++++++--- lib/flag.c | 36 +++++++++++++-- lib/flag.h | 36 +++++++++++++-- lib/fs.c | 99 +++++++++++++++++++++++++++++---------- lib/fs.h | 36 +++++++++++++-- lib/proginfo.c | 37 +++++++++++++-- lib/proginfo.h | 36 +++++++++++++-- lib/say.c | 105 ++++++++++++++++++++++++++++++++++++++++++ lib/say.h | 50 ++++++++++++++++++++ lib/textc.c | 51 +++++++++++++++++++++ lib/textc.h | 40 ++++++++++++++++ lib/xmem.c | 36 +++++++++++++-- lib/xmem.h | 36 +++++++++++++-- src/yait.c | 34 ++++++++------ 16 files changed, 711 insertions(+), 100 deletions(-) create mode 100644 lib/say.c create mode 100644 lib/say.h diff --git a/gcklib b/gcklib index c12cd32..c47af84 160000 --- a/gcklib +++ b/gcklib @@ -1 +1 @@ -Subproject commit c12cd326f4359bd2f6f96f71b2863ded23d47d05 +Subproject commit c47af845bde5724c2517fde893c3364044a947fe diff --git a/lib/err.c b/lib/err.c index 601d662..bfaeb77 100644 --- a/lib/err.c +++ b/lib/err.c @@ -1,40 +1,102 @@ -/* Copyright (C) GCK +/* + * gcklib.err - Contains color printing functions * - * This file is part of gcklib + * CONFIGURATION + * #define NOCOLOR + * Force no color when printing * - * This project and file is licensed under the BSD-3-Clause license. - * + * + * 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. */ -#include #include +#include #include #include #include #include "err.h" -#define RESET "\e[0m" -#define ERROR "\e[1;91m" -#define WARN "\e[1;95m" -#define NOTE "\e[1;94m" -#define HINT "\e[38;5;166m" +#include "proginfo.h" -static bool __check(void) +#define RESET "\x1B[0m" +#define ERROR "\x1B[1;91m" +#define WARN "\x1B[1;95m" +#define NOTE "\x1B[1;94m" +#define HINT "\x1B[38;5;166m" + +static bool err_support_color(void) { - static int done = 0; - static bool cached = false; - - if (!done) { - const char *term = getenv("TERM"); - cached = isatty(STDOUT_FILENO) && term != NULL && - (strstr(term, "color") != NULL || - strstr(term, "ansi") != NULL || - strstr(term, "xterm") != NULL); - done = 1; +#ifdef NOCOLOR + 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; } - - return cached; + 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, ...) @@ -42,7 +104,7 @@ void errorf(const char *format, ...) va_list args; va_start(args, format); - if (__check()) { + if (err_support_color()) { fprintf(stderr, "%serror%s: ", ERROR, RESET); } else { fputs("error: ", stderr); @@ -58,7 +120,7 @@ void fatalf(const char *format, ...) va_list args; va_start(args, format); - if (__check()) { + if (err_support_color()) { fprintf(stderr, "%sfatal error%s: ", ERROR, RESET); } else { fputs("fatal error: ", stderr); @@ -77,7 +139,7 @@ void warnf(const char *format, ...) va_list args; va_start(args, format); - if (__check()) { + if (err_support_color()) { fprintf(stderr, "%swarning%s: ", WARN, RESET); } else { fputs("warning: ", stderr); @@ -93,7 +155,7 @@ void notef(const char *format, ...) va_list args; va_start(args, format); - if (__check()) { + if (err_support_color()) { fprintf(stderr, "%snote%s: ", NOTE, RESET); } else { fputs("note: ", stderr); @@ -109,7 +171,7 @@ void hintf(const char *format, ...) va_list args; va_start(args, format); - if (__check()) { + if (err_support_color()) { fprintf(stderr, "%shint: ", HINT); } else { fputs("hint: ", stderr); @@ -117,7 +179,7 @@ void hintf(const char *format, ...) vfprintf(stderr, format, args); - if (__check()) { + if (err_support_color()) { fprintf(stderr, "%s\n", RESET); } else { fputc('\n', stderr); @@ -150,3 +212,5 @@ void hintfa(int code) { hintf(strerror(code)); } + +/* end of file err.c */ diff --git a/lib/err.h b/lib/err.h index 6b53e21..09d21c2 100644 --- a/lib/err.h +++ b/lib/err.h @@ -1,24 +1,67 @@ -/* Copyright (C) GCK +/* + * gcklib.err - Contains color printing functions * - * This file is part of gcklib + * CONFIGURATION + * #define NOCOLOR + * Force no color when printing + * #define SHOW_TRACE + * Add debug traces to error printing * - * This project and file is licensed under the BSD-3-Clause licence. - * + * + * 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. */ #ifndef ERR_H #define ERR_H void errorf(const char *format, ...); -void fatalf(const char *format, ...); +_Noreturn void fatalf(const char *format, ...); void notef(const char *format, ...); void warnf(const char *format, ...); void hintf(const char *format, ...); void errorfa(int code); -void fatalfa(int code); +_Noreturn void fatalfa(int code); void notefa(int code); void warnfa(int code); void hintfa(int code); +#if defined(SHOW_TRACE) +#define errorf(fmt, ...) \ + errorf("%s:%s:%d: " fmt, __FILE__, __func__, \ + __LINE__ __VA_OPT__(, ) __VA_ARGS__) +#define fatalf(fmt, ...) \ + fatalf("%s:%s:%d: " fmt, __FILE__, __func__, \ + __LINE__ __VA_OPT__(, ) __VA_ARGS__) #endif + +#endif + +/* end of file err.h */ diff --git a/lib/flag.c b/lib/flag.c index bd4fc09..c62943d 100644 --- a/lib/flag.c +++ b/lib/flag.c @@ -1,9 +1,35 @@ -/* Copyright (C) GCK +/* + * gcklib.flag - Contains command-line flag parsing functions * - * This file is part of gcklib * - * This project and file is licensed under the BSD-3-Clause licence. - * + * 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. */ #include @@ -94,3 +120,5 @@ int getopt_long(int argc, char *const argv[], const char *optstring, } return c; } + +/* end of file flag.c */ diff --git a/lib/flag.h b/lib/flag.h index 74b45f1..81bc8ee 100644 --- a/lib/flag.h +++ b/lib/flag.h @@ -1,9 +1,35 @@ -/* Copyright (C) GCK +/* + * gcklib.flag - Contains command-line flag parsing functions * - * This file is part of gcklib * - * This project and file is licensed under the BSD-3-Clause licence. - * + * 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. */ #ifndef FLAG_H @@ -13,3 +39,5 @@ int getopt_long(int argc, char *const argv[], const char *optstring, const struct option *longopts, int *longindex); #endif + +/* end of file flag.h */ diff --git a/lib/fs.c b/lib/fs.c index c01b602..f961fc2 100644 --- a/lib/fs.c +++ b/lib/fs.c @@ -1,9 +1,41 @@ -/* Copyright (C) GCK +/* + * gcklib.fs - Contains file system related iterations * - * This file is part of gcklib + * CONFIGURATION + * #define FS_ERROR_ON + * Error instead of fatal on failure + * #define FS_FATAL_OFF + * Disable auto fatal on failure * - * This project and file is licensed under the BSD-3-Clause licence. - * + * + * 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. */ #include @@ -13,23 +45,37 @@ #include #include #include -#include "err.h" #include "fs.h" +#include "err.h" +#include "xmem.h" + +#if defined(FS_ERROR_ON) +#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) + if (!fptr) { +#if defined(FS_ERROR_ON) + errorfa(errno); +#elif defined(FS_FATAL_ON) + fatalfa(errno); +#endif return NULL; + } size_t cap = 1024; size_t len = 0; - char *buf = malloc(cap); - if (!buf) { - fclose(fptr); - return NULL; - } + char *buf = xmalloc(cap); int c; while ((c = fgetc(fptr)) != EOF) { @@ -63,6 +109,7 @@ bool fs_exists(const char *path) exists = false; } + fclose(fptr); return exists; } @@ -70,7 +117,7 @@ int fs_append(const char *path, const char *format, ...) { FILE *fp = fopen(path, "a"); if (!fp) - return -1; + RETURN(errno); va_list ap; va_start(ap, format); @@ -79,18 +126,18 @@ int fs_append(const char *path, const char *format, ...) if (ret < 0) { fclose(fp); - return -1; + RETURN(errno); } if (fclose(fp) != 0) - return -1; + RETURN(errno); return ret; } int fs_del(const char *path) { - return remove(path); + RETURN(remove(path)); } int fs_new(const char *path) @@ -100,22 +147,22 @@ int fs_new(const char *path) if (path == NULL) { errno = EINVAL; - return -1; + RETURN(-1); } len = strlen(path); if (len == 0) { errno = EINVAL; - return -1; + RETURN(-1); } if (path[len - 1] == '/') { if (mkdir(path, 0777) == -1) - return -1; + RETURN(-1); } else { fd = open(path, O_CREAT | O_EXCL | O_WRONLY, 0666); if (fd == -1) - return -1; + RETURN(-1); close(fd); } @@ -126,7 +173,7 @@ int fs_write(const char *path, const char *format, ...) { FILE *fptr = fopen(path, "w"); if (!fptr) - return -1; + RETURN(-1); va_list ap; va_start(ap, format); @@ -135,11 +182,11 @@ int fs_write(const char *path, const char *format, ...) if (ret < 0) { fclose(fptr); - return -1; + RETURN(-1); } if (fclose(fptr) != 0) - return -1; + RETURN(-1); return ret; } @@ -149,9 +196,15 @@ FILE *fs_temp() FILE *fptr = tmpfile(); if (!fptr) { - errorf("tmpfile failed"); +#if defined(FS_ERROR_ON) + errorf("tmp failed"); +#elif defined(FS_FATAL_ON) + fatalf("tmp failed"); +#endif return NULL; } return fptr; } + +/* end of file fs.c */ diff --git a/lib/fs.h b/lib/fs.h index 35019e5..e68cdb1 100644 --- a/lib/fs.h +++ b/lib/fs.h @@ -1,9 +1,35 @@ -/* Copyright (C) GCK +/* + * gcklib.fs - Contains file system related iterations * - * This file is part of gcklib * - * This project and file is licensed under the BSD-3-Clause licence. - * + * 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. */ #ifndef fs_H @@ -24,3 +50,5 @@ int fs_write(const char *path, const char *format, ...); FILE *fs_temp(); #endif + +/* end of file fs.h */ diff --git a/lib/proginfo.c b/lib/proginfo.c index dca283c..4b48469 100644 --- a/lib/proginfo.c +++ b/lib/proginfo.c @@ -1,9 +1,35 @@ -/* Copyright (C) GCK +/* + * gcklib.proginfo - Program information functions, macros, and variables * - * This file is part of gcklib * - * This project and file is licensed under the BSD-3-Clause licence. - * + * 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. */ #include @@ -14,6 +40,7 @@ #include "proginfo.h" +/* TODO(vx-clutch): default this to argv[0] */ const char *prog_name = ""; void set_prog_name(char *name) @@ -51,3 +78,5 @@ int parse_standard_options(int argc, char **argv, void (*print_help)(), } return 0; } + +/* end of file proginfo.c */ diff --git a/lib/proginfo.h b/lib/proginfo.h index d2a79f4..371990f 100644 --- a/lib/proginfo.h +++ b/lib/proginfo.h @@ -1,9 +1,35 @@ -/* Copyright (C) GCK +/* + * gcklib.proginfo - Program information functions, macros, and variables * - * This file is part of gcklib * - * This project and file is licensed under the BSD-3-Clause licence. - * + * 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. */ #ifndef proginfo_H @@ -20,3 +46,5 @@ int parse_standard_options(int argc, char **argv, void (*print_help)(), void (*print_version)()); #endif + +/* end of file proginfo.h */ diff --git a/lib/say.c b/lib/say.c new file mode 100644 index 0000000..f5d3139 --- /dev/null +++ b/lib/say.c @@ -0,0 +1,105 @@ +/* + * gcklib.say - Provides printing and some string utilies + * + * + * 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. + */ + +#include "say.h" + +#include +#include +#include +#include +#include +#include + +#include "xmem.h" + +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 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; + } + + 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); + + fflush(stdout); // ensure it's visible immediately + printf("\0338"); // restore cursor (ESC 8) + fflush(stdout); + + return ret; +} + +_Noreturn void die(const char *msg) +{ + fputs(msg, stderr); + exit(EXIT_FAILURE); +} + +/* end of file say.c */ diff --git a/lib/say.h b/lib/say.h new file mode 100644 index 0000000..9907b6d --- /dev/null +++ b/lib/say.h @@ -0,0 +1,50 @@ +/* + * gcklib.say - Provides printing and some string utilies + * + * + * 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. + */ + +#ifndef SAY_H +#define SAY_H + +#include + +int asprintf(char **buf, const char *fmt, ...); +int vasprintf(char **buf, const char *fmt, va_list ap); +int say(const char *restrict format, ...); + +void alert(); + +_Noreturn void die(const char *msg); + +#endif + +/* end of file say.h */ diff --git a/lib/textc.c b/lib/textc.c index 575ae97..920f503 100644 --- a/lib/textc.c +++ b/lib/textc.c @@ -1,3 +1,37 @@ +/* + * gcklib.textc - Text control + * + * + * 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. + */ + #include #include #include "xmem.h" @@ -26,3 +60,20 @@ char *tostrlwr(char *s) new[i] = tolower((unsigned char)new[i]); return new; } + +char *textc_trim(char *s) +{ + return NULL; +} + +char *textc_pad_left(int count, char *s, char pad) +{ + char *buffer = xmalloc(strlen(s) + 1); + + free(buffer); + buffer = "NOT IMPLEMENTED"; + + return buffer; +} + +/* end of file textc.c */ diff --git a/lib/textc.h b/lib/textc.h index b9bc131..fee7559 100644 --- a/lib/textc.h +++ b/lib/textc.h @@ -1,3 +1,37 @@ +/* + * gcklib.textc - Text control + * + * + * 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. + */ + #ifndef TEXTC_H #define TEXTC_H @@ -5,4 +39,10 @@ char *str_dup(char *s); char *tostrupr(char *s); char *tostrlwr(char *s); +char *textc_trim(char *s); +char *textc_pad_left(int count, char *s, char pad); +char *textc_pad_right(int count, char *s, char pad); + #endif + +/* end of file textc.h */ diff --git a/lib/xmem.c b/lib/xmem.c index dac8abe..ed80631 100644 --- a/lib/xmem.c +++ b/lib/xmem.c @@ -1,9 +1,35 @@ -/* Copyright (C) GCK +/* + * gcklib.xmem - Xplosive memory * - * This file is part of gcklib * - * This project and file is licensed under the BSD-3-Clause licence. - * + * 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. */ #include @@ -34,3 +60,5 @@ void *xcalloc(size_t nmemb, size_t size) { return ensure_nonnull(calloc(nmemb, size)); } + +/* end of file xmem.c */ diff --git a/lib/xmem.h b/lib/xmem.h index 2d4ccc3..585078a 100644 --- a/lib/xmem.h +++ b/lib/xmem.h @@ -1,9 +1,35 @@ -/* Copyright (C) GCK +/* + * gcklib.xmem - Xplosive memory * - * This file is part of gcklib * - * This project and file is licensed under the BSD-3-Clause licence. - * + * 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. */ #ifndef xmem_H @@ -16,3 +42,5 @@ void *xrealloc(void *ptr, size_t size); void *xcalloc(size_t nmemb, size_t size); #endif + +/* end of file xmem.h */ diff --git a/src/yait.c b/src/yait.c index e4a4569..8bda3ad 100644 --- a/src/yait.c +++ b/src/yait.c @@ -56,12 +56,14 @@ #include #include +#define SHOW_TRACE + #include "licence.h" #include "../lib/err.h" #include "../lib/fs.h" -#include "../lib/xmem.h" #include "../lib/proginfo.h" #include "../lib/textc.h" +#include "../lib/say.h" typedef enum { MIT, GPL, BSD, UNL } licence_t; @@ -268,14 +270,15 @@ done\n\ return exit_status; } - size_t len = strlen(package); - char *pdir = xmalloc(len + 2); - memcpy(pdir, package, len); - pdir[len] = '/'; - pdir[len + 1] = '\0'; + // size_t len = strlen(package); + // char *pdir = xmalloc(len + 2); + // memcpy(pdir, package, len); + // pdir[len] = '/'; + // pdir[len + 1] = '\0'; + char *pdir; + asprintf(&pdir, "%s/", package); - if (fs_new(pdir) == -1 && errno != EEXIST) - fatalfa(errno); + fs_new(pdir); if (chdir(pdir)) fatalfa(errno); @@ -287,9 +290,10 @@ done\n\ ", "1 January 1970", "January 2025"); - char path[BUFSIZ]; - snprintf(path, sizeof(path), "doc/%s.texi", package); - fs_write(path, "\ + char *texi_buffer; + // snprintf(path_buffer, sizeof(path_buffer), "doc/%s.texi", package); + 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\ @@ -460,10 +464,13 @@ a @file{ChangeLog} entry.\n\ ", author, author, author, author, author, author, author, author, author, author, author, author, author, author); + free(texi_buffer); // TODO(vx-clutch): Why dosn't this write the source? - snprintf(path, sizeof(path), "src/%s.c", package); - fs_write(path, "\ + // snprintf(path_buffer, sizeof(path_buffer), "src/%s.c", package); + char *src_path; + asprintf(&src_path, "src/%s.c", package); + fs_write(src_path, "\ /* Copyright (C) %s\n\ *\n\ * This file is part of %s\n\ @@ -525,6 +532,7 @@ void print_version()\n\ }\ ", author, package, package, package, author); + free(src_path); fs_write("tools/Cleanup", "\ #!/bin/sh\n\