OSDN Git Service

first commit
authorHidetaka Sakai <sakaihdt@debian-r8.sakaihdt.jp>
Wed, 30 Dec 2009 09:10:16 +0000 (18:10 +0900)
committerHidetaka Sakai <sakaihdt@debian-r8.sakaihdt.jp>
Wed, 30 Dec 2009 09:10:16 +0000 (18:10 +0900)
all files

12 files changed:
Makefile [new file with mode: 0644]
cgigenerator.pl [new file with mode: 0755]
sample.cgc [new file with mode: 0644]
sample1/Makefile [new file with mode: 0644]
sample1/sample1.cgc [new file with mode: 0644]
sample2/Makefile [new file with mode: 0644]
sample2/background.png [new file with mode: 0644]
sample2/off_button.png [new file with mode: 0644]
sample2/on_button.png [new file with mode: 0644]
sample2/sample2.cgc [new file with mode: 0644]
testdir/Makefile [new file with mode: 0644]
testdir/test.dat [new file with mode: 0644]

diff --git a/Makefile b/Makefile
new file mode 100644 (file)
index 0000000..82d4625
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,9 @@
+
+
+all:
+       make -C testdir
+       make -C sample1
+       make -C sample2
+
+       
+
diff --git a/cgigenerator.pl b/cgigenerator.pl
new file mode 100755 (executable)
index 0000000..0b54b88
--- /dev/null
@@ -0,0 +1,222 @@
+#!/usr/bin/perl
+#
+# cgigenerator.pl
+#
+# Copyright (C) 2009 Hidetaka Sakai
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+#
+#
+# =======================
+# This program convert jsp style text to Apache cgi c program .
+# This is fileter program.
+#
+# usage :
+#   - help usage 
+#     ./cgigenerator.pl -h  
+#
+#   - option use
+#     ./cgigenerator.pl -i <srcfile> -o <dstfile>
+#   - filetr program
+#     ./cgigenerator.pl < <srcfile> > <dstfile>
+#
+#   - printf change
+#     ./cgigenerator.pl -f samprintf -i <srcfile> -o <dstfile>
+#
+# can use tags:
+#   - C progaram 
+#     <%  
+#      ....
+#     %>
+#     -> not convert 
+#   - numeric value
+#     <%d=1%>
+#     -> printf( "%d", 1 );
+#   - string value
+#     <%d="sample"%>
+#     -> printf( "%s", "samble" );
+#   - normal string
+#     <input type="text" name="sample1" value="ok">
+#     -> printf( "<input type=\"text\" name=\"sample1\" value=\"ok\">\n" );
+# 
+#  you can read sample dir.
+#
+#
+#
+
+my $PRGNAM= "cgigenerator.pl";
+
+sub usage(){
+print( " $PRGNAM usage : \n" );
+print( "   - help usage \n");
+print( "     ./cgigenerator.pl -h  \n");
+print( "\n");
+print( "   - option use\n");
+print( "     ./cgigenerator.pl -i <srcfile> -o <dstfile>\n");
+print( "   - filetr program\n");
+print( "     ./cgigenerator.pl < <srcfile> > <dstfile>\n");
+print( "\n");
+print( "   - printf change\n");
+print( "     ./cgigenerator.pl -f samprintf -i <srcfile> -o <dstfile>\n");
+print( "\n");
+print( " can use tags:\n");
+print( "   - C progaram \n");
+print( "     <%  \n");
+print( "      ....\n");
+print( "     %>\n");
+print( "     -> not convert \n");
+print( "   - numeric value\n");
+print( "     <%d=1%>\n");
+print( "     -> printf( \"%d\", 1 );\n");
+print( "   - string value\n");
+print( "     <%d=\"sample\"%>\n");
+print( "     -> printf( \"%s\", \"samble\" );\n");
+print( "   - normal string\n");
+print( "     <input type=\"text\" name=\"sample1\" value=\"ok\">\n");
+print( "     -> printf( \"<input type=\\\"text\\\" name=\\\"sample1\\\" value=\\\"ok\\\">\\n\" );\n");
+print( " \n");
+print( "  you can read sample dir. \n" );
+}
+
+
+
+use Getopt::Std;
+
+# my $DEBUG = 1;
+
+################################
+# parameter valiable
+################################
+my $MODE_HTML = 0;
+my $MODE_C    = 1;
+my $PRINTFUNC_ST = "printf(";
+my $PRINTFUNC_ED = ")";
+my $INPUTFILENM  = "STDIN";
+my $OUTPUTFILENM = "STDOUT";
+my $mode = $MODE_HTML; 
+my @lines ;
+
+my %opts = ();
+
+################################
+# option parse
+################################
+getopts ("i:o:f:h", \%opts);
+# foreach $key(keys %opts ) {
+#   print "$key = $opts{$key}\n";
+#}
+if( exists $opts{"h"}  ) {
+       usage();
+       exit(0);
+}
+if( exists $opts{"f"}  ) {
+       my $tmp = $opts{"f"} ;
+       $PRINTFUNC_ST = "$tmp(";
+}
+if( exists $opts{"i"}  ) {
+       $INPUTFILENM = $opts{"i"};
+       if( !  open( IN, "< $INPUTFILENM" ) ) {
+               die("can't open($INPUTFILENM) :$!");
+       }
+       @lines = <IN>;
+       close( IN );    
+}else{
+       # read stdin;
+       @lines = <STDIN>;
+}
+
+
+
+################################
+# temporary valiable
+################################
+my $linestr="";
+my $linecnt=0;
+my $tmpstr = "";
+my $printstr = "";
+
+################################
+# Start Convert
+################################
+print(  "#line 1 \"$INPUTFILENM\"\n" );
+foreach $linestr (@lines) {
+       $linecnt++;
+       $tmpstr = $linestr;
+
+#print "@ line:$linecnt -> mode:$mode tmpstr:$tmpstr \n";
+       while( ( $tmpstr =~ /^[\r\n]+$/) || ( $tmpstr !~ /^$/ ) ) {
+               if( $DEBUG ){
+                       my $dbgstr = $tmpstr;
+                       $dbgstr =~ s/\r/\\r/g;
+                       $dbgstr =~ s/\n/\\n/g;
+                       print "@ line:$linecnt -> mode:$mode tmpstr:$dbgstr \n";
+               }
+               if( $mode == $MODE_HTML ){
+                       # mode HTML
+                       # search <%s
+                       if( $tmpstr =~ /^(.*?)?<%(.*[\r\n]*)?/ ){
+                               $printstr = $1;
+                               $tmpstr   = $2;
+                               $mode = $MODE_C;
+                       } else {
+                               # not found <%
+                               $printstr = $tmpstr;
+                               if( $printstr =~ /^$/ ){
+                                       $printstr = "";
+                               }
+                               $tmpstr = "";
+                       }
+                       $printstr =~ s/"/\\"/g;
+                       $printstr =~ s/\r/\\r/g;
+                       $printstr =~ s/\n/\\n/g;
+                       if( length( $printstr ) != 0  ){
+                               print(  "$PRINTFUNC_ST \"$printstr\" $PRINTFUNC_ED;\n" );
+                       }
+                       print(  "#line $linecnt \"$INPUTFILENM\"\n" );
+                       next;
+               } else {
+                       # mode C
+                       # search %>
+                       # if( $tmpstr =~ /^(.*)?%>(.*)?$/ ){
+                       if( $tmpstr =~ /^(.*?)?%>(.*[\r\n]*)?$/ ){
+                               $printstr = $1;
+                               $tmpstr   = $2;
+                               $mode = $MODE_HTML;
+                       } else {
+                               $printstr = $tmpstr;
+                               $tmpstr = "";
+                       }
+#                      print( $$OUTFD $printstr );
+                       if( $printstr =~ /^d=(.*)$/ ){
+                               print(  "$PRINTFUNC_ST \"%d\", $1 $PRINTFUNC_ED;" );
+                       }elsif( $printstr =~ /^s=(.*)$/ ){
+                               print(  "$PRINTFUNC_ST \"%s\", $1 $PRINTFUNC_ED;" );
+                       }else{
+                               print(  $printstr );
+                       }
+
+                       if( $printstr !~ /\n/g ){
+#                              print $$OUTFD "\n" ;
+                               print "\n" ;
+                       }
+                       next;
+               }
+       }       
+}
+
+if( exists $opts{"o"}  ) {
+       close( STDOUT, "> $OUTPUTFILENM" );
+}
+
diff --git a/sample.cgc b/sample.cgc
new file mode 100644 (file)
index 0000000..3e50641
--- /dev/null
@@ -0,0 +1,153 @@
+<%
+#include <stdio.h>
+#include <stdlib.h>
+#define BUNDLENAME "testbundle"
+
+typedef struct _MYNAMEDATA_ {
+       char* name;
+       char* dispname; 
+       int   height;
+       int   width;
+       int   top;
+       int   left;
+} MYNAMEDATA;
+
+static MYNAMEDATA datatable[] ={
+/* name,  disp,      height, width, top, left } */
+{ "name1", "light",  20,100,  100, 100 },
+{ "name2", "window", 20,100,  200, 200 },
+{ "name3", "shutter",20,100,   50, 200 },
+{ NULL, NULL,        100,30,  200, 200 },
+} ;
+%>
+<%
+static int disp_test_VGA()
+{
+       int i;
+       char *name = NULL;
+       char *dispname = NULL;
+%>
+Content-type:text/html
+Cache-Control: no-cache
+Pragma: no-cache
+<%s="\r\n"%>
+<%s="\r\n"%>
+<html>
+<head>
+<style type="text/css"><!--
+.backimage {
+position:absolute;
+}
+#backimage {
+z-index:1;
+}
+
+<%
+for(i = 0; datatable[i].name != NULL ; i++ ){ 
+       name=datatable[i].name;
+%>
+/* <%s=name%> <%s=datatable[i].dispname%> */
+.<%s=name%>_possize {
+position:absolute;
+width: <%d=datatable[i].width%>px;
+height:<%d=datatable[i].height%>px;
+top:   <%d=datatable[i].top%>px;
+left:  <%d=datatable[i].left%>px;
+position:absolute;
+z-index:10;
+}
+#<%s=name%>_img_on {
+visibility:hidden;
+} 
+#<%s=name%>_img_off {
+visibility:visible;
+} 
+#<%s=name%>_on_text {
+visibility:hidden;
+} 
+#<%s=name%>_off_text {
+visibility:visible;
+} 
+
+<%
+}
+%>
+
+</style>
+
+<script type="text/javascript">
+<!--
+<%
+for(i = 0; datatable[i].name != NULL ; i++ ){ 
+       name=datatable[i].name;
+%>
+/* <%s=name%>  */
+function <%s=name%>_on_click(){
+       alert( "<%s=name%> on clicked" );
+       var <%s=name%>_img_on  = document.getElementById("<%s=name%>_img_on");
+       var <%s=name%>_img_off = document.getElementById("<%s=name%>_img_off");
+       var <%s=name%>_on_text = document.getElementById("<%s=name%>_on_text");
+       var <%s=name%>_off_text= document.getElementById("<%s=name%>_off_text");
+
+       <%s=name%>_img_on.style.visibility = "hidden";
+       <%s=name%>_img_off.style.visibility = "visible"; 
+       <%s=name%>_on_text.style.visibility = "hidden";
+       <%s=name%>_off_text.style.visibility = "visible"; 
+}
+function <%s=name%>_off_click(){
+       alert( "<%s=name%> off clicked" );
+       var <%s=name%>_img_on  = document.getElementById("<%s=name%>_img_on");
+       var <%s=name%>_img_off = document.getElementById("<%s=name%>_img_off");
+       var <%s=name%>_on_text = document.getElementById("<%s=name%>_on_text");
+       var <%s=name%>_off_text= document.getElementById("<%s=name%>_off_text");
+
+       <%s=name%>_img_on.style.visibility = "visible";
+       <%s=name%>_img_off.style.visibility = "hidden"; 
+       <%s=name%>_on_text.style.visibility = "visible";
+       <%s=name%>_off_text.style.visibility = "hidden"; 
+}
+<%
+}
+%>
+
+//-->
+</script>
+
+</head>
+<body>
+
+<%
+for(i = 0; datatable[i].name != NULL ; i++ ){ 
+       name=datatable[i].name;
+       dispname=datatable[i].dispname;
+%>
+<img id="<%s=name%>_img_on"  class="<%s=name%>_possize" src="on_button.png" />
+<img id="<%s=name%>_img_off" class="<%s=name%>_possize" src="off_button.png" />
+<a href="#" id="<%s=name%>_on_text"  class="<%s=name%>_possize" 
+       onclick="<%s=name%>_on_click()"  >
+       <%s=dispname%>:OFF </a>
+<a href="#" id="<%s=name%>_off_text" class="<%s=name%>_possize" 
+       onclick="<%s=name%>_off_click()" >
+       <%s=dispname%>:ON </a>
+<%
+}
+%>
+
+<img id="backimage"    class="backimage"  src="background.png" />
+
+</body>
+</html>
+<%
+}
+%>
+
+
+<% 
+/* CGI main function */
+int main(int argc, char** argv ){
+
+       disp_test_VGA();
+
+       return 0;
+}
+%>
diff --git a/sample1/Makefile b/sample1/Makefile
new file mode 100644 (file)
index 0000000..463f65b
--- /dev/null
@@ -0,0 +1,18 @@
+
+TARGET = sample1
+OBJECT = sample1.o
+
+CGCPROGRAM=../cgigenerator.pl
+
+all:$(TARGET) $(TARGET).c
+
+$(TARGET): $(OBJECT)
+
+#.c.cgc:
+%.c:%.cgc
+       $(CGCPROGRAM) < $^  >$@
+       
+clean:
+       -rm $(TARGET) $(OBJECT) $(TARGET).c
+
+
diff --git a/sample1/sample1.cgc b/sample1/sample1.cgc
new file mode 100644 (file)
index 0000000..7283d90
--- /dev/null
@@ -0,0 +1,40 @@
+<%
+#include <stdio.h>
+#include <stdlib.h>
+%>
+
+<%
+int main(int argc, char** argv)
+{
+       int i;
+       char* Title = "Table Test";
+       char buff[64];
+%>
+<html>
+
+<head>
+<title><%s=Title%> </title>
+</head>
+<body>
+<p><%s=Title%></p>
+
+<table>
+<tbody>
+<%
+for( i = 0; i< 10 ; i++ ){
+%>
+<tr>
+<td> <%d=i%> </td>
+<% snprintf( buff, sizeof(buff), "test-%d", i ); %>
+<td> <%s=buff%> </td>
+<% snprintf( buff, sizeof(buff), "test:%d", i ); %>
+<td> <%s=buff%> </td>
+</tr>
+<%
+}
+%>
+</tbody>
+</table>
+</body>
+</html>
+<% } %>
diff --git a/sample2/Makefile b/sample2/Makefile
new file mode 100644 (file)
index 0000000..11cbd7d
--- /dev/null
@@ -0,0 +1,18 @@
+
+TARGET = sample2
+OBJECT = sample2.o
+
+CGCPROGRAM=../cgigenerator.pl
+
+all:$(TARGET) $(TARGET).c
+
+$(TARGET): $(OBJECT)
+
+#.c.cgc:
+%.c:%.cgc
+       $(CGCPROGRAM) < $^  >$@
+       
+clean:
+       -rm $(TARGET) $(OBJECT) $(TARGET).c
+
+
diff --git a/sample2/background.png b/sample2/background.png
new file mode 100644 (file)
index 0000000..725736a
Binary files /dev/null and b/sample2/background.png differ
diff --git a/sample2/off_button.png b/sample2/off_button.png
new file mode 100644 (file)
index 0000000..550871f
Binary files /dev/null and b/sample2/off_button.png differ
diff --git a/sample2/on_button.png b/sample2/on_button.png
new file mode 100644 (file)
index 0000000..7f37670
Binary files /dev/null and b/sample2/on_button.png differ
diff --git a/sample2/sample2.cgc b/sample2/sample2.cgc
new file mode 100644 (file)
index 0000000..3e50641
--- /dev/null
@@ -0,0 +1,153 @@
+<%
+#include <stdio.h>
+#include <stdlib.h>
+#define BUNDLENAME "testbundle"
+
+typedef struct _MYNAMEDATA_ {
+       char* name;
+       char* dispname; 
+       int   height;
+       int   width;
+       int   top;
+       int   left;
+} MYNAMEDATA;
+
+static MYNAMEDATA datatable[] ={
+/* name,  disp,      height, width, top, left } */
+{ "name1", "light",  20,100,  100, 100 },
+{ "name2", "window", 20,100,  200, 200 },
+{ "name3", "shutter",20,100,   50, 200 },
+{ NULL, NULL,        100,30,  200, 200 },
+} ;
+%>
+<%
+static int disp_test_VGA()
+{
+       int i;
+       char *name = NULL;
+       char *dispname = NULL;
+%>
+Content-type:text/html
+Cache-Control: no-cache
+Pragma: no-cache
+<%s="\r\n"%>
+<%s="\r\n"%>
+<html>
+<head>
+<style type="text/css"><!--
+.backimage {
+position:absolute;
+}
+#backimage {
+z-index:1;
+}
+
+<%
+for(i = 0; datatable[i].name != NULL ; i++ ){ 
+       name=datatable[i].name;
+%>
+/* <%s=name%> <%s=datatable[i].dispname%> */
+.<%s=name%>_possize {
+position:absolute;
+width: <%d=datatable[i].width%>px;
+height:<%d=datatable[i].height%>px;
+top:   <%d=datatable[i].top%>px;
+left:  <%d=datatable[i].left%>px;
+position:absolute;
+z-index:10;
+}
+#<%s=name%>_img_on {
+visibility:hidden;
+} 
+#<%s=name%>_img_off {
+visibility:visible;
+} 
+#<%s=name%>_on_text {
+visibility:hidden;
+} 
+#<%s=name%>_off_text {
+visibility:visible;
+} 
+
+<%
+}
+%>
+
+</style>
+
+<script type="text/javascript">
+<!--
+<%
+for(i = 0; datatable[i].name != NULL ; i++ ){ 
+       name=datatable[i].name;
+%>
+/* <%s=name%>  */
+function <%s=name%>_on_click(){
+       alert( "<%s=name%> on clicked" );
+       var <%s=name%>_img_on  = document.getElementById("<%s=name%>_img_on");
+       var <%s=name%>_img_off = document.getElementById("<%s=name%>_img_off");
+       var <%s=name%>_on_text = document.getElementById("<%s=name%>_on_text");
+       var <%s=name%>_off_text= document.getElementById("<%s=name%>_off_text");
+
+       <%s=name%>_img_on.style.visibility = "hidden";
+       <%s=name%>_img_off.style.visibility = "visible"; 
+       <%s=name%>_on_text.style.visibility = "hidden";
+       <%s=name%>_off_text.style.visibility = "visible"; 
+}
+function <%s=name%>_off_click(){
+       alert( "<%s=name%> off clicked" );
+       var <%s=name%>_img_on  = document.getElementById("<%s=name%>_img_on");
+       var <%s=name%>_img_off = document.getElementById("<%s=name%>_img_off");
+       var <%s=name%>_on_text = document.getElementById("<%s=name%>_on_text");
+       var <%s=name%>_off_text= document.getElementById("<%s=name%>_off_text");
+
+       <%s=name%>_img_on.style.visibility = "visible";
+       <%s=name%>_img_off.style.visibility = "hidden"; 
+       <%s=name%>_on_text.style.visibility = "visible";
+       <%s=name%>_off_text.style.visibility = "hidden"; 
+}
+<%
+}
+%>
+
+//-->
+</script>
+
+</head>
+<body>
+
+<%
+for(i = 0; datatable[i].name != NULL ; i++ ){ 
+       name=datatable[i].name;
+       dispname=datatable[i].dispname;
+%>
+<img id="<%s=name%>_img_on"  class="<%s=name%>_possize" src="on_button.png" />
+<img id="<%s=name%>_img_off" class="<%s=name%>_possize" src="off_button.png" />
+<a href="#" id="<%s=name%>_on_text"  class="<%s=name%>_possize" 
+       onclick="<%s=name%>_on_click()"  >
+       <%s=dispname%>:OFF </a>
+<a href="#" id="<%s=name%>_off_text" class="<%s=name%>_possize" 
+       onclick="<%s=name%>_off_click()" >
+       <%s=dispname%>:ON </a>
+<%
+}
+%>
+
+<img id="backimage"    class="backimage"  src="background.png" />
+
+</body>
+</html>
+<%
+}
+%>
+
+
+<% 
+/* CGI main function */
+int main(int argc, char** argv ){
+
+       disp_test_VGA();
+
+       return 0;
+}
+%>
diff --git a/testdir/Makefile b/testdir/Makefile
new file mode 100644 (file)
index 0000000..0c88fb6
--- /dev/null
@@ -0,0 +1,14 @@
+
+
+CGCPROGRAM=../cgigenerator.pl
+
+
+
+test:
+       echo "hoge<%test();%>hoge2<%d=1%>hoge3" | $(CGCPROGRAM) > tmp.dat
+       diff tmp.dat test.dat
+       @echo "TEST OK "
+       rm tmp.dat
+
+
+
diff --git a/testdir/test.dat b/testdir/test.dat
new file mode 100644 (file)
index 0000000..5bd0a19
--- /dev/null
@@ -0,0 +1,9 @@
+#line 1 "STDIN"
+printf( "hoge" );
+#line 1 "STDIN"
+test();
+printf( "hoge2" );
+#line 1 "STDIN"
+printf( "%d", 1 );
+printf( "hoge3\n" );
+#line 1 "STDIN"