diff rvinterf/libasync/ttymagic.c @ 0:e7502631a0f9

initial import from freecalypso-sw rev 1033:5ab737ac3ad7
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Jun 2016 00:13:35 +0000
parents
children 09b4fd9b3827
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/rvinterf/libasync/ttymagic.c	Sat Jun 11 00:13:35 2016 +0000
@@ -0,0 +1,112 @@
+/*
+ * This module contains the tty "magic" code for fc-tmsh.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <termios.h>
+
+extern int ttyhacks;
+
+struct termios orig_termios, our_termios;
+
+#define	MAX_USER_CMD	78
+char usercmd[MAX_USER_CMD+1];
+int usercmd_len;
+
+void
+tty_init()
+{
+	if (!ttyhacks)
+		return;
+	tcgetattr(0, &orig_termios);
+	bcopy(&orig_termios, &our_termios, sizeof(struct termios));
+	our_termios.c_oflag &= ~(OCRNL | ONOCR | ONLRET);
+	our_termios.c_lflag &= ~(ICANON | ECHO);
+	our_termios.c_cc[VMIN] = 1;
+	our_termios.c_cc[VTIME] = 0;
+	tcsetattr(0, TCSAFLUSH, &our_termios);
+	putchar('>');
+	fflush(stdout);
+}
+
+void
+tty_cleanup()
+{
+	if (!ttyhacks)
+		return;
+	tcsetattr(0, TCSAFLUSH, &orig_termios);
+}
+
+void
+handle_tty_input()
+{
+	char buf[256];
+	int i, c, cc;
+
+	cc = read(0, buf, sizeof buf);
+	if (cc <= 0) {
+		tty_cleanup();
+		exit(0);
+	}
+	for (i = 0; i < cc; i++) {
+		c = buf[i];
+		if (c >= ' ' && c <= '~') {
+			if (usercmd_len >= MAX_USER_CMD)
+				continue;
+			usercmd[usercmd_len++] = c;
+			if (ttyhacks)
+				putchar(c);	/* echo */
+			continue;
+		}
+		switch (c) {
+		case '\b':
+		case 0x7F:
+			if (!usercmd_len)
+				continue;
+			usercmd_len--;
+			if (ttyhacks) {
+				putchar('\b');
+				putchar(' ');
+				putchar('\b');
+			}
+			continue;
+		case '\n':
+		case '\r':
+			usercmd[usercmd_len] = '\0';
+			if (ttyhacks)
+				putchar('\n');	/* echo */
+			dispatch_user_cmd();
+			usercmd_len = 0;
+			if (ttyhacks)
+				putchar('>');	/* new prompt */
+		}
+	}
+}
+
+void
+async_msg_output(msg)
+	char *msg;
+{
+	int msglen, i;
+
+	msglen = strlen(msg);
+	if (ttyhacks)
+		putchar('\r');
+	fputs(msg, stdout);
+	if (ttyhacks)
+		for (i = msglen; i < usercmd_len + 1; i++)
+			putchar(' ');
+	putchar('\n');
+	if (!ttyhacks)
+		return;
+	/* reprint the input line */
+	putchar('>');
+	if (!usercmd_len)
+		return;
+	fwrite(usercmd, 1, usercmd_len, stdout);
+}