about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNakidai <nakidai@disroot.org>2025-01-07 23:56:21 +0300
committerNakidai <nakidai@disroot.org>2025-01-08 00:11:47 +0300
commitf1f118eab699b396ecfa21dff9c553bed876e3a8 (patch)
tree85c569bb4714d1d711951a83c32e1dd24e9f217f
downloadtr2cyr-f1f118eab699b396ecfa21dff9c553bed876e3a8.tar.gz
tr2cyr-f1f118eab699b396ecfa21dff9c553bed876e3a8.zip
Add code
-rw-r--r--.gitignore1
-rw-r--r--LICENSE10
-rw-r--r--Makefile16
-rw-r--r--tr2cyr.c125
4 files changed, 152 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..251ecbd
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1 @@
+tr2cyr
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..7ae8865
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,10 @@
+Permission to use, copy, modify, and/or distribute this software for
+any purpose with or without fee is hereby granted.
+
+THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL
+WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
+OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
+FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
+DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
+AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
+OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..607a790
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,16 @@
+DESTDIR ?= /usr/local
+
+all: tr2cyr
+
+README: README.7
+	mandoc -Tascii $< | col -b > $@
+
+install: tr2cyr
+	install -d ${DESTDIR}/bin
+	install -m755 tr2cyr ${DESTDIR}/bin
+
+uninstall:
+	${RM} ${DESTDIR}/bin/tr2cyr
+
+clean:
+	${RM} tr2cyr
diff --git a/tr2cyr.c b/tr2cyr.c
new file mode 100644
index 0000000..48b9b7a
--- /dev/null
+++ b/tr2cyr.c
@@ -0,0 +1,125 @@
+#include <errno.h>
+#include <locale.h>
+#include <stdio.h>
+#include <wchar.h>
+
+/*
+ * a = а
+ * b = б
+ * v = в
+ * g = г
+ * d = д
+ * e = е
+ * yo = ё
+ * yz = ж
+ * z = з
+ * i = и
+ * j = й
+ * k = к
+ * l = л
+ * m = м
+ * n = н
+ * o = о
+ * p = п
+ * r = р
+ * s = с
+ * t = т
+ * u = у
+ * f = ф
+ * h = х
+ * c = ц
+ * yc = ч
+ * ys = ш
+ * yg = щ
+ * y" = ъ
+ * yi = ы
+ * y' = ь
+ * ye = э
+ * yu = ю
+ * ya = я
+ */
+
+typedef int Translator_Writer(wchar_t ch, void *arg);
+
+int Translator_convert(FILE *file, Translator_Writer *writer, void *arg)
+{
+    wint_t ch;
+    while ((ch = getwchar()) != WEOF)
+    {
+        wchar_t towrite;
+#define CASE(x, y) case x: towrite = y; break
+        switch (ch)
+        {
+        CASE(L'a', L'а');
+        CASE(L'b', L'б');
+        CASE(L'v', L'в');
+        CASE(L'g', L'г');
+        CASE(L'd', L'д');
+        CASE(L'e', L'е');
+        CASE(L'z', L'з');
+        CASE(L'i', L'и');
+        CASE(L'j', L'й');
+        CASE(L'k', L'к');
+        CASE(L'l', L'л');
+        CASE(L'm', L'м');
+        CASE(L'n', L'н');
+        CASE(L'o', L'о');
+        CASE(L'p', L'п');
+        CASE(L'r', L'р');
+        CASE(L's', L'с');
+        CASE(L't', L'т');
+        CASE(L'u', L'у');
+        CASE(L'f', L'ф');
+        CASE(L'h', L'х');
+        CASE(L'c', L'ц');
+        case 'y':
+        {
+            if ((ch = getwchar()) == WEOF)
+                return errno ? -1 : 0;
+
+            switch(ch)
+            {
+            CASE(L'o', L'ё');
+            CASE(L'z', L'ж');
+            CASE(L'c', L'ч');
+            CASE(L's', L'ш');
+            CASE(L'g', L'щ');
+            CASE(L'"', L'ъ');
+            CASE(L'i', L'ы');
+            CASE(L'\'', L'ь');
+            CASE(L'e', L'э');
+            CASE(L'u', L'ю');
+            CASE(L'a', L'я');
+            default:
+            {
+                int ret = writer(L'y', arg);
+                if (ret)
+                    return ret;
+
+                towrite = ch;
+            } break;
+            }
+        } break;
+        default:
+            towrite = ch;
+        }
+#undef CASE
+
+        int ret = writer(towrite, arg);
+        if (ret)
+            return ret;
+    }
+    return errno ? -1 : 0;
+}
+
+static int writer(wchar_t ch, void *arg)
+{
+    (void)arg;
+    return putwchar(ch) == WEOF ? -1 : 0;
+}
+
+int main(int argc, char **argv)
+{
+    setlocale(LC_CTYPE, "ru_RU.UTF-8");
+    Translator_convert(stdin, &writer, 0);
+}