TeraDesk ARGV/Pexec

C and PASCAL (or any other high-level languages) in here please

Moderators: simonsunnyboy, Mug UK, Zorro 2, Moderator Team

JeanMars
Captain Atari
Captain Atari
Posts: 317
Joined: Fri Apr 09, 2010 5:15 pm
Location: France
Contact:

TeraDesk ARGV/Pexec

Post by JeanMars »

Hi,

A VISION's user is facing some issue that puzzles me regarding parameters passed to Pexec:
To look for an update, VISION starts wget to fetch vision.ver, the command line is typically:
u:\usr\bin\wget http://vision.atari.org/vupdate/staging/vision.ver -O h:\pure_c\projects\vision\UPDATE\vision.ver
So VISION calls Pexec like this:
Pexec (0, "u:\usr\bin\wget ", [L]"http://vision.atari.org/vupdate/staging/vision.ver -O h:\pure_c\projects\vision\UPDATE\vision.ver", NULL);
(Actually the command line is precedded by its length as it is Pascal convention, this is what the [L] stands for).

And that works good...until this case:
Using TeraDesk AND a file extension associated with VISION (i.e. JPG), so using ARGV protocol, if you click on a JPG file, VISION starts update process (from VISION's log file):
AUCheckUpdate: current_version:4.8b0, path_update:E:\vision\UPDATE, updater:wget, URL=http://vision.atari.org/vupdate/staging/
AUCheckUpdate: request update with wget http://vision.atari.org/vupdate/staging/vision.ver -O E:\vision\UPDATE\vision.ver
AUCheckUpdate: command line u:\usr\bin\wget http://vision.atari.org/vupdate/staging/vision.ver -O E:\vision\UPDATE\vision.ver returned error 4
Can't update: updater wget reported error, check Internet connection

However, what wget receives is different (from console):
ftp://e/%5Cvision%5Cromcover.jpg
=> '\\vision\\romcover.jpg.1'
Resolving e... failed: Unknown host.
E:\vision\vision.prg: unable to resolve host address 'e'

Then file is loaded as usual.
Note that only happens when a file extension is associated with VISION, a simple Drag'n'Drop on VISION's icon works fine.

Just like if VISION passes the double-clicked JPG file instead of URL to wget, but according to logs, it passes the proper URL and even when returned from Pexec, the passed buffer is the same, so it has not been corrupted during the call.

Any idea why the passed command line would not be properly received by wget through Pexec?

Thanks,
Jean
ThorstenOtto
Atari God
Atari God
Posts: 1482
Joined: Sun Aug 03, 2014 5:54 pm

Re: TeraDesk ARGV/Pexec

Post by ThorstenOtto »

What startup code do you use in VISION? Does it handle ARGV itself? If not, then it is likely that is also does not remove the ARGV environment variable, and then what happens is that the ARGV that is passed from teradesk to VISION is passed along to wget.
JeanMars
Captain Atari
Captain Atari
Posts: 317
Joined: Fri Apr 09, 2010 5:15 pm
Location: France
Contact:

Re: TeraDesk ARGV/Pexec

Post by JeanMars »

Hi Thorsten,

Just PureC startup code.
ARGV is not handled in VISION (just the classic 'C' argc, argv), but how can this affect the Pexec call where I state the parameters to pass regardiless of what VISION received as ARGV?
User avatar
mfro
Atari Super Hero
Atari Super Hero
Posts: 961
Joined: Thu Aug 02, 2012 10:33 am
Location: SW Germany

Re: TeraDesk ARGV/Pexec

Post by mfro »

ThorstenOtto wrote: Tue May 04, 2021 4:01 pm What startup code do you use in VISION? Does it handle ARGV itself? If not, then it is likely that is also does not remove the ARGV environment variable, and then what happens is that the ARGV that is passed from teradesk to VISION is passed along to wget.
That shouldn't happen even if ARGV is still set (if it does, one of the programs involved has broken startup code).

Pexec ARGV processing should only be done when the length byte equals 127 (which would be plain wrong for a program that is not supposed to support ARGV).

I would bet for an Fsfirst() call with a missing Fsetdta() call before somewhere.

If you miss to set the DTA address, it will still be set to the basepage. Calling Fsfirst() then will overwrite the command line.
JeanMars
Captain Atari
Captain Atari
Posts: 317
Joined: Fri Apr 09, 2010 5:15 pm
Location: France
Contact:

Re: TeraDesk ARGV/Pexec

Post by JeanMars »

Hi Mikro,

just checked, there is no such Fsfirst without a previous Fsetdta in VISION.
Even if I don't understand how a call to Pexec would in this case replace the parameter sent.
There is no control of parameter length passed to as command line to Pexec, but according to the logs it is 81; far from 127.

Or maybe wget does not call Fsetdta ?

Sorry, I'm still confused :-(
mikro
Hardware Guru
Hardware Guru
Posts: 2365
Joined: Sat Sep 10, 2005 11:11 am
Location: Kosice, Slovakia
Contact:

Re: TeraDesk ARGV/Pexec

Post by mikro »

To me it looks like the problem with ARGV env variable -- you're passing NULL to Pexec() therefore you're passing the original ARGV= from Vision startup code and therefore it takes precedence.

As Thorsten pointed out, you are most likely using the non-ARGV PureC startup code. You should have PCVSTART.O in your project file instead of PCSTART.O.

An easy way to verify this hypothesis is to pass -1 instead of NULL to Pexec().
JeanMars
Captain Atari
Captain Atari
Posts: 317
Joined: Fri Apr 09, 2010 5:15 pm
Location: France
Contact:

Re: TeraDesk ARGV/Pexec

Post by JeanMars »

Tried that.
Crash under Aranym/Mint/XaaAES/Thing
To make sure I understand: if VISION is started with ARGV protocol, the program I ran from VISION just takes this ARGV regardless of the provided command line?
mikro
Hardware Guru
Hardware Guru
Posts: 2365
Joined: Sat Sep 10, 2005 11:11 am
Location: Kosice, Slovakia
Contact:

Re: TeraDesk ARGV/Pexec

Post by mikro »

Tried what? That -1 instead of NULL?

I'm not sure I understand your statement. It's like this:

You start VISION.PRG
If VISION.PRG is linked against PCSTART.O, it happily ignores the ARGV= string in the environment.
You create a child process via Pexec() and pass it the whole parent's environment (in our case including the "ARGV=" string and following data)
The child process is confused.

If you have ARGV-aware startup:

You start VISION.PRG
VISION.PRG is linked against PCVSTART.O, it parses the "ARGV=" (and following) strings and erases it
You create a child process via Pexec() and everything's fine because there is no "ARGV=" anymore.

But it's interesting that this is really happening - because not only "ARGV=" should be the decision factor but the length of command line as well (which is clearly not 127).
JeanMars
Captain Atari
Captain Atari
Posts: 317
Joined: Fri Apr 09, 2010 5:15 pm
Location: France
Contact:

Re: TeraDesk ARGV/Pexec

Post by JeanMars »

Hi Mikro,
Tried what? That -1 instead of NULL?
Yes but I think I made the change a bit too quick yesterday and passed (void*)-1 instead of (void*)-1L which would explain the crash. Need to retest this evening.

In case of VISION linked against PCSTART.O and desktop passing ARGV, when VISION calls:
Pexec( app, cmd, NULL) --> app will be started with ARGV as command line, so cmd will be ignored
Pexec( app, cmd, (void*)-1L) --> app will be started with cmd as command line as

Am I correct?
mikro
Hardware Guru
Hardware Guru
Posts: 2365
Joined: Sat Sep 10, 2005 11:11 am
Location: Kosice, Slovakia
Contact:

Re: TeraDesk ARGV/Pexec

Post by mikro »

Yes, that's how I imagine the test will go. But of course I may be totally wrong. ;)

Btw when this is cleared up, I would suggest to use that ARGV-aware startup module anyway -- now you cut yourself from the ability to process long names in VISION, when passed via the command line.
ThorstenOtto
Atari God
Atari God
Posts: 1482
Joined: Sun Aug 03, 2014 5:54 pm

Re: TeraDesk ARGV/Pexec

Post by ThorstenOtto »

JeanMars wrote: Wed May 05, 2021 7:07 am Yes but I think I made the change a bit too quick yesterday and passed (void*)-1
That should not make any difference as long as you use the prototype for Pexec.
In case of VISION linked against PCSTART.O and desktop passing ARGV, when VISION calls:
Pexec( app, cmd, NULL) --> app will be started with ARGV as command line, so cmd will be ignored
NULL just means pass along the environment that vision got when it was started. That may or may not mean ARGV (which is part of the environment) is also passed along.
Pexec( app, cmd, (void*)-1L) --> app will be started with cmd as command line as
It just will start the new program with no environment at all. That also means the p_env member of the basepage is null, maybe the startup code of the app you are running does not like that, hence the crash.

In case of wget, if it was compiled using mintlib (most likely), then mintlib is to blame:

https://github.com/freemint/mintlib/blo ... #L230-L231

It will try to parse the environment even if it is null.
JeanMars
Captain Atari
Captain Atari
Posts: 317
Joined: Fri Apr 09, 2010 5:15 pm
Location: France
Contact:

Re: TeraDesk ARGV/Pexec

Post by JeanMars »

Hi Thorsten,

Thanks for your remarks, I still need more testing on this :-)
But I'm missing something here; what's the point of providing a command line to Pexec (param 2, which I would expect would be passed to the ran program as argv), if tis ran program is getting it from the caller environment (param3=NULL) or no env at all (param3=-1) ?

