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;
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
@ -336,7 +336,7 @@ static_assert(sizeof(void *) == 8, _SA_MSG);
#define atomic_xadd(P, V) __sync_fetch_and_add((P), (V)) #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_cmpxchg(P, O, N) __sync_val_compare_and_swap((P), (O), (N))
#define atomic_inc(P) __sync_add_and_fetch((P), 1) #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_add(P, V) __sync_add_and_fetch((P), (V))
#define atomic_set_bit(P, V) __sync_or_and_fetch((P), 1<<(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))) #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 bool controlPressed = 0;
static void KeybPrint(uchar _code) static void KeybPrint(uchar _code)
{ {
uint code = (uint)_code; uint code = (uint)_code;
const uint *table; const uint *table;
if (!e0received) { if (!e0received) {
table = (altPressed ? LeftAltScanCodes table = (altPressed ? LeftAltScanCodes
: (controlPressed ? LeftControlScanCodes : (controlPressed ? LeftControlScanCodes
@ -65,7 +65,7 @@ static void KeybPrint(uchar _code)
} }
uchar ch = KEY(code); uchar ch = KEY(code);
if (!ch) { if (!ch) {
switch (code) { switch (code) {
case 0xE0: e0received = 1; break; case 0xE0: e0received = 1; break;
@ -82,12 +82,12 @@ static void KeybPrint(uchar _code)
case 0x1D: controlPressed = 1; break; case 0x1D: controlPressed = 1; break;
case 0x9D: controlPressed = 0; break; case 0x9D: controlPressed = 0; break;
case 0x3A: capsLockActive = !capsLockActive; break; case 0x3A: capsLockActive = !capsLockActive; break;
} }
return; return;
} }
bputc(BStdIn, ch); bputc(BStdIn, ch);
} }
@ -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;
@ -123,13 +123,13 @@ void IoCreateInputBuffer(void)
void IoEnableKeyb(void) void IoEnableKeyb(void)
{ {
ulong flags = KePauseIRQs(); ulong flags = KePauseIRQs();
KeRegisterISR(KeybHandler, 0x21); KeRegisterISR(KeybHandler, 0x21);
KeUnmaskIRQ(0x1); KeUnmaskIRQ(0x1);
KeRestoreIRQs(flags); KeRestoreIRQs(flags);
KeEnableNMI(); KeEnableNMI();
IoCreateInputBuffer(); 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(); brk = (size_t)_heap_start + MmGetHeapSize();
req = _ALIGN_UP(req + brk, align) - brk; req = _ALIGN_UP(req + brk, align) - brk;
/* DebugLog("MALLOC: start=%p, size=%lx, brk=%p, req=%lx\n", */ /* DebugLog("MALLOC: start=%p, size=%lx, brk=%p, req=%lx\n", */
/* _heap_start, MmGetHeapSize(), brk, req); */ /* _heap_start, MmGetHeapSize(), brk, req); */
@ -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

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

View File

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

View File

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

View File

@ -76,13 +76,13 @@ error_t BOpenPureBufEx(Buffer_t **pbuf, char *source, int mode, size_t size,
buf->flusher = flusher; buf->flusher = flusher;
ExReleaseLock(&buf->lock); ExReleaseLock(&buf->lock);
// Second allocation failed // Second allocation failed
if (buf->buf == NULL) { if (buf->buf == NULL) {
BCloseBuf(buf); BCloseBuf(buf);
return ENOMEM; return ENOMEM;
} }
*pbuf = buf; *pbuf = buf;
return EOK; return EOK;
} }
@ -108,16 +108,16 @@ error_t BOpenTermBufEx(Buffer_t **pbuf, char *source, int mode,
size_t size = lineLen * nLines * (pbCount + 1); size_t size = lineLen * nLines * (pbCount + 1);
assert(lineLen > 0 && nLines > 0 && pbCount >= 0); assert(lineLen > 0 && nLines > 0 && pbCount >= 0);
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;
(*pbuf)->flags |= BF_TERM; (*pbuf)->flags |= BF_TERM;
} }
return rc; 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 BPrintOnBufV(Buffer_t *buf, const char *fmt, va_list ap)
{ {
size_t sz; size_t sz;
ExAcquireLock(&buf->lock); ExAcquireLock(&buf->lock);
sz = vbprintf(buf, fmt, ap); sz = vbprintf(buf, fmt, ap);
ExReleaseLock(&buf->lock); ExReleaseLock(&buf->lock);
return sz; return sz;
} }
@ -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 != '%') {
@ -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++; plus++; }
else if (*fmt == '-') { fmt++; minus++; } else if (*fmt == '-') { fmt++; minus++; }
else if (*fmt == ' ') { fmt++; space++; } else if (*fmt == ' ') { fmt++; space++; }
// Next field // Next field
else break; else break;
} }
@ -172,7 +172,7 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
} \ } \
} \ } \
} while (0) } while (0)
// Extract width // Extract width
bextractwp(width); bextractwp(width);
@ -181,7 +181,7 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
width = -width; width = -width;
minus++; minus++;
} }
// Extract precision // Extract precision
if (*fmt == '.') { if (*fmt == '.') {
fmt++; fmt++;
@ -202,7 +202,7 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
fmt++; fmt++;
h++; h++;
} }
// Next field // Next field
else break; else break;
} }
@ -228,7 +228,7 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
#ifdef _KALEID_KERNEL #ifdef _KALEID_KERNEL
if (type == 'C') { if (type == 'C') {
base = va_arg(ap, int); base = va_arg(ap, int);
if (!(base < 0 || base > VGA_COLOR_WHITE)) { if (!(base < 0 || base > VGA_COLOR_WHITE)) {
rc = bputc(buf, RtlColorToChar(base)); rc = bputc(buf, RtlColorToChar(base));
written++; 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 // amount of characters to be read from the stream
// 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++;
} }
continue; continue;
} }
// Make sure width and prec aren't too big // 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) { if (width > CONVBUFSIZE || prec > CONVBUFSIZE) {
written++; // Work around "if (rc) return written - 1;" written++; // Work around "if (rc) return written - 1;"
rc = EINVAL; rc = EINVAL;
@ -339,7 +339,7 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
// Reset s // Reset s
s = convbuf; s = convbuf;
} }
// Compute s's length // Compute s's length
// It will always fit in an int // It will always fit in an int
else len = (int)strlen(s); 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 // 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++;
} }
@ -365,11 +365,11 @@ size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
else if (sgn && space) { rc = bputc(buf, ' '); written++; } else if (sgn && space) { rc = bputc(buf, ' '); written++; }
// Print 0 for octal, 0x for hexadecimal, 0b for binary // 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'); 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,34 +378,34 @@ 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++;
} }
} }
// Carry on to next modifier // Carry on to next modifier
} }
// 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

