ncurses 5.6 - patch 20070512
[ncurses.git] / ncurses / tinfo / alloc_ttype.c
index 660381a32b5f036100398457ba33678a8f4b8ca1..9f31ed3ffab75b6712727847b9058534df257c84 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
 /****************************************************************************
- * Copyright (c) 1999,2000 Free Software Foundation, Inc.                   *
+ * Copyright (c) 1999-2003,2006 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            *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -27,7 +27,7 @@
  ****************************************************************************/
 
 /****************************************************************************
  ****************************************************************************/
 
 /****************************************************************************
- *  Author: Thomas E. Dickey <dickey@clark.net> 1999                        *
+ *  Author: Thomas E. Dickey <dickey@clark.net> 1999-on                     *
  ****************************************************************************/
 
 /*
  ****************************************************************************/
 
 /*
@@ -43,7 +43,7 @@
 #include <tic.h>
 #include <term_entry.h>
 
 #include <tic.h>
 #include <term_entry.h>
 
-MODULE_ID("$Id: alloc_ttype.c,v 1.8 2000/03/25 17:03:11 tom Exp $")
+MODULE_ID("$Id: alloc_ttype.c,v 1.16 2006/07/08 19:18:38 tom Exp $")
 
 #if NCURSES_XNAMES
 /*
 
 #if NCURSES_XNAMES
 /*
@@ -54,7 +54,7 @@ static int
 merge_names(char **dst, char **a, int na, char **b, int nb)
 {
     int n = 0;
 merge_names(char **dst, char **a, int na, char **b, int nb)
 {
     int n = 0;
-    while (na && nb) {
+    while (na > 0 && nb > 0) {
        int cmp = strcmp(*a, *b);
        if (cmp < 0) {
            dst[n++] = *a++;
        int cmp = strcmp(*a, *b);
        if (cmp < 0) {
            dst[n++] = *a++;
@@ -92,18 +92,20 @@ find_name(char **table, int length, char *name)
 }
 
 static void
 }
 
 static void
-realign_data(TERMTYPE * to, char **ext_Names, int ext_Booleans, int
-    ext_Numbers, int ext_Strings)
+realign_data(TERMTYPE *to, char **ext_Names,
+            int ext_Booleans,
+            int ext_Numbers,
+            int ext_Strings)
 {
     int n, m, base;
     int limit = (to->ext_Booleans + to->ext_Numbers + to->ext_Strings);
 
     if (to->ext_Booleans != ext_Booleans) {
        to->num_Booleans += (ext_Booleans - to->ext_Booleans);
 {
     int n, m, base;
     int limit = (to->ext_Booleans + to->ext_Numbers + to->ext_Strings);
 
     if (to->ext_Booleans != ext_Booleans) {
        to->num_Booleans += (ext_Booleans - to->ext_Booleans);
-       to->Booleans = typeRealloc(char, to->num_Booleans, to->Booleans);
+       to->Booleans = typeRealloc(NCURSES_SBOOL, to->num_Booleans, to->Booleans);
        for (n = to->ext_Booleans - 1,
        for (n = to->ext_Booleans - 1,
-           m = ext_Booleans - 1,
-           base = to->num_Booleans - (m + 1); m >= 0; m--) {
+            m = ext_Booleans - 1,
+            base = to->num_Booleans - (m + 1); m >= 0; m--) {
            if (find_name(to->ext_Names, limit, ext_Names[m])) {
                to->Booleans[base + m] = to->Booleans[base + n--];
            } else {
            if (find_name(to->ext_Names, limit, ext_Names[m])) {
                to->Booleans[base + m] = to->Booleans[base + n--];
            } else {
@@ -116,8 +118,8 @@ realign_data(TERMTYPE * to, char **ext_Names, int ext_Booleans, int
        to->num_Numbers += (ext_Numbers - to->ext_Numbers);
        to->Numbers = typeRealloc(short, to->num_Numbers, to->Numbers);
        for (n = to->ext_Numbers - 1,
        to->num_Numbers += (ext_Numbers - to->ext_Numbers);
        to->Numbers = typeRealloc(short, to->num_Numbers, to->Numbers);
        for (n = to->ext_Numbers - 1,
-           m = ext_Numbers - 1,
-           base = to->num_Numbers - (m + 1); m >= 0; m--) {
+            m = ext_Numbers - 1,
+            base = to->num_Numbers - (m + 1); m >= 0; m--) {
            if (find_name(to->ext_Names, limit, ext_Names[m + ext_Booleans])) {
                to->Numbers[base + m] = to->Numbers[base + n--];
            } else {
            if (find_name(to->ext_Names, limit, ext_Names[m + ext_Booleans])) {
                to->Numbers[base + m] = to->Numbers[base + n--];
            } else {
@@ -130,8 +132,8 @@ realign_data(TERMTYPE * to, char **ext_Names, int ext_Booleans, int
        to->num_Strings += (ext_Strings - to->ext_Strings);
        to->Strings = typeRealloc(char *, to->num_Strings, to->Strings);
        for (n = to->ext_Strings - 1,
        to->num_Strings += (ext_Strings - to->ext_Strings);
        to->Strings = typeRealloc(char *, to->num_Strings, to->Strings);
        for (n = to->ext_Strings - 1,
-           m = ext_Strings - 1,
-           base = to->num_Strings - (m + 1); m >= 0; m--) {
+            m = ext_Strings - 1,
+            base = to->num_Strings - (m + 1); m >= 0; m--) {
            if (find_name(to->ext_Names, limit, ext_Names[m + ext_Booleans + ext_Numbers])) {
                to->Strings[base + m] = to->Strings[base + n--];
            } else {
            if (find_name(to->ext_Names, limit, ext_Names[m + ext_Booleans + ext_Numbers])) {
                to->Strings[base + m] = to->Strings[base + n--];
            } else {
@@ -146,7 +148,7 @@ realign_data(TERMTYPE * to, char **ext_Names, int ext_Booleans, int
  * Returns the first index in ext_Names[] for the given token-type
  */
 static int
  * Returns the first index in ext_Names[] for the given token-type
  */
 static int
