File: term.c

package info (click to toggle)
ucblogo 5.5-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 6,048 kB
  • ctags: 3,560
  • sloc: ansic: 15,086; lisp: 5,588; makefile: 175
file content (310 lines) | stat: -rw-r--r-- 6,464 bytes parent folder | download | duplicates (2)
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
/*
 *	term.c		terminal cursor control			dvb
 *
 *	Copyright (C) 1993 by the Regents of the University of California
 *
 *      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.
 *  
 *      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.
 *  
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "logo.h"
#include "globals.h"

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifdef mac
#include <console.h>
#endif

#ifdef HAVE_TERMIO_H
#include <termio.h>
#else
#ifdef HAVE_SGTTY_H
#include <sgtty.h>
#endif
#endif

#undef TRUE
#undef FALSE

#ifdef HAVE_TERMCAP_H
#include <termcap.h>
#else
#ifdef HAVE_TERMLIB_H
#include <termlib.h>
#else
#ifdef HAVE_CURSES_H
#include <curses.h>
#endif
#endif
#endif

#undef TRUE
#undef FALSE

#define FALSE	0
#define TRUE	1

int x_coord, y_coord, x_max, y_max;

char PC;
char *BC;
char *UP;
short ospeed;
char bp[1024];
char cl_arr[40];
char cm_arr[40];
char so_arr[40];
char se_arr[40];

#ifdef HAVE_TERMIO_H
struct termio tty_cooked, tty_cbreak;
#else
#ifdef HAVE_SGTTY_H
struct sgttyb tty_cooked, tty_cbreak;
#endif
#endif

int interactive, tty_charmode;

extern char **environ, *tgoto(), *tgetstr();

char *termcap_ptr;

int termcap_putter(char ch) {
    *termcap_ptr++ = ch;
    return 0;
}

#ifdef unix
void termcap_getter(char *cap, char *buf) {
    char temp[40];
    char *str;
    char *temp_ptr = temp;

    termcap_ptr = buf;
    str=tgetstr(cap,&temp_ptr);
    /* if (str == NULL) str = temp; */
    tputs(str,1,termcap_putter);
}
#endif

void term_init(void) {
#ifdef unix
    char *emacs; /* emacs change */
    int term_sg;
#endif

#ifdef WIN32
    interactive = 1;
#else
    interactive = isatty(0);
#endif

#ifdef mac
    term_init_mac();
    return;
#else
#ifdef ibm
#ifndef WIN32
    term_init_ibm();
#endif /* WIN32 */
#else
    if (interactive) {
#ifdef HAVE_TERMIO_H
	ioctl(0,TCGETA,(char *)(&tty_cooked));
	tty_cbreak = tty_cooked;
	tty_cbreak.c_cc[VMIN] = '\01';
	tty_cbreak.c_cc[VTIME] = '\0';
	tty_cbreak.c_lflag &= ~(ECHO|ICANON);
#else
	ioctl(0,TIOCGETP,(char *)(&tty_cooked));
	tty_cbreak = tty_cooked;
	tty_cbreak.sg_flags |= CBREAK;
	tty_cbreak.sg_flags &= ~ECHO;
#endif
    }
    tty_charmode = 0;
    tgetent(bp, getenv("TERM"));

    /* emacs changes */

    emacs = getenv("EMACS");

    /* check if started from emacs */
    if (!emacs || *emacs != 't') { /* read from termcap */
      x_max = tgetnum("co");
      y_max = tgetnum("li");
    }
    else { /* read environment variables */
      emacs = getenv("COLUMNS");
      if (!emacs) x_max = 0;
      else x_max = atoi(emacs);
      emacs = getenv("LINES");
      if (!emacs) y_max = 0;
      else y_max = atoi(emacs);
    } 

    /* end emacs changes */

    if (x_max <= 0) x_max = 80;
    if (y_max <= 0) y_max = 24;
    term_sg = tgetnum("sg");

    x_coord = y_coord = 0;
    termcap_getter("cm", cm_arr);
    termcap_getter("cl", cl_arr);

    if (term_sg <= 0) {
	termcap_getter("so", so_arr);
	termcap_getter("se", se_arr);
    } else	/* don't mess with stupid standout modes */
	so_arr[0] = se_arr[0] = '\0';
#endif
#endif
}

