aboutsummaryrefslogtreecommitdiff
path: root/contrib/ncurses/tack/output.c
blob: 8a3d0f42dfeb4e0e9e51e821c379e7a47f495428 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
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
247
248
249
250
251
252
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
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
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
/*
** Copyright (C) 1991, 1997 Free Software Foundation, Inc.
** 
** This file is part of TACK.
** 
** TACK 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, or (at your option)
** any later version.
** 
** TACK is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
** 
** You should have received a copy of the GNU General Public License
** along with TACK; see the file COPYING.  If not, write to
** the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
** Boston, MA 02110-1301, USA
*/
/* screen formatting and I/O utility functions */

#include <tack.h>
#include <time.h>

MODULE_ID("$Id: output.c,v 1.11 2006/11/26 00:16:49 tom Exp $")

/* globals */
long char_sent;			/* number of characters sent */
int char_count;			/* counts characters */
int line_count;			/* counts line feeds */
int expand_chars;		/* length of expand() string */
int replace_mode;		/* used to output replace mode padding */
int can_go_home;		/* TRUE if we can fashion a home command */
int can_clear_screen;		/* TRUE if we can somehow clear the screen */
int raw_characters_sent;	/* Total output characters */
static int log_count;		/* Number of characters on a log line */

/* translate mode default strings */
#define TM_carriage_return	TM_string[0].value
#define TM_cursor_down		TM_string[1].value
#define TM_scroll_forward	TM_string[2].value
#define TM_newline		TM_string[3].value
#define TM_cursor_left		TM_string[4].value
#define TM_bell			TM_string[5].value
#define TM_form_feed		TM_string[6].value
#define TM_tab			TM_string[7].value

struct default_string_list TM_string[TM_last] = {
	{"cr", "\r", 0},
	{"cud1", "\n", 0},
	{"ind", "\n", 0},
	{"nel", "\r\n", 0},
	{"cub1", "\b", 0},
	{"bel", "\007", 0},
	{"ff", "\f", 0},
	{"ht", "\t", 0}
};

static const char *c0[32] = {
	"NUL", "SOH", "STX", "ETX", "EOT", "ENQ", "ACK",
	"BEL", "BS", "HT", "LF", "VT", "FF", "CR", "SO", "SI",
	"DLE", "DC1", "DC2", "DC3", "DC4", "NAK", "SYN", "ETB",
	"CAN", "EM", "SUB", "ESC", "FS", "GS", "RS", "US"
};

static const char *c1[32] = {
	"", "", "", "", "IND", "NEL", "SSA", "ESA",
	"HTS", "HTJ", "VTS", "PLD", "PLU", "RI", "SS2", "SS3",
	"DCS", "PU1", "PU2", "STS", "CCH", "MW", "SPA", "EPA",
	"", "", "", "CSI", "ST", "OSC", "PM", "APC"
};

int
getnext(int mask)
{				/* get the next character without scan mode
				   conversion */
	int ch;
	unsigned char buf;

	tc_putp(req_for_input);
	fflush(stdout);
	if (nodelay_read)
		while (1) {
			ch = read(fileno(stdin), &buf, 1);
			if (ch == -1)
				return EOF;
			if (ch == 1)
				return buf;
		}
	ch = getchar();
	if (ch == EOF)
		return EOF;
	return ch & mask;
}


int
getchp(int mask)
{				/* read a character with scan mode conversion */
	if (scan_mode) {
		tc_putp(req_for_input);
		fflush(stdout);
		return scan_key();
	} else
		return getnext(mask);
}

/*
**	tc_putch(c)
**
**	Output one character
*/
int
tc_putch(int c)
{
	char_sent++;
	raw_characters_sent++;
	putchar(c);
	if ((raw_characters_sent & 31) == 31) {
		fflush(stdout);
	}
	if (log_fp) {
		/* terminal output logging */
		c = UChar(c);
		if (c < 32) {
			fprintf(log_fp, "<%s>", c0[c]);
			log_count += 5;
		} else
		if (c < 127) {
			fprintf(log_fp, "%c", c);
			log_count += 1;
		} else {
			fprintf(log_fp, "<%02x>", c);
			log_count += 4;
		}
		if (c == '\n' || log_count >= 80) {
			fprintf(log_fp, "\n");
			log_count = 0;
		}
	}
	return (c);
}

/*
**	tt_tputs(string, reps)
**
**	Output a string with tputs() translation.
**	Use this function inside timing tests.
*/
void
tt_tputs(const char *string, int reps)
{
	int i;

	if (string) {
		for (i = 0; i < TT_MAX; i++) {
			if (i >= ttp) {
				tt_cap[i] = string;
				tt_affected[i] = reps;
				tt_count[i] = 1;
				tt_delay[i] = msec_cost(string, reps);
				ttp++;
				break;
			}
			if (string == tt_cap[i] && reps == tt_affected[i]) {
				tt_count[i]++;
				tt_delay_used += tt_delay[i];
				break;
			}
		}
		(void) tputs(string, reps, tc_putch);
	}
}

