Compare commits
120 Commits
pg
...
getopt-for
| Author | SHA1 | Date | |
|---|---|---|---|
|
a3bbad467e
|
|||
|
07a2f1efc2
|
|||
|
accec33301
|
|||
|
444e682f02
|
|||
|
fde682a10e
|
|||
|
87c9e29932
|
|||
|
1037e32e6e
|
|||
|
5c70852890
|
|||
|
8ec18ccf75
|
|||
|
3bfdc62035
|
|||
|
7d174c1f7c
|
|||
|
3f41110617
|
|||
|
7a69e1d516
|
|||
|
64e943f64a
|
|||
|
16dcd4da37
|
|||
|
d892fa1cac
|
|||
|
3862a95151
|
|||
|
264ae2e82c
|
|||
|
1634761593
|
|||
|
0b40360e7e
|
|||
|
590d98d7f7
|
|||
|
07aa9a9abc
|
|||
|
fce49bed85
|
|||
|
62b38a6765
|
|||
|
23521bade5
|
|||
|
3b774cab27
|
|||
|
e889159619
|
|||
|
fbacfecce8
|
|||
|
579ff65b67
|
|||
|
334433536b
|
|||
|
e9058803d3
|
|||
|
71d4d6ba05
|
|||
|
821f5d09e9
|
|||
|
a4a556a5b6
|
|||
|
e2c03842a3
|
|||
|
0fd66bff38
|
|||
|
0c4923016e
|
|||
|
e0c985f7ff
|
|||
|
d2f8fb8106
|
|||
|
4c81516742
|
|||
|
98c4d94f6d
|
|||
|
da190f713c
|
|||
|
10b7f7706b
|
|||
|
b1a4a1a2b9
|
|||
|
a693ced9d9
|
|||
|
1003c82d23
|
|||
|
baa75a2619
|
|||
|
d6d9c2088e
|
|||
|
ea2efdf5b9
|
|||
|
6c558654f3
|
|||
|
8699d04ccc
|
|||
|
18dfd20937
|
|||
|
42010596de
|
|||
|
0ddfa6e474
|
|||
|
326c8f77d1
|
|||
|
72ef8d00bc
|
|||
|
851f729ebd
|
|||
|
e253cdf79c
|
|||
|
d89707a47c
|
|||
|
2a75b8f820
|
|||
|
bda7c074b0
|
|||
|
2f805cc942
|
|||
|
70bec49127
|
|||
|
eae0b0352b
|
|||
|
25eb08eb84
|
|||
|
6c882f54cb
|
|||
|
cf96a13419
|
|||
|
1f59a9806e
|
|||
|
c7c71c725b
|
|||
|
b76ff8fd90
|
|||
|
66f809162b
|
|||
|
7278a8fc41
|
|||
|
8e5090d13d
|
|||
|
eb821715f7
|
|||
|
0bc0ffa0a5
|
|||
|
a7f16b5a7e
|
|||
|
a94884cc2a
|
|||
|
9412f95cb1
|
|||
|
74d6ec16ac
|
|||
|
3880abaa4f
|
|||
|
e93d218b87
|
|||
|
cd5983b10b
|
|||
|
acdbecf178
|
|||
|
0f2d357476
|
|||
|
588680406a
|
|||
|
ee7b7e89b2
|
|||
|
91de98cea3
|
|||
|
bd09d16949
|
|||
|
ce8a0a5be3
|
|||
|
0b0bd9bd76
|
|||
|
18aac06113
|
|||
|
4aeba9d13f
|
|||
|
cd875df547
|
|||
|
94873a2ddc
|
|||
|
aa819cabc2
|
|||
|
7939985c98
|
|||
|
45329ccb8c
|
|||
|
014485d3c5
|
|||
|
5cfccf75af
|
|||
|
c88c41b213
|
|||
|
406feb3dc7
|
|||
|
787f0dc6e2
|
|||
|
94ada03ce4
|
|||
|
9bfc587623
|
|||
|
8508479a5b
|
|||
|
e028844825
|
|||
|
f022121436
|
|||
|
e278307daf
|
|||
|
4ad9e0da92
|
|||
|
0113cf793d
|
|||
|
1aa2b596d0
|
|||
|
c6f30c4195
|
|||
|
3398fc372c
|
|||
|
28f2d44e2f
|
|||
|
aefa87d9e5
|
|||
|
057f5571d6
|
|||
|
e7021e127c
|
|||
|
417d7ca405
|
|||
|
f7a74dc430
|
|||
|
cabe08bca4
|
46
CONTRIBUTING
46
CONTRIBUTING
@@ -1,26 +1,29 @@
|
|||||||
Make sure to read our code of conduct in the CONDUCT file.
|
Make sure to read our code of conduct in the CONDUCT file.
|
||||||
|
|
||||||
When contributing a pull request to the main branch, please sign your commits
|
|
||||||
with a PGP key and add your name and the year to the bottom of the list of
|
Copyright Information
|
||||||
copyright holders for the file. For example, an existing copyright header might
|
=====================
|
||||||
read:
|
|
||||||
|
When editing a file, create a copyright statement correlated to your
|
||||||
|
identity so that it is easier to keep track of who has touched what file.
|
||||||
|
Pseudonymous contributions are welcome (and encouraged). Place new copyright
|
||||||
|
information below existing copyright information. If there is an existing
|
||||||
|
copyright statement:
|
||||||
|
|
||||||
* Copyright (c) 2022–2023 Emma Tebibyte <emma@tebibyte.media>
|
* Copyright (c) 2022–2023 Emma Tebibyte <emma@tebibyte.media>
|
||||||
|
|
||||||
You would add your name below it like this:
|
you would add your name below it like this:
|
||||||
|
|
||||||
* Copyright (c) 2022–2023 Emma Tebibyte <emma@tebibyte.media>
|
* Copyright (c) 2022–2023 Emma Tebibyte <emma@tebibyte.media>
|
||||||
* Copyright (c) 20XX Your Name <your e-mail address or website>
|
* Copyright (c) 20XX Your Name <your e-mail address or website>
|
||||||
|
|
||||||
We accept contributions from people using aliases.
|
|
||||||
|
|
||||||
Only list years in which you modified the source file. For example:
|
Only list years in which you modified the source file. For example:
|
||||||
|
|
||||||
* Copyright (c) 2020–2021, 2023 Your Name <your-address@example.com>
|
* Copyright (c) 2020–2021, 2023 Your Name <your-address@example.com>
|
||||||
|
|
||||||
This header shows that “Your Name” worked on this source file in 2020, 2021, and
|
This header shows that “Your Name” worked on this source file in 2020, 2021, and
|
||||||
2023. Please use the en dash (“–”) to separate the years in the copyright
|
2023. Please use the en dash (“–”, U+2013) to separate consecutive years in the
|
||||||
notice.
|
copyright notice.
|
||||||
|
|
||||||
If you are contributing a new file, please prepend the following license header
|
If you are contributing a new file, please prepend the following license header
|
||||||
text to it, replacing the proper text on the copyright line:
|
text to it, replacing the proper text on the copyright line:
|
||||||
@@ -92,6 +95,10 @@ notice:
|
|||||||
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
* USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
Style
|
||||||
|
=====
|
||||||
|
|
||||||
Make sure lines never exceed 80 columns in width when using four-character
|
Make sure lines never exceed 80 columns in width when using four-character
|
||||||
indentation steps. This helps contributors with smaller screens, those using
|
indentation steps. This helps contributors with smaller screens, those using
|
||||||
side-by-side editor windows or panes, and those who have no text wrapping in
|
side-by-side editor windows or panes, and those who have no text wrapping in
|
||||||
@@ -99,12 +106,18 @@ their editor or terminal.
|
|||||||
|
|
||||||
For usage text and help messages, do not implement a -h option. Instead, print
|
For usage text and help messages, do not implement a -h option. Instead, print
|
||||||
usage information when any erroneous option is specified. Follow the NetBSD
|
usage information when any erroneous option is specified. Follow the NetBSD
|
||||||
style guide for the usage text’s output format [1].
|
style guide for the usage text’s output format [0].
|
||||||
|
|
||||||
[1] <http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/share/misc/style>
|
If committing a new utility, please include tests and documentation (see
|
||||||
|
tests/ and docs/) for the new tool.
|
||||||
|
|
||||||
If committing a new source file, format the commit message following these
|
Committing
|
||||||
guidelines:
|
==========
|
||||||
|
|
||||||
|
When contributing to Bonsai, please sign your commit with a PGP key and create
|
||||||
|
the commit with an identity which can be easily contacted.
|
||||||
|
|
||||||
|
Format commit messages following these guidelines:
|
||||||
|
|
||||||
$ git commit -m 'tool(1): add feature x'
|
$ git commit -m 'tool(1): add feature x'
|
||||||
|
|
||||||
@@ -130,6 +143,13 @@ $ git commit -m 'tool(1): fix #42 & add feature x'
|
|||||||
|
|
||||||
Commit messages should be written in the present tense.
|
Commit messages should be written in the present tense.
|
||||||
|
|
||||||
|
|
||||||
|
References
|
||||||
|
==========
|
||||||
|
|
||||||
|
[0] <http://cvsweb.netbsd.org/bsdweb.cgi/~checkout~/src/share/misc/style>
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
This work © 2023–2024 by Emma Tebibyte is licensed under CC BY-SA 4.0. To view a
|
This work © 2023–2024 by Emma Tebibyte is licensed under CC BY-SA 4.0. To view a
|
||||||
copy of this license, visit <http://creativecommons.org/licenses/by-sa/4.0/>
|
copy of this license, visit <http://creativecommons.org/licenses/by-sa/4.0/>
|
||||||
|
|||||||
64
Makefile
64
Makefile
@@ -16,23 +16,33 @@
|
|||||||
DESTDIR ?= dist
|
DESTDIR ?= dist
|
||||||
PREFIX ?= /usr/local
|
PREFIX ?= /usr/local
|
||||||
|
|
||||||
|
# for conditionally compiling OS features
|
||||||
|
OS != uname
|
||||||
|
OS_INCLUDE != test -e include/$(OS).mk && printf 'include/$(OS).mk\n' \
|
||||||
|
|| printf '/dev/null\n'
|
||||||
|
|
||||||
# normalized prefix
|
# normalized prefix
|
||||||
PREFIX_N != (test -d $(PREFIX) && [ '-' != $(PREFIX) ] \
|
PREFIX_N != realpath $(PREFIX)
|
||||||
&& CDPATH= cd -P -- $(PREFIX) && pwd -P)
|
MANDIR != test $(PREFIX_N) = / && printf '/usr/share/man\n' \
|
||||||
MANDIR != [ $(PREFIX_N) = / ] && printf '/usr/share/man\n' \
|
|| printf '$(PREFIX_N)/man\n'
|
||||||
|| printf '/share/man\n'
|
|
||||||
SYSEXITS != printf '\043include <sysexits.h>\n' | cpp -M - | tr ' ' '\n' \
|
SYSEXITS != printf '\043include <sysexits.h>\n' | cpp -M - | tr ' ' '\n' \
|
||||||
| sed -n 's/sysexits\.h//p' || printf 'include\n'
|
| sed -n 's/sysexits\.h//p' || printf 'include\n'
|
||||||
|
|
||||||
CC ?= cc
|
CC ?= cc
|
||||||
RUSTC ?= rustc
|
RUSTC ?= rustc
|
||||||
RUSTLIBS = --extern getopt=build/o/libgetopt.rlib \
|
RUSTFLAGS += --extern getopt=build/o/libgetopt.rlib \
|
||||||
--extern sysexits=build/o/libsysexits.rlib \
|
--extern strerror=build/o/libstrerror.rlib \
|
||||||
--extern strerror=build/o/libstrerror.rlib
|
--extern sysexits=build/o/libsysexits.rlib
|
||||||
CFLAGS += -I$(SYSEXITS)
|
CFLAGS += -I$(SYSEXITS)
|
||||||
|
|
||||||
|
# testing requires the absolute path to the bin directory set
|
||||||
|
BIN = build/bin
|
||||||
|
|
||||||
|
.PHONY: default
|
||||||
|
default: all test
|
||||||
|
|
||||||
.PHONY: all
|
.PHONY: all
|
||||||
all: dj false fop hru intcmp mm npc rpn scrut str strcmp swab true
|
all: dj false fop hru intcmp mm npc peek rpn scrut str strcmp swab true
|
||||||
|
|
||||||
# keep build/include until bindgen(1) has stdin support
|
# keep build/include until bindgen(1) has stdin support
|
||||||
# https://github.com/rust-lang/rust-bindgen/issues/2703
|
# https://github.com/rust-lang/rust-bindgen/issues/2703
|
||||||
@@ -44,18 +54,20 @@ clean:
|
|||||||
rm -rf build dist
|
rm -rf build dist
|
||||||
|
|
||||||
dist: all docs
|
dist: all docs
|
||||||
mkdir -p $(DESTDIR)/$(PREFIX)/bin $(DESTDIR)/$(PREFIX)/$(MANDIR)/man1
|
mkdir -p $(DESTDIR)/$(PREFIX_N)/bin $(DESTDIR)/$(MANDIR)/man1
|
||||||
cp build/bin/* $(DESTDIR)/$(PREFIX)/bin
|
cp build/bin/* $(DESTDIR)/$(PREFIX_N)/bin
|
||||||
cp build/docs/*.1 $(DESTDIR)/$(PREFIX)/$(MANDIR)/man1
|
cp build/docs/*.1 $(DESTDIR)/$(MANDIR)/man1
|
||||||
|
|
||||||
.PHONY: install
|
.PHONY: install
|
||||||
install: dist
|
install: dist
|
||||||
cp -r $(DESTDIR)/* /
|
cp -r $(DESTDIR)/* /
|
||||||
|
|
||||||
|
include tests/tests.mk
|
||||||
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test: build /tmp/getopt
|
test: all $(TESTS) /tmp/getopt
|
||||||
|
@echo $(TESTS)
|
||||||
/tmp/getopt
|
/tmp/getopt
|
||||||
tests/posix-compat.sh
|
|
||||||
|
|
||||||
/tmp/getopt: src/libgetopt.rs
|
/tmp/getopt: src/libgetopt.rs
|
||||||
$(RUSTC) --test -o /tmp/getopt src/libgetopt.rs
|
$(RUSTC) --test -o /tmp/getopt src/libgetopt.rs
|
||||||
@@ -67,9 +79,12 @@ docs: docs/ build
|
|||||||
"s/X\.X\.X/$$(git describe --tags --long | cut -d'-' -f1)/g")"; \
|
"s/X\.X\.X/$$(git describe --tags --long | cut -d'-' -f1)/g")"; \
|
||||||
sed "s/$$original/$$title/g" <"$$file" >"build/$$file"; done
|
sed "s/$$original/$$title/g" <"$$file" >"build/$$file"; done
|
||||||
|
|
||||||
|
# include OS feature libraries for compilation
|
||||||
|
include $(OS_INCLUDE)
|
||||||
|
|
||||||
.PHONY: rustlibs
|
.PHONY: rustlibs
|
||||||
rustlibs: build/o/libsysexits.rlib build/o/libgetopt.rlib \
|
rustlibs: build/o/libgetopt.rlib build/o/libstrerror.rlib \
|
||||||
build/o/libstrerror.rlib
|
build/o/libsysexits.rlib $(OSLIB)
|
||||||
|
|
||||||
build/o/libgetopt.rlib: build src/libgetopt.rs
|
build/o/libgetopt.rlib: build src/libgetopt.rs
|
||||||
$(RUSTC) $(RUSTFLAGS) --crate-type=lib --crate-name=getopt \
|
$(RUSTC) $(RUSTFLAGS) --crate-type=lib --crate-name=getopt \
|
||||||
@@ -100,32 +115,37 @@ build/bin/false: src/false.c build
|
|||||||
.PHONY: fop
|
.PHONY: fop
|
||||||
fop: build/bin/fop
|
fop: build/bin/fop
|
||||||
build/bin/fop: src/fop.rs build rustlibs
|
build/bin/fop: src/fop.rs build rustlibs
|
||||||
$(RUSTC) $(RUSTFLAGS) $(RUSTLIBS) -o $@ src/fop.rs
|
$(RUSTC) $(RUSTFLAGS) -o $@ src/fop.rs
|
||||||
|
|
||||||
.PHONY: hru
|
.PHONY: hru
|
||||||
hru: build/bin/hru
|
hru: build/bin/hru
|
||||||
build/bin/hru: src/hru.rs build rustlibs
|
build/bin/hru: src/hru.rs build rustlibs
|
||||||
$(RUSTC) $(RUSTFLAGS) $(RUSTLIBS) -o $@ src/hru.rs
|
$(RUSTC) $(RUSTFLAGS) -o $@ src/hru.rs
|
||||||
|
|
||||||
.PHONY: intcmp
|
.PHONY: intcmp
|
||||||
intcmp: build/bin/intcmp
|
intcmp: build/bin/intcmp
|
||||||
build/bin/intcmp: src/intcmp.rs build rustlibs
|
build/bin/intcmp: src/intcmp.rs build rustlibs
|
||||||
$(RUSTC) $(RUSTFLAGS) $(RUSTLIBS) -o $@ src/intcmp.rs
|
$(RUSTC) $(RUSTFLAGS) -o $@ src/intcmp.rs
|
||||||
|
|
||||||
.PHONY: mm
|
.PHONY: mm
|
||||||
mm: build/bin/mm
|
mm: build/bin/mm
|
||||||
build/bin/mm: src/mm.rs build rustlibs
|
build/bin/mm: src/mm.rs build rustlibs
|
||||||
$(RUSTC) $(RUSTFLAGS) $(RUSTLIBS) -o $@ src/mm.rs
|
$(RUSTC) $(RUSTFLAGS) -o $@ src/mm.rs
|
||||||
|
|
||||||
.PHONY: npc
|
.PHONY: npc
|
||||||
npc: build/bin/npc
|
npc: build/bin/npc
|
||||||
build/bin/npc: src/npc.c build
|
build/bin/npc: src/npc.c build
|
||||||
$(CC) $(CFLAGAS) -o $@ src/npc.c
|
$(CC) $(CFLAGS) -o $@ src/npc.c
|
||||||
|
|
||||||
|
.PHONY: peek
|
||||||
|
peek: build/bin/peek
|
||||||
|
build/bin/peek: src/peek.c build
|
||||||
|
$(CC) $(CFLAGS) -o $@ src/peek.c
|
||||||
|
|
||||||
.PHONY: rpn
|
.PHONY: rpn
|
||||||
rpn: build/bin/rpn
|
rpn: build/bin/rpn
|
||||||
build/bin/rpn: src/rpn.rs build rustlibs
|
build/bin/rpn: src/rpn.rs build rustlibs
|
||||||
$(RUSTC) $(RUSTFLAGS) $(RUSTLIBS) -o $@ src/rpn.rs
|
$(RUSTC) $(RUSTFLAGS) -o $@ src/rpn.rs
|
||||||
|
|
||||||
.PHONY: scrut
|
.PHONY: scrut
|
||||||
scrut: build/bin/scrut
|
scrut: build/bin/scrut
|
||||||
@@ -145,7 +165,7 @@ build/bin/strcmp: src/strcmp.c build
|
|||||||
.PHONY: swab
|
.PHONY: swab
|
||||||
swab: build/bin/swab
|
swab: build/bin/swab
|
||||||
build/bin/swab: src/swab.rs build rustlibs
|
build/bin/swab: src/swab.rs build rustlibs
|
||||||
$(RUSTC) $(RUSTFLAGS) $(RUSTLIBS) -o $@ src/swab.rs
|
$(RUSTC) $(RUSTFLAGS) -o $@ src/swab.rs
|
||||||
|
|
||||||
.PHONY: true
|
.PHONY: true
|
||||||
true: build/bin/true
|
true: build/bin/true
|
||||||
|
|||||||
49
README
49
README
@@ -1,24 +1,28 @@
|
|||||||
“Seek not to walk the path of the masters; seek what they sought.”
|
“Seek not to walk the path of the masters; seek what they sought.”
|
||||||
– Matsuo Basho
|
– Matsuo Basho
|
||||||
|
|
||||||
The Bonsai harakit utilities are a replacement for standard POSIX utilities
|
Bonsai’s Harakit is an alternative to the standard POSIX utilities that aims to
|
||||||
which aim to fill its niche while expanding on their capabilities. These new
|
be simpler, easier, and more powerful. These tools are the result of careful
|
||||||
tools are the result of the careful examination of the current state of POSIX
|
examination of the current state common Unix utilities, POSIX-compliant and
|
||||||
and Unix utilies. The Unix Philosophy of “do one thing and do it well” are their
|
otherwise, following frustrations with design decisions and implementation
|
||||||
core but they avoid clinging to the past.
|
details. They represent a vision of accomplishing everyday use cases with tools
|
||||||
|
that follow the Unix philosophy of “do one thing and do it well” without
|
||||||
|
clinging to the past.
|
||||||
|
|
||||||
The era of the original Unix tools has been long and fruitful, but they have
|
The intent of Harakit is not to conform to or extend POSIX, like the GNU or BSD
|
||||||
their flaws. This project originated from frustrations with the way certain
|
utilities do, but to invent new utilities to perform the same tasks in more
|
||||||
tools work and how other projects that extend POSIX don’t make anything better.
|
intuitive ways. GNU and BSD extensions are convenient but often unhealthy,
|
||||||
|
forgetting the purposes of the tools they extend, or building into existing
|
||||||
This project will not follow in the footsteps of GNU; extensions of POSIX will
|
utilities features that would be more useful as their own tools to be used
|
||||||
not be found here. GNU extensions are a gateway to the misuse of the shell. The
|
anywhere. Other utility sets aim to provide a number of fully-featured
|
||||||
harakit utilities will intentionally discourage use of the shell for purposes
|
programs to be used individually, Harakit utilities are meant to be easily
|
||||||
beyond its scope.
|
composable and work together in pipelines.
|
||||||
|
|
||||||
See docs/ for more on the specific utilities currently implemented.
|
See docs/ for more on the specific utilities currently implemented.
|
||||||
|
|
||||||
|
|
||||||
Building
|
Building
|
||||||
|
========
|
||||||
|
|
||||||
Harakit utilities require a POSIX-compliant environment to compile, including a
|
Harakit utilities require a POSIX-compliant environment to compile, including a
|
||||||
C compiler and preprocessor (cc(1) and cpp(1) by default), an edition 2023 Rust
|
C compiler and preprocessor (cc(1) and cpp(1) by default), an edition 2023 Rust
|
||||||
@@ -43,7 +47,22 @@ To remove all build and distributable files:
|
|||||||
|
|
||||||
$ make clean
|
$ make clean
|
||||||
|
|
||||||
|
|
||||||
|
Contributing
|
||||||
|
============
|
||||||
|
|
||||||
|
See the CONTRIBUTING file for contribution guidelines.
|
||||||
|
|
||||||
|
|
||||||
|
Community
|
||||||
|
=========
|
||||||
|
|
||||||
|
xmpp://bonsai@covenant.murderu.us
|
||||||
|
irc://feeling.murderu.us/#bonsai
|
||||||
|
|
||||||
|
|
||||||
Read More
|
Read More
|
||||||
|
=========
|
||||||
|
|
||||||
An Introduction to the Unix Shell
|
An Introduction to the Unix Shell
|
||||||
<https://porkmail.org/era/unix/shell>
|
<https://porkmail.org/era/unix/shell>
|
||||||
@@ -57,6 +76,10 @@ Master Foo Discourses on the Unix-Nature
|
|||||||
Shell Programming!
|
Shell Programming!
|
||||||
<https://tldp.org/LDP/abs/html/why-shell.html>
|
<https://tldp.org/LDP/abs/html/why-shell.html>
|
||||||
|
|
||||||
|
UNIX Style, or cat -v Considered Harmful
|
||||||
|
<http://harmful.cat-v.org/cat-v/>
|
||||||
|
|
||||||
|
|
||||||
--
|
--
|
||||||
Copyright © 2023–2024 Emma Tebibyte <emma@tebibyte.media>
|
Copyright © 2023–2024 Emma Tebibyte <emma@tebibyte.media>
|
||||||
Copyright © 2024 DTB <trinity@trinity.moe>
|
Copyright © 2024 DTB <trinity@trinity.moe>
|
||||||
|
|||||||
111
docs/peek.1
Normal file
111
docs/peek.1
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
.\" Copyright (c) 2023-2024 DTB <trinity@trinity.moe>
|
||||||
|
.\" Copyright (c) 2024 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 PEEK 1 2024-08-14 "Harakit X.X.X"
|
||||||
|
.SH NAME
|
||||||
|
peek \(en read from the standard input, furtively
|
||||||
|
.\"
|
||||||
|
.SH SYNOPSIS
|
||||||
|
|
||||||
|
peek
|
||||||
|
.RB [ -i ]
|
||||||
|
.\"
|
||||||
|
.SH DESCRIPTION
|
||||||
|
|
||||||
|
Read input from the standard input with terminal echo disabled.
|
||||||
|
.\"
|
||||||
|
.SH OPTIONS
|
||||||
|
|
||||||
|
.IP \fB-i\fP
|
||||||
|
Allows input to come from sources other than terminals (pipes).
|
||||||
|
.\"
|
||||||
|
.SH DIAGNOSTICS
|
||||||
|
|
||||||
|
In the event of an error, a debug message will be printed and the program will
|
||||||
|
exit with the appropriate
|
||||||
|
.BR sysexits.h (3)
|
||||||
|
error code.
|
||||||
|
|
||||||
|
In order to ensure the user\(cqs terminal is still usable after premature
|
||||||
|
termination, the program attempts to handle the SIGINT signal; if it cannot,
|
||||||
|
an error message is printed and execution continues. If the program is
|
||||||
|
interrupted, it exits unsuccessfully without an error message.
|
||||||
|
.\"
|
||||||
|
.SH RATIONALE
|
||||||
|
|
||||||
|
This tool was originally written to accept passwords in shell scripts as an
|
||||||
|
extremely simple alternative to the GNU Privacy Guard project\(cqs
|
||||||
|
.BR pinentry (1)
|
||||||
|
utility.
|
||||||
|
|
||||||
|
Accepting input without showing what is being typed is useful when keying in
|
||||||
|
secrets in public settings or in places where surveillance cameras are
|
||||||
|
installed.
|
||||||
|
.\"
|
||||||
|
.SH CAVEATS
|
||||||
|
|
||||||
|
This program does nothing to prevent others from seeing the key presses input to
|
||||||
|
a keyboard. It also does not protect against the sound of typing being analyzed
|
||||||
|
to determine what was input without needing to see screen or keyboard.
|
||||||
|
|
||||||
|
Accepting secrets in shell scripts is probably not advisable.
|
||||||
|
|
||||||
|
On systems that support it, the
|
||||||
|
.BR ioctl (2)
|
||||||
|
command TIOCSTI can be used to insert characters into the standard input. This
|
||||||
|
doesn't allow snooping but can be used for general mischief.
|
||||||
|
.\"
|
||||||
|
.SH EXAMPLES
|
||||||
|
|
||||||
|
This is an
|
||||||
|
.BR sh (1p)
|
||||||
|
command line that hashes a given password. It uses
|
||||||
|
.BR head (1p)
|
||||||
|
to only accept one line of input,
|
||||||
|
.BR xargs (1p)
|
||||||
|
and
|
||||||
|
.BR printf (1p)
|
||||||
|
to strip the trailing newline,
|
||||||
|
.BR htpasswd (1)
|
||||||
|
from Apache\(cqs utilities to hash the input with the bcrypt algorithm, and
|
||||||
|
.BR cut (1p)
|
||||||
|
to print only the resulting hash:
|
||||||
|
|
||||||
|
.RS
|
||||||
|
$ peek | head -n 1 | xargs printf '%s' | htpasswd -nBi _ | cut -d : -f 2
|
||||||
|
.RE
|
||||||
|
|
||||||
|
This is an
|
||||||
|
.BR sh (1p)
|
||||||
|
command line that allows a user to write blindly into a text file but displaying
|
||||||
|
only written lines. Some writers have the habit of prematurely revising their
|
||||||
|
work and use tools with functionality similar to this to prevent it.
|
||||||
|
It uses
|
||||||
|
.BR mm (1)
|
||||||
|
to pipe the output of the program to both the standard error and the regular
|
||||||
|
file writing.txt:
|
||||||
|
|
||||||
|
.RS
|
||||||
|
$ echo Input ^D to quit. && peek | mm -eo - >writing.txt
|
||||||
|
.RE
|
||||||
|
.\"
|
||||||
|
.SH AUTHOR
|
||||||
|
|
||||||
|
Written by DTB
|
||||||
|
.MT trinity@trinity.moe
|
||||||
|
.ME .
|
||||||
|
.\"
|
||||||
|
.SH COPYRIGHT
|
||||||
|
|
||||||
|
Copyright \(co 2023-2024 DTB. License AGPLv3+: GNU AGPL version 3 or later
|
||||||
|
<https://gnu.org/licenses/gpl.html>.
|
||||||
|
.\"
|
||||||
|
.SH SEE ALSO
|
||||||
|
.BR ioctl (2),
|
||||||
|
.BR ioctl_tty (2),
|
||||||
|
.BR read (1p),
|
||||||
|
.BR sh (1p),
|
||||||
|
.BR stty (1p)
|
||||||
@@ -33,6 +33,7 @@ line:
|
|||||||
.RS
|
.RS
|
||||||
printf 'hello world!\(rsn' | swab
|
printf 'hello world!\(rsn' | swab
|
||||||
.RE
|
.RE
|
||||||
|
.\" If you change this, make sure to change it in tests/bonsai/swab.mk too.
|
||||||
|
|
||||||
Produces the following output:
|
Produces the following output:
|
||||||
|
|
||||||
|
|||||||
6
include/FreeBSD.mk
Normal file
6
include/FreeBSD.mk
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
# Copyright (c) 2024 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.
|
||||||
13
include/OpenBSD.mk
Normal file
13
include/OpenBSD.mk
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
OSLIB = build/o/libopenbsd.rlib
|
||||||
|
RUSTFLAGS += --extern openbsd=$(OSLIB)
|
||||||
|
|
||||||
|
$(OSLIB): src/libopenbsd.rs
|
||||||
|
$(RUSTC) $(RUSTFLAGS) --crate-type=lib --crate-name=openbsd \
|
||||||
|
-o $@ src/libopenbsd.rs
|
||||||
25
src/dj.c
25
src/dj.c
@@ -26,7 +26,8 @@
|
|||||||
#include <string.h> /* memcpy(3), memmove(3), memset(3) */
|
#include <string.h> /* memcpy(3), memmove(3), memset(3) */
|
||||||
#include <sysexits.h> /* EX_OK, EX_OSERR, EX_USAGE */
|
#include <sysexits.h> /* EX_OK, EX_OSERR, EX_USAGE */
|
||||||
#include <unistd.h> /* close(2), getopt(3), lseek(2), read(2), write(2),
|
#include <unistd.h> /* close(2), getopt(3), lseek(2), read(2), write(2),
|
||||||
* optarg, optind, STDIN_FILENO, STDOUT_FILENO */
|
* pledge(2), unveil(2), optarg, optind, STDIN_FILENO,
|
||||||
|
* STDOUT_FILENO */
|
||||||
#include <sys/stat.h> /* S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR */
|
#include <sys/stat.h> /* S_IRGRP, S_IROTH, S_IRUSR, S_IWGRP, S_IWOTH, S_IWUSR */
|
||||||
|
|
||||||
char *program_name = "dj";
|
char *program_name = "dj";
|
||||||
@@ -107,8 +108,10 @@ Io_write(struct Io *io) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
oserr(char *e, int n) {
|
oserr(char *e, int n) { /* program_name: [failing component:] error */
|
||||||
(void)fprintf(stderr, "%s: %s: %s\n", program_name, e, strerror(n));
|
(void)fprintf(stderr, "%s: ", program_name);
|
||||||
|
if (e != NULL) { (void)fprintf(stderr, "%s: ", e); }
|
||||||
|
(void)fprintf(stderr, "%s\n", strerror(n));
|
||||||
|
|
||||||
return EX_OSERR;
|
return EX_OSERR;
|
||||||
}
|
}
|
||||||
@@ -174,6 +177,12 @@ int main(int argc, char *argv[]) {
|
|||||||
bool retry; /* false if exits on partial reads or writes */
|
bool retry; /* false if exits on partial reads or writes */
|
||||||
struct Io io[2 /* { in, out } */];
|
struct Io io[2 /* { in, out } */];
|
||||||
|
|
||||||
|
#ifdef __OpenBSD__
|
||||||
|
if (pledge("cpath rpath stdio unveil wpath", NULL) == -1) {
|
||||||
|
return oserr(NULL, errno);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* Set defaults. */
|
/* Set defaults. */
|
||||||
align = -1;
|
align = -1;
|
||||||
count = -1;
|
count = -1;
|
||||||
@@ -209,6 +218,12 @@ int main(int argc, char *argv[]) {
|
|||||||
} else {
|
} else {
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
|
#ifdef __OpenBSD__
|
||||||
|
if (unveil(optarg, i == 0 ? "r" : "wc") == -1) {
|
||||||
|
return oserr(NULL, errno);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (
|
if (
|
||||||
(fd = open(optarg, io[i].fl, creat_mode)) != -1
|
(fd = open(optarg, io[i].fl, creat_mode)) != -1
|
||||||
&& (fdisstd(io[i].fd) || close(io[i].fd) == 0)
|
&& (fdisstd(io[i].fd) || close(io[i].fd) == 0)
|
||||||
@@ -249,6 +264,10 @@ int main(int argc, char *argv[]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifdef __OpenBSD__
|
||||||
|
if (unveil(NULL, NULL) == -1) { return oserr(NULL, errno); }
|
||||||
|
#endif
|
||||||
|
|
||||||
assert(io->fd != STDIN_FILENO || io->fl == read_flags);
|
assert(io->fd != STDIN_FILENO || io->fl == read_flags);
|
||||||
assert(io->fd != STDOUT_FILENO || io->fl == write_flags);
|
assert(io->fd != STDOUT_FILENO || io->fl == write_flags);
|
||||||
|
|
||||||
|
|||||||
14
src/false.c
14
src/false.c
@@ -1,9 +1,19 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
* Copyright (c) 2023–2024 Emma Tebibyte <emma@tebibyte.media>
|
||||||
|
* Copyright (c) 2024 DTB <trinity@trinity.moe>
|
||||||
* SPDX-License-Identifier: CC0
|
* SPDX-License-Identifier: CC0
|
||||||
*
|
*
|
||||||
* This work is marked with CC0 1.0. To view a copy of this license, visit
|
* This work is marked with CC0 1.0. To view a copy of this license, visit
|
||||||
* <http://creativecommons.org/publicdomain/zero/1.0>.
|
* <http://creativecommons.org/publicdomain/zero/1.0>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int main() { return 1; }
|
#ifdef __OpenBSD__
|
||||||
|
# include <unistd.h> /* pledge(2) */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
#ifdef __OpenBSD__
|
||||||
|
pledge(NULL, NULL);
|
||||||
|
#endif
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|||||||
12
src/fop.rs
12
src/fop.rs
@@ -30,11 +30,23 @@ use getopt::GetOpt;
|
|||||||
use strerror::StrError;
|
use strerror::StrError;
|
||||||
use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE };
|
use sysexits::{ EX_DATAERR, EX_IOERR, EX_UNAVAILABLE, EX_USAGE };
|
||||||
|
|
||||||
|
#[cfg(target_os="openbsd")] use sysexits::EX_OSERR;
|
||||||
|
#[cfg(target_os="openbsd")] extern crate openbsd;
|
||||||
|
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge };
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let argv = args().collect::<Vec<String>>();
|
let argv = args().collect::<Vec<String>>();
|
||||||
let mut d = '\u{1E}'.to_string(); /* ASCII record separator */
|
let mut d = '\u{1E}'.to_string(); /* ASCII record separator */
|
||||||
let mut optind = 1;
|
let mut optind = 1;
|
||||||
|
|
||||||
|
#[cfg(target_os="openbsd")] {
|
||||||
|
let promises = Promises::new("stdio proc exec");
|
||||||
|
if let Err(e) = pledge(Some(promises), None) {
|
||||||
|
eprintln!("{}: {}", argv[0], e.strerror());
|
||||||
|
exit(EX_OSERR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let usage = format!(
|
let usage = format!(
|
||||||
"Usage: {} [-d delimiter] index command [args...]",
|
"Usage: {} [-d delimiter] index command [args...]",
|
||||||
argv[0],
|
argv[0],
|
||||||
|
|||||||
20
src/hru.rs
20
src/hru.rs
@@ -27,7 +27,11 @@ extern crate strerror;
|
|||||||
extern crate sysexits;
|
extern crate sysexits;
|
||||||
|
|
||||||
use strerror::StrError;
|
use strerror::StrError;
|
||||||
use sysexits::{ EX_DATAERR, EX_IOERR, EX_SOFTWARE };
|
use sysexits::{ EX_DATAERR, EX_IOERR, EX_SOFTWARE, EX_USAGE };
|
||||||
|
|
||||||
|
#[cfg(target_os="openbsd")] use sysexits::EX_OSERR;
|
||||||
|
#[cfg(target_os="openbsd")] extern crate openbsd;
|
||||||
|
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge };
|
||||||
|
|
||||||
/* list of SI prefixes */
|
/* list of SI prefixes */
|
||||||
const LIST: [(u32, &str); 10] = [
|
const LIST: [(u32, &str); 10] = [
|
||||||
@@ -76,6 +80,20 @@ fn convert(input: u128) -> Result<(f64, (u32, &'static str)), String> {
|
|||||||
|
|
||||||
fn main() -> ExitCode {
|
fn main() -> ExitCode {
|
||||||
let argv = args().collect::<Vec<String>>();
|
let argv = args().collect::<Vec<String>>();
|
||||||
|
|
||||||
|
if let Some(_) = argv.get(1) {
|
||||||
|
eprintln!("Usage: {}", argv[0]);
|
||||||
|
return ExitCode::from(EX_USAGE as u8);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_os="openbsd")] {
|
||||||
|
let promises = Promises::new("stdio");
|
||||||
|
if let Err(e) = pledge(Some(promises), None) {
|
||||||
|
eprintln!("{}: {}", argv[0], e.strerror());
|
||||||
|
return ExitCode::from(EX_OSERR as u8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
|
|
||||||
while let Ok(_) = stdin().read_line(&mut buf) {
|
while let Ok(_) = stdin().read_line(&mut buf) {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2023–2024 DTB <trinity@trinity.moe>
|
* Copyright (c) 2023–2024 DTB <trinity@trinity.moe>
|
||||||
|
* Copyright (c) 2024 Emma Tebibyte <emma@tebibyte.media>
|
||||||
* SPDX-License-Identifier: AGPL-3.0-or-later
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
||||||
*
|
*
|
||||||
* This program is free software: you can redistribute it and/or modify it under
|
* This program is free software: you can redistribute it and/or modify it under
|
||||||
@@ -22,11 +23,17 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
extern crate getopt;
|
extern crate getopt;
|
||||||
use getopt::GetOpt;
|
|
||||||
|
|
||||||
extern crate sysexits;
|
extern crate sysexits;
|
||||||
|
|
||||||
|
use getopt::GetOpt;
|
||||||
use sysexits::EX_USAGE;
|
use sysexits::EX_USAGE;
|
||||||
|
|
||||||
|
#[cfg(target_os="openbsd")] use sysexits::EX_OSERR;
|
||||||
|
#[cfg(target_os="openbsd")] extern crate openbsd;
|
||||||
|
#[cfg(target_os="openbsd")] extern crate strerror;
|
||||||
|
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge };
|
||||||
|
#[cfg(target_os="openbsd")] use strerror::StrError;
|
||||||
|
|
||||||
fn usage(s: &str) -> ExitCode {
|
fn usage(s: &str) -> ExitCode {
|
||||||
eprintln!("Usage: {} [-egl] integer integer...", s);
|
eprintln!("Usage: {} [-egl] integer integer...", s);
|
||||||
ExitCode::from(EX_USAGE as u8)
|
ExitCode::from(EX_USAGE as u8)
|
||||||
@@ -34,6 +41,15 @@ fn usage(s: &str) -> ExitCode {
|
|||||||
|
|
||||||
fn main() -> ExitCode {
|
fn main() -> ExitCode {
|
||||||
let argv = args().collect::<Vec<String>>();
|
let argv = args().collect::<Vec<String>>();
|
||||||
|
|
||||||
|
#[cfg(target_os="openbsd")] {
|
||||||
|
let promises = Promises::new("stdio");
|
||||||
|
if let Err(e) = pledge(Some(promises), None) {
|
||||||
|
eprintln!("{}: {}", argv[0], e.strerror());
|
||||||
|
return ExitCode::from(EX_OSERR as u8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut e = false; /* args can be == */
|
let mut e = false; /* args can be == */
|
||||||
let mut g = false; /* args can be > */
|
let mut g = false; /* args can be > */
|
||||||
let mut l = false; /* args can be < */
|
let mut l = false; /* args can be < */
|
||||||
@@ -46,11 +62,13 @@ fn main() -> ExitCode {
|
|||||||
Ok("e") => e = true,
|
Ok("e") => e = true,
|
||||||
Ok("g") => g = true,
|
Ok("g") => g = true,
|
||||||
Ok("l") => l = true,
|
Ok("l") => l = true,
|
||||||
_ => { return usage(&argv[0]); },
|
_ => return usage(&argv[0]),
|
||||||
}
|
}
|
||||||
optind = opt.ind();
|
optind = opt.ind();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !e & !g & !l { return usage(&argv[0]); }
|
||||||
|
|
||||||
if argv.len() - optind < 2 /* see usage */ { return usage(&argv[0]); }
|
if argv.len() - optind < 2 /* see usage */ { return usage(&argv[0]); }
|
||||||
|
|
||||||
let mut prev: Option<usize> = None; /* no previous operand */
|
let mut prev: Option<usize> = None; /* no previous operand */
|
||||||
@@ -59,8 +77,8 @@ fn main() -> ExitCode {
|
|||||||
for arg in argv.iter().skip(optind) { /* iterate operands */
|
for arg in argv.iter().skip(optind) { /* iterate operands */
|
||||||
match arg.parse::<usize>() { /* parse current operand */
|
match arg.parse::<usize>() { /* parse current operand */
|
||||||
Ok(n) => currn = n,
|
Ok(n) => currn = n,
|
||||||
_ => {
|
Err(e) => {
|
||||||
eprintln!("{}: {}: Invalid integer", &argv[0], arg);
|
eprintln!("{}: {}: {}", &argv[0], arg, e);
|
||||||
return ExitCode::from(EX_USAGE as u8);
|
return ExitCode::from(EX_USAGE as u8);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -79,8 +79,8 @@ pub trait GetOpt {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl GetOpt for Vec<String> {
|
impl GetOpt for Vec<String> {
|
||||||
fn getopt(&self, optstring: &str) -> Option<Opt> {
|
fn getopt(&self, optstring: &str) -> Option<Opt> {
|
||||||
let c_strings: Vec<_> = self
|
let c_strings: Vec<_> = self
|
||||||
.iter()
|
.iter()
|
||||||
.cloned()
|
.cloned()
|
||||||
.map(|x| CString::new(x).unwrap().into_raw())
|
.map(|x| CString::new(x).unwrap().into_raw())
|
||||||
@@ -91,11 +91,11 @@ impl GetOpt for Vec<String> {
|
|||||||
let argv = boxed as *const *mut c_char;
|
let argv = boxed as *const *mut c_char;
|
||||||
|
|
||||||
/* operations are separated out so that everything lives long enough */
|
/* operations are separated out so that everything lives long enough */
|
||||||
let opts = CString::new(optstring).unwrap().into_raw();
|
let opts = CString::new(optstring).unwrap().into_raw();
|
||||||
let len = self.len() as c_int;
|
let len = self.len() as c_int;
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
let ret = match getopt(len, argv, opts) {
|
let ret = match getopt(len, argv, opts) {
|
||||||
/* From getopt(3p):
|
/* From getopt(3p):
|
||||||
*
|
*
|
||||||
* The getopt() function shall return the next option character
|
* The getopt() function shall return the next option character
|
||||||
@@ -141,8 +141,8 @@ impl GetOpt for Vec<String> {
|
|||||||
* argv[optind] points to the string "--"
|
* argv[optind] points to the string "--"
|
||||||
*
|
*
|
||||||
* getopt() shall return -1 after incrementing optind. */
|
* getopt() shall return -1 after incrementing optind. */
|
||||||
-1 => return None,
|
-1 => return None,
|
||||||
opt => {
|
opt => {
|
||||||
let arg: Option<String>;
|
let arg: Option<String>;
|
||||||
|
|
||||||
if optarg.is_null() { arg = None; }
|
if optarg.is_null() { arg = None; }
|
||||||
@@ -162,13 +162,13 @@ impl GetOpt for Vec<String> {
|
|||||||
opt: Ok((opt as u8 as char).to_string()),
|
opt: Ok((opt as u8 as char).to_string()),
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
/* delloc argv (something online said I should do this) */
|
/* delloc argv (something online said I should do this) */
|
||||||
let _ = Box::from_raw(boxed);
|
let _ = Box::from_raw(boxed);
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* tests (good) */
|
/* tests (good) */
|
||||||
|
|||||||
97
src/libopenbsd.rs
Normal file
97
src/libopenbsd.rs
Normal file
@@ -0,0 +1,97 @@
|
|||||||
|
/*
|
||||||
|
* 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/.
|
||||||
|
*/
|
||||||
|
|
||||||
|
use std::{
|
||||||
|
ffi::{ CString, c_char },
|
||||||
|
io::Error,
|
||||||
|
ptr::null,
|
||||||
|
};
|
||||||
|
|
||||||
|
mod openbsd {
|
||||||
|
use std::ffi::{ c_char, c_int };
|
||||||
|
extern "C" {
|
||||||
|
pub fn pledge(arg1: *const c_char, arg2: *const c_char) -> c_int;
|
||||||
|
|
||||||
|
pub fn unveil(arg1: *const c_char, arg2: *const c_char) -> c_int;
|
||||||
|
|
||||||
|
pub fn __errno() -> *mut c_int;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Promises(*const c_char);
|
||||||
|
|
||||||
|
impl Promises {
|
||||||
|
pub fn new(promises: &str) -> Self {
|
||||||
|
let p = CString::new(promises).unwrap();
|
||||||
|
|
||||||
|
Promises(p.into_raw() as *const c_char)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn pledge(
|
||||||
|
promises: Option<Promises>, execpromises: Option<Promises>
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
/* From pledge(2):
|
||||||
|
*
|
||||||
|
* Passing NULL to promises or execpromises specifies to not change
|
||||||
|
* the current value. */
|
||||||
|
let arg1 = promises.unwrap_or(Promises(null())).0;
|
||||||
|
let arg2 = execpromises.unwrap_or(Promises(null())).0;
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
match openbsd::pledge(arg1, arg2) {
|
||||||
|
-1 => Err(Error::from_raw_os_error(*openbsd::__errno())),
|
||||||
|
0 => Ok(()),
|
||||||
|
_ => panic!(), /* unreachable */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct UnveilPerms(CString);
|
||||||
|
|
||||||
|
impl UnveilPerms {
|
||||||
|
pub fn new(permissions: Vec<char>) -> Self {
|
||||||
|
if permissions.is_empty() {
|
||||||
|
return UnveilPerms(CString::new("").unwrap());
|
||||||
|
}
|
||||||
|
|
||||||
|
UnveilPerms(
|
||||||
|
CString::new(permissions.iter().collect::<String>()).unwrap()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn unveil(
|
||||||
|
path: Option<&str>,
|
||||||
|
permissions: Option<UnveilPerms>,
|
||||||
|
) -> Result<(), Error> {
|
||||||
|
let path_c = path.map(CString::new).map(Result::unwrap);
|
||||||
|
let arg1 = path_c.map(|p| p.into_raw() as *const c_char).unwrap_or(null());
|
||||||
|
|
||||||
|
let arg2 = permissions
|
||||||
|
.map(|p| p.0.into_raw() as *const c_char)
|
||||||
|
.unwrap_or(null());
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
match openbsd::unveil(arg1, arg2) {
|
||||||
|
-1 => Err(Error::from_raw_os_error(*openbsd::__errno())),
|
||||||
|
0 => Ok(()),
|
||||||
|
_ => panic!(), /* unreachable */
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
51
src/mm.rs
51
src/mm.rs
@@ -33,6 +33,16 @@ use getopt::GetOpt;
|
|||||||
use strerror::StrError;
|
use strerror::StrError;
|
||||||
use sysexits::{ EX_IOERR, EX_USAGE };
|
use sysexits::{ EX_IOERR, EX_USAGE };
|
||||||
|
|
||||||
|
#[cfg(target_os="openbsd")] use sysexits::EX_OSERR;
|
||||||
|
#[cfg(target_os="openbsd")] extern crate openbsd;
|
||||||
|
#[cfg(target_os="openbsd")]
|
||||||
|
use openbsd::{
|
||||||
|
Promises,
|
||||||
|
UnveilPerms,
|
||||||
|
pledge,
|
||||||
|
unveil,
|
||||||
|
};
|
||||||
|
|
||||||
use ArgMode::*;
|
use ArgMode::*;
|
||||||
|
|
||||||
enum ArgMode { In, Out }
|
enum ArgMode { In, Out }
|
||||||
@@ -41,6 +51,14 @@ fn main() -> ExitCode {
|
|||||||
let argv = args().collect::<Vec<_>>();
|
let argv = args().collect::<Vec<_>>();
|
||||||
let usage = format!("Usage: {} [-aetu] [-i input] [-o output]", argv[0]);
|
let usage = format!("Usage: {} [-aetu] [-i input] [-o output]", argv[0]);
|
||||||
|
|
||||||
|
#[cfg(target_os="openbsd")] {
|
||||||
|
let promises = Promises::new("cpath rpath stdio unveil wpath");
|
||||||
|
if let Err(e) = pledge(Some(promises), None) {
|
||||||
|
eprintln!("{}: {}", argv[0], e.strerror());
|
||||||
|
return ExitCode::from(EX_OSERR as u8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut a = false; /* append to the file */
|
let mut a = false; /* append to the file */
|
||||||
let mut e = false; /* use stderr as an output */
|
let mut e = false; /* use stderr as an output */
|
||||||
let mut t = true; /* do not truncate the file before writing */
|
let mut t = true; /* do not truncate the file before writing */
|
||||||
@@ -62,7 +80,7 @@ fn main() -> ExitCode {
|
|||||||
mode = Some(In); /* latest argument == -i */
|
mode = Some(In); /* latest argument == -i */
|
||||||
},
|
},
|
||||||
Ok("o") => { /* add output */
|
Ok("o") => { /* add output */
|
||||||
let output = opt.arg().unwrap();
|
let output = opt.arg().unwrap();
|
||||||
outs.push(output);
|
outs.push(output);
|
||||||
mode = Some(Out); /* latest argument == -o */
|
mode = Some(Out); /* latest argument == -o */
|
||||||
},
|
},
|
||||||
@@ -86,8 +104,35 @@ fn main() -> ExitCode {
|
|||||||
Out => outs.push(arg.to_string()),
|
Out => outs.push(arg.to_string()),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
} else {
|
}
|
||||||
eprintln!("{}", usage);
|
|
||||||
|
#[cfg(target_os="openbsd")] {
|
||||||
|
for input in &ins {
|
||||||
|
let perms = UnveilPerms::new(vec!['r']);
|
||||||
|
|
||||||
|
if let Err(e) = unveil(Some(&input), Some(perms)) {
|
||||||
|
eprintln!("{}: {}", argv[0], e.strerror());
|
||||||
|
return ExitCode::from(EX_OSERR as u8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for output in &outs {
|
||||||
|
let perms = UnveilPerms::new(vec!['c', 'w']);
|
||||||
|
|
||||||
|
if let Err(e) = unveil(Some(&output), Some(perms)) {
|
||||||
|
eprintln!("{}: {}", argv[0], e.strerror());
|
||||||
|
return ExitCode::from(EX_OSERR as u8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Err(e) = unveil(None, None) {
|
||||||
|
eprintln!("{}: {}", argv[0], e.strerror());
|
||||||
|
return ExitCode::from(EX_OSERR as u8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ins.is_empty() && outs.is_empty() && argv.len() > optind {
|
||||||
|
eprintln!("Usage: {}", usage);
|
||||||
return ExitCode::from(EX_USAGE as u8);
|
return ExitCode::from(EX_USAGE as u8);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
11
src/npc.c
11
src/npc.c
@@ -19,8 +19,8 @@
|
|||||||
|
|
||||||
#include <stdio.h> /* fprintf(3), fputs(3), getc(3), perror(3), putc(3), stdin,
|
#include <stdio.h> /* fprintf(3), fputs(3), getc(3), perror(3), putc(3), stdin,
|
||||||
* stdout, EOF */
|
* stdout, EOF */
|
||||||
#include <sysexits.h> /* EX_IOERR, EX_OK, EX_USAGE */
|
#include <sysexits.h> /* EX_IOERR, EX_OK, EX_OSERR, EX_USAGE */
|
||||||
#include <unistd.h> /* getopt(3) */
|
#include <unistd.h> /* pledge(2), getopt(3) */
|
||||||
|
|
||||||
char *program_name = "npc";
|
char *program_name = "npc";
|
||||||
|
|
||||||
@@ -43,6 +43,13 @@ int main(int argc, char *argv[]) {
|
|||||||
char showend = 0; /* print a dollar sign before each newline */
|
char showend = 0; /* print a dollar sign before each newline */
|
||||||
char showtab = 0; /* prints tab characters in caret notation */
|
char showtab = 0; /* prints tab characters in caret notation */
|
||||||
|
|
||||||
|
#ifdef __OpenBSD__
|
||||||
|
if (pledge("stdio", NULL) == -1) {
|
||||||
|
perror(argv[0] == NULL ? program_name : argv[0]);
|
||||||
|
return EX_OSERR;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (argc > 0) {
|
if (argc > 0) {
|
||||||
program_name = argv[0];
|
program_name = argv[0];
|
||||||
|
|
||||||
|
|||||||
147
src/peek.c
Normal file
147
src/peek.c
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2023–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 <signal.h> /* sigaction(2), signal(2), struct sigaction, SIGINT */
|
||||||
|
#include <stdbool.h> /* bool */
|
||||||
|
#include <stdio.h> /* fprintf(3), fgetc(3), perror(3), fputc(3), stderr, stdin,
|
||||||
|
* stdout, EOF, NULL */
|
||||||
|
#include <stdlib.h> /* exit(3), EXIT_FAILURE */
|
||||||
|
#include <sysexits.h> /* EX_IOERR, EX_OK, EX_USAGE */
|
||||||
|
#include <termios.h> /* tcgetattr(3), tcsetattr(3), struct termios, ECHO */
|
||||||
|
#include <unistd.h> /* getopt(3), isatty(3), pledge(2), unveil(2),
|
||||||
|
* STDIN_FILENO */
|
||||||
|
|
||||||
|
char *program_name = "peek";
|
||||||
|
|
||||||
|
/* Restores terminal echo; otherwise when a user ^Cs the terminal would
|
||||||
|
* continue to not display typed text. If sig isn't zero, this will terminate
|
||||||
|
* the program. */
|
||||||
|
static void
|
||||||
|
restore_echo(int sig) {
|
||||||
|
static struct termios t;
|
||||||
|
|
||||||
|
/* Failure isn't reported because this is the termination routine anyway;
|
||||||
|
* errors will be obvious. */
|
||||||
|
if (tcgetattr(STDIN_FILENO, &t) == 0) {
|
||||||
|
t.c_lflag |= ECHO;
|
||||||
|
(void)tcsetattr(STDIN_FILENO, TCSAFLUSH, &t);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (sig != 0) { exit(EXIT_FAILURE); } /* Terminated by signal. */
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
ioerr(char *argv0) {
|
||||||
|
perror(argv0);
|
||||||
|
restore_echo(0);
|
||||||
|
return EX_IOERR;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
usage(char *argv0) {
|
||||||
|
(void)fprintf(stderr, "Usage: %s [-i]\n", argv0);
|
||||||
|
|
||||||
|
return EX_USAGE;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char *argv[]){
|
||||||
|
bool is_term; /* Is stdin a terminal? */
|
||||||
|
bool must_be_term = 1; /* Must it be? */
|
||||||
|
|
||||||
|
#ifdef __OpenBSD__
|
||||||
|
if (pledge("stdio tty unveil", "") != 0 || unveil(NULL, NULL) != 0) {
|
||||||
|
/* This isn't fatal; these return values could be cast to void just as
|
||||||
|
* easily. */
|
||||||
|
(void)perror(argv[0] == NULL ? argv[0] : program_name);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
is_term = isatty(STDIN_FILENO);
|
||||||
|
|
||||||
|
if (argc > 0) { /* option parsing */
|
||||||
|
int c;
|
||||||
|
|
||||||
|
program_name = argv[0];
|
||||||
|
|
||||||
|
while ((c = getopt(argc, argv, "i")) != -1) {
|
||||||
|
switch (c) {
|
||||||
|
case 'i': must_be_term = 0; break;
|
||||||
|
default: return usage(argv[0]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (argc > optind) { return usage(argv[0]); }
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!is_term && must_be_term) {
|
||||||
|
(void)fprintf(
|
||||||
|
stderr,
|
||||||
|
"%s: Must be run in a terminal (specify -i to skip this check)\n",
|
||||||
|
argv[0]
|
||||||
|
);
|
||||||
|
return EX_USAGE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_term) {
|
||||||
|
{ /* Install signal handler */
|
||||||
|
/* There isn't a difference in functionality between the signal(2)
|
||||||
|
* and sigaction(2) methods. sigaction(2) is vastly preferred for
|
||||||
|
* portability but some older systems only have signal(2). */
|
||||||
|
/* Errors aren't terminating because the worst that happens is some
|
||||||
|
* terminal phooeyness if things go awry. */
|
||||||
|
#if defined _POSIX_C_SOURCE
|
||||||
|
struct sigaction act = { 0 };
|
||||||
|
|
||||||
|
act.sa_handler = restore_echo;
|
||||||
|
if (sigaction(SIGINT, &act, NULL) != 0) { perror(program_name); }
|
||||||
|
#else
|
||||||
|
if (signal(SIGINT, restore_echo) == SIG_ERR) {
|
||||||
|
perror(program_name);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
{ /* Banish terminal echo */
|
||||||
|
/* This terminates when it fails because it's the whole point of
|
||||||
|
* the program. */
|
||||||
|
struct termios t;
|
||||||
|
|
||||||
|
if (tcgetattr(STDIN_FILENO, &t) != 0) {
|
||||||
|
return ioerr(program_name);
|
||||||
|
}
|
||||||
|
t.c_lflag ^= ECHO;
|
||||||
|
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &t) != 0) {
|
||||||
|
return ioerr(program_name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
{ /* Input loop */
|
||||||
|
int c;
|
||||||
|
|
||||||
|
while ((c = fgetc(stdin)) != EOF) {
|
||||||
|
if (fputc(c, stdout) == EOF) { return ioerr(program_name); }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_term) { restore_echo(0); }
|
||||||
|
|
||||||
|
return EX_OK;
|
||||||
|
}
|
||||||
15
src/rpn.rs
15
src/rpn.rs
@@ -56,6 +56,12 @@ extern crate sysexits;
|
|||||||
|
|
||||||
use sysexits::EX_DATAERR;
|
use sysexits::EX_DATAERR;
|
||||||
|
|
||||||
|
#[cfg(target_os="openbsd")] use sysexits::EX_OSERR;
|
||||||
|
#[cfg(target_os="openbsd")] extern crate strerror;
|
||||||
|
#[cfg(target_os="openbsd")] extern crate openbsd;
|
||||||
|
#[cfg(target_os="openbsd")] use strerror::StrError;
|
||||||
|
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge };
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, PartialOrd, Debug)]
|
#[derive(Clone, PartialEq, PartialOrd, Debug)]
|
||||||
/* enum CalcType is a type containing operations used in the calculator */
|
/* enum CalcType is a type containing operations used in the calculator */
|
||||||
enum CalcType {
|
enum CalcType {
|
||||||
@@ -191,6 +197,15 @@ fn round_precise(value: &f64, precision: usize) -> f64 {
|
|||||||
|
|
||||||
fn main() -> ExitCode {
|
fn main() -> ExitCode {
|
||||||
let argv = args().collect::<Vec<String>>();
|
let argv = args().collect::<Vec<String>>();
|
||||||
|
|
||||||
|
#[cfg(target_os="openbsd")] {
|
||||||
|
let promises = Promises::new("stdio");
|
||||||
|
if let Err(e) = pledge(Some(promises), None) {
|
||||||
|
eprintln!("{}: {}", argv[0], e.strerror());
|
||||||
|
return ExitCode::from(EX_OSERR as u8);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut stack = VecDeque::new();
|
let mut stack = VecDeque::new();
|
||||||
let mut buf = String::new();
|
let mut buf = String::new();
|
||||||
/* Set floating-point precision for correcting rounding errors based on
|
/* Set floating-point precision for correcting rounding errors based on
|
||||||
|
|||||||
34
src/scrut.c
34
src/scrut.c
@@ -17,18 +17,21 @@
|
|||||||
* along with this program. If not, see https://www.gnu.org/licenses/.
|
* along with this program. If not, see https://www.gnu.org/licenses/.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <assert.h> /* assert(3) */
|
||||||
#include <stdio.h> /* fprintf(3), stderr, NULL */
|
#include <stdio.h> /* fprintf(3), stderr, NULL */
|
||||||
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
|
#include <stdlib.h> /* EXIT_FAILURE, EXIT_SUCCESS */
|
||||||
#include <string.h> /* memset(3), strchr(3) */
|
#include <string.h> /* memset(3), strchr(3) */
|
||||||
#include <sysexits.h> /* EX_USAGE */
|
#include <sysexits.h> /* EX_OSERR, EX_USAGE */
|
||||||
#include <unistd.h> /* access(3), getopt(3), F_OK, R_OK, W_OK, X_OK */
|
#include <unistd.h> /* access(3), getopt(3), pledge(2), unveil(2), F_OK, R_OK,
|
||||||
|
* W_OK, X_OK */
|
||||||
#include <sys/stat.h> /* lstat(3), stat struct, S_ISBLK, S_ISCHR, S_ISDIR,
|
#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_ISFIFO, S_ISGID, S_ISREG, S_ISLNK, S_ISSOCK,
|
||||||
* S_ISUID, S_ISVTX */
|
* S_ISUID, S_ISVTX */
|
||||||
|
|
||||||
char *program_name = "scrut";
|
char *program_name = "scrut";
|
||||||
#define OPTS "bcdefgkprsuwxLS"
|
#define OPTS "bcdefgkprsuwxLS"
|
||||||
static char *opts = OPTS;
|
/* this is an array so main:sel's size can be known at compile time */
|
||||||
|
static char opts[] = OPTS;
|
||||||
|
|
||||||
static int
|
static int
|
||||||
usage(char *argv0) {
|
usage(char *argv0) {
|
||||||
@@ -40,7 +43,16 @@ usage(char *argv0) {
|
|||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
char sel[(sizeof opts) / (sizeof *opts)];
|
char sel[(sizeof opts) / (sizeof *opts)];
|
||||||
|
|
||||||
if (argc < 2) { return usage(argv[0] == NULL ? program_name : argv[0]); }
|
program_name = argv[0] == NULL ? program_name : argv[0];
|
||||||
|
|
||||||
|
#ifdef __OpenBSD__
|
||||||
|
if (pledge("rpath stdio unveil", NULL) == -1) {
|
||||||
|
perror(program_name);
|
||||||
|
return EX_OSERR;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (argc < 2) { return usage(program_name); }
|
||||||
|
|
||||||
{ /* option parsing */
|
{ /* option parsing */
|
||||||
char *p;
|
char *p;
|
||||||
@@ -48,7 +60,10 @@ int main(int argc, char *argv[]) {
|
|||||||
memset(sel, '\0', sizeof sel);
|
memset(sel, '\0', sizeof sel);
|
||||||
for (int c; (c = getopt(argc, argv, opts)) != -1;) {
|
for (int c; (c = getopt(argc, argv, opts)) != -1;) {
|
||||||
if ((p = strchr(opts, c)) == NULL) { return usage(argv[0]); }
|
if ((p = strchr(opts, c)) == NULL) { return usage(argv[0]); }
|
||||||
else { sel[p - opts] = c; }
|
else {
|
||||||
|
assert(p - opts < sizeof sel / sizeof *sel); /* bounds check */
|
||||||
|
sel[p - opts] = c;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* straighten out selections; permute out nulls */
|
/* straighten out selections; permute out nulls */
|
||||||
@@ -63,9 +78,16 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
if (optind == argc) { return usage(argv[0]); }
|
if (optind == argc) { return usage(argv[0]); }
|
||||||
|
|
||||||
for (argv += optind ; *argv != NULL; ++argv) {
|
for (argv += optind ; *argv != NULL; argv = &argv[1]) {
|
||||||
struct stat buf;
|
struct stat buf;
|
||||||
|
|
||||||
|
#ifdef __OpenBSD__
|
||||||
|
if (unveil(*argv, "r") == -1) {
|
||||||
|
perror(program_name);
|
||||||
|
return EX_OSERR;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if(access(*argv, F_OK) != 0 || lstat(*argv, &buf) == -1) {
|
if(access(*argv, F_OK) != 0 || lstat(*argv, &buf) == -1) {
|
||||||
return EXIT_FAILURE; /* doesn't exist or isn't stattable */
|
return EXIT_FAILURE; /* doesn't exist or isn't stattable */
|
||||||
}
|
}
|
||||||
|
|||||||
18
src/str.c
18
src/str.c
@@ -19,10 +19,14 @@
|
|||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <stddef.h> /* NULL */
|
#include <stddef.h> /* NULL */
|
||||||
#include <stdio.h> /* fprintf(3) */
|
#include <stdio.h> /* fprintf(3), perror(3) */
|
||||||
#include <stdlib.h> /* size_t, EXIT_FAILURE */
|
#include <stdlib.h> /* size_t, EXIT_FAILURE */
|
||||||
#include <string.h> /* strcmp(3) */
|
#include <string.h> /* strcmp(3) */
|
||||||
#include <sysexits.h> /* EX_USAGE */
|
#include <sysexits.h> /* EX_OSERR, EX_USAGE */
|
||||||
|
|
||||||
|
#ifdef __OpenBSD__
|
||||||
|
# include <unistd.h> /* pledge(2) */
|
||||||
|
#endif
|
||||||
|
|
||||||
char *program_name = "str";
|
char *program_name = "str";
|
||||||
|
|
||||||
@@ -55,8 +59,16 @@ usage(char *argv0) {
|
|||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
size_t ctype; // selected from ctypes.h; index of ctype
|
size_t ctype; // selected from ctypes.h; index of ctype
|
||||||
int retval; // initially fail but becomes success on the first valid char
|
int retval; // initially fail but becomes success on the first valid char
|
||||||
|
program_name = argv[0] == NULL ? program_name : argv[0];
|
||||||
|
|
||||||
if (argc < 3) { return usage(argv[0] == NULL ? program_name : argv[0]); }
|
#ifdef __OpenBSD__
|
||||||
|
if (pledge("stdio", NULL) == -1) {
|
||||||
|
perror(program_name);
|
||||||
|
return EX_OSERR;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (argc < 3) { return usage(program_name); }
|
||||||
|
|
||||||
for ( /* iterate ctypes */
|
for ( /* iterate ctypes */
|
||||||
ctype = 0;
|
ctype = 0;
|
||||||
|
|||||||
18
src/strcmp.c
18
src/strcmp.c
@@ -16,13 +16,25 @@
|
|||||||
* You should have received a copy of the GNU Affero General Public License
|
* 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/.
|
* along with this program. If not, see https://www.gnu.org/licenses/.
|
||||||
*/
|
*/
|
||||||
#include <stdio.h> /* fprintf(3), stderr */
|
#include <stdio.h> /* fprintf(3), perror(3), stderr */
|
||||||
#include <sysexits.h> /* EX_OK, EX_USAGE */
|
#include <sysexits.h> /* EX_OK, EX_OSERR, EX_USAGE */
|
||||||
|
|
||||||
|
#ifdef __OpenBSD__
|
||||||
|
# include <unistd.h> /* pledge(2) */
|
||||||
|
#endif
|
||||||
|
|
||||||
char *program_name = "strcmp";
|
char *program_name = "strcmp";
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
int i;
|
unsigned int i;
|
||||||
|
|
||||||
|
#ifdef __OpenBSD__
|
||||||
|
if (pledge("stdio", NULL) == -1) {
|
||||||
|
perror(argv[0] == NULL ? program_name : argv[0]);
|
||||||
|
|
||||||
|
return EX_OSERR;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
if (argc < 3) {
|
if (argc < 3) {
|
||||||
(void)fprintf(
|
(void)fprintf(
|
||||||
|
|||||||
12
src/swab.rs
12
src/swab.rs
@@ -32,6 +32,10 @@ use getopt::GetOpt;
|
|||||||
use sysexits::{ EX_IOERR, EX_OK, EX_OSERR, EX_USAGE };
|
use sysexits::{ EX_IOERR, EX_OK, EX_OSERR, EX_USAGE };
|
||||||
use strerror::StrError;
|
use strerror::StrError;
|
||||||
|
|
||||||
|
#[cfg(target_os="openbsd")] extern crate openbsd;
|
||||||
|
#[cfg(target_os="openbsd")] use openbsd::{ Promises, pledge };
|
||||||
|
|
||||||
|
|
||||||
fn oserr(argv0: &str, e: Error) -> ExitCode {
|
fn oserr(argv0: &str, e: Error) -> ExitCode {
|
||||||
eprintln!("{}: {}", argv0, e.strerror());
|
eprintln!("{}: {}", argv0, e.strerror());
|
||||||
ExitCode::from(EX_OSERR as u8)
|
ExitCode::from(EX_OSERR as u8)
|
||||||
@@ -49,6 +53,14 @@ fn usage(s: &str) -> ExitCode {
|
|||||||
|
|
||||||
fn main() -> ExitCode {
|
fn main() -> ExitCode {
|
||||||
let argv = args().collect::<Vec<String>>();
|
let argv = args().collect::<Vec<String>>();
|
||||||
|
|
||||||
|
#[cfg(target_os="openbsd")] {
|
||||||
|
let promises = Promises::new("stdio");
|
||||||
|
if let Err(e) = pledge(Some(promises), None) {
|
||||||
|
return oserr(&argv[0], e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut buf: Vec<u8> = Vec::new(); // holds the sequence getting swabbed
|
let mut buf: Vec<u8> = Vec::new(); // holds the sequence getting swabbed
|
||||||
let mut input = stdin();
|
let mut input = stdin();
|
||||||
let mut output = stdout().lock();
|
let mut output = stdout().lock();
|
||||||
|
|||||||
13
src/true.c
13
src/true.c
@@ -1,9 +1,18 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2023 Emma Tebibyte <emma@tebibyte.media>
|
* Copyright (c) 2023–2024 Emma Tebibyte <emma@tebibyte.media>
|
||||||
|
* Copyright (c) 2024 DTB <trinity@trinity.moe>
|
||||||
* SPDX-License-Identifier: CC0
|
* SPDX-License-Identifier: CC0
|
||||||
*
|
*
|
||||||
* This work is marked with CC0 1.0. To view a copy of this license, visit
|
* This work is marked with CC0 1.0. To view a copy of this license, visit
|
||||||
* <http://creativecommons.org/publicdomain/zero/1.0>.
|
* <http://creativecommons.org/publicdomain/zero/1.0>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
int main() {}
|
#ifdef __OpenBSD__
|
||||||
|
# include <unistd.h> /* pledge(2) */
|
||||||
|
#endif
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
#ifdef __OpenBSD__
|
||||||
|
pledge(NULL, NULL);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|||||||
36
tests/README
Normal file
36
tests/README
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
The testing suite contains two trees: the Bonsai tree and the POSIX tree:
|
||||||
|
|
||||||
|
.
|
||||||
|
├── README
|
||||||
|
├── bonsai/
|
||||||
|
│ ├── dj.mk
|
||||||
|
│ ├── false.mk
|
||||||
|
│ ├── fop.mk
|
||||||
|
│ └── ...
|
||||||
|
├── posix/
|
||||||
|
└── tests.mk
|
||||||
|
|
||||||
|
The Bonsai tree tests the functionality of Harakit utilities for regressions and
|
||||||
|
other issues relating to compliance to our standards of practice.
|
||||||
|
|
||||||
|
The POSIX tests are currently a work-in-progress. Their status in this
|
||||||
|
repository is uncertain.
|
||||||
|
|
||||||
|
Both sets of tests also inherit the environment set by the top-level Makefile,
|
||||||
|
which sets the BIN variable to the build/bin directory at the root of the
|
||||||
|
project; therefore, each binary is located at $(BIN)/tool for idiomatic access.
|
||||||
|
|
||||||
|
Each test contains a set of PHONY targets which are prefixed with the name of
|
||||||
|
the tool being tested and an underscore. The first target is tests, which
|
||||||
|
depends on all the other targets in the test file. These test files are each
|
||||||
|
included in the top Makefile, so they can be called from the root of the
|
||||||
|
repository. This also means that BIN can be set manually so that tests can be
|
||||||
|
run using make(1) inside of the tests directory:
|
||||||
|
|
||||||
|
$ make -f tests.mk BIN=../build/bin dj_tests
|
||||||
|
|
||||||
|
--
|
||||||
|
Copyright © 2024 Emma Tebibyte <emma@tebibyte.media>
|
||||||
|
|
||||||
|
This work is licensed under CC BY-SA 4.0. To view a copy of this license, visit
|
||||||
|
<http://creativecommons.org/licenses/by-sa/4.0/>.
|
||||||
48
tests/bonsai/dj.mk
Executable file
48
tests/bonsai/dj.mk
Executable file
@@ -0,0 +1,48 @@
|
|||||||
|
# Copyright (c) 2024 DTB <trinity@trinity.moe>
|
||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
.PRAGMA: command_comment
|
||||||
|
|
||||||
|
/dev/full:
|
||||||
|
/dev/null:
|
||||||
|
|
||||||
|
.PHONY: dj_tests
|
||||||
|
dj_tests: dj_help dj_full dj_null # dj_skip_stdin
|
||||||
|
|
||||||
|
.PHONY: dj_full
|
||||||
|
# Linux has a /dev/full pseudodevice useful for testing errors.
|
||||||
|
dj_full: $(BIN)/dj /dev/full
|
||||||
|
case "$$(uname)" in \
|
||||||
|
Linux) \
|
||||||
|
! $(BIN)/dj -Hi /dev/zero -o /dev/full 2>&1 \
|
||||||
|
| tee /dev/stderr \
|
||||||
|
| xargs -I out test '1+0 > 0+0; 1024 > 0' = out \
|
||||||
|
;; \
|
||||||
|
esac
|
||||||
|
|
||||||
|
.PHONY: dj_help
|
||||||
|
dj_help: $(BIN)/dj
|
||||||
|
! $(BIN)/dj -h
|
||||||
|
|
||||||
|
.PHONY: dj_null
|
||||||
|
# Read nothing from /dev/null, write nothing to /dev/null.
|
||||||
|
dj_null: $(BIN)/dj /dev/null
|
||||||
|
$(BIN)/dj -Hi /dev/null -o /dev/null 2>&1 \
|
||||||
|
| tee /dev/stderr \
|
||||||
|
| xargs -I out test '0+0 > 0+0; 0 > 0' = out
|
||||||
|
|
||||||
|
# This test currently fails. This is probably due to dj(1) being stale relative
|
||||||
|
# to the main harakit branch. TODO: Reassess once the testing branch is merged.
|
||||||
|
# .PHONY: dj_skip_stdin
|
||||||
|
# # Test skipping stdin.
|
||||||
|
# dj_skip_stdin: $(BIN)/dj
|
||||||
|
# # Pipe 1024B of '\0' into dj(1); skip the first 24B; expect 1000B written.
|
||||||
|
# dd count=1 bs=1024 </dev/zero 2>/dev/null \
|
||||||
|
# | $(BIN)/dj -H -s 24 -o /dev/null 2>&1 \
|
||||||
|
# | tee /dev/stderr \
|
||||||
|
# | xargs -I out test '1+0 > 1+0; 1024 > 1000' = out
|
||||||
18
tests/bonsai/false.mk
Executable file
18
tests/bonsai/false.mk
Executable file
@@ -0,0 +1,18 @@
|
|||||||
|
# Copyright (c) 2024 DTB <trinity@trinity.moe>
|
||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
.PHONY: false_tests
|
||||||
|
false_tests: false_test false_help
|
||||||
|
|
||||||
|
.PHONY: false
|
||||||
|
false_test: $(BIN)/false
|
||||||
|
! $(BIN)/false
|
||||||
|
|
||||||
|
.PHONY: false_help
|
||||||
|
false_help: $(BIN)/false
|
||||||
|
! $(BIN)/false -h
|
||||||
31
tests/bonsai/fop.mk
Executable file
31
tests/bonsai/fop.mk
Executable file
@@ -0,0 +1,31 @@
|
|||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
.PHONY: fop_tests
|
||||||
|
fop_tests: fop_functionality fop_delimiter fop_help fop_fail
|
||||||
|
|
||||||
|
.PHONY: fop_help
|
||||||
|
fop_help: $(BIN)/fop
|
||||||
|
! $(BIN)/fop -h
|
||||||
|
|
||||||
|
.PHONY: fop_delimiter
|
||||||
|
fop_delimiter: $(BIN)/fop
|
||||||
|
test "$$(printf 'test1 test1 test1\n' | $(BIN)/fop -d' ' 2 sed 's/1/4/g')" \
|
||||||
|
= 'test1 test1 test4'
|
||||||
|
test "$$(printf 'meowsetwoofsetribbit\n' \
|
||||||
|
| $(BIN)/fop -d 'set' 1 sed 's/woof/meow/g')" = 'meowsetmeowsetribbit'
|
||||||
|
|
||||||
|
.PHONY: fop_fail
|
||||||
|
fop_fail: $(BIN)/fop
|
||||||
|
! printf 'test\n' | $(BIN)/fop 1 cat
|
||||||
|
! printf 'test\n' | $(BIN)/fop 'test' cat
|
||||||
|
! printf 'test\n' | $(BIN)/fop -d'test' cat
|
||||||
|
|
||||||
|
.PHONY: fop_functionality
|
||||||
|
fop_functionality: $(BIN)/fop
|
||||||
|
test "$$(printf 'test1\036test1\036test1\n' | $(BIN)/fop 1 sed 's/1/4/g')" \
|
||||||
|
= "$$(printf 'test1\036test4\036test1\n')"
|
||||||
32
tests/bonsai/hru.mk
Executable file
32
tests/bonsai/hru.mk
Executable file
@@ -0,0 +1,32 @@
|
|||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
.PHONY: hru_tests
|
||||||
|
hru_tests: hru_help hru_functionality hru_negative hru_regressions
|
||||||
|
|
||||||
|
.PHONY: hru_help
|
||||||
|
hru_help: $(BIN)/hru
|
||||||
|
! $(BIN)/hru -h
|
||||||
|
|
||||||
|
.PHONY: hru_functionality
|
||||||
|
hru_functionality: $(BIN)/hru
|
||||||
|
test "$$(printf '1234\n' | $(BIN)/hru)" = '1.2 kB'
|
||||||
|
test "$$(printf '0\n' | $(BIN)/hru)" = '0 B'
|
||||||
|
|
||||||
|
.PHONY: hru_negative
|
||||||
|
hru_negative: $(BIN)/hru
|
||||||
|
! printf '%s\n' '-1' | $(BIN)/hru
|
||||||
|
|
||||||
|
.PHONY: hru_regressions
|
||||||
|
hru_regressions: $(BIN)/hru
|
||||||
|
n=1; \
|
||||||
|
while true; \
|
||||||
|
do \
|
||||||
|
printf '%s\n' "$$n" | $(BIN)/hru || break; \
|
||||||
|
n="$$(($$n * 10))"; \
|
||||||
|
done; \
|
||||||
|
printf 'Max float: %s\n' "$$n"
|
||||||
67
tests/bonsai/intcmp.mk
Executable file
67
tests/bonsai/intcmp.mk
Executable file
@@ -0,0 +1,67 @@
|
|||||||
|
# Copyright (c) 2024 DTB <trinity@trinity.moe>
|
||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
.PHONY: intcmp_tests
|
||||||
|
intcmp_tests: intcmp_help intcmp_none intcmp_e intcmp_g intcmp_l intcmp_combined
|
||||||
|
|
||||||
|
.PHONY: intcmp_help
|
||||||
|
intcmp_help: $(BIN)/intcmp
|
||||||
|
! $(BIN)/intcmp -h
|
||||||
|
|
||||||
|
# These test that integer comparisons are working as they should. For the sake
|
||||||
|
# of readability (to facilitate faster skimming) these recipes follow a
|
||||||
|
# columned format:
|
||||||
|
# $binary -flags d d d d # op
|
||||||
|
# For flag meanings reference intcmp(1) (though they are somewhat self
|
||||||
|
# explanatory). d here refers to a decimal number; a mixture of 1s, 2s, and 3s
|
||||||
|
# (a particularly lovely number) arranged to demonstrate easily the operation
|
||||||
|
# under scrutiny. The commented op is the operation that is true for the given
|
||||||
|
# numbers. For example:
|
||||||
|
# $(BIN)/intcmp -e 3 3 3 3 # ==
|
||||||
|
# op here is ==; 3 == 3 == 3 == 3. The flag being used is -e, to test for
|
||||||
|
# equality, so this test should succeed.
|
||||||
|
# ! $(BIN)/intcmp -l 3 2 1 # >
|
||||||
|
# op here is >; 3 > 2 > 1. The flag being used is -l, to test for each integer
|
||||||
|
# being less than the next, so intcmp should fail - hence the ! at the start of
|
||||||
|
# the invocation. If this test failed, intcmp(1) would be confusing -l for -g,
|
||||||
|
# so that would be a good place to start looking for bugs.
|
||||||
|
|
||||||
|
.PHONY: intcmp_none
|
||||||
|
intcmp_none: $(BIN)/intcmp
|
||||||
|
! $(BIN)/intcmp 1 2
|
||||||
|
|
||||||
|
.PHONY: intcmp_e
|
||||||
|
intcmp_e: $(BIN)/intcmp
|
||||||
|
$(BIN)/intcmp -e 3 3 3 # ==
|
||||||
|
! $(BIN)/intcmp -e 1 2 3 # <
|
||||||
|
! $(BIN)/intcmp -e 3 2 1 # >
|
||||||
|
|
||||||
|
.PHONY: intcmp_g
|
||||||
|
intcmp_g: $(BIN)/intcmp
|
||||||
|
$(BIN)/intcmp -g 3 2 1 # >
|
||||||
|
! $(BIN)/intcmp -g 3 3 3 # ==
|
||||||
|
! $(BIN)/intcmp -g 1 2 3 # <
|
||||||
|
$(BIN)/intcmp -ge 3 3 1 # >=
|
||||||
|
! $(BIN)/intcmp -ge 1 2 3 # <
|
||||||
|
|
||||||
|
.PHONY: intcmp_l
|
||||||
|
intcmp_l: $(BIN)/intcmp
|
||||||
|
$(BIN)/intcmp -l 1 2 3 # <
|
||||||
|
! $(BIN)/intcmp -l 3 3 3 # ==
|
||||||
|
! $(BIN)/intcmp -l 3 2 1 # >
|
||||||
|
$(BIN)/intcmp -le 1 3 3 # <=
|
||||||
|
! $(BIN)/intcmp -le 3 2 1 # >
|
||||||
|
|
||||||
|
.PHONY: intcmp_combined
|
||||||
|
intcmp_combined: $(BIN)/intcmp
|
||||||
|
$(BIN)/intcmp -gl 1 2 3 # <
|
||||||
|
$(BIN)/intcmp -gl 3 2 1 # >
|
||||||
|
$(BIN)/intcmp -gl 1 3 1 # !=
|
||||||
|
! $(BIN)/intcmp -gl 3 3 3 # ==
|
||||||
|
$(BIN)/intcmp -egl 3 1 1 3 # >, ==, <
|
||||||
|
! $(BIN)/intcmp -egl foo # huh?
|
||||||
34
tests/bonsai/mm.mk
Executable file
34
tests/bonsai/mm.mk
Executable file
@@ -0,0 +1,34 @@
|
|||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
.PHONY: mm_tests
|
||||||
|
mm_tests: mm_args mm_help mm_stderr mm_remaining
|
||||||
|
|
||||||
|
.PHONY: mm_none
|
||||||
|
mm_none: $(BIN)/mm
|
||||||
|
test "$$(printf 'meow\n' | $(BIN)/mm)" = meow
|
||||||
|
|
||||||
|
.PHONY: mm_args
|
||||||
|
# mm(1) will error if positional arguments are given without -i or -o
|
||||||
|
mm_args: $(BIN)/mm
|
||||||
|
! $(BIN)/mm argument
|
||||||
|
|
||||||
|
.PHONY: mm_help
|
||||||
|
mm_help: $(BIN)/mm
|
||||||
|
! $(BIN)/mm -h
|
||||||
|
|
||||||
|
.PHONY: mm_stderr
|
||||||
|
# check if stderr is empty upon specifying -e
|
||||||
|
mm_stderr: $(BIN)/mm
|
||||||
|
test "$$(printf 'test\n' | $(BIN)/mm -e 2>&1 >/dev/null )" = "test"
|
||||||
|
|
||||||
|
.PHONY: mm_remaining
|
||||||
|
# check to make sure remaining arguments are used
|
||||||
|
mm_remaining: $(BIN)/mm
|
||||||
|
test "$$($(BIN)/mm -i README COPYING)" = "$$(cat README COPYING)"
|
||||||
|
$(BIN)/mm -i README -o /tmp/mm_test0 /tmp/mm_test1
|
||||||
|
diff /tmp/mm_test0 /tmp/mm_test1
|
||||||
90
tests/bonsai/npc.mk
Executable file
90
tests/bonsai/npc.mk
Executable file
@@ -0,0 +1,90 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Copyright (c) 2024 DTB <trinity@trinity.moe>
|
||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
.PRAGMA: command_comment
|
||||||
|
|
||||||
|
.PHONY: npc_tests
|
||||||
|
npc_tests: npc_help npc_args npc_ascii
|
||||||
|
|
||||||
|
.PHONY: npc_help
|
||||||
|
npc_help: $(BIN)/npc
|
||||||
|
! $(BIN)/npc -h
|
||||||
|
|
||||||
|
.PHONY: npc_args
|
||||||
|
# arg parsing
|
||||||
|
npc_args:
|
||||||
|
$(BIN)/npc -e </dev/null
|
||||||
|
$(BIN)/npc -t </dev/null
|
||||||
|
$(BIN)/npc -et </dev/null
|
||||||
|
! $(BIN)/npc -et 5 </dev/null
|
||||||
|
|
||||||
|
.PHONY: npc_ascii
|
||||||
|
# Test 0x00 to 0x7f in input; in other words, the full 7b ASCII range.
|
||||||
|
npc_ascii: npc_ascii_controls npc_ascii_symbols npc_ascii_uppers # \
|
||||||
|
# npc_ascii_lowers
|
||||||
|
|
||||||
|
.PHONY: npc_ascii_controls
|
||||||
|
# (control characters)
|
||||||
|
npc_ascii_controls:
|
||||||
|
# The following test prints the bytes 0x00 (inclusive) through 0x20
|
||||||
|
# (exclusive) and pipes them through npc(1). npc(1) should then replace all
|
||||||
|
# non-printing, non-space (in the isspace(3p) sense) characters with their
|
||||||
|
# graphical carat-char counterparts (see the npc(1) man page). The head(1p)
|
||||||
|
# invocation then strips off everything past the first line (or past the
|
||||||
|
# first newline byte, 0x0A) and xargs(1p) is used to test(1p) the output
|
||||||
|
# against the known good answer.
|
||||||
|
|
||||||
|
# Immediately before that newline, 0x09 is printed - in ASCII, the
|
||||||
|
# horizontal tab. If xargs' -I option is used, tr(1p) should used to delete
|
||||||
|
# that tab. If the tab is left as part of input, OpenBSD's xargs(1)
|
||||||
|
# implementation has been observed to strip it along with the other
|
||||||
|
# trailing whitespace (the newline), but Busybox's and GNU's xargs(1)
|
||||||
|
# implementations have been observed to leave the tab in. All three
|
||||||
|
# implementations strip off the trailing tab if `-I` is not used. The POSIX
|
||||||
|
# specification for `-I` is ambiguous as to which behavior is correct.
|
||||||
|
# This comment is the result of much bewilderment and debugging.
|
||||||
|
|
||||||
|
# ASCII 0x00 to 0x0a (before the newline, due to xargs(1p) issues)
|
||||||
|
awk 'BEGIN{ for (i = 0; i < 32; ++i) printf("%c", i); }' \
|
||||||
|
| $(BIN)/npc \
|
||||||
|
| head -n 1 \
|
||||||
|
| xargs test "^@^A^B^C^D^E^F^G^H" =
|
||||||
|
|
||||||
|
# ASCII 0x0a (otherwise the head|tail sequence won't work) to 0x1f
|
||||||
|
awk 'BEGIN{ for (i = 0; i < 32; ++i) printf("%c", i); print }' \
|
||||||
|
| $(BIN)/npc \
|
||||||
|
| head -n 2 \
|
||||||
|
| tail -n 1 \
|
||||||
|
| xargs -I out test "^K^L^M^N^O^P^Q^R^S^T^U^V^W^X^Y^Z^[^\^]^^^_"
|
||||||
|
|
||||||
|
.PHONY: npc_ascii_symbols
|
||||||
|
# ASCII 0x1f to 0x3f (^_ and symbols)
|
||||||
|
npc_ascii_symbols:
|
||||||
|
# shell quoting olympics
|
||||||
|
awk 'BEGIN{ for (i = 31; i < 64; ++i) printf("%c", i); print }' \
|
||||||
|
| $(BIN)/npc \
|
||||||
|
| sed -e s"/\'/\\\'/g" -e 's/"/\\"/g' \
|
||||||
|
| xargs -I out test "^_ !\"#$$%&'()*+,-./0123456789:;<=>?" = out
|
||||||
|
|
||||||
|
.PHONY: npc_ascii_uppers
|
||||||
|
# ASCII 0x40 to 0x5f (uppercases)
|
||||||
|
npc_ascii_uppers:
|
||||||
|
awk 'BEGIN{ for (i = 64; i < 96; ++i) printf("%c", i); print }' \
|
||||||
|
| $(BIN)/npc \
|
||||||
|
| sed 's/\\/\\\\/' \
|
||||||
|
| xargs -I out test @ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_ = out
|
||||||
|
|
||||||
|
# This test is broken and will need closer inspection along with the npc(1)
|
||||||
|
# source.
|
||||||
|
# .PHONY: npc_ascii_lowers
|
||||||
|
# # ASCII 0x60 to 0x7f (lowercases)
|
||||||
|
# npc_ascii_lowers:
|
||||||
|
# awk 'BEGIN{ for (i = 96; i < 128; ++i) printf("%c", i); print }' \
|
||||||
|
# | $(BIN)/npc \
|
||||||
|
# | xargs -I out test "\`abcdefghijklmnopqrstuvwxyz{|}~^?" = out
|
||||||
24
tests/bonsai/peek.mk
Executable file
24
tests/bonsai/peek.mk
Executable file
@@ -0,0 +1,24 @@
|
|||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
# Testing peek is hard as it requires visual confirmation that text isn't being
|
||||||
|
# echoed. These tests don't go that far but are a start, and have already
|
||||||
|
# caught a bug in -i behavior.
|
||||||
|
|
||||||
|
.PHONY: peek_tests
|
||||||
|
peek_tests: peek_help peek_stdio
|
||||||
|
|
||||||
|
.PHONY: peek_help
|
||||||
|
peek_help: $(BIN)/peek
|
||||||
|
! $(BIN)/peek -h
|
||||||
|
|
||||||
|
.PHONY: peek_stdio
|
||||||
|
# Test peek -i
|
||||||
|
peek_stdio: $(BIN)/peek
|
||||||
|
printf 'Test.\n' \
|
||||||
|
| $(BIN)/peek -i \
|
||||||
|
| xargs test 'Test.' =
|
||||||
43
tests/bonsai/rpn.mk
Executable file
43
tests/bonsai/rpn.mk
Executable file
@@ -0,0 +1,43 @@
|
|||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
.PHONY: rpn_tests
|
||||||
|
rpn_tests: rpn_help rpn_add rpn_sub rpn_mul rpn_div rpn_mod rpn_flr
|
||||||
|
|
||||||
|
.PHONY: rpn_help
|
||||||
|
rpn_help: $(BIN)/rpn
|
||||||
|
! $(BIN)/rpn -h
|
||||||
|
|
||||||
|
.PHONY: rpn_add
|
||||||
|
rpn_add: $(BIN)/rpn
|
||||||
|
test "$$($(BIN)/rpn 1 2 +)" -eq 3
|
||||||
|
test "$$($(BIN)/rpn 0.2 0.1 +)" = 0.3
|
||||||
|
|
||||||
|
.PHONY: rpn_sub
|
||||||
|
rpn_sub: $(BIN)/rpn
|
||||||
|
test "$$($(BIN)/rpn 23 5 -)" -eq 18
|
||||||
|
test "$$($(BIN)/rpn 0.3 0.1 -)" = 0.2
|
||||||
|
|
||||||
|
.PHONY: rpn_mul
|
||||||
|
rpn_mul: $(BIN)/rpn
|
||||||
|
test "$$($(BIN)/rpn 1.2 3 '*')" = 3.6
|
||||||
|
test "$$($(BIN)/rpn 0 3 '*')" -eq 0
|
||||||
|
|
||||||
|
.PHONY: rpn_div
|
||||||
|
rpn_div: $(BIN)/rpn
|
||||||
|
test "$$($(BIN)/rpn 12 5 /)" = 2.4
|
||||||
|
test "$$($(BIN)/rpn 3 0 /)" = inf
|
||||||
|
|
||||||
|
.PHONY: rpn_mod
|
||||||
|
rpn_mod: $(BIN)/rpn
|
||||||
|
test "$$($(BIN)/rpn 12 5 %)" -eq 2
|
||||||
|
test "$$($(BIN)/rpn 9 4 %)" -eq 1
|
||||||
|
|
||||||
|
.PHONY: rpn_flr
|
||||||
|
rpn_flr: $(BIN)/rpn
|
||||||
|
test "$$($(BIN)/rpn 12 5 //)" -eq 2
|
||||||
|
test "$$($(BIN)/rpn 9 4 //)" -eq 2
|
||||||
46
tests/bonsai/scrut.mk
Executable file
46
tests/bonsai/scrut.mk
Executable file
@@ -0,0 +1,46 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
.PRAGMA: command_comment
|
||||||
|
|
||||||
|
.PHONY: scrut_tests
|
||||||
|
scrut_tests: scrut_help scrut_options
|
||||||
|
|
||||||
|
.PHONY: scrut_help
|
||||||
|
scrut_help: $(BIN)/scrut
|
||||||
|
! $(BIN)/scrut -h
|
||||||
|
|
||||||
|
.PHONY: scrut_options
|
||||||
|
# scrut tests file attributes, but files of a certain attribute aren't
|
||||||
|
# guaranteed to be present on a system. This test checks all of the files in
|
||||||
|
# harakit and, if test(1p) says a file matches a certain attribute, then checks
|
||||||
|
# scrut.
|
||||||
|
# opts are space-delimited (for command splitting), sel is not
|
||||||
|
scrut_options: $(BIN)/scrut
|
||||||
|
set -e; \
|
||||||
|
opts="b c d e f g k p r s u w x L S"; \
|
||||||
|
sel=; \
|
||||||
|
find . -name .git -prune -o -print \
|
||||||
|
| while read -r f; do \
|
||||||
|
for opt in $$opts; \
|
||||||
|
do if ! printf "%s\n" $$sel | grep $$opt >/dev/null; then \
|
||||||
|
if test -$$opt "$$f"; then \
|
||||||
|
if ! $(BIN)/scrut -$$opt "$$f"; \
|
||||||
|
then printf "[!!] scrut -%s failed on %s.\n" \
|
||||||
|
$$opt "$$f"; \
|
||||||
|
fi; \
|
||||||
|
sel="$$sel$$opt"; \
|
||||||
|
printf "[OK] Tested scrut -%s using %s\n" \
|
||||||
|
$$opt "$$f"; \
|
||||||
|
fi; \
|
||||||
|
fi; \
|
||||||
|
done; \
|
||||||
|
if printf "%s\n" "$$opts" | sed 's/ //g' | xargs test "$$sel" =; \
|
||||||
|
then break; \
|
||||||
|
fi; \
|
||||||
|
done
|
||||||
20
tests/bonsai/str.mk
Executable file
20
tests/bonsai/str.mk
Executable file
@@ -0,0 +1,20 @@
|
|||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
.PRAGMA: command_comment
|
||||||
|
|
||||||
|
.PHONY: str_tests
|
||||||
|
str_tests: str_help str_isalpha
|
||||||
|
|
||||||
|
.PHONY: str_help
|
||||||
|
str_help: $(BIN)/str
|
||||||
|
! $(BIN)/str -h
|
||||||
|
|
||||||
|
.PHONY: str_isalpha
|
||||||
|
str_isalpha: $(BIN)/str
|
||||||
|
$(BIN)/str isalpha c
|
||||||
|
! $(BIN)/str isalpha 3
|
||||||
31
tests/bonsai/strcmp.mk
Executable file
31
tests/bonsai/strcmp.mk
Executable file
@@ -0,0 +1,31 @@
|
|||||||
|
# Copyright (c) 2024 DTB <trinity@trinity.moe>
|
||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
NAME = strcmp
|
||||||
|
TARGET = $(NAME)_tests
|
||||||
|
BINARY = $(BIN)/$(NAME)
|
||||||
|
|
||||||
|
.PHONY: strcmp_tests
|
||||||
|
strcmp_tests: strcmp_equals strcmp_help strcmp_nocmp strcmp_unequals
|
||||||
|
|
||||||
|
.PHONY: strcmp_equals
|
||||||
|
strcmp_equals: $(BIN)/strcmp
|
||||||
|
$(BIN)/strcmp equals equals
|
||||||
|
$(BIN)/strcmp - -
|
||||||
|
|
||||||
|
.PHONY: strcmp_help
|
||||||
|
strcmp_help: $(BIN)/strcmp
|
||||||
|
! $(BIN)/strcmp -h
|
||||||
|
|
||||||
|
.PHONY: strcmp_nocmp
|
||||||
|
strcmp_nocmp: $(BIN)/strcmp
|
||||||
|
! $(BIN)/strcmp nocmp
|
||||||
|
|
||||||
|
.PHONY: strcmp_unequals
|
||||||
|
strcmp_unequals: $(BIN)/strcmp
|
||||||
|
! $(BIN)/strcmp unequals equals
|
||||||
22
tests/bonsai/swab.mk
Executable file
22
tests/bonsai/swab.mk
Executable file
@@ -0,0 +1,22 @@
|
|||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
.PRAGMA: command_comment
|
||||||
|
|
||||||
|
.PHONY: swab_tests
|
||||||
|
swab_tests: swab_help swab_examples
|
||||||
|
|
||||||
|
.PHONY: swab_help
|
||||||
|
swab_help: $(BIN)/swab
|
||||||
|
! $(BIN)/swab -h
|
||||||
|
|
||||||
|
.PHONY: swab_examples
|
||||||
|
# These are the examples present in the man page.
|
||||||
|
swab_examples: $(BIN)/swab
|
||||||
|
printf 'hello world!\n' \
|
||||||
|
| $(BIN)/swab \
|
||||||
|
| xargs -I out test 'ehll oowlr!d' = out
|
||||||
19
tests/bonsai/true.mk
Executable file
19
tests/bonsai/true.mk
Executable file
@@ -0,0 +1,19 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Copyright (c) 2024 DTB <trinity@trinity.moe>
|
||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
.PHONY: true_tests
|
||||||
|
true_tests: true_test
|
||||||
|
|
||||||
|
.PHONY: true_help
|
||||||
|
true_help: $(BIN)/true
|
||||||
|
$(BIN)/true -h
|
||||||
|
|
||||||
|
.PHONY: true_test
|
||||||
|
true_test: $(BIN)/true
|
||||||
|
$(BIN)/true
|
||||||
@@ -1,24 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
# Copyright (c) 2023–2024 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 Makefile >/dev/null 2>&1
|
|
||||||
then
|
|
||||||
printf '%s: Run this script in the root of the project.\n' "$0" 1>&2
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
printf "Starting POSIX compatibility testing.\n"
|
|
||||||
|
|
||||||
for utility in tests/posix/*; do
|
|
||||||
printf '%s: %s: Testing utility.\n' "$0" "$utility"
|
|
||||||
"$utility"
|
|
||||||
printf '\n'
|
|
||||||
done
|
|
||||||
22
tests/posix/bin/cat
Executable file
22
tests/posix/bin/cat
Executable file
@@ -0,0 +1,22 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
# Strictly POSIX-compliant cat(1) implementation. See cat(1p)
|
||||||
|
|
||||||
|
for arg in "$@"; do
|
||||||
|
case "$arg" in
|
||||||
|
-u) args="$(printf '%s %s\n' "$args" "$arg")" ;;
|
||||||
|
*) args="$(printf -- '%s -i %s\n' "$args" "$arg")" ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# See IEEE Std 1003.1-2017 3.282
|
||||||
|
# https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_282
|
||||||
|
IFS=' '
|
||||||
|
|
||||||
|
mm $args
|
||||||
12
tests/posix/bin/false
Executable file
12
tests/posix/bin/false
Executable file
@@ -0,0 +1,12 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Copyright (c) 2024 DTB <trinity@trinity.moe>
|
||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
# Strictly POSIX-compliant false(1) implementation. See false(1p)
|
||||||
|
|
||||||
|
false "$@"
|
||||||
11
tests/posix/bin/true
Executable file
11
tests/posix/bin/true
Executable file
@@ -0,0 +1,11 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Copyright (c) 2024 DTB <trinity@trinity.moe>
|
||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
# Strictly POSIX-compliant true(1) implementation. See true(1p)
|
||||||
|
true "$@"
|
||||||
5
tests/posix/posix_env
Normal file
5
tests/posix/posix_env
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
set -ex
|
||||||
|
|
||||||
|
PATH="$PWD/bin:$PATH"
|
||||||
15
tests/tests.mk
Normal file
15
tests/tests.mk
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
# Copyright (c) 2024 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.
|
||||||
|
|
||||||
|
|
||||||
|
#TESTFILES != for file in tests/bonsai/*.mk tests/posix/*.mk; do printf '%s ' "$$file"; done;
|
||||||
|
TESTFILES != for file in tests/bonsai/*.mk; do printf '%s ' "$$file"; done;
|
||||||
|
|
||||||
|
TESTS != printf '%s\n' "$(TESTFILES)" | xargs -n1 basename \
|
||||||
|
| sed 's/\.mk/_tests/g'
|
||||||
|
|
||||||
|
include $(TESTFILES)
|
||||||
Reference in New Issue
Block a user