#!/bin/sh
#
# Replaces some special characters with their ASCII representations.
#

RCS_ID='$Id$'

my_name=$(basename $0)

while [ $# -ge 1 ] ; do
  case $1 in
    -h*) cat <<_EOH_

        NoSQL operator: ${my_name}

Usage:  ${my_name}  [options]

Options:
    -h        Print this help info.
    -p "/R/"  Translate only those environment variables which name
              matches the regular expression "R". The pattern must
              follow the AWK regexp syntax.
    -b        Escape blanks as well. 
    -H        Escape HTML '<' and '>' tags as well.


Translate some special characters in environment variables into their
ASCII escaped representation. For example, the horizontal tab character,
which ASCII decimal code is '9', gets translated into the ASCII sequence
'&#9;'. Prints the results to STDOUT in the form of variable='value' pairs,
suitable for being reused by the invoking shell to make new assignments.
Characters currently translated are: 

                   \n & " ' / \t | < > \ SPACE 

Default is to translate all the environment variables. Translation can be
applied to only those variables which names match a given regular expression.
The expression must follow the AWK language regular expression syntax.

For instance, to translate only those variables which names begin
with the letter 'W', the command is :  ${my_name} -p /^W/ 

Characters that are special to the Unix shell must be escaped on the
command line, as usual.

This operator is especially useful to write CGI programs for the WWW,
using the 'uncgi' utility (see http://hyperion.com/~koreth/uncgi.html).

An example of usage of this operator from within a shell script which
is run under 'uncgi' is :

                 eval \`${my_name} -p '/^WWW_/'\`

$RCS_ID

            ----------------------
NoSQL RDBMS, Copyright (C) 1998 Carlo Strozzi.
This program comes with ABSOLUTELY NO WARRANTY; for details
refer to the GNU General Public License.

You should have received a copy of the GNU General Public License
along with this program;  if not, write to the Free Software
Foundation, Inc., 59 Temple Place Suite 330, Boston, MA 02111-1307
USA.
            ----------------------

_EOH_
        exit 0
        ;;
    -H)    shift; html=1; shift       ;;
    -b)    shift; blanks=1; shift     ;;
	-p)    shift; v_pattern=$1; shift ;;
    *)     break     ;;
  esac
done

awk '#BEGIN { NULL = ""; FS = "\n"; RS = NULL; ORS = NULL ;
BEGIN { NULL = ""; FS = "\n"; RS = NULL; ORS = "\n"

  for( env in ENVIRON ) {
	if( env !~ '"${v_pattern:-/.*/}"' )
	  continue

    var = ENVIRON[ env ]

    # AWK metacharacters: "&" MUST be done first.
    gsub( "\&", "\&#38;", var )
    gsub( "\\", "\&#92;", var )
    gsub( "\"", "\&#34;", var )

    # General metacharacters.
    gsub( "\n", "\&#10;", var )
    if( '${blanks:-0}' ) gsub( " ", "\&#32;", var )
    gsub( "'"'"'", "\&#39;", var )

    # SED metacharacters.
    #gsub( "\/", "\&#47;", var )

    # NoSQL metacharacters
    gsub( "\t", "\&#9;", var )
    gsub( "\|", "\&#124;", var )

    if( '${html:-0}' ) {
      # HTML metacharacters: < >
      gsub( "<", "\&#60;", var )
      gsub( ">", "\&#62;", var )
    }
    #print var
    print env "='"'"'" var "'"'"'"
  }
}'

