Execute process/script

MarcusF · 2371

MarcusF

  • Junior Member
  • **
    • Posts: 54
    • Karma: +11/-0
on: July 15, 2022, 04:16:24 PM
Hello!

I am trying to figure out how to execute a binary or scripts from FPC, like we do with for example RunCommand()
Tried to find information but have not succeeded :( hoping someone here will have a snippet or link for how to do it.

My goal is to launch installer scripts from my program.



Amiwell

  • Legendary Member
  • *****
    • Posts: 2616
    • Karma: +35/-4
  • Peace
Reply #1 on: July 15, 2022, 05:08:05 PM
Hi

Aros uses InstallerLG, commodore installer replacement in this package there is a guide file maybe for make a script

http://aminet.net/util/sys/InstallerLG.i386-aros.lha



magorium

  • Moderator
  • Legendary Member
  • *****
    • Posts: 632
    • Karma: +62/-0
  • Convicted non contributor
Reply #2 on: July 15, 2022, 10:15:12 PM
Hello!
HI, and sorry for the delay. I'm quite busy atm...

Quote
I am trying to figure out how to execute a binary or scripts from FPC, like we do with for example RunCommand()
I assume RunCommand isn't working for you ? It does for me but perhaps not as you want it to (feel free to let me know).

devil's code:
Code: (pascal) [Select]
program running_with_the_devil;

{$MODE OBJFPC}{$H+}

uses
  sysutils, process;

function DQuote(const s: string): string; inline;
begin
  result := sysutils.AnsiQuotedStr(s, '"');
end;

function GetAppPath: string; inline;
begin
  result := ExtractFilePath(ParamStr(0));
end;

procedure RunACommand;
const
  name = 'hello_command';
var
  fn  : string = '';
  ret : integer;
begin
  writeStr(fn, GetAppPath, name);
  writeln('Run A command ', DQuote(fn));

  ret := ExecuteProcess(fn, '', []);
  writeln('A command ', name, ' was run and returned ', ret)
end;

procedure RunBCommand;
const
  name = 'hello_command';
var
  fn  : string = '';
  ret : string;
begin
  writeStr(fn, GetAppPath, name);
  writeln('Run B command ', DQuote(fn));

  if RunCommand(fn, [], ret, [])
  then writeln('B command ', name, ' was run and returned ', DQuote(ret))
  else writeln('Failed running B command');
end;

procedure RunAScript;
const
  name = 'hello_script';
var
  fn : string = '';
  ret: integer;
begin
  writestr(fn, GetAppPath, name);
  writeln('Execute A script ', DQuote(fn));

  ret := ExecuteProcess('C:execute', fn, []);
  writeln('A script ', name, ' was executed and returned ', ret)
end;

procedure RunBScript;
const
  name = 'hello_script';
var
  fn  : string = '';
  ret : string;
begin
  writeStr(fn, GetAppPath, name);
  writeln('Execute B script ', DQuote(fn));

  if RunCommand('C:execute', [fn], ret, [])
  then writeln('B script ', name, ' was executed and returned ', DQuote(ret))
  else writeln('Failed executing B script');
end;


begin
  RunACommand;
  RunAScript;
  RunBCommand;
  RunBScript;
end.

Commando's executioner:
Code: (pascal) [Select]
program hello_command;
begin
  writeln('hello from command');
  exitcode := 1234;
end.

Scriptology:
Code: [Select]
echo "Hello from script"
quit 4321

All put to rest, next to each other.

There is an issue with piping on AROS, but you can solve that by re-routing to a file, f.e. with using TProcess (see also https://wiki.freepascal.org/Executing_External_Programs )

Also here EdiSyn uses a workaround implementation.

Note that whenever you get stuck with Free Pascal RTL or packages, that you can also fall back to AROS RTL, see documentation from ALB42 and AROS autodoc reference

In this particular case of running a command you could also take a peek at AROS RTL function Systemtags

If something is missing and/or not working as expected for you then please let us know.
« Last Edit: July 15, 2022, 10:19:50 PM by magorium »



MarcusF

  • Junior Member
  • **
    • Posts: 54
    • Karma: +11/-0
Reply #3 on: July 15, 2022, 11:27:28 PM
Thanks a bunch!

Ok so it does work, but it's not working the way I was expecting.

Your sample works and I get the returned data. But when I try this, it returns immediately with a blank string

if RunCommand('AROS:Utilities/Installer', ['APPNAME','test','SCRIPT','AROS:Marcus/Downloads/Installer-43_3/Installer-43_3/SampleScripts/install-test'], ret, [poWaitOnExit])

If I run the command in a shell it works fine and pops up the installer window, but I don't get anything when trying to launch it myself.



magorium

  • Moderator
  • Legendary Member
  • *****
    • Posts: 632
    • Karma: +62/-0
  • Convicted non contributor
Reply #4 on: July 16, 2022, 08:27:11 PM
That is peculiar. As in: it works for me (tm)

Could you share a simple example that fails for you ? It's ok if you wish to do so in a pm.

There are situations where both ExecuteProcess and RunCommand fail, but that has to do with the parameter separation (or actually wrongfully combining), but you do not do so in the example you showed.

Target is aros-i386 ? and which AROS distro/version do you test this on/for (Icaros Desktop/TinyAros/AROSone/Nightly) (please don't forget version/date) ?

First thing that comes to mind is that it is perhaps something stack related, but I tested with running on fumes... so I guess it fails somewhere else. BTW note that runcommand can return immediately depending on what command exactly is executed. Also the return boolean can sometimes be a bit misleading.
« Last Edit: July 16, 2022, 08:30:32 PM by magorium »



magorium

  • Moderator
  • Legendary Member
  • *****
    • Posts: 632
    • Karma: +62/-0
  • Convicted non contributor
Reply #5 on: July 18, 2022, 09:46:34 AM
It should not matter but do you have the same issue when you change the device name from AROS: to SYS: ?


Quote
BTW note that runcommand can return immediately depending on what command exactly is executed. Also the return boolean can sometimes be a bit misleading.
A bit more clarification as that sounded a bit vague. There are AROS/Amiga (shell) commands that return immediately, no matter what. The installer is not one of them.

I did notice though that installer returns a zero value (when aborted) but runcommand still reports that as failing (return value false). I've noted it down as needs further investigation.


MarcusF

  • Junior Member
  • **
    • Posts: 54
    • Karma: +11/-0
Reply #6 on: July 18, 2022, 01:51:19 PM
Sorry for the delayed response magorium.

I'm compiling on Windows 11 Pro, FPC 3.2.2, Lazarus 2.2.0
Target is AROS-i386
AROS is AROSone running in a virtualbox VM.
Transfer program with FTP from inside VM (Works great)

If I run the command in a regular shell like the screenshot, it works just fine and shows the installer window as it should.

But when I run it through runcommand with this exact line it doesn't work
Code: [Select]
if RunCommand('AROS:Utilities/Installer', ['APPNAME','test','SCRIPT','AROS:Marcus/Downloads/Installer-43_3/Installer-43_3/SampleScripts/install-test'], ret, [poWaitOnExit])



AMIGASYSTEM

  • Global Moderator
  • Legendary Member
  • *****
    • Posts: 3740
    • Karma: +69/-2
  • AROS One
    • AROS One
Reply #7 on: July 18, 2022, 03:15:35 PM
Thank you MarcusF for using my AROS One x86 for your tests, if you encounter any problem on AROS One that I can fix please report it.

In scripts I cannot help you because I am not a developer, but I can help you on AROS/Amiga OS where I am quite experienced.


MarcusF

  • Junior Member
  • **
    • Posts: 54
    • Karma: +11/-0
Reply #8 on: July 18, 2022, 03:22:05 PM
It's working really well!

I do have some questions, but I'll do that in a separate thread.

Ignore that it says "script" and such, the code is this:



magorium

  • Moderator
  • Legendary Member
  • *****
    • Posts: 632
    • Karma: +62/-0
  • Convicted non contributor
Reply #9 on: July 18, 2022, 04:29:46 PM
Ignore that it says "script" and such, the code is this:
Was about to ask about that and was preparing a rewind/reset for this issue  ;)

Ok, that is indeed strange because funny or not, i used the exact same installscript you downloaded for testing purposes. The only thing i really do different is using SYS: instead of AROS: and i use variables to 'contain' the locations for the installer executable and the actual installer script.

PS: i think you already noted it but there is a difference between starting your compiled executable from the shell or when double clicked using the workbench icon. However for both situations i get the installer to run correctly. Could you try raising the stack just before running your executable from the shell ? (command = STACK numbersize). Stack without providing a number should (if i remember correctly) return the current used stack-size (thus increase the number where you can go really wild depending on how much memory you gave your vm).


AMIGASYSTEM

  • Global Moderator
  • Legendary Member
  • *****
    • Posts: 3740
    • Karma: +69/-2
  • AROS One
    • AROS One
Reply #10 on: July 18, 2022, 04:37:48 PM
magorium, On AROS there is StackMon that shows the Stack used, on AROS you would need a program like "StackAttack" which automatically adds the required stack


MarcusF

  • Junior Member
  • **
    • Posts: 54
    • Karma: +11/-0
Reply #11 on: July 18, 2022, 05:38:53 PM
Ignore that it says "script" and such, the code is this:
Was about to ask about that and was preparing a rewind/reset for this issue  ;)

Ok, that is indeed strange because funny or not, i used the exact same installscript you downloaded for testing purposes. The only thing i really do different is using SYS: instead of AROS: and i use variables to 'contain' the locations for the installer executable and the actual installer script.

PS: i think you already noted it but there is a difference between starting your compiled executable from the shell or when double clicked using the workbench icon. However for both situations i get the installer to run correctly. Could you try raising the stack just before running your executable from the shell ? (command = STACK numbersize). Stack without providing a number should (if i remember correctly) return the current used stack-size (thus increase the number where you can go really wild depending on how much memory you gave your vm).

I'll try changing the stack soon as I get back to my computer. Can you paste your exact runcommand code?



magorium

  • Moderator
  • Legendary Member
  • *****
    • Posts: 632
    • Karma: +62/-0
  • Convicted non contributor
Reply #12 on: July 18, 2022, 06:20:00 PM
I'll try changing the stack soon as I get back to my computer. Can you paste your exact runcommand code?
Zip with example source-code, compilation log, compiled i386-aros executable and screenshots running the executable from shell and workbench can be found at: https://ufile.io/ncflsu4n


magorium

  • Moderator
  • Legendary Member
  • *****
    • Posts: 632
    • Karma: +62/-0
  • Convicted non contributor
Reply #13 on: July 18, 2022, 06:43:30 PM
magorium, On AROS there is StackMon that shows the Stack used, on AROS you would need a program like "StackAttack" which automatically adds the required stack
Thank you AMIGASYSTEM.

I am aware there are such programs that can help simplifying stack usage.

At the moment I am/we are trying to figure out what is wrong programming technically.

I have made the advise regarding stack because it could be that tinyaros default settings have it configured wrongly/weirdly, which (in the end) could be an issue. I want to prevent that it is actually the last thing I'm trying to advise because that causes a lot of headaches in between :-)

I am also aware that the programs you mention are especially made for that purpose (to automatically adjust the stack according to the needs of the running program) but I prefer to not rely on such programs in my own code because they can mask a real issue that is part of the programmed code (and thus would hide this from the developer/user).


Amiwell

  • Legendary Member
  • *****
    • Posts: 2616
    • Karma: +35/-4
  • Peace
Reply #14 on: July 18, 2022, 08:00:39 PM
Are there any problems with Tinyaros? I create the system of system on Windows with Ultraiso I don't touch any configuration file :-\