~ubuntu-branches/ubuntu/utopic/splitvt/utopic

« back to all changes in this revision

Viewing changes to terminal.c

  • Committer: Bazaar Package Importer
  • Author(s): Joey Hess
  • Date: 2001-10-05 20:09:25 UTC
  • Revision ID: james.westby@ubuntu.com-20011005200925-1crvu3veaaofavdb
Tags: upstream-1.6.5
ImportĀ upstreamĀ versionĀ 1.6.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/* This is the upper layer of the splitvt terminal driver.
 
3
   It contains the termcap stuff and the output routines that 
 
4
   actually write to the terminal.
 
5
*/
 
6
 
 
7
 
 
8
#include        <stdio.h>
 
9
#include        "video.h"
 
10
#include        "terminal.h"
 
11
 
 
12
 
 
13
/* This is a long list of termcap capabilities that can be used by splitvt */
 
14
 
 
15
typedef struct {
 
16
        char *name;
 
17
        char *value;
 
18
        int   required;
 
19
        } capability;
 
20
 
 
21
static capability capabilities[] = {
 
22
#define cs 0
 
23
        { "cs", NULL, 0 },              /* Scrolling regions: VITAL! */
 
24
#define cm 1
 
25
        { "cm", NULL, 1 },              /* Cursor move to coordinates */
 
26
#define sc 2
 
27
        { "sc", NULL, 0 },              /* Save cursor position */
 
28
#define rc 3
 
29
        { "rc", NULL, 0 },              /* Restore cursor position */
 
30
#define sr 4
 
31
        { "sr", NULL, 1 },              /* Reverse scroll 1 line */
 
32
#define sf 5
 
33
        { "sf", "\n", 1 },              /* Forward scroll 1 line */
 
34
#define UP 6
 
35
        { "UP", NULL, 0 },              /* Move cursor up n lines */
 
36
#define up 7
 
37
        { "up", NULL, 0 },              /* Move cursor up 1 line */
 
38
#define DO 8
 
39
        { "DO", NULL, 0 },              /* Move cursor down n lines */
 
40
#define do 9
 
41
        { "do", NULL, 0 },              /* Move cursor down 1 line */
 
42
#define RI 10
 
43
        { "RI", NULL, 0 },              /* Move cursor right n characters */
 
44
#define nd 11
 
45
        { "nd", NULL, 0 },              /* Move cursor right 1 character */
 
46
#define LE 12
 
47
        { "LE", NULL, 0 },              /* Move cursor left n characters */
 
48
#define le 13
 
49
        { "le", "\b", 0 },              /* Move cursor left 1 character */
 
50
#define cr 14
 
51
        { "cr", "\r", 1 },              /* Carriage return */
 
52
#define DC 15
 
53
        { "DC", NULL, 0 },              /* Delete n characters */
 
54
#define dc 16
 
55
        { "dc", NULL, 0 },              /* Delete character */
 
56
#define DL 17
 
57
        { "DL", NULL, 0 },              /* Delete n lines */
 
58
#define dl 18
 
59
        { "dl", NULL, 0 },              /* Delete line */
 
60
#define ce 19
 
61
        { "ce", NULL, 1 },              /* Clear to end of line */
 
62
#define so 20
 
63
        { "so", NULL, 0 },              /* Start standout text mode */
 
64
#define se 21
 
65
        { "se", NULL, 0 },              /* End standout text mode */
 
66
#define us 22
 
67
        { "us", NULL, 0 },              /* Start underline text mode */
 
68
#define ue 23
 
69
        { "ue", NULL, 0 },              /* End underline text mode */
 
70
#define md 24
 
71
        { "md", NULL, 0 },              /* Start bold text mode */
 
72
#define mb 25
 
73
        { "mb", NULL, 0 },              /* Start blinking text mode */
 
74
#define mr 26
 
75
        { "mr", NULL, 0 },              /* Start reverse text mode */
 
76
#define mh 27
 
77
        { "mh", NULL, 0 },              /* Start dim text mode */
 
78
#define me 28
 
79
        { "me", NULL, 0 },              /* Clear all text attributes */
 
80
#define ZF 29
 
81
        { "ZF", NULL, 0 },              /* Set wide (132 column) mode */
 
82
#define IC 30
 
83
        { "IC", NULL, 0 },              /* Insert character */
 
84
        { NULL, NULL, 0 }
 
85
        };
 
