]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/base/lib_screen.c
ncurses 6.0 - patch 20160625
[ncurses.git] / ncurses / base / lib_screen.c
index 4d310be286028a726dde52d995228bd2abec5a5f..e5124a09d9a8d154459f8b7c7970e07cb6feec0f 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 1998-2011,2015 Free Software Foundation, Inc.              *
+ * Copyright (c) 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            *
@@ -41,7 +41,7 @@
 #define CUR SP_TERMTYPE
 #endif
 
-MODULE_ID("$Id: lib_screen.c,v 1.72 2015/03/29 15:25:29 tom Exp $")
+MODULE_ID("$Id: lib_screen.c,v 1.83 2016/05/29 01:38:42 tom Exp $")
 
 #define MAX_SIZE 0x3fff                /* 16k is big enough for a window or pad */
 
@@ -56,7 +56,7 @@ MODULE_ID("$Id: lib_screen.c,v 1.72 2015/03/29 15:25:29 tom Exp $")
  * mistaken for the _cury/_curx pair of 16-bit numbers which start the old
  * format.  It happens to be unused in the file 5.22 database (2015/03/07).
  */
-static char my_magic[] =
+static const char my_magic[] =
 {'\210', '\210', '\210', '\210'};
 
 #if NCURSES_EXT_PUTWIN
@@ -73,19 +73,19 @@ typedef enum {
 } PARAM_TYPE;
 
 typedef struct {
-    const char *name;
+    const char name[11];
     attr_t attr;
 } SCR_ATTRS;
 
 typedef struct {
-    const char *name;
+    const char name[17];
     PARAM_TYPE type;
     size_t size;
     size_t offset;
 } SCR_PARAMS;
 
