use getopt for parsing the console options

This commit is contained in:
Vlasta Vesely 2021-03-11 20:23:07 +01:00
parent dcee865a74
commit bf0ae9d0f0
3 changed files with 73 additions and 52 deletions

View file

@ -7,6 +7,7 @@
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdarg.h> #include <stdarg.h>
#include <getopt.h>
#include <errno.h> #include <errno.h>
#endif /* __COMPAT_H */ #endif /* __COMPAT_H */

122
main.c
View file

@ -5,29 +5,47 @@
#include "config.h" #include "config.h"
#define FLAG_REVERSE 1 << 0 #define FLAG_REVERSE 1 << 0
#define FLAG_ENCODE 1 << 1 #define FLAG_VELTHUIS 1 << 1
#define FLAG_CZECH 1 << 2 #define FLAG_CZECH 1 << 2
static const char *usage_str = static const char *usage_str =
PROGNAME ", a helper for Sanskrit transliteration.\n" PROGNAME ", a helper for Sanskrit transliteration.\n"
"\n" "\n"
"usage:\n" "Usage:\n"
" " PROGNAME " [flags and text arguments in any order]\n" " " PROGNAME " [flags and text arguments in any order]\n"
"\n" "\n"
"options:\n" "Options:\n"
" -h shows this help and exits\n" " -f, --file the input file for transliteration\n"
" -v shows version number and exits\n" " -r, --reverse reverse transliteration (from Latin to Devanagari)\n"
" -f specifies file for conversion (- means standard input)\n" " -e, --encode convert an ASCII text to IAST using the Velthuis scheme\n"
" -r reverse transliteration (from Latin to Devanagari)\n" " -c, --czech transcript Devanagari to Czech language (experimental)\n"
" -e convert Velthuis scheme text to IAST representation\n" " -h, --help show this help and exit\n"
" -c transcript Devanagari to Czech language\n" " -v, --version show version number and exit\n"
"\n" "\n"
" By default, " PROGNAME " takes all input arguments written in Devanagari\n" "Information:\n"
" and transliterates them to the IAST version.\n" " By default, the program takes all non-option text arguments written in\n"
" Devanagari and transliterates them into the IAST version. When the FILE\n"
" is set to '-', the standard input shall be read for input. The input data\n"
" are expected to be a valid Unicode string.\n"
"\n" "\n"
" When the flag -e is set up, the program converts purely ASCII-encoded\n" " Since the program outputs Unicode characters, you need to ensure that your\n"
" strings into the special characters of the IAST alphabet. For example, it\n" " terminal emulator is able to display the characters correctly if you are\n"
" converts sam.skr.tam to saṃskṛtam or s,a-stram to śāstram.\n"; " printing those into the console instead of a file.\n"
"\n"
" For more information see the iast(1) manual page.\n";
static const char *short_opts = "f:rechv";
static const struct option long_opts[] = {
{"file", required_argument, 0, 'f'},
{"reverse", no_argument, 0, 'r'},
{"encode", no_argument, 0, 'e'},
{"velthuis", no_argument, 0, 'e'},
{"czech", no_argument, 0, 'c'},
{"help", no_argument, 0, 'h'},
{"version", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
static void print_usage() static void print_usage()
{ {
@ -52,12 +70,12 @@ static void error(const char *msg, ...)
static char *process_input(const char *input, unsigned int flags) static char *process_input(const char *input, unsigned int flags)
{ {
if (flags & FLAG_ENCODE)
return encode_velthuis_to_iast_punctation(input);
if (flags & FLAG_REVERSE) if (flags & FLAG_REVERSE)
return transliterate_latin_to_devanagari(input); return transliterate_latin_to_devanagari(input);
if (flags & FLAG_VELTHUIS)
return encode_velthuis_to_iast_punctation(input);
if (flags & FLAG_CZECH) if (flags & FLAG_CZECH)
return transcript_devanagari_to_czech(input); return transcript_devanagari_to_czech(input);
@ -113,10 +131,9 @@ 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; int i, retval, c = 0, opt_index = 0;
const char *arg; const char *files[argc];
const char *files[argc], *texts[argc]; unsigned int nfiles = 0;
unsigned int nfiles = 0, ntexts = 0;
unsigned int flags = 0; unsigned int flags = 0;
char *input, *output; char *input, *output;
@ -125,40 +142,43 @@ int main(int argc, const char **argv)
return -1; return -1;
} }
for (i = 1; i < argc; i++) { opterr = 0; /* disable the auto error message */
arg = argv[i]; while (c != -1) {
c = getopt_long(argc, (char * const *) argv, short_opts,
long_opts, &opt_index);
if (*arg == '-') { switch (c) {
switch (arg[1]) { case 'f':
case 'r': files[nfiles++] = optarg;
flags |= FLAG_REVERSE; break;
continue; case 'r':
case 'e': flags |= FLAG_REVERSE;
flags |= FLAG_ENCODE; break;
continue; case 'e':
case 'c': flags |= FLAG_VELTHUIS;
flags |= FLAG_CZECH; break;
continue; case 'c':
case 'f': flags |= FLAG_CZECH;
files[nfiles++] = argv[++i]; break;
continue; case 'h':
case 'h': print_usage();
print_usage(); return 0;
return 0; case 'v':
case 'v': print_version();
print_version(); return 0;
return 0; case '?':
} error("unrecognised option '-%s'.", optopt ?
(char *) &(optopt) : argv[optind - 1] + 1);
error("unknown option '%s'.", arg); break;
return -1; default:
} else { break;
texts[ntexts++] = arg;
} }
} }
for (i = 0; i < ntexts; i++) { while (optind < argc) {
output = process_input(texts[i], flags); const char *arg = argv[optind++];
output = process_input(arg, flags);
fprintf(stdout, "%s\n", output); fprintf(stdout, "%s\n", output);
free(output); free(output);
} }

View file

@ -5,7 +5,7 @@ usage=$(./iast -h)
test -n $(echo "$usage" | grep "iast [flags and text arguments in any order]") test -n $(echo "$usage" | grep "iast [flags and text arguments in any order]")
version=$(./iast -v) version=$(./iast -v)
test "$version" = "iast v2.0" test "$version" = "iast v2.0.0"
./iast -f tests/texts/bhagavadgita-1.txt | ./iast -f tests/texts/bhagavadgita-1.txt |
./iast -r -f - >/tmp/iast-bhagavadgita-1.txt.out ./iast -r -f - >/tmp/iast-bhagavadgita-1.txt.out