PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.php > proc_open problem: \n translates to Windows-style line return \r\n
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
proc_open problem: \n translates to Windows-style line return \r\n

Réponse
 
LinkBack Outils de la discussion
Vieux 11/02/2008, 19h50   #1
Daniel Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut proc_open problem: \n translates to Windows-style line return \r\n

Without getting into a lot of unnecessary detail, I'm working on a project
to allow PHP to communicate with a proprietary database.

The IPC mechanism I'm using is 'proc_open'; there is a server-side component
(written in C) that 'listens' for PHP requests (via its stdin) and spits out
the appropriate response (via its stdout). Conversely, the PHP side uses
fwrite() to send to the component's stdin and fread() to read from the
component's stdout.

Ok so far?

What happens is that there is a situation where the C component will send a
a string with an embedded chr(10) via stdout to PHP's stdin. On Windows this
is getting converted to chr(13).chr(10).

So the problem is that if I send a n byte string ( with an embedded chr(10)
) then fread() gets an n+1 byte string ( with the addition of the chr(13) ).

I've already tried using the 'binary_pipes' in the 'other_options' array.

I've also tried adding a 'b' as a 'pipe' option (see code below) but PHP
doesn't like it.

Here's simplified PHP code to illustrate the problem :

$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"), //tried doing "wb"
2 => array("pipe", "r"));

$process = proc_open('C:\sandbox\bin\cprog.exe', $descriptorspec, $pipes);

if (is_resource($process)) {
$rtnval = fread($pipes[1], 1024);

echo "$rtnval <br />"
echo strlen($rtnval)."<br /><br />";

$a = str_split($rtnval);
foreach ($a as $character) {
echo ord($character)."<br />";
}

fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
}

If the C program sends the string 'LINE1'.chr(10).'LINE2' then PHP responds
with:

LINE1 LINE2
12

76
73
78
69
49
13 <---this is being added in by PHP
10
76
73
78
69
50


Is there some 'switch' that I can use to turn this off? Or am I going to
have to filter this out myself?

Daniel Klein
  Réponse avec citation
Vieux 13/02/2008, 15h40   #2
Daniel Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: proc_open problem: \n translates to Windows-style line return \r\n

On Mon, 11 Feb 2008 14:50:54 -0500, Daniel Klein <danielk@featherbrain.net>
wrote:

Ok, I'm missing a ;. The line should read:

echo "$rtnval <br />";

