summary refs log tree commit diff
path: root/pls.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2018-08-06 14:19:52 -0400
committerJune McEnroe <june@causal.agency>2018-08-06 14:19:52 -0400
commitd6fb797b11aa6dd031032faf425be3ce5a69661d (patch)
treee348e4ec6ed53a9700219336fd6a3e3733093a56 /pls.c
parentRename line editing functions (diff)
downloadcatgirl-d6fb797b11aa6dd031032faf425be3ce5a69661d.tar.gz
catgirl-d6fb797b11aa6dd031032faf425be3ce5a69661d.zip
Use wchar_t strings for all of UI
vaswprintf is a nightmare.
Diffstat (limited to '')
-rw-r--r--pls.c67
1 files changed, 67 insertions, 0 deletions
diff --git a/pls.c b/pls.c
new file mode 100644
index 0000000..bd40dbd
--- /dev/null
+++ b/pls.c
@@ -0,0 +1,67 @@
+/* Copyright (C) 2018  Curtis 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 <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <wchar.h>
+
+wchar_t *wcssep(wchar_t **stringp, const wchar_t *delim) {
+	wchar_t *orig = *stringp;
+	if (!orig) return NULL;
+	size_t i = wcscspn(orig, delim);
+	*stringp = NULL;
+	if (orig[i]) {
+		orig[i] = '\0';
+		*stringp = &orig[i + 1];
+	}
+	return orig;
+}
+
+// From <https://en.cppreference.com/w/c/io/fwprintf#Notes>:
+//
+// While narrow strings provide snprintf, which makes it possible to determine
+// the required output buffer size, there is no equivalent for wide strings
+// (until C11's snwprintf_s), and in order to determine the buffer size, the
+// program may need to call swprintf, check the result value, and reallocate a
+// larger buffer, trying again until successful.
+//
+// snwprintf_s, unlike swprintf_s, will truncate the result to fit within the
+// array pointed to by buffer, even though truncation is treated as an error by
+// most bounds-checked functions.
+int vaswprintf(wchar_t **ret, const wchar_t *format, va_list ap) {
+	*ret = NULL;
+
+	for (size_t cap = 2 * wcslen(format);; cap *= 2) {
+		wchar_t *buf = realloc(*ret, 1 + cap);
+		if (!buf) goto fail;
+		*ret = buf;
+
+		va_list _ap;
+		va_copy(_ap, ap);
+		int len = vswprintf(*ret, 1 + cap, format, _ap);
+		va_end(_ap);
+
+		if (len >= 0) return len;
+		if (errno != EOVERFLOW) goto fail;
+	}
+
+fail:
+	free(*ret);
+	*ret = NULL;
+	return -1;
+}
oad dates from ~/.config/when/datesJune McEnroe 2022-08-15Allow names with prefixes of months and daysJune McEnroe 2022-08-15Add named dates to whenJune McEnroe 2022-08-14Remove tweets text fileJune McEnroe Such link rot. 2022-08-09Fix all copyright noticesJune McEnroe 2022-08-04Add Conversations With FriendsJune McEnroe The better of the two, but largely the same. I feel like these books are a bit too autobiographical, but I don't know if I'm allowed to accuse an author of that. My real problem is that I read these books as largely uncritical of their characters. They behave in nonsense ways, are mostly uncritical of their own behaviour, and don't really have arcs of growth or change. I suppose this book had a bit of one, but only in the last two chapters. 2022-07-30Add Normal PeopleJune McEnroe Unbearably straight. Eyerolls and sighs per page off the charts. Shout out to Joanna, I guess. I kinda like the lack of quotation marks though to be honest. After half of the Ruth Ozeki novel and now this, I need to get back to some genre fiction. 2022-07-26Rewrite glitch from new pngoJune McEnroe 2022-07-26Update Care with time-to-ID and piercingsJune McEnroe 2022-07-26Add -w to upJune McEnroe 2022-07-13Set push.autoSetupRemoteJune McEnroe 2022-07-08Remove TOURJune McEnroe There is not that much distinct stuff here anymore. 2022-07-03Add The Bone Shard EmperorJune McEnroe Suffers a little bit from middle book but I really enjoyed it. Read it faster than the first one too, despite its length. 2022-06-25Bump xterm font size to 12June McEnroe 2022-06-10Handle subshells (and functions) inside substitutionsJune McEnroe 2022-06-10Switch to jorts Install scriptJune McEnroe 2022-06-08Indicate if still reading or no resultsJune McEnroe 2022-06-08Add Maiden, Mother, CroneJune McEnroe Mixed bag like most collections of short stories. Some of them are pretty good. The author of the worst written story also has the worst written bio. 2022-06-05FIRST SHOW IN 2.5 YEARS BABEY!!!June McEnroe 2022-06-03Set line number on File linesJune McEnroe 2022-06-03Stop polling stdin after EOFJune McEnroe 2022-06-02Set TABSIZE=4June McEnroe Absolutely indiscriminately. 2022-06-02Do basic match highlightingJune McEnroe 2022-06-02Clean up parsing a littleJune McEnroe 2022-06-02Don't duplicate path stringJune McEnroe 2022-06-02Use stderr instead of /dev/tty, realloc buffer if lines too longJune McEnroe For some reason I haven't been able to figure out, trying to poll /dev/tty returns POLLNVAL (and this was using 100% CPU looping), but using stderr instead works fine. 2022-06-02Add initial working version of qfJune McEnroe 2022-05-29Set prompt for okshJune McEnroe