xref: /minix3/lib/libc/sys/makelintstub (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1#!/bin/sh -
2# $NetBSD: makelintstub,v 1.25 2012/02/12 01:46:05 martin 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] [-s syscall.h] object ...
41	-n		Create SYSCALL_NOERROR.
42	-p		Create PSEUDO_NOERROR.
43			(also removes leading "_" on syscall name).
44	-o filename	Output to filename.
45	-s syscall.h	Header to #include instead of <sys/syscall.h>.
46
47	The CPP environment variable must be set
48	to the path to the C preprocessor.
49_EOF
50	exit 1
51}
52
53header()
54{
55
56	cat <<- __EOF__
57	/*
58	 * THIS IS AN AUTOMATICALLY GENERATED FILE.  DO NOT EDIT.
59	 */
60
61	#include <sys/param.h>
62	#include <sys/aio.h>
63	#include <sys/time.h>
64	#include <sys/mount.h>
65	#include <sys/stat.h>
66	#include <ufs/ufs/dinode.h>
67	#include <ufs/lfs/lfs.h>
68	#include <sys/resource.h>
69	#include <sys/poll.h>
70	#include <sys/uio.h>
71	#include <sys/ipc.h>
72	#include <sys/lwpctl.h>
73	#include <sys/mqueue.h>
74	#include <sys/msg.h>
75	#include <sys/sem.h>
76	#include <sys/shm.h>
77	#include <sys/spawn.h>
78	#include <sys/sched.h>
79	#include <sys/timex.h>
80	#include <sys/socket.h>
81	#include <sys/event.h>
82	#include <sys/uuid.h>
83	#include <sys/quotactl.h>
84	#ifdef __STDC__
85	#include <stdarg.h>
86	#else
87	#include <varargs.h>
88	#endif
89
90	__EOF__
91}
92
93syscall_stub()
94{
95
96	syscalldump="$1"
97	syscallname="$2"
98	funcname="$3"
99
100    	arglist="$(
101    	sed -e 'ta
102		:a
103		s,^/\* syscall: \"'"$syscallname"'\" ,,
104	        tb
105		d
106		:b
107		s, \*/$,,' $syscalldump
108	)"
109
110	eval set -f -- "$arglist"
111
112	if [ $# -lt 3 ]; then
113		echo syscall $syscallname not found! 1>&2
114		exit 1
115	fi
116
117	shift			# kill "ret:"
118	returntype=$1; shift
119	shift			# kill "args:"
120
121	cat <<- __EOF__
122	/*ARGSUSED*/
123	$returntype
124	__EOF__
125
126	# do ANSI C function header
127	echo	"#ifdef __STDC__"
128
129	echo "$funcname("
130	first=true; i=1
131	for arg; do
132		if $first; then
133			first=false
134		else
135			echo ", "
136		fi
137		case "$arg" in
138		"...") echo "...";;
139		*) echo "$arg arg$i"; i=$(($i + 1));;
140		esac
141	done
142	if $first; then
143		echo "void"
144	fi
145	echo	")"
146
147	# do K&R C function header
148	echo	"#else"
149
150	echo "$funcname("
151	first=true; i=1
152	for arg; do
153		if $first; then
154			first=false
155		else
156			echo ", "
157		fi
158		case "$arg" in
159		"...") echo "va_alist";;
160		*) echo "arg$i"; i=$(($i + 1));;
161		esac
162	done
163	echo	")"
164	i=1
165	for arg; do
166		case "$arg" in
167		"...") echo "	va_dcl";;
168		*) echo "	$arg arg$i;"; i=$(($i + 1));;
169		esac
170	done
171
172	# do function body
173	echo	"#endif"
174
175	echo	"{"
176	if [ "$returntype" != "void" ]; then
177		echo "        return (($returntype)0);"
178	fi
179	echo	"}"
180}
181
182trailer()
183{
184
185	cat <<- __EOF__
186	/* END */
187	__EOF__
188}
189
190
191pflag=false
192nflag=false
193oarg=""
194syscallhdr=/usr/include/sys/syscall.h
195syscalldump=/tmp/makelintstub.$$
196PROG="$(basename "$0")"
197
198if [ -z "${CPP}" ]; then
199	usage
200fi
201
202while getopts no:ps: i
203do
204	case "$i" in
205	n)	nflag=true;;
206	o)	oarg=$OPTARG;;
207	p)	pflag=true;;
208	s)	syscallhdr=$OPTARG;;
209	*)	usage;;
210	esac
211done
212
213shift $(expr $OPTIND - 1)
214
215if $pflag && $nflag
216then
217	echo "$PROG: -n flag and -p flag may not be used together" 1>&2
218	usage
219fi
220
221if [ -n "$oarg" ]; then
222	exec > $oarg
223fi
224
225trap "rm -f $syscalldump" 0 1 2 3 15
226
227header
228
229echo "#include \"$syscallhdr\"" | ${CPP} -D_LIBC -C > $syscalldump
230
231for syscall; do
232	fnname=${syscall%.S}
233	if $pflag; then
234		scname=${fnname#_}
235	else
236		scname=$fnname
237	fi
238	syscall_stub $syscalldump $scname $fnname
239	echo ""
240done
241
242trailer
243
244exit 0
245