1#!/usr/bin/env bash 2 3# Copyright (c) 1993-2009, 2015 Paul Mattes. 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: 9# * Redistributions of source code must retain the above copyright 10# notice, this list of conditions and the following disclaimer. 11# * Redistributions in binary form must reproduce the above copyright 12# notice, this list of conditions and the following disclaimer in the 13# documentation and/or other materials provided with the distribution. 14# * Neither the name of Paul Mattes nor his contributors may be used 15# to endorse or promote products derived from this software without 16# specific prior written permission. 17# 18# THIS SOFTWARE IS PROVIDED BY PAUL MATTES "AS IS" AND ANY EXPRESS OR 19# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 21# NO EVENT SHALL PAUL MATTES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 23# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 25# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 26# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 29# s3270 glue for bash 30# 31# Intended use in a bash script: 32# 33# . s3270_glue.bash 34# Start 35# Connect host 36# Wait InputField 37# # interact with the host as needed 38# Disconnect 39# Stop 40 41#set -x 42 43# Define some handy functions. 44 45# s3270 interface function 46function xi 47{ 48 typeset x y 49 50 if [ "X$1" = "X-s" ] 51 then echo >&5 52 read x <&6 53 read <&6 54 echo "$x" | awk "{print \$$2}" 55 return 56 fi 57 58 echo >&5 "$@" 59 while read x <&6 60 do y=$(expr substr "$x" 1 5) 61 if [ "$y" = "data:" ] 62 then z="${x#?????}" 63 if [ -n "$z" ] 64 then echo "${z#?}" 65 else echo "$z" 66 fi 67 elif [ "$x" = ok ] 68 then return 0 69 elif [ "$x" = error ] 70 then return 1 71 fi 72 done 73 return 1 74} 75 76# 'xi' function, with space-to-comma and double-quote translation 77function xic 78{ 79 typeset sep cmd="$1(" 80 typeset a 81 82 shift 83 while [ $# -gt 0 ] 84 do echo "$1" | sed 's/"/\\"/' >/tmp/x$$ 85 a="$(</tmp/x$$)" 86 rm -f /tmp/x$$ 87 cmd="$cmd$sep\"$a\"" 88 sep="," 89 shift 90 done 91 cmd="$cmd)" 92 echo "cmd=$cmd" 93 xi "$cmd" 94} 95 96 97# All of the s3270 actions. 98function AnsiText 99{ 100 xic AnsiText "$@" 101} 102function Ascii 103{ 104 xic Ascii "$@" 105} 106function AsciiField 107{ 108 xic AsciiField "$@" 109} 110function Attn 111{ 112 xic Attn "$@" 113} 114function BackSpace 115{ 116 xic BackSpace "$@" 117} 118function BackTab 119{ 120 xic BackTab "$@" 121} 122function CircumNot 123{ 124 xic CircumNot "$@" 125} 126function Clear 127{ 128 xic Clear "$@" 129} 130function Connect 131{ 132 xic Connect "$@" 133} 134function CursorSelect 135{ 136 xic CursorSelect "$@" 137} 138function Delete 139{ 140 xic Delete "$@" 141} 142function DeleteField 143{ 144 xic DeleteField "$@" 145} 146function DeleteWord 147{ 148 xic DeleteWord "$@" 149} 150function Disconnect 151{ 152 xic Disconnect "$@" 153} 154function Down 155{ 156 xic Down "$@" 157} 158function Dup 159{ 160 xic Dup "$@" 161} 162function Ebcdic 163{ 164 xic Ebcdic "$@" 165} 166function EbcdicField 167{ 168 xic EbcdicField "$@" 169} 170function Enter 171{ 172 xic Enter "$@" 173} 174function Erase 175{ 176 xic Erase "$@" 177} 178function EraseEOF 179{ 180 xic EraseEOF "$@" 181} 182function EraseInput 183{ 184 xic EraseInput "$@" 185} 186function FieldEnd 187{ 188 xic FieldEnd "$@" 189} 190function FieldMark 191{ 192 xic FieldMark "$@" 193} 194function HexString 195{ 196 xic HexString "$@" 197} 198function Home 199{ 200 xic Home "$@" 201} 202function Insert 203{ 204 xic Insert "$@" 205} 206function Interrupt 207{ 208 xic Interrupt "$@" 209} 210function Key 211{ 212 xic Key "$@" 213} 214function Left 215{ 216 xic Left "$@" 217} 218function Left2 219{ 220 xic Left2 "$@" 221} 222function Macro 223{ 224 xic Macro "$@" 225} 226function MonoCase 227{ 228 xic MonoCase "$@" 229} 230function MoveCursor 231{ 232 xic MoveCursor "$@" 233} 234function Newline 235{ 236 xic Newline "$@" 237} 238function NextWord 239{ 240 xic NextWord "$@" 241} 242function PA 243{ 244 xic PA "$@" 245} 246function PF 247{ 248 xic PF "$@" 249} 250function PreviousWord 251{ 252 xic PreviousWord "$@" 253} 254function Query 255{ 256 xic Query "$@" 257} 258function Quit 259{ 260 xic Quit "$@" 261} 262function ReadBuffer 263{ 264 xic ReadBuffer "$@" 265} 266function Reset 267{ 268 xic Reset "$@" 269} 270function Right 271{ 272 xic Right "$@" 273} 274function Right2 275{ 276 xic Right2 "$@" 277} 278function Script 279{ 280 xic Script "$@" 281} 282function Snap 283{ 284 xic Snap "$@" 285} 286function String 287{ 288 xic String "$@" 289} 290function SysReq 291{ 292 xic SysReq "$@" 293} 294function Tab 295{ 296 xic Tab "$@" 297} 298function Toggle 299{ 300 xic Toggle "$@" 301} 302function ToggleInsert 303{ 304 xic ToggleInsert "$@" 305} 306function ToggleReverse 307{ 308 xic ToggleReverse "$@" 309} 310function Transfer 311{ 312 xic Transfer "$@" 313} 314function Up 315{ 316 xic Up "$@" 317} 318function Wait 319{ 320 xic Wait "$@" 321} 322 323# s3270 cursor column 324function Cursor_col 325{ 326 xi -s 10 327} 328 329# s3270 connection status 330function Cstatus 331{ 332 xi -s 4 333} 334 335# Start function 336function Start 337{ 338 # Set up pipes for s3270 I/O 339 ip=/tmp/ip.$$ 340 op=/tmp/op.$$ 341 rm -f $ip $op 342 trap "rm -f $ip $op" EXIT 343 trap "exit" INT QUIT HUP TERM 344 mknod $ip p 345 mknod $op p 346 347 # Start s3270 348 s3270 "$@" <$ip >$op & 349 xp=$! 350 exec 5>$ip 6<$op # hold the pipes open 351 xi -s 0 >/dev/null || exit 1 352} 353 354# Stop function 355function Stop 356{ 357 # Close the pipes. 358 exec 5>&- 6<&- 359 360 # Remove them. 361 rm -f $ip $op 362} 363 364# Failure. 365function Die 366{ 367 echo >&2 "$@" 368 Stop 369 exit 1 370} 371