From ae5da79f619f34871ad61981ee63638dd3bc8dae Mon Sep 17 00:00:00 2001 From: Joris Vink Date: Mon, 24 Jun 2013 18:22:35 +0200 Subject: [PATCH] new build script for modules, which should be used as a base for all new modules written by others. --- README | 2 +- docs/TODO | 2 - example/Makefile | 49 ------------- example/html/profile.html | 14 ---- modules/example/build.sh | 71 +++++++++++++++++++ .../html => modules/example/media}/index.html | 1 + .../css => modules/example/media}/style.css | 0 example.conf => modules/example/module.conf | 3 +- {example => modules/example}/src/example.c | 13 ---- modules/example/static.h | 18 +++++ .../example/tools/inject.c | 63 +++++++++------- 11 files changed, 131 insertions(+), 105 deletions(-) delete mode 100644 example/Makefile delete mode 100644 example/html/profile.html create mode 100755 modules/example/build.sh rename {example/html => modules/example/media}/index.html (96%) rename {example/css => modules/example/media}/style.css (100%) rename example.conf => modules/example/module.conf (95%) rename {example => modules/example}/src/example.c (89%) create mode 100644 modules/example/static.h rename example/tools/html_inject.c => modules/example/tools/inject.c (68%) diff --git a/README b/README index 62c7b27..ff30b4b 100644 --- a/README +++ b/README @@ -30,6 +30,6 @@ Platforms support Any other BSD might function, but is untested. Right now Kore development is a moving process, so expect bugs. -If you run into said bugs please contact me at patches@coders.se. +If you run into said bugs please contact me at joris@coders.se. More information can be found on https://kore.io/ diff --git a/docs/TODO b/docs/TODO index 0ed5420..1722cd2 100644 --- a/docs/TODO +++ b/docs/TODO @@ -1,6 +1,4 @@ -*BSD support Auxiliary library framework -Better logging facilities Ability to load one module per domain GET arguments (only POST supported) POST multiform/form-data support diff --git a/example/Makefile b/example/Makefile deleted file mode 100644 index 08e03b0..0000000 --- a/example/Makefile +++ /dev/null @@ -1,49 +0,0 @@ -# Example Kore module - -.SUFFIXES: .html .css - -CC=gcc -BIN=example.module - -HTML= html/index.html html/profile.html -H_SRCS= $(HTML:.html=.c) - -CSS= css/style.css -C_SRCS= $(CSS:.css=.c) - -S_SRC= src/example.c $(H_SRCS) $(C_SRCS) -S_OBJS= $(S_SRC:.c=.o) - -CFLAGS+=-I. -I../includes -CFLAGS+=-Wall -Wstrict-prototypes -Wmissing-prototypes -CFLAGS+=-Wmissing-declarations -Wshadow -Wpointer-arith -Wcast-qual -CFLAGS+=-Wsign-compare -g -LDFLAGS+=-shared - -all: - make clean - make example.module - -example.module: html_inject $(H_SRCS) $(C_SRCS) $(S_OBJS) - $(CC) $(LDFLAGS) $(S_OBJS) -o $(BIN) - make clean_o - -html_inject: tools/html_inject.c - $(CC) $(CFLAGS) tools/html_inject.c -o tools/html_inject - -.html.c: - tools/html_inject $< `basename $<` > $@ - -.css.c: - tools/html_inject $< `basename $<` > $@ - -.c.o: - $(CC) -fPIC $(CFLAGS) -c $< -o $@ - -clean: - make clean_o - rm -f css/*.c html/*.c tools/html_inject $(BIN) - rm -f static.h - -clean_o: - rm -f css/*.o html/*.o src/*.o diff --git a/example/html/profile.html b/example/html/profile.html deleted file mode 100644 index 0d9b101..0000000 --- a/example/html/profile.html +++ /dev/null @@ -1,14 +0,0 @@ - - - - Your KORE module worked! - - - - -
-

This is a dynamic (regex) path example

