# 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: 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 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