001: /*
002: * StdChannel.java --
003: *
004: * Copyright (c) 1997 Sun Microsystems, Inc.
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: StdChannel.java,v 1.19 2003/03/08 03:42:44 mdejong Exp $
011: *
012: */
013:
014: package tcl.lang;
015:
016: import java.util.*;
017: import java.io.*;
018:
019: /**
020: * Subclass of the abstract class Channel. It implements all of the
021: * methods to perform read, write, open, close, etc on system stdio channels.
022: */
023:
024: class StdChannel extends Channel {
025:
026: /**
027: * stdType store which type, of the three below, this StdChannel is.
028: */
029:
030: private int stdType = -1;
031:
032: /**
033: * Flags indicating the type of this StdChannel.
034: */
035:
036: static final int STDIN = 0;
037: static final int STDOUT = 1;
038: static final int STDERR = 2;
039:
040: /**
041: * Constructor that does nothing. Open() must be called before
042: * any of the subsequent read, write, etc calls can be made.
043: */
044:
045: StdChannel() {
046: }
047:
048: /**
049: * Constructor that will automatically call open.
050: *
051: * @param stdName name of the stdio channel; stdin, stderr or stdout.
052: */
053:
054: StdChannel(String stdName) {
055: if (stdName.equals("stdin")) {
056: open(STDIN);
057: } else if (stdName.equals("stdout")) {
058: open(STDOUT);
059: } else if (stdName.equals("stderr")) {
060: open(STDERR);
061: } else {
062: throw new TclRuntimeError(
063: "Error: unexpected type for StdChannel");
064: }
065: }
066:
067: StdChannel(int type) {
068: open(type);
069: }
070:
071: /**
072: * Set the channel type to one of the three stdio types. Throw a
073: * tclRuntimeEerror if the stdName is not one of the three types. If
074: * it is a stdin channel, initialize the "in" data member. Since "in"
075: * is static it may have already be initialized, test for this case
076: * first. Set the names to fileX, this will be the key in the chanTable
077: * hashtable to access this object. Note: it is not put into the hash
078: * table in this function. The calling function is responsible for that.
079: *
080: * @param stdName String that equals stdin, stdout, stderr
081: * @return The name of the channelId
082: */
083:
084: String open(int type) {
085:
086: switch (type) {
087: case STDIN:
088: mode = TclIO.RDONLY;
089: setBuffering(TclIO.BUFF_LINE);
090: setChanName("stdin");
091: break;
092: case STDOUT:
093: mode = TclIO.WRONLY;
094: setBuffering(TclIO.BUFF_LINE);
095: setChanName("stdout");
096: break;
097: case STDERR:
098: mode = TclIO.WRONLY;
099: setBuffering(TclIO.BUFF_NONE);
100: setChanName("stderr");
101: break;
102: default:
103: throw new RuntimeException(
104: "type does not match one of STDIN, STDOUT, or STDERR");
105: }
106:
107: stdType = type;
108:
109: return getChanName();
110: }
111:
112: /**
113: * Write to stdout or stderr. If the stdType is not set to
114: * STDOUT or STDERR this is an error; either the stdType wasnt
115: * correctly initialized, or this was called on a STDIN channel.
116: *
117: * @param interp the current interpreter.
118: * @param s the string to write
119: */
120:
121: void write(Interp interp, TclObject outData) throws IOException,
122: TclException {
123:
124: checkWrite(interp);
125:
126: if (stdType == STDERR) {
127: System.err.print(outData.toString());
128: } else {
129: String s = outData.toString();
130: System.out.print(s);
131: if (buffering == TclIO.BUFF_NONE
132: || (buffering == TclIO.BUFF_LINE && s
133: .endsWith("\n"))) {
134: System.out.flush();
135: }
136: }
137: }
138:
139: /**
140: * Check for any output that might still need to be flushed
141: * when the channel is closed.
142: */
143:
144: void close() throws IOException {
145: super .close();
146:
147: if (stdType == STDOUT)
148: System.out.flush();
149: }
150:
151: String getChanType() {
152: return "tty";
153: }
154:
155: protected InputStream getInputStream() throws IOException {
156: return System.in;
157: }
158:
159: protected OutputStream getOutputStream() throws IOException {
160: throw new RuntimeException("should never be called");
161: }
162: }
|