xref: /freebsd/stand/liblua/lerrno.c (revision 61e21613)
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 #define _WANT_KERNEL_ERRNO	1
29 #include <errno.h>
30 
31 #include <lua.h>
32 #include "lauxlib.h"
33 #include "lerrno.h"
34 
35 #ifndef nitems
36 #define	nitems(x)	(sizeof((x)) / sizeof((x)[0]))
37 #endif
38 
39 /*
40  * Populated with:
41  * $ egrep "^#define.E" sys/sys/errno.h | \
42  *   awk '{ print "\tENTRY(" $2 ")," }' >> lerrno.c
43  */
44 #define ENTRY(name)	{ #name, name }
45 static const struct err_name_number {
46 	const char *err_name;
47 	int err_num;
48 } errnoconstants[] = {
49 	ENTRY(EPERM),
50 	ENTRY(ENOENT),
51 	ENTRY(ESRCH),
52 	ENTRY(EINTR),
53 	ENTRY(EIO),
54 	ENTRY(ENXIO),
55 	ENTRY(E2BIG),
56 	ENTRY(ENOEXEC),
57 	ENTRY(EBADF),
58 	ENTRY(ECHILD),
59 	ENTRY(EDEADLK),
60 	ENTRY(ENOMEM),
61 	ENTRY(EACCES),
62 	ENTRY(EFAULT),
63 	ENTRY(ENOTBLK),
64 	ENTRY(EBUSY),
65 	ENTRY(EEXIST),
66 	ENTRY(EXDEV),
67 	ENTRY(ENODEV),
68 	ENTRY(ENOTDIR),
69 	ENTRY(EISDIR),
70 	ENTRY(EINVAL),
71 	ENTRY(ENFILE),
72 	ENTRY(EMFILE),
73 	ENTRY(ENOTTY),
74 	ENTRY(ETXTBSY),
75 	ENTRY(EFBIG),
76 	ENTRY(ENOSPC),
77 	ENTRY(ESPIPE),
78 	ENTRY(EROFS),
79 	ENTRY(EMLINK),
80 	ENTRY(EPIPE),
81 	ENTRY(EDOM),
82 	ENTRY(ERANGE),
83 	ENTRY(EAGAIN),
84 	ENTRY(EWOULDBLOCK),
85 	ENTRY(EINPROGRESS),
86 	ENTRY(EALREADY),
87 	ENTRY(ENOTSOCK),
88 	ENTRY(EDESTADDRREQ),
89 	ENTRY(EMSGSIZE),
90 	ENTRY(EPROTOTYPE),
91 	ENTRY(ENOPROTOOPT),
92 	ENTRY(EPROTONOSUPPORT),
93 	ENTRY(ESOCKTNOSUPPORT),
94 	ENTRY(EOPNOTSUPP),
95 	ENTRY(ENOTSUP),
96 	ENTRY(EPFNOSUPPORT),
97 	ENTRY(EAFNOSUPPORT),
98 	ENTRY(EADDRINUSE),
99 	ENTRY(EADDRNOTAVAIL),
100 	ENTRY(ENETDOWN),
101 	ENTRY(ENETUNREACH),
102 	ENTRY(ENETRESET),
103 	ENTRY(ECONNABORTED),
104 	ENTRY(ECONNRESET),
105 	ENTRY(ENOBUFS),
106 	ENTRY(EISCONN),
107 	ENTRY(ENOTCONN),
108 	ENTRY(ESHUTDOWN),
109 	ENTRY(ETOOMANYREFS),
110 	ENTRY(ETIMEDOUT),
111 	ENTRY(ECONNREFUSED),
112 	ENTRY(ELOOP),
113 	ENTRY(ENAMETOOLONG),
114 	ENTRY(EHOSTDOWN),
115 	ENTRY(EHOSTUNREACH),
116 	ENTRY(ENOTEMPTY),
117 	ENTRY(EPROCLIM),
118 	ENTRY(EUSERS),
119 	ENTRY(EDQUOT),
120 	ENTRY(ESTALE),
121 	ENTRY(EREMOTE),
122 	ENTRY(EBADRPC),
123 	ENTRY(ERPCMISMATCH),
124 	ENTRY(EPROGUNAVAIL),
125 	ENTRY(EPROGMISMATCH),
126 	ENTRY(EPROCUNAVAIL),
127 	ENTRY(ENOLCK),
128 	ENTRY(ENOSYS),
129 	ENTRY(EFTYPE),
130 	ENTRY(EAUTH),
131 	ENTRY(ENEEDAUTH),
132 	ENTRY(EIDRM),
133 	ENTRY(ENOMSG),
134 	ENTRY(EOVERFLOW),
135 	ENTRY(ECANCELED),
136 	ENTRY(EILSEQ),
137 	ENTRY(ENOATTR),
138 	ENTRY(EDOOFUS),
139 	ENTRY(EBADMSG),
140 	ENTRY(EMULTIHOP),
141 	ENTRY(ENOLINK),
142 	ENTRY(EPROTO),
143 	ENTRY(ENOTCAPABLE),
144 	ENTRY(ECAPMODE),
145 	ENTRY(ENOTRECOVERABLE),
146 	ENTRY(EOWNERDEAD),
147 	ENTRY(EINTEGRITY),
148 	ENTRY(ELAST),
149 	ENTRY(ERESTART),
150 	ENTRY(EJUSTRETURN),
151 	ENTRY(ENOIOCTL),
152 	ENTRY(EDIRIOCTL),
153 	ENTRY(ERELOOKUP),
154 };
155 #undef ENTRY
156 
157 static void
158 lerrno_register(lua_State *L)
159 {
160 	size_t i;
161 
162 	for (i = 0; i < nitems(errnoconstants); i++) {
163 		lua_pushinteger(L, errnoconstants[i].err_num);
164 		lua_setfield(L, -2, errnoconstants[i].err_name);
165 	}
166 }
167 
168 static const struct luaL_Reg errnolib[] = {
169 	/* Extra bogus entry required by luaL_newlib API */
170 	{ NULL, NULL },
171 };
172 
173 int
174 luaopen_errno(lua_State *L)
175 {
176 	luaL_newlib(L, errnolib);
177 	lerrno_register(L);
178 	return 1;
179 }
180