ul.sh revision 1.2
1#!/bin/sh 2# 3# Copyright (c) 2015, 2016 Ingo Schwarze <schwarze@openbsd.org> 4# 5# Permission to use, copy, modify, and distribute this software for any 6# purpose with or without fee is hereby granted, provided that the above 7# copyright notice and this permission notice appear in all copies. 8# 9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 17unset LC_ALL 18export LC_CTYPE=en_US.UTF-8 19 20UL=${1:-/usr/bin/ul} 21 22test_ul() 23{ 24 stdin=$1 25 shift 26 for arg in -txterm -tmime -tlpr -i; do 27 if [ -n "$1" ]; then 28 expected=`echo -n "$1."` 29 shift 30 fi 31 result=`echo -n "$stdin" | $UL $arg ; echo -n .` 32 if [ "$result" != "${expected}" ]; then 33 echo input: 34 echo ">>>$stdin<<<" 35 echo -n "$stdin" | hexdump -C 36 echo expected with $arg: 37 echo ">>>$expected<<<" 38 echo -n "$expected" | hexdump -C 39 echo result: 40 echo ">>>$result<<<" 41 echo -n "$result" | hexdump -C 42 exit 1; 43 fi 44 done 45} 46 47b="\033[1m" 48B="\033(B\033[m" 49i="\033[4m" 50I="\033[24m" 51 52# --- Fonts and tabs. -------------------------------------------------- 53 54# ASCII input 55test_ul "ax\troman" "ax roman\n" 56test_ul "a\bax\tbold" \ 57 "${b}a${B}x bold\n" \ 58 "ax bold\n" \ 59 "ax bold\ra\n" \ 60 "ax bold\n!\n" 61test_ul "a\b_x\titalic post" \ 62 "${i}a${I}x italic post\n" \ 63 "a\b\025x italic post\n" \ 64 "ax italic post\r_\n" \ 65 "ax italic post\n_\n" 66test_ul "_\bax\titalic pre" \ 67 "${i}a${I}x italic pre\n" \ 68 "a\b\025x italic pre\n" \ 69 "ax italic pre\r_\n" \ 70 "ax italic pre\n_\n" 71 72# 2 bytes, width 1 73test_ul "\0303\0261x\troman" "\0303\0261x roman\n" 74test_ul "\0303\0261\b\0303\0261x\tbold" \ 75 "${b}\0303\0261${B}x bold\n" \ 76 "\0303\0261x bold\n" \ 77 "\0303\0261x bold\r\0303\0261\n" \ 78 "\0303\0261x bold\n!\n" 79test_ul "\0303\0261\b_x\titalic post" \ 80 "${i}\0303\0261${I}x italic post\n" \ 81 "\0303\0261\b\025x italic post\n" \ 82 "\0303\0261x italic post\r_\n" \ 83 "\0303\0261x italic post\n_\n" 84test_ul "_\b\0303\0261x\titalic pre" \ 85 "${i}\0303\0261${I}x italic pre\n" \ 86 "\0303\0261\b\025x italic pre\n" \ 87 "\0303\0261x italic pre\r_\n" \ 88 "\0303\0261x italic pre\n_\n" 89 90# 3 bytes, width 1 91test_ul "\0340\0270\0202x\troman" "\0340\0270\0202x roman\n" 92test_ul "\0340\0270\0202\b\0340\0270\0202x\tbold" \ 93 "${b}\0340\0270\0202${B}x bold\n" \ 94 "\0340\0270\0202x bold\n" \ 95 "\0340\0270\0202x bold\r\0340\0270\0202\n" \ 96 "\0340\0270\0202x bold\n!\n" 97test_ul "\0340\0270\0202\b_x\titalic post" \ 98 "${i}\0340\0270\0202${I}x italic post\n" \ 99 "\0340\0270\0202\b\025x italic post\n" \ 100 "\0340\0270\0202x italic post\r_\n" \ 101 "\0340\0270\0202x italic post\n_\n" 102test_ul "_\b\0340\0270\0202x\titalic pre" \ 103 "${i}\0340\0270\0202${I}x italic pre\n" \ 104 "\0340\0270\0202\b\025x italic pre\n" \ 105 "\0340\0270\0202x italic pre\r_\n" \ 106 "\0340\0270\0202x italic pre\n_\n" 107 108# 3 bytes, width 2 109test_ul "\0354\0277\0277x\troman" "\0354\0277\0277x roman\n" 110test_ul "\0354\0277\0277\b\0354\0277\0277x\tbold" \ 111 "${b}\0354\0277\0277${B}x bold\n" \ 112 "\0354\0277\0277x bold\n" \ 113 "\0354\0277\0277x bold\r\0354\0277\0277\n" \ 114 "\0354\0277\0277x bold\n!!\n" 115test_ul "\0354\0277\0277\b\b\0354\0277\0277x\tbold" \ 116 "${b}\0354\0277\0277${B}x bold\n" \ 117 "\0354\0277\0277x bold\n" \ 118 "\0354\0277\0277x bold\r\0354\0277\0277\n" \ 119 "\0354\0277\0277x bold\n!!\n" 120test_ul "\0354\0277\0277\b_x\titalic post" \ 121 "${i}\0354\0277\0277${I}x italic post\n" \ 122 "\0354\0277\0277\b\b\025\025x italic post\n" \ 123 "\0354\0277\0277x italic post\r__\n" \ 124 "\0354\0277\0277x italic post\n__\n" 125test_ul "\0354\0277\0277\b\b_x\titalic post" \ 126 "${i}\0354\0277\0277${I}x italic post\n" \ 127 "\0354\0277\0277\b\b\025\025x italic post\n" \ 128 "\0354\0277\0277x italic post\r__\n" \ 129 "\0354\0277\0277x italic post\n__\n" 130test_ul "_\b\0354\0277\0277x\titalic pre" \ 131 "${i}\0354\0277\0277${I}x italic pre\n" \ 132 "\0354\0277\0277\b\b\025\025x italic pre\n" \ 133 "\0354\0277\0277x italic pre\r__\n" \ 134 "\0354\0277\0277x italic pre\n__\n" 135 136# 4 bytes, width 1 137test_ul "\0360\0235\0233\0201x\troman" "\0360\0235\0233\0201x roman\n" 138 139# 2 bytes, width 0 combining diacritic 140test_ul "a\0314\0200x\troman" "a\0314\0200x roman\n" 141test_ul "a\ba\0314\0200\b\0314\0200x\tbold" \ 142 "${b}a\0314\0200${B}x bold\n" \ 143 "a\0314\0200x bold\n" \ 144 "a\0314\0200x bold\ra\0314\0200\n" \ 145 "a\0314\0200x bold\n!\n" 146test_ul "a\b_\0314\0200\b_x\titalic post" \ 147 "${i}a\0314\0200${I}x italic post\n" \ 148 "a\b\025\0314\0200x italic post\n" \ 149 "a\0314\0200x italic post\r_\n" \ 150 "a\0314\0200x italic post\n_\n" 151test_ul "_\ba_\b\0314\0200x\titalic pre" \ 152 "${i}a\0314\0200${I}x italic pre\n" \ 153 "a\b\025\0314\0200x italic pre\n" \ 154 "a\0314\0200x italic pre\r_\n" \ 155 "a\0314\0200x italic pre\n_\n" 156 157 158# --- Overstriking. ---------------------------------------------------- 159 160# Advancing with blanks over all kinds of characters: 161test_ul " _ A \0303\0261\r. . . . .\tx" ". ._.A.\0303\0261. x\n" 162 163# Adding underlining to all kinds of characters: 164test_ul " _ A \0303\0261\r._._._._.\tx" \ 165 "._.${i}_${I}.${i}A${I}.${i}\0303\0261${I}. x\n" \ 166 "._._\b\025.A\b\025.\0303\0261\b\025. x\n" \ 167 "._._.A.\0303\0261. x\r _ _ _\n" \ 168 "._._.A.\0303\0261. x\n _ _ _\n" 169test_ul "_ x_ x_ _x _x _\r_.x_.__._x.__._\n" \ 170 "${i}_${I}.${b}x_${B}.${i}x_${I}.${b}_x${B}.${i}_x${I}.${i}_${I}\n" \ 171 "_\b\025.x_.x\b\025_\b\025._x._\b\025x\b\025._\b\025\n" \ 172 "_.x_.x_._x._x._\r_ x_ __ _x __ _\n" \ 173 "_.x_.x_._x._x._\n_ !! __ !! __ _\n" 174test_ul "_x x_\r_x.x_\n" \ 175 "${b}_x${B}.${b}x_${B}\n" \ 176 "_x.x_\n" \ 177 "_x.x_\r_x x_\n" \ 178 "_x.x_\n!! !!\n" 179 180# Overwriting all kinds of characters with ASCII: 181test_ul "_ AA\bA \0303\0261\b\0303\0261\rA.Aa.A.\tx" \ 182 "${i}A${I}.${b}A${B}A.\0303\0261. x\n" \ 183 "A\b\025.AA.\0303\0261. x\n" \ 184 "A.AA.\0303\0261. x\r_ A\n" \ 185 "A.AA.\0303\0261. x\n_ !\n" 186 187# Overwriting all kinds of characters with UTF-8: 188test_ul " _ A\bA \0303\0261\r.\0303\0261.\0303\0261.\0303\0261.\0303\0261.\tx" \ 189 ".\0303\0261.${i}\0303\0261${I}.A.${b}\0303\0261${B}. x\n" \ 190 ".\0303\0261.\0303\0261\b\025.A.\0303\0261. x\n" \ 191 ".\0303\0261.\0303\0261.A.\0303\0261. x\r _ \0303\0261\n" \ 192 ".\0303\0261.\0303\0261.A.\0303\0261. x\n _ !\n" 193 194# Jumping with tabs into characters: 195test_ul "xxxx\0354\0277\0277\b_\0354\0277\0277\b_\0354\0277\0277\b_xx\r\txx" \ 196 "xxxx${i}\0354\0277\0277\0354\0277\0277${I}\0354\0277\0277${b}x${B}x\n" \ 197 "xxxx\0354\0277\0277\b\b\025\025\0354\0277\0277\b\b\025\025\0354\0277\0277xx\n" \ 198 "xxxx\0354\0277\0277\0354\0277\0277\0354\0277\0277xx\r ____ x\n" \ 199 "xxxx\0354\0277\0277\0354\0277\0277\0354\0277\0277xx\n ____ !\n" 200test_ul "xxxxx\0354\0277\0277\b_\0354\0277\0277\b_\0354\0277\0277xx\r\tx_x" \ 201 "xxxxx${i}\0354\0277\0277${I}\0354\0277\0277${i}\0354\0277\0277${I}${b}x${B}x\n" \ 202 "xxxxx\0354\0277\0277\b\b\025\025\0354\0277\0277\0354\0277\0277\b\b\025\025xx\n" \ 203 "xxxxx\0354\0277\0277\0354\0277\0277\0354\0277\0277xx\r __ __x\n" \ 204 "xxxxx\0354\0277\0277\0354\0277\0277\0354\0277\0277xx\n __ __!\n" 205 206 207# --- Edge cases. ------------------------------------------------------ 208 209# Discarding invalid bytes: 210test_ul "\0354\0277\0277\0377\b\0377_\tx" \ 211 "${i}\0354\0277\0277${I} x\n" \ 212 "\0354\0277\0277\b\b\025\025 x\n" \ 213 "\0354\0277\0277 x\r__\n" \ 214 "\0354\0277\0277 x\n__\n" 215 216exit 0 217