86
 
 
87
#ifdef TERMCAP  /* Termcap versions of the screen update routines */
 
88
 
 
89
/* areas and variables used to get termcap information */
 
90
 
 
91
extern int tgetent();
 
92
extern char *tgetstr();
 
93
 
 
94
static char termcap_entry[2048];        /* termcap entry for the terminal */
 
95
static char termcap_area[2048];         /* Static buffer for capabilities */
 
96
static char *tp=termcap_area;           /* Used in tgetent() calls */
 
97
static char *terminal_type;             /* type of terminal */
 
98
 
 
99
char *termcap_init(termtype)
 
100
char *termtype;
 
101
{
 
102
        static char errmesg[BUFSIZ];
 
103
        char *value;
 
104
        int i;
 
105
 
 
106
        /* Determine the terminal type and get the termcap entry */
 
107
        if ( tgetent(termcap_entry, termtype) < 1 ) {
 
108
                sprintf(errmesg,
 
109
        "Can't find termcap entry for terminal type \"%s\"", terminal_type);
 
110
                return(errmesg);
 
111
        }
 
112
 
 
113
        /* Make sure this terminal has all the required capabilities */ 
 
114
        for ( i=0; capabilities[i].name; ++i ) {
 
115
                if ( (value=tgetstr(capabilities[i].name, &tp)) == NULL ) {
 
116
                        if ( !capabilities[i].value && 
 
117
                                                capabilities[i].required ) {
 
118
                                sprintf(errmesg, 
 
119
                        "Your terminal lacks the %s termcap capability.",
 
120
                                                capabilities[i].name);
 
121
                                return(errmesg);
 
122
                        }
 
123
                } else
 
124
                        capabilities[i].value=value;
 
125
        }
 
126
        return(NULL);
 
127
}
 
128
void outc(c) int c; { putchar(c); }
 
129
 
 
130
void vt_rows_cols(termtype, rows, cols)
 
131
char *termtype;
 
132
int *rows;
 
133
int *cols;
 
134
{
 
135
        if ( rows ) {
 
136
                if ( (*rows=tgetnum("li")) <= 0 )
 
137
                        *rows=24;
 
138
        }
 
139
        if ( cols ) {
 
140
                if ( (*cols=tgetnum("co")) <= 0 )
 
141
                        *cols=80;
 
142
        }
 
143
}
 
144
void vt_bold(on)
 
145
int on;
 
146
{
 
147
        if ( on && capabilities[md].value ) 
 
148
                tputs(capabilities[md].value, 1, outc);
 
149
        else
 
150
                printf("\033[%sm", (on ? "1" : "22"));
 
151
}
 
152
void vt_underline(on)
 
153
int on;
 
154
{
 
155
        if ( capabilities[on ? us : ue].value ) 
 
156
                tputs(capabilities[on ? us : ue].value, 1, outc);
 
157
        else
 
158
                printf("\033[%sm", on ? "4" : "24");
 
159
}
 
160
void vt_blink(on)
 
161
int on;
 
162
{
 
163
        if ( on && capabilities[mb].value ) 
 
164
                tputs(capabilities[mb].value, 1, outc);
 
165
        else
 
166
                printf("\033[%sm", on ? "5" : "25");
 
167
}
 
168
void vt_reverse(on)
 
169
int on;
 
170
{
 
171
        if ( on && capabilities[mr].value ) 
 
172
                tputs(capabilities[mr].value, 1, outc);
 
173
        else
 
174
                printf("\033[%sm", on ? "7" : "27");
 
175
}
 
176
void vt_resetattr()
 
