From 0af726ba091be6024ccd4a7f03fa08e7d42acc59 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 5 Feb 2020 22:03:23 +0100 Subject: [PATCH 01/10] Bug with makefile that can erase host MBR --- build/install-os-k.sh | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/build/install-os-k.sh b/build/install-os-k.sh index 3522eac..3c08f2a 100755 --- a/build/install-os-k.sh +++ b/build/install-os-k.sh @@ -30,15 +30,33 @@ NC='\033[1;37m' set -e #exit if error -echo ${CL2}[install-os-k.sh]${NC} Mount image... \(udisksctl\)${CL3} -udisksctl loop-setup --offset 1048576 --no-user-interaction --file $1 -lpblock=$(/sbin/losetup -j $1 | cut -d ':' -f 1) +## Mount +echo ${CL2}[install-os-k.sh]${NC} Mount image... \(losetup\)${CL3} +lpblock=$(sudo losetup -f) +sudo losetup $lpblock $1 -o 1048576 +if [ -z $lpblock ]; +then + echo No loop device partition to target. Fatal error ! + return 13 +fi + +sudo udisksctl mount -b $lpblock --no-user-interaction + +## Sync echo ${CL2}[install-os-k.sh]${NC} Sync image... \(sync\)${CL3} sync +# Get the mountpoint mountpoint=$(udisksctl info -b $lpblock | grep MountPoint | cut -d ':' -f 2 | sed 's/ //g') +if [ -z $mountpoint ]; +then + echo Impossible to mount the image $mountpoint. Fatal error ! + return 13 +fi + + echo ${CL2}[install-os-k.sh]${NC} Copying grub.cfg cp $2 $mountpoint/boot/grub/grub.cfg echo ${CL2}[install-os-k.sh]${NC} Copying kernel From 04fbcf9a02457e545dd544cc33b67f53b9c95be4 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 5 Feb 2020 22:23:58 +0100 Subject: [PATCH 02/10] Bug with makefile that can erase host MBR --- Makefile | 2 +- build/create_disk.sh | 2 +- build/install-os-k.sh | 26 ++++++++++---------------- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 8d307c9..6a19b98 100644 --- a/Makefile +++ b/Makefile @@ -328,7 +328,7 @@ dddnokvm: all installonimage ## HD IMAGE RELATED ---------------------------------------------------------- # installonimage: $(installdisk) $(MBRDIR)/grub.cfg - @echo ${CL2}[$@] ${NC}Installing MBR on image...${CL3} + @echo ${CL2}[$@] ${NC}Installing boot configuration on image...${CL3} @$(BUILDDIR)/install-os-k.sh $(installdisk) $(MBRDIR)/grub.cfg $(BINDIR)/kaleid @echo ${CL2}[$@] ${CL}Success.${CL3} diff --git a/build/create_disk.sh b/build/create_disk.sh index 018b406..91f499c 100755 --- a/build/create_disk.sh +++ b/build/create_disk.sh @@ -76,7 +76,7 @@ fi if [ -z $lpblock ]; then - echo No loop device partition to target. Fatal error ! + Impossible to find an available loop device. Fatal error ! return 13 fi diff --git a/build/install-os-k.sh b/build/install-os-k.sh index 3c08f2a..e96a211 100755 --- a/build/install-os-k.sh +++ b/build/install-os-k.sh @@ -30,33 +30,27 @@ NC='\033[1;37m' set -e #exit if error -## Mount -echo ${CL2}[install-os-k.sh]${NC} Mount image... \(losetup\)${CL3} -lpblock=$(sudo losetup -f) -sudo losetup $lpblock $1 -o 1048576 +echo ${CL2}[install-os-k.sh]${NC} Mount image... \(udisksctl\)${CL3} +udisksctl loop-setup --offset 1048576 --no-user-interaction --file $1 +lpblock=$(/sbin/losetup -j $1 | cut -d ':' -f 1) -if [ -z $lpblock ]; -then - echo No loop device partition to target. Fatal error ! - return 13 -fi - -sudo udisksctl mount -b $lpblock --no-user-interaction - -## Sync echo ${CL2}[install-os-k.sh]${NC} Sync image... \(sync\)${CL3} sync -# Get the mountpoint +if [ -z $lpblock ]; +then + echo Impossible to find an available loop device. Fatal error ! + return 13 +fi + mountpoint=$(udisksctl info -b $lpblock | grep MountPoint | cut -d ':' -f 2 | sed 's/ //g') if [ -z $mountpoint ]; then - echo Impossible to mount the image $mountpoint. Fatal error ! + echo Impossible to mount the image at $lpblock. Fatal error ! return 13 fi - echo ${CL2}[install-os-k.sh]${NC} Copying grub.cfg cp $2 $mountpoint/boot/grub/grub.cfg echo ${CL2}[install-os-k.sh]${NC} Copying kernel From cb4d2cc8de2164a2d48e2a80d62af1f557a46eea Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 5 Feb 2020 22:33:30 +0100 Subject: [PATCH 03/10] Bug with makefile that can erase host MBR (3) --- build/install-os-k.sh | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/build/install-os-k.sh b/build/install-os-k.sh index e96a211..cfe6b19 100755 --- a/build/install-os-k.sh +++ b/build/install-os-k.sh @@ -31,8 +31,7 @@ NC='\033[1;37m' set -e #exit if error echo ${CL2}[install-os-k.sh]${NC} Mount image... \(udisksctl\)${CL3} -udisksctl loop-setup --offset 1048576 --no-user-interaction --file $1 -lpblock=$(/sbin/losetup -j $1 | cut -d ':' -f 1) +lpblock=$(udisksctl loop-setup --offset 1048576 --no-user-interaction --file $1 | cut -d ' ' -f 5 | sed 's/\.//g') echo ${CL2}[install-os-k.sh]${NC} Sync image... \(sync\)${CL3} sync @@ -51,9 +50,9 @@ then return 13 fi -echo ${CL2}[install-os-k.sh]${NC} Copying grub.cfg +echo ${CL2}[install-os-k.sh]${NC} Copying grub.cfg to $lpblock as $mountpoint cp $2 $mountpoint/boot/grub/grub.cfg -echo ${CL2}[install-os-k.sh]${NC} Copying kernel +echo ${CL2}[install-os-k.sh]${NC} Copying kernel to $lpblock as $mountpoint cp $3 $mountpoint/boot/kaleid echo ${CL2}[install-os-k.sh]${NC} Sync image... \(sync\)${CL3} From 7797b6b1fe391ed7ff52d7513ab4552b26bfc063 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 5 Feb 2020 22:38:53 +0100 Subject: [PATCH 04/10] Bug with makefile that can erase host MBR (4) --- build/create_disk.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/create_disk.sh b/build/create_disk.sh index 91f499c..f2af324 100755 --- a/build/create_disk.sh +++ b/build/create_disk.sh @@ -76,7 +76,7 @@ fi if [ -z $lpblock ]; then - Impossible to find an available loop device. Fatal error ! + echo Impossible to find an available loop device. Fatal error ! return 13 fi From 343290407ede1d9928b6957438edc4839c84b35a Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 5 Feb 2020 22:58:31 +0100 Subject: [PATCH 05/10] Bug with makefile that can erase host MBR (5) --- build/install-os-k.sh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/build/install-os-k.sh b/build/install-os-k.sh index cfe6b19..e4e440e 100755 --- a/build/install-os-k.sh +++ b/build/install-os-k.sh @@ -44,6 +44,11 @@ fi mountpoint=$(udisksctl info -b $lpblock | grep MountPoint | cut -d ':' -f 2 | sed 's/ //g') +if [ -z $mountpoint ]; +then + mountpoint=$(udisksctl mount -b $lpblock | cut -d ' ' -f 2 | sed 's/\.//g') +fi + if [ -z $mountpoint ]; then echo Impossible to mount the image at $lpblock. Fatal error ! From 960a3c97c8c60b99c1a0ba275b2a8eec23185439 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 5 Feb 2020 23:05:41 +0100 Subject: [PATCH 06/10] Bug with makefile that can erase host MBR (6) --- build/install-os-k.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/install-os-k.sh b/build/install-os-k.sh index e4e440e..92cf6ad 100755 --- a/build/install-os-k.sh +++ b/build/install-os-k.sh @@ -46,7 +46,7 @@ mountpoint=$(udisksctl info -b $lpblock | grep MountPoint | cut -d ':' -f 2 | se if [ -z $mountpoint ]; then - mountpoint=$(udisksctl mount -b $lpblock | cut -d ' ' -f 2 | sed 's/\.//g') + mountpoint=$(udisksctl mount -b $lpblock | cut -d ' ' -f 4 | sed 's/\.//g') fi if [ -z $mountpoint ]; From 66bb849b8cd9446908abcfc7ab37bac1ab07129a Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 5 Feb 2020 23:17:13 +0100 Subject: [PATCH 07/10] Bug with makefile that can erase host MBR (6) --- build/install-os-k.sh | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/build/install-os-k.sh b/build/install-os-k.sh index 92cf6ad..6705e69 100755 --- a/build/install-os-k.sh +++ b/build/install-os-k.sh @@ -64,4 +64,12 @@ echo ${CL2}[install-os-k.sh]${NC} Sync image... \(sync\)${CL3} sync echo ${CL2}[install-os-k.sh]${NC} Unmount image... \(udisksctl\)${CL3} -udisksctl unmount --no-user-interaction -b $lpblock \ No newline at end of file +udisksctl unmount --no-user-interaction -b $lpblock + +lpblock=$(/sbin/losetup -j $1 | cut -d ':' -f 1) +if [ -z $lpblock ]; +then + return 0 +fi + +udisksctl loop-delete -b $lpblock From ddbf8ac07e78992ba25c3674ba1f9bfe1d2507ed Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Thu, 6 Feb 2020 10:11:32 +0100 Subject: [PATCH 08/10] new printf & proto-scanf --- include/lib/buf.h | 30 +++--- include/libc.h | 6 +- kaleid/kernel/ke/panic.c | 1 - kaleid/libbuf/bgetc.c | 6 ++ kaleid/libbuf/bprint.c | 152 ++++++++++++++-------------- kaleid/libbuf/bscan.c | 208 ++++++++++++++++++++++++++++++++++++++- kaleid/libc/sprintf.c | 18 ++-- 7 files changed, 318 insertions(+), 103 deletions(-) diff --git a/include/lib/buf.h b/include/lib/buf.h index 07311dc..72536b2 100644 --- a/include/lib/buf.h +++ b/include/lib/buf.h @@ -138,14 +138,18 @@ int BGetState(Buffer_t *); error_t BFlushBuf(Buffer_t *); 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 BReadBuf(Buffer_t *, uchar *, size_t); -error_t BScanBuf(Buffer_t *, const char *, ...); -error_t BScanBufV(Buffer_t *, const char *, va_list); + +// The following functions return the number of bytes written to the buffer +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 BUnlockBuf(Buffer_t *); @@ -157,14 +161,14 @@ bool BTrylockBuf(Buffer_t *); // error_t bputc(Buffer_t *, uchar); -error_t bwrite(Buffer_t *, uchar *, size_t); -error_t bprintf(Buffer_t *, const char *, ...); -error_t vbprintf(Buffer_t *, const char *, va_list); +size_t bwrite(Buffer_t *, uchar *, size_t); +size_t bprintf(Buffer_t *, const char *, ...); +size_t vbprintf(Buffer_t *, const char *, va_list); error_t bgetc(Buffer_t *, uchar *); -error_t bread(Buffer_t *, uchar *, size_t); -error_t bscanf(Buffer_t *, const char *, ...); -error_t vbscanf(Buffer_t *, const char *, va_list); +size_t bread(Buffer_t *, uchar *, size_t); +size_t bscanf(Buffer_t *, size_t *, const char *, ...); +size_t vbscanf(Buffer_t *, size_t *, const char *, va_list); error_t bemptybuf(Buffer_t *); error_t bscrolldown(Buffer_t *); diff --git a/include/libc.h b/include/libc.h index 21a52e2..f35ae16 100644 --- a/include/libc.h +++ b/include/libc.h @@ -146,8 +146,12 @@ char *strrev(char *restrict, const char *restrict); char *strrev2(char *); size_t sprintf(char *, const char *, ...); -size_t snprintf(char *, size_t, const char *, ...); size_t vsprintf(char *, const char *, va_list); + +// These return the number of bytes **written** +// not the number of bytes which would have been +// written had the buffer been large enough +size_t snprintf(char *, size_t, const char *, ...); size_t vsnprintf(char *, size_t, const char *, va_list); //----------------------------------------------------------------------------// diff --git a/kaleid/kernel/ke/panic.c b/kaleid/kernel/ke/panic.c index 8f3bf8d..0b95652 100644 --- a/kaleid/kernel/ke/panic.c +++ b/kaleid/kernel/ke/panic.c @@ -26,7 +26,6 @@ #include #include -error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap); extern bool KeIdtIsInitialized; // diff --git a/kaleid/libbuf/bgetc.c b/kaleid/libbuf/bgetc.c index f0f6d0f..ef53d4c 100644 --- a/kaleid/libbuf/bgetc.c +++ b/kaleid/libbuf/bgetc.c @@ -43,6 +43,9 @@ error_t BGetFromBuf(Buffer_t *buf, uchar *ch) ExReleaseLock(&buf->lock); 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 rc = EOK; @@ -70,6 +73,9 @@ error_t bgetc(Buffer_t *buf, uchar *ch) while (buf->rp >= buf->wp) { #ifdef _KALEID_KERNEL KeSleep(1); // XXX synchronization with keyboard driver +#else + buf->flags |= BF_EOF; + return EENDF; // Currently empty #endif } diff --git a/kaleid/libbuf/bprint.c b/kaleid/libbuf/bprint.c index 48dbdeb..c407cf4 100644 --- a/kaleid/libbuf/bprint.c +++ b/kaleid/libbuf/bprint.c @@ -31,73 +31,41 @@ // // 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_start(ap, fmt); ExAcquireLock(&buf->lock); - rc = vbprintf(buf, fmt, ap); + sz = vbprintf(buf, fmt, ap); ExReleaseLock(&buf->lock); 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); - rc = vbprintf(buf, fmt, ap); + sz = vbprintf(buf, fmt, ap); 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; + size_t sz; va_list ap; va_start(ap, fmt); - rc = vbprintf(buf, fmt, ap); + sz = vbprintf(buf, fmt, ap); va_end(ap); - return rc; -} - -// -// 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; + return sz; } #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 // 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; + size_t written = 0; ssize_t width, prec, len; char type; @@ -134,10 +103,11 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap) // Base int base; - if (!buf || !fmt) return EINVAL; - if (buf->flags & (BF_EOF|BF_ERR)) return EENDF; + if (!buf || !fmt) { seterrno(EINVAL); return 0; } + if (buf->flags & (BF_EOF|BF_ERR)) { seterrno(EENDF); return 0; } if (buf->state != BS_RDWR && buf->state != BS_WRONLY) { - return EBADF; + seterrno(EBADF); + return 0; } // Progress in format string @@ -146,14 +116,14 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap) // Deal with all non-'%' characters if (*fmt != '%') { rc = bputc(buf, *fmt); - + written++; fmt++; + continue; } // - // %[parameter][flags][width|*][.precision|*][length]type - // We aren't dealing with parameters and floating stuff just yet + // %[flags][width|*][.precision|*][length]type // // Skip the '%' @@ -162,8 +132,9 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap) // "%%" modifier if (*fmt == '%') { rc = bputc(buf, '%'); - + written++; fmt++; + continue; } @@ -253,6 +224,7 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap) if (type == 'c') { uch = (uchar)va_arg(ap, int); rc = bputc(buf, uch); + written++; continue; } @@ -261,8 +233,10 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap) if (type == 'C') { base = va_arg(ap, int); - if (!(base < 0 || base > VGA_COLOR_WHITE)) + if (!(base < 0 || base > VGA_COLOR_WHITE)) { rc = bputc(buf, RtlColorToChar(base)); + written++; + } assert(!rc && 1); @@ -281,7 +255,7 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap) // Zero/nonspecified precision means unlimited amount if (prec == 0) prec = INT_MAX; - for (; *s && prec-- ; s++) { + for (; !rc && *s && prec-- ; s++) { rc = bputc(buf, (uchar)*s); } @@ -292,6 +266,7 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap) // Make sure width and prec aren't too big // (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; break; } @@ -313,16 +288,16 @@ error_t vbprintf(Buffer_t *buf, const char *fmt, va_list ap) // End of string too soon else if (type == '\0') { - bputc(buf, '%'); rc = EINVAL; + written++; // fool the line "if (rc) return written-1;" break; } // Unknown/unsupported modifier else { - bputc(buf, '%'); - bputc(buf, '?'); - rc = bputc(buf, type); + rc = bputc(buf, '%'); + written++; + continue; } @@ -382,36 +357,63 @@ error_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) - bdopadding(buf, width, len, ' '); + if (!minus && !zero && width > len) { + for (; !rc && width > len ; width--) { + rc = bputc(buf, ' '); + written++; + } + } // Deal with signs and the hash flag - if (*s == '-') { rc = bputc(buf, '-'); s++, len--; } - else if (sgn && plus) rc = bputc(buf, '+'); - else if (sgn && space) rc = bputc(buf, ' '); - else if (hash) bprinthash(buf, base, cap); + if (*s == '-') { rc = bputc(buf, '-'); s++, len--; written++; } + else if (sgn && plus) { rc = bputc(buf, '+'); written++; } + 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)) { + rc = bputc(buf, '0'); + written++; + + if (!rc && base != 8) { + rc = bputc(buf, (base == 2 ? 'b' : (cap ? 'X' : 'x'))); + written++; + } + } // Deal with padding by zeroes // The 'minus' flag makes no sense with the 'zero' one - if (zero && width > len) - bdopadding(buf, width, len, '0'); + if (zero && width > len) { + for (; !rc && width > len ; width--) { + rc = bputc(buf, '0'); + written++; + } + } // Output the actual number for (; !rc && *s ; s++) { rc = bputc(buf, (uchar)*s); + written++; } // 'minus' padding, only with spaces - if (minus && !zero && width > len) - bdopadding(buf, width, base, ' '); - + if (minus && !zero && width > len) { + for (; !rc && width > len ; width--) { + rc = bputc(buf, ' '); + written++; + } + } + // Carry on to next modifier - } - - // For debugging purposes - if(rc)KeStartPanic("%s",strerror(rc)); - assert(!rc && "vbprintf() error"); + } - return rc; + // For debugging purposes + assert(!rc && "vbprintf() error"); + seterrno(rc); + + if (rc) + return written - 1; // "- 1" because last bputc() must have failed + + else + return written; } diff --git a/kaleid/libbuf/bscan.c b/kaleid/libbuf/bscan.c index e6df43c..cb31ee8 100644 --- a/kaleid/libbuf/bscan.c +++ b/kaleid/libbuf/bscan.c @@ -21,6 +21,212 @@ // You should have received a copy of the GNU General Public License // // along with OS/K. If not, see . // //----------------------------------------------------------------------------// - +#if 0 #include + +// +// 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 + + + + + + + + + + + + + + + + + + + + + + + diff --git a/kaleid/libc/sprintf.c b/kaleid/libc/sprintf.c index 3e0f306..bbee9b0 100644 --- a/kaleid/libc/sprintf.c +++ b/kaleid/libc/sprintf.c @@ -35,7 +35,7 @@ // Once libbuf is supports dynamic buffers, the only changes necessary // 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 @@ -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 ret; - error_t rc = EOK; + error_t rc; + size_t ret, sz; Buffer_t *buf = NULL; assert(str && fmt); @@ -96,16 +96,11 @@ size_t vsnprintf(char *str, size_t n, const char *fmt, va_list ap) 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 - if (rc != EOK) { - if (rc == EENDF) { - if (!(buf->flags & BF_EOF)) { - goto fail; - } - } - else { + if (sz == 0) { + if (!(buf->flags & BF_EOF)) { goto fail; } } @@ -113,7 +108,6 @@ size_t vsnprintf(char *str, size_t n, const char *fmt, va_list ap) ret = (size_t)buf->wp - (size_t)buf->buf; if (ret > 0) { - // To be changed to memcpy() memmove(str, (char *)buf->buf, ret); } From be5ec0fcf5ca13086a802cda286c56bf6486e4ba Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Thu, 6 Feb 2020 10:26:44 +0100 Subject: [PATCH 09/10] scanf (proto) --- include/lib/buf.h | 4 ++-- kaleid/libbuf/bscan.c | 26 +++++++++++--------------- 2 files changed, 13 insertions(+), 17 deletions(-) diff --git a/include/lib/buf.h b/include/lib/buf.h index 72536b2..8255d2b 100644 --- a/include/lib/buf.h +++ b/include/lib/buf.h @@ -167,8 +167,8 @@ size_t vbprintf(Buffer_t *, const char *, va_list); error_t bgetc(Buffer_t *, uchar *); size_t bread(Buffer_t *, uchar *, size_t); -size_t bscanf(Buffer_t *, size_t *, const char *, ...); -size_t vbscanf(Buffer_t *, size_t *, const char *, va_list); +size_t bscanf(Buffer_t *, const char *, ...); +size_t vbscanf(Buffer_t *, const char *, va_list); error_t bemptybuf(Buffer_t *); error_t bscrolldown(Buffer_t *); diff --git a/kaleid/libbuf/bscan.c b/kaleid/libbuf/bscan.c index cb31ee8..f7b5c25 100644 --- a/kaleid/libbuf/bscan.c +++ b/kaleid/libbuf/bscan.c @@ -21,7 +21,7 @@ // You should have received a copy of the GNU General Public License // // along with OS/K. If not, see . // //----------------------------------------------------------------------------// -#if 0 + #include // @@ -43,7 +43,7 @@ size_t BScanFromBuf(Buffer_t *buf, const char *fmt, ...) size_t BScanFromBufV(Buffer_t *buf, const char *fmt, va_list ap) { - size_t rc; + size_t sz; ExAcquireLock(&buf->lock); sz = vbscanf(buf, fmt, ap); @@ -70,13 +70,14 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap) size_t readcnt = 0; uchar ch = 0; + char *chptr; 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 || !fmt) { seterrno(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); @@ -84,7 +85,7 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap) } // Progress in format string - for (*fmt && !rc) { + while (*fmt && !rc) { // Skip all kinds of whitespaces if (isspace(*fmt)) { @@ -154,8 +155,6 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap) fmt++; } - assert(!(width < 0)); - // // Extract length field // @@ -167,7 +166,7 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap) else if (*fmt == 'h') { h = 1; - fmt++ + fmt++; } // @@ -183,16 +182,17 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap) for (; width; width--) { rc = bgetc(buf, &ch); if (!rc) break; + if (ignore) continue; - char *chptr = va_arg(ap, char *); - *chptr = (char)ch; - readcnt++; + chptr = va_arg(ap, char *); + *chptr = (char)ch; + readcnt++; } } // Decimal integer else if (*fmt == 'd') { - + ; } } @@ -205,10 +205,6 @@ size_t vbscanf(Buffer_t *buf, const char *fmt, va_list ap) -#endif - - - From a57a8ba1920b32c18811a1f8b8d7db98d6911599 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Thu, 6 Feb 2020 13:15:15 +0100 Subject: [PATCH 10/10] sched.h --- include/ke/sched.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/ke/sched.h b/include/ke/sched.h index 9005de2..32856e4 100644 --- a/include/ke/sched.h +++ b/include/ke/sched.h @@ -61,8 +61,8 @@ extern const char *PsPrioClassesNames[]; // Re-scheduling and preemption // XXX atomic operations // -#define PsRequestReSched() (++KeCurCPU->needReSched) -#define PsDisablePreemption() (++KeCurCPU->preemptCount) +#define PsRequestReSched() do { ++KeCurCPU->needReSched; } while (0) +#define PsDisablePreemption() do { ++KeCurCPU->preemptCount; } while (0) #define PsEnablePreemption() do { assert(KeCurCPU->preemptCount > 0); \ --KeCurCPU->preemptCount; } while(0)