Implement JSONTRANSLATE -zj command.

This commit is contained in:
Dianne Skoll
2024-12-10 11:48:01 -05:00
parent 1c81d4cae3
commit 0bbbaaf3d9
3 changed files with 37 additions and 10 deletions

View File

@@ -114,6 +114,7 @@ int DoRun (ParsePtr p);
int DoExpr (ParsePtr p); int DoExpr (ParsePtr p);
int DoTranslate (ParsePtr p); int DoTranslate (ParsePtr p);
int InsertTranslation(char const *orig, char const *translated); int InsertTranslation(char const *orig, char const *translated);
void DumpTranslationTable(FILE *fp, int json);
int DoErrMsg (ParsePtr p); int DoErrMsg (ParsePtr p);
int ClearGlobalOmits (void); int ClearGlobalOmits (void);
int DoClear (ParsePtr p); int DoClear (ParsePtr p);

View File

@@ -917,6 +917,15 @@ static void ServerWait(struct timeval *sleep_tv)
printf("NOTE ENDJSONQUEUE\n"); printf("NOTE ENDJSONQUEUE\n");
} }
fflush(stdout); 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")) { } else if (!strcmp(cmdLine, "REREAD\n")) {
if (DaemonJSON) { if (DaemonJSON) {
printf("{\"response\":\"reread\",\"command\":\"REREAD\"}\n"); printf("{\"response\":\"reread\",\"command\":\"REREAD\"}\n");

View File

@@ -131,21 +131,38 @@ print_escaped_string(FILE *fp, char const *s)
/* DumpTranslationTable - Dump the table to a file descriptor */ /* DumpTranslationTable - Dump the table to a file descriptor */
/* */ /* */
/***************************************************************/ /***************************************************************/
static void void
DumpTranslationTable(FILE *fp) DumpTranslationTable(FILE *fp, int json)
{ {
XlateItem *item; XlateItem *item;
int done = 0;
fprintf(fp, "# Translation table\n"); if (!json) {
fprintf(fp, "# Translation table\n");
} else {
fprintf(fp, "{");
}
item = hash_table_next(&TranslationTable, NULL); item = hash_table_next(&TranslationTable, NULL);
while(item) { while(item) {
fprintf(fp, "TRANSLATE "); if (!json) {
print_escaped_string(fp, item->orig); fprintf(fp, "TRANSLATE ");
fprintf(fp, " "); print_escaped_string(fp, item->orig);
print_escaped_string(fp, item->translated); fprintf(fp, " ");
fprintf(fp, "\n"); 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); item = hash_table_next(&TranslationTable, item);
} }
if (json) {
fprintf(fp, "}\n");
}
} }
static unsigned int static unsigned int
@@ -241,7 +258,7 @@ DoTranslate(ParsePtr p)
if (!StrCmpi(DBufValue(&orig), "dump")) { if (!StrCmpi(DBufValue(&orig), "dump")) {
DBufFree(&orig); DBufFree(&orig);
if (r) return r; if (r) return r;
DumpTranslationTable(stdout); DumpTranslationTable(stdout, 0);
return OK; return OK;
} }
if (!StrCmpi(DBufValue(&orig), "clear")) { if (!StrCmpi(DBufValue(&orig), "clear")) {