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
37 # From src/sys/kern/makedevops.pl,v 1.12 1999/11/22 14:40:04 n_hibma Exp
38 # From src/sys/kern/makeobjops.pl,v 1.8 2001/11/16 02:02:42 joe Exp
40 # $FreeBSD: src/sys/tools/makeobjops.awk,v 1.3 2003/10/16 13:29:26 dfr Exp $
41 # $DragonFly: src/sys/tools/makeobjops.awk,v 1.5 2005/08/29 16:03:31 hsu Exp $
44 # Script to produce kobj front-end sugar.
49 print "usage: makeobjops.awk <srcfile.m> [-d] [-p] [-l <nr>] [-c|-h]";
50 print "where -c produce only .c files";
51 print " -h produce only .h files";
52 print " -p use the path component in the source file for destination dir";
53 print " -l set line width for output files [80]";
54 print " -d switch on debugging";
60 print "makeobjops.awk:", msg
> "/dev/stderr";
63 function warnsrc
(msg
)
65 warn
(src
":" lineno
": " msg
);
80 # These are just for convenience ...
81 function printc
(s
) {if (opt_c
) print s
> ctmpfilename
;}
82 function printh
(s
) {if (opt_h
) print s
> htmpfilename
;}
85 # If a line exceeds maxlength, split it into multiple
86 # lines at commas. Subsequent lines are indented by
87 # the specified number of spaces.
89 # In other words: Lines are split by replacing ", "
90 # by ",\n" plus indent spaces.
93 function format_line
(line
, maxlength
, indent
)
97 while (length(line
) > maxlength
) {
99 # Find the rightmost ", " so that the part
100 # to the left of it is just within maxlength.
101 # If there is none, give up and leave it as-is.
103 if (!
match(substr(line
, 1, maxlength
+ 1), /^.
*, /))
105 rline = rline
substr(line
, 1, RLENGTH - 1) "\n";
106 line =
sprintf("%*s", indent
, "") substr(line
, RLENGTH + 1);
112 # Join an array into a string.
115 function join
(separator
, array
, num
)
120 _result = separator array
[num
--] _result
;
121 _result = array
[1] _result
;
127 # Execute a system command and report if it failed.
130 function system_check
(cmd
)
132 if ((rc =
system(cmd
)))
133 warn
(cmd
" failed (" rc
")");
137 # Handle "INTERFACE" line.
140 function handle_interface
()
143 sub(/;$
/, "", intname
);
144 if (intname !~
/^
[a
-z_
][a
-z0
-9_
]*$
/) {
146 warnsrc
("Invalid interface name '" intname
"', use [a-z_][a-z0-9_]*");
151 warnsrc
("Semicolon missing at end of line, no problem");
153 debug
("Interface " intname
);
155 printh
("#ifndef _" intname
"_if_h_");
156 printh
("#define _" intname
"_if_h_\n");
157 printc
("#include \"" intname
"_if.h\"\n");
161 # Handle "CODE" and "HEADER" sections.
162 # Returns the code as-is.
165 function handle_code
()
170 sub(/[^
].
*$
/, "", indent
); # find the indent used
172 sub("^" indent
, ""); # remove the indent
181 # Handle "METHOD" and "STATICMETHOD" sections.
184 function handle_method
(static
)
187 # Get the return type and function name and delete that from
188 # the line. What is left is the possibly first function argument
189 # if it is on the same line.
192 warnsrc
("No interface name defined");
196 sub(/^
[^
]+[ ]+/, "");
198 sub(/[ ]*\
{.
*$
/, "", ret
);
200 sub(/^.
*[ ]/, "", name
); # last element is name of method
201 sub(/[ ]+[^
]+$
/, "", ret
); # return type
202 debug
("Method: name=" name
" return type=" ret
);
204 sub(/^
[^\
{]*\
{[ ]*/, "");
208 warnsrc
("Invalid method specification");
213 if (name !~
/^
[a
-z_
][a
-z_0
-9]*$
/) {
214 warnsrc
("Invalid method name '" name
"', use [a-z_][a-z0-9_]*");
220 warnsrc
("Duplicate method name");
224 methods
[name
] = name
;
227 while (line !~
/\
}/ && (getline < src
) > 0) {
233 if (!
match(line
, /\
};?
/)) {
234 warnsrc
("Premature end of file");
238 extra =
substr(line
, RSTART + RLENGTH);
239 if (extra ~
/[ ]*DEFAULT
[ ]*[a
-zA
-Z_
][a
-zA
-Z_0
-9]*[ ]*;/) {
241 sub(/.
*DEFAULT
[ ]*/, "", default
);
242 sub(/[; ]+.
*$
/, "", default
);
244 else if (extra
&& opt_d
) {
245 # Warn about garbage at end of line.
246 warnsrc
("Ignored '" extra
"'");
248 sub(/\
};?.
*$
/, "", line
);
251 # Create a list of variables without the types prepended.
253 sub(/^
[ ]+/, "", line
); # remove leading ...
254 sub(/[ ]+$
/, "", line
); # ... and trailing whitespace
255 gsub(/[ ]+/, " ", line
); # remove double spaces
257 num_arguments =
split(line
, arguments
, / *; */) - 1;
258 delete varnames
; # list of varnames
260 for (i =
1; i
<= num_arguments
; i
++) {
262 continue; # skip argument if argument is empty
263 num_ar =
split(arguments
[i
], ar
, /[* ]+/);
264 if (num_ar
< 2) { # only 1 word in argument?
265 warnsrc
("no type for '" arguments
[i
] "'");
269 # Last element is name of variable.
270 varnames
[++num_varnames
] = ar
[num_ar
];
273 argument_list = join
(", ", arguments
, num_arguments
);
274 varname_list = join
(", ", varnames
, num_varnames
);
277 warn
("Arguments: " argument_list
);
278 warn
("Varnames: " varname_list
);
281 mname = intname
"_" name
; # method name
282 umname =
toupper(mname
); # uppercase method name
284 firstvar = varnames
[1];
287 default =
"kobj_error_method";
289 # the method description
290 printh
("extern struct kobjop_desc " mname
"_desc;");
292 prototype =
"typedef " ret
" " mname
"_t(";
293 printh
(format_line
(prototype argument_list
");",
294 line_width
, length(prototype
)));
296 # Print out the method desc
297 printc
("struct kobj_method " mname
"_method_default = {");
298 printc
("\t&" mname
"_desc, (kobjop_t) " default
);
301 printc
("struct kobjop_desc " mname
"_desc = {");
302 printc
("\t0, &" mname
"_method_default");
305 # Print out the method itself
306 if (0) { # haven't chosen the format yet
307 printh
("static __inline " ret
" " umname
"(" varname_list
")");
308 printh
("\t" join
(";\n\t", arguments
, num_arguments
) ";");
311 prototype =
"static __inline " ret
" " umname
"(";
312 printh
(format_line
(prototype argument_list
")",
313 line_width
, length(prototype
)));
316 printh
("\tkobjop_t _m;");
318 firstvar =
"((kobj_t)" firstvar
")";
319 printh
("\tKOBJOPLOOKUP(" firstvar
"->ops, " mname
");");
320 retrn =
(ret
!= "void") ?
"return " : "";
321 printh
("\t" retrn
"((" mname
"_t *) _m)(" varname_list
");");
326 # Begin of the main program.
335 # Process the command line.
340 for (i =
1; i
< ARGC; i
++) {
341 if (ARGV[i
] ~
/^
-/) {
343 # awk doesn't have getopt(), so we have to do it ourselves.
344 # This is a bit clumsy, but it works.
346 for (j =
2; j
<=
length(ARGV[i
]); j
++) {
347 o =
substr(ARGV[i
], j
, 1);
348 if (o ==
"c") opt_c =
1;
349 else if (o ==
"h") opt_h =
1;
350 else if (o ==
"p") opt_p =
1;
351 else if (o ==
"d") opt_d =
1;
353 if (length(ARGV[i
]) > j
) {
354 opt_l =
substr(ARGV[i
], j
+ 1);
368 else if (ARGV[i
] ~
/\.m$
/)
369 filenames
[num_files
++] =
ARGV[i
];
374 if (!num_files
|| !
(opt_c
|| opt_h
))
378 debug
("Will produce files in original not in current directory");
381 if (opt_l !~
/^
[0-9]+$
/ || opt_l
< 1)
382 die
("Invalid line width '" opt_l
"'");
384 debug
("Line width set to " line_width
);
387 for (i =
0; i
< num_files
; i
++)
388 debug
("Filename: " filenames
[i
]);
390 for (file_i =
0; file_i
< num_files
; file_i
++) {
391 src = filenames
[file_i
];
392 cfilename = hfilename = src
;
393 sub(/\.m$
/, ".c", cfilename
);
394 sub(/\.m$
/, ".h", hfilename
);
396 sub(/^.
*\
//, "", cfilename
);
397 sub(/^.
*\
//, "", hfilename
);
400 debug
("Processing from " src
" to " cfilename
" / " hfilename
);
402 ctmpfilename = cfilename
".tmp";
403 htmpfilename = hfilename
".tmp";
407 " * This file is produced automatically.\n" \
408 " * Do not modify anything in here by hand.\n" \
410 " * Created from source file\n" \
413 " * makeobjops.awk\n" \
415 " * See the source file for legal information\n" \
418 printc
(common_head
"\n" \
419 "#include <sys/param.h>\n" \
420 "#include <sys/queue.h>\n" \
421 "#include <sys/kernel.h>\n" \
422 "#include <sys/kobj.h>");
426 delete methods
; # clear list of methods
429 error =
0; # to signal clean up and gerror setting
431 while (!error
&& (getline < src
) > 0) {
435 # Take special notice of include directives.
437 if (/^
#[ ]*include[ ]+["<][^">]+[">]/) {
439 sub(/^
#[ ]*include[ ]+/, "", incld);
440 debug
("Included file: " incld
);
441 printc
("#include " incld
);
444 sub(/#.*/, ""); # remove comments
445 sub(/^
[ ]+/, ""); # remove leading ...
446 sub(/[ ]+$
/, ""); # ... and trailing whitespace
448 if (/^$
/) { # skip empty lines
450 else if (/^INTERFACE
[ ]+[^
;]*[ ]*;?
[ ]*$
/)
452 else if (/^CODE
[ ]*{$
/)
453 printc
(handle_code
());
454 else if (/^HEADER
[ ]*{$
/)
455 printh
(handle_code
());
458 else if (/^STATICMETHOD
/)
462 warnsrc
("Invalid line encountered");
468 # Print the final '#endif' in the header file.
470 printh
("#endif /* _" intname
"_if_h_ */");
472 close (ctmpfilename
);
473 close (htmpfilename
);
476 warn
("Output skipped");
477 system_check
("rm -f " ctmpfilename
" " htmpfilename
);
482 system_check
("mv -f " ctmpfilename
" " cfilename
);
484 system_check
("mv -f " htmpfilename
" " hfilename
);