File: lines.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 # lines [options...] [files...]
  27 #
  28 # Unlike `cat`, this tool guarantees no lines across inputs are accidentally
  29 # joined, since all lines it outputs end with line-feeds, even when the
  30 # original files don't.
  31 #
  32 # The options are, available both in single and double-dash versions
  33 #
  34 #   -h, -help      show this help message
  35 #   -n, -number    number all output lines
  36 #   -z, -zero      end each output line with a null byte
  37 #   -0, -null      read/treat null-byte-delimited chunks as input lines
  38 
  39 
  40 zin=0
  41 buf=0
  42 nums=0
  43 zout=0
  44 
  45 while [ $# -gt 0 ]; do
  46     case "$1" in
  47         -b|--b|-buffered|--buffered) buf=1; shift; continue ;;
  48 
  49         -h|--h|-help|--help)
  50             awk '/^# +lines /, /^$/ { gsub(/^# ?/, ""); print }' "$0"
  51             exit 0
  52         ;;
  53 
  54         -n|--n|-number|--number) nums=1; shift; continue ;;
  55 
  56         -z|--z|-zero|--zero) zout=1; shift; continue ;;
  57 
  58         -0|--0|-null|--null) zin=1; shift; continue ;;
  59 
  60         -) break ;;
  61 
  62         --) shift; break ;;
  63 
  64         -*)
  65             printf "%s: unsupported option '%s'\n" "$0" "$1" >&2
  66             exit 1
  67         ;;
  68     esac
  69 
  70     break
  71 done
  72 
  73 # show all non-existing files given
  74 dashes=0
  75 failed=0
  76 for arg in "$@"; do
  77     if [ "${arg}" = "-" ]; then
  78         dashes="$((dashes + 1))"
  79         continue
  80     fi
  81     [ -e "${arg}" ] && continue
  82     printf "%s: no file named \"%s\"\n" "$0" "${arg}" >&2
  83     failed=1
  84 done
  85 
  86 if [ "${dashes}" -gt 1 ]; then
  87     printf "%s: can't read from the standard input more than once\n" "$0" >&2
  88     failed=1
  89 fi
  90 
  91 [ "${failed}" -gt 0 ] && exit 2
  92 
  93 flush=0
  94 if [ "${buf}" -eq 0 ] && { [ -p /dev/stdout ] || [ -t 1 ]; }; then
  95     flush=1
  96 fi
  97 
  98 ors="\n"
  99 if [ "${zout}" -eq 1 ]; then
 100     ors="\000"
 101 fi
 102 
 103 awk -v ORS="${ors}" -v zin="${zin}" -v nums="${nums}" -v flush="${flush}" '
 104     BEGIN { if (zin) RS = "\000" }
 105 
 106     FNR == 1 { gsub(/^\xef\xbb\xbf/, "") }
 107 
 108     {
 109         gsub(/\r?$/, "")
 110         if (nums) printf "%6d\t", NR
 111         print
 112         if (flush) fflush()
 113     }
 114 ' "$@"