You are not logged in.

#1 2008-11-24 22:32:28

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Hacking urxvt scripts -- Perl help needed [FINISHED]

Before I start, I have no knowledge of Perl, and I'm finding it a fairly challenging language to read.

I have one last thing that I want for urxvt to make it just right. I'd like to be able to select some text from the terminal, and launch a search in a browser using only the mouse. I started with the first example in the urxvtperl man pages:

sub on_sel_grab {
   warn "you selected ", $_[0]->selection;
   ()
}

I saved it to a file called 'grab' in my $HOME directory, and involked it with:

urxvt --perl-lib $HOME -pe grab

It works as it should; It echos whatever I highlight in the new terminal on the old terminal.

The next step was to make a basic launcher. I thought something like this would work:

sub on_sel_grab {
   system("firefox http://www.google.com/search?q=", $_[0]->selection);
   ()
}

It doesn't, and other variations like '< $_[0]->selection' dont' work either. Static launchers do:

sub on_sel_grab {
   system("firefox http://www.google.com/search?q=test");
   ()
}

For my first question, how do I get the value of $_[0]->selection appended to the search URL?

Last edited by skottish (2008-12-05 05:34:35)

Offline

#2 2008-11-24 22:56:33

string
Member
Registered: 2008-11-03
Posts: 286

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

sub on_sel_grab {
   system("firefox http://www.google.com/search?q=" . $_[0]->selection);
   ()
}

Hope that works.

Last edited by string (2008-11-24 22:56:57)

Offline

#3 2008-11-24 23:16:32

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

string wrote:
sub on_sel_grab {
   system("firefox http://www.google.com/search?q=" . $_[0]->selection);
   ()
}

Hope that works.

That's just beautiful! It mostly works.

One thing that came up is my command prompt is confusing either Perl or Firefox. For instance, this is what my home and music directories look like on the terminal:

~ >
music >

So if I type 'test' in the music directory and highlight it, only 'music' gets passed to Firefox. Is this because > is a redirection operator in Perl?

Offline

#4 2008-11-24 23:23:03

string
Member
Registered: 2008-11-03
Posts: 286

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

Yes, you might find that you now have a "test" file in your music directory smile

Offline

#5 2008-11-24 23:24:31

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

string wrote:

Yes, you might find that you now have a "test" file in your music directory smile

I just realized that I was dumping files all over $HOME. Whoops. Is there a way to pass the entire data as a single string?

Offline

#6 2008-11-24 23:38:00

string
Member
Registered: 2008-11-03
Posts: 286

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

Well you *are* passing it as a single string, but because you are using system(), it is exactly (well, almost) like issuing, from the command line:

$ firefox http://www.google.com/search?q=music > test

Perhaps trying to issue:

firefox http://www.google.com/search?q=music%20%3E%20test

instead will fix your problem.

Last edited by string (2008-11-24 23:38:49)

Offline

#7 2008-11-24 23:56:45

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

Here's the problem of what just happened. The > is part of my bash prompt, and it was grabbed from a sort of random selection. If > is going to cause problems, then I suspect other operators will too.

Last edited by skottish (2008-11-24 23:57:07)

Offline

#8 2008-11-25 00:11:14

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

URI::Escape was made for this:

use URI::Escape;
my $query = uri_escape( $_[0]->selection );
system( "/usr/bin/firefox http://www.google.com/search?q=$query" );

Or without the extra variable:

use URI::Escape;
system( "/usr/bin/firefox http://www.google.com/search?q=" . uri_escape( $_[0]->selection ) );

Last edited by Daenyth (2008-11-25 00:13:39)

Offline

#9 2008-11-25 00:22:01

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

Daenyth wrote:

URI::Escape was made for this:

use URI::Escape;
my $query = uri_escape( $_[0]->selection );
system( "/usr/bin/firefox http://www.google.com/search?q=$query" );

Or without the extra variable:

use URI::Escape;
system( "/usr/bin/firefox http://www.google.com/search?q=" . uri_escape( $_[0]->selection ) );

Sweet! Getting closer...

Perl didn't find the package, and a Google search turned up this:

http://kobesearch.cpan.org/htdocs/URI/U … pe.pm.html

Is this what I'm looking for? If so, is there a standard place for user packages in Perl?

Last edited by skottish (2008-11-25 00:22:20)

Offline

#10 2008-11-25 00:36:30

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

Ok, first, add this line to the top of your script somewhere:

use URI::Escape;

It seems to be a standard module (I have it on my system by default). This will enable you to escape characters for URIs (yeah, I know the name is obvious).

Then update your sub as follows:

