Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

con_out.cpp 35.5 KB
Newer Older
1 2 3 4 5 6
/* Synchronet console output routines */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
7
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *																			*
 * 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										*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#include "sbbs.h"
23
#include "utf8.h"
24
#include "unicode.h"
25
#include "petdefs.h"
rswindell's avatar
rswindell committed
26
#include "cp437defs.h"
27

28
char* sbbs_t::auto_utf8(const char* str, long& mode)
rswindell's avatar
rswindell committed
29 30
{
	if(strncmp(str, "\xEF\xBB\xBF", 3) == 0) {
31
		mode |= P_UTF8;
rswindell's avatar
rswindell committed
32 33
		return (char*)(str + 3);
	}
34
	if(mode & P_AUTO_UTF8) {
rswindell's avatar
rswindell committed
35
		if(!str_is_ascii(str) && utf8_str_is_valid(str))
36
			mode |= P_UTF8;
rswindell's avatar
rswindell committed
37 38 39 40
	}
	return (char*)str;
}

41 42
/****************************************************************************/
/* Outputs a NULL terminated string locally and remotely (if applicable)    */
43
/* Handles ctrl-a codes, Telnet-escaping, column & line count, auto-pausing */
44 45 46 47 48
/* Supported P_* mode flags:
   P_PETSCII
   P_UTF8
   P_AUTO_UTF8
   P_NOATCODES
49
   P_TRUNCATE
50
   P_REMOTE
51
 ****************************************************************************/
rswindell's avatar
rswindell committed
52
int sbbs_t::bputs(const char *str, long mode)
53 54 55
{
	int i;
    ulong l=0;
rswindell's avatar
rswindell committed
56
	long term = term_supports();
57

58 59 60
	if((mode & P_REMOTE) && online != ON_REMOTE)
		return 0;

61
	if(online==ON_LOCAL && console&CON_L_ECHO) 	/* script running as event */
62
		return(lputs(LOG_INFO, str));
63

64
	str = auto_utf8(str, mode);
rswindell's avatar
rswindell committed
65 66
	size_t len = strlen(str);
	while(l < len && online) {
67 68 69 70 71 72 73 74 75 76 77 78 79
		switch(str[l]) {
			case '\b':
			case '\r':
			case '\n':
			case FF:
			case CTRL_A:
				break;
			default: // printing char
				if((mode&P_TRUNCATE) && column >= (cols - 1)) {
					l++;
					continue;
				}
		}
80 81
		if(str[l]==CTRL_A && str[l+1]!=0) {
			l++;
82 83
			if(str[l] == 'Z')	/* EOF (uppercase 'Z' only) */
				break;
84
			if(str[l] == '~') { // Mouse hot-spot (hungry)
85
				l++;
86 87 88 89
				if(str[l] >= ' ')
					add_hotspot(str[l], /* hungry */true);
				else
					add_hotspot('\r', /* hungry */true);
90 91
				continue;
			}
92
			if(str[l] == '`' && str[l + 1] >= ' ') { // Mouse hot-spot (strict)
93 94
				l++;
				add_hotspot(str[l], /* hungry */false);
95 96
				continue;
			}
97
			ctrl_a(str[l++]);
98
			continue;
99
		}
100
		if(!(mode&P_NOATCODES) && str[l]=='@') {
101 102 103
			if(str==mnestr			/* Mnemonic string or */
				|| (str>=text[0]	/* Straight out of TEXT.DAT */
					&& str<=text[TOTAL_TEXT-1])) {
104
				i=show_atcode(str+l);	/* return 0 if not valid @ code */
105 106
				l+=i;					/* i is length of code string */
				if(i)					/* if valid string, go to top */
107
					continue;
108
			}
109 110 111 112
			for(i=0;i<TOTAL_TEXT;i++)
				if(str==text[i])
					break;
			if(i<TOTAL_TEXT) {		/* Replacement text */
113
				i=show_atcode(str+l);
114 115
				l+=i;
				if(i)
116 117
					continue;
			}
118
		}
rswindell's avatar
rswindell committed
119 120 121 122 123 124 125 126 127
		if(mode&P_PETSCII) {
			if(term&PETSCII)
				outcom(str[l++]);
			else
				petscii_to_ansibbs(str[l++]);
		} else if((str[l]&0x80) && (mode&P_UTF8)) {
			if(term&UTF8)
				outcom(str[l++]);
			else
rswindell's avatar
rswindell committed
128
				l += print_utf8_as_cp437(str + l, len - l);
rswindell's avatar
rswindell committed
129 130
		} else
			outchar(str[l++]);
131
	}
132 133 134
	return(l);
}

