and added files
[bcm963xx.git] / userapps / opensource / net-snmp / mibs / smistrip
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: smistrip,v 5.0 2002/04/20 07:30:17 hardaker Exp $
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 GETOPTS=getopts
24 VERSION=0.2-cvs
25
26
27 do_version () {
28     echo "smistrip $VERSION"
29 }
30
31
32
33 do_usage () {
34     echo "Usage: smistrip [-Vhn] [-d dir] [-s suffix] [-m modules] file ..."
35     echo "-V         show version and license information"
36     echo "-h         show usage information"
37     echo "-n         do not write module files"
38     echo "-d dir     write module to directory dir"
39     echo "-x suffix  append suffix to the module file name"
40     echo "-m modules strip only the specified modules. For a list of modules"
41     echo "           use : as a separator"
42     echo "file ...   input files to parse (RFCs, I-Ds, ...)"
43 }
44
45
46
47 do_strip () {
48     cat $1 | $AWK -vtest="$test" -vdir="$dir" -vsingle="$single" -vsuffix="$suffix" '
49
50     # start of module
51     /^[ \t]*[A-Za-z0-9-]* *DEFINITIONS */ {
52         module = $1
53         collect = 1
54         macro = 0
55         n = 0
56     }
57
58     # page footer - start skipping
59     /\[Page [iv0-9]*\] */ {
60         collect = 0
61         next
62     }
63
64     /^[ \t]*(::=|DESCRIPTION|SYNTAX|(MAX-|MIN-|)ACCESS|STATUS|REFERENCE|INDEX|AUGMENTS|DEFVAL|UNITS|DISPLAY|")/ {
65         if (collect)
66             if (line[n-1] == "") n--
67     }
68
69     # a blank line - suppress multiple
70     /^[ \t\r]*$/ {
71         if (collect)
72             if (line[n-1] != "" && line[n-1] !~ /,[ \t\r]*$/) line[n++] = ""    
73         next
74     }
75
76     # collect non-blank line when inside mib module
77     /[^ \f\t]/ {
78         if (length(module) > 0) {
79             if (!collect)
80                 collect = 1     # page header, stop skipping
81             else
82                 line[n++] = $0
83         }
84     }
85
86     # remember when we enter a macro definition
87     / *MACRO *::=/ {
88         macro = 1
89     }
90
91     # end of module
92     /^[ \t]*END[ \t\r]*$/ {
93         if (macro)
94             macro = 0
95         else if (length(single) == 0 || match(":"single":", ":"module":")) {
96             strip = 99
97             for (i = 0 ; i < n ; i++) {
98                 # find the minimum column that contains non-blank characters
99                 # in order to cut a blank prefix off.
100                 p = match(line[i], "[^ ]")
101                 if (p < strip && length(line[i]) > p) strip = p
102             }
103
104             if (test != "1") {
105                 if (dir)
106                     f = dir "/" module suffix
107                 else
108                     f = module suffix
109                 for (i = 0 ; i < n ; i++)
110                     print substr(line[i], strip) >f
111             }
112
113             print module ": " n " lines."
114             module = ""
115             collect = 0
116         }
117         else
118             print module ": ignored."
119     }
120     '
121 }
122
123
124 while $GETOPTS Vhnm:d:x: c ; do
125     case $c in
126         n)      test=1
127                 ;;
128         m)      single=$OPTARG
129                 ;;
130         d)      dir=$OPTARG
131                 ;;
132         x)      suffix=$OPTARG
133                 ;;
134         h)      do_usage
135                 exit 0
136                 ;;
137         V)      do_version
138                 exit 0
139                 ;;
140         *)      do_usage
141                 exit 1
142                 ;;
143     esac
144 done
145
146 shift `expr $OPTIND - 1`
147
148
149 if [ $# -eq 0 ] ; then
150     do_strip -
151 else 
152     for f in $@ ; do
153         do_strip $f
154     done
155 fi
156
157 exit 0