177
{
 
178
        if ( capabilities[me].value ) 
 
179
                tputs(capabilities[me].value, 1, outc);
 
180
        else
 
181
                printf("\033[m");
 
182
}
 
183
void vt_widemode(on)
 
184
int on;
 
185
{
 
186
        if ( on && capabilities[ZF].value ) 
 
187
                tputs(capabilities[ZF].value, 1, outc);
 
188
        else
 
189
                printf("\033[?3%c", on ? 'h' : 'l');
 
190
}
 
191
void vt_savecursor()
 
192
{
 
193
        if ( capabilities[sc].value )
 
194
                tputs(capabilities[sc].value, 1, outc);
 
195
        else
 
196
                printf("\0337");
 
197
}
 
198
void vt_restcursor()
 
199
{
 
200
        if ( capabilities[rc].value )
 
201
                tputs(capabilities[rc].value, 1, outc);
 
202
        else
 
203
                printf("\0338");
 
204
}
 
205
#else
 
206
char *termcap_init(termtype)
 
207
char *termtype;
 
208
{
 
209
        return(NULL);
 
210
}
 
211
void vt_rows_cols(termtype, rows, cols)
 
212
char *termtype;
 
213
int *rows;
 
214
int *cols;
 
215
{
 
216
        if ( cols ) {
 
217
                if ( strcmp("vt100-w", termtype) == 0 )
 
218
                { /* vt100-wide terminal (132 columns) */
 
219
                        *cols=132;
 
220
                } else
 
221
                        *cols = 80;
 
222
        } 
 
223
        if ( rows )
 
224
                *rows = 24;
 
225
}
 
226
void vt_bold(on)
 
227
int on;
 
228
{
 
229
        printf("\033[%sm", (on ? "1" : "22"));
 
230
}
 
231
void vt_underline(on)
 
232
int on;
 
233
{
 
234
        printf("\033[%sm", on ? "4" : "24");
 
235
}
 
236
void vt_blink(on)
 
237
int on;
 
238
{
 
239
        printf("\033[%sm", on ? "5" : "25");
 
240
}
 
241
void vt_reverse(on)
 
242
int on;
 
243
{
 
244
        printf("\033[%sm", on ? "7" : "27");
 
245
}
 
246
void vt_resetattr()
 
247
{
 
248
        printf("\033[m");
 
249
}
 
250
void vt_widemode(on)
 
251
int on;
 
252
{
 
253
        printf("\033[?3%c", on ? 'h' : 'l');
 
254
}
 
255
void vt_savecursor()
 
256
{
 
257
        printf("\0337");
 
258
}
 
259
void vt_restcursor()
 
260
{
 
261
        printf("\0338");
 
262
}
 
263
#endif
 
264
                /* vt100 compatible versions of the screen update routines */
 
265
char *vt_initterm(terminal_type, rows, cols)
 
266
char *terminal_type;
 
267
int *rows;
 
268
int *cols;
 
269
{
 
270
        extern char *getenv();
 
271
        static char *termtype=NULL, *error=NULL;
 
272
 
 
273
        if ( (termtype=getenv("TERM")) == NULL )
 
274
                return("Terminal type must be set to vt100");
 
275
 
 
276
        if ( strncmp(termtype, "vt10x", 4) != 0 &&
 
277
                        /* A vt10x emulation detector -->  */   ! vttest() )
 
278
                return("Terminal type must be set to vt100");
 
279
 
 
280
        if ( strlen(termtype) > 256 )
 
281
                return("Terminal type too long");
 
282
 
 
283
        /* Get extra (possibly) capabilities from termcap */
 
284
        if ( (error=termcap_init(termtype)) != NULL )
 
285
                return(error);
 
286
 
 
287
        vt_rows_cols(termtype, rows, cols);
 
288
        if ( terminal_type )
 
289
                strcpy(terminal_type, termtype);
 
290
        return(NULL);
 
291
}
 
292
void vt_bell()
 
293
{
 
294
        printf("\007");
 
295
}
 
296
void vt_goto(row, col)
 
297
int row, col;
 