-
- - - diff --git a/modules/example/build.sh b/modules/example/build.sh new file mode 100755 index 0000000..3d95ab2 --- /dev/null +++ b/modules/example/build.sh @@ -0,0 +1,71 @@ +#!/bin/sh +# +# Copyright (c) 2013 Joris Vink +# +# Kore module build script, use this as a base for building +# your own modules for kore. + +# The name of the module you will be building +MODULE=example.module + +# The directory containing all your media files (HTML, CSS, ...). +# These files will be compiled into the module and symbols will +# be exported for you to use in your code (see static.h after building). +MEDIA_DIR=media + +# The directory containing your module source. +SOURCE_DIR=src + +# The directory containing the Kore source code. +KORE_DIR=../../ + +# Compiler settings. +CC=gcc +CFLAGS="-I. -I${KORE_DIR}includes -Wall -Wstrict-prototypes \ + -Wmissing-prototypes -Wmissing-declarations -Wshadow \ + -Wpointer-arith -Wcast-qual -Wsign-compare -g" +LDFLAGS+=-shared + +# Functions used in the build process. +function create_and_empty_dir { + if [ ! -d $1 ]; then + mkdir $1; + fi + + rm -f $1/* +} + +### Begin building #### +echo "Building module ${MODULE}..." +rm -f ${MODULE} + +${CC} ${CFLAGS} tools/inject.c -o tools/inject + +create_and_empty_dir ${SOURCE_DIR}/${MEDIA_DIR} +create_and_empty_dir .objs + +for file in `find ${MEDIA_DIR} -type f`; do + echo "Injecting $file"; + base=`basename $file`; + ./tools/inject $file $base > ${SOURCE_DIR}/${MEDIA_DIR}/${base}.c; + if [ $? -ne 0 ]; then + echo "Injection error, check above messages for clues."; + exit 1; + fi +done + +for src in `find ${SOURCE_DIR} -type f`; do + base=`basename $src`; + ${CC} ${CFLAGS} -fPIC -c $src -o .objs/${base}.o + if [ $? -ne 0 ]; then + echo "Build error, check above messages for clues."; + exit 1; + fi +done + +${CC} ${LDFLAGS} `find .objs -name \*.o -type f` -o ${MODULE} +echo "Building completed!" + +rm -rf ${SOURCE_DIR}/${MEDIA_DIR} +rm -rf .objs +rm -f tools/inject diff --git a/example/html/index.html b/modules/example/media/index.html similarity index 96% rename from example/html/index.html rename to modules/example/media/index.html index bf8b7d7..6aaa70d 100644 --- a/example/html/index.html +++ b/modules/example/media/index.html @@ -1,4 +1,5 @@ + Your KORE module worked! diff --git a/example/css/style.css b/modules/example/media/style.css similarity index 100% rename from example/css/style.css rename to modules/example/media/style.css diff --git a/example.conf b/modules/example/module.conf similarity index 95% rename from example.conf rename to modules/example/module.conf index c6a96b0..1396da4 100644 --- a/example.conf +++ b/modules/example/module.conf @@ -20,7 +20,7 @@ workers 2 #onload myinit # Specifies what module to be loaded. -load example/example.module +load ./example.module # Domain configuration # @@ -46,7 +46,6 @@ domain 10.211.55.3 { accesslog /var/log/kore_access.log static /css/style.css serve_style_css static / serve_index - dynamic ^/[a-z0-9_]*$ serve_profile } #domain domain.com { diff --git a/example/src/example.c b/modules/example/src/example.c similarity index 89% rename from example/src/example.c rename to modules/example/src/example.c index b8729a8..327b6ad 100644 --- a/example/src/example.c +++ b/modules/example/src/example.c @@ -42,7 +42,6 @@ int serve_style_css(struct http_request *); int serve_index(struct http_request *); -int serve_profile(struct http_request *); int serve_style_css(struct http_request *req) @@ -85,15 +84,3 @@ serve_index(struct http_request *req) return (ret); } - -int -serve_profile(struct http_request *req) -{ - int ret; - - http_response_header_add(req, "content-type", "text/html"); - ret = http_response(req, 200, static_html_profile, - static_len_html_profile); - - return (ret); -} diff --git a/modules/example/static.h b/modules/example/static.h new file mode 100644 index 0000000..e7efa25 --- /dev/null +++ b/modules/example/static.h @@ -0,0 +1,18 @@ +extern u_int8_t static_css_style[]; +extern u_int32_t static_len_css_style; +extern time_t static_mtime_css_style; +extern u_int8_t static_html_index[]; +extern u_int32_t static_len_html_index; +extern time_t static_mtime_html_index; +extern u_int8_t static_css_style[]; +extern u_int32_t static_len_css_style; +extern time_t static_mtime_css_style; +extern u_int8_t static_html_index[]; +extern u_int32_t static_len_html_index; +extern time_t static_mtime_html_index; +extern u_int8_t static_css_style[]; +extern u_int32_t static_len_css_style; +extern time_t static_mtime_css_style; +extern u_int8_t static_html_index[]; +extern u_int32_t static_len_html_index; +extern time_t static_mtime_html_index; diff --git a/example/tools/html_inject.c b/modules/example/tools/inject.c similarity index 68% rename from example/tools/html_inject.c rename to modules/example/tools/inject.c index 2e75356..1e4876f 100755 --- a/example/tools/html_inject.c +++ b/modules/example/tools/inject.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -29,52 +30,66 @@ int main(int argc, char *argv[]) { struct stat st; - size_t len; - FILE *fp, *hdr; - char *ext, *p, *c, buf[1024]; + ssize_t len; + FILE *hdr; + char *ext, c[1]; + int fd, newline, count; if (argc != 3) - err(1, "arguments"); - if ((fp = fopen(argv[1], "r")) == NULL) - err(1, "fopen() %d", errno); + exit(1); + + if ((fd = open(argv[1], O_RDONLY)) == -1) + err(1, "open() %d", errno); if ((hdr = fopen("static.h", "a+")) == NULL) err(1, "fopen() %d", errno); if ((ext = strchr(argv[2], '.')) != NULL) *(ext)++ = '\0'; + else + ext = ""; if (stat(argv[1], &st) == -1) { printf("stat(%s) failed: %d\n", argv[1], errno); - exit(99); + exit(1); } printf("/**** AUTO GENERATED BY MAKEFILE - DO NOT TOUCH ****/\n"); printf("#include \n\n"); - printf("u_int8_t *static_%s_%s = (u_int8_t *)", ext, argv[2]); + printf("u_int8_t static_%s_%s[] = {", ext, argv[2]); len = 0; - while (fgets(buf, sizeof(buf), fp)) { - if ((p = strchr(buf, '\n')) != NULL) - *p = '\0'; - - printf("\n\t\""); - for (c = buf; *c != '\0'; c++) { - if (*c == '"' || *c == '\\') - printf("\\"); - printf("%c", *c); - len++; + count = 0; + newline = 1; + for (;;) { + if (newline) { + printf("\n\t"); + count = 0; + newline = 0; } - printf("\\n\""); - len++; + len = read(fd, c, 1); + if (len == 0) + break; + + if (len == -1) { + printf("read(): %d\n", errno); + exit(1); + } + + if (len != 1) + exit(1); + + printf("0x%02x, ", c[0]); + if (count++ == 10) + newline = 1; } - fclose(fp); + close(fd); - printf(";\n\n"); - printf("u_int32_t static_len_%s_%s = %ld;\n", ext, argv[2], len); + printf("};\n\n"); + printf("u_int32_t static_len_%s_%s = %ld;\n", ext, argv[2], st.st_size); printf("time_t static_mtime_%s_%s = %ld;\n", ext, argv[2], st.st_mtime); - fprintf(hdr, "extern u_int8_t *static_%s_%s;\n", ext, argv[2]); + fprintf(hdr, "extern u_int8_t static_%s_%s[];\n", ext, argv[2]); fprintf(hdr, "extern u_int32_t static_len_%s_%s;\n", ext, argv[2]); fprintf(hdr, "extern time_t static_mtime_%s_%s;\n", ext, argv[2]); fclose(hdr);