Rev 4581 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4581 | Rev 4718 | ||
---|---|---|---|
Line 87... | Line 87... | ||
87 | FILE *stdout = NULL; |
87 | FILE *stdout = NULL; |
88 | FILE *stderr = NULL; |
88 | FILE *stderr = NULL; |
89 | 89 | ||
90 | static LIST_INITIALIZE(files); |
90 | static LIST_INITIALIZE(files); |
91 | 91 | ||
92 | void stdio_init(int filc, fdi_node_t *filv[]) |
92 | void __stdio_init(int filc, fdi_node_t *filv[]) |
93 | { |
93 | { |
94 | if (filc > 0) { |
94 | if (filc > 0) { |
95 | stdin = fopen_node(filv[0], "r"); |
95 | stdin = fopen_node(filv[0], "r"); |
96 | } else { |
96 | } else { |
97 | stdin = &stdin_null; |
97 | stdin = &stdin_null; |
Line 111... | Line 111... | ||
111 | stderr = &stderr_klog; |
111 | stderr = &stderr_klog; |
112 | list_append(&stderr->link, &files); |
112 | list_append(&stderr->link, &files); |
113 | } |
113 | } |
114 | } |
114 | } |
115 | 115 | ||
116 | void stdio_done(void) |
116 | void __stdio_done(void) |
117 | { |
117 | { |
118 | link_t *link = files.next; |
118 | link_t *link = files.next; |
119 | 119 | ||
120 | while (link != &files) { |
120 | while (link != &files) { |
121 | FILE *file = list_get_instance(link, FILE, link); |
121 | FILE *file = list_get_instance(link, FILE, link); |
Line 178... | Line 178... | ||
178 | stream->buf = buf; |
178 | stream->buf = buf; |
179 | stream->buf_size = size; |
179 | stream->buf_size = size; |
180 | stream->buf_head = stream->buf; |
180 | stream->buf_head = stream->buf; |
181 | } |
181 | } |
182 | 182 | ||
- | 183 | static void _setvbuf(FILE *stream) |
|
- | 184 | { |
|
- | 185 | /* FIXME: Use more complex rules for setting buffering options. */ |
|
- | 186 | ||
- | 187 | switch (stream->fd) { |
|
- | 188 | case 1: |
|
- | 189 | setvbuf(stream, NULL, _IOLBF, BUFSIZ); |
|
- | 190 | break; |
|
- | 191 | case 0: |
|
- | 192 | case 2: |
|
- | 193 | setvbuf(stream, NULL, _IONBF, 0); |
|
- | 194 | break; |
|
- | 195 | default: |
|
- | 196 | setvbuf(stream, NULL, _IOFBF, BUFSIZ); |
|
- | 197 | } |
|
- | 198 | } |
|
- | 199 | ||
183 | /** Allocate stream buffer. */ |
200 | /** Allocate stream buffer. */ |
184 | static int _fallocbuf(FILE *stream) |
201 | static int _fallocbuf(FILE *stream) |
185 | { |
202 | { |
186 | assert(stream->buf == NULL); |
203 | assert(stream->buf == NULL); |
187 | 204 | ||
Line 223... | Line 240... | ||
223 | 240 | ||
224 | stream->error = false; |
241 | stream->error = false; |
225 | stream->eof = false; |
242 | stream->eof = false; |
226 | stream->klog = false; |
243 | stream->klog = false; |
227 | stream->phone = -1; |
244 | stream->phone = -1; |
228 | - | ||
229 | /* FIXME: Should select buffering type based on what was opened. */ |
- | |
230 | setvbuf(stream, NULL, _IOFBF, BUFSIZ); |
245 | _setvbuf(stream); |
231 | 246 | ||
232 | list_append(&stream->link, &files); |
247 | list_append(&stream->link, &files); |
233 | 248 | ||
234 | return stream; |
249 | return stream; |
235 | } |
250 | } |
Line 246... | Line 261... | ||
246 | stream->fd = fd; |
261 | stream->fd = fd; |
247 | stream->error = false; |
262 | stream->error = false; |
248 | stream->eof = false; |
263 | stream->eof = false; |
249 | stream->klog = false; |
264 | stream->klog = false; |
250 | stream->phone = -1; |
265 | stream->phone = -1; |
251 | - | ||
252 | /* FIXME: Should select buffering type based on what was opened. */ |
- | |
253 | setvbuf(stream, NULL, _IOLBF, BUFSIZ); |
266 | _setvbuf(stream); |
254 | 267 | ||
255 | list_append(&stream->link, &files); |
268 | list_append(&stream->link, &files); |
256 | 269 | ||
257 | return stream; |
270 | return stream; |
258 | } |
271 | } |
Line 279... | Line 292... | ||
279 | 292 | ||
280 | stream->error = false; |
293 | stream->error = false; |
281 | stream->eof = false; |
294 | stream->eof = false; |
282 | stream->klog = false; |
295 | stream->klog = false; |
283 | stream->phone = -1; |
296 | stream->phone = -1; |
284 | - | ||
285 | /* FIXME: Should select buffering type based on what was opened. */ |
- | |
286 | setvbuf(stream, NULL, _IOLBF, BUFSIZ); |
297 | _setvbuf(stream); |
287 | 298 | ||
288 | list_append(&stream->link, &files); |
299 | list_append(&stream->link, &files); |
289 | 300 | ||
290 | return stream; |
301 | return stream; |
291 | } |
302 | } |
Line 378... | Line 389... | ||
378 | /** Drain stream buffer, do not sync stream. */ |
389 | /** Drain stream buffer, do not sync stream. */ |
379 | static void _fflushbuf(FILE *stream) |
390 | static void _fflushbuf(FILE *stream) |
380 | { |
391 | { |
381 | size_t bytes_used; |
392 | size_t bytes_used; |
382 | 393 | ||
383 | if (!stream->buf || stream->btype == _IONBF || stream->error) |
394 | if ((!stream->buf) || (stream->btype == _IONBF) || (stream->error)) |
384 | return; |
395 | return; |
385 | 396 | ||
386 | bytes_used = stream->buf_head - stream->buf; |
397 | bytes_used = stream->buf_head - stream->buf; |
387 | if (bytes_used == 0) |
398 | if (bytes_used == 0) |
388 | return; |
399 | return; |
Line 409... | Line 420... | ||
409 | size_t i; |
420 | size_t i; |
410 | uint8_t b; |
421 | uint8_t b; |
411 | bool need_flush; |
422 | bool need_flush; |
412 | 423 | ||
413 | /* If not buffered stream, write out directly. */ |
424 | /* If not buffered stream, write out directly. */ |
414 | if (stream->btype == _IONBF) |
425 | if (stream->btype == _IONBF) { |
415 | return _fwrite(buf, size, nmemb, stream); |
426 | now = _fwrite(buf, size, nmemb, stream); |
- | 427 | fflush(stream); |
|
- | 428 | return now; |
|
- | 429 | } |
|
416 | 430 | ||
417 | /* Perform lazy allocation of stream buffer. */ |
431 | /* Perform lazy allocation of stream buffer. */ |
418 | if (stream->buf == NULL) { |
432 | if (stream->buf == NULL) { |
419 | if (_fallocbuf(stream) != 0) |
433 | if (_fallocbuf(stream) != 0) |
420 | return 0; /* Errno set by _fallocbuf(). */ |
434 | return 0; /* Errno set by _fallocbuf(). */ |
Line 423... | Line 437... | ||
423 | data = (uint8_t *) buf; |
437 | data = (uint8_t *) buf; |
424 | bytes_left = size * nmemb; |
438 | bytes_left = size * nmemb; |
425 | total_written = 0; |
439 | total_written = 0; |
426 | need_flush = false; |
440 | need_flush = false; |
427 | 441 | ||
428 | while (!stream->error && bytes_left > 0) { |
442 | while ((!stream->error) && (bytes_left > 0)) { |
429 | - | ||
430 | buf_free = stream->buf_size - (stream->buf_head - stream->buf); |
443 | buf_free = stream->buf_size - (stream->buf_head - stream->buf); |
431 | if (bytes_left > buf_free) |
444 | if (bytes_left > buf_free) |
432 | now = buf_free; |
445 | now = buf_free; |
433 | else |
446 | else |
434 | now = bytes_left; |
447 | now = bytes_left; |
435 | 448 | ||
436 | for (i = 0; i < now; i++) { |
449 | for (i = 0; i < now; i++) { |
437 | b = data[i]; |
450 | b = data[i]; |
438 | stream->buf_head[i] = b; |
451 | stream->buf_head[i] = b; |
439 | 452 | ||
440 | if (b == '\n' && stream->btype == _IOLBF) |
453 | if ((b == '\n') && (stream->btype == _IOLBF)) |
441 | need_flush = true; |
454 | need_flush = true; |
442 | } |
455 | } |
443 | 456 | ||
444 | buf += now; |
457 | buf += now; |
445 | stream->buf_head += now; |
458 | stream->buf_head += now; |