summaryrefslogtreecommitdiffstats
path: root/tools/driver/IOChannel.h
blob: 36653a0c289f5b9bd39ab8b482a170d1fc3fb08d (plain)
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
//===-- IOChannel.h ---------------------------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef lldb_IOChannel_h_
#define lldb_IOChannel_h_

#include <string>
#include <queue>

#if defined(__FreeBSD__)
#include <readline/readline.h>
#else
#include <editline/readline.h>
#endif
#include <histedit.h>
#include <pthread.h>
#include <sys/time.h>

#include "Driver.h"

class IOChannel : public lldb::SBBroadcaster
{
public:
    enum {
        eBroadcastBitHasUserInput     = (1 << 0),
        eBroadcastBitUserInterrupt    = (1 << 1),
        eBroadcastBitThreadShouldExit = (1 << 2),
        eBroadcastBitThreadDidExit    = (1 << 3),
        eBroadcastBitThreadDidStart   = (1 << 4),
        eBroadcastBitsSTDOUT          = (1 << 5),
        eBroadcastBitsSTDERR          = (1 << 6),
        eBroadcastBitsSTDIN           = (1 << 7),
        eAllEventBits                 = 0xffffffff
    };
    
    enum LibeditGetInputResult
    {
        eLibeditGetInputEOF = 0,
        eLibeditGetInputValid = 1,
        eLibeditGetInputEmpty = 2,
        eLibeditGetInputResultError = 4,
        eLibeditGetInputResultUnknown = 0xffffffff
    };

    IOChannel (FILE *editline_in,
               FILE *editline_out,
               FILE *out,
               FILE *err,
               Driver *driver = NULL);

    virtual
    ~IOChannel ();

    bool
    Start ();

    bool
    Stop ();

    static void *
    IOReadThread (void *);

    void
    Run ();

    void
    OutWrite (const char *buffer, size_t len, bool asynchronous);

    void
    ErrWrite (const char *buffer, size_t len, bool asynchronous);

    LibeditGetInputResult
    LibeditGetInput (std::string &);
    
    static void
    LibeditOutputBytesReceived (void *baton, const void *src,size_t src_len);

    void
    SetPrompt ();

    void
    RefreshPrompt ();

    void
    AddCommandToQueue (const char *command);

    bool
    GetCommandFromQueue (std::string &cmd);

    int
    CommandQueueSize () const;

    void
    ClearCommandQueue ();

    bool
    CommandQueueIsEmpty () const;

    const char *
    GetPrompt ();

    bool
    EditLineHasCharacters ();
    
    void
    EraseCharsBeforeCursor ();

    static unsigned char 
    ElCompletionFn (EditLine *e, int ch);
    
    void
    ElResize();

protected:

    bool
    IsGettingCommand () const;

    void
    SetGettingCommand (bool new_value);

private:

    pthread_mutex_t m_output_mutex;
    struct timeval m_enter_elgets_time;

    Driver *m_driver;
    lldb::thread_t m_read_thread;
    bool m_read_thread_should_exit;
    FILE *m_out_file;
    FILE *m_err_file;
    std::queue<std::string> m_command_queue;
    const char *m_completion_key;

    EditLine *m_edit_line;
    History *m_history;
    HistEvent m_history_event;
    bool m_getting_command;
    bool m_expecting_prompt;
	std::string m_prompt_str;  // for accumlating the prompt as it gets written out by editline
    bool m_refresh_request_pending;

    void
    HistorySaveLoad (bool save);

    unsigned char
    HandleCompletion (EditLine *e, int ch);
};

class IOLocker 
{
public:

    IOLocker (pthread_mutex_t &mutex);

    ~IOLocker ();

protected:

    pthread_mutex_t *m_mutex_ptr;

private:

    IOLocker (const IOLocker&);
    const IOLocker& operator= (const IOLocker&);
};

#endif  // lldb_IOChannel_h_
OpenPOWER on IntegriCloud