4 # Copyright (c) 1992, 1993
5 # The Regents of the University of California. All rights reserved.
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
15 # 4. Neither the name of the University nor the names of its contributors
16 # may be used to endorse or promote products derived from this software
17 # without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # From @(#)vnode_if.sh 8.1 (Berkeley) 6/10/93
32 # From @(#)makedevops.sh 1.1 1998/06/14 13:53:12 dfr Exp $
33 # From @(#)makedevops.sh ?.? 1998/10/05
34 # From src/sys/kern/makedevops.pl,v 1.12 1999/11/22 14:40:04 n_hibma Exp
35 # From src/sys/kern/makeobjops.pl,v 1.8 2001/11/16 02:02:42 joe Exp
40 # Script to produce kobj front-end sugar.
45 print "usage: makeobjops.awk <srcfile.m> [-d] [-p] [-l <nr>] [-c|-h]";
46 print "where -c produce only .c files";
47 print " -h produce only .h files";
48 print " -p use the path component in the source file for destination dir";
49 print " -l set line width for output files [80]";
50 print " -d switch on debugging";
56 print "makeobjops.awk:", msg > "/dev/stderr";
59 function warnsrc (msg)
61 warn(src ":" lineno ": " msg);
76 # These are just for convenience ...
77 function printc(s) {if (opt_c) print s > ctmpfilename;}
78 function printh(s) {if (opt_h) print s > htmpfilename;}
81 # If a line exceeds maxlength, split it into multiple
82 # lines at commas. Subsequent lines are indented by
83 # the specified number of spaces.
85 # In other words: Lines are split by replacing ", "
86 # by ",\n" plus indent spaces.
89 function format_line (line, maxlength, indent)
93 while (length(line) > maxlength) {
95 # Find the rightmost ", " so that the part
96 # to the left of it is just within maxlength.
97 # If there is none, give up and leave it as-is.
99 if (!match(substr(line, 1, maxlength + 1), /^.*, /))
101 rline = rline substr(line, 1, RLENGTH - 1) "\n";
102 line = sprintf("%*s", indent, "") substr(line, RLENGTH + 1);
108 # Join an array into a string.
111 function join (separator, array, num)
116 _result = separator array[num--] _result;
117 _result = array[1] _result;
123 # Execute a system command and report if it failed.
126 function system_check (cmd)
128 if ((rc = system(cmd)))
129 warn(cmd " failed (" rc ")");
133 # Handle "INTERFACE" line.
136 function handle_interface ()
139 sub(/;$/, "", intname);
140 if (intname !~ /^[a-z_][a-z0-9_]*$/) {
142 warnsrc("Invalid interface name '" intname "', use [a-z_][a-z0-9_]*");
147 warnsrc("Semicolon missing at end of line, no problem");
149 debug("Interface " intname);
151 printh("#ifndef _" intname "_if_h_");
152 printh("#define _" intname "_if_h_\n");
153 printc("#include \"" intname "_if.h\"\n");
157 # Pass doc comments through to the C file
159 function handle_doc ()
172 # Handle "CODE" and "HEADER" sections.
173 # Returns the code as-is.
176 function handle_code ()
181 sub(/[^ ].*$/, "", indent); # find the indent used
183 sub("^" indent, ""); # remove the indent
192 # Handle "METHOD" and "STATICMETHOD" sections.
195 function handle_method (static, doc)
198 # Get the return type and function name and delete that from
199 # the line. What is left is the possibly first function argument
200 # if it is on the same line.
203 warnsrc("No interface name defined");
207 sub(/^[^ ]+[ ]+/, "");
209 sub(/[ ]*\{.*$/, "", ret);
211 sub(/^.*[ ]/, "", name); # last element is name of method
212 sub(/[ ]+[^ ]+$/, "", ret); # return type
213 debug("Method: name=" name " return type=" ret);
215 sub(/^[^\{]*\{[ ]*/, "");
219 warnsrc("Invalid method specification");
224 if (name !~ /^[a-z_][a-z_0-9]*$/) {
225 warnsrc("Invalid method name '" name "', use [a-z_][a-z0-9_]*");
231 warnsrc("Duplicate method name");
235 methods[name] = name;
238 while (line !~ /\}/ && (getline < src) > 0) {
243 default_function = "";
244 if (!match(line, /\};?/)) {
245 warnsrc("Premature end of file");
249 extra = substr(line, RSTART + RLENGTH);
250 if (extra ~ /[ ]*DEFAULT[ ]*[a-zA-Z_][a-zA-Z_0-9]*[ ]*;/) {
251 default_function = extra;
252 sub(/.*DEFAULT[ ]*/, "", default_function);
253 sub(/[; ]+.*$/, "", default_function);
255 else if (extra && opt_d) {
256 # Warn about garbage at end of line.
257 warnsrc("Ignored '" extra "'");
259 sub(/\};?.*$/, "", line);
262 # Create a list of variables without the types prepended.
264 sub(/^[ ]+/, "", line); # remove leading ...
265 sub(/[ ]+$/, "", line); # ... and trailing whitespace
266 gsub(/[ ]+/, " ", line); # remove double spaces
268 num_arguments = split(line, arguments, / *; */) - 1;
269 delete varnames; # list of varnames
271 for (i = 1; i <= num_arguments; i++) {
273 continue; # skip argument if argument is empty
274 num_ar = split(arguments[i], ar, /[* ]+/);
275 if (num_ar < 2) { # only 1 word in argument?
276 warnsrc("no type for '" arguments[i] "'");
280 # Last element is name of variable.
281 varnames[++num_varnames] = ar[num_ar];
284 argument_list = join(", ", arguments, num_arguments);
285 varname_list = join(", ", varnames, num_varnames);
288 warn("Arguments: " argument_list);
289 warn("Varnames: " varname_list);
292 mname = intname "_" name; # method name
293 umname = toupper(mname); # uppercase method name
295 firstvar = varnames[1];
297 if (default_function == "")
298 default_function = "kobj_error_method";
300 # the method description
301 printh("/** @brief Unique descriptor for the " umname "() method */");
302 printh("extern struct kobjop_desc " mname "_desc;");
304 printh("/** @brief A function implementing the " umname "() method */");
305 prototype = "typedef " ret " " mname "_t(";
306 printh(format_line(prototype argument_list ");",
307 line_width, length(prototype)));
309 # Print out the method desc
310 printc("struct kobj_method " mname "_method_default = {");
311 printc("\t&" mname "_desc, (kobjop_t) " default_function);
314 printc("struct kobjop_desc " mname "_desc = {");
315 printc("\t0, &" mname "_method_default");
318 # Print out the method itself
320 if (0) { # haven't chosen the format yet
321 printh("static __inline " ret " " umname "(" varname_list ")");
322 printh("\t" join(";\n\t", arguments, num_arguments) ";");
325 prototype = "static __inline " ret " " umname "(";
326 printh(format_line(prototype argument_list ")",
327 line_width, length(prototype)));
330 printh("\tkobjop_t _m;");
332 firstvar = "((kobj_t)" firstvar ")";
333 printh("\tKOBJOPLOOKUP(" firstvar "->ops," mname ");");
334 retrn = (ret != "void") ? "return " : "";
335 printh("\t" retrn "((" mname "_t *) _m)(" varname_list ");");
340 # Begin of the main program.
349 # Process the command line.
354 for (i = 1; i < ARGC; i++) {
355 if (ARGV[i] ~ /^-/) {
357 # awk doesn't have getopt(), so we have to do it ourselves.
358 # This is a bit clumsy, but it works.
360 for (j = 2; j <= length(ARGV[i]); j++) {
361 o = substr(ARGV[i], j, 1);
362 if (o == "c") opt_c = 1;
363 else if (o == "h") opt_h = 1;
364 else if (o == "p") opt_p = 1;
365 else if (o == "d") opt_d = 1;
367 if (length(ARGV[i]) > j) {
368 opt_l = substr(ARGV[i], j + 1);
382 else if (ARGV[i] ~ /\.m$/)
383 filenames[num_files++] = ARGV[i];
388 if (!num_files || !(opt_c || opt_h))
392 debug("Will produce files in original not in current directory");
395 if (opt_l !~ /^[0-9]+$/ || opt_l < 1)
396 die("Invalid line width '" opt_l "'");
398 debug("Line width set to " line_width);
401 for (i = 0; i < num_files; i++)
402 debug("Filename: " filenames[i]);
404 for (file_i = 0; file_i < num_files; file_i++) {
405 src = filenames[file_i];
406 cfilename = hfilename = src;
407 sub(/\.m$/, ".c", cfilename);
408 sub(/\.m$/, ".h", hfilename);
410 sub(/^.*\//, "", cfilename);
411 sub(/^.*\//, "", hfilename);
414 debug("Processing from " src " to " cfilename " / " hfilename);
416 ctmpfilename = cfilename ".tmp";
417 htmpfilename = hfilename ".tmp";
421 " * This file is produced automatically.\n" \
422 " * Do not modify anything in here by hand.\n" \
424 " * Created from source file\n" \
427 " * makeobjops.awk\n" \
429 " * See the source file for legal information\n" \
432 printc(common_head "\n" \
433 "#include <sys/param.h>\n" \
434 "#include <sys/queue.h>\n" \
435 "#include <sys/kernel.h>\n" \
436 "#include <sys/kobj.h>");
440 delete methods; # clear list of methods
443 error = 0; # to signal clean up and gerror setting
446 while (!error && (getline < src) > 0) {
450 # Take special notice of include directives.
452 if (/^#[ ]*include[ ]+["<][^">]+[">]/) {
454 sub(/^#[ ]*include[ ]+/, "", incld);
455 debug("Included file: " incld);
456 printc("#include " incld);
459 sub(/#.*/, ""); # remove comments
460 sub(/^[ ]+/, ""); # remove leading ...
461 sub(/[ ]+$/, ""); # ... and trailing whitespace
463 if (/^$/) { # skip empty lines
466 lastdoc = handle_doc();
467 else if (/^INTERFACE[ ]+[^ ;]*[ ]*;?[ ]*$/) {
471 } else if (/^CODE[ ]*{$/)
472 printc(handle_code());
473 else if (/^HEADER[ ]*{$/)
474 printh(handle_code());
475 else if (/^METHOD/) {
476 handle_method(0, lastdoc);
478 } else if (/^STATICMETHOD/) {
479 handle_method(1, lastdoc);
483 warnsrc("Invalid line encountered");
489 # Print the final '#endif' in the header file.
491 printh("#endif /* _" intname "_if_h_ */");
493 close (ctmpfilename);
494 close (htmpfilename);
497 warn("Output skipped");
498 system_check("rm -f " ctmpfilename " " htmpfilename);
503 system_check("mv -f " ctmpfilename " " cfilename);
505 system_check("mv -f " htmpfilename " " hfilename);