This commit is contained in:
2025-10-22 19:22:43 -04:00
parent e684ef152d
commit 1f6130c7a5
16 changed files with 711 additions and 100 deletions

2
gcklib

Submodule gcklib updated: c12cd326f4...c47af845bd

122
lib/err.c
View File

@@ -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.
* <https://opensource.org/licenses/BSD-3-Clause>
*
* 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 <stdio.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#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 */

View File

@@ -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.
* <https://opensource.org/licence/bsd-3-clause>
*
* 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 */

View File

@@ -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.
* <https://opensource.org/licence/bsd-3-clause>
* 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 <getopt.h>
@@ -94,3 +120,5 @@ int getopt_long(int argc, char *const argv[], const char *optstring,
}
return c;
}
/* end of file flag.c */

View File

@@ -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.
* <https://opensource.org/licence/bsd-3-clause>
* 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 */

View File

@@ -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.
* <https://opensource.org/licence/bsd-3-clause>
*
* 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 <errno.h>
@@ -13,23 +45,37 @@
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#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 */

View File

@@ -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.
* <https://opensource.org/licence/bsd-3-clause>
* 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 */

View File

@@ -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.
* <https://opensource.org/licence/bsd-3-clause>
* 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 <stdio.h>
@@ -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 */

View File

@@ -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.
* <https://opensource.org/licence/bsd-3-clause>
* 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 */

105
lib/say.c Normal file
View File

@@ -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 <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#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 */

50
lib/say.h Normal file
View File

@@ -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 <stdarg.h>
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 */

View File

@@ -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 <string.h>
#include <ctype.h>
#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 */

View File

@@ -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 */

View File

@@ -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.
* <https://opensource.org/licence/bsd-3-clause>
* 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 <stdio.h>
@@ -34,3 +60,5 @@ void *xcalloc(size_t nmemb, size_t size)
{
return ensure_nonnull(calloc(nmemb, size));
}
/* end of file xmem.c */

View File

@@ -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.
* <https://opensource.org/licence/bsd-3-clause>
* 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 */

View File

@@ -56,12 +56,14 @@
#include <stdbool.h>
#include <time.h>
#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\