]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/tty/hashmap.c
ncurses 6.3 - patch 20211120
[ncurses.git] / ncurses / tty / hashmap.c
index 05c379199c217f4aa513b1f8d4f1aef5ccc7ff95..3f124c96c8968e11dad46c37ac8b7d4ea7a44e7f 100644 (file)
@@ -1,5 +1,6 @@
 /****************************************************************************
- * Copyright (c) 1998-2007,2009 Free Software Foundation, Inc.              *
+ * Copyright 2019,2020 Thomas E. Dickey                                     *
+ * Copyright 1998-2015,2016 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            *
@@ -69,19 +70,27 @@ AUTHOR
 
 #include <curses.priv.h>
 
-MODULE_ID("$Id: hashmap.c,v 1.57 2009/04/18 19:03:50 tom Exp $")
+#ifndef CUR
+#define CUR SP_TERMTYPE
+#endif
+
+MODULE_ID("$Id: hashmap.c,v 1.69 2020/05/31 17:50:48 tom Exp $")
 
 #ifdef HASHDEBUG
 
 # define _tracef       printf
 # undef TR
+# ifdef TRACE
 # define TR(n, a)      if (_nc_tracing & (n)) { _tracef a ; putchar('\n'); }
+# else
+# define TR(n, a)      { _tracef a ; putchar('\n'); }
+# endif
 # undef screen_lines
-# define screen_lines MAXLINES
-# define TEXTWIDTH     1
-int oldnums[MAXLINES], reallines[MAXLINES];
-static NCURSES_CH_T oldtext[MAXLINES][TEXTWIDTH];
-static NCURSES_CH_T newtext[MAXLINES][TEXTWIDTH];
+# define screen_lines(sp) MAXLINES
+# define TEXTWIDTH(sp) 1
+static int oldnums[MAXLINES], reallines[MAXLINES];
+static NCURSES_CH_T oldtext[MAXLINES][TEXTWIDTH(sp)];
+static NCURSES_CH_T newtext[MAXLINES][TEXTWIDTH(sp)];
 # define OLDNUM(sp,n)  oldnums[n]
 # define OLDTEXT(sp,n) oldtext[n]
 # define NEWTEXT(sp,m) newtext[m]
@@ -90,10 +99,10 @@ static NCURSES_CH_T newtext[MAXLINES][TEXTWIDTH];
 #else /* !HASHDEBUG */
 
 # define OLDNUM(sp,n)  (sp)->_oldnum_list[n]
-# define OLDTEXT(sp,n) (sp)->_curscr->_line[n].text
-# define NEWTEXT(sp,m) (sp)->_newscr->_line[m].text
-# define TEXTWIDTH(sp) ((sp)->_curscr->_maxx+1)
-# define PENDING(sp,n)  ((sp)->_newscr->_line[n].firstchar != _NOCHANGE)
+# define OLDTEXT(sp,n) CurScreen(sp)->_line[n].text
+# define NEWTEXT(sp,m) NewScreen(sp)->_line[m].text
+# define TEXTWIDTH(sp) (CurScreen(sp)->_maxx + 1)
+# define PENDING(sp,n)  (NewScreen(sp)->_line[n].firstchar != _NOCHANGE)
 
 #endif /* !HASHDEBUG */
 
@@ -111,24 +120,27 @@ static NCURSES_CH_T newtext[MAXLINES][TEXTWIDTH];
 static const NCURSES_CH_T blankchar = NewChar(BLANK_TEXT);
 
 static NCURSES_INLINE unsigned long
-hash(SCREEN *sp, NCURSES_CH_T * text)
+hash(SCREEN *sp, NCURSES_CH_T *text)
 {
     int i;
     NCURSES_CH_T ch;
     unsigned long result = 0;
+    (void) sp;
+
     for (i = TEXTWIDTH(sp); i > 0; i--) {
        ch = *text++;
-       result += (result << 5) + HASH_VAL(ch);
+       result += (result << 5) + (unsigned long) HASH_VAL(ch);
     }
     return result;
 }
 
 /* approximate update cost */
 static int
