0

I am trying to execute a TCL script from PHP. I am using PHP's proc_open for the communication .But I am unable to get the result from the tcl script . Can someone go through the code and let me know where I am going wrong ?

PHP code

<?php
$app = 'tclsh84.exe';

$spec = array(array("pipe", "r"), array("pipe", "w"), array("pipe", "w"));

$process = proc_open($app, $spec, $pipes);

if (is_resource($process)) 
{

    fwrite($pipes[0], 'source sum.tcl ');
    fwrite($pipes[0], 'tclsh test.tcl ');
    fclose($pipes[0]);

  echo stream_get_contents($pipes[1]);
  fclose($pipes[1]);

 //   echo fread($pipes[1],1024).'<hr>';


   proc_close($process);
}
?>


//sum.tcl 
proc sum {arg1 arg2} {
    set x [expr {$arg1 + $arg2}];
    return $x
}

//test.tcl
puts " the sum is [sum 10 9 ] "
Vidya
  • 7,717
  • 12
  • 48
  • 75
  • What do you see on the error pipe? – glenn jackman Aug 24 '09 at 17:54
  • "tclsh" is not a Tcl command. Did you mean "source test.tcl"? – glenn jackman Aug 24 '09 at 17:54
  • I don't see any error . I just get a blank page on the browser. tclsh is a command , it is used to execute a tcl file. – Vidya Aug 25 '09 at 00:56
  • I know tclsh is a command. But it is not a command that tclsh84.exe understands (which is the $app of your $process). Also, what's the exit status from proc_close? What error messages can you read from $pipes[2]? You have a lot of debugging you should be doing yourself. – glenn jackman Aug 25 '09 at 13:26
  • tclsh was creating a new child process .hence the script was not executing .I just sourced the file and called the proc from the same file – Vidya Aug 25 '09 at 13:39
  • hey glen, Could you look in to this ? http://stackoverflow.com/questions/1326499/how-to-make-fread-blocking-and-read-until-the-end – Vidya Aug 25 '09 at 13:40

1 Answers1

1

You're not passing newlines to the application (fwrite($pipes[0], "source sum.tcl\n")), could that be the cause? Otherwise make sure to check all return values of your function calls. You should fail early, if the first fwrite() fails, for example.

soulmerge
  • 73,842
  • 19
  • 118
  • 155