1 /*
2 * Copyright (c) 2002-2012, the original author or authors.
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 * http://www.opensource.org/licenses/bsd-license.php
8 */
9 package jline.internal;
10
11 import java.io.ByteArrayOutputStream;
12 import java.io.Closeable;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.OutputStream;
16 import java.text.MessageFormat;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import static jline.internal.Preconditions.checkNotNull;
21
22 /**
23 * Provides access to terminal line settings via <tt>stty</tt>.
24 *
25 * @author <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
26 * @author <a href="mailto:dwkemp@gmail.com">Dale Kemp</a>
27 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
28 * @author <a href="mailto:jbonofre@apache.org">Jean-Baptiste Onofré</a>
29 * @since 2.0
30 */
31 public final class TerminalLineSettings
32 {
33 public static final String JLINE_STTY = "jline.stty";
34
35 public static final String DEFAULT_STTY = "stty";
36
37 public static final String JLINE_SH = "jline.sh";
38
39 public static final String DEFAULT_SH = "sh";
40
41 private String sttyCommand;
42
43 private String shCommand;
44
45 private String config;
46 private String initialConfig;
47
48 private long configLastFetched;
92 try {
93 // tty properties are cached so we don't have to worry too much about getting term width/height
94 if (config == null || currentTime - configLastFetched > 1000) {
95 config = get("-a");
96 }
97 } catch (Exception e) {
98 if (e instanceof InterruptedException) {
99 Thread.currentThread().interrupt();
100 }
101 Log.debug("Failed to query stty ", name, "\n", e);
102 if (config == null) {
103 return -1;
104 }
105 }
106
107 // always update the last fetched time and try to parse the output
108 if (currentTime - configLastFetched > 1000) {
109 configLastFetched = currentTime;
110 }
111
112 return this.getProperty(name, config);
113 }
114
115 /**
116 * <p>
117 * Parses a stty output (provided by stty -a) and return the value of a given property.
118 * </p>
119 *
120 * @param name property name.
121 * @param stty string resulting of stty -a execution.
122 * @return value of the given property.
123 */
124 protected static int getProperty(String name, String stty) {
125 // try the first kind of regex
126 Pattern pattern = Pattern.compile(name + "\\s+=\\s+(.*?)[;\\n\\r]");
127 Matcher matcher = pattern.matcher(stty);
128 if (!matcher.find()) {
129 // try a second kind of regex
130 pattern = Pattern.compile(name + "\\s+([^;]*)[;\\n\\r]");
131 matcher = pattern.matcher(stty);
132 if (!matcher.find()) {
|
1 /*
2 * Copyright (c) 2002-2012, the original author or authors.
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 * http://www.opensource.org/licenses/bsd-license.php
8 */
9 package jdk.internal.jline.internal;
10
11 import java.io.ByteArrayOutputStream;
12 import java.io.Closeable;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.io.OutputStream;
16 import java.text.MessageFormat;
17 import java.util.regex.Matcher;
18 import java.util.regex.Pattern;
19
20 import static jdk.internal.jline.internal.Preconditions.checkNotNull;
21
22 /**
23 * Provides access to terminal line settings via <tt>stty</tt>.
24 *
25 * @author <a href="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
26 * @author <a href="mailto:dwkemp@gmail.com">Dale Kemp</a>
27 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
28 * @author <a href="mailto:jbonofre@apache.org">Jean-Baptiste Onofr\u00E9</a>
29 * @since 2.0
30 */
31 public final class TerminalLineSettings
32 {
33 public static final String JLINE_STTY = "jline.stty";
34
35 public static final String DEFAULT_STTY = "stty";
36
37 public static final String JLINE_SH = "jline.sh";
38
39 public static final String DEFAULT_SH = "sh";
40
41 private String sttyCommand;
42
43 private String shCommand;
44
45 private String config;
46 private String initialConfig;
47
48 private long configLastFetched;
92 try {
93 // tty properties are cached so we don't have to worry too much about getting term width/height
94 if (config == null || currentTime - configLastFetched > 1000) {
95 config = get("-a");
96 }
97 } catch (Exception e) {
98 if (e instanceof InterruptedException) {
99 Thread.currentThread().interrupt();
100 }
101 Log.debug("Failed to query stty ", name, "\n", e);
102 if (config == null) {
103 return -1;
104 }
105 }
106
107 // always update the last fetched time and try to parse the output
108 if (currentTime - configLastFetched > 1000) {
109 configLastFetched = currentTime;
110 }
111
112 return getProperty(name, config);
113 }
114
115 /**
116 * <p>
117 * Parses a stty output (provided by stty -a) and return the value of a given property.
118 * </p>
119 *
120 * @param name property name.
121 * @param stty string resulting of stty -a execution.
122 * @return value of the given property.
123 */
124 protected static int getProperty(String name, String stty) {
125 // try the first kind of regex
126 Pattern pattern = Pattern.compile(name + "\\s+=\\s+(.*?)[;\\n\\r]");
127 Matcher matcher = pattern.matcher(stty);
128 if (!matcher.find()) {
129 // try a second kind of regex
130 pattern = Pattern.compile(name + "\\s+([^;]*)[;\\n\\r]");
131 matcher = pattern.matcher(stty);
132 if (!matcher.find()) {
|