summaryrefslogtreecommitdiff
path: root/mibs/smistrip
blob: 773d4fd09e3a19fb2289345078b65bb28916ce79 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/bin/sh
#
# smistrip --
#
#	Extract MIB modules from text files, like RFCs or I-Ds.
#
# This is variant of smistrip from libsmi-0.2, modified to be somewhat
# more aggressive in suppressing blank lines, and support the -x option.
#
# Copyright (c) 1999 Frank Strauss, Technical University of Braunschweig.
# Modified by Niels Baggesen
#
# See the file "COPYING" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
#
# $Id: smistrip 11912 2005-02-08 20:08:10Z nba $
#
# NOTE, that this script relies on awk (tested with GNU awk) and getopts
# (shell builtin like in bash or standalone).
#

AWK=awk
[ `uname` != SunOS ] || AWK=/usr/bin/nawk
GETOPTS=getopts
VERSION=0.3-cvs


do_version () {
    echo "smistrip $VERSION"
}



do_usage () {
    echo "Usage: smistrip [-Vhn] [-d dir] [-s suffix] [-m modules] file ..."
    echo "-V         show version and license information"
    echo "-v         verbose"
    echo "-h         show usage information"
    echo "-n         do not write module files"
    echo "-d dir     write module to directory dir"
    echo "-x suffix  append suffix to the module file name"
    echo "-m modules strip only the specified modules. For a list of modules"
    echo "           use : as a separator"
    echo "file ...   input files to parse (RFCs, I-Ds, ...)"
}



do_strip () {
    cat $1 | $AWK -v test="$test" -v dir="$dir" -v single="$single" -v suffix="$suffix" -v verbose="$verbose" '

    BEGIN {
	if (length(single) != 0) {
	    single = ":"single":"
	}
	else {
	    single = ""
	}
    }

    END {
	if (single != "" && single != ":") {
	    gsub(":", " ", single)
	    print "WARNING: Module(s) not found:" single
	}
    }

    # start of module
    /^[ \t]*[A-Za-z0-9-]* *DEFINITIONS *::= *BEGIN/ {
	module = $1
	collect = 1
	macro = 0
	n = 0
    }

    # page footer - start skipping
    /\[Page [iv0-9]*\] */ {
        collect = 0
	next
    }

    /^[ \t]*(::=|DESCRIPTION|SYNTAX|MAX-ACCESS|MIN-ACCESS|ACCESS|STATUS|REFERENCE|INDEX|AUGMENTS|DEFVAL|UNITS|DISPLAY|")/ {
	if (collect)
	    if (line[n-1] == "") n--
    }

    # a blank line - suppress multiple
    /^[ \t\r]*$/ {
        if (collect)
	    if (line[n-1] != "" && line[n-1] !~ /,[ \t\r]*$/) line[n++] = ""    
	next
    }

    # collect non-blank line when inside mib module
    /[^ \f\t]/ {
	if (length(module) > 0) {
	    if (!collect)
		collect = 1	# page header, stop skipping
	    else
		line[n++] = $0
	}
    }

    # remember when we enter a macro definition
    / *MACRO *::=/ {
	macro = 1
    }

    # end of module
    /^[ \t]*END[ \t\r]*$/ {
	if (macro)
	    macro = 0
	else if (single == "" || match(single, ":"module":")) {
	    sub(":"module, "", single)
	    strip = 99
	    for (i = 0 ; i < n ; i++) {
		# find the minimum column that contains non-blank characters
		# in order to cut a blank prefix off.
		p = match(line[i], "[^ ]")
		if (p < strip && length(line[i]) > p) strip = p
	    }

	    if (test != "1") {
		if (dir)
		    f = dir "/" module suffix
		else
		    f = module suffix
		for (i = 0 ; i < n ; i++)
		    print substr(line[i], strip) >f
	    }

	    if (verbose) {
		print module ": " n " lines."
	    }
	    module = ""
	    collect = 0
	}
	else
	    print "NOTE: " module ": ignored."
    }
    '
}


while $GETOPTS Vvhnm:d:x: c ; do
    case $c in
	v)	verbose=1
		;;
	n)	test=1
		;;
	m)	single=$OPTARG
		;;
	d)	dir=$OPTARG
		;;
	x)	suffix=$OPTARG
		;;
	h)	do_usage
		exit 0
		;;
	V)	do_version
		exit 0
		;;
	*)	do_usage
		exit 1
		;;
    esac
done

shift `expr $OPTIND - 1`

if [ $# -eq 0 ] ; then
    do_strip -
else 
    for f in $@ ; do
	do_strip $f
    done
fi

exit 0