term: do not terminate VT100 attributes with a semicolon
[neatvi.git] / term.c
blob879eb39d2f88f1e936153aa73f930bad2911326f
1 #include <poll.h>
2 #include <signal.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/ioctl.h>
7 #include <termios.h>
8 #include <unistd.h>
9 #include "vi.h"
11 static struct sbuf *term_sbuf;
12 static int rows, cols;
13 static struct termios termios;
15 void term_init(void)
17 struct winsize win;
18 struct termios newtermios;
19 tcgetattr(0, &termios);
20 newtermios = termios;
21 newtermios.c_lflag &= ~(ICANON | ISIG);
22 newtermios.c_lflag &= ~ECHO;
23 tcsetattr(0, TCSAFLUSH, &newtermios);
24 if (getenv("LINES"))
25 rows = atoi(getenv("LINES"));
26 if (getenv("COLUMNS"))
27 cols = atoi(getenv("COLUMNS"));
28 if (!ioctl(0, TIOCGWINSZ, &win)) {
29 cols = cols ? cols : win.ws_col;
30 rows = rows ? rows : win.ws_row;
32 cols = cols ? cols : 80;
33 rows = rows ? rows : 25;
36 void term_done(void)
38 term_commit();
39 tcsetattr(0, 0, &termios);
42 void term_suspend(void)
44 term_done();
45 kill(getpid(), SIGSTOP);
46 term_init();
49 void term_record(void)
51 if (!term_sbuf)
52 term_sbuf = sbuf_make();
55 void term_commit(void)
57 if (term_sbuf) {
58 write(1, sbuf_buf(term_sbuf), sbuf_len(term_sbuf));
59 sbuf_free(term_sbuf);
60 term_sbuf = NULL;
64 static void term_out(char *s)
66 if (term_sbuf)
67 sbuf_str(term_sbuf, s);
68 else
69 write(1, s, strlen(s));
72 void term_str(char *s)
74 term_out(s);
77 void term_chr(int ch)
79 char s[4] = {ch};
80 term_out(s);
83 void term_kill(void)
85 term_out("\33[K");
88 void term_room(int n)
90 char cmd[16];
91 if (n < 0)
92 sprintf(cmd, "\33[%dM", -n);
93 if (n > 0)
94 sprintf(cmd, "\33[%dL", n);
95 if (n)
96 term_out(cmd);
99 void term_pos(int r, int c)
101 char buf[32] = "\r";
102 if (c < 0)
103 c = 0;
104 if (c >= xcols)
105 c = cols - 1;
106 if (r < 0)
107 sprintf(buf, "\r\33[%d%c", abs(c), c > 0 ? 'C' : 'D');
108 else
109 sprintf(buf, "\33[%d;%dH", r + 1, c + 1);
110 term_out(buf);
113 int term_rows(void)
115 return rows;
118 int term_cols(void)
120 return cols;
123 static char ibuf[4096]; /* input character buffer */
124 static char icmd[4096]; /* read after the last term_cmd() */
125 static int ibuf_pos, ibuf_cnt; /* ibuf[] position and length */
126 static int icmd_pos; /* icmd[] position */
128 /* read s before reading from the terminal */
129 void term_push(char *s, int n)
131 n = MIN(n, sizeof(ibuf) - ibuf_cnt);
132 memcpy(ibuf + ibuf_cnt, s, n);
133 ibuf_cnt += n;
136 /* return a static buffer containing inputs read since the last term_cmd() */
137 char *term_cmd(int *n)
139 *n = icmd_pos;
140 icmd_pos = 0;
141 return icmd;
144 int term_read(int ms)
146 struct pollfd ufds[1];
147 char n, c;
148 if (ibuf_pos >= ibuf_cnt) {
149 ufds[0].fd = 0;
150 ufds[0].events = POLLIN;
151 if (poll(ufds, 1, ms * 1000) <= 0)
152 return -1;
153 /* read a single input character */
154 if ((n = read(0, ibuf, 1)) <= 0)
155 return -1;
156 ibuf_cnt = n;
157 ibuf_pos = 0;
159 c = ibuf_pos < ibuf_cnt ? (unsigned char) ibuf[ibuf_pos++] : -1;
160 if (icmd_pos < sizeof(icmd))
161 icmd[icmd_pos++] = c;
162 return c;
165 /* return a static string that changes text attributes from old to att */
166 char *term_att(int att, int old)
168 static char buf[128];
169 char *s = buf;
170 int fg = SYN_FG(att);
171 int bg = SYN_BG(att);
172 if (att == old)
173 return "";
174 s += sprintf(s, "\33[");
175 if (fg & SYN_BD)
176 s += sprintf(s, ";1");
177 if (fg & SYN_IT)
178 s += sprintf(s, ";3");
179 else if (fg & SYN_RV)
180 s += sprintf(s, ";7");
181 if ((fg & 0xff) < 8)
182 s += sprintf(s, ";%d", 30 + (fg & 0xff));
183 else
184 s += sprintf(s, ";38;5;%d", (fg & 0xff));
185 if (bg) {
186 if ((bg & 0xff) < 8)
187 s += sprintf(s, ";%d", 40 + (bg & 0xff));
188 else
189 s += sprintf(s, ";48;5;%d", (bg & 0xff));
191 s += sprintf(s, "m");
192 return buf;