aboutsummaryrefslogtreecommitdiff
blob: 8c8e8e6e35b7c87db493493294d9d6ea8de8e170 (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
#include "headers.h"

#define _msg(std, fmt, args...) fprintf(std, "%s:%s():%i: " fmt "\n", __FILE__, __func__, __LINE__, ##args)
#define _stderr_msg(fmt, args...) _msg(stderr, fmt, ##args)
#define _stderr_pmsg(fmt, args...) _msg(stderr, fmt ": %s", ##args, strerror(errno))
#define err(fmt, args...) ({ _stderr_msg(fmt, ##args); exit(1); })
#define errp(fmt, args...) ({ _stderr_pmsg(fmt, ##args); exit(1); })

typedef struct {
	const char *name;
	int val;
} value_pair;
#define PAIR(x) { #x, x },

int lookup_val(const value_pair *tbl, const char *name)
{
	size_t i;
	for (i = 0; tbl[i].name; ++i)
		if (!strcmp(name, tbl[i].name))
			return tbl[i].val;
	err("unable to locate '%s'", name);
}

int lookup_errno(const char *str_errno)
{
	const value_pair tbl[] = {
		PAIR(EACCES)
		PAIR(EBADF)
		PAIR(EEXIST)
		PAIR(EFAULT)
		PAIR(EINVAL)
		PAIR(EISDIR)
		PAIR(ELOOP)
		PAIR(ENAMETOOLONG)
		PAIR(ENODEV)
		PAIR(ENOENT)
		PAIR(ENOTDIR)
		PAIR(EPERM)
		PAIR(ETXTBSY)
		{ }
	};
	return lookup_val(tbl, str_errno);
}

int lookup_signal(const char *str_signal)
{
	const value_pair tbl[] = {
		{ "SIGEXIT", 0 },
		PAIR(SIGABRT)
		PAIR(SIGALRM)
		PAIR(SIGCHLD)
		PAIR(SIGCONT)
		PAIR(SIGHUP)
		PAIR(SIGILL)
		PAIR(SIGINT)
		PAIR(SIGKILL)
		PAIR(SIGPIPE)
		PAIR(SIGQUIT)
		PAIR(SIGSEGV)
		PAIR(SIGSTOP)
		PAIR(SIGTRAP)
		PAIR(SIGTERM)
		PAIR(SIGUSR1)
		PAIR(SIGUSR2)
		{ }
	};
	return lookup_val(tbl, str_signal);
}