binfmt_misc C 脚本接口
Binfmtc – binfmt_misc C scripting interface

原始链接: https://www.netfort.gr.jp/~dancer/software/binfmtc.html.en

Binfmtc 允许 C 程序员使用 C 语言执行通常由 Perl 或 Shell 等语言完成的脚本任务。它解决了创建 makefile 和 C 语言执行所需的其他设置的不便。通过利用 Linux 的 binfmt_misc,binfmtc 可以即时编译和执行 C 脚本。只需在脚本中添加一个神奇的关键字`/*BINFMTC: compile-time options`,赋予其执行权限,binfmtc 就会处理剩下的事情。该软件包包含一个“真正的 csh”示例,演示了用于系统管理任务的 C 风格语法。在基于 Debian 的系统上安装它需要向 `sources.list` 文件添加一个特定的仓库并使用 `apt-get`。该工具旨在实现“C 无处不在”,未来的计划包括支持其他编译器,如 gcj、g77、gpc,以及 C# 等其他语言。

The Hacker News discussion centers around `binfmtc`, a tool facilitating C scripting using `binfmt_misc`. Users explored alternative methods of achieving similar functionality. One approach involves a clever trick where the first line is a comment for the shell but a preprocessor directive for the C compiler, enabling self-compiling executable C scripts. This leverages the shell to compile and execute the C code directly. A potential issue with using `$@` for argument passing was raised, suggesting `$0` might be more appropriate. Other solutions mentioned include embedding the compiler binary and using `tcc` with shebang extensions. The benefits of using `binfmt_misc` for extension registration versus other methods were also discussed. One user pointed out the platform-agnostic nature of a solution based on multiline shebang. Several users shared similar projects and hacks they've created to achieve comparable results.
相关文章

原文
binfmtc - binfmt_misc C scripting interface

Introducing the binfmt_misc C scripting interface

"I love C". "I enjoy writing in C". "I don't feel well when I have passed a day without coding a line of C". "My conversation with my wife simply doesn't flow without the C language". "I want to write everything in C, even the everyday scripting, but due to the steps required for writing make files and other things, I tend to choose interpreted languages". A good news for the C programmers suffering from these symptoms. binfmtc is a hack to allow you to use the C language for cases where script languages such as perl and shell were the languages of choice.

Also included is a "real csh" as an example, allowing you to use the C language style for executing everyday sysadmin work. Please experience the real C shell, which has a slightly different tint to the original C shell we've been used to for more than 10 years.

Screenshots

C assembly C++ real csh

Examples of execution in C, assembly, and C++. The last entry is the one for real csh.

Usage

Simply add a magic keyword /*BINFMTC: compile-time options and add execution permission to the C-script. Every time you invoke the script, the compiler will compile and execute the program for you.

How to install

For sid add the following line to /etc/apt/sources.list and do apt-get update; apt-get install binfmtc.

deb http://www.netfort.gr.jp/~dancer/tmp/20050523 ./
    

Mechanics behind it

By registering magic through Linux binfmt_misc, binfmtc-interpreter will be invoked every time a C script is invoked. binfmtc-interpreter will parse the specified script file, and will invoke gcc with the required options, and compile to a temporary binary, and invoke the binary.

Future plans

Do you actually find it, ... useful?

  • gcj support 0.3 done
  • g77 support 0.5 done
  • gpc support 0.5 done
  • gnat support
  • gobjc support
  • chill support
  • mono/pnet c# support
  • use binfmt-support done

References


Junichi Uekawa

$Id: binfmtc.html.en,v 1.11 2006/04/16 03:03:16 dancer Exp $

联系我们 contact @ memedata.com