2024-02-22 20:05:56 +00:00
|
|
|
#include <asm-generic/socket.h>
|
|
|
|
#define _GNU_SOURCE
|
2021-03-31 21:28:26 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <locale.h>
|
|
|
|
#include <unistd.h>
|
2024-02-22 20:05:56 +00:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/epoll.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <fcntl.h>
|
2021-03-31 21:28:26 +00:00
|
|
|
#include "tmt.h"
|
|
|
|
#define HASHSIZE 2053
|
|
|
|
#define MAXNAMELEN 256
|
|
|
|
#define MAXDATALEN 8192
|
|
|
|
struct terment {
|
|
|
|
struct terment *next;
|
|
|
|
char *name;
|
2024-02-22 20:05:56 +00:00
|
|
|
int fd;
|
2021-03-31 21:28:26 +00:00
|
|
|
TMT *vt;
|
|
|
|
};
|
|
|
|
|
|
|
|
#define SETNODE 1
|
|
|
|
#define WRITE 2
|
|
|
|
#define READBUFF 0
|
2024-02-22 20:05:56 +00:00
|
|
|
#define CLOSECONN 3
|
|
|
|
#define MAXEVTS 16
|
2021-03-31 21:28:26 +00:00
|
|
|
static struct terment *buffers[HASHSIZE];
|
2024-02-22 20:05:56 +00:00
|
|
|
static char* nodenames[HASHSIZE];
|
2021-03-31 21:28:26 +00:00
|
|
|
|
|
|
|
unsigned long hash(char *str)
|
|
|
|
/* djb2a */
|
|
|
|
{
|
|
|
|
unsigned long idx = 5381;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
while ((c = *str++))
|
|
|
|
idx = ((idx << 5) + idx) + c;
|
|
|
|
return idx % HASHSIZE;
|
|
|
|
}
|
|
|
|
|
|
|
|
TMT *get_termentbyname(char *name) {
|
|
|
|
struct terment *ret;
|
|
|
|
for (ret = buffers[hash(name)]; ret != NULL; ret = ret->next)
|
|
|
|
if (strcmp(name, ret->name) == 0)
|
|
|
|
return ret->vt;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-02-22 20:05:56 +00:00
|
|
|
TMT *set_termentbyname(char *name, int fd) {
|
2021-03-31 21:28:26 +00:00
|
|
|
struct terment *ret;
|
|
|
|
int idx;
|
|
|
|
|
2024-02-22 20:05:56 +00:00
|
|
|
if (nodenames[fd] == NULL) {
|
|
|
|
nodenames[fd] = strdup(name);
|
|
|
|
}
|
2021-03-31 21:28:26 +00:00
|
|
|
idx = hash(name);
|
|
|
|
for (ret = buffers[idx]; ret != NULL; ret = ret->next)
|
|
|
|
if (strcmp(name, ret->name) == 0)
|
|
|
|
return ret->vt;
|
|
|
|
ret = (struct terment *)malloc(sizeof(*ret));
|
|
|
|
ret->next = buffers[idx];
|
|
|
|
ret->name = strdup(name);
|
2024-02-22 20:05:56 +00:00
|
|
|
ret->fd = fd;
|
2021-03-31 21:28:26 +00:00
|
|
|
ret->vt = tmt_open(31, 100, NULL, NULL, L"→←↑↓■◆▒°±▒┘┐┌└┼⎺───⎽├┤┴┬│≤≥π≠£•");
|
|
|
|
buffers[idx] = ret;
|
|
|
|
return ret->vt;
|
|
|
|
}
|
|
|
|
|
2024-02-22 20:05:56 +00:00
|
|
|
void dump_vt(TMT* outvt, int outfd) {
|
2021-03-31 21:28:26 +00:00
|
|
|
const TMTSCREEN *out = tmt_screen(outvt);
|
|
|
|
const TMTPOINT *curs = tmt_cursor(outvt);
|
2021-04-01 16:51:32 +00:00
|
|
|
int line, idx, maxcol, maxrow;
|
2021-04-01 20:28:28 +00:00
|
|
|
bool bold = false;
|
|
|
|
bool dim = false;
|
|
|
|
bool underline = false;
|
|
|
|
bool blink = false;
|
|
|
|
bool reverse = false;
|
|
|
|
bool invisible = false;
|
|
|
|
bool intensitychg = false;
|
|
|
|
tmt_color_t fg = TMT_COLOR_DEFAULT;
|
|
|
|
tmt_color_t bg = TMT_COLOR_DEFAULT;
|
|
|
|
wchar_t sgrline[30];
|
2024-02-22 20:05:56 +00:00
|
|
|
char strbuffer[128];
|
2021-04-01 20:28:28 +00:00
|
|
|
size_t srgidx = 0;
|
|
|
|
char colorcode = 0;
|
2024-02-22 20:05:56 +00:00
|
|
|
write(outfd, "\033c", 2);
|
2021-04-01 16:51:32 +00:00
|
|
|
maxcol = 0;
|
|
|
|
maxrow = 0;
|
|
|
|
for (line = out->nline - 1; line >= 0; --line) {
|
|
|
|
for (idx = out->ncol - 1; idx > maxcol; --idx) {
|
|
|
|
if (out->lines[line]->chars[idx].c != L' ') {
|
|
|
|
if (maxrow < line)
|
|
|
|
maxrow = line;
|
|
|
|
maxcol = idx;
|
|
|
|
break;
|
2021-03-31 21:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-01 16:51:32 +00:00
|
|
|
}
|
|
|
|
for (line = 0; line <= maxrow; line++) {
|
|
|
|
for (idx = 0; idx <= maxcol; idx++) {
|
2021-04-01 20:28:28 +00:00
|
|
|
sgrline[0] = L'\x00';
|
|
|
|
intensitychg = false;
|
|
|
|
if (out->lines[line]->chars[idx].a.bold != bold) {
|
|
|
|
bold = out->lines[line]->chars[idx].a.bold;
|
|
|
|
intensitychg = true; // Can't unbold without changing dim
|
|
|
|
}
|
|
|
|
if (out->lines[line]->chars[idx].a.dim != dim) {
|
|
|
|
dim = out->lines[line]->chars[idx].a.dim;
|
|
|
|
intensitychg = true; // Can't undim without changing bold
|
|
|
|
}
|
|
|
|
if (intensitychg) {
|
|
|
|
intensitychg = false;
|
|
|
|
wcscat(sgrline, L"22;");
|
|
|
|
if (bold)
|
|
|
|
wcscat(sgrline, L"1;");
|
|
|
|
if (dim)
|
|
|
|
wcscat(sgrline, L"2;");
|
|
|
|
}
|
|
|
|
if (out->lines[line]->chars[idx].a.underline != underline) {
|
|
|
|
underline = out->lines[line]->chars[idx].a.underline;
|
|
|
|
if (underline)
|
|
|
|
wcscat(sgrline, L"4;");
|
|
|
|
else
|
|
|
|
wcscat(sgrline, L"24;");
|
|
|
|
}
|
|
|
|
if (out->lines[line]->chars[idx].a.blink != blink) {
|
|
|
|
blink = out->lines[line]->chars[idx].a.blink;
|
|
|
|
if (blink)
|
|
|
|
wcscat(sgrline, L"5;");
|
|
|
|
else
|
|
|
|
wcscat(sgrline, L"25;");
|
|
|
|
}
|
|
|
|
if (out->lines[line]->chars[idx].a.reverse != reverse) {
|
|
|
|
reverse = out->lines[line]->chars[idx].a.reverse;
|
|
|
|
if (reverse)
|
|
|
|
wcscat(sgrline, L"7;");
|
|
|
|
else
|
|
|
|
wcscat(sgrline, L"27;");
|
|
|
|
}
|
|
|
|
if (out->lines[line]->chars[idx].a.invisible != invisible) {
|
|
|
|
invisible = out->lines[line]->chars[idx].a.invisible;
|
|
|
|
if (invisible)
|
|
|
|
wcscat(sgrline, L"8;");
|
|
|
|
else
|
|
|
|
wcscat(sgrline, L"28;");
|
|
|
|
}
|
|
|
|
if (out->lines[line]->chars[idx].a.fg != fg) {
|
|
|
|
fg = out->lines[line]->chars[idx].a.fg;
|
|
|
|
if (fg == TMT_COLOR_DEFAULT)
|
|
|
|
colorcode = 39;
|
|
|
|
else
|
|
|
|
colorcode = 29 + fg;
|
|
|
|
swprintf(sgrline + wcslen(sgrline), 4, L"%d;", colorcode);
|
|
|
|
}
|
|
|
|
if (out->lines[line]->chars[idx].a.bg != bg) {
|
|
|
|
bg = out->lines[line]->chars[idx].a.bg;
|
|
|
|
if (bg == TMT_COLOR_DEFAULT)
|
|
|
|
colorcode = 49;
|
|
|
|
else
|
|
|
|
colorcode = 39 + bg;
|
|
|
|
swprintf(sgrline + wcslen(sgrline), 4, L"%d;", colorcode);
|
|
|
|
}
|
|
|
|
if (sgrline[0] != 0) {
|
|
|
|
sgrline[wcslen(sgrline) - 1] = 0; // Trim last ;
|
2024-02-22 20:05:56 +00:00
|
|
|
|
|
|
|
snprintf(strbuffer, sizeof(strbuffer), "\033[%lsm", sgrline);
|
|
|
|
write(outfd, strbuffer, strlen(strbuffer));
|
|
|
|
write(outfd, "\033[]", 3);
|
2021-04-01 20:28:28 +00:00
|
|
|
}
|
2024-02-22 20:05:56 +00:00
|
|
|
snprintf(strbuffer, sizeof(strbuffer), "%lc", out->lines[line]->chars[idx].c);
|
|
|
|
write(outfd, strbuffer, strlen(strbuffer));
|
2021-04-01 16:51:32 +00:00
|
|
|
}
|
|
|
|
if (line < maxrow)
|
2024-02-22 20:05:56 +00:00
|
|
|
write(outfd, "\r\n", 2);
|
2021-03-31 21:28:26 +00:00
|
|
|
}
|
2024-02-22 20:05:56 +00:00
|
|
|
//fflush(stdout);
|
|
|
|
snprintf(strbuffer, sizeof(strbuffer), "\x1b[%ld;%ldH", curs->r + 1, curs->c + 1);
|
|
|
|
write(outfd, strbuffer, strlen(strbuffer));
|
|
|
|
//fflush(stdout);
|
2021-03-31 21:28:26 +00:00
|
|
|
}
|
|
|
|
|
2024-02-22 20:05:56 +00:00
|
|
|
int handle_traffic(int fd) {
|
2021-03-31 21:28:26 +00:00
|
|
|
int cmd, length;
|
|
|
|
char currnode[MAXNAMELEN];
|
2024-02-22 20:05:56 +00:00
|
|
|
char cmdbuf[MAXDATALEN];
|
|
|
|
char *nodename;
|
2021-03-31 21:28:26 +00:00
|
|
|
TMT *currvt = NULL;
|
|
|
|
TMT *outvt = NULL;
|
2024-02-22 20:05:56 +00:00
|
|
|
length = read(fd, &cmd, 4);
|
|
|
|
if (length <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
length = cmd & 536870911;
|
|
|
|
cmd = cmd >> 29;
|
|
|
|
if (cmd == SETNODE) {
|
|
|
|
cmd = read(fd, currnode, length);
|
|
|
|
currnode[length] = 0;
|
|
|
|
if (cmd < 0)
|
|
|
|
return 0;
|
|
|
|
currvt = set_termentbyname(currnode, fd);
|
|
|
|
} else if (cmd == WRITE) {
|
|
|
|
if (currvt == NULL) {
|
|
|
|
nodename = nodenames[fd];
|
|
|
|
currvt = set_termentbyname(nodename, fd);
|
|
|
|
}
|
|
|
|
cmd = read(fd, cmdbuf, length);
|
|
|
|
cmdbuf[length] = 0;
|
|
|
|
if (cmd < 0)
|
|
|
|
return 0;
|
|
|
|
tmt_write(currvt, cmdbuf, length);
|
|
|
|
} else if (cmd == READBUFF) {
|
|
|
|
cmd = read(fd, cmdbuf, length);
|
|
|
|
cmdbuf[length] = 0;
|
|
|
|
if (cmd < 0)
|
|
|
|
return 0;
|
|
|
|
outvt = get_termentbyname(cmdbuf);
|
|
|
|
if (outvt != NULL)
|
|
|
|
dump_vt(outvt, fd);
|
|
|
|
length = write(fd, "\x00", 1);
|
|
|
|
if (length < 0)
|
|
|
|
return 0;
|
|
|
|
} else if (cmd == CLOSECONN) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
int numevts;
|
|
|
|
int status;
|
|
|
|
int poller;
|
2024-06-07 13:36:18 +00:00
|
|
|
int n, rt;
|
2024-02-22 20:05:56 +00:00
|
|
|
socklen_t len;
|
2024-06-07 13:36:18 +00:00
|
|
|
int ctlsock = 0;
|
|
|
|
int currsock = 0;
|
|
|
|
socklen_t addrlen = 0;
|
2024-02-22 20:05:56 +00:00
|
|
|
struct ucred ucr;
|
|
|
|
|
|
|
|
struct epoll_event epvt, evts[MAXEVTS];
|
2021-03-31 21:28:26 +00:00
|
|
|
stdin = freopen(NULL, "rb", stdin);
|
|
|
|
if (stdin == NULL) {
|
|
|
|
exit(1);
|
|
|
|
}
|
2024-02-22 20:05:56 +00:00
|
|
|
memset(&addr, 0, sizeof(struct sockaddr_un));
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
strncpy(addr.sun_path + 1, argv[1], sizeof(addr.sun_path) - 2); // abstract namespace socket
|
|
|
|
ctlsock = socket(AF_UNIX, SOCK_STREAM, 0);
|
2024-06-07 13:36:18 +00:00
|
|
|
if (ctlsock < 0) {
|
|
|
|
perror("Unable to open unix socket - ");
|
|
|
|
exit(1);
|
|
|
|
}
|
2024-02-22 20:05:56 +00:00
|
|
|
status = bind(ctlsock, (const struct sockaddr*)&addr, sizeof(sa_family_t) + strlen(argv[1]) + 1); //sizeof(struct sockaddr_un));
|
|
|
|
if (status < 0) {
|
|
|
|
perror("Unable to open unix socket - ");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
listen(ctlsock, 128);
|
|
|
|
poller = epoll_create(1);
|
|
|
|
memset(&epvt, 0, sizeof(struct epoll_event));
|
|
|
|
epvt.events = EPOLLIN;
|
|
|
|
epvt.data.fd = ctlsock;
|
|
|
|
if (epoll_ctl(poller, EPOLL_CTL_ADD, ctlsock, &epvt) < 0) {
|
|
|
|
perror("Unable to poll the socket");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
// create a unix domain socket for accepting, each connection is only allowed to either read or write, not both
|
2021-03-31 21:28:26 +00:00
|
|
|
while (1) {
|
2024-02-22 20:05:56 +00:00
|
|
|
numevts = epoll_wait(poller, evts, MAXEVTS, -1);
|
|
|
|
if (numevts < 0) {
|
|
|
|
perror("Failed wait");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
for (n = 0; n < numevts; ++n) {
|
|
|
|
if (evts[n].data.fd == ctlsock) {
|
|
|
|
currsock = accept(ctlsock, (struct sockaddr *) &addr, &addrlen);
|
|
|
|
len = sizeof(ucr);
|
2024-06-07 13:36:18 +00:00
|
|
|
rt = getsockopt(currsock, SOL_SOCKET, SO_PEERCRED, &ucr, &len);
|
|
|
|
if (rt < 0) {
|
|
|
|
close(currsock);
|
|
|
|
continue;
|
|
|
|
}
|
2024-02-22 20:05:56 +00:00
|
|
|
if (ucr.uid != getuid()) { // block access for other users
|
|
|
|
close(currsock);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
memset(&epvt, 0, sizeof(struct epoll_event));
|
|
|
|
epvt.events = EPOLLIN;
|
|
|
|
epvt.data.fd = currsock;
|
|
|
|
epoll_ctl(poller, EPOLL_CTL_ADD, currsock, &epvt);
|
|
|
|
} else {
|
|
|
|
if (!handle_traffic(evts[n].data.fd)) {
|
|
|
|
epoll_ctl(poller, EPOLL_CTL_DEL, evts[n].data.fd, NULL);
|
|
|
|
close(evts[n].data.fd);
|
|
|
|
if (nodenames[evts[n].data.fd] != NULL) {
|
|
|
|
free(nodenames[evts[n].data.fd]);
|
|
|
|
nodenames[evts[n].data.fd] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-31 21:28:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-22 20:05:56 +00:00
|
|
|
|
|
|
|
|