diff options
author | John Ankarström <john@ankarstrom.se> | 2021-09-17 22:34:52 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2021-09-17 22:34:52 +0200 |
commit | 56509ab16c8d2225182eb0400e5c6a4900870391 (patch) | |
tree | e3c3bcc760ecc187a3f4ee8c6616855d15e86d83 | |
parent | 3e400c3df5572524b7ba08b030f45eb3ba6b7734 (diff) | |
download | cforum-56509ab16c8d2225182eb0400e5c6a4900870391.tar.gz |
Fix mktpl bug
-rw-r--r-- | cforum.c | 6 | ||||
-rw-r--r-- | ctl.c | 13 | ||||
-rw-r--r-- | db.c | 16 | ||||
-rw-r--r-- | misc.c | 19 | ||||
-rw-r--r-- | misc.h | 1 | ||||
-rwxr-xr-x | mktpl/mktpl | bin | 23988 -> 23988 bytes | |||
-rw-r--r-- | mktpl/mktpl.c | 71 | ||||
-rw-r--r-- | mktpl/mktpl.lex | 21 | ||||
-rw-r--r-- | t/err.tc | 2 | ||||
-rw-r--r-- | t/front.tc | 6 | ||||
-rw-r--r-- | t/post.t | 12 | ||||
-rw-r--r-- | t/post.tc | 18 |
12 files changed, 90 insertions, 95 deletions
@@ -70,7 +70,7 @@ main(int argc, char *argv[]) } /* Parse query string. */ - postid = userid = -1; + postid = userid = 0; while(p = nextparam(GET, 128)){ v = split(p); if(strcmp(p, "post") == 0) postid = atoi(v); @@ -78,9 +78,9 @@ main(int argc, char *argv[]) } /* Handle request. */ - if(postid != -1) + if(postid) showpost(postid); - else if(userid != -1) + else if(userid) showuser(userid); else showfront(); @@ -6,21 +6,16 @@ #include "query.h" #include "site.h" -#define PFREE(s) do{ printf("%s", s); free(s); }while(0) - -char * -date(int timestamp) +void +printdate(int timestamp) { - char *buf; + char buf[20]; struct tm *t; - if(!(buf = malloc(20))) - err(1, "malloc"); - t = localtime((time_t *)×tamp); strftime(buf, 20, "%Y-%m-%d %H:%M", t); - return buf; + printf(buf); } void @@ -21,6 +21,12 @@ byid(int id, char *sql) return stmt; } +char * +textdup(const unsigned char *s) +{ + return s? strdup((char *)s): NULL; +} + struct post * getpost(int id) { @@ -39,8 +45,8 @@ getpost(int id) post->user = sqlite3_column_int(stmt, 1); post->created = sqlite3_column_int(stmt, 2); post->edited = sqlite3_column_int(stmt, 3); - post->subject = strdup((char *)sqlite3_column_text(stmt, 4)); - post->text = strdup((char *)sqlite3_column_text(stmt, 5)); + post->subject = textdup(sqlite3_column_text(stmt, 4)); + post->text = textdup(sqlite3_column_text(stmt, 5)); sqlite3_finalize(stmt); return post; @@ -61,9 +67,9 @@ getuser(int id) err(1, "malloc"); user->id = id; - user->name = strdup((char *)sqlite3_column_text(stmt, 0)); - user->full = strdup((char *)sqlite3_column_text(stmt, 1)); - user->hash = strdup((char *)sqlite3_column_text(stmt, 2)); + user->name = textdup(sqlite3_column_text(stmt, 0)); + user->full = textdup(sqlite3_column_text(stmt, 1)); + user->hash = textdup(sqlite3_column_text(stmt, 2)); sqlite3_finalize(stmt); return user; @@ -1,19 +0,0 @@ -#include <err.h> -#include <time.h> -#include <stdio.h> -#include "misc.h" - -char * -date(int timestamp) -{ - char *buf; - time_t *t; - - if(!(buf = malloc(20))) - err(1, "malloc"); - - t = localtime(×tamp); - strftime(buf, 20, "%Y-%m-%d %H:%M", t); - - return buf; -}
\ No newline at end of file @@ -1 +0,0 @@ -char *date(int);
\ No newline at end of file diff --git a/mktpl/mktpl b/mktpl/mktpl Binary files differindex b1c459c..092441e 100755 --- a/mktpl/mktpl +++ b/mktpl/mktpl diff --git a/mktpl/mktpl.c b/mktpl/mktpl.c index 7ed1626..2ebc343 100644 --- a/mktpl/mktpl.c +++ b/mktpl/mktpl.c @@ -285,10 +285,10 @@ static void yy_fatal_error YY_PROTO(( yyconst char msg[] )); #define YY_NUM_RULES 8 #define YY_END_OF_BUFFER 9 -static yyconst short int yy_accept[21] = +static yyconst short int yy_accept[20] = { 0, 0, 0, 0, 0, 0, 0, 9, 1, 1, 4, - 8, 4, 6, 6, 0, 5, 7, 3, 2, 0 + 4, 6, 6, 0, 5, 7, 3, 2, 0 } ; static yyconst int yy_ec[256] = @@ -328,34 +328,32 @@ static yyconst int yy_meta[7] = 1, 1, 2, 1, 1, 3 } ; -static yyconst short int yy_base[28] = +static yyconst short int yy_base[27] = { 0, - 22, 21, 0, 2, 21, 20, 22, 27, 0, 27, - 27, 0, 27, 0, 16, 27, 27, 27, 27, 27, - 5, 8, 11, 18, 16, 15, 14 + 19, 18, 18, 17, 16, 15, 17, 24, 0, 24, + 0, 24, 0, 11, 24, 24, 24, 24, 24, 0, + 3, 6, 13, 11, 10, 9 } ; -static yyconst short int yy_def[28] = +static yyconst short int yy_def[27] = { 0, - 21, 21, 22, 22, 23, 23, 20, 20, 24, 20, - 20, 25, 20, 26, 27, 20, 20, 20, 20, 0, - 20, 20, 20, 20, 20, 20, 20 + 20, 20, 21, 21, 22, 22, 19, 19, 23, 19, + 24, 19, 25, 26, 19, 19, 19, 19, 0, 19, + 19, 19, 19, 19, 19, 19 } ; -static yyconst short int yy_nxt[34] = +static yyconst short int yy_nxt[31] = { 0, - 20, 11, 12, 11, 12, 8, 8, 8, 10, 10, - 10, 13, 13, 13, 18, 18, 18, 17, 16, 15, - 19, 20, 14, 14, 9, 9, 7, 20, 20, 20, - 20, 20, 20 + 8, 8, 8, 10, 10, 10, 12, 12, 12, 17, + 17, 17, 16, 15, 14, 18, 19, 13, 13, 11, + 11, 9, 9, 7, 19, 19, 19, 19, 19, 19 } ; -static yyconst short int yy_chk[34] = +static yyconst short int yy_chk[31] = { 0, - 0, 3, 3, 4, 4, 21, 21, 21, 22, 22, - 22, 23, 23, 23, 27, 27, 27, 26, 25, 24, - 15, 7, 6, 5, 2, 1, 20, 20, 20, 20, - 20, 20, 20 + 20, 20, 20, 21, 21, 21, 22, 22, 22, 26, + 26, 26, 25, 24, 23, 14, 7, 6, 5, 4, + 3, 2, 1, 19, 19, 19, 19, 19, 19, 19 } ; static yy_state_type yy_last_accepting_state; @@ -386,7 +384,7 @@ void prnt(); char *buf; int len; int sz; -#line 390 "mktpl.c" +#line 388 "mktpl.c" /* Macros after this point can all be overridden by user definitions in * section 1. @@ -539,7 +537,7 @@ YY_DECL #line 18 "mktpl.lex" -#line 543 "mktpl.c" +#line 541 "mktpl.c" if ( yy_init ) { @@ -590,13 +588,13 @@ yy_match: while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) { yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 21 ) + if ( yy_current_state >= 20 ) yy_c = yy_meta[(unsigned int) yy_c]; } yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; ++yy_cp; } - while ( yy_base[yy_current_state] != 27 ); + while ( yy_base[yy_current_state] != 24 ); yy_find_action: yy_act = yy_accept[yy_current_state]; @@ -674,7 +672,7 @@ YY_RULE_SETUP #line 32 "mktpl.lex" ECHO; YY_BREAK -#line 678 "mktpl.c" +#line 676 "mktpl.c" case YY_END_OF_BUFFER: { @@ -964,7 +962,7 @@ static yy_state_type yy_get_previous_state() while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) { yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 21 ) + if ( yy_current_state >= 20 ) yy_c = yy_meta[(unsigned int) yy_c]; } yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; @@ -999,11 +997,11 @@ yy_state_type yy_current_state; while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state ) { yy_current_state = (int) yy_def[yy_current_state]; - if ( yy_current_state >= 21 ) + if ( yy_current_state >= 20 ) yy_c = yy_meta[(unsigned int) yy_c]; } yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c]; - yy_is_jam = (yy_current_state == 20); + yy_is_jam = (yy_current_state == 19); return yy_is_jam ? 0 : yy_current_state; } @@ -1582,27 +1580,26 @@ apnd() } buf[len++] = *yytext; - buf[len+1] = 0; } void eval() { - printf("%s\n", buf); + printf("%.*s\n", len, buf); len = 0; } void text() { - char *t; + int i; printf("printf(\""); - for(t = buf; *t; t++){ - if(*t == '\\') printf("\\\\"); - else if(*t == '"') printf("\\\""); - else if(*t == '\n') printf("\\n"); - else printf("%c", *t); + for(i = 0; i < len; i++){ + if(buf[i] == '\\') printf("\\\\"); + else if(buf[i] == '"') printf("\\\""); + else if(buf[i] == '\n') printf("\\n"); + else printf("%c", buf[i]); } printf("\");\n"); len = 0; @@ -1611,6 +1608,6 @@ text() void prnt() { - printf("printf(\"%%s\", %s);\n", buf); + printf("printf(\"%%s\", %.*s);\n", len, buf); len = 0; }
\ No newline at end of file diff --git a/mktpl/mktpl.lex b/mktpl/mktpl.lex index e530e45..56ce93f 100644 --- a/mktpl/mktpl.lex +++ b/mktpl/mktpl.lex @@ -20,10 +20,10 @@ \<%= text(); BEGIN(PRNT); \<%[^=] text(); BEGIN(EVAL); -<PRNT>. apnd(); +<PRNT>(.|\n) apnd(); <PRNT>%\> prnt(); BEGIN(0); -<EVAL>(.|\n) apnd(); +<EVAL>(.|\n) apnd(); <EVAL>%\> eval(); BEGIN(0); <PRNT><<EOF>> prnt(); yyterminate(); @@ -52,27 +52,26 @@ apnd() } buf[len++] = *yytext; - buf[len+1] = 0; } void eval() { - printf("%s\n", buf); + printf("%.*s\n", len, buf); len = 0; } void text() { - char *t; + int i; printf("printf(\""); - for(t = buf; *t; t++){ - if(*t == '\\') printf("\\\\"); - else if(*t == '"') printf("\\\""); - else if(*t == '\n') printf("\\n"); - else printf("%c", *t); + for(i = 0; i < len; i++){ + if(buf[i] == '\\') printf("\\\\"); + else if(buf[i] == '"') printf("\\\""); + else if(buf[i] == '\n') printf("\\n"); + else printf("%c", buf[i]); } printf("\");\n"); len = 0; @@ -81,6 +80,6 @@ text() void prnt() { - printf("printf(\"%%s\", %s);\n", buf); + printf("printf(\"%%s\", %.*s);\n", len, buf); len = 0; }
\ No newline at end of file @@ -2,4 +2,4 @@ printf("<h1>"); printf("%s", title ); printf("</h1>\n<p>"); printf("%s", err ); -printf("</p>\n"); +printf("</p>"); @@ -6,10 +6,12 @@ printf(" request!</p>\n<table border=\"1\">\n <tr style=\"font-weight: bold;\">\ char *p, *v; while(p = nextparam(GET, 512)){ v = split(p); - printf("\n <tr>\n <td>"); + +printf("\n <tr>\n <td>"); printf("%s", p ); printf("</td>\n <td>"); printf("%s", v ); printf("</td>\n </tr>\n "); } - printf("\n</table>"); + +printf("\n</table>"); @@ -1,7 +1,13 @@ <h1>Post <% printf("%d", id); %>: <%= post->subject %></h1> -<p>From: <%= user->full %> -<<a href="?user=<% printf("%d", user->id); %>"><%= user->name %></a>> -<br>Date: <% PFREE(date(post->created)); %> +<p>From: <%= user->full? user->full: "" %> <% +if(user->full) printf("<"); +%><a href="?user=<% printf("%d", user->id); %>"><%= user->name %></a><% +if(user->full) printf(">"); %> +<br>Date: <% printdate(post->created); %> +<% if(post->edited){ + printf("<br>Edited: "); + printdate(post->edited); +} %> <pre> <%= post->text %> </pre>
\ No newline at end of file @@ -3,13 +3,23 @@ printf("%d", id); printf(": "); printf("%s", post->subject ); printf("</h1>\n<p>From: "); -printf("%s", user->full ); -printf("\n<<a href=\"?user="); +printf("%s", user->full? user->full: "" ); +printf(" "); +if(user->full) printf("<"); + +printf("<a href=\"?user="); printf("%d", user->id); printf("\">"); printf("%s", user->name ); -printf("</a>>\n<br>Date: "); -PFREE(date(post->created)); +printf("</a>"); +if(user->full) printf(">"); +printf("\n<br>Date: "); +printdate(post->created); +printf("\n"); +if(post->edited){ + printf("<br>Edited: "); + printdate(post->edited); +} printf("\n<pre>\n"); printf("%s", post->text ); printf("\n</pre>"); |