001: /*
002: * TestChannelCmd.java --
003: *
004: * Copyright (c) 2001 Bruce A. Johnson
005: *
006: * See the file "license.terms" for information on usage and
007: * redistribution of this file, and for a DISCLAIMER OF ALL
008: * WARRANTIES.
009: *
010: * RCS: @(#) $Id: TestChannelCmd.java,v 1.4 2006/01/26 19:49:19 mdejong Exp $
011: *
012: */
013:
014: package tcl.lang;
015:
016: import java.util.Map;
017: import java.util.HashMap;
018: import java.util.Iterator;
019: import java.io.IOException;
020:
021: /**
022: * This class implements the built-in "testchannel" command in Tcl.
023: */
024:
025: class TestChannelCmd implements Command {
026: // FIXME: The C implementation in Tcl 8.4 seems to support
027: // a number of additional options
028:
029: static final private String validCmds[] = { "info",
030: "inputbuffered", "name", "open", "outputbuffered",
031: "queuedcr", "refcount", "type" };
032:
033: static final int OPT_INFO = 0;
034: static final int OPT_INPUTBUFFERED = 1;
035: static final int OPT_NAME = 2;
036: static final int OPT_OPEN = 3;
037: static final int OPT_OUTPUTBUFFERED = 4;
038: static final int OPT_QUEUEDCR = 5;
039: static final int OPT_REFCOUNT = 6;
040: static final int OPT_TYPE = 7;
041:
042: /**
043: * This procedure is invoked to process the "testchannel" Tcl command.
044: * See the user documentation for details on what it does.
045: *
046: * @param interp the current interpreter.
047: * @param argv command arguments.
048: */
049:
050: public void cmdProc(Interp interp, TclObject argv[])
051: throws TclException {
052: Channel chan;
053:
054: if (argv.length == 2) {
055: if (!argv[1].toString().equals("open")) {
056: throw new TclNumArgsException(interp, 1, argv,
057: "only option open can have two args");
058: }
059: } else if (argv.length != 3) {
060: throw new TclNumArgsException(interp, 1, argv,
061: "command channelId");
062: }
063:
064: if (argv.length == 2) {
065: // return list of all name/value pairs for this channelId
066: HashMap chanTable = TclIO.getInterpChanTable(interp);
067: TclObject list = TclList.newInstance();
068:
069: for (Iterator iter = chanTable.entrySet().iterator(); iter
070: .hasNext();) {
071: Map.Entry entry = (Map.Entry) iter.next();
072: chan = (Channel) entry.getValue();
073: TclList.append(interp, list, TclString.newInstance(chan
074: .getChanName()));
075: }
076: interp.setResult(list);
077: return;
078: }
079: chan = TclIO.getChannel(interp, argv[2].toString());
080: if (chan == null) {
081: throw new TclException(interp,
082: "can not find channel named \""
083: + argv[2].toString() + "\"");
084: }
085:
086: int index = TclIndex.get(interp, argv[1], validCmds, "option",
087: 0);
088: switch (index) {
089: case OPT_INFO: {
090: TclObject list = TclList.newInstance();
091: TclList.append(interp, list, argv[2]); // 0
092: TclList.append(interp, list, TclString.newInstance(chan
093: .getChanType())); // 1
094:
095: if (chan.isReadOnly() || chan.isReadWrite()) {
096: TclList.append(interp, list, TclString
097: .newInstance("read")); // 2
098: } else {
099: TclList.append(interp, list, TclString.newInstance(""));
100: }
101:
102: if (chan.isWriteOnly() || chan.isReadWrite()) {
103: TclList.append(interp, list, TclString
104: .newInstance("write")); // 3
105: } else {
106: TclList.append(interp, list, TclString.newInstance(""));
107: }
108:
109: if (chan.getBlocking()) {
110: TclList.append(interp, list, TclString
111: .newInstance("blocking")); // 4
112: } else {
113: TclList.append(interp, list, TclString
114: .newInstance("nonblocking"));
115: }
116:
117: if (chan.getBuffering() == TclIO.BUFF_FULL) {
118: TclList.append(interp, list, TclString
119: .newInstance("full")); // 5
120: } else if (chan.getBuffering() == TclIO.BUFF_LINE) {
121: TclList.append(interp, list, TclString
122: .newInstance("line"));
123: } else {
124: TclList.append(interp, list, TclString
125: .newInstance("none"));
126: }
127:
128: if (chan.isBgFlushScheduled()) {
129: TclList.append(interp, list, TclString
130: .newInstance("async_flush")); // 6
131: } else {
132: TclList.append(interp, list, TclString.newInstance(""));
133: }
134:
135: if (chan.eof()) {
136: TclList.append(interp, list, TclString
137: .newInstance("eof")); // 7
138: } else {
139: TclList.append(interp, list, TclString.newInstance(""));
140: }
141:
142: if (chan.isBlocked(interp)) {
143: TclList.append(interp, list, TclString
144: .newInstance("blocked")); // 8
145: } else {
146: TclList.append(interp, list, TclString
147: .newInstance("unblocked"));
148: }
149:
150: int translation = chan.getInputTranslation();
151:
152: if (translation == TclIO.TRANS_AUTO) {
153: TclList.append(interp, list, TclString
154: .newInstance("auto")); // 9
155:
156: TclList.append(interp, list, TclString.newInstance(chan
157: .inputSawCR() ? "queued_cr" : "")); // 10
158: } else if (translation == TclIO.TRANS_LF) {
159: TclList.append(interp, list, TclString
160: .newInstance("lf"));
161: TclList.append(interp, list, TclString.newInstance(""));
162: } else if (translation == TclIO.TRANS_CR) {
163: TclList.append(interp, list, TclString
164: .newInstance("cr"));
165: TclList.append(interp, list, TclString.newInstance(""));
166: } else if (translation == TclIO.TRANS_CRLF) {
167: TclList.append(interp, list, TclString
168: .newInstance("crlf"));
169: TclList.append(interp, list, TclString.newInstance(""));
170: }
171:
172: translation = chan.getOutputTranslation();
173:
174: if (translation == TclIO.TRANS_AUTO) {
175: TclList.append(interp, list, TclString
176: .newInstance("auto")); // 11
177: } else if (translation == TclIO.TRANS_LF) {
178: TclList.append(interp, list, TclString
179: .newInstance("lf"));
180: } else if (translation == TclIO.TRANS_CR) {
181: TclList.append(interp, list, TclString
182: .newInstance("cr"));
183: } else if (translation == TclIO.TRANS_CRLF) {
184: TclList.append(interp, list, TclString
185: .newInstance("crlf"));
186: }
187:
188: TclList.append(interp, list, TclInteger.newInstance(chan
189: .getNumBufferedInputBytes())); // 12
190:
191: TclList.append(interp, list, TclInteger.newInstance(chan
192: .getNumBufferedOutputBytes())); // 13
193:
194: try {
195: TclList.append(interp, list, TclInteger
196: .newInstance((int) chan.tell())); // 14
197: } catch (IOException e) {
198: throw new TclException(interp, e.toString());
199: }
200:
201: TclList.append(interp, list, TclInteger.newInstance(chan
202: .getRefCount())); // 15
203:
204: interp.setResult(list);
205: break;
206: }
207: case OPT_INPUTBUFFERED: {
208: interp.setResult(chan.getNumBufferedInputBytes());
209: break;
210: }
211: case OPT_NAME: {
212: interp.setResult(chan.getChanName());
213: break;
214: }
215: case OPT_OUTPUTBUFFERED: {
216: interp.setResult(chan.getNumBufferedOutputBytes());
217: break;
218: }
219: case OPT_QUEUEDCR: {
220: interp.setResult(chan.inputSawCR());
221: break;
222: }
223: case OPT_REFCOUNT: {
224: interp.setResult(chan.getRefCount());
225: break;
226: }
227: case OPT_TYPE: {
228: interp.setResult(chan.getChanType());
229: break;
230: }
231: default: {
232: throw new TclRuntimeError("TestChannel.cmdProc() error: "
233: + "incorrect index returned from TclIndex.get()");
234: }
235: }
236: }
237: }
|