forked from bonsai/harakit
Compare commits
103 Commits
fix-instal
...
0.10.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
5b364e104e
|
|||
|
d3470233ea
|
|||
|
3e39739e88
|
|||
|
0f5247e722
|
|||
|
7fb68a2c7a
|
|||
|
b95b198923
|
|||
|
af53375ff2
|
|||
|
4e040e0021
|
|||
|
d768a257a3
|
|||
|
5b47936477
|
|||
|
8e0eaeab38
|
|||
|
6d7522be01
|
|||
|
228a0111c7
|
|||
|
a30152d783
|
|||
|
4993c53080
|
|||
|
6f98fdd1d4
|
|||
|
fb2a164507
|
|||
|
4cb5e8e2b0
|
|||
|
885f167afc
|
|||
|
2ec04c0564
|
|||
|
a39772d7a1
|
|||
|
258881dbb2
|
|||
|
b35f87bd27
|
|||
|
e7ee75b03f
|
|||
|
a90c9b7998
|
|||
|
fb929e63f5
|
|||
|
791db2ed6d
|
|||
|
870548b46b
|
|||
|
af0c472173
|
|||
|
6715e0a50f
|
|||
|
0d97f81f49
|
|||
|
b68f13acdc
|
|||
|
42d268319e
|
|||
|
f7108245ea
|
|||
|
34b9519e03
|
|||
|
90ca10990f
|
|||
|
f966233e19
|
|||
|
5f8e90b592
|
|||
|
5debb9d954
|
|||
|
942e284f93
|
|||
|
4870f4679c
|
|||
|
32537895cb
|
|||
|
ab4d732aaf
|
|||
|
e246290bff
|
|||
|
271510476c
|
|||
|
caff26209e
|
|||
|
9095602296
|
|||
|
7cc5cf162c
|
|||
|
601954a0f6
|
|||
|
b7bd6a6e7b
|
|||
|
5e1749ca43
|
|||
|
f99cc797e6
|
|||
|
a1b29bb6ed
|
|||
|
c237af0e37
|
|||
|
48c6ad1252
|
|||
|
0cbfb5c6cc
|
|||
|
04923fb1ad
|
|||
|
c444198efc
|
|||
|
28becbafbc
|
|||
|
52def0a32d
|
|||
|
91e129b813
|
|||
|
49a3bb9f30
|
|||
|
622c13021d
|
|||
|
ba7bd26fe6
|
|||
|
d25c8a64f2
|
|||
| 14fd23ddba | |||
|
612067890f
|
|||
|
88a66bcc01
|
|||
|
0c2223d4fb
|
|||
|
75ead441ec
|
|||
|
f89a686d3c
|
|||
|
cb12a5b8fc
|
|||
|
6f4756bc76
|
|||
|
e16e7bd322
|
|||
|
5df9c33d70
|
|||
|
9ed70e9648
|
|||
|
b74bbd6c92
|
|||
|
3355cb3dc3
|
|||
|
5d9f6f3245
|
|||
|
7eda8bb721
|
|||
|
36c0c9193c
|
|||
|
1dff7d7e18
|
|||
|
d20a1da6f8
|
|||
|
cbe8f5f8e8
|
|||
|
83d6f0b2c2
|
|||
|
94ba336ee4
|
|||
|
74d5b65f78
|
|||
|
3249a83861
|
|||
|
10647a01fd
|
|||
|
a675386fe3
|
|||
|
a54a634516
|
|||
|
de304b7ef7
|
|||
|
8a8558442e
|
|||
|
324328d2cc
|
|||
|
8e330d8a63
|
|||
|
d4ee9bb93b
|
|||
|
52d150b02b
|
|||
|
1bc574eafa
|
|||
|
cee6575a57
|
|||
|
47f673ef45
|
|||
|
cfdc2821b2
|
|||
|
c12f382608
|
|||
|
48acf30cd4
|
17
.editorconfig
Normal file
17
.editorconfig
Normal file
@@ -0,0 +1,17 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
indent_style = tab
|
||||
indent_size = 4
|
||||
insert_final_newline = true
|
||||
|
||||
[*akefile]
|
||||
indent_size = 2
|
||||
|
||||
[*.sh]
|
||||
indent_size = 2
|
||||
|
||||
[configure]
|
||||
indent_size = 2
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
build/
|
||||
dist/
|
||||
|
||||
@@ -91,7 +91,7 @@ notice:
|
||||
*/
|
||||
|
||||
When writing code, make sure lines never exceed 80 characters in width when
|
||||
using two-character-wide tabs.
|
||||
using four-character-wide indentation steps.
|
||||
|
||||
For usage text and help messages, please do not implement a -h option. Just
|
||||
print usage information when any erroneous option is specified. Follow the
|
||||
|
||||
73
GNUmakefile
73
GNUmakefile
@@ -1,73 +0,0 @@
|
||||
# Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
# Copyright (c) 2023 DTB <trinity@trinity.moe>
|
||||
# SPDX-License-Identifier: FSFAP
|
||||
#
|
||||
# Copying and distribution of this file, with or without modification, are
|
||||
# permitted in any medium without royalty provided the copyright notice and this
|
||||
# notice are preserved. This file is offered as-is, without any warranty.
|
||||
|
||||
# If we want to use POSIX make we can’t use ifeq
|
||||
# .POSIX:
|
||||
# .PRAGMA: posix_202x
|
||||
.PHONY: clean
|
||||
.PHONY: install
|
||||
.PHONY: test
|
||||
|
||||
PREFIX=/usr/local
|
||||
CC=cc
|
||||
CFLAGS=-O3 -Lbuild
|
||||
|
||||
ifeq ($(CC), gcc)
|
||||
CFLAGS=-O3 -s -Wl,-z,noseparate-code,-z,nosectionheader -flto -Lbuild
|
||||
endif
|
||||
|
||||
ifeq ($(CC), clang)
|
||||
CFLAGS=-O3 -Wall -Lbuild
|
||||
endif
|
||||
|
||||
ifeq ($(CC), tcc)
|
||||
CFLAGS=-O3 -s -Wl -flto -Lbuild
|
||||
endif
|
||||
|
||||
build: build_dir false intcmp scrut str strcmp true
|
||||
|
||||
build_dir:
|
||||
mkdir -p build
|
||||
|
||||
clean:
|
||||
rm -rf build/
|
||||
|
||||
install: build
|
||||
mkdir -p $(PREFIX)/bin $(PREFIX)/lib $(PREFIX)/man/man1 $(PREFIX)/man/man3
|
||||
cp -f build/*.o $(PREFIX)/lib/
|
||||
cp -f build/*.so $(PREFIX)/lib/
|
||||
cp -f build/* $(PREFIX)/bin/
|
||||
cp -f docs/*.1 $(PREFIX)/man/man1/
|
||||
cp -f docs/*.3 $(PREFIX)/man/man3/
|
||||
|
||||
test: build
|
||||
tests/cc-compat.sh
|
||||
tests/posix-compat.sh
|
||||
|
||||
false: src/false.c build_dir
|
||||
$(CC) $(CFLAGS) -o build/false src/false.c
|
||||
|
||||
intcmp: src/intcmp.c build_dir
|
||||
$(CC) $(CFLAGS) -o build/intcmp src/intcmp.c
|
||||
|
||||
scrut: src/scrut.c libfileis build_dir
|
||||
$(CC) $(CFLAGS) -lfileis -o build/scrut src/scrut.c
|
||||
|
||||
str: src/str.c build_dir
|
||||
$(CC) $(CFLAGS) -o build/str src/str.c
|
||||
|
||||
strcmp: src/strcmp.c build_dir
|
||||
$(CC) $(CFLAGS) -o build/strcmp src/strcmp.c
|
||||
|
||||
true: src/true.c build_dir
|
||||
$(CC) $(CFLAGS) -o build/true src/true.c
|
||||
|
||||
libfileis: src/libfileis.c src/libfileis.h build_dir
|
||||
$(CC) $(CFLAGS) -c -fPIC -o build/libfileis.o src/libfileis.c
|
||||
$(CC) -shared -o build/libfileis.so build/libfileis.o
|
||||
|
||||
107
Makefile
Normal file
107
Makefile
Normal file
@@ -0,0 +1,107 @@
|
||||
# Copyright (c) 2023–2024 Emma Tebibyte <emma@tebibyte.media>
|
||||
# Copyright (c) 2023–2024 DTB <trinity@trinity.moe>
|
||||
# Copyright (c) 2023 Sasha Koshka <sashakoshka@tebibyte.media>
|
||||
# SPDX-License-Identifier: FSFAP
|
||||
#
|
||||
# Copying and distribution of this file, with or without modification, are
|
||||
# permitted in any medium without royalty provided the copyright notice and this
|
||||
# notice are preserved. This file is offered as-is, without any warranty.
|
||||
|
||||
.POSIX:
|
||||
.PRAGMA: posix_202x # future POSIX standard support à la pdpmake(1)
|
||||
.PRAGMA: command_comment # breaks without this?
|
||||
|
||||
PREFIX=/usr/local
|
||||
|
||||
CC=cc
|
||||
RUSTC=rustc
|
||||
|
||||
.PHONY: all
|
||||
all: dj false fop intcmp rpn scrut str strcmp true
|
||||
|
||||
build:
|
||||
# keep build/include until bindgen(1) has stdin support
|
||||
# https://github.com/rust-lang/rust-bindgen/issues/2703
|
||||
mkdir -p build/bin build/include build/lib build/o build/test
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -rf build/ dist/
|
||||
|
||||
dist: all
|
||||
mkdir -p dist/bin dist/share/man/man1
|
||||
cp build/bin/* dist/bin/
|
||||
cp docs/*.1 dist/share/man/man1/
|
||||
|
||||
.PHONY: install
|
||||
install: dist
|
||||
mkdir -p $(PREFIX)
|
||||
cp -r dist/* $(PREFIX)/
|
||||
|
||||
.PHONY: test
|
||||
test: build
|
||||
tests/posix-compat.sh
|
||||
$(RUSTC) --test src/getopt-rs/lib.rs -o build/test/getopt
|
||||
|
||||
build/o/libsysexits.rlib: build
|
||||
# bandage solution until bindgen(1) gets stdin support
|
||||
printf '#define EXIT_FAILURE 1\n' | cat - include/sysexits.h \
|
||||
> build/include/sysexits.h
|
||||
bindgen --default-macro-constant-type signed --use-core --formatter=none \
|
||||
"$$(printf '#include <sysexits.h>\n' \
|
||||
| cpp -M -idirafter "build/include" - \
|
||||
| sed 's/ /\n/g' | grep sysexits.h)" \
|
||||
| $(RUSTC) $(RUSTFLAGS) --crate-type lib -o build/o/libsysexits.rlib -
|
||||
|
||||
build/o/libgetopt.rlib: src/getopt-rs/lib.rs
|
||||
$(RUSTC) $(RUSTCFLAGS) --crate-type=lib --crate-name=getopt \
|
||||
-o build/o/libgetopt.rlib src/getopt-rs/lib.rs
|
||||
|
||||
.PHONY: dj
|
||||
dj: build/bin/dj
|
||||
build/bin/dj: src/dj.c build
|
||||
$(CC) $(CFLAGS) -o $@ src/dj.c
|
||||
|
||||
.PHONY: false
|
||||
false: build/bin/false
|
||||
build/bin/false: src/false.c build
|
||||
$(CC) $(CFLAGS) -o $@ src/false.c
|
||||
|
||||
.PHONY: fop
|
||||
fop: build/bin/fop
|
||||
build/bin/fop: src/fop.rs build build/o/libgetopt.rlib build/o/libsysexits.rlib
|
||||
$(RUSTC) $(RUSTFLAGS) --extern getopt=build/o/libgetopt.rlib \
|
||||
--extern sysexits=build/o/libsysexits.rlib \
|
||||
-o $@ src/fop.rs
|
||||
|
||||
.PHONY: intcmp
|
||||
intcmp: build/bin/intcmp
|
||||
build/bin/intcmp: src/intcmp.c build
|
||||
$(CC) $(CFLAGS) -o $@ src/intcmp.c
|
||||
|
||||
.PHONY: rpn
|
||||
rpn: build/bin/rpn
|
||||
build/bin/rpn: src/rpn.rs build build/o/libsysexits.rlib
|
||||
$(RUSTC) $(RUSTFLAGS) \
|
||||
--extern sysexits=build/o/libsysexits.rlib \
|
||||
-o $@ src/rpn.rs
|
||||
|
||||
.PHONY: scrut
|
||||
scrut: build/bin/scrut
|
||||
build/bin/scrut: src/scrut.c build
|
||||
$(CC) $(CFLAGS) -o $@ src/scrut.c
|
||||
|
||||
.PHONY: str
|
||||
str: build/bin/str
|
||||
build/bin/str: src/str.c build
|
||||
$(CC) $(CFLAGS) -o $@ src/str.c
|
||||
|
||||
.PHONY: strcmp
|
||||
strcmp: build/bin/strcmp
|
||||
build/bin/strcmp: src/strcmp.c build
|
||||
$(CC) $(CFLAGS) -o $@ src/strcmp.c
|
||||
|
||||
.PHONY: true
|
||||
true: build/bin/true
|
||||
build/bin/true: src/true.c build
|
||||
$(CC) $(CFLAGS) -o $@ src/true.c
|
||||
160
docs/dj.1
Normal file
160
docs/dj.1
Normal file
@@ -0,0 +1,160 @@
|
||||
.\" Copyright (c) 2024 DTB <trinity@trinity.moe>
|
||||
.\"
|
||||
.\" This work is licensed under CC BY-SA 4.0. To see a copy of this license,
|
||||
.\" visit <http://creativecommons.org/licenses/by-sa/4.0/>.
|
||||
|
||||
.TH dj 1
|
||||
|
||||
.SH NAME
|
||||
|
||||
dj \(en disk jockey
|
||||
|
||||
.SH SYNOPSIS
|
||||
|
||||
dj
|
||||
.RB ( -AdHnq )
|
||||
.RB ( -a
|
||||
.RB [ byte ])
|
||||
.RB ( -c
|
||||
.RB [ count ])
|
||||
|
||||
.RB ( -i
|
||||
.R [
|
||||
.B input file
|
||||
.R ])
|
||||
.RB ( -b
|
||||
.R [
|
||||
.B input block size
|
||||
.R ])
|
||||
.RB ( -s
|
||||
.R [
|
||||
.B input offset
|
||||
.R ])
|
||||
|
||||
.RB ( -o
|
||||
.R [
|
||||
.B output file
|
||||
.R ])
|
||||
.RB ( -B
|
||||
.R [
|
||||
.B output block size
|
||||
.R ])
|
||||
.RB ( -S
|
||||
.R [
|
||||
.B output offset
|
||||
.R ])
|
||||
|
||||
.SH USAGE
|
||||
|
||||
The
|
||||
.B -i
|
||||
option takes a path as an argument to open and use in place of standard input.
|
||||
The
|
||||
.B -o
|
||||
option does the same in place of standard output. Dj does not truncate output
|
||||
files and instead writes over the bytes in the existing file.
|
||||
.PP
|
||||
The
|
||||
.B -b
|
||||
option takes a numeric argument as the size in bytes of the input buffer and
|
||||
the
|
||||
.B -B
|
||||
option does the same for the output buffer, the default for both being 1024
|
||||
bytes, or one kibibyte (KiB).
|
||||
.PP
|
||||
The
|
||||
.B -s
|
||||
option takes a numeric argument as the number of bytes to skip into the input
|
||||
before starting to read, and the
|
||||
.B -S
|
||||
option skips a number of bytes through the output before starting to write from
|
||||
the input. If the input is a stream the bytes are read and discarded. If the
|
||||
output is a stream, nul characters are printed.
|
||||
.PP
|
||||
The
|
||||
.B -a
|
||||
option takes one argument of one byte in length and pads the input buffer with
|
||||
that byte in the event that a read doesn't fill the input buffer, and the
|
||||
.B -A
|
||||
option takes no arguments and pads with nuls.
|
||||
The
|
||||
.B -c
|
||||
option specifies an amount of reads to make, and if 0 (the default) dj will
|
||||
continue reading until a partial or empty read.
|
||||
.PP
|
||||
On a partial or empty read, dj prints a diagnostic message (unless the
|
||||
.B -q
|
||||
option is specified) and exits (unless the
|
||||
.B -n
|
||||
option is specified, in which case only two consecutive empty reads will cause
|
||||
dj to exit).
|
||||
At exit, usage statistics are printed unless the option
|
||||
.B -q
|
||||
is specified a second time. The
|
||||
.B -H
|
||||
option will make these diagnostics human-readable.
|
||||
|
||||
.SH DIAGNOSTICS
|
||||
|
||||
The
|
||||
.B -d
|
||||
option prints all information, user-specified or otherwise, before program
|
||||
execution.
|
||||
.PP
|
||||
When dj exits, by default statistics are printed for input and output to
|
||||
standard error in the following format:
|
||||
.PP
|
||||
.R {records read} {ASCII unit separator} {partial records read}
|
||||
.R {ASCII record separator} {records written} {ASCII unit separator}
|
||||
.R {partial records written} {ASCII group separator} {bytes read}
|
||||
.R {ASCII record separator} {bytes written} {ASCII file separator}
|
||||
.PP
|
||||
If the
|
||||
.B -H
|
||||
option is specified dj instead uses this following format:
|
||||
.PP
|
||||
.R {records read} '+' {partial records read} '>' {records written}
|
||||
.R '+' {partial records written} ';' {bytes read} '>' {bytes written}
|
||||
.R {ASCII line feed}
|
||||
.PP
|
||||
The
|
||||
.B -q
|
||||
option suppresses error messages which print when a read or write is partial or
|
||||
empty and when used twice suppresses diagnostic output entirely.
|
||||
.PP
|
||||
In non-recoverable errors that don't pertain to dj's read-write cycle, a
|
||||
diagnostic message is printed and dj exits with the appropriate sysexits(3)
|
||||
status.
|
||||
|
||||
.SH BUGS
|
||||
|
||||
If
|
||||
.B -n
|
||||
is specified along with a specified count, actual byte output may be lower than
|
||||
expected (the product of the count multiplied by the input block size). If the
|
||||
.B -a
|
||||
or
|
||||
.B -A
|
||||
options are used this could make data written nonsensical.
|
||||
.PP
|
||||
Many lowercase options have capitalized variants and vice-versa which can be
|
||||
confusing. Capitalized options tend to affect output or are more intense
|
||||
versions of lowercase options.
|
||||
|
||||
.SH RATIONALE
|
||||
|
||||
Dj was modeled after the dd utility specified in POSIX but adds additional
|
||||
features: typical option formatting, allowing seeks to be specified in bytes
|
||||
rather than in blocks, allowing arbitrary bytes as padding, and printing in a
|
||||
format that's easy to parse for machines. It also neglects character
|
||||
conversion, which may be dd's original intent but is irrelevant to its modern
|
||||
use.
|
||||
|
||||
.SH COPYRIGHT
|
||||
|
||||
Copyright (C) 2023 DTB. License AGPLv3+: GNU AGPL version 3 or later
|
||||
<https://gnu.org/licenses/agpl.html>.
|
||||
|
||||
.SH SEE ALSO
|
||||
|
||||
dd(1)
|
||||
@@ -1,4 +1,4 @@
|
||||
.\" Copyright (c) 2022 DTB <trinity@trinity.moe>
|
||||
.\" Copyright (c) 2022, 2024 DTB <trinity@trinity.moe>
|
||||
.\" Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
.\"
|
||||
.\" This work is licensed under CC BY-SA 4.0. To see a copy of this license,
|
||||
@@ -32,4 +32,4 @@ This work is marked with CC0 1.0. To see a copy of this license, visit
|
||||
|
||||
.SH SEE ALSO
|
||||
|
||||
true(1)
|
||||
true(1p)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
.\" Copyright (c) 2023 DTB <trinity@trinity.moe>
|
||||
.\" Copyright (c) 2023–2024 DTB <trinity@trinity.moe>
|
||||
.\" Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
.\"
|
||||
.\" This work is licensed under CC BY-SA 4.0. To see a copy of this license,
|
||||
@@ -75,4 +75,4 @@ Copyright © 2023 DTB. License AGPLv3+: GNU AGPL version 3 or later
|
||||
|
||||
.SH SEE ALSO
|
||||
|
||||
strcmp(1), scrut(1), str(1), test(1)
|
||||
strcmp(1), scrut(1), str(1), test(1p)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
.\" Copyright (c) 2023 DTB <trinity@trinity.moe>
|
||||
.\" Copyright (c) 2023–2024 DTB <trinity@trinity.moe>
|
||||
.\" Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
.\"
|
||||
.\" This work is licensed under CC BY-SA 4.0. To see a copy of this license,
|
||||
@@ -62,7 +62,7 @@ Copyright © 2023 DTB. License AGPLv3+: GNU AGPL version 3 or later
|
||||
|
||||
.SH SEE ALSO
|
||||
|
||||
cat(1), cat-v(1)
|
||||
cat(1p), cat-v(1)
|
||||
|
||||
.I UNIX Style, or cat -v Considered Harmful
|
||||
by Rob Pike
|
||||
|
||||
70
docs/rpn.1
Normal file
70
docs/rpn.1
Normal file
@@ -0,0 +1,70 @@
|
||||
.\" Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
|
||||
.\" Copyright (c) 2024 DTB <trinity@trinity.moe>
|
||||
.\"
|
||||
.\" This work is licensed under CC BY-SA 4.0. To see a copy of this license,
|
||||
.\" visit <http://creativecommons.org/licenses/by-sa/4.0/>.
|
||||
|
||||
.TH rpn 1
|
||||
|
||||
.SH NAME
|
||||
|
||||
rpn \(en reverse polish notation evaluation
|
||||
|
||||
.SH SYNOPSIS
|
||||
|
||||
rpn
|
||||
.RB [numbers...]\ [operators...]
|
||||
|
||||
.SH DESCRIPTION
|
||||
|
||||
Rpn evaluates reverse polish notation expressions either read from the standard
|
||||
input or parsed from provided arguments. See the STANDARD INPUT section.
|
||||
|
||||
Upon evaluation, rpn will print the resulting number on the stack to the
|
||||
standard output. Any further specified numbers will be placed at the end of the
|
||||
stack.
|
||||
|
||||
For information on for reverse polish notation syntax, see rpn(7).
|
||||
|
||||
.SH STANDARD INPUT
|
||||
|
||||
If arguments are passed to rpn, it interprets them as an expression to be
|
||||
evaluated. Otherwise, it reads whitespace-delimited numbers and operations from
|
||||
the standard input.
|
||||
|
||||
.SH DIAGNOSTICS
|
||||
|
||||
If encountering a syntax error, rpn will exit with the appropriate error code
|
||||
as defined by sysexits.h(3) and print an error message.
|
||||
|
||||
.SH CAVEATS
|
||||
|
||||
Due to precision constraints and the way floats are represented in accordance
|
||||
with the IEEE Standard for Floating Point Arithmetic (IEEE 754), floating-point
|
||||
arithmetic has rounding errors. This is somewhat curbed by using the
|
||||
machine epsilon as provided by the Rust standard library to which to round
|
||||
numbers. Because of this, variation is expected in the number of decimal places
|
||||
rpn can handle based on the platform and hardware of any given machine.
|
||||
|
||||
.SH RATIONALE
|
||||
|
||||
An infix notation calculation utility, bc(1p), is included in the POSIX
|
||||
standard, but does not accept expressions as arguments; in scripts, any
|
||||
predefined, non-interactive input must be piped into the program. A dc(1)
|
||||
pre-dates the standardized bc(1p), the latter originally being a preprocessor
|
||||
for the former, and was included in UNIX v2 onward. While it implements reverse
|
||||
polish notation, it still suffers from being unable to accept an expression as
|
||||
an argument.
|
||||
|
||||
.SH AUTHOR
|
||||
|
||||
Written by Emma Tebibyte <emma@tebibyte.media>.
|
||||
|
||||
.SH COPYRIGHT
|
||||
|
||||
Copyright (c) 2024 Emma Tebibyte. License AGPLv3+: GNU AGPL version 3 or later
|
||||
<https://gnu.org/licenses/agpl.html>.
|
||||
|
||||
.SH SEE ALSO
|
||||
|
||||
bc(1p), dc(1), rpn(7), IEEE 754
|
||||
@@ -1,4 +1,4 @@
|
||||
.\" Copyright (c) 2023 DTB <trinity@trinity.moe>
|
||||
.\" Copyright (c) 2023–2024 DTB <trinity@trinity.moe>
|
||||
.\" Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
.\"
|
||||
.\" This work is licensed under CC BY-SA 4.0. To see a copy of this license,
|
||||
@@ -55,4 +55,4 @@ Copyright © 2023 DTB. License AGPLv3+: GNU AGPL version 3 or later
|
||||
|
||||
.SH SEE ALSO
|
||||
|
||||
ctype(3), strcmp(1), ascii(7)
|
||||
ctype(3p), strcmp(1), ascii(7)
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
.\" Copyright (c) 2023 DTB <trinity@trinity.moe>
|
||||
.\" Copyright (c) 2023–2024 DTB <trinity@trinity.moe>
|
||||
.\" Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
.\"
|
||||
.\" This work is licensed under CC BY-SA 4.0. To see a copy of this license,
|
||||
.\" visit <http://creativecommons.org/licenses/by-sa/4.0/>.
|
||||
|
||||
|
||||
.TH STRCMP 1
|
||||
|
||||
.SH NAME
|
||||
@@ -60,4 +59,4 @@ Copyright © 2023 DTB. License AGPLv3+: GNU AGPL version 3 or later
|
||||
|
||||
.SH SEE ALSO
|
||||
|
||||
strcmp(3), intcmp(1), scrut(1), test(1)
|
||||
strcmp(3), intcmp(1), scrut(1), test(1p)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
.\" Copyright (c) 2022 DTB <trinity@trinity.moe>
|
||||
.\" Copyright (c) 2022, 2024 DTB <trinity@trinity.moe>
|
||||
.\" Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
.\"
|
||||
.\" This work is licensed under CC BY-SA 4.0. To see a copy of this license,
|
||||
@@ -32,4 +32,4 @@ This work is marked with CC0 1.0. To see a copy of this license, visit
|
||||
|
||||
.SH SEE ALSO
|
||||
|
||||
false(1)
|
||||
false(1p)
|
||||
|
||||
38
include/sysexits.h
Normal file
38
include/sysexits.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright (c) 2023 DTB <trinity@trinity.moe>
|
||||
* Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
* SPDX-License-Identifier: CC0
|
||||
*
|
||||
* This work is marked with CC0 1.0. To view a copy of this license, visit
|
||||
* <http://creativecommons.org/publicdomain/zero/1.0>.
|
||||
*/
|
||||
|
||||
#ifndef _SYSEXITS_H
|
||||
#define _SYSEXITS_H 2
|
||||
|
||||
#ifndef EXIT_FAILURE
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
#define EX_OK EXIT_SUCCESS
|
||||
|
||||
#define EX__BASE EXIT_FAILURE /* base value for error messages */
|
||||
#define EX_USAGE EXIT_FAILURE /* command line usage error */
|
||||
#define EX_DATAERR EXIT_FAILURE /* data format error */
|
||||
#define EX_NOINPUT EXIT_FAILURE /* cannot open input */
|
||||
#define EX_NOUSER EXIT_FAILURE /* addressee unknown */
|
||||
#define EX_NOHOST EXIT_FAILURE /* host name unknown */
|
||||
#define EX_UNAVAILABLE EXIT_FAILURE /* service unavailable */
|
||||
#define EX_SOFTWARE EXIT_FAILURE /* internal software error */
|
||||
#define EX_OSERR EXIT_FAILURE /* system error (e.g., can't fork) */
|
||||
#define EX_OSFILE EXIT_FAILURE /* critical OS file missing */
|
||||
#define EX_CANTCREAT EXIT_FAILURE /* can't create (user) output file */
|
||||
#define EX_IOERR EXIT_FAILURE /* input/output error */
|
||||
#define EX_TEMPFAIL EXIT_FAILURE /* temp failure; user is invited to retry */
|
||||
#define EX_PROTOCOL EXIT_FAILURE /* remote error in protocol */
|
||||
#define EX_NOPERM EXIT_FAILURE /* permission denied */
|
||||
#define EX_CONFIG EXIT_FAILURE /* configuration error */
|
||||
|
||||
#define EX__MAX EXIT_FAILURE /* maximum listed value */
|
||||
|
||||
#endif
|
||||
452
src/dj.c
Normal file
452
src/dj.c
Normal file
@@ -0,0 +1,452 @@
|
||||
/*
|
||||
* Copyright (c) 2024 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/.
|
||||
*/
|
||||
|
||||
#include <ctype.h> /* isupper(3), tolower(3) */
|
||||
#include <errno.h> /* errno */
|
||||
#include <fcntl.h> /* open(2) */
|
||||
#include <stdio.h> /* fprintf(3), stderr */
|
||||
#include <stdlib.h> /* free(3), malloc(3), strtol(3), size_t */
|
||||
#include <string.h> /* memcpy(3), memmove(3), memset(3) */
|
||||
#include <sysexits.h> /* EX_OK, EX_USAGE */
|
||||
#include <unistd.h> /* close(2), getopt(3), lseek(2), read(2), write(2),
|
||||
* optarg, optind, STDIN_FILENO, STDOUT_FILENO */
|
||||
extern int errno;
|
||||
|
||||
/* dj uses two structures that respectively correspond to the reading and
|
||||
* writing ends of its jockeyed "pipe". User-configurable members are noted
|
||||
* with their relevant options. */
|
||||
struct Io{
|
||||
int bs; /* buffer size (-bB) */
|
||||
size_t bufuse; /* buffer usage */
|
||||
char *buf; /* buffer */
|
||||
int bytes; /* bytes processed */
|
||||
int fd; /* file descriptor */
|
||||
int fl; /* file opening flags */
|
||||
char *fn; /* file name (may be stdin_name or stdout_name) (-io) */
|
||||
int prec; /* partial records processed */
|
||||
int rec; /* records processed */
|
||||
long seek; /* bytes to seek/skip (will be 0 after skippage) (-sS) */
|
||||
} ep[2]; /* "engineered pipe"; also "extended play", for the deejay */
|
||||
|
||||
/* Additionally, the following global variables are used to store user options.
|
||||
*/
|
||||
|
||||
/* (-a) */ static int align; /* Only the lower 8b are used but align is
|
||||
* negative if no alignment is being done. */
|
||||
|
||||
/* (-c) */ static int count; /* 0 if dj(1) runs until no more reads are
|
||||
* possible. */
|
||||
|
||||
/* ASCII field separator delimited statistics */
|
||||
static char *fmt_asv = "%d\037%d\036%d\037%d\035%d\036%d\034";
|
||||
/* human-readable statistics */
|
||||
static char *fmt_human = "%d+%d > %d+%d; %d > %d\n";
|
||||
/* pointer to chosen formatting */
|
||||
/* (-H) */ static char *fmt_output; /* fmt_asv (default) or fmt_human (-H) */
|
||||
|
||||
/* (-dq) */ static char debug; /*
|
||||
* -d increments dj -qq | 0 - no diagnostic output whatsoever
|
||||
* -q decrements dj -q | 1 - typical output without
|
||||
* | notifications on partial reads or
|
||||
* | writes
|
||||
* dj | 2 - typical output (default)
|
||||
* dj -d | 3 - verbose status messages */
|
||||
|
||||
/* (-n) */ static char noerror; /* 0 - exits on partial reads or writes
|
||||
* (default)
|
||||
* 1 - retries on partial reads/writes
|
||||
* (-n) */
|
||||
|
||||
/* Non-configurable defaults. */
|
||||
#define bs_default 1024 /* GNU dd(1) default; twice POSIX but a neat 2^10 */
|
||||
static char *program_name = "<no argv[0]>";
|
||||
static char *stdin_name = "<stdin>";
|
||||
static char *stdout_name = "<stdout>";
|
||||
static int read_flags = O_RDONLY; /* These flags are consistent with Busybox */
|
||||
static int write_flags = O_WRONLY | O_CREAT; /* dd(1). */
|
||||
|
||||
/* Macro to set defaults for user-configurable options. */
|
||||
#define setdefaults do{ \
|
||||
align = -1; \
|
||||
count = 0; \
|
||||
debug = 2; \
|
||||
fmt_output = fmt_asv; \
|
||||
noerror = 0; \
|
||||
ep[0].fl = read_flags; \
|
||||
Io_setdefaults(&ep[0]); \
|
||||
ep[1].fl = write_flags; \
|
||||
Io_setdefaults(&ep[1]); }while(0)
|
||||
|
||||
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
||||
|
||||
/* Macro to check if fd is a std* file, e.g. stdin. */
|
||||
#define fdisstd(fd) \
|
||||
((fd) == STDIN_FILENO \
|
||||
|| (fd) == STDOUT_FILENO \
|
||||
|| (fd) == STDERR_FILENO)
|
||||
|
||||
/* Macro to call the cleanup functions that operate on struct io on the
|
||||
* particular io[2] used in main. Error conditions are not checked because this
|
||||
* is only used when the program is about to terminate (hence its name). */
|
||||
#define terminate(io) do{ \
|
||||
Io_buffree(&(io)[0]); \
|
||||
Io_buffree(&(io)[1]); \
|
||||
Io_fdclose(&(io)[0]); \
|
||||
Io_fdclose(&(io)[1]); }while(0)
|
||||
|
||||
/* Allocates *io's buffer. Returns NULL if unsuccessful. */
|
||||
static void *
|
||||
Io_bufalloc(struct Io *io){
|
||||
|
||||
return (io->buf = malloc(io->bs * (sizeof *io->buf)));
|
||||
}
|
||||
|
||||
/* Frees *io's buffer. Returns io. */
|
||||
static struct Io *
|
||||
Io_buffree(struct Io *io){
|
||||
|
||||
free(io->buf);
|
||||
|
||||
return io;
|
||||
}
|
||||
|
||||
/* Fills the unused portion of io's buffer with padding, updating io->bufuse.
|
||||
* Returns io. */
|
||||
static struct Io *
|
||||
Io_bufrpad(struct Io *io, int padding){
|
||||
|
||||
memset(io->buf + io->bufuse, padding, io->bs - io->bufuse);
|
||||
io->bufuse = io->bs;
|
||||
|
||||
return io;
|
||||
}
|
||||
|
||||
/* Copies from the buffer in src as much as possible to the free space in the
|
||||
* dest buffer, removing the copied units from src and permuting the remaining
|
||||
* units in the src buffer to the start of the buffer, modifying both the src
|
||||
* and dest bufuse and returning dest. */
|
||||
static struct Io*
|
||||
Io_bufxapp(struct Io *dest, struct Io *src){
|
||||
int n;
|
||||
|
||||
n = MIN(src->bufuse, dest->bs - dest->bufuse);
|
||||
memcpy(dest->buf + dest->bufuse, src->buf, n);
|
||||
dest->bufuse += n;
|
||||
memmove(src->buf, src->buf + n, src->bs - n);
|
||||
src->bufuse -= n;
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
/* Copies from the buffer in src to the buffer in dest no more than n units,
|
||||
* removing the copied units from src and permuting the remaining units in the
|
||||
* src buffer to the start of the buffer, modifying both the src and dest
|
||||
* bufuse and returning dest. */
|
||||
static struct Io*
|
||||
Io_bufxfer(struct Io *dest, struct Io *src, int n){
|
||||
|
||||
memcpy(dest->buf, src->buf, (dest->bufuse = n));
|
||||
memmove(src->buf, src->buf + n, (src->bufuse -= n));
|
||||
|
||||
return dest;
|
||||
}
|
||||
|
||||
/* Closes io->fn and returns -1 on error, otherwise io->fd. */
|
||||
static int
|
||||
Io_fdclose(struct Io *io){
|
||||
|
||||
return fdisstd(io->fd)
|
||||
? 0
|
||||
: close(io->fd);
|
||||
}
|
||||
|
||||
/* Opens io->fn and saves the file descriptor into io->fd. Returns io->fd,
|
||||
* which will be -1 if an error occured. */
|
||||
static int
|
||||
Io_fdopen(struct Io *io, char *fn){
|
||||
int fd;
|
||||
|
||||
if((fd = open(fn, io->fl,
|
||||
/* these are the flags used by touch(1p) */
|
||||
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH))
|
||||
!= -1
|
||||
&& Io_fdclose(io) == 0){
|
||||
io->fd = fd;
|
||||
io->fn = fn;
|
||||
}
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
/* Seeks io->seek bytes through *io's file descriptor, (counter-intuitively)
|
||||
* returning -1 if successful and a sysexits.h exit code if an unrecoverable
|
||||
* error occurred. io->buf will be cleared of useful bytes and io->seek will
|
||||
* be set to zero to indicate the seek occurred. */
|
||||
static int
|
||||
Io_fdseek(struct Io *io){
|
||||
int (*op)(int, void *, size_t);
|
||||
|
||||
if(!fdisstd(io->fd) && lseek(io->fd, io->seek, SEEK_SET) != -1)
|
||||
return -1;
|
||||
else if(io->fl == write_flags){
|
||||
memset(io->buf, '\0', io->bs);
|
||||
/* This is a dirty trick; rather than testing conditions and operating
|
||||
* likewise, because the parameters to read or write are going to be
|
||||
* the same either way, just use a function pointer to keep track of
|
||||
* the intended operation. */
|
||||
op = (int (*)(int, void *, size_t))&write;
|
||||
/* Function pointer casts are risky; this works because the difference
|
||||
* is in the second parameter and only that write(2) makes the buffer
|
||||
* const whereas read(2) does not. To avoid even the slightest
|
||||
* undefined behavior comment out the cast, just be ready for a
|
||||
* -Wincompatible-function-pointer-types if your compiler notices it.
|
||||
*/
|
||||
}else
|
||||
op = &read;
|
||||
|
||||
/* We're going to cheat and use bufuse as the retval for write(2), which is
|
||||
* fine because it'll be zeroed as this function returns anyway. */
|
||||
do{ if( (io->bufuse = (*op)(io->fd, io->buf, MIN(io->bs, io->seek))) == 0)
|
||||
/* second chance */
|
||||
io->bufuse = (*op)(io->fd, io->buf, MIN(io->bs, io->seek));
|
||||
}while((io->seek -= io->bufuse) > 0 && io->bufuse != 0);
|
||||
|
||||
io->bufuse = 0;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Reads io->bs bytes from *io's file descriptor into io->buf, storing the
|
||||
* number of read bytes in io->bufuse and updating io->bytes. If io->bufuse is
|
||||
* 0, errno will probably be set. Returns io. */
|
||||
static struct Io *
|
||||
Io_read(struct Io *io){
|
||||
|
||||
io->bytes += (io->bufuse = read(io->fd, io->buf, io->bs));
|
||||
|
||||
return io;
|
||||
}
|
||||
|
||||
/* Sets the variables in a struct *io to the defaults. Identifies the read/
|
||||
* write ends of the "pipe" by checking io->fl. Returns io. */
|
||||
static struct Io *
|
||||
Io_setdefaults(struct Io *io){
|
||||
|
||||
io->bs = bs_default;
|
||||
io->buf = NULL;
|
||||
io->bytes = 0;
|
||||
io->fd = (io->fl == read_flags) ? STDIN_FILENO : STDOUT_FILENO;
|
||||
io->fn = (io->fl == read_flags) ? stdin_name : stdout_name;
|
||||
io->prec = 0;
|
||||
io->rec = 0;
|
||||
io->seek = 0;
|
||||
|
||||
return io;
|
||||
}
|
||||
|
||||
/* Writes io->bufuse units from io->buf to io->fd, permuting any unwritten
|
||||
* bytes to the start of io->buf and updating io->bufuse. If io->bufuse doesn't
|
||||
* change, errno will probably be set. Returns io. */
|
||||
static struct Io *
|
||||
Io_write(struct Io *io){
|
||||
int t;
|
||||
|
||||
if((t = write(io->fd, io->buf, io->bufuse)) > 0)
|
||||
memmove(io->buf, io->buf + t, (io->bufuse -= t));
|
||||
io->bytes += t;
|
||||
|
||||
return io;
|
||||
}
|
||||
|
||||
/* Prints an error message suitable for the event of an operating system error,
|
||||
* with the error itself to be described in the string s. */
|
||||
static int
|
||||
oserr(char *s){
|
||||
|
||||
fprintf(stderr, "%s: %s: %s\n", program_name, s, strerror(errno));
|
||||
|
||||
return EX_OSERR;
|
||||
}
|
||||
|
||||
/* Prints statistics regarding the use of dj, particularly partially and
|
||||
* completely read and written records, accessing debug, ep, and fmt_output. */
|
||||
static void
|
||||
output(void){
|
||||
|
||||
if(debug >= 1)
|
||||
fprintf(stderr, fmt_output,
|
||||
ep[0].rec, ep[0].prec, ep[1].rec, ep[1].prec,
|
||||
ep[0].bytes, ep[1].bytes);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* Parses the string s to an integer, returning either the integer or in the
|
||||
* case of an error a negative integer. This is used for argument parsing
|
||||
* (e.g. -B [int]) in dj and no negative integer would be valid anyway. */
|
||||
static long
|
||||
parse(char *s){
|
||||
long r;
|
||||
|
||||
errno = 0;
|
||||
r = strtol(s, &s, 0);
|
||||
return (*s == '\0' /* no chars left unparsed */ && errno == 0)
|
||||
? r
|
||||
: -1;
|
||||
}
|
||||
|
||||
static int
|
||||
usage(void){
|
||||
|
||||
fprintf(stderr, "Usage: %s (-AdfHqQ) (-a [byte]) (-c [count])\n"
|
||||
"\t(-i [input file]) (-b [input block size]) (-s [input offset])\n"
|
||||
"\t(-o [output file]) (-B [output block size]) (-S [output offset])\n",
|
||||
program_name);
|
||||
|
||||
return EX_USAGE;
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
int c;
|
||||
int i;
|
||||
|
||||
setdefaults;
|
||||
|
||||
if(argc > 0){
|
||||
program_name = argv[0];
|
||||
while((c = getopt(argc, argv, "a:Ab:B:c:di:hHnqs:S:o:")) != -1)
|
||||
switch(c){
|
||||
case 'i': case 'o':
|
||||
i = (c == 'o');
|
||||
if(optarg[0] == '-' && optarg[1] == '\0'){ /* optarg == "-" */
|
||||
ep[i].fd = (i == 0) ? STDIN_FILENO : STDOUT_FILENO;
|
||||
ep[i].fn = (i == 0) ? stdin_name : stdout_name;
|
||||
break;
|
||||
}else if(Io_fdopen(&ep[i], optarg) != -1)
|
||||
break;
|
||||
terminate(ep);
|
||||
return oserr(optarg);
|
||||
case 'A': align = '\0'; break;
|
||||
case 'd': ++debug; break;
|
||||
case 'n': noerror = 1; break;
|
||||
case 'H': fmt_output = fmt_human; break;
|
||||
case 'q': --debug; break;
|
||||
case 'a':
|
||||
if(optarg[0] != '\0' && optarg[1] == '\0'){
|
||||
align = optarg[0];
|
||||
break;
|
||||
}
|
||||
/* FALLTHROUGH */
|
||||
case 'c': case 'b': case 's': case 'B': case 'S':
|
||||
if(c == 'c' && (count = parse(optarg)) >= 0)
|
||||
break;
|
||||
i = isupper(c);
|
||||
c = tolower(c);
|
||||
if((c == 'b' && (ep[i].bs = parse(optarg)) > 0)
|
||||
|| (c == 's' && (ep[i].seek = parse(optarg)) >= 0))
|
||||
break;
|
||||
/* FALLTHROUGH */
|
||||
default:
|
||||
terminate(ep);
|
||||
return usage();
|
||||
}
|
||||
}
|
||||
|
||||
if(debug >= 3)
|
||||
fprintf(stderr,
|
||||
"argv0=%s\n"
|
||||
"in=%s\tibs=%d\tskip=%ld\talign=%hhx\tcount=%d\n"
|
||||
"out=%s\tobs=%d\tseek=%ld\tdebug=%2d\tnoerror=%d\n",
|
||||
program_name,
|
||||
ep[0].fn, ep[0].bs, ep[0].seek, align, count,
|
||||
ep[1].fn, ep[1].bs, ep[1].seek, debug, noerror);
|
||||
|
||||
if(argc > optind){
|
||||
terminate(ep);
|
||||
return usage();
|
||||
}
|
||||
|
||||
for(i = 0; i <= 1; ++i){
|
||||
if(Io_bufalloc(&ep[i]) == NULL){
|
||||
fprintf(stderr, "%s: Failed to allocate %d bytes\n",
|
||||
program_name, ep[i].bs);
|
||||
terminate(ep);
|
||||
return EX_OSERR;
|
||||
}else if(ep[i].seek > 0)
|
||||
switch(Io_fdseek(&ep[i])){
|
||||
case EX_OK:
|
||||
output();
|
||||
terminate(ep);
|
||||
return EX_OK;
|
||||
}
|
||||
}
|
||||
|
||||
do{ /* read */
|
||||
Io_read(&ep[0]);
|
||||
if(!noerror && ep[0].bufuse == 0)
|
||||
Io_read(&ep[0]); /* second chance */
|
||||
if(ep[0].bufuse == 0) /* that's all she wrote */
|
||||
break;
|
||||
else if(ep[0].bufuse < ep[0].bs){
|
||||
++ep[0].prec;
|
||||
if(debug >= 2){
|
||||
fprintf(stderr, "%s: Partial read:\n\t", program_name);
|
||||
output();
|
||||
}
|
||||
if(!noerror)
|
||||
count = 1;
|
||||
if(align >= 0)
|
||||
Io_bufrpad(&ep[0], align);
|
||||
}else
|
||||
++ep[0].rec;
|
||||
|
||||
/* write */
|
||||
do{ if(ep[1].bs > ep[0].bs){ /* io[1].bs > io[0].bs */
|
||||
Io_bufxapp(&ep[1], &ep[0]);
|
||||
if(ep[0].bs + ep[1].bufuse <= ep[1].bs && count != 1)
|
||||
continue; /* we could write more */
|
||||
}else
|
||||
Io_bufxfer(&ep[1], &ep[0], MIN(ep[0].bufuse, ep[1].bs));
|
||||
|
||||
c = ep[1].bufuse;
|
||||
Io_write(&ep[1]);
|
||||
if(!noerror && ep[1].bufuse == c)
|
||||
Io_write(&ep[1]); /* second chance */
|
||||
if(c == ep[1].bufuse){ /* no more love */
|
||||
count = 1;
|
||||
break;
|
||||
}else if(c > ep[1].bufuse && ep[1].bufuse > 0){
|
||||
ep[1].prec += 1;
|
||||
if(debug >= 2){
|
||||
fprintf(stderr, "%s: Partial write:\n\t", program_name);
|
||||
output();
|
||||
}
|
||||
if(!noerror)
|
||||
count = 1;
|
||||
}else if(ep[1].bufuse == 0 && c < ep[1].bs)
|
||||
++ep[1].prec;
|
||||
else
|
||||
++ep[1].rec;
|
||||
}while(ep[0].bufuse > 0);
|
||||
}while(count == 0 || --count > 0);
|
||||
|
||||
output();
|
||||
terminate(ep);
|
||||
|
||||
return EX_OK;
|
||||
}
|
||||
96
src/fop.rs
Normal file
96
src/fop.rs
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* Copyright (c) 2023–2024 Emma Tebibyte <emma@tebibyte.media>
|
||||
* 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::{ Read, stdin, Write },
|
||||
process::{ Command, exit, Stdio },
|
||||
};
|
||||
|
||||
extern crate sysexits;
|
||||
extern crate getopt;
|
||||
|
||||
use getopt::{ Opt, Parser };
|
||||
use sysexits::{ EX_DATAERR, EX_USAGE };
|
||||
|
||||
fn main() {
|
||||
let argv = args().collect::<Vec<String>>();
|
||||
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;
|
||||
|
||||
argv.get(command_arg).unwrap_or_else(|| {
|
||||
eprintln!("Usage: {} [-d delimiter] index command [args...]", argv[0]);
|
||||
exit(EX_USAGE);
|
||||
});
|
||||
|
||||
let index = argv[index_arg].parse::<usize>().unwrap_or_else(|_| {
|
||||
eprintln!("{}: {}: Not an integer.", argv[0], argv[1]);
|
||||
exit(EX_DATAERR);
|
||||
});
|
||||
|
||||
let mut buf = String::new();
|
||||
stdin().read_to_string(&mut buf).unwrap();
|
||||
let mut fields = buf.split(d).collect::<Vec<&str>>();
|
||||
|
||||
let opts = argv.iter().clone().skip(command_arg + 1).collect::<Vec<&String>>();
|
||||
|
||||
let mut spawned = Command::new(argv.get(command_arg).unwrap())
|
||||
.args(opts)
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.spawn()
|
||||
.unwrap();
|
||||
|
||||
let field = fields.get(index).unwrap_or_else(|| {
|
||||
eprintln!(
|
||||
"{}: {}: No such index in input.",
|
||||
argv[0],
|
||||
index.to_string()
|
||||
);
|
||||
exit(EX_DATAERR);
|
||||
});
|
||||
|
||||
if let Some(mut child_stdin) = spawned.stdin.take() {
|
||||
child_stdin.write_all(field.as_bytes()).unwrap();
|
||||
drop(child_stdin);
|
||||
}
|
||||
|
||||
let output = spawned.wait_with_output().unwrap();
|
||||
|
||||
let new_field = String::from_utf8(output.stdout).unwrap();
|
||||
|
||||
fields[index] = &new_field;
|
||||
|
||||
print!("{}", fields.join(&d.to_string()));
|
||||
}
|
||||
95
src/getopt-rs/error.rs
Normal file
95
src/getopt-rs/error.rs
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
* 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/.
|
||||
*
|
||||
* This file incorporates work covered by the following copyright and permission
|
||||
* notice:
|
||||
* The Clear BSD License
|
||||
*
|
||||
* Copyright © 2017-2023 David Wildasin
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted (subject to the limitations in the disclaimer
|
||||
* below) provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions, and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions, and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of the copyright holder nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
|
||||
* BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
||||
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
||||
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
use std::{ error, fmt };
|
||||
|
||||
use crate::ErrorKind::{ self, * };
|
||||
|
||||
/// A basic error type for [`Parser`](struct.Parser.html)
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct Error {
|
||||
culprit: char,
|
||||
kind: ErrorKind,
|
||||
}
|
||||
|
||||
impl Error {
|
||||
/// Creates a new error using a known kind and the character that caused the
|
||||
/// issue.
|
||||
pub fn new(kind: ErrorKind, culprit: char) -> Self {
|
||||
Self { culprit, kind }
|
||||
}
|
||||
|
||||
/// Returns the [`ErrorKind`](enum.ErrorKind.html) for this error.
|
||||
pub fn kind(self) -> ErrorKind {
|
||||
self.kind
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self.kind {
|
||||
MissingArgument => write!(
|
||||
f,
|
||||
"option requires an argument -- {:?}",
|
||||
self.culprit,
|
||||
),
|
||||
UnknownOption => write!(f, "unknown option -- {:?}", self.culprit),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for Error {
|
||||
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
||||
None
|
||||
}
|
||||
}
|
||||
61
src/getopt-rs/errorkind.rs
Normal file
61
src/getopt-rs/errorkind.rs
Normal file
@@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
* 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/.
|
||||
*
|
||||
* This file incorporates work covered by the following copyright and permission
|
||||
* notice:
|
||||
* The Clear BSD License
|
||||
*
|
||||
* Copyright © 2017-2023 David Wildasin
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted (subject to the limitations in the disclaimer
|
||||
* below) provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions, and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions, and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of the copyright holder nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
|
||||
* BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
||||
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
||||
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/// What kinds of errors [`Parser`](struct.Parser.html) can return.
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub enum ErrorKind {
|
||||
/// An argument was not found for an option that was expecting one.
|
||||
MissingArgument,
|
||||
/// An unknown option character was encountered.
|
||||
UnknownOption,
|
||||
}
|
||||
72
src/getopt-rs/lib.rs
Normal file
72
src/getopt-rs/lib.rs
Normal file
@@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
* 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/.
|
||||
*
|
||||
* This file incorporates work covered by the following copyright and permission
|
||||
* notice:
|
||||
* The Clear BSD License
|
||||
*
|
||||
* Copyright © 2017-2023 David Wildasin
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted (subject to the limitations in the disclaimer
|
||||
* below) provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions, and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions, and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of the copyright holder nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
|
||||
* BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
||||
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
||||
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
//! # getopt
|
||||
//!
|
||||
//! `getopt` provides a minimal, (essentially) POSIX-compliant option parser.
|
||||
|
||||
pub use crate::{
|
||||
error::Error,
|
||||
errorkind::ErrorKind,
|
||||
opt::Opt,
|
||||
parser::Parser,
|
||||
result::Result
|
||||
};
|
||||
|
||||
mod error;
|
||||
mod errorkind;
|
||||
mod opt;
|
||||
mod parser;
|
||||
mod result;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
89
src/getopt-rs/opt.rs
Normal file
89
src/getopt-rs/opt.rs
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
* 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/.
|
||||
*
|
||||
* This file incorporates work covered by the following copyright and permission
|
||||
* notice:
|
||||
* The Clear BSD License
|
||||
*
|
||||
* Copyright © 2017-2023 David Wildasin
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted (subject to the limitations in the disclaimer
|
||||
* below) provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions, and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions, and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of the copyright holder nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
|
||||
* BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
||||
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
||||
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
use std::fmt;
|
||||
|
||||
/// A single option.
|
||||
///
|
||||
/// For `Opt(x, y)`:
|
||||
/// - `x` is the character representing the option.
|
||||
/// - `y` is `Some` string, or `None` if no argument was expected.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// use getopt::Opt;
|
||||
///
|
||||
/// // args = ["program", "-abc", "foo"];
|
||||
/// # let args: Vec<String> = vec!["program", "-abc", "foo"]
|
||||
/// # .into_iter()
|
||||
/// # .map(String::from)
|
||||
/// # .collect();
|
||||
/// let optstring = "ab:c";
|
||||
/// let mut opts = getopt::Parser::new(&args, optstring);
|
||||
///
|
||||
/// assert_eq!(Opt('a', None), opts.next().transpose()?.unwrap());
|
||||
/// assert_eq!(Opt('b', Some("c".to_string())), opts.next().transpose()?.unwrap());
|
||||
/// assert_eq!(None, opts.next().transpose()?);
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)]
|
||||
pub struct Opt(pub char, pub Option<String>);
|
||||
|
||||
impl fmt::Display for Opt {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "Opt({:?}, {:?})", self.0, self.1)
|
||||
}
|
||||
}
|
||||
382
src/getopt-rs/parser.rs
Normal file
382
src/getopt-rs/parser.rs
Normal file
@@ -0,0 +1,382 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
* 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/.
|
||||
*
|
||||
* This file incorporates work covered by the following copyright and permission
|
||||
* notice:
|
||||
* The Clear BSD License
|
||||
*
|
||||
* Copyright © 2017-2023 David Wildasin
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted (subject to the limitations in the disclaimer
|
||||
* below) provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions, and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions, and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of the copyright holder nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
|
||||
* BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
||||
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
||||
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{ error::Error, errorkind::ErrorKind, opt::Opt, result::Result };
|
||||
|
||||
/// The core of the `getopt` crate.
|
||||
///
|
||||
/// `Parser` is implemented as an iterator over the options present in the given
|
||||
/// argument vector.
|
||||
///
|
||||
/// The method [`next`](#method.next) does the heavy lifting.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ## Simplified usage:
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// use getopt::Opt;
|
||||
///
|
||||
/// // args = ["program", "-abc", "foo"];
|
||||
/// # let args: Vec<String> = vec!["program", "-abc", "foo"]
|
||||
/// # .into_iter()
|
||||
/// # .map(String::from)
|
||||
/// # .collect();
|
||||
/// let mut opts = getopt::Parser::new(&args, "ab:c");
|
||||
///
|
||||
/// assert_eq!(Some(Opt('a', None)), opts.next().transpose()?);
|
||||
/// assert_eq!(1, opts.index());
|
||||
/// assert_eq!(Some(Opt('b', Some("c".to_string()))), opts.next().transpose()?);
|
||||
/// assert_eq!(2, opts.index());
|
||||
/// assert_eq!(None, opts.next());
|
||||
/// assert_eq!(2, opts.index());
|
||||
/// assert_eq!("foo", args[opts.index()]);
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
///
|
||||
/// ## A more idiomatic example:
|
||||
/// ```
|
||||
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
/// use getopt::Opt;
|
||||
///
|
||||
/// // args = ["program", "-abc", "-d", "foo", "-e", "bar"];
|
||||
/// # let mut args: Vec<String> = vec!["program", "-abc", "-d", "foo", "-e", "bar"]
|
||||
/// # .into_iter()
|
||||
/// # .map(String::from)
|
||||
/// # .collect();
|
||||
/// let mut opts = getopt::Parser::new(&args, "ab:cd:e");
|
||||
///
|
||||
/// let mut a_flag = false;
|
||||
/// let mut b_flag = String::new();
|
||||
/// let mut c_flag = false;
|
||||
/// let mut d_flag = String::new();
|
||||
/// let mut e_flag = false;
|
||||
///
|
||||
/// loop {
|
||||
/// match opts.next().transpose()? {
|
||||
/// None => break,
|
||||
/// Some(opt) => match opt {
|
||||
/// Opt('a', None) => a_flag = true,
|
||||
/// Opt('b', Some(arg)) => b_flag = arg.clone(),
|
||||
/// Opt('c', None) => c_flag = true,
|
||||
/// Opt('d', Some(arg)) => d_flag = arg.clone(),
|
||||
/// Opt('e', None) => e_flag = true,
|
||||
/// _ => unreachable!(),
|
||||
/// },
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// let new_args = args.split_off(opts.index());
|
||||
///
|
||||
/// assert_eq!(true, a_flag);
|
||||
/// assert_eq!("c", b_flag);
|
||||
/// assert_eq!(false, c_flag);
|
||||
/// assert_eq!("foo", d_flag);
|
||||
/// assert_eq!(true, e_flag);
|
||||
///
|
||||
/// assert_eq!(1, new_args.len());
|
||||
/// assert_eq!("bar", new_args.first().unwrap());
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
#[derive(Debug, Eq, PartialEq)]
|
||||
pub struct Parser {
|
||||
opts: HashMap<char, bool>,
|
||||
args: Vec<Vec<char>>,
|
||||
index: usize,
|
||||
point: usize,
|
||||
}
|
||||
|
||||
impl Parser {
|
||||
/// Create a new `Parser`, which will process the arguments in `args`
|
||||
/// according to the options specified in `optstring`.
|
||||
///
|
||||
/// For compatibility with
|
||||
/// [`std::env::args`](https://doc.rust-lang.org/std/env/fn.args.html),
|
||||
/// valid options are expected to begin at the second element of `args`, and
|
||||
/// `index` is
|
||||
/// initialised to `1`.
|
||||
/// If `args` is structured differently, be sure to call
|
||||
/// [`set_index`](#method.set_index) before the first invocation of
|
||||
/// [`next`](#method.next).
|
||||
///
|
||||
/// `optstring` is a string of recognised option characters; if a character
|
||||
/// is followed by a colon (`:`), that option takes an argument.
|
||||
///
|
||||
/// # Note:
|
||||
/// Transforming the OS-specific argument strings into a vector of `String`s
|
||||
/// is the sole responsibility of the calling program, as it involves some
|
||||
/// level of potential information loss (which this crate does not presume
|
||||
/// to handle unilaterally) and error handling (which would complicate the
|
||||
/// interface).
|
||||
pub fn new(args: &[String], optstring: &str) -> Self {
|
||||
let optstring: Vec<char> = optstring.chars().collect();
|
||||
let mut opts = HashMap::new();
|
||||
let mut i = 0;
|
||||
let len = optstring.len();
|
||||
|
||||
while i < len {
|
||||
let j = i + 1;
|
||||
|
||||
if j < len && optstring[j] == ':' {
|
||||
opts.insert(optstring[i], true);
|
||||
i += 1;
|
||||
} else {
|
||||
opts.insert(optstring[i], false);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
|
||||
Self {
|
||||
opts,
|
||||
// "explode" the args into a vector of character vectors, to allow
|
||||
// indexing
|
||||
args: args.iter().map(|e| e.chars().collect()).collect(),
|
||||
index: 1,
|
||||
point: 0,
|
||||
}
|
||||
}
|
||||
|
||||
/// Return the current `index` of the parser.
|
||||
///
|
||||
/// `args[index]` will always point to the the next element of `args`; when
|
||||
/// the parser is
|
||||
/// finished with an element, it will increment `index`.
|
||||
///
|
||||
/// After the last option has been parsed (and [`next`](#method.next) is
|
||||
/// returning `None`),
|
||||
/// `index` will point to the first non-option argument.
|
||||
pub fn index(&self) -> usize {
|
||||
self.index
|
||||
}
|
||||
|
||||
// `point` must be reset to 0 whenever `index` is changed
|
||||
|
||||
/// Modify the current `index` of the parser.
|
||||
pub fn set_index(&mut self, value: usize) {
|
||||
self.index = value;
|
||||
self.point = 0;
|
||||
}
|
||||
|
||||
/// Increment the current `index` of the parser.
|
||||
///
|
||||
/// This use case is common enough to warrant its own optimised method.
|
||||
pub fn incr_index(&mut self) {
|
||||
self.index += 1;
|
||||
self.point = 0;
|
||||
}
|
||||
}
|
||||
|
||||
impl Iterator for Parser {
|
||||
type Item = Result<Opt>;
|
||||
|
||||
/// Returns the next option, if any.
|
||||
///
|
||||
/// Returns an [`Error`](struct.Error.html) if an unexpected option is
|
||||
/// encountered or if an
|
||||
/// expected argument is not found.
|
||||
///
|
||||
/// Parsing stops at the first non-hyphenated argument; or at the first
|
||||
/// argument matching "-";
|
||||
/// or after the first argument matching "--".
|
||||
///
|
||||
/// When no more options are available, `next` returns `None`.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ## "-"
|
||||
/// ```
|
||||
/// use getopt::Parser;
|
||||
///
|
||||
/// // args = ["program", "-", "-a"];
|
||||
/// # let args: Vec<String> = vec!["program", "-", "-a"]
|
||||
/// # .into_iter()
|
||||
/// # .map(String::from)
|
||||
/// # .collect();
|
||||
/// let mut opts = Parser::new(&args, "a");
|
||||
///
|
||||
/// assert_eq!(None, opts.next());
|
||||
/// assert_eq!("-", args[opts.index()]);
|
||||
/// ```
|
||||
///
|
||||
/// ## "--"
|
||||
/// ```
|
||||
/// use getopt::Parser;
|
||||
///
|
||||
/// // args = ["program", "--", "-a"];
|
||||
/// # let args: Vec<String> = vec!["program", "--", "-a"]
|
||||
/// # .into_iter()
|
||||
/// # .map(String::from)
|
||||
/// # .collect();
|
||||
/// let mut opts = Parser::new(&args, "a");
|
||||
///
|
||||
/// assert_eq!(None, opts.next());
|
||||
/// assert_eq!("-a", args[opts.index()]);
|
||||
/// ```
|
||||
///
|
||||
/// ## Unexpected option:
|
||||
/// ```
|
||||
/// use getopt::Parser;
|
||||
///
|
||||
/// // args = ["program", "-b"];
|
||||
/// # let args: Vec<String> = vec!["program", "-b"]
|
||||
/// # .into_iter()
|
||||
/// # .map(String::from)
|
||||
/// # .collect();
|
||||
/// let mut opts = Parser::new(&args, "a");
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// "unknown option -- 'b'".to_string(),
|
||||
/// opts.next().unwrap().unwrap_err().to_string()
|
||||
/// );
|
||||
/// ```
|
||||
///
|
||||
/// ## Missing argument:
|
||||
/// ```
|
||||
/// use getopt::Parser;
|
||||
///
|
||||
/// // args = ["program", "-a"];
|
||||
/// # let args: Vec<String> = vec!["program", "-a"]
|
||||
/// # .into_iter()
|
||||
/// # .map(String::from)
|
||||
/// # .collect();
|
||||
/// let mut opts = Parser::new(&args, "a:");
|
||||
///
|
||||
/// assert_eq!(
|
||||
/// "option requires an argument -- 'a'".to_string(),
|
||||
/// opts.next().unwrap().unwrap_err().to_string()
|
||||
/// );
|
||||
/// ```
|
||||
fn next(&mut self) -> Option<Result<Opt>> {
|
||||
if self.point == 0 {
|
||||
/*
|
||||
* Rationale excerpts below taken verbatim from "The Open Group Base
|
||||
* Specifications Issue 7, 2018 edition", IEEE Std 1003.1-2017
|
||||
* (Revision of IEEE Std 1003.1-2008).
|
||||
* Copyright © 2001-2018 IEEE and The Open Group.
|
||||
*/
|
||||
|
||||
/*
|
||||
* If, when getopt() is called:
|
||||
* argv[optind] is a null pointer
|
||||
* *argv[optind] is not the character '-'
|
||||
* argv[optind] points to the string "-"
|
||||
* getopt() shall return -1 without changing optind.
|
||||
*/
|
||||
if self.index >= self.args.len()
|
||||
|| self.args[self.index].is_empty()
|
||||
|| self.args[self.index][0] != '-'
|
||||
|| self.args[self.index].len() == 1
|
||||
{
|
||||
return None;
|
||||
}
|
||||
|
||||
/*
|
||||
* If:
|
||||
* argv[optind] points to the string "--"
|
||||
* getopt() shall return -1 after incrementing index.
|
||||
*/
|
||||
if self.args[self.index][1] == '-' && self.args[self.index].len() == 2 {
|
||||
self.incr_index();
|
||||
return None;
|
||||
}
|
||||
|
||||
// move past the starting '-'
|
||||
self.point += 1;
|
||||
}
|
||||
|
||||
let opt = self.args[self.index][self.point];
|
||||
self.point += 1;
|
||||
|
||||
match self.opts.get(&opt) {
|
||||
None => {
|
||||
if self.point >= self.args[self.index].len() {
|
||||
self.incr_index();
|
||||
}
|
||||
Some(Err(Error::new(ErrorKind::UnknownOption, opt)))
|
||||
}
|
||||
Some(false) => {
|
||||
if self.point >= self.args[self.index].len() {
|
||||
self.incr_index();
|
||||
}
|
||||
|
||||
Some(Ok(Opt(opt, None)))
|
||||
}
|
||||
Some(true) => {
|
||||
let arg: String = if self.point >= self.args[self.index].len() {
|
||||
self.incr_index();
|
||||
if self.index >= self.args.len() {
|
||||
return Some(Err(Error::new(
|
||||
ErrorKind::MissingArgument,
|
||||
opt,
|
||||
)));
|
||||
}
|
||||
self.args[self.index].iter().collect()
|
||||
} else {
|
||||
self.args[self.index]
|
||||
.clone()
|
||||
.split_off(self.point)
|
||||
.iter()
|
||||
.collect()
|
||||
};
|
||||
|
||||
self.incr_index();
|
||||
|
||||
Some(Ok(Opt(opt, Some(arg))))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
59
src/getopt-rs/result.rs
Normal file
59
src/getopt-rs/result.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
* 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/.
|
||||
*
|
||||
* This file incorporates work covered by the following copyright and permission
|
||||
* notice:
|
||||
* The Clear BSD License
|
||||
*
|
||||
* Copyright © 2017-2023 David Wildasin
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted (subject to the limitations in the disclaimer
|
||||
* below) provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions, and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions, and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of the copyright holder nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
|
||||
* BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
||||
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
||||
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
use std::result;
|
||||
|
||||
use crate::error::Error;
|
||||
|
||||
/// A specialized `Result` type for use with [`Parser`](struct.Parser.html)
|
||||
pub type Result<T> = result::Result<T, Error>;
|
||||
228
src/getopt-rs/tests.rs
Normal file
228
src/getopt-rs/tests.rs
Normal file
@@ -0,0 +1,228 @@
|
||||
/*
|
||||
* Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
* 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/.
|
||||
*
|
||||
* This file incorporates work covered by the following copyright and permission
|
||||
* notice:
|
||||
* The Clear BSD License
|
||||
*
|
||||
* Copyright © 2017-2023 David Wildasin
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted (subject to the limitations in the disclaimer
|
||||
* below) provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions, and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions, and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of the copyright holder nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from
|
||||
* this software without specific prior written permission.
|
||||
*
|
||||
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
|
||||
* BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
||||
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
|
||||
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
||||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
||||
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
use crate::{Opt, Parser};
|
||||
|
||||
macro_rules! basic_test {
|
||||
($name:ident, $expect:expr, $next:expr, [$($arg:expr),+], $optstr:expr) => (
|
||||
#[test]
|
||||
fn $name() -> Result<(), String> {
|
||||
let expect: Option<Opt> = $expect;
|
||||
let args: Vec<String> = vec![$($arg),+]
|
||||
.into_iter()
|
||||
.map(String::from)
|
||||
.collect();
|
||||
let next: Option<String> = $next;
|
||||
let mut opts = Parser::new(&args, $optstr);
|
||||
|
||||
match opts.next().transpose() {
|
||||
Err(error) => {
|
||||
return Err(format!("next() returned {:?}", error))
|
||||
},
|
||||
Ok(actual) => if actual != expect {
|
||||
return Err(
|
||||
format!("expected {:?}; got {:?}", expect, actual)
|
||||
)
|
||||
},
|
||||
};
|
||||
|
||||
match next {
|
||||
None => if opts.index() < args.len() {
|
||||
return Err(format!(
|
||||
"expected end of args; got {:?}", args[opts.index()]
|
||||
))
|
||||
},
|
||||
Some(n) => if args[opts.index()] != n {
|
||||
return Err(format!(
|
||||
"next arg: expected {:?}; got {:?}",
|
||||
n,
|
||||
args[opts.index()]
|
||||
))
|
||||
},
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
#[rustfmt::skip] basic_test!(
|
||||
blank_arg, None, Some(String::new()), ["x", ""], "a"
|
||||
);
|
||||
#[rustfmt::skip] basic_test!(
|
||||
double_dash, None, Some("-a".to_string()), ["x", "--", "-a", "foo"], "a"
|
||||
);
|
||||
#[rustfmt::skip] basic_test!(no_opts_1, None, None, ["x"], "a");
|
||||
#[rustfmt::skip] basic_test!(
|
||||
no_opts_2, None, Some("foo".to_string()), ["x", "foo"], "a"
|
||||
);
|
||||
#[rustfmt::skip] basic_test!(
|
||||
no_opts_3, None, Some("foo".to_string()), ["x", "foo", "-a"], "a"
|
||||
);
|
||||
#[rustfmt::skip] basic_test!(
|
||||
single_dash, None, Some("-".to_string()), ["x", "-", "-a", "foo"], "a"
|
||||
);
|
||||
#[rustfmt::skip] basic_test!(
|
||||
single_opt,
|
||||
Some(Opt('a', None)),
|
||||
Some("foo".to_string()),
|
||||
["x", "-a", "foo"],
|
||||
"a"
|
||||
);
|
||||
#[rustfmt::skip] basic_test!(
|
||||
single_optarg,
|
||||
Some(Opt('a', Some("foo".to_string()))),
|
||||
None,
|
||||
["x", "-a", "foo"],
|
||||
"a:"
|
||||
);
|
||||
|
||||
macro_rules! error_test {
|
||||
($name:ident, $expect:expr, [$($arg:expr),+], $optstr:expr) => (
|
||||
#[test]
|
||||
fn $name() -> Result<(), String> {
|
||||
let expect: String = $expect.to_string();
|
||||
let args: Vec<String> = vec![$($arg),+]
|
||||
.into_iter()
|
||||
.map(String::from)
|
||||
.collect();
|
||||
let mut opts = Parser::new(&args, $optstr);
|
||||
|
||||
match opts.next() {
|
||||
None => {
|
||||
return Err(format!(
|
||||
"unexpected successful response: end of options"
|
||||
))
|
||||
},
|
||||
Some(Err(actual)) => {
|
||||
let actual = actual.to_string();
|
||||
|
||||
if actual != expect {
|
||||
return Err(
|
||||
format!("expected {:?}; got {:?}", expect, actual)
|
||||
);
|
||||
}
|
||||
},
|
||||
Some(Ok(opt)) => {
|
||||
return Err(
|
||||
format!("unexpected successful response: {:?}", opt)
|
||||
)
|
||||
},
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
#[rustfmt::skip] error_test!(
|
||||
bad_opt,
|
||||
"unknown option -- 'b'",
|
||||
["x", "-b"],
|
||||
"a"
|
||||
);
|
||||
|
||||
#[rustfmt::skip] error_test!(
|
||||
missing_optarg,
|
||||
"option requires an argument -- 'a'",
|
||||
["x", "-a"],
|
||||
"a:"
|
||||
);
|
||||
|
||||
#[test]
|
||||
fn multiple() -> Result<(), String> {
|
||||
let args: Vec<String> = vec!["x", "-abc", "-d", "foo", "-e", "bar"]
|
||||
.into_iter()
|
||||
.map(String::from)
|
||||
.collect();
|
||||
let optstring = "ab:d:e".to_string();
|
||||
let mut opts = Parser::new(&args, &optstring);
|
||||
|
||||
macro_rules! check_result {
|
||||
($expect:expr) => {
|
||||
let expect: Option<Opt> = $expect;
|
||||
match opts.next().transpose() {
|
||||
Err(error) => {
|
||||
return Err(format!("next() returned {:?}", error));
|
||||
},
|
||||
Ok(actual) => {
|
||||
if actual != expect {
|
||||
return Err(
|
||||
format!("expected {:?}; got {:?}", expect, actual)
|
||||
);
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
check_result!(Some(Opt('a', None)));
|
||||
check_result!(Some(Opt('b', Some("c".to_string()))));
|
||||
check_result!(Some(Opt('d', Some("foo".to_string()))));
|
||||
check_result!(Some(Opt('e', None)));
|
||||
check_result!(None);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn continue_after_error() {
|
||||
let args: Vec<String> = vec!["x", "-z", "-abc"]
|
||||
.into_iter()
|
||||
.map(String::from)
|
||||
.collect();
|
||||
let optstring = "ab:d:e".to_string();
|
||||
for _opt in Parser::new(&args, &optstring) {
|
||||
// do nothing, should not panic
|
||||
}
|
||||
}
|
||||
@@ -18,11 +18,9 @@
|
||||
|
||||
#include <errno.h> /* errno */
|
||||
#include <stdio.h> /* fprintf(3), stderr */
|
||||
#include <stdlib.h> /* strtol(3), size_t */
|
||||
#ifndef EX_USAGE
|
||||
# include <sysexits.h> /* EX_USAGE */
|
||||
#endif
|
||||
#include <stdlib.h> /* strtol(3), size_t, EXIT_FAILURE */
|
||||
#include <unistd.h> /* getopt(3), optind */
|
||||
#include <sysexits.h>
|
||||
|
||||
/* 0b00? */ /* Equal | -e | 0b001 | 1 */
|
||||
#define EQUAL 0x01 /* Greater | -g | 0b010 | 2 */
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
/*
|
||||
* 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/.
|
||||
*/
|
||||
|
||||
#include "libfileis.h"
|
||||
|
||||
#include <unistd.h> /* access(3), F_OK, R_OK, W_OK, X_OK */
|
||||
|
||||
int f_executable(char *path){ return access(path, X_OK) == 0; } /* test -x */
|
||||
int f_exists(char *path){ return access(path, F_OK) == 0; } /* test -e */
|
||||
int f_readable(char *path){ return access(path, R_OK) == 0; } /* test -r */
|
||||
int f_writeable(char *path){ return access(path, W_OK) == 0; } /* test -w */
|
||||
|
||||
#include <sys/stat.h> /* lstat(3), struct stat, S_ISBLK, S_ISCHR, S_ISDIR,
|
||||
* S_ISFIFO, S_ISGID, S_ISREG, S_ISLNK, S_ISSOCK,
|
||||
* S_ISUID, S_ISVTX */
|
||||
|
||||
static struct stat buf;
|
||||
|
||||
int f_blockspecial(char *path){ /* test -b */
|
||||
return lstat(path, &buf) != -1 && S_ISBLK(buf.st_mode);
|
||||
}
|
||||
int f_charspecial(char *path){ /* test -c */
|
||||
return lstat(path, &buf) != -1 && S_ISCHR(buf.st_mode);
|
||||
}
|
||||
int f_directory(char *path){ /* test -d */
|
||||
return lstat(path, &buf) != -1 && S_ISDIR(buf.st_mode);
|
||||
}
|
||||
int f_fifospecial(char *path){ /* test -p */
|
||||
return lstat(path, &buf) != -1 && S_ISFIFO(buf.st_mode);
|
||||
}
|
||||
int f_gid(char *path){ /* test -g */
|
||||
return lstat(path, &buf) != -1 && (buf.st_mode & S_ISGID);
|
||||
}
|
||||
int f_regular(char *path){ /* test -f */
|
||||
return lstat(path, &buf) != -1 && S_ISREG(buf.st_mode);
|
||||
}
|
||||
int f_socket(char *path){ /* test -S */
|
||||
return lstat(path, &buf) != -1 && S_ISSOCK(buf.st_mode);
|
||||
}
|
||||
int f_sticky(char *path){ /* test -k */
|
||||
return lstat(path, &buf) != -1 && (buf.st_mode & S_ISVTX);
|
||||
}
|
||||
int f_symlink(char *path){ /* test -h; test -L */
|
||||
return lstat(path, &buf) != -1 && S_ISLNK(buf.st_mode);
|
||||
}
|
||||
int f_uid(char *path){ /* test -u */
|
||||
return lstat(path, &buf) != -1 && (buf.st_mode & S_ISUID);
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
/*
|
||||
* 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/.
|
||||
*/
|
||||
|
||||
/* libfileis functions return true if the condition is true and false if the
|
||||
* condition is false. Returned values are 0 or 1 only. */
|
||||
|
||||
/* True if file exists and is a block special file. */
|
||||
int f_blockspecial(char *path);
|
||||
|
||||
/* True if file exists and is a character special file. */
|
||||
int f_charspecial(char *path);
|
||||
|
||||
/* True if file exists and is a directory. */
|
||||
int f_directory(char *path);
|
||||
|
||||
/* True if file exists and is executable. */
|
||||
int f_executable(char *path);
|
||||
|
||||
/* True if file exists (regardless of type). */
|
||||
int f_exists(char *path);
|
||||
|
||||
/* True if file exists and is a named pipe (FIFO). */
|
||||
int f_fifospecial(char *path);
|
||||
|
||||
/* True if file exists and its set group ID flag is set. */
|
||||
int f_gid(char *path);
|
||||
|
||||
/* True if file exists and is readable. */
|
||||
int f_readable(char *path);
|
||||
|
||||
/* True if file exists and is a regular file. */
|
||||
int f_regular(char *path);
|
||||
|
||||
/* True if file exists and is a socket. */
|
||||
int f_socket(char *path);
|
||||
|
||||
/* True if file exists and its sticky bit is set. */
|
||||
int f_sticky(char *path);
|
||||
|
||||
/* True if file exists and is a symbolic link. */
|
||||
int f_symlink(char *path);
|
||||
|
||||
/* True if file exists and its set user ID flag is set. */
|
||||
int f_uid(char *path);
|
||||
|
||||
/* True if file exists and is writeable. */
|
||||
int f_writeable(char *path);
|
||||
@@ -18,10 +18,9 @@
|
||||
|
||||
#include <stdio.h> /* fprintf(3), fputs(3), getc(3), putc(3), stdin, stdout,
|
||||
* EOF */
|
||||
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
|
||||
#include <unistd.h> /* getopt(3) */
|
||||
#if !defined EX_USAGE || !defined EX_OK
|
||||
# include <sysexits.h>
|
||||
#endif
|
||||
#include <sysexits.h>
|
||||
|
||||
int main(int argc, char *argv[]){
|
||||
int c;
|
||||
|
||||
248
src/rpn.rs
Normal file
248
src/rpn.rs
Normal file
@@ -0,0 +1,248 @@
|
||||
/*
|
||||
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
|
||||
* 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/.
|
||||
*
|
||||
* This file incorporates work covered by the following copyright and permission
|
||||
* notice:
|
||||
*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2022 Lilly Cham
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a
|
||||
* copy of this software and associated documentation files
|
||||
* (the "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software
|
||||
* is furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included
|
||||
* in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
use std::{
|
||||
collections::VecDeque,
|
||||
env::args,
|
||||
fmt::{ self, Display, Formatter },
|
||||
io::stdin,
|
||||
process::ExitCode,
|
||||
};
|
||||
|
||||
use CalcType::*;
|
||||
|
||||
extern crate sysexits;
|
||||
|
||||
use sysexits::EX_DATAERR;
|
||||
|
||||
#[derive(Clone, PartialEq, PartialOrd, Debug)]
|
||||
// enum CalcType is a type containing operations used in the calculator
|
||||
enum CalcType {
|
||||
Add,
|
||||
Subtract,
|
||||
Multiply,
|
||||
Divide,
|
||||
Power,
|
||||
Floor,
|
||||
Modulo,
|
||||
Val(f64),
|
||||
Invalid(String),
|
||||
}
|
||||
|
||||
impl From<&str> for CalcType {
|
||||
fn from(value: &str) -> Self {
|
||||
match value {
|
||||
"+" => Add,
|
||||
"-" | "−" => Subtract,
|
||||
"*" | "×" => Multiply,
|
||||
"/" | "÷" => Divide,
|
||||
"^" | "**" => Power,
|
||||
"//" => Floor,
|
||||
"%" => Modulo,
|
||||
_ => {
|
||||
match value.parse::<f64>() {
|
||||
Ok(x) => Val(x),
|
||||
Err(_) => Invalid(value.to_owned()),
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for CalcType {
|
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
|
||||
let y: String;
|
||||
write!(f, "{}", match self {
|
||||
Add => "addition",
|
||||
Subtract => "subtraction",
|
||||
Multiply => "multiplication",
|
||||
Divide => "division",
|
||||
Power => "exponentiation",
|
||||
Floor => "floor division",
|
||||
Modulo => "modulus",
|
||||
Val(x) => {
|
||||
y = x.to_string(); &y
|
||||
},
|
||||
Invalid(i) => {
|
||||
y = i.to_string(); &y
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
struct EvaluationError {
|
||||
message: String,
|
||||
code: i32,
|
||||
}
|
||||
|
||||
// I’m no math nerd but I want the highest possible approximation of 0.9
|
||||
// repeating and it seems this can give it to me
|
||||
const PRECISION_MOD: f64 = 0.9 + f64::EPSILON * 100.0;
|
||||
|
||||
fn eval(
|
||||
input: &str,
|
||||
initial_stack: VecDeque<f64>,
|
||||
) -> Result<(VecDeque<f64>, bool), EvaluationError> {
|
||||
let mut stack = initial_stack;
|
||||
let mut oper = false;
|
||||
|
||||
if input.is_empty() {
|
||||
stack.clear();
|
||||
return Ok((stack, oper));
|
||||
}
|
||||
|
||||
// Split the input into tokens.
|
||||
let mut toks: VecDeque<CalcType> = input
|
||||
.split_whitespace()
|
||||
.rev()
|
||||
.map(|t| CalcType::from(t))
|
||||
.collect();
|
||||
let mut ops: VecDeque<CalcType> = VecDeque::new();
|
||||
|
||||
while let Some(n) = toks.pop_back() {
|
||||
match n {
|
||||
Val(v) => stack.push_back(v),
|
||||
Invalid(i) => {
|
||||
return Err(EvaluationError {
|
||||
message: format!("{}: Invalid token", i),
|
||||
code: EX_DATAERR,
|
||||
})
|
||||
},
|
||||
op => {
|
||||
ops.push_back(op.clone());
|
||||
oper = true;
|
||||
|
||||
let vals = (
|
||||
stack.pop_back(),
|
||||
stack.pop_back(),
|
||||
);
|
||||
|
||||
if let (Some(x), Some(y)) = vals {
|
||||
match op {
|
||||
Add => stack.push_back(y + x),
|
||||
Subtract => stack.push_back(y - x),
|
||||
Multiply => stack.push_back(y * x),
|
||||
Divide => stack.push_back(y / x),
|
||||
Power => stack.push_back(y.powf(x)),
|
||||
Floor => stack.push_back((y / x).floor()),
|
||||
Modulo => stack.push_back(y % x),
|
||||
_ => {},
|
||||
};
|
||||
} else {
|
||||
return Err(EvaluationError {
|
||||
message: format!("{}: Unexpected operation.", op),
|
||||
code: EX_DATAERR,
|
||||
})
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
Ok((stack, oper))
|
||||
}
|
||||
|
||||
// Round a float to the given precision level
|
||||
fn round_precise(value: &f64, precision: usize) -> f64 {
|
||||
let multiplier = 10_f64.powi(precision as i32);
|
||||
(value * multiplier).round() / multiplier
|
||||
}
|
||||
|
||||
fn main() -> ExitCode {
|
||||
let argv = args().collect::<Vec<String>>();
|
||||
let mut stack = VecDeque::new();
|
||||
let mut buf = String::new();
|
||||
// Set floating-point precision for correcting rounding errors based on
|
||||
// machine epsilon
|
||||
let precision = (-f64::EPSILON.log10() * PRECISION_MOD).ceil() as usize;
|
||||
|
||||
if argv.get(1).is_none() {
|
||||
while let Ok(_) = stdin().read_line(&mut buf) {
|
||||
match eval(&buf.trim(), stack) {
|
||||
Ok(s) => {
|
||||
buf.clear();
|
||||
stack = s.0.clone();
|
||||
|
||||
let val = match stack.iter().last() {
|
||||
Some(v) => v,
|
||||
None => break,
|
||||
};
|
||||
|
||||
if s.1 == false { continue; }
|
||||
|
||||
println!("{}", round_precise(val, precision).to_string());
|
||||
},
|
||||
Err(err) => {
|
||||
eprintln!("{}: {}", argv[0], err.message);
|
||||
return ExitCode::from(err.code as u8);
|
||||
},
|
||||
};
|
||||
}
|
||||
} else {
|
||||
let input = argv
|
||||
.iter()
|
||||
.skip(1)
|
||||
.map(|x| x.to_owned())
|
||||
.collect::<Vec<String>>()
|
||||
.join(" ");
|
||||
|
||||
match eval(&input, stack) {
|
||||
Ok(s) => {
|
||||
stack = s.0.clone();
|
||||
|
||||
let val = match stack.iter().last() {
|
||||
Some(v) => v,
|
||||
None => return ExitCode::from(0),
|
||||
};
|
||||
|
||||
println!("{}", round_precise(val, precision).to_string())
|
||||
},
|
||||
Err(err) => {
|
||||
eprintln!("{}: {}", argv[0], err.message);
|
||||
return ExitCode::from(err.code as u8);
|
||||
},
|
||||
};
|
||||
}
|
||||
ExitCode::from(0)
|
||||
}
|
||||
@@ -17,14 +17,13 @@
|
||||
*/
|
||||
|
||||
#include <stdio.h> /* fprintf(3), stderr, NULL */
|
||||
#include <stdlib.h> /* EXIT_FAILURE */
|
||||
#include <string.h> /* strchr(3) */
|
||||
#ifndef EX_USAGE
|
||||
# include <sysexits.h>
|
||||
#endif
|
||||
#include <unistd.h> /* access(3), getopt(3), F_OK, R_OK, W_OK, X_OK */
|
||||
#include <sys/stat.h> /* lstat(3), stat struct, S_ISBLK, S_ISCHR, S_ISDIR,
|
||||
* S_ISFIFO, S_ISGID, S_ISREG, S_ISLNK, S_ISSOCK,
|
||||
* S_ISUID, S_ISVTX */
|
||||
#include <sysexits.h>
|
||||
|
||||
static char args[] = "bcdefghkprsuwxLS";
|
||||
static char ops[(sizeof args) / (sizeof *args)];
|
||||
|
||||
@@ -20,10 +20,9 @@
|
||||
#include <ctype.h>
|
||||
#include <stddef.h> /* NULL */
|
||||
#include <stdio.h> /* fprintf(3) */
|
||||
#include <stdlib.h> /* EXIT_FAILURE */
|
||||
#include <string.h> /* strcmp(3) */
|
||||
#if !defined EX_USAGE
|
||||
# include <sysexits.h>
|
||||
#endif
|
||||
#include <sysexits.h>
|
||||
|
||||
static char *program_name = "str";
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include <stdio.h> /* fprintf(3), stderr */
|
||||
#ifndef EX_USAGE
|
||||
# include <sysexits.h> /* EX_USAGE */
|
||||
#endif
|
||||
#include <stdlib.h> /* EXIT_FAILURE */
|
||||
#include <sysexits.h>
|
||||
|
||||
static char *program_name = "strcmp";
|
||||
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
# SPDX-License-Identifier: FSFAP
|
||||
#
|
||||
# Copying and distribution of this file, with or without modification, are
|
||||
# permitted in any medium without royalty provided the copyright notice and this
|
||||
# notice are preserved. This file is offered as-is, without any warranty.
|
||||
|
||||
set -e
|
||||
|
||||
if ! ls GNUmakefile >/dev/null 2>&1
|
||||
then
|
||||
printf '%s: Run this script in the root of the project.\n' "$0" 1>&2
|
||||
exit 64 # sysexits.h(3) EX_USAGE
|
||||
fi
|
||||
|
||||
make clean
|
||||
|
||||
for CC in cc \
|
||||
clang \
|
||||
gcc \
|
||||
tcc \
|
||||
'zig cc'
|
||||
do
|
||||
export CC
|
||||
|
||||
command -v "$(printf '%s\n' "$CC" | cut -d ' ' -f1)" >/dev/null 2>&1 \
|
||||
|| continue
|
||||
|
||||
printf '%s: %s: Testing build.\n' "$0" "$CC"
|
||||
|
||||
make CC="$CC" && printf '%s: Build successful.\n' "$0"
|
||||
|
||||
ls -lA build/
|
||||
|
||||
make clean
|
||||
printf '\n'
|
||||
done
|
||||
@@ -1,6 +1,6 @@
|
||||
#!/bin/sh
|
||||
|
||||
# Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
||||
# Copyright (c) 2023–2024 Emma Tebibyte <emma@tebibyte.media>
|
||||
# SPDX-License-Identifier: FSFAP
|
||||
#
|
||||
# Copying and distribution of this file, with or without modification, are
|
||||
@@ -9,7 +9,7 @@
|
||||
|
||||
set -e
|
||||
|
||||
if ! ls GNUmakefile >/dev/null 2>&1
|
||||
if ! ls Makefile >/dev/null 2>&1
|
||||
then
|
||||
printf '%s: Run this script in the root of the project.\n' "$0" 1>&2
|
||||
exit 1
|
||||
|
||||
Reference in New Issue
Block a user