%{ /* * Copyright (c) 2006 * Aleksey Pesternikov. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include "htpl_driver.h" char* tplargs=0; char* funcname=0; char* funcprefix="tpl_"; FILE* hout=0; struct htpl_driver* drv=&apache_driver; %} %x ST_IN_HDR %x ST_IN_TPLARGS %x ST_IN_HTML %x ST_IN_SCRIPTING %x ST_IN_VAR %x ST_IN_IVAR WS [[:blank:]]+ OPTWS [[:blank:]]* NL \r?\n %% [ \t\r\n]*"<#" { drv->start(yyout); BEGIN(ST_IN_HDR); } . { drv->start(yyout); yyless(0); drv->finish_header(hout, yyout); BEGIN(ST_IN_HTML); } "#>" { drv->finish_header(hout, yyout); BEGIN(ST_IN_HTML); } ^"#define"{WS}"TPLARGS" { ECHO; BEGIN(ST_IN_TPLARGS); } .*{NL} { tplargs=strdup(yytext); ECHO; char* cp; if((cp=strchr(tplargs, '\n'))!=0) *cp=0; if((cp=strchr(tplargs, '\r'))!=0) *cp=0; BEGIN(ST_IN_HDR); } . { // printf("HDR>"); ECHO; } ("<\?x")|(([^<\$#]|"<"[^\?]|[\$#][^\{]){1,400})|"<" { // printf("TEXT: '%*s'\n", yyleng, yytext); drv->out_static_block(yyout, yytext, yyleng); } {NL}?"<\?"[^x] { // printf("script begins\n"); BEGIN(ST_IN_SCRIPTING); } "${" { // printf("VAR begins\n"); BEGIN(ST_IN_VAR); } "#{" { // printf("VAR begins\n"); BEGIN(ST_IN_IVAR); } <> { fprintf(yyout, "\n}\n"); yyterminate(); } "\?>"{NL}? { // printf("script ends\n"); fprintf(yyout, "\n"); BEGIN(ST_IN_HTML); } [^\?]|"?"[!>] { ECHO; // printf("script: '%*s'\n", yyleng, yytext); } "}" { // printf("VAR ends"); BEGIN(ST_IN_HTML); } "}" { // printf("VAR ends"); BEGIN(ST_IN_HTML); } [^}]{1,400} { // printf("VAR: '%*s'\n", yyleng, yytext); drv->out_dynamic_block(yyout, yytext, yyleng); } [^}]{1,400} { // printf("IVAR: '%*s'\n", yyleng, yytext); drv->out_printf_block(yyout, yytext, yyleng); } %% int yylex(); void usage(){ fprintf(stderr, "Usage: %s [-o output_filename] [-h header_output_filename] [-f funcname] [-p prefix] [-d D] [file]\n", getprogname()); fprintf(stderr, " D is one-letter driver identifier.\n"); fprintf(stderr, " a Apache\n"); fprintf(stderr, " c CGI\n"); exit(1); } int main( argc, argv ) int argc; char *argv[]; { int ch; char* output_filename=0; char* header_output_filename=0; char* infilename=0; while ((ch = getopt(argc, argv, "o:h:f:p:d:")) != -1) { switch (ch) { case 'o': output_filename = optarg; break; case 'h': header_output_filename=optarg; break; case 'f': funcname=optarg; break; case 'p': funcprefix=optarg; break; case 'd': //driver switch(*optarg){ case 'a': //Apache (default) drv=&apache_driver; break; case 'f': //FFast drv=&ff_driver; break; case 'c': drv=&cgi_driver; break; default: usage(); } break; default: usage(); } } argc -= optind; argv += optind; if(argc>=1) { infilename=argv[0]; if(!funcname) { funcname=strdup(infilename); char* cp; if((cp = strrchr(funcname, '/'))!=0) funcname=cp+1; if((cp = strchr(funcname, '.'))!=0) *cp=0; } } if(infilename) { yyin = fopen(infilename, "r"); if(!yyin) { perror(infilename); exit(1); } } if(funcname) { if(!output_filename) asprintf(&output_filename, "%s.c", funcname); if(!header_output_filename) asprintf(&header_output_filename, "%s.h", funcname); } if(output_filename) { yyout = fopen(output_filename, "w"); if(!yyout) { perror(output_filename); exit(1); } if(!header_output_filename) { asprintf(&header_output_filename, "%s.h", output_filename); } } if(header_output_filename) { hout = fopen(header_output_filename, "w"); if(!hout) { perror(header_output_filename); exit(1); } } if(!funcname) funcname="handler"; while ( yylex() != 0 ) ; return 0; }