]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - progs/dump_entry.c
ncurses 5.5
[ncurses.git] / progs / dump_entry.c
index 48ef488165b610def2ee07be1f819687b20614a8..6187d5b13084e76c8364d1dbba14c171149d569d 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2002,2003 Free Software Foundation, Inc.              *
+ * Copyright (c) 1998-2004,2005 Free Software Foundation, Inc.              *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -39,7 +39,7 @@
 #include "termsort.c"          /* this C file is generated */
 #include <parametrized.h>      /* so is this */
 
-MODULE_ID("$Id: dump_entry.c,v 1.66 2003/05/24 22:43:59 tom Exp $")
+MODULE_ID("$Id: dump_entry.c,v 1.70 2005/07/23 20:03:30 tom Exp $")
 
 #define INDENT                 8
 #define DISCARD(string) string = ABSENT_STRING
@@ -59,11 +59,14 @@ static int column;          /* current column, limited by 'width' */
 static int oldcol;             /* last value of column before wrap */
 static bool pretty;            /* true if we format if-then-else strings */
 
+static char *save_sgr;
+static char *save_acsc;
+
 static DYNBUF outbuf;
 static DYNBUF tmpbuf;
 
 /* indirection pointers for implementing sort and display modes */
-static const int *bool_indirect, *num_indirect, *str_indirect;
+static const PredIdx *bool_indirect, *num_indirect, *str_indirect;
 static NCURSES_CONST char *const *bool_names;
 static NCURSES_CONST char *const *num_names;
 static NCURSES_CONST char *const *str_names;
