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