打印格式化程序示例

此示例显示打印格式化程序如何与文档中的打印机格式化程序子例程进行交互。

编写打印格式化程序包含四个步骤:

  1. 如下所示创建打印格式化程序源文件
  2. 创建导入文件
  3. 创建导出文件
  4. 编译并链接打印格式化程序

打印格式化程序源文件

使用 ASCII 编辑器来创建名为 sample.c的格式化程序源文件。 该文件应包含以下行:

#include <stdio.h>
#include <piostruct.h>
 
/* STRING CONSTANTS */
/* Initialize Printer, Restore Printer, Form Feed */
#define INIT_CMD    "ci"
#define REST_CMD    "cr"
#define FF_CMD      "af"
/* INTEGER and STRING VARIABLES */
/* page length, page width, top margin, bottom margin */
#define Pglen          (*(_Pglen + piomode))
#define Pgwidth        (*(_Pgwidth + piomode))
#define Tmarg          (*(_Tmarg + piomode))
#define Bmarg          (*(_Bmarg + piomode))
/* indentation, begin page, form feed?, pass-through? */
#define Indent         (*(_Indent + piomode))
#define Beginpg        (*(_Beginpg + piomode))
#define Do_formfeed    (*(_Do_formfeed + piomode))
#define Passthru       (*(_Passthru + piomode))
/* initialize printer?, restore printer? */
#define Init_printer    (*(_Init_printer + piomode))
#define Restoreprinter  (*(_Restoreprinter + piomode))
/* Command names: form feed, vertical increment and decrement */
#define Ff_cmd         (*(_Ff_cmd + piomode))
#define Vincr_cmd      (*(_Vincr_cmd + piomode))
#define Vdecr_cmd      (*(_Vdecr_cmd + piomode))
/* Work variables for vertical increment and decrement */
#define Vincr          (*(_Vincr + piomode))
#define Vdecr          (*(_Vdecr + piomode))
/* Variables referenced by above #defines */
int *_Pglen, *_Pgwidth, *_Tmarg, *_Bmarg, *_Indent, *_Beginpg, *_Do_
formfeed, *_Passthru, *_Init_printer, *_Restoreprinter, *_Vincr, *_V
decr;
struct str_info *_Ff_cmd, *_Vincr_cmd, *_Vdecr_cmd;
/* TABLE OF ATTRIBUTE VALUES */
struct attrparms attrtable[] = { /*
name  data type  lookup  address of pointer */
"_b",  VAR_INT,   NULL,   (union dtypes *) &_Bmarg,
"_g",  VAR_INT,   NULL,   (union dtypes *) &_Beginpg,
"_i",  VAR_INT,   NULL,   (union dtypes *) &_Indent,
"_j",  VAR_INT,   NULL,   (union dtypes *) &_Init_printer,
"_l",  VAR_INT,   NULL,   (union dtypes *) &_Pglen,
"_t",  VAR_INT,   NULL,   (union dtypes *) &_Tmarg,
"_w",  VAR_INT,   NULL,   (union dtypes *) &_Pgwidth,
"_J",  VAR_INT,   NULL,   (union dtypes *) &_Restoreprinter,
"_Z",  VAR_INT,   NULL,   (union dtypes *) &_Do_formfeed,
"wp",  VAR_INT,   NULL,   (union dtypes *) &_Passthru,
"wf",  VAR_STR,   NULL,   (union dtypes *) &_Ff_cmd,
"wi",  VAR_STR,   NULL,   (union dtypes *) &_Vincr_cmd,
"wy",  VAR_STR,   NULL,   (union dtypes *) &_Vdecr_cmd,
"wV",  VAR_INT,   NULL,   (union dtypes *) &_Vincr,
"wD",  VAR_INT,   NULL,   (union dtypes *) &_Vdecr,
NULL,  0     ,   NULL,    NULL };
int pglen, tmarg, bmarg, vpos, vtab_base;
struct shar_vars sharevars;
struct shar_vars * /*** Setup Processing ***/
setup(argc, argv, passthru)
   unsigned argc;
    char *argv[];
    int passthru:
{
/* Initialize variables and command line values */
(void) piogetvals(attrtable, NULL);
(void) piogetopt(argc, argv, NULL, NULL);
/* (need to verify values entered by user) */
/* Initialize work variables */
pglen = Pglen * Vincr;
tmarg = Tmarg * Vincr;
bmarg = Bmarg * Vincr;
piopgskip = Beginpg - 1;
/* Check for pass-through option */
if (Passthru = passthru)
     return(NULL);
/* Initialize pointers to vertical spacing */
/* variables shared with formatter driver  */
/* (Refer to /usr/include/piostruct.h)     */
sharevars._pl            = &pglen;
sharevars._tmarg         = &tmarg;
sharevars._bmarg         = &bmarg;
sharevars._vpos          = &vpos;
sharevars._vtab_base     = &vtab_base;
sharevars._vincr         = &Vincr;
sharevars._vincr_cmd     = (&Vincr_cmd)->ptr;
sharevars._vdecr         = &Vdecr;
sharevars._vdecr_cmd     = (&Vdecr_cmd)->ptr;
sharevars._ff_cmd        = (&Ff_cmd)->ptr;
sharevars._ff_at_eof     = &Do_formfeed;
return(&sharevars);
}
initialize() /*** Initialize the Printer ***/
{
if (Init_printer)
      (void) piocmdout(INIT_CMD, NULL, 0, NULL);
return(0);
}
lineout(fileptr)  /***  Format a Line  ***/
FILE *fileptr;
{
int ch, charcount = 0;
for (ch = 0; ch < Indent; ch++)
          pioputchar(' ');
while ((ch=piogetc(fileptr)) != '\n' && ch != EOF
             && charcount < Pgwidth) {
           charcount++;
            pioputchar(c);
}
vpos += Vincr;
return(charcount);
}
passthru()  /*** Pass-through Option ***/
{
int ch;
while ((ch = piogetc(stdin)) != EOF)
           pioputchar(ch);
if (piodatasent && Do_formfeed)
          (void) piocmdout(FF_CMD, NULL, 0, NULL);
return(0);
}
restore() /*** Restore the Printer ***/
{
if (Restoreprinter)
       (void) piocmdout(REST_CMD, NULL, 0, NULL);
return(0);
}

打印格式化程序编译和链接

用编辑器创建一个名为 sample.imp 的导入文件。 该文件应该包含:

#!
main
piogetvals
piogetopt
piomsgout
pioexit
piomode
piodatasent
piopgskip
statusfile
piocmdout
piogetstr

使用编辑器来创建名为 sample.exp的导出文件。 该文件应该包含:

#!
setup
initialize
passthru
restore
lineout

输入以下内容来编译并链接格式化程序:

cc -o sample -bI:sample.imp -bE:sample.exp sample.c