Re: [fpc-pascal] TProcess questions

2006-10-04 Thread Adrian Maier

On 10/3/06, Adrian Maier [EMAIL PROTECTED] wrote:

On 10/3/06, Michael Van Canneyt [EMAIL PROTECTED] wrote:

 On Tue, 3 Oct 2006, Adrian Maier wrote:

  On 10/3/06, Michael Van Canneyt [EMAIL PROTECTED] wrote:
Any idea how should i quote the arguments so that they would be properly
treated by TProcess?
  
   You can't. The following lines (line 107 of unix/process.inc)
   Result:=StringReplace(Result,'','',[rfReplaceAll]);
   Result:=StringReplace(Result,,'',[rfReplaceAll]);
   Will strip all quotes inside a quoted string.
  
   Obviously, this is a bug.
 
  It's a painful one.  I was hoping to switch to TProcess instead of shell (
  which isn't  cross-platform )  ...

 The bug won't occur on windows :)

 I will see about fixing it. Could you please post a bug report so it won't
 be forgotten.


Hello guys,

Luckily I have discovered a workaround . It is possible to add the
simple quotes
later, inside the sql script. So, it is ok to run something like :
psql  -f  abc.sql   -v DATE1=1-jan-2006

if I add at the beginning of the script:
\set  DATE1 '\'':DATE1'\''

Chers !
Adrian Maier
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] TProcess questions

2006-10-03 Thread Michael Van Canneyt


On Tue, 3 Oct 2006, Adrian Maier wrote:

 Hello,
 
 I have several questions about executing external programs with TProcess
 
 1.  I need to execute the following command :
  psql -q -f /home/am/src/gfaf/hlemit_d.sql  -v D1='1-apr-06' -v
 D2='1-jun-06'
 
 (it might not be obvious at first sight that there are both   and  '
 enclosing those dates , because the argument must contain the '  ) .
 
 This command works fine when executed from the bash shell.  However, it
 no longer works when executed from my pascal program (with TProcess).
 Any idea how should i quote the arguments so that they would be properly
 treated by TProcess?

You can't. The following lines (line 107 of unix/process.inc)
 Result:=StringReplace(Result,'','',[rfReplaceAll]);
 Result:=StringReplace(Result,,'',[rfReplaceAll]);
Will strip all quotes inside a quoted string.

Obviously, this is a bug.

 
 2.  Where is the TProcess documented on the freepascal website? I had
 found a FCL.pdf  some time ago , but I am unable to find that doc on the
 freepascal website.

It is not documented currently. It's scheduled as the next unit to be
documented.

 
 3.  The code returned by the called program can be got with
 TProcess.ExitCode , right ?   I have a situation in which a program  1
 , but  TProcess.ExitCode is 0.   Therefore it's impossible to detect
 the failure.

That can be if the program exited because of a signal.

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] TProcess questions

2006-10-03 Thread Michael Van Canneyt


On Tue, 3 Oct 2006, Michael Van Canneyt wrote:

  2.  Where is the TProcess documented on the freepascal website? I had
  found a FCL.pdf  some time ago , but I am unable to find that doc on the
  freepascal website.
 
 It is not documented currently. It's scheduled as the next unit to be
 documented.

Eeh, forget this. it's documented (must be getting old, memory starts
failing) you can find it at:

ftp://ftp.freepascal.org/pub/fpc/docs-pdf/fcl.pdf

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] TProcess questions

2006-10-03 Thread Vincent Snijders

Michael Van Canneyt schreef:


On Tue, 3 Oct 2006, Adrian Maier wrote:


2.  Where is the TProcess documented on the freepascal website? I had
found a FCL.pdf  some time ago , but I am unable to find that doc on the
freepascal website.


It is not documented currently. It's scheduled as the next unit to be
documented.



It is documented using fpdoc:
http://lazarus-ccr.sourceforge.net/docs/fcl/process/index.html

I guess there will a similar page on www.freepascal.org too.

Vincent
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] TProcess questions

2006-10-03 Thread Adrian Maier

On 10/3/06, Michael Van Canneyt [EMAIL PROTECTED] wrote:

 Any idea how should i quote the arguments so that they would be properly
 treated by TProcess?

