]> Nishi Git Mirror - mandshurica.git/commitdiff
template system works
authornishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Fri, 19 Apr 2024 05:15:51 +0000 (05:15 +0000)
committernishi <nishi@f982e544-4a7d-3444-ad1a-fde59a2a69f1>
Fri, 19 Apr 2024 05:15:51 +0000 (05:15 +0000)
git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@36 f982e544-4a7d-3444-ad1a-fde59a2a69f1

Makefile
Mandshurica/main.c
Mandshurica/ms_config.h
Mandshurica/ms_template.h
Mandshurica/template.c

index 8b243a8e6a2f6b52c5f51291f9029d7289a3ed9f..d798f504023a4f26481bd976f8881ff02f028134 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -24,8 +24,10 @@ format:
 
 replace:
        for i in $(wildcard Module/*.c Module/*.h Mandshurica/*.c Mandshurica/*.h); do \
+               echo -n $$i"... "; \
                cat $$i | perl replace.pl > $$i.new; \
                mv $$i.new $$i; \
+               echo done; \
        done
 
 clean:
index e5b1198587be25502e658a7053d1243414320ab4..35ba0f8f1ab24b4e98effe238005b33820624a16 100644 (file)
@@ -39,6 +39,8 @@
 #include <stdlib.h>
 #include <string.h>
 
+#include "ms_template.h"
+
 extern struct mandshurica_config config;
 
 int main(int argc, char** argv) {
@@ -75,6 +77,7 @@ int main(int argc, char** argv) {
                int ret = mandshurica_load_config(PREFIX "/etc/mandshurica.conf");
                if(ret != 0) return ret;
        }
+       printf("%s\n", mandshurica_parse_template("@version@"));
        mandshurica_log(MS_INFO, "Hello World, initialization done");
        if(loaded_mods != NULL) {
                mandshurica_log(MS_INFO, "Starting server");
index e8ce1d2066dd622cf2201da4a89ab4b14958d007..0a0cbbab56c09447ef90ded89cdca54eb8be7797 100644 (file)
@@ -37,6 +37,7 @@
 
 int mandshurica_load_config(const char* path);
 int mandshurica_create_config(const char* path);
+char* mandshurica_get_param(const char* param);
 
 struct mandshurica_mod {
        void* lib;
index 193fbd430c94e701c0ce8562eee20fcd74580a8c..67bae3ad0d892f4f3a07e8d08ad1bfe82c34629b 100644 (file)
@@ -31,6 +31,6 @@
 #ifndef __MANDSHURICA_MS_TEMPLATE_H__
 #define __MANDSHURICA_MS_TEMPLATE_H__
 
-
+char* mandshurica_parse_template(const char* data);
 
 #endif
index 3b94306346ba608b2743457aa5d03e8761159140..e49ccad50a2466e9e6dcec862dd837b5b29d7b36 100644 (file)
@@ -1,5 +1,116 @@
 /* $Id$ */
 /* --- START LICENSE --- */
+/* -------------------------------------------------------------------------- */
+/*                                            Mandshurica - 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 --- */
 
 #include "ms_template.h"
+
+#include "ms_config.h"
+#include "ms_util.h"
+
+#include "mandshurica.h"
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+
+char* mandshurica_parse_template(const char* data) {
+       char* webroot = mandshurica_get_param("HTTPRoot");
+       if(webroot == NULL) webroot = WEBROOT_PREFIX;
+
+       char cbuf[2];
+       cbuf[1] = 0;
+       int i;
+       bool template = false;
+       int start = 0;
+       char* ret = malloc(1);
+       ret[0] = 0;
+       for(i = 0; data[i] != 0; i++) {
+               cbuf[0] = data[i];
+               if(data[i] == '@') {
+                       template = !template;
+                       if(template) {
+                               start = i + 1;
+                       } else {
+                               char* tmpl = malloc(i - start + 1);
+                               tmpl[i - start] = 0;
+                               memcpy(tmpl, data + start, i - start);
+
+                               int j;
+                               char* op = tmpl;
+                               char* arg = NULL;
+                               for(j = 0; tmpl[j] != 0; j++) {
+                                       if(tmpl[j] == ' ') {
+                                               tmpl[j] = 0;
+                                               arg = tmpl + j + 1;
+                                               break;
+                                       }
+                               }
+
+                               if(strcmp(op, "include") == 0) {
+                                       if(arg != NULL) {
+                                               FILE* f = fopen(arg, "r");
+                                               if(f != NULL) {
+                                                       struct stat s;
+                                                       stat(arg, &s);
+                                                       char* fb = malloc(s.st_size + 1);
+                                                       fb[s.st_size] = 0;
+                                                       fread(fb, s.st_size, 1, f);
+                                                       fclose(f);
+
+                                                       char* res = mandshurica_parse_template(fb);
+
+                                                       char* tmp = ret;
+                                                       ret = mandshurica_strcat(tmp, res);
+                                                       free(tmp);
+
+                                                       free(res);
+
+                                                       free(fb);
+                                               }
+                                       }
+                               } else if(strcmp(op, "version") == 0) {
+                                       char* tmp = ret;
+                                       ret = mandshurica_strcat(tmp, MANDSHURICA_VERSION);
+                                       free(tmp);
+                               } else if(strcmp(op, "chdir_webroot") == 0) {
+                                       chdir(webroot);
+                               }
+
+                               free(tmpl);
+                       }
+               } else if(!template) {
+                       char* tmp = ret;
+                       ret = mandshurica_strcat(tmp, cbuf);
+                       free(tmp);
+               }
+       }
+       return ret;
+}