Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 56ed5ea5400002f33167facd6897f821ab05a2af (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
/*******************************************************************************
 * Copyright (c) 2009, 2011 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.tcf.internal.debug.ui.launch.setup;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;

abstract class AbstractRemoteShell implements IRemoteShell {

    protected boolean debug;
    protected BufferedReader inp;
    protected PrintWriter out;

    public void setDebug(boolean debug) {
        this.debug = debug;
    }

    public synchronized void write(String s) {
        out.write(s);
    }

    public synchronized void expect(String s) throws IOException {
        expect(new String[]{ s });
    }

    public synchronized int expect(String s[]) throws IOException {
        out.flush();
        int[] pos = new int[s.length];
        for (int i = 0; i < pos.length; i++) pos[i] = 0;
        StringBuffer buf = new StringBuffer();
        try {
            for (;;) {
                int ch = inp.read();
                if (ch < 0) throw new IOException("Unexpected EOF");
                if (ch == '\r') continue;
                if (debug) System.out.write(ch);
                for (int i = 0; i < pos.length; i++) {
                    String p = s[i];
                    if (ch == p.charAt(pos[i])) {
                        pos[i]++;
                        if (pos[i] == p.length()) return i;
                    }
                    else {
                        int nps = pos[i];
                        while (nps > 0) {
                            if (ch == p.charAt(nps - 1)) {
                                int j = nps - 2;
                                int k = pos[i] - 1;
                                while (j >= 0 && p.charAt(j) == p.charAt(k)) {
                                    j--;
                                    k--;
                                }
                                if (j < 0) break;
                            }
                            nps--;
                        }
                        pos[i] = nps;
                    }
                }
                buf.append((char)ch);
                if (ch == '\n') buf.setLength(0);
            }
        }
        catch (IOException x) {
            if (buf.length() == 0) throw x;
            IOException y = new IOException("I/O error, last text received: " + buf);
            y.initCause(x);
            throw y;
        }
        finally {
            if (debug) System.out.flush();
        }
    }

    public synchronized String waitPrompt() throws IOException {
        StringBuffer res = new StringBuffer();
        StringBuffer buf = new StringBuffer();
        out.flush();
        try {
            for (;;) {
                int ch = inp.read();
                if (ch < 0) throw new IOException("Unexpected EOF");
                if (ch == '\r') continue;
                if (debug) System.out.write(ch);
                buf.append((char)ch);
                if (buf.length() == PROMPT.length() && buf.toString().equals(PROMPT)) break;
                if (ch == '\n') {
                    res.append(buf);
                    buf.setLength(0);
                }
            }
            return res.toString();
        }
        catch (IOException x) {
            if (buf.length() == 0) throw x;
            IOException y = new IOException("I/O error, last text received: " + buf);
            y.initCause(x);
            throw y;
        }
        finally {
            if (debug) System.out.flush();
        }
    }
}

Back to the top