From d9c755098fa8b5b9f798439639183bd966018a62 Mon Sep 17 00:00:00 2001 From: nishi Date: Fri, 19 Apr 2024 05:15:51 +0000 Subject: [PATCH] template system works git-svn-id: file:///raid/svn-main/nishi-mandshurica/trunk@36 f982e544-4a7d-3444-ad1a-fde59a2a69f1 --- Makefile | 2 + Mandshurica/main.c | 3 ++ Mandshurica/ms_config.h | 1 + Mandshurica/ms_template.h | 2 +- Mandshurica/template.c | 111 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 118 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 8b243a8..d798f50 100644 --- 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: diff --git a/Mandshurica/main.c b/Mandshurica/main.c index e5b1198..35ba0f8 100644 --- a/Mandshurica/main.c +++ b/Mandshurica/main.c @@ -39,6 +39,8 @@ #include #include +#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"); diff --git a/Mandshurica/ms_config.h b/Mandshurica/ms_config.h index e8ce1d2..0a0cbba 100644 --- a/Mandshurica/ms_config.h +++ b/Mandshurica/ms_config.h @@ -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; diff --git a/Mandshurica/ms_template.h b/Mandshurica/ms_template.h index 193fbd4..67bae3a 100644 --- a/Mandshurica/ms_template.h +++ b/Mandshurica/ms_template.h @@ -31,6 +31,6 @@ #ifndef __MANDSHURICA_MS_TEMPLATE_H__ #define __MANDSHURICA_MS_TEMPLATE_H__ - +char* mandshurica_parse_template(const char* data); #endif diff --git a/Mandshurica/template.c b/Mandshurica/template.c index 3b94306..e49ccad 100644 --- a/Mandshurica/template.c +++ b/Mandshurica/template.c @@ -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 +#include +#include +#include +#include + +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; +} -- 2.43.0