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