Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Storing Tcl source code encoded, and running via your own C program

Tcl is a language. And Tcl is also a C library. How's that?

Tcl originates from a requirement to provide a taioring capability within C programs that could be accessed in a program-like style. And the program-like style that was implemented actually had the full capabilities of a programming language. That means that you can write a piece of Tcl to run within your C program if it has appropriate hooks, and you can also write a piece of Tcl to run within a C program such as tclsh which is provided with Tcl as a shell in which Tcl can run - basically, tclsh is a C program with the hooks to run Tcl and not much more.

On some Tcl courses (where it's relevant to any of the delegates) we demonstrate how a Tcl interpretter can be embedded into a C program, and how C code can be added to a Tcl program to allow for processor-intensive tasks and ready written C functionallity. And there are examples [here] and [here] on our web site. Also see [here] for the extended piece of Tcl that calls the C.

The fact that Tcl is a true scripting language, in which the program is run directly from source code, really worries some users. They're concerned that their source code could be ripped off by users. And they're concerned that the source code could be modified, and then not do what was intended. We have an example in our notes in which the Tcl program source is actually embedded within the C program - that's [here] - but of course that doesn't make for an editable system, nor an elegant one. What's really wanted is a way in which an obfurscated / encoded source code can be kept in one file - useless except to the specific program that it's set up to run in, and rendered useless by attempts to edit it.

This concern came up today, and I found myself - long after the course would normally have been finished on a Friday afternoon - writing C code! My C program reads in a file of mixed-up Tcl, adding all the lines into a character array held in memory that's allocated via realloc to make sure that any length of code can be handled. It then decodes the Tcl program which looked like this when loaded:

  or csaukes rp{orpm}t{
  zp tu sn-noweilen" p$orpm?t"
  lfsu htsodtu
  es tzr[ egsts dtni
  ]r teru nr$

into this:

  proc askuser {prompt} {
    global rz
    puts -nonewline "$prompt? "
    flush stdout
    set rz [gets stdin]
    return $rz

and then ran it via the Tcl library. Full source - [here].
(written 2011-09-02)

Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
T243 - Tcl/Tk - Using Tcl from C
  [208] Examples - Gadfly, NI Number, and Tcl to C interface - (2005-02-10)

C212 - C and C based languages - Memory Management
  [1497] Training Season Starts again! - (2008-01-07)
  [1581] What is an lvalue? (Perl, C) - (2008-03-18)
  [1589] Dynamic Memory Allocation in C - calloc, realloc - (2008-03-22)
  [1670] Dynamic Memory Allocation in C - (2008-06-09)
  [1845] Passing a table from Lua into C - (2008-10-18)
  [2669] Efficient use of dynamic memory - C and realloc - (2010-03-10)
  [2848] C course - final course example puts it all together - (2010-07-02)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [3144] Setting up arrays in C - fixed size at compile time, or dynamic - (2011-01-24)
  [3386] Adding the pieces together to make a complete language - C - (2011-08-11)
  [4128] Allocating memory dynamically in a static language like C - (2013-06-30)
  [4340] Simple C structs - building up to full, dynamic example - (2014-12-03)
  [4634] Regression testing - via a very short C testing framework - (2016-01-29)
  [4635] Encapsulating logic in functions and structs - the C approach to Object Oriented techniques - (2016-01-30)

Back to
User defined sorting and other uses of callbacks in Tcl and Tk
Previous and next
Horse's mouth home
Forward to
What is a namespace and why do we need them?
Some other Articles
Making best use of the new enthusiasm for Melksham
Data that we use during our training courses, and other training resources
Tcl packages, pkg_mkIndex, pkgIndex.tcl -what are they and why use them.
What is a namespace and why do we need them?
Storing Tcl source code encoded, and running via your own C program
User defined sorting and other uses of callbacks in Tcl and Tk
Passing back multiple results in Tcl - upvar and uplevel
If its Sunday, must it be Weymouth?
Handling binary data in Perl is easy!
Single and double quotes strings in Perl - what is the difference?
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3416_Sto ... ogram.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb