2023-12-27 15:44:19 -07:00
|
|
|
|
/*
|
2024-01-15 16:33:51 -07:00
|
|
|
|
* Copyright (c) 2023–2024 Emma Tebibyte <emma@tebibyte.media>
|
2023-12-27 15:44:19 -07:00
|
|
|
|
* 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,
|
2024-02-23 21:46:50 -07:00
|
|
|
|
io::{ Read, stdin, stdout, Write },
|
2023-12-27 15:44:19 -07:00
|
|
|
|
process::{ Command, exit, Stdio },
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-15 16:30:00 -07:00
|
|
|
|
extern crate getopt;
|
2024-03-01 23:10:28 -07:00
|
|
|
|
extern crate strerror;
|
|
|
|
|
extern crate sysexits;
|
2024-01-15 16:30:00 -07:00
|
|
|
|
|
|
|
|
|
use getopt::{ Opt, Parser };
|
2024-03-01 23:10:28 -07:00
|
|
|
|
use strerror::c_error;
|
2024-02-14 22:53:45 -07:00
|
|
|
|
use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE };
|
2023-12-27 22:42:50 -07:00
|
|
|
|
|
2023-12-27 15:44:19 -07:00
|
|
|
|
fn main() {
|
|
|
|
|
let argv = args().collect::<Vec<String>>();
|
2024-01-15 16:30:00 -07:00
|
|
|
|
let mut d = '␞';
|
|
|
|
|
let mut arg_parser = Parser::new(&argv, "d:");
|
|
|
|
|
|
|
|
|
|
while let Some(opt) = arg_parser.next() {
|
|
|
|
|
match opt {
|
|
|
|
|
Ok(Opt('d', Some(arg))) => {
|
|
|
|
|
let arg_char = arg.chars().collect::<Vec<char>>();
|
|
|
|
|
if arg_char.len() > 1 {
|
|
|
|
|
eprintln!("{}: {}: Not a character.", argv[0], arg);
|
|
|
|
|
exit(EX_USAGE);
|
|
|
|
|
} else { d = arg_char[0]; }
|
|
|
|
|
},
|
|
|
|
|
_ => {},
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let index_arg = arg_parser.index();
|
|
|
|
|
let command_arg = arg_parser.index() + 1;
|
2023-12-27 15:44:19 -07:00
|
|
|
|
|
2024-01-15 16:30:00 -07:00
|
|
|
|
argv.get(command_arg).unwrap_or_else(|| {
|
2024-01-15 16:33:51 -07:00
|
|
|
|
eprintln!("Usage: {} [-d delimiter] index command [args...]", argv[0]);
|
2023-12-28 22:42:05 -07:00
|
|
|
|
exit(EX_USAGE);
|
|
|
|
|
});
|
2023-12-27 22:42:50 -07:00
|
|
|
|
|
2024-02-23 21:46:50 -07:00
|
|
|
|
let index = argv[index_arg].parse::<usize>().unwrap_or_else(|e| {
|
2024-03-01 23:10:28 -07:00
|
|
|
|
eprintln!("{}: {}: {}", argv[0], argv[1], e);
|
2023-12-28 22:42:05 -07:00
|
|
|
|
exit(EX_DATAERR);
|
|
|
|
|
});
|
2023-12-27 15:44:19 -07:00
|
|
|
|
|
|
|
|
|
let mut buf = String::new();
|
2024-02-14 22:53:45 -07:00
|
|
|
|
let _ = stdin().read_to_string(&mut buf);
|
2024-01-15 16:30:00 -07:00
|
|
|
|
let mut fields = buf.split(d).collect::<Vec<&str>>();
|
2023-12-27 15:44:19 -07:00
|
|
|
|
|
2024-02-14 22:53:45 -07:00
|
|
|
|
let opts = argv
|
|
|
|
|
.iter()
|
|
|
|
|
.clone()
|
|
|
|
|
.skip(command_arg + 1)
|
|
|
|
|
.collect::<Vec<&String>>();
|
2023-12-27 15:44:19 -07:00
|
|
|
|
|
2024-01-15 16:30:00 -07:00
|
|
|
|
let mut spawned = Command::new(argv.get(command_arg).unwrap())
|
2023-12-27 15:44:19 -07:00
|
|
|
|
.args(opts)
|
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
|
.spawn()
|
2024-02-23 21:46:50 -07:00
|
|
|
|
.unwrap_or_else( |e| {
|
2024-03-01 23:10:28 -07:00
|
|
|
|
eprintln!("{}: {}: {}", argv[0], argv[command_arg], c_error(e));
|
2024-02-14 22:53:45 -07:00
|
|
|
|
exit(EX_UNAVAILABLE);
|
|
|
|
|
});
|
2023-12-27 15:44:19 -07:00
|
|
|
|
|
|
|
|
|
let field = fields.get(index).unwrap_or_else(|| {
|
2023-12-27 22:42:50 -07:00
|
|
|
|
eprintln!(
|
2024-03-01 23:10:28 -07:00
|
|
|
|
"{}: {}: No such index in input",
|
2023-12-27 22:42:50 -07:00
|
|
|
|
argv[0],
|
2024-02-23 21:46:50 -07:00
|
|
|
|
index.to_string(),
|
2023-12-27 22:42:50 -07:00
|
|
|
|
);
|
2023-12-27 22:47:06 -07:00
|
|
|
|
exit(EX_DATAERR);
|
2023-12-27 15:44:19 -07:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if let Some(mut child_stdin) = spawned.stdin.take() {
|
2024-02-14 22:53:45 -07:00
|
|
|
|
let _ = child_stdin.write_all(field.as_bytes());
|
2023-12-27 15:44:19 -07:00
|
|
|
|
drop(child_stdin);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-23 21:46:50 -07:00
|
|
|
|
let output = spawned.wait_with_output().unwrap_or_else(|e| {
|
2024-03-01 23:10:28 -07:00
|
|
|
|
eprintln!("{}: {}: {}", argv[0], argv[command_arg], c_error(e));
|
2024-02-14 22:53:45 -07:00
|
|
|
|
exit(EX_IOERR);
|
|
|
|
|
});
|
2023-12-27 15:44:19 -07:00
|
|
|
|
|
2024-02-14 23:05:39 -07:00
|
|
|
|
let mut replace = output.stdout.clone();
|
|
|
|
|
|
|
|
|
|
if replace.pop() != Some(b'\n') { replace = output.stdout; }
|
|
|
|
|
|
2024-02-23 21:46:50 -07:00
|
|
|
|
let new_field = String::from_utf8(replace).unwrap_or_else(|e| {
|
2024-03-01 23:10:28 -07:00
|
|
|
|
eprintln!("{}: {}: {}", argv[0], argv[command_arg], e);
|
2024-02-14 22:53:45 -07:00
|
|
|
|
exit(EX_IOERR);
|
|
|
|
|
});
|
2023-12-27 15:44:19 -07:00
|
|
|
|
|
2024-02-14 23:05:39 -07:00
|
|
|
|
fields[index] = &new_field;
|
2023-12-27 15:44:19 -07:00
|
|
|
|
|
2024-02-23 21:46:50 -07:00
|
|
|
|
stdout().write_all(
|
|
|
|
|
fields.join(&d.to_string()).as_bytes()
|
2024-03-01 23:10:28 -07:00
|
|
|
|
).unwrap_or_else(|e| {
|
|
|
|
|
eprintln!("{}: {}", argv[0], c_error(e));
|
2024-02-23 21:46:50 -07:00
|
|
|
|
exit(EX_IOERR);
|
|
|
|
|
});
|
2023-12-27 15:44:19 -07:00
|
|
|
|
}
|