1#!/bin/sh 2# 3# Install modified versions of certain ANSI-incompatible system header 4# files which are fixed to work correctly with ANSI C and placed in a 5# directory that GCC will search. 6# 7# See README-fixinc for more information. 8# 9# fixincludes copyright (c) 1998, 1999, 2000, 2002, 2009 10# The Free Software Foundation, Inc. 11# 12# fixincludes is free software. 13# 14# You may redistribute it and/or modify it under the terms of the 15# GNU General Public License, as published by the Free Software 16# Foundation; either version 3, or (at your option) any later version. 17# 18# fixincludes is distributed in the hope that it will be useful, 19# but WITHOUT ANY WARRANTY; without even the implied warranty of 20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 21# See the GNU General Public License for more details. 22# 23# You should have received a copy of the GNU General Public License 24# along with fixincludes; see the file COPYING3. If not see 25# <http://www.gnu.org/licenses/>. 26# 27# # # # # # # # # # # # # # # # # # # # # 28 29# Usage: fixinc.sh output-dir input-dir 30# 31# Directory in which to store the results. 32# Fail if no arg to specify a directory for the output. 33if [ "x$1" = "x" ] 34then 35 echo fixincludes: no output directory specified 36 exit 1 37fi 38 39LIB=${1} 40shift 41 42# Make sure it exists. 43if [ ! -d $LIB ]; then 44 mkdir -p $LIB || { 45 echo fixincludes: output dir '`'$LIB"' cannot be created" 46 exit 1 47 } 48else 49 ( cd $LIB && touch DONE && rm DONE ) || { 50 echo fixincludes: output dir '`'$LIB"' is an invalid directory" 51 exit 1 52 } 53fi 54 55if test -z "$VERBOSE" 56then 57 VERBOSE=2 58 export VERBOSE 59else 60 case "$VERBOSE" in 61 [0-9] ) : ;; 62 * ) VERBOSE=3 ;; 63 esac 64fi 65 66# Define what target system we're fixing. 67# 68if test -r ./Makefile; then 69 target_canonical="`sed -n -e 's,^target[ ]*=[ ]*\(.*\)$,\1,p' < Makefile`" 70fi 71 72# If not from the Makefile, then try config.guess 73# 74if test -z "${target_canonical}" ; then 75 if test -x ./config.guess ; then 76 target_canonical="`config.guess`" ; fi 77 test -z "${target_canonical}" && target_canonical=unknown 78fi 79export target_canonical 80 81# # # # # # # # # # # # # # # # # # # # # 82# 83# Define PWDCMD as a command to use to get the working dir 84# in the form that we want. 85PWDCMD=${PWDCMD-pwd} 86 87case "`$PWDCMD`" in 88//*) 89 # On an Apollo, discard everything before `/usr'. 90 PWDCMD="eval pwd | sed -e 's,.*/usr/,/usr/,'" 91 ;; 92esac 93 94# Original directory. 95ORIGDIR=`${PWDCMD}` 96export ORIGDIR 97FIXINCL=`${PWDCMD}`/fixincl 98if [ ! -x $FIXINCL ] ; then 99 echo "Cannot find fixincl" >&2 100 exit 1 101fi 102export FIXINCL 103 104# Make LIB absolute only if needed to avoid problems with the amd. 105case $LIB in 106/*) 107 ;; 108*) 109 cd $LIB; LIB=`${PWDCMD}` 110 ;; 111esac 112 113if test $VERBOSE -gt 0 114then echo Fixing headers into ${LIB} for ${target_canonical} target ; fi 115 116# Determine whether this system has symbolic links. 117if test -n "$DJDIR"; then 118 LINKS=false 119elif ln -s X $LIB/ShouldNotExist 2>/dev/null; then 120 rm -f $LIB/ShouldNotExist 121 LINKS=true 122elif ln -s X /tmp/ShouldNotExist 2>/dev/null; then 123 rm -f /tmp/ShouldNotExist 124 LINKS=true 125else 126 LINKS=false 127fi 128 129# # # # # # # # # # # # # # # # # # # # # 130# 131# Check to see if the machine_name fix needs to be disabled. 132# 133# On some platforms, machine_name doesn't work properly and 134# breaks some of the header files. Since everything works 135# properly without it, just wipe the macro list to 136# disable the fix. 137 138case "${target_canonical}" in 139 *-*-vxworks* | powerpc*-*-linux*) 140 test -f ${MACRO_LIST} && echo > ${MACRO_LIST} 141 ;; 142esac 143 144 145# # # # # # # # # # # # # # # # # # # # # 146# 147# In the file macro_list are listed all the predefined 148# macros that are not in the C89 reserved namespace (the reserved 149# namespace is all identifiers beginnning with two underscores or one 150# underscore followed by a capital letter). A regular expression to find 151# any of those macros in a header file is written to MN_NAME_PAT. 152# 153# Note dependency on ASCII. \012 = newline. 154# tr ' ' '\n' is, alas, not portable. 155 156if test -s ${MACRO_LIST} 157then 158 if test $VERBOSE -gt 0; then 159 echo "Forbidden identifiers: `tr '\012' ' ' < ${MACRO_LIST}`" 160 fi 161 MN_NAME_PAT="`sed 's/^/\\\\</; s/$/\\\\>/; $!s/$/|/' \ 162 < ${MACRO_LIST} | tr -d '\012'`" 163 export MN_NAME_PAT 164else 165 if test $VERBOSE -gt 0 166 then echo "No forbidden identifiers defined by this target" ; fi 167fi 168 169# # # # # # # # # # # # # # # # # # # # # 170# 171# Search each input directory for broken header files. 172# This loop ends near the end of the file. 173# 174if test $# -eq 0 175then 176 INPUTLIST="/usr/include" 177else 178 INPUTLIST="$@" 179fi 180 181for INPUT in ${INPUTLIST} ; do 182 183cd ${ORIGDIR} 184 185# Make sure a directory exists before changing into it, 186# otherwise Solaris2 will fail-exit the script. 187# 188if [ ! -d ${INPUT} ]; then 189 continue 190fi 191cd ${INPUT} 192 193INPUT=`${PWDCMD}` 194export INPUT 195 196# 197# # # # # # # # # # # # # # # # # # # # # 198# 199if test $VERBOSE -gt 1 200then echo Finding directories and links to directories ; fi 201 202# Find all directories and all symlinks that point to directories. 203# Put the list in $all_dirs. 204# Each time we find a symlink, add it to newdirs 205# so that we do another find within the dir the link points to. 206# Note that $all_dirs may have duplicates in it; 207# later parts of this file are supposed to ignore them. 208dirs="." 209levels=2 210all_dirs="" 211search_dirs="" 212 213while [ -n "$dirs" ] && [ $levels -gt 0 ] 214do 215 levels=`expr $levels - 1` 216 newdirs= 217 for d in $dirs 218 do 219 if test $VERBOSE -gt 1 220 then echo " Searching $INPUT/$d" ; fi 221 222 # Find all directories under $d, relative to $d, excluding $d itself. 223 # (The /. is needed after $d in case $d is a symlink.) 224 all_dirs="$all_dirs `find $d/. -type d -print | \ 225 sed -e '/\/\.$/d' -e 's@/./@/@g'`" 226 # Find all links to directories. 227 # Using `-exec test -d' in find fails on some systems, 228 # and trying to run test via sh fails on others, 229 # so this is the simplest alternative left. 230 # First find all the links, then test each one. 231 theselinks= 232 $LINKS && \ 233 theselinks=`find $d/. -type l -print | sed -e 's@/./@/@g'` 234 for d1 in $theselinks --dummy-- 235 do 236 # If the link points to a directory, 237 # add that dir to $newdirs 238 if [ -d $d1 ] 239 then 240 all_dirs="$all_dirs $d1" 241 if [ "`ls -ld $d1 | sed -n 's/.*-> //p'`" != "." ] 242 then 243 newdirs="$newdirs $d1" 244 search_dirs="$search_dirs $d1" 245 fi 246 fi 247 done 248 done 249 250 dirs="$newdirs" 251done 252 253# # # # # # # # # # # # # # # # # # # # # 254# 255dirs= 256if test $VERBOSE -gt 2 257then echo "All directories (including links to directories):" 258 echo $all_dirs 259fi 260 261OLDDIR=`${PWDCMD}` 262cd $LIB 263echo "$all_dirs" | xargs mkdir -p 264cd ${OLDDIR} 265 266mkdir $LIB/root 267 268# # # # # # # # # # # # # # # # # # # # # 269# 270# treetops gets an alternating list 271# of old directories to copy 272# and the new directories to copy to. 273treetops=". ${LIB}" 274 275if $LINKS; then 276 if test $VERBOSE -gt 1 277 then echo 'Making symbolic directory links' ; fi 278 cwd=`${PWDCMD}` 279 280 for sym_link in $search_dirs; do 281 cd ${INPUT} 282 dest=`ls -ld ${sym_link} | sed -n 's/.*-> //p'` 283 284 # In case $dest is relative, get to ${sym_link}'s dir first. 285 # 286 cd ./`echo ${sym_link} | sed 's;/[^/]*$;;'` 287 288 # Check that the target directory exists. 289 # Redirections changed to avoid bug in sh on Ultrix. 290 # 291 (cd $dest) > /dev/null 2>&1 292 if [ $? = 0 ]; then 293 cd $dest 294 295 # full_dest_dir gets the dir that the link actually leads to. 296 # 297 full_dest_dir=`${PWDCMD}` 298 299 # Canonicalize ${INPUT} now to minimize the time an 300 # automounter has to change the result of ${PWDCMD}. 301 # 302 cinput=`cd ${INPUT}; ${PWDCMD}` 303 304 # If a link points to ., make a similar link to . 305 # 306 if [ ${full_dest_dir} = ${cinput} ]; then 307 if test $VERBOSE -gt 2 308 then echo ${sym_link} '->' . ': Making self link' ; fi 309 rm -fr ${LIB}/${sym_link} > /dev/null 2>&1 310 ln -s . ${LIB}/${sym_link} > /dev/null 2>&1 311 312 # If link leads back into ${INPUT}, 313 # make a similar link here. 314 # 315 elif expr ${full_dest_dir} : "${cinput}/.*" > /dev/null; then 316 # Y gets the actual target dir name, relative to ${INPUT}. 317 y=`echo ${full_dest_dir} | sed -n "s&${cinput}/&&p"` 318 # DOTS is the relative path from ${LIB}/${sym_link} back to ${LIB}. 319 dots=`echo "${sym_link}" | 320 sed -e 's@^./@@' -e 's@/./@/@g' -e 's@[^/][^/]*@..@g' -e 's@..$@@'` 321 if test $VERBOSE -gt 2 322 then echo ${sym_link} '->' $dots$y ': Making local link' ; fi 323 rm -fr ${LIB}/${sym_link} > /dev/null 2>&1 324 ln -s $dots$y ${LIB}/${sym_link} > /dev/null 2>&1 325 326 else 327 # If the link is to a dir $target outside ${INPUT}, 328 # repoint the link at ${INPUT}/root$target 329 # and process $target into ${INPUT}/root$target 330 # treat this directory as if it actually contained the files. 331 # 332 if test $VERBOSE -gt 2 333 then echo ${sym_link} '->' root${full_dest_dir} ': Making rooted link' 334 fi 335 if [ -d $LIB/root${full_dest_dir} ] 336 then true 337 else 338 dirname=root${full_dest_dir}/ 339 dirmade=. 340 cd $LIB 341 while [ x$dirname != x ]; do 342 component=`echo $dirname | sed -e 's|/.*$||'` 343 mkdir $component >/dev/null 2>&1 344 cd $component 345 dirmade=$dirmade/$component 346 dirname=`echo $dirname | sed -e 's|[^/]*//*||'` 347 done 348 fi 349 350 # Duplicate directory structure created in ${LIB}/${sym_link} in new 351 # root area. 352 # 353 for file2 in $all_dirs; do 354 case $file2 in 355 ${sym_link}/*) 356 dupdir=${LIB}/root${full_dest_dir}/`echo $file2 | 357 sed -n "s|^${sym_link}/||p"` 358 if test $VERBOSE -gt 2 359 then echo "Duplicating ${sym_link}'s ${dupdir}" ; fi 360 if [ -d ${dupdir} ] 361 then true 362 else 363 mkdir ${dupdir} 364 fi 365 ;; 366 *) 367 ;; 368 esac 369 done 370 371 # Get the path from ${LIB} to ${sym_link}, accounting for symlinks. 372 # 373 parent=`echo "${sym_link}" | sed -e 's@/[^/]*$@@'` 374 libabs=`cd ${LIB}; ${PWDCMD}` 375 file2=`cd ${LIB}; cd $parent; ${PWDCMD} | sed -e "s@^${libabs}@@"` 376 377 # DOTS is the relative path from ${LIB}/${sym_link} back to ${LIB}. 378 # 379 dots=`echo "$file2" | sed -e 's@/[^/]*@../@g'` 380 rm -fr ${LIB}/${sym_link} > /dev/null 2>&1 381 ln -s ${dots}root${full_dest_dir} ${LIB}/${sym_link} > /dev/null 2>&1 382 treetops="$treetops ${sym_link} ${LIB}/root${full_dest_dir}" 383 fi 384 fi 385 done 386fi 387 388# # # # # # # # # # # # # # # # # # # # # 389# 390required= 391set x $treetops 392shift 393while [ $# != 0 ]; do 394 # $1 is an old directory to copy, and $2 is the new directory to copy to. 395 # 396 SRCDIR=`cd ${INPUT} ; cd $1 ; ${PWDCMD}` 397 export SRCDIR 398 399 FIND_BASE=$1 400 export FIND_BASE 401 shift 402 403 DESTDIR=`cd $1;${PWDCMD}` 404 export DESTDIR 405 shift 406 407 # The same dir can appear more than once in treetops. 408 # There's no need to scan it more than once. 409 # 410 if [ -f ${DESTDIR}/DONE ] 411 then continue ; fi 412 413 touch ${DESTDIR}/DONE 414 if test $VERBOSE -gt 1 415 then echo Fixing directory ${SRCDIR} into ${DESTDIR} ; fi 416 417 # Check files which are symlinks as well as those which are files. 418 # 419 cd ${INPUT} 420 required="$required `if $LINKS; then 421 find ${FIND_BASE}/. -name '*.h' \( -type f -o -type l \) -print 422 else 423 find ${FIND_BASE}/. -name '*.h' -type f -print 424 fi | \ 425 sed -e 's;/\./;/;g' -e 's;//*;/;g' | \ 426 ${FIXINCL}`" 427done 428 429## Make sure that any include files referenced using double quotes 430## exist in the fixed directory. This comes last since otherwise 431## we might end up deleting some of these files "because they don't 432## need any change." 433set x `echo $required` 434shift 435while [ $# != 0 ]; do 436 newreq= 437 while [ $# != 0 ]; do 438 # $1 is the directory to copy from, 439 # $2 is the unfixed file, 440 # $3 is the fixed file name. 441 # 442 cd ${INPUT} 443 cd $1 444 if [ -f $2 ] ; then 445 if [ -r $2 ] && [ ! -r $3 ]; then 446 cp $2 $3 >/dev/null 2>&1 || echo "Can't copy $2" >&2 447 chmod +w $3 2>/dev/null 448 chmod a+r $3 2>/dev/null 449 if test $VERBOSE -gt 2 450 then echo Copied $2 ; fi 451 for include in `egrep '^[ ]*#[ ]*include[ ]*"[^/]' $3 | 452 sed -e 's/^[ ]*#[ ]*include[ ]*"\([^"]*\)".*$/\1/'` 453 do 454 dir=`echo $2 | sed -e s'|/[^/]*$||'` 455 dir2=`echo $3 | sed -e s'|/[^/]*$||'` 456 newreq="$newreq $1 $dir/$include $dir2/$include" 457 done 458 fi 459 fi 460 shift; shift; shift 461 done 462 set x $newreq 463 shift 464done 465 466if test $VERBOSE -gt 2 467then echo 'Cleaning up DONE files.' ; fi 468cd $LIB 469# Look for files case-insensitively, for the benefit of 470# DOS/Windows filesystems. 471find . -name '[Dd][Oo][Nn][Ee]' -exec rm -f '{}' ';' 472 473if test $VERBOSE -gt 1 474then echo 'Cleaning up unneeded directories:' ; fi 475cd $LIB 476all_dirs=`find . -type d \! -name '.' -print | sort -r` 477for file in $all_dirs; do 478 if rmdir $LIB/$file > /dev/null 479 then 480 test $VERBOSE -gt 3 && echo " removed $file" 481 fi 482done 2> /dev/null 483 484# On systems which don't support symlinks, `find' may barf 485# if called with "-type l" predicate. So only use that if 486# we know we should look for symlinks. 487if $LINKS; then 488 test $VERBOSE -gt 2 && echo "Removing unused symlinks" 489 490 all_dirs=`find . -type l -print` 491 for file in $all_dirs 492 do 493 if test ! -d $file 494 then 495 rm -f $file 496 test $VERBOSE -gt 3 && echo " removed $file" 497 rmdir `dirname $file` > /dev/null && \ 498 test $VERBOSE -gt 3 && \ 499 echo " removed `dirname $file`" 500 fi 501 done 2> /dev/null 502fi 503 504if test $VERBOSE -gt 0 505then echo fixincludes is done ; fi 506 507# # # # # # # # # # # # # # # # # # # # # 508# 509# End of for INPUT directories 510# 511done 512# 513# # # # # # # # # # # # # # # # # # # # # 514