001: /*
002: * FconfigureCmd.java --
003: *
004: * Copyright (c) 2001 Bruce A. Johnson
005: * Copyright (c) 1997 Sun Microsystems, Inc.
006: *
007: * See the file "license.terms" for information on usage and
008: * redistribution of this file, and for a DISCLAIMER OF ALL
009: * WARRANTIES.
010: *
011: * RCS: @(#) $Id: FconfigureCmd.java,v 1.13 2006/07/07 23:36:00 mdejong Exp $
012: *
013: */
014:
015: package tcl.lang;
016:
017: import java.util.*;
018:
019: /**
020: * This class implements the built-in "fconfigure" command in Tcl.
021: */
022:
023: class FconfigureCmd implements Command {
024:
025: static final private String validCmds[] = { "-blocking",
026: "-buffering", "-buffersize", "-encoding", "-eofchar",
027: "-translation", };
028:
029: static final int OPT_BLOCKING = 0;
030: static final int OPT_BUFFERING = 1;
031: static final int OPT_BUFFERSIZE = 2;
032: static final int OPT_ENCODING = 3;
033: static final int OPT_EOFCHAR = 4;
034: static final int OPT_TRANSLATION = 5;
035:
036: /**
037: * This procedure is invoked to process the "fconfigure" Tcl command.
038: * See the user documentation for details on what it does.
039: *
040: * @param interp the current interpreter.
041: * @param argv command arguments.
042: */
043:
044: public void cmdProc(Interp interp, TclObject argv[])
045: throws TclException {
046:
047: Channel chan; // The channel being operated on this method
048:
049: if ((argv.length < 2)
050: || (((argv.length % 2) == 1) && (argv.length != 3))) {
051: throw new TclNumArgsException(interp, 1, argv,
052: "channelId ?optionName? ?value? ?optionName value?...");
053: }
054:
055: chan = TclIO.getChannel(interp, argv[1].toString());
056: if (chan == null) {
057: throw new TclException(interp,
058: "can not find channel named \""
059: + argv[1].toString() + "\"");
060: }
061:
062: if (argv.length == 2) {
063: // return list of all name/value pairs for this channelId
064: TclObject list = TclList.newInstance();
065:
066: TclList.append(interp, list, TclString
067: .newInstance("-blocking"));
068: TclList.append(interp, list, TclBoolean.newInstance(chan
069: .getBlocking()));
070:
071: TclList.append(interp, list, TclString
072: .newInstance("-buffering"));
073: TclList.append(interp, list, TclString.newInstance(TclIO
074: .getBufferingString(chan.getBuffering())));
075:
076: TclList.append(interp, list, TclString
077: .newInstance("-buffersize"));
078: TclList.append(interp, list, TclInteger.newInstance(chan
079: .getBufferSize()));
080:
081: // -encoding
082:
083: TclList.append(interp, list, TclString
084: .newInstance("-encoding"));
085:
086: String javaEncoding = chan.getEncoding();
087: String tclEncoding;
088: if (javaEncoding == null) {
089: tclEncoding = "binary";
090: } else {
091: tclEncoding = EncodingCmd.getTclName(javaEncoding);
092: }
093: TclList.append(interp, list, TclString
094: .newInstance(tclEncoding));
095:
096: // -eofchar
097:
098: TclList.append(interp, list, TclString
099: .newInstance("-eofchar"));
100: if (chan.isReadOnly()) {
101: char eofChar = chan.getInputEofChar();
102: TclList.append(interp, list, (eofChar == 0) ? TclString
103: .newInstance("") : TclString
104: .newInstance(eofChar));
105: } else if (chan.isWriteOnly()) {
106: char eofChar = chan.getOutputEofChar();
107: TclList.append(interp, list, (eofChar == 0) ? TclString
108: .newInstance("") : TclString
109: .newInstance(eofChar));
110: } else if (chan.isReadWrite()) {
111: char inEofChar = chan.getInputEofChar();
112: char outEofChar = chan.getOutputEofChar();
113:
114: TclObject eofchar_pair = TclList.newInstance();
115:
116: TclList.append(interp, eofchar_pair,
117: (inEofChar == 0) ? TclString.newInstance("")
118: : TclString.newInstance(inEofChar));
119:
120: TclList.append(interp, eofchar_pair,
121: (outEofChar == 0) ? TclString.newInstance("")
122: : TclString.newInstance(outEofChar));
123:
124: TclList.append(interp, list, eofchar_pair);
125: } else {
126: // Not readable or writeable, do nothing
127: }
128:
129: // -translation
130:
131: TclList.append(interp, list, TclString
132: .newInstance("-translation"));
133:
134: if (chan.isReadOnly()) {
135: TclList.append(interp, list, TclString
136: .newInstance(TclIO.getTranslationString(chan
137: .getInputTranslation())));
138: } else if (chan.isWriteOnly()) {
139: TclList.append(interp, list, TclString
140: .newInstance(TclIO.getTranslationString(chan
141: .getOutputTranslation())));
142: } else if (chan.isReadWrite()) {
143: TclObject translation_pair = TclList.newInstance();
144:
145: TclList.append(interp, translation_pair, TclString
146: .newInstance(TclIO.getTranslationString(chan
147: .getInputTranslation())));
148: TclList.append(interp, translation_pair, TclString
149: .newInstance(TclIO.getTranslationString(chan
150: .getOutputTranslation())));
151:
152: TclList.append(interp, list, translation_pair);
153: } else {
154: // Not readable or writeable, do nothing
155: }
156:
157: interp.setResult(list);
158: }
159:
160: if (argv.length == 3) {
161: // return value for supplied name
162:
163: int index = TclIndex.get(interp, argv[2], validCmds,
164: "option", 0);
165:
166: switch (index) {
167: case OPT_BLOCKING: { // -blocking
168: interp.setResult(chan.getBlocking());
169: break;
170: }
171: case OPT_BUFFERING: { // -buffering
172: interp.setResult(TclIO.getBufferingString(chan
173: .getBuffering()));
174: break;
175: }
176: case OPT_BUFFERSIZE: { // -buffersize
177: interp.setResult(chan.getBufferSize());
178: break;
179: }
180: case OPT_ENCODING: { // -encoding
181: String javaEncoding = chan.getEncoding();
182: if (javaEncoding == null) {
183: interp.setResult("binary");
184: } else {
185: interp.setResult(EncodingCmd
186: .getTclName(javaEncoding));
187: }
188: break;
189: }
190: case OPT_EOFCHAR: { // -eofchar
191: if (chan.isReadOnly()) {
192: char eofChar = chan.getInputEofChar();
193: interp.setResult((eofChar == 0) ? TclString
194: .newInstance("") : TclString
195: .newInstance(eofChar));
196: } else if (chan.isWriteOnly()) {
197: char eofChar = chan.getOutputEofChar();
198: interp.setResult((eofChar == 0) ? TclString
199: .newInstance("") : TclString
200: .newInstance(eofChar));
201: } else if (chan.isReadWrite()) {
202: char inEofChar = chan.getInputEofChar();
203: char outEofChar = chan.getOutputEofChar();
204:
205: TclObject eofchar_pair = TclList.newInstance();
206:
207: TclList.append(interp, eofchar_pair,
208: (inEofChar == 0) ? TclString
209: .newInstance("") : TclString
210: .newInstance(inEofChar));
211:
212: TclList.append(interp, eofchar_pair,
213: (outEofChar == 0) ? TclString
214: .newInstance("") : TclString
215: .newInstance(outEofChar));
216:
217: interp.setResult(eofchar_pair);
218: } else {
219: // Not readable or writeable, do nothing
220: }
221:
222: break;
223: }
224: case OPT_TRANSLATION: { // -translation
225: if (chan.isReadOnly()) {
226: interp.setResult(TclIO.getTranslationString(chan
227: .getInputTranslation()));
228: } else if (chan.isWriteOnly()) {
229: interp.setResult(TclIO.getTranslationString(chan
230: .getOutputTranslation()));
231: } else if (chan.isReadWrite()) {
232: TclObject translation_pair = TclList.newInstance();
233:
234: TclList.append(interp, translation_pair, TclString
235: .newInstance(TclIO
236: .getTranslationString(chan
237: .getInputTranslation())));
238: TclList.append(interp, translation_pair, TclString
239: .newInstance(TclIO
240: .getTranslationString(chan
241: .getOutputTranslation())));
242:
243: interp.setResult(translation_pair);
244: } else {
245: // Not readable or writeable, do nothing
246: }
247:
248: break;
249: }
250: default: {
251: throw new TclRuntimeError(
252: "Fconfigure.cmdProc() error: "
253: + "incorrect index returned from TclIndex.get()");
254: }
255: }
256: }
257: for (int i = 3; i < argv.length; i += 2) {
258: // Iterate through the list setting the name with the
259: // corresponding value.
260:
261: int index = TclIndex.get(interp, argv[i - 1], validCmds,
262: "option", 0);
263:
264: switch (index) {
265: case OPT_BLOCKING: { // -blocking
266: chan.setBlocking(TclBoolean.get(interp, argv[i]));
267: break;
268: }
269: case OPT_BUFFERING: { // -buffering
270: int id = TclIO.getBufferingID(argv[i].toString());
271:
272: if (id == -1) {
273: throw new TclException(interp,
274: "bad value for -buffering: must be "
275: + "one of full, line, or none");
276: }
277:
278: chan.setBuffering(id);
279: break;
280: }
281: case OPT_BUFFERSIZE: { // -buffersize
282: chan.setBufferSize(TclInteger.get(interp, argv[i]));
283: break;
284: }
285: case OPT_ENCODING: { // -encoding
286: String tclEncoding = argv[i].toString();
287:
288: if (tclEncoding.equals("")
289: || tclEncoding.equals("binary")) {
290: chan.setEncoding(null);
291: } else {
292: String javaEncoding = EncodingCmd
293: .getJavaName(tclEncoding);
294: if (javaEncoding == null) {
295: throw new TclException(interp,
296: "unknown encoding \"" + tclEncoding
297: + "\"");
298: }
299: if (!EncodingCmd.isSupported(javaEncoding)) {
300: throw new TclException(interp,
301: "unsupported encoding \"" + tclEncoding
302: + "\"");
303: }
304: chan.setEncoding(javaEncoding);
305: }
306:
307: break;
308: }
309: case OPT_EOFCHAR: { // -eofchar
310: int length = TclList.getLength(interp, argv[i]);
311:
312: if (length > 2) {
313: throw new TclException(
314: interp,
315: "bad value for -eofchar: "
316: + "should be a list of zero, one, or two elements");
317: }
318:
319: char inputEofChar, outputEofChar;
320: String s;
321:
322: if (length == 0) {
323: inputEofChar = outputEofChar = 0;
324: } else if (length == 1) {
325: s = TclList.index(interp, argv[i], 0).toString();
326: inputEofChar = outputEofChar = s.charAt(0);
327: } else {
328: s = TclList.index(interp, argv[i], 0).toString();
329: inputEofChar = s.charAt(0);
330:
331: s = TclList.index(interp, argv[i], 1).toString();
332: outputEofChar = s.charAt(0);
333: }
334:
335: chan.setInputEofChar(inputEofChar);
336: chan.setOutputEofChar(outputEofChar);
337:
338: break;
339: }
340: case OPT_TRANSLATION: { // -translation
341: int length = TclList.getLength(interp, argv[i]);
342:
343: if (length < 1 || length > 2) {
344: throw new TclException(
345: interp,
346: "bad value for -translation: "
347: + "must be a one or two element list");
348: }
349:
350: String inputTranslationArg, outputTranslationArg;
351: int inputTranslation, outputTranslation;
352:
353: if (length == 2) {
354: inputTranslationArg = TclList.index(interp,
355: argv[i], 0).toString();
356: inputTranslation = TclIO
357: .getTranslationID(inputTranslationArg);
358: outputTranslationArg = TclList.index(interp,
359: argv[i], 1).toString();
360: outputTranslation = TclIO
361: .getTranslationID(outputTranslationArg);
362: } else {
363: outputTranslationArg = inputTranslationArg = argv[i]
364: .toString();
365: outputTranslation = inputTranslation = TclIO
366: .getTranslationID(outputTranslationArg);
367: }
368:
369: if ((inputTranslation == -1)
370: || (outputTranslation == -1)) {
371: throw new TclException(
372: interp,
373: "bad value for -translation: "
374: + "must be one of auto, binary, cr, lf, "
375: + "crlf, or platform");
376: }
377:
378: if (outputTranslation == TclIO.TRANS_AUTO)
379: outputTranslation = TclIO.TRANS_PLATFORM;
380:
381: if (chan.isReadOnly()) {
382: chan.setInputTranslation(inputTranslation);
383: if (inputTranslationArg.equals("binary")) {
384: chan.setEncoding(null);
385: }
386: } else if (chan.isWriteOnly()) {
387: chan.setOutputTranslation(outputTranslation);
388: if (outputTranslationArg.equals("binary")) {
389: chan.setEncoding(null);
390: }
391: } else if (chan.isReadWrite()) {
392: chan.setInputTranslation(inputTranslation);
393: chan.setOutputTranslation(outputTranslation);
394: if (inputTranslationArg.equals("binary")
395: || outputTranslationArg.equals("binary")) {
396: chan.setEncoding(null);
397: }
398: } else {
399: // Not readable or writeable, do nothing
400: }
401:
402: break;
403: }
404: default: {
405: throw new TclRuntimeError(
406: "Fconfigure.cmdProc() error: "
407: + "incorrect index returned from TclIndex.get()");
408: }
409: }
410: }
411: }
412: }
|