/**********************************************************************/
/*                                                                    */
/* Proportionaldruckertreiber für alle 24-Nadler mit ESC/P Kommandos  */
/*                                                                    */
/*        V1.3 geschrieben am 15/01/89 von Martin Silbernagl          */
/*                                                                    */
/*                   (c) 1988 by Martin Silbernagl                    */
/*                                                                    */
/**********************************************************************/

#include <functions.h>			/* hab Aztec V3.6a benutzt */
#include	<libraries/dos.h>
#define	esc 27					/* der Übersicht wegen	 	*/
#define	NUM_OF_COMS	29			/* Anzahl der Kommandos		*/

USHORT	*file_in,*file_out;	/* file-pointer in/out		*/
USHORT	i;							/*	Zählvariable				*/
UBYTE		mychar,lastchar;		/* Lesebuffer					*/
UBYTE		what;						/* Lesebuffer					*/
UBYTE		ed_file,show;			/* Flags							*/

UBYTE	Initstring[] =				/* Drucker-Initalisierung	*/
{
	esc,'@',	esc,'x',1,	esc,'p',1,	esc,'a',3,	esc,'t',1
};

UBYTE Coms[] =						/* Kommandobuchstaben		*/
{
	'U','u',							/* Unterstreichen	ein/aus	*/
	'F','f',							/* Fettdruck		ein/aus	*/
	'D','d',							/* Doppeldruck		ein/aus	*/
	'H','h',							/* Hochstellung	ein/aus	*/
	'T','t',							/* Tiefstellung	ein/aus	*/
	'S','s',							/* Schmaldruck		ein/aus	*/
	'B','b',							/* Breitdruck		ein/aus	*/
	'K','k',							/* Kursivdruck		ein/aus	*/
	'P','p',							/* Proportional	ein/aus	*/
	'0','1',							/* linksbündig / mittig		*/
	'2','3',							/* rechtsbündig / block		*/
	'A','4',							/* 3 bzw. 4 Lines per Inch	*/
	'6','8',							/* 6 bzw. 8 Lines per Inch	*/
	'!','@',							/* Tabulator / Seitenende	*/
	'\n'								/*	Return						*/
};

UBYTE Seq[] =						/* 3 Byte Kommandos			*/
{
   esc,'-',1,           esc,'-',0,
   esc,'E',NULL,        esc,'F',NULL,
   esc,'G',NULL,        esc,'H',NULL,
   esc,'S',0,           esc,'T',NULL,
   esc,'S',1,           esc,'T',NULL,
   esc,15,NULL,         18,NULL,NULL,
   esc,'W',1,           esc,'W',0,
   esc,'4',NULL,        esc,'5',NULL,
   esc,'p',1,           esc,'p',0,
   esc,'a',0,           esc,'a',1,
   esc,'a',2,           esc,'a',3,
	esc,'3',60,				esc,'3',45,
   esc,'0',NULL,        esc,'2',NULL,
   9,NULL,NULL,			12,NULL,NULL,
	10,0,0
};

UBYTE LeftBorder[] =				/* setzt linken Rand			*/
{
	esc,'l',0
};

UBYTE RightBorder[] =			/* setzt rechten Rand		*/
{
	esc,'Q',0
};

UBYTE	Translation[] =
{
	  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15,
	 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 
	 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
	 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
	 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
	 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
	 96, 97, 98, 99,100,101,102,103,104,105,106,107,108,109,110,111,
	112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,176,
	  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
	  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
	 32,173,155,156,249,157,124, 21, 34, 99,166,174,170,196, 82,169,
	248,241,253,252,251,230,227,250, 44,254,167,175,172,171, 32,168,
	133,160,131, 32,142,143,146,128,138,144,136,137,141,161,140,139,
	 32,165,149,162,147, 32,153,  0, 48,151,163,150,154, 32, 32,225,
	133,160,131, 32,132,134,145,135,138,130,136,137,141,161,140,139,
	 32,164,149,162,147, 32,148,  0, 32,151,163,150,129, 32, 32,152
};


main(argc,argv)
int argc;
char *argv[];
{
   if(argc<2)						/* keine Argumente ?			*/	
   {
      info:
      printf("Use: 1> ");
      printf(argv[0]);
      puts(" inputfile [EDITOR] [SHOW]");
      exit(RETURN_FAIL);
   }
	puts("(c) 15/01/1989 by Martin Silbernagl\n");
   ed_file=FALSE;					/* Fließtext					*/
   show=FALSE;						/* nicht mitschreiben		*/
   for(i=2; i<argc; i++)
   {
      if(strcmp("EDITOR",argv[i])==0) ed_file=TRUE;
      else if(strcmp("SHOW",argv[i])==0) show=TRUE;
      else
      {
         puts("bad args!");
         goto info;
      }
   }
   if((file_in=Open(argv[1],MODE_OLDFILE))==NULL)	/* Input öffnen	*/
   {
      puts("Unable to open input file");
      exit(ERROR_OBJECT_NOT_FOUND);
   }
   if((file_out=Open("par:",MODE_NEWFILE))==NULL)	/* Drucker öffnen	*/
   {
      puts("Unable to open PAR: for output");
      exit(ERROR_OBJECT_NOT_FOUND);
   }
	Write(file_out,Initstring,14L);			/* Drucker intialisieren 	*/
   while(Read(file_in,&mychar,1L)!=0) 		/* ein Zeichen holen 		*/
   {
      if(ed_file) TestRet();					/* Fließtext ?					*/
      if(mychar=='`') DoThings();			/* Kommando ?					*/
      if(show) printf("%c",mychar);			/* Mitschreiben 				*/
		mychar=Translation[mychar];			/* in ASCII umwandeln		*/
      Write(file_out,&mychar,1L);			/* Zeichen ausgeben			*/
   }

   Close(file_in);
   Close(file_out);
   return(RETURN_OK);
}

TestRet()			/*	Return? */
{
   if(mychar==10) mychar=0;
   return(0L);
}

DoThings()			/*	Kommandos ausgeben	*/
{
   Read(file_in,&what,1L);
   if(what=='`') return(0L);

   for(i=0; i<NUM_OF_COMS; i++)
   {
      if(what==Coms[i])
      {
         Write(file_out,&Seq[i*3],3L);
			goto end_do;
      }
   }

	if(what=='L' || what=='l')
	{
		LeftBorder[2]=GetBorder();
		Write(file_out,LeftBorder,3L);
		goto end_do;
	}
	if(what=='R' || what=='r')
	{
		RightBorder[2]=GetBorder();
		Write(file_out,RightBorder,3L);
		goto end_do;
	}

end_do:
   mychar=0;
   return(0L);
}

GetBorder()			/* Zweistellige Ziffernfolge umwandeln */
{
	UBYTE	buffer;

	Read(file_in,&what,1L);
	if(what<'0' || what >'9') return(0);
	what-='0';
	what*=10;
	buffer=what;
	Read(file_in,&what,1L);
	if(what<'0' || what >'9') return(0);
	what-='0';
	what+=buffer;

	return(what);
}
                                        
