Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6554dd9e07bdabfe0fb54c2839f55e1dd716729d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*******************************************************************************
 * Copyright (c) 2009, 2010 Wind River Systems, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tm.internal.tcf.debug.ui.launch.setup;

import java.io.*;
import java.net.*;
import java.util.ArrayList;

class TelnetInputStream extends FilterInputStream {

    public interface TelnetTraceListener {
        void command(String s);
    }

    private final boolean echo;
    private final String prompt;

    private final InputStream inp;
    private final OutputStream out;
    private final boolean mode_rem[] = new boolean[256];
    private final int mode_cnt[] = new int[256];
    private final Reader reader = new Reader();
    private final byte buf[] = new byte[512];
    private int buf_inp = 0;
    private int buf_out = 0;
    private boolean eof;
    private IOException err;
    private boolean closed;

    private final ArrayList<TelnetTraceListener> trace_listeners = new ArrayList<TelnetTraceListener>();

    private static final int
        cm_IAC      = 255,
        cm_WILL     = 251,
        cm_WONT     = 252,
        cm_DO       = 253,
        cm_DONT     = 254,
        cm_SB       = 250,
        cm_SE       = 240,
        cm_DataMark = 242;

    private static final int
        co_ECHO                 = 1,
        co_SUPPRESS_GO_AHEAD    = 3,
        co_STATUS               = 5,
        co_TERMINAL_TYPE        = 24,
        co_NAWS                 = 31, // Negotiate About Window Size
        co_TERMINAL_SPEED       = 32,
        co_TOGGLE_FLOW_CONTROL  = 33,
        co_X_DISPLAY_LOCATION   = 35,
        co_ENVIRON              = 36,
        co_NEW_ENVIRON          = 39;

    @SuppressWarnings("unused")
    private static final int
        sp_VAR                  = 0,
        sp_VALUE                = 1,
        sp_ESC                  = 2,
        sp_USERVAR              = 3;

    @SuppressWarnings("unused")
    private static final int
        ac_IS                   = 0,
        ac_SEND                 = 1,
        ac_INFO                 = 2;

    private class Reader extends Thread {

        private void logCommand(int cmd, int opt) {
            String s = "" + cmd;
            switch (cmd) {
            case cm_WILL:
                s = "WILL";
                break;
            case cm_WONT:
                s = "WONT";
                break;
            case cm_DO:
                s = "DO";
                break;
            case cm_DONT:
                s = "DONT";
                break;
            case cm_SB:
                s = "SB";
                break;
            }
            s += " ";
            switch (opt) {
            case co_ECHO:
                s += "ECHO";
                break;
            case co_SUPPRESS_GO_AHEAD:
                s += "SUPPRESS_GO_AHEAD";
                break;
            case co_STATUS:
                s += "STATUS";
                break;
            case co_TERMINAL_TYPE:
                s += "TERMINAL_TYPE";
                break;
            case co_NAWS:
                s += "NAWS";
                break;
            case co_TERMINAL_SPEED:
                s += "TERMINAL_SPEED";
                break;
            case co_TOGGLE_FLOW_CONTROL:
                s += "TOGGLE_FLOW_CONTROL";
                break;
            case co_X_DISPLAY_LOCATION:
                s += "X_DISPLAY_LOCATION";
                break;
            case co_ENVIRON:
                s += "ENVIRON";
                break;
            case co_NEW_ENVIRON:
                s += "NEW_ENVIRON";
                break;
            default:
                s += opt;
                break;
            }
            for (TelnetTraceListener l : trace_listeners) l.command(s);
        }

        private int read_ch() throws IOException {
            try {
                return inp.read();
            }
            catch (SocketException x) {
                String s = x.getMessage();
                if (s.startsWith("Socket closed")) return -1;
                if (s.startsWith("socket closed")) return -1;
                throw x;
            }
        }

