Hello Roland,

stdin is a terminal, you must set echo off (stty -echo).
I made few changes to deal with end of line and to be able to test it.

Because you are reading caracters one by one, you are in a raw mode.
So you must deal with end-of-file.

And ... I hard-code Ctrl-D as EOF.
I can get eof from stty, like :
    eof=$(stty -a | sed -n 's/eof = \([^;]*\);.*/\1/p')
But after, I need to change ^D to $'\cd' and I don't know how to do it!!

Regards,

Yves

-- snip --
#!/bin/ksh
set -u
# line buffer class
# The buffer class tries to read characters from the given <fd> until
# it has read a whole line.
typeset -T linebuf_t=(
       typeset buf

       function reset
       {
               _.buf=''
               return 0
       }

       function readbuf
       {
               nameref var=$1
               integer fd=$2
               typeset ch

               while read -u $fd -N 1 ch ; do
                        if [[ $ch == +($'\n'|$'\r'|$'\cd') ]]; then
                               var=${_.buf}
                               _.reset
                                [[ $var == '' ]] && return 1
                               return 0
                        fi
                        _.buf+=$ch
               done

               return 1
       }
)

integer infd
linebuf_t clientbuf

redirect {infd}< '/dev/stdin'
stty --fd=$infd -echo

trap 'stty --fd=$infd echo' EXIT

       while clientbuf.readbuf line $infd ; do
               printf $"client: %q\n" "$line"
               printf '%s\n' "$line" >&$infd
       done
# End Of File
-- snip --


yves crespin

On Wed, May 26, 2010 at 10:04 PM, Roland Mainz <[email protected]> wrote:
>
> Hi!
>
> ----
>
> Does anyone know why the example below echos the characters I type _twice_ ?
> -- snip --
> # line buffer class
> # The buffer class tries to read characters from the given <fd> until
> # it has read a whole line.
> typeset -T linebuf_t=(
>        typeset buf
>
>        function reset
>        {
>                _.buf=''
>                return 0
>        }
>
>        function readbuf
>        {
>                nameref var=$1
>                integer fd=$2
>                typeset ch
>
>                while IFS='' read -u${fd} -t 0.2 -N 1 ch ; do
>                        [[ "${ch}" == $'\r' ]] && continue
>
>                        if [[ "${ch}" == $'\n' ]] ; then
>                                var="${_.buf}"
>                                _.reset
>                                return 0
>                        fi
>
>                        _.buf+="$ch"
>                done
>
>                return 1
>        }
> )
>
> redirect {infd}< '/dev/stdin'
> linebuf_t clientbuf
>
> for (( ;; )) ; do
>        while clientbuf.readbuf line ${infd} ; do
>                printf $"client: %q\n" "${line}"
>                printf '%s\n' "${line}" >&${fd}
>        done
> done
> -- snip --
>
> If I run this script and enter "FISH" I get:
> FFIISSHH
>
> Erm... why does this happen ?
>
> ----
>
> Bye,
> Roland
>
> --
>  __ .  . __
>  (o.\ \/ /.o) [email protected]
>  \__\/\/__/  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
>  /O /==\ O\  TEL +49 641 3992797
>  (;O/ \/ \O;)
> _______________________________________________
> ast-users mailing list
> [email protected]
> https://mailman.research.att.com/mailman/listinfo/ast-users
>

_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users

Reply via email to