run pascal programs as scripts

classic Classic list List threaded Threaded
44 messages Options
123
Reply | Threaded
Open this post in threaded view
|

run pascal programs as scripts

Mattias Gaertner
Hi all,

I would like to execute small pascal programs like normal unix
scripts. For example:

#!/usr/bin/instantfpc
begin
  writeln('shebang executed');
end.

Has anyone already written a tool, that compiles and
runs the program?


Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Henry Vermaak
On 24 March 2011 16:06, Mattias Gaertner <[hidden email]> wrote:

> Hi all,
>
> I would like to execute small pascal programs like normal unix
> scripts. For example:
>
> #!/usr/bin/instantfpc
> begin
>  writeln('shebang executed');
> end.
>
> Has anyone already written a tool, that compiles and
> runs the program?

Using an idea I saw somewhere else, I've done this:

hcv@technical09:~/source/fpc_script$ ls -l
total 4
-rwxr-xr-x 1 hcv hcv 148 Mar 24 16:38 fpc_script.pp

hcv@technical09:~/source/fpc_script$ cat fpc_script.pp
// 2>/dev/null; fpc fpc_script.pp &> /dev/null && exec ./fpc_script "$@"; exit
program fpc_script;
begin
        writeln('hi there, from a script!');
end.

hcv@technical09:~/source/fpc_script$ ./fpc_script.pp
hi there, from a script!

It would be nice to have #!fpc --run

Henry
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Henry Vermaak
On 24 March 2011 16:44, Henry Vermaak <[hidden email]> wrote:

> On 24 March 2011 16:06, Mattias Gaertner <[hidden email]> wrote:
>> Hi all,
>>
>> I would like to execute small pascal programs like normal unix
>> scripts. For example:
>>
>> #!/usr/bin/instantfpc
>> begin
>>  writeln('shebang executed');
>> end.
>>
>> Has anyone already written a tool, that compiles and
>> runs the program?
>
> Using an idea I saw somewhere else, I've done this:
>
> hcv@technical09:~/source/fpc_script$ ls -l
> total 4
> -rwxr-xr-x 1 hcv hcv 148 Mar 24 16:38 fpc_script.pp
>
> hcv@technical09:~/source/fpc_script$ cat fpc_script.pp
> // 2>/dev/null; fpc fpc_script.pp &> /dev/null && exec ./fpc_script "$@"; exit

Or even:

// 2>/dev/null; fpc fpc_script.pp &> build.log && exec ./fpc_script
"$@" || cat build.log; exit
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Mattias Gaertner
On Thu, 24 Mar 2011 16:53:54 +0000
Henry Vermaak <[hidden email]> wrote:

> On 24 March 2011 16:44, Henry Vermaak <[hidden email]> wrote:
> > On 24 March 2011 16:06, Mattias Gaertner <[hidden email]> wrote:
> >> Hi all,
> >>
> >> I would like to execute small pascal programs like normal unix
> >> scripts. For example:
> >>
> >> #!/usr/bin/instantfpc
> >> begin
> >>  writeln('shebang executed');
> >> end.
> >>
> >> Has anyone already written a tool, that compiles and
> >> runs the program?
> >
> > Using an idea I saw somewhere else, I've done this:
> >
> > hcv@technical09:~/source/fpc_script$ ls -l
> > total 4
> > -rwxr-xr-x 1 hcv hcv 148 Mar 24 16:38 fpc_script.pp
> >
> > hcv@technical09:~/source/fpc_script$ cat fpc_script.pp
> > // 2>/dev/null; fpc fpc_script.pp &> /dev/null && exec ./fpc_script "$@"; exit
>
> Or even:
>
> // 2>/dev/null; fpc fpc_script.pp &> build.log && exec ./fpc_script
> "$@" || cat build.log; exit

Nice idea. I didn't know the // 2>/dev/null trick.
It does not work with tcsh.
The working directory must be '.'.
The temp files should go to another directory.


Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Michael Van Canneyt
In reply to this post by Mattias Gaertner


On Thu, 24 Mar 2011, Mattias Gaertner wrote:

> Hi all,
>
> I would like to execute small pascal programs like normal unix
> scripts. For example:
>
> #!/usr/bin/instantfpc
> begin
>  writeln('shebang executed');
> end.
>
> Has anyone already written a tool, that compiles and
> runs the program?

As far as I know, the "!/something just means, start /something,
and pass current filename as a commandline param ?

