Adds perror()

This commit is contained in:
2025-07-22 15:54:25 +02:00
parent 8f4f0a83f5
commit bc5e6e2342
3 changed files with 353 additions and 1 deletions

View File

@@ -7,7 +7,8 @@ OBJS := $(DIR_BLD)/tests.o \
$(DIR_BLD)/core.o \
$(DIR_BLD)/console.o \
$(DIR_BLD)/string.o \
$(DIR_BLD)/convert.o
$(DIR_BLD)/convert.o \
$(DIR_BLD)/perror.o \
OBJS_LST := false
SRCS := $(patsubst $(DIR_BLD)/%.o,$(SRC_DIR)/%.asm,$(OBJS))

View File

@@ -41,3 +41,139 @@ section .rodata
MAX_INT32 equ 0x7FFFFFFF
MAX_INT16 equ 0x7FFF
MAX_INT8 equ 0x7F
; System error codes
ENONE equ 0
EPERM equ 1
ENOENT equ 2
ESRCH equ 3
EINTR equ 4
EIO equ 5
ENXIO equ 6
E2BIG equ 7
ENOEXEC equ 8
EBADF equ 9
ECHILD equ 10
EAGAIN equ 11
ENOMEM equ 12
EACCES equ 13
EFAULT equ 14
ENOTBLK equ 15
EBUSY equ 16
EEXIST equ 17
EXDEV equ 18
ENODEV equ 19
ENOTDIR equ 20
EISDIR equ 21
EINVAL equ 22
ENFILE equ 23
EMFILE equ 24
ENOTTY equ 25
ETXTBSY equ 26
EFBIG equ 27
ENOSPC equ 28
ESPIPE equ 29
EROFS equ 30
EMLINK equ 31
EPIPE equ 32
EDOM equ 33
ERANGE equ 34
EDEADLK equ 35
ENAMETOOLONG equ 36
ENOLCK equ 37
ENOSYS equ 38
ENOTEMPTY equ 39
ELOOP equ 40
EWOULDBLOCK equ EAGAIN
ENOMSG equ 42
EIDRM equ 43
ECHRNG equ 44
EL2NSYNC equ 45
EL3HLT equ 46
EL3RST equ 47
ELNRNG equ 48
EUNATCH equ 49
ENOCSI equ 50
EL2HLT equ 51
EBADE equ 52
EBADR equ 53
EXFULL equ 54
ENOANO equ 55
EBADRQC equ 56
EBADSLT equ 57
EDEADLOCK equ EDEADLK
EBFONT equ 59
ENOSTR equ 60
ENODATA equ 61
ETIME equ 62
ENOSR equ 63
ENONET equ 64
ENOPKG equ 65
EREMOTE equ 66
ENOLINK equ 67
EADV equ 68
ESRMNT equ 69
ECOMM equ 70
EPROTO equ 71
EMULTIHOP equ 72
EDOTDOT equ 73
EBADMSG equ 74
EOVERFLOW equ 75
ENOTUNIQ equ 76
EBADFD equ 77
EREMCHG equ 78
ELIBACC equ 79
ELIBBAD equ 80
ELIBSCN equ 81
ELIBMAX equ 82
ELIBEXEC equ 83
EILSEQ equ 84
ERESTART equ 85
ESTRPIPE equ 86
EUSERS equ 87
ENOTSOCK equ 88
EDESTADDRREQ equ 89
EMSGSIZE equ 90
EPROTOTYPE equ 91
ENOPROTOOPT equ 92
EPROTONOSUPPORT equ 93
ESOCKTNOSUPPORT equ 94
EOPNOTSUPP equ 95
EPFNOSUPPORT equ 96
EAFNOSUPPORT equ 97
EADDRINUSE equ 98
EADDRNOTAVAIL equ 99
ENETDOWN equ 100
ENETUNREACH equ 101
ENETRESET equ 102
ECONNABORTED equ 103
ECONNRESET equ 104
ENOBUFS equ 105
EISCONN equ 106
ENOTCONN equ 107
ESHUTDOWN equ 108
ETOOMANYREFS equ 109
ETIMEDOUT equ 110
ECONNREFUSED equ 111
EHOSTDOWN equ 112
EHOSTUNREACH equ 113
EALREADY equ 114
EINPROGRESS equ 115
ESTALE equ 116
EUCLEAN equ 117
ENOTNAM equ 118
ENAVAIL equ 119
EISNAM equ 120
EREMOTEIO equ 121
EDQUOT equ 122
ENOMEDIUM equ 123
EMEDIUMTYPE equ 124
ECANCELED equ 125
ENOKEY equ 126
EKEYEXPIRED equ 127
EKEYREVOKED equ 128
EKEYREJECTED equ 129
EOWNERDEAD equ 130
ENOTRECOVERABLE equ 131
ERFKILL equ 132
EHWPOISON equ 133

