#include #include #include #include #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; }