So all your tool has to do is write all except the first line to a temp file,
compile temp file and execute it ?

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Mattias Gaertner
On Thu, 24 Mar 2011 18:38:04 +0100 (CET)
Michael Van Canneyt <[hidden email]> wrote:

>
>
> On Thu, 24 Mar 2011, Mattias Gaertner wrote:
>
> > Hi all,
> >
> > I would like to execute small pascal programs like normal unix
> > scripts. For example:
> >
> > #!/usr/bin/instantfpc
> > begin
> >  writeln('shebang executed');
> > end.
> >
> > Has anyone already written a tool, that compiles and
> > runs the program?
>
> As far as I know, the "!/something just means, start /something,
> and pass current filename as a commandline param ?
>
> So all your tool has to do is write all except the first line to a temp file,
> compile temp file and execute it ?

Almost.
- It should return the exit code.
- If compile fails it should print out the fpc output.
- It should give nice error messages.
- Handle spaces in parameters correctly.
- Preferably it recompiles only if source changed.


Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Mark Morgan Lloyd-5
In reply to this post by Michael Van Canneyt
Michael Van Canneyt wrote:

> On Thu, 24 Mar 2011, Mattias Gaertner wrote:
>
>> Hi all,
>>
>> I would like to execute small pascal programs like normal unix
>> scripts. For example:
>>
>> #!/usr/bin/instantfpc
>> begin
>>  writeln('shebang executed');
>> end.
>>
>> Has anyone already written a tool, that compiles and
>> runs the program?
>
> As far as I know, the "!/something just means, start /something, and
> pass current filename as a commandline param ?

In general, "pass all parameters" allowing that unix doctrine expects an
implicit zeroeth parameter which is the name of the current program.

> So all your tool has to do is write all except the first line to a temp
> file, compile temp file and execute it ?

If I recall correctly, Rexx has special handling for the first line
which- intentionally or otherwise- facilitates shebang handling. Also
I'm reminded of a Perl option which causes it to throw away anything
before valid scripting.

Would it be feasible to modify FPC so that it discarded everything
preceding "program", "unit" etc?

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Robert Wolfe, MCP
On 3/24/2011 2:27 PM, Mark Morgan Lloyd wrote:

> Michael Van Canneyt wrote:
>> On Thu, 24 Mar 2011, Mattias Gaertner wrote:
>>
>>> Hi all,
>>>
>>> I would like to execute small pascal programs like normal unix
>>> scripts. For example:
>>>
>>> #!/usr/bin/instantfpc
>>> begin
>>>  writeln('shebang executed');
>>> end.
>>>
>>> Has anyone already written a tool, that compiles and
>>> runs the program?
>>
>> As far as I know, the "!/something just means, start /something, and
>> pass current filename as a commandline param ?
>
> In general, "pass all parameters" allowing that unix doctrine expects
> an implicit zeroeth parameter which is the name of the current program.
>
>> So all your tool has to do is write all except the first line to a
>> temp file, compile temp file and execute it ?
>
> If I recall correctly, Rexx has special handling for the first line
> which- intentionally or otherwise- facilitates shebang handling. Also
> I'm reminded of a Perl option which causes it to throw away anything
> before valid scripting.
>
> Would it be feasible to modify FPC so that it discarded everything
> preceding "program", "unit" etc?
>

It wouldn't be Pascal then :)
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Felipe Monteiro de Carvalho
In reply to this post by Mark Morgan Lloyd-5
On Thu, Mar 24, 2011 at 7:27 PM, Mark Morgan Lloyd
<[hidden email]> wrote:
> Would it be feasible to modify FPC so that it discarded everything preceding
> "program", "unit" etc?

Feasable, yes, but it is probably easier to make a pre-processor which
removes this line, simply.

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Mattias Gaertner
On Thu, 24 Mar 2011 22:40:22 +0100
Felipe Monteiro de Carvalho <[hidden email]> wrote:

> On Thu, Mar 24, 2011 at 7:27 PM, Mark Morgan Lloyd
> <[hidden email]> wrote:
> > Would it be feasible to modify FPC so that it discarded everything preceding
> > "program", "unit" etc?
>
> Feasable, yes, but it is probably easier to make a pre-processor which
> removes this line, simply.

Commenting is better. Then the reported error line numbers are still
valid.

Mattias

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Felipe Monteiro de Carvalho
> Commenting is better. Then the reported error line numbers are still
> valid.

