summaryrefslogtreecommitdiff
path: root/hyp.c
diff options
context:
space:
mode:
Diffstat (limited to 'hyp.c')
-rw-r--r--hyp.c81
1 files changed, 57 insertions, 24 deletions
diff --git a/hyp.c b/hyp.c
index 3285b54..e1c9c7d 100644
--- a/hyp.c
+++ b/hyp.c
@@ -4,7 +4,9 @@
#include <hyphen.h>
#include "config.h"
-extern char *dictfile;
+/* Set in config.h */
+extern const char *en;
+extern const char *es;
/* Read a tag into a character array and return its length. */
int
@@ -183,7 +185,8 @@ hyptag(FILE *in, FILE *out, int skiplen, char *tag, HyphenDict *dict)
fputc(ch, out);
/* A simple way of working around
HTML character codes. Each is 5 ( epsiv )
- or 6 ( hellip ) characters long, plus '&' and ';'. */
+ or 6 ( hellip ) characters long,
+ plus '&' and ';'. */
i = -3;
}
/* Check for closing tag. */
@@ -208,44 +211,73 @@ hyptag(FILE *in, FILE *out, int skiplen, char *tag, HyphenDict *dict)
}
}
+void
+usage()
+{
+ fprintf(stderr, "%s\n", "hyp [-l language] [input] [output]");
+}
+
+void
+fail(char *error)
+{
+ fprintf(stderr, "%s\n", error);
+ usage();
+}
+
/* Hyphenate HTML input via `&shy;'.
hyp [in] [out] */
int
main(int argc, char **argv)
{
- FILE *in;
- if (argc < 2)
- in = stdin;
- else {
- in = fopen(argv[1], "r");
- if (in == NULL) {
- printf("%s %s\n", argv[1], "inaccessible.");
- return 1;
+ FILE *in = stdin;
+ FILE *out = stdout;
+ char *dictpath = calloc(pathbuf, sizeof(char));
+ for (argv++; *argv != NULL; argv++) {
+ if (strcmp(*argv, "-l") == 0) {
+ argv++;
+ if (*argv == NULL) {
+ fail("Incomplete language argument.");
+ return -1;
+ }
+ if (strcmp(*argv, "en") == 0) {
+ sprintf(dictpath, "%s/%s",
+ buildpath, en);
+ } else if (strcmp(*argv, "es") == 0) {
+ sprintf(dictpath, "%s/%s",
+ buildpath, es);
+ } else {
+ fail("Unknown language.");
+ return -1;
+ }
+ } else if (in == stdin) {
+ in = fopen(*argv, "r");
+ } else if (out == stdout) {
+ out = fopen(*argv, "w");
+ } else {
+ fail("Too many arguments.");
+ return -1;
}
}
+ if (in == NULL || out == NULL) {
+ fail("Inaccessible file.");
+ return -1;
+ }
- FILE *out;
- if (argc < 3)
- out = stdout;
- else {
- out = fopen(argv[2], "w");
- if (out == NULL) {
- printf("%s %s\n", argv[2], "inaccessible.");
- return 2;
- }
+ /* Default language. */
+ if (*dictpath == '\0') {
+ sprintf(dictpath, "%s/%s", buildpath, en);
}
if (findbody(in, out) == 0) {
- puts("There is no body.");
+ fputs("There is no body.", stderr);
return 3;
}
- HyphenDict *dict = hnj_hyphen_load(dictfile);
+ HyphenDict *dict = hnj_hyphen_load(dictpath);
if (dict == NULL) {
- puts("Dict not readable.");
+ fputs("Dict not readable.", stderr);
return 4;
}
- dict->utf8 = 1;
int tagamt = 0;
while (taglist[tagamt][0] != '\0')
@@ -257,8 +289,9 @@ main(int argc, char **argv)
while ((ch = fgetc(in)) != EOF) {
fputc(ch, out);
if (ch == '<' && (len = readtag(tag, in, out)) > 0
- && checktag(tag, tagamt, in, out) != -1)
+ && checktag(tag, tagamt, in, out) != -1) {
hyptag(in, out, skiplen, tag, dict);
+ }
}
return 0;
}