Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2006-03

Executing another program from Pascal

7 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Hans Mårtensson

In good old days the Turbo Pascal had an EXEC procedure that could execute another program by invoking the DOS command interpreter.

In better new days the FPC does not seem to have such an option, or I couldn’t find it in the docs.
Now I am particularly interested in doing such a thing in a Linux system.

I have found in the unix unit a function FPEXECV(path, pp) that may be used for it, but it leaves a few questions to me.

According to the docs the function does not return (if it succeeds). Is there no way of calling another program and then continue the calling program? Could it be done by starting a new thread, putting the call into that thread and let that thread not return while the main program continues? Will it leave allocated memory, that is never freed?

If the function does not return, why is there a return integer value defined?
(Or: why is it called a function, when it does not return?)

In the doc’s example the FPEXECV(path, pp) has the same pchar in the path and as the first pchar of the pchar array that pp points to. I have tried similar examples, and they always seem to work with the same phar in the two places, but not otherwise. Is there some deeper philosophy in having the program-name in both path and in the first phar of the array?

Hans Maartensson

Elio Cuevas Gómez
to Hans Mårtensson
Hans Mårtensson wrote
In good old days the Turbo Pascal had an EXEC procedure that could execute another program by invoking the DOS command interpreter.

I think that function is in the DOS unit. And it even works in Linux!

Show quoted text 14 lines
In better new days the FPC does not seem to have such an option, or I couldn’t find it in the docs. Now I am particularly interested in doing such a thing in a Linux system. I have found in the unix unit a function FPEXECV(path, pp) that may be used for it, but it leaves a few questions to me. According to the docs the function does not return (if it succeeds). Is there no way of calling another program and then continue the calling program? Could it be done by starting a new thread, putting the call into that thread and let that thread not return while the main program continues? Will it leave allocated memory, that is never freed?

You must call fork() first an then call execv from the child process.

If the function does not return, why is there a return integer value defined? (Or: why is it called a function, when it does not return?)

Who knows?

Show quoted text 12 lines
In the doc’s example the FPEXECV(path, pp) has the same pchar in the path and as the first pchar of the pchar array that pp points to. I have tried similar examples, and they always seem to work with the same phar in the two places, but not otherwise. Is there some deeper philosophy in having the program-name in both path and in the first phar of the array? Hans Maartensson _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Marco van de Voort
to Hans Mårtensson: In good old days the Turbo Pascal had an EXEC procedure that could execute another…
In good old days the Turbo Pascal had an EXEC procedure that could execute another program by invoking the DOS command interpreter.

Dos.exec still exist, but as a substitute sysutils.ExecuteProcess is recommended. (1.9.2+ though)

Note that both don't exec the command itself.

Show quoted text 8 lines
I have found in the unix unit a function FPEXECV(path, pp) that may be used for it, but it leaves a few questions to me. According to the docs the function does not return (if it succeeds). Is there no way of calling another program and then continue the calling program? Could it be done by starting a new thread, putting the call into that thread and let that thread not return while the main program continues? Will it leave allocated memory, that is never freed?

See the earlier replies (and the source of executeprocess)

Show quoted text 5 lines
In the doc?s example the FPEXECV(path, pp) has the same pchar in the path and as the first pchar of the pchar array that pp points to. I have tried similar examples, and they always seem to work with the same phar in the two places, but not otherwise. Is there some deeper philosophy in having the program-name in both path and in the first phar of the array?

Note that fp* calls are calls that roughly comply to unix standards. A man page for the call minus the "fp" is usually available. (so "man execv" in this case). From the FreeBSD version:

..... The
first argument, by convention, should point to the file name associated with the file being executed. The list of arguments must be terminated by a NULL pointer.....

So the answer is "by convention" :-)

Also have a look at the tprocess class in the VCL, it allows for executing processes and keeping pipes to it open.

Only if these two instruments (tprocess/executeprocess) are not enough, then you need to mess with OS specific routines like execv, or windows like shellexecute.

Hans Mårtensson
to Marco van de Voort: Dos.exec still exist, but as a substitute sysutils.ExecuteProcess is recommended. (1.9.2+…
Marco van de Voort wrote 7 lines
Dos.exec still exist, but as a substitute sysutils.ExecuteProcess is recommended. (1.9.2+ though) Note that both don't exec the command itself.

Thank you for the information
Sometimes the docs looks like a haystack with no needle. (According to my view)

Elio Cuevas Gómez wrote 9 lines
El Mar 28 Mar 2006 13:40, Hans Mårtensson escribió: >In good old days the Turbo Pascal had an EXEC procedure .. > > I think that function is in the DOS unit. And it even works in Linux!

Until recently I have only made win32 programs. Now I am trying to do something on a Linux box. And I was surprised that the Linux version of FPC had the same units. Even the DOS unit works on the Linux system? Wonderful!

Thank you
Hans Mårtensson

Marco van de Voort
to Hans Mårtensson
Show quoted text 28 lines
Marco van de Voort wrote: >Dos.exec still exist, but as a substitute sysutils.ExecuteProcess is >recommended. (1.9.2+ though) > >Note that both don't exec the command itself. > Thank you for the information Sometimes the docs looks like a haystack with no needle. (According to my view) Elio Cuevas G?mez wrote: >El Mar 28 Mar 2006 13:40, Hans M?rtensson escribi?: > > >>In good old days the Turbo Pascal had an EXEC procedure .. >> >> >I think that function is in the DOS unit. And it even works in Linux! > > Until recently I have only made win32 programs. Now I am trying to do something on a Linux box. And I was surprised that the Linux version of FPC had the same units. Even the DOS unit works on the Linux system? Wonderful!

The more normal functions yes. Dos interrupt calls of course don't work.

However please use executeprocess, it will avoid problems with too deeply nested paths. (dos.exec is limited to an overall 255 chars).

Hans Mårtensson
to Marco van de Voort
Marco van de Voort wrote
However please use executeprocess, it will avoid problems with too deeply nested paths. (dos.exec is limited to an overall 255 chars).

Yes I got that - and that is exactly the function I need for my program, I think.