raz / commit f408ba19

commit
f408ba192c3444060f6a4a86c56358cc3989af87
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
bc0ed42c
Sys: errorText, what an errno means

On Linux a table of glibc's texts, carried only by a program that calls
it; over C, strerror. Library growth that breaks no program, met in
rwalk's Rwfiles, which named four errnos by hand.

patch · log

5 files changed, +169 -18

PROPOSALS.md modified +0 -17

@@ -583,23 +583,6 @@
583583
584584 Met in rrem's parser, documenting `(?>x*)`.
585585
586-## The text of an errno
587-
588-`Sys` returns minus an errno, and a tool that tells the user what
589-failed must turn it into words: `Rwfiles` maps four by hand, and says
590-"error 13" for the rest.
591-
592-The proposal, in `Sys`:
593-
594-```
595-proc errorText*(err: INT): []U8; (* what errno err (or minus it) means, as strerror says; "unknown error" otherwise *)
596-```
597-
598-a fixed table of Linux's names, so it needs no C. It breaks no
599-program.
600-
601-Met in rwalk's `Rwfiles`.
602-
603586 ## Setting an environment variable
604587
605588 `Sys.env` reads a variable; nothing sets one. A test can't give the

doc/raz-lib.md (history) modified +3 -1

@@ -11,7 +11,7 @@
1111 | Module | What |
1212 |---|---|
1313 | `Rt` | ending the program, `exit` and `atExit`; `stop`, which ends it with a message; `trace`, the calls under way; what the compiler's checks call when they fail |
14-| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `close`, `dup`, `dupTo`, `dupAbove`, `keepOnExec`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `envAt`, `workDir`, `setWorkDir`; programs, `spawn` and `exec`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others |
14+| `Sys` | system calls: `open`, `openMode`, `openRead`, `create`, `read`, `write`, `seek`, `position`, `truncate`, `sync`, `syncAll`, `lock`, `close`, `dup`, `dupTo`, `dupAbove`, `keepOnExec`, `mmap`, `munmap`, `isTerminal`, `yield`, `exit`; files and directories, `rename`, `remove`, `removeDir`, `makeDir`, `chmod`, `access`, `umask`, `symlink`, `readLink`, `info`, `linkInfo`, `infoOf`, `openDir`, `nextEntry`, `entryKind`, `closeDir`; the terminal, `rawMode`, `restoreMode`, `getMode`, `setMode`, `termSize`, `ready`; the environment, `env`, `envAt`, `workDir`, `setWorkDir`; programs, `spawn` and `exec`, pipes and signals; the user, the machine, time used and limits; time, `clock`, `now`, `sleep`; `random`; what an errno means, `errorText`; sockets, among them datagrams served and files passed; and memory shared through a file. One file per target, and `Sys.raz` over the C library for the others |
1515 | `Out` | writing text and numbers to standard output, buffered; safe to call from several threads |
1616 | `C` | the parts of the C library the others use, and `printf`, `fflush`, `stdout`, `pthread_create`; a program that imports it uses C |
1717 | `Lock` | a lock for short sections, for C's threads and `Thread`'s alike |
@@ -249,6 +249,8 @@
249249 proc readyOf*(fds: []INT; ms: INT): INT; (* the first of fds with something to read within ms, or -1 *)
250250 proc readyFor*(reads, writes: []INT; ms: INT): INT; (* readyOf, and the first of writes with room to write *)
251251 proc setBlocking*(fd: INT; yes: BOOL): INT; (* if not yes, a read or write that would wait is -11 at once *)
252+
253+proc errorText*(err: INT): []U8; (* what errno err, or minus it, means, as C's strerror says *)
252254
253255 proc env*(name: []U8): []U8; (* the environment variable's value; "" if unset *)
254256 proc envAt*(i: INT): ([]U8, BOOL); (* the environment's ith NAME=value, from 0; false past the last *)

lib/C.raz (history) modified +1 -0

@@ -55,6 +55,7 @@
5555 extern proc clock_nanosleep*(clock, flags: I32; t, left: ^U8): I32;
5656 extern proc getrandom*(buf: ^U8; n: UINT; flags: U32): INT;
5757 extern proc getenv*(name: ^U8): ^U8;
58+extern proc strerror*(err: I32): ^U8;
5859 extern proc getcwd*(buf: ^U8; n: UINT): ^U8;
5960 extern proc chdir*(path: ^U8): I32;
6061 extern proc stat*(path, st: ^U8): I32;

lib/Sys.linux-x86_64.raz (history) modified +150 -0

@@ -1003,6 +1003,154 @@
10031003 return n, k;
10041004 end;
10051005
1006+const
1007+ (* what each errno means, as glibc's strerror says it; 41 and 58
1008+ are none *)
1009+ errorTexts: [134][]U8 = [
1010+ "Success", (* 0 *)
1011+ "Operation not permitted", (* 1, EPERM *)
1012+ "No such file or directory", (* 2, ENOENT *)
1013+ "No such process", (* 3, ESRCH *)
1014+ "Interrupted system call", (* 4, EINTR *)
1015+ "Input/output error", (* 5, EIO *)
1016+ "No such device or address", (* 6, ENXIO *)
1017+ "Argument list too long", (* 7, E2BIG *)
1018+ "Exec format error", (* 8, ENOEXEC *)
1019+ "Bad file descriptor", (* 9, EBADF *)
1020+ "No child processes", (* 10, ECHILD *)
1021+ "Resource temporarily unavailable", (* 11, EAGAIN *)
1022+ "Cannot allocate memory", (* 12, ENOMEM *)
1023+ "Permission denied", (* 13, EACCES *)
1024+ "Bad address", (* 14, EFAULT *)
1025+ "Block device required", (* 15, ENOTBLK *)
1026+ "Device or resource busy", (* 16, EBUSY *)
1027+ "File exists", (* 17, EEXIST *)
1028+ "Invalid cross-device link", (* 18, EXDEV *)
1029+ "No such device", (* 19, ENODEV *)
1030+ "Not a directory", (* 20, ENOTDIR *)
1031+ "Is a directory", (* 21, EISDIR *)
1032+ "Invalid argument", (* 22, EINVAL *)
1033+ "Too many open files in system", (* 23, ENFILE *)
1034+ "Too many open files", (* 24, EMFILE *)
1035+ "Inappropriate ioctl for device", (* 25, ENOTTY *)
1036+ "Text file busy", (* 26, ETXTBSY *)
1037+ "File too large", (* 27, EFBIG *)
1038+ "No space left on device", (* 28, ENOSPC *)
1039+ "Illegal seek", (* 29, ESPIPE *)
1040+ "Read-only file system", (* 30, EROFS *)
1041+ "Too many links", (* 31, EMLINK *)
1042+ "Broken pipe", (* 32, EPIPE *)
1043+ "Numerical argument out of domain", (* 33, EDOM *)
1044+ "Numerical result out of range", (* 34, ERANGE *)
1045+ "Resource deadlock avoided", (* 35, EDEADLOCK *)
1046+ "File name too long", (* 36, ENAMETOOLONG *)
1047+ "No locks available", (* 37, ENOLCK *)
1048+ "Function not implemented", (* 38, ENOSYS *)
1049+ "Directory not empty", (* 39, ENOTEMPTY *)
1050+ "Too many levels of symbolic links", (* 40, ELOOP *)
1051+ "unknown error", (* 41 *)
1052+ "No message of desired type", (* 42, ENOMSG *)
1053+ "Identifier removed", (* 43, EIDRM *)
1054+ "Channel number out of range", (* 44, ECHRNG *)
1055+ "Level 2 not synchronized", (* 45, EL2NSYNC *)
1056+ "Level 3 halted", (* 46, EL3HLT *)
1057+ "Level 3 reset", (* 47, EL3RST *)
1058+ "Link number out of range", (* 48, ELNRNG *)
1059+ "Protocol driver not attached", (* 49, EUNATCH *)
1060+ "No CSI structure available", (* 50, ENOCSI *)
1061+ "Level 2 halted", (* 51, EL2HLT *)
1062+ "Invalid exchange", (* 52, EBADE *)
1063+ "Invalid request descriptor", (* 53, EBADR *)
1064+ "Exchange full", (* 54, EXFULL *)
1065+ "No anode", (* 55, ENOANO *)
1066+ "Invalid request code", (* 56, EBADRQC *)
1067+ "Invalid slot", (* 57, EBADSLT *)
1068+ "unknown error", (* 58 *)
1069+ "Bad font file format", (* 59, EBFONT *)
1070+ "Device not a stream", (* 60, ENOSTR *)
1071+ "No data available", (* 61, ENODATA *)
1072+ "Timer expired", (* 62, ETIME *)
1073+ "Out of streams resources", (* 63, ENOSR *)
1074+ "Machine is not on the network", (* 64, ENONET *)
1075+ "Package not installed", (* 65, ENOPKG *)
1076+ "Object is remote", (* 66, EREMOTE *)
1077+ "Link has been severed", (* 67, ENOLINK *)
1078+ "Advertise error", (* 68, EADV *)
1079+ "Srmount error", (* 69, ESRMNT *)
1080+ "Communication error on send", (* 70, ECOMM *)
1081+ "Protocol error", (* 71, EPROTO *)
1082+ "Multihop attempted", (* 72, EMULTIHOP *)
1083+ "RFS specific error", (* 73, EDOTDOT *)
1084+ "Bad message", (* 74, EBADMSG *)
1085+ "Value too large for defined data type", (* 75, EOVERFLOW *)
1086+ "Name not unique on network", (* 76, ENOTUNIQ *)
1087+ "File descriptor in bad state", (* 77, EBADFD *)
1088+ "Remote address changed", (* 78, EREMCHG *)
1089+ "Can not access a needed shared library", (* 79, ELIBACC *)
1090+ "Accessing a corrupted shared library", (* 80, ELIBBAD *)
1091+ ".lib section in a.out corrupted", (* 81, ELIBSCN *)
1092+ "Attempting to link in too many shared libraries", (* 82, ELIBMAX *)
1093+ "Cannot exec a shared library directly", (* 83, ELIBEXEC *)
1094+ "Invalid or incomplete multibyte or wide character", (* 84, EILSEQ *)
1095+ "Interrupted system call should be restarted", (* 85, ERESTART *)
1096+ "Streams pipe error", (* 86, ESTRPIPE *)
1097+ "Too many users", (* 87, EUSERS *)
1098+ "Socket operation on non-socket", (* 88, ENOTSOCK *)
1099+ "Destination address required", (* 89, EDESTADDRREQ *)
1100+ "Message too long", (* 90, EMSGSIZE *)
1101+ "Protocol wrong type for socket", (* 91, EPROTOTYPE *)
1102+ "Protocol not available", (* 92, ENOPROTOOPT *)
1103+ "Protocol not supported", (* 93, EPROTONOSUPPORT *)
1104+ "Socket type not supported", (* 94, ESOCKTNOSUPPORT *)
1105+ "Operation not supported", (* 95, ENOTSUP *)
1106+ "Protocol family not supported", (* 96, EPFNOSUPPORT *)
1107+ "Address family not supported by protocol", (* 97, EAFNOSUPPORT *)
1108+ "Address already in use", (* 98, EADDRINUSE *)
1109+ "Cannot assign requested address", (* 99, EADDRNOTAVAIL *)
1110+ "Network is down", (* 100, ENETDOWN *)
1111+ "Network is unreachable", (* 101, ENETUNREACH *)
1112+ "Network dropped connection on reset", (* 102, ENETRESET *)
1113+ "Software caused connection abort", (* 103, ECONNABORTED *)
1114+ "Connection reset by peer", (* 104, ECONNRESET *)
1115+ "No buffer space available", (* 105, ENOBUFS *)
1116+ "Transport endpoint is already connected", (* 106, EISCONN *)
1117+ "Transport endpoint is not connected", (* 107, ENOTCONN *)
1118+ "Cannot send after transport endpoint shutdown", (* 108, ESHUTDOWN *)
1119+ "Too many references: cannot splice", (* 109, ETOOMANYREFS *)
1120+ "Connection timed out", (* 110, ETIMEDOUT *)
1121+ "Connection refused", (* 111, ECONNREFUSED *)
1122+ "Host is down", (* 112, EHOSTDOWN *)
1123+ "No route to host", (* 113, EHOSTUNREACH *)
1124+ "Operation already in progress", (* 114, EALREADY *)
1125+ "Operation now in progress", (* 115, EINPROGRESS *)
1126+ "Stale file handle", (* 116, ESTALE *)
1127+ "Structure needs cleaning", (* 117, EUCLEAN *)
1128+ "Not a XENIX named type file", (* 118, ENOTNAM *)
1129+ "No XENIX semaphores available", (* 119, ENAVAIL *)
1130+ "Is a named type file", (* 120, EISNAM *)
1131+ "Remote I/O error", (* 121, EREMOTEIO *)
1132+ "Disk quota exceeded", (* 122, EDQUOT *)
1133+ "No medium found", (* 123, ENOMEDIUM *)
1134+ "Wrong medium type", (* 124, EMEDIUMTYPE *)
1135+ "Operation canceled", (* 125, ECANCELED *)
1136+ "Required key not available", (* 126, ENOKEY *)
1137+ "Key has expired", (* 127, EKEYEXPIRED *)
1138+ "Key has been revoked", (* 128, EKEYREVOKED *)
1139+ "Key was rejected by service", (* 129, EKEYREJECTED *)
1140+ "Owner died", (* 130, EOWNERDEAD *)
1141+ "State not recoverable", (* 131, ENOTRECOVERABLE *)
1142+ "Operation not possible due to RF-kill", (* 132, ERFKILL *)
1143+ "Memory page has hardware error" (* 133, EHWPOISON *)];
1144+
1145+(* What errno err, or minus it, means, as C's strerror says it;
1146+ "unknown error" for a number that is no errno. *)
1147+proc errorText*(err: INT): []U8;
1148+begin
1149+ if (err < 0) and (err > -LEN(errorTexts)) then err := -err; end;
1150+ if (err < 0) or (err >= LEN(errorTexts)) then return "unknown error"; end;
1151+ return errorTexts[err];
1152+end;
1153+
10061154 (* The value of the environment variable name; "" if it isn't set. The
10071155 environment follows the arguments and their nil. *)
10081156 proc env*(name: []U8): []U8;
@@ -1894,6 +2042,8 @@
18942042 if (n # 5) or (buf[0..5] # "hello") or (read(fd, buf) # 0) then exit(2); end;
18952043 close(fd);
18962044 if openRead("/no/such/file") >= 0 then exit(2); end;
2045+ if (errorText(-2) # "No such file or directory") or (errorText(13) # "Permission denied")
2046+ or (errorText(41) # "unknown error") or (errorText(134) # "unknown error") then exit(2); end;
18972047 if isTerminal(-1) then exit(2); end;
18982048 p := mmap(100);
18992049 if (p = nil) or (p[99] # 0) then exit(2); end;

lib/Sys.raz (history) modified +15 -0

@@ -1005,6 +1005,20 @@
10051005 return n, k;
10061006 end;
10071007
1008+(* What errno err, or minus it, means, as the C library's strerror
1009+ says it. *)
1010+proc errorText*(err: INT): []U8;
1011+var s: ^U8; n: INT;
1012+begin
1013+ if err < 0 then err := -err; end;
1014+ if (err < 0) or (err > 7FFFFFFFH) then return "unknown error"; end; (* the smallest INT, or past an int *)
1015+ s := C.strerror(I32(err));
1016+ if s = nil then return "unknown error"; end;
1017+ n := 0;
1018+ while s[n] # 0 do INC(n); end;
1019+ return s[0..n];
1020+end;
1021+
10081022 (* The value of the environment variable name; "" if it isn't set. *)
10091023 proc env*(name: []U8): []U8;
10101024 var a: [4096]U8; s: ^U8; n: INT;
@@ -1914,6 +1928,7 @@
19141928 if (n # 5) or (buf[0..5] # "hello") or (read(fd, buf) # 0) then exit(2); end;
19151929 close(fd);
19161930 if openRead("/no/such/file") >= 0 then exit(2); end;
1931+ if (errorText(-2) # "No such file or directory") or (errorText(13) # "Permission denied") then exit(2); end;
19171932 if isTerminal(-1) then exit(2); end;
19181933 p := mmap(100);
19191934 if (p = nil) or (p[99] # 0) then exit(2); end;