You can't. The following lines (line 107 of unix/process.inc)
 Result:=StringReplace(Result,'','',[rfReplaceAll]);
 Result:=StringReplace(Result,,'',[rfReplaceAll]);
Will strip all quotes inside a quoted string.

Obviously, this is a bug.


It's a painful one.  I was hoping to switch to TProcess instead of shell (
which isn't  cross-platform )  ...



 2.  Where is the TProcess documented on the freepascal website?


Michael, Vincent ,
Thanks for the links for TProcess docs .   :-)


 3.  The code returned by the called program can be got with
 TProcess.ExitCode , right ?   I have a situation in which a program  1
 , but  TProcess.ExitCode is 0.   Therefore it's impossible to detect
 the failure.

That can be if the program exited because of a signal.


I'm not sure if this was the case - I'll have to investigate.
Anyway, is it possible to detect this situation ?


Cheers,
Adrian Maier
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] TProcess questions

2006-10-03 Thread Michael Van Canneyt


On Tue, 3 Oct 2006, Adrian Maier wrote:

 On 10/3/06, Michael Van Canneyt [EMAIL PROTECTED] wrote:
   Any idea how should i quote the arguments so that they would be properly
   treated by TProcess?
 
  You can't. The following lines (line 107 of unix/process.inc)
  Result:=StringReplace(Result,'','',[rfReplaceAll]);
  Result:=StringReplace(Result,,'',[rfReplaceAll]);
  Will strip all quotes inside a quoted string.
 
  Obviously, this is a bug.
 
 It's a painful one.  I was hoping to switch to TProcess instead of shell (
 which isn't  cross-platform )  ...

The bug won't occur on windows :)

I will see about fixing it. Could you please post a bug report so it won't
be forgotten.

 
 
   2.  Where is the TProcess documented on the freepascal website?
 
 Michael, Vincent ,
 Thanks for the links for TProcess docs .   :-)
 
   3.  The code returned by the called program can be got with
   TProcess.ExitCode , right ?   I have a situation in which a program  1
   , but  TProcess.ExitCode is 0.   Therefore it's impossible to detect
   the failure.
 
  That can be if the program exited because of a signal.
 
 I'm not sure if this was the case - I'll have to investigate.
 Anyway, is it possible to detect this situation ?

Well, first of all you should do a 'IsRunning' (or running) before checking 
the exitcode. Only after that you can check the exit code. About the
signalling, we'd need to extend TProcess for that.

Michael.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] TProcess questions

2006-10-03 Thread Adrian Maier

On 10/3/06, Michael Van Canneyt [EMAIL PROTECTED] wrote:


On Tue, 3 Oct 2006, Adrian Maier wrote:

 On 10/3/06, Michael Van Canneyt [EMAIL PROTECTED] wrote:
   Any idea how should i quote the arguments so that they would be properly
   treated by TProcess?
 
  You can't. The following lines (line 107 of unix/process.inc)
  Result:=StringReplace(Result,'','',[rfReplaceAll]);
  Result:=StringReplace(Result,,'',[rfReplaceAll]);
  Will strip all quotes inside a quoted string.
 
  Obviously, this is a bug.

 It's a painful one.  I was hoping to switch to TProcess instead of shell (
 which isn't  cross-platform )  ...

The bug won't occur on windows :)



I will see about fixing it. Could you please post a bug report so it won't
be forgotten.


Ok.


   3.  The code returned by the called program can be got with
   TProcess.ExitCode , right ?   I have a situation in which a program  1
   , but  TProcess.ExitCode is 0.   Therefore it's impossible to detect
   the failure.
 
  That can be if the program exited because of a signal.

 I'm not sure if this was the case - I'll have to investigate.
 Anyway, is it possible to detect this situation ?

Well, first of all you should do a 'IsRunning' (or running) before checking
the exitcode.


I'm using the poWaitOnExit  option,  so my program doesn't waits
until the external process ends.   All I need is to know if the
external process ended successfully or not .


Only after that you can check the exit code. About the signalling,
we'd need to extend TProcess for that.


I understand.


Cheers,
Adrian Maier
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] TProcess questions

2006-10-03 Thread Adrian Maier

