/*
 * coorno - The incredible fortune cookie system hardcopy utility
 */

/* Version T1.00 - 18-Apr-87 - tmk - Initial portable version		*/
/* Version T1.01 - 02-Jul-87 - tmk - Add support for Turbo C		*/
/* Version T1.02 - 27-Nov-87 - tmk - Add function prototypes		*/
/* Version V1.20 - 24-Jul-88 - tmk - Official release version (finally)	*/

#define VERSION		"V1.20 24-Jul-88 - tmk"

/*
 * Define module ID for VMS (must go for Turbo C, else bogus error)
 */
#ifdef	vms
#ifdef	__DECC
#pragma module	cookie	VERSION
#else
#module	cookie	VERSION
#endif	/* __DECC */
#endif	/* vms */

/*
 * Convince Turbo C it's running under MS-DOS (it isn't sure)
 */
#ifdef	__TURBOC__
#define	MSDOS		1
#endif	/* __TURBOC__ */

/*
 * Grab some header files
 */
#include <stdio.h>

/*
 * Have a fight about function declarations
 */
#ifdef	decus
#define	void
#endif	/* decus */

/*
 * Define some constants
 */
#define	EOS	0
#define	TRUE	1

/*
 * Global variables
 */
char	line[513];

/*
 * For 'modern' compilers (all except decus), supply function prototypes
 */
#ifndef	decus
extern	int  main(int, char **);
extern	void process(FILE *);
#endif	/* decus */

/*
 * Actual code starts here
 */
int main(argc, argv)
int		argc;
char		*argv[];
{
	register int	i;
	register FILE	*fp;

#ifdef	MSDOS
	puts(".op\n.lm 1\n.rm 81\n.pl 66\n.mt 1\n.mb 1\n.po 0");
#else
	puts(".lm 0.rm 81.nf");
#endif	/* MSDOS */
	if (argc < 2) {
	    process(stdin);
	}
	else {
	    for (i = 1; i < argc; ++i) {
		if ((fp = fopen(argv[i], "r")) == NULL) {
		    perror(argv[i]);
		}
		else {
		    process(fp);
		    fclose(fp);
		}
	    }
	}
}

int percent = TRUE;

void process(fp)
FILE		*fp;			/* File pointer			*/
{
	register int	flag;
	register char	*tp;

	while (fgets(line, sizeof(line), fp) != NULL) {
	    if (percent && line[0] == '\n')
		continue;
	    flag = (line[0] == '%' && line[1] == '%');
	    if (flag && percent)
		continue;
	    if (flag)
#ifdef	MSDOS
		puts(".cp 6\n");
#else
		puts(".tp 6.s");
#endif	/* MSDOS */
	    else {
#ifdef	MSDOS
		for (tp = line; *tp != EOS; tp++) {
		    putchar(*tp);
		}
#else
		for (tp = line; *tp != EOS; tp++) {
		    switch (*tp) {
		    case '\\':
		    case '_':
		    case '^':
		    case '#':
		    case '%':
		    case '&':
			putchar('_');
		    default:
			putchar(*tp);
		    }
		}
#endif	/* MSDOS */
	    }
	    percent = flag;
	}
	if (!percent) {
#ifdef	MSDOS
	    puts(".cp 6\n");
#else
	    puts(".tp 6.s");
#endif	/* MSDOS */
	    percent = TRUE;
	}
}
