3 # Copyright (c) 1992, 1993
4 # The Regents of the University of California. All rights reserved.
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # 3. All advertising materials mentioning features or use of this software
15 # must display the following acknowledgement:
16 # This product includes software developed by the University of
17 # California, Berkeley and its contributors.
18 # 4. Neither the name of the University nor the names of its contributors
19 # may be used to endorse or promote products derived from this software
20 # without specific prior written permission.
22 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 # From @(#)vnode_if.sh 8.1 (Berkeley) 6/10/93
35 # From @(#)makedevops.sh 1.1 1998/06/14 13:53:12 dfr Exp $
36 # From @(#)makedevops.sh ?.? 1998/10/05
39 # Script to produce device front-end sugar.
43 $cfile = 0; # by default do not produce any file type
52 # Process the command line
54 while ( $arg = shift @ARGV ) {
56 warn "Producing .c output files"
59 } elsif ( $arg eq '-h' ) {
60 warn "Producing .h output files"
63 } elsif ( $arg eq '-ch' || $arg eq '-hc' ) {
64 warn "Producing .c and .h output files"
68 } elsif ( $arg eq '-d' ) {
70 } elsif ( $arg eq '-p' ) {
71 warn "Will produce files in original not in current directory"
74 } elsif ( $arg eq '-l' ) {
75 if ( $line_width = shift @ARGV and $line_width > 0 ) {
76 warn "Line width set to $line_width"
79 die "Please specify a valid line width after -l";
81 } elsif ( $arg =~ m/\.m$/ ) {
84 push @filenames, $arg;
92 # Validate the command line parameters
94 die "usage: $0 [-d] [-p] [-c|-h] srcfile
95 where -c produce only .c files
96 -h produce only .h files
97 -p use the path component in the source file for destination dir
98 -l set line width for output files [80]
99 -d switch on debugging
101 unless ($cfile or $hfile)
102 and $#filenames != -1;
104 # FIXME should be able to do this more easily
106 $tmpdir = $ENV{'TMPDIR'}; # environment variables
107 $tmpdir = $ENV{'TMP'}
109 $tmpdir = $ENV{'TEMP'}
111 $tmpdir = '/tmp' # look for a physical directory
112 if !$tmpdir and -d '/tmp';
114 if !$tmpdir and -d '/usr/tmp';
116 if !$tmpdir and -d '/var/tmp';
117 $tmpdir = '.' # give up and use current dir
120 foreach $src ( @filenames ) {
121 # Names of the created files
122 $ctmpname = "$tmpdir/ctmp.$$";
123 $htmpname = "$tmpdir/htmp.$$";
125 ($name, $path, $suffix) = &fileparse($src, '.m');
128 $cfilename="$path/$name.c";
129 $hfilename="$path/$name.h";
131 warn "Processing from $src to $cfile / $hfile via $ctmp / $htmp"
134 die "Could not open $src, $!"
135 if !open SRC, "$src";
136 die "Could not open $ctmpname, $!"
137 if $cfile and !open CFILE, ">$ctmpname";
138 die "Could not open $htmpname, $!"
139 if $hfile and !open HFILE, ">$htmpname";
142 # Produce the header of the C file
145 print CFILE " * This file is produced automatically.\n";
146 print CFILE " * Do not modify anything in here by hand.\n";
148 print CFILE " * Created from\n";
149 print CFILE " * $src\n";
150 print CFILE " * with\n";
151 print CFILE " * $0\n";
154 print CFILE "#include <sys/param.h>\n";
155 print CFILE "#include <sys/queue.h>\n";
156 print CFILE "#include <sys/bus_private.h>\n";
160 # Produce the header of the H file
163 print HFILE " * This file is produced automatically.\n";
164 print HFILE " * Do not modify anything in here by hand.\n";
166 print HFILE " * Created from\n";
167 print HFILE " * $src\n";
168 print HFILE " * with\n";
169 print HFILE " * $0\n";
174 %methods = (); # clear list of methods
176 $error = 0; # to signal clean up and gerror setting
178 LINE: while ( $line = <SRC> ) {
181 # take special notice of include directives.
183 if ( $line =~ m/^#\s*include\s+(["<])([^">]+)([">]).*/i ) {
184 warn "Included file: $1$2" . ($1 eq '<'? '>':'"')
186 print CFILE "#include $1$2" . ($1 eq '<'? '>':'"') . "\n"
190 $line =~ s/#.*//; # remove comments
191 $line =~ s/^\s+//; # remove leading ...
192 $line =~ s/\s+$//; # remove trailing whitespace
194 if ( $line =~ m/^$/ ) { # skip empty lines
197 } elsif ( $line =~ m/^INTERFACE\s*([^\s;]*)(\s*;?)/i ) {
200 unless ( $intname =~ m/^[a-z_][a-z0-9_]*$/ ) {
203 warn "$src:$lineno: Invalid interface name '$intname', use [a-z_][a-z0-9_]*";
208 warn "$src:$lineno: semicolon missing at end of line, no problem"
209 if $semicolon !~ s/;$//;
211 warn "Interface $intname"
214 print HFILE '#ifndef _'.$intname."_if_h_\n"
216 print HFILE '#define _'.$intname."_if_h_\n\n"
218 print CFILE '#include "'.$intname.'_if.h"'."\n\n"
221 } elsif ( $line =~ m/^METHOD/i ) {
222 # Get the return type function name and delete that from
223 # the line. What is left is the possibly first function argument
224 # if it is on the same line.
226 # FIXME For compatibilities sake METHOD and METHODE is accepted.
229 warn "$src:$lineno: No interface name defined";
233 $line =~ s/^METHODE?\s+([^{]+?)\s*{\s*//i;
234 @ret = split m/\s+/, $1;
235 $name = pop @ret; # last element is name of method
236 $ret = join(" ", @ret); # return type
238 warn "Method: name=$name return type=$ret"
241 if ( !$name or !$ret ) {
244 warn "$src:$lineno: Invalid method specification";
249 unless ( $name =~ m/^[a-z_][a-z_0-9]*$/ ) {
252 warn "$src:$lineno: Invalid method name '$name', use [a-z_][a-z0-9_]*";
257 if ( defined($methods{$name}) ) {
258 warn "$src:$lineno: Duplicate method name";
263 $methods{$name} = 'VIS';
265 while ( $line !~ m/}/ and $line .= <SRC> ) { }
267 if ( $line !~ s/};?(.*)// ) { # remove first '}' and trailing garbage
268 # The '}' was not there (the rest is optional), so complain
269 warn "$src:$lineno: Premature end of file";
273 warn "$src:$lineno: Ignored '$1'" # warn about garbage at end of line
276 # Create a list of variables without the types prepended
278 $line =~ s/^\s+//; # remove leading ...
279 $line =~ s/\s+$//; # ... and trailing whitespace
280 $line =~ s/\s+/ /; # remove double spaces
282 @arguments = split m/\s*;\s*/, $line;
283 @varnames = (); # list of varnames
284 foreach $argument (@arguments) {
285 next # skip argument if argument is empty
288 @ar = split m/[*\s]+/, $argument;
289 if ( $#ar == 0 ) { # only 1 word in argument?
290 warn "$src:$lineno: no type for '$argument'";
295 push @varnames, $ar[-1]; # last element is name of variable
298 warn 'Arguments: ' . join(', ', @arguments) . "\n"
299 . 'Varnames: ' . join(', ', @varnames)
302 $mname = $intname.'_'.$name; # method name
303 $umname = uc($mname); # uppercase method name
305 $arguments = join(", ", @arguments);
306 $varnames = join(", ", @varnames);
309 # the method description
310 print HFILE "extern struct device_op_desc $mname\_desc;\n";
312 print HFILE &format_line("typedef $ret $mname\_t($arguments);",
314 ',',' ' x length("typedef $ret $mname\_t("))
316 # the method declaration
317 print HFILE "$mname\_t $umname;\n\n";
321 # Print out the method desc
322 print CFILE "struct device_op_desc $mname\_desc = {\n";
323 print CFILE "\t0, \"$mname\"\n";
324 print CFILE "};\n\n";
326 # Print out the method itself
327 if ( 0 ) { # haven't chosen the format yet
328 print CFILE "$ret $umname($varnames)\n";
329 print CFILE "\t".join(";\n\t", @arguments).";\n";
331 print CFILE &format_line("$ret $umname($arguments)",
333 ',', ' ' x length("$ret $umname(")) . "\n";
336 print CFILE &format_line("\t$mname\_t *m = ($mname\_t *) DEVOPMETH(dev, $mname);",
337 $line_width-8, ' = ', ' =', "\t\t")
339 print CFILE "\t".($ret eq 'void'? '':'return ') . "m($varnames);\n";
345 warn "$src:$lineno: Invalid line encountered";
351 # print the final '#endif' in the header file
353 print HFILE "#endif /* _".$intname."_if_h_ */\n"
364 ($rc = system("mv $ctmpname $cfilename"))
365 and warn "mv $ctmpname $cfilename failed, $rc";
369 ($rc = system("mv $htmpname $hfilename"))
370 and warn "mv $htmpname $hfilename failed, $rc";
373 warn 'File' . ($hfile and $cfile? 's':'') . ' skipped';
374 ($rc = system("rm -f $htmpname $ctmpname"))
375 and warn "rm -f $htmpname $ctmpname failed, $rc";
384 my ($line, $maxlength, $break, $new_end, $new_start) = @_;
387 while ( length($line) > $maxlength
388 and ($i = rindex $line, $break, $maxlength-length($new_end)) != -1 ) {
389 $rline .= substr($line, 0, $i) . $new_end . "\n";
390 $line = $new_start . substr($line, $i+length($break));
393 return $rline . $line;