1153486Sphk/******************************************************************************* 2153486Sphk * $Revision: 1.1.1.1 $ 3153486Sphk * $Date: 2023/03/21 16:41:16 $ 4153486Sphk * $Author: christos $ 5153486Sphk * 6153486Sphk * Contents: A streambuf which uses the GNU readline library for line I/O 7153486Sphk * (c) 2001 by Dimitris Vyzovitis [vyzo@media.mit.edu] 8153486Sphk * 9153486Sphk * This program is free software; you can redistribute it and/or modify 10153486Sphk * it under the terms of the GNU General Public License as published by 11153486Sphk * the Free Software Foundation; either version 2 of the License, or 12153486Sphk * (at your option) any later version. 13153486Sphk * 14153486Sphk * This program is distributed in the hope that it will be useful, 15153486Sphk * but WITHOUT ANY WARRANTY; without even the implied warranty of 16153486Sphk * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17153486Sphk * General Public License for more details. 18153486Sphk * 19153486Sphk * You should have received a copy of the GNU General Public 20153486Sphk * License along with this program; if not, write to the Free 21153486Sphk * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, 22153486Sphk * MA 02111-1307 USA 23153486Sphk * 24153486Sphk ******************************************************************************/ 25153486Sphk 26153486Sphk#ifndef _READLINEBUF_H_ 27153486Sphk#define _READLINEBUF_H_ 28153486Sphk 29153486Sphk#include <iostream> 30153486Sphk#include <cstring> 31153486Sphk#include <cassert> 32153486Sphk#include <cstdlib> 33153486Sphk#include <cstdio> 34153486Sphk 35153486Sphk#include <readline/readline.h> 36153486Sphk#include <readline/history.h> 37153486Sphk 38153486Sphk#if (defined __GNUC__) && (__GNUC__ < 3) 39153486Sphk#include <streambuf.h> 40153486Sphk#else 41153486Sphk#include <streambuf> 42153486Sphkusing std::streamsize; 43153486Sphkusing std::streambuf; 44153486Sphk#endif 45153486Sphk 46153486Sphkclass readlinebuf : public streambuf { 47153486Sphkpublic: 48153486Sphk#if (defined __GNUC__) && (__GNUC__ < 3) 49153486Sphk typedef char char_type; 50153486Sphk typedef int int_type; 51153486Sphk typedef streampos pos_type; 52153486Sphk typedef streamoff off_type; 53153486Sphk#endif 54153486Sphk static const int_type eof = EOF; // this is -1 55153486Sphk static const int_type not_eof = 0; 56153486Sphk 57153486Sphkprivate: 58153486Sphk const char* prompt_; 59153486Sphk bool history_; 60153486Sphk char* line_; 61153486Sphk int low_; 62153486Sphk int high_; 63153486Sphk 64153486Sphkprotected: 65153486Sphk 66153486Sphk virtual int_type showmanyc() const { return high_ - low_; } 67153486Sphk 68153486Sphk virtual streamsize xsgetn( char_type* buf, streamsize n ) { 69153486Sphk int rd = n > (high_ - low_)? (high_ - low_) : n; 70153486Sphk memcpy( buf, line_, rd ); 71153486Sphk low_ += rd; 72153486Sphk 73153486Sphk if ( rd < n ) { 74153486Sphk low_ = high_ = 0; 75153486Sphk free( line_ ); // free( NULL ) is a noop 76153486Sphk line_ = readline( prompt_ ); 77153486Sphk if ( line_ ) { 78153486Sphk high_ = strlen( line_ ); 79153486Sphk if ( history_ && high_ ) add_history( line_ ); 80153486Sphk rd += xsgetn( buf + rd, n - rd ); 81153486Sphk } 82153486Sphk } 83153486Sphk 84153486Sphk return rd; 85153486Sphk } 86153486Sphk 87153486Sphk virtual int_type underflow() { 88153486Sphk if ( high_ == low_ ) { 89153486Sphk low_ = high_ = 0; 90153486Sphk free( line_ ); // free( NULL ) is a noop 91153486Sphk line_ = readline( prompt_ ); 92153486Sphk if ( line_ ) { 93153486Sphk high_ = strlen( line_ ); 94153486Sphk if ( history_ && high_ ) add_history( line_ ); 95153486Sphk } 96153486Sphk } 97153486Sphk 98153486Sphk if ( low_ < high_ ) return line_[low_]; 99153486Sphk else return eof; 100153486Sphk } 101153486Sphk 102153486Sphk virtual int_type uflow() { 103153486Sphk int_type c = underflow(); 104153486Sphk if ( c != eof ) ++low_; 105153486Sphk return c; 106153486Sphk } 107153486Sphk 108153486Sphk virtual int_type pbackfail( int_type c = eof ) { 109153486Sphk if ( low_ > 0 ) --low_; 110153486Sphk else if ( c != eof ) { 111153486Sphk if ( high_ > 0 ) { 112153486Sphk char* nl = (char*)realloc( line_, high_ + 1 ); 113153486Sphk if ( nl ) { 114153486Sphk line_ = (char*)memcpy( nl + 1, line_, high_ ); 115153486Sphk high_ += 1; 116153486Sphk line_[0] = char( c ); 117153486Sphk } else return eof; 118153486Sphk } else { 119153486Sphk assert( !line_ ); 120153486Sphk line_ = (char*)malloc( sizeof( char ) ); 121153486Sphk *line_ = char( c ); 122153486Sphk high_ = 1; 123153486Sphk } 124153486Sphk } else return eof; 125153486Sphk 126153486Sphk return not_eof; 127153486Sphk } 128153486Sphk 129153486Sphkpublic: 130153486Sphk readlinebuf( const char* prompt = NULL, bool history = true ) 131153486Sphk : prompt_( prompt ), history_( history ), 132153486Sphk line_( NULL ), low_( 0 ), high_( 0 ) { 133153486Sphk setbuf( 0, 0 ); 134153486Sphk } 135153486Sphk 136153486Sphk 137153486Sphk}; 138153486Sphk 139153486Sphk#endif 140153486Sphk