Imported Upstream version 0.4.8
[platform/upstream/libsmi.git] / tools / smistrip.in
1 #!@SH@
2 #
3 # smistrip --
4 #
5 #       Extract MIB and PIB modules from text files, like RFCs or I-Ds.
6 #
7 # Copyright (c) 1999 Frank Strauss, Technical University of Braunschweig.
8 #
9 # See the file "COPYING" for information on usage and redistribution
10 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 #
12 # $Id: smistrip.in 3434 2006-04-07 07:02:51Z strauss $
13 #
14 # NOTE, that this script relies on awk (tested with GNU awk) and getopts
15 # (shell builtin like in bash or standalone).
16 #
17
18 AWK=@AWK@
19 GETOPTS=getopts
20 VERSION=@VERSION@
21
22
23 do_version () {
24     echo "smistrip $VERSION"
25 }
26
27
28
29 do_usage () {
30     echo "Usage: smistrip [-Vhn] [-i dir] [-d dir] [-m module] file1 [file2 [...]]"
31     echo "-V         show version and license information"
32     echo "-h         show usage information"
33     echo "-n         do not write module files"
34     echo "-i dir     try to read files from directory dir"
35     echo "-d dir     write module to directory dir"
36     echo "-m module  strip only the specified module"
37     echo "file1 ...  input files to parse (RFCs, I-Ds, ...)"
38 }
39
40
41
42 do_strip () {
43     if [ "$indir" ] ; then
44         FILE="$indir/$1"
45     else
46         FILE="$1"
47     fi
48     if [ ! -f "$FILE" -a -f "$FILE.gz" ] ; then
49         FILE="$FILE.gz"
50     fi
51     echo "$FILE" | grep -q '\.gz$'
52     if [ $? -ne 0 ] ; then
53         CMD=cat
54     else
55         CMD=zcat
56     fi
57
58     $CMD "$FILE" | \
59     tr -d '\015' | \
60     grep -i -v '^[ ]*Internet[ \-]Draft' | \
61     $AWK -vtest="$test" -vdir="$dir" -vsingle="$single" '
62
63     # start of module
64     /^[ \t]*[A-Za-z0-9-]* *(PIB-)?DEFINITIONS *(::=)? *(BEGIN)? *$/ {
65         module = $1
66         skip = 9
67         skipped = -1
68         macro = 0
69         n = 0
70     }
71
72     # process each line
73     {
74         # at the end of a page we go back one line (which is expected to
75         # be a separator line), and start the counter skipped to skip the
76         # next few lines.
77         if ($0 ~ /\[[pP]age [iv0-9]*\] */) {
78             # some drafts do not use that separator line. so keep it if
79             # there are non-blank characters.
80             if (!(line[n] ~ /^[ \t]*$/)) { print "WARNING: the line ::"line[n]":: should be a separator before a page break. It was kept. " ; n-- }
81             skipped = 0
82         }
83
84         # if we are skipping...
85         if (skipped >= 0) {
86             skipped++
87
88             # if we have skipped enough lines to the top of the next page...
89             if (skipped >= skip) {
90                 skipped = -1
91             } else {
92     
93                 # finish skipping, if we find a non-empty line, but not before
94                 # we have skipped four lines. remember the miminum of lines
95                 # we have ever skipped to keep empty lines in a modules that
96                 # appear near the top of a page.
97                 if ((skipped >= 4) && ($0 ~ /[^ \t]/)) {
98                     if (skipped < skip) { skip = skipped }
99                     skipped = -1
100                 }   
101             }
102         }
103
104         # so, if we are not skipping and inside a module, remember the line.
105         if ((skipped == -1) && (length(module) > 0)) {
106             line[n++] = $0
107         }
108     }
109
110     # remember when we enter a macro definition
111     /^[ \t]*[A-Za-z0-9-]* *MACRO *::=/ {
112         macro = 1
113     }
114
115     # end of module
116     /^[ \t]*END[ \t]*$/ {
117         if (macro == 0) {
118             if ((length(single) == 0) || (single == module)) {
119                 strip = 99
120                 for (i=0 ; i < n ; i++) {
121                     # find the minimum column that contains non-blank characters
122                     # in order to cut a blank prefix off. Ignore lines that only
123                     # contain white spaces.
124                     if (!(line[i] ~ /^[ \t]*$/)) {
125                         p = match(line[i], "[^ ]")
126                         if ((p < strip) && (length(line[i]) > p)) { strip = p }
127                     }
128                 }
129     
130                 if (test != "1") {
131                     if (dir) {
132                        f = dir"/"module
133                     } else {
134                        f = module
135                     }
136                     for (i=0 ; i < n ; i++) {
137                         print substr(line[i], strip) >f
138                     }
139                 }
140     
141                 print module ": " n " lines."
142             }
143             module = ""
144         } else {
145             macro = 0
146         }
147     }
148     '
149 }
150
151
152
153 while $GETOPTS Vhnm:i:d: c ; do
154     case $c in
155         n)      test=1
156                 ;;
157         m)      single=$OPTARG
158                 ;;
159         i)      indir=$OPTARG
160                 ;;
161         d)      dir=$OPTARG
162                 ;;
163         h)      do_usage
164                 exit 0
165                 ;;
166         V)      do_version
167                 exit 0
168                 ;;
169         *)      do_usage
170                 exit 1
171                 ;;
172     esac
173 done
174
175 shift `expr $OPTIND - 1`
176
177
178
179 if [ $# -eq 0 ] ; then
180     do_strip -
181 else 
182     for f in "$@" ; do
183         do_strip "$f"
184     done
185 fi
186
187 exit 0