#include "bbs.h"
extern BYTE DeBuG;
extern int  HistorySelect;
extern UBYTE NumHistItems,WhichHist;
//(JOE) add linecount
extern int LineCount;
long DoHistory=0,LineHasChanged;

int LineInput(char *atstart, char *tohere, int maxlen, int time_allowed)
{
int c, x;
LineCount=0;
DoHistory=LineHasChanged=1;

x=strlen(atstart);
AEPutStr(atstart);
strcpy(tohere,atstart);
if(Whence_The_Logon==REMOTE_LOGON&&!(c=CheckCarrier()))
	{
	DoHistory=0;
	return(NO_CARRIER);
	}

FOREVER
	{
	  c=Check_Online_Status();
	  if(c<0) return(c);
     c=ReadChar((long)time_allowed);

	if(c==NO_CARRIER||c==TIMEOUT) { return(c); }
	if(c==HISTORY)
		{
		LineHasChanged=0;
		while(x)
			{
			AEPutStr("\b \b");
			--x;
			}
		stccpy(tohere,HistoryBuf+(HistorySelect*250),(maxlen<248?maxlen:248)+1);
		x=strlen(tohere);
		AEPutStr(tohere);
		}
	else
		{
          if(c=='\x9b') c='A';
		if(c!='\r')
			{
			LineHasChanged=1;
			}
		if(x<maxlen)
			{
			switch(c)
				{
				case '\r': /*  RETURN, IGNORE \n  */
					tohere[x]='\0';
					AEPutStr("\r\n");
					if(CaptureFP)
						{
						fprintf(CaptureFP,"%s\n",tohere);
						}
					if(DeBuG)
						{
						DebugOutput(tohere);
						}
					if(x>0&&LineHasChanged)
						{
						strcpy(HistoryBuf+(WhichHist*250),tohere);
						WhichHist++;
						if(WhichHist==MAXHIST)
							{
							WhichHist=0;
							}
						HistorySelect=WhichHist;
						if(NumHistItems<MAXHIST)
							{
							NumHistItems++;
							}
						/*printf("%s\n",tohere);*/
						}
					DoHistory=0;
					return(x);
				case '\b': /*  BACKSPACE && DELETE  */
				case '\177':
					if(x)
						{
						tohere[--x]='\0';
						AEPutStr("\b \b");
						}
					break;
				case '\30': /*  ^X DELETE LINE */
					while(x)
						{
						AEPutStr("\b \b");
						--x;
						}
					tohere[x]='\0';
					break;
				case '\11': /* TAB */
					if(x%5==0)
						{
						tohere[x++]=' ';
						SendChar(' ');
						}
					if(x<(maxlen-5))
						{
						while(x%5)
							{
							tohere[x++]=' ';
							SendChar(' ');
							}
						}
					break;
				case 2:
					NumHistItems=0;
					WhichHist=0;
					HistorySelect=0;
					break;
				default:
					if(c<' ') { break; } /* CONTROL FILTER */
					tohere[x++]=(char)c;
					SendChar(c);
					break;
				}
			}
		else
			{
			switch(c)
				{
				case '\r': /*  RETURNS  */
					tohere[x]='\0';
					AEPutStr("\r\n");
					if(CaptureFP)
						{
						fprintf(CaptureFP,"%s\n",tohere);
						}
					if(DeBuG)
						{
						DebugOutput(tohere);
						}
					if(x>0&&LineHasChanged)
						{
						strcpy(HistoryBuf+(WhichHist*250),tohere);
						WhichHist++;
						if(WhichHist==MAXHIST)
							{
							WhichHist=0;
							}
						HistorySelect=WhichHist;
						if(NumHistItems<MAXHIST)
							{
							NumHistItems++;
							}
						}
					DoHistory=0;
					return(x);
				case '\b': /*  BACKSPACES  */
				case '\177':
					tohere[--x]='\0';
					AEPutStr("\b \b");
					break;
				case '\30': /*  ^X  */
					while(x)
						{
						AEPutStr("\b \b");
						--x;
						}
					tohere[x]='\0';
					break;
				}
			}
		}
	}
return(SUCCESS);
}

