#!/bin/sh
#       $NetBSD: mkioctls,v 1.57 2020/09/12 01:36:26 roy Exp $
#
# Copyright (c) 1994
#       The Regents of the University of California.  All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#       This product includes software developed by the University of
#       California, Berkeley and its contributors.
# 4. Neither the name of the University nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
#       @(#)mkioctls    8.2 (Berkeley) 4/28/95
#

: ${AWK:=awk}
: ${CC:=cc}
: ${SED:=sed}

echo "/* Automatically generated file, do not edit! */"
#echo "#define const"   # XXX: timepps lossage
echo "#define COMPAT_43"
echo "#define COMPAT_FREEBSD_NET80211"
echo "struct lwp;"
echo "struct crap { int _xxx; };" # XXX arm video lossage
echo "typedef struct crap videomemory_t;" # XXX arm video lossage
echo "typedef struct crap RF_AccTotals_t;" # XXX Raidframe lossage
echo "struct rf_test_acc { int _xxx; };" # XXX Raidframe lossage
echo "struct map_info { int _xxx[22]; };" # XXX Xorg lossage
echo "#include <stdio.h>"
echo "#include <sys/types.h>"
echo "#include <sys/param.h>"
echo "#include <sys/mutex.h>"
echo "#ifndef NBPG"
echo "#define NBPG 4096 /* workaround / dummy for sparc */"
echo "#endif /* NBPG */"
echo "#include <sys/disk.h>"
echo "#include <sys/dkbad.h>"
echo "#include <sys/mount.h>"
echo "#include <sys/termios.h>"
echo "#include <sys/disklabel.h>"
echo "#include <sys/exec_elf.h>"
echo "#include <net/if.h>"
echo "#include <net/if_ether.h>"
echo "#include <net/route.h>"
echo "#include <net/ppp_defs.h>"
echo "#include <netinet/in.h>"
echo "#include <netinet/in_var.h>"
echo "#include <netinet/in_systm.h>"
echo "#include <netinet/ip.h>"
echo "#include <netinet/ip_mroute.h>"
echo "#include <netinet/ip_compat.h>"
echo "#include <net80211/ieee80211.h>"
echo "#include <net80211/ieee80211_radiotap.h>"
echo "#include <netinet6/in6_var.h>"
echo "#include <netinet6/nd6.h>"
echo "#include <dev/ic/hd44780reg.h>"
echo "#include <dev/pci/mlyreg.h>"
echo "#include <prop/proplib.h>"
echo "#include <nv.h>"
echo

# kernel headers <sys/*.h> and <net/*.h> should not include stdbool.h
# but they do; then xf86Opt.h needs a bool struct field.

needundef=true
for i
do
       case $i in
       *X11*)
               if $needundef; then
                       echo "#undef bool"
                       needundef=false;
               fi;;
       esac
       echo "#include <$i>"
done | ${SED} -e "s,${DESTDIR}/usr/include/,,g" \
       -e "s,${DESTDIR}/usr/X11R7/include/,,g" \
       -e "s,${NETBSDSRCDIR}/external/cddl/osnet/dist/uts/common/,,g"

if ! $needundef; then
   echo "#define bool _Bool"
fi

${CC} -E -nostdinc -isystem ${DESTDIR}/usr/include ${CPPFLAGS} -dM "${@}" | \
   ${AWK} '
