]> www.ginac.de Git - ginac.git/blobdiff - ginsh/ginsh_lexer.lpp
[BUGFIX] Fix crash in parser.
[ginac.git] / ginsh / ginsh_lexer.lpp
index 7fbc513cc5857604283b5c41c12451d62bb54dc5..449ce6b5c44234dbab7ca977d6142313f9abb36d 100644 (file)
@@ -4,7 +4,7 @@
  *  This file must be processed with flex. */
 
 /*
  *  This file must be processed with flex. */
 
 /*
- *  GiNaC Copyright (C) 1999-2014 Johannes Gutenberg University Mainz, Germany
+ *  GiNaC Copyright (C) 1999-2024 Johannes Gutenberg University Mainz, Germany
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
@@ -26,6 +26,8 @@
  *  Definitions
  */
 
  *  Definitions
  */
 
+%option nounput
+
 %pointer
 
 %{
 %pointer
 
 %{
@@ -36,6 +38,9 @@
 #include "ginsh.h"
 #include "ginsh_parser.hpp"
 
 #include "ginsh.h"
 #include "ginsh_parser.hpp"
 
+using namespace std;
+using namespace GiNaC;
+
 #define YY_INPUT(buf, result, max_size) (result = ginsh_input(buf, max_size))
 
 // Table of all used symbols
 #define YY_INPUT(buf, result, max_size) (result = ginsh_input(buf, max_size))
 
 // Table of all used symbols
@@ -142,7 +147,7 @@ real_symbols    return T_REAL_SYMBOLS;
  */
 
 static int line_length = 0;
  */
 
 static int line_length = 0;
-static char *line_read = NULL;
+static char *line_read = nullptr;
 static char *line_ptr;
 
 // Input function that uses libreadline for interactive input
 static char *line_ptr;
 
 // Input function that uses libreadline for interactive input
@@ -198,15 +203,23 @@ static int ginsh_input(char *buf, int max_size)
                        YY_FATAL_ERROR("input in flex scanner failed");
                result = n;
 #endif
                        YY_FATAL_ERROR("input in flex scanner failed");
                result = n;
 #endif
-       } else if (((result = fread(buf, 1, max_size, yyin)) == 0) && ferror(yyin))
-               YY_FATAL_ERROR("input in flex scanner failed");
+       } else {
+               int c = '*', n;
+               for (n = 0; n < max_size && (c = getc(yyin)) != EOF && c != '\n'; ++n)
+                       buf[n] = (char)c;
+               if (c == '\n')
+                       buf[n++] = (char)c;
+               if (c == EOF && ferror(yyin))
+                       YY_FATAL_ERROR("input in flex scanner failed");
+               result = n;
+       }
 
        return result;
 }
 
 // List of input files to be processed
 int num_files = 0;
 
        return result;
 }
 
 // List of input files to be processed
 int num_files = 0;
-char **file_list = NULL;
+char **file_list = nullptr;
 
 // EOF encountered, connect to next file. If this was the last file,
 // connect to stdin. If this was stdin, terminate the scanner.
 
 // EOF encountered, connect to next file. If this was the last file,
 // connect to stdin. If this was stdin, terminate the scanner.
@@ -218,7 +231,7 @@ int yywrap()
        fclose(yyin);
        if (num_files) {
                yyin = fopen(*file_list, "r");
        fclose(yyin);
        if (num_files) {
                yyin = fopen(*file_list, "r");
-               if (yyin == NULL) {
+               if (yyin == nullptr) {
                        cerr << "Can't open " << *file_list << endl;
                        return 1;
                }
                        cerr << "Can't open " << *file_list << endl;
                        return 1;
                }