1#! /bin/sh -
2#
3
4set -e
5
6case $# in
7    0)	echo "usage: $0 input-file <config-file>" 1>&2
8	exit 1
9	;;
10esac
11
12if [ -n "$2" -a -f "$2" ]; then
13	. $2
14fi
15
16sed -e '
17s/\$//g
18:join
19	/\\$/{a\
20
21	N
22	s/\\\n//
23	b join
24	}
252,${
26	/^#/!s/\([{}()*,]\)/ \1 /g
27}
28' < $1 | awk '
29	BEGIN {
30		printf "#include <sys/param.h>\n"
31		printf "#include <machine/atomic.h>\n"
32		printf "\n"
33		printf "#include <sys/_semaphore.h>\n"
34		printf "#include <sys/aio.h>\n"
35		printf "#include <sys/cpuset.h>\n"
36		printf "#include <sys/jail.h>\n"
37		printf "#include <sys/linker.h>\n"
38		printf "#include <sys/mac.h>\n"
39		printf "#include <sys/module.h>\n"
40		printf "#include <sys/mount.h>\n"
41		printf "#include <sys/mqueue.h>\n"
42		printf "#include <sys/msg.h>\n"
43		printf "#include <sys/poll.h>\n"
44		printf "#include <sys/proc.h>\n"
45		printf "#include <sys/resource.h>\n"
46		printf "#include <sys/sem.h>\n"
47		printf "#include <sys/shm.h>\n"
48		printf "#include <sys/signal.h>\n"
49		printf "#include <sys/signalvar.h>\n"
50		printf "#include <sys/socket.h>\n"
51		printf "#include <sys/stat.h>\n"
52		printf "#include <sys/thr.h>\n"
53		printf "#include <sys/time.h>\n"
54		printf "#include <sys/timex.h>\n"
55		printf "#include <sys/timeffc.h>\n"
56		printf "#include <sys/ucontext.h>\n"
57		printf "#include <sys/utsname.h>\n"
58		printf "#include <sys/uuid.h>\n"
59		printf "#include <sys/wait.h>\n"
60		printf "\n"
61		printf "#ifndef _ACL_PRIVATE\n"
62		printf "#define _ACL_PRIVATE\n"
63		printf "#endif\n"
64		printf "#include <sys/acl.h>\n"
65		printf "\n"
66		printf "#ifndef EBUSY\n"
67		printf "#define errno 0\n"
68		printf "#define EBUSY 0\n"
69		printf "#endif\n"
70		printf "#include <sys/umtx.h>\n"
71		printf "\n"
72		# existing compat shims
73		printf "struct ostat;\n"
74		printf "struct nstat;\n"
75		printf "struct ostatfs;\n"
76		printf "struct osigaction;\n"
77		printf "struct osigcontext;\n"
78		printf "struct oaiocb;\n"
79		printf "union semun_old;\n"
80		printf "typedef unsigned int osigset_t;\n"
81		printf "struct msqid_ds_old;\n"
82		printf "struct shmid_ds_old;\n"
83		# TODO
84		printf "struct freebsd4_ucontext;\n"
85		printf "struct sctp_sndrcvinfo;\n"
86		printf "\n"
87	}
88	NF < 4 || $1 !~ /^[0-9]+$/ {
89		next
90	}
91	$3 ~ "UNIMPL" || $3 ~ "OBSOL" || $3 ~ "NODEF" || $3 ~ "NOPROTO" ||
92	$3 ~ "NOSTD"{
93		next
94	}
95	$4 == "{" {
96		if ($3 ~ /COMPAT[0-9]*/) {
97			n = split($3, flags, /\|/)
98			for (i = 1; i <= n; i++) {
99				if (flags[i] == "COMPAT") {
100					$6 = "o" $6
101				} else if (flags[i] ~ /COMPAT[0-9]+/) {
102					sub(/COMPAT/, "freebsd", flags[i])
103					$6 = flags[i] "_" $6
104				}
105			}
106		}
107		$6 = "__sysfake_" $6
108		r = ""
109		if ($5 != "void")
110			r = "0"
111		def = ""
112		impl = ""
113		for ( i = 5; i <= NF; i++) {
114			if ($i == ";")
115				break;
116			if ($i == "," || $i == ")")
117				impl = impl " __unused"
118			impl = impl " " $i
119			def = def " " $i
120		}
121		printf "%s;\n", def
122		printf "%s\n{ return %s; }\n", impl, r
123		next
124	}
125	{
126		printf "invalid line: "
127		print
128	}
129'
130