*****Listing 2*****

/*
 *	Bold.c - filters nroff output for terminal display
 *		 displays bold in SO, underlines, expanded font
 *	copyright 1987 Ronald Florence
 */

#include	<stdio.h>

#define UL		01
#define BOLD		02
#define ULSTOP		04
#define Bold()		tputs(so, 1, prch), att |= BOLD
#define Stopbold()	tputs(se, 1, prch), att &= ~BOLD
#define Uline()		tputs(us, 1, prch), att |= UL
#define Stopuline()	tputs(ue, 1, prch), att &= ~(UL|ULSTOP)

prch(c)
     register char c;
{
  putchar(c);
}

char	*so, *se, *us, *ue;

main()
{
  static  char	sbuf[256]; 
  char	tbuf[1024], *fill = sbuf, *tgetstr(), *getenv();
  register	a, c;
  int	i, att = 0;

  if (tgetent(tbuf, getenv("TERM")) == 1 && tgetnum("sg") < 1) 
    {
      so = tgetstr("so", &fill);
      se = tgetstr("se", &fill);
      us = tgetstr("us", &fill);
      ue = tgetstr("ue", &fill);
    }
  a = getchar();
  while ((c = getchar()) != EOF) 
    {
      if (a == '_') 
	{
	  if (c == '_' && (att & UL) == 0) 
	    Uline();
	  else if (c == '\b')	/* nroff italics */ 
	    {		
	      if ((a = getchar()) == EOF)
		a = 0;
	      c = getchar();
	      if ((att & UL) == 0)
		Uline();
	    }
	  if (c != '_' && (att & UL)) 
				/* c is the last underline */
	    att |= ULSTOP;
	}
      else if (c == '\b')
	{
	  if ((c = getchar()) != a) 
	    {			/* Not a bold: print the character
				   and pass the \b to be printed. */
	      putout(a);
	      a = '\b';
	    }
	  else 
	    {
	      if ((att & BOLD) == 0) 
		Bold();
	      for (i = 0; i < 5; i++)
		if ((c = getchar()) != a && c != '\b') 
		  break;
	    }
	}
      else if (att & BOLD) 
	Stopbold();
      putout(a);
      if (att & ULSTOP) 
	Stopuline();
      a = c;
    }
}


putout(c)
     register	char	c;
{
  static int	expanded;

  if (c == 07)			/* ^G signals expanded font */
    {
      expanded++;
      return(0);
    }
  putchar(c);
  if (expanded) 
    {
      if (c == '\n')
	expanded = 0;
      else
	putchar(' ');
    }
}