Basically, how to start a program with providing a unique command line and not the same one as the program from which it is called (which does not make much sense; why would I like to run some other program passing the very same parameters as mine?) ?

@Mikro: Good point about linking against PCVSTART.O; at least this will enable long filenames and filenames with space characters. Will do.

Cheers,
Jean
ThorstenOtto
Atari God
Atari God
Posts: 1482
Joined: Sun Aug 03, 2014 5:54 pm

Re: TeraDesk ARGV/Pexec

Post by ThorstenOtto »

JeanMars wrote: Wed May 05, 2021 11:17 am what's the point of providing a command line to Pexec (param 2, which I would expect would be passed to the ran program as argv), if tis ran program is getting it from the caller environment (param3=NULL) or no env at all (param3=-1) ?
It does not always get its commandline from the environment, only if it is aware of the newer ARGV mechanism. Passing the cmdline via the 2nd Parameter is the original way, but that commandline is written to the basepage and thus limited to 126 chars. ARGV also allows to pass a) the path of the program itself in argv[0], b) filenames with spaces, and c) empty arguments.
Basically, how to start a program with providing a unique command line and not the same one as the program from which it is called
Easiest way to avoid that is to use startup code that is ARGV aware, and thus will remove it from your own environment. 2nd thing that you can try is to use ARGV yourself when invoking the program (that requires a bit more work though, you have to construct a new environment with enough space of the current environment, plus any parameters for the program). Drawback: you typically don't know if the called program understands ARGV or not, so you either have to just assume that (which should be rather safe in the case of wget), or use a fallback if it doesn't.
JeanMars
Captain Atari
Captain Atari
Posts: 317
Joined: Fri Apr 09, 2010 5:15 pm
Location: France
Contact:

