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

libbuf (reversal of reversal)

This commit is contained in:
Julian Barathieu 2020-02-11 11:16:24 +01:00
parent b0c5ef74d2
commit ff0cb34df0
7 changed files with 293 additions and 113 deletions

View File

@ -61,8 +61,8 @@ extern const char *PsPrioClassesNames[];
// Re-scheduling and preemption // Re-scheduling and preemption
// XXX atomic operations // XXX atomic operations
// //
#define PsRequestReSched() (++KeCurCPU->needReSched) #define PsRequestReSched() do { ++KeCurCPU->needReSched; } while (0)
#define PsDisablePreemption() (++KeCurCPU->preemptCount) #define PsDisablePreemption() do { ++KeCurCPU->preemptCount; } while (0)
#define PsEnablePreemption() do { assert(KeCurCPU->preemptCount > 0); \ #define PsEnablePreemption() do { assert(KeCurCPU->preemptCount > 0); \
--KeCurCPU->preemptCount; } while(0) --KeCurCPU->preemptCount; } while(0)

View File

@ -138,14 +138,18 @@ int BGetState(Buffer_t *);
error_t BFlushBuf(Buffer_t *); error_t BFlushBuf(Buffer_t *);
error_t BPutOnBuf(Buffer_t *, uchar); error_t BPutOnBuf(Buffer_t *, uchar);
error_t BWriteOnBuf(Buffer_t *, uchar *, size_t);
error_t BPrintOnBuf(Buffer_t *, const char *, ...);
error_t BPrintOnBufV(Buffer_t *, const char *, va_list);
error_t BGetFromBuf(Buffer_t *, uchar *); error_t BGetFromBuf(Buffer_t *, uchar *);
error_t BReadBuf(Buffer_t *, uchar *, size_t);
error_t BScanBuf(Buffer_t *, const char *, ...); // The following functions return the number of bytes written to the buffer
error_t BScanBufV(Buffer_t *, const char *, va_list); size_t BWriteOnBuf(Buffer_t *, uchar *, size_t);
size_t BPrintOnBuf(Buffer_t *, const char *, ...);
size_t BPrintOnBufV(Buffer_t *, const char *, va_list);
// The following functions return the number of elements from their
// va_list's their wrote too, e.g. successful bscanf("%d %d", &a, &b) == 2
size_t BReadFromBuf(Buffer_t *, uchar *, size_t);
size_t BScanFromBuf(Buffer_t *, const char *, ...);
size_t BScanFromBufV(Buffer_t *, const char *, va_list);
void BLockBuf(Buffer_t *); void BLockBuf(Buffer_t *);
void BUnlockBuf(Buffer_t *); void BUnlockBuf(Buffer_t *);
@ -157,14 +161,14 @@ bool BTrylockBuf(Buffer_t *);
// //
error_t bputc(Buffer_t *, uchar); error_t bputc(Buffer_t *, uchar);
error_t bwrite(Buffer_t *, uchar *, size_t); size_t bwrite(Buffer_t *, uchar *, size_t);
error_t bprintf(Buffer_t *, const char *, ...); size_t bprintf(Buffer_t *, const char *, ...);
error_t vbprintf(Buffer_t *, const char *, va_list); size_t vbprintf(Buffer_t *, const char *, va_list);
error_t bgetc(Buffer_t *, uchar *); error_t bgetc(Buffer_t *, uchar *);
error_t bread(Buffer_t *, uchar *, size_t); size_t bread(Buffer_t *, uchar *, size_t);
error_t bscanf(Buffer_t *, const char *, ...); size_t bscanf(Buffer_t *, size_t *, const char *, ...);
error_t vbscanf(Buffer_t *, const char *, va_list); size_t vbscanf(Buffer_t *, size_t *, const char *, va_list);
error_t bemptybuf(Buffer_t *); error_t bemptybuf(Buffer_t *);
error_t bscrolldown(Buffer_t *); error_t bscrolldown(Buffer_t *);

View File

@ -26,7 +26,6 @@
#include <ke/proc.h> #include <ke/proc.h>
#include <io/vga.h> #include <io/vga.h>
error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap);
extern bool KeIdtIsInitialized; extern bool KeIdtIsInitialized;
// //

View File

