]> Nishi Git Mirror - mandshurica.git/commitdiff
config loading
authornishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Sat, 13 Apr 2024 01:25:29 +0000 (01:25 +0000)
committernishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Sat, 13 Apr 2024 01:25:29 +0000 (01:25 +0000)
git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@5 f982e544-4a7d-3444-ad1a-fde59a2a69f1

DevForge/Makefile
DevForge/config.c
DevForge/df_config.h
DevForge/df_util.h [new file with mode: 0644]
DevForge/main.c
DevForge/util.c [new file with mode: 0644]
Makefile

index 0fe32920a4640600f2e0fb8b89e91a64882fe957..6f3b44256e725c649c3e5c0b15a6d07c0fdc77c2 100644 (file)
@@ -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
index 1bd3bfe60c5c4ad41e6a7aa39c7762620c61ff20..7b765fccd24f85af5a9abd2037f9e3d538463e66 100644 (file)
 /* -------------------------------------------------------------------------- */
 /* --- END LICENSE --- */
 
-void devforge_load_config(const char* path) {}
+#include "df_config.h"
+
+#include "df_log.h"
+#include "df_util.h"
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+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;
+}
index ff337b2944dc3d07ded5cc29caebfcf76ec0b094..ab35e5f85b3527267de66c37da36de644f8f3aee 100644 (file)
@@ -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 (file)
index 0000000..226f869
--- /dev/null
@@ -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
index 070b17c52b6c95c5adab2b0c0280eec6a165c1ea..cb913b8776998f08fc8bbdfa1a670175e80c24b2 100644 (file)
 #include "df_config.h"
 #include "df_log.h"
 
+#include <stdbool.h>
 #include <stdio.h>
 #include <string.h>
 
 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 (file)
index 0000000..59dab3c
--- /dev/null
@@ -0,0 +1,23 @@
+/* $Id$ */
+/* --- START LICENESE --- */
+/* --- END LICENSE --- */
+
+#include "df_util.h"
+
+#include <stdlib.h>
+#include <string.h>
+
+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;
+}
index 598a945102c92d7f4f5285a042b97df6813afaf7..507181704a91682d1281dcd123cb8542131096ea 100644 (file)
--- 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 :=