Pre-ansification changes for lib/cpp
[43bsd.git] / scripts / ansify.sh
1 #!/bin/sh
2
3 ROOT=`dirname $0`/..
4 #ROOT=../..
5 ROOT=`cd $ROOT && pwd`
6 INCLUDE=$ROOT/stage0/usr/include
7 SCRIPTS=$ROOT/scripts
8
9 all_c=`echo *.c`
10 if test "$all_c" = "*.c"
11 then
12   all_c=
13 fi
14 #all_c=`find . -type f -name '*.c' -print |sed -e 's:^\./::' |LC_ALL=C sort`
15 #all_c=`find . -type f -name '*.c' -print |sed -e 's:^\./::' |grep -v '^vax/' |LC_ALL=C sort`
16
17 all_h=`echo *.h`
18 if test "$all_h" = "*.h"
19 then
20   all_h=
21 fi
22 #all_h=`find . -type f -name '*.h' -print |sed -e 's:^\./::' |LC_ALL=C sort`
23 #all_h=`find $INCLUDE -type f -name '*.h' -print |LC_ALL=C sort`
24
25 rm -f ansify.log
26
27 if test -z "$1" || test $1 -eq 0
28 then
29   echo "===stage 0==="
30
31   rm -f *.[ch].allprotos
32   rm -f *.[ch].cproto
33   rm -f *.[ch].nocomm
34   rm -f *.[ch].oldprotos
35   rm -f *.[ch].protos
36   rm -f *.[ch].protos.nocomm
37   rm -f *.[ch].usedby
38   rm -f *.[ch].uses
39   rm -rf .xify
40   #rm -f `find . -name '*.[ch].allprotos' -print`
41   #rm -f `find . -name '*.[ch].cproto' -print`
42   #rm -f `find . -name '*.[ch].nocomm' -print`
43   #rm -f `find . -name '*.[ch].oldprotos' -print`
44   #rm -f `find . -name '*.[ch].protos' -print`
45   #rm -f `find . -name '*.[ch].protos.nocomm' -print`
46   #rm -f `find . -name '*.[ch].usedby' -print`
47   #rm -f `find . -name '*.[ch].uses' -print`
48   #rm -rf `find . -name .xify -print`
49
50   #rm -f `find $INCLUDE -name '*.h.allprotos' -print`
51   rm -f `find $INCLUDE -name '*.h.nocomm' -print`
52   #rm -f `find $INCLUDE -name '*.h.oldprotos' -print`
53   #rm -f `find $INCLUDE -name '*.h.protos' -print`
54   #rm -f `find $INCLUDE -name '*.h.protos.nocomm' -print`
55   rm -f `find $INCLUDE -name '*.h.usedby' -print`
56   #rm -f `find $INCLUDE -name '*.h.uses' -print`
57   rm -rf `find $INCLUDE -name .xify -print`
58
59   rm -f a b conflicts.temp oldprotos.txt oldprotos.temp xx*
60
61   if test "$1" = 000
62   then
63     git checkout $all_c $all_h
64     #rm -rf $ROOT/include $ROOT/sys
65     #git checkout $ROOT/include $ROOT/sys $all_c
66     #rm -rf $INCLUDE
67     #mkdir -p $INCLUDE
68     #(cd $ROOT/include && $SCRIPTS/make.sh SHARED=copies install)
69   fi
70 fi >>ansify.log
71
72 all_c=`echo *.c`
73 if test "$all_c" = "*.c"
74 then
75   all_c=
76 fi
77 #all_c=`find . -type f -name '*.c' -print |sed -e 's:^\./::' |LC_ALL=C sort`
78 #all_c=`find . -type f -name '*.c' -print |sed -e 's:^\./::' |grep -v '^vax/' |LC_ALL=C sort`
79
80 all_h=`echo *.h`
81 if test "$all_h" = "*.h"
82 then
83   all_h=
84 fi
85 #all_h=`find . -type f -name '*.h' -print |sed -e 's:^\./::' |LC_ALL=C sort`
86 #all_h=`find $INCLUDE -type f -name '*.h' -print |LC_ALL=C sort`
87
88 std_h=`find $INCLUDE -type f -name '*.h' -print |grep -v "^$INCLUDE/\(stand\|vaxif\|vaxmba\|vaxuba\)/" |LC_ALL=C sort`
89 #std_h=
90
91 if test -z "$1" || test $1 -eq 1
92 then
93   echo "===stage 1==="
94
95   if test -f uniqify.sed
96   then
97     sed -f uniqify.sed -i $all_c $all_h
98   fi
99
100   for i in $all_c $all_h
101   do
102     echo "i=$i"
103
104     grep -H '^[A-Za-z_][0-9A-Za-z_]*[^0-9A-Za-z_][^;]*[A-Za-z_][0-9A-Za-z_]*[    ]*([    ]*)[    ]*\(,[^;]*\)\?;' $i >>oldprotos.txt
105     sed -e 's/^[A-Za-z_][0-9A-Za-z_]*[^0-9A-Za-z_][^;]*[A-Za-z_][0-9A-Za-z_]*[   ]*([    ]*)[    ]*\(,[^;]*\)\?;/\/\*&\*\//' -i $i
106
107     sed -e 's/^\(#[      ]*\(else\|endif\)\)\([^0-9A-Za-z_].*\)\?/\1/' -i $i
108
109     rm -f xx*
110     csplit -b '%05d' -q $i '/^#[         ]*if.*\(SCCS\|lint\)/' '{*}'
111     for j in xx*
112     do
113       if test $j != xx00000 && sed -ne '2p' $j |grep -q 'char[   ]*\(copyright[  ]*\[[   ]*\]\|\*[       ]*copyright\)[  ]*='
114       then
115         sed -e 's/^\(#[  ]*if\).*\(SCCS\|lint\).*$/\1 defined(DOCOPYRIGHT) \&\& !defined(lint)/' -i $j
116       fi
117       if test $j != xx00000 && sed -ne '2p' $j |grep -q 'char[   ]*\(sccsid[     ]*\[[   ]*\]\|\*[       ]*sccsid\)[     ]*='
118       then
119         sed -e 's/^\(#[  ]*if\).*\(SCCS\|lint\).*$/\1 defined(DOSCCS) \&\& !defined(lint)/' -i $j
120       else
121         sed -e 's/^.*char[       ]*\(sccsid[     ]*\[[   ]*\]\|\*[       ]*sccsid\)[     ]*=.*$/#if defined(DOSCCS) \&\& !defined(lint)\n&\n#endif/' -i $j
122       fi
123     done
124     cat xx* >$i
125  
126     rm -f xx*
127     csplit -b '%05d' -q $i '/^[  ]*{[    ]*$/' '{*}'
128     prev=
129     for j in xx*
130     do
131       if test -n "$prev" && ! sed -ne '$p' $prev |grep -q '^#'
132       then
133         sed -e '$s/$/ {/' -i $prev
134         sed -ne '2,$p' -i $j
135       fi
136       prev=$j
137     done
138     cat xx* >$i
139   done
140
141   grep '\.h:' oldprotos.txt >oldprotos.temp
142
143   sed -e 's/^[A-Za-z_].*__P.*;$/\/\*AAA&\*\//' -i $std_h
144   if ! test -f $INCLUDE/setjmp.h.save
145   then
146     mv $INCLUDE/setjmp.h $INCLUDE/setjmp.h.save
147     mv $INCLUDE/stdarg.h $INCLUDE/stdarg.h.save
148     mv $INCLUDE/stdint.h $INCLUDE/stdint.h.save
149     mv $INCLUDE/varargs.h $INCLUDE/varargs.h.save
150   fi
151   cat <<EOF >$INCLUDE/setjmp.h
152 #ifndef _SETJMP_H_
153 #define _SETJMP_H_
154 typedef int jmp_buf;
155 #endif
156 EOF
157 #  cat <<EOF >$INCLUDE/stdarg.h
158 ##ifndef _STDARG_H_
159 ##define _STDARG_H_
160 #typedef int va_list;
161 ##endif
162 #EOF
163   touch $INCLUDE/stdarg.h
164 #  cat <<EOF >$INCLUDE/stdint.h
165 ##ifndef _STDINT_H_
166 ##define _STDINT_H_
167 #typedef int int8_t;
168 #typedef int int16_t;
169 #typedef int int32_t;
170 #typedef int int64_t;
171 #typedef int intptr_t;
172 #typedef int ssize_t;
173 #typedef int uint8_t;
174 #typedef int uint16_t;
175 #typedef int uint32_t;
176 #typedef int uint64_t;
177 #typedef int uintptr_t;
178 #typedef int size_t;
179 ##endif
180 #EOF
181   touch $INCLUDE/stdint.h
182   cat <<EOF >$INCLUDE/varargs.h
183 #ifndef _VARARGS_H_
184 #define _VARARGS_H_
185 typedef int va_list;
186 #endif
187 EOF
188   for i in $all_c
189   do
190     echo "i=$i"
191
192     group=
193     #group=$INCLUDE/`dirname $i`.h
194     #touch $group
195     if test -f groups.txt
196     then
197       j=`sed -ne "s:^$i \\(.*\\):\\1:p" groups.txt`
198       if test -n "$j"
199       then
200         #j=$INCLUDE/$j
201         #echo $j >>$group.uses
202         group=$j
203         touch $group
204       fi
205     fi
206     echo "group=$group"
207
208     cp $i $i.cproto
209     $ROOT/cproto-4.6/cproto -i$INCLUDE -Dvax -t -H -s $i |sed -ne '2,$p' >$i.allprotos
210     if ! grep -qv '^int main __P((' $i.allprotos
211     then
212       echo -n >$i.allprotos
213     fi
214
215     grep "^static " <$i.allprotos >a
216     if test -s a
217     then
218       mv a $i.protos
219     fi
220  
221     grep -v "^static " <$i.allprotos >a
222     if test -s a
223     then
224       if test -z "$group"
225       then
226         cp $i.allprotos $i.protos
227       else
228         (
229           cat <<EOF
230
231 /* $i */
232 EOF
233           cat a
234         ) >>$group.protos
235       fi
236
237       protos="`sed -ne 's/^.*[^0-9A-Za-z_]\([A-Za-z_][0-9A-Za-z_]*\) __P((.*))\\( NORETURN\\)\\?;$/\1/p' a`"
238       echo "protos=$protos"
239
240       pattern=
241       prefix=
242       for j in $protos
243       do
244         pattern="$pattern$prefix$j"
245         prefix='\|'
246       done
247       echo "pattern=$pattern"
248
249       grep "[^0-9A-Za-z_]\\($pattern\\)[         ]*(" oldprotos.temp >a
250       if test -s a
251       then
252         mv a $i.oldprotos
253         grep -v "^$group:" $i.oldprotos >&2
254       fi
255     fi
256   done
257   mv $INCLUDE/setjmp.h.save $INCLUDE/setjmp.h
258   mv $INCLUDE/stdarg.h.save $INCLUDE/stdarg.h
259   mv $INCLUDE/stdint.h.save $INCLUDE/stdint.h
260   mv $INCLUDE/varargs.h.save $INCLUDE/varargs.h
261   sed -e 's/^\/\*AAA\(.*\)\*\/$/\1/' -i $std_h
262 fi >>ansify.log
263
264 all_h=`echo *.h`
265 if test "$all_h" = "*.h"
266 then
267   all_h=
268 fi
269 #all_h=`find . -type f -name '*.h' -print |sed -e 's:^\./::' |LC_ALL=C sort`
270 #all_h=`find $INCLUDE -type f -name '*.h' -print |LC_ALL=C sort`
271
272 if test -z "$1" || test $1 -eq 2
273 then
274   echo "===stage 2==="
275  
276   all_c_nocomm=
277   all_c_protos_nocomm=
278   for i in $all_c
279   do
280     $SCRIPTS/nocomment <$i |$SCRIPTS/nostring >$i.nocomm
281     all_c_nocomm="$all_c_nocomm $i.nocomm"
282     if test -f $i.protos
283     then
284       $SCRIPTS/nocomment <$i.protos |$SCRIPTS/nostring >$i.protos.nocomm
285       all_c_protos_nocomm="$all_c_protos_nocomm $i.protos.nocomm"
286     fi
287   done
288
289   all_h_nocomm=
290   all_h_protos_nocomm=
291   for i in $all_h
292   do
293     $SCRIPTS/nocomment <$i |$SCRIPTS/nostring >$i.nocomm
294     all_h_nocomm="$all_h_nocomm $i.nocomm"
295     if test -f $i.protos
296     then
297       $SCRIPTS/nocomment <$i.protos |$SCRIPTS/nostring >$i.protos.nocomm
298       all_h_protos_nocomm="$all_h_protos_nocomm $i.protos.nocomm"
299     fi
300   done
301
302   if ! test -f $INCLUDE/setjmp.h.save
303   then
304     mv $INCLUDE/setjmp.h $INCLUDE/setjmp.h.save
305     mv $INCLUDE/stdarg.h $INCLUDE/stdarg.h.save
306     mv $INCLUDE/stdint.h $INCLUDE/stdint.h.save
307     mv $INCLUDE/varargs.h $INCLUDE/varargs.h.save
308   fi
309   cat <<EOF >$INCLUDE/setjmp.h
310 #ifndef _SETJMP_H_
311 #define _SETJMP_H_
312 typedef int jmp_buf;
313 #endif
314 EOF
315 #  cat <<EOF >$INCLUDE/stdarg.h
316 ##ifndef _STDARG_H_
317 ##define _STDARG_H_
318 #typedef int va_list;
319 ##endif
320 #EOF
321   touch $INCLUDE/stdarg.h
322 #  cat <<EOF >$INCLUDE/stdint.h
323 ##ifndef _STDINT_H_
324 ##define _STDINT_H_
325 #typedef int int8_t;
326 #typedef int int16_t;
327 #typedef int int32_t;
328 #typedef int int64_t;
329 #typedef int intptr_t;
330 #typedef int ssize_t;
331 #typedef int uint8_t;
332 #typedef int uint16_t;
333 #typedef int uint32_t;
334 #typedef int uint64_t;
335 #typedef int uintptr_t;
336 #typedef int size_t;
337 ##endif
338 #EOF
339   touch $INCLUDE/stdint.h
340   cat <<EOF >$INCLUDE/varargs.h
341 #ifndef _VARARGS_H_
342 #define _VARARGS_H_
343 typedef int va_list;
344 #endif
345 EOF
346   for i in $std_h
347   do
348     $SCRIPTS/nocomment <$i |$SCRIPTS/nostring >$i.nocomm
349   done
350   mv $INCLUDE/setjmp.h.save $INCLUDE/setjmp.h
351   mv $INCLUDE/stdarg.h.save $INCLUDE/stdarg.h
352   mv $INCLUDE/stdint.h.save $INCLUDE/stdint.h
353   mv $INCLUDE/varargs.h.save $INCLUDE/varargs.h
354
355   if test -f conflicts.txt
356   then
357     sed -e 's/^/:/;s/ /::/g;s/$/:/' <conflicts.txt >conflicts.temp
358   else
359     touch conflicts.temp
360   fi
361
362   for i in $all_h $std_h
363   do
364     echo "i=$i"
365     h=`echo $i |sed -e "s:^$INCLUDE/::"`
366     echo "h=$h"
367
368     macros="`sed -ne 's/^#[      ]*define[       ]\+\([A-Za-z_][0-9A-Za-z_]*\).*/\1/p' $i.nocomm |grep -v '^\(NULL\|__P\)$'`"
369     echo "macros=$macros"
370     typedefs="`sed -ne 's/^\(.*[^0-9A-Za-z_]\)\?typedef[^0-9A-Za-z_]\(.*[^0-9A-Za-z_]\)\?\([A-Za-z_][0-9A-Za-z_]*\)[     ]*\(\[[         0-9]*\][        ]*\)\?;.*/\3/p' $i.nocomm; sed -ne 's/^}[       ]*\([A-Za-z_][0-9A-Za-z_]*\)[   ]*;.*/\1/p' $i.nocomm`"
371     echo "typedefs=$typedefs"
372     structs="`sed -ne 's/^\(.*[^0-9A-Za-z_]\)\?struct[   ]\+\([A-Za-z_][0-9A-Za-z_]*\)[  ]*{.*/\2/p' $i.nocomm`"
373     echo "structs=$structs"
374     unions="`sed -ne 's/^\(.*[^0-9A-Za-z_]\)\?union[     ]\+\([A-Za-z_][0-9A-Za-z_]*\)[  ]*{.*/\2/p' $i.nocomm`"
375     echo "unions=$unions"
376     if test -f $i.protos.nocomm
377     then
378       protos="`sed -ne 's/^.*[^0-9A-Za-z_]\([A-Za-z_][0-9A-Za-z_]*\) __P((.*))\\( NORETURN\\)\\?;$/\1/p' $i.nocomm $i.protos.nocomm`"
379     else
380       protos="`sed -ne 's/^.*[^0-9A-Za-z_]\([A-Za-z_][0-9A-Za-z_]*\) __P((.*))\\( NORETURN\\)\\?;$/\1/p' $i.nocomm`"
381     fi
382     echo "protos=$protos"
383
384     rm -f $i.usedby
385
386     prefix0=
387     pattern0=
388
389     pattern1=
390     prefix1=
391     for j in $macros
392     do
393       pattern1="$pattern1$prefix1$j"
394       prefix1='\|'
395     done
396     for j in $typedefs
397     do
398       pattern1="$pattern1$prefix1$j"
399       prefix1='\|'
400     done
401     for j in $structs
402     do
403       pattern1="$pattern1${prefix1}struct[       ]\\+$j"
404       prefix1='\|'
405     done
406     for j in $unions
407     do
408       pattern1="$pattern1${prefix1}union[        ]\\+$j"
409       prefix1='\|'
410     done
411     echo "pattern1=$pattern1"
412     if test -n "$pattern1"
413     then
414       pattern0="$pattern0$prefix0\\(.*[^0-9A-Za-z_]\\)\\?\\($pattern1\\)\\([^0-9A-Za-z_].*\\)\\?"
415       prefix0='\|'
416     fi
417
418     pattern1=
419     prefix1=
420     for j in $protos
421     do
422       pattern1="$pattern1$prefix1$j"
423       prefix1='\|'
424     done
425     echo "pattern1=$pattern1"
426     if test -n "$pattern1"
427     then
428       pattern0="$pattern0$prefix0\\(.*[^0-9A-Za-z_]\\)\\?\\($pattern1\\)[        ]*(.*"
429       prefix0='\|'
430     fi
431
432     echo "pattern0=$pattern0"
433
434     if test -n "$pattern0" && test -n "$all_c_nocomm$all_c_protos_nocomm$all_h_protos_nocomm"
435     then
436       grep -H "^\\($pattern0\\)$" $all_c_nocomm $all_c_protos_nocomm $all_h_protos_nocomm |grep -v "^$i\.protos.nocomm:" >a
437       if test -s a
438       then
439         cat a >>$i.usedby
440         for j in `sed -e 's/\.protos\.nocomm:.*//; s/\.nocomm:.*//' <a |sort |uniq`
441         do
442           echo "j=$j"
443           k=`echo $j |sed -e "s:^$INCLUDE/::"`
444           echo "k=$k"
445           if ! grep -q ":$h:.*:$k:\\|:$k:.*:$h:" conflicts.temp
446           then
447             echo $i >>$j.uses
448           fi
449         done
450       fi
451     fi
452
453     pattern0=
454     prefix0=
455
456     pattern1=
457     prefix1=
458     for j in $macros
459     do
460       pattern1="$pattern1$prefix1$j"
461       prefix1='\|'
462     done
463     for j in $typedefs
464     do
465       pattern1="$pattern1$prefix1$j"
466       prefix1='\|'
467     done
468     echo "pattern1=$pattern1"
469     if test -n "$pattern1"
470     then
471       pattern0="$pattern0$prefix0\\(.*[^0-9A-Za-z_]\\)\\?\\($pattern1\\)\\([^0-9A-Za-z_].*\\)\\?"
472       prefix0='\|'
473     fi
474
475     pattern1=
476     prefix1=
477     for j in $structs
478     do
479       pattern1="$pattern1${prefix1}struct[       ]\\+$j"
480       prefix1='\|'
481     done
482     for j in $unions
483     do
484       pattern1="$pattern1${prefix1}union[        ]\\+$j"
485       prefix1='\|'
486     done
487     echo "pattern1=$pattern1"
488     if test -n "$pattern1"
489     then
490       pattern0="$pattern0$prefix0\\(.*[^0-9A-Za-z_]\\)\\?\\($pattern1\\)\\([^    *0-9A-Za-z_].*\\|[      ]\\+\\([^*].*\\)\\?\\)\\?"
491       prefix0='\|'
492     fi
493
494     pattern1=
495     prefix1=
496     for j in $protos
497     do
498       pattern1="$pattern1$prefix1$j"
499       prefix1='\|'
500     done
501     echo "pattern1=$pattern1"
502     if test -n "$pattern1"
503     then
504       pattern0="$pattern0$prefix0\\(.*[^0-9A-Za-z_]\\)\\?\\($pattern1\\)[        ]*(.*"
505       prefix0='\|'
506     fi
507
508     echo "pattern0=$pattern0"
509
510     if test -n "$pattern0" && test -n "$all_h_nocomm"
511     then
512       grep -H "^\\($pattern0\\)$" $all_h_nocomm |grep -v "^$i\.nocomm:" >a
513       if test -s a
514       then
515         cat a >>$i.usedby
516         for j in `sed -e 's/\.nocomm:.*//' <a |sort |uniq`
517         do
518           echo "j=$j"
519           k=`echo $j |sed -e "s:^$INCLUDE/::"`
520           echo "k=$k"
521           if ! grep -q ":$h:.*:$k:\\|:$k:.*:$h:" conflicts.temp
522           then
523             echo $i >>$j.uses
524           fi
525         done
526       fi
527     fi
528   done
529 fi >>ansify.log
530
531 if test -z "$1" || test $1 -eq 3
532 then
533   echo "===stage 3==="
534
535   for i in $all_h
536   do
537     echo "i=$i"
538     rm -f xx*
539     csplit -b '%05d' -q $i '/^#/' '{*}'
540     if test -f xx00001 && ! $SCRIPTS/nocomment <xx00000 |grep -q '[^     ]'
541     then
542       for j in xx*
543       do
544         :
545       done
546       echo "j=$j"
547       define="`sed -ne 's/^#[    ]*ifndef[       ]\+\([A-Za-z_][0-9A-Za-z_]*\).*/\1/p' xx00001`"
548       echo "define=$define"
549       if test -n "$define" && grep -q '^#[       ]*endif$' $j && ! grep -v "^#" $j |$SCRIPTS/nocomment |grep -q '[^      ]'
550       then
551         sed -e "s/^#[    ]*ifndef[       ]\\+$define/\\/\\*&\\*\\//" -i xx00001
552         sed -e "s/^\\(#[         ]*define[       ]\\+$define\\)[         ]*$/\\/\\*\1\\*\\//" -i xx00002
553         sed -e 's/^#[    ]*endif$/\/\*&\*\//' -i $j
554         cat xx* >a
555         iflevel=0
556         for j in `sed -ne 's/^#[         ]*\(if\|ifdef\|ifndef\)[^0-9A-Za-z_].*/1/p; s/^#[       ]*endif$/-1/p' a`
557         do
558           iflevel=`expr $iflevel + $j`
559           if test $iflevel -lt 0
560           then
561             break
562           fi
563         done
564         echo "iflevel=$iflevel"
565         if test $iflevel -eq 0
566         then
567           mv a $i
568         fi
569       fi
570     fi
571   done
572
573   for i in $all_c $all_h
574   do
575     echo "i=$i"
576     rm -f xx*
577     csplit -b '%05d' -q $i '/^#[         ]*include[^0-9A-Za-z_]/' '{*}'
578     for j in xx*
579     do
580       sed -e 's/^#[      ]*include[      ]*\(<[^>]*>\|"[^"]*"\)/\/\*&\*\//' -i $j
581       if grep -v "^#[    ]*include[^0-9A-Za-z_]" $j |sed -e '/^#[        ]*if.*\(SCCS\|lint\)/,/^#[      ]*endif/s/.*//' |$SCRIPTS/nocomment |grep -q '[^        ]'
582       then
583         break
584       fi
585     done
586     cat xx* >$i
587   done
588
589   for i in $all_c
590   do
591     echo "i=$i"
592
593     if test -s $i.protos
594     then
595       pattern="`sed -ne '1{s/\*/\\\*/g; s/ __P((.*));/(/p}' $i.allprotos`"
596       echo "pattern=$pattern"
597       rm -f xx*
598       csplit -q $i "/^$pattern/"
599       (
600         cat xx00
601         if test `wc -l <$i.protos` -ge `wc -l <$i.allprotos`
602         then
603           cat <<EOF
604 #ifndef __P
605 #ifdef __STDC__
606 #define __P(args) args
607 #else
608 #define __P(args) ()
609 #endif
610 #endif
611
612 EOF
613         fi
614         cat $i.protos
615         if test -f xx01
616         then
617           echo
618           cat xx01
619         fi
620       ) >$i
621     fi
622
623     rm -f a
624     if test -s $i.uses
625     then
626       LC_ALL=C sort <$i.uses |uniq >a
627
628       pattern=
629       prefix=
630       for j in `sed -e "s:^$INCLUDE/::" a`
631       do
632         pattern="$pattern$prefix$j"
633         prefix='\|'
634       done
635       echo "pattern=$pattern"
636
637       sed -e "s:^#[      ]*include[      ]*\\(<\\($pattern\\)>\\|\"\\($pattern\\)\"\\):\\/\\*&\\*\\/:" -i $i
638       #sed -e "s:^#[     ]*include[      ]*<\\($pattern\\)>:\\/\\*&\\*\\/:" -i $i
639     fi
640
641     (
642       if test -s a
643       then
644         sed -e "s:.*:#include \"&\":; s:\"$INCLUDE/\\(.*\\)\":<\\1>:; s:^#include <varargs.h>$:#ifdef __STDC__\\n#include <stdarg.h>\\n#define _va_start(argp, arg) va_start(argp, arg)\\n#else\\n#include <varargs.h>\\n#define _va_start(argp, arg) va_start(argp)\\n#endif:" a
645         echo
646       fi
647       sed -e 's/\([^0-9A-Za-z_]\)va_start(argp);/\1_va_start(argp, fmt);/' $i
648     ) |$SCRIPTS/newline >b
649     mv b $i
650   done
651
652   for i in $all_h
653   do
654     echo "i=$i"
655     h=`echo $i |sed -e "s:^$INCLUDE/::"`
656     echo "h=$h"
657
658     rm -f a
659     if test -s $i.uses
660     then
661       LC_ALL=C sort <$i.uses |uniq >a
662
663       pattern=
664       prefix=
665       for j in `sed -e "s:^$INCLUDE/::" a`
666       do
667         pattern="$pattern$prefix$j"
668         prefix='\|'
669       done
670       echo "pattern=$pattern"
671
672       sed -e "s:^#[      ]*include[      ]*\\(<\\($pattern\\)>\\|\"\\($pattern\\)\"\\):\\/\\*&\\*\\/:" -i $i
673       #sed -e "s:^#[     ]*include[      ]*<\\($pattern\\)>:\\/\\*&\\*\\/:" -i $i
674     fi
675
676     name=_`echo $h |tr '.\-/abcdefghijklmnopqrstuvwxyz' '___ABCDEFGHIJKLMNOPQRSTUVWXYZ'`_
677     echo "name=$name"
678     (
679       cat <<EOF
680 #ifndef $name
681 #define $name
682
683 EOF
684       if test -s a
685       then
686         sed -e "s:.*:#include \"&\":; s:\"$INCLUDE/\\(.*\\)\":<\\1>:; s:^#include <varargs.h>$:#ifdef __STDC__\\n#include <stdarg.h>\\n#endif:" a
687         echo
688       fi
689       cat $i
690       if test -s $i.protos
691       then
692         echo
693         #if echo $h |grep -q '^\(net\|netimp\|netinet\|netns\|sys\|vax\|vaxif\|vaxmba\|vaxuba\)/'
694         #then
695         #  echo "#ifndef KERNEL"
696         #fi
697         cat <<EOF
698 #ifndef __P
699 #ifdef __STDC__
700 #define __P(args) args
701 #else
702 #define __P(args) ()
703 #endif
704 #endif
705 EOF
706         cat $i.protos
707         #if echo $h |grep -q '^\(net\|netimp\|netinet\|netns\|sys\|vax\|vaxif\|vaxmba\|vaxuba\)/'
708         #then
709         #  echo "#endif"
710         #fi
711       fi
712       cat <<EOF
713
714 #endif
715 EOF
716     ) |$SCRIPTS/newline >b
717     mv b $i
718   done
719 fi >>ansify.log
720
721 if test -z "$1" || test $1 -eq 4
722 then
723   echo "===stage 4==="
724
725   # checking header loops
726   if test -n "$all_h"
727   then
728     grep -H '^#include \(<[^>]*>\|"[^"]*"\)' $all_h |\
729     sed -e 's:^\(.*\)\:#include \(<\([^>]*\)>\|"\([^"]*\)"\):\1 \3\4:' |\
730     tsort >a
731     #grep -H '^#include <[^>]*>' $all_h |\
732     #sed -e "s:^$INCLUDE/::; s:^\\(.*\\)\\:#include <\\([^>]*\\)>:\1 \2:" |\
733     #tsort >a
734   fi
735
736   # removing useless extra header inclusions that are pulled in anyway
737   rm -f a
738   for i in $all_h $std_h
739   do
740     echo "i=$i"
741     h=`echo $i |sed -e "s:^$INCLUDE/::"`
742     echo "h=$h"
743
744     pattern=
745     prefix=
746     cp $i temp.c
747     for j in `cpp -nostdinc -I$INCLUDE -Dvax -DNCMD=1 -DNRSP=1 -M temp.c`
748     do
749       echo "j=$j"
750       case $j in
751       $INCLUDE/stdarg.h)
752         # system header files are not supposed to define stdarg.h, even if
753         # (like vfprintf etc) they have va_list paramters, it is silly and
754         # we ignore this restriction, but clients shouldn't rely on this
755         # (anyway, the above cpp command defines __STDC__ so we do not pick
756         # up any dependency on varargs.h, hence let's keep it symmetrical)
757         ;;
758       *.h)
759         k=`echo $j |sed -e "s:^$INCLUDE/::"`
760         echo "k=$k"
761         echo "$h $k" >>a
762         pattern="$pattern$prefix$k"
763         prefix='\|'
764         ;;
765       esac
766     done
767     echo "pattern=$pattern"
768     if test -n "$pattern"
769     then
770       grep -H "^#include \\(<$h>\\|\"$h\"\\)$" $all_c $all_h >b
771       if test -s b
772       then
773         sed -e "s:^#include \\(<\\($pattern\\)>\\|\"\\($pattern\\)\"\\)$:/\\*& $h\\*/:" -i `sed -e 's/:.*//' b`
774         #sed -e "s:^#include <\\($pattern\\)>$:/\\*& $h\\*/:" -i `sed -e 's/:.*//' b`
775       fi
776     fi
777   done
778   rm -f temp.c
779   tsort <a >b
780 fi >>ansify.log
781
782 #if test -z "$1" || test $1 -eq 5
783 #then
784 #  echo "===stage 5==="
785 #  for i in . arpa protocols
786 #  do
787 #    echo "i=$i"
788 #    for j in $ROOT/include/$i/*.h
789 #    do
790 #      echo "j=$j"
791 #      cp $INCLUDE/$i/`basename $j` $ROOT/include/$i
792 #    done
793 #  done
794 #  for i in compat-4.1 compat-sys5 gen inet net ns stdio sys
795 #  do
796 #    echo "i=$i"
797 #    cp $INCLUDE/$i.h $ROOT/include
798 #  done
799 #  for i in net netimp netinet netns vax vaxif vaxmba vaxuba
800 #  do
801 #    echo "i=$i"
802 #    for j in $ROOT/sys/$i/*.h
803 #    do
804 #      echo "j=$j"
805 #      cp $INCLUDE/$i/`basename $j` $ROOT/sys/$i
806 #    done
807 #  done
808 #  for i in $ROOT/sys/h/*.h
809 #  do
810 #    echo "i=$i"
811 #    cp $INCLUDE/sys/`basename $i` $ROOT/sys/h
812 #  done
813 #fi >>ansify.log