blob: 5d35d60f93218cca8ca9f1e086fc4130527dca4b [file] [log] [blame]
Chris Lattner30fdc8d2010-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 Chen8f3d8382011-08-02 20:52:42 +000016#if defined(__FreeBSD__)
17#include <readline/readline.h>
18#else
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include <editline/readline.h>
Johnny Chen8f3d8382011-08-02 20:52:42 +000020#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +000021#include <histedit.h>
Caroline Ticebd13b8d2010-09-29 18:35:42 +000022#include <pthread.h>
23#include <sys/time.h>
Chris Lattner30fdc8d2010-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 Tice969ed3d2011-05-02 20:41:46 +000042 IOChannel (FILE *editline_in,
43 FILE *editline_out,
Chris Lattner30fdc8d2010-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 Tice969ed3d2011-05-02 20:41:46 +000064 OutWrite (const char *buffer, size_t len, bool asynchronous);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065
66 void
Caroline Tice969ed3d2011-05-02 20:41:46 +000067 ErrWrite (const char *buffer, size_t len, bool asynchronous);
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068
69 bool
70 LibeditGetInput (std::string &);
Caroline Tice969ed3d2011-05-02 20:41:46 +000071
72 static void
73 LibeditOutputBytesReceived (void *baton, const void *src,size_t src_len);
Chris Lattner30fdc8d2010-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
Greg Claytonc982c762010-07-09 20:39:50 +000099 static unsigned char
100 ElCompletionFn (EditLine *e, int ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101
Caroline Ticebd13b8d2010-09-29 18:35:42 +0000102protected:
103
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000104 bool
105 IsGettingCommand () const;
106
Caroline Ticebd13b8d2010-09-29 18:35:42 +0000107 void
108 SetGettingCommand (bool new_value);
109
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110private:
111
Caroline Ticebd13b8d2010-09-29 18:35:42 +0000112 pthread_mutex_t m_output_mutex;
113 struct timeval m_enter_elgets_time;
114
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000115 Driver *m_driver;
116 lldb::thread_t m_read_thread;
117 bool m_read_thread_should_exit;
118 FILE *m_out_file;
119 FILE *m_err_file;
120 std::queue<std::string> m_command_queue;
121 const char *m_completion_key;
122
123 EditLine *m_edit_line;
124 History *m_history;
125 HistEvent m_history_event;
126 bool m_getting_command;
Caroline Tice969ed3d2011-05-02 20:41:46 +0000127 bool m_expecting_prompt;
128 std::string m_prompt_str; // for accumlating the prompt as it gets written out by editline
Caroline Tice86a73f92011-05-03 20:53:11 +0000129 bool m_refresh_request_pending;
Greg Claytonc982c762010-07-09 20:39:50 +0000130
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131 void
132 HistorySaveLoad (bool save);
Greg Claytonc982c762010-07-09 20:39:50 +0000133
134 unsigned char
135 HandleCompletion (EditLine *e, int ch);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000136};
137
Caroline Ticebd13b8d2010-09-29 18:35:42 +0000138class IOLocker
139{
140public:
141
142 IOLocker (pthread_mutex_t &mutex);
143
144 ~IOLocker ();
145
146protected:
147
148 pthread_mutex_t *m_mutex_ptr;
149
150private:
151
152 IOLocker (const IOLocker&);
153 const IOLocker& operator= (const IOLocker&);
154};
155
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000156#endif // lldb_IOChannel_h_