01: /*
02: * UpvarCmd.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: UpvarCmd.java,v 1.4 2006/03/15 23:07:22 mdejong Exp $
12: *
13: */
14:
15: package tcl.lang;
16:
17: /**
18: * This class implements the built-in "upvar" command in Tcl.
19: */
20:
21: class UpvarCmd implements Command {
22: /**
23: * Tcl_UpvarObjCmd -> UpvarCmd.cmdProc
24: *
25: * This procedure is invoked to process the "upvar" Tcl command.
26: * See the user documentation for details on what it does.
27: */
28:
29: public void cmdProc(Interp interp, TclObject[] objv)
30: throws TclException {
31: CallFrame frame;
32: String frameSpec, otherVarName, myVarName;
33: int p;
34: int objc = objv.length, objv_index;
35: int result;
36:
37: if (objv.length < 3) {
38: throw new TclNumArgsException(interp, 1, objv,
39: "?level? otherVar localVar ?otherVar localVar ...?");
40: }
41:
42: // Find the call frame containing each of the "other variables" to be
43: // linked to.
44:
45: frameSpec = objv[1].toString();
46: // Java does not support passing a reference by refernece so use an array
47: CallFrame[] frameArr = new CallFrame[1];
48: result = CallFrame.getFrame(interp, frameSpec, frameArr);
49: frame = frameArr[0];
50: objc -= result + 1;
51: if ((objc & 1) != 0) {
52: throw new TclNumArgsException(interp, 1, objv,
53: "?level? otherVar localVar ?otherVar localVar ...?");
54: }
55: objv_index = result + 1;
56:
57: // Iterate over each (other variable, local variable) pair.
58: // Divide the other variable name into two parts, then call
59: // MakeUpvar to do all the work of linking it to the local variable.
60:
61: for (; objc > 0; objc -= 2, objv_index += 2) {
62: myVarName = objv[objv_index + 1].toString();
63: otherVarName = objv[objv_index].toString();
64:
65: int otherLength = otherVarName.length();
66: p = otherVarName.indexOf('(');
67: if ((p != -1)
68: && (otherVarName.charAt(otherLength - 1) == ')')) {
69: // This is an array variable name
70: Var.makeUpvar(interp, frame, otherVarName.substring(0,
71: p), otherVarName.substring(p + 1,
72: otherLength - 1), 0, myVarName, 0, -1);
73: } else {
74: // This is a scalar variable name
75: Var.makeUpvar(interp, frame, otherVarName, null, 0,
76: myVarName, 0, -1);
77: }
78: }
79: interp.resetResult();
80: return;
81: }
82: }
|