Merge branch 'dj-formatting' of git.tebibyte.media:bonsai/harakit into dj-formatting

This commit is contained in:
dtb
2024-07-28 21:26:08 -06:00
5 changed files with 39 additions and 25 deletions

View File

@@ -165,7 +165,6 @@ int main(int argc, char *argv[]) {
size_t i; /* side of io being modified */
char noerror; /* 0=exits (default) 1=retries on partial reads or writes */
struct Io io[2 /* { in, out } */];
program_name = (argv[0] == NULL ? program_name : argv[0]);
/* Set defaults. */
align = -1;

View File

@@ -42,12 +42,12 @@ fn main() {
while let Some(opt) = argv.getopt("d:") {
match opt.opt() {
Ok(_) => {
Ok("d") => {
/* delimiter */
d = opt.arg().unwrap();
optind = opt.ind();
},
Err(_) => {
_ => {
eprintln!("{}", usage);
exit(EX_USAGE);
}
@@ -70,13 +70,16 @@ fn main() {
});
let mut buf = String::new();
let _ = stdin().read_to_string(&mut buf);
if let Err(e) = stdin().read_to_string(&mut buf) {
eprintln!("{}: {}", argv[0], e.strerror());
exit(EX_IOERR);
};
/* split the buffer by the delimiter (by default, '\u{1E}') */
let mut fields = buf.split(&d).collect::<Vec<&str>>();
/* collect arguments for the operator command */
let opts = argv
let command_args = argv
.iter()
.clone()
.skip(command_arg + 1) /* skip the command name */
@@ -84,7 +87,7 @@ fn main() {
/* spawn the command to operate on the field */
let mut spawned = Command::new(operator)
.args(opts) /* spawn with the specified arguments */
.args(command_args) /* spawn with the specified arguments */
.stdin(Stdio::piped())
.stdout(Stdio::piped()) /* piped stdout to handle output ourselves */
.spawn()
@@ -117,8 +120,13 @@ fn main() {
/* get the output with which the original field will be replaced */
let mut replace = output.stdout.clone();
/* as long as its not a newline, set the replacement to the output */
if replace.pop() != Some(b'\n') { replace = output.stdout; }
/* pop trailing newline out if the input did not contain it */
if fields[index].chars().last() != Some('\n') /* no newline */
&& replace.pop() != Some(b'\n') { /* pop last char of replacement */
/* restore replacement to original command output if popped char was not
* a newline */
replace = output.stdout;
}
/* convert the output of the program to UTF-8 */
let new_field = String::from_utf8(replace).unwrap_or_else(|e| {

View File

@@ -44,22 +44,22 @@ int main(int argc, char *argv[]) {
size_t i;
unsigned char mode;
int r; /* reference integer */
program_name = (argv[0] == NULL ? program_name : argv[0]);
mode = 0;
if (argc == 0) { return usage(program_name); }
if (argc < 3) {
return usage(argv[0] == NULL ? program_name : argv[0]);
}
while ((c = getopt(argc, argv, "egl")) != -1) {
switch (c) {
case 'e': mode |= EQUAL; break;
case 'g': mode |= GREATER; break;
case 'l': mode |= LESSER; break;
default: return usage(program_name);
default: return usage(argv[0]);
}
}
if (optind + 2 /* ref cmp */ > argc) { return usage(program_name); }
if (optind + 2 /* ref cmp */ > argc) { return usage(argv[0]); }
i = optind;
@@ -71,7 +71,7 @@ int main(int argc, char *argv[]) {
fprintf(
stderr,
"%s: argument #%d: Invalid integer\n",
program_name,
argv[0],
(int)i
);
return EX_USAGE;

View File

@@ -39,16 +39,15 @@ usage(char *argv0) {
int main(int argc, char *argv[]) {
char sel[(sizeof opts) / (sizeof *opts)];
program_name = (argv[0] == NULL ? program_name : argv[0]);
if (argc < 2) { return usage(program_name); }
if (argc < 2) { return usage(argv[0] == NULL ? program_name : argv[0]); }
{ /* option parsing */
char *p;
memset(sel, '\0', sizeof sel);
for (int c; (c = getopt(argc, argv, opts)) != -1;) {
if ((p = strchr(opts, c)) == NULL) { return usage(program_name); }
if ((p = strchr(opts, c)) == NULL) { return usage(argv[0]); }
else { sel[p - opts] = c; }
}
@@ -62,7 +61,7 @@ int main(int argc, char *argv[]) {
}
}
if (optind == argc) { return usage(program_name); }
if (optind == argc) { return usage(argv[0]); }
for (argv += optind ; *argv != NULL; ++argv) {
struct stat buf;