Chris Lattner | 30fdc8d | 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 | |
| 16 | #include <editline/readline.h> |
| 17 | #include <histedit.h> |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 18 | #include <pthread.h> |
| 19 | #include <sys/time.h> |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 20 | |
| 21 | #include "Driver.h" |
| 22 | |
| 23 | class IOChannel : public lldb::SBBroadcaster |
| 24 | { |
| 25 | public: |
| 26 | enum { |
| 27 | eBroadcastBitHasUserInput = (1 << 0), |
| 28 | eBroadcastBitUserInterrupt = (1 << 1), |
| 29 | eBroadcastBitThreadShouldExit = (1 << 2), |
| 30 | eBroadcastBitThreadDidExit = (1 << 3), |
| 31 | eBroadcastBitThreadDidStart = (1 << 4), |
| 32 | eBroadcastBitsSTDOUT = (1 << 5), |
| 33 | eBroadcastBitsSTDERR = (1 << 6), |
| 34 | eBroadcastBitsSTDIN = (1 << 7), |
| 35 | eAllEventBits = 0xffffffff |
| 36 | }; |
| 37 | |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 38 | IOChannel (FILE *editline_in, |
| 39 | FILE *editline_out, |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 40 | FILE *out, |
| 41 | FILE *err, |
| 42 | Driver *driver = NULL); |
| 43 | |
| 44 | virtual |
| 45 | ~IOChannel (); |
| 46 | |
| 47 | bool |
| 48 | Start (); |
| 49 | |
| 50 | bool |
| 51 | Stop (); |
| 52 | |
| 53 | static void * |
| 54 | IOReadThread (void *); |
| 55 | |
| 56 | void |
| 57 | Run (); |
| 58 | |
| 59 | void |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 60 | OutWrite (const char *buffer, size_t len, bool asynchronous); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | |
| 62 | void |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 63 | ErrWrite (const char *buffer, size_t len, bool asynchronous); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 64 | |
| 65 | bool |
| 66 | LibeditGetInput (std::string &); |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 67 | |
| 68 | static void |
| 69 | LibeditOutputBytesReceived (void *baton, const void *src,size_t src_len); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 70 | |
| 71 | void |
| 72 | SetPrompt (); |
| 73 | |
| 74 | void |
| 75 | RefreshPrompt (); |
| 76 | |
| 77 | void |
| 78 | AddCommandToQueue (const char *command); |
| 79 | |
| 80 | bool |
| 81 | GetCommandFromQueue (std::string &cmd); |
| 82 | |
| 83 | int |
| 84 | CommandQueueSize () const; |
| 85 | |
| 86 | void |
| 87 | ClearCommandQueue (); |
| 88 | |
| 89 | bool |
| 90 | CommandQueueIsEmpty () const; |
| 91 | |
| 92 | const char * |
| 93 | GetPrompt (); |
| 94 | |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 95 | static unsigned char |
| 96 | ElCompletionFn (EditLine *e, int ch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 97 | |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 98 | protected: |
| 99 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 100 | bool |
| 101 | IsGettingCommand () const; |
| 102 | |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 103 | void |
| 104 | SetGettingCommand (bool new_value); |
| 105 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | private: |
| 107 | |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 108 | pthread_mutex_t m_output_mutex; |
| 109 | struct timeval m_enter_elgets_time; |
| 110 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 111 | Driver *m_driver; |
| 112 | lldb::thread_t m_read_thread; |
| 113 | bool m_read_thread_should_exit; |
| 114 | FILE *m_out_file; |
| 115 | FILE *m_err_file; |
| 116 | std::queue<std::string> m_command_queue; |
| 117 | const char *m_completion_key; |
| 118 | |
| 119 | EditLine *m_edit_line; |
| 120 | History *m_history; |
| 121 | HistEvent m_history_event; |
| 122 | bool m_getting_command; |
Caroline Tice | 969ed3d | 2011-05-02 20:41:46 +0000 | [diff] [blame] | 123 | bool m_expecting_prompt; |
| 124 | std::string m_prompt_str; // for accumlating the prompt as it gets written out by editline |
Caroline Tice | 86a73f9 | 2011-05-03 20:53:11 +0000 | [diff] [blame] | 125 | bool m_refresh_request_pending; |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 126 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 127 | void |
| 128 | HistorySaveLoad (bool save); |
Greg Clayton | c982c76 | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 129 | |
| 130 | unsigned char |
| 131 | HandleCompletion (EditLine *e, int ch); |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
Caroline Tice | bd13b8d | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 134 | class IOLocker |
| 135 | { |
| 136 | public: |
| 137 | |
| 138 | IOLocker (pthread_mutex_t &mutex); |
| 139 | |
| 140 | ~IOLocker (); |
| 141 | |
| 142 | protected: |
| 143 | |
| 144 | pthread_mutex_t *m_mutex_ptr; |
| 145 | |
| 146 | private: |
| 147 | |
| 148 | IOLocker (const IOLocker&); |
| 149 | const IOLocker& operator= (const IOLocker&); |
| 150 | }; |
| 151 | |
Chris Lattner | 30fdc8d | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 152 | #endif // lldb_IOChannel_h_ |