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