stagit

static git page generator
git clone git://git.sgregoratto.me/stagit
Log | Files | Refs | README | LICENSE

configure (38136B)


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