From 3355cb3dc3705b9fa31841cde1b6f405746f9098 Mon Sep 17 00:00:00 2001 From: emma Date: Wed, 27 Dec 2023 22:42:50 -0700 Subject: [PATCH] GNUmakefile, fop(1): added sysexits support --- GNUmakefile | 16 +++++++++++----- src/fop.rs | 23 ++++++++++++++++------- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/GNUmakefile b/GNUmakefile index 64b433f..b357cd4 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -20,8 +20,9 @@ CC=cc CFLAGS=-O3 -Lbuild/lib -idirafter include RUSTC=rustc +nightly -RUSTCFLAGS=-Zlocation-detail=none -Copt-level=z -Ccodegen-units=1 -Cpanic=abort - -Clto=y -Cstrip=symbols -Ctarget-cpu=native +RUSTCFLAGS=-Zlocation-detail=none -Copt-level=z -Ccodegen-units=1 \ + -Cpanic=abort -Clto=y -Cstrip=symbols -Ctarget-cpu=native \ + --extern sysexits=build/o/libsysexits.rlib ifeq ($(CC), gcc) CFLAGS=-O3 -s -Wl,-z,noseparate-code,-z,nosectionheader -flto -Lbuild/lib \ @@ -39,7 +40,7 @@ endif build: build_dir false intcmp scrut str strcmp true build_dir: - mkdir -p build/o build/lib build/bin + mkdir -p build/bin build/lib build/o clean: rm -rf build/ @@ -56,10 +57,16 @@ test: build tests/cc-compat.sh tests/posix-compat.sh +sysexits: build_dir + bindgen --default-macro-constant-type signed \ + "$$(printf '#include \n' | cpp -M -idirafter include - \ + | sed 's/ /\n/g' | grep sysexits.h)" \ + | $(RUSTC) $(RUSTCFLAGS) --crate-type lib -o build/o/libsysexits.rlib - + false: src/false.rs build_dir $(RUSTC) $(RUSTCFLAGS) -o build/bin/false src/false.rs -fop: src/fop.rs build_dir +fop: src/fop.rs build_dir sysexits $(RUSTC) $(RUSTCFLAGS) -o build/bin/fop src/fop.rs intcmp: src/intcmp.c build_dir @@ -76,4 +83,3 @@ strcmp: src/strcmp.c build_dir true: src/true.rs build_dir $(RUSTC) $(RUSTCFLAGS) -o build/bin/true src/true.rs - diff --git a/src/fop.rs b/src/fop.rs index 9c25463..287660c 100644 --- a/src/fop.rs +++ b/src/fop.rs @@ -18,13 +18,18 @@ use std::{ env::args, - io::{ Read, stdin, Write }, + io::{ stdin, Write }, process::{ Command, exit, Stdio }, }; +extern crate sysexits; +use sysexits::EX_USAGE; + fn main() { let argv = args().collect::>(); + let usage = format!("Usage: {} index command [args...]", argv[0]); + let index = match argv.get(1) { Some(i) => { i.parse::().unwrap_or_else(|_| { @@ -33,18 +38,18 @@ fn main() { }) }, None => { - eprintln!("Usage: {} index command args...", argv[0]); - exit(1); + eprintln!("{}", usage); + exit(EX_USAGE); }, }; let mut buf = String::new(); - stdin().read_to_string(&mut buf).unwrap(); + stdin().read_line(&mut buf).unwrap(); let mut fields = buf.split('␞').collect::>(); argv.get(2).unwrap_or_else(|| { - eprintln!("Usage: {} index command args...", argv[0]); - exit(1); + eprintln!("{}", usage); + exit(EX_USAGE); }); let opts = argv.iter().clone().skip(3).collect::>(); @@ -57,7 +62,11 @@ fn main() { .unwrap(); let field = fields.get(index).unwrap_or_else(|| { - eprintln!("{}: {}: No such index in input.", argv[0], index.to_string()); + eprintln!( + "{}: {}: No such index in input.", + argv[0], + index.to_string() + ); exit(1); });