11 static struct sbuf
*term_sbuf
;
12 static int rows
, cols
;
13 static struct termios termios
;
18 struct termios newtermios
;
19 tcgetattr(0, &termios
);
21 newtermios
.c_lflag
&= ~(ICANON
| ISIG
);
22 newtermios
.c_lflag
&= ~ECHO
;
23 tcsetattr(0, TCSAFLUSH
, &newtermios
);
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;
39 tcsetattr(0, 0, &termios
);
42 void term_suspend(void)
45 kill(getpid(), SIGSTOP
);
49 void term_record(void)
52 term_sbuf
= sbuf_make();
55 void term_commit(void)
58 write(1, sbuf_buf(term_sbuf
), sbuf_len(term_sbuf
));
64 static void term_out(char *s
)
67 sbuf_str(term_sbuf
, s
);
69 write(1, s
, strlen(s
));
72 void term_str(char *s
)
88 void term_pos(int r
, int c
)
96 sprintf(buf
, "\r\33[%d%c", abs(c
), c
> 0 ? 'C' : 'D');
98 sprintf(buf
, "\33[%d;%dH", r
+ 1, c
+ 1);
112 static char ibuf
[4096]; /* input character buffer */
113 static char icmd
[4096]; /* read after the last term_cmd() */
114 static int ibuf_pos
, ibuf_cnt
; /* ibuf[] position and length */
115 static int icmd_pos
; /* icmd[] position */
117 /* read s before reading from the terminal */
118 void term_push(char *s
, int n
)
120 n
= MIN(n
, sizeof(ibuf
) - ibuf_cnt
);
121 memcpy(ibuf
+ ibuf_cnt
, s
, n
);
125 /* return a static buffer containing inputs read since the last term_cmd() */
126 char *term_cmd(int *n
)
133 int term_read(int ms
)
135 struct pollfd ufds
[1];
137 if (ibuf_pos
>= ibuf_cnt
) {
139 ufds
[0].events
= POLLIN
;
140 if (poll(ufds
, 1, ms
* 1000) <= 0)
142 /* read a single input character */
143 if ((n
= read(0, ibuf
, 1)) <= 0)
148 c
= ibuf_pos
< ibuf_cnt
? (unsigned char) ibuf
[ibuf_pos
++] : -1;
149 if (icmd_pos
< sizeof(icmd
))
150 icmd
[icmd_pos
++] = c
;
154 /* return a static string that changes text attributes from old to att */
155 char *term_att(int att
, int old
)
157 static char buf
[128];
159 int fg
= SYN_FG(att
);
160 int bg
= SYN_BG(att
);
163 s
+= sprintf(s
, "\33[m\33[");
165 s
+= sprintf(s
, "1;");
167 s
+= sprintf(s
, "3;");
168 else if (fg
& SYN_RV
)
169 s
+= sprintf(s
, "7;");
171 s
+= sprintf(s
, "%d;", 30 + (fg
& 0xff));
173 s
+= sprintf(s
, "38;5;%d;", (fg
& 0xff));
176 s
+= sprintf(s
, "%d;", 40 + (bg
& 0xff));
178 s
+= sprintf(s
, "48;5;%d;", (bg
& 0xff));
180 s
+= sprintf(s
, "m");