1//===-- Editline.h ----------------------------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef liblldb_Editline_h_
11#define liblldb_Editline_h_
12#if defined(__cplusplus)
13
14#include "lldb/lldb-private.h"
15
16#include <stdio.h>
17#ifdef _WIN32
18#include "lldb/Host/windows/editlinewin.h"
19#else
20#include <histedit.h>
21#endif
22
23#include <string>
24#include <vector>
25
26#include "lldb/Host/Condition.h"
27#include "lldb/Host/FileSpec.h"
28#include "lldb/Host/Mutex.h"
29
30namespace lldb_private {
31
32//----------------------------------------------------------------------
33/// @class Editline Editline.h "lldb/Host/Editline.h"
34/// @brief A class that encapsulates editline functionality.
35//----------------------------------------------------------------------
36class Editline
37{
38public:
39    typedef LineStatus (*LineCompletedCallbackType) (
40        Editline *editline,
41        StringList &lines,
42        uint32_t line_idx,
43        Error &error,
44        void *baton);
45
46    typedef int (*CompleteCallbackType) (
47        const char *current_line,
48        const char *cursor,
49        const char *last_char,
50        int skip_first_n_matches,
51        int max_matches,
52        StringList &matches,
53        void *baton);
54
55    typedef int (*GetCharCallbackType) (
56        ::EditLine *,
57        char *c);
58
59    Editline(const char *prog,  // Used for the history file and for editrc program name
60             const char *prompt,
61             FILE *fin,
62             FILE *fout,
63             FILE *ferr);
64
65    ~Editline();
66
67    Error
68    GetLine (std::string &line);
69
70    Error
71    GetLines (const std::string &end_line, StringList &lines);
72
73    bool
74    LoadHistory ();
75
76    bool
77    SaveHistory ();
78
79    FILE *
80    GetInputFile ();
81
82    FILE *
83    GetOutputFile ();
84
85    FILE *
86    GetErrorFile ();
87
88    bool
89    GettingLine () const
90    {
91        return m_getting_line;
92    }
93
94    void
95    Hide ();
96
97    void
98    Refresh();
99
100    void
101    Interrupt ();
102
103    void
104    SetAutoCompleteCallback (CompleteCallbackType callback,
105                             void *baton)
106    {
107        m_completion_callback = callback;
108        m_completion_callback_baton = baton;
109    }
110
111    void
112    SetLineCompleteCallback (LineCompletedCallbackType callback,
113                             void *baton)
114    {
115        m_line_complete_callback = callback;
116        m_line_complete_callback_baton = baton;
117    }
118
119    size_t
120    Push (const char *bytes, size_t len);
121
122    // Cache bytes and use them for input without using a FILE. Calling this function
123    // will set the getc callback in the editline
124    size_t
125    SetInputBuffer (const char *c, size_t len);
126
127    static int
128    GetCharFromInputFileCallback (::EditLine *e, char *c);
129
130    void
131    SetGetCharCallback (GetCharCallbackType callback);
132
133    const char *
134    GetPrompt();
135
136    void
137    SetPrompt (const char *p);
138
139private:
140
141    Error
142    PrivateGetLine(std::string &line);
143
144    FileSpec
145    GetHistoryFile();
146
147    unsigned char
148    HandleCompletion (int ch);
149
150    int
151    GetChar (char *c);
152
153
154    static unsigned char
155    CallbackEditPrevLine (::EditLine *e, int ch);
156
157    static unsigned char
158    CallbackEditNextLine (::EditLine *e, int ch);
159
160    static unsigned char
161    CallbackComplete (::EditLine *e, int ch);
162
163    static const char *
164    GetPromptCallback (::EditLine *e);
165
166    static Editline *
167    GetClientData (::EditLine *e);
168
169    static FILE *
170    GetFilePointer (::EditLine *e, int fd);
171
172    static int
173    GetCharInputBufferCallback (::EditLine *e, char *c);
174
175    enum class Command
176    {
177        None = 0,
178        EditPrevLine,
179        EditNextLine,
180    };
181    ::EditLine *m_editline;
182    ::History *m_history;
183    ::HistEvent m_history_event;
184    std::string m_program;
185    std::string m_prompt;
186    std::string m_lines_prompt;
187    std::string m_getc_buffer;
188    Mutex m_getc_mutex;
189    Condition m_getc_cond;
190    CompleteCallbackType m_completion_callback;
191    void *m_completion_callback_baton;
192//    Mutex m_gets_mutex; // Make sure only one thread
193    LineCompletedCallbackType m_line_complete_callback;
194    void *m_line_complete_callback_baton;
195    Command m_lines_command;
196    uint32_t m_lines_curr_line;
197    uint32_t m_lines_max_line;
198    bool m_prompt_with_line_numbers;
199    bool m_getting_line;
200    bool m_got_eof;    // Set to true when we detect EOF
201    bool m_interrupted;
202
203    DISALLOW_COPY_AND_ASSIGN(Editline);
204};
205
206} // namespace lldb_private
207
208#endif  // #if defined(__cplusplus)
209#endif  // liblldb_Host_h_
210