rswindell's avatar
rswindell committed
135 136 137 138 139
/****************************************************************************/
/* Returns the printed columns from 'str' accounting for Ctrl-A codes		*/
/****************************************************************************/
size_t sbbs_t::bstrlen(const char *str, long mode)
{
140
	str = auto_utf8(str, mode);
rswindell's avatar
rswindell committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
	size_t count = 0;
	const char* end = str + strlen(str);
	while (str < end) {
		int len = 1;
		if(*str == CTRL_A) {
			str++;
			if(*str == 0 || *str == 'Z')	// EOF
				break;
			if(*str == '[') // CR
				count = 0;
			else if(*str == '<' && count) // ND-Backspace
				count--;
		} else if(((*str) & 0x80) && (mode&P_UTF8)) {
			enum unicode_codepoint codepoint = UNICODE_UNDEFINED;
			len = utf8_getc(str, end - str, &codepoint);
			if(len < 1)
				break;
			count += unicode_width(codepoint);;
		} else
			count++;
		str += len;
	}
	return count;
}


167 168
/* Perform PETSCII terminal output translation (from ASCII/CP437) */
unsigned char cp437_to_petscii(unsigned char ch)
rswindell's avatar
rswindell committed
169
{
170
	if(IS_ALPHA(ch))
rswindell's avatar
rswindell committed
171 172 173
		return ch ^ 0x20;	/* swap upper/lower case */
	switch(ch) {
		case '\1':		return '@';
174 175 176 177 178 179 180
		case '\x10':	return '>';
		case '\x11':	return '<';
		case '\x18':	
		case '\x1e':	return PETSCII_UPARROW;
		case '\x19':
		case '\x1f':	return 'V';
		case '\x1a':	return '>';
rswindell's avatar
rswindell committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
		case '|':		return PETSCII_VERTLINE;
		case '\\':		return PETSCII_BACKSLASH;
		case '`':		return PETSCII_BACKTICK;
		case '~':		return PETSCII_TILDE;
		case '_':		return PETSCII_UNDERSCORE;
		case '{':		return '(';
		case '}':		return ')';
		case '\b':		return PETSCII_LEFT;
		case 156:		return PETSCII_BRITPOUND;
		case 251:		return PETSCII_CHECKMARK;
		case 176:		return PETSCII_LIGHTHASH;
		case 177:		return PETSCII_MEDIUMHASH;
		case 178:		return PETSCII_HEAVYHASH;
		case 219:		return PETSCII_SOLID;
		case 220:		return PETSCII_BOTTOMHALF;
		case 221:		return PETSCII_LEFTHALF;
		case 222:		return PETSCII_RIGHTHALF;
		case 223:		return PETSCII_TOPHALF;
		case 254:		return PETSCII_UPRLFTBOX;
		/* Line drawing chars */
		case 186:
		case 179:		return PETSCII_VERTLINE;
		case 205:
		case 196:		return PETSCII_HORZLINE;
		case 206:
		case 215:
		case 216:
		case 197:		return PETSCII_CROSS;
		case 188:
		case 189:
		case 190:
		case 217:		return '\xBD';
		case 201:
		case 213:
		case 214:
		case 218:		return '\xB0';
		case 183:
		case 184:
		case 187:
		case 191:		return '\xAE';
		case 200:
		case 211:
		case 212:
		case 192:		return '\xAD';
		case 198:
		case 199:
		case 204:
		case 195:		return '\xAB';
		case 180:
		case 181:
		case 182:
		case 185:		return '\xB3';
		case 203:
		case 209:
		case 210:
		case 194:		return '\xB2';
		case 202:
		case 207:
		case 208:
		case 193:		return '\xB1';
	}
	if(ch&0x80)
		return exascii_to_ascii_char(ch);
	return ch;
}

