aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Makefile11
-rw-r--r--config.def.h13
-rw-r--r--wiki.c39
4 files changed, 42 insertions, 22 deletions
diff --git a/.gitignore b/.gitignore
index 006d339..c3ae088 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
pages
wiki.cgi
+config.h
diff --git a/Makefile b/Makefile
index 65b2c57..fdf45bc 100644
--- a/Makefile
+++ b/Makefile
@@ -1,2 +1,9 @@
-wiki.cgi: wiki.c
- ${CC} -std=c11 -Os -o wiki.cgi wiki.c -lmarxup
+wiki.cgi: config.h wiki.c
+ ${CC} -std=c11 -Os -o $@ wiki.c -lmarxup
+
+config.h:
+ cp config.def.h $@
+
+clean:
+ rm wiki.cgi
+
diff --git a/config.def.h b/config.def.h
new file mode 100644
index 0000000..a83aa8e
--- /dev/null
+++ b/config.def.h
@@ -0,0 +1,13 @@
+static char *title = "a wiki";
+static char *id = "anonymous";
+
+static char *links[][2] = {
+ { "home", "home" },
+ { "planer", "planer" },
+ 0
+};
+
+static char *(*authenticate)() = NULL;
+
+static char *script;
+static char *base;
diff --git a/wiki.c b/wiki.c
index 035eac8..36b86cb 100644
--- a/wiki.c
+++ b/wiki.c
@@ -10,17 +10,8 @@
#include <marxup.h>
-static char *wikititle = "a wiki";
-static char *id = "anonymous";
-static char *links[][2] = {
- { "home", "home" },
- { "planer", "planer" },
- 0
-};
-static char *(*authenticate)() = NULL;
-
-static char *script;
-static char *base;
+#include "config.h"
+
static const char *badchars = "\\?%:|\"<> ";
@@ -40,15 +31,15 @@ int writable(char *path) {
return 0;
}
-void head(char *title, char *link, char *what) {
+void head(char *page, char *link, char *what) {
printf("Content-Type: text/html; charset=utf-8\n\n");
printf("<!doctype html>\n");
- printf("<title>%s · %s</title>\n", wikititle, title);
+ printf("<title>%s · %s</title>\n", title, page);
printf("<link rel='stylesheet' href='%s/wiki.css'>\n", base);
if(link && what)
- printf("<h1><a data-text='%s' href='%s' >%s</a></h1>", what, link, title);
+ printf("<h1><a data-text='%s' href='%s' >%s</a></h1>", what, link, page);
else
- printf("<h1>%s</h1>", title);
+ printf("<h1>%s</h1>", page);
}
void foot(int file) {
@@ -82,6 +73,13 @@ int legit(char *page) {
return 1;
}
+int problem(int status, char *message) {
+ printf("Status: %d\n", status);
+ printf("Content-Type: text/plain\n\n");
+ printf("%s\n", message);
+ return 1;
+}
+
int redirect(char *path) {
printf("Status: 303\n");
if(*path == '?')
@@ -103,8 +101,8 @@ int unauthorized(int status) {
int edit(char *path) {
if(!writable(path)) return unauthorized(403);
FILE *fp = fopen(path, "r");
- char *title = clean(path);
- fp ? head(title, path, "vis") : head(title, NULL, NULL);
+ char *page = clean(path);
+ fp ? head(page, path, "vis") : head(page, NULL, NULL);
char buffer[sysconf(_SC_PAGESIZE)];
printf("<form method='post' enctype='text/plain'>\n");
printf("<p><textarea name=text rows=24 cols=72>");
@@ -183,8 +181,8 @@ int view(char *path) {
return list(path, 1);
FILE *fp = fopen(path, "r");
if(!fp) return redirect("?e");
- char *title = clean(path);
- writable(path) ? head(title, "?e", "rediger") : head(title, NULL, NULL);
+ char *page = clean(path);
+ writable(path) ? head(page, "?e", "rediger") : head(page, NULL, NULL);
marxup(fp, stdout);
foot(1);
return 0;
@@ -206,7 +204,8 @@ int main(int argc, char **argv) {
setenv("MARXUP_HEADER", "2", 1);
setenv("MARXUP_PREFIX", base, 1);
- chdir("pages");
+ if(chdir("pages"))
+ return problem(404, "Not found");
if(!strncmp(method, "POST", 4)) return post(page);
while(query && *query) {