summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/examples/stdio.rs
blob: 9e79d53567a552457426e84c09336ee0322ff493 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! A command which prints out information about the standard input,
//! output, and error streams provided to it.

#![cfg_attr(io_lifetimes_use_std, feature(io_safety))]

#[cfg(not(windows))]
use rustix::fd::AsFd;
#[cfg(not(windows))]
use rustix::io::{self, stderr, stdin, stdout};
#[cfg(feature = "termios")]
#[cfg(not(windows))]
use rustix::termios::isatty;
#[cfg(all(not(windows), feature = "termios", feature = "procfs"))]
use rustix::termios::ttyname;

#[cfg(not(windows))]
fn main() -> io::Result<()> {
    let (stdin, stdout, stderr) = unsafe { (stdin(), stdout(), stderr()) };

    println!("Stdin:");
    show(&stdin)?;

    println!("Stdout:");
    show(&stdout)?;

    println!("Stderr:");
    show(&stderr)?;

    Ok(())
}

#[cfg(not(windows))]
fn show<Fd: AsFd>(fd: Fd) -> io::Result<()> {
    let fd = fd.as_fd();
    println!(" - ready: {:?}", rustix::io::ioctl_fionread(fd)?);

    #[cfg(feature = "termios")]
    if isatty(fd) {
        #[cfg(feature = "procfs")]
        println!(" - ttyname: {}", ttyname(fd, Vec::new())?.to_string_lossy());
        println!(" - process group: {:?}", rustix::termios::tcgetpgrp(fd)?);
        println!(" - winsize: {:?}", rustix::termios::tcgetwinsize(fd)?);

        {
            use rustix::termios::*;
            let term = tcgetattr(fd)?;

            if let Some(speed) = speed_value(cfgetispeed(&term)) {
                println!(" - ispeed: {}", speed);
            }
            if let Some(speed) = speed_value(cfgetospeed(&term)) {
                println!(" - ospeed: {}", speed);
            }

            print!(" - in flags:");
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_iflag & IGNBRK) != 0 {
                print!(" IGNBRK");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_iflag & BRKINT) != 0 {
                print!(" BRKINT");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_iflag & IGNPAR) != 0 {
                print!(" IGNPAR");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_iflag & PARMRK) != 0 {
                print!(" PARMRK");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_iflag & INPCK) != 0 {
                print!(" INPCK");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_iflag & ISTRIP) != 0 {
                print!(" ISTRIP");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_iflag & INLCR) != 0 {
                print!(" INLCR");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_iflag & IGNCR) != 0 {
                print!(" IGNCR");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_iflag & ICRNL) != 0 {
                print!(" ICRNL");
            }
            #[cfg(any(
                linux_raw,
                all(
                    libc,
                    any(target_os = "haiku", target_os = "illumos", target_os = "solaris"),
                )
            ))]
            if (term.c_iflag & IUCLC) != 0 {
                print!(" IUCLC");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_iflag & IXON) != 0 {
                print!(" IXON");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_iflag & IXANY) != 0 {
                print!(" IXANY");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_iflag & IXOFF) != 0 {
                print!(" IXOFF");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_iflag & IMAXBEL) != 0 {
                print!(" IMAXBEL");
            }
            #[cfg(not(any(
                target_os = "dragonfly",
                target_os = "freebsd",
                target_os = "illumos",
                target_os = "ios",
                target_os = "macos",
                target_os = "netbsd",
                target_os = "openbsd",
                target_os = "redox",
            )))]
            if (term.c_iflag & IUTF8) != 0 {
                print!(" IUTF8");
            }
            println!();

            print!(" - out flags:");
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_oflag & OPOST) != 0 {
                print!(" OPOST");
            }
            #[cfg(not(any(
                target_os = "dragonfly",
                target_os = "freebsd",
                target_os = "ios",
                target_os = "macos",
                target_os = "netbsd",
                target_os = "redox",
            )))]
            if (term.c_oflag & OLCUC) != 0 {
                print!(" OLCUC");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_oflag & ONLCR) != 0 {
                print!(" ONLCR");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_oflag & OCRNL) != 0 {
                print!(" OCRNL");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_oflag & ONOCR) != 0 {
                print!(" ONOCR");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_oflag & ONLRET) != 0 {
                print!(" ONLRET");
            }
            #[cfg(not(any(
                target_os = "dragonfly",
                target_os = "freebsd",
                target_os = "ios",
                target_os = "macos",
                target_os = "netbsd",
                target_os = "openbsd",
            )))]
            if (term.c_oflag & OFILL) != 0 {
                print!(" OFILL");
            }
            #[cfg(not(any(
                target_os = "dragonfly",
                target_os = "freebsd",
                target_os = "ios",
                target_os = "macos",
                target_os = "netbsd",
                target_os = "openbsd",
            )))]
            if (term.c_oflag & OFDEL) != 0 {
                print!(" OFDEL");
            }
            #[cfg(not(any(
                target_os = "dragonfly",
                target_os = "freebsd",
                target_os = "illumos",
                target_os = "ios",
                target_os = "macos",
                target_os = "netbsd",
                target_os = "openbsd",
                target_os = "redox",
            )))]
            if (term.c_oflag & NLDLY) != 0 {
                print!(" NLDLY");
            }
            #[cfg(not(any(
                target_os = "dragonfly",
                target_os = "freebsd",
                target_os = "illumos",
                target_os = "ios",
                target_os = "macos",
                target_os = "netbsd",
                target_os = "openbsd",
                target_os = "redox",
            )))]
            if (term.c_oflag & CRDLY) != 0 {
                print!(" CRDLY");
            }
            #[cfg(not(any(
                target_os = "ios",
                target_os = "macos",
                target_os = "netbsd",
                target_os = "openbsd",
                target_os = "illumos",
                target_os = "redox",
            )))]
            if (term.c_oflag & TABDLY) != 0 {
                print!(" TABDLY");
            }
            #[cfg(not(any(
                target_os = "dragonfly",
                target_os = "freebsd",
                target_os = "illumos",
                target_os = "ios",
                target_os = "macos",
                target_os = "netbsd",
                target_os = "openbsd",
                target_os = "redox",
            )))]
            if (term.c_oflag & BSDLY) != 0 {
                print!(" BSDLY");
            }
            #[cfg(not(any(
                all(libc, target_env = "musl"),
                target_os = "dragonfly",
                target_os = "freebsd",
                target_os = "illumos",
                target_os = "ios",
                target_os = "macos",
                target_os = "netbsd",
                target_os = "openbsd",
                target_os = "redox",
            )))]
            if (term.c_oflag & VTDLY) != 0 {
                print!(" VTDLY");
            }
            #[cfg(not(any(
                all(libc, target_env = "musl"),
                target_os = "dragonfly",
                target_os = "freebsd",
                target_os = "illumos",
                target_os = "ios",
                target_os = "macos",
                target_os = "netbsd",
                target_os = "openbsd",
                target_os = "redox",
            )))]
            if (term.c_oflag & FFDLY) != 0 {
                print!(" FFDLY");
            }
            println!();

            print!(" - control flags:");
            #[cfg(not(any(
                target_os = "dragonfly",
                target_os = "freebsd",
                target_os = "ios",
                target_os = "macos",
                target_os = "netbsd",
                target_os = "openbsd",
                target_os = "redox",
            )))]
            if (term.c_cflag & CBAUD) != 0 {
                print!(" CBAUD");
            }
            #[cfg(not(any(
                target_os = "dragonfly",
                target_os = "freebsd",
                target_os = "illumos",
                target_os = "ios",
                target_os = "macos",
                target_os = "netbsd",
                target_os = "openbsd",
                target_os = "redox",
            )))]
            if (term.c_cflag & CBAUDEX) != 0 {
                print!(" CBAUDEX");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_cflag & CSIZE) != 0 {
                print!(" CSIZE");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_cflag & CSTOPB) != 0 {
                print!(" CSTOPB");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_cflag & CREAD) != 0 {
                print!(" CREAD");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_cflag & PARENB) != 0 {
                print!(" PARENB");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_cflag & PARODD) != 0 {
                print!(" PARODD");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_cflag & HUPCL) != 0 {
                print!(" HUPCL");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_cflag & CLOCAL) != 0 {
                print!(" CLOCAL");
            }
            #[cfg(not(any(
                target_os = "dragonfly",
                target_os = "freebsd",
                target_os = "ios",
                target_os = "macos",
                target_os = "netbsd",
                target_os = "openbsd",
                target_os = "redox",
            )))]
            if (term.c_cflag & CIBAUD) != 0 {
                print!(" CIBAUD");
            }
            #[cfg(not(any(
                target_os = "dragonfly",
                target_os = "freebsd",
                target_os = "illumos",
                target_os = "ios",
                target_os = "macos",
                target_os = "netbsd",
                target_os = "openbsd",
                target_os = "redox",
            )))]
            if (term.c_cflag & CMSPAR) != 0 {
                print!(" CMSPAR");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_cflag & CRTSCTS) != 0 {
                print!(" CRTSCTS");
            }
            println!();

            print!(" - local flags:");
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_lflag & ISIG) != 0 {
                print!(" ISIG");
            }
            if (term.c_lflag & ICANON) != 0 {
                print!(" ICANON");
            }
            #[cfg(any(linux_raw, all(libc, any(target_arch = "s390x", target_os = "haiku"))))]
            if (term.c_lflag & XCASE) != 0 {
                print!(" XCASE");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_lflag & ECHO) != 0 {
                print!(" ECHO");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_lflag & ECHOE) != 0 {
                print!(" ECHOE");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_lflag & ECHOK) != 0 {
                print!(" ECHOK");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_lflag & ECHONL) != 0 {
                print!(" ECHONL");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_lflag & ECHOCTL) != 0 {
                print!(" ECHOCTL");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_lflag & ECHOPRT) != 0 {
                print!(" ECHOPRT");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_lflag & ECHOKE) != 0 {
                print!(" ECHOKE");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_lflag & FLUSHO) != 0 {
                print!(" FLUSHO");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_lflag & NOFLSH) != 0 {
                print!(" NOFLSH");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_lflag & TOSTOP) != 0 {
                print!(" TOSTOP");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_lflag & PENDIN) != 0 {
                print!(" PENDIN");
            }
            #[cfg(not(any(target_os = "ios", target_os = "macos")))]
            if (term.c_lflag & IEXTEN) != 0 {
                print!(" IEXTEN");
            }
            println!();

            println!(
                " - keys: INTR={} QUIT={} ERASE={} KILL={} EOF={} TIME={} MIN={} ",
                key(term.c_cc[VINTR]),
                key(term.c_cc[VQUIT]),
                key(term.c_cc[VERASE]),
                key(term.c_cc[VKILL]),
                key(term.c_cc[VEOF]),
                term.c_cc[VTIME],
                term.c_cc[VMIN]
            );
            println!(
                "         START={} STOP={} SUSP={} EOL={} REPRINT={} DISCARD={}",
                key(term.c_cc[VSTART]),
                key(term.c_cc[VSTOP]),
                key(term.c_cc[VSUSP]),
                key(term.c_cc[VEOL]),
                key(term.c_cc[VREPRINT]),
                key(term.c_cc[VDISCARD])
            );
            println!(
                "         WERASE={} LNEXT={} EOL2={}",
                key(term.c_cc[VWERASE]),
                key(term.c_cc[VLNEXT]),
                key(term.c_cc[VEOL2])
            );
        }
    } else {
        println!(" - is not a tty");
    }

    println!();
    Ok(())
}

#[cfg(feature = "termios")]
#[cfg(not(windows))]
fn key(b: u8) -> String {
    if b == 0 {
        format!("<undef>")
    } else if b < 0x20 {
        format!("^{}", (b + 0x40) as char)
    } else if b == 0x7f {
        format!("^?")
    } else {
        format!("{}", b as char)
    }
}

#[cfg(windows)]
fn main() {
    unimplemented!()
}