247 248 249 250 251
/* Perform PETSCII conversion to ANSI-BBS/CP437 */
int sbbs_t::petscii_to_ansibbs(unsigned char ch)
{
	if((ch&0xe0) == 0xc0)	/* "Codes $60-$7F are, actually, copies of codes $C0-$DF" */
		ch = 0x60 | (ch&0x1f);
252
	if(IS_ALPHA(ch))
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
		return outchar(ch ^ 0x20);	/* swap upper/lower case */
	switch(ch) {
		case '\r':					newline();		break;
		case PETSCII_HOME:			cursor_home();	break;
		case PETSCII_CLEAR:			return CLS;
		case PETSCII_DELETE:		backspace();	break;
		case PETSCII_LEFT:			cursor_left();	break;
		case PETSCII_RIGHT:			cursor_right();	break;
		case PETSCII_UP:			cursor_up();	break;
		case PETSCII_DOWN:			cursor_down();	break;

		case PETSCII_BRITPOUND:		return outchar((char)156);
		case PETSCII_CHECKMARK:		return outchar((char)251);
		case PETSCII_CHECKERBRD:
		case PETSCII_LIGHTHASH:		return outchar((char)176);
		case 0x7e:
		case PETSCII_MEDIUMHASH:	return outchar((char)177);
		case PETSCII_HEAVYHASH:		return outchar((char)178);
		case PETSCII_SOLID:			return outchar((char)219);
		case PETSCII_BOTTOMHALF:	return outchar((char)220);
		case PETSCII_LEFTHALF:		return outchar((char)221);
		case PETSCII_RIGHTHALF:		return outchar((char)222);
		case PETSCII_TOPHALF:		return outchar((char)223);
		case PETSCII_LWRLFTBOX:
		case PETSCII_LWRRHTBOX:
		case PETSCII_UPRRHTBOX:
		case PETSCII_UPRLFTBOX:		return outchar((char)254);

		/* Line drawing chars */
		case 0x7D:
		case PETSCII_VERTLINE:		return outchar((char)179);
		case PETSCII_HORZLINE:		return outchar((char)196);
		case 0x7B:
		case PETSCII_CROSS:			return outchar((char)197);
		case (uchar)'\xBD':			return outchar((char)217);
		case (uchar)'\xB0':			return outchar((char)218);
		case (uchar)'\xAE':			return outchar((char)191);
		case (uchar)'\xAD':			return outchar((char)192);
		case (uchar)'\xAB':			return outchar((char)195);
		case (uchar)'\xB3':			return outchar((char)180);
		case (uchar)'\xB2':			return outchar((char)194);
		case (uchar)'\xB1':			return outchar((char)193);
		case PETSCII_BLACK:			return attr(BLACK);
		case PETSCII_WHITE:			return attr(WHITE);
		case PETSCII_RED:			return attr(RED);
		case PETSCII_GREEN:			return attr(GREEN);
		case PETSCII_BLUE:			return attr(BLUE);
		case PETSCII_ORANGE:		return attr(MAGENTA);
		case PETSCII_BROWN:			return attr(BROWN);
		case PETSCII_YELLOW:		return attr(YELLOW);
		case PETSCII_CYAN:			return attr(LIGHTCYAN);
		case PETSCII_LIGHTRED:		return attr(LIGHTRED);
		case PETSCII_DARKGRAY:		return attr(DARKGRAY);
		case PETSCII_MEDIUMGRAY:	return attr(CYAN);
		case PETSCII_LIGHTGREEN:	return attr(LIGHTGREEN);
		case PETSCII_LIGHTBLUE:		return attr(LIGHTBLUE);
		case PETSCII_LIGHTGRAY:		return attr(LIGHTGRAY);
		case PETSCII_PURPLE:		return attr(LIGHTMAGENTA);
		case PETSCII_REVERSE_ON:	return attr((curatr&0x07) << 4);
		case PETSCII_REVERSE_OFF:	return attr(curatr >> 4);
		case PETSCII_FLASH_ON:		return attr(curatr | BLINK);
		case PETSCII_FLASH_OFF:		return attr(curatr & ~BLINK);
		default:					
			if(ch&0x80)				return bprintf("#%3d", ch);
			return outchar(ch);
		case PETSCII_UPPERLOWER:
		case PETSCII_UPPERGRFX:
			/* Do nothing */
			return 0;
	}
	return 0;
}

326
// Return length of sequence
rswindell's avatar
rswindell committed
327
size_t sbbs_t::print_utf8_as_cp437(const char* str, size_t len)
328 329 330 331 332
{
	if(((*str)&0x80) == 0) {
		outchar(*str);
		return sizeof(char);
	}
333
	enum unicode_codepoint codepoint = UNICODE_UNDEFINED;
334
	len = utf8_getc(str, len, &codepoint);
335
	if((int)len < 2) {
336 337
		outchar(*str);	// Assume it's a CP437 character
		lprintf(LOG_DEBUG, "Invalid UTF-8 sequence: %02X (error = %d)", (uchar)*str, (int)len);
338 339 340 341 342 343 344 345 346
		return 1;
	}
	for(int i = 1; i < 0x100; i++) {
		if(cp437_unicode_tbl[i]
			&& cp437_unicode_tbl[i] == codepoint) {
			outchar(i);
			return len;
		}
	}
347
	char ch = unicode_to_cp437(codepoint);
348 349
	if(ch)
		outchar(ch);
350
	else if(unicode_width(codepoint) > 0) {
351
		outchar(CP437_INVERTED_QUESTION_MARK);
352 353 354 355 356 357 358
		char seq[32] = "";
		for(size_t i = 0; i < len; i++)
			sprintf(seq + strlen(seq), "%02X ", (uchar)*(str + i));
		lprintf(LOG_DEBUG, "Unsupported UTF-8 sequence: %s (U+%X)", seq, codepoint);
	}
	return len;
}
359

