]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/base/lib_screen.c
ncurses 6.0 - patch 20150725
[ncurses.git] / ncurses / base / lib_screen.c
index 63ef7f5bcf1737ececa1c1f8297a3f69ca0b9e6a..a03ceecef691b50836fb4cd0c7fa6924e32f7589 100644 (file)
 #define CUR SP_TERMTYPE
 #endif
 
-MODULE_ID("$Id: lib_screen.c,v 1.64 2015/03/21 23:59:32 tom Exp $")
+MODULE_ID("$Id: lib_screen.c,v 1.77 2015/07/04 22:54:14 tom Exp $")
 
 #define MAX_SIZE 0x3fff                /* 16k is big enough for a window or pad */
 
 #define MARKER '\\'
+#define APPEND '+'
 #define GUTTER '|'
 #define L_CURL '{'
 #define R_CURL '}'
 
-#define L_MARK "\\{"
-#define R_MARK "}"
-
 /*
  * Use 0x8888 as the magic number for new-format files, since it cannot be
  * 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_SCREEN_DUMP
+#if NCURSES_EXT_PUTWIN
 typedef enum {
     pINT                       /* int */
     ,pSHORT                    /* short */
@@ -75,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),
@@ -113,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),
@@ -181,6 +179,7 @@ read_txt(FILE *fp)
                    result = 0;
                    break;
                }
+               result = buffer;
            }
            ch = fgetc(fp);
            if (ch == EOF)
@@ -222,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))) {
@@ -266,10 +265,12 @@ decode_char(char *source, int *target)
     T(("decode_char   '%s'", source));
     *target = ' ';
     switch (*source) {
-    case '\\':
+    case MARKER:
        switch (*++source) {
-       case '\\':
-           *target = '\\';
+       case APPEND:
+           break;
+       case MARKER:
+           *target = MARKER;
            ++source;
            break;
        case 's':
@@ -315,13 +316,14 @@ 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));
     source = decode_attr(source, &attr, &color);
     source = decode_char(source, &value);
-    *target = ChCharOf(value) | attr | COLOR_PAIR(color);
+    *target = (ChCharOf(value) | attr | (chtype) COLOR_PAIR(color));
+    /* FIXME - ignore combining characters */
     return source;
 }
 
@@ -332,6 +334,8 @@ decode_cchar(char *source, cchar_t *fillin, cchar_t *target)
     int color;
     attr_t attr = fillin->attr;
     wchar_t chars[CCHARW_MAX];
+    int append = 0;
+    int value = 0;
 
     T(("decode_cchar  '%s'", source));
     *target = blank;
@@ -342,8 +346,16 @@ decode_cchar(char *source, cchar_t *fillin, cchar_t *target)
 #endif
     source = decode_attr(source, &attr, &color);
     memset(chars, 0, sizeof(chars));
-    source = decode_char(source, &chars[0]);
-    /* FIXME - handle combining characters at this point */
+    source = decode_char(source, &value);
+    chars[0] = (wchar_t) value;
+    /* handle combining characters */
+    while (source[0] == MARKER && source[1] == APPEND) {
+       source += 2;
+       source = decode_char(source, &value);
+       if (++append < CCHARW_MAX) {
+           chars[append] = (wchar_t) value;
+       }
+    }
     setcchar(target, chars, attr, (short) color, NULL);
     return source;
 }
@@ -423,7 +435,18 @@ 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;
        source = decode_cchar(source, prior, target);
+       len = wcwidth(target->chars[0]);
+       if (len > 1) {
+           SetWidecExt(CHDEREF(target), 0);
+           for (n = 1; n < len; ++n) {
+               target[n] = target[0];
+               SetWidecExt(CHDEREF(target), n);
+           }
+           target += (len - 1);
+           length -= (len - 1);
+       }
 #else
        source = decode_chtype(source, *prior, target);
 #endif
@@ -437,7 +460,7 @@ read_row(char *source, NCURSES_CH_T * prior, NCURSES_CH_T * target, int length)
     /* FIXME - see what error conditions should apply if I need to return ERR */
     return 0;
 }
-#endif /* NCURSES_EXT_SCREEN_DUMP */
+#endif /* NCURSES_EXT_PUTWIN */
 
 /*
  * Originally, getwin/putwin used fread/fwrite, because they used binary data.
@@ -487,7 +510,7 @@ NCURSES_SP_NAME(getwin) (NCURSES_SP_DCLx FILE *filep)
      * If this is a new-format file, and we do not support it, give up.
      */
     if (!memcmp(&tmp, my_magic, 4)) {
-#if NCURSES_EXT_SCREEN_DUMP
+#if NCURSES_EXT_PUTWIN
        if (read_win(&tmp, filep) < 0)
 #endif
            returnWin(0);
@@ -563,7 +586,7 @@ NCURSES_SP_NAME(getwin) (NCURSES_SP_DCLx FILE *filep)
                }
            }
        }
