diff options
author | John Ankarström <john@ankarstrom.se> | 2021-09-17 11:20:43 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-09-17 11:20:43 +0200 |
commit | 38f8e2242353711d5c87c58f3831306934d2e6b7 (patch) | |
tree | 92ec8e297731fb1de90b0e465bb52207bbe5c037 /cforum.c | |
parent | 4b0e767fd645687e8d2a5ded778c57b8cf42bc4a (diff) | |
download | cforum-38f8e2242353711d5c87c58f3831306934d2e6b7.tar.gz |
Compartmentalize
Diffstat (limited to 'cforum.c')
-rw-r--r-- | cforum.c | 74 |
1 files changed, 40 insertions, 34 deletions
@@ -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 |