From 10875ff940e70881065c921e24408caded04974c Mon Sep 17 00:00:00 2001
From: nekobit <nekobit@noemail.net>
Date: Fri, 22 Jul 2022 03:56:41 +0000
Subject: [PATCH] Load perl

FossilOrigin-Name: 0107dd3706329714f684f913c2ae67d072851eca96b981c11f5315e1ef780c22
---
 .fossil-settings/ignore-glob |  3 ++-
 Makefile                     | 14 +++++++++++---
 config.def.h                 |  2 ++
 perl/main.pl                 |  9 +++++++++
 src/global_perl.h            | 26 ++++++++++++++++++++++++++
 src/main.c                   | 22 +++++++++++++++++++++-
 6 files changed, 71 insertions(+), 5 deletions(-)
 create mode 100644 perl/main.pl
 create mode 100644 src/global_perl.h

diff --git a/.fossil-settings/ignore-glob b/.fossil-settings/ignore-glob
index cab4136..0cf097b 100644
--- a/.fossil-settings/ignore-glob
+++ b/.fossil-settings/ignore-glob
@@ -9,4 +9,5 @@ mastodont-c
 config.h
 treebird
 test/tests
-scripts/*.o
\ No newline at end of file
+scripts/*.o
+perl/*.cpl
\ No newline at end of file
diff --git a/Makefile b/Makefile
index 9a32a50..b7e958f 100644
--- a/Makefile
+++ b/Makefile
@@ -2,26 +2,31 @@ CC ?= cc
 GIT ?= git
 MASTODONT_DIR = mastodont-c/
 MASTODONT = $(MASTODONT_DIR)libmastodont.a
-CFLAGS += -Wall -I $(MASTODONT_DIR)include/ -Wno-unused-variable -Wno-ignored-qualifiers -I/usr/include/ -I $(MASTODONT_DIR)/libs $(shell pkg-config --cflags libcurl libpcre2-8)
-LDFLAGS = -L$(MASTODONT_DIR) -lmastodont $(shell pkg-config --libs libcurl libpcre2-8) -lfcgi -lpthread
+CFLAGS += -Wall -I $(MASTODONT_DIR)include/ -Wno-unused-variable -Wno-ignored-qualifiers -I/usr/include/ -I $(MASTODONT_DIR)/libs $(shell pkg-config --cflags libcurl libpcre2-8) `perl -MExtUtils::Embed -e ccopts`
+LDFLAGS = -L$(MASTODONT_DIR) -lmastodont $(shell pkg-config --libs libcurl libpcre2-8) -lfcgi -lpthread `perl -MExtUtils::Embed -e ldopts`
 SRC = $(wildcard src/*.c)
 OBJ = $(patsubst %.c,%.o,$(SRC))
 HEADERS = $(wildcard src/*.h) config.h
 PAGES_DIR = static
+PERL_DIR = perl
 PAGES = $(wildcard $(PAGES_DIR)/*.tmpl)
+PERLS = $(wildcard $(PERL_DIR)/*.pl)
 PAGES_CMP = $(patsubst %.tmpl,%.ctmpl,$(PAGES))
 PAGES_C = $(patsubst %.tmpl, %.c,$(PAGES))
 PAGES_C_OBJ = $(patsubst %.c,%.o,$(PAGES_C))
+PERLS_C = $(patsubst %.pl,%.cpl,$(PERLS))
 DIST = dist/
 PREFIX ?= /usr/local
 TARGET = treebird
 
+$(info $(PERLS) and $(PERLS_C))
+
 MASTODONT_URL = https://fossil.nekobit.net/mastodont-c
 
 all: $(MASTODONT_DIR) dep_build $(TARGET)
 apache: all apache_start
 
-$(TARGET): filec template $(PAGES_CMP) $(PAGES_C) $(PAGES_C_OBJ)  $(OBJ) $(HEADERS)
+$(TARGET): filec template $(PERLS_C) $(PAGES_CMP) $(PAGES_C) $(PAGES_C_OBJ) $(OBJ) $(HEADERS) 
 	$(CC) -o $(TARGET) $(OBJ) $(PAGES_C_OBJ) $(LDFLAGS)
 
 template: src/template/main.o
@@ -39,6 +44,9 @@ emojitoc: scripts/emoji-to.o
 $(PAGES_DIR)/%.ctmpl: $(PAGES_DIR)/%.tmpl
 	./template $< $(notdir $*) 2> $(PAGES_DIR)/$(notdir $*).c 1> $@
 
+$(PERL_DIR)/%.cpl: $(PERL_DIR)/%.pl
+	./filec $< data_$(notdir $*)_pl > $@
+
 $(MASTODONT_DIR): 
 	cd ..; fossil clone $(MASTODONT_URL) || true
 	cd treebird; ln -s ../mastodont-c .
diff --git a/config.def.h b/config.def.h
index a5eed39..34e65c0 100644
--- a/config.def.h
+++ b/config.def.h
@@ -9,8 +9,10 @@
 #ifndef CONFIG_H
 #define CONFIG_H
 #include <mastodont.h>
+#if !(defined(FALSE) && defined(TRUE))
 #define FALSE 0
 #define TRUE 1
+#endif
 #define UNSET NULL
 
 /*
diff --git a/perl/main.pl b/perl/main.pl
new file mode 100644
index 0000000..8c02b21
--- /dev/null
+++ b/perl/main.pl
@@ -0,0 +1,9 @@
+use strict;
+use warnings;
+
+sub square
+{
+    2 * 2;
+}
+
+print "Hello treebird" . square;
diff --git a/src/global_perl.h b/src/global_perl.h
new file mode 100644
index 0000000..cf43d4d
--- /dev/null
+++ b/src/global_perl.h
@@ -0,0 +1,26 @@
+/*
+ * Treebird - Lightweight frontend for Pleroma
+ * Copyright (C) 2022 Nekobit
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef GLOBAL_PERL_H
+#define GLOBAL_PERL_H
+#include <EXTERN.h>
+#include <perl.h>
+
+static PerlInterpreter* perl;
+
+#endif /* GLOBAL_PERL_H */
diff --git a/src/main.c b/src/main.c
index d936a60..7fbc59a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -16,8 +16,11 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+#include <EXTERN.h>
+#include <perl.h>
 #include <pthread.h>
 #include <fcgi_stdio.h>
