/* $RCSfile: main.c,v $  $Revision: 2.0 $ */

/*  

Program: pcl2english

Author: Allen Norskog

Revision history:
	V1.x:           - (unreleased development versions)
	V2.0:   9/16/90 - First release 

This program released into the Public Domain.

*/

#include <stdio.h>
#include <string.h>
#include "gdefs.h"
#include "externals.h"
#include "protos.h"
FILE *fopen(),*ifile,*ofile;

#define ERR_USAGE		1
#define ERR_INFILE		2
#define ERR_OUTFILE		3

#define MODE_CHAR		0
#define MODE_ESC		1

extern int optind;
static char version[]="\npcl2english V2.0\n";

void init(void)
{
	opt_e = FALSE;
	opt_v = FALSE;
	opt_x = FALSE;
	first_col = current_col = 0;
	last_col = 79;
	num_lines = 0;
	raster_mode = NO_RASTER;
	raster_count = 0;
}

void main(int argc, char *argv[])
{
	int c;
	int done;


	init();
	/* Decode the options.  */

	while ((c = getopt (argc, argv, "evx")) != EOF) {
		switch (c) {
			case 'e':
				/* Print escape sequences only.  */
				opt_e = TRUE;
				break;
			case 'v':
				/* Print version number.  */
				opt_v = TRUE;
				break;
			case 'x':
				/* Print hexadecimal equivalents.  */
				opt_x = TRUE;
				break;
			case '?':
				/* Illegal option -- print usage message */
				err_exit(ERR_USAGE);
				break;
		}
	}
	if (optind > argc - 1) {
		if (opt_v) {
			printf("%s\n\n",version);
			exit(0);
		}
		else {
			err_exit(ERR_USAGE);
		}
	}
	ifile=fopen(argv[optind],"r");
	if (!ifile) {
		err_exit(ERR_INFILE);
	}
	if (optind <= argc - 2) {
		ofile=fopen(argv[optind + 1],"w");
		if (!ofile) {
			err_exit(ERR_OUTFILE);
		}
	}
	else ofile=stdout;
	if (opt_v) {
		fprintf(ofile, "%s\n\n",version);
	}
	mode = MODE_CHAR;
	done = FALSE;
	while (done == FALSE) {
		c = getc(ifile);
		if (c == EOF) {
			done = TRUE;
		}
		switch (mode) {
			case MODE_CHAR:
				switch (c) {
					case ESC:
						if (char_count > 0) {
							print_chars();  
							/* print_chars will make sure raster_mode 
							   is cleared */
						}
						else {
						}
						mode = MODE_ESC;
						esc_count = 0;
						esc_string[esc_count] = c;
						break;
					case FORM_FEED:
						print_chars();
						fprintf(ofile,
							"\n<Form feed>  Move to first line at top of next page\n");
						if (opt_x) {
							fprintf(ofile,"0C");
						}
						else {
							fprintf(ofile,"  ");
						}
						fprintf(ofile,
							"           while maintaining current column ");
						fprintf(ofile,"at %d.\n", current_col);
						break;
					case SHIFT_IN:
						print_chars();
						fprintf(ofile,
							"\n<Shift In>  Select characters that follow from the\n");
						if (opt_x) {
							fprintf(ofile,"0F");
						}
						else {
							fprintf(ofile,"  ");
						}
						fprintf(ofile,"          current primary font.\n");
						break;
					case SHIFT_OUT:
						print_chars();
						fprintf(ofile,
							"\n<Shift Out> Select characters that follow from the\n");
						if (opt_x) {
							fprintf(ofile,"0E");
						}
						else {
							fprintf(ofile,"  ");
						}
						fprintf(ofile,"          current secondary font.\n");
						break;
					case EOF:
						print_chars();
						fprintf(ofile,"\n");
						break;
					case CARRIAGE_RETURN:
						current_col = first_col;
						add_char(c);
						break;
					case LINE_FEED:
						num_lines++;
						add_char(c);
						break;
					case BACK_SPACE:
						if (current_col > first_col) {
							current_col--;
						}
						add_char(c);
						break;
					case TAB:
						{
							int i;
							i = (current_col - first_col) / 8;
							current_col = (i+1)*8 + first_col;
							if (current_col > last_col) {
								current_col = last_col;
							}
							add_char(c);
						}
						break;
					default:
						add_char(c);
						if (((c >= 32) && (c <= 127)) ||
							((c >= 161) && (c <= 254))) {
							/* Ignore non-printing characters in
								Roman-8 character set */
							if (current_col < last_col) {
								current_col++;
							}
						}
						break;
				}
				break;
			case MODE_ESC:
				if (c == EOF) {
					fprintf(ofile,
						"\nEOF reached before completing last escape sequence.\n");
				}
				else {
					esc_count++;
					esc_string[esc_count] = c;
					if (((esc_count == 1) && (c >= '0') && (c <= '~')) ||
						/* A two character escape string */
						((c >= '@') && (c <= 'Z'))) {	
						/* Look for terminating character.
							@ is just before A */

							esc_string[esc_count + 1] = 0;
						if (raster_mode == NO_RASTER) {
							print_esc_string();
						}
						else {
							if (strncmp(esc_string, raster_header, 
								strlen(raster_header)) != 0) {

								print_extra_rasters();
								raster_mode = NO_RASTER;
								print_esc_string();
							}
						}
						decode_esc_string();
						mode = MODE_CHAR;
						char_count = 0;
					}
				}
				break;
			default:
				break;
		}
	}
	fclose(ifile);
	fclose(ofile);
	exit(0);
}
