OSDN Git Service

Initial revision
[pf3gnuchains/gcc-fork.git] / gcc / fixincludes
1 #! /bin/sh
2 # Install modified versions of certain ANSI-incompatible system header files
3 # which are fixed to work correctly with ANSI C
4 # and placed in a directory that GNU C will search.
5
6 # See README-fixinc for more information.
7
8 # Directory containing the original header files.
9 # (This was named INCLUDES, but that conflicts with a name in Makefile.in.)
10 INPUT=${2-${INPUT-/usr/include}}
11
12 # This prevents /bin/ex from failing if the current terminal type is
13 # unrecognizable.
14 TERM=unknown
15 export TERM
16
17 # Directory in which to store the results.
18 LIB=${1-${LIB-/usr/local/lib/gcc-include}}
19
20 # Make sure it exists.
21 if [ ! -d $LIB ]; then
22   mkdir $LIB || exit 1
23 fi
24
25 # Make LIB absolute.
26 cd $LIB; LIB=`pwd`
27
28 # Fail if no arg to specify a directory for the output.
29 if [ x$1 = x ]
30 then echo fixincludes: no output directory specified
31 exit 1
32 fi
33
34 echo 'Building fixincludes in ' ${LIB}
35
36 # Determine whether this system has symbolic links.
37 if ln -s X $LIB/ShouldNotExist 2>/dev/null; then
38   rm -f $LIB/ShouldNotExist
39   LINKS=true
40 else
41   LINKS=false
42 fi
43
44 echo 'Making directories:'
45 cd ${INPUT}
46 if $LINKS; then
47   files=`ls -LR | sed -n s/:$//p`
48 else
49   files=`find . -type d -print | sed '/^.$/d'`
50 fi
51 for file in $files; do
52   rm -rf $LIB/$file
53   if [ ! -d $LIB/$file ]
54   then mkdir $LIB/$file
55   fi
56 done
57
58 # treetops gets an alternating list
59 # of old directories to copy
60 # and the new directories to copy to.
61 treetops="${INPUT} ${LIB}"
62
63 if $LINKS; then
64   echo 'Making internal symbolic directory links'
65   for file in $files; do
66     dest=`ls -ld $file | sed -n 's/.*-> //p'`
67     if [ "$dest" ]; then    
68       cwd=`pwd`
69       # In case $dest is relative, get to $file's dir first.
70       cd ${INPUT}
71       cd `echo ./$file | sed -n 's&[^/]*$&&p'`
72       # Check that the target directory exists.
73       # Redirections changed to avoid bug in sh on Ultrix.
74       (cd $dest) > /dev/null 2>&1
75       if [ $? = 0 ]; then
76         cd $dest
77         # X gets the dir that the link actually leads to.
78         x=`pwd`
79         # If link leads back into ${INPUT},
80         # make a similar link here.
81         if expr $x : "${INPUT}/.*" > /dev/null; then
82           # Y gets the actual target dir name, relative to ${INPUT}.
83           y=`echo $x | sed -n "s&${INPUT}/&&p"`
84           echo $file '->' $y ': Making link'
85           rm -fr ${LIB}/$file > /dev/null 2>&1
86           ln -s ${LIB}/$y ${LIB}/$file > /dev/null 2>&1
87         else
88           # If the link is to outside ${INPUT},
89           # treat this directory as if it actually contained the files.
90 # This line used to have $dest instead of $x.
91 # $dest seemed to be wrong for links found in subdirectories
92 # of ${INPUT}.  Does this change break anything?
93           treetops="$treetops $x ${LIB}/$file"
94         fi
95       fi
96       cd $cwd
97     fi
98   done
99 fi
100
101 set - $treetops
102 while [ $# != 0 ]; do
103   # $1 is an old directory to copy, and $2 is the new directory to copy to.
104   echo "Finding header files in $1:"
105   cd ${INPUT}
106   cd $1
107   files=`find . -name '*.h' -type f -print`
108   echo 'Checking header files:'
109 # Note that BSD43_* are used on recent MIPS systems.
110   for file in $files; do
111 # This call to egrep is essential, since checking a file with egrep
112 # is much faster than actually trying to fix it.
113 # But the argument to egrep must be kept small, or many versions of egrep
114 # won't be able to handle it.
115     if egrep '[         _]_IO|CTRL|#define.NULL|#[el]*if.*([0-9]|#e[nl]|sparc|vax|sun|pyr)' $file > /dev/null; then
116       echo Fixing $file
117       if [ -r $file ]; then
118         cp $file $2/$file >/dev/null 2>&1       \
119         || echo "Can't copy $file"
120         chmod +w $2/$file
121         sed -e '
122                                    :loop
123           /\\$/                 N
124           /\\$/                 b loop
125           s%\(#[        ]*endif[        ]*\)\([^/       ].*\)$%\1/* \2 */%
126           s%\(#[        ]*else[         ]*\)\([^/       ].*\)$%\1/* \2 */%
127           /[    ]_IO[A-Z]*[     ]*(/    s/(\(.\),/('\''\1'\'',/
128           /[    ]BSD43__IO[A-Z]*[       ]*(/    s/(\(.\),/('\''\1'\'',/
129           /#define._IO/         s/'\''x'\''/x/g
130           /#define.BSD43__IO/           s/'\''x'\''/x/g
131           /[^A-Z]CTRL[  ]*(/    s/\([^'\'']\))/'\''\1'\'')/
132           /#define.CTRL/                s/'\''c'\''/c/g
133           /#define._CTRL/               s/'\''c'\''/c/g
134           /#define.BSD43_CTRL/          s/'\''c'\''/c/g
135           /#[a-z]*if.*[  (]m68k/                s/\([^_]\)m68k/\1__m68k__/g
136           /#[a-z]*if.*[  (]__i386/      s/__i386/__i386__/g
137           /#[a-z]*if.*[  (]i386/                s/\([^_]\)i386/\1__i386__/g
138           /#[a-z]*if.*[  (]sparc/       s/\([^_]\)sparc/\1__sparc__/g
139           /#[a-z]*if.*[  (]mc68000/     s/\([^_]\)mc68000/\1__mc68000__/g
140           /#[a-z]*if.*[  (]vax/         s/\([^_]\)vax/\1__vax__/g
141           /#[a-z]*if.*[  (]sun/         s/\([^_]\)\(sun[a-z0-9]*\)\([^a-z0-9_]\)/\1__\2__\3/g
142           /#[a-z]*if.*[  (]sun/         s/\([^_]\)\(sun[a-z0-9]*\)$/\1__\2__/g
143           /#[a-z]*if.*[  (]ns32000/     s/\([^_]\)ns32000/\1__ns32000__/g
144           /#[a-z]*if.*[  (]pyr/         s/\([^_]\)pyr/\1__pyr__/g
145           /#[a-z]*if.*[  (]is68k/       s/\([^_]\)is68k/\1__is68k__/g
146           /#define.NULL[        ]/      i\
147                 #undef NULL
148         ' $2/$file > $2/$file.sed
149         mv $2/$file.sed $2/$file
150         if cmp $file $2/$file >/dev/null 2>&1; then
151            echo Deleting $2/$file\; no fixes were needed.
152            rm $2/$file
153         fi
154       fi
155     fi
156   done
157   shift; shift
158 done
159
160 cd ${INPUT}
161
162 # Fix one other error in this file: a mismatched quote not inside a C comment.
163 file=sundev/vuid_event.h
164 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
165   mkdir ${LIB}/sundev 2>/dev/null
166   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
167   chmod +w ${LIB}/$file 2>/dev/null
168 fi
169
170 if [ -r ${LIB}/$file ]; then
171   echo Fixing $file comment
172   ex ${LIB}/$file <<EOF
173   g/doesn't/s/doesn't/does not/
174   wq
175 EOF
176   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
177     echo Deleting ${LIB}/$file\; no fixes were needed.
178     rm ${LIB}/$file
179   fi
180 fi
181
182 # Fix this Sun file to avoid intefering with stddef.h.
183 file=sys/stdtypes.h
184 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
185   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
186   chmod +w ${LIB}/$file 2>/dev/null
187 fi
188
189 if [ -r ${LIB}/$file ]; then
190   echo Fixing $file
191   ex ${LIB}/$file <<EOF
192   /size_t.*;/
193   i
194 #ifndef _SIZE_T
195 #define _SIZE_T
196 .
197   /size_t/+1
198   i
199 #endif
200 .
201   /ptrdiff_t.*;/
202   i
203 #ifndef _PTRDIFF_T
204 #define _PTRDIFF_T
205 .
206   /ptrdiff_t/+1
207   i
208 #endif
209 .
210   /wchar_t.*;/
211   i
212 #ifndef _WCHAR_T
213 #define _WCHAR_T
214 .
215   /wchar_t/+1
216   i
217 #endif
218 .
219   wq
220 EOF
221   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
222     echo Deleting ${LIB}/$file\; no fixes were needed.
223     rm ${LIB}/$file
224   fi
225 fi
226
227 # Fix this file to avoid intefering with stddef.h.
228 file=sys/types.h
229 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
230   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
231   chmod +w ${LIB}/$file 2>/dev/null
232 fi
233
234 if [ -r ${LIB}/$file ]; then
235   echo Fixing $file
236   ex ${LIB}/$file <<EOF
237   /size_t.*;/
238   i
239 #ifndef _SIZE_T
240 #define _SIZE_T
241 .
242   /size_t/+1
243   i
244 #endif
245 .
246   wq
247 EOF
248   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
249     echo Deleting ${LIB}/$file\; no fixes were needed.
250     rm ${LIB}/$file
251   fi
252 fi
253
254 # Fix an error in this file: a missing semi-colon at the end of the statsswtch
255 # structure definition.
256 file=rpcsvc/rstat.h
257 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
258   mkdir ${LIB}/rpcsvc 2>/dev/null
259   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
260   chmod +w ${LIB}/$file 2>/dev/null
261 fi
262
263 if [ -r ${LIB}/$file ]; then
264   echo Fixing $file, definition of statsswtch
265   ex ${LIB}/$file <<EOF
266   g/boottime$/s//&;/
267   wq
268 EOF
269   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
270     echo Deleting ${LIB}/$file\; no fixes were needed.
271     rm ${LIB}/$file
272   fi
273 fi
274
275 # Fix an error in this file: a missing semi-colon at the end of the nodeent
276 # structure definition.
277 file=netdnet/dnetdb.h
278 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
279   mkdir ${LIB}/netdnet 2>/dev/null
280   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
281   chmod +w ${LIB}/$file 2>/dev/null
282 fi
283
284 if [ -r ${LIB}/$file ]; then
285   echo Fixing $file, definition of nodeent
286   ex ${LIB}/$file <<EOF
287   g/na_addr/s//&;/
288   wq
289 EOF
290   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
291     echo Deleting ${LIB}/$file\; no fixes were needed.
292     rm ${LIB}/$file
293   fi
294 fi
295
296 # Check for bad #ifdef line (in Ultrix 4.1)
297 file=sys/file.h
298 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
299   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
300   chmod +w ${LIB}/$file 2>/dev/null
301 fi
302
303 if [ -r ${LIB}/$file ]; then
304   echo Fixing $file, bad \#ifdef line
305   ex ${LIB}/$file <<EOF
306   g/^#ifdef KERNEL && !defined/
307   s/#ifdef KERNEL && !defined/#if defined(KERNEL) \&\& !defined/
308   wq
309 EOF
310   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
311     echo Deleting ${LIB}/$file\; no fixes were needed.
312     rm ${LIB}/$file
313   fi
314 fi
315
316 # Check for superfluous `static' (in Ultrix 4.2)
317 file=machine/cpu.h
318 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
319   mkdir ${LIB}/machine 2>/dev/null
320   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
321   chmod +w ${LIB}/$file 2>/dev/null
322 fi
323
324 if [ -r ${LIB}/$file ]; then
325   echo Fixing $file, superfluous static
326   ex ${LIB}/$file <<EOF
327   g/^static struct tlb_pid_state/
328   s/static//
329   wq
330 EOF
331   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
332     echo Deleting ${LIB}/$file\; no fixes were needed.
333     rm ${LIB}/$file
334   else
335 # This file has an alternative name, mips/cpu.h.  Fix that name, too.
336     if cmp machine/cpu.h mips/cpu.h > /dev/null 2>& 1; then
337       mkdir ${LIB}/mips 2>&-
338       ln ${LIB}/$file ${LIB}/mips/cpu.h 
339     fi
340   fi
341 fi
342
343 # Deal with yet another challenge, this in X11/Xmu.h
344 file=X11/Xmu.h
345 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
346   mkdir ${LIB}/X11 2>/dev/null
347   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
348   chmod +w ${LIB}/$file 2>/dev/null
349 fi
350
351 if [ -r ${LIB}/$file ]; then
352   echo Fixing $file sprintf declaration
353   ex ${LIB}/$file <<EOF
354   /^extern char \*      sprintf();$/c
355 #ifndef __STDC__
356 extern char *   sprintf();
357 #endif /* !defined __STDC__ */
358 .
359   wq
360 EOF
361   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
362     echo Deleting ${LIB}/$file\; no fixes were needed.
363     rm ${LIB}/$file
364   fi
365 fi
366
367 # Check for missing ';' in struct
368 file=netinet/ip.h
369 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
370   mkdir ${LIB}/netinet 2>/dev/null
371   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
372   chmod +w ${LIB}/$file 2>/dev/null
373 fi
374
375 if [ -r ${LIB}/$file ]; then
376   echo Fixing $file
377   sed -e '/^struct/,/^};/s/}$/};/' ${LIB}/$file > ${LIB}/${file}.sed
378   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
379   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
380     echo Deleting ${LIB}/$file\; no fixes were needed.
381     rm -f ${LIB}/$file
382   fi
383 fi
384
385 # Fix the CAT macro in memvar.h.
386 file=pixrect/memvar.h
387 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
388   mkdir ${LIB}/pixrect 2>/dev/null
389   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
390   chmod +w ${LIB}/$file 2>/dev/null
391 fi
392
393 if [ -r ${LIB}/$file ]; then
394   echo Fixing $file
395   sed -e '/^#define.CAT(a,b)/ i\
396 #ifdef __STDC__ \
397 #define CAT(a,b) a##b\
398 #else
399 /^#define.CAT(a,b)/ a\
400 #endif
401 ' ${LIB}/$file > ${LIB}/${file}.sed
402   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
403   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
404     echo Deleting ${LIB}/$file\; no fixes were needed.
405     rm -f ${LIB}/$file
406   fi
407 fi
408
409 # Check for yet more missing ';' in struct (in SunOS 4.0.x)
410 file=rpcsvc/rusers.h
411 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
412   mkdir ${LIB}/rpcsvc 2>/dev/null
413   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
414   chmod +w ${LIB}/$file 2>/dev/null
415 fi
416
417 if [ -r ${LIB}/$file ]; then
418   echo Fixing $file
419   sed -e '/^struct/,/^};/s/_cnt$/_cnt;/' ${LIB}/$file > ${LIB}/${file}.sed
420   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
421   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
422     echo Deleting ${LIB}/$file\; no fixes were needed.
423     rm -f ${LIB}/$file
424   fi
425 fi
426
427 # Fix return type of exit and abort in <stdlib.h> on SunOS 4.1.
428 file=stdlib.h
429 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
430   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
431   chmod +w ${LIB}/$file 2>/dev/null
432 fi
433
434 if [ -r ${LIB}/$file ]; then
435   echo Fixing $file
436   sed -e 's/int abort/void      abort/g' \
437   -e 's/int     exit/void       exit/g' ${LIB}/$file > ${LIB}/${file}.sed
438   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
439   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
440     echo Deleting ${LIB}/$file\; no fixes were needed.
441     rm -f ${LIB}/$file
442   fi
443 fi
444
445 # Fix bogus comment in <locale.h> on SunOS 4.1.
446 file=locale.h
447 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
448   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
449   chmod +w ${LIB}/$file 2>/dev/null
450 fi
451
452 if [ -r ${LIB}/$file ]; then
453   echo Fixing $file
454   sed -e 's%#endif / \*%#endif /\* %g' ${LIB}/$file > ${LIB}/${file}.sed
455   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
456   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
457     echo Deleting ${LIB}/$file\; no fixes were needed.
458     rm -f ${LIB}/$file
459   fi
460 fi
461
462 # Fix bogus #ifdef in <hsfs/hsfs_spec.h> on SunOS 4.1.
463 file=hsfs/hsfs_spec.h
464 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
465   mkdir ${LIB}/hsfs 2>/dev/null
466   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
467   chmod +w ${LIB}/$file 2>/dev/null
468 fi
469
470 if [ -r ${LIB}/$file ]; then
471   echo Fixing $file
472   sed -e 's/\#ifdef __i386__ || __vax__/\#if __i386__ || __vax__/g' \
473     ${LIB}/$file > ${LIB}/${file}.sed
474   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
475   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
476     echo Deleting ${LIB}/$file\; no fixes were needed.
477     rm -f ${LIB}/$file
478   fi
479 fi
480
481 # Fix bogus #ifdef in <hsfs/hsnode.h> on SunOS 4.1.
482 file=hsfs/hsnode.h
483 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
484   mkdir ${LIB}/hsfs 2>/dev/null
485   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
486   chmod +w ${LIB}/$file 2>/dev/null
487 fi
488
489 if [ -r ${LIB}/$file ]; then
490   echo Fixing $file
491   sed -e 's/\#ifdef __i386__ || __sun4c__/\#if __i386__ || __sun4c__/g' \
492     ${LIB}/$file > ${LIB}/${file}.sed
493   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
494   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
495     echo Deleting ${LIB}/$file\; no fixes were needed.
496     rm -f ${LIB}/$file
497   fi
498 fi
499
500 # Fix bogus #ifdef in <hsfs/iso_spec.h> on SunOS 4.1.
501 file=hsfs/iso_spec.h
502 if [ -r $file ] && [ ! -r ${LIB}/$file ]; then
503   mkdir ${LIB}/hsfs 2>/dev/null
504   cp $file ${LIB}/$file >/dev/null 2>&1 || echo "Can't copy $file"
505   chmod +w ${LIB}/$file 2>/dev/null
506 fi
507
508 if [ -r ${LIB}/$file ]; then
509   echo Fixing $file
510   sed -e 's/\#ifdef __i386__ || __vax__/\#if __i386__ || __vax__/g' \
511     ${LIB}/$file > ${LIB}/${file}.sed
512   rm -f ${LIB}/$file; mv ${LIB}/${file}.sed ${LIB}/$file
513   if cmp $file ${LIB}/$file >/dev/null 2>&1; then
514     echo Deleting ${LIB}/$file\; no fixes were needed.
515     rm -f ${LIB}/$file
516   fi
517 fi
518
519 echo 'Removing unneeded directories:'
520 cd $LIB
521 files=`find . -type d -print | sort -r`
522 for file in $files; do
523   rmdir $LIB/$file > /dev/null 2>&1
524 done
525
526 if $LINKS; then
527   echo 'Making internal symbolic non-directory links'
528   cd ${INPUT}
529   files=`find . -type l -print`
530   for file in $files; do
531     dest=`ls -ld $file | sed -n 's/.*-> //p'`
532     if expr "$dest" : '[^/].*' > /dev/null; then    
533       target=${LIB}/`echo file | sed "s|[^/]*\$|$dest|"`
534       if [ -f $target ]; then
535         ln -s $dest ${LIB}/$file >/dev/null 2>&1
536       fi
537     fi
538   done
539 fi
540
541 exit 0