summaryrefslogtreecommitdiffhomepage
path: root/tests/zos/tagging.sh
blob: 7f254f8b7454d33d11359596770b9d4d6ad9ac9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# z/OS: file tags and automatic conversion.
#
# What /bin/sh documents for _TAG_REDIR_IN, _TAG_REDIR_OUT and
# _TAG_REDIR_ERR (z/OS UNIX Command Reference, sh, "Shell variables for
# automatic conversion"), plus what zash adds: an ASCII build tags the
# files it creates through redirection and its pipes, and any build reads
# scripts in either character set.  See DESIGN.md, "File tags".
#
# The shell's own CCSID is printed as "self" so that one .exp serves both
# builds: ISO8859-1 for an ASCII build, IBM-1047 for an EBCDIC build.
#
# Verified on z/OS 3.1; the ls -T format is "t IBM-1047 T=on name".  An
# ASCII build tags the file a ">" creates and converts its own writes to
# IBM-1047, so the file holds what /bin/sh would have written (DESIGN.md,
# "File tags and automatic conversion", has the table).
command -v chtag >/dev/null 2>&1 || exit 77
if [ "$(printf '\101')" = A ]; then self=ISO8859-1; else self=IBM-1047; fi

# Print a file's tag as: <t|b|m|-> <codeset or self or untagged> <T=..>
tag() {
    ls -T "$1" | awk -v self="$self" '{
        cs = $2 == self ? "self" : $2
        print $1, cs, $3
    }'
}

# What a plain ">" does depends on the build: an ASCII build tags the
# file as text in the writer's CCSID; an EBCDIC build leaves it
# untagged, as /bin/sh does.
default_tag() {
    if [ "$self" = ISO8859-1 ]; then want="t IBM-1047 T=on"; else want="- untagged T=off"; fi
    got=$(tag "$1")
    [ "$got" = "$want" ] && echo "$1: build default" || echo "$1: $got"
}

echo "-- default"
echo text >plain
default_tag plain

echo "-- _TAG_REDIR_OUT"
_TAG_REDIR_OUT=TXT echo text >t1
tag t1
_TAG_REDIR_OUT=BIN echo text >b1
tag b1
# a one-command prefix does not persist
echo text >after
default_tag after
# an already tagged target keeps its tag
printf 'x\n' >keep; chtag -tc IBM-037 keep
_TAG_REDIR_OUT=TXT echo text >>keep
tag keep
# external writer: the tag is the writer's CCSID (cat is EBCDIC)
_TAG_REDIR_OUT=TXT cat plain >t2
ls -T t2 | awk '{ print $1, $2 }'

echo "-- _TAG_REDIR_ERR"
_TAG_REDIR_ERR=TXT ls /nonexistent 2>e1
ls -T e1 | awk '{ print $1, $3 }'

echo "-- exported reaches a child shell"
_TAG_REDIR_OUT=BIN; export _TAG_REDIR_OUT
"$TSH" -c 'echo text >c1'
tag c1
unset _TAG_REDIR_OUT

echo "-- _TAG_REDIR_IN"
# Bytes are written untranslated (binary tag), then tagged by chtag.
_TAG_REDIR_OUT=BIN printf 'hello\n' >in819     # ASCII bytes in an ASCII build
[ "$self" = IBM-1047 ] && _TAG_REDIR_OUT=BIN printf '\150\145\154\154\157\012' >in819
chtag -tc ISO8859-1 in819
_TAG_REDIR_IN=TXT cat <in819
_TAG_REDIR_IN=BIN od -An -tx1 <in819 | tr -d ' \n' | tr '[:upper:]' '[:lower:]'
echo

echo "-- pipeline stage tagged by _TAG_REDIR_OUT"
_TAG_REDIR_OUT=TXT; export _TAG_REDIR_OUT
echo through | cat
unset _TAG_REDIR_OUT

echo "-- here-document read by an EBCDIC tool"
cat <<END
here doc text
END

echo "-- scripts in the other character set"
# The bodies are written as octal bytes with a binary tag, so they are
# exactly IBM-1047 or ISO8859-1 whichever build writes them.
_TAG_REDIR_OUT=BIN printf '\205\203\210\226\100\206\231\226\224\140\361\360\364\367\025' >s1047
chtag -tc IBM-1047 s1047
_TAG_REDIR_OUT=BIN printf '\145\143\150\157\040\146\162\157\155\055\070\061\071\012' >s819
chtag -tc ISO8859-1 s819
_TAG_REDIR_OUT=BIN printf '\205\203\210\226\100\244\225\243\201\207\207\205\204\140\361\360\364\367\025' >u1047
chtag -r u1047
"$TSH" s1047
"$TSH" s819
. ./s819
"$TSH" u1047

echo "-- binary output through a redirect"
# The tag and the conversion live on the descriptor, so a child inherits
# both.  A program that writes binary turns conversion off for its own
# output, and the archive must then be the one /bin/sh's redirect makes.
if command -v gzip >/dev/null 2>&1; then
    /bin/sh -c 'gzip -c plain >g1'
    "$TSH" -c 'gzip -c plain' >g2
    "$TSH" -c 'gzip -c plain >g3'
    cmp -s g1 g2 && echo "stdout: same archive"
    cmp -s g1 g3 && echo "redirect: same archive"
    gzip -dc g2 | head -n 1
fi