sys/vfs/msdosfs: Sync with FreeBSD (non functional diffs)
[dragonfly.git] / sys / tools / makeobjops.awk
1 #!/usr/bin/awk -f
2 #
3 # Copyright (c) 1992, 1993
4 #        The Regents of the University of California.  All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
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. Neither the name of the University nor the names of its contributors
15 #    may be used to endorse or promote products derived from this software
16 #    without specific prior written permission.
17 #
18 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 # SUCH DAMAGE.
29 #
30 # From @(#)vnode_if.sh        8.1 (Berkeley) 6/10/93
31 # From @(#)makedevops.sh 1.1 1998/06/14 13:53:12 dfr Exp $
32 # From @(#)makedevops.sh ?.? 1998/10/05
33 # From src/sys/kern/makedevops.pl,v 1.12 1999/11/22 14:40:04 n_hibma Exp
34 # From src/sys/kern/makeobjops.pl,v 1.8 2001/11/16 02:02:42 joe Exp
35 #
36 # $FreeBSD: src/sys/tools/makeobjops.awk,v 1.3 2003/10/16 13:29:26 dfr Exp $
37
38 #
39 #   Script to produce kobj front-end sugar.
40 #
41
42 function usage ()
43 {
44         print "usage: makeobjops.awk <srcfile.m> [-d] [-p] [-l <nr>] [-c|-h]";
45         print "where -c   produce only .c files";
46         print "      -h   produce only .h files";
47         print "      -p   use the path component in the source file for destination dir";
48         print "      -l   set line width for output files [80]";
49         print "      -d   switch on debugging";
50         exit 1;
51 }
52
53 function warn (msg)
54 {
55         print "makeobjops.awk:", msg > "/dev/stderr";
56 }
57
58 function warnsrc (msg)
59 {
60         warn(src ":" lineno ": " msg);
61 }
62
63 function debug (msg)
64 {
65         if (opt_d)
66                 warn(msg);
67 }
68
69 function die (msg)
70 {
71         warn(msg);
72         exit 1;
73 }
74
75 #   These are just for convenience ...
76 function printc(s) {if (opt_c) print s > ctmpfilename;}
77 function printh(s) {if (opt_h) print s > htmpfilename;}
78
79 #
80 #   If a line exceeds maxlength, split it into multiple
81 #   lines at commas.  Subsequent lines are indented by
82 #   the specified number of spaces.
83 #
84 #   In other words:  Lines are split by replacing ", "
85 #   by ",\n" plus indent spaces.
86 #
87
88 function format_line (line, maxlength, indent)
89 {
90         rline = "";
91
92         while (length(line) > maxlength) {
93                 #
94                 #   Find the rightmost ", " so that the part
95                 #   to the left of it is just within maxlength.
96                 #   If there is none, give up and leave it as-is.
97                 #
98                 if (!match(substr(line, 1, maxlength + 1), /^.*, /))
99                         break;
100                 rline = rline substr(line, 1, RLENGTH - 1) "\n";
101                 line = sprintf("%*s", indent, "") substr(line, RLENGTH + 1);
102         }
103         return rline line;
104 }
105
106 #
107 #   Join an array into a string.
108 #
109
110 function join (separator, array, num)
111 {
112         _result = ""
113         if (num) {
114                 while (num > 1)
115                         _result = separator array[num--] _result;
116                 _result = array[1] _result;
117         }
118         return _result;
119 }
120
121 #
122 #   Execute a system command and report if it failed.
123 #
124
125 function system_check (cmd)
126 {
127         if ((rc = system(cmd)))
128                 warn(cmd " failed (" rc ")");
129 }
130
131 #
132 #   Handle "INTERFACE" line.
133 #
134
135 function handle_interface ()
136 {
137         intname = $2;
138         sub(/;$/, "", intname);
139         if (intname !~ /^[a-z_][a-z0-9_]*$/) {
140                 debug($0);
141                 warnsrc("Invalid interface name '" intname "', use [a-z_][a-z0-9_]*");
142                 error = 1;
143                 return;
144         }
145         if (!/;[        ]*$/)
146                 warnsrc("Semicolon missing at end of line, no problem");
147
148         debug("Interface " intname);
149
150         printh("#ifndef _" intname "_if_h_");
151         printh("#define _" intname "_if_h_\n");
152         printc("#include \"" intname "_if.h\"\n");
153 }
154
155 #
156 #   Handle "CODE" and "HEADER" sections.
157 #   Returns the code as-is.
158 #
159
160 function handle_code ()
161 {
162         code = "\n";
163         getline < src;
164         indent = $0;
165         sub(/[^  ].*$/, "", indent);    # find the indent used
166         while (!/^}/) {
167                 sub("^" indent, "");    # remove the indent
168                 code = code $0 "\n";
169                 getline < src;
170                 lineno++;;
171         }
172         return code;
173 }
174
175 #
176 #   Handle "METHOD" and "STATICMETHOD" sections.
177 #
178
179 function handle_method (static)
180 {
181         #
182         #   Get the return type and function name and delete that from
183         #   the line. What is left is the possibly first function argument
184         #   if it is on the same line.
185         #
186         if (!intname) {
187                 warnsrc("No interface name defined");
188                 error = 1;
189                 return;
190         }
191         sub(/^[^        ]+[     ]+/, "");
192         ret = $0;
193         sub(/[  ]*\{.*$/, "", ret);
194         name = ret;
195         sub(/^.*[       ]/, "", name);  # last element is name of method
196         sub(/[  ]+[^    ]+$/, "", ret); # return type
197         debug("Method: name=" name " return type=" ret);
198
199         sub(/^[^\{]*\{[  ]*/, "");
200
201         if (!name || !ret) {
202                 debug($0);
203                 warnsrc("Invalid method specification");
204                 error = 1;
205                 return;
206         }
207
208         if (name !~ /^[a-z_][a-z_0-9]*$/) {
209                 warnsrc("Invalid method name '" name "', use [a-z_][a-z0-9_]*");
210                 error = 1;
211                 return;
212         }
213
214         if (methods[name]) {
215                 warnsrc("Duplicate method name");
216                 error = 1;
217                 return;
218         }
219         methods[name] = name;
220
221         line = $0;
222         while (line !~ /\}/ && (getline < src) > 0) {
223                 line = line " " $0;
224                 lineno++
225         }
226
227         default = "";
228         if (!match(line, /\};?/)) {
229                 warnsrc("Premature end of file");
230                 error = 1;
231                 return;
232         }
233         extra = substr(line, RSTART + RLENGTH);
234         if (extra ~ /[   ]*DEFAULT[     ]*[a-zA-Z_][a-zA-Z_0-9]*[       ]*;/) {
235                 default = extra;
236                 sub(/.*DEFAULT[  ]*/, "", default);
237                 sub(/[;         ]+.*$/, "", default);
238         }
239         else if (extra && opt_d) {
240                 #   Warn about garbage at end of line.
241                 warnsrc("Ignored '" extra "'");
242         }
243         sub(/\};?.*$/, "", line);
244
245         #
246         #   Create a list of variables without the types prepended.
247         #
248         sub(/^[  ]+/, "", line);        # remove leading ...
249         sub(/[  ]+$/, "", line);        # ... and trailing whitespace
250         gsub(/[  ]+/, " ", line);       # remove double spaces
251
252         num_arguments = split(line, arguments, / *; */) - 1;
253         delete varnames;                # list of varnames
254         num_varnames = 0;
255         for (i = 1; i <= num_arguments; i++) {
256                 if (!arguments[i])
257                         continue;       # skip argument if argument is empty
258                 num_ar = split(arguments[i], ar, /[*    ]+/);
259                 if (num_ar < 2) {       # only 1 word in argument?
260                         warnsrc("no type for '" arguments[i] "'");
261                         error = 1;
262                         return;
263                 }
264                 #   Last element is name of variable.
265                 varnames[++num_varnames] = ar[num_ar];
266         }
267
268         argument_list = join(", ", arguments, num_arguments);
269         varname_list = join(", ", varnames, num_varnames);
270
271         if (opt_d) {
272                 warn("Arguments: " argument_list);
273                 warn("Varnames: " varname_list);
274         }
275
276         mname = intname "_" name;       # method name
277         umname = toupper(mname);        # uppercase method name
278
279         firstvar = varnames[1];
280
281         if (default == "")
282                 default = "kobj_error_method";
283
284         # the method description 
285         printh("extern struct kobjop_desc " mname "_desc;");
286         # the method typedef
287         prototype = "typedef " ret " " mname "_t(";
288         printh(format_line(prototype argument_list ");",
289             line_width, length(prototype)));
290
291         # Print out the method desc
292         printc("struct kobjop_desc " mname "_desc = {");
293         printc("\t0, { &" mname "_desc, (kobjop_t)" default " }");
294         printc("};\n");
295
296         # Print out the method itself
297         if (0) {                # haven't chosen the format yet
298                 printh("static __inline " ret " " umname "(" varname_list ")");
299                 printh("\t" join(";\n\t", arguments, num_arguments) ";");
300         }
301         else {
302                 prototype = "static __inline " ret " " umname "(";
303                 printh(format_line(prototype argument_list ")",
304                     line_width, length(prototype)));
305         }
306         printh("{");
307         printh("\tkobjop_t _m;");
308         if (!static)
309                 firstvar = "((kobj_t)" firstvar ")";
310         printh("\tKOBJOPLOOKUP(" firstvar "->ops, " mname ");");
311         retrn =  (ret != "void") ? "return " : "";
312         printh("\t" retrn "((" mname "_t *) _m)(" varname_list ");");
313         printh("}\n");
314 }
315
316 #
317 #   Begin of the main program.
318 #
319
320 BEGIN {
321
322 line_width = 80;
323 gerror = 0;
324
325 #
326 #   Process the command line.
327 #
328
329 num_files = 0;
330
331 for (i = 1; i < ARGC; i++) {
332         if (ARGV[i] ~ /^-/) {
333                 #
334                 #   awk doesn't have getopt(), so we have to do it ourselves.
335                 #   This is a bit clumsy, but it works.
336                 #
337                 for (j = 2; j <= length(ARGV[i]); j++) {
338                         o = substr(ARGV[i], j, 1);
339                         if      (o == "c")      opt_c = 1;
340                         else if (o == "h")      opt_h = 1;
341                         else if (o == "p")      opt_p = 1;
342                         else if (o == "d")      opt_d = 1;
343                         else if (o == "l") {
344                                 if (length(ARGV[i]) > j) {
345                                         opt_l = substr(ARGV[i], j + 1);
346                                         break;
347                                 }
348                                 else {
349                                         if (++i < ARGC)
350                                                 opt_l = ARGV[i];
351                                         else
352                                                 usage();
353                                 }
354                         }
355                         else
356                                 usage();
357                 }
358         }
359         else if (ARGV[i] ~ /\.m$/)
360                 filenames[num_files++] = ARGV[i];
361         else
362                 usage();
363 }
364
365 if (!num_files || !(opt_c || opt_h))
366         usage();
367
368 if (opt_p)
369         debug("Will produce files in original not in current directory");
370
371 if (opt_l) {
372         if (opt_l !~ /^[0-9]+$/ || opt_l < 1)
373                 die("Invalid line width '" opt_l "'");
374         line_width = opt_l;
375         debug("Line width set to " line_width);
376 }
377
378 for (i = 0; i < num_files; i++)
379         debug("Filename: " filenames[i]);
380
381 for (file_i = 0; file_i < num_files; file_i++) {
382         src = filenames[file_i];
383         cfilename = hfilename = src;
384         sub(/\.m$/, ".c", cfilename);
385         sub(/\.m$/, ".h", hfilename);
386         if (!opt_p) {
387                 sub(/^.*\//, "", cfilename);
388                 sub(/^.*\//, "", hfilename);
389         }
390
391         debug("Processing from " src " to " cfilename " / " hfilename);
392
393         ctmpfilename = cfilename ".tmp";
394         htmpfilename = hfilename ".tmp";
395
396         common_head = \
397             "/*\n" \
398             " * This file is produced automatically.\n" \
399             " * Do not modify anything in here by hand.\n" \
400             " *\n" \
401             " * Created from source file\n" \
402             " *   " src "\n" \
403             " * with\n" \
404             " *   makeobjops.awk\n" \
405             " *\n" \
406             " * See the source file for legal information\n" \
407             " */\n";
408
409         printc(common_head "\n" \
410             "#include <sys/param.h>\n" \
411             "#include <sys/queue.h>\n" \
412             "#include <sys/kernel.h>\n" \
413             "#include <sys/kobj.h>");
414
415         printh(common_head);
416
417         delete methods;         # clear list of methods
418         intname = "";
419         lineno = 0;
420         error = 0;              # to signal clean up and gerror setting
421
422         while (!error && (getline < src) > 0) {
423                 lineno++;
424
425                 #
426                 #   Take special notice of include directives.
427                 #
428                 if (/^#[        ]*include[      ]+["<][^">]+[">]/) {
429                         incld = $0;
430                         sub(/^#[        ]*include[      ]+/, "", incld);
431                         debug("Included file: " incld);
432                         printc("#include " incld);
433                 }
434
435                 sub(/#.*/, "");         # remove comments
436                 sub(/^[  ]+/, "");      # remove leading ...
437                 sub(/[  ]+$/, "");      # ... and trailing whitespace
438
439                 if (/^$/) {             # skip empty lines
440                 }
441                 else if (/^\/\*\*/)     # ... and doxygen comments
442                         while (!/\*\//) {
443                                 getline < src;
444                                 lineno++;
445                         }
446                 else if (/^INTERFACE[   ]+[^    ;]*[    ]*;?[   ]*$/)
447                         handle_interface();
448                 else if (/^CODE[        ]*{$/)
449                         printc(handle_code());
450                 else if (/^HEADER[       ]*{$/)
451                         printh(handle_code());
452                 else if (/^METHOD/)
453                         handle_method(0);
454                 else if (/^STATICMETHOD/)
455                         handle_method(1);
456                 else {
457                         debug($0);
458                         warnsrc("Invalid line encountered");
459                         error = 1;
460                 }
461         }
462
463         #
464         #   Print the final '#endif' in the header file.
465         #
466         printh("#endif /* _" intname "_if_h_ */");
467
468         close (ctmpfilename);
469         close (htmpfilename);
470
471         if (error) {
472                 warn("Output skipped");
473                 system_check("rm -f " ctmpfilename " " htmpfilename);
474                 gerror = 1;
475         }
476         else {
477                 if (opt_c)
478                         system_check("mv -f " ctmpfilename " " cfilename);
479                 if (opt_h)
480                         system_check("mv -f " htmpfilename " " hfilename);
481         }
482 }
483
484 exit gerror;
485
486 }