xstr Command

Purpose

Extracts strings from C programs to implement shared strings.

Syntax

xstr-v ] [  -c ] [  ] [ File ]

Description

The xstr command maintains a file strings into which strings in component parts of a large program are hashed. These strings are replaced with references to this array. This serves to implement shared constant strings, most useful if they are also read-only.

The command:

xstr -c File

extracts the strings from the C source in the File parameter, replacing string references by expressions of the form (&xstr[number]) for some number. An appropriate declaration of the xstr array is prepended to the file. The resulting C text is placed in the file x.c, to then be compiled. The strings from this file are appended into the strings file if they are not there already. Repeated strings and strings which are suffixes of existing strings do not cause changes to the file strings.

If a string is a suffix of another string in the file but the shorter string is seen first by the xstr command, both strings are placed in the file strings.

After all components of a large program have been compiled, a file xs.c declaring the common xstr array space can be created by a command of the form:

xstr

This xs.c file should then be compiled and loaded with the rest of the program. If possible, the array can be made read-only (shared), saving space and swap overhead.

The xstr command can also be used on a single file. The command:

xstr File

creates files x.c and xs.c as before, without using or affecting any strings file in the same directory.

It may be useful to run the xstr command after the C preprocessor if any macro definitions yield strings or if there is conditional code which contains strings which may not, in fact, be needed.

The xstr command reads from its standard input when the - (minus sign) flag is given and does not alter the strings file unless the -c flag is specified also.

An appropriate command sequence for running the xstr command after the C preprocessor is:

cc -E name.c | xstr -c -
cc -c x.c
mv x.o name.o

The xstr command does not touch the file strings unless new items are added, thus the make command can avoid remaking the xs.o file unless truly necessary.

Flags

Item Description
-c Extracts strings from the specified file, and places them in the strings file.
-v Verbose mode. Tells when strings are found, or new in the strings file.
- Reads from standard input.

Examples

  1. To extract the strings from the C source in the File.c parameter, replacing string references by expressions of the form (&xstr[number]):
    xstr -c File.c
    An appropriate declaration of the xstr array is prepended to the file. The resulting C text is placed in the file x.c, to then be compiled.
  2. To declare the common xstr array space in the xs.c file:
    xstr

Files

Item Description
strings File which contains the extracted strings.
x.c Massaged C source.
xs.c C source for definition of array xstr.
/tmp/xs* Temporary file when xstr command does not touch the strings file.
/usr/ccs/bin/mkstr Contains an executable file.
/usr/ccs/bin/mkstr Contains an executable file for Berkeley environment.