crypt-portable

Portable version of the classic crypt(1) utility
git clone git://git.sgregoratto.me/crypt-portable
Log | Files | Refs

configure (38701B)


      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.10"
     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 # This is just for size_t.
    353 # Most of these functions, in the real world, pull in <string.h> or
    354 # someting that pulls in support for size_t.
    355 # Our function declarations are standalone, so specify them here.
    356 
    357 [ ${HAVE_MD5} -eq 0 -o \
    358   ${HAVE_READPASSPHRASE} -eq 0 -o \
    359   ${HAVE_REALLOCARRAY} -eq 0 -o \
    360   ${HAVE_RECALLOCARRAY} -eq 0 -o \
    361   ${HAVE_STRLCAT} -eq 0 -o \
    362   ${HAVE_STRLCPY} -eq 0 -o \
    363   ${HAVE_STRNDUP} -eq 0 -o \
    364   ${HAVE_STRNLEN} -eq 0 ] \
    365 	&& echo "#include <sys/types.h>"
    366 
    367 [ ${HAVE_ERR} -eq 0 ] \
    368 	&& echo "#include <stdarg.h>"
    369 
    370 # Now we handle our HAVE_xxxx values.
    371 # Most will just be defined as 0 or 1.
    372 
    373 [ ${HAVE_PATH_MAX} -eq 0 ] \
    374 	&& echo "#define PATH_MAX 4096"
    375 
    376 [ ${HAVE_INFTIM} -eq 0 ] \
    377 	&& echo "#define INFTIM (-1)"
    378 
    379 cat << __HEREDOC__
    380 #define HAVE_ARC4RANDOM ${HAVE_ARC4RANDOM}
    381 #define HAVE_B64_NTOP ${HAVE_B64_NTOP}
    382 #define HAVE_CAPSICUM ${HAVE_CAPSICUM}
    383 #define HAVE_ERR ${HAVE_ERR}
    384 #define HAVE_EXPLICIT_BZERO ${HAVE_EXPLICIT_BZERO}
    385 #define HAVE_GETPROGNAME ${HAVE_GETPROGNAME}
    386 #define HAVE_INFTIM ${HAVE_INFTIM}
    387 #define HAVE_MD5 ${HAVE_MD5}
    388 #define HAVE_MEMMEM ${HAVE_MEMMEM}
    389 #define HAVE_MEMRCHR ${HAVE_MEMRCHR}
    390 #define HAVE_MEMSET_S ${HAVE_MEMSET_S}
    391 #define HAVE_PATH_MAX ${HAVE_PATH_MAX}
    392 #define HAVE_PLEDGE ${HAVE_PLEDGE}
    393 #define HAVE_PROGRAM_INVOCATION_SHORT_NAME ${HAVE_PROGRAM_INVOCATION_SHORT_NAME}
    394 #define HAVE_READPASSPHRASE ${HAVE_READPASSPHRASE}
    395 #define HAVE_REALLOCARRAY ${HAVE_REALLOCARRAY}
    396 #define HAVE_RECALLOCARRAY ${HAVE_RECALLOCARRAY}
    397 #define HAVE_SANDBOX_INIT ${HAVE_SANDBOX_INIT}
    398 #define HAVE_SECCOMP_FILTER ${HAVE_SECCOMP_FILTER}
    399 #define HAVE_SOCK_NONBLOCK ${HAVE_SOCK_NONBLOCK}
    400 #define HAVE_STRLCAT ${HAVE_STRLCAT}
    401 #define HAVE_STRLCPY ${HAVE_STRLCPY}
    402 #define HAVE_STRNDUP ${HAVE_STRNDUP}
    403 #define HAVE_STRNLEN ${HAVE_STRNLEN}
    404 #define HAVE_STRTONUM ${HAVE_STRTONUM}
    405 #define HAVE_SYS_QUEUE ${HAVE_SYS_QUEUE}
    406 #define HAVE_SYSTRACE ${HAVE_SYSTRACE}
    407 #define HAVE_UNVEIL ${HAVE_UNVEIL}
    408 #define HAVE_ZLIB ${HAVE_ZLIB}
    409 #define HAVE___PROGNAME ${HAVE___PROGNAME}
    410 __HEREDOC__
    411 
    412 # Now we do our function declarations for missing functions.
    413 
    414 if [ ${HAVE_ERR} -eq 0 ]; then
    415 	echo "extern void err(int, const char *, ...);"
    416 	echo "extern void errx(int, const char *, ...);"
    417 	echo "extern void warn(const char *, ...);"
    418 	echo "extern void warnx(const char *, ...);"
    419 	echo "extern void vwarn(const char *, va_list);"
    420 	echo "extern void vwarnx(const char *, va_list);"
    421 fi
    422 
    423 if [ ${HAVE_MD5} -eq 0 ]; then
    424 	echo "#define MD5_BLOCK_LENGTH 64"
    425 	echo "#define MD5_DIGEST_LENGTH 16"
    426 	echo "#define MD5_DIGEST_STRING_LENGTH (MD5_DIGEST_LENGTH * 2 + 1)"
    427 	cat <<!!
    428 typedef struct MD5Context {
    429 	u_int32_t state[4];
    430 	u_int64_t count;
    431 	u_int8_t buffer[MD5_BLOCK_LENGTH];
    432 } MD5_CTX;
    433 !!
    434 	echo "extern void MD5Init(MD5_CTX *);"
    435 	echo "extern void MD5Update(MD5_CTX *, const u_int8_t *, size_t);"
    436 	echo "extern void MD5Pad(MD5_CTX *);"
    437 	echo "extern void MD5Transform(u_int32_t [4], const u_int8_t [MD5_BLOCK_LENGTH]);"
    438 	echo "extern char *MD5End(MD5_CTX *, char *);"
    439 	echo "extern void MD5Final(u_int8_t [MD5_DIGEST_LENGTH], MD5_CTX *);";
    440 fi
    441 
    442 if [ ${HAVE_SECCOMP_FILTER} -eq 1 ]; then
    443 	arch=`uname -m 2>/dev/null || echo unknown`
    444 	case "$arch" in
    445 		x86_64)
    446 			echo "#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_X86_64"
    447 			;;
    448 		i*86)
    449 			echo "#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_I386"
    450 			;;
    451 		arm*)
    452 			echo "#define SECCOMP_AUDIT_ARCH AUDIT_ARCH_ARM"
    453 			;;
    454 	esac
    455 fi
    456 
    457 if [ ${HAVE_B64_NTOP} -eq 0 ]; then
    458 	echo "extern int b64_ntop(unsigned char const *, size_t, char *, size_t);";
    459 	echo "extern int b64_pton(char const *, unsigned char *, size_t);"
    460 fi
    461 
    462 if [ ${HAVE_EXPLICIT_BZERO} -eq 0 ]; then
    463 	echo "extern void explicit_bzero(void *, size_t);"
    464 fi
    465 
    466 if [ ${HAVE_MEMMEM} -eq 0 ]; then
    467 	echo "void *memmem(const void *, size_t, const void *, size_t);"
    468 fi
    469 
    470 if [ ${HAVE_MEMRCHR} -eq 0 ]; then
    471 	echo "void *memrchr(const void *b, int, size_t);"
    472 fi
    473 
    474 if [ ${HAVE_GETPROGNAME} -eq 0 ]; then
    475 	echo "extern const char *getprogname(void);"
    476 fi
    477 
    478 if [ ${HAVE_READPASSPHRASE} -eq 0 ]; then
    479 	echo "#define RPP_ECHO_OFF 0x00"
    480 	echo "#define RPP_ECHO_ON 0x01"
    481 	echo "#define RPP_REQUIRE_TTY 0x02"
    482 	echo "#define RPP_FORCELOWER 0x04"
    483 	echo "#define RPP_FORCEUPPER 0x08"
    484 	echo "#define RPP_SEVENBIT 0x10"
    485 	echo "#define RPP_STDIN 0x20"
    486 	echo "char *readpassphrase(const char *, char *, size_t, int);"
    487 fi
    488 
    489 if [ ${HAVE_REALLOCARRAY} -eq 0 ]; then
    490 	echo "extern void *reallocarray(void *, size_t, size_t);"
    491 fi
    492 
    493 if [ ${HAVE_RECALLOCARRAY} -eq 0 ]; then
    494 	echo "extern void *recallocarray(void *, size_t, size_t, size_t);"
    495 fi
    496 
    497 if [ ${HAVE_STRLCAT} -eq 0 ]; then
    498 	echo "extern size_t strlcat(char *, const char *, size_t);"
    499 fi
    500 
    501 if [ ${HAVE_STRLCPY} -eq 0 ]; then
    502 	echo "extern size_t strlcpy(char *, const char *, size_t);"
    503 fi
    504 
    505 if [ ${HAVE_STRNDUP} -eq 0 ]; then
    506 	echo "extern char *strndup(const char *, size_t);"
    507 fi
    508 
    509 if [ ${HAVE_STRNLEN} -eq 0 ]; then
    510 	echo "extern size_t strnlen(const char *, size_t);"
    511 fi
    512 
    513 if [ ${HAVE_STRTONUM} -eq 0 ]; then
    514 	echo "extern long long strtonum(const char *, long long, long long, const char **);"
    515 fi
    516 
    517 if [ ${HAVE_SYS_QUEUE} -eq 0 ]; then
    518 cat <<!!
    519 
    520 /*	$OpenBSD$	*/
    521 /*	$NetBSD: queue.h,v 1.11 1996/05/16 05:17:14 mycroft Exp $	*/
    522 
    523 /*
    524  * Copyright (c) 1991, 1993
    525  *	The Regents of the University of California.  All rights reserved.
    526  *
    527  * Redistribution and use in source and binary forms, with or without
    528  * modification, are permitted provided that the following conditions
    529  * are met:
    530  * 1. Redistributions of source code must retain the above copyright
    531  *    notice, this list of conditions and the following disclaimer.
    532  * 2. Redistributions in binary form must reproduce the above copyright
    533  *    notice, this list of conditions and the following disclaimer in the
    534  *    documentation and/or other materials provided with the distribution.
    535  * 3. Neither the name of the University nor the names of its contributors
    536  *    may be used to endorse or promote products derived from this software
    537  *    without specific prior written permission.
    538  *
    539  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
    540  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    541  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    542  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
    543  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    544  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    545  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    546  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    547  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    548  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    549  * SUCH DAMAGE.
    550  *
    551  *	@(#)queue.h	8.5 (Berkeley) 8/20/94
    552  */
    553 
    554 /* OPENBSD ORIGINAL: sys/sys/queue.h */
    555 
    556 #ifndef	_FAKE_QUEUE_H_
    557 #define	_FAKE_QUEUE_H_
    558 
    559 /*
    560  * Require for OS/X and other platforms that have old/broken/incomplete
    561  * <sys/queue.h>.
    562  */
    563 #undef SLIST_HEAD
    564 #undef SLIST_HEAD_INITIALIZER
    565 #undef SLIST_ENTRY
    566 #undef SLIST_FOREACH_PREVPTR
    567 #undef SLIST_FOREACH_SAFE
    568 #undef SLIST_FIRST
    569 #undef SLIST_END
    570 #undef SLIST_EMPTY
    571 #undef SLIST_NEXT
    572 #undef SLIST_FOREACH
    573 #undef SLIST_INIT
    574 #undef SLIST_INSERT_AFTER
    575 #undef SLIST_INSERT_HEAD
    576 #undef SLIST_REMOVE_HEAD
    577 #undef SLIST_REMOVE_AFTER
    578 #undef SLIST_REMOVE
    579 #undef SLIST_REMOVE_NEXT
    580 #undef LIST_HEAD
    581 #undef LIST_HEAD_INITIALIZER
    582 #undef LIST_ENTRY
    583 #undef LIST_FIRST
    584 #undef LIST_END
    585 #undef LIST_EMPTY
    586 #undef LIST_NEXT
    587 #undef LIST_FOREACH
    588 #undef LIST_FOREACH_SAFE
    589 #undef LIST_INIT
    590 #undef LIST_INSERT_AFTER
    591 #undef LIST_INSERT_BEFORE
    592 #undef LIST_INSERT_HEAD
    593 #undef LIST_REMOVE
    594 #undef LIST_REPLACE
    595 #undef SIMPLEQ_HEAD
    596 #undef SIMPLEQ_HEAD_INITIALIZER
    597 #undef SIMPLEQ_ENTRY
    598 #undef SIMPLEQ_FIRST
    599 #undef SIMPLEQ_END
    600 #undef SIMPLEQ_EMPTY
    601 #undef SIMPLEQ_NEXT
    602 #undef SIMPLEQ_FOREACH
    603 #undef SIMPLEQ_INIT
    604 #undef SIMPLEQ_INSERT_HEAD
    605 #undef SIMPLEQ_INSERT_TAIL
    606 #undef SIMPLEQ_INSERT_AFTER
    607 #undef SIMPLEQ_REMOVE_HEAD
    608 #undef TAILQ_HEAD
    609 #undef TAILQ_HEAD_INITIALIZER
    610 #undef TAILQ_ENTRY
    611 #undef TAILQ_FIRST
    612 #undef TAILQ_END
    613 #undef TAILQ_NEXT
    614 #undef TAILQ_LAST
    615 #undef TAILQ_PREV
    616 #undef TAILQ_EMPTY
    617 #undef TAILQ_FOREACH
    618 #undef TAILQ_FOREACH_REVERSE
    619 #undef TAILQ_FOREACH_SAFE
    620 #undef TAILQ_FOREACH_REVERSE_SAFE
    621 #undef TAILQ_INIT
    622 #undef TAILQ_INSERT_HEAD
    623 #undef TAILQ_INSERT_TAIL
    624 #undef TAILQ_INSERT_AFTER
    625 #undef TAILQ_INSERT_BEFORE
    626 #undef TAILQ_REMOVE
    627 #undef TAILQ_REPLACE
    628 #undef CIRCLEQ_HEAD
    629 #undef CIRCLEQ_HEAD_INITIALIZER
    630 #undef CIRCLEQ_ENTRY
    631 #undef CIRCLEQ_FIRST
    632 #undef CIRCLEQ_LAST
    633 #undef CIRCLEQ_END
    634 #undef CIRCLEQ_NEXT
    635 #undef CIRCLEQ_PREV
    636 #undef CIRCLEQ_EMPTY
    637 #undef CIRCLEQ_FOREACH
    638 #undef CIRCLEQ_FOREACH_REVERSE
    639 #undef CIRCLEQ_INIT
    640 #undef CIRCLEQ_INSERT_AFTER
    641 #undef CIRCLEQ_INSERT_BEFORE
    642 #undef CIRCLEQ_INSERT_HEAD
    643 #undef CIRCLEQ_INSERT_TAIL
    644 #undef CIRCLEQ_REMOVE
    645 #undef CIRCLEQ_REPLACE
    646 
    647 /*
    648  * This file defines five types of data structures: singly-linked lists, 
    649  * lists, simple queues, tail queues, and circular queues.
    650  *
    651  *
    652  * A singly-linked list is headed by a single forward pointer. The elements
    653  * are singly linked for minimum space and pointer manipulation overhead at
    654  * the expense of O(n) removal for arbitrary elements. New elements can be
    655  * added to the list after an existing element or at the head of the list.
    656  * Elements being removed from the head of the list should use the explicit
    657  * macro for this purpose for optimum efficiency. A singly-linked list may
    658  * only be traversed in the forward direction.  Singly-linked lists are ideal
    659  * for applications with large datasets and few or no removals or for
    660  * implementing a LIFO queue.
    661  *
    662  * A list is headed by a single forward pointer (or an array of forward
    663  * pointers for a hash table header). The elements are doubly linked
    664  * so that an arbitrary element can be removed without a need to
    665  * traverse the list. New elements can be added to the list before
    666  * or after an existing element or at the head of the list. A list
    667  * may only be traversed in the forward direction.
    668  *
    669  * A simple queue is headed by a pair of pointers, one the head of the
    670  * list and the other to the tail of the list. The elements are singly
    671  * linked to save space, so elements can only be removed from the
    672  * head of the list. New elements can be added to the list before or after
    673  * an existing element, at the head of the list, or at the end of the
    674  * list. A simple queue may only be traversed in the forward direction.
    675  *
    676  * A tail queue is headed by a pair of pointers, one to the head of the
    677  * list and the other to the tail of the list. The elements are doubly
    678  * linked so that an arbitrary element can be removed without a need to
    679  * traverse the list. New elements can be added to the list before or
    680  * after an existing element, at the head of the list, or at the end of
    681  * the list. A tail queue may be traversed in either direction.
    682  *
    683  * A circle queue is headed by a pair of pointers, one to the head of the
    684  * list and the other to the tail of the list. The elements are doubly
    685  * linked so that an arbitrary element can be removed without a need to
    686  * traverse the list. New elements can be added to the list before or after
    687  * an existing element, at the head of the list, or at the end of the list.
    688  * A circle queue may be traversed in either direction, but has a more
    689  * complex end of list detection.
    690  *
    691  * For details on the use of these macros, see the queue(3) manual page.
    692  */
    693 
    694 #if defined(QUEUE_MACRO_DEBUG) || (defined(_KERNEL) && defined(DIAGNOSTIC))
    695 #define _Q_INVALIDATE(a) (a) = ((void *)-1)
    696 #else
    697 #define _Q_INVALIDATE(a)
    698 #endif
    699 
    700 /*
    701  * Singly-linked List definitions.
    702  */
    703 #define SLIST_HEAD(name, type)						\\
    704 struct name {								\\
    705 	struct type *slh_first;	/* first element */			\\
    706 }
    707  
    708 #define	SLIST_HEAD_INITIALIZER(head)					\\
    709 	{ NULL }
    710  
    711 #define SLIST_ENTRY(type)						\\
    712 struct {								\\
    713 	struct type *sle_next;	/* next element */			\\
    714 }
    715  
    716 /*
    717  * Singly-linked List access methods.
    718  */
    719 #define	SLIST_FIRST(head)	((head)->slh_first)
    720 #define	SLIST_END(head)		NULL
    721 #define	SLIST_EMPTY(head)	(SLIST_FIRST(head) == SLIST_END(head))
    722 #define	SLIST_NEXT(elm, field)	((elm)->field.sle_next)
    723 
    724 #define	SLIST_FOREACH(var, head, field)					\\
    725 	for((var) = SLIST_FIRST(head);					\\
    726 	    (var) != SLIST_END(head);					\\
    727 	    (var) = SLIST_NEXT(var, field))
    728 
    729 #define	SLIST_FOREACH_SAFE(var, head, field, tvar)			\\
    730 	for ((var) = SLIST_FIRST(head);				\\
    731 	    (var) && ((tvar) = SLIST_NEXT(var, field), 1);		\\
    732 	    (var) = (tvar))
    733 
    734 /*
    735  * Singly-linked List functions.
    736  */
    737 #define	SLIST_INIT(head) {						\\
    738 	SLIST_FIRST(head) = SLIST_END(head);				\\
    739 }
    740 
    741 #define	SLIST_INSERT_AFTER(slistelm, elm, field) do {			\\
    742 	(elm)->field.sle_next = (slistelm)->field.sle_next;		\\
    743 	(slistelm)->field.sle_next = (elm);				\\
    744 } while (0)
    745 
    746 #define	SLIST_INSERT_HEAD(head, elm, field) do {			\\
    747 	(elm)->field.sle_next = (head)->slh_first;			\\
    748 	(head)->slh_first = (elm);					\\
    749 } while (0)
    750 
    751 #define	SLIST_REMOVE_AFTER(elm, field) do {				\\
    752 	(elm)->field.sle_next = (elm)->field.sle_next->field.sle_next;	\\
    753 } while (0)
    754 
    755 #define	SLIST_REMOVE_HEAD(head, field) do {				\\
    756 	(head)->slh_first = (head)->slh_first->field.sle_next;		\\
    757 } while (0)
    758 
    759 #define SLIST_REMOVE(head, elm, type, field) do {			\\
    760 	if ((head)->slh_first == (elm)) {				\\
    761 		SLIST_REMOVE_HEAD((head), field);			\\
    762 	} else {							\\
    763 		struct type *curelm = (head)->slh_first;		\\
    764 									\\
    765 		while (curelm->field.sle_next != (elm))			\\
    766 			curelm = curelm->field.sle_next;		\\
    767 		curelm->field.sle_next =				\\
    768 		    curelm->field.sle_next->field.sle_next;		\\
    769 		_Q_INVALIDATE((elm)->field.sle_next);			\\
    770 	}								\\
    771 } while (0)
    772 
    773 /*
    774  * List definitions.
    775  */
    776 #define LIST_HEAD(name, type)						\\
    777 struct name {								\\
    778 	struct type *lh_first;	/* first element */			\\
    779 }
    780 
    781 #define LIST_HEAD_INITIALIZER(head)					\\
    782 	{ NULL }
    783 
    784 #define LIST_ENTRY(type)						\\
    785 struct {								\\
    786 	struct type *le_next;	/* next element */			\\
    787 	struct type **le_prev;	/* address of previous next element */	\\
    788 }
    789 
    790 /*
    791  * List access methods
    792  */
    793 #define	LIST_FIRST(head)		((head)->lh_first)
    794 #define	LIST_END(head)			NULL
    795 #define	LIST_EMPTY(head)		(LIST_FIRST(head) == LIST_END(head))
    796 #define	LIST_NEXT(elm, field)		((elm)->field.le_next)
    797 
    798 #define LIST_FOREACH(var, head, field)					\\
    799 	for((var) = LIST_FIRST(head);					\\
    800 	    (var)!= LIST_END(head);					\\
    801 	    (var) = LIST_NEXT(var, field))
    802 
    803 #define	LIST_FOREACH_SAFE(var, head, field, tvar)			\\
    804 	for ((var) = LIST_FIRST(head);				\\
    805 	    (var) && ((tvar) = LIST_NEXT(var, field), 1);		\\
    806 	    (var) = (tvar))
    807 
    808 /*
    809  * List functions.
    810  */
    811 #define	LIST_INIT(head) do {						\\
    812 	LIST_FIRST(head) = LIST_END(head);				\\
    813 } while (0)
    814 
    815 #define LIST_INSERT_AFTER(listelm, elm, field) do {			\\
    816 	if (((elm)->field.le_next = (listelm)->field.le_next) != NULL)	\\
    817 		(listelm)->field.le_next->field.le_prev =		\\
    818 		    &(elm)->field.le_next;				\\
    819 	(listelm)->field.le_next = (elm);				\\
    820 	(elm)->field.le_prev = &(listelm)->field.le_next;		\\
    821 } while (0)
    822 
    823 #define	LIST_INSERT_BEFORE(listelm, elm, field) do {			\\
    824 	(elm)->field.le_prev = (listelm)->field.le_prev;		\\
    825 	(elm)->field.le_next = (listelm);				\\
    826 	*(listelm)->field.le_prev = (elm);				\\
    827 	(listelm)->field.le_prev = &(elm)->field.le_next;		\\
    828 } while (0)
    829 
    830 #define LIST_INSERT_HEAD(head, elm, field) do {				\\
    831 	if (((elm)->field.le_next = (head)->lh_first) != NULL)		\\
    832 		(head)->lh_first->field.le_prev = &(elm)->field.le_next;\\
    833 	(head)->lh_first = (elm);					\\
    834 	(elm)->field.le_prev = &(head)->lh_first;			\\
    835 } while (0)
    836 
    837 #define LIST_REMOVE(elm, field) do {					\\
    838 	if ((elm)->field.le_next != NULL)				\\
    839 		(elm)->field.le_next->field.le_prev =			\\
    840 		    (elm)->field.le_prev;				\\
    841 	*(elm)->field.le_prev = (elm)->field.le_next;			\\
    842 	_Q_INVALIDATE((elm)->field.le_prev);				\\
    843 	_Q_INVALIDATE((elm)->field.le_next);				\\
    844 } while (0)
    845 
    846 #define LIST_REPLACE(elm, elm2, field) do {				\\
    847 	if (((elm2)->field.le_next = (elm)->field.le_next) != NULL)	\\
    848 		(elm2)->field.le_next->field.le_prev =			\\
    849 		    &(elm2)->field.le_next;				\\
    850 	(elm2)->field.le_prev = (elm)->field.le_prev;			\\
    851 	*(elm2)->field.le_prev = (elm2);				\\
    852 	_Q_INVALIDATE((elm)->field.le_prev);				\\
    853 	_Q_INVALIDATE((elm)->field.le_next);				\\
    854 } while (0)
    855 
    856 /*
    857  * Simple queue definitions.
    858  */
    859 #define SIMPLEQ_HEAD(name, type)					\\
    860 struct name {								\\
    861 	struct type *sqh_first;	/* first element */			\\
    862 	struct type **sqh_last;	/* addr of last next element */		\\
    863 }
    864 
    865 #define SIMPLEQ_HEAD_INITIALIZER(head)					\\
    866 	{ NULL, &(head).sqh_first }
    867 
    868 #define SIMPLEQ_ENTRY(type)						\\
    869 struct {								\\
    870 	struct type *sqe_next;	/* next element */			\\
    871 }
    872 
    873 /*
    874  * Simple queue access methods.
    875  */
    876 #define	SIMPLEQ_FIRST(head)	    ((head)->sqh_first)
    877 #define	SIMPLEQ_END(head)	    NULL
    878 #define	SIMPLEQ_EMPTY(head)	    (SIMPLEQ_FIRST(head) == SIMPLEQ_END(head))
    879 #define	SIMPLEQ_NEXT(elm, field)    ((elm)->field.sqe_next)
    880 
    881 #define SIMPLEQ_FOREACH(var, head, field)				\\
    882 	for((var) = SIMPLEQ_FIRST(head);				\\
    883 	    (var) != SIMPLEQ_END(head);					\\
    884 	    (var) = SIMPLEQ_NEXT(var, field))
    885 
    886 #define	SIMPLEQ_FOREACH_SAFE(var, head, field, tvar)			\\
    887 	for ((var) = SIMPLEQ_FIRST(head);				\\
    888 	    (var) && ((tvar) = SIMPLEQ_NEXT(var, field), 1);		\\
    889 	    (var) = (tvar))
    890 
    891 /*
    892  * Simple queue functions.
    893  */
    894 #define	SIMPLEQ_INIT(head) do {						\\
    895 	(head)->sqh_first = NULL;					\\
    896 	(head)->sqh_last = &(head)->sqh_first;				\\
    897 } while (0)
    898 
    899 #define SIMPLEQ_INSERT_HEAD(head, elm, field) do {			\\
    900 	if (((elm)->field.sqe_next = (head)->sqh_first) == NULL)	\\
    901 		(head)->sqh_last = &(elm)->field.sqe_next;		\\
    902 	(head)->sqh_first = (elm);					\\
    903 } while (0)
    904 
    905 #define SIMPLEQ_INSERT_TAIL(head, elm, field) do {			\\
    906 	(elm)->field.sqe_next = NULL;					\\
    907 	*(head)->sqh_last = (elm);					\\
    908 	(head)->sqh_last = &(elm)->field.sqe_next;			\\
    909 } while (0)
    910 
    911 #define SIMPLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\\
    912 	if (((elm)->field.sqe_next = (listelm)->field.sqe_next) == NULL)\\
    913 		(head)->sqh_last = &(elm)->field.sqe_next;		\\
    914 	(listelm)->field.sqe_next = (elm);				\\
    915 } while (0)
    916 
    917 #define SIMPLEQ_REMOVE_HEAD(head, field) do {			\\
    918 	if (((head)->sqh_first = (head)->sqh_first->field.sqe_next) == NULL) \\
    919 		(head)->sqh_last = &(head)->sqh_first;			\\
    920 } while (0)
    921 
    922 #define SIMPLEQ_REMOVE_AFTER(head, elm, field) do {			\\
    923 	if (((elm)->field.sqe_next = (elm)->field.sqe_next->field.sqe_next) \\
    924 	    == NULL)							\\
    925 		(head)->sqh_last = &(elm)->field.sqe_next;		\\
    926 } while (0)
    927 
    928 /*
    929  * Tail queue definitions.
    930  */
    931 #define TAILQ_HEAD(name, type)						\\
    932 struct name {								\\
    933 	struct type *tqh_first;	/* first element */			\\
    934 	struct type **tqh_last;	/* addr of last next element */		\\
    935 }
    936 
    937 #define TAILQ_HEAD_INITIALIZER(head)					\\
    938 	{ NULL, &(head).tqh_first }
    939 
    940 #define TAILQ_ENTRY(type)						\\
    941 struct {								\\
    942 	struct type *tqe_next;	/* next element */			\\
    943 	struct type **tqe_prev;	/* address of previous next element */	\\
    944 }
    945 
    946 /* 
    947  * tail queue access methods 
    948  */
    949 #define	TAILQ_FIRST(head)		((head)->tqh_first)
    950 #define	TAILQ_END(head)			NULL
    951 #define	TAILQ_NEXT(elm, field)		((elm)->field.tqe_next)
    952 #define TAILQ_LAST(head, headname)					\\
    953 	(*(((struct headname *)((head)->tqh_last))->tqh_last))
    954 /* XXX */
    955 #define TAILQ_PREV(elm, headname, field)				\\
    956 	(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
    957 #define	TAILQ_EMPTY(head)						\\
    958 	(TAILQ_FIRST(head) == TAILQ_END(head))
    959 
    960 #define TAILQ_FOREACH(var, head, field)					\\
    961 	for((var) = TAILQ_FIRST(head);					\\
    962 	    (var) != TAILQ_END(head);					\\
    963 	    (var) = TAILQ_NEXT(var, field))
    964 
    965 #define	TAILQ_FOREACH_SAFE(var, head, field, tvar)			\\
    966 	for ((var) = TAILQ_FIRST(head);					\\
    967 	    (var) != TAILQ_END(head) &&					\\
    968 	    ((tvar) = TAILQ_NEXT(var, field), 1);			\\
    969 	    (var) = (tvar))
    970 
    971 
    972 #define TAILQ_FOREACH_REVERSE(var, head, headname, field)		\\
    973 	for((var) = TAILQ_LAST(head, headname);				\\
    974 	    (var) != TAILQ_END(head);					\\
    975 	    (var) = TAILQ_PREV(var, headname, field))
    976 
    977 #define	TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar)	\\
    978 	for ((var) = TAILQ_LAST(head, headname);			\\
    979 	    (var) != TAILQ_END(head) &&					\\
    980 	    ((tvar) = TAILQ_PREV(var, headname, field), 1);		\\
    981 	    (var) = (tvar))
    982 
    983 /*
    984  * Tail queue functions.
    985  */
    986 #define	TAILQ_INIT(head) do {						\\
    987 	(head)->tqh_first = NULL;					\\
    988 	(head)->tqh_last = &(head)->tqh_first;				\\
    989 } while (0)
    990 
    991 #define TAILQ_INSERT_HEAD(head, elm, field) do {			\\
    992 	if (((elm)->field.tqe_next = (head)->tqh_first) != NULL)	\\
    993 		(head)->tqh_first->field.tqe_prev =			\\
    994 		    &(elm)->field.tqe_next;				\\
    995 	else								\\
    996 		(head)->tqh_last = &(elm)->field.tqe_next;		\\
    997 	(head)->tqh_first = (elm);					\\
    998 	(elm)->field.tqe_prev = &(head)->tqh_first;			\\
    999 } while (0)
   1000 
   1001 #define TAILQ_INSERT_TAIL(head, elm, field) do {			\\
   1002 	(elm)->field.tqe_next = NULL;					\\
   1003 	(elm)->field.tqe_prev = (head)->tqh_last;			\\
   1004 	*(head)->tqh_last = (elm);					\\
   1005 	(head)->tqh_last = &(elm)->field.tqe_next;			\\
   1006 } while (0)
   1007 
   1008 #define TAILQ_INSERT_AFTER(head, listelm, elm, field) do {		\\
   1009 	if (((elm)->field.tqe_next = (listelm)->field.tqe_next) != NULL)\\
   1010 		(elm)->field.tqe_next->field.tqe_prev =			\\
   1011 		    &(elm)->field.tqe_next;				\\
   1012 	else								\\
   1013 		(head)->tqh_last = &(elm)->field.tqe_next;		\\
   1014 	(listelm)->field.tqe_next = (elm);				\\
   1015 	(elm)->field.tqe_prev = &(listelm)->field.tqe_next;		\\
   1016 } while (0)
   1017 
   1018 #define	TAILQ_INSERT_BEFORE(listelm, elm, field) do {			\\
   1019 	(elm)->field.tqe_prev = (listelm)->field.tqe_prev;		\\
   1020 	(elm)->field.tqe_next = (listelm);				\\
   1021 	*(listelm)->field.tqe_prev = (elm);				\\
   1022 	(listelm)->field.tqe_prev = &(elm)->field.tqe_next;		\\
   1023 } while (0)
   1024 
   1025 #define TAILQ_REMOVE(head, elm, field) do {				\\
   1026 	if (((elm)->field.tqe_next) != NULL)				\\
   1027 		(elm)->field.tqe_next->field.tqe_prev =			\\
   1028 		    (elm)->field.tqe_prev;				\\
   1029 	else								\\
   1030 		(head)->tqh_last = (elm)->field.tqe_prev;		\\
   1031 	*(elm)->field.tqe_prev = (elm)->field.tqe_next;			\\
   1032 	_Q_INVALIDATE((elm)->field.tqe_prev);				\\
   1033 	_Q_INVALIDATE((elm)->field.tqe_next);				\\
   1034 } while (0)
   1035 
   1036 #define TAILQ_REPLACE(head, elm, elm2, field) do {			\\
   1037 	if (((elm2)->field.tqe_next = (elm)->field.tqe_next) != NULL)	\\
   1038 		(elm2)->field.tqe_next->field.tqe_prev =		\\
   1039 		    &(elm2)->field.tqe_next;				\\
   1040 	else								\\
   1041 		(head)->tqh_last = &(elm2)->field.tqe_next;		\\
   1042 	(elm2)->field.tqe_prev = (elm)->field.tqe_prev;			\\
   1043 	*(elm2)->field.tqe_prev = (elm2);				\\
   1044 	_Q_INVALIDATE((elm)->field.tqe_prev);				\\
   1045 	_Q_INVALIDATE((elm)->field.tqe_next);				\\
   1046 } while (0)
   1047 
   1048 /*
   1049  * Circular queue definitions.
   1050  */
   1051 #define CIRCLEQ_HEAD(name, type)					\\
   1052 struct name {								\\
   1053 	struct type *cqh_first;		/* first element */		\\
   1054 	struct type *cqh_last;		/* last element */		\\
   1055 }
   1056 
   1057 #define CIRCLEQ_HEAD_INITIALIZER(head)					\\
   1058 	{ CIRCLEQ_END(&head), CIRCLEQ_END(&head) }
   1059 
   1060 #define CIRCLEQ_ENTRY(type)						\\
   1061 struct {								\\
   1062 	struct type *cqe_next;		/* next element */		\\
   1063 	struct type *cqe_prev;		/* previous element */		\\
   1064 }
   1065 
   1066 /*
   1067  * Circular queue access methods 
   1068  */
   1069 #define	CIRCLEQ_FIRST(head)		((head)->cqh_first)
   1070 #define	CIRCLEQ_LAST(head)		((head)->cqh_last)
   1071 #define	CIRCLEQ_END(head)		((void *)(head))
   1072 #define	CIRCLEQ_NEXT(elm, field)	((elm)->field.cqe_next)
   1073 #define	CIRCLEQ_PREV(elm, field)	((elm)->field.cqe_prev)
   1074 #define	CIRCLEQ_EMPTY(head)						\\
   1075 	(CIRCLEQ_FIRST(head) == CIRCLEQ_END(head))
   1076 
   1077 #define CIRCLEQ_FOREACH(var, head, field)				\\
   1078 	for((var) = CIRCLEQ_FIRST(head);				\\
   1079 	    (var) != CIRCLEQ_END(head);					\\
   1080 	    (var) = CIRCLEQ_NEXT(var, field))
   1081 
   1082 #define	CIRCLEQ_FOREACH_SAFE(var, head, field, tvar)			\\
   1083 	for ((var) = CIRCLEQ_FIRST(head);				\\
   1084 	    (var) != CIRCLEQ_END(head) &&				\\
   1085 	    ((tvar) = CIRCLEQ_NEXT(var, field), 1);			\\
   1086 	    (var) = (tvar))
   1087 
   1088 #define CIRCLEQ_FOREACH_REVERSE(var, head, field)			\\
   1089 	for((var) = CIRCLEQ_LAST(head);					\\
   1090 	    (var) != CIRCLEQ_END(head);					\\
   1091 	    (var) = CIRCLEQ_PREV(var, field))
   1092 
   1093 #define	CIRCLEQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar)	\\
   1094 	for ((var) = CIRCLEQ_LAST(head, headname);			\\
   1095 	    (var) != CIRCLEQ_END(head) && 				\\
   1096 	    ((tvar) = CIRCLEQ_PREV(var, headname, field), 1);		\\
   1097 	    (var) = (tvar))
   1098 
   1099 /*
   1100  * Circular queue functions.
   1101  */
   1102 #define	CIRCLEQ_INIT(head) do {						\\
   1103 	(head)->cqh_first = CIRCLEQ_END(head);				\\
   1104 	(head)->cqh_last = CIRCLEQ_END(head);				\\
   1105 } while (0)
   1106 
   1107 #define CIRCLEQ_INSERT_AFTER(head, listelm, elm, field) do {		\\
   1108 	(elm)->field.cqe_next = (listelm)->field.cqe_next;		\\
   1109 	(elm)->field.cqe_prev = (listelm);				\\
   1110 	if ((listelm)->field.cqe_next == CIRCLEQ_END(head))		\\
   1111 		(head)->cqh_last = (elm);				\\
   1112 	else								\\
   1113 		(listelm)->field.cqe_next->field.cqe_prev = (elm);	\\
   1114 	(listelm)->field.cqe_next = (elm);				\\
   1115 } while (0)
   1116 
   1117 #define CIRCLEQ_INSERT_BEFORE(head, listelm, elm, field) do {		\\
   1118 	(elm)->field.cqe_next = (listelm);				\\
   1119 	(elm)->field.cqe_prev = (listelm)->field.cqe_prev;		\\
   1120 	if ((listelm)->field.cqe_prev == CIRCLEQ_END(head))		\\
   1121 		(head)->cqh_first = (elm);				\\
   1122 	else								\\
   1123 		(listelm)->field.cqe_prev->field.cqe_next = (elm);	\\
   1124 	(listelm)->field.cqe_prev = (elm);				\\
   1125 } while (0)
   1126 
   1127 #define CIRCLEQ_INSERT_HEAD(head, elm, field) do {			\\
   1128 	(elm)->field.cqe_next = (head)->cqh_first;			\\
   1129 	(elm)->field.cqe_prev = CIRCLEQ_END(head);			\\
   1130 	if ((head)->cqh_last == CIRCLEQ_END(head))			\\
   1131 		(head)->cqh_last = (elm);				\\
   1132 	else								\\
   1133 		(head)->cqh_first->field.cqe_prev = (elm);		\\
   1134 	(head)->cqh_first = (elm);					\\
   1135 } while (0)
   1136 
   1137 #define CIRCLEQ_INSERT_TAIL(head, elm, field) do {			\\
   1138 	(elm)->field.cqe_next = CIRCLEQ_END(head);			\\
   1139 	(elm)->field.cqe_prev = (head)->cqh_last;			\\
   1140 	if ((head)->cqh_first == CIRCLEQ_END(head))			\\
   1141 		(head)->cqh_first = (elm);				\\
   1142 	else								\\
   1143 		(head)->cqh_last->field.cqe_next = (elm);		\\
   1144 	(head)->cqh_last = (elm);					\\
   1145 } while (0)
   1146 
   1147 #define	CIRCLEQ_REMOVE(head, elm, field) do {				\\
   1148 	if ((elm)->field.cqe_next == CIRCLEQ_END(head))			\\
   1149 		(head)->cqh_last = (elm)->field.cqe_prev;		\\
   1150 	else								\\
   1151 		(elm)->field.cqe_next->field.cqe_prev =			\\
   1152 		    (elm)->field.cqe_prev;				\\
   1153 	if ((elm)->field.cqe_prev == CIRCLEQ_END(head))			\\
   1154 		(head)->cqh_first = (elm)->field.cqe_next;		\\
   1155 	else								\\
   1156 		(elm)->field.cqe_prev->field.cqe_next =			\\
   1157 		    (elm)->field.cqe_next;				\\
   1158 	_Q_INVALIDATE((elm)->field.cqe_prev);				\\
   1159 	_Q_INVALIDATE((elm)->field.cqe_next);				\\
   1160 } while (0)
   1161 
   1162 #define CIRCLEQ_REPLACE(head, elm, elm2, field) do {			\\
   1163 	if (((elm2)->field.cqe_next = (elm)->field.cqe_next) ==		\\
   1164 	    CIRCLEQ_END(head))						\\
   1165 		(head).cqh_last = (elm2);				\\
   1166 	else								\\
   1167 		(elm2)->field.cqe_next->field.cqe_prev = (elm2);	\\
   1168 	if (((elm2)->field.cqe_prev = (elm)->field.cqe_prev) ==		\\
   1169 	    CIRCLEQ_END(head))						\\
   1170 		(head).cqh_first = (elm2);				\\
   1171 	else								\\
   1172 		(elm2)->field.cqe_prev->field.cqe_next = (elm2);	\\
   1173 	_Q_INVALIDATE((elm)->field.cqe_prev);				\\
   1174 	_Q_INVALIDATE((elm)->field.cqe_next);				\\
   1175 } while (0)
   1176 #endif	/* !_FAKE_QUEUE_H_ */
   1177 
   1178 !!
   1179 fi
   1180 
   1181 echo "config.h: written" 1>&2
   1182 echo "config.h: written" 1>&3
   1183 
   1184 #----------------------------------------------------------------------
   1185 # Now we go to generate our Makefile.configure.
   1186 # This file is simply a bunch of Makefile variables.
   1187 # They'll work in both GNUmakefile and BSDmakefile.
   1188 # You MIGHT want to change this.
   1189 #----------------------------------------------------------------------
   1190 
   1191 exec > Makefile.configure
   1192 
   1193 [ -z "${BINDIR}"     ] && BINDIR="${PREFIX}/bin"
   1194 [ -z "${SBINDIR}"    ] && SBINDIR="${PREFIX}/sbin"
   1195 [ -z "${INCLUDEDIR}" ] && INCLUDEDIR="${PREFIX}/include"
   1196 [ -z "${LIBDIR}"     ] && LIBDIR="${PREFIX}/lib"
   1197 [ -z "${MANDIR}"     ] && MANDIR="${PREFIX}/man"
   1198 [ -z "${SHAREDIR}"   ] && SHAREDIR="${PREFIX}/share"
   1199 
   1200 [ -z "${INSTALL_PROGRAM}" ] && INSTALL_PROGRAM="${INSTALL} -m 0555"
   1201 [ -z "${INSTALL_LIB}"     ] && INSTALL_LIB="${INSTALL} -m 0444"
   1202 [ -z "${INSTALL_MAN}"     ] && INSTALL_MAN="${INSTALL} -m 0444"
   1203 [ -z "${INSTALL_DATA}"    ] && INSTALL_DATA="${INSTALL} -m 0444"
   1204 
   1205 cat << __HEREDOC__
   1206 CC		= ${CC}
   1207 CFLAGS		= ${CFLAGS}
   1208 CPPFLAGS	= ${CPPFLAGS}
   1209 LDADD		= ${LDADD}
   1210 LDFLAGS		= ${LDFLAGS}
   1211 STATIC		= ${STATIC}
   1212 PREFIX		= ${PREFIX}
   1213 BINDIR		= ${BINDIR}
   1214 SHAREDIR	= ${SHAREDIR}
   1215 SBINDIR		= ${SBINDIR}
   1216 INCLUDEDIR	= ${INCLUDEDIR}
   1217 LIBDIR		= ${LIBDIR}
   1218 MANDIR		= ${MANDIR}
   1219 INSTALL		= ${INSTALL}
   1220 INSTALL_PROGRAM	= ${INSTALL_PROGRAM}
   1221 INSTALL_LIB	= ${INSTALL_LIB}
   1222 INSTALL_MAN	= ${INSTALL_MAN}
   1223 INSTALL_DATA	= ${INSTALL_DATA}
   1224 __HEREDOC__
   1225 
   1226 echo "Makefile.configure: written" 1>&2
   1227 echo "Makefile.configure: written" 1>&3
   1228 
   1229 exit 0