/****************************************************************************
- * Copyright (c) 1998-2001,2002 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 *
/****************************************************************************
* Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
* and: Eric S. Raymond <esr@snark.thyrsus.com> *
+ * and: Thomas E. Dickey 1996-on *
****************************************************************************/
/*
*
* There is just one entry point:
*
- * char *_nc_captoinfo(n, s, parametrized)
+ * char *_nc_captoinfo(n, s, parameterized)
*
* Convert value s for termcap string capability named n into terminfo
* format.
#include <ctype.h>
#include <tic.h>
-MODULE_ID("$Id: captoinfo.c,v 1.43 2002/09/28 16:38:59 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 */
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;
return d + strlen(d);
}
-static inline char *
+static NCURSES_INLINE char *
save_char(char *s, int c)
{
static char temp[2];
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;
case '$':
case '\\':
case '%':
- c = *sp;
+ c = (unsigned char) (*sp);
len = 2;
break;
case '\0':
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 != ':') {
* Convert a termcap string to terminfo format.
* 'cap' is the relevant terminfo capability index.
* 's' is the string value of the capability.
- * 'parametrized' tells what type of translations to do:
+ * 'parameterized' tells what type of translations to do:
* % translations if 1
* pad translations if >=0
*/
-char *
-_nc_captoinfo(const char *cap, const char *s, int const parametrized)
+NCURSES_EXPORT(char *)
+_nc_captoinfo(const char *cap, const char *s, int const parameterized)
{
const char *capstart;
capstart = 0;
if (s == 0)
s = "";
- if (parametrized >= 0 && isdigit(UChar(*s)))
+ if (parameterized >= 0 && isdigit(UChar(*s)))
for (capstart = s;; s++)
if (!(isdigit(UChar(*s)) || *s == '*' || *s == '.'))
break;
switch (*s) {
case '%':
s++;
- if (parametrized < 1) {
+ if (parameterized < 1) {
dp = save_char(dp, '%');
break;
}
* Convert a terminfo string to termcap format. Parameters are as in
* _nc_captoinfo().
*/
-char *
-_nc_infotocap(const char *cap GCC_UNUSED, const char *str, int const parametrized)
+NCURSES_EXPORT(char *)
+_nc_infotocap(const char *cap GCC_UNUSED, const char *str, int const parameterized)
{
int seenone = 0, seentwo = 0, saw_m = 0, saw_n = 0;
const char *padding;
/* we may have to move some trailing mandatory padding up front */
padding = str + strlen(str) - 1;
- if (*padding == '>' && *--padding == '/') {
+ if (padding > str && *padding == '>' && *--padding == '/') {
--padding;
while (isdigit(UChar(*padding)) || *padding == '.' || *padding == '*')
padding--;
- if (*padding == '<' && *--padding == '$')
+ if (padding > str && *padding == '<' && *--padding == '$')
trimmed = padding;
padding += 2;
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))
--str;
} else if (str[0] == '%' && str[1] == '%') { /* escaped '%' */
bufptr = save_string(bufptr, "%%");
- } else if (*str != '%' || (parametrized < 1)) {
+ ++str;
+ } else if (*str != '%' || (parameterized < 1)) {
bufptr = save_char(bufptr, *str);
} else if (sscanf(str, "%%?%%{%d}%%>%%t%%{%d}%%+%%;", &c1, &c2) == 2) {
str = strchr(str, ';');
} /* 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;
}
#endif /* MAIN */
-/* captoinfo.c ends here */
+#if NO_LEAKS
+NCURSES_EXPORT(void)
+_nc_captoinfo_leaks(void)
+{
+ if (my_string != 0) {
+ FreeAndNull(my_string);
+ }
+ my_length = 0;
+}
+#endif