Skip to content

Conversation

@quickfur
Copy link
Member

Turns out, only a trivial change is needed to make this work, by hooking into the code implemented in #6880 for reading source code from stdin.

With this change, dmd acquires the functionality:

$ echo 'import std.stdio; void main() { writeln("Hello, World!"); }' | dmd -run -
Hello, World!
$

which compiles and runs standard input without the user needing to specify any intermediate filenames.

The above is a trivial example, of course. A more pertinent use case would be executing the source code in an editor's buffer via stdin, and having the output captured by the editor as stdout, without needing to specify intermediate temporary files. Another use case would be code generation tools that can instantly produce output without needing to manage temporary files.

Turns out, a trivial change is all that's needed.
@dlang-bot
Copy link
Contributor

Thanks for your pull request, @quickfur!

Bugzilla references

Auto-close Bugzilla Description
18076 dmd -run should work with - (stdin) too

Copy link
Contributor

@wilzbach wilzbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow. That's a simple fix! A few comments regarding the test case.

output_file=${dir}/test18076.sh.out

echo 'import std.stdio; void main() { writeln("Success"); }' | \
$DMD -m${MODEL} -run - > ${output_file} || exit 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't you have to check that it actually prints "Success" here?

[[ $(echo "Success") == "Success" ]] || exit 1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, fixed.

@@ -0,0 +1,9 @@
#!/usr/bin/env bash

src=runnable${SEP}extra-files
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleted, thanks!


echo 'import std.stdio; void main() { writeln("Success"); }' | \
$DMD -m${MODEL} -run - > ${output_file} || exit 1

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the empty, final line?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@wilzbach wilzbach added the Review:Trivial typos, formatting, comments label Dec 14, 2017
@quickfur quickfur force-pushed the issue18076 branch 2 times, most recently from 8e98977 to a920164 Compare December 14, 2017 06:04

echo 'import std.stdio; void main() { writeln("Success"); }' | \
$DMD -m${MODEL} -run - > ${output_file} || exit 1
grep -q '^Success$' ${output_file} || exit 1
Copy link
Contributor

@wilzbach wilzbach Dec 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW the || exit is superfluous here.
A bash script will yield the last return value as exit code if not explicitly exited
Convince yourself:

sh -c "grep -q 'foo' afile.d" ; echo $? 

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. I'm wary of relying on these implicit behaviours, though. Stating || exit 1 makes it more obvious what's happening. Not that it makes a difference, I just prefer to have things up-front than implicit. Implicit things tend to be fragile and unintentionally break later.

Copy link
Contributor

@wilzbach wilzbach Dec 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's why one usually does a set -e at the beginning 😉
https://unix.stackexchange.com/questions/15998/what-does-the-e-do-in-a-bash-shebang

Copy link
Member

@PetarKirov PetarKirov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like the new functionality and the changes LGTM too.

break;
}
files.push(arguments[i + 1]);
if (strcmp(arguments[i + 1], "-") == 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too simplistic, it won't work if there's a command after -run, e.g. dmd -run -m64 -

Copy link
Member Author

@quickfur quickfur Dec 15, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, -run takes an argument, so your example is invalid. (I'm not sure why whoever originally implemented -run did it this way, but that's how it currently works, and this PR is just following in its steps.)(And yes, the first time I realized -run took an argument I was surprised too. :-P)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh sorry. I just looked over this again from my phone and imagined that doing dmd -run -version=Foo myfile.d is legimate, so yeah that seems like bad design too me too, but that's an opportunity for another PR ;-)

@wilzbach wilzbach dismissed their stale review December 15, 2017 01:57

Oh I didn't realize that -run always has to take an argument

@wilzbach wilzbach merged commit 0b8d1fc into dlang:master Dec 15, 2017
@quickfur quickfur deleted the issue18076 branch December 15, 2017 16:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants