xref: /freebsd/stand/liblua/lerrno.c (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
1 /*-
2  * Copyright (c) 2018 Conrad Meyer <cem@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #define _WANT_KERNEL_ERRNO	1
31 #include <errno.h>
32 
33 #include <lua.h>
34 #include "lauxlib.h"
35 #include "lerrno.h"
36 
37 #ifndef nitems
38 #define	nitems(x)	(sizeof((x)) / sizeof((x)[0]))
39 #endif
40 
41 /*
42  * Populated with:
43  * $ egrep "^#define.E" sys/sys/errno.h | \
44  *   awk '{ print "\tENTRY(" $2 ")," }' >> lerrno.c
45  */
46 #define ENTRY(name)	{ #name, name }
47 static const struct err_name_number {
48 	const char *err_name;
49 	int err_num;
50 } errnoconstants[] = {
51 	ENTRY(EPERM),
52 	ENTRY(ENOENT),
53 	ENTRY(ESRCH),
54 	ENTRY(EINTR),
55 	ENTRY(EIO),
56 	ENTRY(ENXIO),
57 	ENTRY(E2BIG),
58 	ENTRY(ENOEXEC),
59 	ENTRY(EBADF),
60 	ENTRY(ECHILD),
61 	ENTRY(EDEADLK),
62 	ENTRY(ENOMEM),
63 	ENTRY(EACCES),
64 	ENTRY(EFAULT),
65 	ENTRY(ENOTBLK),
66 	ENTRY(EBUSY),
67 	ENTRY(EEXIST),
68 	ENTRY(EXDEV),
69 	ENTRY(ENODEV),
70 	ENTRY(ENOTDIR),
71 	ENTRY(EISDIR),
72 	ENTRY(EINVAL),
73 	ENTRY(ENFILE),
74 	ENTRY(EMFILE),
75 	ENTRY(ENOTTY),
76 	ENTRY(ETXTBSY),
77 	ENTRY(EFBIG),
78 	ENTRY(ENOSPC),
79 	ENTRY(ESPIPE),
80 	ENTRY(EROFS),
81 	ENTRY(EMLINK),
82 	ENTRY(EPIPE),
83 	ENTRY(EDOM),
84 	ENTRY(ERANGE),
85 	ENTRY(EAGAIN),
86 	ENTRY(EWOULDBLOCK),
87 	ENTRY(EINPROGRESS),
88 	ENTRY(EALREADY),
89 	ENTRY(ENOTSOCK),
90 	ENTRY(EDESTADDRREQ),
91 	ENTRY(EMSGSIZE),
92 	ENTRY(EPROTOTYPE),
93 	ENTRY(ENOPROTOOPT),
94 	ENTRY(EPROTONOSUPPORT),
95 	ENTRY(ESOCKTNOSUPPORT),
96 	ENTRY(EOPNOTSUPP),
97 	ENTRY(ENOTSUP),
98 	ENTRY(EPFNOSUPPORT),
99 	ENTRY(EAFNOSUPPORT),
100 	ENTRY(EADDRINUSE),
101 	ENTRY(EADDRNOTAVAIL),
102 	ENTRY(ENETDOWN),
103 	ENTRY(ENETUNREACH),
104 	ENTRY(ENETRESET),
105 	ENTRY(ECONNABORTED),
106 	ENTRY(ECONNRESET),
107 	ENTRY(ENOBUFS),
108 	ENTRY(EISCONN),
109 	ENTRY(ENOTCONN),
110 	ENTRY(ESHUTDOWN),
111 	ENTRY(ETOOMANYREFS),
112 	ENTRY(ETIMEDOUT),
113 	ENTRY(ECONNREFUSED),
114 	ENTRY(ELOOP),
115 	ENTRY(ENAMETOOLONG),
116 	ENTRY(EHOSTDOWN),
117 	ENTRY(EHOSTUNREACH),
118 	ENTRY(ENOTEMPTY),
119 	ENTRY(EPROCLIM),
120 	ENTRY(EUSERS),
121 	ENTRY(EDQUOT),
122 	ENTRY(ESTALE),
123 	ENTRY(EREMOTE),
124 	ENTRY(EBADRPC),
125 	ENTRY(ERPCMISMATCH),
126 	ENTRY(EPROGUNAVAIL),
127 	ENTRY(EPROGMISMATCH),
128 	ENTRY(EPROCUNAVAIL),
129 	ENTRY(ENOLCK),
130 	ENTRY(ENOSYS),
131 	ENTRY(EFTYPE),
132 	ENTRY(EAUTH),
133 	ENTRY(ENEEDAUTH),
134 	ENTRY(EIDRM),
135 	ENTRY(ENOMSG),
136 	ENTRY(EOVERFLOW),
137 	ENTRY(ECANCELED),
138 	ENTRY(EILSEQ),
139 	ENTRY(ENOATTR),
140 	ENTRY(EDOOFUS),
141 	ENTRY(EBADMSG),
142 	ENTRY(EMULTIHOP),
143 	ENTRY(ENOLINK),
144 	ENTRY(EPROTO),
145 	ENTRY(ENOTCAPABLE),
146 	ENTRY(ECAPMODE),
147 	ENTRY(ENOTRECOVERABLE),
148 	ENTRY(EOWNERDEAD),
149 	ENTRY(EINTEGRITY),
150 	ENTRY(ELAST),
151 	ENTRY(ERESTART),
152 	ENTRY(EJUSTRETURN),
153 	ENTRY(ENOIOCTL),
154 	ENTRY(EDIRIOCTL),
155 	ENTRY(ERELOOKUP),
156 };
157 #undef ENTRY
158 
159 static void
160 lerrno_register(lua_State *L)
161 {
162 	size_t i;
163 
164 	for (i = 0; i < nitems(errnoconstants); i++) {
165 		lua_pushinteger(L, errnoconstants[i].err_num);
166 		lua_setfield(L, -2, errnoconstants[i].err_name);
167 	}
168 }
169 
170 static const struct luaL_Reg errnolib[] = {
171 	/* Extra bogus entry required by luaL_newlib API */
172 	{ NULL, NULL },
173 };
174 
175 int
176 luaopen_errno(lua_State *L)
177 {
178 	luaL_newlib(L, errnolib);
179 	lerrno_register(L);
180 	return 1;
181 }
182