Re: TeraDesk ARGV/Pexec

Post by JeanMars »

Easiest way to avoid that is to use startup code that is ARGV aware, and thus will remove it from your own environment.
I generated a VISION linked against PCVSTART.O:
wget started with Pexec( app, cmd, NULL ) --> same result :-( (wget tries to download local JPG file passed as ARGV to VISION)

Note that this is only happening with TeraDesk, it works fine with Thing! (wgets gets the command line not the ARGV parameters sent to VISION).

Cheers,
Jean
ThorstenOtto
Atari God
Atari God
Posts: 1482
Joined: Sun Aug 03, 2014 5:54 pm

Re: TeraDesk ARGV/Pexec

Post by ThorstenOtto »

Maybe thing does not use ARGV when the commandline fits into 126 chars? Or there is some other setting that prevents it from being used. In GEMINI, you can specify different settings for individual programs.


Strange that it still does not work. Maybe you can try https://github.com/th-otto/MagicMac/blo ... /cstartv.s instead of PCVSTART, it should be compatible to Pure-C's startup code, and also handles ARGV.

BTW, the trick to pass (void *)-1 as env does not seem to be supported by TOS or EmuTOS. Until @mikro mentioned it, i did not even know that it is supposed to work. Maybe that is a MagiC-only extension?
JeanMars
Captain Atari
Captain Atari
Posts: 317
Joined: Fri Apr 09, 2010 5:15 pm
Location: France
Contact:

Re: TeraDesk ARGV/Pexec

Post by JeanMars »

BTW, the trick to pass (void *)-1 as env does not seem to be supported by TOS or EmuTOS. Until @mikro mentioned it, i did not even know that it is supposed to work. Maybe that is a MagiC-only extension?
Online toshyp doc does not mention anything specific to Magic for mode 0 and env=-1:
https://freemint.github.io/tos.hyp/en/g ... html#Pexec
It does not always get its commandline from the environment, only if it is aware of the newer ARGV mechanism. Passing the cmdline via the 2nd Parameter is the original way, but that commandline is written to the basepage and thus limited to 126 chars. ARGV also allows to pass a) the path of the program itself in argv[0], b) filenames with spaces, and c) empty arguments.
Yeah, that's exactly what I want, the good old argc/argv with limitations is fine in my case!

