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
)) {
32 cols
= cols
? cols
: 80;
33 rows
= rows
? rows
: 25;
40 tcsetattr(0, 0, &termios
);
43 void term_suspend(void)
46 kill(getpid(), SIGSTOP
);
50 void term_record(void)
53 term_sbuf
= sbuf_make();
56 void term_commit(void)
59 write(1, sbuf_buf(term_sbuf
), sbuf_len(term_sbuf
));
65 static void term_out(char *s
)
68 sbuf_str(term_sbuf
, s
);
70 write(1, s
, strlen(s
));
73 void term_str(char *s
)
93 sprintf(cmd
, "\33[%dM", -n
);
95 sprintf(cmd
, "\33[%dL", n
);
100 void term_pos(int r
, int c
)
108 sprintf(buf
, "\r\33[%d%c", abs(c
), c
> 0 ? 'C' : 'D');
110 sprintf(buf
, "\33[%d;%dH", r
+ 1, c
+ 1);
124 static char ibuf
[4096]; /* input character buffer */
125 static char icmd
[4096]; /* read after the last term_cmd() */
126 static int ibuf_pos
, ibuf_cnt
; /* ibuf[] position and length */
127 static int icmd_pos
; /* icmd[] position */
129 /* read s before reading from the terminal */
130 void term_push(char *s
, int n
)
132 n
= MIN(n
, sizeof(ibuf
) - ibuf_cnt
);
133 memcpy(ibuf
+ ibuf_cnt
, s
, n
);
137 /* return a static buffer containing inputs read since the last term_cmd() */
138 char *term_cmd(int *n
)
147 struct pollfd ufds
[1];
149 if (ibuf_pos
>= ibuf_cnt
) {
151 ufds
[0].events
= POLLIN
;
152 if (poll(ufds
, 1, -1) <= 0)
154 /* read a single input character */
155 if ((n
= read(0, ibuf
, 1)) <= 0)
160 c
= ibuf_pos
< ibuf_cnt
? (unsigned char) ibuf
[ibuf_pos
++] : -1;
161 if (icmd_pos
< sizeof(icmd
))
162 icmd
[icmd_pos
++] = c
;
166 /* return a static string that changes text attributes from old to att */
167 char *term_att(int att
, int old
)
169 static char buf
[128];
171 int fg
= SYN_FG(att
);
172 int bg
= SYN_BG(att
);
175 s
+= sprintf(s
, "\33[");
177 s
+= sprintf(s
, ";1");
179 s
+= sprintf(s
, ";3");
180 else if (att
& SYN_RV
)
181 s
+= sprintf(s
, ";7");
182 if (SYN_FGSET(att
)) {
184 s
+= sprintf(s
, ";%d", 30 + (fg
& 0xff));
186 s
+= sprintf(s
, ";38;5;%d", (fg
& 0xff));
188 if (SYN_BGSET(att
)) {
190 s
+= sprintf(s
, ";%d", 40 + (bg
& 0xff));
192 s
+= sprintf(s
, ";48;5;%d", (bg
& 0xff));
194 s
+= sprintf(s
, "m");