-#if NCURSES_EXT_SCREEN_DUMP
+#if NCURSES_EXT_PUTWIN
        else {
            char *txt;
            bool success = TRUE;
@@ -616,17 +639,20 @@ getwin(FILE *filep)
 }
 #endif
 
-#if NCURSES_EXT_SCREEN_DUMP
+#if NCURSES_EXT_PUTWIN
 static void
 encode_attr(char *target, attr_t source, attr_t prior)
 {
+    source &= ~A_CHARTEXT;
+    prior &= ~A_CHARTEXT;
+
     *target = '\0';
     if (source != prior) {
        size_t n;
        bool first = TRUE;
 
-       strcpy(target, L_MARK);
-       target += strlen(target);
+       *target++ = MARKER;
+       *target++ = L_CURL;
 
        for (n = 0; n < SIZEOF(scr_attrs); ++n) {
            if ((source & scr_attrs[n].attr) != 0 ||
@@ -644,11 +670,12 @@ 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);
        }
 
-       strcpy(target, R_MARK);
+       *target++ = R_CURL;
+       *target = '\0';
     }
 }
 
@@ -665,28 +692,35 @@ encode_cell(char *target, CARG_CH_T source, CARG_CH_T previous)
     target += strlen(target);
 #if NCURSES_EXT_COLORS
     if (previous->ext_color != source->ext_color) {
-       sprintf(target, "%sC%d%s", L_MARK, source->ext_color, R_MARK);
+       sprintf(target, "%c%cC%d%c", MARKER, L_CURL, source->ext_color, R_CURL);
     }
 #endif
     for (n = 0; n < SIZEOF(source->chars); ++n) {
-       if (source->chars[n] == 0)
+       unsigned uch = (unsigned) source->chars[n];
+       if (uch == 0)
            continue;
-       if (source->chars[n] > 0xffff) {
-           sprintf(target, "\\U%08x", source->chars[n]);
-       } else if (source->chars[n] > 0xff) {
-           sprintf(target, "\\u%04x", source->chars[n]);
-       } else if (source->chars[n] < 32 || source->chars[n] >= 127) {
-           sprintf(target, "\\%03o", source->chars[n] & 0xff);
+       if (n) {
+           *target++ = MARKER;
+           *target++ = APPEND;
+       }
+       *target++ = MARKER;
+       if (uch > 0xffff) {
+           sprintf(target, "U%08x", uch);
+       } else if (uch > 0xff) {
+           sprintf(target, "u%04x", uch);
+       } else if (uch < 32 || uch >= 127) {
+           sprintf(target, "%03o", uch & 0xff);
        } else {
-           switch (source->chars[n]) {
+           switch (uch) {
            case ' ':
-               strcpy(target, "\\s");
+               strcpy(target, "s");
                break;
-           case '\\':
-               strcpy(target, "\\\\");
+           case MARKER:
+               *target++ = MARKER;
+               *target = '\0';
                break;
            default:
-               sprintf(target, "%c", source->chars[n]);
+               sprintf(--target, "%c", uch);
                break;
            }
        }
@@ -700,18 +734,20 @@ encode_cell(char *target, CARG_CH_T source, CARG_CH_T previous)
        encode_attr(target, AttrOfD(source), AttrOfD(previous));
     }
     target += strlen(target);
+    *target++ = MARKER;
     if (ch < 32 || ch >= 127) {
-       sprintf(target, "\\%03o", ch);
+       sprintf(target, "%03o", UChar(ch));
     } else {
        switch (ch) {
        case ' ':
-           strcpy(target, "\\s");
+           strcpy(target, "s");
            break;
-       case '\\':
-           strcpy(target, "\\\\");
+       case MARKER:
+           *target++ = MARKER;
+           *target = '\0';
            break;
        default:
-           sprintf(target, "%c", ch);
+           sprintf(--target, "%c", UChar(ch));
            break;
        }
     }
@@ -728,7 +764,7 @@ putwin(WINDOW *win, FILE *filep)
 
     T((T_CALLED("putwin(%p,%p)"), (void *) win, (void *) filep));
 
-#if NCURSES_EXT_SCREEN_DUMP
+#if NCURSES_EXT_PUTWIN
     if (win != 0) {
        const char *version = curses_version();
        char buffer[1024];
@@ -808,9 +844,18 @@ 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");
        }