[T106][ZXW-22]7520V3SCV2.01.01.02P42U09_VEC_V0.8_AP_VEC origin source commit
Change-Id: Ic6e05d89ecd62fc34f82b23dcf306c93764aec4b
diff --git a/ap/build/uClibc/extra/scripts/MAKEALL b/ap/build/uClibc/extra/scripts/MAKEALL
new file mode 100755
index 0000000..f46d3bd
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/MAKEALL
@@ -0,0 +1,146 @@
+#!/bin/sh
+#
+# helper script to quick build testing with cross-compilers
+#
+
+: ${MAKE:=make}
+
+: ${BUILD_NCPUS:=$(getconf _NPROCESSORS_ONLN)}
+if [ "$BUILD_NCPUS" -gt 1 ] ; then
+ JOBS=-j$((BUILD_NCPUS + 1))
+else
+ JOBS=""
+fi
+MAKE="${MAKE} ${JOBS}"
+
+: ${CROSS_COMPILE:=${CROSS_COMPILER_PREFIX}}
+
+setconfig()
+{
+ local opt=$1
+ shift
+ case $1 in
+ [yn]) ;;
+ [0-9]*) ;;
+ *) set -- '"'$*'"'
+ esac
+ sed -i \
+ -e "/${opt}=/s:=.*:=$*:" \
+ .config
+ echo " ## setconfig ${opt} $*"
+}
+
+get_arches()
+{
+ case $1 in
+ hppa) echo hppa hppa2.0 hppa1.1 hppa1.0;;
+ i386) echo i386 i486 i586 i686;;
+ sh) echo sh4 sh2 sh3 sh1 sh;;
+ sparc) echo sparc sparc64;;
+ *) echo $1;;
+ esac
+}
+
+find_compiler()
+{
+ local t a v o l
+ for a in $(get_arches $1) ; do
+ for l in uclibc gnu gnueabi "" ; do
+ for v in unknown pc gentoo "" ; do
+ for o in linux uclinux "" ; do
+ t="${a}${v:+-${v}}${o:+-${o}}${l:+-${l}}"
+ if ${t}-gcc --help > /dev/null 2>&1 ; then
+ echo ${t}-
+ return 0
+ fi
+ done
+ done
+ done
+ done
+}
+
+do_make()
+{
+ echo " ## ${MAKE} -s $*"
+ ${MAKE} -s "$@"
+}
+
+mark_arch()
+{
+ local r=$1 a=$2
+ eval $r=\"\$$r $a\"
+}
+
+if [ -z "$*" ] ; then
+ set -- $(awk \
+ '$0 ~ /^config TARGET_/ { sub("TARGET_",""); print $NF }' \
+ extra/Configs/Config.in | grep -v SUBARCH)
+fi
+pass=""
+fail=""
+skip=""
+for a in "$@" ; do
+ if [ -z "${CROSS_COMPILE}" ] ; then
+ CROSS_COMPILE=$(find_compiler ${a})
+ fi
+
+ if [ -z "${CROSS_COMPILE}" ] ; then
+ mark_arch skip $a
+ echo " ### SKIP: ${a}: could not find compiler"
+ continue
+ fi
+
+ rm -f ${a}.log ${a}.fail
+ (
+ set -e
+
+ echo " ### Building target ${a} (${CROSS_COMPILE})"
+
+ do_make distclean
+ do_make ARCH=$a defconfig
+ do_make oldconfig
+
+ setconfig CROSS_COMPILER_PREFIX ${CROSS_COMPILE}
+
+ header_path=${KERNEL_HEADERS:-$(echo '#include <linux/version.h>' | ${CROSS_COMPILE}cpp 2>/dev/null | grep -o '[^"]*linux/version.h')} || :
+ if [ -z "${header_path}" ] ; then
+ for p in /usr/${CROSS_COMPILE%-}/usr/include ; do
+ if [ -e ${p}/linux/version.h ] ; then
+ header_path=${p}
+ break
+ fi
+ done
+ if [ -z "${header_path}" ] ; then
+ echo " ## unable to locate KERNEL_HEADERS"
+ fi
+ fi
+ setconfig KERNEL_HEADERS ${header_path%/linux/version.h}
+
+ if do_make ; then
+ echo " ## PASS"
+ else
+ echo " ## FAIL"
+ touch ${a}.fail
+ fi
+ ) 2>&1 | tee ${a}.log
+
+ if [ -e ${a}.fail ] ; then
+ rm -f ${a}.fail
+ mark_arch fail $a
+ else
+ mark_arch pass $a
+ fi
+
+ unset CROSS_COMPILE
+done
+
+if [ -n "${skip}" ] ; then
+ printf '\nSKIPPED: %s\n' "${skip}"
+fi
+if [ -n "${fail}" ] ; then
+ printf '\nPASSED: %s\nFAILED: %s\n\n' "${pass}" "${fail}"
+ exit 1
+else
+ printf '\nAll arches passed!\n\n'
+ exit 0
+fi
diff --git a/ap/build/uClibc/extra/scripts/Makefile.arch.lvl3 b/ap/build/uClibc/extra/scripts/Makefile.arch.lvl3
new file mode 100644
index 0000000..ac00637
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/Makefile.arch.lvl3
@@ -0,0 +1,13 @@
+# Makefile for uClibc
+#
+# Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
+#
+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
+#
+
+top_srcdir=../../../
+top_builddir=../../../
+all: objs
+include $(top_builddir)Rules.mak
+include Makefile.arch
+include $(top_srcdir)Makerules
diff --git a/ap/build/uClibc/extra/scripts/Makefile.arch.lvl4 b/ap/build/uClibc/extra/scripts/Makefile.arch.lvl4
new file mode 100644
index 0000000..633c91f
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/Makefile.arch.lvl4
@@ -0,0 +1,13 @@
+# Makefile for uClibc
+#
+# Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
+#
+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
+#
+
+top_srcdir=../../../../
+top_builddir=../../../../
+all: objs
+include $(top_builddir)Rules.mak
+include Makefile.arch
+include $(top_srcdir)Makerules
diff --git a/ap/build/uClibc/extra/scripts/Makefile.libs.lvl0 b/ap/build/uClibc/extra/scripts/Makefile.libs.lvl0
new file mode 100644
index 0000000..4709fc2
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/Makefile.libs.lvl0
@@ -0,0 +1,13 @@
+# Makefile for uClibc
+#
+# Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
+#
+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
+#
+
+top_srcdir=./
+top_builddir=./
+#include $(top_builddir)Rules.mak
+#all: libs
+include Makefile.in
+include $(top_srcdir)Makerules
diff --git a/ap/build/uClibc/extra/scripts/Makefile.libs.lvl1 b/ap/build/uClibc/extra/scripts/Makefile.libs.lvl1
new file mode 100644
index 0000000..c8dc9b4
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/Makefile.libs.lvl1
@@ -0,0 +1,13 @@
+# Makefile for uClibc
+#
+# Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
+#
+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
+#
+
+top_srcdir=../
+top_builddir=../
+include $(top_builddir)Rules.mak
+all: libs
+include Makefile.in
+include $(top_srcdir)Makerules
diff --git a/ap/build/uClibc/extra/scripts/Makefile.libs.lvl2 b/ap/build/uClibc/extra/scripts/Makefile.libs.lvl2
new file mode 100644
index 0000000..f910021
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/Makefile.libs.lvl2
@@ -0,0 +1,13 @@
+# Makefile for uClibc
+#
+# Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
+#
+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
+#
+
+top_srcdir=../../
+top_builddir=../../
+include $(top_builddir)Rules.mak
+all: libs
+include Makefile.in
+include $(top_srcdir)Makerules
diff --git a/ap/build/uClibc/extra/scripts/Makefile.objs.lvl2 b/ap/build/uClibc/extra/scripts/Makefile.objs.lvl2
new file mode 100644
index 0000000..11f362a
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/Makefile.objs.lvl2
@@ -0,0 +1,13 @@
+# Makefile for uClibc
+#
+# Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
+#
+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
+#
+
+top_srcdir=../../
+top_builddir=../../
+all: objs
+include $(top_builddir)Rules.mak
+include Makefile.in
+include $(top_srcdir)Makerules
diff --git a/ap/build/uClibc/extra/scripts/Makefile.objs.lvl3 b/ap/build/uClibc/extra/scripts/Makefile.objs.lvl3
new file mode 100644
index 0000000..4a8f4a0
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/Makefile.objs.lvl3
@@ -0,0 +1,13 @@
+# Makefile for uClibc
+#
+# Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
+#
+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
+#
+
+top_srcdir=../../../
+top_builddir=../../../
+all: objs
+include $(top_builddir)Rules.mak
+include Makefile.in
+include $(top_srcdir)Makerules
diff --git a/ap/build/uClibc/extra/scripts/Makefile.objs.lvl4 b/ap/build/uClibc/extra/scripts/Makefile.objs.lvl4
new file mode 100644
index 0000000..3ed177a
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/Makefile.objs.lvl4
@@ -0,0 +1,13 @@
+# Makefile for uClibc
+#
+# Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
+#
+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
+#
+
+top_srcdir=../../../../
+top_builddir=../../../../
+all: objs
+include $(top_builddir)Rules.mak
+include Makefile.in
+include $(top_srcdir)Makerules
diff --git a/ap/build/uClibc/extra/scripts/Makefile.utils.lvl1 b/ap/build/uClibc/extra/scripts/Makefile.utils.lvl1
new file mode 100644
index 0000000..bdaea9f
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/Makefile.utils.lvl1
@@ -0,0 +1,13 @@
+# Makefile for uClibc
+#
+# Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
+#
+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
+#
+
+top_srcdir=../
+top_builddir=../
+include $(top_builddir)Rules.mak
+all: utils
+include Makefile.in
+include $(top_srcdir)Makerules
diff --git a/ap/build/uClibc/extra/scripts/conf-header.sh b/ap/build/uClibc/extra/scripts/conf-header.sh
new file mode 100755
index 0000000..8044f8c
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/conf-header.sh
@@ -0,0 +1,27 @@
+#!/bin/sh -e
+
+# Turn .config into a header file
+
+if [ -z "$1" ] ; then
+ echo "Usage: conf-header.sh <.config>"
+ exit 1
+fi
+
+cat <<EOF
+#if !defined _FEATURES_H && !defined __need_uClibc_config_h
+# error Never include <bits/uClibc_config.h> directly; use <features.h> instead
+#endif
+
+#define __UCLIBC_MAJOR__ ${MAJOR_VERSION}
+#define __UCLIBC_MINOR__ ${MINOR_VERSION}
+#define __UCLIBC_SUBLEVEL__ ${SUBLEVEL}
+EOF
+
+exec \
+sed \
+ -e '/^#$/d' \
+ -e '/^[^#]/s:^\([^=]*\)=\(.*\):#define __\1__ \2:' \
+ -e '/^#define /s: y$: 1:' \
+ -e '/^# .* is not set$/s:^# \(.*\) is not set$:#undef __\1__:' \
+ -e 's:^# \(.*\)$:/* \1 */:' \
+ $1
diff --git a/ap/build/uClibc/extra/scripts/cppcheck.sh b/ap/build/uClibc/extra/scripts/cppcheck.sh
new file mode 100755
index 0000000..127c509
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/cppcheck.sh
@@ -0,0 +1,56 @@
+#! /bin/sh
+
+# usage:
+#
+# make \
+# REAL_CC=gcc-mine \
+# CC=extra/scripts/cppcheck.sh \
+# CPPCHECK_FLAGS="--enable=style,performance,portability,information,missingInclude --max-configs=256 -j $(($(getconf _NPROCESSORS_ONLN)-1))" \
+# CPPCHECK_LIMIT="yes"
+
+# CPPCHECK_FLAGS are optional and are not set per default.
+# CPPCHECK_LIMIT limits cppcheck to the -D and -U that would be passed to CC.
+# Setting CPPCHECK_LIMIT greatly improves the check-time but obviously
+# just checks a small subset of the defines found in a file.
+
+: ${REAL_CC:=gcc}
+${REAL_CC} $@
+args=""
+limits=""
+next_arg=0
+next_limit=0
+
+for i in $@
+do
+ if [ $next_arg -eq 1 ] ; then
+ next_arg=0
+ case "/$i" in
+ /-*) exit 0 ;;
+ esac
+ [ "x$args" = "x" ] && args="$i" || args="$args $i"
+ continue
+ fi
+ if [ $next_limit -eq 1 ] ; then
+ next_limit=0
+ [ "x$limits" = "x" ] && limits="$i" || limits="$limits $i"
+ continue
+ fi
+ case "/$i" in
+ /-c) next_arg=1 ;;
+ /-isystem)
+ next_arg=1;
+ [ "x$args" = "x" ] && args="-I" || args="$args -I" ;;
+ /-I)
+ next_arg=1;
+ [ "x$args" = "x" ] && args="$i" || args="$args $i" ;;
+ /-I*) [ "x$args" = "x" ] && args="$i" || args="$args $i" ;;
+ /-D|/-U)
+ next_limit=1;
+ [ "x$limit" = "x" ] && limit="$i" || limit="$limit $i" ;;
+ /-D*) [ "x$limits" = "x" ] && limits="$i" || limits="$limits $i" ;;
+ /-s|/-S|/-dump*|/--print*|/-print*) exit 0 ;;
+ *) ;;
+ esac
+done
+[ -z "${CPPCHECK_LIMIT}" ] && limits=""
+[ -z "${args}" ] || exec cppcheck ${CPPCHECK_FLAGS} ${args} ${limits}
diff --git a/ap/build/uClibc/extra/scripts/create_makefiles.sh b/ap/build/uClibc/extra/scripts/create_makefiles.sh
new file mode 100755
index 0000000..1d94b0d
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/create_makefiles.sh
@@ -0,0 +1,71 @@
+#!/bin/sh
+#
+# Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
+#
+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
+#
+
+# Creates the necessary Makefiles to build w/ the Makefile.{arch,in} files
+
+DIRS="ldso libc libcrypt libintl libm libnsl libpthread libresolv librt libutil"
+
+if [ ! -f Makerules ] ; then
+ echo "Run this command in top_srcdir"
+ exit 1
+fi
+
+if [ -z "${USE_CMD}" ] ; then
+USE_CMD="cp"
+fi
+
+RM="rm -f"
+${RM} Makefile
+${USE_CMD} extra/scripts/Makefile.libs.lvl0 Makefile
+
+#for x in ${DIRS} ; do
+# find ./${x} -name Makefile -exec rm -f {} \;
+#done
+
+for x in */Makefile.in ; do
+ ${RM} `dirname ${x}`/Makefile
+ ${USE_CMD} extra/scripts/Makefile.libs.lvl1 `dirname ${x}`/Makefile
+done
+
+for x in utils/Makefile.in ; do
+ ${RM} `dirname ${x}`/Makefile
+ ${USE_CMD} extra/scripts/Makefile.utils.lvl1 `dirname ${x}`/Makefile
+done
+
+for x in */*/Makefile.in ; do
+ ${RM} `dirname ${x}`/Makefile
+ ${USE_CMD} extra/scripts/Makefile.objs.lvl2 `dirname ${x}`/Makefile
+done
+
+# overwrites the earlier ones, we do not add arch specific to libm/arch
+for x in ldso/*/Makefile.in libpthread/*/Makefile.in ; do
+ ${RM} `dirname ${x}`/Makefile
+ ${USE_CMD} extra/scripts/Makefile.libs.lvl2 `dirname ${x}`/Makefile
+done
+
+for x in */*/*/Makefile.in ; do
+ ${RM} `dirname ${x}`/Makefile
+ ${USE_CMD} extra/scripts/Makefile.objs.lvl3 `dirname ${x}`/Makefile
+done
+
+for x in libc/*/*/Makefile.arch ; do
+ ${RM} `dirname ${x}`/Makefile
+ ${USE_CMD} extra/scripts/Makefile.arch.lvl3 `dirname ${x}`/Makefile
+done
+
+for x in */*/*/*/Makefile.in ; do
+ ${RM} `dirname ${x}`/Makefile
+ ${USE_CMD} extra/scripts/Makefile.objs.lvl4 `dirname ${x}`/Makefile
+done
+
+# we do not add these to libpthread/PTNAME/sysdeps/arch
+for x in libc/*/*/*/Makefile.arch ; do
+ ${RM} `dirname ${x}`/Makefile
+ ${USE_CMD} extra/scripts/Makefile.arch.lvl4 `dirname ${x}`/Makefile
+done
+
+exit 0
diff --git a/ap/build/uClibc/extra/scripts/defs.awk b/ap/build/uClibc/extra/scripts/defs.awk
new file mode 100644
index 0000000..1716fb1
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/defs.awk
@@ -0,0 +1,27 @@
+/^[ ]*\.endp/ { need_endp = 1 }
+/^[ ]*\.end/ { need_end = 1 }
+/^[ ]*\.align/ { if($2 > max) max = $2; }
+
+END {
+ if(need_endp)
+ {
+ print "#define END_INIT .endp _init";
+ print "#define END_FINI .endp _fini";
+ } else if(need_end)
+ {
+ print "#define END_INIT .end _init";
+ print "#define END_FINI .end _fini";
+ }
+ else
+ {
+ print "#define END_INIT";
+ print "#define END_FINI";
+ }
+ if(max)
+ print "#define ALIGN .align", max;
+ else
+ print "#define ALIGN";
+
+ print "#include <libc-symbols.h>";
+ #print "weak_extern (__gmon_start__)";
+}
diff --git a/ap/build/uClibc/extra/scripts/format.lds b/ap/build/uClibc/extra/scripts/format.lds
new file mode 100644
index 0000000..c20212e
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/format.lds
@@ -0,0 +1,3 @@
+/* GNU ld script
+ * Use the shared library, but some functions are only in
+ * the static library, so try that secondarily. */
diff --git a/ap/build/uClibc/extra/scripts/gen-as-const.awk b/ap/build/uClibc/extra/scripts/gen-as-const.awk
new file mode 100644
index 0000000..013bd6e
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/gen-as-const.awk
@@ -0,0 +1,33 @@
+# Script used in producing headers of assembly constants from C expressions.
+# The input to this script looks like:
+# #cpp-directive ...
+# NAME1
+# NAME2 expression ...
+# The output of this script is C code to be run through gcc -S and then
+# massaged to extract the integer constant values of the given C expressions.
+# A line giving just a name implies an expression consisting of just that name.
+
+BEGIN { started = 0 }
+
+# cpp directives go straight through.
+/^#/ { print; next }
+
+NF >= 1 && !started {
+ printf "void dummy(void);\n";
+ print "void dummy(void) {";
+ started = 1;
+}
+
+# Separator.
+$1 == "--" { next }
+
+NF == 1 { sub(/^.*$/, "& &"); }
+
+NF > 1 {
+ name = $1;
+ sub(/^[^ ]+[ ]+/, "");
+ printf "__asm__ (\"@@@name@@@%s@@@value@@@%%0@@@end@@@\" : : \"i\" ((long) %s));\n",
+ name, $0;
+}
+
+END { if (started) print "}" }
diff --git a/ap/build/uClibc/extra/scripts/gen_bits_syscall_h.sh b/ap/build/uClibc/extra/scripts/gen_bits_syscall_h.sh
new file mode 100755
index 0000000..f6353ba
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/gen_bits_syscall_h.sh
@@ -0,0 +1,46 @@
+#!/bin/sh
+#
+# Copyright (C) 2001 Manuel Novoa III <mjn3@uclibc.org>
+# Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
+#
+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
+#
+
+# June 27, 2001 Manuel Novoa III
+#
+# This script expects top_builddir and CC (as used in the Makefiles) to be set
+# in the environment, and outputs the appropriate
+# $top_builddir/include/bits/sysnum.h # corresponding to
+# $top_builddir/include/asm/unistd.h to stdout.
+#
+# Warning!!! This does _no_ error checking!!!
+
+INCLUDE_OPTS="-nostdinc -I${KERNEL_HEADERS}"
+
+case $CC in
+*icc*) CC_SYSNUM_ARGS="-dM" ;;
+*) CC_SYSNUM_ARGS="-dN" ;;
+esac
+
+( echo "#include <asm/unistd.h>";
+ echo "#include <asm/unistd.h>" |
+ $CC -E $CC_SYSNUM_ARGS $INCLUDE_OPTS - |
+ sed -ne 's/^[ ]*#define[ ]*\(__ARM_NR_\|__NR_\)\([A-Za-z0-9_]*\).*/UCLIBC\1\2 \1\2/gp' \
+ -e 's/^[ ]*#undef[ ]*\(__ARM_NR_\|__NR_\)\([A-Za-z0-9_]*\).*/UNDEFUCLIBC\1\2 \1\2/gp' # needed to strip out any kernel-internal defines
+) |
+$CC -E $INCLUDE_OPTS - |
+( echo "/* WARNING!!! AUTO-GENERATED FILE!!! DO NOT EDIT!!! */" ;
+ echo ;
+ echo "#ifndef _BITS_SYSNUM_H" ;
+ echo "#define _BITS_SYSNUM_H" ;
+ echo ;
+ echo "#ifndef _SYSCALL_H" ;
+ echo "# error \"Never use <bits/sysnum.h> directly; include <sys/syscall.h> instead.\"" ;
+ echo "#endif" ; echo ;
+ sed -ne 's/^UCLIBC\(__ARM_NR_\|__NR_\)\([A-Za-z0-9_]*\) *\(.*\)/#undef \1\2\
+#define \1\2 \3\
+#define SYS_\2 \1\2/gp' \
+ -e 's/^UNDEFUCLIBC\(__ARM_NR_\|__NR_\)\([A-Za-z0-9_]*\).*/#undef \1\2/gp'
+ echo ;
+ echo "#endif" ;
+)
diff --git a/ap/build/uClibc/extra/scripts/getent b/ap/build/uClibc/extra/scripts/getent
new file mode 100755
index 0000000..30d515b
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/getent
@@ -0,0 +1,43 @@
+#!/bin/sh
+# $Header: /var/cvs/uClibc/extra/scripts/getent,v 1.2 2005/02/02 14:18:01 solar Exp $
+#
+# Closely (not perfectly) emulate the behavior of glibc's getent utility
+#
+#passwd|shadow|group|aliases|hosts|networks|ethers|netgroup|protocols|services|rpc
+# only returns the first match (by design)
+# dns based search is not supported (hosts,networks)
+# case-insensitive matches not supported (ethers; others?)
+# may return false-positives (hosts,protocols,rpc,services,ethers)
+
+export PATH="${PATH}:/bin:/usr/bin"
+
+file="/etc/$1"
+case $1 in
+ passwd|group)
+ match="^$2:\|^[^:]*:[^:]*:$2:" ;;
+ shadow)
+ match="^$2:" ;;
+ networks|netgroup)
+ match="^[[:space:]]*$2\>" ;;
+ hosts|protocols|rpc|services|ethers)
+ match="\<$2\>" ;;
+ aliases)
+ match="^[[:space:]]*$2[[:space:]]*:" ;;
+ ""|-h|--help)
+ echo "USAGE: $0 database [key]"
+ exit 0 ;;
+ *)
+ echo "$0: Unknown database: $1" 1>&2
+ exit 1 ;;
+esac
+
+if [ ! -f "$file" ] ; then
+ echo "$0: Could not find database file for $1" 1>&2
+ exit 1
+fi
+
+if [ $# -eq 1 ] ; then
+ exec cat "$file"
+else
+ sed "s/#.*//; /$match/q; d" "$file" | grep . || exit 2
+fi
diff --git a/ap/build/uClibc/extra/scripts/install_headers.sh b/ap/build/uClibc/extra/scripts/install_headers.sh
new file mode 100755
index 0000000..14d64dc
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/install_headers.sh
@@ -0,0 +1,76 @@
+#!/bin/sh
+# Parameters:
+# $1 = source dir
+# $2 = dst dir
+# $top_builddir = well you guessed it
+
+srcdir=${1:-include}
+dstdir=${2:-`. ./.config 2>/dev/null && echo ${DEVEL_PREFIX}/include`}
+: ${top_builddir:=.}
+
+die_if_not_dir()
+{
+ for dir in "$@"; do
+ test -d "$dir" && continue
+ echo "Error: '$dir' is not a directory"
+ exit 1
+ done
+}
+
+
+# Ensure that created dirs/files have 755/644 perms
+umask 022
+
+
+# Sanity tests
+die_if_not_dir "${srcdir}"
+mkdir -p "${dstdir}" 2>/dev/null
+die_if_not_dir "${dstdir}"
+die_if_not_dir "$top_builddir"
+if ! test -x "$top_builddir/extra/scripts/unifdef"; then
+ echo "Error: need '$top_builddir/extra/scripts/unifdef' executable"
+ exit 1
+fi
+
+
+# Sanitize and copy uclibc headers
+(
+# We must cd, or else we'll prepend "${srcdir}" to filenames!
+cd "${srcdir}" || exit 1
+find . ! -name '.' -a ! -path '*/.*' | sed -e 's/^\.\///' -e '/^config\//d' \
+ -e '/^config$/d'
+) | \
+(
+IFS=''
+while read -r filename; do
+ if test -d "${srcdir}/$filename"; then
+ mkdir -p "${dstdir}/$filename" 2>/dev/null
+ continue
+ fi
+ if test x"${filename##libc-*.h}" = x""; then
+ # Do not install libc-XXXX.h files
+ continue
+ fi
+ # NB: unifdef exits with 1 if output is not
+ # exactly the same as input. That's ok.
+ # Do not abort the script if unifdef "fails"!
+ # NB2: careful with sed command arguments, they contain tab character
+ "$top_builddir/extra/scripts/unifdef" \
+ -U_LIBC \
+ -U__UCLIBC_GEN_LOCALE \
+ -U__NO_CTYPE \
+ "${srcdir}/$filename" \
+ | sed -e '/^rtld_hidden_proto[ ]*([a-zA-Z0-9_]*)$/d' \
+ | sed -e '/^lib\(c\|m\|resolv\|dl\|intl\|rt\|nsl\|util\|crypt\|pthread\)_hidden_proto[ ]*([a-zA-Z0-9_]*)$/d' \
+ >"${dstdir}/$filename"
+done
+)
+
+
+# Fix mode/owner bits
+cd "${dstdir}" || exit 1
+chmod -R u=rwX,go=rX . >/dev/null 2>&1
+chown -R `id | sed 's/^uid=\([0-9]*\).*gid=\([0-9]*\).*$/\1:\2/'` . >/dev/null 2>&1
+
+# ignore errors on unrelated files
+exit 0
diff --git a/ap/build/uClibc/extra/scripts/randconfig.sh b/ap/build/uClibc/extra/scripts/randconfig.sh
new file mode 100755
index 0000000..4c7126b
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/randconfig.sh
@@ -0,0 +1,44 @@
+#!/bin/sh
+
+# build random configurations
+# Usage:
+# ARCH=i386 nohup ./extra/scripts/randconfig.sh & sleep 1800 && touch STOP
+#
+# The above builds random i386 configs and automatically stops after 30 minutes
+
+test "x$AWK" = "x" && AWK=awk
+test "x$ARCH" = "x" && ARCH=`uname -m`
+KCONFIG_ALLCONFIG=.config.allconfig
+(echo TARGET_$ARCH=y
+ echo '# UCLIBC_PREGENERATED_LOCALE_DATA is not set'
+ echo '# DOMULTI is not set'
+ echo '# UCLIBC_DOWNLOAD_PREGENERATED_LOCALE_DATA is not set'
+) > $KCONFIG_ALLCONFIG
+export KCONFIG_ALLCONFIG
+
+if test "x$NCPU" = "x"
+then
+ test -r /proc/cpuinfo && \
+ eval `$AWK 'BEGIN{NCPU=0}
+/processor/{let NCPU++}
+END{if (NCPU<1) {NCPU=1}; print("NCPU="NCPU);}' /proc/cpuinfo` || \
+ NCPU=1
+fi
+MAKELEVEL="-j$NCPU"
+i=0
+while test ! -f STOP
+do
+ ARCH=$ARCH make $* randconfig > /dev/null
+ ARCH=$ARCH make $* silentoldconfig > /dev/null
+ if (make $MAKELEVEL $*) 2>&1 >& mk.log
+ then
+ :
+ else
+ i=`expr $i + 1`
+ num=`printf "%.5d" $i`
+ mv .config FAILED.$num.config
+ mv mk.log FAILED.$num.log
+ fi
+ make distclean > /dev/null || true
+done
+rm -f STOP
diff --git a/ap/build/uClibc/extra/scripts/relative_path.sh b/ap/build/uClibc/extra/scripts/relative_path.sh
new file mode 100755
index 0000000..4dddefa
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/relative_path.sh
@@ -0,0 +1,47 @@
+#! /bin/sh
+#
+# Copyright 2003 Alexandre Oliva <aoliva@redhat.com>
+# Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
+#
+# Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
+#
+
+# This script computes a relative pathname from $1 to $2
+# They are assumed to not contain . or .. pathname components,
+# but if both directories exist and cd/pwd canonicalizes pathnames,
+# this shouldn't matter. PWD_CMD may be set to some pwd command that does.
+
+from=`(cd $1 > /dev/null && ${PWD_CMD-pwd} || echo $1) 2>/dev/null | sed 's,//*,/,g;s,/*$,,'`
+target=`(cd $2 > /dev/null && ${PWD_CMD-pwd} || echo $2) 2>/dev/null | sed 's,//*,/,g;s,/*$,,'`
+
+case $from in /* | "") ;; *) from=`${PWD_CMD-pwd}`/$from ;; esac
+case $target in /* | "") ;; *) target=`${PWD_CMD-pwd}`/$target ;; esac
+
+case $target in
+"$from" | "$from/"*)
+ dots=`echo $from | sed s,.,.,g`
+ echo $target | sed "s,^$dots/*,,;s,[^/]$,&/,"
+ exit 0
+ ;;
+esac
+
+case $from in
+"$target/"*)
+ dots=`echo $target | sed s,.,.,g`
+ echo $from/ | sed "s,^$dots/*,,;s,[^/]$,&/,;s,[^/]*/*,../,g;s,[^/]$,&/,"
+ exit 0
+ ;;
+esac
+
+# Without trailing slash, from=/usr/lib and target=/uclibc/lib
+# mistakenly concludes that prefix=/u
+#prefix=`echo $from///$target | sed 's,\(\(/[^/]*\)*\).*///\1.*,\1,'`
+prefix=`echo $from///$target | sed 's,\(\(/[^/]*\)*/\).*///\1.*,\1,'`
+dots=`echo $prefix | sed s,.,.,g`
+from=`echo $from | sed "s,^$dots,,"`
+target=`echo $target | sed "s,^$dots,,"`
+
+from=`echo $from | sed 's,[^/][^/]*,..,g;s,.$,&/,'`
+echo ${from}$target/
+
+exit 0
diff --git a/ap/build/uClibc/extra/scripts/relinfo.pl b/ap/build/uClibc/extra/scripts/relinfo.pl
new file mode 100755
index 0000000..ec4a5df
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/relinfo.pl
@@ -0,0 +1,78 @@
+#! /usr/bin/perl
+eval "exec /usr/bin/perl -S $0 $*"
+ if 0;
+# Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 Red Hat, Inc.
+# Written by Ulrich Drepper <drepper@redhat.com>, 2000.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 2 as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
+
+for ($cnt = 0; $cnt <= $#ARGV; ++$cnt) {
+ $relent = 0;
+ $relsz = 0;
+ $relcount = 0;
+ $pltrelsz = 0;
+ $extplt = 0;
+ $users = 0;
+
+ open (READLINK, "readlink -f $ARGV[$cnt] |") || die "cannot open readlink";
+ while (<READLINK>) {
+ chop;
+ $fullpath = $_;
+ }
+ close (READLINK);
+
+ open (READELF, "eu-readelf -d $ARGV[$cnt] |") || die "cannot open $ARGV[$cnt]";
+ while (<READELF>) {
+ chop;
+ if (/.* RELA?ENT *([0-9]*).*/) {
+ $relent = $1 + 0;
+ } elsif (/.* RELA?SZ *([0-9]*).*/) {
+ $relsz = $1 + 0;
+ } elsif (/.* RELA?COUNT *([0-9]*).*/) {
+ $relcount = $1 + 0;
+ } elsif (/.* PLTRELSZ *([0-9]*).*/) {
+ $pltrelsz = $1 + 0;
+ }
+ }
+ close (READELF);
+
+ open (READELF, "eu-readelf -r $ARGV[$cnt] | sed '/'.gnu.conflict'/,/^\$/d' |") || die "cannot open $ARGV[$cnt]";
+ while (<READELF>) {
+ chop;
+ if (/.*JU?MP_SLOT *0+ .*/) {
+ ++$extplt;
+ }
+ }
+ close (READELF);
+
+ if (open (PRELINK, "/usr/sbin/prelink -p 2>/dev/null | fgrep \"$fullpath\" |")) {
+ while (<PRELINK>) {
+ ++$users;
+ }
+ close(PRELINK);
+ } else {
+ $users = -1;
+ }
+
+ printf ("%s: %d relocations, %d relative (%d%%), %d PLT entries, %d for local syms (%d%%)",
+ $ARGV[$cnt], $relent == 0 ? 0 : $relsz / $relent, $relcount,
+ $relent == 0 ? 0 : ($relcount * 100) / ($relsz / $relent),
+ $relent == 0 ? 0 : $pltrelsz / $relent,
+ $relent == 0 ? 0 : $pltrelsz / $relent - $extplt,
+ $relent == 0 ? 0 : (($pltrelsz / $relent - $extplt) * 100) / ($pltrelsz / $relent));
+ if ($users >= 0) {
+ printf(", %d users", $users);
+ }
+ printf("\n");
+}
diff --git a/ap/build/uClibc/extra/scripts/unifdef.c b/ap/build/uClibc/extra/scripts/unifdef.c
new file mode 100644
index 0000000..56abf89
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/unifdef.c
@@ -0,0 +1,1072 @@
+/*
+ * Copyright (c) 2002 - 2005 Tony Finch <dot@dotat.at>. All rights reserved.
+ *
+ * This code is derived from software contributed to Berkeley by Dave Yost.
+ * It was rewritten to support ANSI C by Tony Finch. The original version of
+ * unifdef carried the following copyright notice. None of its code remains
+ * in this version (though some of the names remain).
+ *
+ * Copyright (c) 1985, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+
+#ifndef lint
+#if 0
+static const char copyright[] =
+"@(#) Copyright (c) 1985, 1993\n\
+ The Regents of the University of California. All rights reserved.\n";
+#endif
+#ifdef __IDSTRING
+__IDSTRING(Berkeley, "@(#)unifdef.c 8.1 (Berkeley) 6/6/93");
+__IDSTRING(NetBSD, "$NetBSD: unifdef.c,v 1.8 2000/07/03 02:51:36 matt Exp $");
+__IDSTRING(dotat, "$dotat: things/unifdef.c,v 1.171 2005/03/08 12:38:48 fanf2 Exp $");
+#endif
+#endif /* not lint */
+#ifdef __FBSDID
+__FBSDID("$FreeBSD: /repoman/r/ncvs/src/usr.bin/unifdef/unifdef.c,v 1.20 2005/05/21 09:55:09 ru Exp $");
+#endif
+
+/*
+ * unifdef - remove ifdef'ed lines
+ *
+ * Wishlist:
+ * provide an option which will append the name of the
+ * appropriate symbol after #else's and #endif's
+ * provide an option which will check symbols after
+ * #else's and #endif's to see that they match their
+ * corresponding #ifdef or #ifndef
+ *
+ * The first two items above require better buffer handling, which would
+ * also make it possible to handle all "dodgy" directives correctly.
+ */
+
+#include <errno.h>
+#include <ctype.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+/* Avoid err.h since uClibc can disable these things */
+#define vwarnx(fmt, args) ({ fprintf(stderr, "unifdef: "); vfprintf(stderr, fmt, args); fprintf(stderr, "\n"); })
+#define warnx(fmt, args...) fprintf(stderr, "unifdef: " fmt "\n", ## args)
+#define errx(exit_code, fmt, args...) ({ warnx(fmt, ## args); exit(exit_code); })
+#define err(exit_code, fmt, args...) errx(exit_code, fmt ": %s", ## args, strerror(errno))
+
+/* types of input lines: */
+typedef enum {
+ LT_TRUEI, /* a true #if with ignore flag */
+ LT_FALSEI, /* a false #if with ignore flag */
+ LT_IF, /* an unknown #if */
+ LT_TRUE, /* a true #if */
+ LT_FALSE, /* a false #if */
+ LT_ELIF, /* an unknown #elif */
+ LT_ELTRUE, /* a true #elif */
+ LT_ELFALSE, /* a false #elif */
+ LT_ELSE, /* #else */
+ LT_ENDIF, /* #endif */
+ LT_DODGY, /* flag: directive is not on one line */
+ LT_DODGY_LAST = LT_DODGY + LT_ENDIF,
+ LT_PLAIN, /* ordinary line */
+ LT_EOF, /* end of file */
+ LT_COUNT
+} Linetype;
+
+static char const * const linetype_name[] = {
+ "TRUEI", "FALSEI", "IF", "TRUE", "FALSE",
+ "ELIF", "ELTRUE", "ELFALSE", "ELSE", "ENDIF",
+ "DODGY TRUEI", "DODGY FALSEI",
+ "DODGY IF", "DODGY TRUE", "DODGY FALSE",
+ "DODGY ELIF", "DODGY ELTRUE", "DODGY ELFALSE",
+ "DODGY ELSE", "DODGY ENDIF",
+ "PLAIN", "EOF"
+};
+
+/* state of #if processing */
+typedef enum {
+ IS_OUTSIDE,
+ IS_FALSE_PREFIX, /* false #if followed by false #elifs */
+ IS_TRUE_PREFIX, /* first non-false #(el)if is true */
+ IS_PASS_MIDDLE, /* first non-false #(el)if is unknown */
+ IS_FALSE_MIDDLE, /* a false #elif after a pass state */
+ IS_TRUE_MIDDLE, /* a true #elif after a pass state */
+ IS_PASS_ELSE, /* an else after a pass state */
+ IS_FALSE_ELSE, /* an else after a true state */
+ IS_TRUE_ELSE, /* an else after only false states */
+ IS_FALSE_TRAILER, /* #elifs after a true are false */
+ IS_COUNT
+} Ifstate;
+
+static char const * const ifstate_name[] = {
+ "OUTSIDE", "FALSE_PREFIX", "TRUE_PREFIX",
+ "PASS_MIDDLE", "FALSE_MIDDLE", "TRUE_MIDDLE",
+ "PASS_ELSE", "FALSE_ELSE", "TRUE_ELSE",
+ "FALSE_TRAILER"
+};
+
+/* state of comment parser */
+typedef enum {
+ NO_COMMENT = false, /* outside a comment */
+ C_COMMENT, /* in a comment like this one */
+ CXX_COMMENT, /* between // and end of line */
+ STARTING_COMMENT, /* just after slash-backslash-newline */
+ FINISHING_COMMENT, /* star-backslash-newline in a C comment */
+ CHAR_LITERAL, /* inside '' */
+ STRING_LITERAL /* inside "" */
+} Comment_state;
+
+static char const * const comment_name[] = {
+ "NO", "C", "CXX", "STARTING", "FINISHING", "CHAR", "STRING"
+};
+
+/* state of preprocessor line parser */
+typedef enum {
+ LS_START, /* only space and comments on this line */
+ LS_HASH, /* only space, comments, and a hash */
+ LS_DIRTY /* this line can't be a preprocessor line */
+} Line_state;
+
+static char const * const linestate_name[] = {
+ "START", "HASH", "DIRTY"
+};
+
+/*
+ * Minimum translation limits from ISO/IEC 9899:1999 5.2.4.1
+ */
+#define MAXDEPTH 64 /* maximum #if nesting */
+#define MAXLINE 4096 /* maximum length of line */
+#define MAXSYMS 4096 /* maximum number of symbols */
+
+/*
+ * Sometimes when editing a keyword the replacement text is longer, so
+ * we leave some space at the end of the tline buffer to accommodate this.
+ */
+#define EDITSLOP 10
+
+/*
+ * Globals.
+ */
+
+static bool complement; /* -c: do the complement */
+static bool debugging; /* -d: debugging reports */
+static bool iocccok; /* -e: fewer IOCCC errors */
+static bool killconsts; /* -k: eval constant #ifs */
+static bool lnblank; /* -l: blank deleted lines */
+static bool lnnum; /* -n: add #line directives */
+static bool symlist; /* -s: output symbol list */
+static bool text; /* -t: this is a text file */
+
+static const char *symname[MAXSYMS]; /* symbol name */
+static const char *value[MAXSYMS]; /* -Dsym=value */
+static bool ignore[MAXSYMS]; /* -iDsym or -iUsym */
+static int nsyms; /* number of symbols */
+
+static FILE *input; /* input file pointer */
+static const char *filename; /* input file name */
+static int linenum; /* current line number */
+
+static char tline[MAXLINE+EDITSLOP];/* input buffer plus space */
+static char *keyword; /* used for editing #elif's */
+
+static Comment_state incomment; /* comment parser state */
+static Line_state linestate; /* #if line parser state */
+static Ifstate ifstate[MAXDEPTH]; /* #if processor state */
+static bool ignoring[MAXDEPTH]; /* ignore comments state */
+static int stifline[MAXDEPTH]; /* start of current #if */
+static int depth; /* current #if nesting */
+static int delcount; /* count of deleted lines */
+static bool keepthis; /* don't delete constant #if */
+
+static int exitstat; /* program exit status */
+
+static void addsym(bool, bool, char *);
+static void debug(const char *, ...);
+static void done(void);
+static void error(const char *);
+static int findsym(const char *);
+static void flushline(bool);
+static Linetype get_line(void);
+static Linetype ifeval(const char **);
+static void ignoreoff(void);
+static void ignoreon(void);
+static void keywordedit(const char *);
+static void nest(void);
+static void process(void);
+static const char *skipcomment(const char *);
+static const char *skipsym(const char *);
+static void state(Ifstate);
+static int strlcmp(const char *, const char *, size_t);
+static void unnest(void);
+static void usage(void);
+
+#define endsym(c) (!isalpha((unsigned char)c) && !isdigit((unsigned char)c) && c != '_')
+
+/*
+ * The main program.
+ */
+int
+main(int argc, char *argv[])
+{
+ int opt;
+
+ while ((opt = getopt(argc, argv, "i:D:U:I:cdeklnst")) != -1)
+ switch (opt) {
+ case 'i': /* treat stuff controlled by these symbols as text */
+ /*
+ * For strict backwards-compatibility the U or D
+ * should be immediately after the -i but it doesn't
+ * matter much if we relax that requirement.
+ */
+ opt = *optarg++;
+ if (opt == 'D')
+ addsym(true, true, optarg);
+ else if (opt == 'U')
+ addsym(true, false, optarg);
+ else
+ usage();
+ break;
+ case 'D': /* define a symbol */
+ addsym(false, true, optarg);
+ break;
+ case 'U': /* undef a symbol */
+ addsym(false, false, optarg);
+ break;
+ case 'I':
+ /* no-op for compatibility with cpp */
+ break;
+ case 'c': /* treat -D as -U and vice versa */
+ complement = true;
+ break;
+ case 'd':
+ debugging = true;
+ break;
+ case 'e': /* fewer errors from dodgy lines */
+ iocccok = true;
+ break;
+ case 'k': /* process constant #ifs */
+ killconsts = true;
+ break;
+ case 'l': /* blank deleted lines instead of omitting them */
+ lnblank = true;
+ break;
+ case 'n': /* add #line directive after deleted lines */
+ lnnum = true;
+ break;
+ case 's': /* only output list of symbols that control #ifs */
+ symlist = true;
+ break;
+ case 't': /* don't parse C comments */
+ text = true;
+ break;
+ default:
+ usage();
+ }
+ argc -= optind;
+ argv += optind;
+ if (argc > 1) {
+ errx(2, "can only do one file");
+ } else if (argc == 1 && strcmp(*argv, "-") != 0) {
+ filename = *argv;
+ input = fopen(filename, "r");
+ if (input == NULL)
+ err(2, "can't open %s", filename);
+ } else {
+ filename = "[stdin]";
+ input = stdin;
+ }
+ process();
+ debug("bug at line %d", __LINE__);
+ abort(); /* bug */
+}
+
+static void
+usage(void)
+{
+ fprintf(stderr, "usage: unifdef [-cdeklnst] [-Ipath]"
+ " [-Dsym[=val]] [-Usym] [-iDsym[=val]] [-iUsym] ... [file]\n");
+ exit(2);
+}
+
+/*
+ * A state transition function alters the global #if processing state
+ * in a particular way. The table below is indexed by the current
+ * processing state and the type of the current line.
+ *
+ * Nesting is handled by keeping a stack of states; some transition
+ * functions increase or decrease the depth. They also maintain the
+ * ignore state on a stack. In some complicated cases they have to
+ * alter the preprocessor directive, as follows.
+ *
+ * When we have processed a group that starts off with a known-false
+ * #if/#elif sequence (which has therefore been deleted) followed by a
+ * #elif that we don't understand and therefore must keep, we edit the
+ * latter into a #if to keep the nesting correct.
+ *
+ * When we find a true #elif in a group, the following block will
+ * always be kept and the rest of the sequence after the next #elif or
+ * #else will be discarded. We edit the #elif into a #else and the
+ * following directive to #endif since this has the desired behaviour.
+ *
+ * "Dodgy" directives are split across multiple lines, the most common
+ * example being a multi-line comment hanging off the right of the
+ * directive. We can handle them correctly only if there is no change
+ * from printing to dropping (or vice versa) caused by that directive.
+ * If the directive is the first of a group we have a choice between
+ * failing with an error, or passing it through unchanged instead of
+ * evaluating it. The latter is not the default to avoid questions from
+ * users about unifdef unexpectedly leaving behind preprocessor directives.
+ */
+typedef void state_fn(void);
+
+/* report an error */
+static void Eelif (void) { error("Inappropriate #elif"); }
+static void Eelse (void) { error("Inappropriate #else"); }
+static void Eendif(void) { error("Inappropriate #endif"); }
+static void Eeof (void) { error("Premature EOF"); }
+static void Eioccc(void) { error("Obfuscated preprocessor control line"); }
+/* plain line handling */
+static void print (void) { flushline(true); }
+static void drop (void) { flushline(false); }
+/* output lacks group's start line */
+static void Strue (void) { drop(); ignoreoff(); state(IS_TRUE_PREFIX); }
+static void Sfalse(void) { drop(); ignoreoff(); state(IS_FALSE_PREFIX); }
+static void Selse (void) { drop(); state(IS_TRUE_ELSE); }
+/* print/pass this block */
+static void Pelif (void) { print(); ignoreoff(); state(IS_PASS_MIDDLE); }
+static void Pelse (void) { print(); state(IS_PASS_ELSE); }
+static void Pendif(void) { print(); unnest(); }
+/* discard this block */
+static void Dfalse(void) { drop(); ignoreoff(); state(IS_FALSE_TRAILER); }
+static void Delif (void) { drop(); ignoreoff(); state(IS_FALSE_MIDDLE); }
+static void Delse (void) { drop(); state(IS_FALSE_ELSE); }
+static void Dendif(void) { drop(); unnest(); }
+/* first line of group */
+static void Fdrop (void) { nest(); Dfalse(); }
+static void Fpass (void) { nest(); Pelif(); }
+static void Ftrue (void) { nest(); Strue(); }
+static void Ffalse(void) { nest(); Sfalse(); }
+/* variable pedantry for obfuscated lines */
+static void Oiffy (void) { if (!iocccok) Eioccc(); Fpass(); ignoreon(); }
+static void Oif (void) { if (!iocccok) Eioccc(); Fpass(); }
+static void Oelif (void) { if (!iocccok) Eioccc(); Pelif(); }
+/* ignore comments in this block */
+static void Idrop (void) { Fdrop(); ignoreon(); }
+static void Itrue (void) { Ftrue(); ignoreon(); }
+static void Ifalse(void) { Ffalse(); ignoreon(); }
+/* edit this line */
+static void Mpass (void) { strncpy(keyword, "if ", 4); Pelif(); }
+static void Mtrue (void) { keywordedit("else\n"); state(IS_TRUE_MIDDLE); }
+static void Melif (void) { keywordedit("endif\n"); state(IS_FALSE_TRAILER); }
+static void Melse (void) { keywordedit("endif\n"); state(IS_FALSE_ELSE); }
+
+static state_fn * const trans_table[IS_COUNT][LT_COUNT] = {
+/* IS_OUTSIDE */
+{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Eendif,
+ Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eendif,
+ print, done },
+/* IS_FALSE_PREFIX */
+{ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Strue, Sfalse,Selse, Dendif,
+ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Mpass, Eioccc,Eioccc,Eioccc,Eioccc,
+ drop, Eeof },
+/* IS_TRUE_PREFIX */
+{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Dfalse,Dfalse,Dfalse,Delse, Dendif,
+ Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
+ print, Eeof },
+/* IS_PASS_MIDDLE */
+{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Pelif, Mtrue, Delif, Pelse, Pendif,
+ Oiffy, Oiffy, Fpass, Oif, Oif, Pelif, Oelif, Oelif, Pelse, Pendif,
+ print, Eeof },
+/* IS_FALSE_MIDDLE */
+{ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Pelif, Mtrue, Delif, Pelse, Pendif,
+ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eioccc,Eioccc,Eioccc,Eioccc,Eioccc,
+ drop, Eeof },
+/* IS_TRUE_MIDDLE */
+{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Melif, Melif, Melif, Melse, Pendif,
+ Oiffy, Oiffy, Fpass, Oif, Oif, Eioccc,Eioccc,Eioccc,Eioccc,Pendif,
+ print, Eeof },
+/* IS_PASS_ELSE */
+{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Pendif,
+ Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Pendif,
+ print, Eeof },
+/* IS_FALSE_ELSE */
+{ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Dendif,
+ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Eelif, Eelif, Eelif, Eelse, Eioccc,
+ drop, Eeof },
+/* IS_TRUE_ELSE */
+{ Itrue, Ifalse,Fpass, Ftrue, Ffalse,Eelif, Eelif, Eelif, Eelse, Dendif,
+ Oiffy, Oiffy, Fpass, Oif, Oif, Eelif, Eelif, Eelif, Eelse, Eioccc,
+ print, Eeof },
+/* IS_FALSE_TRAILER */
+{ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Dendif,
+ Idrop, Idrop, Fdrop, Fdrop, Fdrop, Dfalse,Dfalse,Dfalse,Delse, Eioccc,
+ drop, Eeof }
+/*TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF
+ TRUEI FALSEI IF TRUE FALSE ELIF ELTRUE ELFALSE ELSE ENDIF (DODGY)
+ PLAIN EOF */
+};
+
+/*
+ * State machine utility functions
+ */
+static void
+done(void)
+{
+ if (incomment)
+ error("EOF in comment");
+ exit(exitstat);
+}
+static void
+ignoreoff(void)
+{
+ if (depth == 0) {
+ debug("bug at line %d", __LINE__);
+ abort(); /* bug */
+ }
+ ignoring[depth] = ignoring[depth-1];
+}
+static void
+ignoreon(void)
+{
+ ignoring[depth] = true;
+}
+static void
+keywordedit(const char *replacement)
+{
+ size_t size = tline + sizeof(tline) - keyword;
+ char *dst = keyword;
+ const char *src = replacement;
+ if (size != 0) {
+ while ((--size != 0) && (*src != '\0'))
+ *dst++ = *src++;
+ *dst = '\0';
+ }
+ print();
+}
+static void
+nest(void)
+{
+ depth += 1;
+ if (depth >= MAXDEPTH)
+ error("Too many levels of nesting");
+ stifline[depth] = linenum;
+}
+static void
+unnest(void)
+{
+ if (depth == 0) {
+ debug("bug at line %d", __LINE__);
+ abort(); /* bug */
+ }
+ depth -= 1;
+}
+static void
+state(Ifstate is)
+{
+ ifstate[depth] = is;
+}
+
+/*
+ * Write a line to the output or not, according to command line options.
+ */
+static void
+flushline(bool keep)
+{
+ if (symlist)
+ return;
+ if (keep ^ complement) {
+ if (lnnum && delcount > 0)
+ printf("#line %d\n", linenum);
+ fputs(tline, stdout);
+ delcount = 0;
+ } else {
+ if (lnblank)
+ putc('\n', stdout);
+ exitstat = 1;
+ delcount += 1;
+ }
+}
+
+/*
+ * The driver for the state machine.
+ */
+static void
+process(void)
+{
+ Linetype lineval;
+
+ for (;;) {
+ linenum++;
+ lineval = get_line();
+ trans_table[ifstate[depth]][lineval]();
+ debug("process %s -> %s depth %d",
+ linetype_name[lineval],
+ ifstate_name[ifstate[depth]], depth);
+ }
+}
+
+/*
+ * Parse a line and determine its type. We keep the preprocessor line
+ * parser state between calls in the global variable linestate, with
+ * help from skipcomment().
+ */
+static Linetype
+get_line(void)
+{
+ const char *cp;
+ int cursym;
+ int kwlen;
+ Linetype retval;
+ Comment_state wascomment;
+
+ if (fgets(tline, MAXLINE, input) == NULL)
+ return (LT_EOF);
+ retval = LT_PLAIN;
+ wascomment = incomment;
+ cp = skipcomment(tline);
+ if (linestate == LS_START) {
+ if (*cp == '#') {
+ linestate = LS_HASH;
+ cp = skipcomment(cp + 1);
+ } else if (*cp != '\0')
+ linestate = LS_DIRTY;
+ }
+ if (!incomment && linestate == LS_HASH) {
+ keyword = tline + (cp - tline);
+ cp = skipsym(cp);
+ kwlen = cp - keyword;
+ /* no way can we deal with a continuation inside a keyword */
+ if (strncmp(cp, "\\\n", 2) == 0)
+ Eioccc();
+ if (strlcmp("ifdef", keyword, kwlen) == 0 ||
+ strlcmp("ifndef", keyword, kwlen) == 0) {
+ cp = skipcomment(cp);
+ if ((cursym = findsym(cp)) < 0)
+ retval = LT_IF;
+ else {
+ retval = (keyword[2] == 'n')
+ ? LT_FALSE : LT_TRUE;
+ if (value[cursym] == NULL)
+ retval = (retval == LT_TRUE)
+ ? LT_FALSE : LT_TRUE;
+ if (ignore[cursym])
+ retval = (retval == LT_TRUE)
+ ? LT_TRUEI : LT_FALSEI;
+ }
+ cp = skipsym(cp);
+ } else if (strlcmp("if", keyword, kwlen) == 0)
+ retval = ifeval(&cp);
+ else if (strlcmp("elif", keyword, kwlen) == 0)
+ retval = ifeval(&cp) - LT_IF + LT_ELIF;
+ else if (strlcmp("else", keyword, kwlen) == 0)
+ retval = LT_ELSE;
+ else if (strlcmp("endif", keyword, kwlen) == 0)
+ retval = LT_ENDIF;
+ else {
+ linestate = LS_DIRTY;
+ retval = LT_PLAIN;
+ }
+ cp = skipcomment(cp);
+ if (*cp != '\0') {
+ linestate = LS_DIRTY;
+ if (retval == LT_TRUE || retval == LT_FALSE ||
+ retval == LT_TRUEI || retval == LT_FALSEI)
+ retval = LT_IF;
+ if (retval == LT_ELTRUE || retval == LT_ELFALSE)
+ retval = LT_ELIF;
+ }
+ if (retval != LT_PLAIN && (wascomment || incomment)) {
+ retval += LT_DODGY;
+ if (incomment)
+ linestate = LS_DIRTY;
+ }
+ /* skipcomment should have changed the state */
+// Hmm hppens sometimes on valid files
+// if (linestate == LS_HASH) {
+// debug("bug at line %d", __LINE__);
+// abort(); /* bug */
+// }
+ }
+ if (linestate == LS_DIRTY) {
+ while (*cp != '\0')
+ cp = skipcomment(cp + 1);
+ }
+ debug("parser %s comment %s line",
+ comment_name[incomment], linestate_name[linestate]);
+ return (retval);
+}
+
+/*
+ * These are the binary operators that are supported by the expression
+ * evaluator. Note that if support for division is added then we also
+ * need short-circuiting booleans because of divide-by-zero.
+ */
+static int op_lt(int a, int b) { return (a < b); }
+static int op_gt(int a, int b) { return (a > b); }
+static int op_le(int a, int b) { return (a <= b); }
+static int op_ge(int a, int b) { return (a >= b); }
+static int op_eq(int a, int b) { return (a == b); }
+static int op_ne(int a, int b) { return (a != b); }
+static int op_or(int a, int b) { return (a || b); }
+static int op_and(int a, int b) { return (a && b); }
+
+/*
+ * An evaluation function takes three arguments, as follows: (1) a pointer to
+ * an element of the precedence table which lists the operators at the current
+ * level of precedence; (2) a pointer to an integer which will receive the
+ * value of the expression; and (3) a pointer to a char* that points to the
+ * expression to be evaluated and that is updated to the end of the expression
+ * when evaluation is complete. The function returns LT_FALSE if the value of
+ * the expression is zero, LT_TRUE if it is non-zero, or LT_IF if the
+ * expression could not be evaluated.
+ */
+struct ops;
+
+typedef Linetype eval_fn(const struct ops *, int *, const char **);
+
+static eval_fn eval_table, eval_unary;
+
+/*
+ * The precedence table. Expressions involving binary operators are evaluated
+ * in a table-driven way by eval_table. When it evaluates a subexpression it
+ * calls the inner function with its first argument pointing to the next
+ * element of the table. Innermost expressions have special non-table-driven
+ * handling.
+ */
+static const struct ops {
+ eval_fn *inner;
+ struct op {
+ const char *str;
+ int short_circuit_val;
+ int (*fn)(int, int);
+ } op[5];
+} eval_ops[] = {
+ { eval_table, { { "||", 1, op_or } } },
+ { eval_table, { { "&&", 0, op_and } } },
+ { eval_table, { { "==", -1, op_eq },
+ { "!=", -1, op_ne } } },
+ { eval_unary, { { "<=", -1, op_le },
+ { ">=", -1, op_ge },
+ { "<", -1, op_lt },
+ { ">", -1, op_gt } } }
+};
+
+/*
+ * Function for evaluating the innermost parts of expressions, viz.
+ * "!expr", "(expr)", "defined(symbol)", "defined symbol", "symbol", "number".
+ * We reset the keepthis flag when we find a non-constant subexpression.
+ */
+// TODO: we use LT_IF both as "I don't know whether it's false or true"
+// (example: "#if defined FOO") and when we see syntax error
+// (example: "#if (1 || 2" - no closing paren!), but this is wrong.
+// Binary && and || need to distinguish these cases in order to handle this:
+// "#if defined KNOWN_UNDEFINED && FOO" - discard
+// "#if defined KNOWN_UNDEFINED && (syntax_error_here" - do not discard!
+static Linetype
+eval_unary(const struct ops *ops, int *valp, const char **cpp)
+{
+ const char *cp;
+ char *ep;
+ int sym;
+
+ cp = skipcomment(*cpp);
+ if (*cp == '!') {
+ debug("eval%d !", ops - eval_ops);
+ cp++;
+ if (eval_unary(ops, valp, &cp) == LT_IF) {
+ *cpp = cp;
+ return (LT_IF);
+ }
+ *valp = !*valp;
+
+ } else if (*cp == '(') {
+ Linetype expr_res;
+
+ cp++;
+ debug("eval%d (%s", ops - eval_ops, cp);
+ expr_res = eval_table(eval_ops, valp, &cp);
+ cp = skipcomment(cp);
+ *cpp = cp;
+ if (*cp++ != ')')
+ return (LT_IF);
+ *cpp = cp;
+ if (expr_res == LT_IF)
+ return (LT_IF);
+
+ } else if (isdigit((unsigned char)*cp)) {
+ debug("eval%d number", ops - eval_ops);
+ *valp = strtol(cp, &ep, 0);
+ cp = skipsym(cp);
+
+ } else if (strncmp(cp, "defined", 7) == 0 && endsym(cp[7])) {
+ bool parens;
+
+ cp = skipcomment(cp+7);
+ debug("eval%d defined '%s'", ops - eval_ops, cp);
+ parens = (*cp == '(');
+ if (parens)
+ cp = skipcomment(cp+1);
+ sym = findsym(cp);
+ cp = skipsym(cp);
+ cp = skipcomment(cp);
+ if (parens) {
+ if (*cp != ')')
+ return (LT_IF);
+ cp = skipcomment(cp+1);
+ }
+ *cpp = cp;
+ if (sym < 0) {
+ debug("sym not found, returning LT_IF");
+ return (LT_IF);
+ }
+ *valp = (value[sym] != NULL);
+ keepthis = false;
+
+ } else if (!endsym(*cp)) {
+ debug("eval%d symbol", ops - eval_ops);
+ sym = findsym(cp);
+ cp = skipsym(cp);
+ *cpp = cp;
+ if (sym < 0)
+ return (LT_IF);
+ if (value[sym] == NULL)
+ *valp = 0;
+ else {
+ *valp = strtol(value[sym], &ep, 0);
+ if (*ep != '\0' || ep == value[sym])
+ return (LT_IF);
+ }
+ keepthis = false;
+
+ } else {
+ debug("eval%d bad expr", ops - eval_ops);
+ return (LT_IF);
+ }
+
+ *cpp = cp;
+ debug("eval%d = %d", ops - eval_ops, *valp);
+ return (*valp ? LT_TRUE : LT_FALSE);
+}
+
+/*
+ * Table-driven evaluation of binary operators.
+ */
+static Linetype
+eval_table(const struct ops *ops, int *valp, const char **cpp)
+{
+ Linetype left_side;
+ const struct op *op;
+ const char *cp;
+ int val;
+
+ debug("eval%d '%s'", ops - eval_ops, *cpp);
+ left_side = ops->inner(ops+1, valp, cpp);
+ cp = *cpp;
+
+ for (;;) {
+ Linetype right_side;
+
+ cp = skipcomment(cp);
+ for (op = ops->op; op->str != NULL; op++)
+ if (strncmp(cp, op->str, strlen(op->str)) == 0)
+ break;
+ if (op->str == NULL)
+ break;
+ cp += strlen(op->str);
+ debug("eval%d '%s'", ops - eval_ops, op->str);
+ right_side = ops->inner(ops+1, &val, &cp);
+ *cpp = cp;
+
+ /* If short_circuit_val is 0 or 1, we can ignore
+ * right side if left size is known, and its value
+ * (i.e., *valp) is 0 or !0, respectively */
+ if (left_side != LT_IF && op->short_circuit_val == !!*valp) {
+ debug("op->short_circuit_val:%d *valp:%d cp:'%s'",
+ op->short_circuit_val, *valp, cp);
+ *valp = !!*valp;
+ break;
+ }
+ /* Same for the right side */
+ if (right_side != LT_IF && op->short_circuit_val == !!val) {
+ debug("op->short_circuit_val:%d val:%d cp:'%s'",
+ op->short_circuit_val, val, cp);
+ left_side = right_side;
+ *valp = !!val;
+ break;
+ }
+
+ if (left_side == LT_IF || right_side == LT_IF)
+ return (LT_IF);
+ *valp = op->fn(*valp, val);
+ left_side = right_side;
+ }
+
+ debug("eval%d = %d LT_IF:%d", ops - eval_ops, *valp, (left_side == LT_IF));
+ if (left_side == LT_IF)
+ return (LT_IF);
+ return (*valp ? LT_TRUE : LT_FALSE);
+}
+
+/*
+ * Evaluate the expression on a #if or #elif line. If we can work out
+ * the result we return LT_TRUE or LT_FALSE accordingly, otherwise we
+ * return just a generic LT_IF.
+ */
+static Linetype
+ifeval(const char **cpp)
+{
+ int ret;
+ int val;
+
+ debug("eval %s", *cpp);
+ keepthis = killconsts ? false : true;
+ ret = eval_table(eval_ops, &val, cpp);
+ debug("val:%d ret:%d keepthis:%d", val, ret, keepthis);
+ return (keepthis ? LT_IF : ret);
+}
+
+/*
+ * Skip over comments, strings, and character literals and stop at the
+ * next character position that is not whitespace. Between calls we keep
+ * the comment state in the global variable incomment, and we also adjust
+ * the global variable linestate when we see a newline.
+ * XXX: doesn't cope with the buffer splitting inside a state transition.
+ */
+static const char *
+skipcomment(const char *cp)
+{
+ if (text || ignoring[depth]) {
+ for (; isspace((unsigned char)*cp); cp++)
+ if (*cp == '\n')
+ linestate = LS_START;
+ return (cp);
+ }
+ while (*cp != '\0')
+ /* don't reset to LS_START after a line continuation */
+ if (strncmp(cp, "\\\n", 2) == 0)
+ cp += 2;
+ else switch (incomment) {
+ case NO_COMMENT:
+ if (strncmp(cp, "/\\\n", 3) == 0) {
+ incomment = STARTING_COMMENT;
+ cp += 3;
+ } else if (strncmp(cp, "/*", 2) == 0) {
+ incomment = C_COMMENT;
+ cp += 2;
+ } else if (strncmp(cp, "//", 2) == 0) {
+ incomment = CXX_COMMENT;
+ cp += 2;
+ } else if (strncmp(cp, "\'", 1) == 0) {
+ incomment = CHAR_LITERAL;
+ linestate = LS_DIRTY;
+ cp += 1;
+ } else if (strncmp(cp, "\"", 1) == 0) {
+ incomment = STRING_LITERAL;
+ linestate = LS_DIRTY;
+ cp += 1;
+ } else if (strncmp(cp, "\n", 1) == 0) {
+ linestate = LS_START;
+ cp += 1;
+ } else if (strchr(" \t", *cp) != NULL) {
+ cp += 1;
+ } else
+ return (cp);
+ continue;
+ case CXX_COMMENT:
+ if (strncmp(cp, "\n", 1) == 0) {
+ incomment = NO_COMMENT;
+ linestate = LS_START;
+ }
+ cp += 1;
+ continue;
+ case CHAR_LITERAL:
+ case STRING_LITERAL:
+ if ((incomment == CHAR_LITERAL && cp[0] == '\'') ||
+ (incomment == STRING_LITERAL && cp[0] == '\"')) {
+ incomment = NO_COMMENT;
+ cp += 1;
+ } else if (cp[0] == '\\') {
+ if (cp[1] == '\0')
+ cp += 1;
+ else
+ cp += 2;
+ } else if (strncmp(cp, "\n", 1) == 0) {
+ if (incomment == CHAR_LITERAL)
+ error("unterminated char literal");
+ else
+ error("unterminated string literal");
+ } else
+ cp += 1;
+ continue;
+ case C_COMMENT:
+ if (strncmp(cp, "*\\\n", 3) == 0) {
+ incomment = FINISHING_COMMENT;
+ cp += 3;
+ } else if (strncmp(cp, "*/", 2) == 0) {
+ incomment = NO_COMMENT;
+ cp += 2;
+ } else
+ cp += 1;
+ continue;
+ case STARTING_COMMENT:
+ if (*cp == '*') {
+ incomment = C_COMMENT;
+ cp += 1;
+ } else if (*cp == '/') {
+ incomment = CXX_COMMENT;
+ cp += 1;
+ } else {
+ incomment = NO_COMMENT;
+ linestate = LS_DIRTY;
+ }
+ continue;
+ case FINISHING_COMMENT:
+ if (*cp == '/') {
+ incomment = NO_COMMENT;
+ cp += 1;
+ } else
+ incomment = C_COMMENT;
+ continue;
+ default:
+ debug("bug at line %d", __LINE__);
+ abort(); /* bug */
+ }
+ return (cp);
+}
+
+/*
+ * Skip over an identifier.
+ */
+static const char *
+skipsym(const char *cp)
+{
+ while (!endsym(*cp))
+ ++cp;
+ return (cp);
+}
+
+/*
+ * Look for the symbol in the symbol table. If is is found, we return
+ * the symbol table index, else we return -1.
+ */
+static int
+findsym(const char *str)
+{
+ const char *cp;
+ int symind;
+
+ cp = skipsym(str);
+ if (cp == str)
+ return (-1);
+ if (symlist) {
+ printf("%.*s\n", (int)(cp-str), str);
+ /* we don't care about the value of the symbol */
+ return (0);
+ }
+ for (symind = 0; symind < nsyms; ++symind) {
+ if (strlcmp(symname[symind], str, cp-str) == 0) {
+ debug("findsym %s %s", symname[symind],
+ value[symind] ? value[symind] : "");
+ return (symind);
+ }
+ }
+ return (-1);
+}
+
+/*
+ * Add a symbol to the symbol table.
+ */
+static void
+addsym(bool ignorethis, bool definethis, char *sym)
+{
+ int symind;
+ char *val;
+
+ symind = findsym(sym);
+ if (symind < 0) {
+ if (nsyms >= MAXSYMS)
+ errx(2, "too many symbols");
+ symind = nsyms++;
+ }
+ symname[symind] = sym;
+ ignore[symind] = ignorethis;
+ val = sym + (skipsym(sym) - sym);
+ if (definethis) {
+ if (*val == '=') {
+ value[symind] = val+1;
+ *val = '\0';
+ } else if (*val == '\0')
+ value[symind] = "";
+ else
+ usage();
+ } else {
+ if (*val != '\0')
+ usage();
+ value[symind] = NULL;
+ }
+}
+
+/*
+ * Compare s with n characters of t.
+ * The same as strncmp() except that it checks that s[n] == '\0'.
+ */
+static int
+strlcmp(const char *s, const char *t, size_t n)
+{
+ while (n-- && *t != '\0')
+ if (*s != *t)
+ return ((unsigned char)*s - (unsigned char)*t);
+ else
+ ++s, ++t;
+ return ((unsigned char)*s);
+}
+
+/*
+ * Diagnostics.
+ */
+static void
+debug(const char *msg, ...)
+{
+ va_list ap;
+
+ if (debugging) {
+ va_start(ap, msg);
+ vwarnx(msg, ap);
+ va_end(ap);
+ }
+}
+
+static void
+error(const char *msg)
+{
+ if (depth == 0)
+ warnx("%s: %d: %s", filename, linenum, msg);
+ else
+ warnx("%s: %d: %s (#if line %d depth %d)",
+ filename, linenum, msg, stifline[depth], depth);
+ errx(2, "output may be truncated");
+}
diff --git a/ap/build/uClibc/extra/scripts/unifdef.test b/ap/build/uClibc/extra/scripts/unifdef.test
new file mode 100644
index 0000000..5ba4e48
--- /dev/null
+++ b/ap/build/uClibc/extra/scripts/unifdef.test
@@ -0,0 +1,67 @@
+Run me through unifdef -UA
+*** Nothing should be visible here:
+#if defined A && defined B
+hello world
+#endif
+#if defined A && B
+hello world
+#endif
+#if defined A && 1
+hello world
+#endif
+#if defined A && (1 > 0)
+hello world
+#endif
+#if defined B && defined A
+hello world
+#endif
+#if B && defined A
+hello world
+#endif
+#if 1 && defined A
+hello world
+#endif
+#if (1 > 0) && defined A
+hello world
+#endif
+#if defined A && (defined FOO || defined BAR)
+hello world
+#endif
+#if (defined FOO || defined BAR) && defined A
+hello world
+#endif
+
+*** Everything should be visible here, but #if/#endif removed:
+#if defined B || !defined A
+hello world 1
+#endif
+#if !defined A || defined B
+hello world 2 (last)
+#endif
+
+*** This should be unchanged (#if/#endif not removed):
+#if defined A || defined B
+I am here 1
+#endif
+#if defined B || defined A
+I am here 2
+#endif
+I am here 3
+#if !defined FOO && !defined BAR \
+ && !defined BAZ
+# error "I am here 4"
+#endif
+I am here 5
+#if (!defined FOO \
+ && (defined BAR || defined BAZ \
+ || defined XYZ))
+I am here 6
+#endif
+I am here 7
+#if !defined FOO \
+ && defined BAR
+I am here 8
+#endif
+I am here 9 (last)
+
+*** End