forked from ablevm/able
-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.c
92 lines (88 loc) · 1.49 KB
/
term.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdatomic.h>
#include <pthread.h>
#include <able/able.h>
#include "term.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
int
term_recv_exec(term_recv_t *term_recv) {
struct {
uint8_t t;
uint8_t b[1024];
} mb;
int bc;
bc = 0;
int q;
q = 0;
while (q == 0) {
int c;
c = getchar();
if (c == EOF && feof(stdin)) {
q = 1;
c = 0;
}
if (c != '\n')
mb.b[bc++] = c;
if (q == 1 || c == '\n' || bc == sizeof(mb.b)) {
mb.t = 0;
while (able_mesg_link_send(&term_recv->l, &mb, sizeof(mb.t) + bc) < 0);
bc = 0;
}
}
return 0;
}
int
term_send_exec(term_send_t *term_send) {
uint8_t *r;
r = NULL;
size_t rc;
rc = 0;
for (;;) {
if (rc == 0) {
int y;
while((y = able_edge_clip(&term_send->e, term_send->s, term_send->sc)) < 0);
if (y == 0)
r = term_send->s;
while((rc += able_edge_recv(&term_send->e)) == 0)
able_node_wait(&term_send->n, &term_send->e, NULL);
}
able_mesg_t *m;
if (rc < sizeof(*m))
continue;
m = (able_mesg_t *)r;
r += m->sc;
rc -= m->sc;
struct {
uint8_t t;
uint8_t b[0];
} *mb;
mb = (void *)m->b;
switch (mb->t) {
case 0: { // type
size_t bc;
bc = m->bc - sizeof(mb->t);
if (bc == 0)
break;
bool f;
f = false;
int i;
for (i = 0; i < bc; i++) {
char c;
c = mb->b[i];
if (c == '\n')
f = true;
putchar(c);
}
if (f)
fflush(stdout);
break;
}
case 1: // bye
exit(0);
break;
}
}
// should not happen
return 1;
}