360
/****************************************************************************/
361 362
/* Raw put string (remotely)												*/
/* Performs Telnet IAC escaping												*/
363
/* Performs charset translations											*/
364
/* Performs saveline buffering (for restoreline)							*/
365
/* DOES NOT expand ctrl-A codes, track columns, lines, auto-pause, etc.     */
366
/****************************************************************************/
367
int sbbs_t::rputs(const char *str, size_t len)
368
{
369 370
    size_t	l;

371 372
	if(console&CON_ECHO_OFF)
		return 0;
373 374
	if(len==0)
		len=strlen(str);
rswindell's avatar
rswindell committed
375
	long term = term_supports();
376
	char utf8[UTF8_MAX_LEN + 1] = "";
377
	for(l=0;l<len && online;l++) {
378
		uchar ch = str[l];
379
		utf8[0] = 0;
380
		if(term&PETSCII) {
381
			ch = cp437_to_petscii(ch);
382 383 384
			if(ch == PETSCII_SOLID)
				outcom(PETSCII_REVERSE_ON);
		}
385 386 387
		else if((term&NO_EXASCII) && (ch&0x80))
			ch = exascii_to_ascii_char(ch);  /* seven bit table */
		else if(term&UTF8) {
388
			enum unicode_codepoint codepoint = cp437_unicode_tbl[(uchar)ch];
389 390 391 392 393
			if(codepoint != 0)
				utf8_putc(utf8, sizeof(utf8) - 1, codepoint);
		}
		if(utf8[0])
			putcom(utf8);
394
		else {
395 396
			if(outcom(ch)!=0)
				break;
397
			if((char)ch == (char)TELNET_IAC && !(telnet_mode&TELNET_MODE_OFF))
398
				outcom(TELNET_IAC);	/* Must escape Telnet IAC char (255) */
399 400
			if((term&PETSCII) && ch == PETSCII_SOLID)
				outcom(PETSCII_REVERSE_OFF);
401
		}
402 403 404 405 406
		if(ch == '\n')
			lbuflen=0;
		else if(lbuflen<LINE_BUFSIZE) {
			if(lbuflen == 0)
				latr = curatr;
rswindell's avatar
rswindell committed
407
			lbuf[lbuflen++] = ch;
408
		}
409
	}
410 411 412 413 414 415
	return(l);
}

/****************************************************************************/
/* Performs printf() using bbs bputs function								*/
/****************************************************************************/
416
int sbbs_t::bprintf(const char *fmt, ...)
417 418
{
	va_list argptr;
419
	char sbuf[4096];
420

deuce's avatar
deuce committed
421
	if(strchr(fmt,'%')==NULL)
422 423
		return(bputs(fmt));
	va_start(argptr,fmt);
424 425
	vsnprintf(sbuf,sizeof(sbuf),fmt,argptr);
	sbuf[sizeof(sbuf)-1]=0;	/* force termination */
426 427 428 429
	va_end(argptr);
	return(bputs(sbuf));
}

rswindell's avatar
rswindell committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
/****************************************************************************/
/* Performs printf() using bbs bputs function (with mode)					*/
/****************************************************************************/
int sbbs_t::bprintf(long mode, const char *fmt, ...)
{
	va_list argptr;
	char sbuf[4096];

	if(strchr(fmt,'%')==NULL)
		return(bputs(fmt, mode));
	va_start(argptr,fmt);
	vsnprintf(sbuf,sizeof(sbuf),fmt,argptr);
	sbuf[sizeof(sbuf)-1]=0;	/* force termination */
	va_end(argptr);
	return(bputs(sbuf, mode));
}

447 448 449
/****************************************************************************/
/* Performs printf() using bbs rputs function								*/
/****************************************************************************/
450
int sbbs_t::rprintf(const char *fmt, ...)
451 452
{
	va_list argptr;
453
	char sbuf[4096];
454 455

	va_start(argptr,fmt);
456 457
	vsnprintf(sbuf,sizeof(sbuf),fmt,argptr);
	sbuf[sizeof(sbuf)-1]=0;	/* force termination */
458 459 460 461
	va_end(argptr);
	return(rputs(sbuf));
}

462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
/****************************************************************************/
/* Performs printf() using bbs putcom/outcom functions						*/
/****************************************************************************/
int sbbs_t::comprintf(const char *fmt, ...)
{
	va_list argptr;
	char sbuf[4096];

	va_start(argptr,fmt);
	vsnprintf(sbuf,sizeof(sbuf),fmt,argptr);
	sbuf[sizeof(sbuf)-1]=0;	/* force termination */
	va_end(argptr);
	return(putcom(sbuf));
}

