Tcsh Aliases (3 of 3): Automatically Resolving Aliases using tcshParser

We’ve seen how to write tcsh aliases and how using history substitution commands can transform aliases into relatively sophisticated tools. But aliases have their limitations, particularly when a command needs to be used as the input to another program. We have developed a tool called tcshParser to solve this problem.

What is the problem?

There is no facility in tcsh to find out what the command will look like once you have applied all the aliases. Aliases are only applied to the first ‘word’ in the command, so something like echo mycommand won’t help. This means that if you want to pass your command into any debugging tool, such as Breeze, you don’t know what to pass in unless you can resolve the aliases by hand.

An example should help to make the problem clear. Ellexus Breeze includes a script which traces the execution of a program. You have to pass in the command to trace as an argument to the script. If we type

$ trace-program.sh -f trace ls -l /tmp

then Breeze traces the dependencies of the comamnd ls -l /tmp and everything works as expected. But if we define ll as an alias for ls -l and then try to trace it, we get a “command not found” error.

$ trace-program.sh -f trace ll /tmp
... ll: command not found

Recall that only “the first word of each command is checked to see if it has an alias”. The word ll is no longer the first word of a command and so tcsh doesn’t check to see whether it is an alias.

Introducing tcshParser

We begin by saving the details of all the aliases in your shell environement:

$ alias > my_aliases.txt

The tcshParser program takes this file and your aliased command and prints out the real command that would be run after the aliases are expanded. Repeating the example of ll /tmp, we see that it eventually expands to ls --color=tty -l /tmp

$ ./tcshParser my_aliases.txt ll /tmp
ls --color=tty -l /tmp
$

To make life easier, we have included tcshParser in the Breeze installation. The trace-program.sh script actually has an option  (-at <alias file>) which passes the command line through tcshParser. TcshParser works out what the ‘real’ command should be, so that your aliased commands will just work.

The original version of tcshParser was written in Java, but has now been re-written in C so that it can run on systems which don’t have a Java Virtual Machine installed. The source code for this program has been released under the GPL and can be found on GitHub at:

https://github.com/ellexus/tcshparser

Instructions for building the program and for running a set of tests can be found in README.md in the github repository.

Author: Richard Jordan is a developer at Ellexus