#! /usr/bin/awk -f
#
# $Id: html2c,v 1.4 2004/09/02 00:52:04 dan Exp $
#
# Copyright (c) 2001, 2002 Dan McMahill
# All rights reserved.
#
# This code is derived from software written by Dan McMahill
#
# 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. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#        This product includes software developed by Dan McMahill
#  4. 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.
#
#
#
# This script operates on a html file to produce a C program which
# will print the html file.  In addition one extra markup is introduced.
#
#  <@fmt@var@>  will cause a line like:
#    fprintf(outstr,"%fmt",var);
#
# In the generated C code.
#
# Typical usage would be:
#
#    html2c out=outstr foo.html > foo.c
#
# The stream 'outstr' will be used for all output.  Ie, the generated
# C file will have calls like fprintf(outstr,"");
#
# Then in, for example, bar.c you could do something like:
#
#  print_html()
#  {
#    /* declare variables and do whatever else you need */
#    /* .... */
#
#    /* include the print out of our html */
#    #include "foo.c"
#
#  }
#
#
# The idea is that its a pain in the ass to write HTML in C.  Its much
# easier to work on an HTML file that you can quickly edit and preview.
# Then when you're done, you can quickly generate some C code that you
# might use as part of a CGI program
#

BEGIN{
  printf("\n");
  printf("/* ********* Automatically Generated.  Do not edit! ******** */\n");
  printf("/* *********         Created with html2c            ******** */\n\n");
}

{
  if ( out == "" ){
# provide a default for the output stream
	out = "stdout";
  }
# make a copy of the input line
  exact=$0;

# process the <SOURCEFORGE> tags
  if( sourceforge == 1 ) {
	gsub(/<SOURCEFORGE>/, "");
	gsub(/<\/SOURCEFORGE>/, "");
  } else {
	gsub(/<SOURCEFORGE>/, "<!--");
	gsub(/<\/SOURCEFORGE>/, "-->");
  }

# escape all backslashes
  gsub(/\\/,"\\\\");
 
# escape all double quotes
  gsub(/\"/,"\\\"");

# escape all percents
  gsub(/%/,"%%");

# replace all <@fmt@var@> markup with %fmt
  line = gensub(/<@([^@])@[^@]*@>/,"%\\1","g");

# see if the line had a <@fmt@var@> markup and if so, we need to
# make sure to print out the variables
  numvars = 0;
#  if ($0 ~ /<@([^@]*)@[^@]*@>/){
  if (exact ~ /<@([^@]*)@[^@]*@>/){
#    templine = $0;
    templine = exact;
    done = 0;
    while (!done){
      where = match(templine, /<@([^@]*)@[^@]*@>/);
      if (where == 0){
	done = 1;
      }
      else{
# pick out the <@fmt@var@> markup by its self
	tmpstr = substr(templine,where,RLENGTH);

# keep the rest of the line
	if (length(templine) >= where+RLENGTH)
	  templine = substr(templine,where+RLENGTH);
	else
	  templine = "";

# extract the variable name	
	varname[numvars] = gensub(/<@[^@]*@([^@]*)@>/,"\\1","g",tmpstr);
	numvars = numvars + 1;
      }
    }
  }

  printf("fprintf(%s,\"%s\\n\"",out,line);
  for (i = 0; i<numvars; i=i+1){
    printf(",%s",varname[i]);
  }
  printf(");\n");

}

END{
  printf("\n");
  printf("/* *********        End of Generated Code           ******** */\n\n");
}


