01: /*
02: * PutsCmd.java
03: *
04: * Copyright (c) 1997 Cornell University.
05: * Copyright (c) 1997 Sun Microsystems, Inc.
06: *
07: * See the file "license.terms" for information on usage and
08: * redistribution of this file, and for a DISCLAIMER OF ALL
09: * WARRANTIES.
10: *
11: * RCS: @(#) $Id: PutsCmd.java,v 1.6 2002/01/21 06:34:26 mdejong Exp $
12: *
13: */
14:
15: package tcl.lang;
16:
17: import java.util.*;
18: import java.io.*;
19:
20: /**
21: * This class implements the built-in "puts" command in Tcl.
22: */
23:
24: class PutsCmd implements Command {
25: /**
26: * Prints the given string to a channel. See Tcl user
27: * documentation for details.
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: Channel chan; // The channel being operated on this method
37: String channelId; // String containing the key to chanTable
38: String arg; // Argv[i] converted to a string
39: int i = 1; // Index to the next arg in argv
40: boolean newline = true;
41: // Indicates to print a newline in result
42:
43: if ((argv.length >= 2)
44: && (argv[1].toString().equals("-nonewline"))) {
45: newline = false;
46: i++;
47: }
48: if ((i < argv.length - 3) || (i >= argv.length)) {
49: throw new TclNumArgsException(interp, 1, argv,
50: "?-nonewline? ?channelId? string");
51: }
52:
53: // The code below provides backwards compatibility with an old
54: // form of the command that is no longer recommended or documented.
55:
56: if (i == (argv.length - 3)) {
57: arg = argv[i + 2].toString();
58: if (!arg.equals("nonewline")) {
59: throw new TclException(interp, "bad argument \"" + arg
60: + "\": should be \"nonewline\"");
61: }
62: newline = false;
63: }
64:
65: if (i == (argv.length - 1)) {
66: channelId = "stdout";
67: } else {
68: channelId = argv[i].toString();
69: i++;
70: }
71:
72: if (i != (argv.length - 1)) {
73: throw new TclNumArgsException(interp, 1, argv,
74: "?-nonewline? ?channelId? string");
75: }
76:
77: chan = TclIO.getChannel(interp, channelId);
78: if (chan == null) {
79: throw new TclException(interp,
80: "can not find channel named \"" + channelId + "\"");
81: }
82:
83: try {
84: if (newline) {
85: chan.write(interp, argv[i]);
86: chan.write(interp, "\n");
87: } else {
88: chan.write(interp, argv[i]);
89: }
90: } catch (IOException e) {
91: throw new TclRuntimeError(
92: "PutsCmd.cmdProc() Error: IOException when putting "
93: + chan.getChanName());
94: }
95: }
96: }
|