Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Special aliases that automatically use @WSLPATH?

May
12,845
164
Making one (the easy part) ... could be as easy as

Code:
alias grep `/U wsl grep`

Using one (the hard part) ... when an alias starting with "/U" is invoked it should look at the command line and ...

Code:
if it looks like a file name, use @WSLPATH on it

I suspect that that would be easy for files that exist, but require a bit of AI for a file that doesn't exist, for example, an output file for WSL's sort.

Or maybe a special switch (/U again?) ... only meaningful in /U aliases ... meaning use @WSLPATH on the next parameter, perhaps ...

Code:
alias usort `\U wsl sort`

usort /U infile -o /U outfile

It seems unlikely that a bare "/U" would be needed in a Unix command line. That beats the heck out of typing

Code:
usort %@wslpath[infile] -o %@wslpath[outfile].
 
One strategy works pretty well in a BTM (though I haven't considered all that could go wrong). I first used "/u:filename" as an indication that the argument needed @wslpath but that thwarted file name completion. So I used "/u filename" as suggested in my previous post.

Code:
v:\> type usort.btm
set command=wsl sort
do i=1 to %#
        iff "%[%i]" == "/u" then
                shift
                set command=%command "%@wslpath[%[%i]]"
        else
                set command=%command %[%i]
        endiff
enddo
%command

v:\> type infile
3
2
1

v:\> usort /u infile
1
2
3

v:\> del outfile
Deleting V:\outfile
     1 file deleted

v:\> usort /u infile -o /u outfile

v:\> type outfile
1
2
3

v:\> type infile | usort.btm
1
2
3

v:\> del outfile
Deleting V:\outfile
     1 file deleted

v:\> type infile | usort.btm -o /u outfile

v:\> type outfile
1
2
3

v:\>

Just for fun I tried these which also worked.

Code:
v:\> usort /u infile | usort -r
3
2
1

v:\> del outfile
Deleting V:\outfile
     1 file deleted

v:\> usort /u infile | usort -r -o /u outfile

v:\> type outfile
3
2
1

v:\>
 
I did the same thing with a plugin command (LX). That worked as well as the library routine. Using a plugin plugin cuts down reduces the number of times special symbols must be escaped by escaping &, &&, |, and || (which were unescaped by the parser) and by doubling %s and unquoted ^s (which were un-doubled by the parser). So I can do the likes of this (nonsense, but a decent example). Here's a debug (/D) version which just prints the resulting command.

Code:
v:\> lx /D sort /u infile ^| egrep "1|3" ^| sort -r -o /u outfile ^&^& ps -e ^&^& cat /u outfile
Command: wsl sort "//mnt/d/work/infile" ^| egrep "1|3" ^| sort -r -o "//mnt/d/work/outfile" ^&^& ps -e ^&^& cat "//mnt/d/work/outfile"

And here's it's output ... all from one instance of WSL.

Code:
v:\> lx sort /u infile ^| egrep "1|3" ^| sort -r -o /u outfile ^&^& ps -e ^&^& cat /u outfile
  PID TTY          TIME CMD
    1 ?        00:00:00 init
    6 tty1     00:00:00 init
    7 tty1     00:00:00 bash
   11 tty1     00:00:00 ps
3
1

It would be nice if I didn't have to escape or double anything. That is to have an alias or command that would be exempt from the normal parsing: i.e.,which would send the command line, as is, to WSL. I'm sure I can't do that with a BTM, an alias, or a plugin. And I don't know if TCC itself can do it.

So, at the moment, I don't know about this suggestion.
 
How about a separate command, e.g., ALIASL or LALIAS or WSLAlias ?

An advantage of that would be that just looking at the command name in the declaration would immediately indicate it will work differently from an equivalent ALIAS.
 
Back
Top