]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - ncurses/tinfo/lib_tgoto.c
ncurses 6.0 - patch 20170812
[ncurses.git] / ncurses / tinfo / lib_tgoto.c
index 73557eb7cc258ab4833a8ab90a2e7ed0059838cc..31daf443624f4405709a348650a5024c41b4cdd2 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2000 Free Software Foundation, Inc.                        *
+ * Copyright (c) 2000-2008,2012 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            *
@@ -35,7 +35,7 @@
 #include <ctype.h>
 #include <termcap.h>
 
-MODULE_ID("$Id: lib_tgoto.c,v 1.2 2000/09/24 00:19:14 tom Exp $")
+MODULE_ID("$Id: lib_tgoto.c,v 1.16 2012/02/24 02:08:08 tom Exp $")
 
 #if !PURE_TERMINFO
 static bool
@@ -43,20 +43,24 @@ is_termcap(const char *string)
 {
     bool result = TRUE;
 
-    while ((*string != '\0') && result) {
-       if (*string == '%') {
-           switch (*++string) {
-           case 'p':
+    if (string == 0 || *string == '\0') {
+       result = FALSE;         /* tparm() handles empty strings */
+    } else {
+       while ((*string != '\0') && result) {
+           if (*string == '%') {
+               switch (*++string) {
+               case 'p':
+                   result = FALSE;
+                   break;
+               case '\0':
+                   string--;
+                   break;
+               }
+           } else if (string[0] == '$' && string[1] == '<') {
                result = FALSE;
-               break;
-           case '\0':
-               string--;
-               break;
            }
-       } else if (string[0] == '$' && string[1] == '<') {
-           result = FALSE;
+           string++;
        }
-       string++;
     }
     return result;
 }
@@ -84,13 +88,13 @@ tgoto_internal(const char *string, int x, int y)
     while (*string != 0) {
        if ((used + need) > length) {
            length += (used + need);
-           if ((result = _nc_doalloc(result, length)) == 0) {
+           if ((result = typeRealloc(char, length, result)) == 0) {
                length = 0;
                break;
            }
        }
        if (*string == '%') {
-           char *fmt = 0;
+           const char *fmt = 0;
 
            switch (*++string) {
            case '\0':
@@ -108,7 +112,7 @@ tgoto_internal(const char *string, int x, int y)
                *value %= 1000;
                break;
            case '+':
-               *value += (*++string & 0xff);
+               *value += UChar(*++string);
                /* FALLTHRU */
            case '.':
                /*
@@ -124,7 +128,7 @@ tgoto_internal(const char *string, int x, int y)
                        *value = 0200;  /* tputs will treat this as \0 */
                    }
                }
-               result[used++] = *value++;
+               result[used++] = (char) *value++;
                break;
            case '%':
                result[used++] = *string;
@@ -151,11 +155,12 @@ tgoto_internal(const char *string, int x, int y)
                *value = 16 * (*value / 10) + (*value % 10);
                break;
            case 'D':           /* Reverse coding (Delta Data) */
-               *value -= 2 * (*value / 16);
+               *value -= 2 * (*value % 16);
                break;
            }
            if (fmt != 0) {
-               sprintf(result + used, fmt, *value++);
+               _nc_SPRINTF(result + used, _nc_SLIMIT(length - used)
+                           fmt, *value++);
                used += strlen(result + used);
                fmt = 0;
            }
@@ -168,11 +173,13 @@ tgoto_internal(const char *string, int x, int y)
        }
        string++;
     }
-    if (need_BC) {
-       strcpy(result + used, BC);
-       used += strlen(BC);
+    if (result != 0) {
+       if (need_BC) {
+           _nc_STRCPY(result + used, BC, length - used);
+           used += strlen(BC);
+       }
+       result[used] = '\0';
     }
-    result[used] = '\0';
     return result;
 }
 #endif
@@ -181,7 +188,7 @@ tgoto_internal(const char *string, int x, int y)
  * Retained solely for upward compatibility.  Note the intentional reversing of
  * the last two arguments when invoking tparm().
  */
-char *
+NCURSES_EXPORT(char *)
 tgoto(const char *string, int x, int y)
 {
     char *result;
@@ -192,6 +199,6 @@ tgoto(const char *string, int x, int y)
        result = tgoto_internal(string, x, y);
     else
 #endif
-       result = tparm((NCURSES_CONST char *) string, y, x);
+       result = TPARM_2((NCURSES_CONST char *) string, y, x);
     returnPtr(result);
 }