From 0d2758a9941b64d39fe11ff2b1111d1661e64edb Mon Sep 17 00:00:00 2001 From: Kyle Isom Date: Wed, 21 Feb 2018 12:25:03 -0800 Subject: [PATCH] kf: linux console for I/O --- .gitignore | 4 ++- misc/kf/build.yaml | 2 +- misc/kf/interface.cc | 3 +++ misc/kf/io-linux.cc | 62 ++++++++++++++++++++++++++++++++++++++++++++ misc/kf/io-linux.h | 21 +++++++++++++++ misc/kf/io.h | 19 +++++++++++++- misc/kf/kf.cc | 18 +++++++++++++ misc/kf/linux.h | 12 +++++++++ 8 files changed, 138 insertions(+), 3 deletions(-) create mode 100644 misc/kf/interface.cc create mode 100644 misc/kf/io-linux.cc create mode 100644 misc/kf/io-linux.h create mode 100644 misc/kf/linux.h diff --git a/.gitignore b/.gitignore index 70a8511..6ccae54 100644 --- a/.gitignore +++ b/.gitignore @@ -36,7 +36,9 @@ ltmain.sh m4/ missing -ch??ex?? +# build artifacts +ods/src/ch??ex?? *_test *.la *_bench +misc/kf/kf diff --git a/misc/kf/build.yaml b/misc/kf/build.yaml index ab57cc6..2a3a8f5 100644 --- a/misc/kf/build.yaml +++ b/misc/kf/build.yaml @@ -5,5 +5,5 @@ compilers: targets: cc: kf: - - io.cc + - interface.cc - kf.cc \ No newline at end of file diff --git a/misc/kf/interface.cc b/misc/kf/interface.cc new file mode 100644 index 0000000..44c9091 --- /dev/null +++ b/misc/kf/interface.cc @@ -0,0 +1,3 @@ +#ifdef __linux__ +#include "io-linux.cc" +#endif \ No newline at end of file diff --git a/misc/kf/io-linux.cc b/misc/kf/io-linux.cc new file mode 100644 index 0000000..34f3211 --- /dev/null +++ b/misc/kf/io-linux.cc @@ -0,0 +1,62 @@ +#include +#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; +} \ No newline at end of file diff --git a/misc/kf/io-linux.h b/misc/kf/io-linux.h new file mode 100644 index 0000000..087168d --- /dev/null +++ b/misc/kf/io-linux.h @@ -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__ diff --git a/misc/kf/io.h b/misc/kf/io.h index ed14085..bbc3a0a 100644 --- a/misc/kf/io.h +++ b/misc/kf/io.h @@ -3,8 +3,25 @@ // TODO(kyle): make this selectable by architecture. -class IO { +#ifdef __linux__ +#include +#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__ \ No newline at end of file diff --git a/misc/kf/kf.cc b/misc/kf/kf.cc index abb3494..0a79085 100644 --- a/misc/kf/kf.cc +++ b/misc/kf/kf.cc @@ -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; } \ No newline at end of file diff --git a/misc/kf/linux.h b/misc/kf/linux.h new file mode 100644 index 0000000..79fb71e --- /dev/null +++ b/misc/kf/linux.h @@ -0,0 +1,12 @@ +#ifndef __KF_LINUX_H__ +#define __KF_LINUX_H__ + +#include + +// build support for linux +#include "io-linux.h" + +constexpr uint8_t STACK_SIZE = 128; + + +#endif // __KF_LINUX_H__ \ No newline at end of file