commit 836e861d992672c1b6c45477f2a9109c3d780ee8 Author: Derek Stevens Date: Thu Apr 15 14:40:40 2021 -0400 initial commit - slight modifications from 100r.co generator diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1ae5624 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +inc/ +old/ diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..e8c00cc --- /dev/null +++ b/LICENSE @@ -0,0 +1,8 @@ +Copyright 2021 Derek Stevens +Copyright 2021 Devine Lu Linvega + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..aac4a04 --- /dev/null +++ b/build.sh @@ -0,0 +1,32 @@ +#!/bin/bash + +# Lint +clang-format -i main.c + +# Cleanup +rm -f ./main + +# Linux(debug) +cc -std=c89 -DDEBUG -Wall -Wno-unknown-pragmas -Wpedantic -Wshadow -Wuninitialized -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og -fsanitize=address -fsanitize=undefined main.c -o main + +# Linux(fast) +# cc main.c -std=c89 -Os -DNDEBUG -g0 -s -Wall -Wno-unknown-pragmas -o main + +# RPi +# tcc -Wall main.c -o main + +# Plan9 +# pcc main.c -o main + +# Valgrind +# gcc -std=c89 -DDEBUG -Wall -Wpedantic -Wshadow -Wuninitialized -Wextra -Werror=implicit-int -Werror=incompatible-pointer-types -Werror=int-conversion -Wvla -g -Og main.c -o main +# valgrind ./main + +# Build Size +# echo "$(du -b ./main | cut -f1) bytes written" + +# Run +./main + +# Cleanup +rm -f ./main diff --git a/main.c b/main.c new file mode 100644 index 0000000..56862d7 --- /dev/null +++ b/main.c @@ -0,0 +1,267 @@ +#include +#include +#include +#include +#include + +/* +Copyright (c) 2021 Derek Stevens +Copyright (c) 2021 Devine Lu Linvega + +Permission to use, copy, modify, and distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE. +*/ + +/* clang-format off */ + +#define NAME "nilFM" +#define DOMAIN "https://nilfm.cc" +#define ABOUT "Derek Stevens <"\ + "drkste@zoho.com>
"\ + "artist, programmer, philosopher

"\ + "verify my signature: signing public key
"\ + "send me an encrypted message: encrypted public key" +#define SITEROOT "../www/" +#define MAINCSS "/new.css" +#define FRONTCSS "/front.css" +#define LICENSE "" +#define SOURCE "" + +/* clang-format on */ + +struct dirent *dir; + +typedef struct Lexicon { + int len, refs[512]; + char files[512][64]; +} Lexicon; + +/* clang-format off */ + +char clca(char c) { return c >= 'A' && c <= 'Z' ? c + ('a' - 'A') : c; } /* char to lowercase */ +char cuca(char c) { return c >= 'a' && c <= 'z' ? c - ('a' - 'A') : c; } /* char to uppercase */ +int slen(char *s) { int i = 0; while(s[i] && s[++i]) { ; } return i; } /* string length */ +char *st__(char *s, char (*fn)(char)) { int i = 0; char c; while((c = s[i])) s[i++] = fn(c); return s; } +char *stuc(char *s) { return st__(s, cuca); } /* string to uppercase */ +char *stlc(char *s) { return st__(s, clca); } /* string to lowercase */ +char *scpy(char *src, char *dst, int len) { int i = 0; while((dst[i] = src[i]) && i < len - 2) i++; dst[i + 1] = '\0'; return dst; } /* string copy */ +int scmp(char *a, char *b) { int i = 0; while(a[i] == b[i]) if(!a[i++]) return 1; return 0; } /* string compare */ +char *scsw(char *s, char a, char b) { int i = 0; char c; while((c = s[i])) s[i++] = c == a ? b : c; return s; } /* string char swap */ +char *scat(char *dst, const char *src) { char *ptr = dst + slen(dst); while(*src) *ptr++ = *src++; *ptr = '\0'; return dst; } /* string cat */ +int ssin(char *s, char *ss) { int a = 0, b = 0; while(s[a]) { if(s[a] == ss[b]) { if(!ss[b + 1]) return a - b; b++; } else b = 0; a++; } return -1; } /* string substring index */ +char *ccat(char *dst, char c) { int len = slen(dst); dst[len] = c; dst[len + 1] = '\0'; return dst; } + +/* clang-format on */ + +int fpinject(FILE *f, Lexicon *l, char *filepath); + +int error(char *msg, char *val) { + printf("Error: %s(%s)\n", msg, val); + return 0; +} + +int ismetanav(char *name) { return scmp(name, "meta.nav"); } + +int findf(Lexicon *l, char *f) { + int i; + char filename[64]; + scat(scsw(stlc(scpy(f, filename, 64)), ' ', '_'), ".htm"); + for (i = 0; i < l->len; ++i) + if (scmp(l->files[i], filename)) + return i; + return -1; +} + +void fpedited(FILE *f, char *path) { + struct stat attr; + stat(path, &attr); + fputs("", f); + fprintf(f, "Edited on %s ", ctime(&attr.st_mtime)); + fputs("", f); +} + +int fpportal(FILE *f, Lexicon *l, char *s, int head) { + int target; + char srcpath[64], filename[64]; + target = findf(l, s); + if (target < 0) + return error("Missing portal", s); + srcpath[0] = 0; + filename[0] = 0; + scat(scat(scat(srcpath, "inc/"), scpy(s, filename, 64)), ".htm"); + if (head) + fprintf(f, "