477
/****************************************************************************/
rswindell's avatar
rswindell committed
478
/* Outputs destructive backspace 											*/
479
/****************************************************************************/
480
void sbbs_t::backspace(int count)
481
{
482 483
	if(count < 1)
		return;
484
	if(!(console&CON_ECHO_OFF)) {
485 486 487 488 489 490 491 492
		for(int i = 0; i < count; i++) {
			if(term_supports(PETSCII))
				outcom(PETSCII_DELETE);
			else {
				outcom('\b');
				outcom(' ');
				outcom('\b');
			}
493
			if(column > 0)
494
				column--;
495 496
			if(lbuflen > 0)
				lbuflen--;
rswindell's avatar
rswindell committed
497
		}
498
	}
499 500
}

501 502 503 504 505 506 507
/****************************************************************************/
/* Returns true if the user (or the yet-to-be-logged-in client) supports	*/
/* all of the specified terminal 'cmp_flags' (e.g. ANSI, COLOR, RIP).		*/
/* If no flags specified, returns all terminal flag bits supported			*/
/****************************************************************************/
long sbbs_t::term_supports(long cmp_flags)
{
508
	long flags = ((sys_status&SS_USERON) && !(useron.misc&AUTOTERM)) ? useron.misc : autoterm;
509

510
	if((sys_status&SS_USERON) && (useron.misc&AUTOTERM))
511
		flags |= useron.misc & (NO_EXASCII | SWAP_DELETE | COLOR | ICE_COLOR | MOUSE);
512

513 514 515
	return(cmp_flags ? ((flags&cmp_flags)==cmp_flags) : (flags&TERM_FLAGS));
}

516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
/****************************************************************************/
/* Returns description of the terminal type									*/
/****************************************************************************/
const char* sbbs_t::term_type(long term)
{
	if(term == -1)
		term = term_supports();
	if(term&PETSCII)
		return "PETSCII";
	if(term&RIP)
		return "RIP";
	if(term&ANSI)
		return "ANSI";
	return "DUMB";
}

/****************************************************************************/
/* Returns description of the terminal supported character set (charset)	*/
/****************************************************************************/
const char* sbbs_t::term_charset(long term)
{
	if(term == -1)
		term = term_supports();
	if(term&PETSCII)
		return "CBM-ASCII";
	if(term&UTF8)
		return "UTF-8";
	if(term&NO_EXASCII)
		return "US-ASCII";
	return "CP437";
}

548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
/****************************************************************************/
/* For node spying purposes													*/
/****************************************************************************/
bool sbbs_t::update_nodeterm(void)
{
	str_list_t	ini = strListInit();
	iniSetInteger(&ini, ROOT_SECTION, "cols", cols, NULL);
	iniSetInteger(&ini, ROOT_SECTION, "rows", rows, NULL);
	iniSetString(&ini, ROOT_SECTION, "type", term_type(), NULL);
	iniSetString(&ini, ROOT_SECTION, "chars", term_charset(), NULL);
	iniSetHexInt(&ini, ROOT_SECTION, "flags", term_supports(), NULL);
	iniSetHexInt(&ini, ROOT_SECTION, "mouse", mouse_mode, NULL);
	iniSetHexInt(&ini, ROOT_SECTION, "console", console, NULL);

	char path[MAX_PATH + 1];
	SAFEPRINTF(path, "%sterminal.ini", cfg.node_dir);
	FILE* fp = iniOpenFile(path, /* create: */TRUE);
	bool result = false;
	if(fp != NULL) {
		result = iniWriteFile(fp, ini);
		iniCloseFile(fp);
	}
	strListFree(&ini);
	return result;
}

