2018-04-25 14:29:11 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-04-27 11:59:32 +02:00
|
|
|
#include <unistd.h>
|
2018-06-10 16:50:06 +02:00
|
|
|
#include <stdarg.h>
|
2018-04-27 11:59:32 +02:00
|
|
|
#include <errno.h>
|
2018-04-25 14:29:11 +02:00
|
|
|
|
2018-04-27 11:59:32 +02:00
|
|
|
#include "transliteration.h"
|
|
|
|
#include "iast.h"
|
2018-04-28 14:03:30 +02:00
|
|
|
#include "iast-czech.h"
|
2018-05-18 10:55:23 +02:00
|
|
|
#include "encoder.h"
|
2018-04-28 14:03:30 +02:00
|
|
|
|
2018-06-10 16:50:06 +02:00
|
|
|
#define PROGNAME "iast"
|
2018-06-10 17:09:35 +02:00
|
|
|
#define VERSION "0.1"
|
2018-06-10 16:50:06 +02:00
|
|
|
|
2018-05-13 17:15:26 +02:00
|
|
|
#define FLAG_STDIN 1 << 0
|
|
|
|
#define FLAG_CZECH 1 << 1
|
2018-05-18 10:55:23 +02:00
|
|
|
#define FLAG_ENCODE 1 << 2
|
2018-05-13 17:15:26 +02:00
|
|
|
|
|
|
|
|
2018-06-02 14:22:19 +02:00
|
|
|
static const char *usage_str =
|
2018-06-10 16:50:06 +02:00
|
|
|
PROGNAME ", a helper for Sanskrit transliteration.\n"
|
2018-05-18 15:48:39 +02:00
|
|
|
"\n"
|
|
|
|
"usage:\n"
|
2018-06-10 16:50:06 +02:00
|
|
|
" " PROGNAME " [flags and text arguments in any order]\n"
|
2018-05-18 15:48:39 +02:00
|
|
|
"\n"
|
|
|
|
"options:\n"
|
2018-06-10 17:09:35 +02:00
|
|
|
" -h shows this help and exits\n"
|
|
|
|
" -v shows version number and exits\n"
|
2018-06-10 16:50:06 +02:00
|
|
|
" -c transliterate to Czech language\n"
|
2018-05-18 15:48:39 +02:00
|
|
|
" -e convert symbolic ASCII text to IAST representation\n"
|
|
|
|
" -- read data from the standard input\n"
|
|
|
|
"\n"
|
2018-06-10 16:50:06 +02:00
|
|
|
" By default, `" PROGNAME "` takes all input arguments written in Devanagari\n"
|
2018-05-18 15:48:39 +02:00
|
|
|
" and transliterates them to IAST version.\n"
|
|
|
|
"\n"
|
|
|
|
" When flag `-e` is set up, the program converts purely ASCII-encoded\n"
|
|
|
|
" strings into special characters of IAST alphabet. For example, it\n"
|
|
|
|
" converts `sam.skr.tam` to `saṃskṛtam` or `s,a-stram` to `śāstram`.\n";
|
|
|
|
|
2018-06-10 17:09:35 +02:00
|
|
|
static void print_usage()
|
2018-05-18 15:48:39 +02:00
|
|
|
{
|
|
|
|
fprintf(stdout, "%s\n", usage_str);
|
|
|
|
}
|
|
|
|
|
2018-06-10 17:09:35 +02:00
|
|
|
static void print_version()
|
|
|
|
{
|
|
|
|
fprintf(stdout, PROGNAME " v" VERSION "\n");
|
|
|
|
}
|
|
|
|
|
2018-06-10 16:50:06 +02:00
|
|
|
static void error(const char *msg, ...)
|
|
|
|
{
|
|
|
|
va_list params;
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
va_start(params, msg);
|
|
|
|
vsnprintf(buf, sizeof(buf), msg, params);
|
|
|
|
fprintf(stderr, "[" PROGNAME "] error: %s\n", buf);
|
|
|
|
va_end(params);
|
|
|
|
}
|
|
|
|
|
2018-05-13 17:15:26 +02:00
|
|
|
static char *stdin_read()
|
|
|
|
{
|
|
|
|
char buffer[1024];
|
|
|
|
unsigned int n, length = 0;;
|
|
|
|
char *text = NULL;
|
|
|
|
|
|
|
|
while ((n = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) {
|
2018-06-10 16:50:06 +02:00
|
|
|
|
|
|
|
text = realloc(text, length + n + 1);
|
2018-05-13 17:15:26 +02:00
|
|
|
if (text == NULL)
|
2018-06-10 16:50:06 +02:00
|
|
|
return NULL;
|
|
|
|
|
2018-05-13 17:15:26 +02:00
|
|
|
strncpy(text + length, buffer, n);
|
|
|
|
length += n;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (n == -1)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
2018-04-25 14:29:11 +02:00
|
|
|
|
|
|
|
int main(int argc, const char **argv)
|
|
|
|
{
|
2018-06-10 16:50:06 +02:00
|
|
|
int i;
|
2018-05-13 17:15:26 +02:00
|
|
|
unsigned int flags = 0, n = 0;
|
2018-04-28 14:03:30 +02:00
|
|
|
const char *arg;
|
2018-05-13 17:15:26 +02:00
|
|
|
const char *queue[argc];
|
|
|
|
char *input, *output;
|
2018-05-20 08:29:33 +02:00
|
|
|
const struct transliteration_context *context;
|
2018-04-28 14:03:30 +02:00
|
|
|
|
2018-05-13 17:15:26 +02:00
|
|
|
for (i = 1; i < argc; i++) {
|
2018-04-28 14:03:30 +02:00
|
|
|
arg = argv[i];
|
|
|
|
|
|
|
|
if (*arg == '-') {
|
|
|
|
switch (arg[1]) {
|
2018-05-13 17:15:26 +02:00
|
|
|
case '-':
|
|
|
|
flags |= FLAG_STDIN;
|
|
|
|
continue;
|
2018-04-28 14:03:30 +02:00
|
|
|
case 'c':
|
|
|
|
flags |= FLAG_CZECH;
|
|
|
|
continue;
|
2018-05-18 10:55:23 +02:00
|
|
|
case 'e':
|
|
|
|
flags |= FLAG_ENCODE;
|
|
|
|
continue;
|
2018-05-18 15:48:39 +02:00
|
|
|
case 'h':
|
2018-06-10 17:09:35 +02:00
|
|
|
print_usage();
|
|
|
|
return 0;
|
|
|
|
case 'v':
|
|
|
|
print_version();
|
2018-06-10 16:50:06 +02:00
|
|
|
return 0;
|
2018-04-28 14:03:30 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 16:50:06 +02:00
|
|
|
error("unknown option '%s'.", arg);
|
|
|
|
return -1;
|
2018-05-13 17:15:26 +02:00
|
|
|
} else {
|
|
|
|
queue[n++] = arg;
|
2018-04-28 14:03:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 10:55:23 +02:00
|
|
|
if (flags & FLAG_ENCODE) {
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
output = encode_iast_punctation(queue[i]);
|
|
|
|
fprintf(stdout, "%s\n", output);
|
|
|
|
free(output);
|
|
|
|
}
|
2018-06-10 16:50:06 +02:00
|
|
|
return 0;
|
2018-05-18 10:55:23 +02:00
|
|
|
}
|
|
|
|
|
2018-05-16 14:58:47 +02:00
|
|
|
context = (flags & FLAG_CZECH)
|
2018-05-20 08:29:33 +02:00
|
|
|
? get_iast_czech_transliteration_context()
|
|
|
|
: get_iast_transliteration_context();
|
2018-05-13 17:15:26 +02:00
|
|
|
|
|
|
|
if (flags & FLAG_STDIN) {
|
|
|
|
input = stdin_read();
|
|
|
|
if (input == NULL) {
|
2018-06-10 16:50:06 +02:00
|
|
|
error("failed to read from standard input.");
|
|
|
|
return -1;
|
2018-05-13 17:15:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
output = transliterate_devanagari_to_latin(input, context);
|
|
|
|
fprintf(stdout, "%s\n", output);
|
|
|
|
free(output);
|
|
|
|
free(input);
|
2018-04-28 14:03:30 +02:00
|
|
|
}
|
2018-04-27 11:59:32 +02:00
|
|
|
|
2018-05-13 17:15:26 +02:00
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
output = transliterate_devanagari_to_latin(queue[i], context);
|
|
|
|
fprintf(stdout, "%s\n", output);
|
|
|
|
free(output);
|
|
|
|
}
|
2018-04-27 11:59:32 +02:00
|
|
|
|
2018-06-10 16:50:06 +02:00
|
|
|
return 0;
|
2018-04-25 14:29:11 +02:00
|
|
|
}
|