From c2c50581cae7df89a0dc3621c1c53477d4c58140 Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 13 Apr 2024 01:25:29 +0000 Subject: [PATCH] config loading git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@5 f982e544-4a7d-3444-ad1a-fde59a2a69f1 --- DevForge/Makefile | 2 +- DevForge/config.c | 43 ++++++++++++++++++++++++++++++++++++++++++- DevForge/df_config.h | 3 ++- DevForge/df_util.h | 37 +++++++++++++++++++++++++++++++++++++ DevForge/main.c | 18 +++++++++++++++++- DevForge/util.c | 23 +++++++++++++++++++++++ Makefile | 4 +++- 7 files changed, 125 insertions(+), 5 deletions(-) create mode 100644 DevForge/df_util.h create mode 100644 DevForge/util.c diff --git a/DevForge/Makefile b/DevForge/Makefile index 0fe3292..6f3b442 100644 --- a/DevForge/Makefile +++ b/DevForge/Makefile @@ -12,7 +12,7 @@ endif all: ./devforge -./devforge: ./main.o ./log.o ./config.o +./devforge: ./main.o ./log.o ./config.o ./util.o $(CC) $(LDFLAGS) $(EXTRA_LDFLAGS) -o $@ $^ $(LIBS) $(EXTRA_LIBS) ./main.o: ./main.c ./devforge.h diff --git a/DevForge/config.c b/DevForge/config.c index 1bd3bfe..7b765fc 100644 --- a/DevForge/config.c +++ b/DevForge/config.c @@ -28,4 +28,45 @@ /* -------------------------------------------------------------------------- */ /* --- END LICENSE --- */ -void devforge_load_config(const char* path) {} +#include "df_config.h" + +#include "df_log.h" +#include "df_util.h" + +#include +#include +#include +#include + +int devforge_load_config(const char* path) { + char* str = devforge_strcat3("Loading the config `", path, "`"); + devforge_log(DF_LOG, str); + free(str); + + FILE* f = fopen(path, "r"); + if(f != NULL) { + fclose(f); + } else { + devforge_log(DF_ERROR, strerror(errno)); + return 1; + } + + return 0; +} + +int devforge_create_config(const char* path) { + char* str = devforge_strcat3("Creating the config `", path, "`"); + devforge_log(DF_LOG, str); + free(str); + + FILE* f = fopen(path, "w"); + if(f != NULL) { + fclose(f); + devforge_log(DF_LOG, "Created the config"); + } else { + devforge_log(DF_ERROR, strerror(errno)); + return 1; + } + + return 0; +} diff --git a/DevForge/df_config.h b/DevForge/df_config.h index ff337b2..ab35e5f 100644 --- a/DevForge/df_config.h +++ b/DevForge/df_config.h @@ -31,6 +31,7 @@ #ifndef __DEVFORGE_DF_CONFIG_H__ #define __DEVFORGE_DF_CONFIG_H__ -void devforge_load_config(const char* path); +int devforge_load_config(const char* path); +int devforge_create_config(const char* path); #endif diff --git a/DevForge/df_util.h b/DevForge/df_util.h new file mode 100644 index 0000000..226f869 --- /dev/null +++ b/DevForge/df_util.h @@ -0,0 +1,37 @@ +/* $Id$ */ +/* --- START LICENSE --- */ +/* -------------------------------------------------------------------------- */ +/* DevForge - Build Automation */ +/* -------------------------------------------------------------------------- */ +/* Copyright (c) 2024 Nishi. */ +/* Redistribution and use in source and binary forms, with or without modific */ +/* ation, are permitted provided that the following conditions are met: */ +/* 1. Redistributions of source code must retain the above copyright noti */ +/* ce, this list of conditions and the following disclaimer. */ +/* 2. Redistributions in binary form must reproduce the above copyright n */ +/* otice, this list of conditions and the following disclaimer in the documen */ +/* tation and/or other materials provided with the distribution. */ +/* 3. Neither the name of the copyright holder nor the names of its contr */ +/* ibutors may be used to endorse or promote products derived from this softw */ +/* are 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, TH */ +/* E IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPO */ +/* SE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS */ +/* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CON */ +/* SEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITU */ +/* TE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPT */ +/* ION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, S */ +/* TRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN AN */ +/* Y WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY */ +/* OF SUCH DAMAGE. */ +/* -------------------------------------------------------------------------- */ +/* --- END LICENSE --- */ + +#ifndef __DEVFORGE_DF_UTIL_H__ +#define __DEVFORGE_DF_UTIL_H__ + +char* devforge_strcat(const char* str1, const char* str2); +char* devforge_strcat3(const char* str1, const char* str2, const char* str3); + +#endif diff --git a/DevForge/main.c b/DevForge/main.c index 070b17c..cb913b8 100644 --- a/DevForge/main.c +++ b/DevForge/main.c @@ -33,11 +33,13 @@ #include "df_config.h" #include "df_log.h" +#include #include #include int main(int argc, char** argv) { int i; + bool loaded_config = false; devforge_log(DF_INFO, "DevForge " DEVFORGE_VERSION " Copyright (C) 2024 Nishi"); for(i = 1; i < argc; i++) { if(argv[i][0] == '-') { @@ -47,7 +49,17 @@ int main(int argc, char** argv) { fprintf(stderr, "%s: %s: needs argument\n", argv[0], argv[i - 1]); return 1; } else { - devforge_load_config(argv[i]); + int ret = devforge_load_config(argv[i]); + if(ret != 0) return ret; + loaded_config = true; + } + } else if(strcmp(argv[i], "--create") == 0) { + i++; + if(argv[i] == NULL) { + fprintf(stderr, "%s: %s: needs argument\n", argv[0], argv[i - 1]); + return 1; + } else { + return devforge_create_config(argv[i]); } } else { fprintf(stderr, "%s: %s: unknown option\n", argv[0], argv[i]); @@ -55,4 +67,8 @@ int main(int argc, char** argv) { } } } + if(!loaded_config) { + int ret = devforge_load_config(PREFIX "/etc/devforge.conf"); + if(ret != 0) return ret; + } } diff --git a/DevForge/util.c b/DevForge/util.c new file mode 100644 index 0000000..59dab3c --- /dev/null +++ b/DevForge/util.c @@ -0,0 +1,23 @@ +/* $Id$ */ +/* --- START LICENESE --- */ +/* --- END LICENSE --- */ + +#include "df_util.h" + +#include +#include + +char* devforge_strcat(const char* str1, const char* str2) { + char* str = malloc(strlen(str1) + strlen(str2) + 1); + memcpy(str, str1, strlen(str1)); + memcpy(str + strlen(str1), str2, strlen(str2)); + str[strlen(str1) + strlen(str2)] = 0; + return str; +} + +char* devforge_strcat3(const char* str1, const char* str2, const char* str3) { + char* tmp = devforge_strcat(str1, str2); + char* r = devforge_strcat(tmp, str3); + free(tmp); + return r; +} diff --git a/Makefile b/Makefile index 598a945..5071817 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,8 @@ # $Id$ +PREFIX := /usr/local + CC := gcc -CFLAGS := -g -std=c99 +CFLAGS := -g -std=c99 -DPREFIX=\\\"$(PREFIX)\\\" LDFLAGS := LIBS := -- 2.43.0