-#define DATA(name) { #name, A_##name }
-static SCR_ATTRS scr_attrs[] =
+#define DATA(name) { { #name }, A_##name }
+static const SCR_ATTRS scr_attrs[] =
 {
     DATA(NORMAL),
     DATA(STANDOUT),
@@ -111,9 +111,9 @@ static SCR_ATTRS scr_attrs[] =
 #undef DATA
 
 #define sizeof2(type,name) sizeof(((type *)0)->name)
-#define DATA(name, type) { #name, type, sizeof2(WINDOW, name), offsetof(WINDOW, name) }
+#define DATA(name, type) { { #name }, type, sizeof2(WINDOW, name), offsetof(WINDOW, name) }
 
-static SCR_PARAMS scr_params[] =
+static const SCR_PARAMS scr_params[] =
 {
     DATA(_cury, pSIZE),
     DATA(_curx, pSIZE),
@@ -161,12 +161,12 @@ static char *
 read_txt(FILE *fp)
 {
     size_t limit = 1024;
-    size_t used = 0;
     char *result = malloc(limit);
     char *buffer;
 
     if (result != 0) {
        int ch = 0;
+       size_t used = 0;
 
        clearerr(fp);
        result[used] = '\0';
@@ -179,6 +179,7 @@ read_txt(FILE *fp)
                    result = 0;
                    break;
                }
+               result = buffer;
            }
            ch = fgetc(fp);
            if (ch == EOF)
@@ -220,16 +221,16 @@ decode_attr(char *source, attr_t *target, int *color)
                ++next;
            } else if (*next == 'C') {
                int value = 0;
+               unsigned pair;
                next++;
                while (isdigit(UChar(*next))) {
                    value = value * 10 + (*next++ - '0');
                }
                *target &= ~A_COLOR;
-               if (value > 256) {
-                   *target |= COLOR_PAIR(255);
-               } else {
-                   *target |= COLOR_PAIR(value);
-               }
+               pair = (unsigned) ((value > 256)
+                                  ? COLOR_PAIR(255)
+                                  : COLOR_PAIR(value));
+               *target |= pair;
                *color = value;
            } else {
                while (isalnum(UChar(*next))) {
@@ -315,7 +316,7 @@ static char *
 decode_chtype(char *source, chtype fillin, chtype *target)
 {
     attr_t attr = ChAttrOf(fillin);
-    int color = PAIR_NUMBER(attr);
+    int color = PAIR_NUMBER((int) attr);
     int value;
 
     T(("decode_chtype '%s'", source));
@@ -351,7 +352,7 @@ decode_cchar(char *source, cchar_t *fillin, cchar_t *target)
     while (source[0] == MARKER && source[1] == APPEND) {
        source += 2;
        source = decode_char(source, &value);
-       if (append++ < CCHARW_MAX) {
+       if (++append < CCHARW_MAX) {
            chars[append] = (wchar_t) value;
        }
     }
@@ -364,9 +365,6 @@ static int
 read_win(WINDOW *win, FILE *fp)
 {
     int code = ERR;
-    char *txt;
-    char *name;
-    char *value;
     size_t n;
     int color;
 #if NCURSES_WIDECHAR
@@ -376,7 +374,10 @@ read_win(WINDOW *win, FILE *fp)
 
     memset(win, 0, sizeof(WINDOW));
     for (;;) {
-       txt = read_txt(fp);
+       char *name;
+       char *value;
+       char *txt = read_txt(fp);
+
        if (txt == 0)
            break;
        if (!strcmp(txt, "rows:")) {
@@ -434,10 +435,13 @@ read_row(char *source, NCURSES_CH_T * prior, NCURSES_CH_T * target, int length)
 {
     while (*source != '\0' && length > 0) {
 #if NCURSES_WIDECHAR
-       int n, len;
+       int len;
+
        source = decode_cchar(source, prior, target);
        len = wcwidth(target->chars[0]);
        if (len > 1) {
+           int n;
+
            SetWidecExt(CHDEREF(target), 0);
            for (n = 1; n < len; ++n) {
                target[n] = target[0];
@@ -489,7 +493,6 @@ NCURSES_EXPORT(WINDOW *)
 NCURSES_SP_NAME(getwin) (NCURSES_SP_DCLx FILE *filep)
 {
     WINDOW tmp, *nwin;
-    int n;
     bool old_format = FALSE;
 
     T((T_CALLED("getwin(%p)"), (void *) filep));
@@ -545,6 +548,7 @@ NCURSES_SP_NAME(getwin) (NCURSES_SP_DCLx FILE *filep)
      * made sense is probably gone.
      */
     if (nwin != 0) {
+       int n;
        size_t linesize = sizeof(NCURSES_CH_T) * (size_t) (tmp._maxx + 1);
 
        nwin->_curx = tmp._curx;
@@ -587,7 +591,7 @@ NCURSES_SP_NAME(getwin) (NCURSES_SP_DCLx FILE *filep)
        }
 #if NCURSES_EXT_PUTWIN
        else {
-           char *txt;
+           char *txt = 0;
            bool success = TRUE;
            NCURSES_CH_T prior = blank;
 
@@ -669,7 +673,7 @@ encode_attr(char *target, attr_t source, attr_t prior)
        if ((source & A_COLOR) != (prior & A_COLOR)) {
            if (!first)
                *target++ = '|';
-           sprintf(target, "C%d", PAIR_NUMBER(source));
+           sprintf(target, "C%d", PAIR_NUMBER((int) source));
            target += strlen(target);
        }
 
@@ -695,7 +699,7 @@ encode_cell(char *target, CARG_CH_T source, CARG_CH_T previous)
     }
 #endif
     for (n = 0; n < SIZEOF(source->chars); ++n) {
-       unsigned uch = source->chars[n];
+       unsigned uch = (unsigned) source->chars[n];
        if (uch == 0)
            continue;
        if (n) {
@@ -735,7 +739,7 @@ encode_cell(char *target, CARG_CH_T source, CARG_CH_T previous)
     target += strlen(target);
     *target++ = MARKER;
     if (ch < 32 || ch >= 127) {
-       sprintf(target, "%03o", ch);
+       sprintf(target, "%03o", UChar(ch));
     } else {
        switch (ch) {
        case ' ':
@@ -746,11 +750,10 @@ encode_cell(char *target, CARG_CH_T source, CARG_CH_T previous)
            *target = '\0';
            break;
        default:
-           sprintf(--target, "%c", ch);
+           sprintf(--target, "%c", UChar(ch));
            break;
        }
     }
-    target += strlen(target);
 #endif
 }
 #endif
@@ -759,7 +762,6 @@ NCURSES_EXPORT(int)
 putwin(WINDOW *win, FILE *filep)
 {
     int code = ERR;
-    int y;
 
     T((T_CALLED("putwin(%p,%p)"), (void *) win, (void *) filep));
 
@@ -768,6 +770,7 @@ putwin(WINDOW *win, FILE *filep)
        const char *version = curses_version();
        char buffer[1024];
        NCURSES_CH_T last_cell;
+       int y;
 
        memset(&last_cell, 0, sizeof(last_cell));
 
@@ -843,15 +846,22 @@ putwin(WINDOW *win, FILE *filep)
                || ferror(filep))
                returnCode(code);
            for (x = 0; x <= win->_maxx; x++) {
+#if NCURSES_WIDECHAR
                int len = wcwidth(data[x].chars[0]);
                encode_cell(buffer, CHREF(data[x]), CHREF(last_cell));
                last_cell = data[x];
                PUTS(buffer);
                if (len > 1)
                    x += (len - 1);
+#else
+               encode_cell(buffer, CHREF(data[x]), CHREF(last_cell));
+               last_cell = data[x];
+               PUTS(buffer);
+#endif
            }
            PUTS("\n");
        }
+       code = OK;
     }
 #else
     /*
@@ -862,6 +872,7 @@ putwin(WINDOW *win, FILE *filep)
      */
     if (win != 0) {
        size_t len = (size_t) (win->_maxx + 1);
+       int y;
 
        clearerr(filep);
        if (fwrite(win, sizeof(WINDOW), (size_t) 1, filep) != 1
@@ -934,7 +945,6 @@ scr_dump(const char *file)
 NCURSES_EXPORT(int)
 NCURSES_SP_NAME(scr_init) (NCURSES_SP_DCLx const char *file)
 {
-    FILE *fp = 0;
     int code = ERR;
 
     T((T_CALLED("scr_init(%p,%s)"), (void *) SP_PARM, _nc_visbuf(file)));
@@ -946,6 +956,8 @@ NCURSES_SP_NAME(scr_init) (NCURSES_SP_DCLx const char *file)
        !(exit_ca_mode && non_rev_rmcup)
 #endif
        ) {
+       FILE *fp = 0;
+
        if (_nc_access(file, R_OK) >= 0
            && (fp = fopen(file, "rb")) != 0) {
            delwin(CurScreen(SP_PARM));