emailrelay/bin/expand.sh_
Graeme Walker 4905d3db56 v0.9.7
2002-03-17 12:00:00 +00:00

90 lines
2.0 KiB
Bash

#!/bin/sh
#
# Copyright (C) 2001-2002 Graeme Walker <graeme_walker@users.sourceforge.net>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later
# version.
#
# This program 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 General Public License for more details.
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# ===
#
# expand.sh
#
# Expands "#include#<file>#" directives.
#
# Only does one sustitution per line. Directives which start the line are
# multi-line. Those within a line are one-line, expanded in-place.
#
# The "-t" switch can be used to suppress expansion of files
# with an extension of ".html".
#
# Bugs: Does not do nested expansion. Only supports one include statement
# per line.
#
# usage: expand.sh [-a <awk>] [-t]
#
awk="gawk"
if test "${1}" = "-a"
then
shift
awk="${1}"
shift
fi
expand_html="1"
if test "${1}" = "-t"
then
shift
expand_html="0"
fi
${awk} -v expand_html="${expand_html}" -v cat="${awk} '{print}'" '
{
line = $0
if( match(line,"#include#[^#]*#") )
{
rstart = RSTART
directive = substr(line,RSTART,RLENGTH)
path = substr(line,RSTART+9,RLENGTH-10)
if( !expand_html && match(path,".html$") )
{
print line
}
else
{
head = substr(line,1,rstart-1)
if( match(head,"^[[:space:]]*$") )
{
system( cat " " path )
}
else
{
getline text <path
if( length(text) == 0 )
printf( "expand.sh: warning: line %d: empty text sustitution for %s\n" , NR , path ) >"/dev/fd/2"
sub( directive , text , line )
print line
}
}
}
else
{
print line
}
}
' $@