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/>. //
|
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
|
2019-05-13 23:22:27 +02:00
|
|
|
#include <lib/buf.h>
|
2019-05-19 01:33:16 +02:00
|
|
|
#include <io/spkr.h>
|
2019-03-25 17:33:51 +01:00
|
|
|
|
2020-02-02 13:37:26 +01:00
|
|
|
#include <io/keyb.h>
|
|
|
|
|
2019-03-25 17:33:51 +01:00
|
|
|
//
|
|
|
|
// Writes a character on a buffer
|
|
|
|
//
|
|
|
|
error_t BPutOnBuf(Buffer_t *buf, uchar ch)
|
|
|
|
{
|
|
|
|
error_t rc;
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2019-03-29 14:19:29 +01:00
|
|
|
error_t rc = EOK;
|
2019-03-25 17:33:51 +01:00
|
|
|
|
2019-05-07 22:32:17 +02:00
|
|
|
if (buf->flags & (BF_EOF|BF_ERR)) return EENDF;
|
|
|
|
|
|
|
|
// Implements playback / scrolling up when terminal buffering
|
2019-03-25 17:33:51 +01:00
|
|
|
// Note that '\n', '\r' and '\t' can never occur in the buffer
|
|
|
|
// That should change; there should be a flag for it
|
2019-05-07 22:32:17 +02:00
|
|
|
if (buf->flags & BF_TERM) {
|
2019-03-25 17:33:51 +01:00
|
|
|
|
2019-04-01 11:21:04 +02:00
|
|
|
// 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'
|
|
|
|
if (ch == '\n') {
|
|
|
|
assert(buf->lastLF < buf->lineLen);
|
|
|
|
|
|
|
|
int off = buf->lineLen - buf->lastLF;
|
|
|
|
while (off > 0) {
|
|
|
|
*buf->wp++ = ' ';
|
|
|
|
off--;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf->lastLF = 0;
|
2019-04-23 11:32:45 +02:00
|
|
|
|
|
|
|
// Auto-scrolling
|
|
|
|
if (buf->wp == buf->buf + buf->size && buf->flags&BF_AUTOSCROLL) {
|
|
|
|
bscrolldown(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buf->flusher) buf->flusher(buf);
|
2019-04-01 11:21:04 +02:00
|
|
|
}
|
|
|
|
|
2019-03-25 17:33:51 +01:00
|
|
|
// Deal with carriage returns
|
2019-04-01 11:21:04 +02:00
|
|
|
else if (ch == '\r') {
|
2019-03-25 17:33:51 +01:00
|
|
|
buf->wp -= buf->lastLF;
|
|
|
|
buf->lastLF = 0;
|
|
|
|
assert(buf->wp >= buf->buf);
|
|
|
|
}
|
|
|
|
|
2020-02-02 13:37:26 +01:00
|
|
|
// Deal with tabs... should be dealt with elsewhere tho
|
2019-03-25 17:33:51 +01:00
|
|
|
// We assume tabs are 4 characters long for now
|
|
|
|
else if (ch == '\t') {
|
|
|
|
rc = bputc(buf, ' ');
|
|
|
|
|
|
|
|
while (buf->lastLF % 4 > 0) {
|
|
|
|
rc = bputc(buf, ' ');
|
|
|
|
}
|
2019-05-07 22:32:17 +02:00
|
|
|
}
|
2019-03-25 17:33:51 +01:00
|
|
|
|
|
|
|
// Just a regular character
|
|
|
|
else {
|
|
|
|
if (buf->wp == buf->buf + buf->size) {
|
2019-04-23 11:32:45 +02:00
|
|
|
buf->flags |= BF_EOF;
|
|
|
|
return EENDF;
|
2019-03-25 17:33:51 +01:00
|
|
|
}
|
|
|
|
|
2019-03-29 14:19:29 +01:00
|
|
|
// Write it
|
2019-03-25 17:33:51 +01:00
|
|
|
*buf->wp++ = ch;
|
|
|
|
|
|
|
|
// Did we reach the end of line?
|
|
|
|
if (++buf->lastLF == buf->lineLen) {
|
|
|
|
buf->lastLF = 0;
|
|
|
|
}
|
|
|
|
|
2019-04-23 11:32:45 +02:00
|
|
|
// Auto-scrolling
|
|
|
|
if (buf->wp == buf->buf + buf->size && buf->flags&BF_AUTOSCROLL) {
|
|
|
|
bscrolldown(buf);
|
|
|
|
}
|
|
|
|
}
|
2019-03-25 17:33:51 +01:00
|
|
|
}
|
|
|
|
|
2019-05-07 22:32:17 +02:00
|
|
|
// Not a terminal buffer
|
2019-03-25 17:33:51 +01:00
|
|
|
else {
|
|
|
|
if (buf->wp == buf->buf + buf->size) {
|
2019-04-23 11:32:45 +02:00
|
|
|
buf->flags |= BF_EOF;
|
|
|
|
return EENDF;
|
2019-03-25 17:33:51 +01:00
|
|
|
}
|
|
|
|
|
2019-04-23 11:32:45 +02:00
|
|
|
*buf->wp++ = ch;
|
|
|
|
|
2019-05-07 22:32:17 +02:00
|
|
|
if (buf->wp == buf->buf + buf->size) {
|
|
|
|
if (!(buf->flags & (BF_LINE|BF_FONCLOSE))) {
|
|
|
|
if (buf->flusher) buf->flusher(buf);
|
|
|
|
}
|
2019-04-23 11:32:45 +02:00
|
|
|
}
|
2019-05-07 22:32:17 +02:00
|
|
|
|
|
|
|
// Line-buffering
|
|
|
|
if (ch == '\n' && buf->flags & BF_LINE) {
|
|
|
|
if (buf->flusher) buf->flusher(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rc != EOK) {
|
|
|
|
// buf->flags |= BF_ERR;
|
2019-03-25 17:33:51 +01:00
|
|
|
}
|
2019-04-23 11:32:45 +02:00
|
|
|
|
2019-05-07 22:32:17 +02:00
|
|
|
return rc;
|
2019-03-25 17:33:51 +01:00
|
|
|
}
|
|
|
|
|