TTnScript
Last Updated: 01/27/2007

Frequently Asked Questions for TTnScript:

     
  1. What is it? 
  2. Add event 

[Back to Main]













  
What is it?
Jan Tomasek    
21.09.1998

TTnScript is a descendent from TTnEmulVT. It does exactly what TTnEmulVT does (ANSI terminal emulation) and add some scripting capabilities. TTnScript follows the received data and search in the data stream for given strings. When found, an event handler is called.

Strings to search are specified by calling AddEvent. An ID (an integer number) which must be unique identifies each string.

You can remove a string using RemoveEvent, passing the ID you gave when inserting the string in the list. You can remove all the strings with RemoveAllEvents.

Each string to search for is associated with another string, which will be sent by the component when the search string is found. This can be used for example when you search for a login prompt ('login') to send the username when this prompt is found. Same for password.

Each string to search for is also associated with an event handler, which will be triggered when the string is found, right after having sent the string to send. This specific event can be used to customize what has to be done when the string is found (for example update the user interface or query the user for some value to send).

Finally, each string to search is associated with a set of flags which tells the component some special actions such as ignoring character case when comparing text, or make the string persistent (normally when a string has been found, it is removed from the list).

Strings are searched in the order they are added to the list. So it can be very different if you add 'login' and 'password' to search for than if you add 'login' only and then when 'login' is found, add 'password'.

To scan the data stream, the component use a circular buffer whose dimension is 80 characters by default. You can change that by assigning InputBufferSize. The buffer size should be at least twice the size of the longest string to search. If you use an oversized buffer, you have a performance penalty because the buffer is searched as each data packet comes into the stream.

An automatic login procedure could looks like this:

    TnScript1.AddEvent (1, 'login',    'root', [efIgnoreCase], nil);
    TnScript1.AddEvent (2, 'password', 'boss', [efIgnoreCase], nil);
    TnScript1.Connect;


The nil argument could be replaced by a procedure (event handler) to make some computing when the string to search for is found. Here is an example:

    TnScript1.AddEvent (2, 'prompt', '', [efIgnoreCase], PromptEvent);


procedure TForm1.PromptEvent (Sender : TObject; ID : Integer);
begin
    .... Your code goes here. You can do everything....
    Label1.Caption := `Logged!';
    TnScript1.SendStr ('ls -l' + #13 + #10);    Like sending some data
end;


 
[Return to Top]

  
Add event
Jan Tomasek    
05.11.1998

> Could someone please help me with the syntax for
> AddEvent in BCB1?
> I've tried a few. but can't seem to get it right...

The example in TnScript.PAS is as follows:

TnScript1.AddEvent(1, 'login',    'root' + #13#10, [efIgnoreCase], nil);
TnScript1.AddEvent(2, 'password', 'boss' + #13#10, [efIgnoreCase], nil);
TnScript1.Connect;

The only difference for BCB is how the flags are handled. In PASCAL the flags are implemented as Sets, but in BCB Sets are actually implemented as classes. So they must be initialized and accessed differently.

TnScript1->AddEvent(1,
    "login",    username + "\\r\\n",
    TEventFlags() << efIgnoreCase, NULL);
TnScript1->AddEvent(2,
    "password", password + "\\r\\n",
    TEventFlags() << efIgnoreCase, NULL);
TnScript1->Connect();

or

TEventFlags flags; // Creates an empty TEventFlags set
flags = flags << efIgnoreCase;   // Add a flag to the set
TnScript1->AddEvent(1, "login:",    userName+"\\r\\n", flags, NULL);
TnScript1->AddEvent(2, "Password:", passWord+"\\r\\n", flags, NULL);

flags = flags  << efPersistent;  // Add another flag to the set
TnScript1->AddEvent(3, "Timeout", "\\r\\n", flags, TimeoutEvent);

flags = flags  >> efPersistent;  // Remove a flag from the set
TnScript1->AddEvent(3, "$", command+"\\r\\n", flags, PromptEvent);
TnScript1->Connect();


 
[Return to Top]

   

The ICS FAQ is created and maintained by the ICS VIP Documentation Effort Group.
For more information on how to join our efforts, send email to:
[ICS FAQ]