File: tacl.sh 1 #!/bin/sh 2 3 # The MIT License (MIT) 4 # 5 # Copyright (c) 2026 pacman64 6 # 7 # Permission is hereby granted, free of charge, to any person obtaining a copy 8 # of this software and associated documentation files (the "Software"), to deal 9 # in the Software without restriction, including without limitation the rights 10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 # copies of the Software, and to permit persons to whom the Software is 12 # furnished to do so, subject to the following conditions: 13 # 14 # The above copyright notice and this permission notice shall be included in 15 # all copies or substantial portions of the Software. 16 # 17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 23 # SOFTWARE. 24 25 26 # tacl [options...] [files...] 27 # 28 # Like `tac`, TAC Lines emits lines in reverse order (last to first). 29 # Unlike `tac`, TAC Lines ensures no lines across inputs are accidentally 30 # joined, since all lines it outputs end with line-feeds, even when the 31 # original files don't. 32 # 33 # The options are, available both in single and double-dash versions 34 # 35 # -h, -help show this help message 36 # -0, -null turn null-byte-delimited chunks into proper lines 37 38 39 nulled=0 40 41 while [ $# -gt 0 ]; do 42 case "$1" in 43 -h|--h|-help|--help) 44 awk '/^# +tacl /, /^$/ { gsub(/^# ?/, ""); print }' "$0" 45 exit 0 46 ;; 47 48 -0|--0|-null|--null) nulled=1; shift; continue ;; 49 50 -) break ;; 51 52 --) shift; break ;; 53 54 -*) 55 printf "%s: unsupported option '%s'\n" "$0" "$1" >&2 56 exit 1 57 ;; 58 esac 59 60 break 61 done 62 63 # show all non-existing files given 64 dashes=0 65 failed=0 66 for arg in "$@"; do 67 if [ "${arg}" = "-" ]; then 68 dashes="$((dashes + 1))" 69 continue 70 fi 71 [ -e "${arg}" ] && continue 72 printf "%s: no file named \"%s\"\n" "$0" "${arg}" >&2 73 failed=1 74 done 75 76 if [ "${dashes}" -gt 1 ]; then 77 printf "%s: can't read from the standard input more than once\n" "$0" >&2 78 failed=1 79 fi 80 81 [ "${failed}" -gt 0 ] && exit 2 82 83 src=' 84 BEGIN { if (nulled) RS = "\000" } 85 FNR == 1 { gsub(/^\xef\xbb\xbf/, "") } 86 { gsub(/ *\r?$/, ""); lines[NR] = $0 } 87 END { for (i = NR; i >= 1; i--) print lines[i] } 88 ' 89 90 awk -v nulled="${nulled}" "${src}" "$@"