Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rdmd doesn't handle piping or unix-isms #457

Closed
crazymonkyyy opened this issue Apr 29, 2023 · 3 comments
Closed

rdmd doesn't handle piping or unix-isms #457

crazymonkyyy opened this issue Apr 29, 2023 · 3 comments
Labels
Prio.3.Normal A bug that is neither a blocker nor a regression Type.Enhancement An improvement to an existing functionality

Comments

@crazymonkyyy
Copy link

I want this or something like this to render a video (cause ffmeg commands are nightmares and I need to coordinate the size between the program and the ffmeg command)

#!rdmd gradienttest.d | ffmpeg -f rawvideo -pixel_format rgb24 -video_size 255x255 -i pipe: -preset ultrafast out.mp4
import std;
void main(){
	foreach(i;0..255){
	foreach(y;0..255){
	foreach(x;0..255){
		stdout.rawWrite([cast(ubyte)x,cast(ubyte)y,cast(ubyte)i]);
	}}}
}

Given that rn you handle a magic "--shebang"

tools/rdmd.d

Line 72 in 02365fa

if (args.length > 1 && args[1].startsWith("--shebang ", "--shebang="))

Id think a magic "--dumbpipe" argument seems fine

included code for what Id expect

import std;
void main(string[] args){
	assert(args[1][0..10]=="--dumbpipe");
	writeln("got args: ",args);
	string getarg_(int i){
		if(i==0){
			return args[2][2..$-2];//this seems like a hack even to me, but im unsure what edge cases to try to fix
		}
		if(i+2>=args.length){
			return "";
		}
		return args[i+2];
	}
	string getarg(int i){//debug info, remove if hack above is fixed?
		auto s=getarg_(i);
		writeln("parsing $",i," as ",s);
		return s;
	}
	string command;
	command~="dmd -i -run ";
	string s=args[1][11..$];
	while(s.length!=0){
		if(s[0]=='$' && s.length>1 && s[1].isDigit){
			command~=getarg([s[1]].to!int);
			s=s[2..$];
		} else {
			command~=s[0];
			s=s[1..$];
		}
	}
	writeln("running: ",command);
}

#!myrdmd --dumbpipe $0 | ffmpeg -f rawvideo -pixel_format rgb24 -video_size $1x$2 -i pipe: -preset ultrafast $0.mp4

./testmyrdmd.d 255 255
got args: ["myrdmd", "--dumbpipe $0 | ffmpeg -f rawvideo -pixel_format rgb24 -video_size $1x$2 -i pipe: -preset ultrafast $0.mp4", "./testmyrdmd.d", "255", "255"]
parsing $0 as testmyrdmd
parsing $1 as 255
parsing $2 as 255
parsing $0 as testmyrdmd
running: dmd -i -run testmyrdmd | ffmpeg -f rawvideo -pixel_format rgb24 -video_size 255x255 -i pipe: -preset ultrafast testmyrdmd.mp4
@crazymonkyyy crazymonkyyy added Prio.3.Normal A bug that is neither a blocker nor a regression Type.Enhancement An improvement to an existing functionality labels Apr 29, 2023
@CyberShadow
Copy link
Member

CyberShadow commented Apr 30, 2023

If you want to use shell syntax such as | in the shebang, you have to invoke a shell in your shebang. rdmd (nor any other program) can't be expected to implement parsing and execution of shell syntax.

Try something like this:

#!/usr/bin/env -S sh -c 'rdmd "$0" | ffmpeg -f rawvideo -pixel_format rgb24 -video_size 255x255 -i pipe: -preset ultrafast out.mp4'
...

But since you're modifying the D file to specify what to do with the D program's output anyway, you might as well use std.process.pipeProcess.

@CyberShadow CyberShadow closed this as not planned Won't fix, can't repro, duplicate, stale Apr 30, 2023
@crazymonkyyy
Copy link
Author

rdmd (nor any other program) can't be expected

I do expect this and apparently, perl at least does something
http://reasonableapproximation.net/2010/07/27/silly-things-to-do-with-shebang-lines.html
and its 30 lines of trivial code

@CyberShadow
Copy link
Member

No, that post describes how to write a script which implements that functionality by invoking the shell. You can do the same thing. Note that there is absolutely nothing special in Perl itself that helps achieve this (other than its affinity for succinct scripts).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Prio.3.Normal A bug that is neither a blocker nor a regression Type.Enhancement An improvement to an existing functionality
Projects
None yet
Development

No branches or pull requests

2 participants