/*	PF -- printer filter

Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000 Ogochan .

This module is part of PF.

	PF is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
to anyone for the consequences of using it or for whether it serves
any particular purpose or works at all, unless he says so in writing.
Refer to the GNU General Public License for full details. 

	Everyone is granted permission to copy, modify and redistribute
MACE, but only under the conditions described in the GNU General
Public License.  A copy of this license is supposed to have been given
to you along with PF so you can know your rights and
responsibilities.  It should be in a file named COPYING.  Among other
things, the copyright notice and this notice must be preserved on all
copies. 
*/
/*
#define		TRACE
*/

#define		MAIN
#include	<stdio.h>
#include	<string.h>
#include	<time.h>
#include	<stdlib.h>
#include	"types.h"
#include	"pf.h"
#include	"i18n-char/code.h"
#include	"debug.h"

static	void
MainPrint(
	PrinterControl	*p)
{
	Char	ch;
	Bool	fPrint;

dbgmsg(">MainPrint");
	InitPrinter(p);
	CodeSetEncode(stInput,InputEncode);

	fPrint = FALSE;
	while	(  ( ch = CodeGetChar(stInput) )  !=  Char_EOF  )	{
		CheckLayout(p);
		if		(  IsCTRL(ch)  )	{
			switch	( ch )	{
			  case	Char_TAB:
				DoTab(p);
				break;
			  case	Char_LF:
				NewLine(p);
				break;
			  case	Char_FF:
				NewLine(p);
				NewPage(p,FALSE);
				break;
			  default:
			  	DoPrint(p,ch);
				break;
			}
		} else {
			DoPrint(p,ch);
			fPrint = TRUE;
		}
	}
	if		(  fPrint  )	{
		NewPage(p,TRUE);
	}
dbgmsg("<MainPrint");
}

static	void
PrintHelp(void)
{
	int		i;

	fprintf(stderr,"PF a print filter\n");
	fprintf(stderr,"Ver %s\n",VERSION);
	fprintf(stderr,"pf <parameter> <filename>\n");
	fprintf(stderr,"parameters\n");

	fprintf(stderr,"-h? :paper length(lines).    default is 66.\n");
	fprintf(stderr,"-H? :paper length(dot).      default is 1980.\n");
	fprintf(stderr,"-w? :paper width(chars).     default is 90.\n");
	fprintf(stderr,"-W? :paper width(dot).       default is 1620.\n");

	fprintf(stderr,"-l? :line width(in chars).   default is 80.\n");
	fprintf(stderr,"-L? :line width.             default is 1440.\n");
	fprintf(stderr,"-N? :print lines in page.    default is 50.\n");

	fprintf(stderr,"-C? :line pitch.             default is 30, means 6LPI\n");
	fprintf(stderr,"-A? :pitch of SBCS.          default is 18.\n");
	fprintf(stderr,"-Q? :pitch of DBCS.          default is 36.\n");
	fprintf(stderr,"\tpitch\'s unit is 1/180 inch.\n");

	fprintf(stderr,"-M? :left margine(chars).    default is 0.\n");
	fprintf(stderr,"-U? :top margine(lines).     default is 0\n");

	fprintf(stderr,"-D? :printer type\n");
	for	( i = 0 ; Printers[i]->name  !=  NULL ; i ++ ) {
		fprintf(stderr," %s:",Printers[i]->name);
		fprintf(stderr,"parameter(s)\n");
		Printers[i]->Help();
	}
	fprintf(stderr,"\n");
	fprintf(stderr,"-P  :print page header.\n");
	fprintf(stderr,"-I? :input multi-byte character set\n");
	fprintf(stderr,"\tSJIS EUC JIS NONE\n");
	fprintf(stderr,"-T? :tab width. default is 0.\n");
	fprintf(stderr,"default value means 9*11 inch paper.\n");
}

static	int		LinePitch
		,		PitchSBCS
		,		PitchDBCS
		,		PageLength
		,		PageWidth
		,		PageLines
		,		LineLength
		,		TopMargineInLines
		,		LeftMargine
		,		TabWidth;
static	Bool	fPrintPageHeader;

static	void
SetDefault()
{
	
	InputEncode = ENCODE_NULL;
	CodeInit(InputEncode);

	stInput = STDIN;
	fnInput = "(stdin)";
	PageLength = 1980;
	LineLength = 1440;
	PageLines = 50;
	LinePitch  = 30;
	PitchSBCS = 18;
	PitchDBCS = 36;
	TopMargineInLines = 0;
	LeftMargine = 0;
	TabWidth = 0;
	fPrintPageHeader = FALSE;
}
 
extern	int
main(
	int		argc,
	char	*argv[])
{	char	*p
	,		*printer;
	int		i
	,		LineLengthInChars
	,		PageWidthInChars
	,		PageLengthInLines;

	PrinterControl	*ctrl;

	printer = "ESCP:D";

	SetDefault(); 
	LineLengthInChars = 0;
	PageWidthInChars = 0;
	PageLengthInLines = 0;
	for	( i = 1 ; i < argc ; i ++ )	{
		p = argv[i];
		if		(  *p  ==  '-'  )	{
			switch  (p[1])	{
			  case	'h':
				PageLengthInLines = atoi(p+2);
				break;
			  case	'H':
				PageLength = atoi(p+2);
				break;
			  case	'w':
				PageWidthInChars = atoi(p+2);
				break;
			  case	'W':
				PageWidth = atoi(p+2);
				break;
			  case	'C':
				LinePitch = atoi(p+2);
				break;
			  case	'A':
				PitchSBCS = atoi(p+2);
				break;
			  case	'Q':
				PitchDBCS = atoi(p+2);
				break;
			  case	'N':
				PageLines = atoi(p+2);
				break;
			  case	'l':
				LineLengthInChars = atoi(p+2);
				break;
			  case	'L':
				LineLength = atoi(p+2);
				break;
			  case	'T':
				TabWidth = atoi(p+2);
				break;
			  case	'P':
				fPrintPageHeader = TRUE;
				break;
			  case	'D':
				printer = p + 2;
				break;
			  case	'I':
				if	(  !strcmp(p+2,"SJIS")  )	{
					InputEncode = ENCODE_SJIS;
				} else
				if	(  !strcmp(p+2,"EUC")  )	{
					InputEncode = ENCODE_EUC;
				} else
				if	(  !strcmp(p+2,"JIS")  )	{
					InputEncode = ENCODE_ISO2022;
				} else
				if	(  !strcmp(p+2,"ISO-2022")  )	{
					InputEncode = ENCODE_ISO2022;
				} else {
					InputEncode = ENCODE_NULL;
				}
				break;
			  case	'U':
				TopMargineInLines = atoi(p+2);
				break;
			  case	'M':
				LeftMargine = atoi(p+2);
				break;
			  case	'?':
				PrintHelp();
				exit(0);
			  default:
				break;
			}
		} else {
			stInput = CodeOpenFile(p,"r");
			fnInput = p;
		}
	}
	if		(  LineLengthInChars  >  0  ) {
		LineLength = LineLengthInChars * PitchSBCS;
	}
	if		(  PageWidthInChars  >  0  ) {
		PageWidth = PageWidthInChars * PitchSBCS;
	}
	if		(  PageLengthInLines  >  0  ) {
		PageLength = PageLengthInLines * LinePitch;
	}

	ctrl = SetPrinterType(printer,LinePitch,PitchSBCS,PitchDBCS,
						  PageLength,PageWidth,PageLines,LineLength,
						  TopMargineInLines,LeftMargine,
						  TabWidth,
						  fPrintPageHeader);

	MainPrint(ctrl);
	return	(0);
}