/*
**	tt_putp(string)
**
**	Output a string with tputs() translation.
**	Use this function inside timing tests.
*/
void
tt_putp(const char *string)
{
	tt_tputs(string, 1);
}

/*
**	tt_putparm(string, reps, arg1, arg2)
**
**	Send tt_tputs(tparm(string, args1, arg2), reps)
**	Use this function inside timing tests.
*/
void
tt_putparm(
	NCURSES_CONST char *string,
	int reps,
	int arg1,
	int arg2)
{
	int i;

	if (string) {
		for (i = 0; i < TT_MAX; i++) {
			if (i >= ttp) {
				tt_cap[i] = string;
				tt_affected[i] = reps;
				tt_count[i] = 1;
				tt_delay[i] = msec_cost(string, reps);
				ttp++;
				break;
			}
			if (string == tt_cap[i] && reps == tt_affected[i]) {
				tt_count[i]++;
				tt_delay_used += tt_delay[i];
				break;
			}
		}
		(void) tputs(TPARM_2((NCURSES_CONST char *)string, arg1, arg2), reps, tc_putch);
	}
}

/*
**	tc_putp(string)
**
**	Output a string with tputs() translation.
**	Use this function instead of putp() so we can track
**	the actual number of characters sent.
*/
int
tc_putp(const char *string)
{
	return tputs(string, 1, tc_putch);
}


void
put_this(int c)
{				/* output one character (with padding) */
	tc_putch(c);
	if (char_padding && replace_mode)
		tt_putp(char_padding);
}


void
put_cr(void)
{
	if (translate_mode && carriage_return) {
		tt_putp(carriage_return);
	} else {
		tt_putp(TM_carriage_return);
	}
	char_count = 0;
}


void
put_lf(void)
{				/* send a linefeed (only works in RAW or
				   CBREAK mode) */
	if (translate_mode && cursor_down) {
		tt_putp(cursor_down);
	} else {
		tt_putp(TM_cursor_down);
	}
	line_count++;
}


void
put_ind(void)
{				/* scroll forward (only works in RAW or
				   CBREAK mode) */
	if (translate_mode && scroll_forward) {
		tt_putp(scroll_forward);
	} else {
		tt_putp(TM_scroll_forward);
	}
	line_count++;
}

/*
**	put_crlf()
**
**	Send (nel)  or <cr> <lf>
*/
void
put_crlf(void)
{
	if (translate_mode && newline) {
		tt_putp(newline);
	} else {
		tt_putp(TM_newline);
	}
	char_count = 0;
	line_count++;
}

/*
**	put_new_lines(count)
**
**	Send a number of newlines. (nel)
*/
void
put_newlines(int n)
{
	while (n-- > 0) {
		put_crlf();
	}
}

/*
**	putchp(character)
**
**	Send one character to the terminal.
**	This function does translation of control characters.
*/
void
putchp(int c)
{
	switch (c) {
	case '\b':
		if (translate_mode && cursor_left) {
			tt_putp(cursor_left);
		} else {
			tt_putp(TM_cursor_left);
		}
		char_count--;
		break;
	case 7:
		if (translate_mode && bell) {
			tt_putp(bell);
		} else {
			tt_putp(TM_bell);
		}
		break;
	case '\f':
		if (translate_mode && form_feed) {
			tt_putp(form_feed);
		} else {
			tt_putp(TM_form_feed);
		}
		char_count = 0;
		line_count++;
		break;
	case '\n':
		put_crlf();
		break;
	case '\r':
		put_cr();
		break;
	case '\t':
		if (translate_mode && tab) {
			tt_putp(tab);
		} else {
			tt_putp(TM_tab);
		}
		char_count = ((char_count / 8) + 1) * 8;
		break;
	default:
		put_this(c);
		char_count++;
		break;
	}
}


void
put_str(const char *s)
{				/* send the string to the terminal */
	for (; *s; putchp(*s++));
}


void
putln(const char *s)
{				/* output a string followed by a CR LF */
	for (; *s; putchp(*s++));
	put_crlf();
}


void
put_columns(const char *s, int len, int w)
{				/* put out s in column format */
	int l;

	if (char_count + w > columns) {
		put_crlf();
	}
	l = char_count % w;
	if (l) {
		while (l < w) {
			putchp(' ');
			l++;
		}
	}
	if (char_count && char_count + len >= columns) {
		put_crlf();
	}
	l = char_count;
	put_str(s);
	char_count = l + len;
}


