|
| 1 | +/* |
| 2 | + * Copyright (C) 2009-2023 the original author(s). |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.fusesource.jansi.internal; |
| 17 | + |
| 18 | +import java.io.ByteArrayOutputStream; |
| 19 | +import java.io.File; |
| 20 | +import java.io.FileDescriptor; |
| 21 | +import java.io.IOException; |
| 22 | +import java.io.InputStream; |
| 23 | +import java.lang.reflect.Constructor; |
| 24 | +import java.lang.reflect.Field; |
| 25 | +import java.util.regex.Matcher; |
| 26 | +import java.util.regex.Pattern; |
| 27 | + |
| 28 | +/** |
| 29 | + * Support for MINGW terminals. |
| 30 | + * Those terminals do not use the underlying windows terminal and there's no CLibrary available |
| 31 | + * in these environments. We have to rely on calling {@code stty.exe} and {@code tty.exe} to |
| 32 | + * obtain the terminal name and width. |
| 33 | + */ |
| 34 | +public class MingwSupport { |
| 35 | + |
| 36 | + private final String sttyCommand; |
| 37 | + private final String ttyCommand; |
| 38 | + private final Pattern columnsPatterns; |
| 39 | + |
| 40 | + public MingwSupport() { |
| 41 | + String tty = null; |
| 42 | + String stty = null; |
| 43 | + String path = System.getenv("PATH"); |
| 44 | + if (path != null) { |
| 45 | + String[] paths = path.split(File.pathSeparator); |
| 46 | + for (String p : paths) { |
| 47 | + File ttyFile = new File(p, "tty.exe"); |
| 48 | + if (tty == null && ttyFile.canExecute()) { |
| 49 | + tty = ttyFile.getAbsolutePath(); |
| 50 | + } |
| 51 | + File sttyFile = new File(p, "stty.exe"); |
| 52 | + if (stty == null && sttyFile.canExecute()) { |
| 53 | + stty = sttyFile.getAbsolutePath(); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + if (tty == null) { |
| 58 | + tty = "tty.exe"; |
| 59 | + } |
| 60 | + if (stty == null) { |
| 61 | + stty = "stty.exe"; |
| 62 | + } |
| 63 | + ttyCommand = tty; |
| 64 | + sttyCommand = stty; |
| 65 | + // Compute patterns |
| 66 | + columnsPatterns = Pattern.compile("\\b" + "columns" + "\\s+(\\d+)\\b"); |
| 67 | + } |
| 68 | + |
| 69 | + public String getConsoleName(boolean stdout) { |
| 70 | + try { |
| 71 | + Process p = new ProcessBuilder(ttyCommand) |
| 72 | + .redirectInput(getRedirect(stdout ? FileDescriptor.out : FileDescriptor.err)) |
| 73 | + .start(); |
| 74 | + String result = waitAndCapture(p); |
| 75 | + if (p.exitValue() == 0) { |
| 76 | + return result.trim(); |
| 77 | + } |
| 78 | + } catch (Throwable t) { |
| 79 | + if ("java.lang.reflect.InaccessibleObjectException" |
| 80 | + .equals(t.getClass().getName())) { |
| 81 | + System.err.println("MINGW support requires --add-opens java.base/java.lang=ALL-UNNAMED"); |
| 82 | + } |
| 83 | + // ignore |
| 84 | + } |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + public int getTerminalWidth(String name) { |
| 89 | + try { |
| 90 | + Process p = new ProcessBuilder(sttyCommand, "-F", name, "-a").start(); |
| 91 | + String result = waitAndCapture(p); |
| 92 | + if (p.exitValue() != 0) { |
| 93 | + throw new IOException("Error executing '" + sttyCommand + "': " + result); |
| 94 | + } |
| 95 | + Matcher matcher = columnsPatterns.matcher(result); |
| 96 | + if (matcher.find()) { |
| 97 | + return Integer.parseInt(matcher.group(1)); |
| 98 | + } |
| 99 | + throw new IOException("Unable to parse columns"); |
| 100 | + } catch (Exception e) { |
| 101 | + throw new RuntimeException(e); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private static String waitAndCapture(Process p) throws IOException, InterruptedException { |
| 106 | + ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
| 107 | + try (InputStream in = p.getInputStream(); |
| 108 | + InputStream err = p.getErrorStream()) { |
| 109 | + int c; |
| 110 | + while ((c = in.read()) != -1) { |
| 111 | + bout.write(c); |
| 112 | + } |
| 113 | + while ((c = err.read()) != -1) { |
| 114 | + bout.write(c); |
| 115 | + } |
| 116 | + p.waitFor(); |
| 117 | + } |
| 118 | + return bout.toString(); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * This requires --add-opens java.base/java.lang=ALL-UNNAMED |
| 123 | + */ |
| 124 | + private ProcessBuilder.Redirect getRedirect(FileDescriptor fd) throws ReflectiveOperationException { |
| 125 | + // This is not really allowed, but this is the only way to redirect the output or error stream |
| 126 | + // to the input. This is definitely not something you'd usually want to do, but in the case of |
| 127 | + // the `tty` utility, it provides a way to get |
| 128 | + Class<?> rpi = Class.forName("java.lang.ProcessBuilder$RedirectPipeImpl"); |
| 129 | + Constructor<?> cns = rpi.getDeclaredConstructor(); |
| 130 | + cns.setAccessible(true); |
| 131 | + ProcessBuilder.Redirect input = (ProcessBuilder.Redirect) cns.newInstance(); |
| 132 | + Field f = rpi.getDeclaredField("fd"); |
| 133 | + f.setAccessible(true); |
| 134 | + f.set(input, fd); |
| 135 | + return input; |
| 136 | + } |
| 137 | +} |
0 commit comments