blob: e3c44b58d236f923ab5d2e59e99261002520cf03 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- IOChannel.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 lldb_IOChannel_h_
11#define lldb_IOChannel_h_
12
13#include <string>
14#include <queue>
15
Johnny Chen4b663292011-08-02 20:52:42 +000016#if defined(__FreeBSD__)
17#include <readline/readline.h>
18#else
Chris Lattner24943d22010-06-08 16:52:24 +000019#include <editline/readline.h>
Johnny Chen4b663292011-08-02 20:52:42 +000020#endif
Chris Lattner24943d22010-06-08 16:52:24 +000021#include <histedit.h>
Caroline Tice757500e2010-09-29 18:35:42 +000022#include <pthread.h>
23#include <sys/time.h>
Chris Lattner24943d22010-06-08 16:52:24 +000024
25#include "Driver.h"
26
27class IOChannel : public lldb::SBBroadcaster
28{
29public:
30 enum {
31 eBroadcastBitHasUserInput = (1 << 0),
32 eBroadcastBitUserInterrupt = (1 << 1),
33 eBroadcastBitThreadShouldExit = (1 << 2),
34 eBroadcastBitThreadDidExit = (1 << 3),
35 eBroadcastBitThreadDidStart = (1 << 4),
36 eBroadcastBitsSTDOUT = (1 << 5),
37 eBroadcastBitsSTDERR = (1 << 6),
38 eBroadcastBitsSTDIN = (1 << 7),
39 eAllEventBits = 0xffffffff
40 };
41
Caroline Tice4a348082011-05-02 20:41:46 +000042 IOChannel (FILE *editline_in,
43 FILE *editline_out,
Chris Lattner24943d22010-06-08 16:52:24 +000044 FILE *out,
45 FILE *err,
46 Driver *driver = NULL);
47
48 virtual
49 ~IOChannel ();
50
51 bool
52 Start ();
53
54 bool
55 Stop ();
56
57 static void *
58 IOReadThread (void *);
59
60 void
61 Run ();
62
63 void
Caroline Tice4a348082011-05-02 20:41:46 +000064 OutWrite (const char *buffer, size_t len, bool asynchronous);
Chris Lattner24943d22010-06-08 16:52:24 +000065
66 void
Caroline Tice4a348082011-05-02 20:41:46 +000067 ErrWrite (const char *buffer, size_t len, bool asynchronous);
Chris Lattner24943d22010-06-08 16:52:24 +000068
69 bool
70 LibeditGetInput (std::string &);
Caroline Tice4a348082011-05-02 20:41:46 +000071
72 static void
73 LibeditOutputBytesReceived (void *baton, const void *src,size_t src_len);
Chris Lattner24943d22010-06-08 16:52:24 +000074
75 void
76 SetPrompt ();
77
78 void
79 RefreshPrompt ();
80
81 void
82 AddCommandToQueue (const char *command);
83
84 bool
85 GetCommandFromQueue (std::string &cmd);
86
87 int
88 CommandQueueSize () const;
89
90 void
91 ClearCommandQueue ();
92
93 bool
94 CommandQueueIsEmpty () const;
95
96 const char *
97 GetPrompt ();
98
Johnny Chen8ab2c222012-05-09 21:03:07 +000099 void
100 EraseCharsBeforeCursor ();
101
Greg Clayton54e7afa2010-07-09 20:39:50 +0000102 static unsigned char
103 ElCompletionFn (EditLine *e, int ch);
Chris Lattner24943d22010-06-08 16:52:24 +0000104
Caroline Tice757500e2010-09-29 18:35:42 +0000105protected:
106
Chris Lattner24943d22010-06-08 16:52:24 +0000107 bool
108 IsGettingCommand () const;
109
Caroline Tice757500e2010-09-29 18:35:42 +0000110 void
111 SetGettingCommand (bool new_value);
112
Chris Lattner24943d22010-06-08 16:52:24 +0000113private:
114
Caroline Tice757500e2010-09-29 18:35:42 +0000115 pthread_mutex_t m_output_mutex;
116 struct timeval m_enter_elgets_time;
117
Chris Lattner24943d22010-06-08 16:52:24 +0000118 Driver *m_driver;
119 lldb::thread_t m_read_thread;
120 bool m_read_thread_should_exit;
121 FILE *m_out_file;
122 FILE *m_err_file;
123 std::queue<std::string> m_command_queue;
124 const char *m_completion_key;
125
126 EditLine *m_edit_line;
127 History *m_history;
128 HistEvent m_history_event;
129 bool m_getting_command;
Caroline Tice4a348082011-05-02 20:41:46 +0000130 bool m_expecting_prompt;
131 std::string m_prompt_str; // for accumlating the prompt as it gets written out by editline
Caroline Tice388ca8f2011-05-03 20:53:11 +0000132 bool m_refresh_request_pending;
Greg Clayton54e7afa2010-07-09 20:39:50 +0000133
Chris Lattner24943d22010-06-08 16:52:24 +0000134 void
135 HistorySaveLoad (bool save);
Greg Clayton54e7afa2010-07-09 20:39:50 +0000136
137 unsigned char
138 HandleCompletion (EditLine *e, int ch);
Chris Lattner24943d22010-06-08 16:52:24 +0000139};
140
Caroline Tice757500e2010-09-29 18:35:42 +0000141class IOLocker
142{
143public:
144
145 IOLocker (pthread_mutex_t &mutex);
146
147 ~IOLocker ();
148
149protected:
150
151 pthread_mutex_t *m_mutex_ptr;
152
153private:
154
155 IOLocker (const IOLocker&);
156 const IOLocker& operator= (const IOLocker&);
157};
158
Chris Lattner24943d22010-06-08 16:52:24 +0000159#endif // lldb_IOChannel_h_