kf: linux console for I/O

This commit is contained in:
Kyle Isom 2018-02-21 12:25:03 -08:00
parent 708e812049
commit 0d2758a994
8 changed files with 138 additions and 3 deletions

4
.gitignore vendored
View File

@ -36,7 +36,9 @@ ltmain.sh
m4/
missing
ch??ex??
# build artifacts
ods/src/ch??ex??
*_test
*.la
*_bench
misc/kf/kf

View File

@ -5,5 +5,5 @@ compilers:
targets:
cc:
kf:
- io.cc
- interface.cc
- kf.cc

3
misc/kf/interface.cc Normal file
View File

@ -0,0 +1,3 @@
#ifdef __linux__
#include "io-linux.cc"
#endif

62
misc/kf/io-linux.cc Normal file
View File

@ -0,0 +1,62 @@
#include <iostream>
#include "io.h"
#include "io-linux.h"
char
Console::rdch()
{
char ch;
std::cin >> ch;
return ch;
}
void
Console::wrch(char c)
{
std::cout << c;
}
void
Console::rdbuf(char *buf, size_t len, bool stopat, char stopch)
{
size_t n = 0;
char ch;
while (n < len) {
std::cin >> ch;
if (stopat && stopch == ch) {
break;
}
buf[n] = ch;
}
}
void
Console::wrbuf(char *buf, size_t len)
{
for (size_t n = 0; n < len; n++) {
std::cout << buf[n];
}
}
// Line I/O
bool
Console::rdln(char *buf, size_t len) {
size_t n = 0;
char ch;
bool line = false;
while (n < len) {
std::cin >> ch;
if (ch == 0xa) {
line = true;
break;
}
buf[n] = ch;
}
return line;
}

21
misc/kf/io-linux.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef __KF_IO_LINUX_H__
#define __KF_IO_LINUX_H__
#include "io.h"
class Console : public IO {
public:
~Console() {};
char rdch(void);
void wrch(char c);
// Buffer I/O.
void rdbuf(char *buf, size_t len, bool stopat, char stopch);
void wrbuf(char *buf, size_t len);
// Line I/O
bool rdln(char *buf, size_t len);
private:
};
#endif // __KF_IO_LINUX_H__

View File

@ -3,8 +3,25 @@
// TODO(kyle): make this selectable by architecture.
class IO {
#ifdef __linux__
#include <stddef.h>
#endif
class IO {
public:
// Virtual destructor is required in all ABCs.
virtual ~IO() {};
// Building block methods.
virtual char rdch(void) = 0;
virtual void wrch(char c) = 0;
// Buffer I/O.
virtual void rdbuf(char *buf, size_t len, bool stopat, char stopch) = 0;
virtual void wrbuf(char *buf, size_t len) = 0;
// Line I/O
virtual bool rdln(char *buf, size_t len) = 0;
};
#endif // __KF_IO_H__

View File

@ -1,8 +1,26 @@
#include "io.h"
#ifdef __linux__
#include "linux.h"
#endif // __linux__
static void
repl(IO &interface)
{
static char lbuf[81];
while (true) {
interface.rdln(lbuf, 80);
}
return;
}
int
main(void)
{
#ifdef __linux__
Console interface;
#endif
repl(interface);
return 0;
}

12
misc/kf/linux.h Normal file
View File

@ -0,0 +1,12 @@
#ifndef __KF_LINUX_H__
#define __KF_LINUX_H__
#include <stdint.h>
// build support for linux
#include "io-linux.h"
constexpr uint8_t STACK_SIZE = 128;
#endif // __KF_LINUX_H__