/ctypes.sh

A foreign function interface for bash.

Primary LanguageCMIT LicenseMIT

ctypes.sh

This is ctypes.sh, a foreign function interface for bash.

ctypes.sh is a bash plugin that provides a foreign function interface directly in your shell. In other words, it allows you to call routines in shared libraries from within bash.

A (very) simple example will help illustrate:

$ dlcall $RTLD_DEFAULT puts "hello, world"
hello, world

# A more complex example, use libm to calculate sin(PI/2)
$ dlopen libm.so
0x172ebf0
$ dlcall -r double ${DLHANDLES[libm.so]} sin double:1.57079632679489661923
double:1.000000

ctypes.sh can extend bash scripts to accomplish tasks that were previously impossible, or would require external helpers to be written.

prerequisites

ctypes.sh is dependent on the following libraries and programs:

  • libffi-dev
  • binutils-dev
  • libdl
  • make

install

ctypes.sh can be installed in several ways. You can use the clib or bpkg package manager to install ctypes.sh.

$ clib install taviso/ctypes.sh
$ bpkg install taviso/ctypes.sh

You can also build and install from source:

$ git clone https://github.com/taviso/ctypes.sh.git
$ cd ctypes.sh
$ make
$ [sudo] make install

By default ctypes.sh is installed into /usr/local/bin and /usr/local/lib. You can overload the prefix path by defining the PREFIX environment variable before installing.

$ PREFIX=$HOME clib install taviso/ctypes.sh

or

$ PREFIX=$HOME bpkg install taviso/ctypes.sh

or

$ PREFIX=$HOME make install

example

source ctypes.sh
puts () {
  dlcall $RTLD_DEFAULT puts "$@"
  return $?
}

puts "hello, world"

Here is what people have been saying about ctypes.sh:

  • "that's disgusting"
  • "this has got to stop"
  • "you've gone too far with this"
  • "is this a joke?"
  • "I never knew the c could stand for Cthulu."

You can read more about ctypes.sh and see it in action on the Wiki