215
src/perror.asm Normal file
View File

@@ -0,0 +1,215 @@
%include "src/constants.asm"
extern strlen
section .rodata
errMsgs dq e0,e1,e2,e3,e4,e5,e6,e7,e8,e9,\
e10,e11,e12,e13,e14,e15,e16,e17,e18,e19,\
e20,e21,e22,e23,e24,e25,e26,e27,e28,e29,\
e30,e31,e32,e33,e34,e35,e36,e37,e38,e39,\
e40,e41,e42,e43,e44,e45,e46,e47,e48,e49,\
e50,e51,e52,e53,e54,e55,e56,e57,e58,e59,\
e60,e61,e62,e63,e64,e65,e66,e67,e68,e69,\
e70,e71,e72,e73,e74,e75,e76,e77,e78,e79,\
e80,e81,e82,e83,e84,e85,e86,e87,e88,e89,\
e90,e91,e92,e93,e94,e95,e96,e97,e98,e99,\
e100,e101,e102,e103,e104,e105,e106,e107,e108,e109,\
e110,e111,e112,e113,e114,e115,e116,e117,e118,e119,\
e120,e121,e122,e123,e124,e125,e126,e127,e128,e129,\
e130,e131,e132,e133
e0 db "No error",NL,EOS
e1 db "Operation not permitted",NL,EOS
e2 db "No such file or directory",NL,EOS
e3 db "No such process",NL,EOS
e4 db "Interrupted system call",NL,EOS
e5 db "I/O error",NL,EOS
e6 db "No such device or address",NL,EOS
e7 db "Argument list too long",NL,EOS
e8 db "Exec format error",NL,EOS
e9 db "Bad file number",NL,EOS
e10 db "No child processes",NL,EOS
e11 db "Try again",NL,EOS
e12 db "Out of memory",NL,EOS
e13 db "Permission denied",NL,EOS
e14 db "Bad address",NL,EOS
e15 db "Block device required",NL,EOS
e16 db "Device or resource busy",NL,EOS
e17 db "File exists",NL,EOS
e18 db "Cross-device link",NL,EOS
e19 db "No such device",NL,EOS
e20 db "Not a directory",NL,EOS
e21 db "Is a directory",NL,EOS
e22 db "Invalid argument",NL,EOS
e23 db "File table overflow",NL,EOS
e24 db "Too many open files",NL,EOS
e25 db "Not a typewriter",NL,EOS
e26 db "Text file busy",NL,EOS
e27 db "File too large",NL,EOS
e28 db "No space left on device",NL,EOS
e29 db "Illegal seek",NL,EOS
e30 db "Read-only file system",NL,EOS
e31 db "Too many links",NL,EOS
e32 db "Broken pipe",NL,EOS
e33 db "Math argument out of domain func",NL,EOS
e34 db "Math result not representable",NL,EOS
e35 db "Resource deadlock would occur",NL,EOS
e36 db "File name too long",NL,EOS
e37 db "No record locks available",NL,EOS
e38 db "Invalid system call number",NL,EOS
e39 db "Directory not empty",NL,EOS
e40 db "Too many symbolic links encountered",NL,EOS
e41 equ e11
e42 db "No message of desired type",NL,EOS
e43 db "Identifier removed",NL,EOS
e44 db "Channel number out of range",NL,EOS
e45 db "Level 2 not synchronized",NL,EOS
e46 db "Level 3 halted",NL,EOS
e47 db "Level 3 reset",NL,EOS
e48 db "Link number out of range",NL,EOS
e49 db "Protocol driver not attached",NL,EOS
e50 db "No CSI structure available",NL,EOS
e51 db "Level 2 halted",NL,EOS
e52 db "Invalid exchange",NL,EOS
e53 db "Invalid request descriptor",NL,EOS
e54 db "Exchange full",NL,EOS
e55 db "No anode",NL,EOS
e56 db "Invalid request code",NL,EOS
e57 db "Invalid slot",NL,EOS
e58 equ e35
e59 db "Bad font file format",NL,EOS
e60 db "Device not a stream",NL,EOS
e61 db "No data available",NL,EOS
e62 db "Timer expired",NL,EOS
e63 db "Out of streams resources",NL,EOS
e64 db "Machine is not on the network",NL,EOS
e65 db "Package not installed",NL,EOS
e66 db "Object is remote",NL,EOS
e67 db "Link has been severed",NL,EOS
e68 db "Advertise error",NL,EOS
e69 db "Srmount error",NL,EOS
e70 db "Communication error on send",NL,EOS
e71 db "Protocol error",NL,EOS
e72 db "Multihop attempted",NL,EOS
e73 db "RFS specific error",NL,EOS
e74 db "Not a data message",NL,EOS
e75 db "Value too large for defined data type",NL,EOS
e76 db "Name not unique on network",NL,EOS
e77 db "File descriptor in bad state",NL,EOS
e78 db "Remote address changed",NL,EOS
e79 db "Can not acces a needed shared library",NL,EOS
e80 db "Accessing a corrupted shared library",NL,EOS
e81 db ".lib section in a.out corrupted",NL,EOS
e82 db "Attempting to link in too many shared libraries",NL,EOS
e83 db "Cannot exec a shared library directly",NL,EOS
e84 db "Illegal byte sequence",NL,EOS
e85 db "Interrupted system call should be restarted",NL,EOS
e86 db "Streams pip error",NL,EOS
e87 db "Too many users",NL,EOS
e88 db "Socket operation on non-socket",NL,EOS
e89 db "Destination address required",NL,EOS
e90 db "Message too long",NL,EOS
e91 db "Protocol wrong type for socket",NL,EOS
e92 db "Protocol not available",NL,EOS
e93 db "Protocol not supported",NL,EOS
e94 db "Socket type not supported",NL,EOS
e95 db "Operation not supported on transport endpoint",NL,EOS
e96 db "Protocol family not supported",NL,EOS
e97 db "Address family not supported by protocol",NL,EOS
e98 db "Address already in use",NL,EOS
e99 db "Cannot assign requested address",NL,EOS
e100 db "Network is down",NL,EOS
e101 db "Network is unreachable",NL,EOS
e102 db "Network dropped connection because of reset",NL,EOS
e103 db "Software caused connection abort",NL,EOS
e104 db "Connection reset by peer",NL,EOS
e105 db "No buffer space available",NL,EOS
e106 db "Transport endpoint is already connected",NL,EOS
e107 db "Transport endpoint is not connected",NL,EOS
e108 db "Cannot send after transport endpoint shutdown",NL,EOS
e109 db "Too many references: cannot splice",NL,EOS
e110 db "Connection timed out",NL,EOS
e111 db "Connection refused",NL,EOS
e112 db "Host is down",NL,EOS
e113 db "No route to host",NL,EOS
e114 db "Operation already in progress",NL,EOS
e115 db "Operation now in progress",NL,EOS
e116 db "Stale file handle",NL,EOS
e117 db "Structure needs cleaning",NL,EOS
e118 db "Not a XENIX named type file",NL,EOS
e119 db "No XENIX semaphores available",NL,EOS
e120 db "Is a named type file",NL,EOS
e121 db "Remote I/O error",NL,EOS
e122 db "Quota exceeded",NL,EOS
e123 db "No medium found",NL,EOS
e124 db "Wrong medium type",NL,EOS
e125 db "Operation Canceled",NL,EOS
e126 db "Required key not available",NL,EOS
e127 db "Key has expired",NL,EOS
e128 db "Key has been revoked",NL,EOS
e129 db "Key was rejected by service",NL,EOS
e130 db "Owner died",NL,EOS
e131 db "State not recoverable",NL,EOS
e132 db "Operation not possible due to RF-kill",NL,EOS
e133 db "Memory page has hardware error",NL,EOS
unknownError db "Unknown error",NL,EOS
errMsgUser db ": ",EOS
section .text
global perror
;----- perror(*str[]) -----;
; Prints a descriptive error to stderr.
; Return value: N/A (current error code untouched)
; Used registers:
; rax Current error code (may be both positive or negative)
; rdi* (arg) Pointer to str[] or 0 (no custom message)
; rdx* Used for syscall NR_write
; r8* (Re)stores pointer to str[]// Stores address of errMsgUser for cmov
; r9* (Re)stores error code
; r10* (Re)stores pointer to error msg
perror:
test rax, rax
jz .quit
js .negErr
jmp .cnt
.negErr:
neg rax
.cnt:
mov r9, rax
test rdi, rdi
jz .noUserMsg
mov r8, rdi
call strlen
mov rdx, rax
mov rax, NR_write
mov rdi, FD_stderr
mov rsi, r8
syscall
mov rax, NR_write
mov rdi, FD_stderr
lea rsi, [rel errMsgUser]
mov rdx, 2
syscall
mov rax, r8
.noUserMsg:
cmp r9, 133
ja .invalidErr
lea r10, [errMsgs + r9 * SIZE_QWORD]
mov r10, [r10]
jmp .printErrMsg
.invalidErr:
lea r10, [rel unknownError]
.printErrMsg:
mov rdi, r10
call strlen
mov rdx, rax
mov rax, NR_write
mov rdi, FD_stderr
mov rsi, r10
syscall
.quit:
mov rax, r9
ret