#!/bin/sh
dirname=`echo "$0" | sed 's,^[^/]*$,.,;s,//*[^/]*$,,'`
progname=`echo "$0" | sed 's,.*/,,'`
original_dir=`pwd`
FIX_HEADER=${FIX_HEADER-$original_dir/fix-header}
DEFINES="-D__STDC__=0 -D__cplusplus ${FIXPROTO_DEFINES}"
if mkdir -p . 2> /dev/null; then
mkinstalldirs="mkdir -p"
else
mkinstalldirs=${mkinstalldirs-"/bin/sh $dirname/mkinstalldirs"}
if $mkinstalldirs . 2> /dev/null; then
:
else
mkinstalldirs=mkdir
fi
fi
if [ `echo $1 | wc -w` = 0 ] ; then
echo $progname\: usage\: $progname target-dir \[ source-dir \.\.\. \]
exit 1
fi
std_files="ctype.h dirent.h errno.h curses.h fcntl.h grp.h locale.h math.h pwd.h setjmp.h signal.h stdio.h stdlib.h string.h sys/socket.h sys/stat.h sys/times.h sys/resource.h sys/utsname.h sys/wait.h tar.h termios.h time.h unistd.h utime.h"
rel_target_dir=$1
src_dir_all=$2
src_dir_std=$3
if [ `expr $rel_target_dir : '\(.\)'` != '/' ] ; then
abs_target_dir=$original_dir/$rel_target_dir
else
abs_target_dir=$rel_target_dir
fi
if ln -s X $rel_target_dir/ShouldNotExist 2>/dev/null; then
rm -f $rel_target_dir/ShouldNotExist
LINKS=true
elif ln -s X /tmp/ShouldNotExist 2>/dev/null; then
rm -f /tmp/ShouldNotExist
LINKS=true
else
LINKS=false
fi
if [ \! -d $abs_target_dir ] ; then
echo $progname\: creating directory $rel_target_dir
$mkinstalldirs $abs_target_dir
fi
echo $progname\: populating \`$rel_target_dir\'
include_path=""
if [ `echo $* | wc -w` != 0 ] ; then
for rel_source_dir in $src_dir_all $src_dir_std; do
if [ `expr $rel_source_dir : '\(.\)'` != '/' ] ; then
abs_source_dir=$original_dir/$rel_source_dir
else
abs_source_dir=$rel_source_dir
fi
include_path="$include_path -I$abs_source_dir"
done
fi
required_stdlib_h="abort abs atexit atof atoi atol bsearch calloc exit free getenv labs malloc putenv qsort rand realloc srand strtod strtol strtoul system"
required_unistd_h="_exit access alarm chdir chown close ctermid cuserid dup dup2 execl execle execlp execv execve execvp fork fpathconf getcwd getegid geteuid getgid getlogin getopt getpgrp getpid getppid getuid isatty link lseek pathconf pause pipe read rmdir setgid setpgid setsid setuid sleep sysconf tcgetpgrp tcsetpgrp ttyname unlink write"
done_dirs=""
subdirs_made=""
echo "" >fixproto.list
for code in ALL STD ; do
subdirs="."
case $code in
ALL)
rel_source_dir=$src_dir_all
dirs="."
levels=2
while $LINKS && test -n "$dirs" -a $levels -gt 0
do
levels=`expr $levels - 1`
newdirs=
for d in $dirs ; do
subdirs="$subdirs "`cd $rel_source_dir/$d; find . -type d -print | \
sed -e '/^\.$/d' -e "s|^\./|${d}/|" -e 's|^\./||' \
-e '/CC$/d' -e '/[+][+]/d'`
links=
links=`cd $rel_source_dir; find $d/. -type l -print | \
sed -e "s|$d/./|$d/|" -e 's|^\./||'`
for link in $links --dummy-- ; do
test -d $rel_source_dir/$link/. && newdirs="$newdirs $link"
done
done
dirs="$newdirs"
subdirs="$subdirs $newdirs"
done
;;
STD)
rel_source_dir=$src_dir_std
;;
esac
if [ `expr $rel_source_dir : '\(.\)'` != '/' ] ; then
abs_source_dir=$original_dir/$rel_source_dir
else
abs_source_dir=$rel_source_dir
fi
if [ \! -d $abs_source_dir ] ; then
echo $progname\: warning\: no such directory\: \`$rel_source_dir\'
continue
fi
for rel_source_subdir in $subdirs; do
abs_target_subdir=${abs_target_dir}/${rel_source_subdir}
if [ \! -d $abs_target_subdir ] ; then
if $mkinstalldirs $abs_target_subdir ; then
subdirs_made="$abs_target_subdir $subdirs_made"
fi
fi
rel_source_prefix=`echo $rel_source_subdir | sed -e 's|$|/|' -e 's|^./||'`
case $code in
ALL)
rel_source_files=`cd ${abs_source_dir}/${rel_source_subdir}; echo *.h | sed -e 's|[*].h|NONE|'`
;;
STD)
files_to_check="$std_files"
rel_source_files=""
while [ -n "${files_to_check}" ]
do
new_files_to_check=""
for file in $files_to_check ; do
xxfile=`echo $file | sed -e 's|/\([^/\.][^/\.]*\)/\.\./|/|'`
xxdir=`echo ./$file | sed -e 's|/[^/]*$||'`
if [ \! -d $abs_target_subdir/$xxdir ] ; then
if $mkinstalldirs $abs_target_subdir/$xxdir ; then
subdirs_made="$abs_target_subdir/$xxdir $subdirs_made"
fi
fi
if [ -f $src_dir_std/$file -a -f $src_dir_std/$xxfile ] ; then
file=$xxfile
fi
case " $rel_source_files " in
*" ${file} "*)
;;
*)
if test -f $src_dir_std/$file ; then
rel_dir=`echo $file | sed -n -e 's|^\(.*/\)[^/]*$|\1|p'`
new_files_to_check="$new_files_to_check "`sed -n \
-e 's@ @ @g' \
-e 's@^ * 's@^ * <$src_dir_std/$file`
rel_source_files="$rel_source_files $file"
fi
;;
esac
done
files_to_check="$new_files_to_check"
done
rel_source_files="$rel_source_files"
;;
esac
for filename in $rel_source_files ; do
rel_source_file=${rel_source_prefix}${filename}
abs_source_file=$abs_source_dir/$rel_source_file
abs_target_file=$abs_target_dir/$rel_source_file
if test "$filename" = 'NONE' ; then
echo "(No *.h files in $abs_source_dir/$rel_source_subdir)"
elif test -f $abs_target_file -a -n "$done_dirs" \
&& grep "$rel_source_file" fixproto.list >/dev/null
then true
else
$FIX_HEADER $rel_source_file $abs_source_file $abs_target_file ${DEFINES} $include_path
echo "${rel_source_file}" >>fixproto.list
fi
done
done
done_dirs="$done_dir $rel_source_dir"
done
for rel_source_file in unistd.h stdlib.h
do
if grep "$rel_source_file" fixproto.list >/dev/null
then true
else
echo Adding missing $rel_source_file
rel_source_ident=`echo $rel_source_file | tr ./ __`
required_list=`eval echo '${required_'${rel_source_ident}'-}'`
cat >tmp.h <<EOF
EOF
if test $rel_source_file = stdlib.h
then
cat >>tmp.h <<EOF
EOF
fi
cat >>tmp.h <<EOF
EOF
${FIX_HEADER} $rel_source_file tmp.h $abs_target_dir/$rel_source_file ${DEFINES} $include_path
rm tmp.h
fi
done
rmdir $subdirs_made 2>/dev/null
exit 0