Rev 4492 | Rev 4509 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 4492 | Rev 4508 | ||
|---|---|---|---|
| Line 40... | Line 40... | ||
| 40 | #include <bool.h> |
40 | #include <bool.h> |
| 41 | #include <malloc.h> |
41 | #include <malloc.h> |
| 42 | #include <io/klog.h> |
42 | #include <io/klog.h> |
| 43 | #include <vfs/vfs.h> |
43 | #include <vfs/vfs.h> |
| 44 | #include <ipc/devmap.h> |
44 | #include <ipc/devmap.h> |
| - | 45 | #include <libadt/list.h> |
|
| 45 | 46 | ||
| 46 | FILE stdin_null = { |
47 | static FILE stdin_null = { |
| 47 | .fd = -1, |
48 | .fd = -1, |
| 48 | .error = true, |
49 | .error = true, |
| 49 | .eof = true, |
50 | .eof = true, |
| 50 | .klog = false, |
51 | .klog = false, |
| 51 | .phone = -1 |
52 | .phone = -1 |
| 52 | }; |
53 | }; |
| 53 | 54 | ||
| 54 | FILE stdout_klog = { |
55 | static FILE stdout_klog = { |
| 55 | .fd = -1, |
56 | .fd = -1, |
| 56 | .error = false, |
57 | .error = false, |
| 57 | .eof = false, |
58 | .eof = false, |
| 58 | .klog = true, |
59 | .klog = true, |
| 59 | .phone = -1 |
60 | .phone = -1 |
| 60 | }; |
61 | }; |
| 61 | 62 | ||
| - | 63 | static FILE stderr_klog = { |
|
| - | 64 | .fd = -1, |
|
| - | 65 | .error = false, |
|
| - | 66 | .eof = false, |
|
| - | 67 | .klog = true, |
|
| - | 68 | .phone = -1 |
|
| - | 69 | }; |
|
| - | 70 | ||
| - | 71 | FILE *stdin = NULL; |
|
| - | 72 | FILE *stdout = NULL; |
|
| - | 73 | FILE *stderr = NULL; |
|
| - | 74 | ||
| - | 75 | static LIST_INITIALIZE(files); |
|
| - | 76 | ||
| - | 77 | void stdio_init(int filc, fdi_node_t *filv[]) |
|
| - | 78 | { |
|
| - | 79 | if (filc > 0) { |
|
| - | 80 | stdin = fopen_node(filv[0], "r"); |
|
| - | 81 | } else { |
|
| 62 | FILE *stdin = &stdin_null; |
82 | stdin = &stdin_null; |
| - | 83 | list_append(&stdin->link, &files); |
|
| - | 84 | } |
|
| - | 85 | ||
| - | 86 | if (filc > 1) { |
|
| - | 87 | stdout = fopen_node(filv[1], "w"); |
|
| - | 88 | } else { |
|
| 63 | FILE *stdout = &stdout_klog; |
89 | stdout = &stdout_klog; |
| - | 90 | list_append(&stdout->link, &files); |
|
| - | 91 | } |
|
| - | 92 | ||
| - | 93 | if (filc > 2) { |
|
| - | 94 | stderr = fopen_node(filv[2], "w"); |
|
| - | 95 | } else { |
|
| 64 | FILE *stderr = &stdout_klog; |
96 | stderr = &stderr_klog; |
| - | 97 | list_append(&stderr->link, &files); |
|
| - | 98 | } |
|
| - | 99 | } |
|
| - | 100 | ||
| - | 101 | void stdio_done(void) |
|
| - | 102 | { |
|
| - | 103 | link_t *link = files.next; |
|
| - | 104 | ||
| - | 105 | while (link != &files) { |
|
| - | 106 | FILE *file = list_get_instance(link, FILE, link); |
|
| - | 107 | fclose(file); |
|
| - | 108 | link = files.next; |
|
| - | 109 | } |
|
| - | 110 | } |
|
| 65 | 111 | ||
| 66 | static bool parse_mode(const char *mode, int *flags) |
112 | static bool parse_mode(const char *mode, int *flags) |
| 67 | { |
113 | { |
| 68 | /* Parse mode except first character. */ |
114 | /* Parse mode except first character. */ |
| 69 | const char *mp = mode; |
115 | const char *mp = mode; |
| Line 139... | Line 185... | ||
| 139 | stream->error = false; |
185 | stream->error = false; |
| 140 | stream->eof = false; |
186 | stream->eof = false; |
| 141 | stream->klog = false; |
187 | stream->klog = false; |
| 142 | stream->phone = -1; |
188 | stream->phone = -1; |
| 143 | 189 | ||
| - | 190 | list_append(&stream->link, &files); |
|
| - | 191 | ||
| 144 | return stream; |
192 | return stream; |
| 145 | } |
193 | } |
| 146 | 194 | ||
| 147 | FILE *fopen_node(fdi_node_t *node, const char *mode) |
195 | FILE *fopen_node(fdi_node_t *node, const char *mode) |
| 148 | { |
196 | { |
| Line 167... | Line 215... | ||
| 167 | stream->error = false; |
215 | stream->error = false; |
| 168 | stream->eof = false; |
216 | stream->eof = false; |
| 169 | stream->klog = false; |
217 | stream->klog = false; |
| 170 | stream->phone = -1; |
218 | stream->phone = -1; |
| 171 | 219 | ||
| - | 220 | list_append(&stream->link, &files); |
|
| - | 221 | ||
| 172 | return stream; |
222 | return stream; |
| 173 | } |
223 | } |
| 174 | 224 | ||
| 175 | int fclose(FILE *stream) |
225 | int fclose(FILE *stream) |
| 176 | { |
226 | { |
| Line 182... | Line 232... | ||
| 182 | ipc_hangup(stream->phone); |
232 | ipc_hangup(stream->phone); |
| 183 | 233 | ||
| 184 | if (stream->fd >= 0) |
234 | if (stream->fd >= 0) |
| 185 | rc = close(stream->fd); |
235 | rc = close(stream->fd); |
| 186 | 236 | ||
| - | 237 | list_remove(&stream->link); |
|
| - | 238 | ||
| - | 239 | if ((stream != &stdin_null) |
|
| 187 | if ((stream != &stdin_null) && (stream != &stdout_klog)) |
240 | && (stream != &stdout_klog) |
| - | 241 | && (stream != &stderr_klog)) |
|
| 188 | free(stream); |
242 | free(stream); |
| 189 | 243 | ||
| 190 | stream = NULL; |
244 | stream = NULL; |
| 191 | 245 | ||
| 192 | if (rc != 0) { |
246 | if (rc != 0) { |
| Line 351... | Line 405... | ||
| 351 | } |
405 | } |
| 352 | 406 | ||
| 353 | return -1; |
407 | return -1; |
| 354 | } |
408 | } |
| 355 | 409 | ||
| 356 | void fnode(FILE *stream, fdi_node_t *node) |
410 | int fnode(FILE *stream, fdi_node_t *node) |
| 357 | { |
411 | { |
| 358 | if (stream->fd >= 0) { |
412 | if (stream->fd >= 0) |
| 359 | fd_node(stream->fd, node); |
413 | return fd_node(stream->fd, node); |
| 360 | } else { |
- | |
| 361 | node->fs_handle = 0; |
- | |
| 362 | node->dev_handle = 0; |
- | |
| 363 | node->index = 0; |
- | |
| 364 | } |
414 | |
| - | 415 | return ENOENT; |
|
| 365 | } |
416 | } |
| 366 | 417 | ||
| 367 | /** @} |
418 | /** @} |
| 368 | */ |
419 | */ |