01: /*
02: * GetsCmd.java --
03: *
04: * Copyright (c) 1997 Sun Microsystems, Inc.
05: *
06: * See the file "license.terms" for information on usage and
07: * redistribution of this file, and for a DISCLAIMER OF ALL
08: * WARRANTIES.
09: *
10: * RCS: @(#) $Id: GetsCmd.java,v 1.6 2003/03/08 03:42:44 mdejong Exp $
11: *
12: */
13:
14: package tcl.lang;
15:
16: import java.util.*;
17: import java.io.*;
18:
19: /**
20: * This class implements the built-in "gets" command in Tcl.
21: */
22:
23: class GetsCmd implements Command {
24:
25: /**
26: * This procedure is invoked to process the "gets" Tcl command.
27: * See the user documentation for details on what it does.
28: *
29: * @param interp the current interpreter.
30: * @param argv command arguments.
31: */
32:
33: public void cmdProc(Interp interp, TclObject argv[])
34: throws TclException {
35:
36: boolean writeToVar = false; // If true write to var passes as arg
37: String varName = ""; // The variable to write value to
38: Channel chan; // The channel being operated on
39: int lineLen;
40: TclObject line;
41:
42: if ((argv.length < 2) || (argv.length > 3)) {
43: throw new TclNumArgsException(interp, 1, argv,
44: "channelId ?varName?");
45: }
46:
47: if (argv.length == 3) {
48: writeToVar = true;
49: varName = argv[2].toString();
50: }
51:
52: chan = TclIO.getChannel(interp, argv[1].toString());
53: if (chan == null) {
54: throw new TclException(interp,
55: "can not find channel named \""
56: + argv[1].toString() + "\"");
57: }
58:
59: try {
60: line = TclString.newInstance(new StringBuffer(64));
61: lineLen = chan.read(interp, line, TclIO.READ_LINE, 0);
62: if (lineLen < 0) {
63: // FIXME: Need more specific posix error codes!
64: if (!chan.eof() && !chan.isBlocked(interp)) {
65: throw new TclPosixException(interp,
66: TclPosixException.EIO, true,
67: "error reading \"" + argv[1].toString()
68: + "\"");
69: }
70: lineLen = -1;
71: }
72: if (writeToVar) {
73: interp.setVar(varName, line, 0);
74: interp.setResult(lineLen);
75: } else {
76: interp.setResult(line);
77: }
78: } catch (IOException e) {
79: //e.printStackTrace(System.err);
80: throw new TclRuntimeError(
81: "GetsCmd.cmdProc() Error: IOException when getting "
82: + chan.getChanName() + ": "
83: + e.getMessage());
84: }
85:
86: }
87: }
|