@ -43,6 +43,9 @@ error_t BGetFromBuf(Buffer_t *buf, uchar *ch)
ExReleaseLock(&buf->lock); ExReleaseLock(&buf->lock);
return rc; return rc;
} }
// Note: any change to this must be done while
// keeping in mind vbscanf()'s workaround for ungetc()
error_t bgetc(Buffer_t *buf, uchar *ch) error_t bgetc(Buffer_t *buf, uchar *ch)
{ {
error_t rc = EOK; error_t rc = EOK;
@ -70,6 +73,9 @@ error_t bgetc(Buffer_t *buf, uchar *ch)
while (buf->rp >= buf->wp) { while (buf->rp >= buf->wp) {
#ifdef _KALEID_KERNEL #ifdef _KALEID_KERNEL
KeSleep(1); // XXX synchronization with keyboard driver KeSleep(1); // XXX synchronization with keyboard driver
#else
buf->flags |= BF_EOF;
return EENDF; // Currently empty
#endif #endif
} }

View File

@ -31,73 +31,41 @@
// //
// Prints formatted string on buf according to fmt // Prints formatted string on buf according to fmt
// //
error_t BPrintOnBuf(Buffer_t *buf, const char *fmt, ...) size_t BPrintOnBuf(Buffer_t *buf, const char *fmt, ...)
{ {
error_t rc; size_t sz;
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
ExAcquireLock(&buf->lock); ExAcquireLock(&buf->lock);
rc = vbprintf(buf, fmt, ap); sz = vbprintf(buf, fmt, ap);
ExReleaseLock(&buf->lock); ExReleaseLock(&buf->lock);
va_end(ap); va_end(ap);
return rc; return sz;
} }
error_t BPrintOnBufV(Buffer_t *buf, const char *fmt, va_list ap) size_t BPrintOnBufV(Buffer_t *buf, const char *fmt, va_list ap)
{ {
error_t rc; size_t sz;
ExAcquireLock(&buf->lock); ExAcquireLock(&buf->lock);
rc = vbprintf(buf, fmt, ap); sz = vbprintf(buf, fmt, ap);
ExReleaseLock(&buf->lock); ExReleaseLock(&buf->lock);
return rc; return sz;
} }
error_t bprintf(Buffer_t *buf, const char *fmt, ...) size_t bprintf(Buffer_t *buf, const char *fmt, ...)
{ {
error_t rc; error_t sz;
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
rc = vbprintf(buf, fmt, ap); sz = vbprintf(buf, fmt, ap);
va_end(ap); va_end(ap);
return rc; return sz;
}
//
// Prints 0 for octal, 0x for hexadecimal, 0b for binary
//
static error_t bprinthash(Buffer_t *buf, int base, int cap)
{
error_t rc;
if (base != 2 && base != 8 && base != 16) {
return EOK;
}
rc = bputc(buf, '0');
if (!rc && base != 8) {
rc = bputc(buf, (base == 2 ? 'b' : (cap ? 'X' : 'x')));
}
return rc;
}
static error_t bdopadding(Buffer_t *buf, size_t width, size_t len,
char filler)
{
error_t rc = EOK;
for (; !rc && width > len ; width--) {
rc = bputc(buf, filler);
}
return rc;
} }
#define CONVBUFSIZE 100 #define CONVBUFSIZE 100
@ -106,9 +74,10 @@ static error_t bdopadding(Buffer_t *buf, size_t width, size_t len,
// Actually does BPrintOnBuf's job; doesn't lock anything // Actually does BPrintOnBuf's job; doesn't lock anything
// Quite a long function // Quite a long function
// //
error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap) size_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
{ {
error_t rc = 0; error_t rc = 0;
size_t written = 0;
ssize_t width, prec, len; ssize_t width, prec, len;
char type; char type;
@ -134,10 +103,11 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
// Base // Base
int base; int base;
if (!buf || !fmt) return EINVAL; if (!buf || !fmt) { seterrno(EINVAL); return 0; }
if (buf->flags & (BF_EOF|BF_ERR)) return EENDF; if (buf->flags & (BF_EOF|BF_ERR)) { seterrno(EENDF); return 0; }
if (buf->state != BS_RDWR && buf->state != BS_WRONLY) { if (buf->state != BS_RDWR && buf->state != BS_WRONLY) {
return EBADF; seterrno(EBADF);
return 0;
} }
// Progress in format string // Progress in format string
@ -146,14 +116,12 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
// Deal with all non-'%' characters // Deal with all non-'%' characters
if (*fmt != '%') { if (*fmt != '%') {
rc = bputc(buf, *fmt); rc = bputc(buf, *fmt);
written++, fmt++;
fmt++;
continue; continue;
} }
// //
// %[parameter][flags][width|*][.precision|*][length]type // %[flags][width|*][.precision|*][length]type
// We aren't dealing with parameters and floating stuff just yet
// //
// Skip the '%' // Skip the '%'
@ -162,8 +130,7 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
// "%%" modifier // "%%" modifier
if (*fmt == '%') { if (*fmt == '%') {
rc = bputc(buf, '%'); rc = bputc(buf, '%');
written++, fmt++;
fmt++;
continue; continue;
} }
@ -253,6 +220,7 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
if (type == 'c') { if (type == 'c') {
uch = (uchar)va_arg(ap, int); uch = (uchar)va_arg(ap, int);
rc = bputc(buf, uch); rc = bputc(buf, uch);
written++;
continue; continue;
} }
@ -261,11 +229,11 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
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++;
}
assert(!rc && 1);
continue; continue;
} }
#endif #endif
@ -281,8 +249,9 @@ error_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 (; *s && prec-- ; s++) { for (; !rc && *s && prec-- ; s++) {
rc = bputc(buf, (uchar)*s); rc = bputc(buf, (uchar)*s);
written++;
} }
continue; continue;
@ -292,6 +261,7 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
// 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;"
rc = EINVAL; rc = EINVAL;
break; break;
} }
@ -314,15 +284,15 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap)
// End of string too soon // End of string too soon
else if (type == '\0') { else if (type == '\0') {
bputc(buf, '%'); bputc(buf, '%');
written++; // Work around "if (rc) return written - 1;"
rc = EINVAL; rc = EINVAL;
break; break;
} }
// Unknown/unsupported modifier // Unknown/unsupported modifier
else { else {
bputc(buf, '%'); rc = bputc(buf, '%');
bputc(buf, '?'); written++;
rc = bputc(buf, type);
continue; continue;
} }
@ -382,36 +352,62 @@ error_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) {
bdopadding(buf, width, len, ' '); for (; !rc && width > len ; width--) {
rc = bputc(buf, ' ');
written++;
}
}
// Deal with signs and the hash flag // Deal with signs and the hash flag
if (*s == '-') { rc = bputc(buf, '-'); s++, len--; } if (*s == '-') { rc = bputc(buf, '-'); s++, len--; written++; }
else if (sgn && plus) rc = bputc(buf, '+'); else if (sgn && plus) { rc = bputc(buf, '+'); written++; }
else if (sgn && space) rc = bputc(buf, ' '); else if (sgn && space) { rc = bputc(buf, ' '); written++; }
else if (hash) bprinthash(buf, base, cap);
// Print 0 for octal, 0x for hexadecimal, 0b for binary
else if (hash && (base == 2 || base == 8 || base == 16)) {
rc = bputc(buf, '0');
written++;
if (!rc && base != 8) {
rc = bputc(buf, (base == 2 ? 'b' : (cap ? 'X' : 'x')));
written++;
}
}
// 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) {
bdopadding(buf, width, len, '0'); for (; !rc && width > len ; width--) {
rc = bputc(buf, '0');
written++;
}
}
// Output the actual number // Output the actual number
for (; !rc && *s ; s++) { for (; !rc && *s ; s++) {
rc = bputc(buf, (uchar)*s); rc = bputc(buf, (uchar)*s);
written++;
} }
// 'minus' padding, only with spaces // 'minus' padding, only with spaces
if (minus && !zero && width > len) if (minus && !zero && width > len) {
bdopadding(buf, width, base, ' '); for (; !rc && width > len ; width--) {
rc = bputc(buf, ' ');
written++;
}
}
// Carry on to next modifier // Carry on to next modifier
} }
// For debugging purposes // For debugging purposes
if(rc)KeStartPanic("%s",strerror(rc));
assert(!rc && "vbprintf() error"); assert(!rc && "vbprintf() error");
seterrno(rc);
return rc; if (rc)
return written - 1; // "- 1" because last bputc() must have failed
else
return written;
} }

