Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions plib/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ pub fn input_stream(pathname: &PathBuf, dashed_stdin: bool) -> io::Result<Box<dy
Ok(file)
}

pub fn input_stream_opt(pathname: &Option<PathBuf>) -> io::Result<Box<dyn Read>> {
match pathname {
Some(path) => input_stream(&path, false),
None => input_stream(&PathBuf::new(), false),
}
}

pub fn input_reader(
pathname: &PathBuf,
dashed_stdin: bool,
Expand Down
20 changes: 7 additions & 13 deletions text/src/asa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ extern crate plib;
use clap::Parser;
use gettextrs::{bind_textdomain_codeset, gettext, textdomain};
use plib::PROJECT_NAME;
use std::fs;
use std::io::{self, BufRead, Read};
use std::io::{self, BufRead};
use std::path::PathBuf;

/// asa - interpret carriage-control characters
#[derive(Parser, Debug)]
#[command(author, version, about, long_about)]
struct Args {
/// Files to read as input.
files: Vec<String>,
files: Vec<PathBuf>,
}

struct AsaState {
Expand Down Expand Up @@ -69,14 +69,8 @@ impl AsaState {
}
}

fn asa_file(filename: &str) -> io::Result<()> {
let file: Box<dyn Read>;
if filename == "" {
file = Box::new(io::stdin().lock());
} else {
file = Box::new(fs::File::open(filename)?);
}
let mut reader = io::BufReader::new(file);
fn asa_file(pathname: &PathBuf) -> io::Result<()> {
let mut reader = plib::io::input_reader(pathname, false)?;
let mut line_no: usize = 0;
let mut state = AsaState::new();

Expand Down Expand Up @@ -142,15 +136,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// if no files, read from stdin
if args.files.is_empty() {
args.files.push(String::new());
args.files.push(PathBuf::new());
}

let mut exit_code = 0;

for filename in &args.files {
if let Err(e) = asa_file(filename) {
exit_code = 1;
eprintln!("{}: {}", filename, e);
eprintln!("{}: {}", filename.display(), e);
}
}

Expand Down
17 changes: 6 additions & 11 deletions text/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ extern crate plib;
use clap::Parser;
use gettextrs::{bind_textdomain_codeset, textdomain};
use plib::PROJECT_NAME;
use std::fs;
use std::io::{self, BufWriter, Read, Write};
use std::path::PathBuf;

/// expand - convert tabs to spaces
#[derive(Parser, Debug)]
Expand All @@ -25,7 +25,7 @@ struct Args {
tablist: Option<String>,

/// Files to read as input.
files: Vec<String>,
files: Vec<PathBuf>,
}

enum TabList {
Expand Down Expand Up @@ -67,14 +67,9 @@ fn space_out(column: &mut usize, writer: &mut BufWriter<dyn Write>) -> io::Resul
Ok(())
}

fn expand_file(tablist: &TabList, filename: &str) -> io::Result<()> {
fn expand_file(tablist: &TabList, pathname: &PathBuf) -> io::Result<()> {
// open file, or stdin
let mut file: Box<dyn Read>;
if filename == "" {
file = Box::new(io::stdin().lock());
} else {
file = Box::new(fs::File::open(filename)?);
}
let mut file = plib::io::input_stream(pathname, false)?;

let mut raw_buffer = [0; plib::BUFSZ];
let mut writer = BufWriter::new(io::stdout());
Expand Down Expand Up @@ -160,15 +155,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// if no files, read from stdin
if args.files.is_empty() {
args.files.push(String::new());
args.files.push(PathBuf::new());
}

let mut exit_code = 0;

for filename in &args.files {
if let Err(e) = expand_file(&tablist, filename) {
exit_code = 1;
eprintln!("{}: {}", filename, e);
eprintln!("{}: {}", filename.display(), e);
}
}

Expand Down
17 changes: 6 additions & 11 deletions text/src/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ extern crate plib;
use clap::Parser;
use gettextrs::{bind_textdomain_codeset, textdomain};
use plib::PROJECT_NAME;
use std::fs;
use std::io::{self, Read, Write};
use std::path::PathBuf;

const TABSTOP: usize = 8;

Expand All @@ -39,7 +39,7 @@ struct Args {
width: u64,

/// Files to read as input.
files: Vec<String>,
files: Vec<PathBuf>,
}

struct OutputState {
Expand Down Expand Up @@ -105,14 +105,9 @@ fn find_last_blank(v: &Vec<u8>) -> Option<usize> {
return None;
}

fn fold_file(args: &Args, filename: &str) -> io::Result<()> {
fn fold_file(args: &Args, pathname: &PathBuf) -> io::Result<()> {
// open file, or stdin
let mut file: Box<dyn Read>;
if filename == "" {
file = Box::new(io::stdin().lock());
} else {
file = Box::new(fs::File::open(filename)?);
}
let mut file = plib::io::input_stream(pathname, false)?;

let mut raw_buffer = [0; plib::BUFSZ];
let mut state = OutputState::new(args);
Expand Down Expand Up @@ -185,15 +180,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// if no files, read from stdin
if args.files.is_empty() {
args.files.push(String::new());
args.files.push(PathBuf::new());
}

let mut exit_code = 0;

for filename in &args.files {
if let Err(e) = fold_file(&args, filename) {
exit_code = 1;
eprintln!("{}: {}", filename, e);
eprintln!("{}: {}", filename.display(), e);
}
}

Expand Down
33 changes: 15 additions & 18 deletions text/src/tsort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,22 @@ extern crate plib;
use clap::Parser;
use gettextrs::{bind_textdomain_codeset, textdomain};
use plib::PROJECT_NAME;
use std::fs;
use std::io::{self, BufRead, Read};
use std::io::{self, BufRead};
use std::path::PathBuf;
use topological_sort::TopologicalSort;

/// tsort - topological sort
#[derive(Parser, Debug)]
#[command(author, version, about, long_about)]
struct Args {
/// File to read as input.
file: Option<String>,
file: Option<PathBuf>,
}

fn tsort_file(filename: &str) -> io::Result<()> {
let file: Box<dyn Read>;
if filename == "" {
file = Box::new(io::stdin().lock());
} else {
file = Box::new(fs::File::open(filename)?);
}

fn tsort_file(pathname: &Option<PathBuf>) -> io::Result<()> {
let file = plib::io::input_stream_opt(pathname)?;
let mut reader = io::BufReader::new(file);

let mut ts = TopologicalSort::<String>::new();
let mut sv: Vec<String> = Vec::new();

Expand Down Expand Up @@ -65,6 +60,13 @@ fn tsort_file(filename: &str) -> io::Result<()> {
Ok(())
}

fn pathname_display(path: &Option<PathBuf>) -> String {
match path {
None => String::from("stdin"),
Some(p) => p.display().to_string(),
}
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
// parse command line arguments
let args = Args::parse();
Expand All @@ -74,14 +76,9 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

let mut exit_code = 0;

let filename = match &args.file {
None => String::new(),
Some(name) => String::from(name),
};

if let Err(e) = tsort_file(&filename) {
if let Err(e) = tsort_file(&args.file) {
exit_code = 1;
eprintln!("{}: {}", filename, e);
eprintln!("{}: {}", pathname_display(&args.file), e);
}

std::process::exit(exit_code)
Expand Down
21 changes: 8 additions & 13 deletions xform/src/cksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,19 @@ mod crc32;
use clap::Parser;
use gettextrs::{bind_textdomain_codeset, textdomain};
use plib::PROJECT_NAME;
use std::fs;
use std::io::{self, Read};
use std::path::PathBuf;

/// cksum - write file checksums and sizes
#[derive(Parser, Debug)]
#[command(author, version, about, long_about)]
struct Args {
/// Files to read as input. Use "-" or no-args for stdin.
files: Vec<String>,
files: Vec<PathBuf>,
}

fn cksum_file(filename: &str) -> io::Result<()> {
let mut file: Box<dyn Read>;
if filename == "" {
file = Box::new(io::stdin().lock());
} else {
file = Box::new(fs::File::open(filename)?);
}
fn cksum_file(filename: &PathBuf) -> io::Result<()> {
let mut file = plib::io::input_stream(filename, false)?;

let mut buffer = [0; plib::BUFSZ];
let mut n_bytes: u64 = 0;
Expand All @@ -57,7 +52,7 @@ fn cksum_file(filename: &str) -> io::Result<()> {
}

let filename_prefix = {
if filename == "" {
if filename.as_os_str() == "" {
""
} else {
" "
Expand All @@ -68,7 +63,7 @@ fn cksum_file(filename: &str) -> io::Result<()> {
crc32::finalize(crc, n_bytes as usize),
n_bytes,
filename_prefix,
filename
filename.display()
);

Ok(())
Expand All @@ -83,15 +78,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// if no file args, read from stdin
if args.files.is_empty() {
args.files.push(String::new());
args.files.push(PathBuf::new());
}

let mut exit_code = 0;

for filename in &args.files {
if let Err(e) = cksum_file(filename) {
exit_code = 1;
eprintln!("{}: {}", filename, e);
eprintln!("{}: {}", filename.display(), e);
}
}

Expand Down
20 changes: 7 additions & 13 deletions xform/src/uncompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use clap::Parser;
use gettextrs::{bind_textdomain_codeset, textdomain};
use lzw::UnixLZWReader;
use plib::PROJECT_NAME;
use std::fs;
use std::io::{self, Read, Write};
use std::io::{self, Write};
use std::path::PathBuf;

/// uncompress - expand compressed data
#[derive(Parser, Debug)]
Expand All @@ -41,17 +41,11 @@ struct Args {
verbose: bool,

/// Files to read as input. Use "-" or no-args for stdin.
files: Vec<String>,
files: Vec<PathBuf>,
}

fn uncompress_file(filename: &str) -> io::Result<()> {
let file: Box<dyn Read>;
if filename == "" {
file = Box::new(io::stdin().lock());
} else {
file = Box::new(fs::File::open(filename)?);
}

fn uncompress_file(pathname: &PathBuf) -> io::Result<()> {
let file = plib::io::input_stream(pathname, false)?;
let mut decoder = UnixLZWReader::new(file);

loop {
Expand Down Expand Up @@ -80,7 +74,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

// if no file args, read from stdin
if args.files.is_empty() {
args.files.push(String::new());
args.files.push(PathBuf::new());
}

// zcat is a special case: always write to stdout
Expand All @@ -93,7 +87,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
for filename in &args.files {
if let Err(e) = uncompress_file(filename) {
exit_code = 1;
eprintln!("{}: {}", filename, e);
eprintln!("{}: {}", filename.display(), e);
}
}

Expand Down
Loading