fixed bash/dash/sh issue (Ubuntu)
[zpugcc/jano.git] / toolchain / gdb / readline / examples / readlinebuf.h
blobd5e515ddc727f961400ee0fc66cf114c2640f279
1 /*******************************************************************************
2 * $Revision: 163 $
3 * $Date: 2004-10-15 22:38:47 +0200 (Fri, 15 Oct 2004) $
4 * $Author: oyvind $
6 * Contents: A streambuf which uses the GNU readline library for line I/O
7 * (c) 2001 by Dimitris Vyzovitis [vyzo@media.mit.edu]
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22 * MA 02111-1307 USA
24 ******************************************************************************/
26 #ifndef _READLINEBUF_H_
27 #define _READLINEBUF_H_
29 #include <iostream>
30 #include <cstring>
31 #include <cassert>
32 #include <cstdlib>
33 #include <cstdio>
35 #include <readline/readline.h>
36 #include <readline/history.h>
38 #if (defined __GNUC__) && (__GNUC__ < 3)
39 #include <streambuf.h>
40 #else
41 #include <streambuf>
42 using std::streamsize;
43 using std::streambuf;
44 #endif
46 class readlinebuf : public streambuf {
47 public:
48 #if (defined __GNUC__) && (__GNUC__ < 3)
49 typedef char char_type;
50 typedef int int_type;
51 typedef streampos pos_type;
52 typedef streamoff off_type;
53 #endif
54 static const int_type eof = EOF; // this is -1
55 static const int_type not_eof = 0;
57 private:
58 const char* prompt_;
59 bool history_;
60 char* line_;
61 int low_;
62 int high_;
64 protected:
66 virtual int_type showmanyc() const { return high_ - low_; }
68 virtual streamsize xsgetn( char_type* buf, streamsize n ) {
69 int rd = n > (high_ - low_)? (high_ - low_) : n;
70 memcpy( buf, line_, rd );
71 low_ += rd;
73 if ( rd < n ) {
74 low_ = high_ = 0;
75 free( line_ ); // free( NULL ) is a noop
76 line_ = readline( prompt_ );
77 if ( line_ ) {
78 high_ = strlen( line_ );
79 if ( history_ && high_ ) add_history( line_ );
80 rd += xsgetn( buf + rd, n - rd );
84 return rd;
87 virtual int_type underflow() {
88 if ( high_ == low_ ) {
89 low_ = high_ = 0;
90 free( line_ ); // free( NULL ) is a noop
91 line_ = readline( prompt_ );
92 if ( line_ ) {
93 high_ = strlen( line_ );
94 if ( history_ && high_ ) add_history( line_ );
98 if ( low_ < high_ ) return line_[low_];
99 else return eof;
102 virtual int_type uflow() {
103 int_type c = underflow();
104 if ( c != eof ) ++low_;
105 return c;
108 virtual int_type pbackfail( int_type c = eof ) {
109 if ( low_ > 0 ) --low_;
110 else if ( c != eof ) {
111 if ( high_ > 0 ) {
112 char* nl = (char*)realloc( line_, high_ + 1 );
113 if ( nl ) {
114 line_ = (char*)memcpy( nl + 1, line_, high_ );
115 high_ += 1;
116 line_[0] = char( c );
117 } else return eof;
118 } else {
119 assert( !line_ );
120 line_ = (char*)malloc( sizeof( char ) );
121 *line_ = char( c );
122 high_ = 1;
124 } else return eof;
126 return not_eof;
129 public:
130 readlinebuf( const char* prompt = NULL, bool history = true )
131 : prompt_( prompt ), history_( history ),
132 line_( NULL ), low_( 0 ), high_( 0 ) {
133 setbuf( 0, 0 );
139 #endif