-
Notifications
You must be signed in to change notification settings - Fork 51
/
console.c
68 lines (52 loc) · 1.27 KB
/
console.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
#include "kernel_streams.h"
#include "tinyoslib.h"
/*
Here, we implement two pseudo-streams
that tie to stdin and stdout.
They can be used to run without terminals.
*/
extern FILE *saved_in, *saved_out;
static int stdio_read(void* __this, char *buf, unsigned int size)
{
size_t ret;
while(1) {
ret = fread_unlocked(buf, 1, size, saved_in);
if(ferror_unlocked(saved_in)) {
//assert(errno==EINTR);
if(errno!=EINTR) {
char buf[101];
fprintf(stderr, "error: %s\n",strerror_r(errno, buf, 100));
}
clearerr_unlocked(saved_in);
} else {
break;
}
}
return ret;
}
static int stdio_write(void* __this, const char* buf, unsigned int size)
{
return fwrite_unlocked(buf, 1, size, saved_out);
}
static int stdio_close(void* this) { return 0; }
file_ops __stdio_ops = {
.Read = stdio_read,
.Write = stdio_write,
.Close = stdio_close
};
void tinyos_pseudo_console()
{
Fid_t fid[2];
FCB* fcb[2];
/* Since FCB_reserve allocates fids in increasing order,
we expect pair[0]==0 and pair[1]==1 */
if(FCB_reserve(2, fid, fcb)==0 || fid[0]!=0 || fid[1]!=1)
{
printf("Failed to allocate console Fids\n");
abort();
}
fcb[0]->streamobj = NULL;
fcb[1]->streamobj = NULL;
fcb[0]->streamfunc = &__stdio_ops;
fcb[1]->streamfunc = &__stdio_ops;
}