diff options
Diffstat (limited to 'snag')
| -rw-r--r-- | snag/.gitignore | 6 | ||||
| -rwxr-xr-x | snag/gen.sh | 57 | ||||
| -rw-r--r-- | snag/main.c | 8 | ||||
| -rw-r--r-- | snag/pkg_db.c | 30 | ||||
| -rw-r--r-- | snag/util.c | 31 | ||||
| -rw-r--r-- | snag/util.h | 15 |
6 files changed, 113 insertions, 34 deletions
diff --git a/snag/.gitignore b/snag/.gitignore index 5f2c517..9a9d059 100644 --- a/snag/.gitignore +++ b/snag/.gitignore @@ -1,2 +1,6 @@ *.o -snag
\ No newline at end of file +snag +compile_flags.txt +TAGS +build.ninja +objs/
\ No newline at end of file diff --git a/snag/gen.sh b/snag/gen.sh new file mode 100755 index 0000000..3e420f1 --- /dev/null +++ b/snag/gen.sh @@ -0,0 +1,57 @@ +#!/bin/bash + +bin="snag" +libs="" +builddir="objs" +PREFIX="/usr" + +while [ "$1" ]; do + case "$1" in + -*) exit 1 ;; + esac + shift +done + +CFLAGS=${CFLAGS:-""} + +CFLAGS_RELEASE="$CFLAGS -O2" +CFLAGS_DEBUG="$CFLAGS -Wall -g -DDEBUG=1" + +write() { + echo "$@" >> build.ninja +} + +echo "$CFLAGS_DEBUG $(pkg-config --cflags $libs 2>/dev/null)" | tr ' ' '\n' > compile_flags.txt +rm -f build.ninja + +write "builddir = $builddir" +write "libs = $(pkg-config --libs $libs 2>/dev/null)" + +write 'rule link' +write ' description = LD $out' +write ' command = gcc $libs $in -o $out' +for target in $(echo "debug release"); do + eval write "cflags_$target = \$CFLAGS_${target^^} $(pkg-config --cflags $libs 2>/dev/null)" + + write "rule cc-$target" + write ' deps = gcc' + write ' depfile = $out.d' + write ' description = CC $out' + write " command = gcc -MD -MF \$out.d \$cflags_$target -c \$in -o \$out" + + + files="$(find . -name '*.c')" + sedstr=$(printf 's,^\./\(.*\)\.c$,%s\/%s\/\\1.o,g' ${builddir} ${target}) + ofiles=$(echo "$files" | sed ${sedstr}) + + for f in $files; do + of=$(echo $f | sed ${sedstr}) + write "build $of: cc-$target $f" + done + write "build $builddir/$target/$bin: link $(echo $ofiles)" + write "build $target: phony $builddir/$target/$bin" +done + +write "default release" + +echo "Wrote ./build.ninja file" diff --git a/snag/main.c b/snag/main.c index 4f6aa44..0429fda 100644 --- a/snag/main.c +++ b/snag/main.c @@ -10,22 +10,22 @@ int main(int argc, char* argv[]) { // create argument parser ArgParser* parser = ap_new_parser(); - if (parser == NULL) error_out("failed to initialize cli parsing"); + if (parser == NULL) die("failed to initialize cli parsing"); ap_set_helptext(parser, "Usage: snag <command>"); ap_set_version(parser, "0.0"); // add install subcommand ArgParser* install = ap_new_cmd(parser, "install inst i"); - if (install == NULL) error_out("failed to initialize install cli subcommand"); + if (install == NULL) die("failed to initialize install cli subcommand"); ap_set_helptext(install, "Usage: snag install [packages]"); ap_set_cmd_callback(install, cmd_install); // parse the arguments (automatically executes subcommands) - if (!ap_parse(parser, argc, argv)) error_out("failed to parse arguments"); + if (!ap_parse(parser, argc, argv)) die("failed to parse arguments"); // if no subcommand was specified, parse and execute install as the root command if (!ap_found_cmd(parser)) { - if (!ap_parse(install, argc, argv)) error_out("failed to parse arguments"); + if (!ap_parse(install, argc, argv)) die("failed to parse arguments"); cmd_install("install", install); } diff --git a/snag/pkg_db.c b/snag/pkg_db.c index 3ed36cf..2bfa686 100644 --- a/snag/pkg_db.c +++ b/snag/pkg_db.c @@ -12,7 +12,7 @@ #define PKG_INST_CMD "./run_pkg_install.sh" // note: fixed location of package database for now -const char pkg_db[] = "../packages"; +const char pkg_db[] = "../packages"; const size_t pkg_db_len = sizeof(pkg_db) - 1; bool load_package_info(char* pkgid, package_info_t* info) { @@ -30,7 +30,7 @@ bool load_package_info(char* pkgid, package_info_t* info) { memcpy(pkg_dir + pkg_db_len + 1, pkgid, pkgid_len); pkg_dir[pkg_dir_len] = '\0'; if (access(pkg_dir, F_OK) != 0) { - eprintf("Package '%s' not found\n", pkgid); + log("Package '%s' not found\n", pkgid); free(pkg_dir); return false; } @@ -48,7 +48,7 @@ bool load_package_info(char* pkgid, package_info_t* info) { info->script_path[script_path_len] = '\0'; free(pkg_dir); if (access(info->script_path, F_OK) != 0) { - eprintf("Package '%s' broken, doesn't have description script\n", pkgid); + log("Package '%s' broken, doesn't have description script\n", pkgid); free(info->script_path); return false; } @@ -56,9 +56,9 @@ bool load_package_info(char* pkgid, package_info_t* info) { // PIPE and FORK to run pkg attrs script int pipefds[2] = {0}; - if (pipe(pipefds) < 0) error_out("failed to pipe: %s", strerror(errno)); + if (pipe(pipefds) < 0) die("failed to pipe: %s", strerror(errno)); pid_t pid = fork(); - if (pid < 0) error_out("failed to fork: %s", strerror(errno)); + if (pid < 0) die("failed to fork: %s", strerror(errno)); if (pid == 0) { // child proc @@ -71,7 +71,7 @@ bool load_package_info(char* pkgid, package_info_t* info) { char* envp[] = { NULL }; execve(PKG_INFO_CMD, argv, envp); // what are you still doing here? - error_out("execve failed"); + die("execve failed"); } else { // parent proc, close write end of pipe close(pipefds[1]); @@ -79,12 +79,12 @@ bool load_package_info(char* pkgid, package_info_t* info) { // wait for child to finish int wstatus; - if (waitpid(pid, &wstatus, 0) < 0) error_out("waitpid failed: %s", strerror(errno)); - if (WEXITSTATUS(wstatus) != 0) error_out("failed to execute get pkg attr script"); + if (waitpid(pid, &wstatus, 0) < 0) die("waitpid failed: %s", strerror(errno)); + if (WEXITSTATUS(wstatus) != 0) die("failed to execute get pkg attr script"); // read all bytes from pipe into attr buffer ssize_t bytes_read = read(pipefds[0], info->attrs.buffer, sizeof(info->attrs.buffer)); - if (bytes_read < 0) error_out("failed to read from pipe: %s", strerror(errno)); + if (bytes_read < 0) die("failed to read from pipe: %s", strerror(errno)); close(pipefds[0]); // read attributes from attr buffer @@ -118,9 +118,9 @@ void run_package_install(const package_info_t* info) { // PIPE and FORK to run install script int pipefds[2] = {0}; - if (pipe(pipefds) < 0) error_out("failed to pipe: %s", strerror(errno)); + if (pipe(pipefds) < 0) die("failed to pipe: %s", strerror(errno)); pid_t pid = fork(); - if (pid < 0) error_out("failed to fork: %s", strerror(errno)); + if (pid < 0) die("failed to fork: %s", strerror(errno)); if (pid == 0) { // child proc @@ -134,7 +134,7 @@ void run_package_install(const package_info_t* info) { char* envp[] = { NULL }; execve(PKG_INST_CMD, argv, envp); // what are you still doing here? - error_out("execve failed"); + die("execve failed"); } else { // parent proc, close write end of pipe close(pipefds[1]); @@ -147,11 +147,11 @@ void run_package_install(const package_info_t* info) { // print build out, can log and stuff in future printf("%.*s", (int)bytes_read, buf); } - if (bytes_read < 0) error_out("failed to read from pipe: %s", strerror(errno)); + if (bytes_read < 0) die("failed to read from pipe: %s", strerror(errno)); close(pipefds[0]); // reap the child to finish int wstatus; - if (waitpid(pid, &wstatus, 0) < 0) error_out("waitpid failed: %s", strerror(errno)); - if (WEXITSTATUS(wstatus) != 0) error_out("failed to execute install script"); + if (waitpid(pid, &wstatus, 0) < 0) die("waitpid failed: %s", strerror(errno)); + if (WEXITSTATUS(wstatus) != 0) die("failed to execute install script"); } diff --git a/snag/util.c b/snag/util.c index d8947a7..57a31a4 100644 --- a/snag/util.c +++ b/snag/util.c @@ -4,26 +4,35 @@ #include "util.h" -void eprintf(const char *format, ...) { - // send that shi to stderr +#if DEBUG +void _log(int line, const char* file, const char* format, ...) { + fprintf(stderr, "%s:%d: info: ", file, line); +#else +void _log(const char* format, ...) { +#endif va_list args; va_start(args, format); vfprintf(stderr, format, args); va_end(args); } -void error_out(const char *format, ...) { +#if DEBUG +void _die(int line, const char* file, const char* format, ...) { + fprintf(stderr, "%s:%d: error: ", file, line); +#else +void _die(const char* format, ...) { +#endif va_list args; va_start(args, format); vfprintf(stderr, format, args); va_end(args); - fprintf(stderr, "\n"); - exit(1); + exit(1); } + void* emalloc(size_t size, char* alloc_reason) { void* d = malloc(size); - if (d == NULL) error_out("Failed to allocate %s", alloc_reason); + if (d == NULL) die("Failed to allocate %s", alloc_reason); return d; } @@ -36,19 +45,17 @@ char* vastrcat_(int dummy, ...) { // get len of output string size_t len = 0; - while (1) { - char* arg = va_arg(args, char*); - if (!arg) break; + char* arg = NULL; + while ((arg = va_arg(args, char*))) { len += strlen(arg); } va_end(args); // allocate and build output char* output = malloc(len + 1); + if (!output) return NULL; char* p = output; - while (1) { - char* arg = va_arg(args2, char*); - if (!arg) break; + while ((arg = va_arg(args, char*))) { memcpy(p, arg, strlen(arg)); p += strlen(arg); } diff --git a/snag/util.h b/snag/util.h index 6f23607..fbf9018 100644 --- a/snag/util.h +++ b/snag/util.h @@ -5,8 +5,19 @@ #include <stddef.h> // utility for errors -void eprintf(const char *format, ...) __attribute__((format(printf, 1, 2))); -void error_out(const char *format, ...) __attribute__((format(printf, 1, 2))); // panic, as the kids say +#if DEBUG + #define log(fmt, ...) _log(__LINE__, __FILE__, fmt, ##__VA_ARGS__) + void _log(int line, const char* file, const char* format, ...) __attribute__((format(printf, 3, 4))); + + #define die(fmt, ...) _die(__LINE__, __FILE__, fmt, ##__VA_ARGS__) + void _die(int line, const char* file, const char* format, ...) __attribute__((format(printf, 3, 4))); +#else + #define log(fmt, ...) _log(fmt, ##__VA_ARGS__) + void _log(const char* format, ...) __attribute__((format(printf, 1, 2))); + + #define die(fmt, ...) _die(fmt, ##__VA_ARGS__) + void _die(const char* format, ...) __attribute__((format(printf, 1, 2))); +#endif void* emalloc(size_t size, char* alloc_reason); // string functions |