On 10/3/06, Michael Van Canneyt [EMAIL PROTECTED] wrote:


I will see about fixing it. Could you please post a bug report so it won't
be forgotten.


Mantis issue 7534  added .
Is it likely for the fix to go into fpc 2.0.4? Or just in the HEAD ?


Cheers,
Adrian Maier
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] TProcess questions

2006-10-03 Thread Adrian Maier

On 10/3/06, Adrian Maier [EMAIL PROTECTED] wrote:


 Well, first of all you should do a 'IsRunning' (or running) before checking
 the exitcode.

I'm using the poWaitOnExit  option,  so my program doesn't waits
until the external process ends.   All I need is to know if the
external process ended successfully or not .


Sorry for the spelling mistakes. I meant:
I'm using the poWaitOnExit  option,  so my program waits for the external
process to end.  In this case the process is surely not running anymore when I
get the exitcode. All I need is to know if the external process ended
successfully
or not .



 Only after that you can check the exit code. About the signalling,
 we'd need to extend TProcess for that.

I understand.


Cheers,
Adrian Maier




--
Adrian Maier
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] TProcess questions

2006-10-03 Thread Marco van de Voort
 On Tue, 3 Oct 2006, Adrian Maier wrote:

  
  It's a painful one.  I was hoping to switch to TProcess instead of shell (
  which isn't  cross-platform )  ...
 
 The bug won't occur on windows :)
 
 I will see about fixing it. Could you please post a bug report so it won't
 be forgotten.

Wouldn't it be better to also have a way to set the parameters using an
indexed property one by one instead of a cmdline?

Because this kind of interpretation issues will continue forever, since shell
handling is not portable. This was
pretty much the reason for the change to array of string in ExecuteProcess
and friends.

In such case, there is always a safe fall back.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] TProcess questions

2006-10-03 Thread Adrian Maier

On 10/3/06, Marco van de Voort [EMAIL PROTECTED] wrote:

 On Tue, 3 Oct 2006, Adrian Maier wrote:

 
  It's a painful one.  I was hoping to switch to TProcess instead of shell (
  which isn't  cross-platform )  ...

 The bug won't occur on windows :)

 I will see about fixing it. Could you please post a bug report so it won't
 be forgotten.

Wouldn't it be better to also have a way to set the parameters using an
indexed property one by one instead of a cmdline?

Because this kind of interpretation issues will continue forever, since shell
handling is not portable. This was
pretty much the reason for the change to array of string in ExecuteProcess
and friends.

In such case, there is always a safe fall back.


It seems nice to have a way of specifying parameters like that (in which case
I assume that strings given will be passed as-they-are).

There is one more thing about TProcess  that i've been surprised to
see that after:
  proc.Environment.Append('PGDATABASE=db1');
all the variables inherited from the parent process are no longer passed to the
external program by TProcess.

The behaviour is documented :
 Environment contains the environment for the new process; it's a
list of Name=Value pairs, one per line. If it is empty, the
environment of the current process is passed on to the new process. 

So, initially the Environment is empty and the program inherits the variables.
When I append the first variable, it will no longer inherit the variables.

I find the behaviour counterintuitive:  the first append causes the
Environment to
be initialised.

I haven't discovered yet how could the programmer preserve the
inherited environment
and only add some variables ?

Don't you think that by default the Environment list should be filled
with the current
environment?   This way append would mean indeed append.  It would also
be easy to clear the Environment if needed.


--
Adrian Maier
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] TProcess questions

2006-10-03 Thread Andrew Haines
Adrian Maier wrote:
 So, initially the Environment is empty and the program inherits the
 variables.
 When I append the first variable, it will no longer inherit the variables.
 
 I find the behaviour counterintuitive:  the first append causes the
 Environment to
 be initialised.
 
 I haven't discovered yet how could the programmer preserve the
 inherited environment
 and only add some variables ?

This copies all the envvars to a stringlist. You can add your own of
course too.

function GetEnvironmentVars: TStringList;
var
  I: Integer;
  fName: String;
begin
  Result := TStringList.Create;
  for I := 0 to GetEnvironmentVariableCount-1 do begin
fName := GetEnvironmentString(I);
Result.Add(fName);
  end;
end;


HTH

Andrew Haines
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal