sed(1): add -r option for compatibility with GNU sed.
[dragonfly.git] / usr.bin / kdump / mkioctls
1 #!/bin/sh
2 #
3 # $FreeBSD: src/usr.bin/kdump/mkioctls,v 1.15.2.5 2002/11/15 18:22:31 ru Exp $
4
5 set -e
6
7 if [ "x$1" = "x-s" ]; then
8         use_switch=1
9         shift
10 else
11         use_switch=0
12 fi
13
14 if [ -z "$1" ]; then
15         echo "usage: sh $0 [-s] include-dir"
16         exit 1
17 fi
18
19 LC_ALL=C; export LC_ALL
20
21 # Build a list of headers that have ioctls in them.
22 # XXX should we use an ANSI cpp?
23 # XXX leave out cam/, fs/, netsmb/,
24 #     and ufs/ because they are fake softlinks
25 ioctl_includes=`
26         cd $1
27         find -s * -name '*.h' -follow |
28                 egrep -v '^(cam/)|^(fs/)|^(netsmb/)|^(ufs/)' |
29                 xargs egrep -l \
30 '^#[    ]*define[       ]+[A-Za-z_][A-Za-z0-9_]*[       ]+_IO[^a-z0-9_]' |
31                 awk '{printf("#include <%s>\\\\n", $1)}'
32 `
33
34 awk -v x="$ioctl_includes" 'BEGIN {print x}' |
35         gcc -D_KERNEL_STRUCTURES -E -I$1 -dM - |
36         awk -v ioctl_includes="$ioctl_includes" -v use_switch="$use_switch" '
37 BEGIN {
38         print "/* XXX obnoxious prerequisites. */"
39         print "#define COMPAT_43"
40         print "#define _KERNEL_STRUCTURES"
41         print "#include <sys/tty.h>"
42         print "#include <net/if_arp.h>"
43         print "#include <net/route.h>"
44         print "#include <netinet/in.h>"
45         print "#include <net/ip_mroute/ip_mroute.h>"
46         print "#include <netinet6/nd6.h>"
47         print "#include <netinet6/ip6_mroute.h>"
48         print "#include <stdio.h>"
49         print "#include <cam/cam.h>"
50         print ""
51         print ioctl_includes
52         print "const char *ioctlname(u_long);"
53         print ""
54         print "const char *"
55         print "ioctlname(u_long val)"
56         print "{"
57         if (use_switch)
58                 print "\tswitch(val) {"
59 }
60
61 /^#[    ]*define[       ]+[A-Za-z_][A-Za-z0-9_]*[       ]+_IO/ {
62         
63         # find where the name starts
64         for (i = 1; i <= NF; i++)
65                 if ($i ~ /define/)
66                         break;
67         ++i;
68         # 
69         if (use_switch)
70                 printf("\tcase %s:\n\t\treturn(\"%s\");\n", $i, $i);
71         else
72                 printf("\tif (val ==  %s)\n\t\treturn(\"%s\");\n", $i, $i);
73
74 }
75 END {
76         if (use_switch)
77                 print "\t}"
78         print "\n\treturn(NULL);"
79         print "}"
80 }
81 '