Skip to content
Tcl Source Code Artifact [c1a0660749]
Login

Artifact [c1a0660749]

Artifact c1a06607490d49bc2384ce19ba763348d278bad3:

Attachment "tip103.diff" to ticket [570201ffff] added by pspjuth on 2002-06-18 03:40:25.
Index: generic/tclBasic.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclBasic.c,v
retrieving revision 1.59
diff -u -r1.59 tclBasic.c
--- generic/tclBasic.c	12 Jun 2002 19:36:14 -0000	1.59
+++ generic/tclBasic.c	17 Jun 2002 20:33:43 -0000
@@ -96,6 +96,8 @@
         (CompileProc *) NULL,		1},
     {"exit",		(Tcl_CmdProc *) NULL,	Tcl_ExitObjCmd,
         (CompileProc *) NULL,		0},
+    {"expand",		(Tcl_CmdProc *) NULL,	Tcl_ExpandObjCmd,
+        (CompileProc *) NULL,		1},
     {"expr",		(Tcl_CmdProc *) NULL,	Tcl_ExprObjCmd,
         TclCompileExprCmd,		1},
     {"fcopy",		(Tcl_CmdProc *) NULL,	Tcl_FcopyObjCmd,
Index: generic/tclCmdAH.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclCmdAH.c,v
retrieving revision 1.25
diff -u -r1.25 tclCmdAH.c
--- generic/tclCmdAH.c	13 Jun 2002 09:40:00 -0000	1.25
+++ generic/tclCmdAH.c	17 Jun 2002 20:33:43 -0000
@@ -30,6 +30,33 @@
 static char *		GetTypeFromMode _ANSI_ARGS_((int mode));
 static int		StoreStatData _ANSI_ARGS_((Tcl_Interp *interp,
 			    char *varName, Tcl_StatBuf *statPtr));
+static void		FreeExpandInternalRep _ANSI_ARGS_((Tcl_Obj *objPtr));
+static int		SetExpandFromAny _ANSI_ARGS_((Tcl_Interp *interp,
+			    Tcl_Obj *objPtr));
+
+/*
+ * The structure below defines the "expand" Tcl object type by means of
+ * procedures that can be invoked by generic object code.
+ */
+
+Tcl_ObjType tclExpandType = {
+    "expand",				/* name */
+    FreeExpandInternalRep,		/* freeIntRepPro */
+    NULL,				/* dupIntRepProc */
+    NULL,				/* updateStringProc */
+    SetExpandFromAny			/* setFromAnyProc */
+};
+
+/*
+ * The following structure is the internal rep for an Expand object.
+ */
+
+typedef struct ExpandData {
+    int numWords;
+    Tcl_Obj *cmd;
+    int *expList;
+} ExpandData;
+
 
 /*
  *----------------------------------------------------------------------
@@ -667,6 +694,247 @@
     Tcl_Exit(value);
     /*NOTREACHED*/
     return TCL_OK;			/* Better not ever reach this! */
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * SetExpandFromAny --
+ *
+ *	Create an internal representation of type "Expand" for an object.
+ *
+ * Results:
+ *	The return value is TCL_OK or TCL_ERROR. If an error occurs during
+ *	conversion, an error message is left in the interpreter's result
+ *	unless "interp" is NULL.
+ *
+ * Side effects:
+ *	Any old internal representation for objPtr is freed and the
+ *	internal representation is set to "Expand".
+ *
+ *----------------------------------------------------------------------
+ */
+
+static int
+SetExpandFromAny(interp, objPtr)
+    Tcl_Interp *interp;		/* Used for error reporting if not NULL. */
+    register Tcl_Obj *objPtr;	/* The object to convert. */
+{
+    ExpandData *expandPtr;
+    char *script, *cmd, *next;
+    int length, i;
+    Tcl_Parse parse, parse2;
+    Tcl_Token *tokenPtr;
+
+    if (objPtr->typePtr == &tclExpandType) {
+	return TCL_OK;
+    }
+
+    /*
+     * Parse the command into words.
+     */
+
+    script = Tcl_GetStringFromObj(objPtr, &length);
+    if (Tcl_ParseCommand(interp, script, length, 0, &parse) != TCL_OK) {
+	return TCL_ERROR;
+    }
+
+    /*
+     * Check if the rest contains any command.
+     */
+
+    next = parse.commandStart + parse.commandSize;
+    if (*next != 0) {
+	if (Tcl_ParseCommand(interp, next, -1, 0, &parse2) != TCL_OK) {
+	    Tcl_FreeParse(&parse);
+	    return TCL_ERROR;
+	}
+	if (parse2.numWords > 0) {
+	    Tcl_FreeParse(&parse);
+	    Tcl_FreeParse(&parse2);
+	    if (interp != NULL) {
+		Tcl_SetResult(interp,
+			"only one statement is allowed in expand",
+			TCL_STATIC);
+	    }
+	    return TCL_ERROR;
+	}
+	Tcl_FreeParse(&parse2);
+    }
+
+    /*
+     * Turn into an Expand object
+     */
+
+    if ((objPtr->typePtr != NULL) &&
+	    (objPtr->typePtr->freeIntRepProc != NULL)) {
+	(*objPtr->typePtr->freeIntRepProc)(objPtr);
+    }
+    expandPtr = (ExpandData *) ckalloc(sizeof(ExpandData));
+    objPtr->typePtr = &tclExpandType;
+    objPtr->internalRep.otherValuePtr = (VOID *) expandPtr;
+
+    expandPtr->numWords = parse.numWords;
+
+    if (parse.numWords < 1) {
+	Tcl_FreeParse(&parse);
+	return TCL_OK;
+    }
+
+    /*
+     * Build a command string consisting of 'list' followed by
+     * the command's words with any "@" removed.
+     * This command string when evaluated will return a list of the
+     * command's words after all substitutions.
+     */
+
+    cmd = ckalloc(6 + length);
+    expandPtr->expList = (int *) ckalloc(sizeof(int) * expandPtr->numWords);
+    strcpy(cmd , "list");
+
+    for (i = 0, tokenPtr = parse.tokenPtr; i < parse.numWords;
+	 i++, tokenPtr += (tokenPtr->numComponents + 1)) {
+	strcat(cmd, " ");
+	if (tokenPtr->start[0] == '@') {
+	    strncat(cmd, tokenPtr->start + 1, tokenPtr->size - 1);
+	    expandPtr->expList[i] = 1;
+	} else {
+	    strncat(cmd, tokenPtr->start, tokenPtr->size);
+	    expandPtr->expList[i] = 0;
+	}
+    }
+    Tcl_FreeParse(&parse);
+
+    /*
+     * Put the command string in an object so it can be
+     * byte compiled when evaluated.
+     */
+
+    expandPtr->cmd = Tcl_NewStringObj(cmd, -1);
+    Tcl_IncrRefCount(expandPtr->cmd);
+    ckfree(cmd);
+
+    return TCL_OK;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * FreeExpandInternalRep --
+ *
+ *	Deallocate the storage associated with a Expand data object's
+ *	internal representation.
+ *
+ * Results:
+ *	None.
+ *
+ * Side effects:
+ *	Frees memory. 
+ *
+ *----------------------------------------------------------------------
+ */
+
+static void
+FreeExpandInternalRep(objPtr)
+    Tcl_Obj *objPtr;		/* Object with internal rep to free. */
+{
+    ExpandData *expandPtr = (ExpandData *) objPtr->internalRep.otherValuePtr;
+    if (expandPtr->numWords > 0) {
+	Tcl_DecrRefCount(expandPtr->cmd);
+	ckfree((char *) expandPtr->expList);
+    }
+    ckfree((char *) expandPtr);
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * Tcl_ExpandObjCmd --
+ *
+ *	This procedure is invoked to process the "expand" Tcl command.
+ *	See the user documentation for details on what it does.
+ *
+ * Results:
+ *	A standard Tcl object result.
+ *
+ * Side effects:
+ *	See the user documentation.
+ *
+ *----------------------------------------------------------------------
+ */
+
+	/* ARGSUSED */
+int
+Tcl_ExpandObjCmd(dummy, interp, objc, objv)
+    ClientData dummy;		/* Not used. */
+    Tcl_Interp *interp;		/* Current interpreter. */
+    int objc;			/* Number of arguments. */
+    Tcl_Obj *CONST objv[];	/* Argument objects. */
+{
+    int i, code = TCL_OK;
+    ExpandData *expandPtr;
+    Tcl_Obj *expCmd, *res;
+    Tcl_Obj **resObjv;
+    int resObjc;
+    int all = 0;
+
+    if (objc != 2) {
+        Tcl_WrongNumArgs(interp, 1, objv, "command");
+	return TCL_ERROR;
+    }
+
+    if (SetExpandFromAny(interp, objv[1]) != TCL_OK) {
+	return TCL_ERROR;
+    }
+
+    expandPtr = (ExpandData *) objv[1]->internalRep.otherValuePtr;
+
+    if (expandPtr->numWords < 1) {
+	return TCL_OK;
+    }
+
+    /*
+     * Eval the converted command string.
+     */
+
+    code = Tcl_EvalObjEx(interp, expandPtr->cmd, 0);
+    if (code != TCL_OK) {
+	return code;
+    }
+
+    /*
+     * Get the resulting list and perform expansion.
+     */
+
+    res = Tcl_GetObjResult(interp);
+    code = Tcl_ListObjGetElements(interp, res, &resObjc, &resObjv);
+    if (code != TCL_OK) {
+	return TCL_ERROR;
+    }
+
+    expCmd = Tcl_NewListObj(0, NULL);
+    Tcl_IncrRefCount(expCmd);
+
+    for (i = 0; i < expandPtr->numWords && i < resObjc; i++) {
+	if (all || expandPtr->expList[i]) {
+	    /* Expanded argument, append its elements */
+	    if (Tcl_ListObjAppendList(interp, expCmd, resObjv[i]) != TCL_OK) {
+		Tcl_DecrRefCount(expCmd);
+		return TCL_ERROR;
+	    }
+	} else {
+	    /* Not expanded, just append to command */
+	    Tcl_ListObjAppendElement(interp, expCmd, resObjv[i]);
+	}
+    }
+
+    /*
+     * Execute the expanded command.
+     */
+
+    code = Tcl_EvalObjEx(interp, expCmd, TCL_EVAL_DIRECT);
+    Tcl_DecrRefCount(expCmd);
+    return code;
 }
 
 /*
Index: generic/tclInt.h
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclInt.h,v
retrieving revision 1.94
diff -u -r1.94 tclInt.h
--- generic/tclInt.h	13 Jun 2002 09:40:00 -0000	1.94
+++ generic/tclInt.h	17 Jun 2002 20:33:44 -0000
@@ -1592,6 +1592,7 @@
 extern Tcl_ObjType	tclListType;
 extern Tcl_ObjType	tclProcBodyType;
 extern Tcl_ObjType	tclStringType;
+extern Tcl_ObjType	tclExpandType;
 extern Tcl_ObjType	tclArraySearchType;
 extern Tcl_ObjType	tclIndexType;
 extern Tcl_ObjType	tclNsNameType;
@@ -1969,6 +1970,8 @@
 EXTERN int	Tcl_ExecObjCmd _ANSI_ARGS_((ClientData clientData,
 		    Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));
 EXTERN int	Tcl_ExitObjCmd _ANSI_ARGS_((ClientData clientData,
+		    Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));
+EXTERN int	Tcl_ExpandObjCmd _ANSI_ARGS_((ClientData clientData,
 		    Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));
 EXTERN int	Tcl_ExprObjCmd _ANSI_ARGS_((ClientData clientData,
 		    Tcl_Interp *interp, int objc, Tcl_Obj *CONST objv[]));
Index: generic/tclObj.c
===================================================================
RCS file: /cvsroot/tcl/tcl/generic/tclObj.c,v
retrieving revision 1.33
diff -u -r1.33 tclObj.c
--- generic/tclObj.c	26 Apr 2002 08:34:35 -0000	1.33
+++ generic/tclObj.c	17 Jun 2002 20:33:44 -0000
@@ -237,6 +237,7 @@
     Tcl_RegisterObjType(&tclWideIntType);
 #endif
     Tcl_RegisterObjType(&tclStringType);
+    Tcl_RegisterObjType(&tclExpandType);
     Tcl_RegisterObjType(&tclListType);
     Tcl_RegisterObjType(&tclByteCodeType);
     Tcl_RegisterObjType(&tclProcBodyType);
Index: tests/cmdAH.test
===================================================================
RCS file: /cvsroot/tcl/tcl/tests/cmdAH.test,v
retrieving revision 1.20
diff -u -r1.20 cmdAH.test
--- tests/cmdAH.test	7 May 2002 18:03:04 -0000	1.20
+++ tests/cmdAH.test	17 Jun 2002 20:33:45 -0000
@@ -1616,6 +1616,61 @@
     safeInterp eval [list file channels]
 } {stdout}
 
+set l1 [list a {b b} c d]
+set l2 [list e f {g g} h]
+proc l3 {} {
+    list i j k {l l}
+}
+
+test cmdAH-32.1 {expand command: errors} {
+    list [catch {expand} msg] $msg
+} {1 {wrong # args: should be "expand command"}}
+
+test cmdAH-32.2 {expand command: errors} {
+    list [catch {expand apa bepa} msg] $msg
+} {1 {wrong # args: should be "expand command"}}
+
+test cmdAH-32.3 {expand command: errors} {
+    list [catch {expand {list 1 ; list 2}} msg] $msg
+} {1 {only one statement is allowed in expand}}
+
+test cmdAH-32.4 {expand command: Accept whitespace and comments} {
+    expand { # A comment
+
+        # Another comment
+        list 1 2 \
+                3 @$l1
+
+        # Comment again
+    }
+} {1 2 3 a {b b} c d}
+
+test cmdAH-32.5 {expand command: no expansion} {
+    expand {list $l1 $l2 [l3]}
+} {{a {b b} c d} {e f {g g} h} {i j k {l l}}}
+
+test cmdAH-32.6 {expand command: expansion} {
+    expand {list @$l1 $l2 @[l3]}
+} {a {b b} c d {e f {g g} h} i j k {l l}}
+
+test cmdAH-32.7 {expand command: odd case} {
+    expand {list @$l1$l2}
+} {a {b b} c de f {g g} h}
+
+test cmdAH-32.8 {expand command: odd case and error detection} {
+    list [catch {expand {list @[l3]$l1}} msg] $msg
+} {1 {list element in braces followed by "a" instead of space}}
+
+test cmdAH-32.9 {expand command: Not all @ should trigger} {
+    set l1 [list a b c d]
+    set l2 [list e f g h]
+    expand {list @$l1 \@$l2 "@$l1" {@ i j k}}
+} {a b c d {@e f g h} {@a b c d} {@ i j k}}
+
+unset l1
+unset l2
+rename l3 {}
+
 # This shouldn't work, but just in case a test above failed...
 catch {close $newFileId}