"Linux Gazette...making Linux just a little more fun!"


A Brief Introduction to the kunf Library

By Marc Welz, mwelz@sar8.ee.uct.ac.za

Why ?

The kunf library is an attempt to set up a uniform way of accessing configuration data. Currently most large applications have their own configuration files - these files are likely to have a varying syntax and have no well-specified location. On the other hand small programs and scripts have no configuration files at all - they have values hard-coded into them which sometimes can be overridden from the command line or through environment variables. This entire setup seems somewhat suboptimal - it can be quite daunting to the novice user.

The kunf library attempts change this - it tries to manage configuration data on behalf of the program or script. Instead of each application implementing its own resource file parser, an application calls a set of library functions (in the case of a shell script that would be a call to a utility program) which then return the configuration data.

Each piece of configuration data has a name (actually a sort of path) which identifies it. This makes that data independent of any particular location or configuration file. Once an application requests a data item, the library looks up the value in a location transparent manner and (optionally) performs a set of translations on the value. Then the value is returned to the calling code.

This approach should have the advantage that there is a consistent way of accessing configuration data - data for different applications can be modified with the same utility and the economics of scale should make it possible to construct more sophisticated maintenance tools that would be feasible for a single application. Novice users would not need to have to learn the location of the resource files.


How ?

Once you have downloaded, extracted (tar -xzvf filename) and installed (a make ; make install should suffice) the library, you should be able to make use of the shell and C interface without too much difficulty:

>From a shell script you can use the utility kunfenv to place a particular piece of configuration data into the environment. For example, the template configuration files contain an entry for the nntpserver variable which is stored as news:nntp:nntpserver. A shell script can access that information with a statement like:


#!/bin/bash
# evaluate the result of a call to kunfenv 
eval `kunfenv news:nntp:nntpserver`
# Now we have the variable as news_nntp_nntpserver
echo "My nntpserver is $news_nntp_nntpserver"

A C program can access the same data with the following piece of code:


#include <kunf.h>
  ...
  char *str;
  kunfig_open(NULL,KUNFIG_OPEN_STANDARD);   
  str=kunfig_findvalue(3,"news","nntp","nntpserver");
  printf("My nntpserver is %s\n",str);
  kunfig_close();

Do not forget to link the program with the directive -lkunf.

The configuration file editor can be used to modify the value of news:nntp:nntpserver entry. One simply invokes the editor by typing kunfedit, navigates down to the nntpserver entry (select the news entry ...), modifies the value (hit the escape key to move off a field) and saves it (press escape several times - it will ask you if you want to save).


More ?

There exists a web page which contains more information on this library. You can also ftp the entire package directly. The library is released under the GNU Copyleft. You can contact the author at his difficult-to-spam-address.


Copyright © 1997, Marc Welz
Published in Issue 16 of the Linux Gazette, April 1997


[ TABLE OF CONTENTS ] [ FRONT PAGE ]  Back  Next