Using C functions and libraries in your shell code!!

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
sc0ttman
Posts: 2812
Joined: Wed 16 Sep 2009, 05:44
Location: UK

Using C functions and libraries in your shell code!!

#1 Post by sc0ttman »

https://github.com/taviso/ctypes.sh
A foreign function interface for bash.

This could be really useful :D

:arrow: You can use this to call C functions from with your shell code - making your shell scripts able to perform very fast C functions :!:

This could be used for a number of things:

- write programs in shell script that use real GTK interfaces (not only gtkdialog)
- call C functions to do dependency management in PPM
- embedded webkit in gtk using shell script
- lots more.. See all examples here - https://github.com/taviso/ctypes.sh/tree/master/test

Here's an example which creates a GTK frontend for a shell script:

Code: Select all

#!/bin/bash

# This is a port of the GTK+3 Hello World to bash.
#
# https://developer.gnome.org/gtk3/stable/gtk-getting-started.html
source ctypes.sh

# declare some numeric constants used by GTK+
declare -ri GTK_ORIENTATION_HORIZONTAL=0
declare -ri G_APPLICATION_FLAGS_NONE=0
declare -ri G_CONNECT_AFTER=$((1 << 0))
declare -ri G_CONNECT_SWAPPED=$((1 << 1))

# void print_hello(GtkApplication *app, gpointer user_data)
function print_hello ()
{
    echo "Hello World"
}

# void activate(GtkApplication *app, gpointer user_data)
function activate ()
{
    local app=$2
    local user_data=$3
    local window
    local button
    local button_box

    dlsym -n gtk_widget_destroy gtk_widget_destroy

    dlcall -n window -r pointer gtk_application_window_new $app
    dlcall gtk_window_set_title $window "Window"
    dlcall gtk_window_set_default_size $window 200 200

    dlcall -n button_box -r pointer gtk_button_box_new $GTK_ORIENTATION_HORIZONTAL
    dlcall gtk_container_add $window $button_box

    dlcall -n button -r pointer gtk_button_new_with_label "Hello World"
    dlcall g_signal_connect_data $button "clicked" $print_hello $NULL $NULL 0
    dlcall g_signal_connect_data $button "clicked" $gtk_widget_destroy $window $NULL $G_CONNECT_SWAPPED
    dlcall gtk_container_add $button_box $button

    dlcall gtk_widget_show_all $window
}

declare app     # GtkApplication *app
declare status  # int status

# Generate function pointers that can be called from native code.
callback -n print_hello print_hello void pointer pointer
callback -n activate activate void pointer pointer

# Prevent threading issues.
taskset -p 1 $$ &> /dev/null

# Make libgtk-3 symbols available
dlopen libgtk-3.so.0

dlcall -n app -r pointer gtk_application_new "org.gtk.example" $G_APPLICATION_FLAGS_NONE
dlcall -r ulong g_signal_connect_data $app "activate" $activate $NULL $NULL 0
dlcall -n status -r int g_application_run $app 0 $NULL
dlcall g_object_unref $app

exit ${status##*:}

You should consider the above example similar to our own GtkDialog scripts, except that we're accessing the full GTK interface, not the fairly limited/broken implementation of it that is GtkDialog. :shock:

---
ctypes.sh makes it possible to use GTK+ natively in your shell scripts, or write a high-performance http daemon.
A project which uses it: https://github.com/cemeyer/httpd.sh
[b][url=https://bit.ly/2KjtxoD]Pkg[/url], [url=https://bit.ly/2U6dzxV]mdsh[/url], [url=https://bit.ly/2G49OE8]Woofy[/url], [url=http://goo.gl/bzBU1]Akita[/url], [url=http://goo.gl/SO5ug]VLC-GTK[/url], [url=https://tiny.cc/c2hnfz]Search[/url][/b]

step
Posts: 1349
Joined: Fri 04 May 2012, 11:20

#2 Post by step »

Nice find, thanks.
[url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Fatdog64-810[/url]|[url=http://goo.gl/hqZtiB]+Packages[/url]|[url=http://goo.gl/6dbEzT]Kodi[/url]|[url=http://goo.gl/JQC4Vz]gtkmenuplus[/url]

wiak
Posts: 2040
Joined: Tue 11 Dec 2007, 05:12
Location: not Bulgaria

#3 Post by wiak »

Yes, nice. And MIT license. Might take a look at its inners sometime.

User avatar
rockedge
Posts: 1864
Joined: Wed 11 Apr 2012, 13:32
Location: Connecticut, United States
Contact:

#4 Post by rockedge »

I will be experimenting with this capability immediately!!

Post Reply