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
gcc
can compile from STDIN - we can use that thx to thedefine
hack. Also there's no point in implementing return handling if that's what happens by default.That
rm $F
line might be unnecessary, sinceld
first callsunlink
and 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...