#include"del_prn.h"
#include<jctype.h>

enum PRNERR	prn_putchar(unsigned c)
{
register enum	PRNERR	temp = PRNERR_OK;

	if	(c<256)
	{
		if	(c=='\n')
			return prn_linefeed(30);
		else if	(iscntrl(c))
		{		/* 改行以外のコントロールコードは素通し */
			if	(prn_writebyte(c))
				return PRNERR_TIMEOUT;
			else
				return PRNERR_OK;
		}

		if	(Prn_mode == PRNMODE_ESCP)
		{
			if	(Prn_oldtype_ != CT_ANK)
			{
			static	char	code[] = {0x1c,0x0f};

				temp = prn_putcode(2,code);
				if	(temp)
					return temp;
			}

			if	(Prn_istategaki)
			{
				if	(prn_writebyte(0x1c) ||
							prn_writebyte('K'))
					return PRNERR_TIMEOUT;
			}

			c = Prn_chartable[c];

			if	(prn_writebyte(c>>8) || prn_writebyte(c & 255))
				return PRNERR_TIMEOUT;

			if	(Prn_istategaki)
			{
				
				if	(prn_writebyte(0x1c) ||
							prn_writebyte('J'))
					return PRNERR_TIMEOUT;
			}
		}
		else if	(Prn_mode==PRNMODE_MSX || Prn_mode==PRNMODE_PCPR)
		{
			if	(prn_writebyte('\0') || prn_writebyte(c & 255))
				return PRNERR_TIMEOUT;
		}
		Prn_oldtype_ = CT_ANK;
	}
	else
	{
		if	(c>=0x8140)
			c = mstojis(c);

		if	(Prn_mode == PRNMODE_ESCP)
		{
			if	(Prn_oldtype_ != CT_KJ1)
			{
			static	char	code[] = {0x1c,0x12};

				temp = prn_putcode(2,code);
				if	(temp)
					return temp;
			}

			if	(prn_writebyte(c>>8) || prn_writebyte(c & 255))
				return PRNERR_TIMEOUT;
		}
		else if	(Prn_mode == PRNMODE_MSX || Prn_mode==PRNMODE_PCPR)
		{
			if	(prn_writebyte(c>>8) || prn_writebyte(c & 255))
				return PRNERR_TIMEOUT;
		}
		Prn_oldtype_ = CT_KJ1;
	}

	Prn_islinestart_ = 0;

	if	(Prn_oldtype_==CT_KJ1)
		Prn_carridge += 27*Prn_ishdouble;
	else
	{
	static	int	hosei = 0;

		Prn_carridge += (13+hosei)*Prn_ishdouble;

		hosei ^= 1;		/* 0←→1 */
	}

	return PRNERR_OK;
}
