120 lines
3.0 KiB
Rust
120 lines
3.0 KiB
Rust
/*
|
|
* Copyright (c) 2023 DTB <trinity@trinity.moe>
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify it under
|
|
* the terms of the GNU Affero General Public License as published by the Free
|
|
* Software Foundation, either version 3 of the License, or (at your option) any
|
|
* later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
|
|
* details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see https://www.gnu.org/licenses/.
|
|
*/
|
|
|
|
use std::{
|
|
env::args,
|
|
io::{ stdin, stdout, Error, Read, Write },
|
|
process::ExitCode
|
|
};
|
|
|
|
extern crate getopt;
|
|
use getopt::{ Opt, Parser };
|
|
|
|
extern crate sysexits;
|
|
use sysexits::{ EX_OK, EX_OSERR, EX_USAGE };
|
|
|
|
fn oserr(s: &str, e: Error) -> ExitCode {
|
|
eprintln!("{}: {}", s, e);
|
|
ExitCode::from(EX_OSERR as u8)
|
|
}
|
|
|
|
fn usage(s: &str) -> ExitCode {
|
|
eprintln!("Usage: {} (-aet)", s);
|
|
ExitCode::from(EX_USAGE as u8)
|
|
}
|
|
|
|
fn main() -> ExitCode {
|
|
let argv = args().collect::<Vec<String>>();
|
|
let input = stdin();
|
|
let mut output = stdout().lock();
|
|
|
|
let mut opts = Parser::new(&argv, "7et");
|
|
let mut ascii = false;
|
|
let mut showend = false;
|
|
let mut showtab = false;
|
|
|
|
loop {
|
|
match opts.next() {
|
|
None => break,
|
|
Some(o) =>
|
|
match o {
|
|
Ok(Opt('7', None)) => ascii = true,
|
|
Ok(Opt('e', None)) => showend = true,
|
|
Ok(Opt('t', None)) => showtab = true,
|
|
_ => { return usage(&argv[0]); }
|
|
}
|
|
}
|
|
}
|
|
|
|
/* cat -v emulation */
|
|
if ascii {
|
|
let mut c: u8 = 0;
|
|
|
|
for r in input.bytes() {
|
|
match r {
|
|
Ok(v) => { c = v; },
|
|
Err(e) => { return oserr(&argv[0], e); }
|
|
}
|
|
/* If a given byte isn't expressable in ASCII, print "M-[char]",
|
|
* where char is the chosen representation of the byte with its
|
|
* high bit set to 0. */
|
|
if c & 0x80 /* 0b 1000 0000 */ != 0 { print!("M-"); }
|
|
c ^= 0x80;
|
|
|
|
/* ASCII DEL is represented as ^? */
|
|
if c == 0x7f as u8 {
|
|
if let Err(e) = output.write_all(b"^?") {
|
|
return oserr(&argv[0], e);
|
|
}
|
|
break;
|
|
}
|
|
|
|
if c == '\n' as u8 && showend {
|
|
if let Err(e) = output.write_all(b"$") {
|
|
return oserr(&argv[0], e);
|
|
}
|
|
}
|
|
|
|
/* ASCII visual characters are shown literally. */
|
|
if c >= ' ' as u8 || c == '\n' as u8
|
|
|| (!showtab && c == '\t' as u8) {
|
|
if let Err(e) = output.write_all(&[c]) {
|
|
return oserr(&argv[0], e);
|
|
}
|
|
}
|
|
|
|
/* Otherwise, characters are shown as their control code, with NUL
|
|
* being ^@ and successive characters shown as "^" catenated with
|
|
* successive visual characters past "@". */
|
|
else {
|
|
c += '@' as u8;
|
|
if let Err(e) = output.write_all(b"^") {
|
|
return oserr(&argv[0], e);
|
|
}
|
|
if let Err(e) = output.write_all(&[c]) {
|
|
return oserr(&argv[0], e);
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
eprintln!("not implemented");
|
|
}
|
|
|
|
ExitCode::from(EX_OK as u8)
|
|
}
|