2012-06-17 09:28:02 -07:00
|
|
|
#include "private.h"
|
2012-06-12 03:10:01 -07:00
|
|
|
#include <Elementary.h>
|
|
|
|
#include "termpty.h"
|
2012-07-13 01:46:33 -07:00
|
|
|
#include "termptyesc.h"
|
|
|
|
#include "termptyops.h"
|
2013-05-03 19:44:20 -07:00
|
|
|
#include "termptysave.h"
|
2013-09-22 08:10:39 -07:00
|
|
|
#include "termio.h"
|
2012-06-12 03:10:01 -07:00
|
|
|
#include <sys/types.h>
|
2012-08-05 18:13:20 -07:00
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/wait.h>
|
2012-06-12 03:10:01 -07:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
2012-06-19 12:40:40 -07:00
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2013-09-22 08:10:39 -07:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <termios.h>
|
2012-06-12 03:10:01 -07:00
|
|
|
|
2012-06-19 12:53:02 -07:00
|
|
|
/* specific log domain to help debug only terminal code parser */
|
2012-07-13 01:46:33 -07:00
|
|
|
int _termpty_log_dom = -1;
|
2012-06-19 12:53:02 -07:00
|
|
|
|
|
|
|
#undef CRITICAL
|
|
|
|
#undef ERR
|
|
|
|
#undef WRN
|
|
|
|
#undef INF
|
|
|
|
#undef DBG
|
|
|
|
|
|
|
|
#define CRITICAL(...) EINA_LOG_DOM_CRIT(_termpty_log_dom, __VA_ARGS__)
|
|
|
|
#define ERR(...) EINA_LOG_DOM_ERR(_termpty_log_dom, __VA_ARGS__)
|
|
|
|
#define WRN(...) EINA_LOG_DOM_WARN(_termpty_log_dom, __VA_ARGS__)
|
|
|
|
#define INF(...) EINA_LOG_DOM_INFO(_termpty_log_dom, __VA_ARGS__)
|
|
|
|
#define DBG(...) EINA_LOG_DOM_DBG(_termpty_log_dom, __VA_ARGS__)
|
|
|
|
|
|
|
|
void
|
|
|
|
termpty_init(void)
|
|
|
|
{
|
|
|
|
if (_termpty_log_dom >= 0) return;
|
|
|
|
|
|
|
|
_termpty_log_dom = eina_log_domain_register("termpty", NULL);
|
|
|
|
if (_termpty_log_dom < 0)
|
|
|
|
EINA_LOG_CRIT("could not create log domain 'termpty'.");
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
termpty_shutdown(void)
|
|
|
|
{
|
|
|
|
if (_termpty_log_dom < 0) return;
|
|
|
|
eina_log_domain_unregister(_termpty_log_dom);
|
|
|
|
_termpty_log_dom = -1;
|
|
|
|
}
|
|
|
|
|
2012-06-12 03:10:01 -07:00
|
|
|
static void
|
2012-06-30 17:24:30 -07:00
|
|
|
_handle_buf(Termpty *ty, const Eina_Unicode *codepoints, int len)
|
2012-06-12 03:10:01 -07:00
|
|
|
{
|
2012-06-30 17:24:30 -07:00
|
|
|
Eina_Unicode *c, *ce, *b;
|
|
|
|
int n, bytes;
|
2012-06-12 17:26:00 -07:00
|
|
|
|
2014-01-22 14:22:42 -08:00
|
|
|
c = (Eina_Unicode *)codepoints;
|
2012-06-12 03:10:01 -07:00
|
|
|
ce = &(c[len]);
|
|
|
|
|
|
|
|
if (ty->buf)
|
|
|
|
{
|
|
|
|
bytes = (ty->buflen + len + 1) * sizeof(int);
|
|
|
|
b = realloc(ty->buf, bytes);
|
|
|
|
if (!b)
|
|
|
|
{
|
2012-06-19 12:40:40 -07:00
|
|
|
ERR("memerr");
|
2014-03-24 05:38:42 -07:00
|
|
|
return;
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
2012-06-19 12:40:40 -07:00
|
|
|
INF("realloc add %i + %i", (int)(ty->buflen * sizeof(int)), (int)(len * sizeof(int)));
|
2012-06-30 17:24:30 -07:00
|
|
|
bytes = len * sizeof(Eina_Unicode);
|
2012-06-25 18:38:13 -07:00
|
|
|
memcpy(&(b[ty->buflen]), codepoints, bytes);
|
2012-06-12 03:10:01 -07:00
|
|
|
ty->buf = b;
|
|
|
|
ty->buflen += len;
|
|
|
|
ty->buf[ty->buflen] = 0;
|
|
|
|
c = ty->buf;
|
|
|
|
ce = c + ty->buflen;
|
|
|
|
while (c < ce)
|
|
|
|
{
|
2012-07-13 01:46:33 -07:00
|
|
|
n = _termpty_handle_seq(ty, c, ce);
|
2012-06-12 03:10:01 -07:00
|
|
|
if (n == 0)
|
|
|
|
{
|
2012-06-30 17:24:30 -07:00
|
|
|
Eina_Unicode *tmp = ty->buf;
|
2012-06-12 03:10:01 -07:00
|
|
|
ty->buf = NULL;
|
|
|
|
ty->buflen = 0;
|
2012-06-30 17:24:30 -07:00
|
|
|
bytes = ((char *)ce - (char *)c) + sizeof(Eina_Unicode);
|
|
|
|
INF("malloc til %i", (int)(bytes - sizeof(Eina_Unicode)));
|
2012-06-12 03:10:01 -07:00
|
|
|
ty->buf = malloc(bytes);
|
|
|
|
if (!ty->buf)
|
|
|
|
{
|
2012-06-19 12:40:40 -07:00
|
|
|
ERR("memerr");
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
|
|
|
bytes = (char *)ce - (char *)c;
|
|
|
|
memcpy(ty->buf, c, bytes);
|
2012-06-30 17:24:30 -07:00
|
|
|
ty->buflen = bytes / sizeof(Eina_Unicode);
|
2012-06-12 03:10:01 -07:00
|
|
|
ty->buf[ty->buflen] = 0;
|
|
|
|
free(tmp);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
c += n;
|
|
|
|
}
|
|
|
|
if (c == ce)
|
|
|
|
{
|
|
|
|
if (ty->buf)
|
|
|
|
{
|
|
|
|
free(ty->buf);
|
|
|
|
ty->buf = NULL;
|
|
|
|
}
|
|
|
|
ty->buflen = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (c < ce)
|
|
|
|
{
|
2012-07-13 01:46:33 -07:00
|
|
|
n = _termpty_handle_seq(ty, c, ce);
|
2012-06-12 03:10:01 -07:00
|
|
|
if (n == 0)
|
|
|
|
{
|
2012-06-30 17:24:30 -07:00
|
|
|
bytes = ((char *)ce - (char *)c) + sizeof(Eina_Unicode);
|
2012-06-12 03:10:01 -07:00
|
|
|
ty->buf = malloc(bytes);
|
2012-06-30 17:24:30 -07:00
|
|
|
INF("malloc %i", (int)(bytes - sizeof(Eina_Unicode)));
|
2012-06-12 03:10:01 -07:00
|
|
|
if (!ty->buf)
|
|
|
|
{
|
2012-06-19 12:40:40 -07:00
|
|
|
ERR("memerr");
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
2014-03-24 05:37:07 -07:00
|
|
|
else
|
|
|
|
{
|
|
|
|
bytes = (char *)ce - (char *)c;
|
|
|
|
memcpy(ty->buf, c, bytes);
|
|
|
|
ty->buflen = bytes / sizeof(Eina_Unicode);
|
|
|
|
ty->buf[ty->buflen] = 0;
|
|
|
|
}
|
2012-06-12 03:10:01 -07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
c += n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_pty_size(Termpty *ty)
|
|
|
|
{
|
|
|
|
struct winsize sz;
|
2012-06-12 17:26:00 -07:00
|
|
|
|
2012-06-12 03:10:01 -07:00
|
|
|
sz.ws_col = ty->w;
|
|
|
|
sz.ws_row = ty->h;
|
|
|
|
sz.ws_xpixel = 0;
|
|
|
|
sz.ws_ypixel = 0;
|
2012-06-19 12:40:40 -07:00
|
|
|
if (ioctl(ty->fd, TIOCSWINSZ, &sz) < 0)
|
|
|
|
ERR("Size set ioctl failed: %s", strerror(errno));
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Bool
|
2013-08-27 06:36:50 -07:00
|
|
|
_cb_exe_exit(void *data, int type EINA_UNUSED, void *event)
|
2012-06-12 03:10:01 -07:00
|
|
|
{
|
|
|
|
Ecore_Exe_Event_Del *ev = event;
|
|
|
|
Termpty *ty = data;
|
2012-06-12 17:26:00 -07:00
|
|
|
|
2012-06-12 03:10:01 -07:00
|
|
|
if (ev->pid != ty->pid) return ECORE_CALLBACK_PASS_ON;
|
2012-06-20 07:12:26 -07:00
|
|
|
ty->exit_code = ev->exit_code;
|
2012-08-05 02:38:58 -07:00
|
|
|
|
2012-08-05 18:13:20 -07:00
|
|
|
ty->pid = -1;
|
|
|
|
|
2012-08-05 02:38:58 -07:00
|
|
|
if (ty->hand_exe_exit) ecore_event_handler_del(ty->hand_exe_exit);
|
|
|
|
ty->hand_exe_exit = NULL;
|
|
|
|
if (ty->hand_fd) ecore_main_fd_handler_del(ty->hand_fd);
|
|
|
|
ty->hand_fd = NULL;
|
|
|
|
if (ty->fd >= 0) close(ty->fd);
|
|
|
|
ty->fd = -1;
|
|
|
|
if (ty->slavefd >= 0) close(ty->slavefd);
|
|
|
|
ty->slavefd = -1;
|
|
|
|
|
2012-12-27 03:20:32 -08:00
|
|
|
if (ty->cb.exited.func) ty->cb.exited.func(ty->cb.exited.data);
|
|
|
|
|
2012-06-12 03:10:01 -07:00
|
|
|
return ECORE_CALLBACK_PASS_ON;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Eina_Bool
|
2013-08-27 06:36:50 -07:00
|
|
|
_cb_fd_read(void *data, Ecore_Fd_Handler *fd_handler EINA_UNUSED)
|
2012-06-12 03:10:01 -07:00
|
|
|
{
|
|
|
|
Termpty *ty = data;
|
|
|
|
char buf[4097];
|
2012-06-30 17:24:30 -07:00
|
|
|
Eina_Unicode codepoint[4097];
|
2013-05-03 03:53:41 -07:00
|
|
|
int len, i, j, k, reads;
|
2012-06-12 03:10:01 -07:00
|
|
|
|
2012-06-22 23:43:02 -07:00
|
|
|
// read up to 64 * 4096 bytes
|
2012-06-12 03:10:01 -07:00
|
|
|
for (reads = 0; reads < 64; reads++)
|
|
|
|
{
|
2013-04-28 13:47:14 -07:00
|
|
|
char *rbuf = buf;
|
|
|
|
len = sizeof(buf) - 1;
|
|
|
|
|
|
|
|
for (i = 0; i < (int)sizeof(ty->oldbuf) && ty->oldbuf[i] & 0x80; i++)
|
|
|
|
{
|
|
|
|
*rbuf = ty->oldbuf[i];
|
|
|
|
rbuf++;
|
|
|
|
len--;
|
|
|
|
}
|
|
|
|
len = read(ty->fd, rbuf, len);
|
2012-06-12 03:10:01 -07:00
|
|
|
if (len <= 0) break;
|
2013-04-28 13:47:14 -07:00
|
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < (int)sizeof(ty->oldbuf); i++)
|
|
|
|
ty->oldbuf[i] = 0;
|
|
|
|
|
|
|
|
len += rbuf - buf;
|
|
|
|
|
|
|
|
/*
|
2012-06-22 23:43:02 -07:00
|
|
|
printf(" I: ");
|
|
|
|
int jj;
|
2013-04-28 13:47:14 -07:00
|
|
|
for (jj = 0; jj < len; jj++)
|
2012-06-22 23:43:02 -07:00
|
|
|
{
|
|
|
|
if ((buf[jj] < ' ') || (buf[jj] >= 0x7f))
|
2012-09-28 04:10:58 -07:00
|
|
|
printf("\033[33m%02x\033[0m", (unsigned char)buf[jj]);
|
2012-06-22 23:43:02 -07:00
|
|
|
else
|
|
|
|
printf("%c", buf[jj]);
|
|
|
|
}
|
|
|
|
printf("\n");
|
2013-04-28 13:47:14 -07:00
|
|
|
*/
|
2012-06-12 03:10:01 -07:00
|
|
|
buf[len] = 0;
|
2012-06-25 18:38:13 -07:00
|
|
|
// convert UTF8 to codepoint integers
|
2012-06-12 03:10:01 -07:00
|
|
|
j = 0;
|
|
|
|
for (i = 0; i < len;)
|
|
|
|
{
|
2013-04-28 13:47:14 -07:00
|
|
|
int g = 0, prev_i = i;
|
2012-06-12 17:26:00 -07:00
|
|
|
|
2012-06-12 03:10:01 -07:00
|
|
|
if (buf[i])
|
|
|
|
{
|
2013-01-28 19:52:57 -08:00
|
|
|
#if (EINA_VERSION_MAJOR > 1) || (EINA_VERSION_MINOR >= 8)
|
2013-01-21 18:00:55 -08:00
|
|
|
g = eina_unicode_utf8_next_get(buf, &i);
|
2013-05-03 03:53:41 -07:00
|
|
|
if ((0xdc80 <= g) && (g <= 0xdcff) &&
|
|
|
|
(len - prev_i) <= (int)sizeof(ty->oldbuf))
|
2013-04-28 13:47:14 -07:00
|
|
|
#else
|
2013-01-28 19:52:57 -08:00
|
|
|
i = evas_string_char_next_get(buf, i, &g);
|
2013-04-28 13:47:14 -07:00
|
|
|
if (i < 0 &&
|
2013-05-03 03:53:41 -07:00
|
|
|
(len - prev_i) <= (int)sizeof(ty->oldbuf))
|
2013-04-28 13:47:14 -07:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
for (k = 0;
|
2013-05-03 03:53:41 -07:00
|
|
|
(k < (int)sizeof(ty->oldbuf)) &&
|
|
|
|
(k < (len - prev_i));
|
2013-04-28 13:47:14 -07:00
|
|
|
k++)
|
|
|
|
{
|
|
|
|
ty->oldbuf[k] = buf[prev_i+k];
|
|
|
|
}
|
|
|
|
DBG("failure at %d/%d/%d", prev_i, i, len);
|
|
|
|
break;
|
|
|
|
}
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
g = 0;
|
|
|
|
i++;
|
|
|
|
}
|
2012-06-25 18:38:13 -07:00
|
|
|
codepoint[j] = g;
|
2012-06-12 03:10:01 -07:00
|
|
|
j++;
|
|
|
|
}
|
2012-06-25 18:38:13 -07:00
|
|
|
codepoint[j] = 0;
|
2012-06-19 12:40:40 -07:00
|
|
|
// DBG("---------------- handle buf %i", j);
|
2012-06-25 18:38:13 -07:00
|
|
|
_handle_buf(ty, codepoint, j);
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
|
|
|
if (ty->cb.change.func) ty->cb.change.func(ty->cb.change.data);
|
|
|
|
return EINA_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_limit_coord(Termpty *ty, Termstate *state)
|
|
|
|
{
|
|
|
|
state->wrapnext = 0;
|
|
|
|
if (state->cx >= ty->w) state->cx = ty->w - 1;
|
|
|
|
if (state->cy >= ty->h) state->cy = ty->h - 1;
|
|
|
|
if (state->had_cr_x >= ty->w) state->had_cr_x = ty->w - 1;
|
|
|
|
if (state->had_cr_y >= ty->h) state->had_cr_y = ty->h - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Termpty *
|
2013-06-16 13:32:50 -07:00
|
|
|
termpty_new(const char *cmd, Eina_Bool login_shell, const char *cd,
|
2013-09-22 08:10:39 -07:00
|
|
|
int w, int h, int backscroll, Eina_Bool xterm_256color,
|
2014-01-22 03:31:37 -08:00
|
|
|
Eina_Bool erase_is_del, const char *emotion_mod)
|
2012-06-12 03:10:01 -07:00
|
|
|
{
|
|
|
|
Termpty *ty;
|
|
|
|
const char *pty;
|
2013-09-21 15:15:56 -07:00
|
|
|
int mode;
|
2014-01-29 13:57:49 -08:00
|
|
|
struct termios t;
|
2012-06-12 17:26:00 -07:00
|
|
|
|
2012-06-12 03:10:01 -07:00
|
|
|
ty = calloc(1, sizeof(Termpty));
|
|
|
|
if (!ty) return NULL;
|
|
|
|
ty->w = w;
|
|
|
|
ty->h = h;
|
|
|
|
ty->backmax = backscroll;
|
|
|
|
|
2012-07-13 01:46:33 -07:00
|
|
|
_termpty_reset_state(ty);
|
2012-06-12 03:10:01 -07:00
|
|
|
ty->save = ty->state;
|
|
|
|
ty->swap = ty->state;
|
2012-06-12 17:26:00 -07:00
|
|
|
|
2012-06-12 03:10:01 -07:00
|
|
|
ty->screen = calloc(1, sizeof(Termcell) * ty->w * ty->h);
|
2013-08-29 20:14:18 -07:00
|
|
|
if (!ty->screen)
|
|
|
|
{
|
2013-08-29 20:15:07 -07:00
|
|
|
ERR("Allocation of term screen %ix%i", ty->w, ty->h);
|
2013-08-29 20:14:18 -07:00
|
|
|
goto err;
|
|
|
|
}
|
2012-06-12 03:10:01 -07:00
|
|
|
ty->screen2 = calloc(1, sizeof(Termcell) * ty->w * ty->h);
|
2013-08-29 20:14:18 -07:00
|
|
|
if (!ty->screen2)
|
|
|
|
{
|
2013-08-29 20:15:07 -07:00
|
|
|
ERR("Allocation of term screen2 %ix%i", ty->w, ty->h);
|
2013-08-29 20:14:18 -07:00
|
|
|
goto err;
|
|
|
|
}
|
2012-06-12 17:26:00 -07:00
|
|
|
|
2013-01-21 06:02:32 -08:00
|
|
|
ty->circular_offset = 0;
|
|
|
|
|
2012-06-12 03:10:01 -07:00
|
|
|
ty->fd = posix_openpt(O_RDWR | O_NOCTTY);
|
2013-08-29 20:14:18 -07:00
|
|
|
if (ty->fd < 0)
|
|
|
|
{
|
|
|
|
ERR("posix_openpt failed: %s", strerror(errno));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (grantpt(ty->fd) != 0)
|
|
|
|
{
|
2013-08-29 22:17:42 -07:00
|
|
|
WRN("grantpt failed: %s", strerror(errno));
|
2013-08-29 20:14:18 -07:00
|
|
|
}
|
|
|
|
if (unlockpt(ty->fd) != 0)
|
|
|
|
{
|
|
|
|
ERR("unlockpt failed: %s", strerror(errno));
|
|
|
|
goto err;
|
|
|
|
}
|
2012-06-12 03:10:01 -07:00
|
|
|
pty = ptsname(ty->fd);
|
|
|
|
ty->slavefd = open(pty, O_RDWR | O_NOCTTY);
|
2013-08-29 20:14:18 -07:00
|
|
|
if (ty->slavefd < 0)
|
|
|
|
{
|
|
|
|
ERR("open of pty '%s' failed: %s", pty, strerror(errno));
|
|
|
|
goto err;
|
|
|
|
}
|
2013-09-21 15:15:56 -07:00
|
|
|
mode = fcntl(ty->fd, F_GETFL, 0);
|
|
|
|
if (mode < 0)
|
|
|
|
{
|
|
|
|
ERR("fcntl on pty '%s' failed: %s", pty, strerror(errno));
|
|
|
|
goto err;
|
|
|
|
}
|
|
|
|
if (!(mode & O_NDELAY))
|
|
|
|
if (fcntl(ty->fd, F_SETFL, mode | O_NDELAY))
|
|
|
|
{
|
|
|
|
ERR("fcntl on pty '%s' failed: %s", pty, strerror(errno));
|
|
|
|
goto err;
|
|
|
|
}
|
2012-06-12 03:10:01 -07:00
|
|
|
|
2013-09-22 08:10:39 -07:00
|
|
|
|
2014-01-29 13:57:49 -08:00
|
|
|
tcgetattr(ty->fd, &t);
|
|
|
|
t.c_cc[VERASE] = (erase_is_del) ? 0x7f : 0x8;
|
|
|
|
#ifdef IUTF8
|
|
|
|
t.c_iflag |= IUTF8;
|
|
|
|
#endif
|
|
|
|
tcsetattr(ty->fd, TCSANOW, &t);
|
2013-09-22 08:10:39 -07:00
|
|
|
|
2012-06-12 03:10:01 -07:00
|
|
|
ty->hand_exe_exit = ecore_event_handler_add(ECORE_EXE_EVENT_DEL,
|
|
|
|
_cb_exe_exit, ty);
|
2013-08-29 20:14:18 -07:00
|
|
|
if (!ty->hand_exe_exit)
|
|
|
|
{
|
|
|
|
ERR("event handler add failed");
|
|
|
|
goto err;
|
|
|
|
}
|
2012-06-12 03:10:01 -07:00
|
|
|
ty->pid = fork();
|
|
|
|
if (!ty->pid)
|
|
|
|
{
|
2012-06-17 12:13:03 -07:00
|
|
|
const char *shell = NULL;
|
|
|
|
const char *args[4] = {NULL, NULL, NULL, NULL};
|
|
|
|
Eina_Bool needs_shell;
|
2012-06-12 03:10:01 -07:00
|
|
|
int i;
|
2014-01-22 03:31:37 -08:00
|
|
|
char buf[256];
|
2012-06-12 03:10:01 -07:00
|
|
|
|
2012-08-05 02:38:58 -07:00
|
|
|
if (cd)
|
|
|
|
{
|
|
|
|
if (chdir(cd) != 0)
|
|
|
|
{
|
|
|
|
perror("chdir");
|
|
|
|
ERR("Cannot change to directory '%s'", cd);
|
|
|
|
exit(127);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-17 12:13:03 -07:00
|
|
|
needs_shell = ((!cmd) ||
|
|
|
|
(strpbrk(cmd, " |&;<>()$`\\\"'*?#") != NULL));
|
|
|
|
DBG("cmd='%s' needs_shell=%u", cmd ? cmd : "", needs_shell);
|
|
|
|
|
|
|
|
if (needs_shell)
|
|
|
|
{
|
|
|
|
shell = getenv("SHELL");
|
|
|
|
if (!shell)
|
|
|
|
{
|
|
|
|
uid_t uid = getuid();
|
|
|
|
struct passwd *pw = getpwuid(uid);
|
|
|
|
if (pw) shell = pw->pw_shell;
|
|
|
|
}
|
|
|
|
if (!shell)
|
|
|
|
{
|
|
|
|
WRN("Could not find shell, fallback to /bin/sh");
|
|
|
|
shell = "/bin/sh";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!needs_shell)
|
|
|
|
args[0] = cmd;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
args[0] = shell;
|
|
|
|
if (cmd)
|
|
|
|
{
|
|
|
|
args[1] = "-c";
|
|
|
|
args[2] = cmd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define NC(x) (args[x] != NULL ? args[x] : "")
|
|
|
|
DBG("exec %s %s %s %s", NC(0), NC(1), NC(2), NC(3));
|
|
|
|
#undef NC
|
|
|
|
|
2012-06-12 03:10:01 -07:00
|
|
|
for (i = 0; i < 100; i++)
|
|
|
|
{
|
|
|
|
if (i != ty->slavefd) close(i);
|
|
|
|
}
|
|
|
|
setsid();
|
2012-06-12 17:26:00 -07:00
|
|
|
|
2012-12-27 03:20:32 -08:00
|
|
|
dup2(ty->slavefd, 0);
|
|
|
|
dup2(ty->slavefd, 1);
|
|
|
|
dup2(ty->slavefd, 2);
|
2012-06-12 17:26:00 -07:00
|
|
|
|
2012-12-27 03:20:32 -08:00
|
|
|
if (ioctl(ty->slavefd, TIOCSCTTY, NULL) < 0) exit(1);
|
|
|
|
|
|
|
|
close(ty->fd);
|
|
|
|
close(ty->slavefd);
|
|
|
|
|
2012-06-17 12:13:03 -07:00
|
|
|
/* TODO: should we reset signals here? */
|
2012-06-17 11:28:19 -07:00
|
|
|
|
2013-06-16 13:32:50 -07:00
|
|
|
/* pretend to be xterm */
|
|
|
|
if (xterm_256color)
|
|
|
|
{
|
|
|
|
putenv("TERM=xterm-256color");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
putenv("TERM=xterm");
|
|
|
|
}
|
2012-07-23 19:16:21 -07:00
|
|
|
putenv("XTERM_256_COLORS=1");
|
2014-01-22 03:31:37 -08:00
|
|
|
if (emotion_mod)
|
|
|
|
{
|
|
|
|
snprintf(buf, sizeof(buf), "EMOTION_ENGINE=%s", emotion_mod);
|
|
|
|
putenv(buf);
|
|
|
|
}
|
2012-09-27 02:33:33 -07:00
|
|
|
if (!login_shell)
|
|
|
|
execvp(args[0], (char *const *)args);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char *cmdfile, *cmd0;
|
|
|
|
|
|
|
|
cmdfile = (char *)args[0];
|
|
|
|
cmd0 = alloca(strlen(cmdfile) + 2);
|
|
|
|
cmd0[0] = '-';
|
|
|
|
strcpy(cmd0 + 1, cmdfile);
|
|
|
|
args[0] = cmd0;
|
|
|
|
execvp(cmdfile, (char *const *)args);
|
|
|
|
}
|
2012-06-17 12:13:03 -07:00
|
|
|
exit(127); /* same as system() for failed commands */
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
|
|
|
ty->hand_fd = ecore_main_fd_handler_add(ty->fd, ECORE_FD_READ,
|
|
|
|
_cb_fd_read, ty,
|
|
|
|
NULL, NULL);
|
|
|
|
close(ty->slavefd);
|
2012-12-27 03:20:32 -08:00
|
|
|
ty->slavefd = -1;
|
2012-06-12 03:10:01 -07:00
|
|
|
_pty_size(ty);
|
2013-05-03 19:44:20 -07:00
|
|
|
termpty_save_register(ty);
|
2012-06-12 03:10:01 -07:00
|
|
|
return ty;
|
|
|
|
err:
|
|
|
|
if (ty->screen) free(ty->screen);
|
|
|
|
if (ty->screen2) free(ty->screen2);
|
|
|
|
if (ty->fd >= 0) close(ty->fd);
|
|
|
|
if (ty->slavefd >= 0) close(ty->slavefd);
|
|
|
|
free(ty);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
termpty_free(Termpty *ty)
|
|
|
|
{
|
2013-01-28 16:43:13 -08:00
|
|
|
Termexp *ex;
|
2013-05-03 19:44:20 -07:00
|
|
|
|
|
|
|
termpty_save_unregister(ty);
|
2013-01-28 16:43:13 -08:00
|
|
|
EINA_LIST_FREE(ty->block.expecting, ex) free(ex);
|
2013-01-28 08:06:26 -08:00
|
|
|
if (ty->block.blocks) eina_hash_free(ty->block.blocks);
|
2013-04-20 00:54:11 -07:00
|
|
|
if (ty->block.chid_map) eina_hash_free(ty->block.chid_map);
|
2013-01-28 08:06:26 -08:00
|
|
|
if (ty->block.active) eina_list_free(ty->block.active);
|
2012-08-05 21:11:14 -07:00
|
|
|
if (ty->fd >= 0) close(ty->fd);
|
|
|
|
if (ty->slavefd >= 0) close(ty->slavefd);
|
2012-08-05 18:13:20 -07:00
|
|
|
if (ty->pid >= 0)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
// in case someone stopped the child - cont it
|
|
|
|
kill(ty->pid, SIGCONT);
|
|
|
|
// signpipe for shells
|
|
|
|
kill(ty->pid, SIGPIPE);
|
|
|
|
// try 400 time (sleeping for 1ms) to check for death of child
|
|
|
|
for (i = 0; i < 400; i++)
|
|
|
|
{
|
|
|
|
int status = 0;
|
|
|
|
|
|
|
|
// poll exit of child pid
|
|
|
|
if (waitpid(ty->pid, &status, WNOHANG) == ty->pid)
|
|
|
|
{
|
|
|
|
// if child exited - break loop and mark pid as done
|
|
|
|
ty->pid = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// after 100ms set sigint
|
|
|
|
if (i == 100) kill(ty->pid, SIGINT);
|
|
|
|
// after 200ms send term signal
|
|
|
|
else if (i == 200) kill(ty->pid, SIGTERM);
|
|
|
|
// after 300ms send quit signal
|
|
|
|
else if (i == 300) kill(ty->pid, SIGQUIT);
|
|
|
|
usleep(1000); // sleep 1ms
|
|
|
|
}
|
|
|
|
// so 400ms and child not gone - KILL!
|
|
|
|
if (ty->pid >= 0)
|
|
|
|
{
|
|
|
|
kill(ty->pid, SIGKILL);
|
|
|
|
ty->pid = -1;
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 03:10:01 -07:00
|
|
|
if (ty->hand_exe_exit) ecore_event_handler_del(ty->hand_exe_exit);
|
|
|
|
if (ty->hand_fd) ecore_main_fd_handler_del(ty->hand_fd);
|
|
|
|
if (ty->prop.title) eina_stringshare_del(ty->prop.title);
|
|
|
|
if (ty->prop.icon) eina_stringshare_del(ty->prop.icon);
|
|
|
|
if (ty->back)
|
|
|
|
{
|
|
|
|
int i;
|
2012-06-12 17:26:00 -07:00
|
|
|
|
2012-06-12 03:10:01 -07:00
|
|
|
for (i = 0; i < ty->backmax; i++)
|
|
|
|
{
|
2013-05-05 07:10:44 -07:00
|
|
|
if (ty->back[i])
|
|
|
|
{
|
|
|
|
termpty_save_free(ty->back[i]);
|
|
|
|
ty->back[i] = NULL;
|
|
|
|
}
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
|
|
|
free(ty->back);
|
2013-05-05 07:10:44 -07:00
|
|
|
ty->back = NULL;
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
|
|
|
if (ty->screen) free(ty->screen);
|
|
|
|
if (ty->screen2) free(ty->screen2);
|
|
|
|
if (ty->buf) free(ty->buf);
|
|
|
|
memset(ty, 0, sizeof(Termpty));
|
|
|
|
free(ty);
|
|
|
|
}
|
|
|
|
|
2013-05-03 19:44:20 -07:00
|
|
|
void
|
2013-08-27 06:36:50 -07:00
|
|
|
termpty_cellcomp_freeze(Termpty *ty EINA_UNUSED)
|
2013-05-03 19:44:20 -07:00
|
|
|
{
|
|
|
|
termpty_save_freeze();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-08-27 06:36:50 -07:00
|
|
|
termpty_cellcomp_thaw(Termpty *ty EINA_UNUSED)
|
2013-05-03 19:44:20 -07:00
|
|
|
{
|
|
|
|
termpty_save_thaw();
|
|
|
|
}
|
|
|
|
|
2012-06-12 03:10:01 -07:00
|
|
|
Termcell *
|
|
|
|
termpty_cellrow_get(Termpty *ty, int y, int *wret)
|
|
|
|
{
|
2013-05-03 19:44:20 -07:00
|
|
|
Termsave *ts, **tssrc;
|
2012-06-12 17:26:00 -07:00
|
|
|
|
2012-06-12 03:10:01 -07:00
|
|
|
if (y >= 0)
|
|
|
|
{
|
|
|
|
if (y >= ty->h) return NULL;
|
|
|
|
*wret = ty->w;
|
2013-01-21 06:02:32 -08:00
|
|
|
/* fprintf(stderr, "getting: %i (%i, %i)\n", y, ty->circular_offset, ty->h); */
|
|
|
|
return &(TERMPTY_SCREEN(ty, 0, y));
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
2013-08-26 11:44:46 -07:00
|
|
|
if ((y < -ty->backmax) || !ty->back) return NULL;
|
2013-05-03 19:44:20 -07:00
|
|
|
tssrc = &(ty->back[(ty->backmax + ty->backpos + y) % ty->backmax]);
|
|
|
|
ts = termpty_save_extract(*tssrc);
|
2012-06-12 03:10:01 -07:00
|
|
|
if (!ts) return NULL;
|
2013-05-03 19:44:20 -07:00
|
|
|
*tssrc = ts;
|
2012-06-12 03:10:01 -07:00
|
|
|
*wret = ts->w;
|
|
|
|
return ts->cell;
|
|
|
|
}
|
2013-05-03 03:53:41 -07:00
|
|
|
|
2012-06-12 03:10:01 -07:00
|
|
|
void
|
|
|
|
termpty_write(Termpty *ty, const char *input, int len)
|
|
|
|
{
|
2012-08-05 02:38:58 -07:00
|
|
|
if (ty->fd < 0) return;
|
2012-06-19 12:40:40 -07:00
|
|
|
if (write(ty->fd, input, len) < 0) ERR("write %s", strerror(errno));
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
|
|
|
|
2013-05-03 03:53:41 -07:00
|
|
|
ssize_t
|
|
|
|
termpty_line_length(const Termcell *cells, ssize_t nb_cells)
|
2013-03-17 11:14:18 -07:00
|
|
|
{
|
|
|
|
ssize_t len = nb_cells;
|
|
|
|
|
|
|
|
for (len = nb_cells - 1; len >= 0; len--)
|
|
|
|
{
|
|
|
|
const Termcell *cell = cells + len;
|
|
|
|
|
|
|
|
if ((cell->codepoint != 0) &&
|
|
|
|
(cell->att.bg != COL_INVIS))
|
|
|
|
return len + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-12-01 10:43:52 -08:00
|
|
|
static int
|
|
|
|
termpty_line_find_top(Termpty *ty, int y_end)
|
2013-03-10 11:05:51 -07:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
int y_start = y_end;
|
|
|
|
Termsave *ts;
|
2013-03-10 11:05:51 -07:00
|
|
|
|
2013-12-01 10:43:52 -08:00
|
|
|
while (y_start > 0)
|
2013-03-10 11:05:51 -07:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
if (TERMPTY_SCREEN(ty, ty->w - 1, y_start - 1).att.autowrapped)
|
|
|
|
y_start--;
|
2013-03-10 11:05:51 -07:00
|
|
|
else
|
2013-12-01 10:43:52 -08:00
|
|
|
return y_start;
|
2013-03-10 11:05:51 -07:00
|
|
|
}
|
2013-12-01 10:43:52 -08:00
|
|
|
while (-y_start < ty->backscroll_num)
|
2013-03-20 14:50:16 -07:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
ts = termpty_save_extract(ty->back[(y_start + ty->backpos - 1 +
|
|
|
|
ty->backmax) % ty->backmax]);
|
|
|
|
ty->back[(y_start + ty->backpos - 1 + ty->backmax) % ty->backmax] = ts;
|
|
|
|
if (ts->cell[ts->w - 1].att.autowrapped)
|
|
|
|
y_start--;
|
2013-03-10 11:09:45 -07:00
|
|
|
else
|
2013-12-01 10:43:52 -08:00
|
|
|
return y_start;
|
2013-03-10 11:09:45 -07:00
|
|
|
}
|
2013-12-01 10:43:52 -08:00
|
|
|
return y_start;
|
2013-03-10 11:09:45 -07:00
|
|
|
}
|
|
|
|
|
2013-12-01 10:43:52 -08:00
|
|
|
static int
|
|
|
|
termpty_line_rewrap(Termpty *ty, int y_start, int y_end,
|
|
|
|
Termcell *screen2, Termsave **back2,
|
2013-12-01 14:19:54 -08:00
|
|
|
int w2, int y2_end)
|
2013-03-17 11:49:32 -07:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
int x, x2, y, y2, y2_start;
|
|
|
|
int len, len_last, len_remaining, copy_width, ts2_width;
|
|
|
|
Termsave *ts, *ts2;
|
2013-12-06 02:00:23 -08:00
|
|
|
Termcell *line, *line2 = NULL;
|
2013-11-10 06:34:40 -08:00
|
|
|
|
2013-12-01 10:43:52 -08:00
|
|
|
if (y_end >= 0)
|
2013-03-20 14:04:59 -07:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
len_last = termpty_line_length(&TERMPTY_SCREEN(ty, 0, y_end), ty->w);
|
2013-03-17 11:49:32 -07:00
|
|
|
}
|
2013-12-01 10:43:52 -08:00
|
|
|
else
|
2013-05-03 19:44:20 -07:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
ts = termpty_save_extract(ty->back[(y_end + ty->backpos +
|
|
|
|
ty->backmax) % ty->backmax]);
|
|
|
|
ty->back[(y_end + ty->backpos + ty->backmax) % ty->backmax] = ts;
|
|
|
|
len_last = ts->w;
|
2013-05-03 19:44:20 -07:00
|
|
|
}
|
2013-12-01 10:43:52 -08:00
|
|
|
len_remaining = len_last + (y_end - y_start) * ty->w;
|
|
|
|
y2_start = y2_end;
|
|
|
|
if (len_remaining)
|
2013-04-05 12:12:39 -07:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
y2_start -= (len_remaining + w2 - 1) / w2 - 1;
|
2013-04-05 12:12:39 -07:00
|
|
|
}
|
2013-12-01 10:43:52 -08:00
|
|
|
else
|
2013-04-05 13:12:58 -07:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
if (y2_start < 0)
|
|
|
|
back2[y2_start + ty->backmax] = termpty_save_new(0);
|
|
|
|
return y2_start;
|
2013-04-05 13:12:58 -07:00
|
|
|
}
|
2013-12-01 10:43:52 -08:00
|
|
|
if (-y2_start > ty->backmax)
|
2013-03-23 06:43:20 -07:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
y_start += ((-y2_start - ty->backmax) * w2) / ty->w;
|
|
|
|
x = ((-y2_start - ty->backmax) * w2) % ty->w;
|
|
|
|
len_remaining -= (-y2_start - ty->backmax) * w2;
|
|
|
|
y2_start = -ty->backmax;
|
2013-03-23 13:37:03 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
x = 0;
|
2013-03-17 11:49:32 -07:00
|
|
|
}
|
2013-12-01 10:43:52 -08:00
|
|
|
y = y_start;
|
|
|
|
x2 = 0;
|
|
|
|
y2 = y2_start;
|
2013-03-17 11:49:32 -07:00
|
|
|
|
2013-12-01 10:43:52 -08:00
|
|
|
while (y <= y_end)
|
2013-03-24 13:06:34 -07:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
if (y >= 0)
|
2013-03-24 13:06:34 -07:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
line = &TERMPTY_SCREEN(ty, 0, y);
|
2013-03-24 13:06:34 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
ts = termpty_save_extract(ty->back[(y + ty->backpos +
|
|
|
|
ty->backmax) % ty->backmax]);
|
|
|
|
ty->back[(y + ty->backpos + ty->backmax) % ty->backmax] = ts;
|
|
|
|
line = ts->cell;
|
2013-03-24 13:06:34 -07:00
|
|
|
}
|
2013-12-01 10:43:52 -08:00
|
|
|
if (y == y_end)
|
|
|
|
len = len_last;
|
2013-03-29 13:03:21 -07:00
|
|
|
else
|
2013-12-01 10:43:52 -08:00
|
|
|
len = ty->w;
|
|
|
|
line[len - 1].att.autowrapped = 0;
|
|
|
|
while (x < len)
|
2013-03-25 13:34:52 -07:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
copy_width = MIN(len - x, w2 - x2);
|
|
|
|
if (x2 == 0)
|
2013-03-25 13:34:52 -07:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
if (y2 >= 0)
|
2013-03-25 13:34:52 -07:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
line2 = screen2 + (y2 * w2);
|
2013-03-25 13:34:52 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
ts2_width = MIN(len_remaining, w2);
|
|
|
|
ts2 = termpty_save_new(ts2_width);
|
|
|
|
line2 = ts2->cell;
|
|
|
|
back2[y2 + ty->backmax] = ts2;
|
2013-03-25 13:34:52 -07:00
|
|
|
}
|
|
|
|
}
|
2013-12-06 02:00:23 -08:00
|
|
|
if (line2)
|
2013-03-25 13:34:52 -07:00
|
|
|
{
|
2013-12-06 02:00:23 -08:00
|
|
|
termpty_cell_copy(ty, line + x, line2 + x2, copy_width);
|
|
|
|
x += copy_width;
|
|
|
|
x2 += copy_width;
|
|
|
|
len_remaining -= copy_width;
|
|
|
|
if ((x2 == w2) && (y2 != y2_end))
|
|
|
|
{
|
|
|
|
line2[x2 - 1].att.autowrapped = 1;
|
|
|
|
x2 = 0;
|
|
|
|
y2++;
|
|
|
|
}
|
2013-03-25 13:34:52 -07:00
|
|
|
}
|
|
|
|
}
|
2013-12-01 10:43:52 -08:00
|
|
|
x = 0;
|
|
|
|
y++;
|
2013-03-25 13:34:52 -07:00
|
|
|
}
|
2013-12-01 10:43:52 -08:00
|
|
|
return y2_start;
|
2013-03-10 11:09:45 -07:00
|
|
|
}
|
|
|
|
|
2013-03-17 11:49:32 -07:00
|
|
|
|
2012-06-12 03:10:01 -07:00
|
|
|
void
|
2013-12-01 10:43:52 -08:00
|
|
|
termpty_resize(Termpty *ty, int new_w, int new_h)
|
2012-06-12 03:10:01 -07:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
Termcell *new_screen;
|
|
|
|
Termsave **new_back;
|
|
|
|
int y_start, y_end, new_y_start, new_y_end;
|
2013-12-10 12:21:32 -08:00
|
|
|
int i, altbuf = 0;
|
2012-06-12 03:10:01 -07:00
|
|
|
|
2013-12-01 10:43:52 -08:00
|
|
|
if ((ty->w == new_w) && (ty->h == new_h)) return;
|
2013-12-25 02:57:09 -08:00
|
|
|
if ((new_w == new_h) && (new_w == 1)) return; // FIXME: something weird is
|
|
|
|
// going on at term init
|
2013-03-29 13:42:37 -07:00
|
|
|
|
2013-12-01 10:43:52 -08:00
|
|
|
termpty_save_freeze();
|
2012-06-12 17:26:00 -07:00
|
|
|
|
2013-12-01 10:43:52 -08:00
|
|
|
if (ty->altbuf)
|
|
|
|
{
|
|
|
|
termpty_screen_swap(ty);
|
2013-12-10 12:21:32 -08:00
|
|
|
altbuf = 1;
|
2013-12-01 10:43:52 -08:00
|
|
|
}
|
2012-06-12 17:26:00 -07:00
|
|
|
|
2013-12-01 10:43:52 -08:00
|
|
|
new_screen = calloc(1, sizeof(Termcell) * new_w * new_h);
|
2014-03-24 05:35:08 -07:00
|
|
|
if (!new_screen)
|
2012-06-12 03:10:01 -07:00
|
|
|
{
|
|
|
|
ERR("memerr");
|
2014-03-24 05:35:08 -07:00
|
|
|
return;
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
2013-12-01 10:43:52 -08:00
|
|
|
free(ty->screen2);
|
|
|
|
ty->screen2 = calloc(1, sizeof(Termcell) * new_w * new_h);
|
2012-06-12 03:10:01 -07:00
|
|
|
if (!ty->screen2)
|
|
|
|
{
|
|
|
|
ERR("memerr");
|
|
|
|
}
|
2013-12-01 10:43:52 -08:00
|
|
|
new_back = calloc(sizeof(Termsave *), ty->backmax);
|
2012-06-12 17:26:00 -07:00
|
|
|
|
2013-12-01 10:43:52 -08:00
|
|
|
y_end = ty->state.cy;
|
2014-03-15 10:19:22 -07:00
|
|
|
new_y_end = new_h - 1;
|
2013-12-01 10:43:52 -08:00
|
|
|
while ((y_end >= -ty->backscroll_num) && (new_y_end >= -ty->backmax))
|
2013-03-23 13:37:03 -07:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
y_start = termpty_line_find_top(ty, y_end);
|
|
|
|
new_y_start = termpty_line_rewrap(ty, y_start, y_end, new_screen,
|
2013-12-01 14:19:54 -08:00
|
|
|
new_back, new_w, new_y_end);
|
2013-12-01 10:43:52 -08:00
|
|
|
y_end = y_start - 1;
|
|
|
|
new_y_end = new_y_start - 1;
|
2013-03-23 13:37:03 -07:00
|
|
|
}
|
|
|
|
|
2013-12-01 10:43:52 -08:00
|
|
|
free(ty->screen);
|
|
|
|
for (i = 1; i <= ty->backscroll_num; i++)
|
|
|
|
termpty_save_free(ty->back[(ty->backpos - i + ty->backmax) % ty->backmax]);
|
|
|
|
free(ty->back);
|
2013-03-10 11:09:45 -07:00
|
|
|
|
2013-12-01 10:43:52 -08:00
|
|
|
ty->w = new_w;
|
|
|
|
ty->h = new_h;
|
|
|
|
ty->state.cy = MIN((new_h - 1) - new_y_start, new_h - 1);
|
2014-03-15 10:19:22 -07:00
|
|
|
ty->state.cx = termpty_line_length(new_screen + ((new_h - 1) * new_w),
|
|
|
|
new_w);
|
2013-12-01 10:43:52 -08:00
|
|
|
ty->circular_offset = MAX(new_y_start, 0);
|
|
|
|
ty->backpos = 0;
|
|
|
|
ty->backscroll_num = MAX(-new_y_start, 0);
|
|
|
|
ty->state.had_cr = 0;
|
|
|
|
ty->screen = new_screen;
|
|
|
|
ty->back = new_back;
|
2012-06-12 03:10:01 -07:00
|
|
|
|
2013-12-10 12:21:32 -08:00
|
|
|
if (altbuf) termpty_screen_swap(ty);
|
2012-06-12 03:10:01 -07:00
|
|
|
|
2013-04-05 13:12:58 -07:00
|
|
|
_limit_coord(ty, &(ty->state));
|
|
|
|
_limit_coord(ty, &(ty->swap));
|
|
|
|
_limit_coord(ty, &(ty->save));
|
|
|
|
|
2012-06-12 03:10:01 -07:00
|
|
|
_pty_size(ty);
|
2013-12-01 10:43:52 -08:00
|
|
|
|
|
|
|
termpty_save_thaw();
|
2012-06-12 03:10:01 -07:00
|
|
|
}
|
2012-06-16 10:31:54 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
termpty_backscroll_set(Termpty *ty, int size)
|
|
|
|
{
|
|
|
|
int i;
|
2012-06-17 10:04:17 -07:00
|
|
|
|
2012-06-16 10:31:54 -07:00
|
|
|
if (ty->backmax == size) return;
|
2013-05-03 19:44:20 -07:00
|
|
|
|
|
|
|
termpty_save_freeze();
|
2012-06-16 10:31:54 -07:00
|
|
|
|
|
|
|
if (ty->back)
|
|
|
|
{
|
|
|
|
for (i = 0; i < ty->backmax; i++)
|
|
|
|
{
|
2013-05-03 19:44:20 -07:00
|
|
|
if (ty->back[i]) termpty_save_free(ty->back[i]);
|
2012-06-16 10:31:54 -07:00
|
|
|
}
|
|
|
|
free(ty->back);
|
|
|
|
}
|
2012-06-18 04:18:46 -07:00
|
|
|
if (size > 0)
|
|
|
|
ty->back = calloc(1, sizeof(Termsave *) * size);
|
|
|
|
else
|
|
|
|
ty->back = NULL;
|
2012-06-16 10:31:54 -07:00
|
|
|
ty->backscroll_num = 0;
|
|
|
|
ty->backpos = 0;
|
|
|
|
ty->backmax = size;
|
2013-05-03 19:44:20 -07:00
|
|
|
termpty_save_thaw();
|
2012-06-16 10:31:54 -07:00
|
|
|
}
|
2012-10-09 10:09:26 -07:00
|
|
|
|
|
|
|
pid_t
|
|
|
|
termpty_pid_get(const Termpty *ty)
|
|
|
|
{
|
|
|
|
return ty->pid;
|
|
|
|
}
|
2013-01-28 08:06:26 -08:00
|
|
|
|
|
|
|
void
|
|
|
|
termpty_block_free(Termblock *tb)
|
|
|
|
{
|
2013-04-19 18:37:05 -07:00
|
|
|
char *s;
|
2013-01-28 08:06:26 -08:00
|
|
|
if (tb->path) eina_stringshare_del(tb->path);
|
2013-03-11 00:56:07 -07:00
|
|
|
if (tb->link) eina_stringshare_del(tb->link);
|
2013-04-19 18:37:05 -07:00
|
|
|
if (tb->chid) eina_stringshare_del(tb->chid);
|
2013-01-28 08:06:26 -08:00
|
|
|
if (tb->obj) evas_object_del(tb->obj);
|
2013-04-19 18:37:05 -07:00
|
|
|
EINA_LIST_FREE(tb->cmds, s) free(s);
|
2013-01-28 08:06:26 -08:00
|
|
|
free(tb);
|
|
|
|
}
|
|
|
|
|
|
|
|
Termblock *
|
2013-03-11 00:56:07 -07:00
|
|
|
termpty_block_new(Termpty *ty, int w, int h, const char *path, const char *link)
|
2013-01-28 08:06:26 -08:00
|
|
|
{
|
|
|
|
Termblock *tb;
|
|
|
|
int id;
|
|
|
|
|
|
|
|
id = ty->block.curid;
|
|
|
|
if (!ty->block.blocks)
|
|
|
|
ty->block.blocks = eina_hash_int32_new((Eina_Free_Cb)termpty_block_free);
|
|
|
|
if (!ty->block.blocks) return NULL;
|
|
|
|
tb = eina_hash_find(ty->block.blocks, &id);
|
|
|
|
if (tb)
|
|
|
|
{
|
|
|
|
if (tb->active)
|
|
|
|
ty->block.active = eina_list_remove(ty->block.active, tb);
|
|
|
|
eina_hash_del(ty->block.blocks, &id, tb);
|
|
|
|
}
|
|
|
|
tb = calloc(1, sizeof(Termblock));
|
|
|
|
if (!tb) return NULL;
|
2013-04-20 00:54:11 -07:00
|
|
|
tb->pty = ty;
|
2013-01-28 08:06:26 -08:00
|
|
|
tb->id = id;
|
|
|
|
tb->w = w;
|
|
|
|
tb->h = h;
|
|
|
|
tb->path = eina_stringshare_add(path);
|
2013-03-11 00:56:07 -07:00
|
|
|
if (link) tb->link = eina_stringshare_add(link);
|
2013-01-28 08:06:26 -08:00
|
|
|
eina_hash_add(ty->block.blocks, &id, tb);
|
|
|
|
ty->block.curid++;
|
|
|
|
if (ty->block.curid >= 8192) ty->block.curid = 0;
|
|
|
|
return tb;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-01-28 16:43:13 -08:00
|
|
|
termpty_block_insert(Termpty *ty, int ch, Termblock *blk)
|
2013-01-28 08:06:26 -08:00
|
|
|
{
|
|
|
|
// bit 0-8 = y (9b 0->511)
|
|
|
|
// bit 9-17 = x (9b 0->511)
|
|
|
|
// bit 18-30 = id (13b 0->8191)
|
|
|
|
// bit 31 = 1
|
|
|
|
//
|
|
|
|
// fg/bg = 8+8bit unused. (use for extra id bits? so 16 + 13 == 29bit?)
|
|
|
|
//
|
|
|
|
// cp = (1 << 31) | ((id 0x1fff) << 18) | ((x & 0x1ff) << 9) | (y & 0x1ff);
|
2013-01-28 16:43:13 -08:00
|
|
|
Termexp *ex;
|
|
|
|
|
|
|
|
ex = calloc(1, sizeof(Termexp));
|
|
|
|
if (!ex) return;
|
|
|
|
ex->ch = ch;
|
|
|
|
ex->left = blk->w * blk->h;
|
|
|
|
ex->id = blk->id;
|
|
|
|
ex->w = blk->w;
|
|
|
|
ex->h = blk->h;
|
|
|
|
ty->block.expecting = eina_list_append(ty->block.expecting, ex);
|
2013-01-28 08:06:26 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
termpty_block_id_get(Termcell *cell, int *x, int *y)
|
|
|
|
{
|
|
|
|
int id;
|
|
|
|
|
|
|
|
if (!(cell->codepoint & 0x80000000)) return -1;
|
|
|
|
id = (cell->codepoint >> 18) & 0x1fff;
|
|
|
|
*x = (cell->codepoint >> 9) & 0x1ff;
|
|
|
|
*y = cell->codepoint & 0x1ff;
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
Termblock *
|
|
|
|
termpty_block_get(Termpty *ty, int id)
|
|
|
|
{
|
2013-01-29 07:54:47 -08:00
|
|
|
if (!ty->block.blocks) return NULL;
|
2013-01-28 08:06:26 -08:00
|
|
|
return eina_hash_find(ty->block.blocks, &id);
|
|
|
|
}
|
2013-01-29 07:54:47 -08:00
|
|
|
|
2013-04-20 00:54:11 -07:00
|
|
|
void
|
|
|
|
termpty_block_chid_update(Termpty *ty, Termblock *blk)
|
|
|
|
{
|
|
|
|
if (!blk->chid) return;
|
|
|
|
if (!ty->block.chid_map)
|
|
|
|
ty->block.chid_map = eina_hash_string_superfast_new(NULL);
|
|
|
|
if (!ty->block.chid_map) return;
|
|
|
|
eina_hash_add(ty->block.chid_map, blk->chid, blk);
|
|
|
|
}
|
2013-01-29 07:54:47 -08:00
|
|
|
|
2013-04-20 00:54:11 -07:00
|
|
|
Termblock *
|
|
|
|
termpty_block_chid_get(Termpty *ty, const char *chid)
|
|
|
|
{
|
|
|
|
Termblock *tb;
|
|
|
|
|
|
|
|
tb = eina_hash_find(ty->block.chid_map, chid);
|
|
|
|
return tb;
|
|
|
|
}
|
2013-01-29 07:54:47 -08:00
|
|
|
|
2013-10-28 02:55:19 -07:00
|
|
|
static void
|
|
|
|
_handle_block_codepoint_overwrite_heavy(Termpty *ty, int oldc, int newc)
|
2013-01-29 07:54:47 -08:00
|
|
|
{
|
|
|
|
Termblock *tb;
|
|
|
|
int ido = 0, idn = 0;
|
2013-10-25 07:37:10 -07:00
|
|
|
|
2013-01-29 07:54:47 -08:00
|
|
|
if (oldc & 0x80000000) ido = (oldc >> 18) & 0x1fff;
|
|
|
|
if (newc & 0x80000000) idn = (newc >> 18) & 0x1fff;
|
|
|
|
if (((oldc & 0x80000000) && (newc & 0x80000000)) && (idn == ido)) return;
|
|
|
|
|
|
|
|
if (oldc & 0x80000000)
|
|
|
|
{
|
|
|
|
tb = termpty_block_get(ty, ido);
|
|
|
|
if (!tb) return;
|
|
|
|
tb->refs--;
|
|
|
|
if (tb->refs == 0)
|
|
|
|
{
|
|
|
|
if (tb->active)
|
|
|
|
ty->block.active = eina_list_remove(ty->block.active, tb);
|
|
|
|
eina_hash_del(ty->block.blocks, &ido, tb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newc & 0x80000000)
|
|
|
|
{
|
|
|
|
tb = termpty_block_get(ty, idn);
|
|
|
|
if (!tb) return;
|
|
|
|
tb->refs++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-28 02:55:19 -07:00
|
|
|
/* Try to trick the compiler into inlining the first test */
|
|
|
|
static inline void
|
2014-03-25 13:34:48 -07:00
|
|
|
_handle_block_codepoint_overwrite(Termpty *ty, Eina_Unicode oldc, Eina_Unicode newc)
|
2013-10-28 02:55:19 -07:00
|
|
|
{
|
|
|
|
if (!((oldc | newc) & 0x80000000)) return;
|
|
|
|
_handle_block_codepoint_overwrite_heavy(ty, oldc, newc);
|
|
|
|
}
|
|
|
|
|
2013-01-29 07:54:47 -08:00
|
|
|
void
|
|
|
|
termpty_cell_copy(Termpty *ty, Termcell *src, Termcell *dst, int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
_handle_block_codepoint_overwrite(ty, dst[i].codepoint, src[i].codepoint);
|
|
|
|
dst[i] = src[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-18 12:41:30 -08:00
|
|
|
termpty_screen_swap(Termpty *ty)
|
2013-01-29 07:54:47 -08:00
|
|
|
{
|
2013-12-01 10:43:52 -08:00
|
|
|
Termcell *tmp_screen;
|
|
|
|
int tmp_circular_offset;
|
2013-12-10 12:21:32 -08:00
|
|
|
int tmp_appcursor = ty->state.appcursor;
|
2013-12-01 10:43:52 -08:00
|
|
|
|
|
|
|
tmp_screen = ty->screen;
|
|
|
|
ty->screen = ty->screen2;
|
|
|
|
ty->screen2 = tmp_screen;
|
|
|
|
|
2013-12-05 13:46:39 -08:00
|
|
|
if (ty->altbuf)
|
|
|
|
ty->state = ty->swap;
|
|
|
|
else
|
|
|
|
ty->swap = ty->state;
|
2013-12-01 10:43:52 -08:00
|
|
|
|
|
|
|
tmp_circular_offset = ty->circular_offset;
|
|
|
|
ty->circular_offset = ty->circular_offset2;
|
|
|
|
ty->circular_offset2 = tmp_circular_offset;
|
2013-11-18 12:41:30 -08:00
|
|
|
|
2013-12-10 12:21:32 -08:00
|
|
|
ty->state.appcursor = tmp_appcursor;
|
|
|
|
|
2013-11-18 12:41:30 -08:00
|
|
|
ty->altbuf = !ty->altbuf;
|
|
|
|
|
|
|
|
if (ty->cb.cancel_sel.func)
|
|
|
|
ty->cb.cancel_sel.func(ty->cb.cancel_sel.data);
|
2013-01-29 07:54:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
termpty_cell_fill(Termpty *ty, Termcell *src, Termcell *dst, int n)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (src)
|
|
|
|
{
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
_handle_block_codepoint_overwrite(ty, dst[i].codepoint, src[0].codepoint);
|
|
|
|
dst[i] = src[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
_handle_block_codepoint_overwrite(ty, dst[i].codepoint, 0);
|
|
|
|
memset(&(dst[i]), 0, sizeof(*dst));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-03-25 13:34:48 -07:00
|
|
|
termpty_cell_codepoint_att_fill(Termpty *ty, Eina_Unicode codepoint,
|
|
|
|
Termatt att, Termcell *dst, int n)
|
2013-01-29 07:54:47 -08:00
|
|
|
{
|
2013-10-28 02:55:19 -07:00
|
|
|
Termcell local = { codepoint, att };
|
2013-01-29 07:54:47 -08:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
_handle_block_codepoint_overwrite(ty, dst[i].codepoint, codepoint);
|
2013-10-28 02:55:19 -07:00
|
|
|
dst[i] = local;
|
2013-01-29 07:54:47 -08:00
|
|
|
}
|
|
|
|
}
|
2013-09-22 08:10:39 -07:00
|
|
|
|
|
|
|
Config *
|
|
|
|
termpty_config_get(const Termpty *ty)
|
|
|
|
{
|
|
|
|
return termio_config_get(ty->obj);
|
|
|
|
}
|