Tk Source Code

View Ticket
Login
Ticket UUID: 0aa5e852dc27ba9516858b6bd8d35f203bf8b7de
Title: Accept newline characters as value in Tk option files
Type: Bug Version: 8.5.14 and other
Submitter: anonymous Created on: 2013-09-13 13:30:32
Subsystem: 57. [option] Assigned To: jan.nijtmans
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2013-11-11 10:18:17
Resolution: Fixed Closed By: jan.nijtmans
    Closed on: 2013-11-11 10:18:17
Description:
Tk does not honour newline characters in values specified in a resource file. Traditional X resources allow \n for this. Here is a modification for the function tkOption.c:AddFromString that supports this feature for the option value. This patch works for older Tk versions and for 8.6.X as well!

static int
AddFromString(
    Tcl_Interp *interp,		/* Interpreter to use for reporting results. */
    Tk_Window tkwin,		/* Token for window: options are entered for
				 * this window's main window. */
    char *string,		/* String containing option specifiers. */
    int priority)		/* Priority level to use for options in this
				 * string, such as TK_USER_DEFAULT_PRIO or
				 * TK_INTERACTIVE_PRIO. Must be between 0 and
				 * TK_MAX_PRIO. */
{
    register char *src, *dst;
    char *name, *value;
    int lineNum;

    src = string;
    lineNum = 1;
    while (1) {

	/*
	 * Skip leading white space and empty lines and comment lines, and
	 * check for the end of the spec.
	 */

	while ((*src == ' ') || (*src == '\t')) {
	    src++;
	}
	if ((*src == '#') || (*src == '!')) {
	    do {
		src++;
		if ((src[0] == '\\') && (src[1] == '\n')) {
		    src += 2;
		    lineNum++;
		}
	    } while ((*src != '\n') && (*src != 0));
	}
	if (*src == '\n') {
	    src++;
	    lineNum++;
	    continue;
	}
	if (*src == '\0') {
	    break;
	}

	/*
	 * Parse off the option name, collapsing out backslash-newline
	 * sequences of course.
	 */

	dst = name = src;
	while (*src != ':') {
	    if ((*src == '\0') || (*src == '\n')) {
		char buf[32 + TCL_INTEGER_SPACE];

		sprintf(buf, "missing colon on line %d", lineNum);
		Tcl_SetResult(interp, buf, TCL_VOLATILE);
		return TCL_ERROR;
	    }
	    if ((src[0] == '\\') && (src[1] == '\n')) {
		src += 2;
		lineNum++;
	    } else {
		*dst = *src;
		dst++;
		src++;
	    }
	}

	/*
	 * Eliminate trailing white space on the name, and null-terminate
	 * it.
	 */

	while ((dst != name) && ((dst[-1] == ' ') || (dst[-1] == '\t'))) {
	    dst--;
	}
	*dst = '\0';

	/*
	 * Skip white space between the name and the value.
	 */

	src++;
	while ((*src == ' ') || (*src == '\t')) {
	    src++;
	}
	if (*src == '\0') {
	    char buf[32 + TCL_INTEGER_SPACE];

	    sprintf(buf, "missing value on line %d", lineNum);
	    Tcl_SetResult(interp, buf, TCL_VOLATILE);
	    return TCL_ERROR;
	}

	/*
	 * Parse off the value, squeezing out backslash-newline sequences
	 * along the way.
	 */

	dst = value = src;
	while (*src != '\n') {
	    if (*src == '\0') {
		char buf[32 + TCL_INTEGER_SPACE];

		sprintf(buf, "missing newline on line %d", lineNum);
		Tcl_SetResult(interp, buf, TCL_VOLATILE);
		return TCL_ERROR;
	    }
	    if ((src[0] == '\\') && (src[1] == '\n')) {
		src += 2;
		lineNum++;
/* START CHANGES */
	    } else if ((src[0] == '\\') && (src[1] == 'n')) {
		src += 2;
		*dst++ = '\n';
/* END CHANGES */
	    } else {
		*dst = *src;
		dst++;
		src++;
	    }
	}
	*dst = 0;

	/*
	 * Enter the option into the database.
	 */

	Tk_AddOption(tkwin, name, value, priority);
	src++;
	lineNum++;
    }
    return TCL_OK;
}
User Comments: jan.nijtmans added on 2013-11-11 10:18:17:

Fixed in Tk 8.5.16 [d62b08728a] and 8.6.2 [b01dc9d0ff]


jan.nijtmans added on 2013-11-11 09:16:39:

The syntax of X resources is documented here:

http://www.x.org/archive/X11R6.8.1/doc/X.7.html

To allow a Value to begin with whitespace, the two-character sequence ``\space'' (backslash followed by space) is recognized and replaced by a space character, and the two-character sequence ``\tab'' (backslash followed by horizontal tab) is recognized and replaced by a horizontal tab character. To allow a Value to contain embedded newline characters, the two-character sequence ``\n'' is recognized and replaced by a newline character. To allow a Value to be broken across multiple lines in a text file, the two-character sequence ``\newline'' (backslash followed by newline) is recognized and removed from the value. To allow a Value to contain arbitrary character codes, the four-character sequence ``\nnn'', where each n is a digit character in the range of ``0''-``7'', is recognized and replaced with a single byte that contains the octal value specified by the sequence. Finally, the two-character sequence ``\\'' is recognized and replaced with a single backslash.

This means that it's a bug, not an RFE.