about summary refs log tree commit diff
path: root/edit.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-02-05 00:20:39 -0500
committerJune McEnroe <june@causal.agency>2020-02-05 00:20:39 -0500
commit4cce893eab7403821ff211f64a7df05051fd6f52 (patch)
tree3499aa89f3a5b59356abcc9c215babe057bff4a4 /edit.c
parentConvert editHead and editTail from wchar_t (diff)
downloadcatgirl-4cce893eab7403821ff211f64a7df05051fd6f52.tar.gz
catgirl-4cce893eab7403821ff211f64a7df05051fd6f52.zip
Add extremely basic editing and message sending
Diffstat (limited to '')
-rw-r--r--edit.c32
1 files changed, 25 insertions, 7 deletions
diff --git a/edit.c b/edit.c
index e142507..68593d1 100644
--- a/edit.c
+++ b/edit.c
@@ -23,22 +23,40 @@
 #include "chat.h"
 
 enum { Cap = 512 };
-static wchar_t buf[Cap] = L"foo\0033bar\3baz";
-static size_t len = 12;
-static size_t pos = 6;
+static wchar_t buf[Cap];
+static size_t len;
+static size_t pos;
 
-const char *editHead(void) {
+char *editHead(void) {
 	static char mbs[MB_LEN_MAX * Cap];
 	const wchar_t *ptr = buf;
-	size_t n = wcsnrtombs(mbs, &ptr, pos, sizeof(mbs), NULL);
+	size_t n = wcsnrtombs(mbs, &ptr, pos, sizeof(mbs) - 1, NULL);
 	assert(n != (size_t)-1);
+	mbs[n] = '\0';
 	return mbs;
 }
 
-const char *editTail(void) {
+char *editTail(void) {
 	static char mbs[MB_LEN_MAX * Cap];
 	const wchar_t *ptr = &buf[pos];
-	size_t n = wcsnrtombs(mbs, &ptr, len - pos, sizeof(mbs), NULL);
+	size_t n = wcsnrtombs(mbs, &ptr, len - pos, sizeof(mbs) - 1, NULL);
 	assert(n != (size_t)-1);
+	mbs[n] = '\0';
 	return mbs;
 }
+
+void edit(size_t id, enum Edit op, wchar_t ch) {
+	switch (op) {
+		break; case EditKill: len = pos = 0;
+		break; case EditInsert: {
+			if (len == Cap) break;
+			buf[pos++] = ch;
+			len++;
+		}
+		break; case EditEnter: {
+			pos = 0;
+			command(id, editTail());
+			len = 0;
+		}
+	}
+}