|
3 | 3 | use core::{fmt, slice}; |
4 | 4 | use nr; |
5 | 5 |
|
6 | | -/// Host's standard error |
7 | | -pub struct HStderr { |
| 6 | +/// A byte stream to the host (e.g. host's stdout or stderr). |
| 7 | +pub struct HostStream { |
8 | 8 | fd: usize, |
9 | 9 | } |
10 | 10 |
|
11 | | -impl HStderr { |
| 11 | +impl HostStream { |
12 | 12 | /// Attempts to write an entire `buffer` into this sink |
13 | 13 | pub fn write_all(&mut self, buffer: &[u8]) -> Result<(), ()> { |
14 | 14 | write_all(self.fd, buffer) |
15 | 15 | } |
16 | 16 | } |
17 | 17 |
|
18 | | -impl fmt::Write for HStderr { |
19 | | - fn write_str(&mut self, s: &str) -> fmt::Result { |
20 | | - self.write_all(s.as_bytes()).map_err(|_| fmt::Error) |
21 | | - } |
22 | | -} |
23 | | - |
24 | | -/// Host's standard output |
25 | | -pub struct HStdout { |
26 | | - fd: usize, |
27 | | -} |
28 | | - |
29 | | -impl HStdout { |
30 | | - /// Attempts to write an entire `buffer` into this sink |
31 | | - pub fn write_all(&mut self, buffer: &[u8]) -> Result<(), ()> { |
32 | | - write_all(self.fd, buffer) |
33 | | - } |
34 | | -} |
35 | | - |
36 | | -impl fmt::Write for HStdout { |
| 18 | +impl fmt::Write for HostStream { |
37 | 19 | fn write_str(&mut self, s: &str) -> fmt::Result { |
38 | 20 | self.write_all(s.as_bytes()).map_err(|_| fmt::Error) |
39 | 21 | } |
40 | 22 | } |
41 | 23 |
|
42 | 24 | /// Construct a new handle to the host's standard error. |
43 | | -pub fn hstderr() -> Result<HStderr, ()> { |
| 25 | +pub fn hstderr() -> Result<HostStream, ()> { |
44 | 26 | // There is actually no stderr access in ARM Semihosting documentation. Use |
45 | 27 | // convention used in libgloss. |
46 | 28 | // See: libgloss/arm/syscalls.c, line 139. |
47 | 29 | // https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=libgloss/arm/syscalls.c#l139 |
48 | | - open(":tt\0", nr::open::W_APPEND).map(|fd| HStderr { fd }) |
| 30 | + open(":tt\0", nr::open::W_APPEND) |
49 | 31 | } |
50 | 32 |
|
51 | 33 | /// Construct a new handle to the host's standard output. |
52 | | -pub fn hstdout() -> Result<HStdout, ()> { |
53 | | - open(":tt\0", nr::open::W_TRUNC).map(|fd| HStdout { fd }) |
| 34 | +pub fn hstdout() -> Result<HostStream, ()> { |
| 35 | + open(":tt\0", nr::open::W_TRUNC) |
54 | 36 | } |
55 | 37 |
|
56 | | -fn open(name: &str, mode: usize) -> Result<usize, ()> { |
| 38 | +fn open(name: &str, mode: usize) -> Result<HostStream, ()> { |
57 | 39 | let name = name.as_bytes(); |
58 | 40 | match unsafe { syscall!(OPEN, name.as_ptr(), mode, name.len() - 1) } as |
59 | 41 | isize { |
60 | 42 | -1 => Err(()), |
61 | | - fd => Ok(fd as usize), |
| 43 | + fd => Ok(HostStream { fd: fd as usize}), |
62 | 44 | } |
63 | 45 | } |
64 | 46 |
|
|
0 commit comments