/* $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;
+}