getstr.cpp 14.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* 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)		*
 *																			*
11
 * Copyright 2003 Rob Swindell - http://www.synchro.net/copyright.html		*
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
 *																			*
 * 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];
    uchar	ch;
	uchar	ins=0,atr;

56
	console&=~(CON_UPARROW|CON_LEFTARROW|CON_BACKSPACE);
57 58 59 60 61
	sys_status&=~SS_ABORT;
	if(mode&K_LINE && useron.misc&ANSI && !(mode&K_NOECHO)) {
		attr(cfg.color[clr_inputline]);
		for(i=0;i<maxlen;i++)
			outchar(SP);
62 63
		bprintf("\x1b[%dD",maxlen); 
	}
64 65
	if(wordwrap[0]) {
		strcpy(str1,wordwrap);
66 67
		wordwrap[0]=0; 
	}
68 69 70 71 72 73 74 75 76 77 78 79
	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;
80 81
			attr(i); 
		}
82 83
		rputs(str1);
		if(mode&K_EDIT && !(mode&(K_LINE|K_AUTODEL)) && useron.misc&ANSI)
84 85
			bputs("\x1b[K");  /* destroy to eol */ 
	}
86 87 88 89 90

	i=l=strlen(str1);
	if(mode&K_AUTODEL && str1[0] && !(mode&K_NOECHO)) {
		ch=getkey(mode|K_GETSTR);
		attr(atr);
91
		if(isprint(ch) || ch==DEL) {
92 93
			for(i=0;i<l;i++)
				bputs("\b \b");
94 95
			i=l=0; 
		}
96 97 98 99
		else {
			for(i=0;i<l;i++)
				outchar(BS);
			rputs(str1);
100 101
			i=l; 
		}
102
		if(ch!=SP && ch!=TAB)
103 104
			ungetkey(ch); 
	}
105

106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	if(mode&K_USEOFFSET) {
		i=getstr_offset;
		if(i>l)
			i=l;
		if(l-i) {
			if(useron.misc&ANSI)
				bprintf("\x1b[%dD",l-i);
			else
				for(i=0;i<l-i;i++)
					outchar(BS);
		}
	}

	while(!(sys_status&SS_ABORT) && online && input_thread_running) {
		if(console&(CON_UPARROW|CON_LEFTARROW|CON_BACKSPACE))
			break;
		if((ch=getkey(mode|K_GETSTR))==CR)
123
			break;
124
		if(sys_status&SS_ABORT || !online)
125
			break;
126 127
		if(ch==LF && mode&K_MSG) { /* Down-arrow same as CR */
			console|=CON_DOWNARROW;
128
			break;
129
		}
130 131 132 133 134 135 136 137
		if(ch==TAB && (mode&K_TAB || !(mode&K_WRAP)))	/* TAB same as CR */
			break;
		if(!i && mode&K_UPRLWR && (ch==SP || 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");
138 139 140 141
				return(0); 
			} 
		}
		getstr_offset=i;
142
		switch(ch) {
143
			case CTRL_A: /* Ctrl-A for ANSI */
144 145 146 147 148 149 150 151 152 153
				if(!(mode&K_MSG) || useron.rest&FLAG('A') || i>maxlen-3)
					break;
				if(ins) {
					if(l<maxlen)
						l++;
					for(x=l;x>i;x--)
						str1[x]=str1[x-1];
					rprintf("%.*s",l-i,str1+i);
					rprintf("\x1b[%dD",l-i);
					if(i==maxlen-1)
154 155
						ins=0; 
				}
156 157
				outchar(str1[i++]=1);
				break;
158
			case CTRL_B: /* Ctrl-B Beginning of Line */
159 160
				if(useron.misc&ANSI && i && !(mode&K_NOECHO)) {
					bprintf("\x1b[%dD",i);
161 162
					i=0; 
				}
163
				break;
164
			case CTRL_D: /* Ctrl-D Delete word right */
165 166 167 168
				if(i<l) {
					x=i;
					while(x<l && str1[x]!=SP) {
						outchar(SP);
169 170
						x++; 
					}
171 172
					while(x<l && str1[x]==SP) {
						outchar(SP);
173 174
						x++; 
					}
175 176 177 178
					bprintf("\x1b[%dD",x-i);   /* move cursor back */
					z=i;
					while(z<l-(x-i))  {             /* move chars in string */
						outchar(str1[z]=str1[z+(x-i)]);
179 180
						z++; 
					}
181 182
					while(z<l) {                    /* write over extra chars */
						outchar(SP);
183 184
						z++; 
					}
185
					bprintf("\x1b[%dD",z-i);
186 187
					l-=x-i;							/* l=new length */
				}
188
				break;
189
			case CTRL_E: /* Ctrl-E End of line */
190 191
				if(useron.misc&ANSI && i<l) {
					bprintf("\x1b[%dC",l-i);  /* move cursor to eol */
192 193
					i=l; 
				}
194
				break;
195
			case CTRL_F: /* Ctrl-F move cursor forewards */
196 197
				if(i<l && (useron.misc&ANSI)) {
					bputs("\x1b[C");   /* move cursor right one */
198 199
					i++; 
				}
200
				break;
201
			case CTRL_G: /* Bell */
202 203 204 205 206 207 208 209 210 211
				if(!(mode&K_MSG))
					break;
				if(useron.rest&FLAG('B')) {
					if (i+6<maxlen) {
						if(ins) {
							for(x=l+6;x>i;x--)
								str1[x]=str1[x-6];
							if(l+5<maxlen)
								l+=6;
							if(i==maxlen-1)
212 213
								ins=0; 
						}
214 215 216 217 218 219 220
						str1[i++]='(';
						str1[i++]='b';
						str1[i++]='e';
						str1[i++]='e';
						str1[i++]='p';
						str1[i++]=')';
						if(!(mode&K_NOECHO))
221 222
							bputs("(beep)"); 
					}
223 224
					if(ins)
						redrwstr(str1,i,l,0);
225 226 227
					break; 
				}
				if(ins) {
228 229 230 231 232
					if(l<maxlen)
						l++;
					for(x=l;x>i;x--)
						str1[x]=str1[x-1];
					if(i==maxlen-1)
233 234 235
						ins=0; 
				}
				if(i<maxlen) {
236
					str1[i++]=BEL;
237
					if(!(mode&K_NOECHO))
238 239 240
						outchar(BEL); 
				}
				break;
241
			case CTRL_H:	/* Ctrl-H/Backspace */
242 243 244
				if(i==0) {
					if(mode&K_LEFTEXIT)
						console|=CON_BACKSPACE;
245
					break;
246
				}
247 248 249 250 251 252 253
				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]);