Yes, but then bash won't recognize it, I suppose

--
Felipe Monteiro de Carvalho
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Mattias Gaertner
On Thu, 24 Mar 2011 23:10:08 +0100
Felipe Monteiro de Carvalho <[hidden email]> wrote:

> > Commenting is better. Then the reported error line numbers are still
> > valid.
>
> Yes, but then bash won't recognize it, I suppose

I meant:

Original file:
#!/usr/bin/instantfpc
begin
end.

bash is happy.


Preprocessor creates:
//#!/usr/bin/instantfpc
begin
end.

fpc is happy.

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

ik-6




On Fri, Mar 25, 2011 at 00:21, Mattias Gaertner <[hidden email]> wrote:
On Thu, 24 Mar 2011 23:10:08 +0100
Felipe Monteiro de Carvalho <[hidden email]> wrote:

> > Commenting is better. Then the reported error line numbers are still
> > valid.
>
> Yes, but then bash won't recognize it, I suppose

I meant:

Original file:
#!/usr/bin/instantfpc

#!/usr/bin/env instantfpc

That's the cross platform way to do it for all unicses.

 
begin
end.

bash is happy.


Preprocessor creates:
//#!/usr/bin/instantfpc
begin
end.

fpc is happy.

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal



LINESIP - Opening the source for communication
http://www.linesip.com
http://www.linesip.co.il


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Mark Morgan Lloyd-5
In reply to this post by Mattias Gaertner
Mattias Gaertner wrote:

> On Thu, 24 Mar 2011 22:40:22 +0100
> Felipe Monteiro de Carvalho <[hidden email]> wrote:
>
>> On Thu, Mar 24, 2011 at 7:27 PM, Mark Morgan Lloyd
>> <[hidden email]> wrote:
>>> Would it be feasible to modify FPC so that it discarded everything preceding
>>> "program", "unit" etc?
>> Feasable, yes, but it is probably easier to make a pre-processor which
>> removes this line, simply.
>
> Commenting is better. Then the reported error line numbers are still
> valid.

Fpc treats # as // if (and only if) found as the first character of a
file? Character position in this line won't be relevant since it's not
going to be compilable source.

Also I notice that tcc (Tiny C Compiler) has a -run parameter which runs
the compiled program after compilation.

With things as they stand, particularly in view of the requirement that
the program is only recompiled if it's changed, I think the easiest way
would be a wrapper: since that could be of adequate complexity and would
have no restrictions on temporary files etc. it could excise/comment
without restriction.

--
Mark Morgan Lloyd
markMLl .AT. telemetry.co .DOT. uk

[Opinions above are the author's, not those of his employers or colleagues]
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

michael.vancanneyt
In reply to this post by Mattias Gaertner


On Thu, 24 Mar 2011, Mattias Gaertner wrote:

> On Thu, 24 Mar 2011 23:10:08 +0100
> Felipe Monteiro de Carvalho <[hidden email]> wrote:
>
>>> Commenting is better. Then the reported error line numbers are still
>>> valid.
>>
>> Yes, but then bash won't recognize it, I suppose
>
> I meant:
>
> Original file:
> #!/usr/bin/instantfpc
> begin
> end.
>
> bash is happy.
>
>
> Preprocessor creates:
> //#!/usr/bin/instantfpc
> begin
> end.
>
> fpc is happy.

Indeed it works:

fsb: >cat testscript
#!/home/michael/fpcscript
program testscript;

Var
   I : Integer;

begin
  Write('Testscript called as ');
  For I:=0 to ParamCount do
    begin
    if I>0 then
      Write(' ');
    Write(paramstr(i));
    end;
   writeln;
end.
fsb: >./testscript
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
Testscript called as /tmp/fpc00000.tmp
fsb: >

And here is fpcscript (50 lines only):

$mode objfpc}{$h+}
program fpcscript;

uses sysutils,classes,baseunix, unix;

Const
   BufSize = 10 * 1024;
   Comment = '//';

Type
   TStringArray = Array of string;

Var
   F1,F2 : TFileStream;
   FN : String;
   Args : TStringArray;
   E,I : integer;

