1 // %NO_EDIT_WARNING%
2 
3 ////////////////////////////////////////////////////////////////////////
4 //
5 // Copyright (C) 2005-2021 The Octave Project Developers
6 //
7 // See the file COPYRIGHT.md in the top-level directory of this
8 // distribution or <https://octave.org/copyright/>.
9 //
10 // This file is part of Octave.
11 //
12 // Octave is free software: you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by
14 // the Free Software Foundation, either version 3 of the License, or
15 // (at your option) any later version.
16 //
17 // Octave is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU General Public License
23 // along with Octave; see the file COPYING.  If not, see
24 // <https://www.gnu.org/licenses/>.
25 //
26 ////////////////////////////////////////////////////////////////////////
27 
28 #if defined (HAVE_CONFIG_H)
29 #  include "config.h"
30 #endif
31 
32 #include <cerrno>
33 
34 #include "singleton-cleanup.h"
35 
36 #include "oct-errno.h"
37 #include "oct-map.h"
38 #include "error.h"
39 
40 octave_errno *octave_errno::instance = nullptr;
41 
octave_errno(void)42 octave_errno::octave_errno (void)
43 {
44   struct errno_struct
45   {
46     const char *name;
47     int value;
48   };
49 
50   static errno_struct errno_codes[] =
51   {
52     // POSIX.
53 
54 #if defined (E2BIG)
55     { "E2BIG", E2BIG },
56 #endif
57 #if defined (EACCES)
58     { "EACCES", EACCES },
59 #endif
60 #if defined (EADDRINUSE)
61     { "EADDRINUSE", EADDRINUSE },
62 #endif
63 #if defined (EADDRNOTAVAIL)
64     { "EADDRNOTAVAIL", EADDRNOTAVAIL },
65 #endif
66 #if defined (EAFNOSUPPORT)
67     { "EAFNOSUPPORT", EAFNOSUPPORT },
68 #endif
69 #if defined (EAGAIN)
70     { "EAGAIN", EAGAIN },
71 #endif
72 #if defined (EALREADY)
73     { "EALREADY", EALREADY },
74 #endif
75 #if defined (EBADF)
76     { "EBADF", EBADF },
77 #endif
78 #if defined (EBUSY)
79     { "EBUSY", EBUSY },
80 #endif
81 #if defined (ECHILD)
82     { "ECHILD", ECHILD },
83 #endif
84 #if defined (ECONNABORTED)
85     { "ECONNABORTED", ECONNABORTED },
86 #endif
87 #if defined (ECONNREFUSED)
88     { "ECONNREFUSED", ECONNREFUSED },
89 #endif
90 #if defined (ECONNRESET)
91     { "ECONNRESET", ECONNRESET },
92 #endif
93 #if defined (EDEADLK)
94     { "EDEADLK", EDEADLK },
95 #endif
96 #if defined (EDESTADDRREQ)
97     { "EDESTADDRREQ", EDESTADDRREQ },
98 #endif
99 #if defined (EDOM)
100     { "EDOM", EDOM },
101 #endif
102 #if defined (EDQUOT)
103     { "EDQUOT", EDQUOT },
104 #endif
105 #if defined (EEXIST)
106     { "EEXIST", EEXIST },
107 #endif
108 #if defined (EFAULT)
109     { "EFAULT", EFAULT },
110 #endif
111 #if defined (EFBIG)
112     { "EFBIG", EFBIG },
113 #endif
114 #if defined (EHOSTDOWN)
115     { "EHOSTDOWN", EHOSTDOWN },
116 #endif
117 #if defined (EHOSTUNREACH)
118     { "EHOSTUNREACH", EHOSTUNREACH },
119 #endif
120 #if defined (EINPROGRESS)
121     { "EINPROGRESS", EINPROGRESS },
122 #endif
123 #if defined (EINTR)
124     { "EINTR", EINTR },
125 #endif
126 #if defined (EINVAL)
127     { "EINVAL", EINVAL },
128 #endif
129 #if defined (EIO)
130     { "EIO", EIO },
131 #endif
132 #if defined (EISCONN)
133     { "EISCONN", EISCONN },
134 #endif
135 #if defined (EISDIR)
136     { "EISDIR", EISDIR },
137 #endif
138 #if defined (ELOOP)
139     { "ELOOP", ELOOP },
140 #endif
141 #if defined (EMFILE)
142     { "EMFILE", EMFILE },
143 #endif
144 #if defined (EMLINK)
145     { "EMLINK", EMLINK },
146 #endif
147 #if defined (EMSGSIZE)
148     { "EMSGSIZE", EMSGSIZE },
149 #endif
150 #if defined (ENAMETOOLONG)
151     { "ENAMETOOLONG", ENAMETOOLONG },
152 #endif
153 #if defined (ENETDOWN)
154     { "ENETDOWN", ENETDOWN },
155 #endif
156 #if defined (ENETRESET)
157     { "ENETRESET", ENETRESET },
158 #endif
159 #if defined (ENETUNREACH)
160     { "ENETUNREACH", ENETUNREACH },
161 #endif
162 #if defined (ENFILE)
163     { "ENFILE", ENFILE },
164 #endif
165 #if defined (ENOBUFS)
166     { "ENOBUFS", ENOBUFS },
167 #endif
168 #if defined (ENODEV)
169     { "ENODEV", ENODEV },
170 #endif
171 #if defined (ENOENT)
172     { "ENOENT", ENOENT },
173 #endif
174 #if defined (ENOEXEC)
175     { "ENOEXEC", ENOEXEC },
176 #endif
177 #if defined (ENOLCK)
178     { "ENOLCK", ENOLCK },
179 #endif
180 #if defined (ENOMEM)
181     { "ENOMEM", ENOMEM },
182 #endif
183 #if defined (ENOPROTOOPT)
184     { "ENOPROTOOPT", ENOPROTOOPT },
185 #endif
186 #if defined (ENOSPC)
187     { "ENOSPC", ENOSPC },
188 #endif
189 #if defined (ENOSYS)
190     { "ENOSYS", ENOSYS },
191 #endif
192 #if defined (ENOTBLK)
193     { "ENOTBLK", ENOTBLK },
194 #endif
195 #if defined (ENOTCONN)
196     { "ENOTCONN", ENOTCONN },
197 #endif
198 #if defined (ENOTDIR)
199     { "ENOTDIR", ENOTDIR },
200 #endif
201 #if defined (ENOTEMPTY)
202     { "ENOTEMPTY", ENOTEMPTY },
203 #endif
204 #if defined (ENOTSOCK)
205     { "ENOTSOCK", ENOTSOCK },
206 #endif
207 #if defined (ENOTTY)
208     { "ENOTTY", ENOTTY },
209 #endif
210 #if defined (ENXIO)
211     { "ENXIO", ENXIO },
212 #endif
213 #if defined (EOPNOTSUPP)
214     { "EOPNOTSUPP", EOPNOTSUPP },
215 #endif
216 #if defined (EPERM)
217     { "EPERM", EPERM },
218 #endif
219 #if defined (EPFNOSUPPORT)
220     { "EPFNOSUPPORT", EPFNOSUPPORT },
221 #endif
222 #if defined (EPIPE)
223     { "EPIPE", EPIPE },
224 #endif
225 #if defined (EPROTONOSUPPORT)
226     { "EPROTONOSUPPORT", EPROTONOSUPPORT },
227 #endif
228 #if defined (EPROTOTYPE)
229     { "EPROTOTYPE", EPROTOTYPE },
230 #endif
231 #if defined (ERANGE)
232     { "ERANGE", ERANGE },
233 #endif
234 #if defined (EREMOTE)
235     { "EREMOTE", EREMOTE },
236 #endif
237 #if defined (ERESTART)
238     { "ERESTART", ERESTART },
239 #endif
240 #if defined (EROFS)
241     { "EROFS", EROFS },
242 #endif
243 #if defined (ESHUTDOWN)
244     { "ESHUTDOWN", ESHUTDOWN },
245 #endif
246 #if defined (ESOCKTNOSUPPORT)
247     { "ESOCKTNOSUPPORT", ESOCKTNOSUPPORT },
248 #endif
249 #if defined (ESPIPE)
250     { "ESPIPE", ESPIPE },
251 #endif
252 #if defined (ESRCH)
253     { "ESRCH", ESRCH },
254 #endif
255 #if defined (ESTALE)
256     { "ESTALE", ESTALE },
257 #endif
258 #if defined (ETIMEDOUT)
259     { "ETIMEDOUT", ETIMEDOUT },
260 #endif
261 #if defined (ETOOMANYREFS)
262     { "ETOOMANYREFS", ETOOMANYREFS },
263 #endif
264 #if defined (ETXTBSY)
265     { "ETXTBSY", ETXTBSY },
266 #endif
267 #if defined (EUSERS)
268     { "EUSERS", EUSERS },
269 #endif
270 #if defined (EWOULDBLOCK)
271     { "EWOULDBLOCK", EWOULDBLOCK },
272 #endif
273 #if defined (EXDEV)
274     { "EXDEV", EXDEV },
275 #endif
276 
277     // Others (duplicates are OK).
278 
279 @SYSDEP_ERRNO_LIST@
280 
281     { nullptr, 0 },
282   };
283 
284   // Stuff them all in a map for fast access.
285 
286   errno_struct *ptr = errno_codes;
287 
288   while (ptr->name)
289     {
290       errno_tbl[ptr->name] = ptr->value;
291       ptr++;
292     }
293 }
294 
295 bool
instance_ok(void)296 octave_errno::instance_ok (void)
297 {
298   bool retval = true;
299 
300   if (! instance)
301     {
302       instance = new octave_errno ();
303       singleton_cleanup_list::add (cleanup_instance);
304     }
305 
306   return retval;
307 }
308 
309 int
lookup(const std::string & name)310 octave_errno::lookup (const std::string& name)
311 {
312   return (instance_ok ()) ? instance->do_lookup (name) : -1;
313 }
314 
315 octave_scalar_map
list(void)316 octave_errno::list (void)
317 {
318   return (instance_ok ()) ? instance->do_list () : octave_scalar_map ();
319 }
320 
321 int
do_lookup(const std::string & name)322 octave_errno::do_lookup (const std::string& name)
323 {
324   return (errno_tbl.find (name) != errno_tbl.end ()) ? errno_tbl[name] : -1;
325 }
326 
327 octave_scalar_map
do_list(void)328 octave_errno::do_list (void)
329 {
330   octave_scalar_map retval;
331 
332   for (const auto& p : errno_tbl)
333     {
334       retval.assign (p.first, p.second);
335     }
336 
337   return retval;
338 }
339