void charmode_on() {
#ifdef unix
    if ((readstream == stdin) && interactive && !tty_charmode) {
#ifdef HAVE_TERMIO_H
	ioctl(0,TCSETA,(char *)(&tty_cbreak));
#else /* !HAVE_TERMIO_H */
	ioctl(0,TIOCSETP,(char *)(&tty_cbreak));
#endif /* HAVE_TERMIO_H */
	tty_charmode++;
    }
#endif /* unix */
#ifdef WIN32
    win32_charmode_on();
#endif
}

void charmode_off() {
#ifdef unix
    if (tty_charmode) {
#ifdef HAVE_TERMIO_H
	ioctl(0,TCSETA,(char *)(&tty_cooked));
#else /* !HAVE_TERMIO_H */
	ioctl(0,TIOCSETP,(char *)(&tty_cooked));
#endif /* HAVE_TERMIO_H */
	tty_charmode = 0;
    }
#endif /* unix */
#ifdef WIN32
    win32_charmode_off();
#endif
}

NODE *lcleartext(NODE *args) {
#ifdef mac
    cgotoxy(x_margin + 1, y_margin + 1, stdout);
    ccleos(stdout);
#else
#ifdef ibm
#ifndef WIN32 /* sowings */
    ibm_clear_text();
    ibm_gotoxy(x_margin, y_margin);
#else /* WIN32 */
    win32_clear_text();
#endif /* WIN32 || !Win32 */
#else /* !ibm */
    printf("%s", cl_arr);
    printf("%s", tgoto(cm_arr, x_margin, y_margin));
#endif /* ibm */
#endif /* mac */

#ifdef WIN32
	win32_update_text();
#else
	fflush(stdout); /* do it now! */
#endif
#if defined(__RZTC__)
	zflush();
#endif

    x_coord = x_margin;
    y_coord = y_margin;
    return(UNBOUND);
}

NODE *lcursor(NODE *args) {
    return(cons(make_intnode((FIXNUM)(x_coord-x_margin)),
		cons(make_intnode((FIXNUM)(y_coord-y_margin)), NIL)));
}

NODE *lsetcursor(NODE *args) {
#ifdef WIN32
    return (win32_lsetcursor(args));
#else /* !win32 */
    NODE *arg;

    arg = pos_int_vector_arg(args);
    if (NOT_THROWING) {
	x_coord = x_margin + getint(car(arg));
	y_coord = y_margin + getint(cadr(arg));
	while ((x_coord >= x_max || y_coord >= y_max) && NOT_THROWING) {
	    setcar(args, err_logo(BAD_DATA, arg));
	    if (NOT_THROWING) {
		arg = pos_int_vector_arg(args);
		x_coord = x_margin + getint(car(arg));
		y_coord = y_margin + getint(cadr(arg));
	    }
	}
    }
    if (NOT_THROWING) {
#ifdef mac
	mac_gotoxy(x_coord, y_coord);
#else
#ifdef ibm
	ibm_gotoxy(x_coord, y_coord);
#else
	printf("%s", tgoto(cm_arr, x_coord, y_coord));
#endif
#endif
	fflush(stdout);
#ifdef __RZTC__
	zflush();
#endif
    }
    return(UNBOUND);
#endif /* !win32 (for non-windows version of this code) */
}

NODE *lsetmargins(NODE *args) {
    NODE *arg;

    arg = pos_int_vector_arg(args);
    if (NOT_THROWING) {
	x_margin = getint(car(arg));
	y_margin = getint(cadr(arg));
	lcleartext(NIL);
    }
    return(UNBOUND);
}

NODE *lstandout(NODE *args) {
    char textbuf[300];
    char fmtbuf[100];

    sprintf(fmtbuf,"%s%%p%s",so_arr,se_arr);
    print_stringptr = textbuf;
    print_stringlen = 300;
    ndprintf((FILE *)NULL,fmtbuf,car(args));
    *print_stringptr = '\0';
    return(make_strnode(textbuf,NULL,(int)strlen(textbuf),STRING,strnzcpy));
}