| 1 |
#!/bin/sh |
| 2 |
# |
| 3 |
# smistrip -- |
| 4 |
# |
| 5 |
# Extract MIB modules from text files, like RFCs or I-Ds. |
| 6 |
# |
| 7 |
# This is variant of smistrip from libsmi-0.2, modified to be somewhat |
| 8 |
# more aggressive in suppressing blank lines, and support the -x option. |
| 9 |
# |
| 10 |
# Copyright (c) 1999 Frank Strauss, Technical University of Braunschweig. |
| 11 |
# Modified by Niels Baggesen |
| 12 |
# |
| 13 |
# See the file "COPYING" for information on usage and redistribution |
| 14 |
# of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 15 |
# |
| 16 |
# $Id$ |
| 17 |
# |
| 18 |
# NOTE, that this script relies on awk (tested with GNU awk) and getopts |
| 19 |
# (shell builtin like in bash or standalone). |
| 20 |
# |
| 21 |
|
| 22 |
AWK=awk |
| 23 |
[ `uname` != SunOS ] || AWK=/usr/bin/nawk |
| 24 |
GETOPTS=getopts |
| 25 |
VERSION=0.3-cvs |
| 26 |
|
| 27 |
|
| 28 |
do_version () { |
| 29 |
echo "smistrip $VERSION" |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
do_usage () { |
| 35 |
echo "Usage: smistrip [-Vhn] [-d dir] [-s suffix] [-m modules] file ..." |
| 36 |
echo "-V show version and license information" |
| 37 |
echo "-v verbose" |
| 38 |
echo "-h show usage information" |
| 39 |
echo "-n do not write module files" |
| 40 |
echo "-d dir write module to directory dir" |
| 41 |
echo "-x suffix append suffix to the module file name" |
| 42 |
echo "-m modules strip only the specified modules. For a list of modules" |
| 43 |
echo " use : as a separator" |
| 44 |
echo "file ... input files to parse (RFCs, I-Ds, ...)" |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
do_strip () { |
| 50 |
expand $1 | sed 's/[ \r]*$//' | $AWK -v test="$test" -v dir="$dir" -v single="$single" -v suffix="$suffix" -v verbose="$verbose" ' |
| 51 |
|
| 52 |
BEGIN { |
| 53 |
if (length(single) != 0) { |
| 54 |
single = ":"single":" |
| 55 |
} |
| 56 |
else { |
| 57 |
single = "" |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
END { |
| 62 |
if (single != "" && single != ":") { |
| 63 |
gsub(":", " ", single) |
| 64 |
print "WARNING: Module(s) not found:" single |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
# start of module |
| 69 |
/^[ \t]*[A-Za-z0-9-]* *DEFINITIONS( +IMPLICIT +TAGS)? *::= *BEGIN/ { |
| 70 |
module = $1 |
| 71 |
collect = 1 |
| 72 |
macro = 0 |
| 73 |
n = 0 |
| 74 |
} |
| 75 |
|
| 76 |
# page footer - start skipping |
| 77 |
/\[Page [iv0-9]*\] */ { |
| 78 |
collect = 0 |
| 79 |
next |
| 80 |
} |
| 81 |
|
| 82 |
/^[ \t]*(::=|DESCRIPTION|SYNTAX|MAX-ACCESS|MIN-ACCESS|ACCESS|STATUS|REFERENCE|INDEX|AUGMENTS|DEFVAL|UNITS|DISPLAY|")/ { |
| 83 |
if (collect) |
| 84 |
if (line[n-1] == "") n-- |
| 85 |
} |
| 86 |
|
| 87 |
# a blank line - suppress multiple |
| 88 |
/^[ \t\r]*$/ { |
| 89 |
if (collect) |
| 90 |
if (line[n-1] != "" && line[n-1] !~ /,[ \t\r]*$/) line[n++] = "" |
| 91 |
next |
| 92 |
} |
| 93 |
|
| 94 |
# collect non-blank line when inside mib module |
| 95 |
/[^ \f\t]/ { |
| 96 |
if (length(module) > 0) { |
| 97 |
if (!collect) |
| 98 |
collect = 1 # page header, stop skipping |
| 99 |
else |
| 100 |
line[n++] = $0 |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
# remember when we enter a macro definition |
| 105 |
/ *MACRO *::=/ { |
| 106 |
macro = 1 |
| 107 |
} |
| 108 |
|
| 109 |
# end of module |
| 110 |
/^[ \t]*END[ \t\r]*$/ { |
| 111 |
if (macro) |
| 112 |
macro = 0 |
| 113 |
else if (single == "" || match(single, ":"module":")) { |
| 114 |
sub(":"module, "", single) |
| 115 |
strip = 99 |
| 116 |
for (i = 0 ; i < n ; i++) { |
| 117 |
# find the minimum column that contains non-blank characters |
| 118 |
# in order to cut a blank prefix off. |
| 119 |
p = match(line[i], "[^ ]") |
| 120 |
if (p < strip && length(line[i]) > p) strip = p |
| 121 |
} |
| 122 |
|
| 123 |
if (test != "1") { |
| 124 |
if (dir) |
| 125 |
f = dir "/" module suffix |
| 126 |
else |
| 127 |
f = module suffix |
| 128 |
for (i = 0 ; i < n ; i++) |
| 129 |
print substr(line[i], strip) >f |
| 130 |
} |
| 131 |
|
| 132 |
if (verbose) { |
| 133 |
print module ": " n " lines." |
| 134 |
} |
| 135 |
module = "" |
| 136 |
collect = 0 |
| 137 |
} |
| 138 |
else |
| 139 |
print "NOTE: " module ": ignored." |
| 140 |
} |
| 141 |
' |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
while $GETOPTS Vvhnm:d:x: c ; do |
| 146 |
case $c in |
| 147 |
v) verbose=1 |
| 148 |
;; |
| 149 |
n) test=1 |
| 150 |
;; |
| 151 |
m) single=$OPTARG |
| 152 |
;; |
| 153 |
d) dir=$OPTARG |
| 154 |
;; |
| 155 |
x) suffix=$OPTARG |
| 156 |
;; |
| 157 |
h) do_usage |
| 158 |
exit 0 |
| 159 |
;; |
| 160 |
V) do_version |
| 161 |
exit 0 |
| 162 |
;; |
| 163 |
*) do_usage |
| 164 |
exit 1 |
| 165 |
;; |
| 166 |
esac |
| 167 |
done |
| 168 |
|
| 169 |
shift `expr $OPTIND - 1` |
| 170 |
|
| 171 |
if [ $# -eq 0 ] ; then |
| 172 |
do_strip - |
| 173 |
else |
| 174 |
for f in $@ ; do |
| 175 |
do_strip $f |
| 176 |
done |
| 177 |
fi |
| 178 |
|
| 179 |
exit 0 |