summaryrefslogtreecommitdiff
path: root/hyp.c
diff options
context:
space:
mode:
authorkaa <kaa@disroot.org>2023-11-09 22:57:12 -0800
committerkaa <kaa@disroot.org>2023-11-09 22:57:12 -0800
commit3ff2e62722ff5a51a32961d97dc028c56e00481f (patch)
tree4a5ccc63c4780be400ef024c7e973365bf51352e /hyp.c
parent8987d13d3c1a62d2d1db6ecabb29bc76f3529a44 (diff)
A good copy of libhyphen is included in-tree. Makefile has been modified so as to allow easy compilation. The build path is now a compiled-in constant, allowing for the hyphenation patterns in-tree to be referenced from any directory. Hyphenation language may be specified at runtime using -l.
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;
}