298
{
 
299
        printf("\033[%d;%dH", row, col);
 
300
}
 
301
void vt_up(numrows)
 
302
int numrows;
 
303
{
 
304
        printf("\033[%dA", numrows);
 
305
}
 
306
void vt_down(numrows)
 
307
int numrows;
 
308
{
 
309
        printf("\033[%dB", numrows);
 
310
}
 
311
void vt_right(numcols)
 
312
int numcols;
 
313
{
 
314
        printf("\033[%dC", numcols);
 
315
}
 
316
void vt_left(numcols)
 
317
int numcols;
 
318
{
 
319
        printf("\033[%dD", numcols);
 
320
}
 
321
void vt_clrscr()
 
322
{
 
323
        printf("\033[2J");
 
324
}
 
325
void vt_clreos()
 
326
{
 
327
        printf("\033[J");
 
328
}
 
329
void vt_clrbgs()
 
330
{
 
331
        printf("\033[1J");
 
332
}
 
333
void vt_clrline()
 
334
{
 
335
        printf("\033[2K");
 
336
}
 
337
void vt_clreol()
 
338
{
 
339
        printf("\033[K");
 
340
}
 
341
void vt_clrbgl()
 
342
{
 
343
        printf("\033[1K");
 
344
}
 
345
void vt_delunder(num)
 
346
int num;
 
347
{
 
348
        printf("\033[%dP", num);
 
349
}
 
350
void vt_delline(num)
 
351
int num;
 
352
{
 
353
        printf("\033[%dM", num);
 
354
}
 
355
void vt_insline(num)
 
356
int num;
 
357
{
 
358
        printf("\033[%dL", num);
 
359
}
 
360
void vt_setattr(textattr)
 
361
int textattr;
 
362
{
 
363
        vt_resetattr();
 
364
        
 
365
        if ( textattr&BOLD )
 
366
                vt_bold(1);
 
367
        if ( textattr&UNDERLINE )
 
368
                vt_underline(1);
 
369
        if ( textattr&BLINK )
 
370
                vt_blink(1);
 
371
        if ( textattr&REVERSE )
 
372
                vt_reverse(1);
 
373
}
 
374
void vt_setfg(color)
 
375
int color;
 
376
{
 
377
        printf("\033[%dm", color+30);
 
378
}
 
379
void vt_setbg(color)
 
380
int color;
 
381
{
 
382
        printf("\033[%dm", color+40);
 
383
}
 
384
void vt_altcharset(charset, type)
 
385
int charset;
 
386
int type;
 
387
{
 
388
        switch (type) {
 
389
                case UK_CHARSET:
 
390
                                printf("\033%cA", (charset == G0 ? '(' : ')'));
 
391
                                break;
 
392
                case US_CHARSET:
 
393
                                printf("\033%cB", (charset == G0 ? '(' : ')'));
 
394
                                break;
 
395
                case GRAPHICS:  
 
396
                                printf("\033%c0", (charset == G0 ? '(' : ')'));
 
397
                                break;
 
398
                default:        break;
 
399
        }
 
400
}
 
401
void vt_setscroll(upper, lower)
 
402
int upper, lower;
 
403
{
 
404
        if ( !upper && !lower ) {
 
405
                printf("\033[r");
 
406
        } else {
 
407
                printf("\033[%d;%dr", upper, lower);
 
408
        }
 
409
}
 
410
void vt_revscroll()
 
411
{
 
412
        printf("\033M");
 
413
}
 
414
void vt_keystate(application)
 
415
int application;
 
416
{
 
417
        /* Set and reset the numeric keypad and arrowkeys */
 
418
        if ( application )
 
419
                printf("\033=\033[?1h");
 
420
        else
 
421
                printf("\033>\033[?1l");
 
422
}
 
423
void vt_insertchar(numcols)
 
424
{
 
425
        printf("\033[%d@", numcols);
 
426
}
 
427
void vt_reset()
 
428
{
 
429
        printf("\033c");
 
430
}
 
431
void vt_update()
 
432
{
 
433
        fflush(stdout);
 
434
}