File: term.c

package info (click to toggle)
hx 0.7.14-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 564 kB
  • ctags: 834
  • sloc: ansic: 7,901; sh: 152; makefile: 81
file content (289 lines) | stat: -rw-r--r-- 5,503 bytes parent folder | download | duplicates (3)
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
#include "hx_types.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <termcap.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include "term.h"
#include "screen.h"

int LI = 24, CO = 80;
char term_name[256] = "vt100", *HO, *ME, *MR, *US, *CL, *CE, *CM, *CS, *SF, *SR, *DC, *IC;
static char termcap[2048], tbuf[2048];

#define ttyfd 0

static struct termios old_tty, new_tty;

#define term_putc ((int(*)(int))__term_putc)

static void
__term_putc (int c)
{
	char ch = (char)c;

	write(1, &ch, 1);
}

int
term_init (void)
{
	char *ep, *bp = tbuf;

	if ((ep = getenv("TERM"))) {
		strncpy(term_name, ep, sizeof term_name - 1);
		term_name[sizeof term_name - 1] = 0;
	}
	switch (tgetent(termcap, term_name)) {
		case -1:
			term_printf("tgetent had trouble accessing the termcap database\n");
			return -1;
		case 0:
			term_printf("tgetent did not find an entry for '%s'\n", term_name);
			return -1;
	}
	if (!(ep = getenv("COLUMNS")) || !(CO = atoi(ep))) {
		if ((CO = tgetnum("co")) == -1)
			CO = 80;
	}
	if (!(ep = getenv("LINES")) || !(LI = atoi(ep))) {
		if ((LI = tgetnum("li")) == -1)
			LI = 24;
	}

	HO = tgetstr("ho", &bp);
	ME = tgetstr("me", &bp);
	MR = tgetstr("mr", &bp);
	US = tgetstr("us", &bp);
	CL = tgetstr("cl", &bp);
	CM = tgetstr("cm", &bp);
	CE = tgetstr("ce", &bp);
	CS = tgetstr("cs", &bp);
	DC = tgetstr("dc", &bp);
	IC = tgetstr("ic", &bp);
	SF = tgetstr("sf", &bp);
	SR = tgetstr("sr", &bp);

#ifndef __hpux__
	ep = tgetstr("pc", &bp);
	PC = ep ? *ep : 0;
	BC = tgetstr("le", &bp);
	UP = tgetstr("up", &bp);
#endif

	tcgetattr(ttyfd, &old_tty);
	new_tty = old_tty;
	new_tty.c_cc[VMIN] = 1;
	new_tty.c_cc[VTIME] = 0;
	new_tty.c_lflag &= ~(ICANON|ECHO);
	new_tty.c_iflag &= ~IXON;
	tcsetattr(ttyfd, TCSADRAIN, &new_tty);

	term_install_signal_handlers();

	return ttyfd;
}

void
term_puts (const char *buf, unsigned int len)
{
	write(1, buf, len);
}

void
term_printf (const char *fmt, ...)
{
	va_list ap;
	char buf[4096];
	int len;

	va_start(ap, fmt);
	if ((len = vsnprintf(buf, sizeof buf, fmt, ap)) == -1)
		len = sizeof buf;
	va_end(ap);

	write(1, buf, (unsigned)len);
}

#ifndef RETSIGTYPE
#define RETSIGTYPE void
#endif

void
term_install_signal_handlers (void)
{
	struct sigaction act;

	memset(&act, 0, sizeof act);

	act.sa_handler = SIG_IGN;
	sigaction(SIGPIPE, &act, 0);
#ifdef IRIX
	sigaction(SIGFPE, &act, 0);
#endif

	act.sa_handler = (RETSIGTYPE(*)(int))term_sigcont;
	sigaction(SIGINT, &act, 0);
	sigaction(SIGCONT, &act, 0);

	act.sa_handler = (RETSIGTYPE(*)(int))term_sigtstp;
	sigaction(SIGTSTP, &act, 0);

	act.sa_handler = (RETSIGTYPE(*)(int))term_sigwinch;
	sigaction(SIGWINCH, &act, 0);
}

void
term_set_scroll_region (int first, int last)
{
	tputs(tgoto(CS, last, first), 0, term_putc);
}

void
term_scroll_up (void)
{
	tputs(SR, 0, term_putc);
}

void
term_scroll_down (void)
{
	tputs(SF, 0, term_putc);
}

void
term_ce (void)
{
	tputs(CE, 0, term_putc);
}

void
term_clear (void)
{
	tputs(CL, 0, term_putc);
}

void
term_home (void)
{
	tputs(HO, 0, term_putc);
}

void
term_goto_line (int line)
{
	tputs(tgoto(CM, 0, line), 0, term_putc);
}

void
term_move_cursor (int line, int column)
{
	tputs(tgoto(CM, column, line), 0, term_putc);
}

void
term_char_delete (void)
{
	tputs(DC, 0, term_putc);
}

void
term_char_insert (void)
{
	tputs(IC, 0, term_putc);
}

void
term_mode_clear (void)
{
	tputs(ME, 0, term_putc);
}

void
term_mode_underline (void)
{
	tputs(US, 0, term_putc);
}

void
term_mode_reverse (void)
{
	tputs(MR, 0, term_putc);
}

void
term_reset (void)
{
	tcsetattr(ttyfd, TCSADRAIN, &old_tty);
	term_set_scroll_region(0, -1);
	term_move_cursor(LI, 0);
}

RETSIGTYPE
term_sigwinch (void)
{
	struct winsize win_size;
	char LINES[24], COLUMNS[24];

	if (ioctl(1, TIOCGWINSZ, &win_size))
		goto ret;

	LI = win_size.ws_row;
	CO = win_size.ws_col;
#ifdef USE_SETENV
	sprintf(LINES, "%d", win_size.ws_row);
	setenv("LINES", LINES, 1);
	sprintf(COLUMNS, "%d", win_size.ws_col);
	setenv("COLUMNS", COLUMNS, 1);
#else
	sprintf(LINES, "LINES=%d", win_size.ws_row);
	putenv(LINES);
	sprintf(COLUMNS, "COLUMNS=%d", win_size.ws_col);
	putenv(COLUMNS);
#endif
	scr_resize_all(LI - 4, CO);
	term_set_scroll_region(0, (signed)l_curscr->li);
	term_move_cursor(LI, l_curscr->history.cur->pos % CO);
ret:
	scr_draw(l_curscr);
	{}
}

RETSIGTYPE
term_sigtstp (void)
{
	tcgetattr(ttyfd, &new_tty);
	term_reset();
	write(1, "\r", 1);
	kill(getpid(), SIGSTOP);
}

RETSIGTYPE
term_sigcont (void)
{
	tcsetattr(ttyfd, TCSADRAIN, &new_tty);
	term_set_scroll_region(0, (signed)l_curscr->li);
	scr_draw(l_curscr);
}

/* from BitchX/source/misc.c */

#ifdef REVERSE_WHITE_BLACK
char const *colour_str[] = {
"","","","","","","","",
"","","","","","","","", "",
"", "", "","", "","","", "",
"", "", "","", "","","", "",
"", "", "", ""};
#else
char const *colour_str[] = {
"","","","","","","","",
"","","","","","","","", "",
"", "", "","", "","","", "",
"", "", "","", "","","", "",
"", "", "", ""};
#endif