574
/****************************************************************************/
575 576 577
/* Outputs character														*/
/* Performs terminal translations (e.g. EXASCII-to-ASCII, FF->ESC[2J)		*/
/* Performs Telnet IAC escaping												*/
rswindell's avatar
rswindell committed
578
/* Performs tab expansion													*/
579 580
/* Performs column counting, line counting, and auto-pausing				*/
/* Performs saveline buffering (for restoreline)							*/
581
/****************************************************************************/
582
int sbbs_t::outchar(char ch)
583 584
{
	if(console&CON_ECHO_OFF)
585
		return 0;
586 587 588 589 590 591 592
	if(ch == ESC && outchar_esc < ansiState_string)
		outchar_esc = ansiState_esc;
	else if(outchar_esc == ansiState_esc) {
		if(ch == '[')
			outchar_esc = ansiState_csi;
		else if(ch == '_' || ch == 'P' || ch == '^' || ch == ']')
			outchar_esc = ansiState_string;
593
		else if(ch=='X')
594 595 596
			outchar_esc = ansiState_sos;
		else if(ch >= '@' && ch <= '_')
			outchar_esc = ansiState_final;
deuce's avatar
deuce committed
597
		else
598
			outchar_esc = ansiState_none;
deuce's avatar
deuce committed
599
	}
600 601 602
	else if(outchar_esc == ansiState_csi) {
		if(ch >= '@' && ch <= '~')
			outchar_esc = ansiState_final;
603
	}
604
	else if(outchar_esc == ansiState_string) {	// APS, DCS, PM, or OSC
605
		if (ch == ESC)
606 607 608
			outchar_esc = ansiState_esc;
		if (!((ch >= '\b' && ch <= '\r') || (ch >= ' ' && ch <= '~')))
			outchar_esc = ansiState_none;
609
	}
610
	else if(outchar_esc == ansiState_sos) {	// SOS
611
		if (ch == ESC)
612
			outchar_esc = ansiState_sos_esc;
613
	}
614
	else if(outchar_esc == ansiState_sos_esc) {	// ESC inside SOS
615
		if (ch == '\\')
616
			outchar_esc = ansiState_esc;
617
		else if (ch == 'X')
618
			outchar_esc = ansiState_none;
619
		else
620
			outchar_esc = ansiState_sos;
621
	}
622
	else
623
		outchar_esc = ansiState_none;
rswindell's avatar
rswindell committed
624
	long term = term_supports();
625
	char utf8[UTF8_MAX_LEN + 1] = "";
626 627 628
	if(!(term&PETSCII)) {
		if((term&NO_EXASCII) && (ch&0x80))
			ch = exascii_to_ascii_char(ch);  /* seven bit table */
629
		else if(term&UTF8) {
630
			enum unicode_codepoint codepoint = cp437_unicode_tbl[(uchar)ch];
631 632 633
			if(codepoint != 0)
				utf8_putc(utf8, sizeof(utf8) - 1, codepoint);
		}
634
	}
rswindell's avatar
rswindell committed
635

rswindell's avatar
rswindell committed
636
	if(ch==FF && lncntr > 0 && row > 0) {
637
		lncntr=0;
rswindell's avatar
rswindell committed
638
		newline();
639 640 641
		if(!(sys_status&SS_PAUSEOFF)) {
			pause();
			while(lncntr && online && !(sys_status&SS_ABORT))
642
				pause();
643 644
		}
	}
645

646 647 648
	if(!(console&CON_R_ECHO))
		return 0;

649
	if((console&CON_R_ECHOX) && (uchar)ch>=' ' && outchar_esc == ansiState_none) {
650 651 652
		ch=text[YNQP][3];
		if(text[YNQP][2]==0 || ch==0) ch='X';
	}
rswindell's avatar
rswindell committed
653 654
	if(ch==FF)
		clearscreen(term);
655 656 657 658
	else if(ch == '\t') {
		outcom(' ');
		column++;
		while(column%tabstop) {
rswindell's avatar
rswindell committed
659 660
			outcom(' ');
			column++;
661
		}
662
	}
663 664 665
	else {
		if(ch==(char)TELNET_IAC && !(telnet_mode&TELNET_MODE_OFF))
			outcom(TELNET_IAC);	/* Must escape Telnet IAC char (255) */
666 667
		if(ch == '\r' && (console&CON_CR_CLREOL))
			cleartoeol();
668 669 670 671 672 673 674
		if(term&PETSCII) {
			uchar pet = cp437_to_petscii(ch);
			if(pet == PETSCII_SOLID)
				outcom(PETSCII_REVERSE_ON);
			outcom(pet);
			if(pet == PETSCII_SOLID)
				outcom(PETSCII_REVERSE_OFF);
rswindell's avatar
rswindell committed
675 676
			if(ch == '\r' && (curatr&0xf0) != 0) // reverse video is disabled upon CR
				curatr >>= 4;
677
		} else {
678
			if(utf8[0] != 0)
679 680 681 682
				putcom(utf8);
			else
				outcom(ch);
		}
683
	}
684
	if(outchar_esc == ansiState_none) {
685 686 687 688 689 690 691 692 693
		/* Track cursor position locally */
		switch(ch) {
			case '\a':	// 7
			case '\t':	// 9
				/* Non-printing or handled elsewhere */
				break;
			case '\b':	// 8
				if(column > 0)
					column--;
694 695 696 697 698
				if(lbuflen<LINE_BUFSIZE) {
					if(lbuflen == 0)
						latr = curatr;
					lbuf[lbuflen++] = ch;
				}
699 700
				break;
			case '\n':	// 10
rswindell's avatar
rswindell committed
701
				inc_row(1);
702 703 704 705 706 707 708
				if(lncntr || lastlinelen)
					lncntr++;
				lbuflen=0;
				break;
			case FF:	// 12
				lncntr=0;
				lbuflen=0;
rswindell's avatar
rswindell committed
709
				row=0;
710 711
				column=0;
			case '\r':	// 13
712
				lastlinelen = column;
713 714 715
				column=0;
				break;
			default:
716
				inc_column(1);
717 718 719 720 721
				if(!lbuflen)
					latr=curatr;
				if(lbuflen<LINE_BUFSIZE)
					lbuf[lbuflen++]=ch;
				break;
722 723
		}
	}
724 725
	if(outchar_esc == ansiState_final)
		outchar_esc = ansiState_none;
726

727
	if(lncntr==rows-1 && ((useron.misc&(UPAUSE^(console&CON_PAUSEOFF))) || sys_status&SS_PAUSEON)
728
		&& !(sys_status&(SS_PAUSEOFF|SS_ABORT))) {
729
		lncntr=0;
730
		pause();
rswindell's avatar
rswindell committed
731
	}
732
	return 0;
733 734
}