View File

@ -22,5 +22,190 @@
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. // // along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------// //----------------------------------------------------------------------------//
#if 0
#include <lib/buf.h> #include <lib/buf.h>
//
// Builds string reading from buf according to fmt
//
size_t BScanFromBuf(Buffer_t *buf, const char *fmt, ...)
{
size_t sz;
va_list ap;
va_start(ap, fmt);
ExAcquireLock(&buf->lock);
sz = vbscanf(buf, fmt, ap);
ExReleaseLock(&buf->lock);
va_end(ap);
return sz;
}
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;
}
size_t bscanf(Buffer_t *buf, const char *fmt, ...)
{
size_t sz;
va_list ap;
va_start(ap, fmt);
sz = vbscanf(buf, fmt, ap);
va_end(ap);
return sz;
}
size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap)
{
error_t rc = EOK;
size_t readcnt = 0;
uchar ch = 0;
bool l, h;
size_t width;
bool ignore; // '*' modifier, don't write to va_list for current mod
if (!buf || !fmt) { seterrnp(EINVAL); return 0; }
if (buf->flags & (BF_EOF|BF_ERR)) { seterrno(EENDF); return 0; }
if (buf->state != BS_RDWR && buf->state != BS_WRONLY) {
seterrno(EBADF);
return 0;
}
// Progress in format string
for (*fmt && !rc) {
// Skip all kinds of whitespaces
if (isspace(*fmt)) {
another_space:
rc = bgetc(buf, &ch);
if (!rc) break;
// Put back non-whitespaces and progress
if (!isspace(ch)) {
buf->rp--;
do { fmt++; } while (isspace(*fmt));
continue;
}
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) {
// No, so put it back
buf->rp--;
break;
}
// We don't do anything with these characters
continue;
}
//
// %[*][width][modifier]type
//
// Skip the %
fmt++;
// '%%' type
if (*fmt == '%') {
rc = bgetc(buf, &ch);
if (!rc && ch != '%') {
buf->rp--;
break;
}
continue;
}
l = h = 0;
width = 0;
ignore = 0;
// Don't write to variable
if (*fmt == '*') {
ignore = 1;
fmt++;
}
// Extract width field
while (isdigit(*fmt)) {
width = 10 * width + (*fmt - '0');
fmt++;
}
assert(!(width < 0));
//
// Extract length field
//
if (*fmt == 'l') {
l = 1;
fmt++;
}
else if (*fmt == 'h') {
h = 1;
fmt++
}
//
// Types
//
// Read character(s)
if (*fmt == 'c') {
if (width == 0) width = 1;
// For >1 width, read that many characters
// then store them in successive argument pointers
for (; width; width--) {
rc = bgetc(buf, &ch);
if (!rc) break;
char *chptr = va_arg(ap, char *);
*chptr = (char)ch;
readcnt++;
}
}
// Decimal integer
else if (*fmt == 'd') {
}
}
// For debugging purposes
assert(!rc && "vbscanf() error");
seterrno(rc);
return readcnt;
}
#endif

