ptime

Reformat timestamps
git clone git://git.sgregoratto.me/ptime
Log | Files | Refs

configure (38502B)


      1 #! /bin/sh
      2 #
      3 # Copyright (c) 2014, 2015, 2016 Ingo Schwarze <schwarze@openbsd.org>
      4 # Copyright (c) 2017, 2018 Kristaps Dzonsons <kristaps@bsd.lv>
      5 #
      6 # Permission to use, copy, modify, and distribute this software for any
      7 # purpose with or without fee is hereby granted, provided that the above
      8 # copyright notice and this permission notice appear in all copies.
      9 #
     10 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17 
     18 OCONFIGURE_VERSION="0.1.8"
     19 
     20 #
     21 # This script outputs two files: config.h and Makefile.configure.
     22 # It tries to read from configure.local, which contains predefined
     23 # values we won't autoconfigure.
     24 #
     25 # If you want to use configure with your project, have your GNUmakefile
     26 # or BSDmakefile---whichever---try to import/include Makefile.configure
     27 # at the beginning of the file.
     28 #
     29 # Like so (note no quotes, no period, etc.):
     30 #
     31 #   include Makefile.configure
     32 #
     33 # If it exists, configure was run; otherwise, it wasn't.
     34 #
     35 # You'll probably want to change parts of this file.  I've noted the
     36 # parts that you'll probably change in the section documentation.
     37 #
     38 # See https://github.com/kristapsdz/oconfigure for more.
     39 
     40 set -e
     41 
     42 #----------------------------------------------------------------------
     43 # Prepare for running: move aside previous configure runs.
     44 # Output file descriptor usage:
     45 #  1 (stdout): config.h or Makefile.configure
     46 #  2 (stderr): original stderr, usually to the console
     47 #  3: config.log
     48 # You DO NOT want to change this.
     49 #----------------------------------------------------------------------
     50 
     51 [ -w config.log ] && mv config.log config.log.old
     52 [ -w config.h   ] && mv config.h config.h.old
     53 
     54 exec 3> config.log
     55 echo "config.log: writing..."
     56 
     57 #----------------------------------------------------------------------
     58 # Initialize all variables here such that nothing can leak in from the
     59 # environment except for CC and CFLAGS, which we might have passed in.
     60 #----------------------------------------------------------------------
     61 
     62 CC=`printf "all:\\n\\t@echo \\\$(CC)\\n" | make -sf -`
     63 CFLAGS=`printf "all:\\n\\t@echo \\\$(CFLAGS)\\n" | make -sf -`
     64 CFLAGS="${CFLAGS} -g -W -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes"
     65 CFLAGS="${CFLAGS} -Wwrite-strings -Wno-unused-parameter"
     66 LDADD=
     67 CPPFLAGS=
     68 LDFLAGS=
     69 DESTDIR=
     70 PREFIX="/usr/local"
     71 BINDIR=
     72 SBINDIR=
     73 INCLUDEDIR=
     74 LIBDIR=
     75 MANDIR=
     76 SHAREDIR=
     77 INSTALL="install"
     78 INSTALL_PROGRAM=
     79 INSTALL_LIB=
     80 INSTALL_MAN=
     81 INSTALL_DATA=
     82 
     83 #----------------------------------------------------------------------
     84 # Allow certain variables to be overriden on the command line.
     85 #----------------------------------------------------------------------
     86 
     87 for keyvals in "$@"
     88 do
     89 	key=`echo $keyvals | cut -s -d '=' -f 1`
     90 	if [ -z "$key" ]
     91 	then
     92 		echo "$0: invalid key-value: $keyvals" 1>&2
     93 		exit 1
     94 	fi
     95 	val=`echo $keyvals | cut -d '=' -f 2-`
     96 	case "$key" in
     97 	LDADD)
     98 		LDADD="$val" ;;
     99 	LDFLAGS)
    100 		LDFLAGS="$val" ;;
    101 	CPPFLAGS)
    102 		CPPFLAGS="$val" ;;
    103 	DESTDIR)
    104 		DESTDIR="$val" ;;
    105 	PREFIX)
    106 		PREFIX="$val" ;;
    107 	MANDIR)
    108 		MANDIR="$val" ;;
    109 	LIBDIR)
    110 		LIBDIR="$val" ;;
    111 	BINDIR)
    112 		BINDIR="$val" ;;
    113 	SHAREDIR)
    114 		SHAREDIR="$val" ;;
    115 	SBINDIR)
    116 		SBINDIR="$val" ;;
    117 	INCLUDEDIR)
    118 		INCLUDEDIR="$val" ;;
    119 	*)
    120 		echo "$0: invalid key: $key" 1>&2
    121 		exit 1
    122 	esac
    123 done
    124 
    125 
    126 #----------------------------------------------------------------------
    127 # These are the values that will be pushed into config.h after we test
    128 # for whether they're supported or not.
    129 # Each of these must have a runtest(), below.
    130 # Please sort by alpha, for clarity.
    131 # You WANT to change this.
    132 #----------------------------------------------------------------------
    133 
    134 HAVE_ARC4RANDOM=
    135 HAVE_B64_NTOP=
    136 HAVE_CAPSICUM=
    137 HAVE_ERR=
    138 HAVE_EXPLICIT_BZERO=
    139 HAVE_GETPROGNAME=
    140 HAVE_INFTIM=
    141 HAVE_MD5=
    142 HAVE_MEMMEM=
    143 HAVE_MEMRCHR=
    144 HAVE_MEMSET_S=
    145 HAVE_PATH_MAX=
    146 HAVE_PLEDGE=
    147 HAVE_PROGRAM_INVOCATION_SHORT_NAME=
    148 HAVE_READPASSPHRASE=
    149 HAVE_REALLOCARRAY=
    150 HAVE_RECALLOCARRAY=
    151 HAVE_SANDBOX_INIT=
    152 HAVE_SECCOMP_FILTER=
    153 HAVE_SOCK_NONBLOCK=
    154 HAVE_STRLCAT=
    155 HAVE_STRLCPY=
    156 HAVE_STRNDUP=
    157 HAVE_STRNLEN=
    158 HAVE_STRTONUM=
    159 HAVE_SYSTRACE=
    160 HAVE_UNVEIL=
    161 HAVE_ZLIB=
    162 HAVE___PROGNAME=
    163 
    164 #----------------------------------------------------------------------
    165 # Allow configure.local to override all variables, default settings,
    166 # command-line arguments, and tested features, above.
    167 # You PROBABLY DO NOT want to change this.
    168 #----------------------------------------------------------------------
    169 
    170 if [ -r ./configure.local ]; then
    171 	echo "configure.local: reading..." 1>&2
    172 	echo "configure.local: reading..." 1>&3
    173 	cat ./configure.local 1>&3
    174 	. ./configure.local
    175 else
    176 	echo "configure.local: no (fully automatic configuration)" 1>&2
    177 	echo "configure.local: no (fully automatic configuration)" 1>&3
    178 fi
    179 
    180 echo 1>&3
    181 
    182 #----------------------------------------------------------------------
    183 # Infrastructure for running tests.
    184 # These consists of a series of functions that will attempt to run the
    185 # given test file and record its exit into a HAVE_xxx variable.
    186 # You DO NOT want to change this.
    187 #----------------------------------------------------------------------
    188 
    189 COMP="${CC} ${CFLAGS} ${CPPFLAGS} -Wno-unused -Werror"
    190 
    191 # Check whether this HAVE_ setting is manually overridden.
    192 # If yes, use the override, if no, do not decide anything yet.
    193 # Arguments: lower-case test name, manual value
    194 
    195 ismanual() {
    196 	[ -z "${3}" ] && return 1
    197 	echo "${1}: manual (HAVE_${2}=${3})" 1>&2
    198 	echo "${1}: manual (HAVE_${2}=${3})" 1>&3
    199 	echo 1>&3
    200 	return 0
    201 }
    202 
    203 # Run a single autoconfiguration test.
    204 # In case of success, enable the feature.
    205 # In case of failure, do not decide anything yet.
    206 # Arguments: lower-case test name, upper-case test name, additional
    207 # CFLAGS, additional LIBS.
    208 
    209 singletest() {
    210 	extralib=""
    211 	cat 1>&3 << __HEREDOC__
    212 ${1}: testing...
    213 ${COMP} -DTEST_${2} ${3} -o test-${1} tests.c ${4}
    214 __HEREDOC__
    215 	if ${COMP} -DTEST_${2} ${3} -o "test-${1}" tests.c ${4} 1>&3 2>&3; then
    216 		echo "${1}: ${CC} succeeded" 1>&3
    217 	else 
    218 		if [ -n "${5}" ] ; then
    219 			echo "${1}: ${CC} failed with $? (retrying)" 1>&3
    220 			cat 1>&3 << __HEREDOC__
    221 ${1}: testing...
    222 ${COMP} -DTEST_${2} ${3} -o test-${1} tests.c ${5}
    223 __HEREDOC__
    224 			if ${COMP} -DTEST_${2} ${3} -o "test-${1}" tests.c ${5} 1>&3 2>&3; then
    225 				echo "${1}: ${CC} succeeded" 1>&3
    226 				extralib="(with ${5})"
    227 			else 
    228 				echo "${1}: ${CC} failed with $?" 1>&3
    229 				echo 1>&3
    230 				return 1
    231 			fi
    232 		else
    233 			echo "${1}: ${CC} failed with $?" 1>&3
    234 			echo 1>&3
    235 			return 1
    236 		fi
    237 	fi
    238 
    239 	echo "${1}: yes ${extralib}" 1>&2
    240 	echo "${1}: yes ${extralib}" 1>&3
    241 	echo 1>&3
    242 	eval HAVE_${2}=1
    243 	rm "test-${1}"
    244 	return 0
    245 
    246 	# Don't actually run the test: none of our tests check for
    247 	# run-time behaviour.
    248 	# if ./test-${1} 1>&3 2>&3; then
    249 	# 	echo "${1}: yes" 1>&2
    250 	# 	echo "${1}: yes" 1>&3
    251 	# 	echo 1>&3
    252 	# 	eval HAVE_${2}=1
    253 	# 	rm "test-${1}"
    254 	# 	return 0
    255 	# else
    256 	# 	echo "${1}: execution failed with $?" 1>&3
    257 	# 	echo 1>&3
    258 	# 	rm "test-${1}"
    259 	# 	return 1
    260 	# fi
    261 }
    262 
    263 # Run a complete autoconfiguration test, including the check for
    264 # a manual override and disabling the feature on failure.
    265 # Arguments: lower case name, upper case name, additional CFLAGS, 
    266 # additional LDADD, alternative LDADD.
    267 
    268 runtest() {
    269 	eval _manual=\${HAVE_${2}}
    270 	ismanual "${1}" "${2}" "${_manual}" && return 0
    271 	singletest "${1}" "${2}" "${3}" "${4}" "${5}" && return 0
    272 	echo "${1}: no" 1>&2
    273 	eval HAVE_${2}=0
    274 	return 1
    275 }
    276 
    277 #----------------------------------------------------------------------
    278 # Begin running the tests themselves.
    279 # All of your tests must be defined here.
    280 # Please sort as the HAVE_xxxx values were defined.
    281 # You WANT to change this.
    282 # It consists of the following columns:
    283 #    runtest
    284 #    (1) test file
    285 #    (2) macro to set
    286 #    (3) argument to cc *before* -o
    287 #    (4) argument to cc *after* 
    288 #    (5) alternative argument to cc *after* 
    289 #----------------------------------------------------------------------
    290 
    291 runtest arc4random	ARC4RANDOM			  || true
    292 runtest b64_ntop	B64_NTOP "" "" "-lresolv"	  || true
    293 runtest capsicum	CAPSICUM			  || true
    294 runtest err		ERR				  || true
    295 runtest explicit_bzero	EXPLICIT_BZERO			  || true
    296 runtest getprogname	GETPROGNAME			  || true
    297 runtest INFTIM		INFTIM				  || true
    298 runtest md5		MD5 "" "" "-lmd"		  || true
    299 runtest memmem		MEMMEM			  	  || true
    300 runtest memrchr		MEMRCHR			  	  || true
    301 runtest memset_s	MEMSET_S			  || true
    302 runtest PATH_MAX	PATH_MAX			  || true
    303 runtest pledge		PLEDGE				  || true
    304 runtest program_invocation_short_name	PROGRAM_INVOCATION_SHORT_NAME || true
    305 runtest readpassphrase	READPASSPHRASE			  || true
    306 runtest reallocarray	REALLOCARRAY			  || true
    307 runtest recallocarray	RECALLOCARRAY			  || true
    308 runtest sandbox_init	SANDBOX_INIT	"-Wno-deprecated" || true
    309 runtest seccomp-filter	SECCOMP_FILTER			  || true
    310 runtest SOCK_NONBLOCK	SOCK_NONBLOCK			  || true
    311 runtest strlcat		STRLCAT				  || true
    312 runtest strlcpy		STRLCPY				  || true
    313 runtest strndup		STRNDUP				  || true
    314 runtest strnlen		STRNLEN				  || true
    315 runtest strtonum	STRTONUM			  || true
    316 runtest sys_queue	SYS_QUEUE			  || true
    317 runtest systrace	SYSTRACE			  || true
    318 runtest unveil		UNVEIL				  || true
    319 runtest zlib		ZLIB		"" "-lz"	  || true
    320 runtest __progname	__PROGNAME			  || true
    321 
    322 #----------------------------------------------------------------------
    323 # Output writing: generate the config.h file.
    324 # This file contains all of the HAVE_xxxx variables necessary for
    325 # compiling your source.
    326 # You must include "config.h" BEFORE any other variables.
    327 # You WANT to change this.
    328 #----------------------------------------------------------------------
    329 
    330 exec > config.h
    331 
    332 # Start with prologue.
    333 
    334 cat << __HEREDOC__
    335 #ifdef __cplusplus
    336 #error "Do not use C++: this is a C application."
    337 #endif
    338 #if !defined(__GNUC__) || (__GNUC__ < 4)
    339 #define __attribute__(x)
    340 #endif
    341 #if defined(__linux__) || defined(__MINT__)
    342 #define _GNU_SOURCE	/* See test-*.c what needs this. */
    343 #endif
    344 #if !defined(__BEGIN_DECLS)
    345 # define __BEGIN_DECLS
    346 #endif
    347 #if !defined(__END_DECLS)
    348 # define __END_DECLS
    349 #endif
    350 __HEREDOC__
    351 
    352 # For the function declaration variables...
    353 
    354 [ ${HAVE_MD5} -eq 0 -o \
    355   ${HAVE_REALLOCARRAY} -eq 0 -o \
    356   ${HAVE_RECALLOCARRAY} -eq 0 -o \
    357   ${HAVE_STRLCAT} -eq 0 -o \
    358   ${HAVE_STRLCPY} -eq 0 -o \
    359   ${HAVE_STRNDUP} -eq 0 -o \
    360   ${HAVE_STRNLEN} -eq 0 ] \
    361 	&& echo "#include <sys/types.h>"
    362 
    363 [ ${HAVE_ERR} -eq 0 ] \
    364 	&& echo "#include <stdarg.h>"
    365 
    366 # Now we handle our HAVE_xxxx values.
    367 # Most will just be defined as 0 or 1.
    368 
    369 [ ${HAVE_PATH_MAX} -eq 0 ] \
    370 	&& echo "#define PATH_MAX 4096"
    371 
    372 [ ${HAVE_INFTIM} -eq 0 ] \
    373 	&& echo "#define INFTIM (-1)"
    374 
    375 cat << __HEREDOC__
    376 #define HAVE_ARC4RANDOM ${HAVE_ARC4RANDOM}
    377 #define HAVE_B64_NTOP ${HAVE_B64_NTOP}
    378 #define HAVE_CAPSICUM ${HAVE_CAPSICUM}
    379 #define HAVE_ERR ${HAVE_ERR}
    380 #define HAVE_EXPLICIT_BZERO ${HAVE_EXPLICIT_BZERO}
    381 #define HAVE_GETPROGNAME ${HAVE_GETPROGNAME}
    382 #define HAVE_INFTIM ${HAVE_INFTIM}
    383 #define HAVE_MD5 ${HAVE_MD5}
    384 #define HAVE_MEMMEM ${HAVE_MEMMEM}
    385 #define HAVE_MEMRCHR ${HAVE_MEMRCHR}
    386 #define HAVE_MEMSET_S ${HAVE_MEMSET_S}
    387 #define HAVE_PATH_MAX ${HAVE_PATH_MAX}
    388 #define HAVE_PLEDGE ${HAVE_PLEDGE}
    389 #define HAVE_PROGRAM_INVOCATION_SHORT_NAME ${HAVE_PROGRAM_INVOCATION_SHORT_NAME}
    390 #define HAVE_READPASSPHRASE ${HAVE_READPASSPHRASE}
    391 #define HAVE_REALLOCARRAY ${HAVE_REALLOCARRAY}
    392 #define HAVE_RECALLOCARRAY ${HAVE_RECALLOCARRAY}
    393 #define HAVE_SANDBOX_INIT ${HAVE_SANDBOX_INIT}
    394 #define HAVE_SECCOMP_FILTER ${HAVE_SECCOMP_FILTER}
    395 #define HAVE_SOCK_NONBLOCK ${HAVE_SOCK_NONBLOCK}
    396 #define HAVE_STRLCAT ${HAVE_STRLCAT}
    397 #define HAVE_STRLCPY ${HAVE_STRLCPY}
    398 #define HAVE_STRNDUP ${HAVE_STRNDUP}
    399 #define HAVE_STRNLEN ${HAVE_STRNLEN}
    400 #define HAVE_STRTONUM ${HAVE_STRTONUM}
    401 #define HAVE_SYS_QUEUE ${HAVE_SYS_QUEUE}
    402 #define HAVE_SYSTRACE ${HAVE_SYSTRACE}
    403 #define HAVE_UNVEIL ${HAVE_UNVEIL}
    404 #define HAVE_ZLIB ${HAVE_ZLIB}
    405 #define HAVE___PROGNAME ${HAVE___PROGNAME}
    406 __HEREDOC__
    407 
    408 # Now we do our function declarations for missing functions.
    409 
    410 if [ ${HAVE_ERR} -eq 0 ]; then
    411 	echo "extern void err(int, const char *, ...);"
    412 	echo "extern void errx(int, const char *, ...);"
    413 	echo "extern void warn(const char *, ...);"
    414 	echo "extern void warnx(const char *, ...);"
    415 	echo "extern void vwarn(const char *, va_list);"
    416 	echo "extern void vwarnx(const char *, va_list);"
    417 fi
    418 
    419 if [ ${HAVE_MD5} -eq 0 ]; then
    420 	echo "#define MD5_BLOCK_LENGTH 64"
    421 	echo "#define MD5_DIGEST_LENGTH 16"
    422 	echo "#define MD5_DIGEST_STRING_LENGTH (MD5_DIGEST_LENGTH * 2 + 1)"
    423 	cat <<!!
    424 typedef struct MD5Context {
    425 	u_int32_t state[4];
    426 	u_int64_t count;
    427 	u_int8_t buffer[MD5_BLOCK_LENGTH];
    428 } MD5_CTX;
    429 !!
    430 	echo "extern void MD5Init(MD5_CTX *);"
    431 	echo "extern void MD5Update(MD5_CTX *, const u_int8_t *, size_t);"
    432 	echo "extern void MD5Pad(MD5_CTX *);"
    433 	echo "extern void MD5Transform(u_int32_t [4], const u_int8_t [MD5_BLOCK_LENGTH]);"
    434 	echo "extern char *MD5End(MD5_CTX *, char *);"
    435 	echo "extern void MD5Final(u_int8_t [MD5_DIGEST_LENGTH], MD5_CTX *);";
    436 fi
    437 
    438 if [ ${HAVE_SECCOMP_FILTER} -eq 1 ]; then
    439 	arch=`uname -m 2>/dev/null || echo unknown`
    440 	case "$arch" in
    441 		x86_64)
    442 			echo "#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_X86_64"
    443 			;;
    444 		i*86)
    445 			echo "#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_I386"
    446 			;;
    447 		arm*)
    448 			echo "#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARM"
    449 			;;
    450 	esac
    451 fi
    452 
    453 if [ ${HAVE_B64_NTOP} -eq 0 ]; then
    454 	echo "extern int b64_ntop(unsigned char const *, size_t, char *, size_t);";
    455 	echo "extern int b64_pton(char const *, unsigned char *, size_t);"
    456 fi
    457 
    458 if [ ${HAVE_EXPLICIT_BZERO} -eq 0 ]; then
    459 	echo "extern void explicit_bzero(void *, size_t);"
    460 fi
    461 
    462 if [ ${HAVE_MEMMEM} -eq 0 ]; then
    463 	echo "void *memmem(const void *, size_t, const void *, size_t);"
    464 fi
    465 
    466 if [ ${HAVE_MEMRCHR} -eq 0 ]; then
    467 	echo "void *memrchr(const void *b, int, size_t);"
    468 fi
    469 
    470 if [ ${HAVE_GETPROGNAME} -eq 0 ]; then
    471 	echo "extern const char *getprogname(void);"
    472 fi
    473 
    474 if [ ${HAVE_READPASSPHRASE} -eq 0 ]; then
    475 	echo "#define RPP_ECHO_OFF 0x00"
    476 	echo "#define RPP_ECHO_ON 0x01"
    477 	echo "#define RPP_REQUIRE_TTY 0x02"
    478 	echo "#define RPP_FORCELOWER 0x04"
    479 	echo "#define RPP_FORCEUPPER 0x08"
    480 	echo "#define RPP_SEVENBIT 0x10"
    481 	echo "#define RPP_STDIN 0x20"
    482 	echo "char *readpassphrase(const char *, char *, size_t, int);"
    483 fi
    484 
    485 if [ ${HAVE_REALLOCARRAY} -eq 0 ]; then
    486 	echo "extern void *reallocarray(void *, size_t, size_t);"
    487 fi
    488 
    489 if [ ${HAVE_RECALLOCARRAY} -eq 0 ]; then
    490 	echo "extern void *recallocarray(void *, size_t, size_t, size_t);"
    491 fi
    492 
    493 if [ ${HAVE_STRLCAT} -eq 0 ]; then
    494 	echo "extern size_t strlcat(char *, const char *, size_t);"
    495 fi
    496 
    497 if [ ${HAVE_STRLCPY} -eq 0 ]; then
    498 	echo "extern size_t strlcpy(char *, const char *, size_t);"
    499 fi
    500 
    501 if [ ${HAVE_STRNDUP} -eq 0 ]; then
    502 	echo "extern char *strndup(const char *, size_t);"
    503 fi
    504 
    505 if [ ${HAVE_STRNLEN} -eq 0 ]; then
    506 	echo "extern size_t strnlen(const char *, size_t);"
    507 fi
    508 
    509 if [ ${HAVE_STRTONUM} -eq 0 ]; then
    510 	echo "extern long long strtonum(const char *, long long, long long, const char **);"
    511 fi
    512 
    513 if [ ${HAVE_SYS_QUEUE} -eq 0 ]; then
    514 cat <<!!
    515 
    516 /*	$OpenBSD$	*/
    517 /*	$NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $	*/
    518 
    519 /*
    520  * Copyright (c) 1991, 1993
    521  *	The Regents of the University of California.  All rights reserved.
    522  *
    523  * Redistribution and use in source and binary forms, with or without
    524  * modification, are permitted provided that the following conditions
    525  * are met:
    526  * 1. Redistributions of source code must retain the above copyright
    527  *    notice, this list of conditions and the following disclaimer.
    528  * 2. Redistributions in binary form must reproduce the above copyright
    529  *    notice, this list of conditions and the following disclaimer in the
    530  *    documentation and/or other materials provided with the distribution.
    531  * 3. Neither the name of the University nor the names of its contributors
    532  *    may be used to endorse or promote products derived from this software
    533  *    without specific prior written permission.
    534  *
    535  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    536  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    537  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    538  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    539  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    540  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    541  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    542  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    543  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    544  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    545  * SUCH DAMAGE.
    546  *
    547  *	@(#)queue.h	8.5 (Berkeley) 8/20/94
    548  */
    549 
    550 /* OPENBSD ORIGINAL: sys/sys/queue.h */
    551 
    552 #ifndef	_FAKE_QUEUE_H_
    553 #define	_FAKE_QUEUE_H_
    554 
    555 /*
    556  * Require for OS/X and other platforms that have old/broken/incomplete
    557  * <sys/queue.h>.
    558  */
    559 #undef SLIST_HEAD
    560 #undef SLIST_HEAD_INITIALIZER
    561 #undef SLIST_ENTRY
    562 #undef SLIST_FOREACH_PREVPTR
    563 #undef SLIST_FOREACH_SAFE
    564 #undef SLIST_FIRST
    565 #undef SLIST_END
    566 #undef SLIST_EMPTY
    567 #undef SLIST_NEXT
    568 #undef SLIST_FOREACH
    569 #undef SLIST_INIT
    570 #undef SLIST_INSERT_AFTER
    571 #undef SLIST_INSERT_HEAD
    572 #undef SLIST_REMOVE_HEAD
    573 #undef SLIST_REMOVE_AFTER
    574 #undef SLIST_REMOVE
    575 #undef SLIST_REMOVE_NEXT
    576 #undef LIST_HEAD
    577 #undef LIST_HEAD_INITIALIZER
    578 #undef LIST_ENTRY
    579 #undef LIST_FIRST
    580 #undef LIST_END
    581 #undef LIST_EMPTY
    582 #undef LIST_NEXT
    583 #undef LIST_FOREACH
    584 #undef LIST_FOREACH_SAFE
    585 #undef LIST_INIT
    586 #undef LIST_INSERT_AFTER
    587 #undef LIST_INSERT_BEFORE
    588 #undef LIST_INSERT_HEAD
    589 #undef LIST_REMOVE
    590 #undef LIST_REPLACE
    591 #undef SIMPLEQ_HEAD
    592 #undef SIMPLEQ_HEAD_INITIALIZER
    593 #undef SIMPLEQ_ENTRY
    594 #undef SIMPLEQ_FIRST
    595 #undef SIMPLEQ_END
    596 #undef SIMPLEQ_EMPTY
    597 #undef SIMPLEQ_NEXT
    598 #undef SIMPLEQ_FOREACH
    599 #undef SIMPLEQ_INIT
    600 #undef SIMPLEQ_INSERT_HEAD
    601 #undef SIMPLEQ_INSERT_TAIL
    602 #undef SIMPLEQ_INSERT_AFTER
    603 #undef SIMPLEQ_REMOVE_HEAD
    604 #undef TAILQ_HEAD
    605 #undef TAILQ_HEAD_INITIALIZER
    606 #undef TAILQ_ENTRY
    607 #undef TAILQ_FIRST
    608 #undef TAILQ_END
    609 #undef TAILQ_NEXT
    610 #undef TAILQ_LAST
    611 #undef TAILQ_PREV
    612 #undef TAILQ_EMPTY
    613 #undef TAILQ_FOREACH
    614 #undef TAILQ_FOREACH_REVERSE
    615 #undef TAILQ_FOREACH_SAFE
    616 #undef TAILQ_FOREACH_REVERSE_SAFE
    617 #undef TAILQ_INIT
    618 #undef TAILQ_INSERT_HEAD
    619 #undef TAILQ_INSERT_TAIL
    620 #undef TAILQ_INSERT_AFTER
    621 #undef TAILQ_INSERT_BEFORE
    622 #undef TAILQ_REMOVE
    623 #undef TAILQ_REPLACE
    624 #undef CIRCLEQ_HEAD
    625 #undef CIRCLEQ_HEAD_INITIALIZER
    626 #undef CIRCLEQ_ENTRY
    627 #undef CIRCLEQ_FIRST
    628 #undef CIRCLEQ_LAST
    629 #undef CIRCLEQ_END
    630 #undef CIRCLEQ_NEXT
    631 #undef CIRCLEQ_PREV
    632 #undef CIRCLEQ_EMPTY
    633 #undef CIRCLEQ_FOREACH
    634 #undef CIRCLEQ_FOREACH_REVERSE
    635 #undef CIRCLEQ_INIT
    636 #undef CIRCLEQ_INSERT_AFTER
    637 #undef CIRCLEQ_INSERT_BEFORE
    638 #undef CIRCLEQ_INSERT_HEAD
    639 #undef CIRCLEQ_INSERT_TAIL
    640 #undef CIRCLEQ_REMOVE
    641 #undef CIRCLEQ_REPLACE
    642 
    643 /*
    644  * This file defines five types of data structures: singly-linked lists, 
    645  * lists, simple queues, tail queues, and circular queues.
    646  *
    647  *
    648  * A singly-linked list is headed by a single forward pointer. The elements
    649  * are singly linked for minimum space and pointer manipulation overhead at
    650  * the expense of O(n) removal for arbitrary elements. New elements can be
    651  * added to the list after an existing element or at the head of the list.
    652  * Elements being removed from the head of the list should use the explicit
    653  * macro for this purpose for optimum efficiency. A singly-linked list may
    654  * only be traversed in the forward direction.  Singly-linked lists are ideal
    655  * for applications with large datasets and few or no removals or for
    656  * implementing a LIFO queue.
    657  *
    658  * A list is headed by a single forward pointer (or an array of forward
    659  * pointers for a hash table header). The elements are doubly linked
    660  * so that an arbitrary element can be removed without a need to
    661  * traverse the list. New elements can be added to the list before
    662  * or after an existing element or at the head of the list. A list
    663  * may only be traversed in the forward direction.
    664  *
    665  * A simple queue is headed by a pair of pointers, one the head of the
    666  * list and the other to the tail of the list. The elements are singly
    667  * linked to save space, so elements can only be removed from the
    668  * head of the list. New elements can be added to the list before or after
    669  * an existing element, at the head of the list, or at the end of the
    670  * list. A simple queue may only be traversed in the forward direction.
    671  *
    672  * A tail queue is headed by a pair of pointers, one to the head of the
    673  * list and the other to the tail of the list. The elements are doubly
    674  * linked so that an arbitrary element can be removed without a need to
    675  * traverse the list. New elements can be added to the list before or
    676  * after an existing element, at the head of the list, or at the end of
    677  * the list. A tail queue may be traversed in either direction.
    678  *
    679  * A circle queue is headed by a pair of pointers, one to the head of the
    680  * list and the other to the tail of the list. The elements are doubly
    681  * linked so that an arbitrary element can be removed without a need to
    682  * traverse the list. New elements can be added to the list before or after
    683  * an existing element, at the head of the list, or at the end of the list.
    684  * A circle queue may be traversed in either direction, but has a more
    685  * complex end of list detection.
    686  *
    687  * For details on the use of these macros, see the queue(3) manual page.
    688  */
    689 
    690 #if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC))
    691 #define _Q_INVALIDATE(a) (a) = ((void *)-1)
    692 #else
    693 #define _Q_INVALIDATE(a)
    694 #endif
    695 
    696 /*
    697  * Singly-linked List definitions.
    698  */
    699 #define SLIST_HEAD(name, type)						\\
    700 struct name {								\\
    701 	struct type *slh_first;	/* first element */			\\
    702 }
    703  
    704 #define	SLIST_HEAD_INITIALIZER(head)					\\
    705 	{ NULL }
    706  
    707 #define SLIST_ENTRY(type)						\\
    708 struct {								\\
    709 	struct type *sle_next;	/* next element */			\\
    710 }
    711  
    712 /*
    713  * Singly-linked List access methods.
    714  */
    715 #define	SLIST_FIRST(head)	((head)->slh_first)
    716 #define	SLIST_END(head)		NULL
    717 #define	SLIST_EMPTY(head)	(SLIST_FIRST(head) == SLIST_END(head))
    718 #define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)
    719 
    720 #define	SLIST_FOREACH(var, head, field)					\\
    721 	for((var) = SLIST_FIRST(head);					\\
    722 	    (var) != SLIST_END(head);					\\
    723 	    (var) = SLIST_NEXT(var, field))
    724 
    725 #define	SLIST_FOREACH_SAFE(var, head, field, tvar)			\\
    726 	for ((var) = SLIST_FIRST(head);				\\
    727 	    (var) && ((tvar) = SLIST_NEXT(var, field), 1);		\\
    728 	    (var) = (tvar))
    729 
    730 /*
    731  * Singly-linked List functions.
    732  */
    733 #define	SLIST_INIT(head) {						\\
    734 	SLIST_FIRST(head) = SLIST_END(head);				\\
    735 }
    736 
    737 #define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\\
    738 	(elm)->field.sle_next = (slistelm)->field.sle_next;		\\
    739 	(slistelm)->field.sle_next = (elm);				\\
    740 } while (0)
    741 
    742 #define	SLIST_INSERT_HEAD(head, elm, field) do {			\\
    743 	(elm)->field.sle_next = (head)->slh_first;			\\
    744 	(head)->slh_first = (elm);					\\
    745 } while (0)
    746 
    747 #define	SLIST_REMOVE_AFTER(elm, field) do {				\\
    748 	(elm)->field.sle_next = (elm)->field.sle_next->field.sle_next;	\\
    749 } while (0)
    750 
    751 #define	SLIST_REMOVE_HEAD(head, field) do {				\\
    752 	(head)->slh_first = (head)->slh_first->field.sle_next;		\\
    753 } while (0)
    754 
    755 #define SLIST_REMOVE(head, elm, type, field) do {			\\
    756 	if ((head)->slh_first == (elm)) {				\\
    757 		SLIST_REMOVE_HEAD((head), field);			\\
    758 	} else {							\\
    759 		struct type *curelm = (head)->slh_first;		\\
    760 									\\
    761 		while (curelm->field.sle_next != (elm))			\\
    762 			curelm = curelm->field.sle_next;		\\
    763 		curelm->field.sle_next =				\\
    764 		    curelm->field.sle_next->field.sle_next;		\\
    765 		_Q_INVALIDATE((elm)->field.sle_next);			\\
    766 	}								\\
    767 } while (0)
    768 
    769 /*
    770  * List definitions.
    771  */
    772 #define LIST_HEAD(name, type)						\\
    773 struct name {								\\
    774 	struct type *lh_first;	/* first element */			\\
    775 }
    776 
    777 #define LIST_HEAD_INITIALIZER(head)					\\
    778 	{ NULL }
    779 
    780 #define LIST_ENTRY(type)						\\
    781 struct {								\\
    782 	struct type *le_next;	/* next element */			\\
    783 	struct type **le_prev;	/* address of previous next element */	\\
    784 }
    785 
    786 /*
    787  * List access methods
    788  */
    789 #define	LIST_FIRST(head)		((head)->lh_first)
    790 #define	LIST_END(head)			NULL
    791 #define	LIST_EMPTY(head)		(LIST_FIRST(head) == LIST_END(head))
    792 #define	LIST_NEXT(elm, field)		((elm)->field.le_next)
    793 
    794 #define LIST_FOREACH(var, head, field)					\\
    795 	for((var) = LIST_FIRST(head);					\\
    796 	    (var)!= LIST_END(head);					\\
    797 	    (var) = LIST_NEXT(var, field))
    798 
    799 #define	LIST_FOREACH_SAFE(var, head, field, tvar)			\\
    800 	for ((var) = LIST_FIRST(head);				\\
    801 	    (var) && ((tvar) = LIST_NEXT(var, field), 1);		\\
    802 	    (var) = (tvar))
    803 
    804 /*
    805  * List functions.
    806  */
    807 #define	LIST_INIT(head) do {						\\
    808 	LIST_FIRST(head) = LIST_END(head);				\\
    809 } while (0)
    810 
    811 #define LIST_INSERT_AFTER(listelm, elm, field) do {			\\
    812 	if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)	\\
    813 		(listelm)->field.le_next->field.le_prev =		\\
    814 		    &(elm)->field.le_next;				\\
    815 	(listelm)->field.le_next = (elm);				\\
    816 	(elm)->field.le_prev = &(listelm)->field.le_next;		\\
    817 } while (0)
    818 
    819 #define	LIST_INSERT_BEFORE(listelm, elm, field) do {			\\
    820 	(elm)->field.le_prev = (listelm)->field.le_prev;		\\
    821 	(elm)->field.le_next = (listelm);				\\
    822 	*(listelm)->field.le_prev = (elm);				\\
    823 	(listelm)->field.le_prev = &(elm)->field.le_next;		\\
    824 } while (0)
    825 
    826 #define LIST_INSERT_HEAD(head, elm, field) do {				\\
    827 	if (((elm)->field.le_next = (head)->lh_first) != NULL)		\\
    828 		(head)->lh_first->field.le_prev = &(elm)->field.le_next;\\
    829 	(head)->lh_first = (elm);					\\
    830 	(elm)->field.le_prev = &(head)->lh_first;			\\
    831 } while (0)
    832 
    833 #define LIST_REMOVE(elm, field) do {					\\
    834 	if ((elm)->field.le_next != NULL)				\\
    835 		(elm)->field.le_next->field.le_prev =			\\
    836 		    (elm)->field.le_prev;				\\
    837 	*(elm)->field.le_prev = (elm)->field.le_next;			\\
    838 	_Q_INVALIDATE((elm)->field.le_prev);				\\
    839 	_Q_INVALIDATE((elm)->field.le_next);				\\
    840 } while (0)
    841 
    842 #define LIST_REPLACE(elm, elm2, field) do {				\\
    843 	if (((elm2)->field.le_next = (elm)->field.le_next) != NULL)	\\
    844 		(elm2)->field.le_next->field.le_prev =			\\
    845 		    &(elm2)->field.le_next;				\\
    846 	(elm2)->field.le_prev = (elm)->field.le_prev;			\\
    847 	*(elm2)->field.le_prev = (elm2);				\\
    848 	_Q_INVALIDATE((elm)->field.le_prev);				\\
    849 	_Q_INVALIDATE((elm)->field.le_next);				\\
    850 } while (0)
    851 
    852 /*
    853  * Simple queue definitions.
    854  */
    855 #define SIMPLEQ_HEAD(name, type)					\\
    856 struct name {								\\
    857 	struct type *sqh_first;	/* first element */			\\
    858 	struct type **sqh_last;	/* addr of last next element */		\\
    859 }
    860 
    861 #define SIMPLEQ_HEAD_INITIALIZER(head)					\\
    862 	{ NULL, &(head).sqh_first }
    863 
    864 #define SIMPLEQ_ENTRY(type)						\\
    865 struct {								\\
    866 	struct type *sqe_next;	/* next element */			\\
    867 }
    868 
    869 /*
    870  * Simple queue access methods.
    871  */
    872 #define	SIMPLEQ_FIRST(head)	    ((head)->sqh_first)
    873 #define	SIMPLEQ_END(head)	    NULL
    874 #define	SIMPLEQ_EMPTY(head)	    (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
    875 #define	SIMPLEQ_NEXT(elm, field)    ((elm)->field.sqe_next)
    876 
    877 #define SIMPLEQ_FOREACH(var, head, field)				\\
    878 	for((var) = SIMPLEQ_FIRST(head);				\\
    879 	    (var) != SIMPLEQ_END(head);					\\
    880 	    (var) = SIMPLEQ_NEXT(var, field))
    881 
    882 #define	SIMPLEQ_FOREACH_SAFE(var, head, field, tvar)			\\
    883 	for ((var) = SIMPLEQ_FIRST(head);				\\
    884 	    (var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1);		\\
    885 	    (var) = (tvar))
    886 
    887 /*
    888  * Simple queue functions.
    889  */
    890 #define	SIMPLEQ_INIT(head) do {						\\
    891 	(head)->sqh_first = NULL;					\\
    892 	(head)->sqh_last = &(head)->sqh_first;				\\
    893 } while (0)
    894 
    895 #define SIMPLEQ_INSERT_HEAD(head, elm, field) do {			\\
    896 	if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)	\\
    897 		(head)->sqh_last = &(elm)->field.sqe_next;		\\
    898 	(head)->sqh_first = (elm);					\\
    899 } while (0)
    900 
    901 #define SIMPLEQ_INSERT_TAIL(head, elm, field) do {			\\
    902 	(elm)->field.sqe_next = NULL;					\\
    903 	*(head)->sqh_last = (elm);					\\
    904 	(head)->sqh_last = &(elm)->field.sqe_next;			\\
    905 } while (0)
    906 
    907 #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\\
    908 	if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\\
    909 		(head)->sqh_last = &(elm)->field.sqe_next;		\\
    910 	(listelm)->field.sqe_next = (elm);				\\
    911 } while (0)
    912 
    913 #define SIMPLEQ_REMOVE_HEAD(head, field) do {			\\
    914 	if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \\
    915 		(head)->sqh_last = &(head)->sqh_first;			\\
    916 } while (0)
    917 
    918 #define SIMPLEQ_REMOVE_AFTER(head, elm, field) do {			\\
    919 	if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) \\
    920 	    == NULL)							\\
    921 		(head)->sqh_last = &(elm)->field.sqe_next;		\\
    922 } while (0)
    923 
    924 /*
    925  * Tail queue definitions.
    926  */
    927 #define TAILQ_HEAD(name, type)						\\
    928 struct name {								\\
    929 	struct type *tqh_first;	/* first element */			\\
    930 	struct type **tqh_last;	/* addr of last next element */		\\
    931 }
    932 
    933 #define TAILQ_HEAD_INITIALIZER(head)					\\
    934 	{ NULL, &(head).tqh_first }
    935 
    936 #define TAILQ_ENTRY(type)						\\
    937 struct {								\\
    938 	struct type *tqe_next;	/* next element */			\\
    939 	struct type **tqe_prev;	/* address of previous next element */	\\
    940 }
    941 
    942 /* 
    943  * tail queue access methods 
    944  */
    945 #define	TAILQ_FIRST(head)		((head)->tqh_first)
    946 #define	TAILQ_END(head)			NULL
    947 #define	TAILQ_NEXT(elm, field)		((elm)->field.tqe_next)
    948 #define TAILQ_LAST(head, headname)					\\
    949 	(*(((struct headname *)((head)->tqh_last))->tqh_last))
    950 /* XXX */
    951 #define TAILQ_PREV(elm, headname, field)				\\
    952 	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
    953 #define	TAILQ_EMPTY(head)						\\
    954 	(TAILQ_FIRST(head) == TAILQ_END(head))
    955 
    956 #define TAILQ_FOREACH(var, head, field)					\\
    957 	for((var) = TAILQ_FIRST(head);					\\
    958 	    (var) != TAILQ_END(head);					\\
    959 	    (var) = TAILQ_NEXT(var, field))
    960 
    961 #define	TAILQ_FOREACH_SAFE(var, head, field, tvar)			\\
    962 	for ((var) = TAILQ_FIRST(head);					\\
    963 	    (var) != TAILQ_END(head) &&					\\
    964 	    ((tvar) = TAILQ_NEXT(var, field), 1);			\\
    965 	    (var) = (tvar))
    966 
    967 
    968 #define TAILQ_FOREACH_REVERSE(var, head, headname, field)		\\
    969 	for((var) = TAILQ_LAST(head, headname);				\\
    970 	    (var) != TAILQ_END(head);					\\
    971 	    (var) = TAILQ_PREV(var, headname, field))
    972 
    973 #define	TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar)	\\
    974 	for ((var) = TAILQ_LAST(head, headname);			\\
    975 	    (var) != TAILQ_END(head) &&					\\
    976 	    ((tvar) = TAILQ_PREV(var, headname, field), 1);		\\
    977 	    (var) = (tvar))
    978 
    979 /*
    980  * Tail queue functions.
    981  */
    982 #define	TAILQ_INIT(head) do {						\\
    983 	(head)->tqh_first = NULL;					\\
    984 	(head)->tqh_last = &(head)->tqh_first;				\\
    985 } while (0)
    986 
    987 #define TAILQ_INSERT_HEAD(head, elm, field) do {			\\
    988 	if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)	\\
    989 		(head)->tqh_first->field.tqe_prev =			\\
    990 		    &(elm)->field.tqe_next;				\\
    991 	else								\\
    992 		(head)->tqh_last = &(elm)->field.tqe_next;		\\
    993 	(head)->tqh_first = (elm);					\\
    994 	(elm)->field.tqe_prev = &(head)->tqh_first;			\\
    995 } while (0)
    996 
    997 #define TAILQ_INSERT_TAIL(head, elm, field) do {			\\
    998 	(elm)->field.tqe_next = NULL;					\\
    999 	(elm)->field.tqe_prev = (head)->tqh_last;			\\
   1000 	*(head)->tqh_last = (elm);					\\
   1001 	(head)->tqh_last = &(elm)->field.tqe_next;			\\
   1002 } while (0)
   1003 
   1004 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\\
   1005 	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\\
   1006 		(elm)->field.tqe_next->field.tqe_prev =			\\
   1007 		    &(elm)->field.tqe_next;				\\
   1008 	else								\\
   1009 		(head)->tqh_last = &(elm)->field.tqe_next;		\\
   1010 	(listelm)->field.tqe_next = (elm);				\\
   1011 	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\\
   1012 } while (0)
   1013 
   1014 #define	TAILQ_INSERT_BEFORE(listelm, elm, field) do {			\\
   1015 	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\\
   1016 	(elm)->field.tqe_next = (listelm);				\\
   1017 	*(listelm)->field.tqe_prev = (elm);				\\
   1018 	(listelm)->field.tqe_prev = &(elm)->field.tqe_next;		\\
   1019 } while (0)
   1020 
   1021 #define TAILQ_REMOVE(head, elm, field) do {				\\
   1022 	if (((elm)->field.tqe_next) != NULL)				\\
   1023 		(elm)->field.tqe_next->field.tqe_prev =			\\
   1024 		    (elm)->field.tqe_prev;				\\
   1025 	else								\\
   1026 		(head)->tqh_last = (elm)->field.tqe_prev;		\\
   1027 	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\\
   1028 	_Q_INVALIDATE((elm)->field.tqe_prev);				\\
   1029 	_Q_INVALIDATE((elm)->field.tqe_next);				\\
   1030 } while (0)
   1031 
   1032 #define TAILQ_REPLACE(head, elm, elm2, field) do {			\\
   1033 	if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL)	\\
   1034 		(elm2)->field.tqe_next->field.tqe_prev =		\\
   1035 		    &(elm2)->field.tqe_next;				\\
   1036 	else								\\
   1037 		(head)->tqh_last = &(elm2)->field.tqe_next;		\\
   1038 	(elm2)->field.tqe_prev = (elm)->field.tqe_prev;			\\
   1039 	*(elm2)->field.tqe_prev = (elm2);				\\
   1040 	_Q_INVALIDATE((elm)->field.tqe_prev);				\\
   1041 	_Q_INVALIDATE((elm)->field.tqe_next);				\\
   1042 } while (0)
   1043 
   1044 /*
   1045  * Circular queue definitions.
   1046  */
   1047 #define CIRCLEQ_HEAD(name, type)					\\
   1048 struct name {								\\
   1049 	struct type *cqh_first;		/* first element */		\\
   1050 	struct type *cqh_last;		/* last element */		\\
   1051 }
   1052 
   1053 #define CIRCLEQ_HEAD_INITIALIZER(head)					\\
   1054 	{ CIRCLEQ_END(&head), CIRCLEQ_END(&head) }
   1055 
   1056 #define CIRCLEQ_ENTRY(type)						\\
   1057 struct {								\\
   1058 	struct type *cqe_next;		/* next element */		\\
   1059 	struct type *cqe_prev;		/* previous element */		\\
   1060 }
   1061 
   1062 /*
   1063  * Circular queue access methods 
   1064  */
   1065 #define	CIRCLEQ_FIRST(head)		((head)->cqh_first)
   1066 #define	CIRCLEQ_LAST(head)		((head)->cqh_last)
   1067 #define	CIRCLEQ_END(head)		((void *)(head))
   1068 #define	CIRCLEQ_NEXT(elm, field)	((elm)->field.cqe_next)
   1069 #define	CIRCLEQ_PREV(elm, field)	((elm)->field.cqe_prev)
   1070 #define	CIRCLEQ_EMPTY(head)						\\
   1071 	(CIRCLEQ_FIRST(head) == CIRCLEQ_END(head))
   1072 
   1073 #define CIRCLEQ_FOREACH(var, head, field)				\\
   1074 	for((var) = CIRCLEQ_FIRST(head);				\\
   1075 	    (var) != CIRCLEQ_END(head);					\\
   1076 	    (var) = CIRCLEQ_NEXT(var, field))
   1077 
   1078 #define	CIRCLEQ_FOREACH_SAFE(var, head, field, tvar)			\\
   1079 	for ((var) = CIRCLEQ_FIRST(head);				\\
   1080 	    (var) != CIRCLEQ_END(head) &&				\\
   1081 	    ((tvar) = CIRCLEQ_NEXT(var, field), 1);			\\
   1082 	    (var) = (tvar))
   1083 
   1084 #define CIRCLEQ_FOREACH_REVERSE(var, head, field)			\\
   1085 	for((var) = CIRCLEQ_LAST(head);					\\
   1086 	    (var) != CIRCLEQ_END(head);					\\
   1087 	    (var) = CIRCLEQ_PREV(var, field))
   1088 
   1089 #define	CIRCLEQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar)	\\
   1090 	for ((var) = CIRCLEQ_LAST(head, headname);			\\
   1091 	    (var) != CIRCLEQ_END(head) && 				\\
   1092 	    ((tvar) = CIRCLEQ_PREV(var, headname, field), 1);		\\
   1093 	    (var) = (tvar))
   1094 
   1095 /*
   1096  * Circular queue functions.
   1097  */
   1098 #define	CIRCLEQ_INIT(head) do {						\\
   1099 	(head)->cqh_first = CIRCLEQ_END(head);				\\
   1100 	(head)->cqh_last = CIRCLEQ_END(head);				\\
   1101 } while (0)
   1102 
   1103 #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\\
   1104 	(elm)->field.cqe_next = (listelm)->field.cqe_next;		\\
   1105 	(elm)->field.cqe_prev = (listelm);				\\
   1106 	if ((listelm)->field.cqe_next == CIRCLEQ_END(head))		\\
   1107 		(head)->cqh_last = (elm);				\\
   1108 	else								\\
   1109 		(listelm)->field.cqe_next->field.cqe_prev = (elm);	\\
   1110 	(listelm)->field.cqe_next = (elm);				\\
   1111 } while (0)
   1112 
   1113 #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {		\\
   1114 	(elm)->field.cqe_next = (listelm);				\\
   1115 	(elm)->field.cqe_prev = (listelm)->field.cqe_prev;		\\
   1116 	if ((listelm)->field.cqe_prev == CIRCLEQ_END(head))		\\
   1117 		(head)->cqh_first = (elm);				\\
   1118 	else								\\
   1119 		(listelm)->field.cqe_prev->field.cqe_next = (elm);	\\
   1120 	(listelm)->field.cqe_prev = (elm);				\\
   1121 } while (0)
   1122 
   1123 #define CIRCLEQ_INSERT_HEAD(head, elm, field) do {			\\
   1124 	(elm)->field.cqe_next = (head)->cqh_first;			\\
   1125 	(elm)->field.cqe_prev = CIRCLEQ_END(head);			\\
   1126 	if ((head)->cqh_last == CIRCLEQ_END(head))			\\
   1127 		(head)->cqh_last = (elm);				\\
   1128 	else								\\
   1129 		(head)->cqh_first->field.cqe_prev = (elm);		\\
   1130 	(head)->cqh_first = (elm);					\\
   1131 } while (0)
   1132 
   1133 #define CIRCLEQ_INSERT_TAIL(head, elm, field) do {			\\
   1134 	(elm)->field.cqe_next = CIRCLEQ_END(head);			\\
   1135 	(elm)->field.cqe_prev = (head)->cqh_last;			\\
   1136 	if ((head)->cqh_first == CIRCLEQ_END(head))			\\
   1137 		(head)->cqh_first = (elm);				\\
   1138 	else								\\
   1139 		(head)->cqh_last->field.cqe_next = (elm);		\\
   1140 	(head)->cqh_last = (elm);					\\
   1141 } while (0)
   1142 
   1143 #define	CIRCLEQ_REMOVE(head, elm, field) do {				\\
   1144 	if ((elm)->field.cqe_next == CIRCLEQ_END(head))			\\
   1145 		(head)->cqh_last = (elm)->field.cqe_prev;		\\
   1146 	else								\\
   1147 		(elm)->field.cqe_next->field.cqe_prev =			\\
   1148 		    (elm)->field.cqe_prev;				\\
   1149 	if ((elm)->field.cqe_prev == CIRCLEQ_END(head))			\\
   1150 		(head)->cqh_first = (elm)->field.cqe_next;		\\
   1151 	else								\\
   1152 		(elm)->field.cqe_prev->field.cqe_next =			\\
   1153 		    (elm)->field.cqe_next;				\\
   1154 	_Q_INVALIDATE((elm)->field.cqe_prev);				\\
   1155 	_Q_INVALIDATE((elm)->field.cqe_next);				\\
   1156 } while (0)
   1157 
   1158 #define CIRCLEQ_REPLACE(head, elm, elm2, field) do {			\\
   1159 	if (((elm2)->field.cqe_next = (elm)->field.cqe_next) ==		\\
   1160 	    CIRCLEQ_END(head))						\\
   1161 		(head).cqh_last = (elm2);				\\
   1162 	else								\\
   1163 		(elm2)->field.cqe_next->field.cqe_prev = (elm2);	\\
   1164 	if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) ==		\\
   1165 	    CIRCLEQ_END(head))						\\
   1166 		(head).cqh_first = (elm2);				\\
   1167 	else								\\
   1168 		(elm2)->field.cqe_prev->field.cqe_next = (elm2);	\\
   1169 	_Q_INVALIDATE((elm)->field.cqe_prev);				\\
   1170 	_Q_INVALIDATE((elm)->field.cqe_next);				\\
   1171 } while (0)
   1172 #endif	/* !_FAKE_QUEUE_H_ */
   1173 
   1174 !!
   1175 fi
   1176 
   1177 echo "config.h: written" 1>&2
   1178 echo "config.h: written" 1>&3
   1179 
   1180 #----------------------------------------------------------------------
   1181 # Now we go to generate our Makefile.configure.
   1182 # This file is simply a bunch of Makefile variables.
   1183 # They'll work in both GNUmakefile and BSDmakefile.
   1184 # You MIGHT want to change this.
   1185 #----------------------------------------------------------------------
   1186 
   1187 exec > Makefile.configure
   1188 
   1189 [ -z "${BINDIR}"     ] && BINDIR="${PREFIX}/bin"
   1190 [ -z "${SBINDIR}"    ] && SBINDIR="${PREFIX}/sbin"
   1191 [ -z "${INCLUDEDIR}" ] && INCLUDEDIR="${PREFIX}/include"
   1192 [ -z "${LIBDIR}"     ] && LIBDIR="${PREFIX}/lib"
   1193 [ -z "${MANDIR}"     ] && MANDIR="${PREFIX}/man"
   1194 [ -z "${SHAREDIR}"   ] && SHAREDIR="${PREFIX}/share"
   1195 
   1196 [ -z "${INSTALL_PROGRAM}" ] && INSTALL_PROGRAM="${INSTALL} -m 0555"
   1197 [ -z "${INSTALL_LIB}"     ] && INSTALL_LIB="${INSTALL} -m 0444"
   1198 [ -z "${INSTALL_MAN}"     ] && INSTALL_MAN="${INSTALL} -m 0444"
   1199 [ -z "${INSTALL_DATA}"    ] && INSTALL_DATA="${INSTALL} -m 0444"
   1200 
   1201 cat << __HEREDOC__
   1202 CC		= ${CC}
   1203 CFLAGS		= ${CFLAGS}
   1204 CPPFLAGS	= ${CPPFLAGS}
   1205 LDADD		= ${LDADD}
   1206 LDFLAGS		= ${LDFLAGS}
   1207 STATIC		= ${STATIC}
   1208 PREFIX		= ${PREFIX}
   1209 BINDIR		= ${BINDIR}
   1210 SHAREDIR	= ${SHAREDIR}
   1211 SBINDIR		= ${SBINDIR}
   1212 INCLUDEDIR	= ${INCLUDEDIR}
   1213 LIBDIR		= ${LIBDIR}
   1214 MANDIR		= ${MANDIR}
   1215 INSTALL		= ${INSTALL}
   1216 INSTALL_PROGRAM	= ${INSTALL_PROGRAM}
   1217 INSTALL_LIB	= ${INSTALL_LIB}
   1218 INSTALL_MAN	= ${INSTALL_MAN}
   1219 INSTALL_DATA	= ${INSTALL_DATA}
   1220 __HEREDOC__
   1221 
   1222 echo "Makefile.configure: written" 1>&2
   1223 echo "Makefile.configure: written" 1>&3
   1224 
   1225 exit 0