0

I already saw this question similar ques for SQL

I am passing content of web page as argument to c++ program.

my code is here:

<?php
//sent has value "http://www.paulgraham.com/herd.html"
$url=$_POST['sent'];
$text = file_get_contents($url);
$temp=strip_tags($text);
$output=shell_exec("/home/technoworld/Videos/LinSocket/Modular/x '$temp'");
echo $output;
?> 

Which gives output for small content only. Output I get is

most : 1 in : 1 investors : 1 component : 1 2013The : 1 biggest : 1 August : 1 Dynamics : 1 Herd : 1 Investor : 1 Maximum Occurrences Investor Herd Dynamics August 2013The biggest component in most investors 

While actually it show gives count for each word.

Manually when I execute my program in command line:

./x "string"

It works and gives correct result for first paragraph of "http://www.paulgraham.com/herd.html" . But it does not pass second and next para content in argument. It just write it on command line.

How can I pass such a content in argument? For small content it gives correct output.

getconf ARG_MAX

Argument list allowed for my system is : 2097152

Community
  • 1
  • 1
user123
  • 5,269
  • 16
  • 73
  • 121
  • 1
    As an alternative to passing the entire contents as an argument, you could write the output to a file, then pass the file to the program (or, redirect the file as input. For example, write the contents to "tmp.txt" and call the program like "./x < tmp.txt"). – Alex Aug 31 '13 at 06:20
  • @Alex: Thanks, But file operation will become very costly for me. I have to deal with large string and multiple such a request concurrently! – user123 Aug 31 '13 at 06:27
  • The first paragraph contains `' `, which will end the first argument of your 'x' program. You could replace all `'` by `'"'"'`. – Bryan Olivier Aug 31 '13 at 06:27
  • @BryanOlivier: I tested in command line with this string `The biggest component in most investors' opinion of you`. Though it has `'` it proceed till the end of sentence! – user123 Aug 31 '13 at 06:38
  • @Karimkhan In this test did you surround the string with `"`? Note that in your PHP script your surround the string with `'`. – Bryan Olivier Aug 31 '13 at 06:41
  • @BryanOlivier: Now I wrote in this way: `shell_exec("/home/technoworld/Videos/LinSocket/Modular/x '"'$temp'"' ");` which gives error : `Parse error: syntax error, unexpected ''$temp'' (T_CONSTANT_ENCAPSED_STRING) in /opt/lampp/htdocs/graph/sentiment_graph/data.php on line` – user123 Aug 31 '13 at 06:55
  • @Karimkhan Look into the PHP function `escapeshellarg`. I think it will solve all your problems. – Bryan Olivier Aug 31 '13 at 06:56
  • @BryanOlivier: Thanks, it's very similar to my need, but `$output=shell_exec("/home/technoworld/Videos/LinSocket/Modular/x .escapeshellarg($temp)"); echo $output;` does not show anything. – user123 Aug 31 '13 at 07:06

2 Answers2

1

You might modify the C++ and PHP sources and build a pipe.

C++

int main () {
    std::ostringstream txt;
    txt << std::cin.rdbuf();
    std::cout << "Input: \n" << txt.str() << std::endl;
    return 0;
}

PHP

<?php
$pipe = popen('Debug/Test', 'w'); 
if( ! $pipe) {
    // Error
} 
else { 
    $txt = 'Hello World'; 
    fwrite($pipe, $txt, strlen($txt)); 
    pclose($pipe);
}
?>
sehe
  • 374,641
  • 47
  • 450
  • 633
  • I like your suggestion. Too bad it fell on deaf ears. (Did `cin>>txt.rdbuf()` work? I never tried it. Also, I think the other way around makes more sense, semantically) – sehe Aug 31 '13 at 23:18
1

I've changed your PHP program to use escapeshellarg as follows. I didn't test it, but my guess is that it will work

<?php
//sent has value "http://www.paulgraham.com/herd.html"
$url=$_POST['sent'];
$text = file_get_contents($url);
$temp=escapeshellarg(strip_tags($text));
$output=shell_exec("/home/technoworld/Videos/LinSocket/Modular/x " . $temp);
echo $output;
?>
Bryan Olivier
  • 5,207
  • 2
  • 16
  • 18
  • Excellent! Thanks a lot! You so sweet, please keep some nice profile picture :) – user123 Aug 31 '13 at 07:09
  • `$output=shell_exec("/home/technoworld/Videos/LinSocket/Modular/x " . $temp);` is equivalent to `$output=shell_exec("/home/technoworld/Videos/LinSocket/Modular/x '$temp'");`? or which one is correct? ` – user123 Sep 02 '13 at 13:22
  • @Karimkhan My PHP knowledge is a bit rusty. But my guess is that the second is also allowed, except you shouldn't use the quotes around `$temp` as these are added by `escapeshellarg` already. – Bryan Olivier Sep 02 '13 at 14:24