File to C, mastodont link

FossilOrigin-Name: f9526d636873ba3c19fc2958b493c52deb342e75c9a721c2730ae950ef42347c
This commit is contained in:
me@ow.nekobit.net 2022-01-19 04:19:29 +00:00
parent eeffd37e16
commit ce8f50cbfe
5 changed files with 114 additions and 15 deletions

5
.gitignore vendored
View file

@ -1,4 +1,5 @@
file-to-c
filec
**/*.cgi
**/*.o
**/*.chtml
**/*.chtml
mastodont

View file

@ -1,7 +1,9 @@
CC ?= cc
GIT ?= git
CFLAGS = -Wall
LDFLAGS = -lcurl
MASTODONT_DIR = mastodont/
MASTODONT = $(MASTODONT_DIR)libmastodont.a
CFLAGS = -Wall -I $(MASTODONT)include/
LDFLAGS = -L$(MASTODONT_DIR) -lcurl -lmastodont
SRC = $(wildcard src/*.c)
OBJ = $(patsubst %.c,%.o,$(SRC))
PAGES_DIR = static
@ -10,28 +12,23 @@ PAGES_CMP = $(patsubst %.html,%.chtml,$(PAGES))
DIST = dist/
TARGET = ratfe.cgi
# Mastodont
MASTODONT = mastodont/
MASTODONT_REPO = https://git.nekobit.net/repos/mastodont-c.git
all: mastodont $(TARGET)
mastodont: $(MASTODONT)
all: $(MASTODONT) $(TARGET)
$(TARGET): filec $(PAGES_CMP) $(OBJ)
$(CC) -o $(DIST)$(TARGET) $(LDFLAGS) $(OBJ)
filec: src/file-to-c/main.o
$(CC) -o file-to-c $<
$(CC) -o filec $<
%.chtml: %.html
./file-to-c $< $< > $@
./filec $< $< > $@
$(PAGES_DIR)/index.chtml: $(PAGES_DIR)/index.html
./file-to-c $< data_index_html > $@
./filec $< data_index_html > $@
$(MASTODONT):
$(GIT) submodule foreach git pull
make -C $(MASTODONT)
$(GIT) submodule update --init --recursive
make -C $(MASTODONT_DIR)
%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
@ -39,5 +36,6 @@ $(MASTODONT):
clean:
rm -f $(OBJ) src/file-to-c/main.o
rm -f $(PAGES_CMP)
rm -f filec
.PHONY: all filec clean

View file

@ -11,6 +11,26 @@
#define FALSE 0
#define TRUE 1
/*
* String: config_canonical_name
*
* The software's recognizable name.
*
*
* Example: "RatFE"
*/
static const char* config_canonical_name = "RatFE";
/*
* String: config_instance_url
*
* The instances URL which all API calls will be sent to via mastodont-c.
* This MUST include a slash at the end, and the protocol (like https://) at the
* beginning
*
* Example: https://cum.desupost.soy/
*/
static const char* config_instance_url = "https://cum.desupost.soy/";
#endif // CONFIG_H

71
src/file-to-c/main.c Normal file
View file

@ -0,0 +1,71 @@
/*
* RatFE - 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/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum args
{
ARG_FILENAME = 1,
ARG_VARIABLE
};
long filesize(FILE* file)
{
long orig = ftell(file);
fseek(file, 0, SEEK_END);
long size = ftell(file);
fseek(file, orig, SEEK_SET);
return size;
}
int main(int argc, char** argv)
{
char* buf;
FILE* file = fopen(argv[ARG_FILENAME], "rb");
long size = filesize(file);
if (!(buf = malloc(size)))
{
perror("malloc");
return 1;
}
if (fread(buf, 1, size, file) != size)
{
fputs("Didn't read correctly!", stderr);
free(buf);
return 1;
}
fclose(file);
printf("#ifndef __%s\n", argv[ARG_VARIABLE]);
printf("#define __%s\n", argv[ARG_VARIABLE]);
printf("static const char %s[] = {", argv[ARG_VARIABLE]);
for (size_t i = 0; i < size; ++i)
{
printf("0X%hhX,", buf[i]);
}
puts("0};\n#endif");
free(buf);
return 0;
}

View file

@ -19,6 +19,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mastodont.h>
#include "../config.h"
#include "index.h"
int main()
@ -26,10 +28,17 @@ int main()
char* path = getenv("PATH_INFO");
// Content type is always HTML
fputs("Content-type: text/html\r\n\r\n", stdout);
mastodont_t api;
api.url = config_instance_url;
mastodont_init(&api);
// Default index
if (path == NULL || (path && strcmp(path, "/")))
{
content_index();
}
mastodont_free(&api);
}