From 0bbbaaf3d93ec850963268b8b794661d58c5c8f9 Mon Sep 17 00:00:00 2001 From: Dianne Skoll Date: Tue, 10 Dec 2024 11:48:01 -0500 Subject: [PATCH] Implement JSONTRANSLATE -zj command. --- src/protos.h | 1 + src/queue.c | 9 +++++++++ src/trans.c | 37 +++++++++++++++++++++++++++---------- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/protos.h b/src/protos.h index d9e4feca..73f9c5a0 100644 --- a/src/protos.h +++ b/src/protos.h @@ -114,6 +114,7 @@ int DoRun (ParsePtr p); int DoExpr (ParsePtr p); int DoTranslate (ParsePtr p); int InsertTranslation(char const *orig, char const *translated); +void DumpTranslationTable(FILE *fp, int json); int DoErrMsg (ParsePtr p); int ClearGlobalOmits (void); int DoClear (ParsePtr p); diff --git a/src/queue.c b/src/queue.c index ad452ce5..bc764f35 100644 --- a/src/queue.c +++ b/src/queue.c @@ -917,6 +917,15 @@ static void ServerWait(struct timeval *sleep_tv) printf("NOTE ENDJSONQUEUE\n"); } fflush(stdout); + } else if (!strcmp(cmdLine, "JSONTRANSLATE\n")) { + if (!DaemonJSON) { + printf("NOTE JSONTRANSLATE\n"); + } + DumpTranslationTable(stdout, 1); + if (!DaemonJSON) { + printf("NOTE ENDJSONTRANSLATE\n"); + } + fflush(stdout); } else if (!strcmp(cmdLine, "REREAD\n")) { if (DaemonJSON) { printf("{\"response\":\"reread\",\"command\":\"REREAD\"}\n"); diff --git a/src/trans.c b/src/trans.c index b5ce6f28..be4642b9 100644 --- a/src/trans.c +++ b/src/trans.c @@ -131,21 +131,38 @@ print_escaped_string(FILE *fp, char const *s) /* DumpTranslationTable - Dump the table to a file descriptor */ /* */ /***************************************************************/ -static void -DumpTranslationTable(FILE *fp) +void +DumpTranslationTable(FILE *fp, int json) { XlateItem *item; - - fprintf(fp, "# Translation table\n"); + int done = 0; + if (!json) { + fprintf(fp, "# Translation table\n"); + } else { + fprintf(fp, "{"); + } item = hash_table_next(&TranslationTable, NULL); while(item) { - fprintf(fp, "TRANSLATE "); - print_escaped_string(fp, item->orig); - fprintf(fp, " "); - print_escaped_string(fp, item->translated); - fprintf(fp, "\n"); + if (!json) { + fprintf(fp, "TRANSLATE "); + print_escaped_string(fp, item->orig); + fprintf(fp, " "); + print_escaped_string(fp, item->translated); + fprintf(fp, "\n"); + } else { + if (done) { + fprintf(fp, ","); + } + done=1; + print_escaped_string(fp, item->orig); + fprintf(fp, ":"); + print_escaped_string(fp, item->translated); + } item = hash_table_next(&TranslationTable, item); } + if (json) { + fprintf(fp, "}\n"); + } } static unsigned int @@ -241,7 +258,7 @@ DoTranslate(ParsePtr p) if (!StrCmpi(DBufValue(&orig), "dump")) { DBufFree(&orig); if (r) return r; - DumpTranslationTable(stdout); + DumpTranslationTable(stdout, 0); return OK; } if (!StrCmpi(DBufValue(&orig), "clear")) {