%s

", + scsw(filename, ' ', '_'), filename, s); + fpinject(f, l, srcpath); + l->refs[target]++; + return 1; +} + +int fptemplate(FILE *f, Lexicon *l, char *s) { + int target; + if (s[0] == '/') + return fpportal(f, l, s + 1, 1); + target = findf(l, s); + if (target < 0) + return error("Missing link", s); + fprintf(f, "", scsw(stlc(s), ' ', '_')); + fprintf(f, "%s", scsw(stlc(s), '_', ' ')); + l->refs[target]++; + return 1; +} + +int fpinject(FILE *f, Lexicon *l, char *filepath) { + FILE *inc; + char c, s[1024]; + unsigned char t = 0; + scsw(filepath, ' ', '_'); + if (!(inc = fopen(filepath, "r"))) + return error("Missing include", filepath); + s[0] = 0; + while ((c = fgetc(inc)) != EOF) { + if (c == '}') { + t = 0; + if (!fptemplate(f, l, s)) + return 0; + continue; + } + if (c == '{') { + s[0] = 0; + t = 1; + continue; + } + if (slen(s) > 1023) + return error("Templating error", filepath); + if (t) + ccat(s, c); + else + fprintf(f, "%c", c); + } + fclose(inc); + return 1; +} + +FILE *build(FILE *f, Lexicon *l, char *name, char *srcpath) { + if (!f) + return f; + /* begin */ + fputs("", f); + fputs("", f); + fprintf(f, + "" + "" + "", + "lair of drkste aka nilix: programmer, artist, philosopher"); + if (ismetanav(name)) { + fputs("", f); + } else { + fputs("", f); + } + fprintf(f, + "" + "" NAME " — %s", + name); + fputs("", f); + fputs("", f); + /* header */ + fputs("
", f); + if (ismetanav(name)) { + fputs("

nilFM

", f); + } else { + fputs("

nilFM

", f); + } + + fputs("
", f); + /* about (main page only) */ + if (ismetanav(name)) { + fputs("
" ABOUT "
", f); + } + /* nav */ + fputs("", f); + /* main */ + if (!ismetanav(name)) { + fputs("
\n\n", f); + fputs("\n\n", f); + fprintf(f, "

%s

", name); + if (!fpinject(f, l, srcpath)) + printf(">>> Building failed: %s\n", name); + fputs("\n\n
", f); + /* footer */ + fputs("", f); + } + /* end */ + fputs("", f); + return f; +} + +int generate(Lexicon *l) { + int i = 0; + char srcpath[64], dstpath[64], filename[64]; + for (i = 0; i < l->len; ++i) { + srcpath[0] = 0; + dstpath[0] = 0; + filename[0] = 0; + /* src */ + scpy(l->files[i], filename, ssin(l->files[i], ".htm") + 1); + scat(srcpath, "inc/"); + scat(srcpath, filename); + scat(srcpath, ".htm"); + /* dst */ + if (scmp(filename, "meta.nav")) { + scat(dstpath, SITEROOT); + scat(dstpath, "index.html"); + } else { + scat(dstpath, SITEROOT); + scat(dstpath, filename); + scat(dstpath, ".html"); + } + fclose(build(fopen(dstpath, "w"), l, scsw(filename, '_', ' '), srcpath)); + } + printf("Generated %d files\n", i); + return 1; +} + +int index(Lexicon *l, DIR *d) { + while ((dir = readdir(d))) + if (ssin(dir->d_name, ".htm") > 0) { + l->refs[l->len] = 0; + scpy(dir->d_name, l->files[l->len++], 64); + } + closedir(d); + printf("Indexed %d terms\n", l->len); + return 1; +} + +void inspect(Lexicon *l) { + int i; + for (i = 0; i < l->len; ++i) + if (!l->refs[i]) + error("Orphaned", l->files[i]); +} + +int main(void) { + Lexicon lex; + DIR *d; + lex.len = 0; + if (!(d = opendir("inc"))) + return error("Open", "Missing inc/ folder. "); + if (!index(&lex, d)) + return error("Indexing", "Failed"); + if (!generate(&lex)) + return error("Generating", "Failed"); + inspect(&lex); + return 0; +}