View File

@ -35,14 +35,14 @@
// Once libbuf is supports dynamic buffers, the only changes necessary // Once libbuf is supports dynamic buffers, the only changes necessary
// will be to change the value below to and add a line to vsnprintf() // will be to change the value below to and add a line to vsnprintf()
// //
#define VSNPRINTF_MAX 512 #define VSNPRINTF_MAX 4096
// //
// Format str according to fmt using ellipsed arguments // Format str according to fmt using ellipsed arguments
// //
size_t sprintf(char *str, const char *fmt, ...) size_t sprintf(char *str, const char *fmt, ...)
{ {
int ret; size_t ret;
va_list ap; va_list ap;
assert(!"sprintf() is deprecated, used snprintf() for more safety"); assert(!"sprintf() is deprecated, used snprintf() for more safety");
@ -65,7 +65,7 @@ size_t vsprintf(char *str, const char *fmt, va_list ap)
// //
size_t snprintf(char *str, size_t n, const char *fmt, ...) size_t snprintf(char *str, size_t n, const char *fmt, ...)
{ {
int ret; size_t ret;
va_list ap; va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
@ -77,8 +77,8 @@ size_t snprintf(char *str, size_t n, const char *fmt, ...)
size_t vsnprintf(char *str, size_t n, const char *fmt, va_list ap) size_t vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
{ {
size_t ret; error_t rc;
error_t rc = EOK; size_t ret, sz;
Buffer_t *buf = NULL; Buffer_t *buf = NULL;
assert(str && fmt); assert(str && fmt);
@ -96,26 +96,16 @@ size_t vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
goto fail; goto fail;
} }
rc = vbprintf(buf, fmt, ap); sz = vbprintf(buf, fmt, 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 (rc != EOK) { if (sz == 0 && !(buf->flags & BF_EOF))
if (rc == EENDF) {
if (!(buf->flags & BF_EOF)) {
goto fail; goto fail;
}
}
else {
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)
// To be changed to memcpy()
memmove(str, (char *)buf->buf, ret); memmove(str, (char *)buf->buf, ret);
}
str[ret++] = 0; str[ret++] = 0;
assert(ret <= n); assert(ret <= n);