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 | |
| 16 | #include <editline/readline.h> |
| 17 | #include <histedit.h> |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 18 | #include <pthread.h> |
| 19 | #include <sys/time.h> |
Chris Lattner | 24943d2 | 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 | |
| 38 | IOChannel (FILE *in, |
| 39 | FILE *out, |
| 40 | FILE *err, |
| 41 | Driver *driver = NULL); |
| 42 | |
| 43 | virtual |
| 44 | ~IOChannel (); |
| 45 | |
| 46 | bool |
| 47 | Start (); |
| 48 | |
| 49 | bool |
| 50 | Stop (); |
| 51 | |
| 52 | static void * |
| 53 | IOReadThread (void *); |
| 54 | |
| 55 | void |
| 56 | Run (); |
| 57 | |
| 58 | void |
| 59 | OutWrite (const char *buffer, size_t len); |
| 60 | |
| 61 | void |
| 62 | ErrWrite (const char *buffer, size_t len); |
| 63 | |
| 64 | bool |
| 65 | LibeditGetInput (std::string &); |
| 66 | |
| 67 | void |
| 68 | SetPrompt (); |
| 69 | |
| 70 | void |
| 71 | RefreshPrompt (); |
| 72 | |
| 73 | void |
| 74 | AddCommandToQueue (const char *command); |
| 75 | |
| 76 | bool |
| 77 | GetCommandFromQueue (std::string &cmd); |
| 78 | |
| 79 | int |
| 80 | CommandQueueSize () const; |
| 81 | |
| 82 | void |
| 83 | ClearCommandQueue (); |
| 84 | |
| 85 | bool |
| 86 | CommandQueueIsEmpty () const; |
| 87 | |
| 88 | const char * |
| 89 | GetPrompt (); |
| 90 | |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 91 | static unsigned char |
| 92 | ElCompletionFn (EditLine *e, int ch); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 93 | |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 94 | protected: |
| 95 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 96 | bool |
| 97 | IsGettingCommand () const; |
| 98 | |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 99 | void |
| 100 | SetGettingCommand (bool new_value); |
| 101 | |
| 102 | uint64_t |
| 103 | Nanoseconds (const struct timeval &time_val) const; |
| 104 | |
| 105 | uint64_t |
| 106 | ElapsedNanoSecondsSinceEnteringElGets (); |
| 107 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | private: |
| 109 | |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 110 | pthread_mutex_t m_output_mutex; |
| 111 | struct timeval m_enter_elgets_time; |
| 112 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 113 | Driver *m_driver; |
| 114 | lldb::thread_t m_read_thread; |
| 115 | bool m_read_thread_should_exit; |
| 116 | FILE *m_out_file; |
| 117 | FILE *m_err_file; |
| 118 | std::queue<std::string> m_command_queue; |
| 119 | const char *m_completion_key; |
| 120 | |
| 121 | EditLine *m_edit_line; |
| 122 | History *m_history; |
| 123 | HistEvent m_history_event; |
| 124 | bool m_getting_command; |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 126 | void |
| 127 | HistorySaveLoad (bool save); |
Greg Clayton | 54e7afa | 2010-07-09 20:39:50 +0000 | [diff] [blame] | 128 | |
| 129 | unsigned char |
| 130 | HandleCompletion (EditLine *e, int ch); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
Caroline Tice | 757500e | 2010-09-29 18:35:42 +0000 | [diff] [blame] | 133 | class IOLocker |
| 134 | { |
| 135 | public: |
| 136 | |
| 137 | IOLocker (pthread_mutex_t &mutex); |
| 138 | |
| 139 | ~IOLocker (); |
| 140 | |
| 141 | protected: |
| 142 | |
| 143 | pthread_mutex_t *m_mutex_ptr; |
| 144 | |
| 145 | private: |
| 146 | |
| 147 | IOLocker (const IOLocker&); |
| 148 | const IOLocker& operator= (const IOLocker&); |
| 149 | }; |
| 150 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 151 | #endif // lldb_IOChannel_h_ |