@ -60,7 +60,7 @@ error_t bputc(Buffer_t *buf, uchar ch)
// We need a field for choosing a different filler, e.g. '\0' // We need a field for choosing a different filler, e.g. '\0'
if (ch == '\n') { if (ch == '\n') {
assert(buf->lastLF < buf->lineLen); assert(buf->lastLF < buf->lineLen);
int off = buf->lineLen - buf->lastLF; int off = buf->lineLen - buf->lastLF;
while (off > 0) { while (off > 0) {
*buf->wp++ = ' '; *buf->wp++ = ' ';
@ -124,23 +124,23 @@ error_t bputc(Buffer_t *buf, uchar ch)
} }
*buf->wp++ = ch; *buf->wp++ = ch;
if (buf->wp == buf->buf + buf->size) { if (buf->wp == buf->buf + buf->size) {
if (!(buf->flags & (BF_LINE|BF_FONCLOSE))) { if (!(buf->flags & (BF_LINE|BF_FONCLOSE))) {
if (buf->flusher) buf->flusher(buf); if (buf->flusher) buf->flusher(buf);
} }
} }
// Line-buffering // Line-buffering
if (ch == '\n' && buf->flags & BF_LINE) { if (ch == '\n' && buf->flags & BF_LINE) {
if (buf->flusher) buf->flusher(buf); if (buf->flusher) buf->flusher(buf);
} }
} }
if (rc != EOK) { if (rc < EOK) {
// buf->flags |= BF_ERR; // buf->flags |= BF_ERR;
} }
return rc; 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 BScanFromBufV(Buffer_t *buf, const char *fmt, va_list ap)
{ {
size_t rc; size_t rc;
ExAcquireLock(&buf->lock); ExAcquireLock(&buf->lock);
sz = vbscanf(buf, fmt, ap); sz = vbscanf(buf, fmt, ap);
ExReleaseLock(&buf->lock); ExReleaseLock(&buf->lock);
return sz; return sz;
} }
@ -85,15 +85,15 @@ 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)) {
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)) {
@ -104,13 +104,13 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
else goto another_space; else goto another_space;
} }
// Deal with all non-'%' non-whitespace characters // Deal with all non-'%' non-whitespace characters
if (*fmt != '%') { if (*fmt != '%') {
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,11 +131,11 @@ 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;
} }
continue; continue;
} }
@ -155,7 +155,7 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
fmt++; fmt++;
} }
assert(!(width < 0)); assert(!(width < 0));
// //
// Extract length field // Extract length field
@ -180,10 +180,10 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
if (width == 0) width = 1; if (width == 0) width = 1;
// For >1 width, read that many characters // For >1 width, read that many characters
// 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;
@ -193,12 +193,12 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
// Decimal integer // Decimal integer
else if (*fmt == 'd') { else if (*fmt == 'd') {
} }
} }
// 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;
} }
@ -65,10 +65,10 @@ void *calloc(size_t n, size_t m)
{ {
void *ptr; void *ptr;
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;
} }
@ -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 // We don't mind EOFs, just just return how much was successfully written
if (sz == 0 && !(buf->flags & BF_EOF)) if (sz == 0 && !(buf->flags & BF_EOF))
goto fail; goto fail;
ret = (size_t)buf->wp - (size_t)buf->buf; ret = (size_t)buf->wp - (size_t)buf->buf;
if (ret > 0) if (ret > 0)
memmove(str, (char *)buf->buf, ret); memmove(str, (char *)buf->buf, ret);