vermaseren / form

The FORM project for symbolic manipulation of very big expressions
GNU General Public License v3.0
1.15k stars 136 forks source link

Read the input from stdin instead of a .frm file #265

Closed tueda closed 2 years ago

tueda commented 6 years ago

Work in progress. This is an experimental implementation of #264. Example:

cat <<END | form -q -stdin
Off stats;
S x;
L F = (1+x)^2;
P;
.end
END

Setup parameters (#:...) are not supported yet.

It also works with Windows (an binary available in AppVeyor: https://ci.appveyor.com/project/tueda/form/build/job/5ndaxgejblwqa4ho/artifacts).

coveralls commented 6 years ago

Coverage Status

Coverage increased (+0.009%) to 48.222% when pulling 50f39cd185777ecf57ea9a436b69dbda144309d6 on tueda:stdin into eaf85a757a9f7bb69c9bdc4bf6ec07e48f5769ac on vermaseren:master.

tueda commented 6 years ago

Now setup parameters are supported. Example:

cat <<END | form -stdin
#:workspace 1234K
#:wtimestats on
On setup;
S x;
L F = (1+x)^2;
P;
.end
END
tueda commented 6 years ago

Squashed into one commit. Ready to merge (once the others agree).

Possible improvement: currently the stdin is read one by one, for simplicity, by ReadFile(stream->handle,&c,1) in ReadFromStream.

alexmyczko commented 5 years ago

Normally for stdin as file input - is used, if - must be given as parameter to program it's -- -.

Can -stdin be replaced by - for consistency among other command line software? so it should just be form -

tueda commented 5 years ago

I think - for stdin is a reasonable choice.

tueda commented 5 years ago

By the way, the PADXXX macros are useless, memory inefficient, and moreover hard to merge two changes! (Now this branch is non-trivially conflicting with the master...)

tueda commented 5 years ago

Now the filename - means stdin.

benruijl commented 2 years ago

Is this change still ready to be merged?

tueda commented 2 years ago

It seems that there are no conflicts with the current master branch (though the current master fails to pass the tests and travis-ci.org is dead), but probably it has conflicts with 5.0 via PADXXX, I guess.

benruijl commented 2 years ago

I've changed the pull request so that the banner is printed first and the line echoing is disabled. Now form can be used pretty cleanly from the command line as if it were an interpreter! I imagine this may be useful when teaching form.

@tueda can you check if moving the header print is safe for parFORM? If so ,we can merge it :)

tueda commented 2 years ago

We can ignore ParFORM for now because MPI doesn't automatically broadcast stdin (see, for example, https://stackoverflow.com/q/49016638) so it doesn't work anyway. I have added an error message for the case where - is specified in ParFORM.

You can use "Rebase and merge" (or "Squash and merge" if you want).

benruijl commented 2 years ago

I've moved the printing of the header, even when we don't use stdin mode. I think it's safe (MsgPrint needs to be initialized, etc).

jodavies commented 2 years ago

36f5536 causes tform's header to always state "-1 workers", by the way.

tueda commented 2 years ago

Right, I've confirmed that git revert 36f5536 fixes "-1 workers".

If PrintHeader is called before DoTail, then any command-line options like -w4 are not processed yet (i.e., AM.totalnumberofthreads is 0), so tform has to print "-1 workers", I guess.

jodavies commented 1 year ago

Actually, this also breaks the -q option, the header prints regardless.

tueda commented 1 year ago

Maybe a solution would be:

  1. Revert the position of PrintHeader(1) back to just before IniVars(), and
  2. Use AM.silent = 1 (as the -q option) instead of AC.NoShowInput = 1 for - (stdin),

though I'm not sure what is the exact differences between AM.silent = 1 and AC.NoShowInput = 1.

tueda commented 1 year ago

A safer option:

  1. Revert PrintHeader(1),
  2. Keep AC.NoShowInput = 1 for - (stdin), and
  3. Skip PrintHeader(1) if AM.FromStdin is 1.

By the way, if FORM always doesn't print the version header for the stdin case, how can one know the exact version of FORM running? Currently, parsing the version header is the only way to know that.

alexmyczko commented 1 year ago

form has a -v option to only print its version and exit?

benruijl commented 1 year ago

@tueda I'll push your latest suggestion. I've tested it and it seems to work. Running form without any arguments or with -v still prints the version.

tueda commented 1 year ago

form has a -v option to only print its version and exit?

Yes, indeed this seems to be enough. The user program may run FORM twice, form -v to check the version and then form - for the main program. (For python-form, I wrote code checking the version and running the main program in one-go, without the stdin mode.)

I'll push your latest suggestion. I've tested it and it seems to work. Running form without any arguments or with -v still prints the version.

Nice! Go ahead.