BEGIN {
       keep["SLIOCGUNIT"] = 1; # Same as PPPIOCGASYNCMAP
       keep["TUNSLMODE"] = 1;  # Same as PPPIOCGASYNCMAP
       keep["SIOCRMNAT"] = 1;  # Same as SIOCRMAFR
       keep["SIOCADNAT"] = 1;  # Same as SIOCADAFR
       keep["SIOCDELST"] = 1;  # Same as SIOCRMNAT
       keep["SIOCPROXY"] = 1; # Same as SIOCLOOKUPSTAT
       keep["CHIOGPICKER"] = 1; # Same as CDIOREADTOCHEADER
       keep["KBDSTPMAT"] = 1;  # Same as LMREADY, pcvt is gone so ok.
       keep["SIOCSLOWAT"] = 1; # Same as SATIOSBUFSIZE
       keep["UTOPPYIOTURBO"] = 1; # Same as OTIOCSETD
       keep["TIOCHPCL"] = 1; # Same as UTOPPYIOCANCEL
       keep["TIOCCBRK"] = 1; # Same as CONSOLE_X_MODE_OFF
       keep["TIOCSDTR"] = 1; # Same as CONSOLE_X_MODE_ON
       keep["APM_IOC_SUSPEND"] = 1; # Same as AGPIOC_RELEASE
       keep["APM_IOC_STANDBY"] = 1; # Same as AGPIOC_ACQUIRE
       keep["BTDEV_GETINFO"] = 1; # Same as BTSCO_GETINFO
       keep["BTHSET_GETINFO"] = 1; # Same as BTSCO_GETINFO
       keep["JOY_GET_X_OFFSET"] = 1; # Same as GDT_IOCTL_CTRCNT
       keep["JOY_GET_X_OFFSET"] = 1; # Same as GDT_IOCTL_CTRCNT
       keep["SCIOC_USE_ADF"] = 1; # Same as STICIO_STARTQ
       keep["SCBUSIOLLSCAN"] = 1; # Same as USB_DISCOVER
       keep["OPIOCGETNEXT"] = 1; # Same as OFIOCGETNEXT
       keep["OPIOCGET"] = 1; # Same as OFIOCGET
       keep["OPIOCGETCHILD"] = 1; # Same as OFIOCGETCHILD
       keep["OPIOCFINDDEVICE"] = 1; # Same as OFIOCFINDDEVICE
       keep["OPIOCGETOPTNODE"] = 1; # Same as OFIOCGETOPTNODE
       keep["OPIOCNEXTPROP"] = 1; # Same as OFIOCNEXTPROP
       keep["OPIOCSET"] = 1; # Same as OFIOCSET
       keep["MBPPIOCSPARAM"] = 1; # Same as PARIOCSPARAM
       keep["MBPPIOCGPARAM"] = 1; # Same as PARIOCGPARAM
       keep["CLOCKCTL_ADJTIME"] = 1; # Same as TCTRL_MODEM_PWR
       keep["OFIOCNEXTPROP"] = 1; # Same as PNVIOCGETNUMGE
       keep["OFIOCGET"] = 1; # Same as PNVIOCGET
       keep["SIOCPHASE2"] = 1; # Same as GRESADDRS
       keep["I4B_CTL_CLR_CHIPSTAT"] = 1; # Same as CLOCKCTL_NTP_ADJTIME
       keep["STICIO_STARTQ"] = 1; # Same as SPKRTONE
       keep["STICIO_RESET860"] = 1; # Same as SCIOCRESTART
       keep["STICIO_START860"] = 1; # Same as SPKRTUNE
       keep["I4B_CTL_SET_DEBUG"] = 1; # Same as CLOCKCTL_SETTIMEOFDAY

       keep["SIOCGETSGCNT_IN6"] = 1; # Type sioc_sg_req6 not exported
       keep["SIOCGPRLST_IN6"] = 1; Type in6_oprlist not exported
       keep["SIOCGETMIFCNT_IN6"] = 1; Type sioc_mif_req6 not exported

       i = 1;
       altqt[i++] = "RED";
       altqt[i++] = "BLUE";
       altqt[i++] = "RIO";
       altqt[i++] = "FIFOQ";
       altqt[i++] = "HFSC";
       altqt[i++] = "PRIQ";
       altqt[i++] = "WFQ";
       altqt[i++] = "CBQ";
       altqt[i++] = "CNDR";

       i = 1;
       altqf[i++] = "IF_ATTACH";
       altqf[i++] = "DISABLE";
       altqf[i++] = "ENABLE";
       altqf[i++] = "CONFIG";
       altqf[i++] = "GETSTATS";
       altqf[i++] = "IF_DETACH";
       altqf[i++] = "ADD_CLASS";
       altqf[i++] = "DEL_CLASS";
       altqf[i++] = "MOD_CLASS";
       altqf[i++] = "ADD_FILTER";
       altqf[i++] = "DEL_FILTER";
       altqf[i++] = "CLEAR_HIERARCHY";

       for (t in altqt) {
               for (f in altqf) {
                       keep[altqt[t] "_" altqf[f]] = 1;
               }
       }

       print ""
       print "#include \"ktrace.h\""
       print ""
       print "const char *"
       print "ioctlname(u_long val)"
       print "{"
       print ""
       print "\tswitch (val) {\n"
}

/^#[    ]*define[       ]*[A-Z0-9_]*[   ]*[D]*[R]*[M]*_IO[W]*[R]*[      ]*\(/ {

       # find where the name starts
       for (i = 1; i <= NF; i++)
               if ($i ~ /define/)
                       break;
       ++i;
       name = $i;
       printf("/* %s [%s] */\n", $0, name);
       #
       if (name !~ /^[A-Z0-9a-z_]*$/)
               next;
       if (keep[name] == 1)
               next;
       keep[name] = 1;
       printf("\t#ifdef %s\n", name);
       printf("\tcase %s:\n\t\treturn \"%s\";\n", name, name);
       printf("\t#endif /* %s */\n", $i);
       name = name;
       names[name] = name;
       exprs[name] = "";
       files[name] = filename;
       do {
               ++i;
               exprs[name] = exprs[name] " " $i;
       } while ($i !~ /.*)/);
       exprs[name] = substr(exprs[name], 2);
}
END {
       print "\tdefault:\n\t\treturn NULL;\n"
       print "\t}\n"
       print "}"
       print "const struct ioctlinfo ioctlinfo[] = {\n";
       for (name in names) {
               printf("\t{ .name = \"%s\", .expr = \"%s\", .value = %s },\n",
                   names[name], exprs[name], names[name]);
       }
       print "\t{ .name = NULL, .expr = NULL, .value = 0 },\n};";
}
'