comparison sw/mcsi-rxtx/ttymagic.c @ 7:8a386263dd51

fc-mcsi-rxtx skeleton put together
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 28 Oct 2024 01:44:28 +0000
parents
children
comparison
equal deleted inserted replaced
6:a10657f8024e 7:8a386263dd51
1 /*
2 * This module contains the tty "magic" code for fc-mcsi-rxtx,
3 * copied from fc-shell and fc-tmsh.
4 */
5
6 #include <sys/types.h>
7 #include <stdio.h>
8 #include <string.h>
9 #include <strings.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <termios.h>
13
14 extern int ttyhacks;
15
16 static struct termios orig_termios, our_termios;
17
18 #define MAX_USER_CMD 78
19 char usercmd[MAX_USER_CMD+1];
20 int usercmd_len;
21
22 void
23 tty_init()
24 {
25 if (!ttyhacks)
26 return;
27 tcgetattr(0, &orig_termios);
28 bcopy(&orig_termios, &our_termios, sizeof(struct termios));
29 our_termios.c_oflag &= ~(OCRNL | ONOCR | ONLRET);
30 our_termios.c_lflag &= ~(ICANON | ECHO);
31 our_termios.c_cc[VMIN] = 1;
32 our_termios.c_cc[VTIME] = 0;
33 tcsetattr(0, TCSAFLUSH, &our_termios);
34 putchar('>');
35 fflush(stdout);
36 }
37
38 void
39 tty_cleanup()
40 {
41 if (!ttyhacks)
42 return;
43 tcsetattr(0, TCSAFLUSH, &orig_termios);
44 }
45
46 void
47 handle_tty_input()
48 {
49 char buf[256];
50 int i, c, cc;
51
52 cc = read(0, buf, sizeof buf);
53 if (cc <= 0) {
54 tty_cleanup();
55 exit(0);
56 }
57 for (i = 0; i < cc; i++) {
58 c = buf[i];
59 if (c >= ' ' && c <= '~') {
60 if (usercmd_len >= MAX_USER_CMD)
61 continue;
62 usercmd[usercmd_len++] = c;
63 if (ttyhacks)
64 putchar(c); /* echo */
65 continue;
66 }
67 switch (c) {
68 case '\b':
69 case 0x7F:
70 if (!usercmd_len)
71 continue;
72 usercmd_len--;
73 if (ttyhacks) {
74 putchar('\b');
75 putchar(' ');
76 putchar('\b');
77 }
78 continue;
79 case '\n':
80 case '\r':
81 usercmd[usercmd_len] = '\0';
82 if (ttyhacks)
83 putchar('\n'); /* echo */
84 dispatch_user_cmd();
85 usercmd_len = 0;
86 if (ttyhacks)
87 putchar('>'); /* new prompt */
88 }
89 }
90 }
91
92 void
93 async_msg_output(msg)
94 char *msg;
95 {
96 int msglen, i;
97
98 msglen = strlen(msg);
99 if (ttyhacks)
100 putchar('\r');
101 fputs(msg, stdout);
102 if (ttyhacks)
103 for (i = msglen; i < usercmd_len + 1; i++)
104 putchar(' ');
105 putchar('\n');
106 if (!ttyhacks)
107 return;
108 /* reprint the input line */
109 putchar('>');
110 if (!usercmd_len)
111 return;
112 fwrite(usercmd, 1, usercmd_len, stdout);
113 }