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