The Motif widget toolkit (Part 2)

How to process command line arguments

Mario Emmanuel
The Console
4 min readJul 17, 2021

--

An example on how to pass command line arguments to programs in Xt/Motif

There are many examples on how to program Motif applications, but sometimes locating certain good practices required to write complete Motif applications is harder. This series is going to focus more on the practical small features that are required to write a complete application.

Many applications will require command line arguments. While you can parse those command line arguments manually (as in any regular C program), it is wise to use the methods provisioned by Xt and Motif for three reasons:

  1. Doing so allows to specify the command line argument as application X resources, which means that you can configure those arguments by using all methods available to do so in X11, including resource configuration files too.
  2. It is easier as you combine both X related resources with application resources in one side.
  3. The command line argument values reside as resources, hence you reduce the number of global variables to maintain.

The X resource manager

In a X11 application the X resource manager (part of the X server) is the entity in charge of handling all the resource variables related to the application. It will handle all widget resources (colors, etc.) but it can also handle application-level variables —resources — as we are doing here.

The resource manager can be handled both using Xlib functions, which is a bit more involved and requires the developer implementing all functionality or it can be handled using the Xt encapsulating functions. The Xt approach is what you are expected to do when using Motif, as Motif is based on Xt.

Both the Volume 1 Xlib programming manual and the Volume 4 X toolkit programming manual for X cover resource manager.

Specifically, example 4–10 of the Volume 4 (at the time of writing the article the book can be accessed via Internet Archive here) covers an example on how to implement this. It is worth reading the whole 3.5 section that covers application resources.

The application level resource struct

While the full detail can be found in the included documentation, I will highlight that the first step to do is to define the struct which will contain all our application related resources. I.E., X resources which are not related to the X Widgets themselves, but to global application. This can be both graphical related (for example number of charts to draw in a window) or non graphical related (which named pipe shall be used in the application to receive the data).

The struct simply contains the different resources in our application. In this example is just one:

Then an array of resources is defined where for each resource, a complete specification is specified. This might seem a bit overwhelming but it is actually very easy once the basics are understood.

A description of the XtResource struct can be found both in the mentioned book and in the lesstiff documentation, that at the time of writing the article can be found here. lesstiff was a clone used to mimic Motif toolkit when Motif was not open sourced. Since Motif is now open sourced it makes no sense to use it anymore, but its documentation is still valid.

The options description record array

Once we have defined our own custom resource, we can use the array XrmOptionDescRec which is passed as a parameter to XtVaOpenApplication . This array can define many by default values and command line settings for any resource, same as we would do via a resource file.

In the included example we can see how this options array is passed as parameter:

Retrieving the application command line resource

To retrieve the application resources we populate the AppData structure using XtGetApplicationResources . After doing this, we can normally access the struct members of this:

The advantages as mentioned is that the resource can be gathered at any point in program given that you have access to the toplevel entity and that it can be defined not only via command line but also via default resource files, making it consistent with the application behaviour.

Following this pattern ensures that the application is easy to understand as it follows the standard, and it is also easy to configure via user, system or application level resource files.

A complete understanding of how this works requires reading the mentioned chapter of the Volume 4 X toolkit intrinsics programming manual and maybe the lesstiff documentation too.

The included repository can be found here, and covers the example, remember that you have to create the named pipe via mkfifo mynamepipefile:

--

--