735
int sbbs_t::outchar(enum unicode_codepoint codepoint, const char* cp437_fallback)
736 737 738 739 740 741 742 743 744 745
{
	if(term_supports(UTF8)) {
		char str[UTF8_MAX_LEN];
		int len = utf8_putc(str, sizeof(str), codepoint);
		if(len < 1)
			return len;
		putcom(str, len);
		inc_column(unicode_width(codepoint));
		return 0;
	}
746
	if(cp437_fallback == NULL)
747
		return 0;
748 749 750 751 752 753 754
	return bputs(cp437_fallback);
}

int sbbs_t::outchar(enum unicode_codepoint codepoint, char cp437_fallback)
{
	char str[2] = { cp437_fallback, '\0' };
	return outchar(codepoint, str);
755 756 757 758 759 760 761 762 763 764
}

void sbbs_t::inc_column(int count)
{
	column += count;
	if(column >= cols) {	// assume terminal has/will auto-line-wrap
		lncntr++;
		lbuflen = 0;
		lastlinelen = column;
		column = 0;
rswindell's avatar
rswindell committed
765 766 767 768 769 770 771 772 773 774
		inc_row(1);
	}
}

void sbbs_t::inc_row(int count)
{
	row += count;
	if(row >= rows) {
		scroll_hotspots((row - rows) + 1);
		row = rows - 1;
775 776 777
	}
}

Rob Swindell's avatar
Rob Swindell committed
778
void sbbs_t::center(const char *instr, unsigned int columns)
779 780
{
	char str[256];
rswindell's avatar
rswindell committed
781
	size_t len;
782

783 784 785
	if(columns < 1)
		columns = cols;

786
	SAFECOPY(str,instr);
787
	truncsp(str);
rswindell's avatar
rswindell committed
788
	len = bstrlen(str);
789
	carriage_return();
rswindell's avatar
rswindell committed
790 791
	if(len < columns)
		cursor_right((columns - len) / 2);
792
	bputs(str);
rswindell's avatar
rswindell committed
793 794 795
	newline();
}

rswindell's avatar
rswindell committed
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
void sbbs_t::wide(const char* str)
{
	long term = term_supports();
	while(*str != '\0') {
		if((term&UTF8) && *str >= '!' && *str <= '~')
			outchar((enum unicode_codepoint)(UNICODE_FULLWIDTH_EXCLAMATION_MARK + (*str - '!')));
		else {
			outchar(*str);
			outchar(' ');
		}
		str++;
	}
}


rswindell's avatar
rswindell committed
811
// Send a bare carriage return, hopefully moving the cursor to the far left, current row
812
void sbbs_t::carriage_return(int count)
rswindell's avatar
rswindell committed
813
{
814 815 816 817 818 819 820 821 822
	if(count < 1)
		return;
	for(int i = 0; i < count; i++) {
		if(term_supports(PETSCII))
			cursor_left(column);
		else
			outcom('\r');
		column = 0;
	}
rswindell's avatar
rswindell committed
823 824 825
}

// Send a bare line_feed, hopefully moving the cursor down one row, current column
826
void sbbs_t::line_feed(int count)
rswindell's avatar
rswindell committed
827
{
828 829 830 831 832 833 834 835
	if(count < 1)
		return;
	for(int i = 0; i < count; i++) {
		if(term_supports(PETSCII))
			outcom(PETSCII_DOWN);
		else 
			outcom('\n');
	}
836
	inc_row(count);
rswindell's avatar
rswindell committed
837 838
}

839
void sbbs_t::newline(int count)
rswindell's avatar
rswindell committed
840
{
841 842 843 844 845 846
	if(count < 1)
		return;
	for(int i = 0; i < count; i++) { 
		outchar('\r');
		outchar('\n');
	}
847 848
}

