xref: /netbsd-src/lib/libc/sys/makelintstub (revision 7fa608457b817eca6e0977b37f758ae064f3c99c)
1#!/bin/sh -
2# $NetBSD: makelintstub,v 1.19 2007/10/09 18:18:33 rmind Exp $
3#
4# Copyright (c) 1996, 1997 Christopher G. Demetriou
5# 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 for the
18#          NetBSD Project.  See http://www.NetBSD.org/ for
19#          information about NetBSD.
20# 4. The name of the author may not be used to endorse or promote products
21#    derived from this software without specific prior written permission.
22#
23# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33#
34# <<Id: LICENSE,v 1.2 2000/06/14 15:57:33 cgd Exp>>
35
36usage()
37{
38
39cat << _EOF 1>&2
40Usage: $PROG [-n|-p] [-o filename] object ...
41	The CPP environment variable must be set
42	to the path to the C preprocessor.
43_EOF
44	exit 1
45}
46
47header()
48{
49
50	cat <<- __EOF__
51	/*
52	 * THIS IS AN AUTOMATICALLY GENERATED FILE.  DO NOT EDIT.
53	 */
54
55	#include <sys/param.h>
56	#include <sys/aio.h>
57	#include <sys/time.h>
58	#include <sys/mount.h>
59	#include <sys/stat.h>
60	#include <ufs/ufs/dinode.h>
61	#include <ufs/lfs/lfs.h>
62	#include <sys/resource.h>
63	#include <sys/poll.h>
64	#include <sys/uio.h>
65	#include <sys/ipc.h>
66	#include <sys/mqueue.h>
67	#include <sys/msg.h>
68	#include <sys/sem.h>
69	#include <sys/shm.h>
70	#include <sys/timex.h>
71	#include <sys/socket.h>
72	#include <sys/event.h>
73	#include <sys/uuid.h>
74	#ifdef __STDC__
75	#include <stdarg.h>
76	#else
77	#include <varargs.h>
78	#endif
79
80	__EOF__
81}
82
83syscall_stub()
84{
85
86	syscalldump="$1"
87	syscallname="$2"
88	funcname="$3"
89
90    	arglist="$(
91    	sed -e 'ta
92		:a
93		s,^/\* syscall: \"'"$syscallname"'\" ,,
94	        tb
95		d
96		:b
97		s, \*/$,,' $syscalldump
98	)"
99
100	eval set -f -- "$arglist"
101
102	if [ $# -lt 3 ]; then
103		echo syscall $syscallname not found! 1>&2
104		exit 1
105	fi
106
107	shift			# kill "ret:"
108	returntype=$1; shift
109	shift			# kill "args:"
110
111	cat <<- __EOF__
112	/*ARGSUSED*/
113	$returntype
114	__EOF__
115
116	# do ANSI C function header
117	echo	"#ifdef __STDC__"
118
119	echo "$funcname("
120	first=true; i=1
121	for arg; do
122		if $first; then
123			first=false
124		else
125			echo ", "
126		fi
127		case "$arg" in
128		"...") echo "...";;
129		*) echo "$arg arg$i"; i=$(($i + 1));;
130		esac
131	done
132	if $first; then
133		echo "void"
134	fi
135	echo	")"
136
137	# do K&R C function header
138	echo	"#else"
139
140	echo "$funcname("
141	first=true; i=1
142	for arg; do
143		if $first; then
144			first=false
145		else
146			echo ", "
147		fi
148		case "$arg" in
149		"...") echo "va_alist";;
150		*) echo "arg$i"; i=$(($i + 1));;
151		esac
152	done
153	echo	")"
154	i=1
155	for arg; do
156		case "$arg" in
157		"...") echo "	va_dcl";;
158		*) echo "	$arg arg$i;"; i=$(($i + 1));;
159		esac
160	done
161
162	# do function body
163	echo	"#endif"
164
165	echo	"{"
166	if [ "$returntype" != "void" ]; then
167		echo "        return (($returntype)0);"
168	fi
169	echo	"}"
170}
171
172trailer()
173{
174
175	cat <<- __EOF__
176	/* END */
177	__EOF__
178}
179
180
181pflag=false
182nflag=false
183oarg=""
184syscallhdr=/usr/include/sys/syscall.h
185syscalldump=/tmp/makelintstub.$$
186PROG="$(basename "$0")"
187
188if [ -z "${CPP}" ]; then
189	usage
190fi
191
192while getopts no:ps: i
193do
194	case "$i" in
195	n)	nflag=true;;
196	o)	oarg=$OPTARG;;
197	p)	pflag=true;;
198	s)	syscallhdr=$OPTARG;;
199	*)	usage;;
200	esac
201done
202
203shift $(expr $OPTIND - 1)
204
205if $pflag && $nflag
206then
207	echo "$PROG: -n flag and -p flag may not be used together" 1>&2
208	usage
209fi
210
211if [ -n "$oarg" ]; then
212	exec > $oarg
213fi
214
215trap "rm -f $syscalldump" 0 1 2 3 15
216
217header
218
219echo "#include \"$syscallhdr\"" | ${CPP} -D_LIBC -C > $syscalldump
220
221for syscall; do
222	fnname=${syscall%.S}
223	if $pflag; then
224		scname=${fnname#_}
225	else
226		scname=$fnname
227	fi
228	syscall_stub $syscalldump $scname $fnname
229	echo ""
230done
231
232trailer
233
234exit 0
235