#ifdef RTS_OLDSHIT
int LineInput(char *atstart, char *tohere, int maxlen, int time_allowed)
{
int c, x;
LineCount=0;
 DoHistory=LineHasChanged=1;

 x=strlen(atstart);
 AEPutStr(atstart);
 strcpy(tohere,atstart);

 if(Whence_The_Logon==REMOTE_LOGON&&!(c=CheckCarrier())) {
 	DoHistory=0;
	    return(NO_CARRIER);
	}

 FOREVER	{
 	c=Check_Online_Status();
	    if(c<0) return(c);
 	c=ReadChar((long)time_allowed);
	    if(c==NO_CARRIER||c==TIMEOUT)  return(c);
 	if(c==HISTORY)	{
 		LineHasChanged=0;
	    	while(x) {
 			AEPutStr("\b \b");
	    		--x;
			}
		    stccpy(tohere,HistoryBuf+(HistorySelect*250),(maxlen<248?maxlen:248)+1);
 		x=strlen(tohere);
	    	AEPutStr(tohere);
		} else	{
 		if(c!='\r')	{
 			LineHasChanged=1;
			}
	    	if(x<maxlen) {
 			switch(c) {
 				case '\r': /*  RETURN, IGNORE \n  */
	    				tohere[x]='\0';
		    			AEPutStr("\r\n");
			    		if(CaptureFP)
 						fprintf(CaptureFP,"%s\n",tohere);
 					if(DeBuG)
 						DebugOutput(tohere);
 					if(x>0&&LineHasChanged)	{
 						strcpy(HistoryBuf+(WhichHist*250),tohere);
	    					WhichHist++;
		    				if(WhichHist==MAXHIST)
 							WhichHist=0;
 						HistorySelect=WhichHist;
	    					if(NumHistItems<MAXHIST)
 							NumHistItems++;
	    					/*printf("%s\n",tohere);*/
						}
		    			DoHistory=0;
			    		return(x);
				    case '\b': /*  BACKSPACE && DELETE  */
				    case '\177':
					    if(x) {
 						tohere[--x]='\0';
	    					AEPutStr("\b \b");
						}
		    			break;
			    	case '\30': /*  ^X DELETE LINE */
				    	while(x) {
 						AEPutStr("\b \b");
	    					--x;
						}
		    			tohere[x]='\0';
			    		break;
				    case '\11': /* TAB */
					    if(x%5==0)	{
 						tohere[x++]=' ';
	    					SendChar(' ');
						}
		    			if(x<(maxlen-5)) {
 						while(x%5)	{
 							tohere[x++]=' ';
	    						SendChar(' ');
							}
						}
		    			break;
			    	case 2:
				    	NumHistItems=0;
					    WhichHist=0;
 					HistorySelect=0;
	    				break;
		    		default:
			    		if(c<' ') { break; } /* CONTROL FILTER */
				    	tohere[x++]=(char)c;
					    SendChar(c);
 					break;
				}
			} else {
 			switch(c) {
 				case '\r': /*  RETURNS  */
	    				tohere[x]='\0';
		    			AEPutStr("\r\n");
			    		if(CaptureFP)
						fprintf(CaptureFP,"%s\n",tohere);
 					if(DeBuG)
 						DebugOutput(tohere);
 					if(x>0&&LineHasChanged)	{
 						strcpy(HistoryBuf+(WhichHist*250),tohere);
	    					WhichHist++;
		    				if(WhichHist==MAXHIST)
 							WhichHist=0;
 						HistorySelect=WhichHist;
	    					if(NumHistItems<MAXHIST)
 							NumHistItems++;
						}
 					DoHistory=0;
	    				return(x);
		    		case '\b': /*  BACKSPACES  */
			    	case '\177':
				    	tohere[--x]='\0';
					    AEPutStr("\b \b");
 					break;
	    			case '\30': /*  ^X  */
		    			while(x) {
 						AEPutStr("\b \b");
	    					--x;
						}
		    			tohere[x]='\0';
			    		break;
				}
			}
		}
	}
 return(SUCCESS);
}
#endif
