KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jline > UnixTerminal


1 /**
2  * jline - Java console input library
3  * Copyright (c) 2002,2003 Marc Prud'hommeaux mwp1@cornell.edu
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jline;
20
21 import java.io.*;
22 import java.util.*;
23
24
25 /**
26  * <p>
27  * Terminal that is used for unix platforms. Terminal initialization
28  * is handled by issuing the <code>stty</code> command against the
29  * <code>/dev/tty</code> file to disable character echoing and enable
30  * character input. All known unix systems (including
31  * Linux and Macintosh OS X) support the <code>stty</code>), so this
32  * implementation should work for an reasonable POSIX system.
33  * </p>
34  *
35  * @author <a HREF="mailto:mwp1@cornell.edu">Marc Prud'hommeaux</a>
36  */

37 public class UnixTerminal
38     extends Terminal
39 {
40     private Map terminfo;
41     private int width = -1;
42     private int height = -1;
43
44
45     /**
46      * Remove line-buffered input by invoking "stty -icanon min 1"
47      * against the current terminal.
48      */

49     public void initializeTerminal ()
50         throws IOException, InterruptedException JavaDoc
51     {
52         // save the initial tty configuration
53
final String JavaDoc ttyConfig = stty ("-g");
54
55         // sanity check
56
if (ttyConfig.length () == 0
57             || (ttyConfig.indexOf ("=") == -1
58             && ttyConfig.indexOf (":") == -1))
59         {
60             throw new IOException ("Unrecognized stty code: " + ttyConfig);
61         }
62
63
64         // set the console to be character-buffered instead of line-buffered
65
stty ("-icanon min 1");
66
67         // disable character echoing
68
stty ("-echo");
69
70         // at exit, restore the original tty configuration (for JDK 1.3+)
71
try
72         {
73             Runtime.getRuntime ().addShutdownHook (new Thread JavaDoc ()
74             {
75                 public void start ()
76                 {
77                     try
78                     {
79                         stty (ttyConfig);
80                     }
81                     catch (Exception JavaDoc e)
82                     {
83                     }
84                 }
85             });
86         }
87         catch (AbstractMethodError JavaDoc ame)
88         {
89             // JDK 1.3+ only method. Bummer.
90
}
91     }
92
93
94     public boolean isSupported ()
95     {
96         return true;
97     }
98
99
100     public boolean getEcho ()
101     {
102         return false;
103     }
104
105
106     /**
107      * Returns the value of "stty size" width param.
108      *
109      * <strong>Note</strong>: this method caches the value from the
110      * first time it is called in order to increase speed, which means
111      * that changing to size of the terminal will not be reflected
112      * in the console.
113      */

114     public int getTerminalWidth ()
115     {
116         if (width != -1)
117             return width;
118
119         int val = 80;
120         try
121         {
122             String JavaDoc size = stty ("size");
123             if (size.length () != 0 && size.indexOf (" ") != -1)
124             {
125                 val = Integer.parseInt (
126                     size.substring (size.indexOf (" ") + 1));
127             }
128         }
129         catch (Exception JavaDoc e)
130         {
131         }
132
133         return width = val;
134     }
135
136
137     /**
138      * Returns the value of "stty size" height param.
139      *
140      * <strong>Note</strong>: this method caches the value from the
141      * first time it is called in order to increase speed, which means
142      * that changing to size of the terminal will not be reflected
143      * in the console.
144      */

145     public int getTerminalHeight ()
146     {
147         if (height != -1)
148             return height;
149
150         int val = 24;
151
152         try
153         {
154             String JavaDoc size = stty ("size");
155             if (size.length () != 0 && size.indexOf (" ") != -1)
156             {
157                 val = Integer.parseInt (
158                     size.substring (0, size.indexOf (" ")));
159             }
160         }
161         catch (Exception JavaDoc e)
162         {
163         }
164
165         return height = val;
166     }
167
168
169     /**
170      * Execute the stty command with the specified arguments
171      * against the current active terminal.
172      */

173     private static String JavaDoc stty (String JavaDoc args)
174         throws IOException, InterruptedException JavaDoc
175     {
176         return exec ("stty " + args + " < /dev/tty").trim ();
177     }
178
179
180     /**
181      * Execute the specified command and return the output
182      * (both stdout and stderr).
183      */

184     private static String JavaDoc exec (String JavaDoc cmd)
185         throws IOException, InterruptedException JavaDoc
186     {
187         return exec (new String JavaDoc [] { "sh", "-c", cmd });
188     }
189
190
191     /**
192      * Execute the specified command and return the output
193      * (both stdout and stderr).
194      */

195     private static String JavaDoc exec (String JavaDoc [] cmd)
196         throws IOException, InterruptedException JavaDoc
197     {
198         ByteArrayOutputStream bout = new ByteArrayOutputStream ();
199
200         Process JavaDoc p = Runtime.getRuntime ().exec (cmd);
201         int c;
202         InputStream in;
203             
204         in = p.getInputStream ();
205         while ((c = in.read ()) != -1)
206             bout.write (c);
207
208         in = p.getErrorStream ();
209         while ((c = in.read ()) != -1)
210             bout.write (c);
211
212         p.waitFor ();
213
214         String JavaDoc result = new String JavaDoc (bout.toByteArray ());
215         return result;
216     }
217 }
218
219
Popular Tags