1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00

errno & rc's & shell errors

This commit is contained in:
Julian Barathieu 2021-03-04 20:05:53 +01:00
parent 8296688060
commit 8d61c9b5e0
No known key found for this signature in database
GPG Key ID: 381DDCD8FF88E34F
15 changed files with 136 additions and 130 deletions

View File

@ -27,36 +27,37 @@
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#ifndef _eentry #ifndef _EENTRY
#define _estart enum { #define _ESTART enum {
#define _eentry(code, desc) code, #define _EENTRY(code, num, desc) code = num,
#define _eentry(code, desc) code, #define _EEND };
#define _eend _EMAX };
#endif #endif
_estart _ESTART
_eentry(EOK, "No error") _EENTRY(EOK, -0, "No error")
_eentry(EPERM, "Operation not permitted") _EENTRY(EPERM, -1, "Operation not permitted")
_eentry(ENOENT, "No such entry, file, or directory") _EENTRY(ENOENT, -2, "No such entry, file, or directory")
_eentry(ESRCH, "No such process or thread") _EENTRY(ESRCH, -3, "No such process or thread")
_eentry(EINTR, "Operation interrupted") _EENTRY(EINTR, -4, "Operation interrupted")
_eentry(EIO, "I/O error") _EENTRY(EIO, -5, "I/O error")
_eentry(ENXIO, "No such device or address") _EENTRY(ENXIO, -6, "No such device or address")
_eentry(E2BIG, "List too long") _EENTRY(E2BIG, -7, "List too long")
_eentry(ENOEXEC, "Not an executable format") _EENTRY(ENOEXEC, -8, "Not an executable format")
_eentry(EBADF, "Bad file, fd, or stream") _EENTRY(EBADF, -9, "Bad file, fd, or stream")
_eentry(EAGAIN, "Try again") _EENTRY(EAGAIN, -10, "Try again")
_eentry(ENOMEM, "Not enough memory") _EENTRY(ENOMEM, -11, "Not enough memory")
_eentry(EINVAL, "Invalid argument value") _EENTRY(EINVAL, -12, "Invalid argument value")
_eentry(ENOSYS, "Functionality not implemented") _EENTRY(ENOSYS, -13, "Functionality not implemented")
_eentry(EADDRINUSE, "Address in use") _EENTRY(EADDRINUSE, -14, "Address in use")
_eentry(EFAILED, "Failure (unspecified reason)") _EENTRY(EFAILED, -15, "Failure (unspecified reason)")
_eentry(EALIGN, "Alignment error or fault") _EENTRY(EALIGN, -16, "Alignment error or fault")
_eentry(EENDF, "End of file or stream") _EENTRY(EENDF, -17, "End of file or stream")
_eend _EENTRY(_EMAX, -18, "... min error id ...")
_EEND
#undef _estart #undef _ESTART
#undef _eentry #undef _EENTRY
#undef _eend #undef _EEND
#endif #endif

View File

@ -51,7 +51,7 @@ typedef unsigned long uintptr_t;
typedef signed long intmax_t; typedef signed long intmax_t;
typedef unsigned long uintmax_t; typedef unsigned long uintmax_t;
typedef uint error_t; typedef int error_t;
typedef __builtin_va_list va_list; typedef __builtin_va_list va_list;
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//

View File

@ -114,7 +114,7 @@ void IoCreateInputBuffer(void)
rc = BOpenPureBuf(&BStdIn, BS_RDWR, 4 * KB); rc = BOpenPureBuf(&BStdIn, BS_RDWR, 4 * KB);
if (rc) KeStartPanic("[Keyb] Couldn't create BStdIn"); if (rc < EOK) KeStartPanic("[Keyb] Couldn't create BStdIn");
//BEnableLineBuffering(BStdIn); //BEnableLineBuffering(BStdIn);
BStdIn->flusher = bemptybuf; BStdIn->flusher = bemptybuf;

View File

