aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--jdtalk.h15
-rw-r--r--main.c50
2 files changed, 43 insertions, 22 deletions
diff --git a/jdtalk.h b/jdtalk.h
index 1c79f75..7ad5e4b 100644
--- a/jdtalk.h
+++ b/jdtalk.h
@@ -24,14 +24,15 @@
#define WT_ADVERB 3
#define WT_VERB 4
-#define JSON_BEGIN(FP) fprintf(FP, "{\n");
+#define JSON_BEGIN(FP) fprintf(FP, "{\n")
#define JSON_INDENT(FP, LEVEL) for (size_t indenter = 0; indenter < LEVEL; indenter++) { fprintf(FP, " "); }
-#define JSON_NEXT_ITEM(FP) fprintf(FP, ",\n");
-#define JSON_NEXT_LINE(FP) fprintf(FP, "\n");
-#define JSON_LIST_BEGIN(FP, KEY) JSON_INDENT(FP, 1); fprintf(FP, "\"%s\": [", KEY);
-#define JSON_LIST_APPEND(FP, VALUE) JSON_INDENT(FP, 2); fprintf(FP, "\"%s\"", VALUE);
-#define JSON_LIST_END(FP) fprintf(FP, "]\n");
-#define JSON_END(FP) fprintf(FP, "}\n");
+#define JSON_NEXT_ITEM(FP) fprintf(FP, ",\n")
+#define JSON_NEXT_LINE(FP) fprintf(FP, "\n")
+#define JSON_LIST_BEGIN(FP, KEY) JSON_INDENT(FP, 1); fprintf(FP, "\"%s\": [", KEY)
+#define JSON_LIST_APPEND(FP, VALUE) JSON_INDENT(FP, 2); fprintf(FP, "\"%s\"", VALUE)
+#define JSON_LIST_END(FP) fprintf(FP, "]")
+#define JSON_STRING(FP, KEY, VALUE) JSON_INDENT(FP, 1); fprintf(FP, "\"%s\": \"%s\"", KEY, VALUE)
+#define JSON_END(FP) fprintf(FP, "}\n")
struct Word {
char *word;
diff --git a/main.c b/main.c
index b531479..425cbc0 100644
--- a/main.c
+++ b/main.c
@@ -67,6 +67,7 @@ static const char *args_valid = "AabcefhHjlprRsStx";
int main(int argc, char *argv[]) {
struct Dictionary *dict;
char buf[OUTPUT_SIZE_MAX];
+ char errbuf[OUTPUT_SIZE_MAX];
char format[INPUT_SIZE_MAX];
char pattern[INPUT_SIZE_MAX];
char acronym[INPUT_SIZE_MAX];
@@ -232,36 +233,38 @@ int main(int argc, char *argv[]) {
NULL,
};
+ if (do_json && limit) {
+ JSON_BEGIN(stdout);
+ JSON_LIST_BEGIN(stdout, "data");
+ }
+
if (do_pattern && !dictionary_contains(&dicts[1], pattern, WT_ANY)) {
- fprintf(stderr, "Word not found in dictionary: %s\n", pattern);
- exit(1);
+ sprintf(errbuf, "Word not found in dictionary: %s", pattern);
+ goto error_exit;
}
if (!format_safe(format)) {
- fprintf(stderr, "Invalid format: %s\n", format);
- exit(1);
+ sprintf(errbuf, "Invalid format: %s", format);
+ goto error_exit;
}
if ((do_pattern && do_acronym) && !acronym_safe(dict, acronym, pattern, do_format ? NULL: format)) {
- fprintf(stderr, "Using format: %s\n", format);
- fprintf(stderr, "Word will never appear in acronym, '%s': %s\n", acronym, pattern);
- exit(1);
+ sprintf(errbuf, "Word will never appear in acronym, '%s': %s (format: %s)", acronym, pattern, format);
+ goto error_exit;
}
if ((do_pattern && do_heart) && strlen(pattern) > heart_maxlen) {
- fprintf(stderr, "Word '%s' is too long for heart mode. (%zu > %zu)", pattern, strlen(pattern), heart_maxlen);
- exit(1);
+ sprintf(errbuf, "Word is too long for heart mode: %s (%zu > %zu)", pattern, strlen(pattern), heart_maxlen);
+ goto error_exit;
}
- if (do_benchmark)
- start_time = (float)clock() / CLOCKS_PER_SEC;
-
if (do_json && limit) {
- JSON_BEGIN(stdout);
- JSON_LIST_BEGIN(stdout, "data");
JSON_NEXT_LINE(stdout);
}
+ if (do_benchmark)
+ start_time = (float)clock() / CLOCKS_PER_SEC;
+
for (size_t i = 1; ; i++) {
memset(part, 0, sizeof(part) / sizeof(*part) * sizeof(char *));
@@ -314,7 +317,7 @@ int main(int argc, char *argv[]) {
if (do_json && limit) {
JSON_LIST_APPEND(stdout, buf);
if (i < limit)
- JSON_NEXT_ITEM(stdout)
+ JSON_NEXT_ITEM(stdout);
} else {
puts(buf);
@@ -329,6 +332,9 @@ int main(int argc, char *argv[]) {
JSON_NEXT_LINE(stdout);
JSON_INDENT(stdout, 1);
JSON_LIST_END(stdout);
+ JSON_NEXT_ITEM(stdout);
+ JSON_STRING(stdout, "error", "");
+ JSON_NEXT_LINE(stdout);
JSON_END(stdout);
}
@@ -340,4 +346,18 @@ int main(int argc, char *argv[]) {
dictionary_free(dict);
return 0;
+
+ error_exit:
+ if (do_json && limit) {
+ JSON_NEXT_LINE(stdout);
+ JSON_INDENT(stdout, 1);
+ JSON_LIST_END(stdout);
+ JSON_NEXT_ITEM(stdout);
+ JSON_STRING(stdout, "error", errbuf);
+ JSON_NEXT_LINE(stdout);
+ JSON_END(stdout);
+ } else {
+ fprintf(stderr, "%s\n", errbuf);
+ }
+ exit(1);
}