@@ -262,7 +265,7 @@ dump_init(const char *version, int mode, int sort, int twidth, int traceval,
 static TERMTYPE *cur_type;
 
 static int
-dump_predicate(int type, int idx)
+dump_predicate(PredType type, PredIdx idx)
 /* predicate function to use for ordinary decompilation */
 {
     switch (type) {
@@ -282,7 +285,7 @@ dump_predicate(int type, int idx)
     return (FALSE);            /* pacify compiler */
 }
 
-static void set_obsolete_termcaps(TERMTYPE * tp);
+static void set_obsolete_termcaps(TERMTYPE *tp);
 
 /* is this the index of a function key string? */
 #define FNKEY(i)       (((i)<= 65 && (i)>= 75) || ((i)<= 216 && (i)>= 268))
@@ -296,7 +299,7 @@ static void set_obsolete_termcaps(TERMTYPE * tp);
 #define STR_IDX(name)  (&(name) - &(CUR Strings[0]))
 
 static bool
-version_filter(int type, int idx)
+version_filter(PredType type, PredIdx idx)
 /* filter out capabilities we may want to suppress */
 {
     switch (tversion) {
@@ -498,21 +501,24 @@ fmt_complex(char *src, int level)
     return src;
 }
 
+#define SAME_CAP(n,cap) (&tterm->Strings[n] == &cap)
+
 int
-fmt_entry(TERMTYPE * tterm,
-         int (*pred) (int type, int idx),
+fmt_entry(TERMTYPE *tterm,
+         PredFunc pred,
          bool content_only,
          bool suppress_untranslatable,
          bool infodump,
          int numbers)
 {
-    int i, j;
+    PredIdx i, j;
     char buffer[MAX_TERMINFO_LENGTH];
+    char *capability;
     NCURSES_CONST char *name;
     int predval, len;
-    int num_bools = 0;
-    int num_values = 0;
-    int num_strings = 0;
+    PredIdx num_bools = 0;
+    PredIdx num_values = 0;
+    PredIdx num_strings = 0;
     bool outcount = 0;
 
 #define WRAP_CONCAT    \
@@ -607,51 +613,79 @@ fmt_entry(TERMTYPE * tterm,
     for_each_string(j, tterm) {
        i = StrIndirect(j);
        name = ExtStrname(tterm, i, str_names);
+       capability = tterm->Strings[i];
 
        if (!version_filter(STRING, i))
            continue;
        else if (isObsolete(outform, name))
            continue;
 
+#if NCURSES_XNAMES
        /*
-        * Some older versions of vi want rmir/smir to be defined
-        * for ich/ich1 to work.  If they're not defined, force
-        * them to be output as defined and empty.
+        * Extended names can be longer than 2 characters, but termcap programs
+        * cannot read those (filter them out).
         */
+       if (outform == F_TERMCAP && (strlen(name) > 2))
+           continue;
+#endif
+
        if (outform == F_TERMCAP) {
-           if (insert_character || parm_ich) {
-               if (&tterm->Strings[i] == &enter_insert_mode
+           /*
+            * Some older versions of vi want rmir/smir to be defined
+            * for ich/ich1 to work.  If they're not defined, force
+            * them to be output as defined and empty.
+            */
+           if (PRESENT(insert_character) || PRESENT(parm_ich)) {
+               if (SAME_CAP(i, enter_insert_mode)
                    && enter_insert_mode == ABSENT_STRING) {
                    (void) strcpy(buffer, "im=");
                    WRAP_CONCAT;
                    continue;
                }
 
-               if (&tterm->Strings[i] == &exit_insert_mode
+               if (SAME_CAP(i, exit_insert_mode)
                    && exit_insert_mode == ABSENT_STRING) {
                    (void) strcpy(buffer, "ei=");
                    WRAP_CONCAT;
                    continue;
                }
            }
+           /*
+            * termcap applications such as screen will be confused if sgr0
+            * is translated to a string containing rmacs.  Filter that out.
+            */
+           if (PRESENT(exit_attribute_mode)) {
+               if (SAME_CAP(i, exit_attribute_mode)) {
+                   char *trimmed_sgr0;
+                   char *my_sgr = set_attributes;
+
+                   set_attributes = save_sgr;
+
+                   trimmed_sgr0 = _nc_trim_sgr0(tterm);
+                   if (strcmp(capability, trimmed_sgr0))
+                       capability = trimmed_sgr0;
+
+                   set_attributes = my_sgr;
+               }
+           }
        }
 
        predval = pred(STRING, i);
        buffer[0] = '\0';
 
        if (predval != FAIL) {
-           if (tterm->Strings[i] != ABSENT_STRING
+           if (capability != ABSENT_STRING
                && i + 1 > num_strings)
                num_strings = i + 1;
 
-           if (!VALID_STRING(tterm->Strings[i])) {
+           if (!VALID_STRING(capability)) {
                sprintf(buffer, "%s@", name);
                WRAP_CONCAT;
            } else if (outform == F_TERMCAP || outform == F_TCONVERR) {
                int params = ((i < (int) SIZEOF(parametrized))
                              ? parametrized[i]
                              : 0);
-               char *srccap = _nc_tic_expand(tterm->Strings[i], TRUE, numbers);
+               char *srccap = _nc_tic_expand(capability, TRUE, numbers);
                char *cv = _nc_infotocap(name, srccap, params);
 
                if (cv == 0) {
@@ -677,10 +711,10 @@ fmt_entry(TERMTYPE * tterm,
                } else {
                    sprintf(buffer, "%s=%s", name, cv);
                }
-               len += strlen(tterm->Strings[i]) + 1;
+               len += strlen(capability) + 1;
                WRAP_CONCAT;
            } else {
-               char *src = _nc_tic_expand(tterm->Strings[i],
+               char *src = _nc_tic_expand(capability,
                                           outform == F_TERMINFO, numbers);
 
                strcpy_DYN(&tmpbuf, 0);
@@ -693,11 +727,14 @@ fmt_entry(TERMTYPE * tterm,
                } else {
                    strcpy_DYN(&tmpbuf, src);
                }
-               len += strlen(tterm->Strings[i]) + 1;
+               len += strlen(capability) + 1;
                wrap_concat(tmpbuf.text);
                outcount = TRUE;
            }
        }
+       /* e.g., trimmed_sgr0 */
+       if (capability != tterm->Strings[i])
+           free(capability);
     }
     len += num_strings * 2;
 
@@ -786,7 +823,7 @@ fmt_entry(TERMTYPE * tterm,
 }
 
 static bool
-kill_string(TERMTYPE * tterm, char *cap)
+kill_string(TERMTYPE *tterm, char *cap)
 {
     int n;
     for (n = 0; n < NUM_STRINGS(tterm); ++n) {
@@ -799,9 +836,9 @@ kill_string(TERMTYPE * tterm, char *cap)
 }
 
 static char *
-find_string(TERMTYPE * tterm, char *name)
+find_string(TERMTYPE *tterm, char *name)
 {
-    int n;
+    PredIdx n;
     for (n = 0; n < NUM_STRINGS(tterm); ++n) {
        if (version_filter(STRING, n)
            && !strcmp(name, strnames[n])) {
@@ -820,7 +857,7 @@ find_string(TERMTYPE * tterm, char *name)
  * make it smaller.
  */
 static int
-kill_labels(TERMTYPE * tterm, int target)
+kill_labels(TERMTYPE *tterm, int target)
 {
     int n;
     int result = 0;
@@ -845,7 +882,7 @@ kill_labels(TERMTYPE * tterm, int target)
  * make it smaller.
  */
 static int
-kill_fkeys(TERMTYPE * tterm, int target)
+kill_fkeys(TERMTYPE *tterm, int target)
 {
     int n;
     int result = 0;
@@ -874,12 +911,12 @@ kill_fkeys(TERMTYPE * tterm, int target)
 #define SHOW_WHY if (!already_used) PRINTF
 
 int
-dump_entry(TERMTYPE * tterm,
+dump_entry(TERMTYPE *tterm,
           bool suppress_untranslatable,
           bool limited,
           int already_used,
           int numbers,
-          int (*pred) (int type, int idx))
+          PredFunc pred)
 /* dump a single entry */
 {
     int len, critlen;
@@ -898,6 +935,9 @@ dump_entry(TERMTYPE * tterm,
     }
     critlen -= already_used;
 
+    save_sgr = set_attributes;
+    save_acsc = acs_chars;
+
     if (((len = FMT_ENTRY()) > critlen)
        && limited) {
        if (!suppress_untranslatable) {
@@ -911,10 +951,31 @@ dump_entry(TERMTYPE * tterm,
             * is really just an optimization hack.  Another good candidate is
             * acsc since it is both long and unused by BSD termcap.
             */
-           char *oldsgr = set_attributes;
-           char *oldacsc = acs_chars;
            bool changed = FALSE;
 
+#if NCURSES_XNAMES
+           /*
+            * Extended names are most likely function-key definitions.  Drop
+            * those first.
+            */
+           int n;
+           for (n = STRCOUNT; n < NUM_STRINGS(tterm); n++) {
+               char *name = ExtStrname(tterm, n, strnames);
+
+               if (VALID_STRING(tterm->Strings[n])) {
+                   set_attributes = ABSENT_STRING;
+                   /* we remove long names anyway - only report the short */
+                   if (strlen(name) <= 2) {
+                       SHOW_WHY("# (%s removed to fit entry within %d bytes)\n",
+                                name,
+                                critlen);
+                   }
+                   changed = TRUE;
+                   if ((len = FMT_ENTRY()) <= critlen)
+                       break;
+               }
+           }
+#endif
            if (VALID_STRING(set_attributes)) {
                set_attributes = ABSENT_STRING;
                SHOW_WHY("# (sgr removed to fit entry within %d bytes)\n",
@@ -959,8 +1020,8 @@ dump_entry(TERMTYPE * tterm,
                }
                tversion = oldversion;
            }
-           set_attributes = oldsgr;
-           acs_chars = oldacsc;
+           set_attributes = save_sgr;
+           acs_chars = save_acsc;
        }
     }
 
@@ -982,11 +1043,12 @@ dump_uses(const char *name, bool infodump)
 }
 
 void
-compare_entry(void (*hook) (int t, int i, const char *name), TERMTYPE * tp
-             GCC_UNUSED, bool quiet)
+compare_entry(void (*hook) (PredType t, PredIdx i, const char *name),
+             TERMTYPE *tp GCC_UNUSED,
+             bool quiet)
 /* compare two entries */
 {
-    int i, j;
+    PredIdx i, j;
     NCURSES_CONST char *name;
 
     if (!quiet)
@@ -1041,7 +1103,7 @@ compare_entry(void (*hook) (int t, int i, const char *name), TERMTYPE * tp
 #define CUR tp->
 
 static void
-set_obsolete_termcaps(TERMTYPE * tp)
+set_obsolete_termcaps(TERMTYPE *tp)
 {
 #include "capdefaults.c"
 }
@@ -1051,7 +1113,7 @@ set_obsolete_termcaps(TERMTYPE * tp)
  * unique.
  */
 void
-repair_acsc(TERMTYPE * tp)
+repair_acsc(TERMTYPE *tp)
 {
     if (VALID_STRING(acs_chars)) {
        size_t n, m;