#!/bin/bash # filename: ~/factor/work/libexempi/ffi/mk-ffi.sh # yes disgusting -- run on Debian exempi package # to gen bindings for factor # for XMP factor FFI, order of ops # header # typedefs # constants # c structs # utility #defines -> words # functions # mkdir -p tmp cd tmp echo Creating header.fact cat < header.fact ! bindings for xmp library exempi ! 2009feb10 maali@socialdynamics.ca USING: accessors alien alien.syntax combinators kernel system math locals ; IN: libexempi.ffi << "exempi" { { [ os winnt? ] [ "libexempi.dll" ] } { [ os macosx? ] [ "libexempi.dylib" ] } { [ os unix? ] [ "libexempi.so" ] } } cond "cdecl" add-library >> LIBRARY: exempi EOFACT echo Creating typedefs.fact ( echo '! typedefs.fact' echo '! typedefs.fact - custom' echo TYPEDEF: uint uint32_t echo TYPEDEF: int int32_t echo TYPEDEF: longlong int64_t echo TYPEDEF: ulonglong uint64_t echo '! typedefs.fact - structs' grep 'typedef struct .*;' /usr/include/exempi-2.0/exempi/xmp.h | sed 's/typedef /TYPEDEF:/; s/struct//; s/_.*\*/void* /; s/;//' echo '! typedefs.fact - enums' perl -n0e 'while (m/(typedef enum.*{[^}]*}[ ]*([A-Za-z0-9_]+);)/gm) {print "TYPEDEF: uint ".$2."\n" ; } ' "/usr/include/exempi-2.0/exempi/xmp.h" echo ) > typedefs.fact echo Creating consts.fact ( echo '! consts.fact' grep '^[[:space:]]*_*XMP.*=.*' "/usr/include/exempi-2.0/exempi/xmp.h" | sed -e 's|/\*.*\*/||; s|/\*.*$||; s/=/ ( -- n ) /; s/0x/HEX: /; s/,.*$// ; s/^[[:space:]]*\(_*XMP\)/: \1/ ; s/$/ ;/; s/0x// ; s/;/; inline/; s/\+1/1/ ; s/| \([_A-Z]*\)/ \1 bitor/g; ' | sed -e 's/\( [0-9a-fA-F]\+\)U\?L/ \1/g' | grep X | perl -pe 's/\t//g' echo '! consts.fact - custom' echo ': XMP_SCHEMA_NODE ( -- n ) HEX: 80000000 ; inline' echo ) > consts.fact echo Creating structs.fact ( echo '! structs.fact' perl -n0e 'while (m/(typedef struct.*{[^}]*})/gm) {print $1."\n" ; } ' "/usr/include/exempi-2.0/exempi/xmp.h" | sed -e 's=/\*.*\*/==; s/;//; s/typedef \+struct/C-STRUCT:/; s/{// ; s/}/;/; s/^[ \t]\+\(\b[A-Za-z0-9_]\+\b\) \(\b[A-Za-z0-9_]\+\b\)/ { "\1" "\2" } /g ; ' echo ) > structs.fact echo Creating defines.fact ( echo '! defines.fact' grep '#define.*(' "/usr/include/exempi-2.0/exempi/xmp.h" | sed -e 's/.*#define/:: /' | sed -e 's/(\([^()]*opt)\)/ ( \1/; s/,/ /; s/opt)/opt -- x )/; s/$/ ;/; s/(\([^ ]\)/\1/g ; s/(\([^ ]\)/\1/g ; s/(\([^ ]\)/\1/g ; s/\([^ ]\))/\1/g ; s/\([^ ]\))/\1/g ; s/\([^ ]\))/\1/g ; s/\([A-Za-z0-9_]\+\) & \([A-Za-z0-9_]\+\)/\1 \2 bitand/g; s/!= \([0-9A-Za-z_]\+\)/\1 = not/; s/== \([0-9A-Za-z_]\+\)/\1 = /; s/|= \([0-9A-Za-z_]\+\)/\1 bitor/ ; s/~\([0-9A-Za-z_]\+\)/\1 bitnot/ ; s/ &= \(.*\) ;/ \1 bitand ;/' echo ) > defines.fact echo Creating funcs.fact ( echo '! funcs.fact' perl -n0e 'while(m/^[ ]*((bool|int|void|XmpFilePtr|XmpStringPtr|XmpPtr) [^(\n]*\([^)]*\)[^;]*;)/xsgm) {print $1."\n" ; } ' "/usr/include/exempi-2.0/exempi/xmp.h" | perl -n0e 's/\n//sgm ; s/;/;\n/g ; print ' | perl -pe 's/\t+/ /g; s/ +/ /g;' | perl -pe 's/\b([a-zA-Z0-9_]+)[ ]*\*/\1* /g' | perl -pe 's/const//g' | perl -pe 's/(.?)\((.?)/\1 ( \2/g; s/(.?)\)(.?)/\1 ) \2/g; ' | perl -pe 's/^/FUNCTION: / ; s/ +/ /g' echo ) > funcs.fact echo Catting into ffi.factor cat header.fact typedefs.fact consts.fact structs.fact defines.fact funcs.fact > ffi.factor cp ffi.factor .. cd ..