rswindell's avatar
rswindell committed
849 850 851 852 853 854 855 856 857
void sbbs_t::clearscreen(long term)
{
	clear_hotspots();
	if(term&ANSI)
		putcom("\x1b[2J\x1b[H");	/* clear screen, home cursor */
	else if(term&PETSCII)
		outcom(PETSCII_CLEAR);
	else
		outcom(FF);
858 859
	row=0;
	column=0;
860
	lncntr=0;
rswindell's avatar
rswindell committed
861 862
}

863 864
void sbbs_t::clearline(void)
{
rswindell's avatar
rswindell committed
865
	carriage_return();
866
	cleartoeol();
867 868 869 870
}

void sbbs_t::cursor_home(void)
{
rswindell's avatar
rswindell committed
871 872
	long term = term_supports();
	if(term&ANSI)
873
		putcom("\x1b[H");
rswindell's avatar
rswindell committed
874 875
	else if(term&PETSCII)
		outcom(PETSCII_HOME);
876
	else
877
		outchar(FF);	/* this will clear some terminals, do nothing with others */
rswindell's avatar
rswindell committed
878
	row=0;
879
	column=0;
880 881 882 883
}

void sbbs_t::cursor_up(int count)
{
884 885
	if(count<1)
		return;
rswindell's avatar
rswindell committed
886 887 888
	long term = term_supports();
	if(term&ANSI) {
		if(count>1)
889
			comprintf("\x1b[%dA",count);
rswindell's avatar
rswindell committed
890
		else
891
			putcom("\x1b[A");
rswindell's avatar
rswindell committed
892 893 894 895 896 897
	} else {
		if(term&PETSCII) {
			for(int i=0;i<count;i++)
				outcom(PETSCII_UP);
		}
	}
898 899 900 901
}

void sbbs_t::cursor_down(int count)
{
902 903
	if(count<1)
		return;
rswindell's avatar
rswindell committed
904 905
	if(term_supports(ANSI)) {
		if(count>1)
906
			comprintf("\x1b[%dB",count);
rswindell's avatar
rswindell committed
907
		else
908
			putcom("\x1b[B");
909
		inc_row(count);
rswindell's avatar
rswindell committed
910 911 912 913
	} else {
		for(int i=0;i<count;i++)
			line_feed();
	}
914 915 916 917
}

void sbbs_t::cursor_right(int count)
{
918 919
	if(count<1)
		return;
rswindell's avatar
rswindell committed
920 921
	long term = term_supports();
	if(term&ANSI) {
922
		if(count>1)
923
			comprintf("\x1b[%dC",count);
924
		else
925
			putcom("\x1b[C");
926
	} else {
rswindell's avatar
rswindell committed
927 928 929 930 931 932
		for(int i=0;i<count;i++) {
			if(term&PETSCII)
				outcom(PETSCII_RIGHT);
			else
				outcom(' ');
		}
933
	}
934
	column+=count;
935 936 937 938
}

void sbbs_t::cursor_left(int count)
{
939 940
	if(count<1)
		return;
rswindell's avatar
rswindell committed
941 942
	long term = term_supports();
	if(term&ANSI) {
943
		if(count>1)
944
			comprintf("\x1b[%dD",count);
945
		else
946
			putcom("\x1b[D");
947
	} else {
rswindell's avatar
rswindell committed
948 949 950 951 952 953
		for(int i=0;i<count;i++) {
			if(term&PETSCII)
				outcom(PETSCII_LEFT);
			else
				outcom('\b');
		}
954
	}
955 956 957 958
	if(column > count)
		column-=count;
	else
		column=0;
959 960
}

961 962 963 964 965 966 967 968 969 970 971 972 973 974
bool sbbs_t::cursor_xy(int x, int y)
{
	long term = term_supports();
	if(term&ANSI)
		return ansi_gotoxy(x, y);
	if(term&PETSCII) {
		outcom(PETSCII_HOME);
		cursor_down(y - 1);
		cursor_right(x - 1);
		return true;
	}
	return false;
}

975 976 977 978 979 980 981 982 983
bool sbbs_t::cursor_getxy(int* x, int* y)
{
	if(term_supports(ANSI))
		return ansi_getxy(x, y);
	*x = column + 1;
	*y = row + 1;
	return true;
}

984 985
void sbbs_t::cleartoeol(void)
{
986 987
	int i,j;

rswindell's avatar
rswindell committed
988 989
	long term = term_supports();
	if(term&ANSI)
990
		putcom("\x1b[K");
991
	else {
992
		i=j=column;
993
		while(++i<cols)
994
			outcom(' ');
rswindell's avatar
rswindell committed
995 996 997 998 999 1000
		while(++j<cols) {
			if(term&PETSCII)
				outcom(PETSCII_LEFT);
			else
				outcom('\b');
		}
1001 1002
	}
}
1003