bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU readline utility |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 24 | |
Peter Maydell | aafd758 | 2016-01-29 17:49:55 +0000 | [diff] [blame] | 25 | #include "qemu/osdep.h" |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 26 | #include "qemu-common.h" |
Stefan Hajnoczi | 0150cd8 | 2013-11-14 11:54:15 +0100 | [diff] [blame] | 27 | #include "qemu/readline.h" |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 28 | |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 29 | #define IS_NORM 0 |
| 30 | #define IS_ESC 1 |
| 31 | #define IS_CSI 2 |
Kevin Wolf | d34dc45 | 2013-05-14 12:15:54 +0200 | [diff] [blame] | 32 | #define IS_SS3 3 |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 33 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 34 | void readline_show_prompt(ReadLineState *rs) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 35 | { |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 36 | rs->printf_func(rs->opaque, "%s", rs->prompt); |
| 37 | rs->flush_func(rs->opaque); |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 38 | rs->last_cmd_buf_index = 0; |
| 39 | rs->last_cmd_buf_size = 0; |
| 40 | rs->esc_state = IS_NORM; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 41 | } |
| 42 | |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 43 | /* update the displayed command line */ |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 44 | static void readline_update(ReadLineState *rs) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 45 | { |
| 46 | int i, delta, len; |
| 47 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 48 | if (rs->cmd_buf_size != rs->last_cmd_buf_size || |
| 49 | memcmp(rs->cmd_buf, rs->last_cmd_buf, rs->cmd_buf_size) != 0) { |
| 50 | for(i = 0; i < rs->last_cmd_buf_index; i++) { |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 51 | rs->printf_func(rs->opaque, "\033[D"); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 52 | } |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 53 | rs->cmd_buf[rs->cmd_buf_size] = '\0'; |
| 54 | if (rs->read_password) { |
| 55 | len = strlen(rs->cmd_buf); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 56 | for(i = 0; i < len; i++) |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 57 | rs->printf_func(rs->opaque, "*"); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 58 | } else { |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 59 | rs->printf_func(rs->opaque, "%s", rs->cmd_buf); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 60 | } |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 61 | rs->printf_func(rs->opaque, "\033[K"); |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 62 | memcpy(rs->last_cmd_buf, rs->cmd_buf, rs->cmd_buf_size); |
| 63 | rs->last_cmd_buf_size = rs->cmd_buf_size; |
| 64 | rs->last_cmd_buf_index = rs->cmd_buf_size; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 65 | } |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 66 | if (rs->cmd_buf_index != rs->last_cmd_buf_index) { |
| 67 | delta = rs->cmd_buf_index - rs->last_cmd_buf_index; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 68 | if (delta > 0) { |
| 69 | for(i = 0;i < delta; i++) { |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 70 | rs->printf_func(rs->opaque, "\033[C"); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 71 | } |
| 72 | } else { |
| 73 | delta = -delta; |
| 74 | for(i = 0;i < delta; i++) { |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 75 | rs->printf_func(rs->opaque, "\033[D"); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 78 | rs->last_cmd_buf_index = rs->cmd_buf_index; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 79 | } |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 80 | rs->flush_func(rs->opaque); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 81 | } |
| 82 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 83 | static void readline_insert_char(ReadLineState *rs, int ch) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 84 | { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 85 | if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE) { |
| 86 | memmove(rs->cmd_buf + rs->cmd_buf_index + 1, |
| 87 | rs->cmd_buf + rs->cmd_buf_index, |
| 88 | rs->cmd_buf_size - rs->cmd_buf_index); |
| 89 | rs->cmd_buf[rs->cmd_buf_index] = ch; |
| 90 | rs->cmd_buf_size++; |
| 91 | rs->cmd_buf_index++; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 95 | static void readline_backward_char(ReadLineState *rs) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 96 | { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 97 | if (rs->cmd_buf_index > 0) { |
| 98 | rs->cmd_buf_index--; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 102 | static void readline_forward_char(ReadLineState *rs) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 103 | { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 104 | if (rs->cmd_buf_index < rs->cmd_buf_size) { |
| 105 | rs->cmd_buf_index++; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 109 | static void readline_delete_char(ReadLineState *rs) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 110 | { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 111 | if (rs->cmd_buf_index < rs->cmd_buf_size) { |
| 112 | memmove(rs->cmd_buf + rs->cmd_buf_index, |
| 113 | rs->cmd_buf + rs->cmd_buf_index + 1, |
| 114 | rs->cmd_buf_size - rs->cmd_buf_index - 1); |
| 115 | rs->cmd_buf_size--; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 119 | static void readline_backspace(ReadLineState *rs) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 120 | { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 121 | if (rs->cmd_buf_index > 0) { |
| 122 | readline_backward_char(rs); |
| 123 | readline_delete_char(rs); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 127 | static void readline_backword(ReadLineState *rs) |
ths | 33fa11d | 2007-06-25 11:36:50 +0000 | [diff] [blame] | 128 | { |
| 129 | int start; |
| 130 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 131 | if (rs->cmd_buf_index == 0 || rs->cmd_buf_index > rs->cmd_buf_size) { |
ths | 33fa11d | 2007-06-25 11:36:50 +0000 | [diff] [blame] | 132 | return; |
| 133 | } |
| 134 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 135 | start = rs->cmd_buf_index - 1; |
ths | 33fa11d | 2007-06-25 11:36:50 +0000 | [diff] [blame] | 136 | |
| 137 | /* find first word (backwards) */ |
| 138 | while (start > 0) { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 139 | if (!qemu_isspace(rs->cmd_buf[start])) { |
ths | 33fa11d | 2007-06-25 11:36:50 +0000 | [diff] [blame] | 140 | break; |
| 141 | } |
| 142 | |
| 143 | --start; |
| 144 | } |
| 145 | |
| 146 | /* find first space (backwards) */ |
| 147 | while (start > 0) { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 148 | if (qemu_isspace(rs->cmd_buf[start])) { |
ths | 33fa11d | 2007-06-25 11:36:50 +0000 | [diff] [blame] | 149 | ++start; |
| 150 | break; |
| 151 | } |
| 152 | |
| 153 | --start; |
| 154 | } |
| 155 | |
| 156 | /* remove word */ |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 157 | if (start < rs->cmd_buf_index) { |
| 158 | memmove(rs->cmd_buf + start, |
| 159 | rs->cmd_buf + rs->cmd_buf_index, |
| 160 | rs->cmd_buf_size - rs->cmd_buf_index); |
| 161 | rs->cmd_buf_size -= rs->cmd_buf_index - start; |
| 162 | rs->cmd_buf_index = start; |
ths | 33fa11d | 2007-06-25 11:36:50 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 166 | static void readline_bol(ReadLineState *rs) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 167 | { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 168 | rs->cmd_buf_index = 0; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 169 | } |
| 170 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 171 | static void readline_eol(ReadLineState *rs) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 172 | { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 173 | rs->cmd_buf_index = rs->cmd_buf_size; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 174 | } |
| 175 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 176 | static void readline_up_char(ReadLineState *rs) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 177 | { |
| 178 | int idx; |
| 179 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 180 | if (rs->hist_entry == 0) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 181 | return; |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 182 | if (rs->hist_entry == -1) { |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 183 | /* Find latest entry */ |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 184 | for (idx = 0; idx < READLINE_MAX_CMDS; idx++) { |
| 185 | if (rs->history[idx] == NULL) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 186 | break; |
| 187 | } |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 188 | rs->hist_entry = idx; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 189 | } |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 190 | rs->hist_entry--; |
| 191 | if (rs->hist_entry >= 0) { |
| 192 | pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf), |
| 193 | rs->history[rs->hist_entry]); |
| 194 | rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 198 | static void readline_down_char(ReadLineState *rs) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 199 | { |
aliguori | 5b0d272 | 2009-03-13 15:02:28 +0000 | [diff] [blame] | 200 | if (rs->hist_entry == -1) |
| 201 | return; |
| 202 | if (rs->hist_entry < READLINE_MAX_CMDS - 1 && |
| 203 | rs->history[++rs->hist_entry] != NULL) { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 204 | pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf), |
| 205 | rs->history[rs->hist_entry]); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 206 | } else { |
aliguori | 5b0d272 | 2009-03-13 15:02:28 +0000 | [diff] [blame] | 207 | rs->cmd_buf[0] = 0; |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 208 | rs->hist_entry = -1; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 209 | } |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 210 | rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 211 | } |
| 212 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 213 | static void readline_hist_add(ReadLineState *rs, const char *cmdline) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 214 | { |
| 215 | char *hist_entry, *new_entry; |
| 216 | int idx; |
| 217 | |
| 218 | if (cmdline[0] == '\0') |
| 219 | return; |
| 220 | new_entry = NULL; |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 221 | if (rs->hist_entry != -1) { |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 222 | /* We were editing an existing history entry: replace it */ |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 223 | hist_entry = rs->history[rs->hist_entry]; |
| 224 | idx = rs->hist_entry; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 225 | if (strcmp(hist_entry, cmdline) == 0) { |
| 226 | goto same_entry; |
| 227 | } |
| 228 | } |
| 229 | /* Search cmdline in history buffers */ |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 230 | for (idx = 0; idx < READLINE_MAX_CMDS; idx++) { |
| 231 | hist_entry = rs->history[idx]; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 232 | if (hist_entry == NULL) |
| 233 | break; |
| 234 | if (strcmp(hist_entry, cmdline) == 0) { |
| 235 | same_entry: |
| 236 | new_entry = hist_entry; |
| 237 | /* Put this entry at the end of history */ |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 238 | memmove(&rs->history[idx], &rs->history[idx + 1], |
Markus Armbruster | 8af4288 | 2011-11-04 11:10:01 +0100 | [diff] [blame] | 239 | (READLINE_MAX_CMDS - (idx + 1)) * sizeof(char *)); |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 240 | rs->history[READLINE_MAX_CMDS - 1] = NULL; |
| 241 | for (; idx < READLINE_MAX_CMDS; idx++) { |
| 242 | if (rs->history[idx] == NULL) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 243 | break; |
| 244 | } |
| 245 | break; |
| 246 | } |
| 247 | } |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 248 | if (idx == READLINE_MAX_CMDS) { |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 249 | /* Need to get one free slot */ |
Markus Armbruster | c3baa5f | 2013-01-22 11:07:59 +0100 | [diff] [blame] | 250 | g_free(rs->history[0]); |
Nickolai Zeldovich | 7682e85 | 2013-01-07 15:38:39 -0500 | [diff] [blame] | 251 | memmove(rs->history, &rs->history[1], |
| 252 | (READLINE_MAX_CMDS - 1) * sizeof(char *)); |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 253 | rs->history[READLINE_MAX_CMDS - 1] = NULL; |
| 254 | idx = READLINE_MAX_CMDS - 1; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 255 | } |
| 256 | if (new_entry == NULL) |
Markus Armbruster | c3baa5f | 2013-01-22 11:07:59 +0100 | [diff] [blame] | 257 | new_entry = g_strdup(cmdline); |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 258 | rs->history[idx] = new_entry; |
| 259 | rs->hist_entry = -1; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | /* completion support */ |
| 263 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 264 | void readline_add_completion(ReadLineState *rs, const char *str) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 265 | { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 266 | if (rs->nb_completions < READLINE_MAX_COMPLETIONS) { |
Hani Benhabiles | e70871d | 2014-05-27 23:39:35 +0100 | [diff] [blame] | 267 | int i; |
| 268 | for (i = 0; i < rs->nb_completions; i++) { |
| 269 | if (!strcmp(rs->completions[i], str)) { |
| 270 | return; |
| 271 | } |
| 272 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 273 | rs->completions[rs->nb_completions++] = g_strdup(str); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 277 | void readline_set_completion_index(ReadLineState *rs, int index) |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 278 | { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 279 | rs->completion_index = index; |
aliguori | 376253e | 2009-03-05 23:01:23 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Hani Benhabiles | 307b2f0 | 2014-05-01 15:26:42 +0100 | [diff] [blame] | 282 | static int completion_comp(const void *a, const void *b) |
| 283 | { |
| 284 | return strcmp(*(const char **) a, *(const char **) b); |
| 285 | } |
| 286 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 287 | static void readline_completion(ReadLineState *rs) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 288 | { |
ths | b427c72 | 2007-09-25 14:45:23 +0000 | [diff] [blame] | 289 | int len, i, j, max_width, nb_cols, max_prefix; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 290 | char *cmdline; |
| 291 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 292 | rs->nb_completions = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 293 | |
Michael Tokarev | 6ad7c32 | 2014-05-05 13:23:50 +0400 | [diff] [blame] | 294 | cmdline = g_strndup(rs->cmd_buf, rs->cmd_buf_index); |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 295 | rs->completion_finder(rs->opaque, cmdline); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 296 | g_free(cmdline); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 297 | |
| 298 | /* no completion found */ |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 299 | if (rs->nb_completions <= 0) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 300 | return; |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 301 | if (rs->nb_completions == 1) { |
| 302 | len = strlen(rs->completions[0]); |
| 303 | for(i = rs->completion_index; i < len; i++) { |
| 304 | readline_insert_char(rs, rs->completions[0][i]); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 305 | } |
| 306 | /* extra space for next argument. XXX: make it more generic */ |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 307 | if (len > 0 && rs->completions[0][len - 1] != '/') |
| 308 | readline_insert_char(rs, ' '); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 309 | } else { |
Hani Benhabiles | 307b2f0 | 2014-05-01 15:26:42 +0100 | [diff] [blame] | 310 | qsort(rs->completions, rs->nb_completions, sizeof(char *), |
| 311 | completion_comp); |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 312 | rs->printf_func(rs->opaque, "\n"); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 313 | max_width = 0; |
ths | b427c72 | 2007-09-25 14:45:23 +0000 | [diff] [blame] | 314 | max_prefix = 0; |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 315 | for(i = 0; i < rs->nb_completions; i++) { |
| 316 | len = strlen(rs->completions[i]); |
ths | b427c72 | 2007-09-25 14:45:23 +0000 | [diff] [blame] | 317 | if (i==0) { |
| 318 | max_prefix = len; |
| 319 | } else { |
| 320 | if (len < max_prefix) |
| 321 | max_prefix = len; |
| 322 | for(j=0; j<max_prefix; j++) { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 323 | if (rs->completions[i][j] != rs->completions[0][j]) |
ths | b427c72 | 2007-09-25 14:45:23 +0000 | [diff] [blame] | 324 | max_prefix = j; |
| 325 | } |
| 326 | } |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 327 | if (len > max_width) |
| 328 | max_width = len; |
| 329 | } |
ths | b427c72 | 2007-09-25 14:45:23 +0000 | [diff] [blame] | 330 | if (max_prefix > 0) |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 331 | for(i = rs->completion_index; i < max_prefix; i++) { |
| 332 | readline_insert_char(rs, rs->completions[0][i]); |
ths | b427c72 | 2007-09-25 14:45:23 +0000 | [diff] [blame] | 333 | } |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 334 | max_width += 2; |
| 335 | if (max_width < 10) |
| 336 | max_width = 10; |
| 337 | else if (max_width > 80) |
| 338 | max_width = 80; |
| 339 | nb_cols = 80 / max_width; |
| 340 | j = 0; |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 341 | for(i = 0; i < rs->nb_completions; i++) { |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 342 | rs->printf_func(rs->opaque, "%-*s", max_width, rs->completions[i]); |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 343 | if (++j == nb_cols || i == (rs->nb_completions - 1)) { |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 344 | rs->printf_func(rs->opaque, "\n"); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 345 | j = 0; |
| 346 | } |
| 347 | } |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 348 | readline_show_prompt(rs); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 349 | } |
Stefan Weil | fc9fa4b | 2012-05-14 09:47:20 +0000 | [diff] [blame] | 350 | for (i = 0; i < rs->nb_completions; i++) { |
| 351 | g_free(rs->completions[i]); |
| 352 | } |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Hani Benhabiles | 075ccb6 | 2014-06-01 12:53:35 +0100 | [diff] [blame] | 355 | static void readline_clear_screen(ReadLineState *rs) |
| 356 | { |
| 357 | rs->printf_func(rs->opaque, "\033[2J\033[1;1H"); |
| 358 | readline_show_prompt(rs); |
| 359 | } |
| 360 | |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 361 | /* return true if command handled */ |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 362 | void readline_handle_byte(ReadLineState *rs, int ch) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 363 | { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 364 | switch(rs->esc_state) { |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 365 | case IS_NORM: |
| 366 | switch(ch) { |
| 367 | case 1: |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 368 | readline_bol(rs); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 369 | break; |
| 370 | case 4: |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 371 | readline_delete_char(rs); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 372 | break; |
| 373 | case 5: |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 374 | readline_eol(rs); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 375 | break; |
| 376 | case 9: |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 377 | readline_completion(rs); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 378 | break; |
Hani Benhabiles | 075ccb6 | 2014-06-01 12:53:35 +0100 | [diff] [blame] | 379 | case 12: |
| 380 | readline_clear_screen(rs); |
| 381 | break; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 382 | case 10: |
| 383 | case 13: |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 384 | rs->cmd_buf[rs->cmd_buf_size] = '\0'; |
| 385 | if (!rs->read_password) |
| 386 | readline_hist_add(rs, rs->cmd_buf); |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 387 | rs->printf_func(rs->opaque, "\n"); |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 388 | rs->cmd_buf_index = 0; |
| 389 | rs->cmd_buf_size = 0; |
| 390 | rs->last_cmd_buf_index = 0; |
| 391 | rs->last_cmd_buf_size = 0; |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 392 | rs->readline_func(rs->opaque, rs->cmd_buf, rs->readline_opaque); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 393 | break; |
ths | 33fa11d | 2007-06-25 11:36:50 +0000 | [diff] [blame] | 394 | case 23: |
| 395 | /* ^W */ |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 396 | readline_backword(rs); |
ths | 33fa11d | 2007-06-25 11:36:50 +0000 | [diff] [blame] | 397 | break; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 398 | case 27: |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 399 | rs->esc_state = IS_ESC; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 400 | break; |
| 401 | case 127: |
| 402 | case 8: |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 403 | readline_backspace(rs); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 404 | break; |
| 405 | case 155: |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 406 | rs->esc_state = IS_CSI; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 407 | break; |
| 408 | default: |
| 409 | if (ch >= 32) { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 410 | readline_insert_char(rs, ch); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 411 | } |
| 412 | break; |
| 413 | } |
| 414 | break; |
| 415 | case IS_ESC: |
| 416 | if (ch == '[') { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 417 | rs->esc_state = IS_CSI; |
| 418 | rs->esc_param = 0; |
Kevin Wolf | d34dc45 | 2013-05-14 12:15:54 +0200 | [diff] [blame] | 419 | } else if (ch == 'O') { |
| 420 | rs->esc_state = IS_SS3; |
| 421 | rs->esc_param = 0; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 422 | } else { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 423 | rs->esc_state = IS_NORM; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 424 | } |
| 425 | break; |
| 426 | case IS_CSI: |
| 427 | switch(ch) { |
| 428 | case 'A': |
| 429 | case 'F': |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 430 | readline_up_char(rs); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 431 | break; |
| 432 | case 'B': |
| 433 | case 'E': |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 434 | readline_down_char(rs); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 435 | break; |
| 436 | case 'D': |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 437 | readline_backward_char(rs); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 438 | break; |
| 439 | case 'C': |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 440 | readline_forward_char(rs); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 441 | break; |
| 442 | case '0' ... '9': |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 443 | rs->esc_param = rs->esc_param * 10 + (ch - '0'); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 444 | goto the_end; |
| 445 | case '~': |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 446 | switch(rs->esc_param) { |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 447 | case 1: |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 448 | readline_bol(rs); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 449 | break; |
| 450 | case 3: |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 451 | readline_delete_char(rs); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 452 | break; |
| 453 | case 4: |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 454 | readline_eol(rs); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 455 | break; |
| 456 | } |
| 457 | break; |
| 458 | default: |
| 459 | break; |
| 460 | } |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 461 | rs->esc_state = IS_NORM; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 462 | the_end: |
| 463 | break; |
Kevin Wolf | d34dc45 | 2013-05-14 12:15:54 +0200 | [diff] [blame] | 464 | case IS_SS3: |
| 465 | switch(ch) { |
| 466 | case 'F': |
| 467 | readline_eol(rs); |
| 468 | break; |
| 469 | case 'H': |
| 470 | readline_bol(rs); |
| 471 | break; |
| 472 | } |
| 473 | rs->esc_state = IS_NORM; |
| 474 | break; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 475 | } |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 476 | readline_update(rs); |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 477 | } |
| 478 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 479 | void readline_start(ReadLineState *rs, const char *prompt, int read_password, |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 480 | ReadLineFunc *readline_func, void *opaque) |
| 481 | { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 482 | pstrcpy(rs->prompt, sizeof(rs->prompt), prompt); |
| 483 | rs->readline_func = readline_func; |
| 484 | rs->readline_opaque = opaque; |
| 485 | rs->read_password = read_password; |
aliguori | 2724b18 | 2009-03-05 23:01:47 +0000 | [diff] [blame] | 486 | readline_restart(rs); |
| 487 | } |
| 488 | |
| 489 | void readline_restart(ReadLineState *rs) |
| 490 | { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 491 | rs->cmd_buf_index = 0; |
| 492 | rs->cmd_buf_size = 0; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 493 | } |
| 494 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 495 | const char *readline_get_history(ReadLineState *rs, unsigned int index) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 496 | { |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 497 | if (index >= READLINE_MAX_CMDS) |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 498 | return NULL; |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 499 | return rs->history[index]; |
| 500 | } |
| 501 | |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 502 | ReadLineState *readline_init(ReadLinePrintfFunc *printf_func, |
| 503 | ReadLineFlushFunc *flush_func, |
| 504 | void *opaque, |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 505 | ReadLineCompletionFunc *completion_finder) |
| 506 | { |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 507 | ReadLineState *rs = g_malloc0(sizeof(*rs)); |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 508 | |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 509 | rs->hist_entry = -1; |
Stefan Hajnoczi | c60bf33 | 2013-11-14 11:54:14 +0100 | [diff] [blame] | 510 | rs->opaque = opaque; |
| 511 | rs->printf_func = printf_func; |
| 512 | rs->flush_func = flush_func; |
aliguori | 4c36ba3 | 2009-03-05 23:01:37 +0000 | [diff] [blame] | 513 | rs->completion_finder = completion_finder; |
| 514 | |
| 515 | return rs; |
bellard | 7e2515e | 2004-08-01 21:52:19 +0000 | [diff] [blame] | 516 | } |