]>
Commit | Line | Data |
---|---|---|
28f540f4 | 1 | /* stdio on a Mach device port. |
036a2725 | 2 | Translates \n to \r\n on output, echos and translates \r to \n on input. |
581c785b | 3 | Copyright (C) 1992-2022 Free Software Foundation, Inc. |
c84142e8 | 4 | This file is part of the GNU C Library. |
28f540f4 | 5 | |
c84142e8 | 6 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
7 | modify it under the terms of the GNU Lesser General Public |
8 | License as published by the Free Software Foundation; either | |
9 | version 2.1 of the License, or (at your option) any later version. | |
28f540f4 | 10 | |
c84142e8 UD |
11 | The GNU C Library is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 14 | Lesser General Public License for more details. |
28f540f4 | 15 | |
41bdb6e2 | 16 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 | 17 | License along with the GNU C Library; if not, see |
5a82c748 | 18 | <https://www.gnu.org/licenses/>. */ |
28f540f4 RM |
19 | |
20 | #include <stdio.h> | |
21 | #include <mach.h> | |
22 | #include <device/device.h> | |
23 | #include <errno.h> | |
24 | #include <string.h> | |
dba2bdbe | 25 | #include <libioP.h> |
28f540f4 | 26 | |
28f540f4 | 27 | |
b4e54243 RM |
28 | static ssize_t |
29 | devstream_write (void *cookie, const char *buffer, size_t n) | |
28f540f4 | 30 | { |
b4e54243 | 31 | const device_t dev = (device_t) cookie; |
28f540f4 | 32 | |
b4e54243 | 33 | int write_some (const char *p, size_t to_write) |
28f540f4 RM |
34 | { |
35 | kern_return_t err; | |
36 | int wrote; | |
e61abf83 UD |
37 | int thiswrite; |
38 | ||
28f540f4 RM |
39 | while (to_write > 0) |
40 | { | |
e61abf83 UD |
41 | thiswrite = to_write; |
42 | if (thiswrite > IO_INBAND_MAX) | |
43 | thiswrite = IO_INBAND_MAX; | |
44 | ||
b4e54243 | 45 | if (err = device_write_inband (dev, 0, 0, p, thiswrite, &wrote)) |
28f540f4 RM |
46 | { |
47 | errno = err; | |
b4e54243 | 48 | return 0; |
28f540f4 RM |
49 | } |
50 | p += wrote; | |
51 | to_write -= wrote; | |
28f540f4 | 52 | } |
b4e54243 | 53 | return 1; |
28f540f4 | 54 | } |
b4e54243 | 55 | int write_crlf (void) |
28f540f4 RM |
56 | { |
57 | static const char crlf[] = "\r\n"; | |
b4e54243 | 58 | return write_some (crlf, 2); |
28f540f4 RM |
59 | } |
60 | ||
b4e54243 | 61 | /* Search for newlines (LFs) in the buffer. */ |
28f540f4 | 62 | |
b4e54243 RM |
63 | const char *start = buffer, *p; |
64 | while ((p = memchr (start, '\n', n)) != NULL) | |
28f540f4 | 65 | { |
b4e54243 RM |
66 | /* Found one. Write out through the preceding character, |
67 | and then write a CR/LF pair. */ | |
28f540f4 | 68 | |
b4e54243 RM |
69 | if ((p > start && !write_some (start, p - start)) |
70 | || !write_crlf ()) | |
71 | return (start - buffer) ?: -1; | |
28f540f4 | 72 | |
b4e54243 RM |
73 | n -= p + 1 - start; |
74 | start = p + 1; | |
75 | } | |
28f540f4 | 76 | |
b4e54243 RM |
77 | /* Write the remainder of the buffer. */ |
78 | if (write_some (start, n)) | |
79 | start += n; | |
80 | return (start - buffer) ?: -1; | |
81 | } | |
28f540f4 | 82 | |
b4e54243 RM |
83 | static ssize_t |
84 | devstream_read (void *cookie, char *buffer, size_t to_read) | |
85 | { | |
86 | const device_t dev = (device_t) cookie; | |
28f540f4 | 87 | |
b4e54243 RM |
88 | kern_return_t err; |
89 | mach_msg_type_number_t nread = to_read; | |
28f540f4 | 90 | |
b4e54243 RM |
91 | err = device_read_inband (dev, 0, 0, to_read, buffer, &nread); |
92 | if (err) | |
93 | { | |
94 | errno = err; | |
95 | return -1; | |
96 | } | |
28f540f4 | 97 | |
b4e54243 RM |
98 | /* Translate CR to LF. */ |
99 | { | |
100 | char *p; | |
101 | for (p = memchr (buffer, '\r', nread); p; | |
102 | p = memchr (p + 1, '\r', (buffer + nread) - (p + 1))) | |
103 | *p = '\n'; | |
28f540f4 RM |
104 | } |
105 | ||
b4e54243 RM |
106 | /* Echo back what we read. */ |
107 | (void) devstream_write (cookie, buffer, nread); | |
28f540f4 | 108 | |
b4e54243 | 109 | return nread; |
28f540f4 RM |
110 | } |
111 | ||
112 | static int | |
113 | dealloc_ref (void *cookie) | |
114 | { | |
dba2bdbe | 115 | if (__mach_port_deallocate (mach_task_self (), (mach_port_t) cookie)) |
28f540f4 RM |
116 | { |
117 | errno = EINVAL; | |
118 | return -1; | |
119 | } | |
120 | return 0; | |
121 | } | |
122 | ||
28f540f4 RM |
123 | FILE * |
124 | mach_open_devstream (mach_port_t dev, const char *mode) | |
125 | { | |
126 | FILE *stream; | |
127 | ||
128 | if (mach_port_mod_refs (mach_task_self (), dev, MACH_PORT_RIGHT_SEND, 1)) | |
129 | { | |
130 | errno = EINVAL; | |
131 | return NULL; | |
132 | } | |
133 | ||
dba2bdbe ST |
134 | stream = _IO_fopencookie ((void *) dev, mode, |
135 | (cookie_io_functions_t) { write: devstream_write, | |
136 | read: devstream_read, | |
137 | close: dealloc_ref }); | |
28f540f4 RM |
138 | if (stream == NULL) |
139 | { | |
dba2bdbe | 140 | __mach_port_deallocate (mach_task_self (), dev); |
28f540f4 RM |
141 | return NULL; |
142 | } | |
143 | ||
28f540f4 RM |
144 | return stream; |
145 | } |