# HG changeset patch # User John W. Eaton # Date 1361772255 18000 # Node ID 24b3800d30e77f796ed05edef1bb006c026accb6 # Parent 2f4fa62089b30146aedac9d9f4a2ba8d2fac23f8 move octave_read to lex.ll * lex.ll (octave_read): Move here from input.cc and make static. Return YY_NULL when no more characters are available. Error here if eof is not set when no more characters are available instead of in YY_INPUT macro. * input.h, input.cc (get_user_input): Now extern. diff -r 2f4fa62089b3 -r 24b3800d30e7 libinterp/interpfcn/input.cc --- a/libinterp/interpfcn/input.cc Mon Feb 25 00:45:35 2013 -0500 +++ b/libinterp/interpfcn/input.cc Mon Feb 25 01:04:15 2013 -0500 @@ -343,7 +343,7 @@ // Read a line from the input stream. -static std::string +std::string get_user_input (bool& eof) { octave_quit (); @@ -372,67 +372,6 @@ return retval; } -int -octave_read (char *buf, unsigned max_size) -{ - // FIXME -- is this a safe way to buffer the input? - - static const char * const eol = "\n"; - static std::string input_buf; - static const char *pos = 0; - static size_t chars_left = 0; - static bool eof = false; - - int status = 0; - - if (chars_left == 0) - { - pos = 0; - - input_buf = get_user_input (eof); - - chars_left = input_buf.length (); - - pos = input_buf.c_str (); - } - - if (chars_left > 0) - { - size_t len = max_size > chars_left ? chars_left : max_size; - assert (len > 0); - - memcpy (buf, pos, len); - - chars_left -= len; - pos += len; - - // Make sure input ends with a new line character. - if (chars_left == 0 && buf[len-1] != '\n') - { - if (len < max_size) - { - // There is enough room to plug the newline character in - // the buffer. - buf[len++] = '\n'; - } - else - { - // There isn't enough room to plug the newline character - // in the buffer so make sure it is returned on the next - // octave_read call. - pos = eol; - chars_left = 1; - } - } - - status = len; - } - else - status = eof ? 0 : -1; - - return status; -} - // Fix things up so that input can come from file 'name', printing a // warning if the file doesn't exist. diff -r 2f4fa62089b3 -r 24b3800d30e7 libinterp/interpfcn/input.h --- a/libinterp/interpfcn/input.h Mon Feb 25 00:45:35 2013 -0500 +++ b/libinterp/interpfcn/input.h Mon Feb 25 01:04:15 2013 -0500 @@ -35,8 +35,11 @@ class octave_value; -extern OCTINTERP_API int octave_read (char *buf, unsigned max_size); -extern OCTINTERP_API FILE *get_input_from_file (const std::string& name, int warn = 1); +extern OCTINTERP_API std::string get_user_input (bool& eof); + +extern OCTINTERP_API FILE *get_input_from_file (const std::string& name, + int warn = 1); + extern OCTINTERP_API FILE *get_input_from_stdin (void); // Global pointer for eval(). diff -r 2f4fa62089b3 -r 24b3800d30e7 libinterp/parse-tree/lex.ll --- a/libinterp/parse-tree/lex.ll Mon Feb 25 00:45:35 2013 -0500 +++ b/libinterp/parse-tree/lex.ll Mon Feb 25 01:04:15 2013 -0500 @@ -103,8 +103,7 @@ #undef YY_INPUT #endif #define YY_INPUT(buf, result, max_size) \ - if ((result = octave_read (buf, max_size)) < 0) \ - YY_FATAL_ERROR ("octave_read () in flex scanner failed"); + result = octave_read (buf, max_size) // Try to avoid crashing out completely on fatal scanner errors. // The call to yy_fatal_error should never happen, but it avoids a @@ -322,6 +321,7 @@ static void scan_for_comments (const char *); static yum_yum eat_whitespace (void); static yum_yum eat_continuation (void); +static int octave_read (char *buf, unsigned int max_size); static void maybe_warn_separator_insert (char sep); static void gripe_single_quote_string (void); static void gripe_matlab_incompatible (const std::string& msg); @@ -3550,6 +3550,70 @@ BEGIN (FUNCTION_FILE_BEGIN); } +static int +octave_read (char *buf, unsigned max_size) +{ + static const char * const eol = "\n"; + static std::string input_buf; + static const char *pos = 0; + static size_t chars_left = 0; + static bool eof = false; + + int status = 0; + + if (chars_left == 0) + { + pos = 0; + + input_buf = get_user_input (eof); + + chars_left = input_buf.length (); + + pos = input_buf.c_str (); + } + + if (chars_left > 0) + { + size_t len = max_size > chars_left ? chars_left : max_size; + assert (len > 0); + + memcpy (buf, pos, len); + + chars_left -= len; + pos += len; + + // Make sure input ends with a new line character. + if (chars_left == 0 && buf[len-1] != '\n') + { + if (len < max_size) + { + // There is enough room to plug the newline character in + // the buffer. + buf[len++] = '\n'; + } + else + { + // There isn't enough room to plug the newline character + // in the buffer so make sure it is returned on the next + // octave_read call. + pos = eol; + chars_left = 1; + } + } + + status = len; + } + else + { + status = YY_NULL; + + if (! eof) + YY_FATAL_ERROR ("octave_read () in flex scanner failed"); + } + + return status; +} + static void maybe_warn_separator_insert (char sep) {