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 $
43 # Script to produce kobj front-end sugar.
48 print "usage: makeobjops.awk <srcfile.m> [-d] [-p] [-l <nr>] [-c|-h]";
49 print "where -c produce only .c files";
50 print " -h produce only .h files";
51 print " -p use the path component in the source file for destination dir";
52 print " -l set line width for output files [80]";
53 print " -d switch on debugging";
59 print "makeobjops.awk:", msg > "/dev/stderr";
62 function warnsrc (msg)
64 warn(src ":" lineno ": " msg);
79 # These are just for convenience ...
80 function printc(s) {if (opt_c) print s > ctmpfilename;}
81 function printh(s) {if (opt_h) print s > htmpfilename;}
84 # If a line exceeds maxlength, split it into multiple
85 # lines at commas. Subsequent lines are indented by
86 # the specified number of spaces.
88 # In other words: Lines are split by replacing ", "
89 # by ",\n" plus indent spaces.
92 function format_line (line, maxlength, indent)
96 while (length(line) > maxlength) {
98 # Find the rightmost ", " so that the part
99 # to the left of it is just within maxlength.
100 # If there is none, give up and leave it as-is.
102 if (!match(substr(line, 1, maxlength + 1), /^.*, /))
104 rline = rline substr(line, 1, RLENGTH - 1) "\n";
105 line = sprintf("%*s", indent, "") substr(line, RLENGTH + 1);
111 # Join an array into a string.
114 function join (separator, array, num)
119 _result = separator array[num--] _result;
120 _result = array[1] _result;
126 # Execute a system command and report if it failed.
129 function system_check (cmd)
131 if ((rc = system(cmd)))
132 warn(cmd " failed (" rc ")");
136 # Handle "INTERFACE" line.
139 function handle_interface ()
142 sub(/;$/, "", intname);
143 if (intname !~ /^[a-z_][a-z0-9_]*$/) {
145 warnsrc("Invalid interface name '" intname "', use [a-z_][a-z0-9_]*");
150 warnsrc("Semicolon missing at end of line, no problem");
152 debug("Interface " intname);
154 printh("#ifndef _" intname "_if_h_");
155 printh("#define _" intname "_if_h_\n");
156 printc("#include \"" intname "_if.h\"\n");
160 # Handle "CODE" and "HEADER" sections.
161 # Returns the code as-is.
164 function handle_code ()
169 sub(/[^ ].*$/, "", indent); # find the indent used
171 sub("^" indent, ""); # remove the indent
180 # Handle "METHOD" and "STATICMETHOD" sections.
183 function handle_method (static)
186 # Get the return type and function name and delete that from
187 # the line. What is left is the possibly first function argument
188 # if it is on the same line.
191 warnsrc("No interface name defined");
195 sub(/^[^ ]+[ ]+/, "");
197 sub(/[ ]*\{.*$/, "", ret);
199 sub(/^.*[ ]/, "", name); # last element is name of method
200 sub(/[ ]+[^ ]+$/, "", ret); # return type
201 debug("Method: name=" name " return type=" ret);
203 sub(/^[^\{]*\{[ ]*/, "");
207 warnsrc("Invalid method specification");
212 if (name !~ /^[a-z_][a-z_0-9]*$/) {
213 warnsrc("Invalid method name '" name "', use [a-z_][a-z0-9_]*");
219 warnsrc("Duplicate method name");
223 methods[name] = name;
226 while (line !~ /\}/ && (getline < src) > 0) {
232 if (!match(line, /\};?/)) {
233 warnsrc("Premature end of file");
237 extra = substr(line, RSTART + RLENGTH);
238 if (extra ~ /[ ]*DEFAULT[ ]*[a-zA-Z_][a-zA-Z_0-9]*[ ]*;/) {
240 sub(/.*DEFAULT[ ]*/, "", default);
241 sub(/[; ]+.*$/, "", default);
243 else if (extra && opt_d) {
244 # Warn about garbage at end of line.
245 warnsrc("Ignored '" extra "'");
247 sub(/\};?.*$/, "", line);
250 # Create a list of variables without the types prepended.
252 sub(/^[ ]+/, "", line); # remove leading ...
253 sub(/[ ]+$/, "", line); # ... and trailing whitespace
254 gsub(/[ ]+/, " ", line); # remove double spaces
256 num_arguments = split(line, arguments, / *; */) - 1;
257 delete varnames; # list of varnames
259 for (i = 1; i <= num_arguments; i++) {
261 continue; # skip argument if argument is empty
262 num_ar = split(arguments[i], ar, /[* ]+/);
263 if (num_ar < 2) { # only 1 word in argument?
264 warnsrc("no type for '" arguments[i] "'");
268 # Last element is name of variable.
269 varnames[++num_varnames] = ar[num_ar];
272 argument_list = join(", ", arguments, num_arguments);
273 varname_list = join(", ", varnames, num_varnames);
276 warn("Arguments: " argument_list);
277 warn("Varnames: " varname_list);
280 mname = intname "_" name; # method name
281 umname = toupper(mname); # uppercase method name
283 firstvar = varnames[1];
286 default = "kobj_error_method";
288 # the method description
289 printh("extern struct kobjop_desc " mname "_desc;");
291 prototype = "typedef " ret " " mname "_t(";
292 printh(format_line(prototype argument_list ");",
293 line_width, length(prototype)));
295 # Print out the method desc
296 printc("struct kobjop_desc " mname "_desc = {");
297 printc("\t0, { &" mname "_desc, (kobjop_t)" default " }");
300 # Print out the method itself
301 if (0) { # haven't chosen the format yet
302 printh("static __inline " ret " " umname "(" varname_list ")");
303 printh("\t" join(";\n\t", arguments, num_arguments) ";");
306 prototype = "static __inline " ret " " umname "(";
307 printh(format_line(prototype argument_list ")",
308 line_width, length(prototype)));
311 printh("\tkobjop_t _m;");
313 firstvar = "((kobj_t)" firstvar ")";
314 printh("\tKOBJOPLOOKUP(" firstvar "->ops, " mname ");");
315 retrn = (ret != "void") ? "return " : "";
316 printh("\t" retrn "((" mname "_t *) _m)(" varname_list ");");
321 # Begin of the main program.
330 # Process the command line.
335 for (i = 1; i < ARGC; i++) {
336 if (ARGV[i] ~ /^-/) {
338 # awk doesn't have getopt(), so we have to do it ourselves.
339 # This is a bit clumsy, but it works.
341 for (j = 2; j <= length(ARGV[i]); j++) {
342 o = substr(ARGV[i], j, 1);
343 if (o == "c") opt_c = 1;
344 else if (o == "h") opt_h = 1;
345 else if (o == "p") opt_p = 1;
346 else if (o == "d") opt_d = 1;
348 if (length(ARGV[i]) > j) {
349 opt_l = substr(ARGV[i], j + 1);
363 else if (ARGV[i] ~ /\.m$/)
364 filenames[num_files++] = ARGV[i];
369 if (!num_files || !(opt_c || opt_h))
373 debug("Will produce files in original not in current directory");
376 if (opt_l !~ /^[0-9]+$/ || opt_l < 1)
377 die("Invalid line width '" opt_l "'");
379 debug("Line width set to " line_width);
382 for (i = 0; i < num_files; i++)
383 debug("Filename: " filenames[i]);
385 for (file_i = 0; file_i < num_files; file_i++) {
386 src = filenames[file_i];
387 cfilename = hfilename = src;
388 sub(/\.m$/, ".c", cfilename);
389 sub(/\.m$/, ".h", hfilename);
391 sub(/^.*\//, "", cfilename);
392 sub(/^.*\//, "", hfilename);
395 debug("Processing from " src " to " cfilename " / " hfilename);
397 ctmpfilename = cfilename ".tmp";
398 htmpfilename = hfilename ".tmp";
402 " * This file is produced automatically.\n" \
403 " * Do not modify anything in here by hand.\n" \
405 " * Created from source file\n" \
408 " * makeobjops.awk\n" \
410 " * See the source file for legal information\n" \
413 printc(common_head "\n" \
414 "#include <sys/param.h>\n" \
415 "#include <sys/queue.h>\n" \
416 "#include <sys/kernel.h>\n" \
417 "#include <sys/kobj.h>");
421 delete methods; # clear list of methods
424 error = 0; # to signal clean up and gerror setting
426 while (!error && (getline < src) > 0) {
430 # Take special notice of include directives.
432 if (/^#[ ]*include[ ]+["<][^">]+[">]/) {
434 sub(/^#[ ]*include[ ]+/, "", incld);
435 debug("Included file: " incld);
436 printc("#include " incld);
439 sub(/#.*/, ""); # remove comments
440 sub(/^[ ]+/, ""); # remove leading ...
441 sub(/[ ]+$/, ""); # ... and trailing whitespace
443 if (/^$/) { # skip empty lines
445 else if (/^\/\*\*/) # ... and doxygen comments
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);