diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..85ac7e7 --- /dev/null +++ b/Makefile @@ -0,0 +1,5 @@ +all: + @clang -g -lncurses -I include src/*.c main.c -o gen-flake + +bear: + @bear -- clang -g -lncurses -I include src/*.c main.c -o gen-flake diff --git a/flake.nix b/flake.nix index 27653ce..c281858 100644 --- a/flake.nix +++ b/flake.nix @@ -14,8 +14,7 @@ pkgs = nixpkgs.legacyPackages.${system}; codelldb = pkgs.writeShellScriptBin "codelldb" '' - #!/usr/bin/env bash - exec ${pkgs.vscode-extensions.vadimcn.vscode-lldb}/share/vscode/extensions/vadimcn.vscode-lldb/adapter/codelldb "$@" + exec ${pkgs.vscode-extensions.vadimcn.vscode-lldb}/share/vscode/extensions/vadimcn.vscode-lldb/adapter/codelldb "$@" ''; in { @@ -31,6 +30,9 @@ buildInputs = with pkgs; [ ncurses ]; + shellHook = '' + export GEN_FLAKE_SPEC_PATH="${./specs}" + ''; }; } ); diff --git a/gen-flake b/gen-flake new file mode 100755 index 0000000..11e4692 Binary files /dev/null and b/gen-flake differ diff --git a/include/dyn_array.h b/include/dyn_array.h new file mode 100644 index 0000000..e5770ee --- /dev/null +++ b/include/dyn_array.h @@ -0,0 +1,16 @@ +#ifndef GEN_FLAKE_DYN_ARRAY_H +#define GEN_FLAKE_DYN_ARRAY_H + +#include + +struct DynArray; + +struct DynArray *dyn_array_new(size_t capacity); + +size_t dyn_array_size(struct DynArray *a); + +void dyn_array_push(struct DynArray *a, void *item); + +void dyn_array_free(struct DynArray *a); + +#endif /* GEN_FLAKE_DYN_ARRAY_H */ diff --git a/main.c b/main.c index 1f81122..697e4e0 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,67 @@ -#include +#include #include +#include +#include +#include +#include +#include +#include + +#include "dyn_array.h" + +static int glob_specs(char *name, struct DynArray *entries) { + assert(name); + assert(entries); + + int name_length = strlen(name); + char *pattern = malloc(name_length + 3); + if (!pattern) { + goto cleanup; + } + memcpy(pattern, name, name_length); + memcpy(pattern + name_length + 1, "/*", 3); + + glob_t *pglob = 0; + + int retval = 0; + if ((retval = glob(pattern, GLOB_ONLYDIR | GLOB_MARK, 0, pglob))) { + goto cleanup; + } + + // GLOB_ONLYDIR is just a hint. We still need to actually verify the only + // results we are reading in are directories. + // TODO: + + globfree(pglob); + +cleanup: + if (pattern) { + free(pattern); + } + return retval; +} + +static void cleanup(int sig) { + endwin(); + + exit(EXIT_SUCCESS); +} int main(int argc, char **argv) { + int num = 0; - return EXIT_SUCCESS; + if (argc != 2) { + fprintf(stderr, "Usage: gen-flake \n"); + exit(EXIT_FAILURE); + } + + // Allow interrupting the program cleanly. + // TODO: How does this cleanup spec correctly? + signal(SIGINT, cleanup); + + initscr(); + keypad(stdscr, TRUE); // Enable keyboard mapping. + nonl(); // Disables NL to CR/NL conversion on output. + + cleanup(0); } diff --git a/specs/clang/default/flake.nix b/specs/clang/default/flake.nix new file mode 100644 index 0000000..0b6771f --- /dev/null +++ b/specs/clang/default/flake.nix @@ -0,0 +1,36 @@ +{ + description = '' + An opinionated clang flake. + ''; + + inputs = { + flake-utils.url = "github:numtide/flake-utils"; + nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable"; + }; + + outputs = { self, nixpkgs, flake-utils }: + flake-utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + + codelldb = pkgs.writeShellScriptBin "codelldb" '' + #!/usr/bin/env bash + exec ${pkgs.vscode-extensions.vadimcn.vscode-lldb}/share/vscode/extensions/vadimcn.vscode-lldb/adapter/codelldb "$@" + ''; + in + { + devShells.default = pkgs.mkShell.override { + # https://nixos.wiki/wiki/Using_Clang_instead_of_GCC + stdenv = pkgs.clangStdenv; + } { + packages = with pkgs; [ + bear + clang-tools + codelldb + ]; + buildInputs = with pkgs; [ + ncurses + ]; + }; + }); +} diff --git a/specs/clang/run.sh b/specs/clang/run.sh new file mode 100644 index 0000000..fcdce00 --- /dev/null +++ b/specs/clang/run.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash + +if [ -z "$VERSION" ]; then + VERSION="default" +fi + +cp "$VERSION/flake.nix" "$OUT" diff --git a/src/dyn_array.c b/src/dyn_array.c new file mode 100644 index 0000000..38593ea --- /dev/null +++ b/src/dyn_array.c @@ -0,0 +1,44 @@ +#include + +#include "dyn_array.h" + +struct DynArray { + void **buf; + // The size of @buf excluding `NUL`. + size_t size; + // The allocated size of @buf including `NUL`. + size_t _capacity; +}; + +struct DynArray *dyn_array_new(size_t capacity) { + struct DynArray *a = malloc(sizeof(struct DynArray)); + a->buf = calloc(capacity, sizeof(void *)); + a->size = 0; + a->_capacity = capacity; + return a; +} + +size_t dyn_array_size(struct DynArray *a) { + assert(a); + + return a->size; +} + +void dyn_array_push(struct DynArray *a, void *item) { + assert(a); + + if (a->size == a->_capacity) { + // We assume reallocation will work. + a->_capacity *= 2; + a = realloc(a, sizeof(void *) * a->_capacity); + } + a->size += 1; + a->buf[a->size] = item; +} + +void dyn_array_free(struct DynArray *a) { + assert(a); + + free(a->buf); + free(a); +}