From ed32c8fa1d9c3c938c94d67c5739089c20c5f31f Mon Sep 17 00:00:00 2001 From: Vlasta Vesely Date: Sun, 13 May 2018 17:15:26 +0200 Subject: [PATCH] main: transliterates arguments or contents of stdin --- Makefile | 6 ++-- main.c | 86 +++++++++++++++++++++++++++++++++------------------ tests/test.sh | 2 +- 3 files changed, 60 insertions(+), 34 deletions(-) diff --git a/Makefile b/Makefile index 3394e74..a783350 100644 --- a/Makefile +++ b/Makefile @@ -3,14 +3,14 @@ OBJS = syllable.o utf8.o transliteration.o iast.o iast-czech.o -main: main.o $(OBJS) +iast: main.o $(OBJS) $(CC) $^ -o $@ -test: main +test: iast sh tests/test.sh %.o: %.c $(CC) -MMD -MP -c $< -o $@ clean: - $(RM) main test *.o *.d + $(RM) iast *.o *.d diff --git a/main.c b/main.c index 98d0148..78cfc7a 100644 --- a/main.c +++ b/main.c @@ -8,25 +8,48 @@ #include "iast.h" #include "iast-czech.h" -#define FLAG_CZECH 1 << 0 +#define FLAG_STDIN 1 << 0 +#define FLAG_CZECH 1 << 1 + + +static char *stdin_read() +{ + char buffer[1024]; + unsigned int n, length = 0;; + char *text = NULL; + + while ((n = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) { + if (text == NULL) + text = malloc(n + 1); + else + text = realloc(text, length + n + 1); + strncpy(text + length, buffer, n); + length += n; + } + + if (n == -1) + return NULL; + + return text; +} int main(int argc, const char **argv) { - struct transliteration_context *context; - unsigned int n, length = 0; int i, retval = 0; - char buffer[6]; - char *text = malloc(0); - char *out; + unsigned int flags = 0, n = 0; const char *arg; - unsigned int flags = 0; + const char *queue[argc]; + char *input, *output; + struct transliteration_context *context; - - for (i = 0; i < argc; i++) { + for (i = 1; i < argc; i++) { arg = argv[i]; if (*arg == '-') { switch (arg[1]) { + case '-': + flags |= FLAG_STDIN; + continue; case 'c': flags |= FLAG_CZECH; continue; @@ -34,35 +57,38 @@ int main(int argc, const char **argv) fprintf(stderr, "error: unknown option '%s'\n", arg); exit(1); + } else { + queue[n++] = arg; } } - - while ((n = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) { - text = realloc(text, length + n + 1); - strncpy(text + length, buffer, n); - length += n; - } - if (n == -1) { - retval = -errno; - goto out; - } - - text[length] = '\0'; - - if (flags & FLAG_CZECH) { + if (flags & FLAG_CZECH) context = transliteration_context_iast_czech_alloc(); - } else { + else context = transliteration_context_iast_alloc(); + + if (flags & FLAG_STDIN) { + input = stdin_read(); + if (input == NULL) { + fprintf(stderr, "[iast] failed to read from STDIN.\n"); + retval = -1; + goto out; + } + + output = transliterate_devanagari_to_latin(input, context); + fprintf(stdout, "%s\n", output); + free(output); + free(input); } - out = transliterate_devanagari_to_latin(text, context); - printf("%s\n", out); - - transliteration_context_drop(context); - free(text); - free(out); + for (i = 0; i < n; i++) { + output = transliterate_devanagari_to_latin(queue[i], context); + fprintf(stdout, "%s\n", output); + free(output); + } out: + transliteration_context_drop(context); + return retval; } diff --git a/tests/test.sh b/tests/test.sh index 80ce809..5dd7c76 100644 --- a/tests/test.sh +++ b/tests/test.sh @@ -3,7 +3,7 @@ set -e test_word() { - transliterated=$(echo -n "$1" | ./main $3) + transliterated=$(echo -n "$1" | ./iast $3 --) expected="$2" if test "x$transliterated" = "x$expected"