Skip to content
Snippets Groups Projects
getstr.cpp 15.7 KiB
Newer Older
/* getstr.cpp */

/* Synchronet string input routines */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
 * Copyright 2015 Rob Swindell - http://www.synchro.net/copyright.html		*
 *																			*
 * This program is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU General Public License				*
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
 * See the GNU General Public License for more details: gpl.txt or			*
 * http://www.fsf.org/copyleft/gpl.html										*
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"

/****************************************************************************/
/* Waits for remote or local user to input a CR terminated string. 'length' */
/* is the maximum number of characters that getstr will allow the user to   */
/* input into the string. 'mode' specifies upper case characters are echoed */
/* or wordwrap or if in message input (^A sequences allowed). ^W backspaces */
/* a word, ^X backspaces a line, ^Gs, BSs, TABs are processed, LFs ignored. */
/* ^N non-destructive BS, ^V center line. Valid keys are echoed.            */
/****************************************************************************/
size_t sbbs_t::getstr(char *strout, size_t maxlen, long mode)
{
    size_t	i,l,x,z;    /* i=current position, l=length, j=printed chars */
                    /* x&z=misc */
	char	str1[256],str2[256],undo[256];
	console&=~(CON_UPARROW|CON_DOWNARROW|CON_LEFTARROW|CON_BACKSPACE|CON_DELETELINE);
	if(!(mode&K_WRAP))
		console&=~CON_INSERT;
	sys_status&=~SS_ABORT;
	if(mode&K_LINE && term_supports(ANSI) && !(mode&K_NOECHO)) {
		if(column + (long)maxlen >= cols)	/* Don't cause the terminal to line-wrap, just shorten the max input string length instead */
			maxlen = cols-column-1;
		attr(cfg.color[clr_inputline]);
		for(i=0;i<maxlen;i++)
	if(wordwrap[0]) {
	else str1[0]=0;
	if(mode&K_EDIT)
		strcat(str1,strout);
	else
		strout[0]=0;
	if(strlen(str1)>maxlen)
		str1[maxlen]=0;
	atr=curatr;
	if(!(mode&K_NOECHO)) {
		if(mode&K_AUTODEL && str1[0]) {
			i=(cfg.color[clr_inputline]&0x77)<<4;
			i|=(cfg.color[clr_inputline]&0x77)>>4;
		column+=rputs(str1);
		if(mode&K_EDIT && !(mode&(K_LINE|K_AUTODEL)))
	i=l=strlen(str1);
	if(mode&K_AUTODEL && str1[0] && !(mode&K_NOECHO)) {
		ch=getkey(mode|K_GETSTR);
		attr(atr);
		if(isprint(ch) || ch==DEL) {
			for(i=0;i<l;i++)
		else {
			for(i=0;i<l;i++)
				outchar(BS);
			column+=rputs(str1);
		if(ch!=' ' && ch!=TAB)
	if(mode&K_USEOFFSET) {
		i=getstr_offset;
		if(i>l)
			i=l;
		if(l-i) {
	if(console&CON_INSERT && term_supports(ANSI) && !(mode&K_NOECHO))
	while(!(sys_status&SS_ABORT) && online && input_thread_running) {
			&& console&(CON_LEFTARROW|CON_BACKSPACE|CON_DELETELINE))
			break;
		if(console&CON_UPARROW)
			break;
		if((ch=getkey(mode|K_GETSTR))==CR)
		if(sys_status&SS_ABORT || !online)
		if(ch==LF && mode&K_MSG) { /* Down-arrow same as CR */
			console|=CON_DOWNARROW;
		if(ch==TAB && (mode&K_TAB || !(mode&K_WRAP)))	/* TAB same as CR */
			break;
		if(!i && mode&K_UPRLWR && (ch==' ' || ch==TAB))
			continue;	/* ignore beginning white space if upper/lower */
		if(mode&K_E71DETECT && (uchar)ch==(CR|0x80) && l>1) {
			if(strstr(str1,"")) {
				bputs("\r\n\r\nYou must set your terminal to NO PARITY, "
					"8 DATA BITS, and 1 STOP BIT (N-8-1).\7\r\n");
deuce's avatar
deuce committed
			}
			case CTRL_A: /* Ctrl-A for ANSI */
				if(!(mode&K_MSG) || useron.rest&FLAG('A') || i>maxlen-3)
					break;
				if(console&CON_INSERT) {
					if(l<maxlen)
						l++;
					for(x=l;x>i;x--)
						str1[x]=str1[x-1];
					if(i==maxlen-1)
						console&=~CON_INSERT; 
#endif
				outchar(str1[i++]=1);
				break;
			case CTRL_B: /* Ctrl-B Beginning of Line */
			case CTRL_D: /* Ctrl-D Delete word right */
					while(x<l && str1[x]!=' ') {
						outchar(' ');
					while(x<l && str1[x]==' ') {
						outchar(' ');
					cursor_left(x-i);   /* move cursor back */
					z=i;
					while(z<l-(x-i))  {             /* move chars in string */
						outchar(str1[z]=str1[z+(x-i)]);
					while(z<l) {                    /* write over extra chars */
			case CTRL_E: /* Ctrl-E End of line */
					cursor_right(l-i);  /* move cursor to eol */
			case CTRL_F: /* Ctrl-F move cursor forewards */
					cursor_right();   /* move cursor right one */
			case CTRL_G: /* Bell */
				if(!(mode&K_MSG))
					break;
				if(useron.rest&FLAG('B')) {
					if (i+6<maxlen) {
						if(console&CON_INSERT) {
							for(x=l+6;x>i;x--)
								str1[x]=str1[x-6];
							if(l+5<maxlen)
								l+=6;
							if(i==maxlen-1)
								console&=~CON_INSERT; 
#endif
						str1[i++]='(';
						str1[i++]='b';
						str1[i++]='e';
						str1[i++]='e';
						str1[i++]='p';
						str1[i++]=')';
						if(!(mode&K_NOECHO))
						redrwstr(str1,i,l,0);
				if(console&CON_INSERT) {
					if(l<maxlen)
						l++;
					for(x=l;x>i;x--)
						str1[x]=str1[x-1];
					if(i==maxlen-1)
						console&=~CON_INSERT; 
#endif
					if(!(mode&K_NOECHO))
			case CTRL_H:	/* Ctrl-H/Backspace */
					console|=CON_BACKSPACE;
				i--;
				l--;
				if(i!=l) {              /* Deleting char in middle of line */
					outchar(BS);
					z=i;
					while(z<l)  {       /* move the characters in the line */
						outchar(str1[z]=str1[z+1]);
					outchar(' ');        /* write over the last char */
				else if(!(mode&K_NOECHO))
			case CTRL_I:	/* Ctrl-I/TAB */
					if(console&CON_INSERT) {
						if(l<maxlen)
							l++;
						for(x=l;x>i;x--)
							str1[x]=str1[x-1];
						if(i==maxlen-1)
							console&=~CON_INSERT; 
#endif
					if(!(mode&K_NOECHO))
				while(i<maxlen && i%EDIT_TABSIZE) {
					if(console&CON_INSERT) {
						if(l<maxlen)
							l++;
						for(x=l;x>i;x--)
							str1[x]=str1[x-1];
						if(i==maxlen-1)
							console&=~CON_INSERT; 
#endif
					if(!(mode&K_NOECHO))
				if(console&CON_INSERT && !(mode&K_NOECHO))
					redrwstr(str1,i,l,0);
				break;

			case CTRL_L:    /* Ctrl-L   Center line (used to be Ctrl-V) */
				str1[l]=0;
				l=bstrlen(str1);
				if(!l) break;
				for(x=0;x<(maxlen-l)/2;x++)
				str2[x]=0;
				strcat(str2,str1);
				strcpy(strout,str2);
				l=strlen(strout);
				if(mode&K_NOECHO)
					return(l);
				if(mode&K_MSG)
					redrwstr(strout,i,l,K_MSG);
				else {
					while(i--)
						bputs("\b");
					bputs(strout);
					if(mode&K_LINE)
			case CTRL_N:    /* Ctrl-N Next word */
					while(str1[i]!=' ' && i<l)
					while(str1[i]==' ' && i<l)
			case CTRL_R:    /* Ctrl-R Redraw Line */
				if(!(mode&K_NOECHO))
					redrwstr(str1,i,l,0);
				break;
			case CTRL_V:	/* Ctrl-V			Toggles Insert/Overwrite */
			case CTRL_W:    /* Ctrl-W   Delete word left */
				if(i<l) {
					x=i;                            /* x=original offset */
					while(i && str1[i-1]==' ') {
					while(i && str1[i-1]!=' ') {
					z=i;                            /* i=z=new offset */
					while(z<l-(x-i))  {             /* move chars in string */
						outchar(str1[z]=str1[z+(x-i)]);
					while(z<l) {                    /* write over extra chars */
					cursor_left(z-i);				/* back to new x corridnant */
					while(i && str1[i-1]==' ') {
						i--;
						l--;
						if(!(mode&K_NOECHO))
					while(i && str1[i-1]!=' ') {
						i--;
						l--;
						if(!(mode&K_NOECHO))
			case CTRL_Y:    /* Ctrl-Y   Delete to end of line */
				if(i!=l) {	/* if not at EOL */
			case CTRL_X:    /* Ctrl-X   Delete entire line */
				if(mode&K_NOECHO)
					l=0;
				else {
			case CTRL_Z:	/* Undo */
				if(!(mode&K_NOECHO)) {
					while(i--)
						backspace();
				}
				SAFECOPY(str1,undo);
				i=l=strlen(str1);
				break;
			case 28:    /* Ctrl-\ Previous word */
					while(str1[i-1]==' ' && i)
					while(str1[i-1]!=' ' && i)
				break;
			case 29:  /* Ctrl-]/Left Arrow  Reverse Cursor Movement */
				if(i==0) {
					if(mode&K_LEFTEXIT)
						console|=CON_LEFTARROW;
					break;
				}
					cursor_left();   /* move cursor left one */
				break;
			case 30:  /* Ctrl-^/Up Arrow */
				if(!(mode&K_EDIT))
					break;
				if(i>l)
					l=i;
				str1[l]=0;
				strcpy(strout,str1);
				if((strip_invalid_attr(strout) || console&CON_INSERT) && !(mode&K_NOECHO))
					redrwstr(strout,i,l,K_MSG);
				if(mode&K_LINE && !(mode&K_NOECHO))
					attr(LIGHTGRAY);
				console|=CON_UPARROW;
				return(l);
			case DEL:  /* Ctrl-BkSpc (DEL) Delete current char */
				if(i==l) {	/* Backspace if end of line */
					if(i) {
						i--;
						l--;
						if(!(mode&K_NOECHO))
					}
					break;
				}
				l--;
				z=i;
				while(z<l)  {       /* move the characters in the line */
					outchar(str1[z]=str1[z+1]);
				outchar(' ');        /* write over the last char */
				if(mode&K_WRAP && i==maxlen && ch>=' ' && !(console&CON_INSERT)) {
					if(ch==' ' && !(mode&K_CHAT)) { /* don't wrap a space */ 
						strcpy(strout,str1);	   /* as last char */
						if(strip_invalid_attr(strout) && !(mode&K_NOECHO))
							redrwstr(strout,i,l,K_MSG);
						if(!(mode&(K_NOECHO|K_NOCRLF)))
					x=i-1;
					z=1;
					wordwrap[0]=ch;
					while(str1[x]!=' ' && x)
						wordwrap[z++]=str1[x--];
					if(x<(maxlen/2)) {
						wordwrap[1]=0;  /* only wrap one character */
						strcpy(strout,str1);
						if(strip_invalid_attr(strout) && !(mode&K_NOECHO))
							redrwstr(strout,i,l,K_MSG);
						if(!(mode&(K_NOECHO|K_NOCRLF)))
					wordwrap[z]=0;
					if(!(mode&K_NOECHO))
						while(z--) {
					strrev(wordwrap);
					str1[x]=0;
					strcpy(strout,str1);
					if(strip_invalid_attr(strout) && !(mode&K_NOECHO))
						redrwstr(strout,i,x,mode);
					if(!(mode&(K_NOECHO|K_NOCRLF)))
				if(i<maxlen && ch>=' ') {
					if(mode&K_UPRLWR)
						if(!i || (i && (str1[i-1]==' ' || str1[i-1]=='-'
							|| str1[i-1]=='.' || str1[i-1]=='_')))
							ch=toupper(ch);
						else
							ch=tolower(ch);
					if(console&CON_INSERT && i!=l) {
						if(l<maxlen)    /* l<maxlen */
							l++;
						for(x=l;x>i;x--)
							str1[x]=str1[x-1];
						if(i==maxlen-1) {
							bputs("  \b\b");
					str1[i++]=ch;
					if(!(mode&K_NOECHO))
				} else
					outchar(BEL);	/* Added at Angus McLeod's request */
		if(i>l)
			l=i;
		if(mode&K_CHAT && !l)
	if(!online)
		return(0);
	if(i>l)
		l=i;
	str1[l]=0;
	if(!(sys_status&SS_ABORT)) {
		strcpy(strout,str1);
		if((strip_invalid_attr(strout) || console&CON_INSERT) && !(mode&K_NOECHO))
	else
		l=0;
	if(mode&K_LINE && !(mode&K_NOECHO)) attr(LIGHTGRAY);
	if(!(mode&(K_NOCRLF|K_NOECHO))) {
		outchar(CR);
		if(!(mode&K_MSG && sys_status&SS_ABORT))
			outchar(LF);
	return(l);
}

/****************************************************************************/
/* Hot keyed number input routine.                                          */
/* Returns a valid number between 1 and max, 0 if no number entered, or -1  */
/* if the user hit the quit key (e.g. 'Q') or ctrl-c                        */
/****************************************************************************/
long sbbs_t::getnum(ulong max, ulong dflt)
{
    uchar ch,n=0;
	long i=0;

	while(online) {
		ch=getkey(K_UPPER);
		if(ch>0x7f)
			continue;
		if(ch==text[YNQP][2]) {
			outchar(text[YNQP][2]);
			if(useron.misc&COLDKEYS)
				ch=getkey(K_UPPER);
			if(ch==BS || ch==DEL) {
		else if(sys_status&SS_ABORT) {
			CRLF;
			lncntr=0;
		else if(ch==CR) {
			CRLF;
			lncntr=0;
		else if((ch==BS || ch==DEL) && n) {
		else if(isdigit(ch) && (i*10UL)+(ch&0xf)<=max && (dflt || ch!='0' || n)) {
			i*=10L;
			n++;
			i+=ch&0xf;
			outchar(ch);
			if(i*10UL>max && !(useron.misc&COLDKEYS)) {
				CRLF;
				lncntr=0;
	if(term_supports(ANSI)) {
		ansi_save();
		ansi_gotoxy(cols,1);
		uchar z=curatr;                       /* and go to EOL */
		if(console&CON_INSERT) {
			attr(BLINK|BLACK|(LIGHTGRAY<<4));
			outchar('I');
		} else {
			attr(LIGHTGRAY);
			outchar(' ');
		}
		attr(z);
		ansi_restore();