Scripted wrappers for legacy applications, Part 3

By Cameron Laird and Kathryn Soraiz, Unix Insider |  Development Add a new comment


We emphasize wrapping existing applications because it's such good engineering. Suppose, as we did in our previous two columns, that you have a command line application that works correctly -- you just want a GUI with the same functionality. The safest way to achieve that is to keep what you have -- the correct, well-exercised command-line application -- and wrap it with a thin GUI.

Even the smallest change requiring a relinking of a compiled application introduces numerous possibilities for error. That's why we so strongly favor a pipe for IPC -- it fits architectures that reuse what you already have.


However, many other forms of IPC can serve in this role. The most important in the Internet age, of course, is a TCP/IP socket connection. Most scripting languages now support networking quite well. One architecture we often use when scripting existing code is C embedded in a computational server and connected to it with simple, networked GUIs. You may remember that the July installment of Regular Expressions mentioned Effective Tcl/Tk Programming as a good read for scripters, even those who don't use Tcl or Tk. In Chapter 7 of that book, authors Michael McLennan and Mark Harrison work out a nice example of this computational service architecture.


What the authors leave unsaid, though, is that their methods are more or less language-independent. Code a computational server in C or Fortran, with a simple protocol for talking to clients. You can then quickly develop client-side GUIs in any language you choose: Perl/Tk, Tkinter, or something more exotic. The point is not to proliferate technologies, but to realize you're free to combine the ones that best suit different requirements of your projects. Use Fortran and PyQt when both are advantageous; teach the two to communicate and you can enjoy the best of two different worlds.

Extending a scripting processor

Think about:


     print("Hello, world.\n"); 


That is a Perl script. Suppose you've coded a little C function that logs results to a central database. You want to take advantage of the work you've already done, and somehow have the capability to write:


     my_log("Hello, world.\n"); 


in Perl, and have it do what you intend. You want to extend Perl.


Extension is more specialized than the process management we explained earlier. The first two installments of this series demonstrated tiny working programs that illustrated important functionality available in essentially all modern scripting languages. Extension involves several details likely to be specific to a particular compiler, platform, and language; useful code doesn't generalize so transparently.


Our goal in this article is to introduce the vocabulary of language extension, and supply the resources you'll need to pursue the subject with a particular language.


To extend a scripting language with a function coded in C, C++, Fortran, or another language, you usually need:

  1. The function compiled as an object module
  2. A wrapper to match your function with your scripting language
  3. A notification or registration
  4. A way to introduce the wrapper into the scripting language's process space
  5. A script that invokes the wrapper


The last one is usually the easiest -- it might be as easy as replacing print with my_log in a program that already works.


It's impossible to directly extend any of the most common scripting languages with C or Fortran. They must first be wrapped to match the calling conventions of the scripting language implementation. If you have an existing C-coded module:


       void C_my_log(char *text) ... 


you'll write a tiny wrapper:


       RETURN scriptable_my_log(ARGS) 
       { 
               ... 
               C_my_log(...); 
       } 


That kind of wrapping is largely mechanical; several well-supported systems automate the tedium. SWIG, which has been mentioned in Regular Expressions several times in the past 3 years, is the most widely used tool of this kind.


Registration alerts the language processor of an association between my_log (the name of a command or function visible within the extended scripting language) and scriptable_my_log (the name of an implementing C-coded function).

    Add a comment

    Post a comment using one of these accounts
    Or join now
    At least 6 characters

    Note: Comment will appear soon after you have activated your account.
    Obscene/spam comments will be removed and accounts suspended.
    The information you submit is subject to our Privacy Policy and Terms of Service.

    ITworld LIVE

    DevelopmentWhite Papers & Webcasts

    White Paper

    HP NonStop SQL Fundamentals whitepaper

    This whitepaper offers a detailed look into the fundamentals of HP NonStop SQL solutions. See how this system delivers unprecedented levels of application availability with fail-safe data integrity and meets the needs of enterprises with large-scale business critical applications.

    White Paper

    Nebraska Medical Center case study

    See how the Nebraska Medical Center implemented a SQL solution to make information more readily available to streamline operations, improve patient care and facilitate medical research with an enterprise solution running on HP NonStop servers.

    White Paper

    Concepts of NonStop SQL/MX

    For DBAs and developers who are familiar with Oracle solutions and want to learn about NonStop SQL/MX, this whitepaper provides an overview of the similarities and differences between the two products-with a specific focus on implementation.

    White Paper

    6 Things Your CIO Needs to Know About Requirements

    If your organization is not predictably successful on technology projects, there is likely an issue in requirements. CIOs must take action and own requirements maturity improvement. There are 6 main things a CIO must know about requirements.

    Webcast On Demand

    User Experience Monitoring

    In this webinar, you will learn hints & tips for improving end-user response times from Forrester Research analyst, Jean-Pierre Garbani.

    Sponsor: Nimsoft

    See more White Papers | Webcasts

    Ask a question

    Ask a Question