-
Notifications
You must be signed in to change notification settings - Fork 0
/
tinyoslib.c
168 lines (120 loc) · 3.03 KB
/
tinyoslib.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio_ext.h>
#include "util.h"
#include "tinyos.h"
#include "tinyoslib.h"
static ssize_t tinyos_fid_read(void *cookie, char *buf, size_t size)
{
return Read(*(Fid_t*)cookie, buf, size);
}
static ssize_t tinyos_fid_write(void *cookie, const char *buf, size_t size)
{
int ret = Write(*(Fid_t*)cookie, buf, size);
return (ret<0) ? 0 : ret;
}
static int tinyos_fid_close(void* cookie)
{
free(cookie);
return 0;
}
static cookie_io_functions_t tinyos_fid_functions =
{
tinyos_fid_read,
tinyos_fid_write,
NULL,
tinyos_fid_close
};
static FILE* get_std_stream(int fid, const char* mode)
{
FILE* term = fidopen(fid, mode);
assert(term);
/* This is glibc-specific and tunrs off fstream locking */
__fsetlocking(term, FSETLOCKING_BYCALLER);
return term;
}
FILE* fidopen(Fid_t fid, const char* mode)
{
Fid_t* fidloc = (Fid_t *) malloc(sizeof(Fid_t));
* fidloc = fid;
FILE* f = fopencookie(fidloc, mode, tinyos_fid_functions);
CHECKRC(setvbuf(f, NULL, _IONBF, 0));
return f;
}
FILE *saved_in = NULL, *saved_out = NULL;
void tinyos_replace_stdio()
{
assert(saved_in == NULL);
assert(saved_out == NULL);
//if(GetTerminalDevices()==0) return;
FILE* termin = get_std_stream(0, "r");
FILE* termout = get_std_stream(1, "w");
saved_in = stdin;
saved_out = stdout;
stdin = termin;
stdout = termout;
}
void tinyos_restore_stdio()
{
if(saved_out == NULL) return;
fclose(stdin);
fclose(stdout);
stdin = saved_in;
stdout = saved_out;
saved_in = saved_out = NULL;
}
static int exec_wrapper(int argl, void* args)
{
/* unpack the program pointer */
Program prog;
/* unpack the prog pointer */
memcpy(&prog, args, sizeof(prog));
argl -= sizeof(prog);
args += sizeof(prog);
/* unpack the string vector */
size_t argc = argscount(argl, args);
const char* argv[argc];
argvunpack(argc, argv, argl, args);
/* Make the call */
return prog(argc, argv);
}
int ParseProcInfo(procinfo* pinfo, Program* prog, int argc, const char** argv )
{
if(pinfo->main_task != exec_wrapper)
/* We do not recognize the format! */
return -1;
if(pinfo->argl > PROCINFO_MAX_ARGS_SIZE)
/* The full argument is not available */
return -1;
int argl = pinfo->argl;
void* args = pinfo->args;
/* unpack the program pointer */
if(prog) memcpy(&prog, args, sizeof(prog));
argl -= sizeof(Program*);
args += sizeof(Program*);
/* unpack the string vector */
size_t N = argscount(argl, args);
if(argv) {
if(argc>N)
argc = N;
argvunpack(argc, argv, argl, args);
}
return N;
}
int Execute(Program prog, size_t argc, const char** argv)
{
/* We will pack the prog pointer and the arguments to
an argument buffer.
*/
/* compute the argument buffer size */
size_t argl = argvlen(argc, argv) + sizeof(prog);
/* allocate the buffer */
char args[argl];
/* put the pointer at the start */
memcpy(args, &prog, sizeof(prog));
/* add the string vector */
argvpack(args+sizeof(prog), argc, argv);
/* Execute the process */
return Exec(exec_wrapper, argl, args);
}