sub on_sel_grab {
   # Load the selection into a variable, URI-escaping it first, so that the next command becomes clearer.
   # "my" is just for the variable declaration so that you don't encounter any problems if you're using "strict"
   my $selection = uri_escape($_[0]->selection);

   # Add single quotes around the URI to avoid potential shell problems.
   # The curly braces around the variable are just to be sure that it's read correctly.
   # I don't think it's necessary here, but it's good practice when you're your variables aren't space-padded.
   system("firefox 'http://www.google.com/search?q=${selection}');

   # Is this necessary? Why is there here? I think  you can remove this.
   ()
}

If you can't use "URI::Escape", you'll have to add several substitution lines (e.g. $selection =~ s/ /%20/g).

edit
I need to stop submitting replies that I've had open for 10 minutes.

edit 2
Try

pacman -S perl-libwww

I suspect that's the package providing the URI::Escape module.

Last edited by Xyne (2008-11-25 00:41:39)


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#11 2008-11-25 00:44:38

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

You guys rock.

Oddly enough, URI::Escape is in the perl-uri package. Go and figure.

I'll be back when I can't figure out the next step...

Offline

#12 2008-11-25 02:50:54

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

That explains it... I have it installed as a dep for perl-libwww.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#13 2008-12-03 05:31:57

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

I changed my focus on this and there's one thing left to do. I decided that simply having the selection in urxvt paste to the clipboard would solve all of my problems. Code like this works, but it suffers from the same issue that I was having above before URI::Escape:

sub on_sel_grab {
    system( "echo " . $_[0]->selection . " | xsel -ib " );
}

I'm hoping for something simple like URI::Escape but for plain text. Basically I want all characters that need to be escaped to be printed to be escaped.

Last edited by skottish (2008-12-03 05:32:51)

Offline

#14 2008-12-03 13:04:27

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

I don't really understand in this case what you're trying to do. What do you mean by "all characters that need to be escaped"?

Offline

#15 2008-12-03 14:37:03

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

Daenyth wrote:

I don't really understand in this case what you're trying to do. What do you mean by "all characters that need to be escaped"?

That sentence was weird enough to write. I can imaging that reading it wasn't so fun...

It's the same kind of scenario from before. If I copy text now it will go onto the clipboard. But, if any if those characters are operators, they will be executed like normal. I want to be able to copy and paste all text, so I'm looking for a perl library that will automatically add escape sequences to prevent special characters from being executed.

Offline

#16 2008-12-03 14:39:46

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

I'm confused as to what you're trying to do.
In your previous posts you seemed like you wanted to launch firefox on a url, which can be done with urxvt's built in url launching. With this, I don't get the point... When you highlight text it's added to clipboard anyway, what is xsel for.

Offline

#17 2008-12-03 20:29:32

wulax
Member
Registered: 2006-02-20
Posts: 30

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

I know nothing about perl but this urxvt plugin might be of interest:
http://www.jukie.net/~bart/blog/20070503013555

Offline

#18 2008-12-04 02:55:37

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

Daenyth wrote:

I'm confused as to what you're trying to do.
In your previous posts you seemed like you wanted to launch firefox on a url, which can be done with urxvt's built in url launching. With this, I don't get the point... When you highlight text it's added to clipboard anyway, what is xsel for.

Not exactly. When you highlight text, it gets pushed to X's primary selection buffer. This is the middle click paste. This is not the "clipboard" that most applications use with the right click menus or with Ctrl+V. The clipboard is far more convenient under numerous circumstances. This is why I changed my focus from a specialized function to search the web to an all around solution.

On the command line, if I write 'echo ~', it's going to print '/home/skottish'. If I 'echo /~', I'll end up with '~'. If I want to simply print to screen any special character, it needs to be escaped. This is also true for the clipboard. All special characters need to be escaped before they can be pasted or they will be executed. This is the same thing that we discussed this above with URLs, but instead of dealing with URLs, I'm dealing with plain text.

Offline

#19 2008-12-04 03:14:34

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

Ah.. I'm not sure. I'd just use a regex with a big character class.

Offline

#20 2008-12-04 06:23:12

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

Daenyth wrote:

Ah.. I'm not sure. I'd just use a regex with a big character class.

Something like this may cover it:

sub on_sel_grab {

    my $query = $_[0]->selection;

    $query =~ s/([\&;\`'\\\|"*?~<>^\(\)\[\]\{\}\$\n\r])/\\$1/g;
    
    system( "echo " . $query . " | xsel -ib" );
}

I want to thank all of you for participating in this thread. I changed the focus of it mid-stream while not explaining myself real concisely, and still your help made this happen. I'm not 100% sure that this will cover it, but it's working so far.

By the way, I found the search and replace code in something called Phrack Magazine. Coincidence?

http://insecure.org/news/P55-07.txt

Now I have a pesky newline being inserted at the end. But, that has nothing to do with perl or the above code.

Last edited by skottish (2008-12-04 06:23:44)

Offline

#21 2008-12-04 12:29:46

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

You can remove the newline by using the chomp() command.

Offline

#22 2008-12-05 05:34:09

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

Daenyth wrote:

You can remove the newline by using the chomp() command.

The new line was coming from echo, and it was easy enough to fix. I ended up having to add a second line to replace the new line character for reasons that I can't see clearly. This code does work while maintaining the formatting:

#! /usr/bin/perl

sub on_sel_grab {
    my $query = $_[0]->selection;
    $query =~ s/([\&;\`'\\\|"*?~<>^\(\)\[\]\{\}\$\n\r])/\\$1/g;
    $query =~ s/\n/\\n/g;
    system( "echo -en " . $query . " | xsel -ibp" );
}

I saved it as "clipboard" in /usr/lib/urxvt/perl and added it to the urxvt*perl-ext-common: line in .Xdefaults. Now urxvt is just right in every way for me.

Last edited by skottish (2008-12-05 05:44:01)

Offline

#23 2008-12-05 13:39:47

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

Awesome work!

Offline

#24 2008-12-05 18:15:09

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

Daenyth wrote:

Awesome work!

Thanks Daenyth, I appreciate it. Everything really.

I have a question that maybe you can spot. On the first regex line in the code, the new line character gets escaped, but instead of replacing the new line with '\n', it only gets replaced by '\'. This in turn gets ignored by echo and all of the formatting is lost. The escape sequence on the brackets for instance work fine. I had to add the second regex line in order to handle this, which added more complexity. I know that performance here isn't a big deal, but it seems like it could be done more cleanly. I can't see why this is happening. Can you?

Last edited by skottish (2008-12-05 18:52:27)

Offline

#25 2008-12-05 19:55:43

Daenyth
Forum Fellow
From: Boston, MA
Registered: 2008-02-24
Posts: 1,244

Re: Hacking urxvt scripts -- Perl help needed [FINISHED]

Honestly, I don't know. Maybe ask #perl on IRC?

Offline

Board footer

Powered by FluxBB