-_nc_first_ext_name(TERMTYPE * tp, int token_type)
+_nc_first_ext_name(TERMTYPE *tp, int token_type)
 {
     int first;
 
 {
     int first;
 
@@ -171,7 +173,7 @@ _nc_first_ext_name(TERMTYPE * tp, int token_type)
  * Returns the last index in ext_Names[] for the given token-type
  */
 static int
  * Returns the last index in ext_Names[] for the given token-type
  */
 static int
-_nc_last_ext_name(TERMTYPE * tp, int token_type)
+_nc_last_ext_name(TERMTYPE *tp, int token_type)
 {
     int last;
 
 {
     int last;
 
@@ -194,7 +196,7 @@ _nc_last_ext_name(TERMTYPE * tp, int token_type)
  * Lookup an entry from extended-names, returning -1 if not found
  */
 static int
  * Lookup an entry from extended-names, returning -1 if not found
  */
 static int
-_nc_find_ext_name(TERMTYPE * tp, char *name, int token_type)
+_nc_find_ext_name(TERMTYPE *tp, char *name, int token_type)
 {
     unsigned j;
     unsigned first = _nc_first_ext_name(tp, token_type);
 {
     unsigned j;
     unsigned first = _nc_first_ext_name(tp, token_type);
@@ -213,7 +215,7 @@ _nc_find_ext_name(TERMTYPE * tp, char *name, int token_type)
  * (e.g., Booleans[]).
  */
 static int
  * (e.g., Booleans[]).
  */
 static int
-_nc_ext_data_index(TERMTYPE * tp, int n, int token_type)
+_nc_ext_data_index(TERMTYPE *tp, int n, int token_type)
 {
     switch (token_type) {
     case BOOLEAN:
 {
     switch (token_type) {
     case BOOLEAN:
@@ -235,8 +237,8 @@ _nc_ext_data_index(TERMTYPE * tp, int n, int token_type)
  * Adjust tables to remove (not free) an extended name and its corresponding
  * data.
  */
  * Adjust tables to remove (not free) an extended name and its corresponding
  * data.
  */
-static void
-_nc_del_ext_name(TERMTYPE * tp, char *name, int token_type)
+static bool
+_nc_del_ext_name(TERMTYPE *tp, char *name, int token_type)
 {
     int j;
     int first, last;
 {
     int j;
     int first, last;
@@ -270,7 +272,9 @@ _nc_del_ext_name(TERMTYPE * tp, char *name, int token_type)
            tp->num_Strings -= 1;
            break;
        }
            tp->num_Strings -= 1;
            break;
        }
+       return TRUE;
     }
     }
+    return FALSE;
 }
 
 /*
 }
 
 /*
@@ -278,7 +282,7 @@ _nc_del_ext_name(TERMTYPE * tp, char *name, int token_type)
  * index into the corresponding data array is returned.
  */
 static int
  * index into the corresponding data array is returned.
  */
 static int
-_nc_ins_ext_name(TERMTYPE * tp, char *name, int token_type)
+_nc_ins_ext_name(TERMTYPE *tp, char *name, int token_type)
 {
     unsigned first = _nc_first_ext_name(tp, token_type);
     unsigned last = _nc_last_ext_name(tp, token_type);
 {
     unsigned first = _nc_first_ext_name(tp, token_type);
     unsigned last = _nc_last_ext_name(tp, token_type);
@@ -289,7 +293,7 @@ _nc_ins_ext_name(TERMTYPE * tp, char *name, int token_type)
        int cmp = strcmp(name, tp->ext_Names[j]);
        if (cmp == 0)
            /* already present */
        int cmp = strcmp(name, tp->ext_Names[j]);
        if (cmp == 0)
            /* already present */
-           return _nc_ext_data_index(tp, j, token_type);
+           return _nc_ext_data_index(tp, (int) j, token_type);
        if (cmp < 0) {
            break;
        }
        if (cmp < 0) {
            break;
        }
@@ -299,13 +303,13 @@ _nc_ins_ext_name(TERMTYPE * tp, char *name, int token_type)
     for (k = total - 1; k > j; k--)
        tp->ext_Names[k] = tp->ext_Names[k - 1];
     tp->ext_Names[j] = name;
     for (k = total - 1; k > j; k--)
        tp->ext_Names[k] = tp->ext_Names[k - 1];
     tp->ext_Names[j] = name;
-    j = _nc_ext_data_index(tp, j, token_type);
+    j = _nc_ext_data_index(tp, (int) j, token_type);
 
     switch (token_type) {
     case BOOLEAN:
        tp->ext_Booleans += 1;
        tp->num_Booleans += 1;
 
     switch (token_type) {
     case BOOLEAN:
        tp->ext_Booleans += 1;
        tp->num_Booleans += 1;
-       tp->Booleans = typeRealloc(char, tp->num_Booleans, tp->Booleans);
+       tp->Booleans = typeRealloc(NCURSES_SBOOL, tp->num_Booleans, tp->Booleans);
        for (k = tp->num_Booleans - 1; k > j; k--)
            tp->Booleans[k] = tp->Booleans[k - 1];
        break;
        for (k = tp->num_Booleans - 1; k > j; k--)
            tp->Booleans[k] = tp->Booleans[k - 1];
        break;
@@ -333,7 +337,7 @@ _nc_ins_ext_name(TERMTYPE * tp, char *name, int token_type)
  * cancellation of a name that is inherited from another entry.
  */
 static void
  * cancellation of a name that is inherited from another entry.
  */
 static void
-adjust_cancels(TERMTYPE * to, TERMTYPE * from)
+adjust_cancels(TERMTYPE *to, TERMTYPE *from)
 {
     int first = to->ext_Booleans + to->ext_Numbers;
     int last = first + to->ext_Strings;
 {
     int first = to->ext_Booleans + to->ext_Numbers;
     int last = first + to->ext_Strings;
@@ -345,14 +349,22 @@ adjust_cancels(TERMTYPE * to, TERMTYPE * from)
 
        if (to->Strings[j + j_str] == CANCELLED_STRING) {
            if ((k = _nc_find_ext_name(from, to->ext_Names[j], BOOLEAN)) >= 0) {
 
        if (to->Strings[j + j_str] == CANCELLED_STRING) {
            if ((k = _nc_find_ext_name(from, to->ext_Names[j], BOOLEAN)) >= 0) {
-               _nc_del_ext_name(to, name, STRING);
-               k = _nc_ins_ext_name(to, name, BOOLEAN);
-               to->Booleans[k] = FALSE;
+               if (_nc_del_ext_name(to, name, STRING)
+                   || _nc_del_ext_name(to, name, NUMBER)) {
+                   k = _nc_ins_ext_name(to, name, BOOLEAN);
+                   to->Booleans[k] = FALSE;
+               } else {
+                   j++;
+               }
            } else if ((k = _nc_find_ext_name(from, to->ext_Names[j],
            } else if ((k = _nc_find_ext_name(from, to->ext_Names[j],
-               NUMBER)) >= 0) {
-               _nc_del_ext_name(to, name, STRING);
-               k = _nc_ins_ext_name(to, name, NUMBER);
-               to->Numbers[k] = CANCELLED_NUMERIC;
+                                             NUMBER)) >= 0) {
+               if (_nc_del_ext_name(to, name, STRING)
+                   || _nc_del_ext_name(to, name, BOOLEAN)) {
+                   k = _nc_ins_ext_name(to, name, NUMBER);
+                   to->Numbers[k] = CANCELLED_NUMERIC;
+               } else {
+                   j++;
+               }
            }
        } else {
            j++;
            }
        } else {
            j++;
@@ -360,8 +372,8 @@ adjust_cancels(TERMTYPE * to, TERMTYPE * from)
     }
 }
 
     }
 }
 
-void
-_nc_align_termtype(TERMTYPE * to, TERMTYPE * from)
+NCURSES_EXPORT(void)
+_nc_align_termtype(TERMTYPE *to, TERMTYPE *from)
 {
     int na = NUM_EXT_NAMES(to);
     int nb = NUM_EXT_NAMES(from);
 {
     int na = NUM_EXT_NAMES(to);
     int nb = NUM_EXT_NAMES(from);
@@ -369,9 +381,10 @@ _nc_align_termtype(TERMTYPE * to, TERMTYPE * from)
     bool same;
     char **ext_Names;
     int ext_Booleans, ext_Numbers, ext_Strings;
     bool same;
     char **ext_Names;
     int ext_Booleans, ext_Numbers, ext_Strings;
+    bool used_ext_Names = FALSE;
 
     DEBUG(2, ("align_termtype to(%d:%s), from(%d:%s)", na, to->term_names,
 
     DEBUG(2, ("align_termtype to(%d:%s), from(%d:%s)", na, to->term_names,
-           nb, from->term_names));
+             nb, from->term_names));
 
     if (na != 0 || nb != 0) {
        if ((na == nb)          /* check if the arrays are equivalent */
 
     if (na != 0 || nb != 0) {
        if ((na == nb)          /* check if the arrays are equivalent */
@@ -402,26 +415,26 @@ _nc_align_termtype(TERMTYPE * to, TERMTYPE * from)
            adjust_cancels(from, to);
 
        ext_Booleans = merge_names(ext_Names,
            adjust_cancels(from, to);
 
        ext_Booleans = merge_names(ext_Names,
-           to->ext_Names,
-           to->ext_Booleans,
-           from->ext_Names,
-           from->ext_Booleans);
+                                  to->ext_Names,
+                                  to->ext_Booleans,
+                                  from->ext_Names,
+                                  from->ext_Booleans);
        ext_Numbers = merge_names(ext_Names + ext_Booleans,
        ext_Numbers = merge_names(ext_Names + ext_Booleans,
-           to->ext_Names
-           + to->ext_Booleans,
-           to->ext_Numbers,
-           from->ext_Names
-           + from->ext_Booleans,
-           from->ext_Numbers);
+                                 to->ext_Names
+                                 + to->ext_Booleans,
+                                 to->ext_Numbers,
+                                 from->ext_Names
+                                 + from->ext_Booleans,
+                                 from->ext_Numbers);
        ext_Strings = merge_names(ext_Names + ext_Numbers + ext_Booleans,
        ext_Strings = merge_names(ext_Names + ext_Numbers + ext_Booleans,
-           to->ext_Names
-           + to->ext_Booleans
-           + to->ext_Numbers,
-           to->ext_Strings,
-           from->ext_Names
-           + from->ext_Booleans
-           + from->ext_Numbers,
-           from->ext_Strings);
+                                 to->ext_Names
+                                 + to->ext_Booleans
+                                 + to->ext_Numbers,
+                                 to->ext_Strings,
+                                 from->ext_Names
+                                 + from->ext_Booleans
+                                 + from->ext_Numbers,
+                                 from->ext_Strings);
        /*
         * Now we must reallocate the Booleans, etc., to allow the data to be
         * overlaid.
        /*
         * Now we must reallocate the Booleans, etc., to allow the data to be
         * overlaid.
@@ -431,7 +444,8 @@ _nc_align_termtype(TERMTYPE * to, TERMTYPE * from)
            FreeIfNeeded(to->ext_Names);
            to->ext_Names = ext_Names;
            DEBUG(2, ("realigned %d extended names for '%s' (to)",
            FreeIfNeeded(to->ext_Names);
            to->ext_Names = ext_Names;
            DEBUG(2, ("realigned %d extended names for '%s' (to)",
-                   NUM_EXT_NAMES(to), to->term_names));
+                     NUM_EXT_NAMES(to), to->term_names));
+           used_ext_Names = TRUE;
        }
        if (nb != (ext_Booleans + ext_Numbers + ext_Strings)) {
            nb = (ext_Booleans + ext_Numbers + ext_Strings);
        }
        if (nb != (ext_Booleans + ext_Numbers + ext_Strings)) {
            nb = (ext_Booleans + ext_Numbers + ext_Strings);
@@ -439,19 +453,21 @@ _nc_align_termtype(TERMTYPE * to, TERMTYPE * from)
            from->ext_Names = typeRealloc(char *, nb, from->ext_Names);
            memcpy(from->ext_Names, ext_Names, sizeof(char *) * nb);
            DEBUG(2, ("realigned %d extended names for '%s' (from)",
            from->ext_Names = typeRealloc(char *, nb, from->ext_Names);
            memcpy(from->ext_Names, ext_Names, sizeof(char *) * nb);
            DEBUG(2, ("realigned %d extended names for '%s' (from)",
-                   NUM_EXT_NAMES(from), from->term_names));
+                     NUM_EXT_NAMES(from), from->term_names));
        }
        }
+       if (!used_ext_Names)
+           free(ext_Names);
     }
 }
 #endif
 
     }
 }
 #endif
 
-void
-_nc_copy_termtype(TERMTYPE * dst, TERMTYPE * src)
+NCURSES_EXPORT(void)
+_nc_copy_termtype(TERMTYPE *dst, TERMTYPE *src)
 {
 {
-    int i;
+    unsigned i;
 
     *dst = *src;               /* ...to copy the sizes and string-tables */
 
     *dst = *src;               /* ...to copy the sizes and string-tables */
-    dst->Booleans = typeMalloc(char, NUM_BOOLEANS(dst));
+    dst->Booleans = typeMalloc(NCURSES_SBOOL, NUM_BOOLEANS(dst));
     dst->Numbers = typeMalloc(short, NUM_NUMBERS(dst));
     dst->Strings = typeMalloc(char *, NUM_STRINGS(dst));
 
     dst->Numbers = typeMalloc(short, NUM_NUMBERS(dst));
     dst->Strings = typeMalloc(char *, NUM_STRINGS(dst));