summary refs log tree commit diff
path: root/ingest.c
blob: 3991adc12aa6687e0dbea2cc111eb959757da88d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/* Copyright (C) 2019  C. McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <assert.h>
#include <err.h>
#include <fcntl.h>
#include <locale.h>
#include <poll.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sysexits.h>
#include <unistd.h>
#include <wchar.h>

#include "term.h"

int main(void) {
	int error;
	setlocale(LC_CTYPE, "");

	// TODO: Read info from file.
	const char *path = "example.sock";
	struct Term *term = termAlloc(24, 80);
	
	int server = socket(PF_LOCAL, SOCK_STREAM, 0);
	if (server < 0) err(EX_OSERR, "socket");

	struct sockaddr_un addr = { .sun_family = AF_LOCAL };
	strncpy(addr.sun_path, path, sizeof(addr.sun_path));
	error = bind(server, (struct sockaddr *)&addr, SUN_LEN(&addr));
	if (error) err(EX_CANTCREAT, "%s", path);

	error = listen(server, 0);
	if (error) err(EX_OSERR, "listen");

	int maxClient = server;

	struct pollfd fds[2] = {
		{ .events = POLLIN, .fd = STDIN_FILENO },
		{ .events = POLLIN, .fd = server },
	};
	for (;;) {
		int nfds = poll(fds, 2, 60 * 1000);
		if (nfds < 0) err(EX_IOERR, "poll");

		if (!nfds) {
			// TODO: Increment idle time in info.
		}

		if (fds[0].revents) {
			char buf[4096];
			ssize_t rlen = read(STDIN_FILENO, buf, sizeof(buf));
			if (rlen < 0) err(EX_IOERR, "read");

			int client = server + 1;
			while (client <= maxClient) {
				ssize_t wlen = write(client, buf, rlen);
				if (wlen == rlen) {
					client++;
					continue;
				}
				close(client);
				if (client < maxClient) {
					int fd = dup(maxClient);
					assert(fd == client);
					close(maxClient);
				}
				maxClient--;
			}

			ssize_t pos = 0;
			while (pos < rlen) {
				wchar_t ch;
				int n = mbtowc(&ch, &buf[pos], rlen - pos);
				if (n <= 0) break;
				termUpdate(term, ch);
				pos += n;
			}
		}

		if (fds[1].revents) {
			int client = accept(server, NULL, NULL);
			if (client < 0) continue;
			fcntl(client, F_SETFL, O_NONBLOCK);

			int yes = 1;
			error = setsockopt(client, SOL_SOCKET, SO_NOSIGPIPE, &yes, sizeof(yes));
			if (error) {
				close(client);
				continue;
			}

			error = termSnapshot(term, client);
			if (error) {
				close(client);
				continue;
			}

			maxClient++;
			assert(client == maxClient);
		}
	}
}