Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | //===-- 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 Chen | 4b66329 | 2011-08-02 20:52:42 +0000 | [diff] [blame] | 16 | #if defined(__FreeBSD__) |
| 17 | #include <readline/readline.h> |
| 18 | #else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 19 | #include <editline/readline.h> |
Johnny Chen | 4b66329 | 2011-08-02 20:52:42 +0000 | [diff] [blame] | 20 | #endif |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 21 | #include <histedit.h> |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 22 | #include <pthread.h> |
| 23 | #include <sys/time.h> |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 24 | |
| 25 | #include "Driver.h" |
| 26 | |
| 27 | class IOChannel : public lldb::SBBroadcaster |
| 28 | { |
| 29 | public: |
| 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 Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 42 | IOChannel (FILE *editline_in, |
| 43 | FILE *editline_out, |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 44 | 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 Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 64 | OutWrite (const char *buffer, size_t len, bool asynchronous); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 65 | |
| 66 | void |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 67 | ErrWrite (const char *buffer, size_t len, bool asynchronous); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 68 | |
| 69 | bool |
| 70 | LibeditGetInput (std::string &); |
Caroline Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 71 | |
| 72 | static void |
| 73 | LibeditOutputBytesReceived (void *baton, const void *src,size_t src_len); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 74 | |
| 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 Chen | 8ab2c22 | 2012-05-09 21:03:07 +0000 | [diff] [blame] | 99 | void |
| 100 | EraseCharsBeforeCursor (); |
| 101 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 102 | static unsigned char |
| 103 | ElCompletionFn (EditLine *e, int ch); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 104 | |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 105 | protected: |
| 106 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 107 | bool |
| 108 | IsGettingCommand () const; |
| 109 | |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 110 | void |
| 111 | SetGettingCommand (bool new_value); |
| 112 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 113 | private: |
| 114 | |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 115 | pthread_mutex_t m_output_mutex; |
| 116 | struct timeval m_enter_elgets_time; |
| 117 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 118 | 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 Tice | 4a34808 | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 130 | bool m_expecting_prompt; |
| 131 | std::string m_prompt_str; // for accumlating the prompt as it gets written out by editline |
Caroline Tice | 388ca8f | 2011-05-03 20:53:11 +0000 | [diff] [blame] | 132 | bool m_refresh_request_pending; |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 134 | void |
| 135 | HistorySaveLoad (bool save); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 136 | |
| 137 | unsigned char |
| 138 | HandleCompletion (EditLine *e, int ch); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 139 | }; |
| 140 | |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 141 | class IOLocker |
| 142 | { |
| 143 | public: |
| 144 | |
| 145 | IOLocker (pthread_mutex_t &mutex); |
| 146 | |
| 147 | ~IOLocker (); |
| 148 | |
| 149 | protected: |
| 150 | |
| 151 | pthread_mutex_t *m_mutex_ptr; |
| 152 | |
| 153 | private: |
| 154 | |
| 155 | IOLocker (const IOLocker&); |
| 156 | const IOLocker& operator= (const IOLocker&); |
| 157 | }; |
| 158 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 159 | #endif // lldb_IOChannel_h_ |