# This file is part of bish-bosh. It is subject to the licence terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/raphaelcohn/bish-bosh/master/COPYRIGHT. No part of bish-bosh, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
# Copyright © 2014-2015 The developers of bish-bosh. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/raphaelcohn/bish-bosh/master/COPYRIGHT.


bishbosh_connection_read_byte_initialise()
{
	if [ $bishbosh_connection_supportsNonBlockingRead -eq 1 ]; then

		# TMOUT also works for ksh93, bash
		# But works only for auto-logout in pdksh and Busybox's Ash
		bishbosh_connection_read_byte_blocking()
		{
			set +e
			IFS=' ' read -r -u 3 $1
			# $? == 1 is EOF
			set -e
		}
		
		# ksh93, bash, mksh (not yet), zsh, Busybox's Ash
		bishbosh_connection_read_byte_nonBlocking()
		{
			set +e
			IFS=' ' read -r -u 3 -t $bishbosh_readLatency_inFractionalSeconds $1
			set -e
		}
		
		bishbosh_connection_read_byte_nonBlockingInitialise()
		{
			:
		}
		
	else
		# dash, pdksh, other sh
		
		bishbosh_connection_read_byte_blocking()
		{
			set +e
			IFS=' ' read -r $1 <&3
			# $? == 1 is EOF
			set -e
		}

		# Relies on bishbosh_connection_background_processingLoopReadInterrupter, which fires HUP on a timer
		# Doesn't seem to work on bash, but then, should not be required
		bishbosh_connection_read_byte_nonBlocking()
		{
			trap 'true' HUP
			set +e
			# 2>/dev/null for AIX ksh being interrupted
			IFS=' ' read -r $1 <&3 2>/dev/null
			set -e
			trap '' HUP
		}
		
		bishbosh_connection_read_byte_nonBlockingInitialise()
		{
			trap '' HUP
		}
	fi
}