@ -53,7 +53,7 @@ error_t KalAllocMemoryEx(void **ptr, size_t req, int flags, size_t align)
MmUnlockHeap(); MmUnlockHeap();
if (rc) { if (rc < EOK) {
if ((flags & M_CANFAIL) != 0) if ((flags & M_CANFAIL) != 0)
return rc; return rc;
KeStartPanic("KalAllocMemory: Out of memory"); KeStartPanic("KalAllocMemory: Out of memory");

View File

@ -41,7 +41,7 @@ void MmInitMemoryMap(void)
rc = InitMemoryMap(); rc = InitMemoryMap();
if (rc) if (rc < EOK)
KeStartPanic("Failed to initialize the memory map\nError : %s", KeStartPanic("Failed to initialize the memory map\nError : %s",
strerror(rc) ); strerror(rc) );
} }

View File

@ -51,16 +51,20 @@ void ExecuteCommand(char *cmdbuf, Command_t *cmdtable)
memzero(*shargv, ARG_MAX); memzero(*shargv, ARG_MAX);
rc = ShCmdLineToArgVec(cmdbuf, &shargc, argvbuf); rc = ShCmdLineToArgVec(cmdbuf, &shargc, argvbuf);
if (rc) KeStartPanic("Shell: Couldn't parse command line: %d", rc); if (rc < EOK) KeStartPanic("Shell: Couldn't parse command line: %d", rc);
for (cmd = cmdtable; cmd->name != NULL; cmd++) { for (cmd = cmdtable; cmd->name != NULL; cmd++) {
if (!strcmp(cmd->name, shargv[0])) { if (!strcmp(cmd->name, shargv[0])) {
cmd->func(shargc, shargv, cmdbuf); rc = cmd->func(shargc, shargv, cmdbuf);
found = true; found = true;
break; break;
} }
} }
if (found && rc < EOK) {
KernLog("\nerr: %s: return with error code %d: %s\n", cmd->name, rc, strerror(rc));
}
assert(shargv[0] == argvbuf + ARG_MAX); assert(shargv[0] == argvbuf + ARG_MAX);
if (found == false) { if (found == false) {
@ -90,7 +94,6 @@ void ShStartShell(void)
int historyScroll = 0; int historyScroll = 0;
bool insertMode = 0; bool insertMode = 0;
size_t nLines;
argvbuf = malloc(ARG_MAX * 2); argvbuf = malloc(ARG_MAX * 2);
memzero(argvbuf, ARG_MAX * 2); memzero(argvbuf, ARG_MAX * 2);
@ -103,7 +106,7 @@ void ShStartShell(void)
KernLog("\n%C%s%C", VGA_COLOR_WHITE, shprompt, shcol); KernLog("\n%C%s%C", VGA_COLOR_WHITE, shprompt, shcol);
BFlushBuf(BStdOut); BFlushBuf(BStdOut);
while ((rc = BGetFromBuf(BStdIn, &ch)) == EOK || rc == EENDF) { while ((rc = BGetFromBuf(BStdIn, &ch)) >= EOK || rc == EENDF) {
// Reset BStdIn's content // Reset BStdIn's content
if (rc == EENDF) { if (rc == EENDF) {
if (!(BStdIn->flags & BF_ERR)) { if (!(BStdIn->flags & BF_ERR)) {

View File

@ -82,7 +82,7 @@ error_t bgetc(Buffer_t *buf, uchar *ch)
*ch = *buf->rp++; *ch = *buf->rp++;
} }
if (rc != EOK) { if (rc < EOK) {
// buf->flags |= BF_ERR; // buf->flags |= BF_ERR;
} }

View File

@ -111,7 +111,7 @@ error_t BOpenTermBufEx(Buffer_t **pbuf, char *source, int mode,
rc = BOpenPureBufEx(pbuf, source, mode, size, flusher); rc = BOpenPureBufEx(pbuf, source, mode, size, flusher);
if (!rc) { if (!(rc < 0)) {
(*pbuf)->lastLF = 0; (*pbuf)->lastLF = 0;
(*pbuf)->nLines = nLines; (*pbuf)->nLines = nLines;
(*pbuf)->lineLen = lineLen; (*pbuf)->lineLen = lineLen;

View File

@ -111,7 +111,7 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
} }
// Progress in format string // Progress in format string
while (*fmt && !rc) { while (*fmt && !(rc < EOK)) {
// Deal with all non-'%' characters // Deal with all non-'%' characters
if (*fmt != '%') { if (*fmt != '%') {
@ -249,7 +249,7 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
// Zero/nonspecified precision means unlimited amount // Zero/nonspecified precision means unlimited amount
if (prec == 0) prec = INT_MAX; if (prec == 0) prec = INT_MAX;
for (; !rc && *s && prec-- ; s++) { for (; !(rc < EOK) && *s && prec-- ; s++) {
rc = bputc(buf, (uchar)*s); rc = bputc(buf, (uchar)*s);
written++; written++;
} }
@ -353,7 +353,7 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
// When padding with spaces, we pad before +/-'s etc // When padding with spaces, we pad before +/-'s etc
if (!minus && !zero && width > len) { if (!minus && !zero && width > len) {
for (; !rc && width > len ; width--) { for (; !(rc < EOK) && width > len ; width--) {
rc = bputc(buf, ' '); rc = bputc(buf, ' ');
written++; written++;
} }
@ -369,7 +369,7 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
rc = bputc(buf, '0'); rc = bputc(buf, '0');
written++; written++;
if (!rc && base != 8) { if (!(rc < EOK) && base != 8) {
rc = bputc(buf, (base == 2 ? 'b' : (cap ? 'X' : 'x'))); rc = bputc(buf, (base == 2 ? 'b' : (cap ? 'X' : 'x')));
written++; written++;
} }
@ -378,21 +378,21 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
// Deal with padding by zeroes // Deal with padding by zeroes
// The 'minus' flag makes no sense with the 'zero' one // The 'minus' flag makes no sense with the 'zero' one
if (zero && width > len) { if (zero && width > len) {
for (; !rc && width > len ; width--) { for (; !(rc < EOK) && width > len ; width--) {
rc = bputc(buf, '0'); rc = bputc(buf, '0');
written++; written++;
} }
} }
// Output the actual number // Output the actual number
for (; !rc && *s ; s++) { for (; !(rc < EOK) && *s ; s++) {
rc = bputc(buf, (uchar)*s); rc = bputc(buf, (uchar)*s);
written++; written++;
} }
// 'minus' padding, only with spaces // 'minus' padding, only with spaces
if (minus && !zero && width > len) { if (minus && !zero && width > len) {
for (; !rc && width > len ; width--) { for (; !(rc < EOK) && width > len ; width--) {
rc = bputc(buf, ' '); rc = bputc(buf, ' ');
written++; written++;
} }
@ -402,10 +402,10 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
} }
// For debugging purposes // For debugging purposes
assert(!rc && "vbprintf() error"); assert(!(rc < EOK) && "vbprintf() error");
seterrno(rc); seterrno(rc);
if (rc) if (rc < EOK)
return written - 1; // "- 1" because last bputc() must have failed return written - 1; // "- 1" because last bputc() must have failed
else else
return written; return written;

View File

@ -137,7 +137,7 @@ error_t bputc(Buffer_t *buf, uchar ch)
} }
} }
if (rc != EOK) { if (rc < EOK) {
// buf->flags |= BF_ERR; // buf->flags |= BF_ERR;
} }

View File

@ -85,7 +85,7 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
} }
// Progress in format string // Progress in format string
for (*fmt && !rc) { for (*fmt && !(rc < EOK)) {
// Skip all kinds of whitespaces // Skip all kinds of whitespaces
if (isspace(*fmt)) { if (isspace(*fmt)) {
@ -93,7 +93,7 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
another_space: another_space:
rc = bgetc(buf, &ch); rc = bgetc(buf, &ch);
if (!rc) break; if (!(rc < EOK)) break;
// Put back non-whitespaces and progress // Put back non-whitespaces and progress
if (!isspace(ch)) { if (!isspace(ch)) {
@ -110,7 +110,7 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
rc = bgetc(buf, &ch); rc = bgetc(buf, &ch);
// Is it what we expected? // Is it what we expected?
if (!rc && ch != *fmt) { if (!(rc < EOK) && ch != *fmt) {
// No, so put it back // No, so put it back
buf->rp--; buf->rp--;
break; break;
@ -131,7 +131,7 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
if (*fmt == '%') { if (*fmt == '%') {
rc = bgetc(buf, &ch); rc = bgetc(buf, &ch);
if (!rc && ch != '%') { if (!(rc < EOK) && ch != '%') {
buf->rp--; buf->rp--;
break; break;
} }
@ -183,7 +183,7 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
// then store them in successive argument pointers // then store them in successive argument pointers
for (; width; width--) { for (; width; width--) {
rc = bgetc(buf, &ch); rc = bgetc(buf, &ch);
if (!rc) break; if (!(rc < EOK)) break;
char *chptr = va_arg(ap, char *); char *chptr = va_arg(ap, char *);
*chptr = (char)ch; *chptr = (char)ch;
@ -198,7 +198,7 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
} }
// For debugging purposes // For debugging purposes
assert(!rc && "vbscanf() error"); assert(!(rc < EOK) && "vbscanf() error");
seterrno(rc); seterrno(rc);
return readcnt; return readcnt;
@ -208,4 +208,3 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
#endif #endif

View File

@ -25,11 +25,11 @@
#include <libc.h> #include <libc.h>
#undef _ERRNO_H #undef _ERRNO_H
#define _estart #define _ESTART
#define _eentry(code, desc) [code] = desc, #define _EENTRY(code, num, desc) [-code] = desc,
#define _eend #define _EEND
char *__strerror[_EMAX] = { char *__strerror[] = {
#include <errno.h> #include <errno.h>
@ -37,6 +37,9 @@ char *__strerror[_EMAX] = {
const char *strerror(error_t code) const char *strerror(error_t code)
{ {
return __strerror[code]; if (code > 0 || code <= _EMAX)
return NULL;
return __strerror[-code];
} }

View File

@ -41,7 +41,7 @@ void *malloc(size_t n)
#else #else
rc = KalAllocMemoryEx(&ptr, n, M_ZEROED, 0); rc = KalAllocMemoryEx(&ptr, n, M_ZEROED, 0);
#endif #endif
if (rc > 0) seterrno(rc); if (rc < EOK) seterrno(rc);
return ptr; return ptr;
} }
@ -56,7 +56,7 @@ void *memalign(size_t n, size_t align)
#else #else
rc = KalAllocMemoryEx(&ptr, n, M_ZEROED, align); rc = KalAllocMemoryEx(&ptr, n, M_ZEROED, align);
#endif #endif
if (rc > 0) seterrno(rc); if (rc < EOK) seterrno(rc);
return ptr; return ptr;
} }
@ -67,7 +67,7 @@ void *calloc(size_t n, size_t m)
error_t rc; error_t rc;
rc = KalAllocMemoryEx(&ptr, n * m, M_ZEROED, 0); rc = KalAllocMemoryEx(&ptr, n * m, M_ZEROED, 0);
if (rc > 0) seterrno(rc); if (rc < EOK) seterrno(rc);
return ptr; return ptr;
} }

View File

@ -92,7 +92,7 @@ size_t vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
rc = BOpenPureBuf(&buf, BS_WRONLY, n-1); rc = BOpenPureBuf(&buf, BS_WRONLY, n-1);
// n-1 to leave place for the '\0' // n-1 to leave place for the '\0'
if (rc != EOK) { if (rc < EOK) {
goto fail; goto fail;
} }