001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: *
041: * Contributor(s): Ivan Soleimanipour.
042: */
043:
044: package org.netbeans.lib.terminalemulator;
045:
046: import java.util.Stack;
047:
048: /**
049: * Input stream interpreter
050: * Decodes incoming characters into cursor motion etc.
051: */
052: public class InterpDumb extends AbstractInterp {
053:
054: protected static class InterpTypeDumb {
055: public final State st_base = new State("base"); // NOI18N
056:
057: protected final Actor act_nop = new ACT_NOP();
058: protected final Actor act_pause = new ACT_PAUSE();
059: protected final Actor act_err = new ACT_ERR();
060: protected final Actor act_regular = new ACT_REGULAR();
061: protected final Actor act_cr = new ACT_CR();
062: protected final Actor act_lf = new ACT_LF();
063: protected final Actor act_bs = new ACT_BS();
064: protected final Actor act_tab = new ACT_TAB();
065: protected final Actor act_beL = new ACT_BEL();
066:
067: protected InterpTypeDumb() {
068: st_base.setRegular(st_base, act_regular);
069:
070: for (char c = 0; c < 128; c++)
071: st_base.setAction(c, st_base, act_regular);
072:
073: st_base.setAction((char) 0, st_base, act_pause);
074: st_base.setAction('\r', st_base, act_cr);
075: st_base.setAction('\n', st_base, act_lf);
076: st_base.setAction('\b', st_base, act_bs);
077: st_base.setAction('\t', st_base, act_tab);
078: st_base.setAction((char) 7, st_base, act_beL);
079: }
080:
081: protected static final class ACT_NOP implements Actor {
082: public String action(AbstractInterp ai, char c) {
083: return null;
084: }
085: };
086:
087: protected static final class ACT_PAUSE implements Actor {
088: public String action(AbstractInterp ai, char c) {
089: ai.ops.op_pause();
090: return null;
091: }
092: };
093:
094: protected static final class ACT_ERR implements Actor {
095: public String action(AbstractInterp ai, char c) {
096: return "ACT ERROR"; // NOI18N
097: }
098: };
099:
100: protected static final class ACT_REGULAR implements Actor {
101: public String action(AbstractInterp ai, char c) {
102: ai.ops.op_char(c);
103: return null;
104: }
105: };
106:
107: protected static final class ACT_CR implements Actor {
108: public String action(AbstractInterp ai, char c) {
109: ai.ops.op_carriage_return();
110: return null;
111: }
112: };
113:
114: protected static final class ACT_LF implements Actor {
115: public String action(AbstractInterp ai, char c) {
116: ai.ops.op_line_feed();
117: return null;
118: }
119: };
120:
121: protected static final class ACT_BS implements Actor {
122: public String action(AbstractInterp ai, char c) {
123: ai.ops.op_back_space();
124: return null;
125: }
126: };
127:
128: protected static final class ACT_TAB implements Actor {
129: public String action(AbstractInterp ai, char c) {
130: ai.ops.op_tab();
131: return null;
132: }
133: };
134:
135: protected static final class ACT_BEL implements Actor {
136: public String action(AbstractInterp ai, char c) {
137: ai.ops.op_bel();
138: return null;
139: }
140: }
141: }
142:
143: /*
144: * A stack for State
145: */
146: private Stack stack = new Stack();
147:
148: protected void push_state(State s) {
149: stack.push(s);
150: }
151:
152: protected State pop_state() {
153: return (State) stack.pop();
154: }
155:
156: protected void pop_all_states() {
157: while (!stack.empty())
158: stack.pop();
159: }
160:
161: protected String ctl_sequence = null;
162:
163: private InterpTypeDumb type;
164:
165: public static final InterpTypeDumb type_singleton = new InterpTypeDumb();
166:
167: public InterpDumb(Ops ops) {
168: super (ops);
169: this .type = type_singleton;
170: setup();
171: ctl_sequence = null;
172: }
173:
174: protected InterpDumb(Ops ops, InterpTypeDumb type) {
175: super (ops);
176: this .type = type;
177: setup();
178: ctl_sequence = null;
179: }
180:
181: public String name() {
182: return "dumb"; // NOI18N
183: }
184:
185: public void reset() {
186: super .reset();
187: pop_all_states();
188: state = type.st_base;
189: ctl_sequence = null;
190: }
191:
192: private void setup() {
193: state = type.st_base;
194: }
195:
196: private void reset_state_bad() {
197: /*
198: DEBUG
199: System.out.println("Unrecognized sequence in state " + state.name()); // NOI18N
200: if (ctl_sequence != null) {
201: for (int sx = 0; sx < ctl_sequence.length(); sx++)
202: System.out.print(String.valueOf((int)ctl_sequence.charAt(sx)) +
203: " "); // NOI18N
204: System.out.print("\n\t"); // NOI18N
205: for (int sx = 0; sx < ctl_sequence.length(); sx++)
206: System.out.print(ctl_sequence.charAt(sx) + " "); // NOI18N
207:
208: System.out.println(); // NOI18N
209: }
210: */
211: reset();
212: }
213:
214: public void processChar(char c) {
215:
216: // If we're collecting stuff into a control sequence remember the char
217: if (ctl_sequence != null)
218: ctl_sequence += c;
219:
220: try {
221: State.Action a = state.getAction(c);
222: /* DEBUG
223: if (a == null) {
224: System.out.println("null action in state " + state.name() + // NOI18N
225: " for char " + c + " = " + (int) c); // NOI18N
226: }
227: if (a.actor == null) {
228: System.out.println("null a.actor in state " + state.name() + // NOI18N
229: " for char " + c + " = " + (int) c); // NOI18N
230: }
231: */
232: String err_str = a.actor.action(this , c);
233: if (err_str != null) {
234: /* DEBUG
235: System.out.println("action error: " + err_str); // NOI18N
236: */
237: reset_state_bad();
238: return;
239: }
240:
241: if (a.new_state != null)
242: state = a.new_state;
243: else
244: ; // must be set by action, usually using pop_state()
245:
246: } finally {
247: if (state == type.st_base)
248: ctl_sequence = null;
249: }
250: }
251:
252: /*
253: * Actions ..............................................................
254: */
255:
256: }
|