From 5190796a105cae2324623373e9ef36b398c447cb Mon Sep 17 00:00:00 2001 From: Vlasta Vesely Date: Thu, 30 Dec 2021 19:45:29 +0100 Subject: [PATCH] add the '-o' option --- .gitignore | 1 + Makefile.in | 2 +- main.c | 28 ++++++++++++++++++++-------- tests/integration.c | 18 ++++++++++++++++++ 4 files changed, 40 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 30594a0..859659a 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ /iast /tests/test /tests/coverage +/tests/*.test *.1.gz *.gcno *.gcda diff --git a/Makefile.in b/Makefile.in index 8ec6d05..56e7fc5 100644 --- a/Makefile.in +++ b/Makefile.in @@ -70,7 +70,7 @@ uninstall: clean: $(RM) $(PROGNAME) tests/test $(PROGNAME).1.gz - $(RM) -r *.a *.o */*.o */*.d *.d *.gcda *.gcno tests/coverage + $(RM) -r *.a *.o */*.o */*.d *.d *.gcda *.gcno tests/coverage */*.test clean-aux: $(RM) -r $(AUX_FILES) diff --git a/main.c b/main.c index ab78d10..12c7c84 100644 --- a/main.c +++ b/main.c @@ -41,10 +41,11 @@ static const char *usage_str = "\n" " For more information see the iast(1) manual page.\n"; -static const char *short_opts = "f:readcHhv"; +static const char *short_opts = "f:o:readcHhv"; static const struct option long_opts[] = { {"file", required_argument, 0, 'f'}, + {"output", required_argument, 0, 'o'}, {"reverse", no_argument, 0, 'r'}, {"encode", no_argument, 0, 'e'}, {"velthuis", no_argument, 0, 'e'}, @@ -128,7 +129,7 @@ static int process_input(const char *input, char **out, unsigned int flags) return iast_transliterate(input, out, flags); } -static int process_string(const char *input, unsigned int flags) +static int process_string(int fd, const char *input, unsigned int flags) { char *output = NULL; int ret; @@ -137,7 +138,7 @@ static int process_string(const char *input, unsigned int flags) switch (ret) { case 0: - fprintf(stdout, "%s", output); + write(fd, output, strlen(output)); break; default: error("unexpected error."); @@ -213,11 +214,11 @@ static int read_file(char **out, const char *path) int main(int argc, const char **argv) { - int i, retval, c = 0, opt_index = 0; + int i, retval, c = 0, opt_index = 0, out = STDOUT_FILENO; const char *files[argc]; unsigned int nfiles = 0; unsigned int flags = 0; - char *input; + char *input, *output = NULL; if (argc == 1) { print_usage(); @@ -233,6 +234,9 @@ int main(int argc, const char **argv) case 'f': files[nfiles++] = optarg; break; + case 'o': + output = optarg; + break; case 'r': flags |= FLAG_REVERSE; break; @@ -270,13 +274,21 @@ int main(int argc, const char **argv) if (retval != 0) return retval; + if (output) { + out = open(output, O_CREAT | O_TRUNC | O_WRONLY, 0644); + if (out == -1) { + error("failed to open '%s' [%d].", output, errno); + return errno; + } + } + while (optind < argc) { const char *arg = argv[optind++]; - retval = process_string(arg, flags); + retval = process_string(out, arg, flags); if (retval != 0) return retval; - putchar('\n'); + write(out, "\n", 1); } for (i = 0; i < nfiles; i++) { @@ -290,7 +302,7 @@ int main(int argc, const char **argv) error("failed to read file '%s'.", files[i]); return retval; } - retval = process_string(input, flags); + retval = process_string(out, input, flags); free(input); if (retval != 0) return retval; diff --git a/tests/integration.c b/tests/integration.c index 50c032f..40684e2 100644 --- a/tests/integration.c +++ b/tests/integration.c @@ -122,6 +122,23 @@ START_TEST(test_ascii) } END_TEST +#define TEST_FILE "./tests/output.test" +START_TEST(test_file_output) +{ + char *str; + + unlink(TEST_FILE); + str = exec_command("./iast -eo "TEST_FILE" .rta.m"); + free(str); + + str = read_file(TEST_FILE); + ck_assert_str_eq("ṛtaṃ\n", str); + free(str); + + unlink(TEST_FILE); +} +END_TEST + START_TEST(test_version) { test_output("./iast -v", "iast v2.0.0\n"); @@ -161,6 +178,7 @@ void register_integration_tests(TCase *test_case) tcase_add_test(test_case, test_transcript_hindi); tcase_add_test(test_case, test_velthuis); tcase_add_test(test_case, test_ascii); + tcase_add_test(test_case, test_file_output); tcase_add_test(test_case, test_version); tcase_add_test(test_case, test_usage); tcase_add_test(test_case, test_errors);