-
Notifications
You must be signed in to change notification settings - Fork 1
/
bm_unix.c
288 lines (238 loc) · 4.47 KB
/
bm_unix.c
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
/* bm_unix.c - Unix specific subroutines for BMORE
*
* 2000-05-31 V 1.3.0 beta
* 2000-10-12 V 1.3.0 final
* 2002-02-10 V 1.3.1
* 2003-07-04 V 1.3.2
* 2010-06-02 V 1.3.4
* 2013-08-22 V 1.4.0
* 2019-10-09 V 1.4.1
*
* NOTE: Edit this file with tabstop=4 !
*
* Copyright 1996-2019 by Gerhard Buergmann
*
* 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, or (at your option) any
* later version.
*
* This program 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.
*
* See file COPYING for information on distribution conditions.
*/
#include "bmore.h"
#include <termios.h>
#define TBUFSIZ 1024
#define stty(fd,argp) tcsetattr(fd,TCSAFLUSH,argp)
struct termios ostate, nstate;
char *rev_start, *rev_end; /* enter and exit standout mode */
char *Home; /* go to home */
char *clear_sc; /* clear screen */
char *erase_ln; /* erase line */
extern off_t bytepos, screen_home;
extern FILE *curr_file;
int got_int;
int fnum, no_intty, no_tty, slow_tty;
int dum_opt, dlines;
#ifdef HAVE_NCURSES_H
# undef NEED_PUTC_CHAR
#endif
/*
* A real function, for the tputs routine
*/
#ifdef NEED_PUTC_CHAR
int
putchr(char ch)
{return putchar((int)ch);}
#else
int
putchr(ch)
int ch;
{return putchar(ch);}
#endif
void
initterm()
{
char buf[TBUFSIZ];
static char clearbuf[TBUFSIZ];
char *term;
char *clearptr;
struct termios nstate;
no_tty = tcgetattr(fileno(stdout), &ostate);
if (!no_tty) {
nstate = ostate;
/*
* is this really necessary??
*
nstate.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHONL);
*/
nstate.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHONL);
nstate.c_cc[VMIN] = 1;
nstate.c_cc[VTIME] = 0;
tcsetattr(fileno(stdin), TCSADRAIN, &nstate);
}
#ifdef DJGPP
maxx = 80;
maxy = 25;
#else
if ((term = getenv("TERM")) == 0 || tgetent(buf, term) <= 0) {
printf("Dumb terminal\n");
maxx = 80;
maxy = 24;
} else {
maxy = tgetnum("li");
maxx = tgetnum("co");
}
clearptr = clearbuf;
erase_ln = tgetstr("ce", &clearptr);
clear_sc = tgetstr("cl", &clearptr);
rev_start = tgetstr("so", &clearptr);
rev_end = tgetstr("se", &clearptr);
#endif
no_intty = tcgetattr(fileno(stdin), &ostate);
tcgetattr(fileno(stderr), &ostate);
nstate = ostate;
if (!no_tty) {
ostate.c_lflag &= ~(ICANON|ECHO);
}
}
void
set_tty()
{
if (no_tty) return;
ostate.c_lflag &= ~(ICANON|ECHO);
stty(fileno(stderr), &ostate);
}
void
reset_tty()
{
if (no_tty) return;
ostate.c_lflag |= ICANON|ECHO;
stty(fileno(stderr), &ostate);
}
void
sig(sig)
int sig;
{
reset_tty();
printf("\r\n");
exit(0);
}
/*
* doshell() - run a command or an interactive shell
*/
void
doshell(cmd)
char *cmd;
{
int ret;
#ifndef DJGPP
char *getenv();
char *shell;
char cline[128];
#endif
printf("\n");
#ifndef DJGPP
if ((shell = getenv("SHELL")) == NULL) shell = "sh";
else if(strrchr(shell,'/')) shell=(char *)(strrchr(shell,'/')+1);
if (cmd[0] == '\0') {
sprintf(cline, "%s -i", shell);
cmd = cline;
} else {
sprintf(cline, "%s -c \"%s\"", shell, cmd);
cmd = cline;
}
#endif
reset_tty();
ret = system(cmd);
set_tty();
printf("\r");
home();
fseeko(curr_file, screen_home, SEEK_SET);
bytepos = screen_home;
}
void
highlight()
{
#ifndef DJGPP
if (rev_start && rev_end)
tputs(rev_start, 1, putchr);
#endif
}
void
normal()
{
#ifndef DJGPP
if (rev_start && rev_end)
tputs(rev_end, 1, putchr);
#endif
}
void
clearscreen()
{
#ifdef DJGPP
/* if (!no_tty)
{
int n;
for (n = 0; n < maxy; n++) {
cleartoeol();
printf("\n");
}
} */
#else
tputs(clear_sc, 1, putchr);
#endif
}
void
home()
{
#ifdef DJGPP
if (!no_tty) printf("\r");
#else
tputs(Home, 1, putchr);
#endif
}
/* force clear to end of line */
void
cleartoeol()
{
#ifdef DJGPP
int n;
home();
if (!no_tty) for (n = 1; n < maxx; n++) printf(" ");
home();
#else
tputs(erase_ln, 1, putchr);
#endif
}
int
vgetc()
{
char cha;
extern int errno;
errno = 0;
if (read(2, &cha, 1) <= 0) {
if (errno != EINTR)
exit(2);
}
return (cha);
}
#ifndef HAVE_MEMMOVE
/*
* Copy contents of memory (with possible overlapping).
*/
char *
memmove(s1, s2, n)
char *s1;
char *s2;
size_t n;
{
bcopy(s2, s1, n);
return(s1);
}
#endif