Filters in UNIX are generally programs that read from standard in (stdin), write to standard out (stdout), and somewhere in the middle modify the data in some way. Filters can be run directly from the command line, from a shell script, or from within a virtual printer. This document covers how to use a filter from a virtual printer, explains some filter details that are available with AIX, and provides some tips on writing your own filters.
This document applies to AIX version 4.x.
Related documentation
The product documentation library is also available through the following link:
http://www.rs6000.ibm.com/resource/aix_resource/
Pubs/index.html
NOTE: The AIX online product documentation has a sample C filter for adding carriage returns to line feeds.
The trigger that turns on a particular filter is the _f attribute or the -f command line flag. This attribute allows a single character value. This character is used to point to an attribute that begins with an f and ends with the specified character that contains the name of the program to run. For example if _f=l, then the filter program used will be listed in fl. The program name is specified by setting fl=/full/path/name.
Example:
If you are using a korn shell script, use f1=/bin/ksh /usr/local/bin/myfilt
The steps to set this up in AIX 4 are:
User written filters should read from stdin and write to stdout. The following are simple filters used to add carriage returns to line feeds.
/* lfilt.c, compile 'cc -o lfilt lfilt.c' */ /* Program to provide a filter to add \r to every \n in a file This is needed for some printers when using passthrough mode */ #include "stdio.h" #include "termio.h" #include "termios.h" int main(void) { int c; while ( (c = getc(stdin)) != EOF) { if (c=='\n') putc('\r',stdout); putc(c, stdout); } exit(0); }
Be sure to put the sed program in the fl attribute as follows:
In this example the sed script is called rmcr and is in /usr/local/bin.
This sed script is a single line
The pr filter is a program that does basic text formatting for printing such as adding carriage returns to line feeds, adding page numbers, and even creating 2-up documents.
When the qprt -fp flag is used, the pr filter is called automatically. This will add the file name and page numbers automatically to each page. This is useful when comparing files. The printed file name on each page is helpful. You can set a title page for the pr filter from smit chpq under the Default Print Job Attributes.
The virtual printer attribute, fp, calls pr and uses the -l and -w flags to determine the length and width of the page for adding carriage returns and line feeds.
To use pr to create a 2-up page that is landscape with two columns, use the command:
pr -l 60 -w 172 -2 <filename> | qprt -z+ -dp -p17splpThe output will look similar to this:
+--------------------------------------------------------+ | page one on this side Page two on this side + | page one on this side Page two on this side + | page one on this side Page two on this side + | page one on this side Page two on this side + | page one on this side Page two on this side + | page one on this side Page two on this side + | page one on this side Page two on this side + | page one on this side Page two on this side + | page one on this side Page two on this side + | page one on this side Page two on this side + | page one on this side Page two on this side + | page one on this side Page two on this side + +--------------------------------------------------------+
This filter will be called automatically when you send text data to a PostScript virtual printer and the Automatic Detection of Data Type is set to yes. You can also force it to be called by sending the -da flag to qprt as follows:
qprt -da -P<ps_queue> <text_file>
When used with the PostScript queue, many of the standard ASCII virtual printer flags are automatically passed on to enscript after being converted to enscript flags. For example when you use the qprt -z+ flag to print a file in landscape, this is passed to enscript as -r for rotate.
The font used by enscript can be changed by using the qprt -s flag as shown here:
qprt -da -sCourier-Bold -P<ps_queue> <text_file>When using enscript from the command line, 2 column prints can be created by using the following command examples:
enscript -dps39 -2r filex enscript -2rG -dsp filename
The asa and fpr commands are really the same command. They are the default ff attribute for many virtual printers. This means that they will be called by the qprt -ff flag. This filter is usually used when the program has placed carriage control characters in column one of the file as is the tradition from mainframe card punch days.
The asa and fpr commands print FORTRAN files to conform to AIX line-printer conventions. Both commands work like a filter to transform files formatted according to FORTRAN carriage control conventions into files formatted according to AIX line-printer conventions.
Both commands read the first character of each line from the input file, interpret the character, and space the line according to the definition of the first character. If the first character is a blank, a 0 (zero), a dash (-) , a 1 (one), or a plus sign (+), either command does the following:
Blank Advances the carriage one line and prints the input line. 0 Advances the carriage two lines and prints the input line. - Advances the carriage three lines and prints the input line. 1 Advances the carriage to the top of the next page. + Does not advance the carriage and starts printing the input line in the first space of the output file.
The commands interpret a blank line as if its first character is a blank and delete a blank that appears as a carriage control character. It treats lines that begin with characters other than the defined control characters as if they begin with a blank character. The first character of a line is not printed. If any such lines appear, an appropriate diagnostic appears in the standard error.
NOTE: Results are undefined for input lines longer than 170 characters.
To use this filter, the Text Formatting Tools must be installed from fileset bos.txt.ts.
The qprt -fn flag is used to call the ditroff filter. You can see how this is called by looking at the fn attribute of the virtual printer. The contents of this will vary depending on the type of printer being used. For a PCL printer, this will call the hplj filter program as shown here.
ditroff filter fn = { if [[ ! -x /usr/bin/hplj ]]; then %Ide/piomsg -c pioattr1.cat -s 1 -n 804 "Error\072 The /usr/bin/hplj command was not found."; exit 1; fi; /usr/bin/hplj; }%ip '{ if [[ ! -x /usr/bin/hplj ]]; then ' %Ide INCLUDE: (Directory Containing Miscellaneous Modules) '/piomsg -c pioattr1.cat -s 1 -n 804 "Error\072 The /usr/bin/hplj command was not found."; exit 1; fi; /usr/bin/hplj; }' %ip Force Attribute ip as Main Pipeline
The psroff command can be entered from the command line to format man pages for PostScript printers as follows:
psroff -man /usr/local/man/man1/pdf2ps.1
Hint: The man page from this example is a public domain program that is a filter for converting Adobe Acrobat pdf files to PostScript and could be used as a user filter.
It can also be used for Y2K conversion by converting all Y characters to K's. (A little humor)
For example to print a file in all upper case letters use:
cat /etc/motd | tr 'a-z' 'A-Z' | qprtTo replace every nonprinting character, other than valid control characters, with a ? (question mark), enter:
tr -c '[:print:][:cntrl:]' '[?*]' < <textfile> > <newfile>
This filter can be added to a virtual printer as a user filter as described above. There is also a similar command from the BSD UNIX heritage called trbsd.
This document has described some of the filters that come with AIX
and some uses for them. A number of filters are also available through the public domain to perform
tasks such as converting from one format to another. One of the most popular
is a program called GhostScript or often referred to as gs.
This filter can be used to print some PostScript files to PCL
printers.
[ Doc Ref: 92187555516324 Publish Date: Oct. 02, 2000 4FAX Ref: 6500 ]