Any idea how to generate binary output from Tclsh?

I've tried to port a simple Tcl full-screen editor to IOS and failed completely as IOS tclsh escapes control characters written by the puts command. For example, the following escape sequence should clear the screen, but as the ESCAPE character is displayed as ^[, it doesn't work:

router(tcl)#puts "\033\[2J"
^[[2J
router(tcl)#
Any ideas how to persuade the router to display raw binary data?

12 comments:

  1. Not sure if this helps but:

    SYNOPSIS
    binary format formatString ?arg arg ...?
    binary scan string formatString ?varName varName ...?

    DESCRIPTION
    This command provides facilities for manipulating binary data. The first form, binary format, creates a binary string from normal Tcl values. For example, given the values 16 and 22, on a 32 bit architecture, it might produce an 8-byte binary string consisting of two 4-byte integers, one for each of the numbers. The second form of the command, binary scan, does the opposite: it extracts data from a binary string and returns it as ordinary Tcl string values.

    ReplyDelete
  2. Here is a link to a TCL tutorial which i wrote
    http://www.internetworkpro.org/wiki/Edit_files_using_TCL

    ReplyDelete
  3. @windexh8er: Thanks, but it doesn't help. The binary format builds the string representation of binary data and is equivalent to what I did with the \033; the problem is that the puts command doesn't want to display binary data.

    @danshtr: Nice trick, which answers another question I've got a while ago. Thanks!

    ReplyDelete
  4. What if you populated a variable with the binary format formatString line and used puts to display it?

    ReplyDelete
  5. Same thing:

    router(tcl)#set x [binary format ca* 27 "\[2J" ]
    ^[[2J
    router(tcl)#puts "$x"
    ^[[2J

    ReplyDelete
  6. puts "\e[2J" does quite strange effect.

    --
    mtve

    ReplyDelete
  7. If you try to generate ansi escape sequences, you can use "term intern" on exec level

    proc fahne { } {
    exec "terminal international"
    puts "\033\[2J"
    puts "\033\[0;0H"
    puts "\033\[0m |##########"
    puts "\033\[0m |#########"
    puts "\033\[0m |\033\[31m########"
    puts "\033\[0m |\033\[31m########"
    puts "\033\[0m |\033\[33m#########"
    puts "\033\[0m |\033\[33m##########"
    puts "\033\[0m |"
    puts "\033\[0m |"
    puts "\033\[0m |"
    puts "\033\[0m\n\n\033\[32m Yepp \033\[0m"
    }
    fahne

    ReplyDelete
  8. Perfect. It works :) Now I'm getting the file contents on the screen as they should be, but cannot put the terminal into "raw" mode (giving Tclsh a character at a time). Maybe I'll get some crazy idea over weekend ;)

    ReplyDelete
  9. If you are looking for something like Pascals getch I think you can use fileevent

    proc processinput fd {} {
    ...
    }
    fileevent stdin readable {processinput stdin}
    vwait forever

    ReplyDelete
  10. Well, the problem is that Tcl script gets the input only after the user has pressed the ENTER key, which is a problem if you're trying to implement a full-screen editor. So even your suggestion will not make stdin readable until the user has signalled the end of line.

    ReplyDelete
  11. Ok you're right. Have a look at http://wiki.tcl.tk/11820
    If you remove the shell code, all stty commands and the encoding parameter from the fconfigure commands it is at least startable on ios (with "term inter").

    ReplyDelete
  12. I'm trying to port the newer version of it (http://wiki.tcl.tk/16056). And, yes, what we need is exactly the stty -raw :(

    ReplyDelete

You don't have to log in to post a comment, but please do provide your real name/URL. Anonymous comments might get deleted.

Ivan Pepelnjak, CCIE#1354, is the chief technology advisor for NIL Data Communications. He has been designing and implementing large-scale data communications networks as well as teaching and writing books about advanced technologies since 1990. See his full profile, contact him or follow @ioshints on Twitter.