diff mbox

[06/11] paflof: Add a read() function to read keyboard input

Message ID 1473450729-19359-7-git-send-email-thuth@redhat.com
State Accepted
Headers show

Commit Message

Thomas Huth Sept. 9, 2016, 7:52 p.m. UTC
The libnet code uses read() to check for keyboard input
(so that it can abort the network loading when the user
pressed ESC). So to be able to use the libnet code with
Paflof, too, we have got to provide a read() function here.

Signed-off-by: Thomas Huth <thuth@redhat.com>
---
 slof/ppc64.c | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)
diff mbox

Patch

diff --git a/slof/ppc64.c b/slof/ppc64.c
index 7169cb0..e88b240 100644
--- a/slof/ppc64.c
+++ b/slof/ppc64.c
@@ -202,3 +202,29 @@  int send(int fd, const void *buf, int len, int flags)
 
 	return forth_eval_pop("write");
 }
+
+/**
+ * Standard read function for the libc.
+ *
+ * @param fd    file descriptor (should always be 0 or 2)
+ * @param buf   pointer to the array with the output characters
+ * @param len    number of bytes to be read
+ * @return      the number of bytes that have been read successfully
+ */
+ssize_t read(int fd, void *buf, size_t len)
+{
+	char *ptr = (char *)buf;
+	int cnt = 0;
+	char code;
+
+	if (fd == 0 || fd == 2) {
+		while (cnt < len) {
+			code = forth_eval_pop("key? IF key ELSE 0 THEN");
+			if (!code)
+				break;
+			ptr[cnt++] = code;
+		}
+	}
+
+	return cnt;
+}