This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
metaconfig unit change for #14807.
[metaconfig.git] / U / modified / Signal.U
1 ?RCS: $Id: Signal.U,v 3.0.1.1 1997/02/28 15:20:01 ram Exp $
2 ?RCS:
3 ?RCS: Copyright (c) 1991-1993, Raphael Manfredi
4 ?RCS: 
5 ?RCS: You may redistribute only under the terms of the Artistic Licence,
6 ?RCS: as specified in the README file that comes with the distribution.
7 ?RCS: You may reuse parts of this distribution only within the terms of
8 ?RCS: that same Artistic Licence; a copy of which may be found at the root
9 ?RCS: of the source tree for dist 3.0.
10 ?RCS:
11 ?RCS: $Log: Signal.U,v $
12 ?RCS: Revision 3.0.1.1  1997/02/28  15:20:01  ram
13 ?RCS: patch61: created
14 ?RCS:
15 ?X:
16 ?X: This unit produces three files:
17 ?X: 1- A signal.c file, which, when compiled and run, produces an output like:
18 ?X:
19 ?X:   HUP 1
20 ?X:   INT 2
21 ?X:   QUIT 3
22 ?X:   etc...
23 ?X:
24 ?X: 2- A signal.awk script to parse the output of signal.c, fill
25 ?X: in gaps (up to NSIG) and move duplicates to the end.
26 ?X: 3- A signal_cmd script to compile signal.c and run it
27 ?X: through sort -n -k 2 | uniq | awk -f signal.awk.
28 ?X: (This is called signal_cmd to avoid OS/2 confusion with
29 ?X: signal.cmd vs. signal.
30 ?X: The signal_cmd script also falls back on checking signals one at a
31 ?X: time in case the signal.c program fails.  On at least one version of
32 ?X: Linux 2.1.x, the header file #define'd SIGRTMAX to a symbol that 
33 ?X: is not defined by the compiler/linker. :-(.  Further, on that same
34 ?X: version of Linux, the user had a defective C-shell that gave an 
35 ?X: incorrect list for kill -l, so the fall-back didn't work.
36 ?X:
37 ?X: This unit is then used by sig_name.U.
38 ?X:
39 ?MAKE:Signal: test tr rm awk cat grep startsh eunicefix sed sort uniq \
40         Findhdr cppstdin +cppflags cppminus Compile _o _exe trnl run
41 ?MAKE:  -pick add $@ %<
42 ?X:all files declared as "public" since they're used from other units
43 ?F:signal.c signal_cmd signal.lst signal signal.awk
44 ?T: xx xxx xxxfiles
45 : Trace out the files included by signal.h, then look for SIGxxx names.
46 : Remove SIGARRAYSIZE used by HPUX.
47 : Remove SIGSTKSIZE used by Linux.
48 : Remove SIGSTKSZ used by Posix.
49 : Remove SIGTYP void lines used by OS2.
50 : Some cpps, like os390, dont give the file name anywhere
51 if [ "X$fieldn" = X ]; then
52         : Just make some guesses.  We check them later.
53         xxx='/usr/include/signal.h /usr/include/sys/signal.h'
54 else
55         xxx=`echo '#include <signal.h>' |
56         $cppstdin $cppminus $cppflags 2>/dev/null |
57         $grep '^[       ]*#.*include' | 
58         $awk "{print \\$$fieldn}" | $sed 's!"!!g' | $sed 's!\\\\\\\\!/!g' | $sort | $uniq`
59 fi
60 : Check this list of files to be sure we have parsed the cpp output ok.
61 : This will also avoid potentially non-existent files, such 
62 : as ../foo/bar.h
63 xxxfiles=''
64 ?X: Add /dev/null in case the $xxx list is empty.
65 for xx in $xxx /dev/null ; do
66         $test -f "$xx" && xxxfiles="$xxxfiles $xx"
67 done
68 : If we have found no files, at least try signal.h
69 case "$xxxfiles" in
70 '')     xxxfiles=`./findhdr signal.h` ;;
71 esac
72 xxx=`awk '
73 $1 ~ /^#define$/ && $2 ~ /^SIG[A-Z0-9]*$/ && $2 !~ /SIGARRAYSIZE/ && $2 !~ /SIGSTKSIZE/ && $2 !~ /SIGSTKSZ/ && $3 !~ /void/ {
74         print substr($2, 4, 20)
75 }
76 $1 == "#" && $2 ~ /^define$/ && $3 ~ /^SIG[A-Z0-9]*$/ && $3 !~ /SIGARRAYSIZE/ && $4 !~ /void/ {
77         print substr($3, 4, 20)
78 }' $xxxfiles`
79 : Append some common names just in case the awk scan failed.
80 xxx="$xxx ABRT ALRM BUS CANCEL CHLD CLD CONT DIL EMT FPE"
81 xxx="$xxx FREEZE HUP ILL INT IO IOT KILL LOST LWP PHONE"
82 xxx="$xxx PIPE POLL PROF PWR QUIT RTMAX RTMIN SEGV STKFLT STOP"
83 xxx="$xxx SYS TERM THAW TRAP TSTP TTIN TTOU URG USR1 USR2"
84 xxx="$xxx USR3 USR4 VTALRM WAITING WINCH WIND WINDOW XCPU XFSZ"
85
86 : generate a few handy files for later
87 $cat > signal.c <<'EOCP'
88 #include <sys/types.h>
89 #include <signal.h>
90 #include <stdio.h>
91 int main() {
92
93 /* Strange style to avoid deeply-nested #if/#else/#endif */
94 #ifndef NSIG
95 #  ifdef _NSIG
96 #    define NSIG (_NSIG)
97 #  endif
98 #endif
99
100 #ifndef NSIG
101 #  ifdef SIGMAX
102 #    define NSIG (SIGMAX+1)
103 #  endif
104 #endif
105
106 #ifndef NSIG
107 #  ifdef SIG_MAX
108 #    define NSIG (SIG_MAX+1)
109 #  endif
110 #endif
111
112 #ifndef NSIG
113 #  ifdef MAXSIG
114 #    define NSIG (MAXSIG+1)
115 #  endif
116 #endif
117
118 #ifndef NSIG
119 #  ifdef MAX_SIG
120 #    define NSIG (MAX_SIG+1)
121 #  endif
122 #endif
123
124 #ifndef NSIG
125 #  ifdef SIGARRAYSIZE
126 #    define NSIG (SIGARRAYSIZE+1) /* Not sure of the +1 */
127 #  endif
128 #endif
129
130 #ifndef NSIG
131 #  ifdef _sys_nsig
132 #    define NSIG (_sys_nsig) /* Solaris 2.5 */
133 #  endif
134 #endif
135
136 /* Default to some arbitrary number that's big enough to get most
137    of the common signals.
138 */
139 #ifndef NSIG
140 #    define NSIG 50
141 #endif
142
143 printf("NSIG %d\n", NSIG);
144
145 #ifndef JUST_NSIG
146
147 EOCP
148
149 echo $xxx | $tr ' ' $trnl | $sort | $uniq | $awk '
150 {
151         printf "#ifdef SIG"; printf $1; printf "\n"
152         printf "printf(\""; printf $1; printf " %%d\\n\",SIG";
153         printf $1; printf ");\n"
154         printf "#endif\n"
155 }
156 END {
157         printf "#endif /* JUST_NSIG */\n";
158         printf "exit(0);\n}\n";
159 }
160 ' >>signal.c
161 $cat >signal.awk <<'EOP'
162 BEGIN { ndups = 0 }
163 $1 ~ /^NSIG$/ { nsig = $2 }
164 ($1 !~ /^NSIG$/) && (NF == 2) {
165     if ($2 > maxsig) { maxsig = $2 }
166     if (sig_name[$2]) {
167         dup_name[ndups] = $1
168         dup_num[ndups] = $2
169         ndups++ 
170     }
171     else {
172         sig_name[$2] = $1
173         sig_num[$2] = $2
174     }
175 }
176 END { 
177     if (nsig == 0) {
178         nsig = maxsig + 1
179     }
180     printf("NSIG %d\n", nsig);
181     for (n = 1; n < nsig; n++) {
182         if (sig_name[n]) {
183             printf("%s %d\n", sig_name[n], sig_num[n])
184         }
185         else {
186             printf("NUM%d %d\n", n, n) 
187         }
188     }
189     for (n = 0; n < ndups; n++) {
190         printf("%s %d\n", dup_name[n], dup_num[n])
191     }
192 }
193 EOP
194 $cat >signal_cmd <<EOS
195 $startsh
196 if $test -s signal.lst; then
197     echo "Using your existing signal.lst file"
198         exit 0
199 fi
200 xxx="$xxx"
201 EOS
202 ?X:     Avoid variable interpolation problems, especially with
203 ?X:     xxx, which contains newlines.
204 $cat >>signal_cmd <<'EOS'
205
206 set signal
207 if eval $compile_ok; then
208         $run ./signal$_exe | $sort -n -k 2 | $uniq | $awk -f signal.awk >signal.lst
209 else
210         echo "(I can't seem be able to compile the whole test program)" >&4
211         echo "(I'll try it in little pieces.)" >&4
212         set signal -DJUST_NSIG
213         if eval $compile_ok; then
214                 $run ./signal$_exe > signal.nsg
215                 $cat signal.nsg
216         else
217                 echo "I can't seem to figure out how many signals you have." >&4
218                 echo "Guessing 50." >&4
219                 echo 'NSIG 50' > signal.nsg
220         fi
221         : Now look at all the signal names, one at a time.
222         for xx in `echo $xxx | $tr ' ' $trnl | $sort | $uniq`; do
223                 $cat > signal.c <<EOCP
224 #include <sys/types.h>
225 #include <signal.h>
226 #include <stdio.h>
227 int main() {
228 printf("$xx %d\n", SIG${xx});
229 return 0;
230 }
231 EOCP
232                 set signal
233                 if eval $compile; then
234                         echo "SIG${xx} found."
235                         $run ./signal$_exe  >> signal.ls1
236                 else
237                         echo "SIG${xx} NOT found."
238                 fi
239         done
240         if $test -s signal.ls1; then
241                 $cat signal.nsg signal.ls1 |
242                         $sort -n | $uniq | $awk -f signal.awk >signal.lst
243         fi
244
245 fi
246 if $test -s signal.lst; then
247         :
248 else
249         echo "(AAK! I can't compile the test programs -- Guessing)" >&4
250         echo 'kill -l' >signal
251         set X `csh -f <signal`
252         $rm -f signal
253         shift
254         case $# in
255         0) set HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM;;
256         esac
257         echo $@ | $tr ' ' $trnl | \
258             $awk '{ printf "%s %d\n", $1, ++s; }
259                   END { printf "NSIG %d\n", ++s }' >signal.lst
260 fi
261 $rm -f signal.c signal$_exe signal$_o signal.nsg signal.ls1
262 EOS
263 chmod a+x signal_cmd
264 $eunicefix signal_cmd
265