+static void
+indent_DYN(DYNBUF * buffer, int level)
+{
+ int n;
+
+ for (n = 0; n < level; n++)
+ strncpy_DYN(buffer, "\t", 1);
+}
+
+static bool
+has_params(const char *src)
+{
+ bool result = FALSE;
+ int len = (int) strlen(src);
+ int n;
+ bool ifthen = FALSE;
+ bool params = FALSE;
+
+ for (n = 0; n < len - 1; ++n) {
+ if (!strncmp(src + n, "%p", 2)) {
+ params = TRUE;
+ } else if (!strncmp(src + n, "%;", 2)) {
+ ifthen = TRUE;
+ result = params;
+ break;
+ }
+ }
+ if (!ifthen) {
+ result = ((len > 50) && params);
+ }
+ return result;
+}
+
+static char *
+fmt_complex(char *src, int level)
+{
+ bool percent = FALSE;
+ bool params = has_params(src);
+
+ while (*src != '\0') {
+ switch (*src) {
+ case '\\':
+ percent = FALSE;
+ strncpy_DYN(&tmpbuf, src++, 1);
+ break;
+ case '%':
+ percent = TRUE;
+ break;
+ case '?': /* "if" */
+ case 't': /* "then" */
+ case 'e': /* "else" */
+ if (percent) {
+ percent = FALSE;
+ tmpbuf.text[tmpbuf.used - 1] = '\n';
+ /* treat a "%e" as else-if, on the same level */
+ if (*src == 'e') {
+ indent_DYN(&tmpbuf, level);
+ strncpy_DYN(&tmpbuf, "%", 1);
+ strncpy_DYN(&tmpbuf, src, 1);
+ src++;
+ params = has_params(src);
+ if (!params && *src != '\0' && *src != '%') {
+ strncpy_DYN(&tmpbuf, "\n", 1);
+ indent_DYN(&tmpbuf, level + 1);
+ }
+ } else {
+ indent_DYN(&tmpbuf, level + 1);
+ strncpy_DYN(&tmpbuf, "%", 1);
+ strncpy_DYN(&tmpbuf, src, 1);
+ if (*src++ == '?') {
+ src = fmt_complex(src, level + 1);
+ if (*src != '\0' && *src != '%') {
+ strncpy_DYN(&tmpbuf, "\n", 1);
+ indent_DYN(&tmpbuf, level + 1);
+ }
+ } else if (level == 1) {
+ _nc_warning("%%%c without %%?", *src);
+ }
+ }
+ continue;
+ }
+ break;
+ case ';': /* "endif" */
+ if (percent) {
+ percent = FALSE;
+ if (level > 1) {
+ tmpbuf.text[tmpbuf.used - 1] = '\n';
+ indent_DYN(&tmpbuf, level);
+ strncpy_DYN(&tmpbuf, "%", 1);
+ strncpy_DYN(&tmpbuf, src++, 1);
+ return src;
+ }
+ _nc_warning("%%; without %%?");
+ }
+ break;
+ case 'p':
+ if (percent && params) {
+ tmpbuf.text[tmpbuf.used - 1] = '\n';
+ indent_DYN(&tmpbuf, level + 1);
+ strncpy_DYN(&tmpbuf, "%", 1);
+ }
+ params = FALSE;
+ percent = FALSE;
+ break;
+ case ' ':
+ strncpy_DYN(&tmpbuf, "\\s", 2);
+ ++src;
+ continue;
+ default:
+ percent = FALSE;
+ break;
+ }
+ strncpy_DYN(&tmpbuf, src++, 1);
+ }
+ return src;
+}
+
+#define SAME_CAP(n,cap) (&tterm->Strings[n] == &cap)
+#define EXTRA_CAP 20
+
+int
+fmt_entry(TERMTYPE *tterm,
+ PredFunc pred,
+ bool content_only,
+ bool suppress_untranslatable,
+ bool infodump,
+ int numbers)