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/libbuf/bopen.c

124 lines
4.0 KiB
C
Raw Normal View History

2019-03-24 22:51:16 +01:00
//----------------------------------------------------------------------------//
2020-09-27 17:33:48 +02:00
// OS on Kaleid //
2019-03-24 22:51:16 +01:00
// //
// Desc: Buffer library //
// //
// //
// Copyright © 2018-2020 The OS/K Team //
2019-03-24 22:51:16 +01:00
// //
// 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/>. //
//----------------------------------------------------------------------------//
2020-02-19 22:19:58 +01:00
#include <libbuf.h>
2019-05-13 23:22:27 +02:00
#include <ex/malloc.h>
2019-03-24 22:51:16 +01:00
2019-05-07 22:32:17 +02:00
Buffer_t *BStdIn, *BStdOut, *BStdDbg;
2019-03-24 22:51:16 +01:00
//
2019-04-23 11:32:45 +02:00
// Creates an actual buffer
2019-03-24 22:51:16 +01:00
//
2019-04-23 11:32:45 +02:00
error_t BOpenPureBuf(Buffer_t **pbuf, int mode, size_t size)
2019-03-24 22:51:16 +01:00
{
2019-05-07 23:16:56 +02:00
if (*pbuf != NULL) {
assert(0);
return EINVAL;
}
2019-04-23 11:32:45 +02:00
return BOpenPureBufEx(pbuf, NULL, mode, size, NULL);
2019-03-24 22:51:16 +01:00
}
2019-04-23 11:32:45 +02:00
error_t BOpenPureBufEx(Buffer_t **pbuf, char *source, int mode, size_t size,
BFlusher_t flusher)
2019-03-24 22:51:16 +01:00
{
2019-04-23 11:32:45 +02:00
Buffer_t *buf;
2019-03-24 22:51:16 +01:00
2019-04-23 11:32:45 +02:00
assert(mode == BS_RDWR || mode == BS_RDONLY || mode == BS_WRONLY);
2019-04-23 11:32:45 +02:00
if (!pbuf) return EINVAL;
if (!*pbuf) {
buf = malloc(sizeof *buf);
if (!buf) return ENOMEM;
buf->flags = BF_BALLOC;
2019-04-23 11:32:45 +02:00
}
else {
buf = *pbuf;
buf->flags = 0;
}
2019-03-24 22:51:16 +01:00
2019-05-13 23:22:27 +02:00
ExInitLock(&buf->lock);
2019-03-24 22:51:16 +01:00
ExAcquireLock(&buf->lock);
2019-04-23 11:32:45 +02:00
buf->size = size;
2019-03-24 22:51:16 +01:00
buf->state = mode;
//buf->buf = NULL;
2019-03-24 22:51:16 +01:00
if (source == NULL) {
2019-04-06 07:53:58 +02:00
KalAllocMemoryEx((void **)&buf->buf, buf->size, M_ZEROED, 0);
2019-04-23 11:32:45 +02:00
buf->flags |= BF_SALLOC;
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;
ExReleaseLock(&buf->lock);
2019-04-23 11:32:45 +02:00
// Second allocation failed
if (buf->buf == NULL) {
BCloseBuf(buf);
return ENOMEM;
}
*pbuf = buf;
return EOK;
}
//
2019-05-07 22:32:17 +02:00
// Creates a terminal buffer of (nLines + pbCount*nLines) lines each
2019-04-23 11:32:45 +02:00
// of lineLen length (pb = playback buffer, for scrolling up)
//
2019-05-07 22:32:17 +02:00
error_t BOpenTermBuf(Buffer_t **pbuf, int mode,
2019-04-23 11:32:45 +02:00
int lineLen, int nLines, int pbCount)
{
2019-05-07 23:16:56 +02:00
if (*pbuf != NULL) {
assert(0);
return EINVAL;
}
2019-05-07 22:32:17 +02:00
return BOpenTermBufEx(pbuf, NULL, mode, lineLen, nLines, pbCount, NULL);
2019-04-23 11:32:45 +02:00
}
2019-05-07 22:32:17 +02:00
error_t BOpenTermBufEx(Buffer_t **pbuf, char *source, int mode,
2019-04-23 11:32:45 +02:00
int lineLen, int nLines, int pbCount, BFlusher_t flusher)
{
error_t rc;
size_t size = lineLen * nLines * (pbCount + 1);
assert(lineLen > 0 && nLines > 0 && pbCount >= 0);
rc = BOpenPureBufEx(pbuf, source, mode, size, flusher);
if (!rc) {
(*pbuf)->lastLF = 0;
(*pbuf)->nLines = nLines;
(*pbuf)->lineLen = lineLen;
2019-05-07 22:32:17 +02:00
(*pbuf)->flags |= BF_TERM;
2019-04-23 11:32:45 +02:00
}
return rc;
2019-03-24 22:51:16 +01:00
}