254 255
						z++; 
					}
256
					outchar(SP);        /* write over the last char */
257 258
					bprintf("\x1b[%dD",(l-i)+1); 
				}
259 260 261
				else if(!(mode&K_NOECHO))
					bputs("\b \b");
				break;
262
			case CTRL_I:	/* Ctrl-I/TAB */
263
				if(!(i%EDIT_TABSIZE)) {
264 265 266 267 268 269
					if(ins) {
						if(l<maxlen)
							l++;
						for(x=l;x>i;x--)
							str1[x]=str1[x-1];
						if(i==maxlen-1)
270 271
							ins=0; 
					}
272 273
					str1[i++]=SP;
					if(!(mode&K_NOECHO))
274 275
						outchar(SP); 
				}
276
				while(i<maxlen && i%EDIT_TABSIZE) {
277 278 279 280 281 282
					if(ins) {
						if(l<maxlen)
							l++;
						for(x=l;x>i;x--)
							str1[x]=str1[x-1];
						if(i==maxlen-1)
283 284
							ins=0; 
					}
285 286
					str1[i++]=SP;
					if(!(mode&K_NOECHO))
287 288
						outchar(SP); 
				}
289 290 291 292
				if(ins && !(mode&K_NOECHO))
					redrwstr(str1,i,l,0);
				break;

293
			case CTRL_L:    /* Ctrl-L   Center line (used to be Ctrl-V) */
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
				str1[l]=0;
				l=bstrlen(str1);
				if(!l) break;
				for(x=0;x<(maxlen-l)/2;x++)
					str2[x]=SP;
				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)
312 313
						attr(LIGHTGRAY); 
				}
314 315 316
				CRLF;
				return(l);

317
			case CTRL_N:    /* Ctrl-N Next word */
318 319 320 321 322 323
				if(i<l && (useron.misc&ANSI)) {
					x=i;
					while(str1[i]!=SP && i<l)
						i++;
					while(str1[i]==SP && i<l)
						i++;
324 325
					bprintf("\x1b[%dC",i-x); 
				}
326
				break;
327
			case CTRL_R:    /* Ctrl-R Redraw Line */
328 329 330
				if(!(mode&K_NOECHO))
					redrwstr(str1,i,l,0);
				break;
331
			case CTRL_V:	/* Ctrl-V			Toggles Insert/Overwrite */
