aboutsummaryrefslogtreecommitdiff
path: root/include/lldb/Host/Editline.h
blob: b92de1052f296384fec4b869669a48730ac8250f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//===-- Editline.h ----------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef liblldb_Editline_h_
#define liblldb_Editline_h_
#if defined(__cplusplus)

#include "lldb/lldb-private.h"

#include <stdio.h>
#ifdef _WIN32
#include "lldb/Host/windows/editlinewin.h"
#else
#include <histedit.h>
#endif

#include <string>
#include <vector>

#include "lldb/Host/Condition.h"
#include "lldb/Host/FileSpec.h"
#include "lldb/Host/Mutex.h"

namespace lldb_private {

//----------------------------------------------------------------------
/// @class Editline Editline.h "lldb/Host/Editline.h"
/// @brief A class that encapsulates editline functionality.
//----------------------------------------------------------------------
class Editline
{
public:
    typedef LineStatus (*LineCompletedCallbackType) (
        Editline *editline,
        StringList &lines,
        uint32_t line_idx,
        Error &error,
        void *baton);
    
    typedef int (*CompleteCallbackType) (
        const char *current_line,
        const char *cursor,
        const char *last_char,
        int skip_first_n_matches,
        int max_matches,
        StringList &matches,
        void *baton);

    typedef int (*GetCharCallbackType) (
        ::EditLine *,
        char *c);
    
    Editline(const char *prog,  // Used for the history file and for editrc program name
             const char *prompt,
             FILE *fin,
             FILE *fout,
             FILE *ferr);

    ~Editline();

    Error
    GetLine (std::string &line);

    Error
    GetLines (const std::string &end_line, StringList &lines);

    bool
    LoadHistory ();
    
    bool
    SaveHistory ();
    
    FILE *
    GetInputFile ();
    
    FILE *
    GetOutputFile ();
    
    FILE *
    GetErrorFile ();

    bool
    GettingLine () const
    {
        return m_getting_line;
    }

    void
    Hide ();

    void
    Refresh();

    void
    Interrupt ();

    void
    SetAutoCompleteCallback (CompleteCallbackType callback,
                             void *baton)
    {
        m_completion_callback = callback;
        m_completion_callback_baton = baton;
    }

    void
    SetLineCompleteCallback (LineCompletedCallbackType callback,
                             void *baton)
    {
        m_line_complete_callback = callback;
        m_line_complete_callback_baton = baton;
    }

    size_t
    Push (const char *bytes, size_t len);

    // Cache bytes and use them for input without using a FILE. Calling this function
    // will set the getc callback in the editline
    size_t
    SetInputBuffer (const char *c, size_t len);
    
    static int
    GetCharFromInputFileCallback (::EditLine *e, char *c);

    void
    SetGetCharCallback (GetCharCallbackType callback);
    
    const char *
    GetPrompt();
    
    void
    SetPrompt (const char *p);

private:

    Error
    PrivateGetLine(std::string &line);
    
    FileSpec
    GetHistoryFile();

    unsigned char
    HandleCompletion (int ch);
    
    int
    GetChar (char *c);


    static unsigned char
    CallbackEditPrevLine (::EditLine *e, int ch);
    
    static unsigned char
    CallbackEditNextLine (::EditLine *e, int ch);
    
    static unsigned char
    CallbackComplete (::EditLine *e, int ch);

    static const char *
    GetPromptCallback (::EditLine *e);

    static Editline *
    GetClientData (::EditLine *e);
    
    static FILE *
    GetFilePointer (::EditLine *e, int fd);

    static int
    GetCharInputBufferCallback (::EditLine *e, char *c);

    enum class Command
    {
        None = 0,
        EditPrevLine,
        EditNextLine,
    };
    ::EditLine *m_editline;
    ::History *m_history;
    ::HistEvent m_history_event;
    std::string m_program;
    std::string m_prompt;
    std::string m_lines_prompt;
    std::string m_getc_buffer;
    Mutex m_getc_mutex;
    Condition m_getc_cond;
    CompleteCallbackType m_completion_callback;
    void *m_completion_callback_baton;
//    Mutex m_gets_mutex; // Make sure only one thread
    LineCompletedCallbackType m_line_complete_callback;
    void *m_line_complete_callback_baton;
    Command m_lines_command;
    uint32_t m_lines_curr_line;
    uint32_t m_lines_max_line;
    bool m_prompt_with_line_numbers;
    bool m_getting_line;
    bool m_got_eof;    // Set to true when we detect EOF
    bool m_interrupted;
    
    DISALLOW_COPY_AND_ASSIGN(Editline);
};

} // namespace lldb_private

#endif  // #if defined(__cplusplus)
#endif  // liblldb_Host_h_