Skip to content

Commit b35819a

Browse files
committed
Use a table to iterate through supported signals.
1 parent 9142f8a commit b35819a

File tree

1 file changed

+38
-138
lines changed

1 file changed

+38
-138
lines changed

src/njs_builtin.c

+38-138
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,36 @@ static const njs_object_type_init_t *const
100100
};
101101

102102

103+
typedef struct {
104+
njs_str_t name;
105+
int value;
106+
} njs_signal_entry_t;
107+
108+
// P1990 signals from `man 7 signal` are supported
109+
static njs_signal_entry_t njs_signals_table[] = {
110+
{ njs_str("SIGABRT"), SIGABRT },
111+
{ njs_str("SIGALRM"), SIGALRM },
112+
{ njs_str("SIGCHLD"), SIGCHLD },
113+
{ njs_str("SIGCONT"), SIGCONT },
114+
{ njs_str("SIGFPE"), SIGFPE },
115+
{ njs_str("SIGHUP"), SIGHUP },
116+
{ njs_str("SIGILL"), SIGILL },
117+
{ njs_str("SIGINT"), SIGINT },
118+
{ njs_str("SIGKILL"), SIGKILL },
119+
{ njs_str("SIGPIPE"), SIGPIPE },
120+
{ njs_str("SIGQUIT"), SIGQUIT },
121+
{ njs_str("SIGSEGV"), SIGSEGV },
122+
{ njs_str("SIGSTOP"), SIGSTOP },
123+
{ njs_str("SIGTSTP"), SIGTSTP },
124+
{ njs_str("SIGTERM"), SIGTERM },
125+
{ njs_str("SIGTTIN"), SIGTTIN },
126+
{ njs_str("SIGTTOU"), SIGTTOU },
127+
{ njs_str("SIGUSR1"), SIGUSR1 },
128+
{ njs_str("SIGUSR2"), SIGUSR2 },
129+
{ njs_null_str, 0 }
130+
};
131+
132+
103133
njs_inline njs_int_t
104134
njs_object_hash_init(njs_vm_t *vm, njs_lvlhsh_t *hash,
105135
const njs_object_init_t *init)
@@ -1399,148 +1429,18 @@ njs_ext_process_kill(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
13991429
if (njs_value_is_number(arg)) {
14001430
signal = njs_value_number(arg);
14011431
} else if (njs_value_is_string(arg)) {
1432+
njs_signal_entry_t *s;
14021433
njs_str_t str;
14031434
njs_value_string_get(arg, &str);
14041435

1405-
if (njs_strncmp(str.start, "SIGTERM", njs_length("SIGTERM")) == 0) {
1406-
signal = SIGTERM;
1407-
}
1408-
#ifdef SIGABRT
1409-
else if (njs_strncmp(str.start, "SIGABRT", njs_length("SIGABRT")) == 0) {
1410-
signal = SIGABRT;
1411-
}
1412-
#endif
1413-
#ifdef SIGALRM
1414-
else if (njs_strncmp(str.start, "SIGALRM", njs_length("SIGALRM")) == 0) {
1415-
signal = SIGALRM;
1416-
}
1417-
#endif
1418-
#ifdef SIGBUS
1419-
else if (njs_strncmp(str.start, "SIGBUS", njs_length("SIGBUS")) == 0) {
1420-
signal = SIGBUS;
1421-
}
1422-
#endif
1423-
#ifdef SIGCHLD
1424-
else if (njs_strncmp(str.start, "SIGCHLD", njs_length("SIGCHLD")) == 0) {
1425-
signal = SIGCHLD;
1426-
}
1427-
#endif
1428-
#ifdef SIGCONT
1429-
else if (njs_strncmp(str.start, "SIGCONT", njs_length("SIGCONT")) == 0) {
1430-
signal = SIGCONT;
1431-
}
1432-
#endif
1433-
#ifdef SIGFPE
1434-
else if (njs_strncmp(str.start, "SIGFPE", njs_length("SIGFPE")) == 0) {
1435-
signal = SIGFPE;
1436-
}
1437-
#endif
1438-
#ifdef SIGHUP
1439-
else if (njs_strncmp(str.start, "SIGHUP", njs_length("SIGHUP")) == 0) {
1440-
signal = SIGHUP;
1441-
}
1442-
#endif
1443-
#ifdef SIGILL
1444-
else if (njs_strncmp(str.start, "SIGILL", njs_length("SIGILL")) == 0) {
1445-
signal = SIGILL;
1446-
}
1447-
#endif
1448-
#ifdef SIGINT
1449-
else if (njs_strncmp(str.start, "SIGINT", njs_length("SIGINT")) == 0) {
1450-
signal = SIGINT;
1451-
}
1452-
#endif
1453-
#ifdef SIGKILL
1454-
else if (njs_strncmp(str.start, "SIGKILL", njs_length("SIGKILL")) == 0) {
1455-
signal = SIGKILL;
1456-
}
1457-
#endif
1458-
#ifdef SIGPIPE
1459-
else if (njs_strncmp(str.start, "SIGPIPE", njs_length("SIGPIPE")) == 0) {
1460-
signal = SIGPIPE;
1461-
}
1462-
#endif
1463-
#ifdef SIGQUIT
1464-
else if (njs_strncmp(str.start, "SIGQUIT", njs_length("SIGQUIT")) == 0) {
1465-
signal = SIGQUIT;
1466-
}
1467-
#endif
1468-
#ifdef SIGSEGV
1469-
else if (njs_strncmp(str.start, "SIGSEGV", njs_length("SIGSEGV")) == 0) {
1470-
signal = SIGSEGV;
1471-
}
1472-
#endif
1473-
#ifdef SIGSTOP
1474-
else if (njs_strncmp(str.start, "SIGSTOP", njs_length("SIGSTOP")) == 0) {
1475-
signal = SIGSTOP;
1476-
}
1477-
#endif
1478-
#ifdef SIGTSTP
1479-
else if (njs_strncmp(str.start, "SIGTSTP", njs_length("SIGTSTP")) == 0) {
1480-
signal = SIGTSTP;
1481-
}
1482-
#endif
1483-
#ifdef SIGTTIN
1484-
else if (njs_strncmp(str.start, "SIGTTIN", njs_length("SIGTTIN")) == 0) {
1485-
signal = SIGTTIN;
1486-
}
1487-
#endif
1488-
#ifdef SIGTTOU
1489-
else if (njs_strncmp(str.start, "SIGTTOU", njs_length("SIGTTOU")) == 0) {
1490-
signal = SIGTTOU;
1491-
}
1492-
#endif
1493-
#ifdef SIGUSR1
1494-
else if (njs_strncmp(str.start, "SIGUSR1", njs_length("SIGUSR1")) == 0) {
1495-
signal = SIGUSR1;
1496-
}
1497-
#endif
1498-
#ifdef SIGUSR2
1499-
else if (njs_strncmp(str.start, "SIGUSR2", njs_length("SIGUSR2")) == 0) {
1500-
signal = SIGUSR2;
1501-
}
1502-
#endif
1503-
#ifdef SIGPOLL
1504-
else if (njs_strncmp(str.start, "SIGPOLL", njs_length("SIGPOLL")) == 0) {
1505-
signal = SIGPOLL;
1506-
}
1507-
#endif
1508-
#ifdef SIGPROF
1509-
else if (njs_strncmp(str.start, "SIGPROF", njs_length("SIGPROF")) == 0) {
1510-
signal = SIGPROF;
1511-
}
1512-
#endif
1513-
#ifdef SIGSYS
1514-
else if (njs_strncmp(str.start, "SIGSYS", njs_length("SIGSYS")) == 0) {
1515-
signal = SIGSYS;
1516-
}
1517-
#endif
1518-
#ifdef SIGTRAP
1519-
else if (njs_strncmp(str.start, "SIGTRAP", njs_length("SIGTRAP")) == 0) {
1520-
signal = SIGTRAP;
1521-
}
1522-
#endif
1523-
#ifdef SIGURG
1524-
else if (njs_strncmp(str.start, "SIGURG", njs_length("SIGURG")) == 0) {
1525-
signal = SIGURG;
1526-
}
1527-
#endif
1528-
#ifdef SIGVTALRM
1529-
else if (njs_strncmp(str.start, "SIGVTALRM", njs_length("SIGVTALRM")) == 0) {
1530-
signal = SIGVTALRM;
1531-
}
1532-
#endif
1533-
#ifdef SIGXCPU
1534-
else if (njs_strncmp(str.start, "SIGXCPU", njs_length("SIGXCPU")) == 0) {
1535-
signal = SIGXCPU;
1536-
}
1537-
#endif
1538-
#ifdef SIGXFSZ
1539-
else if (njs_strncmp(str.start, "SIGXFSZ", njs_length("SIGXFSZ")) == 0) {
1540-
signal = SIGXFSZ;
1436+
for (s = &njs_signals_table[0]; s->name.length != 0; s++) {
1437+
if (njs_strstr_eq(&str, &s->name)) {
1438+
signal = s->value;
1439+
break;
1440+
}
15411441
}
1542-
#endif
1543-
else {
1442+
1443+
if (s->name.length == 0) {
15441444
njs_vm_type_error(vm, "\"signal\" unknown value");
15451445
return NJS_ERROR;
15461446
}

0 commit comments

Comments
 (0)