%{
#include <tcl.h>
#include <sys/stat.h>
char *script;
int firstly;
#define LF 10
/*
 * The following variable is a special hack that is needed in order for
 * Sun shared libraries to be used for Tcl.
 */

extern int matherr();
int *tclDummyMathPtr = (int *) matherr;

%}
%x begine
%%
.   printf("%s",yytext);
[\n] printf("\n");
"<!begin"   {BEGIN(begine);if(firstly == 1) strcpy(script,""); else firstly=1;}
<begine>"end!>"    {fflush(yyout);parse_html();BEGIN(INITIAL); }
<begine>[ ]       {strcat(script,yytext);};
<begine>[\n]       {strcat(script,yytext);};
<begine>.       {strcat(script,yytext);};

%%

Tcl_Interp *interp;

int
main(argc, argv)
    int argc;			/* Number of command-line arguments. */
    char **argv;		/* Values of command-line arguments. */
{
char *filename = NULL;
struct stat stat_buf;
int code, q_ok;
size_t qleng;


firstly=0;

printf("Content-type: text/html%c\n",LF);

  filename = (char *)getenv("PATH_TRANSLATED");
  if (!filename)
  {
    filename=argv[1];

   }
   if (stat(filename,&stat_buf) < 0)
   {
   printf("Can't stat input file %s", getenv("PATH_INFO"));
   perror("stat");
   printf("\n\n");
   exit(1);
   }

yyin=fopen(filename,"r");
script=(char*)malloc(stat_buf.st_size+150+16535);
sprintf(script,"#\n#\n");
get_query();
interp = Tcl_CreateInterp();
#ifdef USE_DB
if (Mysqltcl_Init(interp) == TCL_ERROR) {
    return TCL_ERROR;
}
#endif
yylex();
free(script);
    return 0;			/* Needed only to prevent compiler warning. */
}

int parse_html ()
{
int code;
code=Tcl_RecordAndEval(interp, script,0);
if(code != TCL_OK) 
 {
/* printf("Error: Cannot parse file!\n");*/
 }
return code;
}

void storeArgs(char *query, char *query_vars)
{
char *qu_, tmp_var[16535], *cher;
int new_var,n_vars;

if (!query) return;
new_var=1;
n_vars=0;
qu_ = query;
cher=(char*)malloc(2);
bzero(tmp_var,sizeof(tmp_var));
strcpy(tmp_var,"");
while(*qu_)
 {
     if( (*qu_ != '=') && (*qu_ != '+')  && (*qu_  != '&'))
     {
         
      if(new_var == 1)
       {
        strcat(tmp_var,"set ");
       new_var = 0; 
       } 
       cher[0]=*qu_;
    strcat(tmp_var,cher);
     qu_++;
     } else
     {
      if (*qu_ == '=')
      {
      qu_++;
      strcat(tmp_var," \"");
      n_vars++;
      continue;
      }
      if (*qu_ == '+')
      {
      qu_++;
      strcat(tmp_var," ");
      continue;     
      }
      if (*qu_ == '&')
      {
      qu_++;
      strcat(tmp_var,"\";\n");
      new_var=1;
      continue;     
      } 
     }
 }
if(new_var == 0 && n_vars !=0 ) strcat(tmp_var,"\";\n");
sprintf(query_vars,"%s\n#\n",tmp_var);
free(cher);
}



int get_query()
{
        char    *query,*method,*query_string;
	int	length, remain;


	query = (char *)getenv("QUERY_STRING");
	if (query)
	{
/*         printf("<h1><br>Query: %s<br></h1>\n",query);*/
         length=strlen(query);
         query_string=(char *)malloc(length*3*sizeof(char));
         storeArgs(query,query_string);
         strcat(script,query_string);
         free(query_string);
/*		storeArgs(query,SRC_GET);*/
	}

	method=(char *)getenv("REQUEST_METHOD");
	if (method)
	{
		if (strcmp(method,"POST" )==0) 
		{
			length = atoi((char *)getenv("CONTENT_LENGTH"));
			query = (char *) malloc(length + 1);
			remain = length;
			while(remain)
			{
				remain -= read(fileno(stdin) , 
					query + length - remain, remain);
			}
			query[length]='\0';
/*                        printf("<h1><br>last query: %s<br></h1>\n",query);*/
                        query_string=(char *)malloc(length*3*sizeof(char));
			storeArgs(query,query_string);
                        strcat(script,query_string);
                        free(query_string);
                        free(query);
/*			storeArgs(query,SRC_POST);*/
		} 
	}
}