begin
   E:=127;
   F1:=TFileStream.Create(ParamStr(1),fmOpenRead);
   try
     FN:=GetTempFileName(GetTempDir,'fpc')+'.pp';
     F2:=TFileStream.Create(FN,fmCreate);
     try
       F2.WriteBuffer(Comment[1],Length(Comment));
       F2.CopyFrom(F1,F1.Size);
     finally
       F2.Free;
     end;
     if (ExecuteProcess('/usr/local/bin/fpc',[FN])=0) then
       begin
       If FileExists(ChangeFileExt(FN,'.o')) then
         DeleteFIle(ChangeFileExt(FN,'.o'));
       SetLength(Args,ParamCount-1);
       For I:=2 to ParamCount do
         Args[I-1]:=Paramstr(i);
       E:=ExecuteProcess(ChangeFileExt(FN,''),Args);
       If FileExists(ChangeFileExt(FN,'')) then
         DeleteFile(ChangeFileExt(FN,''));
       If FileExists(FN) then
         DeleteFile(Fn);
       end;
   Finally
     F1.Free;
   end;
   Halt(E);
end.

Now we need to get rid of the ugly ld message and you're all set.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Mattias Gaertner
I wrote a little tool:

http://wiki.lazarus.freepascal.org/InstantFPC

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Michael Van Canneyt


On Sun, 27 Mar 2011, Mattias Gaertner wrote:

> I wrote a little tool:
>
> http://wiki.lazarus.freepascal.org/InstantFPC

Nice job. OK to include in FPC as one of the utils ?

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Mattias Gaertner
On Sun, 27 Mar 2011 19:05:12 +0200 (CEST)
Michael Van Canneyt <[hidden email]> wrote:

>
>
> On Sun, 27 Mar 2011, Mattias Gaertner wrote:
>
> > I wrote a little tool:
> >
> > http://wiki.lazarus.freepascal.org/InstantFPC
>
> Nice job. OK to include in FPC as one of the utils ?

Yes.

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Michael Van Canneyt


On Sun, 27 Mar 2011, Mattias Gaertner wrote:

> On Sun, 27 Mar 2011 19:05:12 +0200 (CEST)
> Michael Van Canneyt <[hidden email]> wrote:
>
>>
>>
>> On Sun, 27 Mar 2011, Mattias Gaertner wrote:
>>
>>> I wrote a little tool:
>>>
>>> http://wiki.lazarus.freepascal.org/InstantFPC
>>
>> Nice job. OK to include in FPC as one of the utils ?
>
> Yes.

Thank you. But please, fix a couple of bugs first:

1. I get a stream read error each time I execute a script the first time (i.e. it's not yet in the cache) :
home: >./helloworld.pas
An unhandled exception occurred at $0000000000425556 :
EReadError : Stream read error
   $0000000000425556
   $00000000004256FA
   $0000000000469E8A
   $0000000000400A25

home: >./helloworld.pas
Hello world 2


2. passing options on the commandline does not work on my Kubuntu 10.04:

home: >./envvars.pas
/usr/bin/env: instantfpc -Mobjfpc -Sh: No such file or directory

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: run pascal programs as scripts

Mattias Gaertner
On Sun, 27 Mar 2011 21:16:58 +0200 (CEST)
Michael Van Canneyt <[hidden email]> wrote:

>
>
> On Sun, 27 Mar 2011, Mattias Gaertner wrote:
>
> > On Sun, 27 Mar 2011 19:05:12 +0200 (CEST)
> > Michael Van Canneyt <[hidden email]> wrote:
> >
> >>
> >>
> >> On Sun, 27 Mar 2011, Mattias Gaertner wrote:
> >>
> >>> I wrote a little tool:
> >>>
> >>> http://wiki.lazarus.freepascal.org/InstantFPC
> >>
> >> Nice job. OK to include in FPC as one of the utils ?
> >
> > Yes.
>
> Thank you. But please, fix a couple of bugs first:
>
> 1. I get a stream read error each time I execute a script the first time (i.e. it's not yet in the cache) :
> home: >./helloworld.pas
> An unhandled exception occurred at $0000000000425556 :
> EReadError : Stream read error
>    $0000000000425556
>    $00000000004256FA
>    $0000000000469E8A
>    $0000000000400A25

It works here.

 
> home: >./helloworld.pas
> Hello world 2
>
>
> 2. passing options on the commandline does not work on my Kubuntu 10.04:
>
> home: >./envvars.pas
> /usr/bin/env: instantfpc -Mobjfpc -Sh: No such file or directory

Fixed.

Mattias
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/mailman/listinfo/fpc-pascal
123