Name is misleading, but still. The goal of this is to be able to compile and run a C file in one line, with the C source file containing all instructions needed to compile itself when interpreted as a shell script.
So these cases need to be supported:
curl URL | bash
wget URL; source SCRIPT ARGS...
wget URL; chmod +x SCRIPT; ./SCRIPT ARGS...
For development purposes, this also needs to be a valid C file, so this must work too:
gcc source.c -o program
Always creates a binary file with specified name. Can overwrite itself when executed as file.
Relies on quoted heredocs and uses gcc which compiles code from stdin. Of course, the code may not contain the delimiter.
Tested on bash and ash shells and gcc 7.2.1. I'm not sure if traliling # characters are allowed or it's just gcc. We need them though.
Ok, I'm an idiot. We can simply remove ourselves - this doesn't remove the contents on disk - bash can continue. Good. Second thing is that
gcccan compile from STDIN - we can use that thx to thedefinehack. Also there's no point in implementing return handling if that's what happens by default.That
rm $Fline might be unnecessary, sinceldfirst callsunlinkand then creates a new file, but this is not guaranteed since it depends on a specific linker implementation.And finally, I don't like the new header, so here goes
selfcompile-alt.c.Jeez, that's enough...