C – Compile and execute C “scripts” in one go
github.com
github.com
//usr/bin/make -s "${0%.c}" && ./"${0%.c}" "$@"; s=$?; rm ./"${0%.c}"; exit $s
Actually, you could extend this to any file type that Make has built-in rules for, and which uses // as a comment delimiter: //usr/bin/make -s "${0%.*}" && ./"${0%.*}" "$@"; s=$?; rm ./"${0%.*}"; exit $sHow is this a hack?
I'm pretty sure I've seen a case in which a particular script worked when called directly from Bash, but not when invoked by other things like xargs or nohup, because Bash actually will execute scripts under itself, while execvp will execute it under /bin/sh which is Dash on Debian/Ubuntu systems.
In fact, it was even better than that; they had even used #!/bin/bash, but had whitespace before it, causing the shebang to just be treated as a comment and not the as an interpreter:
https://stackoverflow.com/questions/24944405/why-is-the-foll...
Looks like it's fairly standard for shells to have this behavior, and execvp() is intended to have the behavior of executing like a shell would, so searching the path to find the executable and then passing the result to the shell interpreter if the underlying execve() returns noexec. May be a feature to add to your wacky homebrew shell.
//bin/true; make -s "${0%.c}" && ./"${0%.c}" "$@"; s=$?; rm ./"${0%.c}"; exit $s
Which might matter for some platforms, since 'make' isn't usually something that gets the shebang treatment.I considered a few no-ops, like /bin/cat (but that would then consume stdin) or /bin/echo -n, but there may be cases in which these can't be relied on either, so I figured that just keeping it simple but relying on the location of make was the better option.
There's more than one way to do this. It's just a silly hack; I decided to keep it simpler as the other alternatives didn't seem strictly better.
http://www.in-ulm.de/~mascheck/various/shebang/#env
In fact, POSIX doesn't even standardize the shebang at all; it is simply mentioned as a possible extension.
help_msg() {
>&$1 echo "Usage: $0 [file.c...
>&$1 echo "Execute C progams from the command line."
...
}
for that it puts the redirection at the beginning of the line, which is unusual and I didn't even realize until now that it's valid. ( example: >&55 redirects stdout to filescriptor number 55, and here >&$1 redirects stdout of echo to the filedescriptor number given as the first argument to the function) # help if we have no arguments and no stdin
if [ $# -eq 0 ] && [ -t 0 ]; then
help_msg 2 # <--- NOTE 2 = stderr
exit 1
fi
# help if we get the flags
if [ "$1" == "--help" ] || [ "$1" == "-h" ]; then
help_msg 1 - <--- NOTE 1 = stdout
exit 0
fi
And second, that the author seems to switch between outputting the help_msg on stdout or stderr, depending on if stdout exists. I always was under the impression that only the actual script result ought to go to stdout, and personally I always put out general debugging, error messages, but also the usage, unconditionally to stderr.And since getting help is not an error, the exit code is 0.
From what you pasted it doesn't seem to check whether stdout exists, but whether stdin is a terminal. The intended use case is "your_program <input_file" => read from stdin; "your_program" => complain that you forgot a cmdline parameter (instead of blocking waiting for you to type something).
http://bellard.org/tcc/ (use -run)
https://root.cern.ch/drupal/content/cint
https://root.cern.ch/drupal/content/clingCint is a nightmare.
How do they compare?
I would probably use this with Clang rather than TCC if Clang compiles fast enough, which it generally does.
1. There's no guarantee that you can run anything directly out of /tmp/. IIRC lots of distros mount /tmp/ with noexec specifically so you can't do this. You might still be able to invoke ld directly to run it, but that's still kinda a hack to get around the noexec.
2. You need write access to the .c file. That means you can't install any scripts using this system-wide, because you won't have write-access to the .c source unless you're root.
IMO, the most obvious solution to the second is to make a copy of the .c source and edit that instead. AFAIK there isn't an easy solution to the first issue though.
A year or so ago, as part of my work on Project Clearwater (http://www.projectclearwater.org/), I was using a Ruby tool to retrieve statistics from a server, plumbing them into Cacti (http://www.cacti.net/) and graphing the results.
Cacti likes to restart statistics-gathering processes every time it wants new statistics (once a minute in my system), so this meant starting the Ruby interpreter every minute.
Project Clearwater is built to be scalable, so I turned up a few hundred nodes (EC2 makes this easy), at which point Cacti couldn't keep up - it took the best part of a second per Ruby process invocation, and since I was polling every minute, there just wasn't enough time to get through all the nodes.
I rewrote the Ruby tool in C++, at which point it ran in less than 0.1s, which was fast enough for what I needed.
Amusingly (at least to me), it actually _compiled_ (under GCC) and ran in less time than it took for the Ruby interpreter to start.
(This is not intended to be a comparison of the merits of C++ and Ruby. It's quite possible the Ruby code could have been optimized and really I was solving the wrong problem - I probably should have been making the statistics-gathering process long-lived. The point of the above is just that GCC is actually very quick for relatively small programs.)
Matt
All you takes is a few lines of shell code to the top of the C file.
http://rosettacode.org/wiki/Multiline_shebang#C
I contributed that, anonymously. Previously, the task had been marked "omit from C", would you believe it!
Also, note the little "Student exercise" below the code. For this to be useful, you want to cache the compiler output; you want to recompile the underlying executable only if the C script has changed.
The inconvenience of invoking C programs obviously isn't the real obstacle to its use as a scripting language, otherwise this kind of thing would be widely used.
As was already mentioned Fabrice Bellard's tcc is great in this regard. There were similar projects done with LLVM.
What I would really like to have is some kind of compiler or different C preprocessor that would implement modules, such as that building C would be as simple as building Go programs. Price for it I suppose are macros. I think it's possible.
$ clang -xc -c -emit-llvm -o - - | lli
#include <stdio.h>
int main(void) {
puts("hi");
}^D
hi
As a shell executable interpreter #!/usr/bin/env bash
[ -f "$1" ] && file="$1" || file=/dev/stdin
awk 'NR==1&&/^#!/{next}{print}' "$file" | \
clang -xc -c -emit-llvm $CFLAGS -o - - | \
lli -fake-argv0="$file" $IFLAGS -
You can even "link" static archives $ IFLAGS=-extra-archive=/path/to/libz.a \
> CFLAGS+='-include stdio.h -include zlib.h' \
> c <<< 'int main(void){puts(zlibVersion());}'
1.2.8[0] http://www.in-ulm.de/~mascheck/various/shebang/#interpreter-...
//usr/bin/env swift $0 "$@"; exit $?
println("helloo")which supports go, c, and flex.
alias c=tcc -run
?
https://www.netfort.gr.jp/~dancer/software/binfmtc.html.en
For Debian, "apt-get install binfmtc"
Ditto: Shebang, stdin Extras: Automatic includes, nice error reporting, RC files, pkg-config, and C++ too. Lacks: Multiple file
#if 0
THIS=$0
BIN=/tmp/$(basename $0)
cc $THIS -Wall -o $BIN
$BIN
rm $BIN
exit
#else
#include<stdio.h>
int main(){printf("hello\n");}
#endifFor a fully-realized C shell, see TempleOS with its HolyC variant: https://www.youtube.com/watch?v=5gfoDHycEi0
Erm... speak for yourself :)