Add xterm-256color as a valid terminal.
[eco.git] / term.c
blob18a42b8f1003ea256480def3f0a3e98e89a60456
1 /*
2 * Copyright (C) 2008 Diego Hernan Borghetti.
3 * Eco
4 */
6 #include <sys/ioctl.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <termios.h>
11 #include <errno.h>
12 #include <unistd.h>
14 #include "term.h"
15 #include "config.h"
18 static struct termios otermios; /* original terminal characteristics */
19 static struct termios ntermios; /* characteristics to use inside */
21 E_Term *e_term_open(void)
23 struct winsize ws;
24 E_Term *tr;
25 char *s;
27 tr= (E_Term *)malloc(sizeof(E_Term));
28 if (!tr) {
29 perror("malloc");
30 exit(-1);
33 tr->name= getenv("TERM");
34 if (!tr->name) {
35 printf("Shell variable TERM not defined!");
36 exit(-1);
39 if (strcmp(tr->name, "vt100") && strcmp(tr->name, "linux") &&
40 strcmp (tr->name, "xterm") && strcmp(tr->name, "ansi") &&
41 strcmp (tr->name, "xterm-256color")) {
42 printf("Invalid terminal type: [%s]", tr->name);
43 exit(1);
47 tcgetattr(0, &otermios);
49 ntermios= otermios;
50 ntermios.c_iflag&= ~(IGNBRK | BRKINT | IGNPAR | PARMRK
51 | INPCK | INLCR | IGNCR | ICRNL | IXON);
53 ntermios.c_oflag&= ~(OPOST | ONLCR | OLCUC | OCRNL | ONOCR | ONLRET);
55 ntermios.c_lflag&= ~(ISIG | ICANON | XCASE | ECHO | ECHOE | ECHOK
56 | ECHONL | NOFLSH | TOSTOP | ECHOCTL |
57 ECHOPRT | ECHOKE | FLUSHO | PENDIN | IEXTEN);
59 ntermios.c_cc[VMIN] = 1;
60 ntermios.c_cc[VTIME] = 0;
61 tcsetattr(0, TCSADRAIN, &ntermios);
63 /* init default entrys. */
64 tr->col= 999;
65 tr->row= 999;
66 tr->fgcolor= E_TR_WHITE;
67 tr->bgcolor= E_TR_BLACK;
69 if (ioctl(0, TIOCGWINSZ, &ws)) {
70 s= getenv("LINES");
71 if (!s)
72 tr->nrow= 23;
73 else {
74 tr->nrow= atoi(s);
75 tr->nrow--;
78 s= getenv("COLUMNS");
79 if (!s)
80 tr->ncol= 80;
81 else
82 tr->ncol= atoi(s);
84 else {
85 tr->nrow= ws.ws_row-1;
86 tr->ncol= ws.ws_col;
89 return(tr);
92 void e_term_close(E_Term *tr)
94 e_term_fgcol(tr, E_TR_WHITE);
95 e_term_bgcol(tr, E_TR_BLACK);
96 e_term_eeop(tr);
97 tcsetattr(0, TCSADRAIN, &otermios);
100 void e_term_putc(int c)
102 fputc(c, stdout);
105 void e_term_flush(void)
107 int status;
109 status= fflush(stdout);
110 while (status < 0 && errno == EAGAIN) {
111 sleep(1);
112 status= fflush(stdout);
114 if (status < 0)
115 exit(15);
118 int e_term_getc(void)
120 return(255 & fgetc(stdin));
123 void e_term_parm(int n)
125 int q, r;
127 q= n / 10;
128 if (q != 0) {
129 r= q / 10;
130 if (r != 0) {
131 e_term_putc((r % 10) + '0');
133 e_term_putc((q % 10) + '0');
135 e_term_putc((n % 10) + '0');
138 void e_term_fgcol(E_Term *tr, int color)
140 if (color != tr->fgcolor) {
141 e_term_putc(E_TR_ESC);
142 e_term_putc('[');
143 e_term_parm(color + 30);
144 e_term_putc('m');
145 tr->fgcolor= color;
149 void e_term_bgcol(E_Term *tr, int color)
151 if (color != tr->bgcolor) {
152 e_term_putc(E_TR_ESC);
153 e_term_putc('[');
154 e_term_parm(color + 40);
155 e_term_putc('m');
156 tr->bgcolor= color;
160 void e_term_move(int row, int col)
162 e_term_putc(E_TR_ESC);
163 e_term_putc('[');
164 e_term_parm(row + 1);
165 e_term_putc(';');
166 e_term_parm(col + 1);
167 e_term_putc('H');
170 void e_term_eeol(void)
172 e_term_putc(E_TR_ESC);
173 e_term_putc('[');
174 e_term_putc('K');
177 void e_term_eeop(E_Term *tr)
179 e_term_fgcol(tr, tr->fgcolor);
180 e_term_bgcol(tr, tr->bgcolor);
181 e_term_putc(E_TR_ESC);
182 e_term_putc('[');
183 e_term_putc('J');
186 void e_term_rev(E_Term *tr, int state)
188 int ftmp, btmp;
190 e_term_putc(E_TR_ESC);
191 e_term_putc('[');
192 e_term_putc(state ? '7' : '0');
193 e_term_putc('m');
195 if (!state) {
196 ftmp = tr->fgcolor;
197 btmp = tr->bgcolor;
198 tr->fgcolor = -1;
199 tr->bgcolor = -1;
200 e_term_fgcol(tr, ftmp);
201 e_term_bgcol(tr, btmp);
205 void e_term_beep ( void )
207 e_term_putc(E_TR_BEL);
208 e_term_flush();