Blame tools/smistrip.in

Packit 022b05
#!@SH@
Packit 022b05
#
Packit 022b05
# smistrip --
Packit 022b05
#
Packit 022b05
#	Extract MIB and PIB modules from text files, like RFCs or I-Ds.
Packit 022b05
#
Packit 022b05
# Copyright (c) 1999 Frank Strauss, Technical University of Braunschweig.
Packit 022b05
#
Packit 022b05
# See the file "COPYING" for information on usage and redistribution
Packit 022b05
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
Packit 022b05
#
Packit 022b05
# $Id: smistrip.in 3434 2006-04-07 07:02:51Z strauss $
Packit 022b05
#
Packit 022b05
# NOTE, that this script relies on awk (tested with GNU awk) and getopts
Packit 022b05
# (shell builtin like in bash or standalone).
Packit 022b05
#
Packit 022b05
Packit 022b05
AWK=@AWK@
Packit 022b05
GETOPTS=getopts
Packit 022b05
VERSION=@VERSION@
Packit 022b05
Packit 022b05
Packit 022b05
do_version () {
Packit 022b05
    echo "smistrip $VERSION"
Packit 022b05
}
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
do_usage () {
Packit 022b05
    echo "Usage: smistrip [-Vhn] [-i dir] [-d dir] [-m module] file1 [file2 [...]]"
Packit 022b05
    echo "-V         show version and license information"
Packit 022b05
    echo "-h         show usage information"
Packit 022b05
    echo "-n         do not write module files"
Packit 022b05
    echo "-i dir     try to read files from directory dir"
Packit 022b05
    echo "-d dir     write module to directory dir"
Packit 022b05
    echo "-m module  strip only the specified module"
Packit 022b05
    echo "file1 ...  input files to parse (RFCs, I-Ds, ...)"
Packit 022b05
}
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
do_strip () {
Packit 022b05
    if [ "$indir" ] ; then
Packit 022b05
	FILE="$indir/$1"
Packit 022b05
    else
Packit 022b05
	FILE="$1"
Packit 022b05
    fi
Packit 022b05
    if [ ! -f "$FILE" -a -f "$FILE.gz" ] ; then
Packit 022b05
	FILE="$FILE.gz"
Packit 022b05
    fi
Packit 022b05
    echo "$FILE" | grep -q '\.gz$'
Packit 022b05
    if [ $? -ne 0 ] ; then
Packit 022b05
    	CMD=cat
Packit 022b05
    else
Packit 022b05
    	CMD=zcat
Packit 022b05
    fi
Packit 022b05
Packit 022b05
    $CMD "$FILE" | \
Packit 022b05
    tr -d '\015' | \
Packit 022b05
    grep -i -v '^[ ]*Internet[ \-]Draft' | \
Packit 022b05
    $AWK -vtest="$test" -vdir="$dir" -vsingle="$single" '
Packit 022b05
Packit 022b05
    # start of module
Packit 022b05
    /^[ \t]*[A-Za-z0-9-]* *(PIB-)?DEFINITIONS *(::=)? *(BEGIN)? *$/ {
Packit 022b05
	module = $1
Packit 022b05
	skip = 9
Packit 022b05
	skipped = -1
Packit 022b05
	macro = 0
Packit 022b05
	n = 0
Packit 022b05
    }
Packit 022b05
Packit 022b05
    # process each line
Packit 022b05
    {
Packit 022b05
	# at the end of a page we go back one line (which is expected to
Packit 022b05
	# be a separator line), and start the counter skipped to skip the
Packit 022b05
	# next few lines.
Packit 022b05
	if ($0 ~ /\[[pP]age [iv0-9]*\] */) {
Packit 022b05
            # some drafts do not use that separator line. so keep it if
Packit 022b05
            # there are non-blank characters.
Packit 022b05
            if (!(line[n] ~ /^[ \t]*$/)) { print "WARNING: the line ::"line[n]":: should be a separator before a page break. It was kept. " ; n-- }
Packit 022b05
	    skipped = 0
Packit 022b05
	}
Packit 022b05
Packit 022b05
	# if we are skipping...
Packit 022b05
	if (skipped >= 0) {
Packit 022b05
	    skipped++
Packit 022b05
Packit 022b05
	    # if we have skipped enough lines to the top of the next page...
Packit 022b05
	    if (skipped >= skip) {
Packit 022b05
		skipped = -1
Packit 022b05
	    } else {
Packit 022b05
    
Packit 022b05
	    	# finish skipping, if we find a non-empty line, but not before
Packit 022b05
	    	# we have skipped four lines. remember the miminum of lines
Packit 022b05
	    	# we have ever skipped to keep empty lines in a modules that
Packit 022b05
	    	# appear near the top of a page.
Packit 022b05
	    	if ((skipped >= 4) && ($0 ~ /[^ \t]/)) {
Packit 022b05
		    if (skipped < skip) { skip = skipped }
Packit 022b05
		    skipped = -1
Packit 022b05
	    	}   
Packit 022b05
	    }
Packit 022b05
	}
Packit 022b05
Packit 022b05
	# so, if we are not skipping and inside a module, remember the line.
Packit 022b05
        if ((skipped == -1) && (length(module) > 0)) {
Packit 022b05
	    line[n++] = $0
Packit 022b05
	}
Packit 022b05
    }
Packit 022b05
Packit 022b05
    # remember when we enter a macro definition
Packit 022b05
    /^[ \t]*[A-Za-z0-9-]* *MACRO *::=/ {
Packit 022b05
	macro = 1
Packit 022b05
    }
Packit 022b05
Packit 022b05
    # end of module
Packit 022b05
    /^[ \t]*END[ \t]*$/ {
Packit 022b05
	if (macro == 0) {
Packit 022b05
	    if ((length(single) == 0) || (single == module)) {
Packit 022b05
		strip = 99
Packit 022b05
		for (i=0 ; i < n ; i++) {
Packit 022b05
		    # find the minimum column that contains non-blank characters
Packit 022b05
		    # in order to cut a blank prefix off. Ignore lines that only
Packit 022b05
                    # contain white spaces.
Packit 022b05
                    if (!(line[i] ~ /^[ \t]*$/)) {
Packit 022b05
        	        p = match(line[i], "[^ ]")
Packit 022b05
		        if ((p < strip) && (length(line[i]) > p)) { strip = p }
Packit 022b05
                    }
Packit 022b05
		}
Packit 022b05
    
Packit 022b05
		if (test != "1") {
Packit 022b05
		    if (dir) {
Packit 022b05
		       f = dir"/"module
Packit 022b05
		    } else {
Packit 022b05
		       f = module
Packit 022b05
		    }
Packit 022b05
		    for (i=0 ; i < n ; i++) {
Packit 022b05
			print substr(line[i], strip) >f
Packit 022b05
		    }
Packit 022b05
		}
Packit 022b05
    
Packit 022b05
		print module ": " n " lines."
Packit 022b05
	    }
Packit 022b05
	    module = ""
Packit 022b05
	} else {
Packit 022b05
	    macro = 0
Packit 022b05
	}
Packit 022b05
    }
Packit 022b05
    '
Packit 022b05
}
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
while $GETOPTS Vhnm:i:d: c ; do
Packit 022b05
    case $c in
Packit 022b05
	n)	test=1
Packit 022b05
		;;
Packit 022b05
	m)	single=$OPTARG
Packit 022b05
		;;
Packit 022b05
	i)	indir=$OPTARG
Packit 022b05
		;;
Packit 022b05
	d)	dir=$OPTARG
Packit 022b05
		;;
Packit 022b05
	h)	do_usage
Packit 022b05
		exit 0
Packit 022b05
		;;
Packit 022b05
	V)	do_version
Packit 022b05
		exit 0
Packit 022b05
		;;
Packit 022b05
	*)	do_usage
Packit 022b05
		exit 1
Packit 022b05
		;;
Packit 022b05
    esac
Packit 022b05
done
Packit 022b05
Packit 022b05
shift `expr $OPTIND - 1`
Packit 022b05
Packit 022b05
Packit 022b05
Packit 022b05
if [ $# -eq 0 ] ; then
Packit 022b05
    do_strip -
Packit 022b05
else 
Packit 022b05
    for f in "$@" ; do
Packit 022b05
	do_strip "$f"
Packit 022b05
    done
Packit 022b05
fi
Packit 022b05
Packit 022b05
exit 0