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

142 lines
5.1 KiB
C
Raw Normal View History

2019-03-25 17:33:51 +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>
error_t bputc(Buffer_t *buf, uchar ch);
//
// Writes a character on a buffer
//
error_t BPutOnBuf(Buffer_t *buf, uchar ch)
{
error_t rc;
assert(buf && buf->initDone == INITOK);
if (!buf) return EINVAL;
if (buf->state != BS_RDWR && buf->state != BS_WRONLY) {
return EBADF;
}
ExAcquireLock(&buf->lock);
rc = bputc(buf, ch);
ExReleaseLock(&buf->lock);
return rc;
}
//
// Internal, unlocked version of BPutOnBuf
//
error_t bputc(Buffer_t *buf, uchar ch)
{
error_t rc;
size_t bufSize, pbCount;
// Implements playback / scrolling up when line buffering
// Note that '\n', '\r' and '\t' can never occur in the buffer
// That should change; there should be a flag for it
if (buf->flags & BF_LINE) {
// Deal with carriage returns
if (ch == '\r') {
buf->wp -= buf->lastLF;
buf->lastLF = 0;
assert(buf->wp >= buf->buf);
}
// Deal with tabs... we need a tabSize field
// We assume tabs are 4 characters long for now
else if (ch == '\t') {
rc = bputc(buf, ' ');
if (rc > 0) return rc;
while (buf->lastLF % 4 > 0) {
rc = bputc(buf, ' ');
if (rc > 0) return rc;
}
}
// Deal with line feeds by filling the rest of the line with spaces
// We need a field for choosing a different filler, e.g. '\0'
else if (ch == '\n') {
assert(buf->lastLF < buf->lineLen);
while (buf->lastLF > 0) {
rc = bputc(buf, ' ');
if (rc > 0) return rc;
}
}
// Just a regular character
else {
// Do we have to scroll up?
if (buf->wp == buf->buf + buf->size) {
// Yes, so we give up a whole playback buffer worth
// of lines so we don't have to do this too often
// (basically we make the current buffer a pb one)
bufSize = buf->nLines * buf->lineLen;
pbCount = (buf->size / bufSize) - 1;
// Paranoia check
assert(buf->size >= (size_t)(buf->nLines * buf->lineLen));
// If we only have one playback buffer we just give up a line
if (pbCount == 0) {
// Use of memcpy() is safe because the source occur
// after the destination
memcpy(buf->buf, buf->buf + buf->lineLen,
buf->size - buf->lineLen);
buf->wp -= buf->lineLen;
}
// We do have a playback buffer worth of lines to give up
else {
memcpy(buf->buf, buf->buf + bufSize,
buf->size - bufSize);
buf->wp -= buf->lineLen;
}
}
// Write the damn thing
*buf->wp++ = ch;
// Did we reach the end of line?
if (++buf->lastLF == buf->lineLen) {
buf->lastLF = 0;
}
}
return EOK;
}
// No scrolling up when not line-buffering
else {
if (buf->wp == buf->buf + buf->size) {
buf->state = BS_EOF;
}
return ENOSYS;
}
}