From 6448ad0923306de21d324687bd74904b099ec2e4 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Mon, 24 Dec 2012 03:31:56 +0000 Subject: [PATCH 1/1] Saving broken stuff --- ct.c | 6 ++--- ct.h | 17 ++++++++++++++ gallery/gallery.c | 56 +++++++++++++++++++++++++++-------------------- gallery/htacces | 4 ---- gallery/htaccess | 4 ++++ gallery/html.ct | 30 ++++++++++++------------- gallery/html.h | 2 +- gallery/mkfile | 14 +++++++----- gallery/mkgallery | 10 +++++++++ lib.c | 43 ++++++++++++++++++++++++++++++++++-- makefile | 35 +++++++++++++++++++++++++++++ mkcommon | 6 ----- mkfile | 15 ------------- parse.y | 18 ++++++++++----- scan.l | 5 ++++- 15 files changed, 183 insertions(+), 82 deletions(-) create mode 100644 ct.h delete mode 100644 gallery/htacces create mode 100644 gallery/htaccess create mode 100755 gallery/mkgallery create mode 100644 makefile delete mode 100644 mkcommon delete mode 100644 mkfile diff --git a/ct.c b/ct.c index 93eac2e..4f784d8 100644 --- a/ct.c +++ b/ct.c @@ -4,7 +4,7 @@ #include "parse.h" -gpointer parse(FILE *input, GList **data, GList **code); +gpointer parse(char *name, FILE *input, GList **data, GList **code); int main(int argc, char **argv) { @@ -21,7 +21,7 @@ int main(int argc, char **argv) /* Handle input and output */ FILE *input = stdin; - if (argv[1] && !g_str_equal(input, "-")) + if (argv[1] && !g_str_equal(argv[1], "-")) input = fopen(argv[1], "r"); if (!input) g_error("invalid input file"); @@ -41,7 +41,7 @@ int main(int argc, char **argv) /* Start compiling */ GList *data = NULL; GList *code = NULL; - parse(input, &data, &code); + parse(argv[1], input, &data, &code); data = g_list_reverse(data); code = g_list_reverse(code); diff --git a/ct.h b/ct.h new file mode 100644 index 0000000..9b89350 --- /dev/null +++ b/ct.h @@ -0,0 +1,17 @@ +#ifndef CT_H +#define CT_H + +/* Misc */ +void ct_print_header(const char *content_type, const char *charset); + +/* Environment */ +const gchar *ct_get_query_string(void); + +const gchar *ct_get_path_info(void); + +const GHashTable *ct_get_query(void); + +/* Markup escaping */ +void ct_use_escape(void); + +#endif diff --git a/gallery/gallery.c b/gallery/gallery.c index 08590cc..5b727dc 100644 --- a/gallery/gallery.c +++ b/gallery/gallery.c @@ -1,10 +1,11 @@ #include #include +#include #include "html.h" -void resize(gchar *orig, gchar *thumb) +void resize(gchar *orig, gchar *thumb, gchar *size) { - gchar *argv[] = {"convert", "-resize", "200x200", orig, thumb, NULL}; + gchar *argv[] = {"convert", "-resize", size, orig, thumb, NULL}; /* god damn glib */ g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, NULL, NULL, NULL); @@ -12,18 +13,21 @@ void resize(gchar *orig, gchar *thumb) GList *gen_thumbs(GList *images) { - if (!g_file_test("thumbs", G_FILE_TEST_EXISTS)) - g_mkdir("thumbs", 0644); - if (!g_file_test("images", G_FILE_TEST_EXISTS)) - g_mkdir("images", 0644); + if (!g_file_test("large", G_FILE_TEST_EXISTS)) g_mkdir("large", 0775); + if (!g_file_test("small", G_FILE_TEST_EXISTS)) g_mkdir("small", 0775); + if (!g_file_test("thumb", G_FILE_TEST_EXISTS)) g_mkdir("thumb", 0775); for (GList *cur = images; cur; cur = cur->next) { gchar *name = cur->data; - gchar *thumb = g_strconcat("thumbs/", name, NULL); - gchar *image = g_strconcat("images/", name, NULL); - if (!g_file_test(thumb, G_FILE_TEST_EXISTS)) - resize(image, thumb); + gchar *large = g_build_filename("large", name, NULL); + gchar *small = g_build_filename("small", name, NULL); + gchar *thumb = g_build_filename("thumb", name, NULL); + if (!g_file_test(thumb, G_FILE_TEST_EXISTS)) { + resize(large, thumb, "200x200"); + resize(large, small, "800x800"); + } + g_free(large); + g_free(small); g_free(thumb); - g_free(image); } return images; } @@ -35,25 +39,29 @@ GList *read_dir(gchar *dirname) GList *images = NULL; while ((name = g_dir_read_name(dir))) images = g_list_prepend(images, g_strdup(name)); + images = g_list_sort(images, (GCompareFunc)g_strcmp0); return images; } int main() { - header(); - g_print("\n"); + ct_print_header("text/html", NULL); - const gchar *query_string = g_getenv("QUERY_STRING"); - if (query_string == NULL) + const gchar *path = ct_get_path_info(); + const gchar *query = ct_get_query_string(); + + GList *thumbs = gen_thumbs(read_dir("large")); + + if (!path || g_str_equal(path, "/")) frame_index(); - else if (g_str_equal(query_string, "noframe")) - frame_nav(TRUE, gen_thumbs(read_dir("images"))); - else if (g_str_equal(query_string, "nav")) - frame_nav(FALSE, gen_thumbs(read_dir("images"))); - else if (g_str_equal(query_string, "head")) + else if (g_str_equal(path, "/head")) frame_head(); - else if (g_str_equal(query_string, "content")) - frame_content(); - else - frame_index(); + else if (g_str_equal(path, "/nav")) + frame_nav(FALSE, thumbs); + else if (g_str_equal(path, "/noframe")) + frame_nav(TRUE, thumbs); + else if (g_str_equal(path, "/show_small")) + frame_show("small", "show_large", query); + else if (g_str_equal(path, "/show_large")) + frame_show("large", "show_small", query); } diff --git a/gallery/htacces b/gallery/htacces deleted file mode 100644 index dc182aa..0000000 --- a/gallery/htacces +++ /dev/null @@ -1,4 +0,0 @@ -Options +ExecCGI -AddHandler cgi-script .bin -RewriteEngine on -RewriteRule ^$ "index.bin" diff --git a/gallery/htaccess b/gallery/htaccess new file mode 100644 index 0000000..581db05 --- /dev/null +++ b/gallery/htaccess @@ -0,0 +1,4 @@ +Options +ExecCGI +AddHandler cgi-script .bin +RewriteEngine on +RewriteRule ^(?!thumb/|small/|large/|images/|mid/|index.bin).*$ "index.bin/$0" diff --git a/gallery/html.ct b/gallery/html.ct index e62621a..20bd065 100644 --- a/gallery/html.ct +++ b/gallery/html.ct @@ -1,18 +1,12 @@ <% #include %> - -<% void header() { %> -Content-Type: text/html; charset=UTF-8 -<% } %> - - <% void frame_index(){ %> - + - - + + @@ -31,7 +25,7 @@ Content-Type: text/html; charset=UTF-8
- No Frames + No Frames
@@ -78,16 +72,20 @@ Content-Type: text/html; charset=UTF-8 <% for (GList *cur = images; cur; cur = cur->next) { %> <% gchar *img = (gchar *)cur->data; %> <% } %> <% } %> - -<% void frame_content(){ %> - - +<% void frame_show(char *this, char *link, char *image){ %> + + + "> + "> + + + <% } %> diff --git a/gallery/html.h b/gallery/html.h index 2fd46e5..48f0d20 100644 --- a/gallery/html.h +++ b/gallery/html.h @@ -6,4 +6,4 @@ void frame_head(); void frame_nav(int square, GList *images); -void frame_content(); +void frame_show(); diff --git a/gallery/mkfile b/gallery/mkfile index b141b31..2a57660 100644 --- a/gallery/mkfile +++ b/gallery/mkfile @@ -1,8 +1,12 @@ -PROGS=gallery -PKGS=glib-2.0 -CLEAN=html.c +PROGS = gallery +PKGS = glib-2.0 +CLEAN = html.c +CPPFLAGS = -I.. +CFLAGS = --std=c99 -Wall -Wno-format -g -default:V: run -gallery: gallery.o html.o +default:V: gallery + QUERY_STRING=foo ./gallery + +gallery: gallery.o html.o ../lib.o <../mkcommon diff --git a/gallery/mkgallery b/gallery/mkgallery new file mode 100755 index 0000000..60b8c75 --- /dev/null +++ b/gallery/mkgallery @@ -0,0 +1,10 @@ +#!/bin/bash + +mkdir -p large small thumb +mv "$@" large + +chgrp apache small thumb +chmod 775 small thumb + +ln -s ~/git/ct/gallery/gallery index.bin +ln -s ~/git/ct/gallery/htaccess .htaccess diff --git a/lib.c b/lib.c index 7b46a05..301273c 100644 --- a/lib.c +++ b/lib.c @@ -1,8 +1,48 @@ #define _GNU_SOURCE +#include #include #include #include +/* Misc */ +void ct_print_header(const char *content_type, const char *charset) +{ + if (!content_type) content_type = "text/html"; + if (!charset) charset = "UTF-8"; + printf("Content-Type: %s; charset=%s\n\n", + content_type, charset); +} + +/* Environment */ +const gchar *ct_get_path_info(void) +{ + return g_getenv("PATH_INFO") ?: ""; +} + +const gchar *ct_get_query_string(void) +{ + return g_getenv("QUERY_STRING") ?: ""; +} + +const GHashTable *ct_get_query(void) +{ + const gchar *query_string = g_getenv("QUERY_STRING"); + GHashTable *query = g_hash_table_new(g_str_hash, g_str_equal); + if (query_string) { + gchar **vars = g_strsplit(query_string, "&", -1); + for (int i = 0; vars[i]; i++) { + gchar **parts = g_strsplit(vars[i], "=", 2); + gchar *lhs = parts[0] ? parts[0] : ""; + gchar *rhs = parts[0] && parts[1] ? parts[1] : ""; + g_hash_table_insert(query, lhs, rhs); + g_free(parts); // keep lhs/rhs + } + g_strfreev(vars); + } + return query; +} + +/* Markup escaping */ static int printf_markup(FILE *stream, const struct printf_info *info, const void *const *args) @@ -33,11 +73,10 @@ void ct_use_escape() register_printf_specifier('M', printf_markup, printf_markup_arginfo); } -int main(void) +void ct_test(void) { ct_use_escape(); printf("%M\n", ""); printf("%M\n", ""); printf("%M\n", ""); - return 0; } diff --git a/makefile b/makefile new file mode 100644 index 0000000..d162b58 --- /dev/null +++ b/makefile @@ -0,0 +1,35 @@ +# Settings +PROG = ct +CC = gcc +YACC = bison +LEX = flex + +CFLAGS = -Wall -g --std=c99 +CPPFLAGS = $(shell pkg-config --cflags glib-2.0) +LDFLAGS = $(shell pkg-config --libs glib-2.0) + +# Targets +default: test + +all: $(PROG) + +test: $(PROG) + ./$(PROG) example/html.ct + +clean: + rm -f $(PROG) *.o *.a parse.h parse.c scan.c + +# Rules +$(PROG): ct.o parse.o scan.o + $(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS) + +%.o: %.c parse.h + $(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $< + +%.c: %.l parse.h + $(LEX) -o $@ $< + +%.c %.h: %.y + $(YACC) -d -o $*.c $*.y + +.SECONDARY: diff --git a/mkcommon b/mkcommon deleted file mode 100644 index 7ef68be..0000000 --- a/mkcommon +++ /dev/null @@ -1,6 +0,0 @@ -%.c: %.ct - (cd .. && mk) - ../ct <$prereq >$target - ex +'norm gg=G' +'wq' $target - -<$HOME/lib/mkcommon diff --git a/mkfile b/mkfile deleted file mode 100644 index cbbaef1..0000000 --- a/mkfile +++ /dev/null @@ -1,15 +0,0 @@ -PROGS=ct lib -PKGS=glib-2.0 -default:V: run -lib-run:V: lib - ./lib -ct-run:V: ct - ./ct < example/html.ct -ct: ct.o parse.o scan.o -ct.o: parse.h -parse.h parse.c: parse.y - bison -d -o parse.c parse.y -scan.c: scan.l parse.h - flex -o scan.c scan.l -CLEAN=parse.h parse.c scan.c -<$HOME/lib/mkcommon diff --git a/parse.y b/parse.y index 84a62be..6415b05 100644 --- a/parse.y +++ b/parse.y @@ -3,19 +3,21 @@ #include #include extern FILE *yyin; +extern int yylineno; int yylex(void); void yyerror(gpointer node, char const *s); +static char *name = NULL; static GList *code = NULL; static GList *data = NULL; %} %error-verbose %parse-param {gpointer root}; -%token START END DATA OUT +%token START END DATA OUT FMT %% input : all | all input ; -all : data | code | print ; +all : data | code | out | fmt ; data : DATA { static int i = 0; @@ -29,16 +31,22 @@ data : DATA { }; code : START DATA END { + code = g_list_prepend(code, g_strdup_printf("#line %d \"%s\"\n", yylineno, name)); code = g_list_prepend(code, g_strdup_printf("%s\n", $2)); }; -print : START OUT DATA END { +out : START OUT DATA END { + code = g_list_prepend(code, g_strdup_printf("printf(\"%%s\", %s);\n", $3)); +}; + +fmt : START FMT DATA END { code = g_list_prepend(code, g_strdup_printf("printf(%s);\n", $3)); }; %% -gpointer parse(FILE *input, GList **_data, GList **_code) { - yyin = input; +gpointer parse(char *_name, FILE *_input, GList **_data, GList **_code) { + name = _name; + yyin = _input; yyparse(NULL); *_data = data; *_code = code; diff --git a/scan.l b/scan.l index c1b61e7..6b03ce4 100644 --- a/scan.l +++ b/scan.l @@ -7,17 +7,20 @@ %option noyywrap %option nounput %option noinput +%option yylineno +%option never-interactive /* %option nodebug */ START [ \t]*<% END [ \t]*%>[ \t]* DATA ([^<\n]|<[^%])*\n* CODE [ \t\n]([^%\n]|%[^>])*\n* -%s IN FMT +%s IN %% [[:space:]]*\n { debug("NL [%s]"); } {START} { debug("START [%s]"); yylval = g_strdup(yytext); BEGIN(IN); return START; } {DATA} { debug("DATA [%s]"); yylval = g_strdup(yytext); return DATA; } {END} { debug("END [%s]"); yylval = g_strdup(yytext); BEGIN(INITIAL); return END; } = { debug("OUT [%s]"); yylval = g_strdup(yytext); return OUT; } +% { debug("FMT [%s]"); yylval = g_strdup(yytext); return FMT; } {CODE} { debug("CODE [%s]"); yylval = g_strdup(yytext); return DATA; } %% -- 2.43.2