aboutsummaryrefslogtreecommitdiff
path: root/cforum.c
diff options
context:
space:
mode:
Diffstat (limited to 'cforum.c')
-rw-r--r--cforum.c74
1 files changed, 40 insertions, 34 deletions
diff --git a/cforum.c b/cforum.c
index a42a0b3..7fc738e 100644
--- a/cforum.c
+++ b/cforum.c
@@ -3,66 +3,72 @@
#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 "ctl.h" /* Controllers. */
+#include "err.h" /* HTTP errors. */
+#include "site.h" /* Site settings. Defines global struct site. */
+#include "query.h" /* Query functions. Defines global string qs. */
-#include "front.c"
+#define MAXERR 300
int
main(int argc, char *argv[])
{
- char *qs;
+ char err[MAXERR], *qs;
sqlite3 *db;
sqlite3_stmt *res;
- /* Retrieve site name from database. */
+ /*
+ * The database is opened or a server error is generated.
+ * In the rest of the program, the database is always
+ * assumed to be opened.
+ */
if(sqlite3_open("db", &db) != SQLITE_OK){
- warn("%s\n", sqlite3_errmsg(db));
+ snprintf(err, MAXERR,
+ "The database could not be opened: %s\n",
+ sqlite3_errmsg(db));
+ srverr(err);
sqlite3_close(db);
return 1;
}
+ /*
+ * The site name is retrieved from the database. This early on,
+ * it is appropriate to die with a server error on failure.
+ */
if(sqlite3_prepare(db,
"SELECT value FROM settings WHERE key = 'name'",
-1, &res, 0) != SQLITE_OK){
- warn("%s\n", sqlite3_errmsg(db));
+ snprintf(err, MAXERR,
+ "The site name could not be retrieved: %s\n",
+ sqlite3_errmsg(db));
+ srverr(err);
sqlite3_close(db);
return 1;
}
-
if(sqlite3_step(res) == SQLITE_ROW)
site.name = strdup(sqlite3_column_text(res, 0));
-
+ else{
+ snprintf(err, MAXERR, "The site name is not set.\n");
+ srverr(err);
+ sqlite3_finalize(res);
+ sqlite3_close(db);
+ return 1;
+ }
sqlite3_finalize(res);
- sqlite3_close(db);
+
+ /*
+ * The global variable qs is set to the query string, or the
+ * program dies. From now on, qs is assumed to be set.
+ */
+ setqs();
/* Handle request. */
- qs = getenv("QUERY_STRING");
if(!qs || !*qs)
front();
+ /* INSERT CONDITIONS HERE */
+ else
+ front();
+ sqlite3_close(db);
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;
} \ No newline at end of file