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