01: /*
02: * SourceCmd.java
03: *
04: * Implements the "source" command.
05: *
06: * Copyright (c) 1997 Cornell University.
07: * Copyright (c) 1997 Sun Microsystems, Inc.
08: *
09: * See the file "license.terms" for information on usage and
10: * redistribution of this file, and for a DISCLAIMER OF ALL
11: * WARRANTIES.
12: *
13: * RCS: @(#) $Id: SourceCmd.java,v 1.2 2005/11/07 07:41:51 mdejong Exp $
14: *
15: */
16:
17: package tcl.lang;
18:
19: /*
20: * This class implements the built-in "source" command in Tcl.
21: */
22:
23: class SourceCmd implements Command {
24:
25: /*
26: *----------------------------------------------------------------------
27: *
28: * cmdProc --
29: *
30: * This cmdProc is invoked to process the "source" Tcl command.
31: * See the user documentation for details on what it does.
32: *
33: * Results:
34: * None.
35: *
36: * Side effects:
37: * A standard Tcl result is stored in the interpreter. See the
38: * user documentation.
39: *
40: *----------------------------------------------------------------------
41: */
42:
43: public void cmdProc(Interp interp, // Current interpreter.
44: TclObject argv[]) // Argument list.
45: throws TclException // Standard Tcl exception.
46: {
47: String fileName = null;
48: boolean url = false;
49:
50: if (argv.length == 2) {
51: fileName = argv[1].toString();
52: } else if (argv.length == 3) {
53: if (argv[1].toString().equals("-url")) {
54: url = true;
55: fileName = argv[2].toString();
56: }
57: }
58:
59: if (fileName == null) {
60: throw new TclNumArgsException(interp, 1, argv,
61: "?-url? fileName");
62: }
63:
64: try {
65: if (fileName.startsWith("resource:/")) {
66: interp.evalResource(fileName.substring(9));
67: } else if (url) {
68: interp.evalURL(null, fileName);
69: } else {
70: interp.evalFile(fileName);
71: }
72: } catch (TclException e) {
73: int code = e.getCompletionCode();
74:
75: if (code == TCL.RETURN) {
76: int realCode = interp.updateReturnInfo();
77: if (realCode != TCL.OK) {
78: e.setCompletionCode(realCode);
79: throw e;
80: }
81: } else if (code == TCL.ERROR) {
82: // Record information telling where the error occurred.
83:
84: interp.addErrorInfo("\n (file line "
85: + interp.errorLine + ")");
86: throw e;
87: } else {
88: throw e;
89: }
90: }
91: }
92:
93: } // end SourceCmd
|