SWIG (Simplified Wrapper and Interface Generator) is a freely available tool that integrates Perl, Python, Tcl, and other scripting languages with programs written in C, C++, and Objective-C. While SWIG is similar to other Perl extension building tools such as xsubpp and h2xs, SWIG has a number of unique features that help simplify the task of creating Perl extension modules. To interface Perl with FTT code written in C , it is necessary to write wrappers that serve as a glue layer between the Perl interpreter and the underlying C code. These wrappers are responsible for converting data between Perl and C, reporting errors, and other tasks.This paper describes FTT functions, accessible from Perl scripts.
FTT functions were selected for Perl extension building with SWIG:
By building a Perl interface to FTT, our goal is to write similar code in Perl. Thus, the functionality of the FTT library must to be exposed to the Perl interpreter. To do this, a SWIG interface file ftt.i and few C files were written (e.g. ftt_s_open.c, ftt_s_position.c, ftt_s_rdwr.c etc). Also, some Global variables were created, that allow to transfer variable values from FTT to Perl. This paper contains examples of some functions that requared some explanations. All Other functions that not mentioned in this examples executes in the same way as in FTT.
Example:
#!/usr/bin/perl use ftt; $fd = ftt::_open ("/dev/rmt/my_tape", $FTT_FLAG); /*FTT_FLAG = RDONLY/RDWR */ /* do some operations */ ftt::_close($fd);
Example:
#!/usr/bin/perl use ftt; $fd = ftt::_open("/dev/rmt/my_tape", FTT_FLAG); ftt::_get_stats($fd); ftt::_extract_stats("FTT_REMAIN_TAPE"); print "Remain tape is ",$ftt::Stat_value,"\n"; ftt::_close($fd);
Example:
#!/usr/bin/perl use ftt; $fd = ftt::_open("/dev/rmt/my_tape", FTT_FLAG); $status = ftt::_status($fd, $Time_out); ftt::_get_position($fd); print "Status of tape is ",$status," File no. ",$ftt::File_no," Block no. ",$ftt::Block_no,"\n"; ftt::_close($fd);
Example:
#!/usr/bin/perl use ftt; $fd = ftt::_open(/dev/rmt/my_tape", FTT_FLAG); $Bsize = 123456; $File_name = "my_filename"; $status = ftt::_write($fd, $File_name, $Bsize); print "Status write ",$status,"\n"; $status = ftt::_read($fd, $File_name, $Bsize); print "Status read ",$status,"\n"; ftt::_close($fd);
Example:
#!/usr/bin/perl use ftt; $fd = ftt::_open("/dev/rmt/my_tape", FTT_FLAG); /* doing some operations */ ftt::_get_basename($fd); print "Base name of dev is ",$ftt::Stat_value,"\n"; ftt::_close($fd);
Example:
#!/usr/bin/perl use ftt; $fd = ftt::_open("/dev/rmt/my_tape", FTT_FLAG); $Density = ; $Compression = ; $Blocksize = ; ftt::_set_mode($fd, $Density, $Compression, $Blocksize); /* doing some operations */ ftt::_get_mode($fd); print "Device name ",$ftt::Stat_value,"Density ",$ftt::Density,"Compression ", $ftt::Comp,"Blocksize ",$ftt::Blocksize,"\n";
Example:
#!/usr/bin/perl use ftt; $fd = ::_open("/dev/rmt/my_tape", FTT_FLAG); ftt::_describe_dev($fd, "My_device", "My_file"); /* doing some operation and print device description */ open (out,"My_file"); while () { print $_; } close (out); ftt::_close($fd);
To build a Perl5 module, run swig as follows:
swig -perl5 -I/usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE $INC ftt.i
where INC looks like:
-I/$FTT_DIR/include -I/$FTT_DIR/ftt_lib -I/usr/src/linux/include/scsi
This procedure will produce 2 files. The first file ftt_wrap.c contains all of the C code needed to build a Perl5 module. The second file ftt.pm contains supporting Perl code needed to properly load the module into Perl. To finish building a module, you will need to compile the ftt_wrap.c and link it with the C programs that call FTT routines.
gcc -fpic -Dbool=char -D_REENTRANT -D_GNU_SOURCE -DTHREADS_HAVE_PIDS -DDEBUGGING\
-I/usr/lib/perl5/5.8.0/i386-linux-thread-multi/CORE -I. $INC $LIB -c ftt_wrap.c
where LIB looks like:
-L/$FTT_DIR/lib -L/$FTT_DIR/ftt_lib -L/$FTT_DIR/ftt_lib/local/libs
And last operation:
ld -G ftt_wrap.o ftt_s_position.o ftt_s_open.o ftt_s_stats.o ftt_s_rdwr.o ftt_s_mode.o -o ftt.so $LIB -lc -lftt -lc -lftt