From 3be3213d2f76e2968d4854cfb11cecd0b5360129 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Sat, 18 Sep 2021 12:15:16 +0200 Subject: Start work on `new' functions --- README | 8 ++++---- cforum.c | 36 ++++++++++++------------------------ ctl.c | 29 +++++++++++++++++++++++++---- ctl.h | 3 +++ 4 files changed, 44 insertions(+), 32 deletions(-) diff --git a/README b/README index a3352ba..63ca7e0 100644 --- a/README +++ b/README @@ -8,12 +8,12 @@ C89, it can be run on practically any UNIX system. It is also rather small: wc -l *.c *.h */*.t */*.lex - 113 cforum.c - 122 ctl.c + 100 cforum.c + 143 ctl.c 230 db.c 10 err.c 106 query.c - 3 ctl.h + 6 ctl.h 40 db.h 0 err.h 14 query.h @@ -25,4 +25,4 @@ It is also rather small: 28 t/post.t 12 t/user.t 87 mktpl/mktpl.lex - 803 total + 814 total diff --git a/cforum.c b/cforum.c index 900465a..feee431 100644 --- a/cforum.c +++ b/cforum.c @@ -15,10 +15,9 @@ int main(int argc, char *argv[]) { - char msg[MAXMSG], *p, *v; + char msg[MAXMSG], *new, *p, *v; int attid, postid, userid; sqlite3_stmt *stmt; - struct att *att; /* * The database is opened or a server error is generated. @@ -59,21 +58,6 @@ main(int argc, char *argv[]) } sqlite3_finalize(stmt); - /* - if(!(att = malloc(sizeof(struct att)))) - err(1, "malloc"); - att->post = 1; - att->name = strdup("example2"); - att->description = NULL; - att->mime = strdup("text/html"); - att->data = strdup("Hello!"); - att->bytes = strlen(att->data)+1; - - fprintf(stderr, "[add] %d %s\n", - addatt(att), sqlite3_errmsg(db)); - free(att); - */ - /* * The global struct query is set, or the program dies. * From now on, query is assumed to be set. @@ -87,15 +71,14 @@ main(int argc, char *argv[]) } /* Parse query string. */ + new = NULL; attid = postid = userid = 0; while(p = nextparam(GET, 128)){ v = split(p); - if(!attid && strcmp(p, "att") == 0) - attid = atoi(v); - else if(!postid && strcmp(p, "post") == 0) - postid = atoi(v); - else if(!userid && strcmp(p, "user") == 0) - userid = atoi(v); + if(!attid && strcmp(p, "att") == 0) attid = atoi(v); + else if(!postid && strcmp(p, "post") == 0) postid = atoi(v); + else if(!userid && strcmp(p, "user") == 0) userid = atoi(v); + else if(!new && strcmp(p, "new") == 0) new = strdup(v); } /* Handle request. */ @@ -105,7 +88,12 @@ main(int argc, char *argv[]) showpost(postid); else if(userid) showuser(userid); - else + else if(new){ + if(strcmp(v, "att") == 0) newatt(); + else if(strcmp(v, "post") == 0) newpost(); + else if(strcmp(v, "user") == 0) newuser(); + /* else bad request */ + }else showfront(); end: diff --git a/ctl.c b/ctl.c index bab653e..64934cc 100644 --- a/ctl.c +++ b/ctl.c @@ -20,13 +20,34 @@ printdate(int timestamp) printf(buf); } +/* + * The `new' functions provide a way to add a new attachment/post/user. + * On GET, they show a form. On POST, they insert the posted information + * into the database. + */ +void +newatt() +{} + +void +newpost() +{} + +void +newuser() +{} + +/* + * The `show' functions show an existing attachment/post/user + * or some other type of page. + */ void showatt(id) { struct att *att; if(!(att = getatt(selectbyint("atts", "oid", id)))){ - srverr("Could not retrieve att"); + srverr("Could not retrieve attachment"); return; } @@ -83,7 +104,7 @@ showpost(int id) stmt = selectbyint("atts", "post", id); - title = site.name; + title = post->subject; printf("Content-Type: text/html\n\n"); #include "t/post.tc" free(user); @@ -93,7 +114,7 @@ showpost(int id) void showuser(int id) { - char *title; + char title[120]; sqlite3_stmt *stmt; struct post *post; struct user *user; @@ -111,7 +132,7 @@ showuser(int id) if(sqlite3_bind_int(stmt, 1, id) != SQLITE_OK) goto err; - title = site.name; + snprintf(title, 120, "User: %s", user->full? user->full: user->name); printf("Content-Type: text/html\n\n"); #include "t/user.tc" free(user); diff --git a/ctl.h b/ctl.h index 1dda7af..95c66af 100644 --- a/ctl.h +++ b/ctl.h @@ -1,3 +1,6 @@ +void newatt(void); +void newpost(void); +void newuser(void); void showatt(int); void showfront(void); void showpost(int); -- cgit v1.2.3