OILS / frontend / signal_def.py View on Github | oilshell.org

94 lines, 56 significant
1#!/usr/bin/env python2
2"""Signal_def.py."""
3from __future__ import print_function
4
5import signal
6
7from typing import Dict
8
9
10def _MakeSignalsOld():
11 # type: () -> Dict[str, int]
12 """Piggy-back on CPython signal module.
13
14 This causes portability problems
15 """
16 names = {} # type: Dict[str, int]
17 for name in dir(signal):
18 # don't want SIG_DFL or SIG_IGN
19 if name.startswith('SIG') and not name.startswith('SIG_'):
20 int_val = getattr(signal, name)
21 abbrev = name[3:]
22 names[abbrev] = int_val
23 return names
24
25
26# POSIX 2018
27# https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/signal.h.html
28
29_PORTABLE_SIGNALS = [
30 'SIGABRT',
31 'SIGALRM',
32 'SIGBUS',
33 'SIGCHLD',
34 'SIGCONT',
35 'SIGFPE',
36 'SIGHUP',
37 'SIGILL',
38 'SIGINT',
39 #SIGKILL
40 'SIGPIPE',
41 'SIGQUIT',
42 'SIGSEGV',
43 'SIGSTOP',
44 'SIGTERM',
45 'SIGTSTP',
46 'SIGTTIN',
47 'SIGTTOU',
48 'SIGUSR1',
49 'SIGUSR2',
50 'SIGSYS',
51 'SIGTRAP',
52 'SIGURG',
53 'SIGVTALRM',
54 'SIGXCPU',
55 'SIGXFSZ',
56
57 # Not part of POSIX, but essential for Oils to work
58 'SIGWINCH',
59]
60
61
62def _MakeSignals():
63 # type: () -> Dict[str, int]
64 """Piggy-back on CPython signal module.
65
66 This causes portability problems
67 """
68 names = {} # type: Dict[str, int]
69 for name in _PORTABLE_SIGNALS:
70 int_val = getattr(signal, name)
71 assert name.startswith('SIG'), name
72 abbrev = name[3:]
73 names[abbrev] = int_val
74 return names
75
76
77NO_SIGNAL = -1
78
79
80def GetNumber(sig_spec):
81 # type: (str) -> int
82 return _SIGNAL_NAMES.get(sig_spec, NO_SIGNAL)
83
84
85_SIGNAL_NAMES = _MakeSignals()
86
87_BY_NUMBER = _SIGNAL_NAMES.items()
88_BY_NUMBER.sort(key=lambda x: x[1])
89
90
91def PrintSignals():
92 # type: () -> None
93 for name, int_val in _BY_NUMBER:
94 print('%2d SIG%s' % (int_val, name))