proc alias {old new} {
set oldArgs [info args $old]
set newArgs {}
foreach arg $oldArgs {
if [info default $old $arg default] {
lappend newArgs [list $arg $default]
} else {
lappend newArgs $arg
}
}
proc $new $newArgs [info body $old]
}
Does anyone have a better plain-Tcl solution?
Thanks,
David
--
---
David Megginson Department of English, University of Ottawa,
dmeg...@aix1.uottawa.ca Ottawa, Ontario, CANADA K1N 6N5
dmeg...@acadvm1.uottawa.ca Phone: (613) 564-6850 (Office)
ak...@freenet.carleton.ca (613) 564-9175 (FAX)
proc alias {old new} {
proc $new {args} "uplevel 1 \[concat \[list {$old} \] \$args\]"
}
I'm not sure if it "better".
==========================================================================
* Gerald W. Lester ! Voice: (504)-889-2784 *
* Computerized Processes Unlimited ! FAX: (504)-889-2799 *
* 4200 S. I-10 Service Road, Suite #205 ! E-Mail: g...@cpu.com *
* Metairie, LA 70001 ! *
==========================================================================
The following procedure is used in Wafe for implementing aliasing in C.
Once the alias is defined, it has no runtime overhead compared with
the real command.
Since this is take straight out of wafe, you will need for the
compilation tclInt.h and it is necessary to strip out the DBUG statements
and replace wafeInterpreter by an argument passed to alias() or similar.
Nearly half of the code below was automatically generated
by Wafe's c code generator.
-gustaf
/*
* the real work
*/
static int
alias(newCmdName,oldCmdName)
String newCmdName;
String oldCmdName;
{
XtPointer cmdPtr;
Interp *iPtr = (Interp *) wafeInterpreter;
Tcl_HashEntry *hPtr;
int new;
if ((hPtr = Tcl_FindHashEntry(&iPtr->commandTable, newCmdName)) != NULL)
{
fprintf(stderr,
"Wafe(alias): can't alias to '%s': command already exists\n",
newCmdName);
/* Tcl_AppendResult(wafeInterpreter, "can't alias to \"", newCmdName,
"\": command already exists", (char *) NULL); */
return 0;
}
if ((hPtr = Tcl_FindHashEntry(&iPtr->commandTable, oldCmdName)) == NULL)
{
fprintf(stderr,
"Wafe(alias): can't alias '%s': command doesn't exist\n",
oldCmdName);
/* Tcl_AppendResult(wafeInterpreter, "can't alias \"", oldCmdName,
"\": command doesn't exist", (char *) NULL); */
return 0;
}
cmdPtr = (XtPointer) Tcl_GetHashValue(hPtr);
hPtr = Tcl_CreateHashEntry(&iPtr->commandTable, newCmdName, &new);
Tcl_SetHashValue(hPtr, cmdPtr);
return 1;
}
/*
* argument passing
*/
static int
cmd_alias(clientData, comInterpreter, argc, argv)
ClientData clientData;
Tcl_Interp *comInterpreter;
int argc;
char **argv;
{
int returnVar;
char conversionBuffer[100];
DBUG_ENTER("alias");
if (argc != 3)
{
wafeArgcError("alias","",2,argc);
DBUG_RETURN (TCL_ERROR);
}
sprintf(conversionBuffer, "%d", returnVar);
Tcl_SetResult(comInterpreter, conversionBuffer, TCL_VOLATILE);
DBUG_RETURN (TCL_OK);
}
/*
* command registration
*/
...
Tcl_CreateCommand(wafeInterpreter, "alias", cmd_alias, NULL, NULL);
...
--
Gustaf Neumann neu...@watson.ibm.com
Postdoctoral/Visiting Scientist Tel: (914) 784 7086
IBM T.J.Watson Research Center, P.O.Box 704
Yorktown Heights, New York 10598
I'm afraid this probably isn't better. It may work in most cases, but
if "old" contains special characters then it won't work. For example,
alias "{foo" bar
will not work. This is another example of "quoting hell", where you
are trying to generate valid Tcl commands with arbitrary fields by
using substitutions. This doesn't work in general: you have to assemble
such scripts with the list commands, not with substitutions. See
Section 6.7 of the Tcl book for details (page 68-69).