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

View File

@ -51,7 +51,7 @@ typedef unsigned long uintptr_t;
typedef signed long intmax_t;
typedef unsigned long uintmax_t;
typedef uint error_t;
typedef int error_t;
typedef __builtin_va_list va_list;
//----------------------------------------------------------------------------//
@ -336,7 +336,7 @@ static_assert(sizeof(void *) == 8, _SA_MSG);
#define atomic_xadd(P, V) __sync_fetch_and_add((P), (V))
#define atomic_cmpxchg(P, O, N) __sync_val_compare_and_swap((P), (O), (N))
#define atomic_inc(P) __sync_add_and_fetch((P), 1)
#define atomic_dec(P) __sync_add_and_fetch((P), -1)
#define atomic_dec(P) __sync_add_and_fetch((P), -1)
#define atomic_add(P, V) __sync_add_and_fetch((P), (V))
#define atomic_set_bit(P, V) __sync_or_and_fetch((P), 1<<(V))
#define atomic_clear_bit(P, V) __sync_and_and_fetch((P), ~(1<<(V)))

View File

@ -40,11 +40,11 @@ static bool shiftPressed = 0;
static bool controlPressed = 0;
static void KeybPrint(uchar _code)
{
{
uint code = (uint)_code;
const uint *table;
if (!e0received) {
table = (altPressed ? LeftAltScanCodes
: (controlPressed ? LeftControlScanCodes
@ -65,7 +65,7 @@ static void KeybPrint(uchar _code)
}
uchar ch = KEY(code);
if (!ch) {
switch (code) {
case 0xE0: e0received = 1; break;
@ -82,12 +82,12 @@ static void KeybPrint(uchar _code)
case 0x1D: controlPressed = 1; break;
case 0x9D: controlPressed = 0; break;
case 0x3A: capsLockActive = !capsLockActive; break;
}
return;
}
bputc(BStdIn, ch);
}
@ -114,7 +114,7 @@ void IoCreateInputBuffer(void)
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);
BStdIn->flusher = bemptybuf;
@ -123,13 +123,13 @@ void IoCreateInputBuffer(void)
void IoEnableKeyb(void)
{
ulong flags = KePauseIRQs();
KeRegisterISR(KeybHandler, 0x21);
KeUnmaskIRQ(0x1);
KeRestoreIRQs(flags);
KeEnableNMI();
IoCreateInputBuffer();
}

View File

@ -45,7 +45,7 @@ error_t KalAllocMemoryEx(void **ptr, size_t req, int flags, size_t align)
brk = (size_t)_heap_start + MmGetHeapSize();
req = _ALIGN_UP(req + brk, align) - brk;
/* DebugLog("MALLOC: start=%p, size=%lx, brk=%p, req=%lx\n", */
/* _heap_start, MmGetHeapSize(), brk, req); */
@ -53,7 +53,7 @@ error_t KalAllocMemoryEx(void **ptr, size_t req, int flags, size_t align)
MmUnlockHeap();
if (rc) {
if (rc < EOK) {
if ((flags & M_CANFAIL) != 0)
return rc;
KeStartPanic("KalAllocMemory: Out of memory");

View File

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

View File

@ -120,7 +120,7 @@ error_t CmdDmesg(int argc, char **argv, char *cmdline)
BLockBuf(BStdDbg);
ptr = (char *)lmax((ulong)BStdDbg->buf,
ptr = (char *)lmax((ulong)BStdDbg->buf,
(ulong)(BStdDbg->wp - BStdDbg->lastLF - (BStdDbg->lineLen * N)));
KernLog(ptr);

View File

@ -45,24 +45,28 @@ void ExecuteCommand(char *cmdbuf, Command_t *cmdtable)
error_t rc;
Command_t *cmd;
bool found = false;
if (!cmdbuf || !*cmdbuf)
return;
memzero(*shargv, ARG_MAX);
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++) {
if (!strcmp(cmd->name, shargv[0])) {
cmd->func(shargc, shargv, cmdbuf);
rc = cmd->func(shargc, shargv, cmdbuf);
found = true;
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);
if (found == false) {
KernLog("err: command not found: '%.255s' (%ld)\n",
shargv[0], strlen(shargv[0]));
@ -88,9 +92,8 @@ void ShStartShell(void)
char *history = malloc(CMDBUFSIZE * N);
int historyIndex = 0;
int historyScroll = 0;
bool insertMode = 0;
size_t nLines;
argvbuf = malloc(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);
BFlushBuf(BStdOut);
while ((rc = BGetFromBuf(BStdIn, &ch)) == EOK || rc == EENDF) {
while ((rc = BGetFromBuf(BStdIn, &ch)) >= EOK || rc == EENDF) {
// Reset BStdIn's content
if (rc == EENDF) {
if (!(BStdIn->flags & BF_ERR)) {
@ -118,7 +121,7 @@ void ShStartShell(void)
}
else break;
}
switch (ch) {
// Backspace character
@ -145,22 +148,22 @@ void ShStartShell(void)
case K_ESCAPE: PoShutdown(); break;
case K_PAGE_UP: IoScrollUp(); break;
case K_PAGE_DOWN: IoScrollDown(); break;
case K_INSERT:
insertMode = 1 - insertMode;
break;
// Move within buffer
case K_ARRW_LEFT:
if (!(bufptr > cmdbuf))
break;
bufptr--;
IoSetCursorOffset(IoGetCursorOffset()-1);
BFlushBuf(BStdOut);
break;
case K_ARRW_RIGHT:
if (bufptr == bufend)
break;
@ -169,7 +172,7 @@ void ShStartShell(void)
IoSetCursorOffset(IoGetCursorOffset()+1);
BFlushBuf(BStdOut);
break;
case K_HOME:
if (bufptr == cmdbuf)
break;
@ -178,7 +181,7 @@ void ShStartShell(void)
bufptr = cmdbuf;
BFlushBuf(BStdOut);
break;
case K_END:
if (bufptr == bufend)
break;
@ -221,7 +224,7 @@ void ShStartShell(void)
bufend = bufptr;
*bufptr = 0;
// Advance cursor
if (IoGetCursorOffset() < 0)
IoSetCursorOffset(IoGetCursorOffset() + 1);
@ -254,7 +257,7 @@ void ShStartShell(void)
VGA_COLOR_WHITE, shprompt, shcol, cmdbuf);
BUnlockBuf(BStdOut);
IoSetScroll(1);
IoScrollDown(); // This calls the flusher
@ -283,13 +286,13 @@ void ShStartShell(void)
assert(historyIndex == N);
memmove(history, history+CMDBUFSIZE, CMDBUFSIZE * (N-1));
historyIndex = historyScroll = N-1;
}
// Update history
memmove(&history[historyIndex*CMDBUFSIZE], cmdbuf, CMDBUFSIZE);
historyScroll = historyIndex;
historyIndex++;
// Execute & reset
ExecuteCommand(cmdbuf, shcmdtable);
memzero(cmdbuf, CMDBUFSIZE);

View File

@ -51,9 +51,9 @@ error_t bgetc(Buffer_t *buf, uchar *ch)
error_t rc = EOK;
if (buf->flags & (BF_EOF|BF_ERR)) return EENDF;
assert(!(buf->rp > buf->wp)); // Shouldn't be possible right now
if (buf->rp == buf->buf + buf->size) {
buf->flags |= BF_EOF;
return EENDF;
@ -64,7 +64,7 @@ error_t bgetc(Buffer_t *buf, uchar *ch)
*ch = *buf->rp++;
return EOK;
}
// We need to fill the buffer
else {
// We need a BFiller_t
@ -78,14 +78,14 @@ error_t bgetc(Buffer_t *buf, uchar *ch)
return EENDF; // Currently empty
#endif
}
*ch = *buf->rp++;
}
if (rc != EOK) {
if (rc < EOK) {
// buf->flags |= BF_ERR;
}
return rc;
}

View File

@ -76,13 +76,13 @@ error_t BOpenPureBufEx(Buffer_t **pbuf, char *source, int mode, size_t size,
buf->flusher = flusher;
ExReleaseLock(&buf->lock);
// Second allocation failed
if (buf->buf == NULL) {
BCloseBuf(buf);
return ENOMEM;
}
*pbuf = buf;
return EOK;
}
@ -108,16 +108,16 @@ error_t BOpenTermBufEx(Buffer_t **pbuf, char *source, int mode,
size_t size = lineLen * nLines * (pbCount + 1);
assert(lineLen > 0 && nLines > 0 && pbCount >= 0);
rc = BOpenPureBufEx(pbuf, source, mode, size, flusher);
if (!rc) {
if (!(rc < 0)) {
(*pbuf)->lastLF = 0;
(*pbuf)->nLines = nLines;
(*pbuf)->lineLen = lineLen;
(*pbuf)->flags |= BF_TERM;
}
return rc;
}

View File

@ -48,11 +48,11 @@ size_t BPrintOnBuf(Buffer_t *buf, const char *fmt, ...)
size_t BPrintOnBufV(Buffer_t *buf, const char *fmt, va_list ap)
{
size_t sz;
ExAcquireLock(&buf->lock);
sz = vbprintf(buf, fmt, ap);
ExReleaseLock(&buf->lock);
return sz;
}
@ -111,7 +111,7 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
}
// Progress in format string
while (*fmt && !rc) {
while (*fmt && !(rc < EOK)) {
// Deal with all non-'%' characters
if (*fmt != '%') {
@ -149,7 +149,7 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
else if (*fmt == '+') { fmt++; plus++; }
else if (*fmt == '-') { fmt++; minus++; }
else if (*fmt == ' ') { fmt++; space++; }
// Next field
else break;
}
@ -172,7 +172,7 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
} \
} \
} while (0)
// Extract width
bextractwp(width);
@ -181,7 +181,7 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
width = -width;
minus++;
}
// Extract precision
if (*fmt == '.') {
fmt++;
@ -202,7 +202,7 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
fmt++;
h++;
}
// Next field
else break;
}
@ -228,7 +228,7 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
#ifdef _KALEID_KERNEL
if (type == 'C') {
base = va_arg(ap, int);
if (!(base < 0 || base > VGA_COLOR_WHITE)) {
rc = bputc(buf, RtlColorToChar(base));
written++;
@ -248,18 +248,18 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
// amount of characters to be read from the stream
// Zero/nonspecified precision means unlimited amount
if (prec == 0) prec = INT_MAX;
for (; !rc && *s && prec-- ; s++) {
for (; !(rc < EOK) && *s && prec-- ; s++) {
rc = bputc(buf, (uchar)*s);
written++;
}
continue;
}
// Make sure width and prec aren't too big
// (We didn't do that earlier because %s uses width)
// (We didn't do that earlier because %s uses width)
if (width > CONVBUFSIZE || prec > CONVBUFSIZE) {
written++; // Work around "if (rc) return written - 1;"
rc = EINVAL;
@ -339,7 +339,7 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
// Reset s
s = convbuf;
}
// Compute s's length
// It will always fit in an int
else len = (int)strlen(s);
@ -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
if (!minus && !zero && width > len) {
for (; !rc && width > len ; width--) {
for (; !(rc < EOK) && width > len ; width--) {
rc = bputc(buf, ' ');
written++;
}
@ -365,11 +365,11 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
else if (sgn && space) { rc = bputc(buf, ' '); written++; }
// Print 0 for octal, 0x for hexadecimal, 0b for binary
else if (hash && (base == 2 || base == 8 || base == 16)) {
else if (hash && (base == 2 || base == 8 || base == 16)) {
rc = bputc(buf, '0');
written++;
if (!rc && base != 8) {
if (!(rc < EOK) && base != 8) {
rc = bputc(buf, (base == 2 ? 'b' : (cap ? 'X' : 'x')));
written++;
}
@ -378,34 +378,34 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
// Deal with padding by zeroes
// The 'minus' flag makes no sense with the 'zero' one
if (zero && width > len) {
for (; !rc && width > len ; width--) {
for (; !(rc < EOK) && width > len ; width--) {
rc = bputc(buf, '0');
written++;
}
}
// Output the actual number
for (; !rc && *s ; s++) {
for (; !(rc < EOK) && *s ; s++) {
rc = bputc(buf, (uchar)*s);
written++;
}
// 'minus' padding, only with spaces
if (minus && !zero && width > len) {
for (; !rc && width > len ; width--) {
for (; !(rc < EOK) && width > len ; width--) {
rc = bputc(buf, ' ');
written++;
}
}
// Carry on to next modifier
}
// For debugging purposes
assert(!rc && "vbprintf() error");
assert(!(rc < EOK) && "vbprintf() error");
seterrno(rc);
if (rc)
if (rc < EOK)
return written - 1; // "- 1" because last bputc() must have failed
else
return written;

View File

@ -60,7 +60,7 @@ error_t bputc(Buffer_t *buf, uchar ch)
// We need a field for choosing a different filler, e.g. '\0'
if (ch == '\n') {
assert(buf->lastLF < buf->lineLen);
int off = buf->lineLen - buf->lastLF;
while (off > 0) {
*buf->wp++ = ' ';
@ -124,23 +124,23 @@ error_t bputc(Buffer_t *buf, uchar ch)
}
*buf->wp++ = ch;
if (buf->wp == buf->buf + buf->size) {
if (!(buf->flags & (BF_LINE|BF_FONCLOSE))) {
if (buf->flusher) buf->flusher(buf);
}
}
// Line-buffering
if (ch == '\n' && buf->flags & BF_LINE) {
if (buf->flusher) buf->flusher(buf);
}
}
if (rc != EOK) {
if (rc < EOK) {
// buf->flags |= BF_ERR;
}
return rc;
}

View File

@ -45,11 +45,11 @@ size_t BScanFromBuf(Buffer_t *buf, const char *fmt, ...)
size_t BScanFromBufV(Buffer_t *buf, const char *fmt, va_list ap)
{
size_t rc;
ExAcquireLock(&buf->lock);
sz = vbscanf(buf, fmt, ap);
ExReleaseLock(&buf->lock);
return sz;
}
@ -85,15 +85,15 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
}
// Progress in format string
for (*fmt && !rc) {
for (*fmt && !(rc < EOK)) {
// Skip all kinds of whitespaces
if (isspace(*fmt)) {
another_space:
rc = bgetc(buf, &ch);
if (!rc) break;
if (!(rc < EOK)) break;
// Put back non-whitespaces and progress
if (!isspace(ch)) {
@ -104,13 +104,13 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
else goto another_space;
}
// Deal with all non-'%' non-whitespace characters
if (*fmt != '%') {
rc = bgetc(buf, &ch);
// Is it what we expected?
if (!rc && ch != *fmt) {
if (!(rc < EOK) && ch != *fmt) {
// No, so put it back
buf->rp--;
break;
@ -131,11 +131,11 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
if (*fmt == '%') {
rc = bgetc(buf, &ch);
if (!rc && ch != '%') {
if (!(rc < EOK) && ch != '%') {
buf->rp--;
break;
}
continue;
}
@ -155,7 +155,7 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
fmt++;
}
assert(!(width < 0));
assert(!(width < 0));
//
// Extract length field
@ -180,10 +180,10 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
if (width == 0) width = 1;
// For >1 width, read that many characters
// then store them in successive argument pointers
// then store them in successive argument pointers
for (; width; width--) {
rc = bgetc(buf, &ch);
if (!rc) break;
if (!(rc < EOK)) break;
char *chptr = va_arg(ap, char *);
*chptr = (char)ch;
@ -193,12 +193,12 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
// Decimal integer
else if (*fmt == 'd') {
}
}
// For debugging purposes
assert(!rc && "vbscanf() error");
assert(!(rc < EOK) && "vbscanf() error");
seterrno(rc);
return readcnt;
@ -208,4 +208,3 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
#endif

View File

@ -25,11 +25,11 @@
#include <libc.h>
#undef _ERRNO_H
#define _estart
#define _eentry(code, desc) [code] = desc,
#define _eend
#define _ESTART
#define _EENTRY(code, num, desc) [-code] = desc,
#define _EEND
char *__strerror[_EMAX] = {
char *__strerror[] = {
#include <errno.h>
@ -37,6 +37,9 @@ char *__strerror[_EMAX] = {
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
rc = KalAllocMemoryEx(&ptr, n, M_ZEROED, 0);
#endif
if (rc > 0) seterrno(rc);
if (rc < EOK) seterrno(rc);
return ptr;
}
@ -56,7 +56,7 @@ void *memalign(size_t n, size_t align)
#else
rc = KalAllocMemoryEx(&ptr, n, M_ZEROED, align);
#endif
if (rc > 0) seterrno(rc);
if (rc < EOK) seterrno(rc);
return ptr;
}
@ -65,10 +65,10 @@ void *calloc(size_t n, size_t m)
{
void *ptr;
error_t rc;
rc = KalAllocMemoryEx(&ptr, n * m, M_ZEROED, 0);
if (rc > 0) seterrno(rc);
if (rc < EOK) seterrno(rc);
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);
// n-1 to leave place for the '\0'
if (rc != EOK) {
if (rc < EOK) {
goto fail;
}
@ -101,9 +101,9 @@ size_t vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
// We don't mind EOFs, just just return how much was successfully written
if (sz == 0 && !(buf->flags & BF_EOF))
goto fail;
ret = (size_t)buf->wp - (size_t)buf->buf;
if (ret > 0)
memmove(str, (char *)buf->buf, ret);