2019-03-24 22:51:16 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
// GNU GPL OS/K //
|
|
|
|
// //
|
|
|
|
// Desc: Buffer library //
|
|
|
|
// //
|
|
|
|
// //
|
|
|
|
// Copyright © 2018-2019 The OS/K Team //
|
|
|
|
// //
|
|
|
|
// This file is part of OS/K. //
|
|
|
|
// //
|
|
|
|
// OS/K is free software: you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or //
|
|
|
|
// any later version. //
|
|
|
|
// //
|
|
|
|
// OS/K is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
|
|
|
|
#include <kernel/buf.h>
|
|
|
|
#include <extras/malloc.h>
|
|
|
|
|
2019-03-29 14:19:29 +01:00
|
|
|
Buffer_t *BStdOut, *BStdDbg;
|
|
|
|
|
2019-03-24 22:51:16 +01:00
|
|
|
// Straightforward functions
|
|
|
|
int BGetFlags(Buffer_t *buf) { return buf->flags; }
|
|
|
|
int BGetState(Buffer_t *buf) { return buf->state; }
|
|
|
|
int BGetLineLen(Buffer_t *buf) { return buf->lineLen; }
|
|
|
|
void BSetLineLen(Buffer_t *buf, int len) { buf->lineLen = len; }
|
|
|
|
|
|
|
|
void BLockBuf(Buffer_t *buf)
|
|
|
|
{
|
|
|
|
assert(buf && buf->initDone == INITOK);
|
|
|
|
ExAcquireLock(&buf->lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BUnlockBuf(Buffer_t *buf) { ExReleaseLock(&buf->lock); }
|
|
|
|
bool BTrylockBuf(Buffer_t *buf) { return ExAttemptLock(&buf->lock); }
|
|
|
|
|
|
|
|
void BFlushOnClose(Buffer_t *buf) { buf->flags |= BF_FONCLOSE; }
|
|
|
|
|
|
|
|
//
|
|
|
|
// Closes a buffer, not flushing unless the proper flag is set
|
|
|
|
//
|
|
|
|
error_t BCloseBuf(Buffer_t *buf)
|
|
|
|
{
|
2019-03-25 17:33:51 +01:00
|
|
|
assert(buf && buf->initDone == INITOK);
|
2019-03-24 22:51:16 +01:00
|
|
|
ExAcquireLock(&buf->lock);
|
|
|
|
|
|
|
|
if (buf->flags & BF_FONCLOSE) {
|
|
|
|
BFlushBuf(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf->flags & BF_ALLOC) {
|
|
|
|
free(buf->buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->buf = buf->rp = buf->wp = NULL;
|
|
|
|
buf->initDone = buf->flags = buf->state = buf->size = 0;
|
|
|
|
|
|
|
|
ExReleaseLock(&buf->lock);
|
|
|
|
KalFreeMemory(buf);
|
|
|
|
|
|
|
|
return EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Opens a pure text buffer (for WRITING only)
|
|
|
|
// If source==NULL, malloc's the internal buffer
|
|
|
|
//
|
|
|
|
#if 0
|
|
|
|
Buffer_t *BOpenPureBuf(char *source, size_t size)
|
|
|
|
{
|
|
|
|
/*Buffer_t *buf = malloc(sizeof *buf);
|
|
|
|
buf->flags = 0;
|
|
|
|
ExInitLock(&buf->lock);*/
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// Creates a lined buffer of (nLines + pbCount*nLines) lines each
|
|
|
|
// of lineLen length (pb = playback buffer, for scrolling up)
|
|
|
|
//
|
|
|
|
Buffer_t *BOpenLineBuf(char *source, int mode,
|
|
|
|
int lineLen, int nLines, int pbCount, BFlusher_t flusher)
|
|
|
|
{
|
|
|
|
assert(lineLen > 0 && nLines > 0 && pbCount >= 0);
|
|
|
|
assert(mode == BS_RDWR || mode == BS_RDONLY || mode == BS_WRONLY);
|
|
|
|
|
2019-03-29 14:19:29 +01:00
|
|
|
//Buffer_t *buf = malloc(sizeof *buf);
|
|
|
|
static Buffer_t __buf;
|
|
|
|
Buffer_t *buf = &__buf;
|
|
|
|
|
2019-03-24 22:51:16 +01:00
|
|
|
|
|
|
|
ExInitLock(&buf->lock, KLOCK_MUTEX);
|
|
|
|
ExAcquireLock(&buf->lock);
|
|
|
|
|
|
|
|
buf->lastLF = 0;
|
|
|
|
buf->state = mode;
|
|
|
|
buf->flags = BF_LINE;
|
|
|
|
buf->nLines = nLines;
|
|
|
|
buf->lineLen = lineLen;
|
|
|
|
buf->size = lineLen * nLines * (pbCount + 1);
|
|
|
|
|
|
|
|
if (source == NULL) {
|
2019-03-25 17:33:51 +01:00
|
|
|
buf->buf = calloc(buf->size, 1);
|
2019-03-24 22:51:16 +01:00
|
|
|
} else {
|
2019-03-25 17:33:51 +01:00
|
|
|
buf->buf = (uchar *)source;
|
2019-03-24 22:51:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
buf->wp = buf->rp = buf->buf;
|
|
|
|
buf->flusher = flusher;
|
|
|
|
buf->initDone = INITOK;
|
|
|
|
|
|
|
|
ExReleaseLock(&buf->lock);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Flushes a buffer, returns EBADF when not possible
|
|
|
|
//
|
|
|
|
error_t BFlushBuf(Buffer_t *buf)
|
|
|
|
{
|
|
|
|
error_t rc;
|
|
|
|
|
|
|
|
assert(buf && buf->initDone == INITOK);
|
|
|
|
|
|
|
|
if (!buf) return EINVAL;
|
|
|
|
if (!buf->flusher || buf->state == BS_EOF || buf->state == BS_ERR) {
|
|
|
|
return EBADF;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExAcquireLock(&buf->lock);
|
|
|
|
rc = buf->flusher(buf);
|
|
|
|
ExReleaseLock(&buf->lock);
|
|
|
|
|
|
|
|
return rc;
|
2019-03-29 14:19:29 +01:00
|
|
|
}
|