From bug-octave-request at bevo dot che dot wisc dot edu Fri Nov 14 02:05:56 1997 Subject: Interpretation of '\' in strings From: "John W. Eaton" To: Maximilian Pitschi Cc: bug-octave at bevo dot che dot wisc dot edu Date: Fri, 14 Nov 1997 02:03:04 -0600 On 23-Sep-1997, Maximilian Pitschi wrote: | Bug report for Octave 2.0.9 configured for hppa1.1-hp-hpux10.20 | | Description: | ----------- | | Octave with --traditional interprets '\' as an escape character. | In matlab, in normal strings, i.e., no format strings, '\' has no | special meaning. | | Repeat-By: | --------- | | Octave, version 2.0.9 (hppa1.1-hp-hpux10.20). | Copyright (C) 1996, 1997 John W. Eaton. | This is free software with ABSOLUTELY NO WARRANTY. | For details, type `warranty'. | | octave:1> '\[' | warning: unrecognized escape sequence `\[' -- converting to `[' | ans = [ | | or even worse: | | octave:1> '\' | error: unterminated string constant | parse error: | | >>> '\' | ^ | | The examples should return '\[' and '\', respectively. Here is a patch to try. It should turn off all special interpretation of backslash characters in strings, as well as turning off their use as a continuation line marker if you set the new variable `backslash_escapes' to 0. The default value is 1 unless you use the --traditional or --brain-dead options. Unless someone points out a major flaw with this, it should be in 2.0.10. Thanks, jwe Fri Nov 14 01:53:13 1997 John W. Eaton * lex.l (Vbackslash_escapes): New static variable. (backslash_escapes): New function. (do_string_escapes): Return immediately if ! Vbackslash_escapes. (eat_whitespace, eat_continuation): Only call have_continuation if Vbackslash_escapes. (handle_string): Backslash is only special if Vbackslash_escapes. (symbols_of_lex): Add DEFVAR for backslash_escapes. * octave.cc (maximum_braindamage): Set backslash_escapes to 0. *** src/lex.l~ Mon May 19 20:10:10 1997 --- src/lex.l Fri Nov 14 01:46:42 1997 *************** *** 162,167 **** --- 162,170 ---- static int Vwhitespace_in_literal_matrix; + // Should Octave treat backslashes in strings as escapes that + // introduce special characters like newline (\n), tab (\t), etc.? + static bool Vbackslash_escapes; // Forward declarations for functions defined at the bottom of this // file. *************** *** 705,710 **** --- 708,716 ---- static void do_string_escapes (char *s) { + if (! Vbackslash_escapes) + return; + char *p1 = s; char *p2 = s; while (*p2 != '\0') *************** *** 1344,1350 **** break; else { ! if (have_continuation ()) break; else goto done; --- 1350,1356 ---- break; else { ! if (Vbackslash_escapes && have_continuation ()) break; else goto done; *************** *** 1498,1504 **** int retval = ATE_NOTHING; int c = yyinput (); if ((c == '.' && have_ellipsis_continuation ()) ! || (c == '\\' && have_continuation ())) retval = eat_whitespace (); else yyunput (c, yytext); --- 1504,1510 ---- int retval = ATE_NOTHING; int c = yyinput (); if ((c == '.' && have_ellipsis_continuation ()) ! || (c == '\\' && Vbackslash_escapes && have_continuation ())) retval = eat_whitespace (); else yyunput (c, yytext); *************** *** 1518,1524 **** { current_input_column++; ! if (c == '\\') { if (escape_pending) { --- 1524,1530 ---- { current_input_column++; ! if (c == '\\' && Vbackslash_escapes) { if (escape_pending) { *************** *** 1937,1942 **** --- 1943,1956 ---- } int + backslash_escapes (void) + { + Vbackslash_escapes = check_preference ("backslash_escapes"); + + return 0; + } + + int whitespace_in_literal_matrix (void) { int pref = 0; *************** *** 1955,1960 **** --- 1969,1978 ---- void symbols_of_lex (void) { + DEFVAR (backslash_escapes, 1.0, 0, backslash_escapes, + "if nonzero, Octave recognizes backslashes in strings as escapes that\n\ + introduce special characters like newline (\\n), tab (\\t), etc."); + DEFVAR (whitespace_in_literal_matrix, "", 0, whitespace_in_literal_matrix, "control auto-insertion of commas and semicolons in literal matrices"); } *** src/octave.cc~ Mon May 19 20:33:05 1997 --- src/octave.cc Fri Nov 14 01:44:09 1997 *************** *** 374,379 **** --- 374,380 ---- { bind_builtin_variable ("PS1", ">> "); bind_builtin_variable ("PS2", ""); + bind_builtin_variable ("backslash_escapes", 0.0); bind_builtin_variable ("beep_on_error", 1.0); bind_builtin_variable ("default_eval_print_flag", 0.0); bind_builtin_variable ("default_save_format", "mat-binary");