332 333 334 335
				if(!(useron.misc&ANSI) || mode&K_NOECHO)
					break;
				if(ins) {
					ins=0;
336 337
					redrwstr(str1,i,l,0); 
				}
338 339 340 341 342 343 344
				else if(i<l) {
					ins=1;
					bprintf("\x1b[s\x1b[79C");    	/* save pos  */
					z=curatr;                       /* and go to EOL */
					attr(z|BLINK|HIGH);
					outchar('');
					attr(z);
345 346
					bputs("\x1b[u");				/* restore pos */
				}
347
				break;
348
			case CTRL_W:    /* Ctrl-W   Delete word left */
349 350 351 352
				if(i<l) {
					x=i;                            /* x=original offset */
					while(i && str1[i-1]==SP) {
						outchar(BS);
353 354
						i--; 
					}
355 356
					while(i && str1[i-1]!=SP) {
						outchar(BS);
357 358
						i--; 
					}
359 360 361
					z=i;                            /* i=z=new offset */
					while(z<l-(x-i))  {             /* move chars in string */
						outchar(str1[z]=str1[z+(x-i)]);
362 363
						z++; 
					}
364 365
					while(z<l) {                    /* write over extra chars */
						outchar(SP);
366 367
						z++; 
					}
368
					bprintf("\x1b[%dD",z-i);        /* back to new x corridnant */
369 370
					l-=x-i;                         /* l=new length */
				} else {
371 372 373 374
					while(i && str1[i-1]==SP) {
						i--;
						l--;
						if(!(mode&K_NOECHO))
375 376
							bputs("\b \b"); 
					}
377 378 379 380
					while(i && str1[i-1]!=SP) {
						i--;
						l--;
						if(!(mode&K_NOECHO))
381 382 383
							bputs("\b \b"); 
					} 
				}
384
				break;
385
			case CTRL_X:    /* Ctrl-X   Delete entire line */
386 387 388 389 390
				if(mode&K_NOECHO)
					l=0;
				else {
					while(i<l) {
						outchar(SP);
391 392
						i++; 
					}
393 394
					while(l) {
						l--;
395 396 397
						bputs("\b \b"); 
					} 
				}
398 399
				i=0;
				break;
400
			case CTRL_Y:    /* Ctrl-Y   Delete to end of line */
401 402
				if(useron.misc&ANSI && !(mode&K_NOECHO)) {
					bputs("\x1b[K");
403 404
					l=i; 
				}
405 406 407 408 409 410 411 412
				break;
			case 28:    /* Ctrl-\ Previous word */
				if(i && (useron.misc&ANSI) && !(mode&K_NOECHO)) {
					x=i;
					while(str1[i-1]==SP && i)
						i--;
					while(str1[i-1]!=SP && i)
						i--;
413 414
					bprintf("\x1b[%dD",x-i); 
				}
415 416
				break;
			case 29:  /* Ctrl-]/Left Arrow  Reverse Cursor Movement */
417 418 419 420 421 422
				if(i==0) {
					if(mode&K_LEFTEXIT)
						console|=CON_LEFTARROW;
					break;
				}
				if((useron.misc&ANSI) && !(mode&K_NOECHO)) {
423
					bputs("\x1b[D");   /* move cursor left one */
424 425
					i--; 
				}
426 427 428 429
				break;
			case 30:  /* Ctrl-^/Up Arrow */
				if(!(mode&K_EDIT))
					break;
430
#if 0
431 432 433 434
				if(i>l)
					l=i;
				str1[l]=0;
				strcpy(strout,str1);
435
				if((strip_invalid_attr(strout) || ins) && !(mode&K_NOECHO))
436 437 438 439 440
					redrwstr(strout,i,l,K_MSG);
				if(mode&K_LINE && !(mode&K_NOECHO))
					attr(LIGHTGRAY);
				console|=CON_UPARROW;
				return(l);
441 442 443 444
#else
				console|=CON_UPARROW;
				break;
#endif
445
			case DEL:  /* Ctrl-BkSpc (DEL) Delete current char */
446 447 448 449 450 451 452 453 454 455 456 457 458
				if(i==l) {	/* Backspace if end of line */
					if(i) {
						i--;
						l--;
						if(!(mode&K_NOECHO))
							bputs("\b \b");
					}
					break;
				}
				l--;
				z=i;
				while(z<l)  {       /* move the characters in the line */
					outchar(str1[z]=str1[z+1]);
459 460
					z++; 
				}
