aboutsummaryrefslogtreecommitdiff
path: root/cforum.c
blob: a42a0b38ca849894c40aeab509d2fbd2d363362f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <sqlite3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define warn(...) fprintf(stderr, __VA_ARGS__)
#define die(...) do{ fprintf(stderr, __VA_ARGS__); exit(1); }while(0)
char *query(char *);
struct site {
	char *name;
} site;

#include "front.c"

int
main(int argc, char *argv[])
{
	char *qs;
	sqlite3 *db;
	sqlite3_stmt *res;
	
	/* Retrieve site name from database. */
	if(sqlite3_open("db", &db) != SQLITE_OK){
		warn("%s\n", sqlite3_errmsg(db));
		sqlite3_close(db);
		return 1;
	}
	
	if(sqlite3_prepare(db,
	    "SELECT value FROM settings WHERE key = 'name'",
	    -1, &res, 0) != SQLITE_OK){
		warn("%s\n", sqlite3_errmsg(db));
		sqlite3_close(db);
		return 1;
	}
	
	if(sqlite3_step(res) == SQLITE_ROW)
		site.name = strdup(sqlite3_column_text(res, 0));
	
	sqlite3_finalize(res);
	sqlite3_close(db);
	
	/* Handle request. */
	qs = getenv("QUERY_STRING");
	if(!qs || !*qs)
		front();
	
	return 0;
}

char *
query(char *key)
{
	static char *qs;
	char *w;
	int n;
	
	if(!qs) qs = getenv("QUERY_STRING");
	if(!qs) die("no QUERY_STRING");
	
	for(w = strtok(qs, "&"); w; w = strtok(NULL, "&")){
		n = strcspn(w, "=");
		if(strncmp(w, key, n) == 0)
			return w + n + (w[n] == '=');
	}
	
	return NULL;
}