I could be wrong of course, but I would expect Pexec implementation to pass cmd as classic argv if not NULL and less than 126 bytes, or use ARGV from env if not.

Probably, TeraDesk has another opinion on this :-)
But right now, I can't find a way to make this work on TeraDesk. Maybe I can try to put everything in cmd (i.e. cmd+command line).
I'll make some tests.
ThorstenOtto
Atari God
Atari God
Posts: 1482
Joined: Sun Aug 03, 2014 5:54 pm

Re: TeraDesk ARGV/Pexec

Post by ThorstenOtto »

I would expect Pexec implementation to pass cmd as classic argv if not NULL and less than 126 bytes, or use ARGV
There are other reasons to use ARGV, as noted above. And normally, i would expect newly written programs like Vision to support it. In fact, i think there are even very few old programs that don't support it, and where it would make a difference.
JeanMars wrote: Wed May 05, 2021 2:57 pm But right now, I can't find a way to make this work on TeraDesk.
Thats still strange. When you use startup code that scans ARGV, then it should work, whether Teradesk passes the arguments via ARGV or not.

If all else fails, you could try to kill the ARGV environment in your program yourself. Pure-C's getenv() function directly returns a pointer to the process environment (unlike mintlib, which does a copy on startup). So something like this

Code: Select all

char *p = getenv("ARGV");
if (p) p[-5] = 0;
JeanMars
Captain Atari
Captain Atari
Posts: 317
Joined: Fri Apr 09, 2010 5:15 pm
Location: France
Contact:

Re: TeraDesk ARGV/Pexec

Post by JeanMars »

I have linked against PCVSTART.O, now VISION accepts long filenames/space in name with Drag'n'Drop (which was not the case).
I've checked startup code creates argv[] entries to make this work, so VISION just still loop on argv[n].
However, when registering VISION as an to open*.jpg in Thing!, a filename with a space is split in 2 different entries in argv array... so load fails.
char *p = getenv("ARGV");
if (p) p[-5] = 0;
Not sure to understand why p[-5] ? What is supposed to be there?