/*
**	ptext(string)
**
**	Output a string but do not assume the terminal will wrap to a
**	new line.  Break the line at a word boundary then send a CR LF.
**	This is more esthetic on 40 column terminals.
*/
void
ptext(const char *s)
{
	const char *t;

	while (*s) {
		for (t = s + 1; *t > ' '; t++);
		if ((char_count != 0) && ((t - s) + char_count >= columns)) {
			put_crlf();
			while (*s == ' ')
				s++;
		}
		while (s < t) {
			putchp(*s++);
		}
	}
}


void
put_dec(char *f, int i)
{				/* print a line with a decimal number in it */
	char tm[128];

	sprintf(tm, f, i / 10, i % 10);
	ptext(tm);
}


void
three_digit(char *tx, int i)
{				/* convert the decimal number to a string of
				   at least 3 digits */
	if (i < 1000)
		sprintf(tx, "%d.%d", i / 10, i % 10);
	else
		sprintf(tx, "%d", i / 10);
}


void
ptextln(const char *s)
{				/* print the text using ptext() then add a CR
				   LF */
	ptext(s);
	put_crlf();
}


static void
expand_one(int ch, char **v)
{				/* expand one character */
	char *t = *v;

	if (ch & 0x80) {	/* dump it in octal (yuck) */
		*t++ = '\\';
		*t++ = '0' + ((ch >> 6) & 3);
		*t++ = '0' + ((ch >> 3) & 7);
		*t++ = '0' + (ch & 7);
		expand_chars += 4;
	} else if (ch == 127) {	/* DEL */
		*t++ = '^';
		*t++ = '?';
		expand_chars += 2;
	} else if (ch >= ' ') {
		*t++ = ch;
		expand_chars++;
	} else {	/* control characters */
		*t++ = '^';
		*t++ = ch + '@';
		expand_chars += 2;
	}
	*v = t;
}


char *
expand(const char *s)
{				/* convert the string to printable form */
	static char buf[4096];
	char *t, *v;
	int ch;

	if (magic_cookie_glitch <= 0 && exit_attribute_mode) {
		v = enter_reverse_mode;
	} else {
		v = NULL;
	}
	expand_chars = 0;
	t = buf;
	if (s) {
		for (; (ch = *s); s++) {
			if ((ch & 0x80) && v) {	/* print it in reverse video
						   mode */
				strcpy(t, liberated(TPARM_0(v)));
				for (; *t; t++);
				expand_one(ch & 0x7f, &t);
				strcpy(t, liberated(TPARM_0(exit_attribute_mode)));
				for (; *t; t++);
			} else {
				expand_one(ch, &t);
			}
		}
	}
	*t = '\0';
	return buf;
}


char *
print_expand(char *s)
{				/* convert the string to 7-bit printable form */
	static char buf[4096];
	char *t;
	int ch;

	expand_chars = 0;
	t = buf;
	if (s) {
		for (; (ch = *s); s++) {
			expand_one(ch, &t);
		}
	}
	*t = '\0';
	return buf;
}


char *
expand_to(char *s, int l)
{				/* expand s to length l */
	char *t;

	for (s = t = expand(s); *t; t++);
	for (; expand_chars < l; expand_chars++) {
		*t++ = ' ';
	}
	*t = '\0';
	return s;
}


char *
hex_expand_to(char *s, int l)
{				/* expand s to length l in hex */
	static char buf[4096];
	char *t;

	for (t = buf; *s; s++) {
		sprintf(t, "%02X ", UChar(*s));
		t += 3;
		if (t - buf > (int) sizeof(buf) - 4) {
			break;
		}
	}
	for (; t - buf < l;) {
		*t++ = ' ';
	}
	*t = '\0';
	expand_chars = t - buf;
	return buf;
}


char *
expand_command(const char *c)
{				/* expand an ANSI escape sequence */
	static char buf[256];
	int i, j, ch;
	char *s;

	s = buf;
	for (i = FALSE; (ch = UChar(*c)) != 0; c++) {
		if (i) {
			*s++ = ' ';
		}
		i = TRUE;
		if (ch < 32) {
			j = UChar(c[1]);
			if (ch == '\033' && j >= '@' && j <= '_') {
				ch = j - '@';
				c++;
				for (j = 0; (*s = c1[ch][j++]); s++);
			} else
				for (j = 0; (*s = c0[ch][j++]); s++);
		} else {
			*s++ = ch;
			j = UChar(c[1]);
			if (ch >= '0' && ch <= '9' &&
				j >= '0' && j <= '9') {
				i = FALSE;
			}
		}
	}
	*s = '\0';
	return buf;
}

