Skip to content

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

Merged
benruijl merged 2 commits into
form-dev:masterfrom
tueda:stdin
Jul 7, 2022
Merged

benruijl merged 2 commits into
form-dev:masterfrom
tueda:stdin

Conversation

@tueda

@tueda tueda commented Feb 10, 2018 •

Copy link
Copy Markdown
Collaborator

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).

@tueda tueda changed the title Add -stdin command line option to use stdin as input [WIP] Add -stdin option Feb 10, 2018
@coveralls

coveralls commented Feb 10, 2018 •

Copy link
Copy Markdown

Coverage Status

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

@tueda

tueda commented Feb 11, 2018

Copy link
Copy Markdown
Collaborator Author

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

tueda commented Apr 23, 2018

Copy link
Copy Markdown
Collaborator Author

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.

@tueda tueda changed the title [WIP] Add -stdin option Add -stdin option: stdin as a FORM input instead of a .frm file Apr 23, 2018
@alexmyczko

Copy link
Copy Markdown
Contributor

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

tueda commented Nov 21, 2018 •

Copy link
Copy Markdown
Collaborator Author

I think - for stdin is a reasonable choice.

@tueda

tueda commented Nov 21, 2018

Copy link
Copy Markdown
Collaborator Author

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

tueda commented Dec 3, 2018

Copy link
Copy Markdown
Collaborator Author

Now the filename - means stdin.

@tueda tueda changed the title Add -stdin option: stdin as a FORM input instead of a .frm file Read the input from stdin instead of a .frm file Dec 3, 2018
@benruijl

benruijl commented Jul 6, 2022

Copy link
Copy Markdown
Collaborator

Is this change still ready to be merged?

@tueda

tueda commented Jul 7, 2022

Copy link
Copy Markdown
Collaborator Author

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

benruijl commented Jul 7, 2022

Copy link
Copy Markdown
Collaborator

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

tueda commented Jul 7, 2022

Copy link
Copy Markdown
Collaborator Author

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

benruijl commented Jul 7, 2022 •

Copy link
Copy Markdown
Collaborator

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).

tueda and others added 2 commits July 7, 2022 12:47
This patch makes FORM read the input from the standard input rather than
a file when "-" is given in the command line option.

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

Stdin mode is forbidden in ParFORM, since in many MPI implementations stdin is only
accessible from one of the MPI processes.
- Disable line echoing by default in stdin mode
@benruijl
benruijl merged commit b431780 into form-dev:master Jul 7, 2022
@jodavies

Copy link
Copy Markdown
Collaborator

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

@tueda

tueda commented Jul 17, 2022 •

Copy link
Copy Markdown
Collaborator Author

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

Copy link
Copy Markdown
Collaborator

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

@tueda

tueda commented Oct 11, 2022

Copy link
Copy Markdown
Collaborator Author

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

tueda commented Oct 11, 2022

Copy link
Copy Markdown
Collaborator Author

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

alexmyczko commented Oct 11, 2022 •

Copy link
Copy Markdown
Contributor

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

@benruijl

Copy link
Copy Markdown
Collaborator

@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

tueda commented Oct 11, 2022 •

Copy link
Copy Markdown
Collaborator Author

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.

@tueda
tueda deleted the stdin branch October 2, 2025 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants