From b7b31abc0ea31670389838354a9bb3cc4bcf444c Mon Sep 17 00:00:00 2001 From: Liam Waldron Date: Thu, 8 Feb 2024 09:01:44 -0500 Subject: [PATCH] init --- Makefile | 19 +++++ README | 15 ++++ config.mk | 8 ++ etc/example.cfg | 34 ++++++++ include/glacier_config.h | 68 ++++++++++++++++ include/glacier_log.h | 78 ++++++++++++++++++ include/glacier_runtime.h | 24 ++++++ libglacier.c | 165 ++++++++++++++++++++++++++++++++++++++ lua.c | 26 ++++++ 9 files changed, 437 insertions(+) create mode 100644 Makefile create mode 100644 README create mode 100644 config.mk create mode 100644 etc/example.cfg create mode 100644 include/glacier_config.h create mode 100644 include/glacier_log.h create mode 100644 include/glacier_runtime.h create mode 100644 libglacier.c create mode 100644 lua.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..9d3db7b --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +include config.mk + +all: + @echo "Run 'make test' for the test binary, or 'make lib' to generate a static library." + @echo "Run 'make help' for a full list of options." + +help: + @echo "Make options for libglacier:" + @echo "" + @echo "lib - generate the static library" + @echo "test - generate a test binary" + @echo "clean - remove all generated files" + +test: + $(CC) libglacier.c $(LIBFLAGS) -o libglacier.test + cp etc/example.cfg ./glacier.cfg + +clean: + rm libglacier.test libglacier.a glacier.cfg diff --git a/README b/README new file mode 100644 index 0000000..c14eec0 --- /dev/null +++ b/README @@ -0,0 +1,15 @@ ++ libglacier + +The underlying C libraries for the Glacier package manager + ++ Rationale + +Glacier has been written in Bash since its conception. For obvious reasons, this is terrible for portability. +While writing an entire library for a project that's been in the works for a year at this point will take +much more time, it should futureproof Glacier and make it easier to deploy and test. + ++ Usage + + #include + #include + diff --git a/config.mk b/config.mk new file mode 100644 index 0000000..3e9c9ce --- /dev/null +++ b/config.mk @@ -0,0 +1,8 @@ +# +# config.mk +# + +CC = /bin/gcc +AR = /bin/ar + +LIBFLAGS = -lconfig diff --git a/etc/example.cfg b/etc/example.cfg new file mode 100644 index 0000000..ed1d6a5 --- /dev/null +++ b/etc/example.cfg @@ -0,0 +1,34 @@ +# +# glacier.cfg +# The settings defined in this file will be loaded when Glacier is called. +# For more information on this file, see https://www.everestlinux.org/docs/intro-to-glacier +# + +# Services +# Whether Glacier will allow external service files to be called +# WARNING: Services may pose a security risk to your system. Only use services you trust. +GLACIER_ALLOW_SERVICES = false; + +# Permitted software licenses +# When installing a package, Glacier will check its license. If said license is listed here, +# it will proceed with the installation. Otherwise, it'll return an error. +# For more information on licenses listed below, see https://www.everestlinux.org/docs/intro-to-glacier +# Some common license names with their designation codes are: +# - GNU General Public License (GPL) +# - MIT License (MIT) +# - BSD License (BSD) +# - Apache license (APACHE) +# - Binary-redistributable software (BIN-REDIST) +# - End-user-license-agreement (EULA) +# If, for any reason, a package uses a custom license, you can simply add it to this list. +GLACIER_ALLOWED_LICENSES = [ "GPL", "MIT", "BSD", "APACHE" ] + +# Profile-specific settings +# WARNING: The settings below are tied into the system's profile. Changing any of these manually +# will certainly cause breakage. +# If, for any reason, you must change any of these settings, use `glacier-mkprofile`. +# Profile migration may cause breakage. Doing a clean install is the preferred method for changing profiles. + +### DO NOT EDIT ANY SETTINGS BELOW THIS LINE ### +### DO NOT EDIT ANY SETTINGS BELOW THIS LINE ### +### DO NOT EDIT ANY SETTINGS BELOW THIS LINE ### diff --git a/include/glacier_config.h b/include/glacier_config.h new file mode 100644 index 0000000..fe493ea --- /dev/null +++ b/include/glacier_config.h @@ -0,0 +1,68 @@ +/* + * glacier.h - Function declarations for libglacier + * + * This file is part of Glacier. + * + * Glacier is free software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * Glacier is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Glacier. If + * not, see . + */ + +#ifndef GLACIERCONFIG_H_ +#define GLACIERCONFIG_H_ + +/* + * init_config + * DESCRIPTION: Init_config initializes the libconfig library, so it can read the required runtime files + * PARAMETERS: + * None. + * RETURN VAUES: + * 0 on success, 1 on failure + * CAVEATS: + * None. + * EXAMPLE: + * init_config(); + */ + +int init_config(); + +/**************************************************************************************************************/ + +/* + * die_config + * DESCRPTION: Die_config destroys the loaded libconfig library. + * PARAMETERS: + * None. + * RETURN VALUES: + * 0 on success, 1 on failure + * CAVEATS: + * None. + * EXAMPLE: + * die_config(); + */ + +int die_config(); + +/**************************************************************************************************************/ + +/* + * load_setting_from_config + * DESCRIPTION: Initialize all settings from glacier.cfg. + * PARAMETERS: + * None. + * RETURN VALUES: + * None. + * EXAMPLE: + * load_setting_from_config(); + */ + +void load_setting_from_config(); + +#endif diff --git a/include/glacier_log.h b/include/glacier_log.h new file mode 100644 index 0000000..47eccad --- /dev/null +++ b/include/glacier_log.h @@ -0,0 +1,78 @@ +/* + * glacier_log.h - Logging function declarations for libglacier + * + * This file is part of Glacier. + * + * Glacier is free software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * Glacier is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Glacier. If + * not, see . + */ + +#ifndef GLACIERLOG_H_ +#define GLACIERLOG_H_ + +/* + * infolog + * + * DESCRIPTION: Infolog outputs a stylized info message. It follows Glacier's uniform CLI style. + * PARAMETERS: + * char MSG[] -> The message to output + * RETURN VALUES: + * None. + * CAVEATS: + * * Cannot output variables. If you must output variables, use printf instead. + * * A NEWLINE ('\n') character is implied, therefore putting one at the end of + * a string is not needed. + * EXAMPLE: + * infolog("This is an info message."); + */ + +void infolog(char MSG[]); + +/**************************************************************************************************************/ + +/* + * warnlog + * + * DESCRIPTION: Warnlog outputs a stylized warning message. It follows Glacier's uniform CLI style. + * PARAMETERS: + * char MSG[] -> The message to output + * RETURN VALUES: + * None. + * CAVEATS: + * * Cannot output variables. If you must output variables, use printf instead. + * * A NEWLINE ('\n') character is implied, therefore putting one at the end of + * a string is not needed. + * EXAMPLE: + * warnlog("This is a warning message."); + */ + +void warnlog(char MSG[]); + +/**************************************************************************************************************/ + +/* + * errlog + * DESCRIPTION: Errlog outputs a stylized error message. It follows Glacier's uniform CLI style. + * PARAMETERS: + * char MSG[] -> The message to output + * RETURN VALUES: + * None. + * CAVEATS: + * * Cannot output variables. If you must output variables, use printf instead. + * * A NEWLINE ('\n') character is implied, therefore putting one at the end of + * a string is not needed. + * EXAMPLE: + * errlog("This is an error message."); + */ + +void errlog(char MSG[]); + +#endif diff --git a/include/glacier_runtime.h b/include/glacier_runtime.h new file mode 100644 index 0000000..c9d731f --- /dev/null +++ b/include/glacier_runtime.h @@ -0,0 +1,24 @@ +/* + * glacier_runtime.h - Runtime function declarations for libglacier + * + * This file is part of Glacier. + * + * Glacier is free software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * Glacier is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Glacier. If + * not, see . + */ + +#ifndef GLACIERRUNTIME_H_ +#define GLACIERRUNTIME_H_ + +/* + * runtime_exists + * + * DESCRIPTION: runtime_exists checks if all necessary runtime files exist. diff --git a/libglacier.c b/libglacier.c new file mode 100644 index 0000000..71172d6 --- /dev/null +++ b/libglacier.c @@ -0,0 +1,165 @@ +/* + * libglacier.c - Backend C library for Glacier + * + * This file is part of Glacier. + * + * Glacier is free software: you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * Glacier is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Glacier. If + * not, see . + */ + +#include +#include +#include +#include +#include +#include + +/* Global variables */ + +config_t cfg; +config_setting_t *setting; +const char str; +int GLACIER_ALLOW_SERVICES; +char GLACIER_ALLOWED_LICENSES; + +const char *runtime_files[] = { "/etc/glacier.cfg", "/etc/glacier/call-hooks", "/etc/make.conf" }; + +/* + * infolog + * + * DESCRIPTION: Output a stylized info message. + * PARAMETERS: char MSG[] + * + */ + +void +infolog(char MSG[]) +{ + if (MSG == NULL) { + return; + } + + printf(COL_BLUE "[i]" COL_RESET " %s\n", MSG); + return; +} + +/* + * warnlog + * + * DESCRIPTION: Output a stylized warning message. + * Parameters: char MSG[] + * + */ + +void +warnlog(char MSG[]) +{ + if (MSG == NULL) { + return; + } + + printf(COL_YELLOW "[!]" COL_RESET " %s\n", MSG); + return; +} + +/* + * errlog + * + * DESCRIPTION: Output a stylized error message. + * PARAMETERS: char MSG[] + * + */ + +void +errlog(char MSG[]) +{ + if (MSG == NULL) { + return; + } + + fprintf(stderr, COL_RED "[x]" COL_RESET " %s\n", MSG); + return; +} + +/* + * runtime_exists + * + * DESCRIPTION: Check if necesary runtime files exist. + * PARAMETERS: None. + * + */ + +void +runtime_exists() +{ + int f; + + for (f = 0; f < 3; f++) { + if (access(runtime_files[f], F_OK) == 0) { + printf("%s exists\n", runtime_files[f]); + } else { + printf("%s does not exist\n", runtime_files[f]); + } + } + return; +} + +/* + * init_config + * + * DESCRIPTION: Initialize libconfig. + * PARAMETERS: None. + * + */ + +int +init_config() +{ + config_init(&cfg); + + if (! config_read_file(&cfg, "glacier.cfg")) { + fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), + config_error_line(&cfg), config_error_text(&cfg)); + config_destroy(&cfg); + return(EXIT_FAILURE); + } + + infolog("Initialized libconfig"); +} + +int +die_config() +{ + config_destroy(&cfg); + infolog("Destroyed libconfig"); + return(EXIT_SUCCESS); +} + +void +load_setting_from_config() +{ + if(config_lookup_bool(&cfg, "GLACIER_ALLOW_SERVICES", &GLACIER_ALLOW_SERVICES)) + printf("Services allowed: %s\n", GLACIER_ALLOW_SERVICES ? "true" : "false"); + else + warnlog("GLACIER_SERVICES_ALLOWED is not defined in glacier.cfg."); + +} + +int +main(int argc, char *argv[]) +{ + errlog("Unknown option."); + printf(COL_RED "[x]" COL_RESET " usage: %s [-h] [-v] [-f] [-u] [-x] [-fl] [-ul] [-d] [-s] PKG\n", argv[0]); + runtime_exists(); + init_config(); + load_setting_from_config(); + die_config(); +} diff --git a/lua.c b/lua.c new file mode 100644 index 0000000..3e52793 --- /dev/null +++ b/lua.c @@ -0,0 +1,26 @@ +#include +#include +#include +#include +#include + +int +main(void) +{ + char buff[256]; + int error; + lua_State *L = lua_open(); + luaL_openlibs(L); + + while (fgets(buff, sizeof(buff), stdin) != NULL) { + error = lua_loadbuffer(L, buff, strlen(buff), "line") || + lua_pcall(L, 0, 0, 0); + if (error) { + fprintf(stderr, "%s", lua_tostring(L, -1)); + lua_pop(L, 1); + } + } + + lua_close(L); + return 0; +}