-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathterminal.c
72 lines (52 loc) · 1.88 KB
/
terminal.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <muos/naming.h>
#include <muos/message.h>
#include "uart.h"
int main (int argc, char *argv[]) {
int uart_coid = NameOpen("/dev/uart");
while (true) {
UartMessage msg;
UartReply reply;
struct iovec msg_parts[2];
struct iovec reply_parts[2];
char buf[1];
int num;
msg.type = UART_MESSAGE_READ;
msg.payload.read.len = sizeof(buf);
msg_parts[0].iov_base = &msg;
msg_parts[0].iov_len = sizeof(msg);
reply_parts[0].iov_base = &reply;
reply_parts[0].iov_len = offsetof(UartReply, payload.read.buf);
reply_parts[1].iov_base = &buf[0];
reply_parts[1].iov_len = msg.payload.read.len;
num = MessageSendV(uart_coid, msg_parts, 1, reply_parts, 2);
if (num < offsetof(UartReply, payload.read.len) +
sizeof(reply.payload.read.len))
{
/*
Didn't get a valid 'len' field filled in on the response
*/
continue;
}
/* Flip the casing of the character */
for (num = 0; num < reply.payload.read.len; ++num) {
if (buf[num] >= 'A' && buf[num] <= 'Z') {
buf[num] = 'a' + (buf[num] - 'A');
}
else if (buf[num] >= 'a' && buf[num] <= 'z') {
buf[num] = 'A' + (buf[num] - 'a');
}
}
msg.type = UART_MESSAGE_WRITE;
msg.payload.write.len = reply.payload.read.len;
msg_parts[0].iov_base = &msg;
msg_parts[0].iov_len = offsetof(UartMessage, payload.write.buf);
msg_parts[1].iov_base = &buf[0];
msg_parts[1].iov_len = msg.payload.write.len;
reply_parts[0].iov_base = &reply;
reply_parts[0].iov_len = sizeof(reply);
num = MessageSendV(uart_coid, msg_parts, 2, reply_parts, 1);
}
}