X-Git-Url: https://ncurses.scripts.mit.edu/?p=ncurses.git;a=blobdiff_plain;f=ncurses%2Ftinfo%2Fcaptoinfo.c;h=eec8a133bbdeb8a8bd3c1c1da93cdc58fc96324c;hp=84d43a9cd49ad6fc40e4b69249d59a186cb55ae3;hb=11dead39816318fe9601e27756b9497caf7ff490;hpb=55ccd2b959766810cf7db8d1c4462f338ce0afc8 diff --git a/ncurses/tinfo/captoinfo.c b/ncurses/tinfo/captoinfo.c index 84d43a9c..eec8a133 100644 --- a/ncurses/tinfo/captoinfo.c +++ b/ncurses/tinfo/captoinfo.c @@ -1,5 +1,5 @@ /**************************************************************************** - * Copyright (c) 1998-2003,2004 Free Software Foundation, Inc. * + * Copyright (c) 1998-2009,2010 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 * @@ -93,7 +93,7 @@ #include #include -MODULE_ID("$Id: captoinfo.c,v 1.47 2005/06/04 22:06:43 tom Exp $") +MODULE_ID("$Id: captoinfo.c,v 1.57 2010/04/18 00:14:49 tom Exp $") #define MAX_PUSHED 16 /* max # args we can push onto the stack */ @@ -125,10 +125,10 @@ init_string(void) static char * save_string(char *d, const char *const s) { - size_t have = (d - my_string); + size_t have = (size_t) (d - my_string); size_t need = have + strlen(s) + 2; if (need > my_length) { - my_string = (char *) realloc(my_string, my_length = (need + need)); + my_string = (char *) _nc_doalloc(my_string, my_length = (need + need)); if (my_string == 0) _nc_err_abort(MSG_NO_MEMORY); d = my_string + have; @@ -137,7 +137,7 @@ save_string(char *d, const char *const s) return d + strlen(d); } -static inline char * +static NCURSES_INLINE char * save_char(char *s, int c) { static char temp[2]; @@ -149,7 +149,7 @@ static void push(void) /* push onstack on to the stack */ { - if (stackptr > MAX_PUSHED) + if (stackptr >= MAX_PUSHED) _nc_warning("string too complex to convert"); else stack[stackptr++] = onstack; @@ -183,7 +183,7 @@ cvtchar(register const char *sp) case '$': case '\\': case '%': - c = *sp; + c = (unsigned char) (*sp); len = 2; break; case '\0': @@ -196,22 +196,22 @@ cvtchar(register const char *sp) case '3': len = 1; while (isdigit(UChar(*sp))) { - c = 8 * c + (*sp++ - '0'); + c = (unsigned char) (8 * c + (*sp++ - '0')); len++; } break; default: - c = *sp; + c = (unsigned char) (*sp); len = 2; break; } break; case '^': - c = (*++sp & 0x1f); + c = (unsigned char) (*++sp & 0x1f); len = 2; break; default: - c = *sp; + c = (unsigned char) (*sp); len = 1; } if (isgraph(c) && c != ',' && c != '\'' && c != '\\' && c != ':') { @@ -666,8 +666,27 @@ _nc_infotocap(const char *cap GCC_UNUSED, const char *str, int const parameteriz int c1, c2; char *cp = 0; - if (str[0] == '\\' && (str[1] == '^' || str[1] == ',')) { - bufptr = save_char(bufptr, *++str); + if (str[0] == '^') { + if (str[1] == '\0' || (str + 1) == trimmed) { + bufptr = save_string(bufptr, "\\136"); + ++str; + } else { + bufptr = save_char(bufptr, *str++); + bufptr = save_char(bufptr, *str); + } + } else if (str[0] == '\\') { + if (str[1] == '\0' || (str + 1) == trimmed) { + bufptr = save_string(bufptr, "\\134"); + ++str; + } else if (str[1] == '^') { + bufptr = save_string(bufptr, "\\136"); + ++str; + } else if (str[1] == ',') { + bufptr = save_char(bufptr, *++str); + } else { + bufptr = save_char(bufptr, *str++); + bufptr = save_char(bufptr, *str); + } } else if (str[0] == '$' && str[1] == '<') { /* discard padding */ str += 2; while (isdigit(UChar(*str)) @@ -788,6 +807,11 @@ _nc_infotocap(const char *cap GCC_UNUSED, const char *str, int const parameteriz } /* endswitch (*str) */ } /* endelse (*str == '%') */ + /* + * 'str' always points to the end of what was scanned in this step, + * but that may not be the end of the string. + */ + assert(str != 0); if (*str == '\0') break; @@ -839,7 +863,9 @@ main(int argc, char *argv[]) NCURSES_EXPORT(void) _nc_captoinfo_leaks(void) { - FreeAndNull(my_string); + if (my_string != 0) { + FreeAndNull(my_string); + } my_length = 0; } #endif