From e8a758d1a1b3fd6db757196268ff02375a6a73da Mon Sep 17 00:00:00 2001 From: Nakidai Date: Fri, 10 Jan 2025 04:07:36 +0300 Subject: Make this cooler Now it's a library (installable) UwU --- .gitignore | 3 ++ Makefile | 71 +++++++++++++++++++++++++++++++---- tr2cyr.3 | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tr2cyr.c | 60 +----------------------------- tr2cyr.h | 12 ++++++ tr2cyr_exec.c | 23 ++++++++++++ 6 files changed, 221 insertions(+), 65 deletions(-) create mode 100644 tr2cyr.3 create mode 100644 tr2cyr.h create mode 100644 tr2cyr_exec.c diff --git a/.gitignore b/.gitignore index 251ecbd..076f64d 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,4 @@ tr2cyr +*.o +*.a +*.so diff --git a/Makefile b/Makefile index 55836a3..15ce722 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,74 @@ DESTDIR ?= /usr/local -all: tr2cyr +CFLAGS.tr2cyr += -DEXEC + +OBJS += tr2cyr.o +OBJS.tr2cyr += tr2cyr_exec.o + + +all: tr2cyr tr2cyr.a tr2cyr.so + +tr2cyr tr2cyr.a tr2cyr.so: ${OBJS} +tr2cyr: ${OBJS.tr2cyr} + +tr2cyr.c tr2cyr_exec.c: tr2cyr.h + +.SUFFIXES: .o .a +.o.a: + ${AR} rcs $@ $^ + +.SUFFIXES: .o .so +.o.so: + ${LD} -shared -o $@ ${LDFLAGS} ${LDLIBS} $^ README: README.7 mandoc -Tascii $< | col -b > $@ -install: tr2cyr - install -d ${DESTDIR}/bin ${DESTDIR}/share/man/man1 +clean: + ${RM} tr2cyr tr2cyr.a tr2cyr.so ${OBJS} ${OBJS.tr2cyr} + + +.PHONY: install +install: install_bin install_lib install_man1 install_man3 + +.PHONY: install_bin +install_bin: tr2cyr + install -d ${DESTDIR}/bin install -m755 tr2cyr ${DESTDIR}/bin + +.PHONY: install_lib +install_lib: tr2cyr.a tr2cyr.so + install -d ${DESTDIR}/lib ${DESTDIR}/include + install -m644 tr2cyr.a ${DESTDIR}/lib + install -m755 tr2cyr.so ${DESTDIR}/lib + install -m644 tr2cyr.h ${DESTDIR}/include + +.PHONY: install_man1 +install_man1: + install -d ${DESTDIR}/share/man/man1 install -m644 tr2cyr.1 ${DESTDIR}/share/man/man1 -uninstall: - ${RM} ${DESTDIR}/bin/tr2cyr ${DESTDIR}/share/man/man1/tr2cyr.1 +.PHONY: install_man3 +install_man3: + install -d ${DESTDIR}/share/man/man3 + install -m644 tr2cyr.3 ${DESTDIR}/share/man/man3 -clean: - ${RM} tr2cyr + +.PHONY: uninstall +uninstall: uninstall_bin uninstall_lib uninstall_man1 uninstall_man3 + +.PHONY: uninstall_bin +uninstall_bin: + ${RM} ${DESTDIR}/bin/tr2cyr + +.PHONY: uninstall_lib +uninstall_lib: + ${RM} ${DESTDIR}/lib/tr2cyr.a ${DESTDIR}/lib/tr2cyr.so ${DESTDIR}/include/tr2cyr.h + +.PHONY: uninstall_man1 +uninstall_man1: + ${RM} ${DESTDIR}/share/man/man1/tr2cyr.1 + +.PHONY: uninstall_man3 +uninstall_man3: + ${RM} ${DESTDIR}/share/man/man1/tr2cyr.3 diff --git a/tr2cyr.3 b/tr2cyr.3 new file mode 100644 index 0000000..12d06f4 --- /dev/null +++ b/tr2cyr.3 @@ -0,0 +1,117 @@ +.Dd January 10, 2025 +.Dt TR2CYR 3 +.Os +. +.Sh NAME +.Nm tr2cyr +.Nd Convert transliteration +into cyrillic text +. +.Sh SYNOPSIS +.In tr2cyr.h +. +.Ft int +.Fo tr2cyr +.Fa "tr2cyr_reader *reader" +.Fa "void *readerarg" +.Fa "tr2cyr_writer *writer" +.Fa "void *writerarg" +.Fc +. +.Sh DESCRIPTION +.Fn tr2cyr +calls +.Fa reader +until it returns +.Dv WEOF +calling +.Fa writer +with converted +(or not) +character. +.Fa readerarg +and +.Fa writerarg +are passed to +.Fa reader +and +.Fa writer +accordingly. +. +.Pp +.Fn tr2cyr +converts +according to the table below +.Bl -tag -width Ds +.It Sy a +а +.It Sy b +б +.It Sy v +в +.It Sy g +г +.It Sy d +д +.It Sy e +е +.It Sy yo +ё +.It Sy yz +ж +.It Sy z +з +.It Sy i +и +.It Sy j +й +.It Sy k +к +.It Sy l +л +.It Sy m +м +.It Sy n +н +.It Sy o +о +.It Sy p +п +.It Sy r +р +.It Sy s +с +.It Sy t +т +.It Sy u +у +.It Sy f +ф +.It Sy h +х +.It Sy c +с +.It Sy yc +ч +.It Sy ys +ш +.It Sy yg +щ +.It Sy y" +ъ +.It Sy yi +ы +.It Sy y' +ь +.It Sy ye +э +.It Sy yu +ю +.It Sy ya +я +.El +. +.Sh BUGS +You can tell me +about bugs you encountered +.Lk https://github.com/nakidai/tr2cyr/issues/new here . diff --git a/tr2cyr.c b/tr2cyr.c index fc6cfcb..7682d23 100644 --- a/tr2cyr.c +++ b/tr2cyr.c @@ -1,50 +1,11 @@ #include -#include -#include -#include #include #include -/* - * 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 = я - */ +#include "tr2cyr.h" -typedef int Translator_Writer(wchar_t ch, void *arg); -typedef wint_t Translator_Reader(size_t i, void *arg); -int Translator_convert(Translator_Reader *reader, void *readerarg, Translator_Writer *writer, void *writerarg) +int tr2cyr(tr2cyr_reader *reader, void *readerarg, tr2cyr_writer *writer, void *writerarg) { size_t i; int lowercase; @@ -122,20 +83,3 @@ int Translator_convert(Translator_Reader *reader, void *readerarg, Translator_Wr } return errno ? -1 : 0; } - -static wint_t reader(size_t i, void *arg) -{ - return getwchar(); -} - -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, ""); - Translator_convert(&reader, 0, &writer, 0); -} diff --git a/tr2cyr.h b/tr2cyr.h new file mode 100644 index 0000000..d551a58 --- /dev/null +++ b/tr2cyr.h @@ -0,0 +1,12 @@ +#ifndef __TR2CYR_H__ +#define __TR2CYR_H__ + +#include + + +typedef int tr2cyr_writer(wchar_t ch, void *arg); +typedef wint_t tr2cyr_reader(size_t i, void *arg); + +int tr2cyr(tr2cyr_reader *reader, void *readerarg, tr2cyr_writer *writer, void *writerarg); + +#endif /* __TR2CYR_H__ */ diff --git a/tr2cyr_exec.c b/tr2cyr_exec.c new file mode 100644 index 0000000..762dc5e --- /dev/null +++ b/tr2cyr_exec.c @@ -0,0 +1,23 @@ +#include +#include +#include + +#include "tr2cyr.h" + + +static wint_t reader(size_t i, void *arg) +{ + return getwchar(); +} + +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, ""); + tr2cyr(&reader, 0, &writer, 0); +} -- cgit 1.4.1