]> git.proxmox.com Git - rustc.git/blame - vendor/rustix/examples/stdio.rs
New upstream version 1.66.0+dfsg1
[rustc.git] / vendor / rustix / examples / stdio.rs
CommitLineData
064997fb
FG
1//! A command which prints out information about the standard input,
2//! output, and error streams provided to it.
3
4#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]
5
6#[cfg(not(windows))]
7use rustix::fd::AsFd;
8#[cfg(not(windows))]
9use rustix::io::{self, stderr, stdin, stdout};
10#[cfg(feature = "termios")]
11#[cfg(not(windows))]
12use rustix::termios::isatty;
13#[cfg(all(not(windows), feature = "termios", feature = "procfs"))]
14use rustix::termios::ttyname;
15
16#[cfg(not(windows))]
17fn main() -> io::Result<()> {
18 let (stdin, stdout, stderr) = unsafe { (stdin(), stdout(), stderr()) };
19
20 println!("Stdin:");
21 show(&stdin)?;
22
23 println!("Stdout:");
24 show(&stdout)?;
25
26 println!("Stderr:");
27 show(&stderr)?;
28
29 Ok(())
30}
31
32#[cfg(not(windows))]
33fn show<Fd: AsFd>(fd: Fd) -> io::Result<()> {
34 let fd = fd.as_fd();
35 println!(" - ready: {:?}", rustix::io::ioctl_fionread(fd)?);
36
37 #[cfg(feature = "termios")]
38 if isatty(fd) {
39 #[cfg(feature = "procfs")]
40 println!(" - ttyname: {}", ttyname(fd, Vec::new())?.to_string_lossy());
41 println!(" - process group: {:?}", rustix::termios::tcgetpgrp(fd)?);
42 println!(" - winsize: {:?}", rustix::termios::tcgetwinsize(fd)?);
43
44 {
45 use rustix::termios::*;
46 let term = tcgetattr(fd)?;
47
48 if let Some(speed) = speed_value(cfgetispeed(&term)) {
49 println!(" - ispeed: {}", speed);
50 }
51 if let Some(speed) = speed_value(cfgetospeed(&term)) {
52 println!(" - ospeed: {}", speed);
53 }
54
55 print!(" - in flags:");
56 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
57 if (term.c_iflag & IGNBRK) != 0 {
58 print!(" IGNBRK");
59 }
60 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
61 if (term.c_iflag & BRKINT) != 0 {
62 print!(" BRKINT");
63 }
64 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
65 if (term.c_iflag & IGNPAR) != 0 {
66 print!(" IGNPAR");
67 }
68 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
69 if (term.c_iflag & PARMRK) != 0 {
70 print!(" PARMRK");
71 }
72 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
73 if (term.c_iflag & INPCK) != 0 {
74 print!(" INPCK");
75 }
76 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
77 if (term.c_iflag & ISTRIP) != 0 {
78 print!(" ISTRIP");
79 }
80 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
81 if (term.c_iflag & INLCR) != 0 {
82 print!(" INLCR");
83 }
84 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
85 if (term.c_iflag & IGNCR) != 0 {
86 print!(" IGNCR");
87 }
88 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
89 if (term.c_iflag & ICRNL) != 0 {
90 print!(" ICRNL");
91 }
92 #[cfg(any(
93 linux_raw,
94 all(
95 libc,
96 any(target_os = "haiku", target_os = "illumos", target_os = "solaris"),
97 )
98 ))]
99 if (term.c_iflag & IUCLC) != 0 {
100 print!(" IUCLC");
101 }
102 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
103 if (term.c_iflag & IXON) != 0 {
104 print!(" IXON");
105 }
106 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
107 if (term.c_iflag & IXANY) != 0 {
108 print!(" IXANY");
109 }
110 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
111 if (term.c_iflag & IXOFF) != 0 {
112 print!(" IXOFF");
113 }
114 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
115 if (term.c_iflag & IMAXBEL) != 0 {
116 print!(" IMAXBEL");
117 }
118 #[cfg(not(any(
119 target_os = "dragonfly",
120 target_os = "freebsd",
121 target_os = "illumos",
122 target_os = "ios",
123 target_os = "macos",
124 target_os = "netbsd",
125 target_os = "openbsd",
126 target_os = "redox",
127 )))]
128 if (term.c_iflag & IUTF8) != 0 {
129 print!(" IUTF8");
130 }
131 println!();
132
133 print!(" - out flags:");
134 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
135 if (term.c_oflag & OPOST) != 0 {
136 print!(" OPOST");
137 }
138 #[cfg(not(any(
139 target_os = "dragonfly",
140 target_os = "freebsd",
141 target_os = "ios",
142 target_os = "macos",
143 target_os = "netbsd",
144 target_os = "redox",
145 )))]
146 if (term.c_oflag & OLCUC) != 0 {
147 print!(" OLCUC");
148 }
149 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
150 if (term.c_oflag & ONLCR) != 0 {
151 print!(" ONLCR");
152 }
153 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
154 if (term.c_oflag & OCRNL) != 0 {
155 print!(" OCRNL");
156 }
157 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
158 if (term.c_oflag & ONOCR) != 0 {
159 print!(" ONOCR");
160 }
161 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
162 if (term.c_oflag & ONLRET) != 0 {
163 print!(" ONLRET");
164 }
165 #[cfg(not(any(
166 target_os = "dragonfly",
167 target_os = "freebsd",
168 target_os = "ios",
169 target_os = "macos",
170 target_os = "netbsd",
171 target_os = "openbsd",
172 )))]
173 if (term.c_oflag & OFILL) != 0 {
174 print!(" OFILL");
175 }
176 #[cfg(not(any(
177 target_os = "dragonfly",
178 target_os = "freebsd",
179 target_os = "ios",
180 target_os = "macos",
181 target_os = "netbsd",
182 target_os = "openbsd",
183 )))]
184 if (term.c_oflag & OFDEL) != 0 {
185 print!(" OFDEL");
186 }
187 #[cfg(not(any(
188 target_os = "dragonfly",
189 target_os = "freebsd",
190 target_os = "illumos",
191 target_os = "ios",
192 target_os = "macos",
193 target_os = "netbsd",
194 target_os = "openbsd",
195 target_os = "redox",
196 )))]
197 if (term.c_oflag & NLDLY) != 0 {
198 print!(" NLDLY");
199 }
200 #[cfg(not(any(
201 target_os = "dragonfly",
202 target_os = "freebsd",
203 target_os = "illumos",
204 target_os = "ios",
205 target_os = "macos",
206 target_os = "netbsd",
207 target_os = "openbsd",
208 target_os = "redox",
209 )))]
210 if (term.c_oflag & CRDLY) != 0 {
211 print!(" CRDLY");
212 }
213 #[cfg(not(any(
214 target_os = "ios",
215 target_os = "macos",
216 target_os = "netbsd",
217 target_os = "openbsd",
218 target_os = "illumos",
219 target_os = "redox",
220 )))]
221 if (term.c_oflag & TABDLY) != 0 {
222 print!(" TABDLY");
223 }
224 #[cfg(not(any(
225 target_os = "dragonfly",
226 target_os = "freebsd",
227 target_os = "illumos",
228 target_os = "ios",
229 target_os = "macos",
230 target_os = "netbsd",
231 target_os = "openbsd",
232 target_os = "redox",
233 )))]
234 if (term.c_oflag & BSDLY) != 0 {
235 print!(" BSDLY");
236 }
237 #[cfg(not(any(
238 all(libc, target_env = "musl"),
239 target_os = "dragonfly",
240 target_os = "freebsd",
241 target_os = "illumos",
242 target_os = "ios",
243 target_os = "macos",
244 target_os = "netbsd",
245 target_os = "openbsd",
246 target_os = "redox",
247 )))]
248 if (term.c_oflag & VTDLY) != 0 {
249 print!(" VTDLY");
250 }
251 #[cfg(not(any(
252 all(libc, target_env = "musl"),
253 target_os = "dragonfly",
254 target_os = "freebsd",
255 target_os = "illumos",
256 target_os = "ios",
257 target_os = "macos",
258 target_os = "netbsd",
259 target_os = "openbsd",
260 target_os = "redox",
261 )))]
262 if (term.c_oflag & FFDLY) != 0 {
263 print!(" FFDLY");
264 }
265 println!();
266
267 print!(" - control flags:");
268 #[cfg(not(any(
269 target_os = "dragonfly",
270 target_os = "freebsd",
271 target_os = "ios",
272 target_os = "macos",
273 target_os = "netbsd",
274 target_os = "openbsd",
275 target_os = "redox",
276 )))]
277 if (term.c_cflag & CBAUD) != 0 {
278 print!(" CBAUD");
279 }
280 #[cfg(not(any(
281 target_os = "dragonfly",
282 target_os = "freebsd",
283 target_os = "illumos",
284 target_os = "ios",
285 target_os = "macos",
286 target_os = "netbsd",
287 target_os = "openbsd",
288 target_os = "redox",
289 )))]
290 if (term.c_cflag & CBAUDEX) != 0 {
291 print!(" CBAUDEX");
292 }
293 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
294 if (term.c_cflag & CSIZE) != 0 {
295 print!(" CSIZE");
296 }
297 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
298 if (term.c_cflag & CSTOPB) != 0 {
299 print!(" CSTOPB");
300 }
301 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
302 if (term.c_cflag & CREAD) != 0 {
303 print!(" CREAD");
304 }
305 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
306 if (term.c_cflag & PARENB) != 0 {
307 print!(" PARENB");
308 }
309 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
310 if (term.c_cflag & PARODD) != 0 {
311 print!(" PARODD");
312 }
313 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
314 if (term.c_cflag & HUPCL) != 0 {
315 print!(" HUPCL");
316 }
317 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
318 if (term.c_cflag & CLOCAL) != 0 {
319 print!(" CLOCAL");
320 }
321 #[cfg(not(any(
322 target_os = "dragonfly",
323 target_os = "freebsd",
324 target_os = "ios",
325 target_os = "macos",
326 target_os = "netbsd",
327 target_os = "openbsd",
328 target_os = "redox",
329 )))]
330 if (term.c_cflag & CIBAUD) != 0 {
331 print!(" CIBAUD");
332 }
333 #[cfg(not(any(
334 target_os = "dragonfly",
335 target_os = "freebsd",
336 target_os = "illumos",
337 target_os = "ios",
338 target_os = "macos",
339 target_os = "netbsd",
340 target_os = "openbsd",
341 target_os = "redox",
342 )))]
343 if (term.c_cflag & CMSPAR) != 0 {
344 print!(" CMSPAR");
345 }
346 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
347 if (term.c_cflag & CRTSCTS) != 0 {
348 print!(" CRTSCTS");
349 }
350 println!();
351
352 print!(" - local flags:");
353 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
354 if (term.c_lflag & ISIG) != 0 {
355 print!(" ISIG");
356 }
357 if (term.c_lflag & ICANON) != 0 {
358 print!(" ICANON");
359 }
360 #[cfg(any(linux_raw, all(libc, any(target_arch = "s390x", target_os = "haiku"))))]
361 if (term.c_lflag & XCASE) != 0 {
362 print!(" XCASE");
363 }
364 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
365 if (term.c_lflag & ECHO) != 0 {
366 print!(" ECHO");
367 }
368 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
369 if (term.c_lflag & ECHOE) != 0 {
370 print!(" ECHOE");
371 }
372 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
373 if (term.c_lflag & ECHOK) != 0 {
374 print!(" ECHOK");
375 }
376 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
377 if (term.c_lflag & ECHONL) != 0 {
378 print!(" ECHONL");
379 }
380 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
381 if (term.c_lflag & ECHOCTL) != 0 {
382 print!(" ECHOCTL");
383 }
384 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
385 if (term.c_lflag & ECHOPRT) != 0 {
386 print!(" ECHOPRT");
387 }
388 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
389 if (term.c_lflag & ECHOKE) != 0 {
390 print!(" ECHOKE");
391 }
392 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
393 if (term.c_lflag & FLUSHO) != 0 {
394 print!(" FLUSHO");
395 }
396 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
397 if (term.c_lflag & NOFLSH) != 0 {
398 print!(" NOFLSH");
399 }
400 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
401 if (term.c_lflag & TOSTOP) != 0 {
402 print!(" TOSTOP");
403 }
404 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
405 if (term.c_lflag & PENDIN) != 0 {
406 print!(" PENDIN");
407 }
408 #[cfg(not(any(target_os = "ios", target_os = "macos")))]
409 if (term.c_lflag & IEXTEN) != 0 {
410 print!(" IEXTEN");
411 }
412 println!();
413
414 println!(
415 " - keys: INTR={} QUIT={} ERASE={} KILL={} EOF={} TIME={} MIN={} ",
416 key(term.c_cc[VINTR]),
417 key(term.c_cc[VQUIT]),
418 key(term.c_cc[VERASE]),
419 key(term.c_cc[VKILL]),
420 key(term.c_cc[VEOF]),
421 term.c_cc[VTIME],
422 term.c_cc[VMIN]
423 );
424 println!(
425 " START={} STOP={} SUSP={} EOL={} REPRINT={} DISCARD={}",
426 key(term.c_cc[VSTART]),
427 key(term.c_cc[VSTOP]),
428 key(term.c_cc[VSUSP]),
429 key(term.c_cc[VEOL]),
430 key(term.c_cc[VREPRINT]),
431 key(term.c_cc[VDISCARD])
432 );
433 println!(
434 " WERASE={} LNEXT={} EOL2={}",
435 key(term.c_cc[VWERASE]),
436 key(term.c_cc[VLNEXT]),
437 key(term.c_cc[VEOL2])
438 );
439 }
440 } else {
441 println!(" - is not a tty");
442 }
443
444 println!();
445 Ok(())
446}
447
448#[cfg(feature = "termios")]
449#[cfg(not(windows))]
450fn key(b: u8) -> String {
451 if b == 0 {
452 format!("<undef>")
453 } else if b < 0x20 {
454 format!("^{}", (b + 0x40) as char)
455 } else if b == 0x7f {
456 format!("^?")
457 } else {
458 format!("{}", b as char)
459 }
460}
461
462#[cfg(windows)]
463fn main() {
464 unimplemented!()
465}