| 1 |
/* |
| 2 |
* Ikarus Scheme -- A compiler for R6RS Scheme. |
| 3 |
* Copyright (C) 2006,2007,2008 Abdulaziz Ghuloum |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License version 3 as |
| 7 |
* published by the Free Software Foundation. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, but |
| 10 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License |
| 15 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
|
| 19 |
#include <errno.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
#include "ikarus-data.h" |
| 23 |
|
| 24 |
typedef struct errno_info { |
| 25 |
int n; |
| 26 |
char* s; |
| 27 |
ikptr c; |
| 28 |
} errno_info; |
| 29 |
|
| 30 |
#ifndef ECANCELED |
| 31 |
#define ECANCELED 140 |
| 32 |
#endif |
| 33 |
|
| 34 |
#ifndef ENODATA |
| 35 |
#define ENODATA 96 |
| 36 |
#endif |
| 37 |
|
| 38 |
#ifndef ENOSR |
| 39 |
#define ENOSR 98 |
| 40 |
#endif |
| 41 |
|
| 42 |
#ifndef ENOSTR |
| 43 |
#define ENOSTR 99 |
| 44 |
#endif |
| 45 |
|
| 46 |
#ifndef ETIME |
| 47 |
#define ETIME 101 |
| 48 |
#endif |
| 49 |
|
| 50 |
static errno_info errno_table[] = { |
| 51 |
/* errnos from POSIX IEEE Std 1003.1 2004 Edition */ |
| 52 |
{E2BIG, "E2BIG", fix(-1)}, |
| 53 |
{EACCES, "EACCES", fix(-2)}, |
| 54 |
{EADDRINUSE, "EADDRINUSE", fix(-3)}, |
| 55 |
{EADDRNOTAVAIL, "EADDRNOTAVAIL", fix(-4)}, |
| 56 |
{EAFNOSUPPORT, "EAFNOSUPPORT", fix(-5)}, |
| 57 |
{EAGAIN, "EAGAIN", fix(-6)}, |
| 58 |
{EALREADY, "EALREADY", fix(-7)}, |
| 59 |
{EBADF, "EBADF", fix(-8)}, |
| 60 |
{EBADMSG, "EBADMSG", fix(-9)}, |
| 61 |
{EBUSY, "EBUSY", fix(-10)}, |
| 62 |
{ECANCELED, "ECANCELED", fix(-11)}, |
| 63 |
{ECHILD, "ECHILD", fix(-12)}, |
| 64 |
{ECONNABORTED, "ECONNABORTED", fix(-13)}, |
| 65 |
{ECONNREFUSED, "ECONNREFUSED", fix(-14)}, |
| 66 |
{ECONNRESET, "ECONNRESET", fix(-15)}, |
| 67 |
{EDEADLK, "EDEADLK", fix(-16)}, |
| 68 |
{EDESTADDRREQ, "EDESTADDRREQ", fix(-17)}, |
| 69 |
{EDOM, "EDOM", fix(-18)}, |
| 70 |
{EDQUOT, "EDQUOT", fix(-19)}, |
| 71 |
{EEXIST, "EEXIST", fix(-20)}, |
| 72 |
{EFAULT, "EFAULT", fix(-21)}, |
| 73 |
{EFBIG, "EFBIG", fix(-22)}, |
| 74 |
{EHOSTUNREACH, "EHOSTUNREACH", fix(-23)}, |
| 75 |
{EIDRM, "EIDRM", fix(-24)}, |
| 76 |
{EILSEQ, "EILSEQ", fix(-25)}, |
| 77 |
{EINPROGRESS, "EINPROGRESS", fix(-26)}, |
| 78 |
{EINTR, "EINTR", fix(-27)}, |
| 79 |
{EINVAL, "EINVAL", fix(-28)}, |
| 80 |
{EIO, "EIO", fix(-29)}, |
| 81 |
{EISCONN, "EISCONN", fix(-30)}, |
| 82 |
{EISDIR, "EISDIR", fix(-31)}, |
| 83 |
{ELOOP, "ELOOP", fix(-32)}, |
| 84 |
{EMFILE, "EMFILE", fix(-33)}, |
| 85 |
{EMLINK, "EMLINK", fix(-34)}, |
| 86 |
{EMSGSIZE, "EMSGSIZE", fix(-35)}, |
| 87 |
{EMULTIHOP, "EMULTIHOP", fix(-36)}, |
| 88 |
{ENAMETOOLONG, "ENAMETOOLONG", fix(-37)}, |
| 89 |
{ENETDOWN, "ENETDOWN", fix(-38)}, |
| 90 |
{ENETRESET, "ENETRESET", fix(-39)}, |
| 91 |
{ENETUNREACH, "ENETUNREACH", fix(-40)}, |
| 92 |
{ENFILE, "ENFILE", fix(-41)}, |
| 93 |
{ENOBUFS, "ENOBUFS", fix(-42)}, |
| 94 |
{ENODATA, "ENODATA", fix(-43)}, |
| 95 |
{ENODEV, "ENODEV", fix(-44)}, |
| 96 |
{ENOENT, "ENOENT", fix(-45)}, |
| 97 |
{ENOEXEC, "ENOEXEC", fix(-46)}, |
| 98 |
{ENOLCK, "ENOLCK", fix(-47)}, |
| 99 |
{ENOLINK, "ENOLINK", fix(-48)}, |
| 100 |
{ENOMEM, "ENOMEM", fix(-49)}, |
| 101 |
{ENOMSG, "ENOMSG", fix(-50)}, |
| 102 |
{ENOPROTOOPT, "ENOPROTOOPT", fix(-51)}, |
| 103 |
{ENOSPC, "ENOSPC", fix(-52)}, |
| 104 |
{ENOSR, "ENOSR", fix(-53)}, |
| 105 |
{ENOSTR, "ENOSTR", fix(-54)}, |
| 106 |
{ENOSYS, "ENOSYS", fix(-55)}, |
| 107 |
{ENOTCONN, "ENOTCONN", fix(-56)}, |
| 108 |
{ENOTDIR, "ENOTDIR", fix(-57)}, |
| 109 |
{ENOTEMPTY, "ENOTEMPTY", fix(-58)}, |
| 110 |
{ENOTSOCK, "ENOTSOCK", fix(-59)}, |
| 111 |
{ENOTSUP, "ENOTSUP", fix(-60)}, |
| 112 |
{ENOTTY, "ENOTTY", fix(-61)}, |
| 113 |
{ENXIO, "ENXIO", fix(-62)}, |
| 114 |
{EOPNOTSUPP, "EOPNOTSUPP", fix(-63)}, |
| 115 |
{EOVERFLOW, "EOVERFLOW", fix(-64)}, |
| 116 |
{EPERM, "EPERM", fix(-65)}, |
| 117 |
{EPIPE, "EPIPE", fix(-66)}, |
| 118 |
{EPROTO, "EPROTO", fix(-67)}, |
| 119 |
{EPROTONOSUPPORT, "EPROTONOSUPPORT", fix(-68)}, |
| 120 |
{EPROTOTYPE, "EPROTOTYPE", fix(-69)}, |
| 121 |
{ERANGE, "ERANGE", fix(-70)}, |
| 122 |
{EROFS, "EROFS", fix(-71)}, |
| 123 |
{ESPIPE, "ESPIPE", fix(-72)}, |
| 124 |
{ESRCH, "ESRCH", fix(-73)}, |
| 125 |
{ESTALE, "ESTALE", fix(-74)}, |
| 126 |
{ETIME, "ETIME", fix(-75)}, |
| 127 |
{ETIMEDOUT, "ETIMEDOUT", fix(-76)}, |
| 128 |
{ETXTBSY, "ETXTBSY", fix(-77)}, |
| 129 |
{EWOULDBLOCK, "EWOULDBLOCK", fix(-78)}, |
| 130 |
{EXDEV, "EXDEV", fix(-79)} |
| 131 |
}; |
| 132 |
|
| 133 |
#define errno_table_length 79 |
| 134 |
|
| 135 |
ikptr |
| 136 |
ik_errno_num_to_code(int en){ |
| 137 |
/* Used by C to return Ikarus errno code to Scheme. */ |
| 138 |
errno_info* ei; |
| 139 |
int i; |
| 140 |
for(i=0; i < errno_table_length; i++){ |
| 141 |
ei = &errno_table[i]; |
| 142 |
if(ei->n == en){ |
| 143 |
return ei->c; |
| 144 |
} |
| 145 |
} |
| 146 |
fprintf(stderr, "\n*** ikarus-errno.c: Don't know errno %d ***\n\n", en); |
| 147 |
return fix(-99999); /* should always be less than last errno code */ |
| 148 |
} |
| 149 |
|
| 150 |
ikptr |
| 151 |
ik_errno_to_code(){ |
| 152 |
int en = errno; |
| 153 |
return ik_errno_num_to_code(en); |
| 154 |
} |
| 155 |
|
| 156 |
ikptr |
| 157 |
ikrt_errno_code_to_name(ikptr ec, ikpcb* pcb){ |
| 158 |
/* Used by Scheme to get the name of the errno corresponding |
| 159 |
to the Ikarus errno code. */ |
| 160 |
errno_info* ei; |
| 161 |
int i; |
| 162 |
for(i=0; i < errno_table_length; i++){ |
| 163 |
ei = &errno_table[i]; |
| 164 |
if(ei->c == ec){ |
| 165 |
int len = strlen(ei->s); |
| 166 |
ikptr bv = ik_safe_alloc(pcb, align(disp_bytevector_data+len+1)) |
| 167 |
+ bytevector_tag; |
| 168 |
ref(bv, off_bytevector_length) = fix(len); |
| 169 |
memcpy((char*)(bv+off_bytevector_data), ei->s, len+1); |
| 170 |
return bv; |
| 171 |
} |
| 172 |
} |
| 173 |
return false_object; |
| 174 |
} |
| 175 |
|
| 176 |
ikptr |
| 177 |
ikrt_strerror(ikptr ec, ikpcb* pcb){ |
| 178 |
/* Used by Scheme to get the error message of the errno corresponding |
| 179 |
to the Ikarus errno code. */ |
| 180 |
errno_info* ei; |
| 181 |
int i; |
| 182 |
for(i=0; i < errno_table_length; i++){ |
| 183 |
ei = &errno_table[i]; |
| 184 |
if(ei->c == ec){ |
| 185 |
errno = 0; |
| 186 |
char* es = strerror(ei->n); |
| 187 |
if(errno){ |
| 188 |
perror("ikrt_strerror: strerror failed"); |
| 189 |
exit(EXIT_FAILURE); |
| 190 |
} |
| 191 |
int len = strlen(es); |
| 192 |
ikptr bv = ik_safe_alloc(pcb, align(disp_bytevector_data+len+1)) |
| 193 |
+ bytevector_tag; |
| 194 |
ref(bv, off_bytevector_length) = fix(len); |
| 195 |
memcpy((char*)(bv+off_bytevector_data), es, len+1); |
| 196 |
return bv; |
| 197 |
} |
| 198 |
} |
| 199 |
return false_object; |
| 200 |
} |
| 201 |
|