• proposal: a command to check if object is an channel and writeable

    From aotto1968@21:1/5 to All on Mon May 16 12:07:10 2022
    hi,

    to implement a "puts" like command with:

    PrintFile ?channel? ....

    I require something to check if first argument is a channel and writeable.

    I don't have found such a "command" in tcl.

    possible command:

    chan writeable $FH → result boolean yes/no

    today I use something like:

    # usage: PrintFile ?chan? filename1 filename2 …
    proc PrintFile { args } {
    set outH [lindex $args 0]
    if { [ catch { flush $outH } ] } {

    set outH stderr

    } else {
    set args [lrange $args 1 end]

    }
    foreach fn $args {
    set FH [ open $fn r+ ]

    puts $outH [read $FH]

    close $FH

    }

    }

    to test if string is a channel I use "flush $outH" on error


    mfg.



    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Schelte@21:1/5 to All on Mon May 16 12:22:44 2022
    On 16/05/2022 12:07, aotto1968 wrote:
    I require something to check if first argument is a channel and writeable.

    This should work:

    if {[catch {chan event $outH writable}]} {
    error "$outH is not a writable channel"
    }


    Schelte

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ralf Fassel@21:1/5 to All on Mon May 16 13:48:01 2022
    * aotto1968 <aotto1968@t-online.de>
    | hi,

    | to implement a "puts" like command with:

    | > PrintFile ?channel? ....

    | I require something to check if first argument is a channel and writeable.

    Check whether FH is known as a channel could be
    if {$FH in [chan names]} {
    # FH is a known channel
    }

    Check whether FH is Writeable:

    chan pending output $FH
    - returns -1 if FH was not opened for writing

    Whether the channel is actually writeable (i.e. does not block on
    'puts') depends on too many factors (buffersize, blocking mode etc), so
    there is no generic answer.

    R'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From aotto1968@21:1/5 to Ralf Fassel on Tue May 17 09:53:29 2022
    On 16.05.22 13:48, Ralf Fassel wrote:
    * aotto1968 <aotto1968@t-online.de>
    | hi,

    | to implement a "puts" like command with:

    | > PrintFile ?channel? ....

    | I require something to check if first argument is a channel and writeable.

    Check whether FH is known as a channel could be
    if {$FH in [chan names]} {
    # FH is a known channel
    }

    Check whether FH is Writeable:

    chan pending output $FH
    - returns -1 if FH was not opened for writing

    Whether the channel is actually writeable (i.e. does not block on
    'puts') depends on too many factors (buffersize, blocking mode etc), so
    there is no generic answer.

    R'

    thanks, I understand that real "writeable" is complicated but at least I
    want distinguish between stdin and stout so that

    PrintFile stdin …

    is not possible

    example:

    puts stdin aaaaaaaaa
    channel "stdin" wasn't opened for writing

    → something check the stdin.

    mfg ao

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Ralf Fassel@21:1/5 to All on Tue May 17 15:20:35 2022
    * aotto1968 <aotto1968@t-online.de>
    | thanks, I understand that real "writeable" is complicated but at least
    | I want distinguish between stdin and stout so that

    | > PrintFile stdin …

    | is not possible

    | example:

    | puts stdin aaaaaaaaa
    | channel "stdin" wasn't opened for writing

    | → something check the stdin.

    % chan pending output stdin
    -1
    % chan pending output stdout
    0

    https://www.tcl.tk/man/tcl/TclCmd/chan.html

    chan pending mode channelId
    --<snip-snip>--
    Returns -1 if the channel was not opened for the mode in question.

    R'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)