arx
arx

Reputation: 16896

How to make an application scriptable in Linux

I've written an application in C++ that takes a complex binary file format and translates it into human-readable text. Having edited the text you can recompile it back into the binary file format.

This would be more useful if the application's internal object model was scriptable. On Windows I'd expose the objects using COM or .Net but I want this to work on Linux. I could embed a scripting language but that's a fair bit of work, and limits users to the scripting language I choose. Ideally, I'm looking for some way of exposing a scriptable DOM from my application that is:

Some more detail: I was hoping for a solution that provides similar benefits to COM. Namely, once the application's internal objects are exposed via COM they can be manipulated by any scripting language in the same way JavaScript can manipulate an HTML DOM. Objects and methods are easily discoverable, and many IDEs offer automatic command-completion for your objects. Usability on the client side is important.

Upvotes: 4

Views: 310

Answers (3)

liori
liori

Reputation: 42267

The usual way is to expose some CLI commands and allow users to use them in whatever shell/script language user writes; see f.e. imagemagick, which exposes a number of commands to convert images between formats and apply transformations. This works well in any OS.

This can also work with interactive programs, although it is rare. You can use D-BUS interface (which gets more and more popular in both GNOME and KDE), although it is more for processing events or sending simple commands. You might want to make an interactive or daemon-like program which exposes D-BUS (or even simple socket/pipe-based) interface, plus some simple CLI calls that wraps sending commands so that the interface is much simpler. See moc/mocp ("Music On Console Player") or xmms2. This works well in any OS, but usually needs some time to work out implementation details on different OSes.

Do not fear embedding a full language. Languages like Lua or Guile were designed so that they are very simple to embed and pretty powerful. Standarizing on one such language is not always a bad thing, as this means more code reusability between users... and the language actually matters only if you plan users to write big pieces of code as plugins.

There are some ways to expose API to several scripting languages using special libraries. You can read about them f.e. here: Kross@Wikipedia. I have no experience with them.

I assume that your program will be closed-source... Then last option I can see is to expose some kind of API/ABI interface which can be used by users' C programs (f.e. compiled to dynamic library). This way users will be able to make wrappers for any language they want, plus they can make code in plain C for speed. This solution might be difficult to make portable, but it gives you (and your users) flexibility.

Note that it is easy to overdesign scriptability: it is better to leave programming constructs to external languages, and only provide simple means of interaction with your program. I have seen programs which added their own looping facilities to a scripting languages, even though they didn't add any value to user: f.e. ability to pass multiple images to convert at once, even though it didn't make processing faster.

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798556

You may find D-Bus useful. It provides an object-oriented API with activation support.

Upvotes: 3

Felix
Felix

Reputation: 89566

I'm not exactly sure what you mean by "scriptable". Do you mean you want it to have an external interface (sort of like an API) which other programs/scripts can access? If yes, then I think your best bet is to use sockets (either Unix or TCP). They're as cross platform as it gets and most scripting languages have some way of communicating through sockets (I've played with them in Python, PHP and Java).

As a side note, if you decide to go with sockets, it would be nice to implement some sort of standardized protocol, such as XML-RPC over HTTP. This would make your "API" much easier to use.

Again, I hope I understood what you meant by "scriptable". If not, just disregard my answer.

Upvotes: 0

Related Questions