/*
 * $Header: /smsa/bsd:usr.lib/lpr/filters/psfilter.c:bsd  1.1  $
 * $Source: /smsa/bsd:usr.lib/lpr/filters/psfilter.c: $
 *
 * Copyright (c) 1990 Acorn Computers Ltd., Cambridge, England
 *
 * $Log:	psfilter.c,v $
 * Revision 1.1  90/06/04  09:12:53  dave
 * Initial revision
 * 
 */

#if defined(LIB_RCS) && !defined(lint)
static char RcsId[] = "$Header: /smsa/bsd:usr.lib/lpr/filters/psfilter.c:bsd  1.1  $";
#endif

#define point 10
#define chrwid ((point*3)/5)
#define physhyt (797-point)	/* got from the LaserWriter clip path */
#define physwid (560-20)	/* but it misses right side of A4 paper */
#define MAXLINE 200

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <signal.h>
init()
{
  printf("%%!\n/Courier findfont %d scalefont setfont", point);
  initpage();
}

windup()
{
  putchar('\004');
  putchar('\n');		/* poxy filing system */
}


int tabspace = 8;
int line = 0, page = 0;
int inshow = 0, col = 0;
char tabstops[MAXLINE];
int pwidth = 86;		/* default line length */
int plength = 66;		/* page length */
int indent;			/* indentation length */
int literal;			/* print control characters */
char *name;			/* user's login name */
char *host;			/* user's machine name */
char *acctfile;			/* accounting information file */
int wide, length, width, paghyt, pagwid;

initpage()
{
  if (wide) puts(" 20 830 translate -90 rotate");
  else puts(" 20 20 translate");
}

main(argc, argv)
  int argc;
  char **argv;
{
  int c;
  register int i;
  register char *cp;

  while (--argc)
  {
    if (*(cp = *++argv) == '-')
    {
      switch (cp[1])
      {
      case 'n':
	argc--;
	name = *++argv;
	break;

      case 'h':
	argc--;
	host = *++argv;
	break;

      case 'w':
	if ((i = atoi(&cp[2])) > 0 && i <= MAXLINE)
	  pwidth = i;
	break;

      case 'l':
	plength = atoi(&cp[2]);
	break;

      case 'i':
	indent = atoi(&cp[2]);
	break;

      case 'c':		/* Print control chars */
	literal++;
	break;
      }
    }
    else
      acctfile = cp;
  }

  if (pwidth <= 86)
  {
    pagwid = physwid;
    paghyt = physhyt;
    wide = 0;
  }
  else
  {
    pagwid = physhyt;
    paghyt = physwid;
    wide = 1;
  }
  length = (paghyt/point-5);
  width = (pagwid/chrwid);
  settabs(tabstops);
  col = 1;
  line = 1;
  page = 0;

/*
  printf("%%!\n/Courier findfont dup setfont (%s) stringwidth", name);
  printf(" 750 exch sub 2 div 50 exch moveto 500 exch div");
  printf(" scalefont setfont (%s) show showpage\n",name); 
*/

  c = getchar();
  if (c == '%')
  {
    c = getchar();
    if (c == '!')
    {
      putchar('%');
      putchar('!');
      while ((c = getchar()) != EOF)
	putchar(c);
      windup('\004');
      exit(0);
    }
    else
    {
      init();
      process('%');
    }
  }
  else
    init();

  while (c != EOF)
  {
    process(c);
    c = getchar();
  }
  if (inshow || line != 1)
    newpage();
  windup('\004');
  exit(0);
}

process(c)
{
  switch (c)
  {

  case '\t':
    do
      translate(' ');
    while (!tabpos(col, tabstops));
    break;
  case '\n':
    newline();
    break;
  case '\f':
    newpage();
    break;
  case '\b':
    endshow();
    if (col)
      col -= 1;
    break;
  default:
    translate(c);
  }
}

translate(c)
  int c;
{
  if (!inshow)
  {
    if (c == ' ')
    {
      ++col;
      return;
    }
    printf(" %d %d moveto\n(", 30 + col * chrwid, paghyt - point * (line + 1));
    inshow = 1;
  }
  if (c == '\\' || c == '(' || c == ')')
    putchar('\\');
  putchar(c);
  ++col;
/*
  if (col == width + 1) newline();
*/
}

endshow()
{
  if (inshow)
    printf(") show\n");
  inshow = 0;
}


newline()
{
  endshow();
  line += 1;
  if (line == length + 1)
    newpage();
  col = 1;
}

newpage()
{
  endshow();
  page += 1;
/*
  printf(" %d %d moveto (Page %d) show showpage",
	 pagwid / 2, paghyt - point*(length + 3), page );
*/
  printf("showpage");
  line = 1;
  col = 1;
  initpage();
}


tabpos(col, tabstops)
  int col;
  char tabstops[];

{
  if (col > MAXLINE)
    return 1;
  else
    return tabstops[col];
}

settabs(tabstops)
  char tabstops[];

{
  int i;

  for (i = 1; i <= MAXLINE; i++)
    tabstops[i] = ((i % tabspace) == 1);
}
