Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: cc3fe39f9a2bca0fef3b550732f468b16adbf3e8 (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
/*******************************************************************************
 * Copyright (c) 2010, 2017 SAP AG and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 * 
 * Contributors:
 *     Lazar Kirchev, SAP AG - initial API and implementation  
 *******************************************************************************/

package org.eclipse.equinox.console.common;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.equinox.console.common.terminal.ANSITerminalTypeMappings;
import org.eclipse.equinox.console.common.terminal.SCOTerminalTypeMappings;
import org.eclipse.equinox.console.common.terminal.TerminalTypeMappings;
import org.eclipse.equinox.console.common.terminal.VT100TerminalTypeMappings;
import org.eclipse.equinox.console.common.terminal.VT220TerminalTypeMappings;
import org.eclipse.equinox.console.common.terminal.VT320TerminalTypeMappings;

/**
 * A common superclass for content processor for the telnet protocol and for command line editing (processing delete,
 * backspace, arrows, command history, etc.).
 */
public abstract class Scanner {

	private byte BACKSPACE;
	private byte DEL;
	protected static final byte BS = 8;  
	protected static final byte LF = 10;
	protected static final byte CR = 13;
	protected static final byte ESC = 27;
	protected static final byte SPACE = 32;
	protected static final byte MAX_CHAR = 127;
	protected static final String DEFAULT_TTYPE = File.separatorChar == '/' ? "XTERM" : "ANSI";
	// shows if user input should be echoed to the console
	private boolean isEchoEnabled = true;

	protected OutputStream toTelnet;
	protected ConsoleInputStream toShell;
	protected Map<String, KEYS> currentEscapesToKey;
	protected final Map<String, TerminalTypeMappings> supportedEscapeSequences;
	protected String[] escapes;
	
	public Scanner(ConsoleInputStream toShell, OutputStream toTelnet) {
		this.toShell = toShell;
		this.toTelnet = toTelnet;
		supportedEscapeSequences = new HashMap<> ();
		supportedEscapeSequences.put("ANSI", new ANSITerminalTypeMappings());
		supportedEscapeSequences.put("VT100", new VT100TerminalTypeMappings());
		VT220TerminalTypeMappings vtMappings = new VT220TerminalTypeMappings();
		supportedEscapeSequences.put("VT220", vtMappings);
		supportedEscapeSequences.put("XTERM", vtMappings);
		supportedEscapeSequences.put("VT320", new VT320TerminalTypeMappings());
		supportedEscapeSequences.put("SCO", new SCOTerminalTypeMappings());
	}

	public abstract void scan(int b) throws IOException;

	public void toggleEchoEnabled(boolean isEnabled) {
		isEchoEnabled = isEnabled;
	}
	
	protected void echo(int b) throws IOException {
		if (isEchoEnabled) {
			toTelnet.write(b);
		}
	}

	protected void flush() throws IOException {
		toTelnet.flush();
	}

	protected KEYS checkEscape(String possibleEsc) {
		if (currentEscapesToKey.get(possibleEsc) != null) {
			return currentEscapesToKey.get(possibleEsc);
		}

		for (String escape : escapes) {
			if (escape.startsWith(possibleEsc)) {
				return KEYS.UNFINISHED;
			}
		}
		return KEYS.UNKNOWN;
	}

	protected String esc;
	protected boolean isEsc = false;

	protected void startEsc() {
		isEsc = true;
		esc = "";
	}

	protected abstract void scanEsc(final int b) throws IOException;

	public byte getBackspace() {
		return BACKSPACE;
	}

	public void setBackspace(byte backspace) {
		BACKSPACE = backspace;
	}

	public byte getDel() {
		return DEL;
	}

	public void setDel(byte del) {
		DEL = del;
	}

	public Map<String, KEYS> getCurrentEscapesToKey() {
		return currentEscapesToKey;
	}

	public void setCurrentEscapesToKey(Map<String, KEYS> currentEscapesToKey) {
		this.currentEscapesToKey = currentEscapesToKey;
	}

	public String[] getEscapes() {
		if (escapes != null) {
			String[] copy = new String[escapes.length];
			System.arraycopy(escapes, 0, copy, 0, escapes.length);
			return copy;
		} else {
			return null;
		}
	}

	public void setEscapes(String[] escapes) {
		if (escapes != null) {
			this.escapes = new String[escapes.length];
			System.arraycopy(escapes, 0, this.escapes, 0, escapes.length);
		} else {
			this.escapes = null;
		}
	}

}

Back to the top