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

: generate a few handy files for later
$cat > signal.c <<'EOCP'
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
int main() {

/* Strange style to avoid deeply-nested #if/#else/#endif */
#ifndef NSIG
#  ifdef _NSIG
#    define NSIG (_NSIG)
#  endif
#endif

#ifndef NSIG
#  ifdef SIGMAX
#    define NSIG (SIGMAX+1)
#  endif
#endif

#ifndef NSIG
#  ifdef SIG_MAX
#    define NSIG (SIG_MAX+1)
#  endif
#endif

#ifndef NSIG
#  ifdef MAXSIG
#    define NSIG (MAXSIG+1)
#  endif
#endif

#ifndef NSIG
#  ifdef MAX_SIG
#    define NSIG (MAX_SIG+1)
#  endif
#endif

#ifndef NSIG
#  ifdef SIGARRAYSIZE
#    define NSIG (SIGARRAYSIZE+1) /* Not sure of the +1 */
#  endif
#endif

#ifndef NSIG
#  ifdef _sys_nsig
#    define NSIG (_sys_nsig) /* Solaris 2.5 */
#  endif
#endif

/* Default to some arbitrary number that's big enough to get most
   of the common signals.
*/
#ifndef NSIG
#    define NSIG 50
#endif

printf("NSIG %d\n", NSIG);

#ifndef JUST_NSIG

EOCP

echo $xxx | $tr ' ' $trnl | $sort | $uniq | $awk '
{
	printf "#ifdef SIG"; printf $1; printf "\n"
	printf "printf(\""; printf $1; printf " %%d\\n\",SIG";
	printf $1; printf ");\n"
	printf "#endif\n"
}
END {
	printf "#endif /* JUST_NSIG */\n";
	printf "exit(0);\n}\n";
}
' >>signal.c
$cat >signal.awk <<'EOP'
BEGIN { ndups = 0 }
$1 ~ /^NSIG$/ { nsig = $2 }
($1 !~ /^NSIG$/) && (NF == 2) {
    if ($2 > maxsig) { maxsig = $2 }
    if (sig_name[$2]) {
	dup_name[ndups] = $1
	dup_num[ndups] = $2
	ndups++ 
    }
    else {
	sig_name[$2] = $1
	sig_num[$2] = $2
    }
}
END { 
    if (nsig == 0) {
    	nsig = maxsig + 1
    }
    printf("NSIG %d\n", nsig);
    for (n = 1; n < nsig; n++) {
 	if (sig_name[n]) {
 	    printf("%s %d\n", sig_name[n], sig_num[n])
	}
 	else {
 	    printf("NUM%d %d\n", n, n) 
 	}
    }
    for (n = 0; n < ndups; n++) {
 	printf("%s %d\n", dup_name[n], dup_num[n])
    }
}
EOP
$cat >signal_cmd <<EOS
$startsh
if $test -s signal.lst; then
    echo "Using your existing signal.lst file"
	exit 0
fi
xxx="$xxx"
EOS
?X: Avoid variable interpolation problems, especially with
?X: xxx, which contains newlines.
$cat >>signal_cmd <<'EOS'

set signal
if eval $compile_ok; then
	$run ./signal$_exe | ($sort -n -k 2 2>/dev/null || $sort -n +1) | \
		$uniq | $awk -f signal.awk >signal.lst
else
	echo "(I can't seem be able to compile the whole test program)" >&4
	echo "(I'll try it in little pieces.)" >&4
	set signal -DJUST_NSIG
	if eval $compile_ok; then
		$run ./signal$_exe > signal.nsg
		$cat signal.nsg
	else
		echo "I can't seem to figure out how many signals you have." >&4
		echo "Guessing 50." >&4
		echo 'NSIG 50' > signal.nsg
	fi
	: Now look at all the signal names, one at a time.
	for xx in `echo $xxx | $tr ' ' $trnl | $sort | $uniq`; do
		$cat > signal.c <<EOCP
#include <sys/types.h>
#include <signal.h>
#include <stdio.h>
int main() {
printf("$xx %d\n", SIG${xx});
return 0;
}
EOCP
		set signal
		if eval $compile; then
			echo "SIG${xx} found."
			$run ./signal$_exe  >> signal.ls1
		else
			echo "SIG${xx} NOT found."
		fi
	done
	if $test -s signal.ls1; then
		$cat signal.nsg signal.ls1 |
			$sort -n | $uniq | $awk -f signal.awk >signal.lst
	fi

fi
if $test -s signal.lst; then
	:
else
	echo "(AAK! I can't compile the test programs -- Guessing)" >&4
	echo 'kill -l' >signal
	set X `csh -f <signal`
	$rm -f signal
	shift
	case $# in
	0) set HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS PIPE ALRM TERM;;
	esac
	echo $@ | $tr ' ' $trnl | \
 	    $awk '{ printf "%s %d\n", $1, ++s; }
 		  END { printf "NSIG %d\n", ++s }' >signal.lst
fi
$rm -f signal.c signal$_exe signal$_o signal.nsg signal.ls1
EOS
chmod a+x signal_cmd
$eunicefix signal_cmd