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