/*
**	go_home()
**
**	Move the cursor to the home position
*/
void
go_home(void)
{
	int i;

	if (cursor_home)
		tt_putp(cursor_home);
	else if (cursor_address)
		tt_putparm(cursor_address, lines, 0, 0);
	else if (row_address) {	/* use (vpa) */
		put_cr();
		tt_putparm(row_address, 1, 0, 0);
	} else if (cursor_up && cursor_to_ll) {
		tt_putp(cursor_to_ll);
		for (i = 1; i < lines; i++) {
			tt_putp(cursor_up);
		}
	} else {
		can_go_home = FALSE;
		return;
	}
	char_count = line_count = 0;
	can_go_home = TRUE;
}


void
home_down(void)
{				/* move the cursor to the lower left hand
				   corner */
	int i;

	if (cursor_to_ll)
		tt_putp(cursor_to_ll);
	else if (cursor_address)
		tt_putparm(cursor_address, lines, lines - 1, 0);
	else if (row_address) {	/* use (vpa) */
		put_cr();
		tt_putparm(row_address, 1, lines - 1, 0);
	} else if (cursor_down && cursor_home) {
		tt_putp(cursor_home);
		for (i = 1; i < lines; i++)
			tt_putp(cursor_down);
	} else
		return;
	char_count = 0;
	line_count = lines - 1;
}


void
put_clear(void)
{				/* clear the screen */
	int i;

	if (clear_screen)
		tt_tputs(clear_screen, lines);
	else if (clr_eos && can_go_home) {
		go_home();
		tt_tputs(clr_eos, lines);
	} else if (scroll_forward && !over_strike && (can_go_home || cursor_up)) {
		/* clear the screen by scrolling */
		put_cr();
		if (cursor_to_ll) {
			tt_putp(cursor_to_ll);
		} else if (cursor_address) {
			tt_putparm(cursor_address, lines, lines - 1, 0);
		} else if (row_address) {
			tt_putparm(row_address, 1, lines - 1, 0);
		} else {
			for (i = 1; i < lines; i++) {
				tt_putp(scroll_forward);
			}
		}
		for (i = 1; i < lines; i++) {
			tt_putp(scroll_forward);
		}
		if (can_go_home) {
			go_home();
		} else {
			for (i = 1; i < lines; i++) {
				tt_putp(cursor_up);
			}
		}
	} else {
		can_clear_screen = FALSE;
		return;
	}
	char_count = line_count = 0;
	can_clear_screen = TRUE;
}

/*
**	wait_here()
**
**	read one character from the input stream
**	If the terminal is not in RAW mode then this function will
**	wait for a <cr> or <lf>.
*/
int
wait_here(void)
{
	char ch, cc[64];
	char message[16];
	int i, j;

	for (i = 0; i < (int) sizeof(cc); i++) {
		cc[i] = ch = getchp(STRIP_PARITY);
		if (ch == '\r' || ch == '\n') {
			put_crlf();
			char_sent = 0;
			return cc[i ? i - 1 : 0];
		}
		if (ch >= ' ') {
			if (stty_query(TTY_CHAR_MODE)) {
				put_crlf();
				char_sent = 0;
				return ch;
			}
			continue;
		}
		if (ch == 023) {	/* Control S */
			/* ignore control S, but tell me about it */
			while (ch == 023 || ch == 021) {
				ch = getchp(STRIP_PARITY);
				if (i < (int) sizeof(cc))
					cc[++i] = ch;
			}
			put_str("\nThe terminal sent a ^S -");
			for (j = 0; j <= i; j++) {
				sprintf(message, " %02X", cc[j] & 0xFF);
				put_str(message);
			}
			put_crlf();
			i = -1;
		} else if (ch != 021) {	/* Not Control Q */
			/* could be abort character */
			spin_flush();
			if (tty_can_sync == SYNC_TESTED) {
				(void) tty_sync_error();
			} else {
				put_str("\n? ");
			}
		}
	}
	return '?';
}


/*
**	read_string(buffer, length)
**
**	Read a string of characters from the input stream.
*/
void
read_string(
	char *buf,
	int length)
{
	int ch, i;

	for (i = 0; i < length - 1; ) {
		ch = getchp(STRIP_PARITY);
		if (ch == '\r' || ch == '\n') {
			break;
		}
		if (ch == '\b' || ch == 127) {
			if (i) {
				putchp('\b');
				putchp(' ');
				putchp('\b');
				i--;
			}
		} else {
			buf[i++] = ch;
			putchp(ch);
		}
	}
	buf[i] = '\0';
	put_crlf();
	char_sent = 0;
}

/*
**	maybe_wait(lines)
**
**	wait if near the end of the screen, then clear screen
*/
void 
maybe_wait(int n)
{
	if (line_count + n >= lines) {
		if (char_sent != 0) {
			ptext("Go? ");
			(void) wait_here();
		}
		put_clear();
	} else {
		put_crlf();
	}
}