Data file metaformats

A data file metaformat is a set of syntactic and lexical conventions that is either formally standardized or sufficiently well established by practice that there are standard service libraries to handle marshalling and unmarshalling it.

Unix has evolved or adopted metaformats suitable for a wide range of applications. It is good practice to use one of these (rather than an idiosyncratic custom format) wherever possible. The benefits begin with the amount of custom parsing and generation code that you may be able to avoid writing by using a service library. But the most important benefit is that developers and even many users will instantly recognize these formats and feel comfortable with them, which reduces the friction costs of learning new programs.

In the following discussion, when we refer to “traditional Unix tools” we are intending the combination of grep(1), sed(1), awk(1) and tr(1) for doing text searches and transformations. Perl and other scripting languages tend to have good native support for parsing the line-oriented formats that these tools encourage.

Our first case study in textual data metaformats was the /etc/passwd file. This format (one record per line, colon-separated fields) is very traditional under Unix and frequently used for tabular data. Other classic examples include the /etc/group file describing security groups and the /etc/inittab file used to control startup and shutdown of Unix service programs at different run levels of the operating system.

Data files in this style are expected to support inclusion of colons in the data fields via backslash escaping. More generally, code that reads them is expected to support record continuation by ignoring backlash-escaped newlines, and to allow embedding non-printable character data via C-style backslash escapes.

This format is most appropriate when the data is tabular, keyed by a name (in the first field), and records are predictably short (less than 80 characters long). It works well with traditional Unix tools.

Occasionally one sees field separators other than the colon, such as the pipe character | or even an ASCII NUL. Old-school Unix practice used to favor tabs, a preference reflected in the defaults for cut(1) and paste(1); but this has gradually changed as format designers became aware of the many small irriations that ensue from the fact that tabs and spaces are not visually distinguishable.

This format is to Unix what CSV (comma-separated value) format is under Microsoft Windows and elsewhere outside the Unix world. CSV (fields separated by commas, double quotes used to escape commas, no continuation lines) is rarely found under Unix.

The RFC-822 metaformat derives from the textual format of Internet electronic mail messages; RFC822 is the original Internet RFC describing this format (since superseded by RFC2822). The MIME (Multipurpose Internet Media Extension) provides a way to embed typed binary data within RFC822-format messages. (Web searches on either of these names will turn up the relevant standards.)

In this metaformat, record attributes are stored one per line, named by tokens resembling mail header-field names and terminated with a colon followed by whitespace. Field names do not contain whitespace; conventionally a dash is substituted instead. The attribute value is the entire remainder of the line, exclusive of training whitespace and newline. A physical line that begins with tab or whitespace is interpreted as a continuation of the current logical line.

A blank line may be interpreted either as a record terminator or as an indication that unstructured text follows.

Under Unix, this is the traditional and preferred textual metaformat for attributed messages or anything that can be closely analogized to electronic mail. Usenet news uses it; so do the HTTP 1.1 (and later) formats used by the World Wide Web. It is very convenient for editing by humans. Traditional Unix search tools are still good for attribute searches, through finding record boundaries will be a little more work than in a record-per-line format.

For examples of this format, look in your mailbox.

Fortune-cookie format is used by the fortune(1) program for its database of random quotes. It is appropriate for records that are just bags of unstructured text. It simply uses % followed by newline (or sometimes %% followed by newline) as a record separator. Example 5.3 is an example section from a file of email signature quotes:

It is good practice to accept whitespace after % when looking for record delimiters. This helps cope with human editing mistakes.

Fortune-cookie record separators combine well with the RFC-822 metaformat for records. If you need a textual format that will support multiple records with a variable repertoire of explicit fieldnames, one of the least surprising and human-friendliest ways to do it would look like Example 5.4.

Of course, the record delimiter could be a blank line, but a line consisting of "%\n" is more explicit and less likely to be introduced by accident during editing. In a format like this it is good practice to simply ignore blank lines.

XML is well-suited for complex data formats (the sort of things that the old-school Unix tradition would use an RFC-822-like stanza format for) though overkill for simpler ones. It is especially appropriate for formats that have a complex nested or recursive structure of the sort that the RFC-822 metaformat does not handle well. For a good introduction to the format, see XML In A Nutshell [Harold&Means].

XML has a very simple syntax resembling HTML's — angle-bracketed tags and ampersand-led literal sequences. It is about as simple as a plain-text markup can be and yet express recursively nested data structures. XML is just a low-level syntax; it requires a document type definition (such as XHTML) and associated application logic to give it semantics.

Example 5.5 is a simple example of an XML-based configuration file. It is part of the kdeprint tool shipped with the open-source KDE office suite hosted under Linux. It describes options for an an image-to-Postscript filtering operation, and how to map them into arguments for a filter command. For another instructive example, see the discussion of Glade in Chapter 9 (Generation)

One advantage of XML is that it has it is often possible to setect ill-formed, corrupted, or incorrectly-generated data through a syntax check, without knowing the semantics of the data.

The most serious problem with XML is that it doesn't play well with traditional Unix tools. Software that wants to read an XML format needs an XML parser; this means bulky, complicated programs, and may even restrict your choice of language when you write programs that want to read or generate your format.

One application area where XML is clearly winning is in markup formats for document files (we'll have more to say about this in Chapter 16 (Documentation)). Tagging in such documents tends to be relatively sparse among large blocks of plain text; thus, traditional Unix tools still work fairly well for simple text searches and transformations.

One interesting bridge between these worlds is PYX format — a line-oriented translation of XML that can be hacked with traditional line-oriented Unix text tools and then losslessly translated back to XML. A web search for “Pyxie” will turn up resources. The xmltk toolkit takes the ooposite tack, providing stream-oriented tools analogous to grep(1) and sort(1) for filtering XML documents; web search for “xmltk”.

XML can be a simplifying choice or a complicating one. There is a lot of hype surrounding it, but don't be a fashion victim by either adopting or rejecting it uncritically. Choose carefully and bear the KISS principle in mind.

Many Microsoft Windows programs use a textual data format that looks like Example 5.6. This example associates optional resources named ‘account’, ‘directory’, ‘numeric_id’, and ‘developer’ with named projects ‘python’, ‘sng’, ‘fetchmail’, and ‘py-howto’. The DEFAULT entry supplies values that will be used when a named entry fails to supply them.

This style of data file format is not native to Unix, but some Linux programs support it under Windows's influence. This format is readable and not badly designed, but is not widely supported by Unix tools. Like XML it doesn't play well with grep(1) or conventional Unix scripting tools. If you are willing to accept these limitations, using an XML format would probably be a better idea.

There are longstanding Unix traditions about how textual data formats ought to look. Most of these derive from one or more of the standard metaformats we've just described. It is wise to follow these unless you have strong and specific reasons to do otherwise.

In Chapter 10 (Configuration) we will discuss a different set of conventions used for program run-control files.