add the '-o' option

This commit is contained in:
Vlasta Vesely 2021-12-30 19:45:29 +01:00
parent 7960aef783
commit 5190796a10
4 changed files with 40 additions and 9 deletions

1
.gitignore vendored
View file

@ -4,6 +4,7 @@
/iast /iast
/tests/test /tests/test
/tests/coverage /tests/coverage
/tests/*.test
*.1.gz *.1.gz
*.gcno *.gcno
*.gcda *.gcda

View file

@ -70,7 +70,7 @@ uninstall:
clean: clean:
$(RM) $(PROGNAME) tests/test $(PROGNAME).1.gz $(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: clean-aux:
$(RM) -r $(AUX_FILES) $(RM) -r $(AUX_FILES)

28
main.c
View file

@ -41,10 +41,11 @@ static const char *usage_str =
"\n" "\n"
" For more information see the iast(1) manual page.\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[] = { static const struct option long_opts[] = {
{"file", required_argument, 0, 'f'}, {"file", required_argument, 0, 'f'},
{"output", required_argument, 0, 'o'},
{"reverse", no_argument, 0, 'r'}, {"reverse", no_argument, 0, 'r'},
{"encode", no_argument, 0, 'e'}, {"encode", no_argument, 0, 'e'},
{"velthuis", 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); 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; char *output = NULL;
int ret; int ret;
@ -137,7 +138,7 @@ static int process_string(const char *input, unsigned int flags)
switch (ret) { switch (ret) {
case 0: case 0:
fprintf(stdout, "%s", output); write(fd, output, strlen(output));
break; break;
default: default:
error("unexpected error."); error("unexpected error.");
@ -213,11 +214,11 @@ static int read_file(char **out, const char *path)
int main(int argc, const char **argv) 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]; const char *files[argc];
unsigned int nfiles = 0; unsigned int nfiles = 0;
unsigned int flags = 0; unsigned int flags = 0;
char *input; char *input, *output = NULL;
if (argc == 1) { if (argc == 1) {
print_usage(); print_usage();
@ -233,6 +234,9 @@ int main(int argc, const char **argv)
case 'f': case 'f':
files[nfiles++] = optarg; files[nfiles++] = optarg;
break; break;
case 'o':
output = optarg;
break;
case 'r': case 'r':
flags |= FLAG_REVERSE; flags |= FLAG_REVERSE;
break; break;
@ -270,13 +274,21 @@ int main(int argc, const char **argv)
if (retval != 0) if (retval != 0)
return retval; 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) { while (optind < argc) {
const char *arg = argv[optind++]; const char *arg = argv[optind++];
retval = process_string(arg, flags); retval = process_string(out, arg, flags);
if (retval != 0) if (retval != 0)
return retval; return retval;
putchar('\n'); write(out, "\n", 1);
} }
for (i = 0; i < nfiles; i++) { 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]); error("failed to read file '%s'.", files[i]);
return retval; return retval;
} }
retval = process_string(input, flags); retval = process_string(out, input, flags);
free(input); free(input);
if (retval != 0) if (retval != 0)
return retval; return retval;

View file

@ -122,6 +122,23 @@ START_TEST(test_ascii)
} }
END_TEST 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) START_TEST(test_version)
{ {
test_output("./iast -v", "iast v2.0.0\n"); 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_transcript_hindi);
tcase_add_test(test_case, test_velthuis); tcase_add_test(test_case, test_velthuis);
tcase_add_test(test_case, test_ascii); 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_version);
tcase_add_test(test_case, test_usage); tcase_add_test(test_case, test_usage);
tcase_add_test(test_case, test_errors); tcase_add_test(test_case, test_errors);