aboutsummaryrefslogtreecommitdiff
path: root/source/Core/InputReader.cpp
blob: cbaa671bcba5265eb0c6cda04749be2cdbab7093 (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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//===-- InputReader.cpp -----------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#include "lldb/lldb-python.h"

#include <string>

#include "lldb/Core/InputReader.h"
#include "lldb/Core/Debugger.h"
#include "lldb/Interpreter/CommandInterpreter.h"

using namespace lldb;
using namespace lldb_private;

InputReader::InputReader (Debugger &debugger) :
    m_debugger (debugger),
    m_callback (NULL),
    m_callback_baton (NULL),
    m_end_token (),
    m_granularity (eInputReaderGranularityInvalid),
    m_done (true),
    m_echo (true),
    m_active (false), 
    m_reader_done (false),
    m_user_input(),
    m_save_user_input(false)
{
}

InputReader::~InputReader ()
{
}

Error
InputReader::Initialize 
(
    Callback callback, 
    void *baton,
    lldb::InputReaderGranularity granularity,
    const char *end_token,
    const char *prompt,
    bool echo
)
{
    Error err;
    m_callback = callback;
    m_callback_baton = baton,
    m_granularity = granularity;
    if (end_token != NULL)
        m_end_token = end_token;
    if (prompt != NULL)
        m_prompt = prompt;
    m_done = true;
    m_echo = echo;

    if (m_granularity == eInputReaderGranularityInvalid)
    {
        err.SetErrorString ("Invalid read token size:  Reader must be initialized with a token size other than 'eInputReaderGranularityInvalid'.");
    }
    else
    if (end_token != NULL && granularity != eInputReaderGranularityInvalid)
    {
        if (granularity == eInputReaderGranularityByte)
        {
            // Check to see if end_token is longer than one byte.
            
            if (strlen (end_token) > 1)
            {
                err.SetErrorString ("Invalid end token:  End token cannot be larger than specified token size (byte).");
            }
        }
        else if (granularity == eInputReaderGranularityWord)
        {
            // Check to see if m_end_token contains any white space (i.e. is multiple words).
            
            const char *white_space = " \t\n";
            size_t pos = m_end_token.find_first_of (white_space);
            if (pos != std::string::npos)
            {
                err.SetErrorString ("Invalid end token:  End token cannot be larger than specified token size (word).");
            }
        }
        else
        {
            // Check to see if m_end_token contains any newlines; cannot handle multi-line end tokens.
            
            size_t pos = m_end_token.find_first_of ('\n');
            if (pos != std::string::npos)
            {
                err.SetErrorString ("Invalid end token:  End token cannot contain a newline.");
            }
        }
    }
    
    m_done = err.Fail();

    return err;
}

size_t
InputReader::HandleRawBytes (const char *bytes, size_t bytes_len)
{
    const char *end_token = NULL;
    
    if (m_end_token.empty() == false)
    {
        end_token = ::strstr (bytes, m_end_token.c_str());
        if (end_token >= bytes + bytes_len)
            end_token = NULL;
    }

    const char *p = bytes;
    const char *end = bytes + bytes_len;

    switch (m_granularity)
    {
    case eInputReaderGranularityInvalid:
        break;

    case eInputReaderGranularityByte:
        while (p < end)
        {
            if (end_token == p)
            {
                p += m_end_token.size();
                SetIsDone(true);
                break;
            }

            if (m_callback (m_callback_baton, *this, eInputReaderGotToken, p, 1) == 0)
                break;
            ++p;
            if (IsDone())
                break;
        }
        // Return how many bytes were handled.
        return p - bytes;
        break;


    case eInputReaderGranularityWord:
        {
            char quote = '\0';
            const char *word_start = NULL;
            bool send_word = false;
            for (; p < end; ++p, send_word = false)
            {
                if (end_token && end_token == p)
                {
                    m_end_token.size();
                    SetIsDone(true);
                    break;
                }

                const char ch = *p;
                if (isspace(ch) && (!quote || (quote == ch && p[-1] != '\\')))
                {
                    // We have a space character or the terminating quote
                    send_word = word_start != NULL;
                    quote = '\0';
                }
                else if (quote)
                {
                    // We are in the middle of a quoted character
                    continue;
                }
                else if (ch == '"' || ch == '\'' || ch == '`')
                    quote = ch;
                else if (word_start == NULL)
                {
                    // We have the first character in a word
                    word_start = p;
                }
                
                if (send_word)
                {
                    const size_t word_len = p - word_start;
                    size_t bytes_handled = m_callback (m_callback_baton, 
                                                       *this, 
                                                       eInputReaderGotToken, 
                                                       word_start,
                                                       word_len);

                    if (bytes_handled != word_len)
                        return word_start - bytes + bytes_handled;
                    
                    if (IsDone())
                        return p - bytes;
                }
            }
        }
        break;


    case eInputReaderGranularityLine:
        {
            const char *line_start = bytes;
            const char *end_line = NULL;
            while (p < end)
            {
                const char ch = *p;
                if (ch == '\n' || ch == '\r')
                {
                    size_t line_length = p - line_start;
                    // Now skip the newline character
                    ++p; 
                    // Skip a complete DOS newline if we run into one
                    if (ch == 0xd && p < end && *p == 0xa)
                        ++p;

                    if (line_start <= end_token && end_token < line_start + line_length)
                    {
                        SetIsDone(true);
                        m_callback (m_callback_baton, 
                                    *this, 
                                    eInputReaderGotToken, 
                                    line_start, 
                                    end_token - line_start);
                        
                        return p - bytes;
                    }

                    size_t bytes_handled = m_callback (m_callback_baton, 
                                                       *this, 
                                                       eInputReaderGotToken, 
                                                       line_start, 
                                                       line_length);

                    end_line = p;

                    if (bytes_handled != line_length)
                    {
                        // The input reader wasn't able to handle all the data
                        return line_start - bytes + bytes_handled;
                    }


                    if (IsDone())
                        return p - bytes;

                    line_start = p;
                }
                else
                {
                    ++p;
                }                
            }
            
            if (end_line)
                return end_line - bytes;
        }
        break;

    
    case eInputReaderGranularityAll:
        {
            // Nothing should be handle unless we see our end token
            if (end_token)
            {
                size_t length = end_token - bytes;
                size_t bytes_handled = m_callback (m_callback_baton, 
                                                   *this, 
                                                   eInputReaderGotToken, 
                                                   bytes, 
                                                   length);
                m_done = true;

                p += bytes_handled + m_end_token.size();

                // Consume any white space, such as newlines, beyond the end token

                while (p < end && isspace(*p))
                    ++p;

                if (bytes_handled != length)
                    return bytes_handled;
                else
                {
                    return p - bytes;
                    //return bytes_handled + m_end_token.size();
                }
            }
            return 0;
        }
        break;
    }
    return 0;
}

const char *
InputReader::GetPrompt () const
{
    if (!m_prompt.empty())
        return m_prompt.c_str();
    else
        return NULL;
}

void
InputReader::RefreshPrompt ()
{
	if (m_debugger.GetCommandInterpreter().GetBatchCommandMode())
        return;
    
    if (!m_prompt.empty())
    {
        File &out_file = m_debugger.GetOutputFile();
        if (out_file.IsValid())
        {
            out_file.Printf ("%s", m_prompt.c_str());
            out_file.Flush();
        }
    }
}

void
InputReader::Notify (InputReaderAction notification)
{
    switch (notification)
    {
    case eInputReaderActivate:
    case eInputReaderReactivate:
        m_active = true;
        m_reader_done.SetValue(false, eBroadcastAlways);
        break;

    case eInputReaderDeactivate:
    case eInputReaderDone:
        m_active = false;
        break;
    
    case eInputReaderAsynchronousOutputWritten:
        break;
        
    case eInputReaderInterrupt:
    case eInputReaderEndOfFile:
        break;
    
    case eInputReaderGotToken:
        return; // We don't notify the tokens here, it is done in HandleRawBytes
    }
    if (m_callback)
        m_callback (m_callback_baton, *this, notification, NULL, 0);
    if (notification == eInputReaderDone)
        m_reader_done.SetValue(true, eBroadcastAlways);
}

void 
InputReader::WaitOnReaderIsDone ()
{
    m_reader_done.WaitForValueEqualTo (true);
}

const char *
InputReader::GranularityAsCString (lldb::InputReaderGranularity granularity)
{
    switch (granularity)
    {
    case eInputReaderGranularityInvalid:  return "invalid";
    case eInputReaderGranularityByte:     return "byte";
    case eInputReaderGranularityWord:     return "word";
    case eInputReaderGranularityLine:     return "line";
    case eInputReaderGranularityAll:      return "all";
    }

    static char unknown_state_string[64];
    snprintf(unknown_state_string, sizeof (unknown_state_string), "InputReaderGranularity = %i", granularity);
    return unknown_state_string;
}

bool
InputReader::HandlerData::GetBatchMode()
{
    return reader.GetDebugger().GetCommandInterpreter().GetBatchCommandMode();
}

lldb::StreamSP
InputReader::HandlerData::GetOutStream()
{
    return reader.GetDebugger().GetAsyncOutputStream();
}