From 56509ab16c8d2225182eb0400e5c6a4900870391 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Ankarstr=C3=B6m?= Date: Fri, 17 Sep 2021 22:34:52 +0200 Subject: Fix mktpl bug --- cforum.c | 6 ++--- ctl.c | 13 ++++------- db.c | 16 +++++++++---- misc.c | 19 --------------- misc.h | 1 - mktpl/mktpl | Bin 23988 -> 23988 bytes mktpl/mktpl.c | 71 +++++++++++++++++++++++++++----------------------------- mktpl/mktpl.lex | 21 ++++++++--------- t/err.tc | 2 +- t/front.tc | 6 +++-- t/post.t | 12 +++++++--- t/post.tc | 18 ++++++++++---- 12 files changed, 90 insertions(+), 95 deletions(-) delete mode 100644 misc.c delete mode 100644 misc.h diff --git a/cforum.c b/cforum.c index f29d423..19dee61 100644 --- a/cforum.c +++ b/cforum.c @@ -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(); diff --git a/ctl.c b/ctl.c index c5f90f4..773e4f6 100644 --- a/ctl.c +++ b/ctl.c @@ -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 diff --git a/db.c b/db.c index bd2de17..c6ddab2 100644 --- a/db.c +++ b/db.c @@ -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; diff --git a/misc.c b/misc.c deleted file mode 100644 index e5bdeef..0000000 --- a/misc.c +++ /dev/null @@ -1,19 +0,0 @@ -#include -#include -#include -#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 diff --git a/misc.h b/misc.h deleted file mode 100644 index 733c393..0000000 --- a/misc.h +++ /dev/null @@ -1 +0,0 @@ -char *date(int); \ No newline at end of file diff --git a/mktpl/mktpl b/mktpl/mktpl index b1c459c..092441e 100755 Binary files a/mktpl/mktpl and b/mktpl/mktpl differ 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); -. apnd(); +(.|\n) apnd(); %\> prnt(); BEGIN(0); -(.|\n) apnd(); +(.|\n) apnd(); %\> eval(); BEGIN(0); <> 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 diff --git a/t/err.tc b/t/err.tc index f6d48ab..c4369a9 100644 --- a/t/err.tc +++ b/t/err.tc @@ -2,4 +2,4 @@ printf("

"); printf("%s", title ); printf("

\n

"); printf("%s", err ); -printf("

\n"); +printf("

"); diff --git a/t/front.tc b/t/front.tc index 3307c21..d7afb7b 100644 --- a/t/front.tc +++ b/t/front.tc @@ -6,10 +6,12 @@ printf(" request!

\n\n \ char *p, *v; while(p = nextparam(GET, 512)){ v = split(p); - printf("\n \n \n \n \n \n "); } - printf("\n
"); + +printf("\n
"); printf("%s", p ); printf(""); printf("%s", v ); printf("
"); + +printf("\n"); diff --git a/t/post.t b/t/post.t index 56d3c37..83529d7 100644 --- a/t/post.t +++ b/t/post.t @@ -1,7 +1,13 @@

Post <% printf("%d", id); %>: <%= post->subject %>

-

From: <%= user->full %> -<id); %>"><%= user->name %>> -
Date: <% PFREE(date(post->created)); %> +

From: <%= user->full? user->full: "" %> <% +if(user->full) printf("<"); +%>id); %>"><%= user->name %><% +if(user->full) printf(">"); %> +
Date: <% printdate(post->created); %> +<% if(post->edited){ + printf("
Edited: "); + printdate(post->edited); +} %>

 <%= post->text %>
 
\ No newline at end of file diff --git a/t/post.tc b/t/post.tc index 2f81de0..732f0a7 100644 --- a/t/post.tc +++ b/t/post.tc @@ -3,13 +3,23 @@ printf("%d", id); printf(": "); printf("%s", post->subject ); printf("\n

From: "); -printf("%s", user->full ); -printf("\n<full? user->full: "" ); +printf(" "); +if(user->full) printf("<"); + +printf("id); printf("\">"); printf("%s", user->name ); -printf(">\n
Date: "); -PFREE(date(post->created)); +printf(""); +if(user->full) printf(">"); +printf("\n
Date: "); +printdate(post->created); +printf("\n"); +if(post->edited){ + printf("
Edited: "); + printdate(post->edited); +} printf("\n

\n");
 printf("%s",  post->text );
 printf("\n
"); -- cgit v1.2.3