Thanks,
Jean
mikro
Hardware Guru
Hardware Guru
Posts: 2365
Joined: Sat Sep 10, 2005 11:11 am
Location: Kosice, Slovakia
Contact:

Re: TeraDesk ARGV/Pexec

Post by mikro »

Please note that Thing! is not guaranteed to support D&D, AV protocol and ARGV spec. The only desktop I know of to do so is Teradesk, and that's because when I was developing mxPlay, I tested its D&D, AV and ARGV features to the limit and reported all my findings to Djordje. :)

Btw that Thorsten's workaround is really a dirty hack. It's undefined what should happen in such case, so use it as a test but surely not as a definite solution.
JeanMars
Captain Atari
Captain Atari
Posts: 317
Joined: Fri Apr 09, 2010 5:15 pm
Location: France
Contact:

Re: TeraDesk ARGV/Pexec

Post by JeanMars »

It works with TeraDesk if "Use ARGV" is not checked. Maybe then TeraDesk assumes, if this one is checked, that any subsequent call to Pexec will also use ARGV protocol (which is not the case for VISION) and creates an env passed through Pexec.
Not in my scope at this time...
User avatar
christos
Fuji Shaped Bastard
Fuji Shaped Bastard
Posts: 2504
Joined: Tue Apr 13, 2004 8:24 pm
Location: Greece

Re: TeraDesk ARGV/Pexec

Post by christos »

In qextract I use shel_read to achieve the same thing and i remember that selecting ARGV on teradesk did cause issues. So I guess you are on to something.

The ARGV protocol is way too complicated I think.
JeanMars
Captain Atari
Captain Atari
Posts: 317
Joined: Fri Apr 09, 2010 5:15 pm
Location: France
Contact:

Re: TeraDesk ARGV/Pexec

Post by JeanMars »

I would tend to agree with you christos :-)
mikro
Hardware Guru
Hardware Guru
Posts: 2365
Joined: Sat Sep 10, 2005 11:11 am
Location: Kosice, Slovakia
Contact:

Re: TeraDesk ARGV/Pexec

Post by mikro »

It's actually not that hard to debug whether that "ARGV=" string is cut off or not -- from a quick look into PCVSTART.S it seems it should be so fire up Pure Debugger and take a look what's happening there.

It's pretty obvious that this is a PureC bug, its libraries to be precise. gcc + mintlib work fine.
ThorstenOtto
Atari God
Atari God
Posts: 1482
Joined: Sun Aug 03, 2014 5:54 pm

Re: TeraDesk ARGV/Pexec

Post by ThorstenOtto »

Yes, i also looked through pcvstart, and at a first glance, it seems to look ok. But you never know. Did you try my alternative version?
ThorstenOtto
Atari God
Atari God
Posts: 1482
Joined: Sun Aug 03, 2014 5:54 pm

Re: TeraDesk ARGV/Pexec

Post by ThorstenOtto »

mikro wrote: Thu May 06, 2021 7:11 am It's pretty obvious that this is a PureC bug, its libraries to be precise. gcc + mintlib work fine.
Maybe not. Looking at https://github.com/freemint/teradesk/bl ... prg.c#L437 it seems to me that there may be situations where the ARGV string is added to the environment, even if the program is started without using the ARGV protocol, and commandline[0] is set to a value != 127. In that case, the Pure-C startup code (correctly) skips the ARGV detection altogether, and does not remove ARGV from the environment. The startup code of mintlib behaves slightly different here, and looks for ARGV and clears it, even if commandline[0] != 127.

PS.: teradesk also has a workaround similar to what i suggested: https://github.com/freemint/teradesk/bl ... onm.c#L125

it may be a bit ugly, but seems to help.
Post Reply

Return to “C / PASCAL etc.”