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