19 static void setsize(void)
22 size
.ws_col
= pad_cols();
23 size
.ws_row
= pad_rows();
26 ioctl(fd
, TIOCSWINSZ
, &size
);
29 static int readpty(void)
32 if (read(fd
, &b
, 1) > 0)
37 static void move_cursor(int r
, int c
)
39 pad_show(row
, col
, 0);
40 row
= MIN(r
, pad_rows() - 1);
41 col
= MIN(c
, pad_cols() - 1);
42 pad_show(row
, col
, 1);
45 static void scroll_screen(int sr
, int nr
, int n
)
47 pad_show(row
, col
, 0);
48 pad_scroll(sr
, nr
, n
);
49 pad_show(row
, col
, 1);
52 static void advance(int ch
)
78 if (c
>= pad_cols()) {
82 if (r
>= pad_rows()) {
83 int n
= pad_rows() - r
- 1;
86 scroll_screen(-n
, nr
, n
);
93 unsigned char b
= (unsigned char) c
;
98 static void writepty(int c
)
100 pad_put(c
, row
, col
);
104 static void setmode(int m
)
110 if (m
>= 30 && m
<= 37)
112 if (m
>= 40 && m
<= 47)
116 static void kill_line(void)
119 for (i
= col
; i
< pad_cols(); i
++)
120 pad_put('\0', row
, i
);
121 move_cursor(row
, col
);
124 static void delete_lines(int n
)
127 int nr
= pad_rows() - row
- n
;
128 scroll_screen(sr
, nr
, -n
);
131 static void insert_lines(int n
)
133 int nr
= pad_rows() - row
- n
;
134 scroll_screen(row
, nr
, n
);
137 static void escape_bracket(void)
139 int args
[MAXESCARGS
] = {0};
143 for (i
= 0; i
< ARRAY_SIZE(args
) && !isalpha(c
); i
++) {
145 while (isdigit((c
= readpty())))
146 arg
= arg
* 10 + (c
- '0');
152 move_cursor(MAX(0, args
[0] - 1), MAX(0, args
[1] - 1));
159 move_cursor(row
- MAX(1, args
[0]), col
);
162 move_cursor(row
+ MAX(1, args
[0]), col
);
165 move_cursor(row
, col
+ MAX(1, args
[0]));
168 move_cursor(row
, col
- MAX(1, args
[0]));
174 insert_lines(MAX(1, args
[0]));
177 delete_lines(MAX(1, args
[0]));
186 for (i
= 0; i
< n
; i
++)
194 printf("unknown escape bracket char <%c>\n", c
);
198 static void reverse_index()
200 scroll_screen(0, pad_rows() - 1, 1);
203 static int escape_alone(int c
)
210 printf("unknown escape char <%c>\n", c
);
215 static void escape(void)
220 } else if (escape_alone(c
)) {
236 void term_exec(char *cmd
)
238 if ((pid
= forkpty(&fd
, NULL
, NULL
, NULL
)) == -1)
239 xerror("failed to create a pty");
241 setenv("TERM", "linux", 1);
242 execl(cmd
, cmd
, NULL
);
250 void term_save(struct term_state
*state
)
256 pad_save(&state
->pad
);
259 void term_load(struct term_state
*state
)
265 pad_load(&state
->pad
);
266 move_cursor(row
, col
);