+#include "global_perl.h"
 #include <fcgiapp.h>
 #include <string.h>
 #include <mastodont.h>
@@ -44,6 +47,7 @@
 #include "local_config_set.h"
 #include "global_cache.h"
 #include "conversations.h"
+#include "../perl/main.cpl"
 
 #define THREAD_COUNT 20
 
@@ -190,12 +194,23 @@ static void* cgi_start(void* arg)
     return NULL;
 }
 
-int main(void)
+int main(int argc, char **argv, char **env)
 {
     // Global init
     mastodont_global_curl_init();
     FCGX_Init();
 
+    // Initialize Perl
+    PERL_SYS_INIT3(&argc, &argv, &env);
+    perl = perl_alloc();
+    perl_construct(perl);
+    //char* perl_argv[] = { "", "-e", data_main_pl, NULL };
+    char* perl_argv[] = { "", "perl/main.pl", NULL };
+
+    perl_parse(perl, NULL, (sizeof(perl_argv) / sizeof(perl_argv[0])) - 1, perl_argv, (char**)NULL);
+    PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
+    perl_run(perl);
+
     // Initiate mastodont library
     mastodont_t api;
     mastodont_init(&api);
@@ -215,4 +230,9 @@ int main(void)
     free_instance_info_cache();
     mastodont_cleanup(&api);
     mastodont_global_curl_cleanup();
+
+    perl_destruct(perl);
+    perl_free(perl);
+    PERL_SYS_TERM();
+    return EXIT_SUCCESS;
 }
