|
| 1 | +/* |
| 2 | + * Copyright (c) 2002-2023, the original author(s). |
| 3 | + * |
| 4 | + * This software is distributable under the BSD license. See the terms of the |
| 5 | + * BSD license in the documentation provided with this software. |
| 6 | + * |
| 7 | + * https://opensource.org/licenses/BSD-3-Clause |
| 8 | + */ |
| 9 | +package org.jline.reader.impl; |
| 10 | + |
| 11 | +import java.io.BufferedReader; |
| 12 | +import java.io.IOException; |
| 13 | +import java.io.InputStream; |
| 14 | +import java.io.InputStreamReader; |
| 15 | +import java.io.Reader; |
| 16 | +import java.net.URL; |
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Locale; |
| 20 | + |
| 21 | +import org.jline.reader.LineReader; |
| 22 | +import org.jline.reader.Macro; |
| 23 | +import org.jline.reader.Reference; |
| 24 | +import org.jline.utils.Log; |
| 25 | + |
| 26 | +public final class InputRC { |
| 27 | + |
| 28 | + public static void configure(LineReader reader, URL url) throws IOException { |
| 29 | + try (InputStream is = url.openStream()) { |
| 30 | + configure(reader, is); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + public static void configure(LineReader reader, InputStream is) throws IOException { |
| 35 | + try (InputStreamReader r = new InputStreamReader(is)) { |
| 36 | + configure(reader, r); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public static void configure(LineReader reader, Reader r) throws IOException { |
| 41 | + BufferedReader br; |
| 42 | + if (r instanceof BufferedReader) { |
| 43 | + br = (BufferedReader) r; |
| 44 | + } else { |
| 45 | + br = new BufferedReader(r); |
| 46 | + } |
| 47 | + reader.getVariables().putIfAbsent(LineReader.EDITING_MODE, "emacs"); |
| 48 | + reader.setKeyMap(LineReader.MAIN); |
| 49 | + if ("vi".equals(reader.getVariable(LineReader.EDITING_MODE))) { |
| 50 | + reader.getKeyMaps().put(LineReader.MAIN, reader.getKeyMaps().get(LineReader.VIINS)); |
| 51 | + } else if ("emacs".equals(reader.getVariable(LineReader.EDITING_MODE))) { |
| 52 | + reader.getKeyMaps().put(LineReader.MAIN, reader.getKeyMaps().get(LineReader.EMACS)); |
| 53 | + } |
| 54 | + new InputRC(reader).parse(br); |
| 55 | + if ("vi".equals(reader.getVariable(LineReader.EDITING_MODE))) { |
| 56 | + reader.getKeyMaps().put(LineReader.MAIN, reader.getKeyMaps().get(LineReader.VIINS)); |
| 57 | + } else if ("emacs".equals(reader.getVariable(LineReader.EDITING_MODE))) { |
| 58 | + reader.getKeyMaps().put(LineReader.MAIN, reader.getKeyMaps().get(LineReader.EMACS)); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + private final LineReader reader; |
| 63 | + |
| 64 | + private InputRC(LineReader reader) { |
| 65 | + this.reader = reader; |
| 66 | + } |
| 67 | + |
| 68 | + private void parse(BufferedReader br) throws IOException, IllegalArgumentException { |
| 69 | + String line; |
| 70 | + boolean parsing = true; |
| 71 | + List<Boolean> ifsStack = new ArrayList<>(); |
| 72 | + while ((line = br.readLine()) != null) { |
| 73 | + try { |
| 74 | + line = line.trim(); |
| 75 | + if (line.length() == 0) { |
| 76 | + continue; |
| 77 | + } |
| 78 | + if (line.charAt(0) == '#') { |
| 79 | + continue; |
| 80 | + } |
| 81 | + int i = 0; |
| 82 | + if (line.charAt(i) == '$') { |
| 83 | + String cmd; |
| 84 | + String args; |
| 85 | + ++i; |
| 86 | + while (i < line.length() && (line.charAt(i) == ' ' || line.charAt(i) == '\t')) { |
| 87 | + i++; |
| 88 | + } |
| 89 | + int s = i; |
| 90 | + while (i < line.length() && (line.charAt(i) != ' ' && line.charAt(i) != '\t')) { |
| 91 | + i++; |
| 92 | + } |
| 93 | + cmd = line.substring(s, i); |
| 94 | + while (i < line.length() && (line.charAt(i) == ' ' || line.charAt(i) == '\t')) { |
| 95 | + i++; |
| 96 | + } |
| 97 | + s = i; |
| 98 | + while (i < line.length() && (line.charAt(i) != ' ' && line.charAt(i) != '\t')) { |
| 99 | + i++; |
| 100 | + } |
| 101 | + args = line.substring(s, i); |
| 102 | + if ("if".equalsIgnoreCase(cmd)) { |
| 103 | + ifsStack.add(parsing); |
| 104 | + if (!parsing) { |
| 105 | + continue; |
| 106 | + } |
| 107 | + if (args.startsWith("term=")) { |
| 108 | + // TODO |
| 109 | + } else if (args.startsWith("mode=")) { |
| 110 | + String mode = (String) reader.getVariable(LineReader.EDITING_MODE); |
| 111 | + parsing = args.substring("mode=".length()).equalsIgnoreCase(mode); |
| 112 | + } else { |
| 113 | + parsing = args.equalsIgnoreCase(reader.getAppName()); |
| 114 | + } |
| 115 | + } else if ("else".equalsIgnoreCase(cmd)) { |
| 116 | + if (ifsStack.isEmpty()) { |
| 117 | + throw new IllegalArgumentException("$else found without matching $if"); |
| 118 | + } |
| 119 | + boolean invert = true; |
| 120 | + for (boolean b : ifsStack) { |
| 121 | + if (!b) { |
| 122 | + invert = false; |
| 123 | + break; |
| 124 | + } |
| 125 | + } |
| 126 | + if (invert) { |
| 127 | + parsing = !parsing; |
| 128 | + } |
| 129 | + } else if ("endif".equalsIgnoreCase(cmd)) { |
| 130 | + if (ifsStack.isEmpty()) { |
| 131 | + throw new IllegalArgumentException("endif found without matching $if"); |
| 132 | + } |
| 133 | + parsing = ifsStack.remove(ifsStack.size() - 1); |
| 134 | + } else if ("include".equalsIgnoreCase(cmd)) { |
| 135 | + // TODO |
| 136 | + } |
| 137 | + continue; |
| 138 | + } |
| 139 | + if (!parsing) { |
| 140 | + continue; |
| 141 | + } |
| 142 | + if (line.charAt(i++) == '"') { |
| 143 | + boolean esc = false; |
| 144 | + for (; ; i++) { |
| 145 | + if (i >= line.length()) { |
| 146 | + throw new IllegalArgumentException("Missing closing quote on line '" + line + "'"); |
| 147 | + } |
| 148 | + if (esc) { |
| 149 | + esc = false; |
| 150 | + } else if (line.charAt(i) == '\\') { |
| 151 | + esc = true; |
| 152 | + } else if (line.charAt(i) == '"') { |
| 153 | + break; |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + while (i < line.length() && line.charAt(i) != ':' && line.charAt(i) != ' ' && line.charAt(i) != '\t') { |
| 158 | + i++; |
| 159 | + } |
| 160 | + String keySeq = line.substring(0, i); |
| 161 | + boolean equivalency = i + 1 < line.length() && line.charAt(i) == ':' && line.charAt(i + 1) == '='; |
| 162 | + i++; |
| 163 | + if (equivalency) { |
| 164 | + i++; |
| 165 | + } |
| 166 | + if (keySeq.equalsIgnoreCase("set")) { |
| 167 | + String key; |
| 168 | + String val; |
| 169 | + while (i < line.length() && (line.charAt(i) == ' ' || line.charAt(i) == '\t')) { |
| 170 | + i++; |
| 171 | + } |
| 172 | + int s = i; |
| 173 | + while (i < line.length() && (line.charAt(i) != ' ' && line.charAt(i) != '\t')) { |
| 174 | + i++; |
| 175 | + } |
| 176 | + key = line.substring(s, i); |
| 177 | + while (i < line.length() && (line.charAt(i) == ' ' || line.charAt(i) == '\t')) { |
| 178 | + i++; |
| 179 | + } |
| 180 | + s = i; |
| 181 | + while (i < line.length() && (line.charAt(i) != ' ' && line.charAt(i) != '\t')) { |
| 182 | + i++; |
| 183 | + } |
| 184 | + val = line.substring(s, i); |
| 185 | + setVar(reader, key, val); |
| 186 | + } else { |
| 187 | + while (i < line.length() && (line.charAt(i) == ' ' || line.charAt(i) == '\t')) { |
| 188 | + i++; |
| 189 | + } |
| 190 | + int start = i; |
| 191 | + if (i < line.length() && (line.charAt(i) == '\'' || line.charAt(i) == '\"')) { |
| 192 | + char delim = line.charAt(i++); |
| 193 | + boolean esc = false; |
| 194 | + for (; ; i++) { |
| 195 | + if (i >= line.length()) { |
| 196 | + break; |
| 197 | + } |
| 198 | + if (esc) { |
| 199 | + esc = false; |
| 200 | + } else if (line.charAt(i) == '\\') { |
| 201 | + esc = true; |
| 202 | + } else if (line.charAt(i) == delim) { |
| 203 | + break; |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + for (; i < line.length() && line.charAt(i) != ' ' && line.charAt(i) != '\t'; i++) |
| 208 | + ; |
| 209 | + String val = line.substring(Math.min(start, line.length()), Math.min(i, line.length())); |
| 210 | + if (keySeq.charAt(0) == '"') { |
| 211 | + keySeq = translateQuoted(keySeq); |
| 212 | + } else { |
| 213 | + // Bind key name |
| 214 | + String keyName = |
| 215 | + keySeq.lastIndexOf('-') > 0 ? keySeq.substring(keySeq.lastIndexOf('-') + 1) : keySeq; |
| 216 | + char key = getKeyFromName(keyName); |
| 217 | + keyName = keySeq.toLowerCase(); |
| 218 | + keySeq = ""; |
| 219 | + if (keyName.contains("meta-") || keyName.contains("m-")) { |
| 220 | + keySeq += "\u001b"; |
| 221 | + } |
| 222 | + if (keyName.contains("control-") || keyName.contains("c-") || keyName.contains("ctrl-")) { |
| 223 | + key = (char) (Character.toUpperCase(key) & 0x1f); |
| 224 | + } |
| 225 | + keySeq += key; |
| 226 | + } |
| 227 | + if (val.length() > 0 && (val.charAt(0) == '\'' || val.charAt(0) == '\"')) { |
| 228 | + reader.getKeys().bind(new Macro(translateQuoted(val)), keySeq); |
| 229 | + } else { |
| 230 | + reader.getKeys().bind(new Reference(val), keySeq); |
| 231 | + } |
| 232 | + } |
| 233 | + } catch (IllegalArgumentException e) { |
| 234 | + Log.warn("Unable to parse user configuration: ", e); |
| 235 | + } |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + private static String translateQuoted(String keySeq) { |
| 240 | + int i; |
| 241 | + String str = keySeq.substring(1, keySeq.length() - 1); |
| 242 | + StringBuilder sb = new StringBuilder(); |
| 243 | + for (i = 0; i < str.length(); i++) { |
| 244 | + char c = str.charAt(i); |
| 245 | + if (c == '\\') { |
| 246 | + boolean ctrl = str.regionMatches(i, "\\C-", 0, 3) || str.regionMatches(i, "\\M-\\C-", 0, 6); |
| 247 | + boolean meta = str.regionMatches(i, "\\M-", 0, 3) || str.regionMatches(i, "\\C-\\M-", 0, 6); |
| 248 | + i += (meta ? 3 : 0) + (ctrl ? 3 : 0) + (!meta && !ctrl ? 1 : 0); |
| 249 | + if (i >= str.length()) { |
| 250 | + break; |
| 251 | + } |
| 252 | + c = str.charAt(i); |
| 253 | + if (meta) { |
| 254 | + sb.append("\u001b"); |
| 255 | + } |
| 256 | + if (ctrl) { |
| 257 | + c = c == '?' ? 0x7f : (char) (Character.toUpperCase(c) & 0x1f); |
| 258 | + } |
| 259 | + if (!meta && !ctrl) { |
| 260 | + switch (c) { |
| 261 | + case 'a': |
| 262 | + c = 0x07; |
| 263 | + break; |
| 264 | + case 'b': |
| 265 | + c = '\b'; |
| 266 | + break; |
| 267 | + case 'd': |
| 268 | + c = 0x7f; |
| 269 | + break; |
| 270 | + case 'e': |
| 271 | + c = 0x1b; |
| 272 | + break; |
| 273 | + case 'f': |
| 274 | + c = '\f'; |
| 275 | + break; |
| 276 | + case 'n': |
| 277 | + c = '\n'; |
| 278 | + break; |
| 279 | + case 'r': |
| 280 | + c = '\r'; |
| 281 | + break; |
| 282 | + case 't': |
| 283 | + c = '\t'; |
| 284 | + break; |
| 285 | + case 'v': |
| 286 | + c = 0x0b; |
| 287 | + break; |
| 288 | + case '\\': |
| 289 | + c = '\\'; |
| 290 | + break; |
| 291 | + case '0': |
| 292 | + case '1': |
| 293 | + case '2': |
| 294 | + case '3': |
| 295 | + case '4': |
| 296 | + case '5': |
| 297 | + case '6': |
| 298 | + case '7': |
| 299 | + c = 0; |
| 300 | + for (int j = 0; j < 3; j++, i++) { |
| 301 | + if (i >= str.length()) { |
| 302 | + break; |
| 303 | + } |
| 304 | + int k = Character.digit(str.charAt(i), 8); |
| 305 | + if (k < 0) { |
| 306 | + break; |
| 307 | + } |
| 308 | + c = (char) (c * 8 + k); |
| 309 | + } |
| 310 | + c &= 0xFF; |
| 311 | + break; |
| 312 | + case 'x': |
| 313 | + i++; |
| 314 | + c = 0; |
| 315 | + for (int j = 0; j < 2; j++, i++) { |
| 316 | + if (i >= str.length()) { |
| 317 | + break; |
| 318 | + } |
| 319 | + int k = Character.digit(str.charAt(i), 16); |
| 320 | + if (k < 0) { |
| 321 | + break; |
| 322 | + } |
| 323 | + c = (char) (c * 16 + k); |
| 324 | + } |
| 325 | + c &= 0xFF; |
| 326 | + break; |
| 327 | + case 'u': |
| 328 | + i++; |
| 329 | + c = 0; |
| 330 | + for (int j = 0; j < 4; j++, i++) { |
| 331 | + if (i >= str.length()) { |
| 332 | + break; |
| 333 | + } |
| 334 | + int k = Character.digit(str.charAt(i), 16); |
| 335 | + if (k < 0) { |
| 336 | + break; |
| 337 | + } |
| 338 | + c = (char) (c * 16 + k); |
| 339 | + } |
| 340 | + break; |
| 341 | + } |
| 342 | + } |
| 343 | + sb.append(c); |
| 344 | + } else { |
| 345 | + sb.append(c); |
| 346 | + } |
| 347 | + } |
| 348 | + return sb.toString(); |
| 349 | + } |
| 350 | + |
| 351 | + private static char getKeyFromName(String name) { |
| 352 | + if ("DEL".equalsIgnoreCase(name) || "Rubout".equalsIgnoreCase(name)) { |
| 353 | + return 0x7f; |
| 354 | + } else if ("ESC".equalsIgnoreCase(name) || "Escape".equalsIgnoreCase(name)) { |
| 355 | + return '\033'; |
| 356 | + } else if ("LFD".equalsIgnoreCase(name) || "NewLine".equalsIgnoreCase(name)) { |
| 357 | + return '\n'; |
| 358 | + } else if ("RET".equalsIgnoreCase(name) || "Return".equalsIgnoreCase(name)) { |
| 359 | + return '\r'; |
| 360 | + } else if ("SPC".equalsIgnoreCase(name) || "Space".equalsIgnoreCase(name)) { |
| 361 | + return ' '; |
| 362 | + } else if ("Tab".equalsIgnoreCase(name)) { |
| 363 | + return '\t'; |
| 364 | + } else { |
| 365 | + return name.charAt(0); |
| 366 | + } |
| 367 | + } |
| 368 | + |
| 369 | + static void setVar(LineReader reader, String key, String val) { |
| 370 | + if (LineReader.KEYMAP.equalsIgnoreCase(key)) { |
| 371 | + reader.setKeyMap(val); |
| 372 | + return; |
| 373 | + } |
| 374 | + |
| 375 | + for (LineReader.Option option : LineReader.Option.values()) { |
| 376 | + if (option.name().toLowerCase(Locale.ENGLISH).replace('_', '-').equals(val)) { |
| 377 | + if ("on".equalsIgnoreCase(val)) { |
| 378 | + reader.setOpt(option); |
| 379 | + } else if ("off".equalsIgnoreCase(val)) { |
| 380 | + reader.unsetOpt(option); |
| 381 | + } |
| 382 | + return; |
| 383 | + } |
| 384 | + } |
| 385 | + |
| 386 | + reader.setVariable(key, val); |
| 387 | + } |
| 388 | +} |
0 commit comments