461 462 463 464 465 466 467 468
				outchar(SP);        /* write over the last char */
				bprintf("\x1b[%dD",(l-i)+1);
				break;
			default:
				if(mode&K_WRAP && i==maxlen && ch>=SP && !ins) {
					str1[i]=0;
					if(ch==SP && !(mode&K_CHAT)) { /* don't wrap a space */ 
						strcpy(strout,str1);	   /* as last char */
469
						if(strip_invalid_attr(strout) && !(mode&K_NOECHO))
470 471 472
							redrwstr(strout,i,l,K_MSG);
						if(!(mode&K_NOECHO))
							CRLF;
473 474
						return(i); 
					}
475 476 477 478 479 480 481 482
					x=i-1;
					z=1;
					wordwrap[0]=ch;
					while(str1[x]!=SP && x)
						wordwrap[z++]=str1[x--];
					if(x<(maxlen/2)) {
						wordwrap[1]=0;  /* only wrap one character */
						strcpy(strout,str1);
483
						if(strip_invalid_attr(strout) && !(mode&K_NOECHO))
484 485 486
							redrwstr(strout,i,l,K_MSG);
						if(!(mode&K_NOECHO))
							CRLF;
487 488
						return(i); 
					}
489 490 491 492
					wordwrap[z]=0;
					if(!(mode&K_NOECHO))
						while(z--) {
							bputs("\b \b");
493 494
							i--; 
						}
495 496 497
					strrev(wordwrap);
					str1[x]=0;
					strcpy(strout,str1);
498
					if(strip_invalid_attr(strout) && !(mode&K_NOECHO))
499 500 501
						redrwstr(strout,i,x,(char)mode);
					if(!(mode&K_NOECHO))
						CRLF;
502 503
					return(x); 
				}
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
				if(i<maxlen && ch>=SP) {
					if(mode&K_UPRLWR)
						if(!i || (i && (str1[i-1]==SP || str1[i-1]=='-'
							|| str1[i-1]=='.' || str1[i-1]=='_')))
							ch=toupper(ch);
						else
							ch=tolower(ch);
					if(ins) {
						if(l<maxlen)    /* l<maxlen */
							l++;
						for(x=l;x>i;x--)
							str1[x]=str1[x-1];
						rprintf("%.*s",l-i,str1+i);
						rprintf("\x1b[%dD",l-i);
						if(i==maxlen-1) {
							bputs("  \b\b");
520 521 522
							ins=0; 
						} 
					}
523 524
					str1[i++]=ch;
					if(!(mode&K_NOECHO))
525 526 527
						outchar(ch); 
				} 
		}
528 529 530
		if(i>l)
			l=i;
		if(mode&K_CHAT && !l)
531 532 533
			return(0); 
	}
	getstr_offset=i;
534 535 536 537 538 539 540
	if(!online)
		return(0);
	if(i>l)
		l=i;
	str1[l]=0;
	if(!(sys_status&SS_ABORT)) {
		strcpy(strout,str1);
541
		if((strip_invalid_attr(strout) || ins) && !(mode&K_NOECHO))
542 543
			redrwstr(strout,i,l,K_MSG); 
	}
544 545 546 547 548 549 550
	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);
551 552
		lncntr=0; 
	}
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
	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 'Q' or ctrl-c                                            */
/****************************************************************************/
long sbbs_t::getnum(ulong max)
{
    uchar ch,n=0;
	long i=0;

	while(online) {
		ch=getkey(K_UPPER);
		if(ch>0x7f)
			continue;
		if(ch=='Q') {
			outchar('Q');
			if(useron.misc&COLDKEYS)
				ch=getkey(K_UPPER);
574
			if(ch==BS || ch==DEL) {
575
				bputs("\b \b");
576 577
				continue; 
			}
578 579
			CRLF;
			lncntr=0;
580 581
			return(-1); 
		}
582 583 584
		else if(sys_status&SS_ABORT) {
			CRLF;
			lncntr=0;
585 586
			return(-1); 
		}
587 588 589
		else if(ch==CR) {
			CRLF;
			lncntr=0;
590 591
			return(i); 
		}
592
		else if((ch==BS || ch==DEL) && n) {
593 594
			bputs("\b \b");
			i/=10;
595 596
			n--; 
		}
597 598 599 600 601 602 603 604
		else if(isdigit(ch) && (i*10UL)+(ch&0xf)<=max && (ch!='0' || n)) {
			i*=10L;
			n++;
			i+=ch&0xf;
			outchar(ch);
			if(i*10UL>max && !(useron.misc&COLDKEYS)) {
				CRLF;
				lncntr=0;
605 606 607 608
				return(i); 
			} 
		} 
	}
609 610
	return(0);
}