f



l'C program executing from a tcl script with pipe opened using open command in Tcl

Hi all,

I wanted to communicate with a C program from a Tcl script.. The test
code i have written is below
Both the 'C' program and Tcl script are located in the same directory.

The 'C' program is below. I have named it  CTCL.C

#include<stdio.h>
#include<string.h>
int main()
{
int i; char c[30];
printf("Reading...");
scanf("%s",c);
printf("String read is :%s\n",c);

fflush(stdout);
return 0;
}

I have compiled the program with the command
cc CTCL.C -o ctcl
So  the executable was generated with the name 'ctcl'


The Tcl script I have written is. I have named it TCLPIPETEST.tcl

#!/usr/add-on/exptools/bin/tclsh

set fd [open "|./ctcl" r+]
gets $fd tmp1
puts "tmp1 = $tmp1"
set stat 0
set tmp2 "Default value...  "
if { ![catch { puts $fd "abcdef" } res]} {
    if { ![catch { flush $fd } res] } {
          if { ![catch { gets $fd tmp2} res ] } {
               set stat 1
          } else { puts "Error 'gets' from pipe : $res" }
    } else { puts "Error 'flush' from pipe:$res " }
 } else { puts "Error 'puts' from pipe:$res " }

if { $stat == 1 } {
     puts "Success......:$tmp2 :)"
} else {
     puts "Failure......:$tmp2 :("
}


I have given all permissions for both the files above.
When i execute the  Tcl script
My aim is to open a pipe with the 'C' executable 'ctcl',
initially read some information from the pipe..which i have printed in
the c file...
write some information to the pipe so that the 'scanf in 'C' program
reads that information i wrote from the tcl script.

and print appropriate string back.
Now i wanted to read the string read back from the Tcl pipe using gets
function.

and print what i have read.

But what i observe is when i run the Tcl script nothing is happening..
I am not clear what is going wrong. Could anyone help me out.

Thanks in Advance
Ravi




0
rkanth.vvn (26)
7/11/2008 4:11:35 PM
comp.lang.tcl 23428 articles. 2 followers. Post Follow

1 Replies
1503 Views

Similar Articles

[PageSpeed] 47

Ravikanth wrote:
> Hi all,
> 
> I wanted to communicate with a C program from a Tcl script.. The test
> code i have written is below
> Both the 'C' program and Tcl script are located in the same directory.
> 
> The 'C' program is below. I have named it  CTCL.C
> 
> #include<stdio.h>
> #include<string.h>
> int main()
> {
> int i; char c[30];
> printf("Reading...");
> scanf("%s",c);
> printf("String read is :%s\n",c);
> 
> fflush(stdout);
> return 0;
> }
> 
> I have compiled the program with the command
> cc CTCL.C -o ctcl
> So  the executable was generated with the name 'ctcl'
> 
> 
> The Tcl script I have written is. I have named it TCLPIPETEST.tcl
> 
> #!/usr/add-on/exptools/bin/tclsh
> 
> set fd [open "|./ctcl" r+]
> gets $fd tmp1
> puts "tmp1 = $tmp1"
> set stat 0
> set tmp2 "Default value...  "
> if { ![catch { puts $fd "abcdef" } res]} {
>     if { ![catch { flush $fd } res] } {
>           if { ![catch { gets $fd tmp2} res ] } {
>                set stat 1
>           } else { puts "Error 'gets' from pipe : $res" }
>     } else { puts "Error 'flush' from pipe:$res " }
>  } else { puts "Error 'puts' from pipe:$res " }
> 
> if { $stat == 1 } {
>      puts "Success......:$tmp2 :)"
> } else {
>      puts "Failure......:$tmp2 :("
> }
> 
> 
> I have given all permissions for both the files above.
> When i execute the  Tcl script
> My aim is to open a pipe with the 'C' executable 'ctcl',
> initially read some information from the pipe..which i have printed in
> the c file...
> write some information to the pipe so that the 'scanf in 'C' program
> reads that information i wrote from the tcl script.
> 
> and print appropriate string back.
> Now i wanted to read the string read back from the Tcl pipe using gets
> function.
> 
> and print what i have read.
> 
> But what i observe is when i run the Tcl script nothing is happening..
> I am not clear what is going wrong. Could anyone help me out.

Gets in Tcl reads a line (one terminated with an end of line character). 
Your C program is not putting out the EOL nor is it flushing the buffer.

So what is happening is that the Tcl is waiting to read, and the C is 
waiting to read (with some stuff in its output buffer).

NOTE -- this is not a Tcl unique "problem", but would have happened if both 
pieces were written in any language.

-- 
+--------------------------------+---------------------------------------+
| Gerald W. Lester                                                       |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
0
Gerald.Lester (2014)
7/11/2008 5:30:38 PM
Reply: