From e1d6b5976cfb69454d96ccb0438624a61406b63c Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 10 Sep 2021 16:49:10 +0200 Subject: Add multicall binary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- .gitignore | 5 +- Makefile | 20 +++++--- common.h | 6 +++ config.mk | 5 +- numtext-strip.c | 121 ++----------------------------------------------- numtext.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 165 insertions(+), 130 deletions(-) create mode 100644 numtext.c diff --git a/.gitignore b/.gitignore index c0404b1..28639af 100644 --- a/.gitignore +++ b/.gitignore @@ -12,7 +12,4 @@ *.gcda *.test *.dylib -/numtext-strip -/num2text -/text2num -/card2ord +/numtext diff --git a/Makefile b/Makefile index 4d963c0..b1a70f6 100644 --- a/Makefile +++ b/Makefile @@ -39,15 +39,12 @@ LIB_LOBJ = $(LIB_OBJ:.o=.lo) TEST_OBJ = $(TEST:=.o) -all: libnumtext.a libnumtext.$(LIBEXT) $(CMD) +all: libnumtext.a libnumtext.$(LIBEXT) numtext $(OBJ): $(HDR) $(LOBJ): $(HDR) $(TEST_OBJ): $(HDR) $(TEST): libnumtext.a -.o: - $(CC) -o $@ $< libnumtext.a $(LDFLAGS) - .c.o: $(CC) -c -o $@ $< $(CFLAGS) $(CPPFLAGS) @@ -57,6 +54,9 @@ $(TEST): libnumtext.a .test.o.test: $(CC) -o $@ $< libnumtext.a $(LDFLAGS) +numtext: numtext.o $(CMD_OBJ) + $(CC) -o $@ $@.o $(CMD_OBJ) libnumtext.a $(LDFLAGS) + libnumtext.a: $(LIB_OBJ) @rm -f -- $@ $(AR) rc $@ $(LIB_OBJ) @@ -71,21 +71,29 @@ check: $(TEST) "./$$t" || exit 1;\ done -install: libnumtext.a libnumtext.$(LIBEXT) +install: libnumtext.a libnumtext.$(LIBEXT) numtext mkdir -p -- "$(DESTDIR)$(PREFIX)/lib" mkdir -p -- "$(DESTDIR)$(PREFIX)/include" + mkdir -p -- "$(DESTDIR)$(PREFIX)/bin" + mkdir -p -- "$(DESTDIR)$(PREFIX)/$(LIBEXEC)" cp -- libnumtext.a "$(DESTDIR)$(PREFIX)/lib/" cp -- libnumtext.$(LIBEXT) "$(DESTDIR)$(PREFIX)/lib/libnumtext.$(LIBMINOREXT)" ln -sf -- libnumtext.$(LIBMINOREXT) "$(DESTDIR)$(PREFIX)/lib/libnumtext.$(LIBMAJOREXT)" ln -sf -- libnumtext.$(LIBMINOREXT) "$(DESTDIR)$(PREFIX)/lib/libnumtext.$(LIBEXT)" cp -- libnumtext.h "$(DESTDIR)$(PREFIX)/include/" + cp -- numtext "$(DESTDIR)$(PREFIX)/$(LIBEXEC)/" + set -e && for cmd in $(CMD); do\ + ln -sf -- "../$(LIBEXEC)/numtext" "$(DESTDIR)$(PREFIX)/bin/$$cmd" || exit 1;\ + done uninstall: -rm -f -- "$(DESTDIR)$(PREFIX)/lib/libnumtext".* -rm -f -- "$(DESTDIR)$(PREFIX)/include/libnumtext.h" + -cd -- "$(DESTDIR)$(PREFIX)/bin" && rm -- $(CMD) + -rm -f -- "$(DESTDIR)$(PREFIX)/$(LIBEXEC)/numtext" clean: - -rm -f -- *.o *.a *.lo *.su *.so *.so.* *.gch *.gcov *.gcno *.gcda *.test *.dylib $(CMD) + -rm -f -- *.o *.a *.lo *.su *.so *.so.* *.gch *.gcov *.gcno *.gcda *.test *.dylib numtext .SUFFIXES: .SUFFIXES: .lo .o .c .test .test.o diff --git a/common.h b/common.h index 1f99622..978103a 100644 --- a/common.h +++ b/common.h @@ -49,5 +49,11 @@ struct common_num2text_params { }; +int run(int argc, char *argv[], ssize_t (*callback)(char *outbuf, size_t outbuf_size, const char *num, size_t num_len)); +int get_language(const char *arg, enum libnumtext_language *langp, int *have_langp); + +int numtext_strip_main(int argc, char *argv[]); + + ssize_t libnumtext_num2text_swedish__(struct common_num2text_params *params, const char *num, size_t num_len, uint32_t flags); ssize_t libnumtext_card2ord_swedish__(char *outbuf, size_t outbuf_size, const char *num, size_t num_len, uint32_t flags); diff --git a/config.mk b/config.mk index 9b16e1d..fe7d664 100644 --- a/config.mk +++ b/config.mk @@ -1,5 +1,6 @@ -PREFIX = /usr -MANPREFIX = $(PREFIX)/share/man +PREFIX = /usr +MANPREFIX = $(PREFIX)/share/man +LIBEXEC = libexec CC = cc diff --git a/numtext-strip.c b/numtext-strip.c index b9b453e..5ebe2ff 100644 --- a/numtext-strip.c +++ b/numtext-strip.c @@ -1,3 +1,5 @@ +/* See LICENSE file for copyright and license details. */ +#define LIBSIMPLY_CONFIG_MULTICALL_BINARY #include "common.h" #include @@ -7,123 +9,6 @@ USAGE("-l language"); static enum libnumtext_language lang; -static int -run(int argc, char *argv[], ssize_t (*callback)(char *, size_t, const char *, size_t)) -{ - char *line = NULL; - size_t size = 0; - ssize_t len; - int ret = 0; - char *outbuf = NULL; - size_t outbuf_size = 0; - size_t num_len; - - if (argc) { - for (; *argv; argv++) { - num_len = strlen(*argv); - len = callback(outbuf, outbuf_size, *argv, num_len); - if (len < 0) { - ret = 1; - continue; - } - if ((size_t)len > outbuf_size) { - outbuf_size = (size_t)len; - outbuf = realloc(outbuf, outbuf_size); - if (!outbuf) { - fprintf(stderr, "%s: realloc %zu: %s\n", argv0, outbuf_size, strerror(errno)); - exit(1); - } - } - len = callback(outbuf, outbuf_size, *argv, num_len); - if (len < 0) { - ret = 1; - continue; - } - printf("%s\n", outbuf); - } - } else { - for (;;) { - len = getline(&line, &size, stdin); - if (len == -1) - break; - if (len && line[len - 1] == '\n') - line[--len] = '\0'; - if (!len) - continue; - num_len = (size_t)len; - len = callback(outbuf, outbuf_size, line, num_len); - if (len < 0) { - ret = 1; - continue; - } - if ((size_t)len > outbuf_size) { - outbuf_size = (size_t)len; - outbuf = realloc(outbuf, outbuf_size); - if (!outbuf) { - fprintf(stderr, "%s: realloc %zu: %s\n", argv0, outbuf_size, strerror(errno)); - exit(1); - } - } - len = callback(outbuf, outbuf_size, line, num_len); - if (len < 0) { - ret = 1; - continue; - } - printf("%s\n", outbuf); - } - if (ferror(stdin)) { - fprintf(stderr, "%s: getline : %s\n", argv0, strerror(errno)); - ret = 1; - } - free(line); - } - - if (fflush(stdout) || fclose(stdout)) { - fprintf(stderr, "%s: printf: %s\n", argv0, strerror(errno)); - ret = 1; - } - - free(outbuf); - return ret; -} - - -static int -get_language(const char *arg, enum libnumtext_language *langp, int *have_langp) -{ - static const struct language { - enum libnumtext_language value; - const char *code; - const char *name; - } languages[] = { - {LIBNUMTEXT_SWEDISH, "sv", "swedish"} - }; - - size_t i; - - if (*have_langp) - return 0; - *have_langp = 1; - - if (!strcmp(arg, "?")) { - for (i = 0; i < sizeof(languages) / sizeof(*languages); i++) { - printf("Languages:\n"); - printf("\t%s %s\n", languages[i].code, languages[i].name); - } - exit(0); - } else { - for (i = 0; i < sizeof(languages) / sizeof(*languages); i++) { - if (!strcasecmp(arg, languages[i].code) || !strcasecmp(arg, languages[i].name)) { - *langp = languages[i].value; - return 1; - } - } - fprintf(stderr, "%s: unrecognised language, use ? to list available languages: %s\n", argv0, arg); - exit(1); - } -} - - static ssize_t process(char *outbuf, size_t outbuf_size, const char *num, size_t num_len) { @@ -141,7 +26,7 @@ process(char *outbuf, size_t outbuf_size, const char *num, size_t num_len) int -main(int argc, char *argv[]) +numtext_strip_main(int argc, char *argv[]) { int have_lang = 0; diff --git a/numtext.c b/numtext.c new file mode 100644 index 0000000..cd07e63 --- /dev/null +++ b/numtext.c @@ -0,0 +1,138 @@ +/* See LICENSE file for copyright and license details. */ +#include "common.h" + + +char *argv0; + + +int +main(int argc, char *argv[]) +{ + if (!argc) { + fprintf(stderr, "numtext: no command lines arguments specified, don't know what to execute\n"); + return 1; + } + + if (!strcmp(argv[0], "numtext-strip")) + return numtext_strip_main(argc, argv); + + fprintf(stderr, "%s: not a recognised command for numtext multicall binary\n", argv[0]); + return 1; +} + + +int +run(int argc, char *argv[], ssize_t (*callback)(char *outbuf, size_t outbuf_size, const char *num, size_t num_len)) +{ + char *line = NULL; + size_t size = 0; + ssize_t len; + int ret = 0; + char *outbuf = NULL; + size_t outbuf_size = 0; + size_t num_len; + + if (argc) { + for (; *argv; argv++) { + num_len = strlen(*argv); + len = callback(outbuf, outbuf_size, *argv, num_len); + if (len < 0) { + ret = 1; + continue; + } + if ((size_t)len > outbuf_size) { + outbuf_size = (size_t)len; + outbuf = realloc(outbuf, outbuf_size); + if (!outbuf) { + fprintf(stderr, "%s: realloc %zu: %s\n", argv0, outbuf_size, strerror(errno)); + exit(1); + } + } + len = callback(outbuf, outbuf_size, *argv, num_len); + if (len < 0) { + ret = 1; + continue; + } + printf("%s\n", outbuf); + } + } else { + for (;;) { + len = getline(&line, &size, stdin); + if (len == -1) + break; + if (len && line[len - 1] == '\n') + line[--len] = '\0'; + if (!len) + continue; + num_len = (size_t)len; + len = callback(outbuf, outbuf_size, line, num_len); + if (len < 0) { + ret = 1; + continue; + } + if ((size_t)len > outbuf_size) { + outbuf_size = (size_t)len; + outbuf = realloc(outbuf, outbuf_size); + if (!outbuf) { + fprintf(stderr, "%s: realloc %zu: %s\n", argv0, outbuf_size, strerror(errno)); + exit(1); + } + } + len = callback(outbuf, outbuf_size, line, num_len); + if (len < 0) { + ret = 1; + continue; + } + printf("%s\n", outbuf); + } + if (ferror(stdin)) { + fprintf(stderr, "%s: getline : %s\n", argv0, strerror(errno)); + ret = 1; + } + free(line); + } + + if (fflush(stdout) || fclose(stdout)) { + fprintf(stderr, "%s: printf: %s\n", argv0, strerror(errno)); + ret = 1; + } + + free(outbuf); + return ret; +} + + +int +get_language(const char *arg, enum libnumtext_language *langp, int *have_langp) +{ + static const struct language { + enum libnumtext_language value; + const char *code; + const char *name; + } languages[] = { + {LIBNUMTEXT_SWEDISH, "sv", "swedish"} + }; + + size_t i; + + if (*have_langp) + return 0; + *have_langp = 1; + + if (!strcmp(arg, "?")) { + for (i = 0; i < sizeof(languages) / sizeof(*languages); i++) { + printf("Languages:\n"); + printf("\t%s %s\n", languages[i].code, languages[i].name); + } + exit(0); + } else { + for (i = 0; i < sizeof(languages) / sizeof(*languages); i++) { + if (!strcasecmp(arg, languages[i].code) || !strcasecmp(arg, languages[i].name)) { + *langp = languages[i].value; + return 1; + } + } + fprintf(stderr, "%s: unrecognised language, use ? to list available languages: %s\n", argv0, arg); + exit(1); + } +} -- cgit v1.2.3-70-g09d2