blob: 773e8b4164c1636978f9d331d615dcad2bcae0ba [file] [log] [blame]
Eric S. Raymondb60f2d02001-08-18 09:24:38 +00001\section{\module{compilerlike} ---
2 framework code for building compiler-like programs.}
3
4\declaremodule{standard}{set}
5\modulesynopsis{Framework code for building compiler-like programs.}
6\moduleauthor{Eric S. Raymond}{esr@thyrsus.com}
7\sectionauthor{Eric S. Raymond}{esr@thyrsus.com}
8
9There is a common `compiler-like' pattern in Unix scripts which is useful
10for translation utilities of all sorts. A program following this pattern
11behaves as a filter when no argument files are specified on the command
12line, but otherwise transforms each file individually into a corresponding
13output file.
14
15The \function{filefilter}, \function{linefilter}, and
16\function{sponge} functions in this module provide a framework and
17glue code to make such programs easy to write. You supply a function
18to massage the file data; depending on which entry point you use, it
19can take input and output file pointers, or it can take a string
20consisting of the entire file's data and return a replacement, or it
21can take in succession strings consisting of each of the file's lines
22and return a translated line for each.
23
24All three of these entry points take a name, an argument list of files,
25a data transformation function, and a name transformation function.
26They differ only in the arguments they pass to the transformation
27function when it is called.
28
29The name argument is not used by the functions in this module, it is
30simply passed as the first argument to the transformation function.
31Typically it is a string that names the filter and is used in
32generating error messages, but it could be arbitrary data.
33
Eric S. Raymond29bb1152001-08-20 13:16:30 +000034The second argument is interpreted as a list of filenames. The files
Eric S. Raymondb60f2d02001-08-18 09:24:38 +000035are transformed in left to right order in the list. A filename
36consisting of a dash is interpreted as a directive to read from
37standard input (this can be useful in pipelines).
38
39The third argument is the data transformation function.
40Interpretation of this argument varies across the three
41entry points and is described below.
42
43The fourth, optional argument is a name transformation function or
44name suffix string. If it is of string type, the shortest suffix of each
45filename beginning with the first character of the argument string
46is stripped off. If the first character of the argument does not
47occur in the filename, no suffix is removed. Then the name suffix
48argument is concatenated to the end of the stripped filename. (Thus,
49a name suffix argument of ".x" will cause the filenames foo.c and
50bar.d to be transformed to foo.x and bar.x respectively.)
51
52If the fourth argument is specified and is a function, the name of the
53input file is passed to it and the return value of the function
54becomes the name of the output software. If this argument is not
55specified, the imnput file is replaced with the transformed version.
56
57Replacement of each file is atomic and doesn't occur until the
58translation of that file has completed. Any tempfiles are removed
59automatically on any exception thrown by the translation function,
60and the exception is then passed upwards.
61
Eric S. Raymond29bb1152001-08-20 13:16:30 +000062\begin{funcdesc}{filefilter}{name, file, arguments, trans_data\optional{,trans_file}}
63Filter using a function taking the name, filename, and two file-object
Eric S. Raymondb60f2d02001-08-18 09:24:38 +000064arguments. The function is expected to read data from the input file
65object, transform it, and write the data to the output file object.
66When the function terminates, the translation is done. The return
67value of the transformation function is not used.
68\end{funcdesc}
69
Eric S. Raymond29bb1152001-08-20 13:16:30 +000070\begin{funcdesc}{linefilter}{name, file, arguments,trans_data\optional{,trans_file}}
71Filter using a function taking the name, the filename, and a string
72argument. The return value of the function should be a string. This
73function is applied to each line in the input file in turn; the return
74values become the lines of the transformed file.
Eric S. Raymondb60f2d02001-08-18 09:24:38 +000075\end{funcdesc}
76
Eric S. Raymond29bb1152001-08-20 13:16:30 +000077\begin{funcdesc}{sponge}{name, file, arguments, trans_data\optional{, trans_file}}
78Filter using a function taking the name, the filename, and a string
79argument. The return value of the function should be a string. The
80function will be passed the entire contents of the input file as a
81string. The string return value of the function will become the
82entire contents of the transformed file.
Eric S. Raymondb60f2d02001-08-18 09:24:38 +000083\end{funcdesc}
84
85# End
86
87