| 1 | /* |
| 2 | This file is part of the GVars3 Library. |
| 3 | |
| 4 | Copyright (C) 2005 The Authors |
| 5 | |
| 6 | This library is free software, see LICENSE file for details |
| 7 | */ |
| 8 | |
| 9 | #include "gvars3/instances.h" |
| 10 | #include "src/GUI_impl.h" |
| 11 | #include "gvars3/GUI_readline.h" |
| 12 | |
| 13 | #include <pthread.h> |
| 14 | #include <poll.h> |
| 15 | #include <cstdlib> |
| 16 | #include <readline/readline.h> |
| 17 | #include <readline/history.h> |
| 18 | |
| 19 | using namespace std; |
| 20 | |
| 21 | namespace GVars3 |
| 22 | { |
| 23 | |
| 24 | bool spawn_readline_thread::running=0; |
| 25 | bool spawn_readline_thread::quit=0; |
| 26 | std::string spawn_readline_thread::quit_callback; |
| 27 | |
| 28 | spawn_readline_thread::spawn_readline_thread(const std::string& cb) |
| 29 | { |
| 30 | if(!running) |
| 31 | { |
| 32 | pthread_create(&cmd, 0, proc, 0); |
| 33 | running = 1; |
| 34 | quit=0; |
| 35 | none=0; |
| 36 | quit_callback = cb; |
| 37 | } |
| 38 | else |
| 39 | { |
| 40 | none =1; |
| 41 | std::cerr << "Error: readline thread already running.\n"; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | |
| 46 | int rlhook() |
| 47 | { |
| 48 | return 1; |
| 49 | } |
| 50 | |
| 51 | void* spawn_readline_thread::proc(void *) |
| 52 | { |
| 53 | char* line; |
| 54 | GUI.SetupReadlineCompletion(); |
| 55 | |
| 56 | rl_event_hook = rlhook; |
| 57 | |
| 58 | while(1) |
| 59 | { |
| 60 | line=readline("> "); |
| 61 | |
| 62 | if(quit || !line) |
| 63 | break; |
| 64 | |
| 65 | if(*line) |
| 66 | { |
| 67 | GUI.ParseLine(line); |
| 68 | add_history(line); |
| 69 | }; |
| 70 | } |
| 71 | |
| 72 | GUI.ParseLine(quit_callback); |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | spawn_readline_thread::~spawn_readline_thread() |
| 78 | { |
| 79 | //Terminate the readline and wait for it to finish |
| 80 | if(!none) |
| 81 | { |
| 82 | quit = 1; |
| 83 | rl_done = 1; |
| 84 | rl_stuff_char('\n'); |
| 85 | pthread_join(cmd, 0); |
| 86 | quit_callback = ""; |
| 87 | quit = 0; |
| 88 | running = 0; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | |
| 93 | std::string readline_in_current_thread::quit_callback; |
| 94 | |
| 95 | readline_in_current_thread::~readline_in_current_thread() |
| 96 | { |
| 97 | rl_deprep_terminal(); |
| 98 | } |
| 99 | |
| 100 | readline_in_current_thread::readline_in_current_thread(const string& s) |
| 101 | { |
| 102 | quit_callback = s; |
| 103 | GUI.SetupReadlineCompletion(); |
| 104 | rl_event_hook = rlhook; |
| 105 | rl_set_keyboard_input_timeout(0); |
| 106 | rl_callback_handler_install("> ", lineread); |
| 107 | } |
| 108 | |
| 109 | void readline_in_current_thread::poll() |
| 110 | { |
| 111 | struct pollfd p; |
| 112 | |
| 113 | p.fd = 0; |
| 114 | p.events=POLLIN | POLLHUP; |
| 115 | |
| 116 | if(::poll(&p, 1, 0) > 0) |
| 117 | rl_callback_read_char(); |
| 118 | } |
| 119 | |
| 120 | void readline_in_current_thread::lineread(char* line) |
| 121 | { |
| 122 | if(line == NULL) |
| 123 | { |
| 124 | rl_deprep_terminal(); |
| 125 | GUI.ParseLine(quit_callback); |
| 126 | } |
| 127 | else |
| 128 | { |
| 129 | GUI.ParseLine(line); |
| 130 | if(line != string("")) |
| 131 | add_history(line); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | |
| 136 | char * GUI_impl::ReadlineCommandGeneratorCB(const char *szText, int nState) |
| 137 | { |
| 138 | return mpReadlineCompleterGUI->ReadlineCommandGenerator(szText, nState); |
| 139 | |
| 140 | } |
| 141 | char* GV3ReadlineCommandGenerator(const char *szText, int nState) |
| 142 | { |
| 143 | static vector<string> tags; |
| 144 | static vector<string>::iterator tag_i; |
| 145 | |
| 146 | if(!nState) |
| 147 | { |
| 148 | tags = GV3::tag_list(); |
| 149 | tag_i = tags.begin(); |
| 150 | |
| 151 | }; |
| 152 | |
| 153 | while(tag_i != tags.end()) |
| 154 | { |
| 155 | size_t text_len = strlen(szText); |
| 156 | bool bEqualsAtEnd = false; |
| 157 | if(text_len>0) |
| 158 | if(szText[text_len-1]=='=') |
| 159 | bEqualsAtEnd = true; |
| 160 | string sCurrent = *tag_i; |
| 161 | string sComparison = *tag_i; |
| 162 | if(bEqualsAtEnd) |
| 163 | sComparison = sComparison + "="; |
| 164 | |
| 165 | tag_i++; |
| 166 | |
| 167 | if(strncmp(szText, sComparison.c_str(), text_len) == 0) |
| 168 | { |
| 169 | //If it's a prefect match, complete with the value |
| 170 | string sCompleted; |
| 171 | if(text_len == sComparison.size()) |
| 172 | sCompleted = szText + ((bEqualsAtEnd?"":"=") + GV3::get_var(sCurrent)); |
| 173 | else |
| 174 | sCompleted = sCurrent; |
| 175 | |
| 176 | char* r; |
| 177 | |
| 178 | if((r= (char*)malloc(sCompleted.size() + 1)) == NULL) |
| 179 | return 0; |
| 180 | |
| 181 | strcpy(r, sCompleted.c_str()); |
| 182 | return r; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | |
| 190 | |
| 191 | char * GUI_impl::ReadlineCommandGenerator(const char *szText, int nState) |
| 192 | { |
| 193 | static map<string,CallbackVector>::iterator iRegistered; |
| 194 | static int nTextLength; |
| 195 | static int nOffset; |
| 196 | const char *pcName; |
| 197 | |
| 198 | if(!nState) |
| 199 | { |
| 200 | iRegistered = mmCallBackMap.begin(); |
| 201 | nTextLength = strlen(szText); |
| 202 | nOffset=-1; |
| 203 | } |
| 204 | |
| 205 | while(iRegistered!=mmCallBackMap.end()) |
| 206 | { |
| 207 | pcName = iRegistered->first.c_str(); |
| 208 | iRegistered++; |
| 209 | if(strncmp(pcName,szText,nTextLength) == 0) |
| 210 | { |
| 211 | char *t = (char*) malloc(strlen(pcName)+2); |
| 212 | if(!t) return NULL; |
| 213 | strcpy(t,pcName); |
| 214 | t[strlen(pcName)]=' '; |
| 215 | t[strlen(pcName)+1]=0; |
| 216 | return t; |
| 217 | }; |
| 218 | }; |
| 219 | |
| 220 | if(nOffset==-1) |
| 221 | nOffset = nState; |
| 222 | |
| 223 | return GV3ReadlineCommandGenerator(szText, nState-nOffset); |
| 224 | |
| 225 | }; |
| 226 | |
| 227 | } |