That's what I get for changing the script in my newsreader instead of in my
IDE (phpDesigner, which would have caugth this pronto) :-(

***

Anyway, it's been 2 days with no response; does this mean that I have found
a 'buglet'?

Daniel Klein
  Réponse avec citation
Vieux 13/02/2008, 15h54   #3
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: proc_open problem: \n translates to Windows-style line return \r\n

On Wed, 13 Feb 2008 16:40:58 +0100, Daniel Klein
<danielk@featherbrain.net> wrote:

> On Mon, 11 Feb 2008 14:50:54 -0500, Daniel Klein
> <danielk@featherbrain.net>
> wrote:
>
> Ok, I'm missing a ;. The line should read:
>
> echo "$rtnval <br />";
>
> That's what I get for changing the script in my newsreader instead of in
> my
> IDE (phpDesigner, which would have caugth this pronto) :-(
>
> ***
>
> Anyway, it's been 2 days with no response; does this mean that I have
> found
> a 'buglet'?


Depends, I cannot find you original post on my nntp server, it doesn't
ring a bell, and you haven't quoted yourself, so I have no idea what
you're writing about. Care to restate the problem for me?
--
Rik Wasmus
  Réponse avec citation
Vieux 13/02/2008, 17h47   #4
Daniel Klein
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: proc_open problem: \n translates to Windows-style line return \r\n

On Wed, 13 Feb 2008 16:54:54 +0100, "Rik Wasmus"
<luiheidsgoeroe@hotmail.com> wrote:

>On Wed, 13 Feb 2008 16:40:58 +0100, Daniel Klein
><danielk@featherbrain.net> wrote:
>
>> On Mon, 11 Feb 2008 14:50:54 -0500, Daniel Klein
>> <danielk@featherbrain.net>
>> wrote:
>>
>> Ok, I'm missing a ;. The line should read:
>>
>> echo "$rtnval <br />";
>>
>> That's what I get for changing the script in my newsreader instead of in
>> my
>> IDE (phpDesigner, which would have caugth this pronto) :-(
>>
>> ***
>>
>> Anyway, it's been 2 days with no response; does this mean that I have
>> found
>> a 'buglet'?

>
>Depends, I cannot find you original post on my nntp server, it doesn't
>ring a bell, and you haven't quoted yourself, so I have no idea what
>you're writing about. Care to restate the problem for me?


Here's the original (corrected) post:

===============================================
Without getting into a lot of unnecessary detail, I'm working on a project
to allow PHP to communicate with a proprietary database.

The IPC mechanism I'm using is 'proc_open'; there is a server-side component
(written in C) that 'listens' for PHP requests (via its stdin) and spits out
the appropriate response (via its stdout). Conversely, the PHP side uses
fwrite() to send to the component's stdin and fread() to read from the
component's stdout.

Ok so far?

What happens is that there is a situation where the C component will send a
a string with an embedded chr(10) via stdout to PHP's stdin. On Windows this
is getting converted to chr(13).chr(10).

So the problem is that if I send a n byte string ( with an embedded chr(10)
) then fread() gets an n+1 byte string ( with the addition of the chr(13) ).

I've already tried using the 'binary_pipes' in the 'other_options' array.

I've also tried adding a 'b' as a 'pipe' option (see code below) but PHP
doesn't like it.

Here's simplified PHP code to illustrate the problem :

$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"), //tried doing "wb"
2 => array("pipe", "r"));

$process = proc_open('C:\sandbox\bin\cprog.exe', $descriptorspec, $pipes);

if (is_resource($process)) {
$rtnval = fread($pipes[1], 1024);

echo "$rtnval <br />";
echo strlen($rtnval)."<br /><br />";

$a = str_split($rtnval);
foreach ($a as $character) {
echo ord($character)."<br />";
}

fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($process);
}

If the C program sends the string 'LINE1'.chr(10).'LINE2' then PHP responds
with:

LINE1 LINE2
12

76
73
78
69
49
13 <---this is being added in by PHP
10
76
73
78
69
50


Is there some 'switch' that I can use to turn this off? Or am I going to
have to filter this out myself?
================================================== ==

Daniel Klein
  Réponse avec citation
Vieux 13/02/2008, 20h02   #5
Rik Wasmus
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: proc_open problem: \n translates to Windows-style line return \r\n

On Wed, 13 Feb 2008 18:47:51 +0100, Daniel Klein
<danielk@featherbrain.net> wrote:
> Here's the original (corrected) post:
>
> ===============================================
> Without getting into a lot of unnecessary detail, I'm working on a
> project
> to allow PHP to communicate with a proprietary database.
>
> The IPC mechanism I'm using is 'proc_open'; there is a server-side
> component
> (written in C) that 'listens' for PHP requests (via its stdin) and spits
> out
> the appropriate response (via its stdout). Conversely, the PHP side uses
> fwrite() to send to the component's stdin and fread() to read from the
> component's stdout.
>
> Ok so far?
>
> What happens is that there is a situation where the C component will
> send a
> a string with an embedded chr(10) via stdout to PHP's stdin. On Windows
> this
> is getting converted to chr(13).chr(10).
>
> So the problem is that if I send a n byte string ( with an embedded
> chr(10)
> ) then fread() gets an n+1 byte string ( with the addition of the
> chr(13) ).
>
> I've already tried using the 'binary_pipes' in the 'other_options' array.


I assume you don't have PHP6, or did the secretly implement this allready?

> I've also tried adding a 'b' as a 'pipe' option (see code below) but PHP
> doesn't like it.
>
> Here's simplified PHP code to illustrate the problem :
>
> $descriptorspec = array(
> 0 => array("pipe", "r"),
> 1 => array("pipe", "w"), //tried doing "wb"
> 2 => array("pipe", "r"));


Shouldn't STDERR be 'w'?


> $process = proc_open('C:\sandbox\bin\cprog.exe', $descriptorspec,
> $pipes);
>
> if (is_resource($process)) {
> $rtnval = fread($pipes[1], 1024);
> $a = str_split($rtnval);
> foreach ($a as $character) {
> echo ord($character)."<br />";
> }
> }
>
> If the C program sends the string 'LINE1'.chr(10).'LINE2' then PHP
> responds
> with:
>


> 76
> 73
> 78
> 69
> 49
> 13 <---this is being added in by PHP
> 10


> Is there some 'switch' that I can use to turn this off? Or am I going to
> have to filter this out myself?


Hmmz, I tried this under windows:
'program with output' test.php:
<?php
echo "foo\nbar\n";
?>
Actual code:
<?php
$h = proc_open('C:\PHP\php.exe
G:\Personal\echo.php',array(0=>array('pipe','r'),1 =>array('pipe','w'),2=>array('pipe','w')),$pipes) ;
//,NULL,NULL,array('bypass_shell',true)); <- tried, not needed
if (is_resource($h)) {
$rtnval = fread($pipes[1], 1024);
echo "$rtnval \n";
echo strlen($rtnval)."\n\n";

$a = str_split($rtnval);
foreach ($a as $character) {
echo ord($character)."\n";
}

fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
proc_close($h);
}
?>
Output:
foo
bar

8

102
111
111
10
98
97
114
10

So, it's your C(++?) program. See for instance
<http://www.velocityreviews.com/forums/t281073-tricky-cout-with-0x0a-value.html>.
Make sure you output in binary instead of text mode, or oh so convenient
translation is done for you...
--
Rik Wasmus
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 03h02.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,16246 seconds with 13 queries