-update_cost(SCREEN *sp, NCURSES_CH_T * from, NCURSES_CH_T * to)
+update_cost(SCREEN *sp, NCURSES_CH_T *from, NCURSES_CH_T *to)
 {
     int cost = 0;
     int i;
+    (void) sp;
 
     for (i = TEXTWIDTH(sp); i > 0; i--, from++, to++)
        if (!(CharEq(*from, *to)))
@@ -138,11 +150,12 @@ update_cost(SCREEN *sp, NCURSES_CH_T * from, NCURSES_CH_T * to)
 }
 
 static int
-update_cost_from_blank(SCREEN *sp, NCURSES_CH_T * to)
+update_cost_from_blank(SCREEN *sp, NCURSES_CH_T *to)
 {
     int cost = 0;
     int i;
     NCURSES_CH_T blank = blankchar;
+    (void) sp;
 
     if (back_color_erase)
        SetPair(blank, GetPair(stdscr->_nc_bkgd));
@@ -159,7 +172,7 @@ update_cost_from_blank(SCREEN *sp, NCURSES_CH_T * to)
  * effective. 'blank' indicates whether the line 'to' would become blank.
  */
 static NCURSES_INLINE bool
-cost_effective(SCREEN *sp, const int from, const int to, const bool blank)
+cost_effective(SCREEN *sp, const int from, const int to, const int blank)
 {
     int new_from;
 
@@ -186,9 +199,8 @@ cost_effective(SCREEN *sp, const int from, const int to, const bool blank)
 static void
 grow_hunks(SCREEN *sp)
 {
-    int start, end, shift;
-    int back_limit, forward_limit;     /* limits for cells to fill */
-    int back_ref_limit, forward_ref_limit;     /* limits for refrences */
+    int back_limit;            /* limits for cells to fill */
+    int back_ref_limit;                /* limit for references */
     int i;
     int next_hunk;
 
@@ -203,8 +215,11 @@ grow_hunks(SCREEN *sp)
     while (i < screen_lines(sp) && OLDNUM(sp, i) == _NEWINDEX)
        i++;
     for (; i < screen_lines(sp); i = next_hunk) {
-       start = i;
-       shift = OLDNUM(sp, i) - i;
+       int forward_limit;
+       int forward_ref_limit;
+       int end;
+       int start = i;
+       int shift = OLDNUM(sp, i) - i;
 
        /* get forward limit */
        i = start + 1;
@@ -273,12 +288,12 @@ NCURSES_SP_NAME(_nc_hash_map) (NCURSES_SP_DCL0)
 {
     HASHMAP *hsp;
     register int i;
-    int start, shift, size;
 
     if (screen_lines(SP_PARM) > lines_alloc(SP_PARM)) {
        if (hashtab(SP_PARM))
            free(hashtab(SP_PARM));
-       hashtab(SP_PARM) = typeMalloc(HASHMAP, (screen_lines(SP_PARM) + 1) * 2);
+       hashtab(SP_PARM) = typeMalloc(HASHMAP,
+                                     ((size_t) screen_lines(SP_PARM) + 1) * 2);
        if (!hashtab(SP_PARM)) {
            if (oldhash(SP_PARM)) {
                FreeAndNull(oldhash(SP_PARM));
@@ -299,10 +314,10 @@ NCURSES_SP_NAME(_nc_hash_map) (NCURSES_SP_DCL0)
        /* re-hash all */
        if (oldhash(SP_PARM) == 0)
            oldhash(SP_PARM) = typeCalloc(unsigned long,
-                                           (unsigned) screen_lines(SP_PARM));
+                                           (size_t) screen_lines(SP_PARM));
        if (newhash(SP_PARM) == 0)
            newhash(SP_PARM) = typeCalloc(unsigned long,
-                                           (unsigned) screen_lines(SP_PARM));
+                                           (size_t) screen_lines(SP_PARM));
        if (!oldhash(SP_PARM) || !newhash(SP_PARM))
            return;             /* malloc failure */
        for (i = 0; i < screen_lines(SP_PARM); i++) {
@@ -324,7 +339,8 @@ NCURSES_SP_NAME(_nc_hash_map) (NCURSES_SP_DCL0)
      * Set up and count line-hash values.
      */
     memset(hashtab(SP_PARM), '\0',
-          sizeof(*(hashtab(SP_PARM))) * (screen_lines(SP_PARM) + 1) * 2);
+          sizeof(*(hashtab(SP_PARM)))
+          * ((size_t) screen_lines(SP_PARM) + 1) * 2);
     for (i = 0; i < screen_lines(SP_PARM); i++) {
        unsigned long hashval = oldhash(SP_PARM)[i];
 
@@ -373,6 +389,8 @@ NCURSES_SP_NAME(_nc_hash_map) (NCURSES_SP_DCL0)
      * more than carry.
      */
     for (i = 0; i < screen_lines(SP_PARM);) {
+       int start, shift, size;
+
        while (i < screen_lines(SP_PARM) && OLDNUM(SP_PARM, i) == _NEWINDEX)
            i++;
        if (i >= screen_lines(SP_PARM))
@@ -429,7 +447,7 @@ NCURSES_SP_NAME(_nc_scroll_oldhash) (NCURSES_SP_DCLx int n, int top, int bot)
     if (!oldhash(SP_PARM))
        return;
 
-    size = sizeof(*(oldhash(SP_PARM))) * (bot - top + 1 - abs(n));
+    size = sizeof(*(oldhash(SP_PARM))) * (size_t) (bot - top + 1 - abs(n));
     if (n > 0) {
        memmove(oldhash(SP_PARM) + top, oldhash(SP_PARM) + top + n, size);
        for (i = bot; i > bot - n; i--)
@@ -480,13 +498,13 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
        return EXIT_FAILURE;
     (void) _nc_alloc_screen();
 
-    for (n = 0; n < screen_lines; n++) {
+    for (n = 0; n < screen_lines(sp); n++) {
        reallines[n] = n;
        oldnums[n] = _NEWINDEX;
        CharOf(oldtext[n][0]) = CharOf(newtext[n][0]) = '.';
     }
 
-    if (isatty(fileno(stdin)))
+    if (NC_ISATTY(fileno(stdin)))
        usage();
 
 #ifdef TRACE
@@ -503,7 +521,7 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
            break;
 
        case 'l':               /* get initial line number vector */
-           for (n = 0; n < screen_lines; n++) {
+           for (n = 0; n < screen_lines(sp); n++) {
                reallines[n] = n;
                oldnums[n] = _NEWINDEX;
            }
@@ -516,9 +534,9 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
            break;
 
        case 'n':               /* use following letters as text of new lines */
-           for (n = 0; n < screen_lines; n++)
+           for (n = 0; n < screen_lines(sp); n++)
                CharOf(newtext[n][0]) = '.';
-           for (n = 0; n < screen_lines; n++)
+           for (n = 0; n < screen_lines(sp); n++)
                if (line[n + 1] == '\n')
                    break;
                else
@@ -526,9 +544,9 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
            break;
 
        case 'o':               /* use following letters as text of old lines */
-           for (n = 0; n < screen_lines; n++)
+           for (n = 0; n < screen_lines(sp); n++)
                CharOf(oldtext[n][0]) = '.';
-           for (n = 0; n < screen_lines; n++)
+           for (n = 0; n < screen_lines(sp); n++)
                if (line[n + 1] == '\n')
                    break;
                else
@@ -540,12 +558,12 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
            _nc_linedump();
 #endif
            (void) fputs("Old lines: [", stdout);
-           for (n = 0; n < screen_lines; n++)
+           for (n = 0; n < screen_lines(sp); n++)
                putchar(CharOf(oldtext[n][0]));
            putchar(']');
            putchar('\n');
            (void) fputs("New lines: [", stdout);
-           for (n = 0; n < screen_lines; n++)
+           for (n = 0; n < screen_lines(sp); n++)
                putchar(CharOf(newtext[n][0]));
            putchar(']');
            putchar('\n');
@@ -566,11 +584,7 @@ main(int argc GCC_UNUSED, char *argv[]GCC_UNUSED)
            break;
        }
     }
-#if NO_LEAKS
-    _nc_free_and_exit(EXIT_SUCCESS);
-#else
-    return EXIT_SUCCESS;
-#endif
+    exit_curses(EXIT_SUCCESS);
 }
 
 #endif /* HASHDEBUG */