Question & Answer
Question
AIX Print Filters
Answer
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 versions 4 and 5L.
NOTE: The AIX Version 4 online manual,
Guide to Printers and Printing, has a sample C filter
for adding carriage returns to line feeds.
or The AIX Version 5L online manual
Guide to Printers and Printing, has a sample C filter
for adding carriage returns to line feeds.
This document contains the following topics:
Examples of AIX filters
How to use a filter from a virtual printer
Examples of user written filters
Using the pr filter
Using the enscript filter
Using the asa or fpr FORTRAN filter
Using the troff filter
Using the tr filter
Conclusion
Examples of AIX filters
Examples of filters that are a part of AIX include:
- pr
- This filter formats ASCII data into pages, adds
page numbers, and can provide print rotation and page numbers.
It is very similar to piobe formatting.
- asa or fpr
- This filter interprets the carriage control characters
in column one, sometimes known as FORTRAN formatting. Although
this filter goes by two names, there is only one filter.
- nroff, troff, psroff, hplj, psc, tbl, pic, eqn, grap
- These filters are for formatting xroff data to/from ditroff
format. These
filters allow a tagged file to create a formatted document that supports
multiple fonts, tables, pictures, equations, and the like.
- enscript
- This filter converts ASCII text data to PostScript.
- tr
- This filter translates ASCII text from one character to another.
How to use a filter from a virtual printer
Filters are generally set up in a virtual printer to run before the data goes to the formatting pipeline. In fact, you can implement a filter even when the virtual printer is set in pass through mode and when it does not use either the ci (initialization) or cr (reset) sequences.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:
- Create filter program is /usr/local/bin/myfilt
- Set f1=/usr/local/bin/myfilt
- Print with qprt -f1 -Pqueue or set f=1
If you are using a korn shell script, use f1=/bin/ksh /usr/local/bin/myfilt
Setup at AIX 4 and later
The steps to set this up are:
- Create filter program is /usr/local/bin/myfilt
- Enter smit spooler
- Select Programming Tools
- Select Change / Show Pre-processing Filters
- Choose the virtual printer of interest
- Enter for User defined filter 1
/usr/local/bin/myfilt - Press the Enter key or click the Do button
- Back out to Print Spooling with F3
- Select Change / Show Print Queue Characteristics
- Select the print queue name
- Select 2 Default Print Job Attributes
- Go to Pre-processing FILTER NAME
- Press F4 for the popup menu
- Select 1 User defined filter 1
Hit Enter
Entry will show [1]
You can assign multiple filters by adding different filters to different filter numbers (for example, Select 2).
- Hit Enter to save changes
Examples of user written filters
User written filters should read from stdin and write to stdout. The following are simple filters used to add carriage returns to line feeds.
Example filter in C
/* lfilt.c, compile 'cc -o lfilt lfilt.c' */ /* Program to provide a filter to add to every 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==' ') putc(' ',stdout); putc(c, stdout); } exit(0); }
Example sed filter
Be sure to put the sed program in the fl attribute as follows:
- fl = /usr/bin/sed -f /usr/local/bin/rmcr
In this example the sed script is called rmcr and is in /usr/local/bin.
This sed script is a single line
- s/^M//g
Using the pr filter
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 + +--------------------------------------------------------+
Using the enscript filter
The enscript filter is a program than converts ASCII text to PostScript. See the man pages of enscript for details on the flags.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
Using the asa or fpr FORTRAN filter
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.
Using the troff filter
The set of ~roff commands includes troff, nroff, psc, psroff, hplj and others. These commands are designed to handle a markup language that has long been used for typesetting and creating books. It is not used as much today, but can still be useful when printing things like man pages. For more information, read the man pages for the individual commands.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: 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: 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.
Using the tr filter
The tr filter can be used to convert one character or string to another. This could be used as a spell correcting technique, a technique to remove null characters, or it could be used to change all lower case letters to upper case.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.
Conclusion
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.
Historical Number
isg1pTechnote0465
Was this topic helpful?
Document Information
Modified date:
17 June 2018
UID
isg3T1000299