main: add error() + fix typos in usage string

This commit is contained in:
Vlasta Vesely 2018-06-10 16:50:06 +02:00
parent 2d6547a435
commit ee39899d5c
2 changed files with 33 additions and 17 deletions

View file

@ -59,6 +59,9 @@ char *encode_iast_punctation(const char *text)
char *buf, *dest; char *buf, *dest;
buf = malloc(strlen(text) << 1); buf = malloc(strlen(text) << 1);
if (buf == NULL)
return NULL;
buf[0] = 0; buf[0] = 0;
dest = buf; dest = buf;

47
main.c
View file

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <stdarg.h>
#include <errno.h> #include <errno.h>
#include "transliteration.h" #include "transliteration.h"
@ -9,24 +10,26 @@
#include "iast-czech.h" #include "iast-czech.h"
#include "encoder.h" #include "encoder.h"
#define PROGNAME "iast"
#define FLAG_STDIN 1 << 0 #define FLAG_STDIN 1 << 0
#define FLAG_CZECH 1 << 1 #define FLAG_CZECH 1 << 1
#define FLAG_ENCODE 1 << 2 #define FLAG_ENCODE 1 << 2
static const char *usage_str = static const char *usage_str =
"iast, a sanskrit transliteration helper.\n" PROGNAME ", a helper for Sanskrit transliteration.\n"
"\n" "\n"
"usage:\n" "usage:\n"
" iast [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\n" " -h shows this help\n"
" -c transliterate to czech language\n" " -c transliterate to Czech language\n"
" -e convert symbolic ASCII text to IAST representation\n" " -e convert symbolic ASCII text to IAST representation\n"
" -- read data from the standard input\n" " -- read data from the standard input\n"
"\n" "\n"
" By default, `iast` takes all input arguments written in Devanagari\n" " By default, `" PROGNAME "` takes all input arguments written in Devanagari\n"
" and transliterates them to IAST version.\n" " and transliterates them to IAST version.\n"
"\n" "\n"
" When flag `-e` is set up, the program converts purely ASCII-encoded\n" " When flag `-e` is set up, the program converts purely ASCII-encoded\n"
@ -38,6 +41,17 @@ static void usage()
fprintf(stdout, "%s\n", usage_str); fprintf(stdout, "%s\n", usage_str);
} }
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);
}
static char *stdin_read() static char *stdin_read()
{ {
char buffer[1024]; char buffer[1024];
@ -45,10 +59,11 @@ static char *stdin_read()
char *text = NULL; char *text = NULL;
while ((n = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) { while ((n = read(STDIN_FILENO, buffer, sizeof(buffer))) > 0) {
text = realloc(text, length + n + 1);
if (text == NULL) if (text == NULL)
text = malloc(n + 1); return NULL;
else
text = realloc(text, length + n + 1);
strncpy(text + length, buffer, n); strncpy(text + length, buffer, n);
length += n; length += n;
} }
@ -61,7 +76,7 @@ static char *stdin_read()
int main(int argc, const char **argv) int main(int argc, const char **argv)
{ {
int i, retval = 0; int i;
unsigned int flags = 0, n = 0; unsigned int flags = 0, n = 0;
const char *arg; const char *arg;
const char *queue[argc]; const char *queue[argc];
@ -84,11 +99,11 @@ int main(int argc, const char **argv)
continue; continue;
case 'h': case 'h':
usage(); usage();
goto out; return 0;
} }
fprintf(stderr, "error: unknown option '%s'\n", arg); error("unknown option '%s'.", arg);
exit(1); return -1;
} else { } else {
queue[n++] = arg; queue[n++] = arg;
} }
@ -100,7 +115,7 @@ int main(int argc, const char **argv)
fprintf(stdout, "%s\n", output); fprintf(stdout, "%s\n", output);
free(output); free(output);
} }
goto out; return 0;
} }
context = (flags & FLAG_CZECH) context = (flags & FLAG_CZECH)
@ -110,9 +125,8 @@ int main(int argc, const char **argv)
if (flags & FLAG_STDIN) { if (flags & FLAG_STDIN) {
input = stdin_read(); input = stdin_read();
if (input == NULL) { if (input == NULL) {
fprintf(stderr, "[iast] failed to read from STDIN.\n"); error("failed to read from standard input.");
retval = -1; return -1;
goto out;
} }
output = transliterate_devanagari_to_latin(input, context); output = transliterate_devanagari_to_latin(input, context);
@ -127,6 +141,5 @@ int main(int argc, const char **argv)
free(output); free(output);
} }
out: return 0;
return retval;
} }