        public void run() {
            try {
                synchronized (out) {
                    out.write(cm_IAC);
                    out.write(echo ? cm_DO : cm_DONT);
                    out.write(co_ECHO);
                    out.flush();
                }
                for (;;) {
                    int rd = read_ch();
                    if (rd < 0) break;
                    if (rd == cm_IAC) {
                        int cm = read_ch();
                        if (cm < 0) break;
                        if (cm != cm_IAC) {
                            int co = read_ch();
                            if (co < 0) break;
                            if (co == cm_DataMark) continue;
                            logCommand(cm, co);
                            synchronized (out) {
                                if (mode_cnt[co] >= 5) continue;
                                switch (cm) {
                                case cm_WILL:
                                    mode_rem[co] = true;
                                    break;
                                case cm_WONT:
                                    mode_rem[co] = false;
                                    break;
                                case cm_DO:
                                    out.write(cm_IAC);
                                    if (co == co_SUPPRESS_GO_AHEAD) {
                                        out.write(cm_WILL);
                                    }
                                    else if (co == co_NEW_ENVIRON && prompt != null) {
                                        out.write(cm_WILL);
                                    }
                                    else {
                                        out.write(cm_WONT);
                                    }
                                    out.write(co);
                                    break;
                                case cm_DONT:
                                    out.write(cm_IAC);
                                    out.write(cm_WONT);
                                    out.write(co);
                                    break;
                                case cm_SB:
                                    int ac = read_ch();
                                    if (ac < 0) break;
                                    if (ac == ac_SEND) {
                                        if (co == co_NEW_ENVIRON) {
                                            out.write(cm_IAC);
                                            out.write(cm_SB);
                                            out.write(co_NEW_ENVIRON);
                                            out.write(ac_IS);
                                            if (prompt != null) {
                                                out.write(sp_VAR);
                                                out.write('P');
                                                out.write('S');
                                                out.write('1');
                                                out.write(sp_VALUE);
                                                for (int k = 0; k < prompt.length(); k++) out.write(prompt.charAt(k));
                                            }
                                            out.write(cm_IAC);
                                            out.write(cm_SE);
                                        }
                                    }
                                    int c0 = 0;
                                    for (;;) {
                                        int c1 = read_ch();
                                        if (c0 == cm_IAC && c1 == cm_SE) break;
                                        if (c0 == cm_IAC && c1 == cm_IAC) c1 = 0;
                                        c0 = c1;
                                    }
                                    break;
                                default:
                                    throw new IOException("Invalid command: " + cm);
                                }
                                out.flush();
                                mode_cnt[co]++;
                            }
                            continue;
                        }
                    }
                    synchronized (TelnetInputStream.this) {
                        int new_inp = (buf_inp + 1) % buf.length;
                        while (new_inp == buf_out) TelnetInputStream.this.wait();
                        buf[buf_inp] = (byte)rd;
                        buf_inp = new_inp;
                        TelnetInputStream.this.notify();
                    }
                }
            }
            catch (InterruptedException x) {
                err = new InterruptedIOException();
            }
            catch (IOException x) {
                if (!closed) err = x;
            }
            finally {
                synchronized (TelnetInputStream.this) {
                    eof = true;
                    TelnetInputStream.this.notify();
                }
            }
        }
    }

    TelnetInputStream(InputStream inp, OutputStream out, boolean echo, String prompt) {
        super(inp);
        if (!(inp instanceof BufferedInputStream)) inp = new BufferedInputStream(inp);
        this.inp = inp;
        this.out = out;
        this.echo = echo;
        this.prompt = prompt;
        reader.start();
    }

    public synchronized void addTraceListener(TelnetTraceListener l) {
        trace_listeners.add(l);
    }

    public synchronized void removeTraceListener(TelnetTraceListener l) {
        trace_listeners.remove(l);
    }

    public synchronized int read() throws IOException {
        try {
            while (buf_out == buf_inp) {
                if (err != null) throw new IOException(err.getMessage());
                if (eof) return -1;
                wait();
            }
            int res = buf[buf_out] & 0xff;
            buf_out = (buf_out + 1) % buf.length;
            notify();
            return res;
        }
        catch (InterruptedException x) {
            throw new InterruptedIOException();
        }
    }

    public synchronized int read(byte b[], int off, int len) throws IOException {
        boolean nfy = false;
        try {
            int cnt = 0;
            while (cnt < len) {
                while (buf_out == buf_inp) {
                    if (cnt > 0) return cnt;
                    if (err != null) throw new IOException(err.getMessage());
                    if (eof) return -1;
                    if (nfy) {
                        notify();
                        nfy = false;
                    }
                    wait();
                }
                b[off++] = buf[buf_out];
                buf_out = (buf_out + 1) % buf.length;
                nfy = true;
                cnt++;
            }
            return cnt;
        }
        catch (InterruptedException x) {
            throw new InterruptedIOException();
        }
        finally {
            if (nfy) notify();
        }
    }

    public synchronized int available() throws IOException {
        return (buf_inp + buf.length - buf_out) % buf.length;
    }

    public synchronized void close() throws IOException {
        closed = true;
        super.close();
    }
}

Back to the top