Back to index

tetex-bin  3.0
alloc_ttype.c
Go to the documentation of this file.
00001 /****************************************************************************
00002  * Copyright (c) 1999-2002,2003 Free Software Foundation, Inc.              *
00003  *                                                                          *
00004  * Permission is hereby granted, free of charge, to any person obtaining a  *
00005  * copy of this software and associated documentation files (the            *
00006  * "Software"), to deal in the Software without restriction, including      *
00007  * without limitation the rights to use, copy, modify, merge, publish,      *
00008  * distribute, distribute with modifications, sublicense, and/or sell       *
00009  * copies of the Software, and to permit persons to whom the Software is    *
00010  * furnished to do so, subject to the following conditions:                 *
00011  *                                                                          *
00012  * The above copyright notice and this permission notice shall be included  *
00013  * in all copies or substantial portions of the Software.                   *
00014  *                                                                          *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
00016  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
00017  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
00018  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
00019  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
00020  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
00021  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
00022  *                                                                          *
00023  * Except as contained in this notice, the name(s) of the above copyright   *
00024  * holders shall not be used in advertising or otherwise to promote the     *
00025  * sale, use or other dealings in this Software without prior written       *
00026  * authorization.                                                           *
00027  ****************************************************************************/
00028 
00029 /****************************************************************************
00030  *  Author: Thomas E. Dickey <dickey@clark.net> 1999                        *
00031  ****************************************************************************/
00032 
00033 /*
00034  * align_ttype.c --  functions for TERMTYPE
00035  *
00036  *     _nc_align_termtype()
00037  *     _nc_copy_termtype()
00038  *
00039  */
00040 
00041 #include <curses.priv.h>
00042 
00043 #include <tic.h>
00044 #include <term_entry.h>
00045 
00046 MODULE_ID("$Id: alloc_ttype.c,v 1.14 2003/05/24 21:10:28 tom Exp $")
00047 
00048 #if NCURSES_XNAMES
00049 /*
00050  * Merge the a/b lists into dst.  Both a/b are sorted (see _nc_extend_names()),
00051  * so we do not have to worry about order dependencies.
00052  */
00053 static int
00054 merge_names(char **dst, char **a, int na, char **b, int nb)
00055 {
00056     int n = 0;
00057     while (na > 0 && nb > 0) {
00058        int cmp = strcmp(*a, *b);
00059        if (cmp < 0) {
00060            dst[n++] = *a++;
00061            na--;
00062        } else if (cmp > 0) {
00063            dst[n++] = *b++;
00064            nb--;
00065        } else if (cmp == 0) {
00066            dst[n++] = *a;
00067            a++, b++;
00068            na--, nb--;
00069        }
00070     }
00071     while (na-- > 0) {
00072        dst[n++] = *a++;
00073     }
00074     while (nb-- > 0) {
00075        dst[n++] = *b++;
00076     }
00077     DEBUG(4, ("merge_names -> %d", n));
00078     return n;
00079 }
00080 
00081 static bool
00082 find_name(char **table, int length, char *name)
00083 {
00084     while (length-- > 0) {
00085        if (!strcmp(*table++, name)) {
00086            DEBUG(4, ("found name '%s'", name));
00087            return TRUE;
00088        }
00089     }
00090     DEBUG(4, ("did not find name '%s'", name));
00091     return FALSE;
00092 }
00093 
00094 static void
00095 realign_data(TERMTYPE * to, char **ext_Names,
00096             int ext_Booleans,
00097             int ext_Numbers,
00098             int ext_Strings)
00099 {
00100     int n, m, base;
00101     int limit = (to->ext_Booleans + to->ext_Numbers + to->ext_Strings);
00102 
00103     if (to->ext_Booleans != ext_Booleans) {
00104        to->num_Booleans += (ext_Booleans - to->ext_Booleans);
00105        to->Booleans = typeRealloc(char, to->num_Booleans, to->Booleans);
00106        for (n = to->ext_Booleans - 1,
00107             m = ext_Booleans - 1,
00108             base = to->num_Booleans - (m + 1); m >= 0; m--) {
00109            if (find_name(to->ext_Names, limit, ext_Names[m])) {
00110               to->Booleans[base + m] = to->Booleans[base + n--];
00111            } else {
00112               to->Booleans[base + m] = FALSE;
00113            }
00114        }
00115        to->ext_Booleans = ext_Booleans;
00116     }
00117     if (to->ext_Numbers != ext_Numbers) {
00118        to->num_Numbers += (ext_Numbers - to->ext_Numbers);
00119        to->Numbers = typeRealloc(short, to->num_Numbers, to->Numbers);
00120        for (n = to->ext_Numbers - 1,
00121             m = ext_Numbers - 1,
00122             base = to->num_Numbers - (m + 1); m >= 0; m--) {
00123            if (find_name(to->ext_Names, limit, ext_Names[m + ext_Booleans])) {
00124               to->Numbers[base + m] = to->Numbers[base + n--];
00125            } else {
00126               to->Numbers[base + m] = ABSENT_NUMERIC;
00127            }
00128        }
00129        to->ext_Numbers = ext_Numbers;
00130     }
00131     if (to->ext_Strings != ext_Strings) {
00132        to->num_Strings += (ext_Strings - to->ext_Strings);
00133        to->Strings = typeRealloc(char *, to->num_Strings, to->Strings);
00134        for (n = to->ext_Strings - 1,
00135             m = ext_Strings - 1,
00136             base = to->num_Strings - (m + 1); m >= 0; m--) {
00137            if (find_name(to->ext_Names, limit, ext_Names[m + ext_Booleans + ext_Numbers])) {
00138               to->Strings[base + m] = to->Strings[base + n--];
00139            } else {
00140               to->Strings[base + m] = ABSENT_STRING;
00141            }
00142        }
00143        to->ext_Strings = ext_Strings;
00144     }
00145 }
00146 
00147 /*
00148  * Returns the first index in ext_Names[] for the given token-type
00149  */
00150 static int
00151 _nc_first_ext_name(TERMTYPE * tp, int token_type)
00152 {
00153     int first;
00154 
00155     switch (token_type) {
00156     case BOOLEAN:
00157        first = 0;
00158        break;
00159     case NUMBER:
00160        first = tp->ext_Booleans;
00161        break;
00162     case STRING:
00163        first = tp->ext_Booleans + tp->ext_Numbers;
00164        break;
00165     default:
00166        first = 0;
00167        break;
00168     }
00169     return first;
00170 }
00171 
00172 /*
00173  * Returns the last index in ext_Names[] for the given token-type
00174  */
00175 static int
00176 _nc_last_ext_name(TERMTYPE * tp, int token_type)
00177 {
00178     int last;
00179 
00180     switch (token_type) {
00181     case BOOLEAN:
00182        last = tp->ext_Booleans;
00183        break;
00184     case NUMBER:
00185        last = tp->ext_Booleans + tp->ext_Numbers;
00186        break;
00187     default:
00188     case STRING:
00189        last = NUM_EXT_NAMES(tp);
00190        break;
00191     }
00192     return last;
00193 }
00194 
00195 /*
00196  * Lookup an entry from extended-names, returning -1 if not found
00197  */
00198 static int
00199 _nc_find_ext_name(TERMTYPE * tp, char *name, int token_type)
00200 {
00201     unsigned j;
00202     unsigned first = _nc_first_ext_name(tp, token_type);
00203     unsigned last = _nc_last_ext_name(tp, token_type);
00204 
00205     for (j = first; j < last; j++) {
00206        if (!strcmp(name, tp->ext_Names[j])) {
00207            return j;
00208        }
00209     }
00210     return -1;
00211 }
00212 
00213 /*
00214  * Translate an index into ext_Names[] into the corresponding index into data
00215  * (e.g., Booleans[]).
00216  */
00217 static int
00218 _nc_ext_data_index(TERMTYPE * tp, int n, int token_type)
00219 {
00220     switch (token_type) {
00221     case BOOLEAN:
00222        n += (tp->num_Booleans - tp->ext_Booleans);
00223        break;
00224     case NUMBER:
00225        n += (tp->num_Numbers - tp->ext_Numbers)
00226            - (tp->ext_Booleans);
00227        break;
00228     default:
00229     case STRING:
00230        n += (tp->num_Strings - tp->ext_Strings)
00231            - (tp->ext_Booleans + tp->ext_Numbers);
00232     }
00233     return n;
00234 }
00235 
00236 /*
00237  * Adjust tables to remove (not free) an extended name and its corresponding
00238  * data.
00239  */
00240 static bool
00241 _nc_del_ext_name(TERMTYPE * tp, char *name, int token_type)
00242 {
00243     int j;
00244     int first, last;
00245 
00246     if ((first = _nc_find_ext_name(tp, name, token_type)) >= 0) {
00247        last = NUM_EXT_NAMES(tp) - 1;
00248        for (j = first; j < last; j++) {
00249            tp->ext_Names[j] = tp->ext_Names[j + 1];
00250        }
00251        first = _nc_ext_data_index(tp, first, token_type);
00252        switch (token_type) {
00253        case BOOLEAN:
00254            last = tp->num_Booleans - 1;
00255            for (j = first; j < last; j++)
00256               tp->Booleans[j] = tp->Booleans[j + 1];
00257            tp->ext_Booleans -= 1;
00258            tp->num_Booleans -= 1;
00259            break;
00260        case NUMBER:
00261            last = tp->num_Numbers - 1;
00262            for (j = first; j < last; j++)
00263               tp->Numbers[j] = tp->Numbers[j + 1];
00264            tp->ext_Numbers -= 1;
00265            tp->num_Numbers -= 1;
00266            break;
00267        case STRING:
00268            last = tp->num_Strings - 1;
00269            for (j = first; j < last; j++)
00270               tp->Strings[j] = tp->Strings[j + 1];
00271            tp->ext_Strings -= 1;
00272            tp->num_Strings -= 1;
00273            break;
00274        }
00275        return TRUE;
00276     }
00277     return FALSE;
00278 }
00279 
00280 /*
00281  * Adjust tables to insert an extended name, making room for new data.  The
00282  * index into the corresponding data array is returned.
00283  */
00284 static int
00285 _nc_ins_ext_name(TERMTYPE * tp, char *name, int token_type)
00286 {
00287     unsigned first = _nc_first_ext_name(tp, token_type);
00288     unsigned last = _nc_last_ext_name(tp, token_type);
00289     unsigned total = NUM_EXT_NAMES(tp) + 1;
00290     unsigned j, k;
00291 
00292     for (j = first; j < last; j++) {
00293        int cmp = strcmp(name, tp->ext_Names[j]);
00294        if (cmp == 0)
00295            /* already present */
00296            return _nc_ext_data_index(tp, (int) j, token_type);
00297        if (cmp < 0) {
00298            break;
00299        }
00300     }
00301 
00302     tp->ext_Names = typeRealloc(char *, total, tp->ext_Names);
00303     for (k = total - 1; k > j; k--)
00304        tp->ext_Names[k] = tp->ext_Names[k - 1];
00305     tp->ext_Names[j] = name;
00306     j = _nc_ext_data_index(tp, (int) j, token_type);
00307 
00308     switch (token_type) {
00309     case BOOLEAN:
00310        tp->ext_Booleans += 1;
00311        tp->num_Booleans += 1;
00312        tp->Booleans = typeRealloc(char, tp->num_Booleans, tp->Booleans);
00313        for (k = tp->num_Booleans - 1; k > j; k--)
00314            tp->Booleans[k] = tp->Booleans[k - 1];
00315        break;
00316     case NUMBER:
00317        tp->ext_Numbers += 1;
00318        tp->num_Numbers += 1;
00319        tp->Numbers = typeRealloc(short, tp->num_Numbers, tp->Numbers);
00320        for (k = tp->num_Numbers - 1; k > j; k--)
00321            tp->Numbers[k] = tp->Numbers[k - 1];
00322        break;
00323     case STRING:
00324        tp->ext_Strings += 1;
00325        tp->num_Strings += 1;
00326        tp->Strings = typeRealloc(char *, tp->num_Strings, tp->Strings);
00327        for (k = tp->num_Strings - 1; k > j; k--)
00328            tp->Strings[k] = tp->Strings[k - 1];
00329        break;
00330     }
00331     return j;
00332 }
00333 
00334 /*
00335  * Look for strings that are marked cancelled, which happen to be the same name
00336  * as a boolean or number.  We'll get this as a special case when we get a
00337  * cancellation of a name that is inherited from another entry.
00338  */
00339 static void
00340 adjust_cancels(TERMTYPE * to, TERMTYPE * from)
00341 {
00342     int first = to->ext_Booleans + to->ext_Numbers;
00343     int last = first + to->ext_Strings;
00344     int j, k;
00345 
00346     for (j = first; j < last;) {
00347        char *name = to->ext_Names[j];
00348        unsigned j_str = to->num_Strings - first - to->ext_Strings;
00349 
00350        if (to->Strings[j + j_str] == CANCELLED_STRING) {
00351            if ((k = _nc_find_ext_name(from, to->ext_Names[j], BOOLEAN)) >= 0) {
00352               if (_nc_del_ext_name(to, name, STRING)
00353                   || _nc_del_ext_name(to, name, NUMBER)) {
00354                   k = _nc_ins_ext_name(to, name, BOOLEAN);
00355                   to->Booleans[k] = FALSE;
00356               } else {
00357                   j++;
00358               }
00359            } else if ((k = _nc_find_ext_name(from, to->ext_Names[j],
00360                                          NUMBER)) >= 0) {
00361               if (_nc_del_ext_name(to, name, STRING)
00362                   || _nc_del_ext_name(to, name, BOOLEAN)) {
00363                   k = _nc_ins_ext_name(to, name, NUMBER);
00364                   to->Numbers[k] = CANCELLED_NUMERIC;
00365               } else {
00366                   j++;
00367               }
00368            }
00369        } else {
00370            j++;
00371        }
00372     }
00373 }
00374 
00375 NCURSES_EXPORT(void)
00376 _nc_align_termtype(TERMTYPE * to, TERMTYPE * from)
00377 {
00378     int na = NUM_EXT_NAMES(to);
00379     int nb = NUM_EXT_NAMES(from);
00380     int n;
00381     bool same;
00382     char **ext_Names;
00383     int ext_Booleans, ext_Numbers, ext_Strings;
00384 
00385     DEBUG(2, ("align_termtype to(%d:%s), from(%d:%s)", na, to->term_names,
00386              nb, from->term_names));
00387 
00388     if (na != 0 || nb != 0) {
00389        if ((na == nb)              /* check if the arrays are equivalent */
00390            &&(to->ext_Booleans == from->ext_Booleans)
00391            && (to->ext_Numbers == from->ext_Numbers)
00392            && (to->ext_Strings == from->ext_Strings)) {
00393            for (n = 0, same = TRUE; n < na; n++) {
00394               if (strcmp(to->ext_Names[n], from->ext_Names[n])) {
00395                   same = FALSE;
00396                   break;
00397               }
00398            }
00399            if (same)
00400               return;
00401        }
00402        /*
00403         * This is where we pay for having a simple extension representation. 
00404         * Allocate a new ext_Names array and merge the two ext_Names arrays
00405         * into it, updating to's counts for booleans, etc.  Fortunately we do
00406         * this only for the terminfo compiler (tic) and comparer (infocmp).
00407         */
00408        ext_Names = typeMalloc(char *, na + nb);
00409 
00410        if (to->ext_Strings && (from->ext_Booleans + from->ext_Numbers))
00411            adjust_cancels(to, from);
00412 
00413        if (from->ext_Strings && (to->ext_Booleans + to->ext_Numbers))
00414            adjust_cancels(from, to);
00415 
00416        ext_Booleans = merge_names(ext_Names,
00417                                to->ext_Names,
00418                                to->ext_Booleans,
00419                                from->ext_Names,
00420                                from->ext_Booleans);
00421        ext_Numbers = merge_names(ext_Names + ext_Booleans,
00422                               to->ext_Names
00423                               + to->ext_Booleans,
00424                               to->ext_Numbers,
00425                               from->ext_Names
00426                               + from->ext_Booleans,
00427                               from->ext_Numbers);
00428        ext_Strings = merge_names(ext_Names + ext_Numbers + ext_Booleans,
00429                               to->ext_Names
00430                               + to->ext_Booleans
00431                               + to->ext_Numbers,
00432                               to->ext_Strings,
00433                               from->ext_Names
00434                               + from->ext_Booleans
00435                               + from->ext_Numbers,
00436                               from->ext_Strings);
00437        /*
00438         * Now we must reallocate the Booleans, etc., to allow the data to be
00439         * overlaid.
00440         */
00441        if (na != (ext_Booleans + ext_Numbers + ext_Strings)) {
00442            realign_data(to, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
00443            FreeIfNeeded(to->ext_Names);
00444            to->ext_Names = ext_Names;
00445            DEBUG(2, ("realigned %d extended names for '%s' (to)",
00446                     NUM_EXT_NAMES(to), to->term_names));
00447        }
00448        if (nb != (ext_Booleans + ext_Numbers + ext_Strings)) {
00449            nb = (ext_Booleans + ext_Numbers + ext_Strings);
00450            realign_data(from, ext_Names, ext_Booleans, ext_Numbers, ext_Strings);
00451            from->ext_Names = typeRealloc(char *, nb, from->ext_Names);
00452            memcpy(from->ext_Names, ext_Names, sizeof(char *) * nb);
00453            DEBUG(2, ("realigned %d extended names for '%s' (from)",
00454                     NUM_EXT_NAMES(from), from->term_names));
00455        }
00456     }
00457 }
00458 #endif
00459 
00460 NCURSES_EXPORT(void)
00461 _nc_copy_termtype(TERMTYPE * dst, TERMTYPE * src)
00462 {
00463     unsigned i;
00464 
00465     *dst = *src;            /* ...to copy the sizes and string-tables */
00466     dst->Booleans = typeMalloc(char, NUM_BOOLEANS(dst));
00467     dst->Numbers = typeMalloc(short, NUM_NUMBERS(dst));
00468     dst->Strings = typeMalloc(char *, NUM_STRINGS(dst));
00469 
00470     /* FIXME: use memcpy for these and similar loops */
00471     for_each_boolean(i, dst)
00472        dst->Booleans[i] = src->Booleans[i];
00473     for_each_number(i, dst)
00474        dst->Numbers[i] = src->Numbers[i];
00475     for_each_string(i, dst)
00476        dst->Strings[i] = src->Strings[i];
00477 
00478     /* FIXME: we probably should also copy str_table and ext_str_table,
00479      * but tic and infocmp are not written to exploit that (yet).
00480      */
00481 
00482 #if NCURSES_XNAMES
00483     if ((i = NUM_EXT_NAMES(src)) != 0) {
00484        dst->ext_Names = typeMalloc(char *, i);
00485        memcpy(dst->ext_Names, src->ext_Names, i * sizeof(char *));
00486     } else {
00487        dst->ext_Names = 0;
00488     }
00489 #endif
00490 
00491 }