Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / tools / vnode_if.awk
1 #!/usr/bin/awk -f
2
3 #
4 # Copyright (c) 1992, 1993
5 #       The Regents of the University of California.  All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
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 # 3. All advertising materials mentioning features or use of this software
16 #    must display the following acknowledgement:
17 #       This product includes software developed by the University of
18 #       California, Berkeley and its contributors.
19 # 4. Neither the name of the University nor the names of its contributors
20 #    may be used to endorse or promote products derived from this software
21 #    without specific prior written permission.
22 #
23 # THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 # ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 # SUCH DAMAGE.
34 #
35 #       @(#)vnode_if.sh 8.1 (Berkeley) 6/10/93
36 # $FreeBSD: src/sys/tools/vnode_if.awk,v 1.39 2003/06/22 21:20:06 truckman Exp $
37 # $DragonFly: src/sys/tools/Attic/vnode_if.awk,v 1.5 2004/03/07 12:48:34 eirikn Exp $
38 #
39 # Script to produce VFS front-end sugar.
40 #
41 # usage: vnode_if.awk <srcfile> [-c | -h]
42 #       (where <srcfile> is currently /sys/kern/vnode_if.src)
43 #
44
45 function usage()
46 {
47         print "usage: vnode_if.awk <srcfile> [-c|-h]";
48         exit 1;
49 }
50
51 function die(msg, what)
52 {
53         printf msg "\n", what > "/dev/stderr";
54         exit 1;
55 }
56
57 function t_spc(type)
58 {
59         # Append a space if the type is not a pointer
60         return (type ~ /\*$/) ? type : type " ";
61 }
62
63 # These are just for convenience ...
64 function printc(s) {print s > cfile;}
65 function printh(s) {print s > hfile;}
66
67 function add_debug_code(name, arg, pos)
68 {
69         if (arg == "vpp")
70                 arg = "*vpp";
71         if (lockdata[name, arg, pos] && (lockdata[name, arg, pos] != "-")) {
72                 if (arg ~ /^\*/) {
73                         printh("\tif ("substr(arg, 2)" != NULL) {");
74                 }
75                 # Add assertions for locking
76                 if (lockdata[name, arg, pos] == "L")
77                         printh("\tASSERT_VOP_LOCKED("arg", \""uname"\");");
78                 else if (lockdata[name, arg, pos] == "U")
79                         printh("\tASSERT_VOP_UNLOCKED("arg", \""uname"\");");
80                 else if (0) {
81                         # XXX More checks!
82                 }
83                 if (arg ~ /^\*/) {
84                         printh("\t}");
85                 }
86         }
87 }
88
89 function add_debug_pre(name)
90 {
91         if (lockdata[name, "pre"]) {
92                 printh("#ifdef  DEBUG_VFS_LOCKS");
93                 printh("\t"lockdata[name, "pre"]"(&a);");
94                 printh("#endif");
95         }
96 }
97
98 function add_debug_post(name)
99 {
100         if (lockdata[name, "post"]) {
101                 printh("#ifdef  DEBUG_VFS_LOCKS");
102                 printh("\t"lockdata[name, "post"]"(&a, rc);");
103                 printh("#endif");
104         }
105 }
106
107 function find_arg_with_type (type)
108 {
109         for (jj = 0; jj < numargs; jj++) {
110                 if (types[jj] == type) {
111                         return "VOPARG_OFFSETOF(struct " \
112                             name "_args,a_" args[jj] ")";
113                 }
114         }
115
116         return "VDESC_NO_OFFSET";
117 }
118
119 BEGIN{
120
121 # Process the command line
122 for (i = 1; i < ARGC; i++) {
123         arg = ARGV[i];
124         if (arg !~ /^-[ch]+$/ && arg !~ /\.src$/)
125                 usage();
126         if (arg ~ /^-.*c/)
127                 cfile = "vnode_if.c";
128         if (arg ~ /^-.*h/)
129                 hfile = "vnode_if.h";
130         if (arg ~ /\.src$/)
131                 srcfile = arg;
132 }
133 ARGC = 1;
134
135 if (!cfile && !hfile)
136         exit 0;
137
138 if (!srcfile)
139         usage();
140
141 common_head = \
142     "/*\n" \
143     " * This file is produced automatically.\n" \
144     " * Do not modify anything in here by hand.\n" \
145     " *\n" \
146     " * Created from $DragonFly: src/sys/tools/Attic/vnode_if.awk,v 1.5 2004/03/07 12:48:34 eirikn Exp $\n" \
147     " */\n" \
148     "\n";
149
150 if (hfile)
151         printh(common_head "extern struct vnodeop_desc vop_default_desc;");
152
153 if (cfile) {
154         printc(common_head \
155             "#include <sys/param.h>\n" \
156             "#include <sys/systm.h>\n" \
157             "#include <sys/vnode.h>\n" \
158             "\n" \
159             "struct vnodeop_desc vop_default_desc = {\n" \
160             "   1,\t\t\t/* special case, vop_default => 1 */\n" \
161             "   \"default\",\n" \
162             "   0,\n" \
163             "   NULL,\n" \
164             "   VDESC_NO_OFFSET,\n" \
165             "   VDESC_NO_OFFSET,\n" \
166             "   VDESC_NO_OFFSET,\n" \
167             "   VDESC_NO_OFFSET,\n" \
168             "   NULL,\n" \
169             "};\n");
170 }
171
172 while ((getline < srcfile) > 0) {
173         if (NF == 0)
174                 continue;
175         if ($1 ~ /^#%/) {
176                 if (NF != 6  ||  $1 != "#%"  || \
177                     $2 !~ /^[a-z]+$/  ||  $3 !~ /^[a-z]+$/  || \
178                     $4 !~ /^.$/  ||  $5 !~ /^.$/  ||  $6 !~ /^.$/)
179                         continue;
180                 if ($3 == "vpp")
181                         $3 = "*vpp";
182                 lockdata["vop_" $2, $3, "Entry"] = $4;
183                 lockdata["vop_" $2, $3, "OK"]    = $5;
184                 lockdata["vop_" $2, $3, "Error"] = $6;                  
185                 continue;
186         }
187
188         if ($1 ~ /^#!/) {
189                 if (NF != 4 || $1 != "#!")
190                         continue;
191                 if ($3 != "pre" && $3 != "post")
192                         continue;
193                 lockdata["vop_" $2, $3] = $4;
194                 continue;
195         }
196         if ($1 ~ /^#/)
197                 continue;
198
199         # Get the function name.
200         name = $1;
201         uname = toupper(name);
202
203         # Get the function arguments.
204         for (numargs = 0; ; ++numargs) {
205                 if ((getline < srcfile) <= 0) {
206                         die("Unable to read through the arguments for \"%s\"",
207                             name);
208                 }
209                 if ($1 ~ /^\};/)
210                         break;
211
212                 # Delete comments, if any.
213                 gsub (/\/\*.*\*\//, "");
214
215                 # Condense whitespace and delete leading/trailing space.
216                 gsub(/[[:space:]]+/, " ");
217                 sub(/^ /, "");
218                 sub(/ $/, "");
219
220                 # Pick off direction.
221                 if ($1 != "INOUT" && $1 != "IN" && $1 != "OUT")
222                         die("No IN/OUT direction for \"%s\".", $0);
223                 dirs[numargs] = $1;
224                 sub(/^[A-Z]* /, "");
225
226                 if ((reles[numargs] = $1) == "WILLRELE")
227                         sub(/^[A-Z]* /, "");
228                 else
229                         reles[numargs] = "WONTRELE";
230
231                 # kill trailing ;
232                 if (sub(/;$/, "") < 1)
233                         die("Missing end-of-line ; in \"%s\".", $0);
234
235                 # pick off variable name
236                 if ((argp = match($0, /[A-Za-z0-9_]+$/)) < 1)
237                         die("Missing var name \"a_foo\" in \"%s\".", $0);
238                 args[numargs] = substr($0, argp);
239                 $0 = substr($0, 1, argp - 1);
240
241                 # what is left must be type
242                 # remove trailing space (if any)
243                 sub(/ $/, "");
244                 types[numargs] = $0;
245
246         }
247
248         if (hfile) {
249                 # Print out the vop_F_args structure.
250                 printh("struct "name"_args {\n\tstruct vnodeop_desc *a_desc;");
251                 for (i = 0; i < numargs; ++i)
252                         printh("\t" t_spc(types[i]) "a_" args[i] ";");
253                 printh("};");
254
255                 # Print out extern declaration.
256                 printh("extern struct vnodeop_desc " name "_desc;");
257
258                 # Print out prototype.
259                 printh("static __inline int " uname "(");
260                 for (i = 0; i < numargs; ++i) {
261                         printh("\t" t_spc(types[i]) args[i] \
262                             (i < numargs - 1 ? "," : ");"));
263                 }
264
265                 # Print out function.
266                 printh("static __inline int " uname "(");
267                 for (i = 0; i < numargs; ++i) {
268                         printh("\t" t_spc(types[i]) args[i] \
269                             (i < numargs - 1 ? "," : ")"));
270                 }
271                 printh("{\n\tstruct " name "_args a;");
272                 printh("\tint rc;");
273                 printh("\ta.a_desc = VDESC(" name ");");
274                 for (i = 0; i < numargs; ++i)
275                         printh("\ta.a_" args[i] " = " args[i] ";");
276                 for (i = 0; i < numargs; ++i)
277                         add_debug_code(name, args[i], "Entry");
278                 add_debug_pre(name);
279                 printh("\trc = VCALL(" args[0] ", VOFFSET(" name "), &a);");
280                 printh("if (rc == 0) {");
281                 for (i = 0; i < numargs; ++i)
282                         add_debug_code(name, args[i], "OK");
283                 printh("} else {");
284                 for (i = 0; i < numargs; ++i)
285                         add_debug_code(name, args[i], "Error");
286                 printh("}");
287                 add_debug_post(name);
288                 printh("\treturn (rc);\n}");
289         }
290
291         if (cfile) {
292                 # Print out the vop_F_vp_offsets structure.  This all depends
293                 # on naming conventions and nothing else.
294                 printc("static int " name "_vp_offsets[] = {");
295                 # as a side effect, figure out the releflags
296                 releflags = "";
297                 vpnum = 0;
298                 for (i = 0; i < numargs; i++) {
299                         if (types[i] == "struct vnode *") {
300                                 printc("\tVOPARG_OFFSETOF(struct " name \
301                                     "_args,a_" args[i] "),");
302                                 if (reles[i] == "WILLRELE") {
303                                         releflags = releflags \
304                                             "|VDESC_VP" vpnum "_WILLRELE";
305                                 }
306                                 vpnum++;
307                         }
308                 }
309
310                 sub(/^\|/, "", releflags);
311                 printc("\tVDESC_NO_OFFSET");
312                 printc("};");
313
314                 # Print out the vnodeop_desc structure.
315                 printc("struct vnodeop_desc " name "_desc = {");
316                 # offset
317                 printc("\t0,");
318                 # printable name
319                 printc("\t\"" name "\",");
320                 # flags
321                 vppwillrele = "";
322                 for (i = 0; i < numargs; i++) {
323                         if (types[i] == "struct vnode **" && \
324                             reles[i] == "WILLRELE") {
325                                 vppwillrele = "|VDESC_VPP_WILLRELE";
326                         }
327                 }
328
329                 if (!releflags)
330                         releflags = "0";
331                 printc("\t" releflags vppwillrele ",");
332
333                 # vp offsets
334                 printc("\t" name "_vp_offsets,");
335                 # vpp (if any)
336                 printc("\t" find_arg_with_type("struct vnode **") ",");
337                 # cred (if any)
338                 printc("\t" find_arg_with_type("struct ucred *") ",");
339                 # thread (if any)
340                 printc("\t" find_arg_with_type("struct proc *") ",");
341                 # componentname
342                 printc("\t" find_arg_with_type("struct componentname *") ",");
343                 # transport layer information
344                 printc("\tNULL,\n};\n");
345         }
346 }
347  
348 if (hfile)
349         close(hfile);
350 if (cfile)
351         close(cfile);
352 close(srcfile);
353
354 exit 0;
355
356 }