You are not logged in.

#1 2004-08-09 19:30:30

Vrok
Member
From: Jaworzno, Poland
Registered: 2004-07-13
Posts: 20
Website

[PATCH] Regular expressions

That's the thing I really missed. Apt-cache has it, but I couldn't find it in pacman. So, here's a patch I wrote for 2.8.3:

diff -ur pacman-2.8.3/src/db.c pacman-2.8.3-regex/src/db.c
--- pacman-2.8.3/src/db.c    2004-08-03 19:24:52.000000000 -0700
+++ pacman-2.8.3-regex/src/db.c    2004-08-09 20:54:57.000000000 -0700
@@ -27,10 +27,19 @@
 #include <sys/stat.h>
 #include <libgen.h>
 #include <unistd.h>
+#include <regex.h>
 #include "package.h"
 #include "util.h"
 #include "db.h"
 
+short unsigned _regexp = 0;
+
+void use_regexp(int use)
+{
+    if (use)
+        _regexp = 1;
+}
+
 /* Open a database and return a pacdb_t handle */
 pacdb_t* db_open(char *root, char *pkgdir, char *treename)
 {
@@ -481,9 +490,23 @@
         return;
     }
 
+
     for(i = needles; i; i = i->next) {
         char *targ = strdup(i->data);
-        strtoupper(targ);
+
+      regex_t comp; /* will store 'compiled' reg. exp. */
+      char bywhat;  /* stores first sign from arg. - decides by what pacman should search (name, desc., provides) */
+      char * regexp;   /* stores targ... */
+    int result;  
+    strtoupper(targ); 
+        regexp = targ+1; /* ...except first sign. */
+        bywhat = targ[0];
+         /* let's compile the regexp */
+          if (regcomp(&comp, regexp, REG_ICASE))
+        {
+            fprintf(stderr, "Something went wrong while compiling the regular expression.");
+            return;
+        }
         for(j = cache; j; j = j->next) {
             pkginfo_t *pkg = (pkginfo_t*)j->data;
             char *haystack;
@@ -491,18 +514,35 @@
             /* check name */
             haystack = strdup(pkg->name);
             strtoupper(haystack);
-            if(strstr(haystack, targ)) {
-                match = 1;
-            }
+            if (! _regexp)
+            {
+                if(strstr(haystack, targ)) {
+                    match = 1;
+                }
+            } else
+            if ((bywhat == 'N') || (bywhat == 'A'))
+                if (! (result = regexec(&comp, haystack, 0, NULL, 0)))
+                {
+                    match = 1; 
+                }
+
             FREE(haystack);
 
             /* check description */
             if(!match) {
                 haystack = strdup(pkg->desc);
                 strtoupper(haystack);
-                if(strstr(haystack, targ)) {
-                    match = 1;
-                }
+        if (! _regexp)
+           {
+             if(strstr(haystack, targ)) {
+                   match = 1;
+                }       
+        } else  
+        if ((bywhat == 'D') || (bywhat == 'A'))
+            if (! (result = regexec(&comp, haystack, 0, NULL, 0)))
+            {
+              match = 1; 
+            }   
                 FREE(haystack);
             }
 
@@ -514,9 +554,17 @@
                     for(m = info->provides; m; m = m->next) {
                         haystack = strdup(m->data);
                         strtoupper(haystack);
-                        if(strstr(haystack, targ)) {
-                            match = 1;
-                        }
+            if (! _regexp)
+            {
+               if(strstr(haystack, targ)) {
+                  match = 1;
+               }
+            } else
+            if ((bywhat == 'P') || (bywhat == 'A'))
+                if (! (result = regexec(&comp, haystack, 0, NULL, 0)))
+                {
+                  match = 1;
+                }
                         FREE(haystack);
                     }
                     FREEPKG(info);
diff -ur pacman-2.8.3/src/db.h pacman-2.8.3-regex/src/db.h
--- pacman-2.8.3/src/db.h    2004-07-02 21:25:47.000000000 -0700
+++ pacman-2.8.3-regex/src/db.h    2004-08-09 20:54:57.000000000 -0700
@@ -35,6 +35,7 @@
     DIR* dir;
 } pacdb_t;
 
+void use_regexp(int use);
 pacdb_t* db_open(char *root, char *dbpath, char *treename);
 void db_close(pacdb_t *db);
 PMList* db_loadpkgs(pacdb_t *db);
diff -ur pacman-2.8.3/src/pacman.c pacman-2.8.3-regex/src/pacman.c
--- pacman-2.8.3/src/pacman.c    2004-08-03 22:57:17.000000000 -0700
+++ pacman-2.8.3-regex/src/pacman.c    2004-08-09 20:55:12.000000000 -0700
@@ -19,6 +19,7 @@
  *  USA.
  */
 
+
 #include "config.h"
 #include <stdio.h>
 #include <stdarg.h>
@@ -3265,7 +3266,7 @@
         {0, 0, 0, 0}
     };
 
-    while((opt = getopt_long(argc, argv, "ARUFQSTDYr:b:vkhscVfnoldepiuwyg", opts, &option_index))) {
+    while((opt = getopt_long(argc, argv, "ARUFQSTDYr:b:vkhscVfnoldepiuwygx", opts, &option_index))) {
         if(opt < 0) {
             break;
         }
@@ -3300,7 +3301,9 @@
                                     perror("bad root path");
                                     return(1);
                                 } break;
-            case 's': pmo_s_search = 1; pmo_q_search = 1; pmo_r_recurse = 1; break;
+            #define SEARCH pmo_s_search = 1; pmo_q_search = 1; pmo_r_recurse = 1;
+            case 's': SEARCH break;
+            case 'x': use_regexp(1); SEARCH break;
             case 'u': pmo_s_upgrade = 1; break;
             case 'v': pmo_verbose = 1; break;
             case 'w': pmo_s_downloadonly = 1; break;
@@ -3626,6 +3629,7 @@
             printf("  -l, --list          list all packages belonging to the specified repositoryn");
             printf("  -p, --print-uris    print out download URIs for each package to be installedn");
             printf("  -s, --search        search remote repositories for matching stringsn");
+            printf("  -x Cregexp          search remote repositories for matching case-insensitive regular expressions.nregexp is the regular expression, C may be N - name, D - description, P - provides or A - alln");
             printf("  -u, --sysupgrade    upgrade all packages that are out of daten");
             printf("  -w, --downloadonly  download packages but do not install/upgrade anythingn");
             printf("  -y, --refresh       download fresh package databases from the servern");

It's a bit messed up, but it works. And it's pretty nice for me. smile

So, if you want to find all packages in remote reps. whose names start with 'alsa', you type 'pacman -Sx n^alsa'.
If you want to search by descripton, you type 'pacman -Sx d^alsa'. I hope you know what I mean, rest of that you can find in patch. smile

Some examples:

[vrok@ankh pacman-2.8.3-regex]$ ./pacman -Sx n^alsa  
current/alsa-driver 1.0.5-2
    An alternative implementation of Linux sound support
current/alsa-lib 1.0.5-1
    An alternative implementation of Linux sound support
current/alsa-oss 1.0.5-1
    OSS compatibility library
current/alsa-utils 1.0.5-1
    An alternative implementation of Linux sound support
extra/alsaplayer 0.99.76-1
    AlsaPlayer is a new type of PCM player. It is heavily multi-threaded and
    tries to excercise the ALSA library and driver quite a bit.
[vrok@ankh pacman-2.8.3-regex]$
[vrok@ankh pacman-2.8.3-regex]$ ./pacman -Sx n'(gnome|kde).*games'
extra/gnome-games 2.6.1-1
    Some Games for GNOME
extra/kdegames 3.2.3-1
    Games for KDE.

^ due to shell special signs, you need to use '' char sometimes smile

[vrok@ankh pacman-2.8.3-regex]$ ./pacman -Sx pxmms
extra/xmms-arts 0.7.1-1
    ARTS-Output for XMMS

^ Searching by "provides"


Now consider the tortoise and the eagle.
"Small Gods" by The Great Terry.

Offline

#2 2004-08-09 21:28:53

dp
Member
From: Zürich, Switzerland
Registered: 2003-05-27
Posts: 3,366
Website

Re: [PATCH] Regular expressions

excellent work - let Judd know about


The impossible missions are the only ones which succeed.

Offline

#3 2004-08-09 22:43:37

tehdely
Member
Registered: 2004-02-20
Posts: 148
Website

Re: [PATCH] Regular expressions

Wonderful patch smile

I hope it makes it in.  wink


[Arch GNUstep Repository] [ PKGBUILDS ]
[code][gnustep]
Server = ftp://blkwidow.lerp.com/pub/mirror/arch/gnustep[/code]

Offline

#4 2004-08-10 00:55:57

contrasutra
Member
From: New Jersey
Registered: 2003-07-26
Posts: 507

Re: [PATCH] Regular expressions

Great, and very useful, but is it really needed? Right now, Pacman's search function is very weak. Judd has two choices (I think) to solve the problem:
1. Add Regexps, and other advanced searching features
2. Just let people pipe -Ss output to their favorite text CLI tools (what most people do now)

I guess he'll probably need to do a mix of that. I mean, right now you could do "pacman -Ss | grep" for regexps, but of course this may be better integrated (don't have an opinion either way).

All of this tangent is just to say: worry about bloat in pacman. We can only go so far in rewriting existing text tools.

Thanks for helping out with Arch/Pacman though. Most of us (well, me) complain without ever writing any code.

Also, maybe advanced searching could be done through a BASH wrapper, since you wouldn't have to reimplement anything? Just a thought.


"Contrary to popular belief, penguins are not the salvation of modern technology.  Neither do they throw parties for the urban proletariat."

Offline

#5 2004-08-10 02:07:37

Xentac
Forum Fellow
From: Victoria, BC
Registered: 2003-01-17
Posts: 1,797
Website

Re: [PATCH] Regular expressions

I could write a regexp script to search the pacman database in... oh... about 45 seconds.


I have discovered that all of mans unhappiness derives from only one source, not being able to sit quietly in a room
- Blaise Pascal

Offline

#6 2004-08-10 11:58:18

dp
Member
From: Zürich, Switzerland
Registered: 2003-05-27
Posts: 3,366
Website

Re: [PATCH] Regular expressions

Xentac wrote:

I could write a regexp script to search the pacman database in... oh... about 45 seconds.

does it take you 45s to write the script or does the script need 45s to give results?


The impossible missions are the only ones which succeed.

Offline

#7 2004-08-10 13:47:46

Vrok
Member
From: Jaworzno, Poland
Registered: 2004-07-13
Posts: 20
Website

Re: [PATCH] Regular expressions

contrasutra wrote:

I guess he'll probably need to do a mix of that. I mean, right now you could do "pacman -Ss | grep" for regexps, but of course this may be better integrated (don't have an opinion either way).

It's a bit crappy way, because if you grep by name, you can't see pkg's description. And, if you search by description, you even can't get package's name. smile
Of course, there are some other ways than grep (maybe I don't know grep good enough;) ), like "pacman -Ss | awk '{if(en==1){print; en=0}} /alsa/ {print; en=1}'" to view names of pkgs with 'alsa' AND their descriptions. But you have to think for a while before typing so long command, and it's not as quick as C-way - running a process for every item and compiling regex every time item is checked is not too efficient.


Now consider the tortoise and the eagle.
"Small Gods" by The Great Terry.

Offline

#8 2004-08-10 15:04:04

Xentac
Forum Fellow
From: Victoria, BC
Registered: 2003-01-17
Posts: 1,797
Website

Re: [PATCH] Regular expressions

dp wrote:
Xentac wrote:

I could write a regexp script to search the pacman database in... oh... about 45 seconds.

does it take you 45s to write the script or does the script need 45s to give results?

To write the script.


I have discovered that all of mans unhappiness derives from only one source, not being able to sit quietly in a room
- Blaise Pascal

Offline

#9 2004-08-10 16:07:49

dp
Member
From: Zürich, Switzerland
Registered: 2003-05-27
Posts: 3,366
Website

Re: [PATCH] Regular expressions

Xentac wrote:
dp wrote:
Xentac wrote:

I could write a regexp script to search the pacman database in... oh... about 45 seconds.

does it take you 45s to write the script or does the script need 45s to give results?

To write the script.

if your script is faster and has similar output like pacman with this patch - then please invest these 45s to make something like "searchpac" to use regex for search for pkgs


The impossible missions are the only ones which succeed.

Offline

#10 2004-08-10 18:05:41

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: [PATCH] Regular expressions

Xentac wrote:

I could write a regexp script to search the pacman database in... oh... about 45 seconds.

Prove it.  I mean really prove it, prove that you did it in 45 seconds. It has to be completely bug free.

Dusty

Offline

#11 2004-08-10 20:14:09

dp
Member
From: Zürich, Switzerland
Registered: 2003-05-27
Posts: 3,366
Website

Re: [PATCH] Regular expressions

Dusty wrote:

It has to be completely bug free.

you cannot ask for THAT, no, not even Xantac, the master of IT is able to do the impossible :-)

bugs are part of the game - you cannot have software wihtout them

read on:

Acceptance testing:
        An unsuccessful attempt to find bugs.


There are bugs and then there are bugs.  And then there are bugs.
-- Karl Lehenbauer

and of course:

There are never any bugs you haven't found yet.


The impossible missions are the only ones which succeed.

Offline

#12 2004-08-10 20:50:30

Xentac
Forum Fellow
From: Victoria, BC
Registered: 2003-01-17
Posts: 1,797
Website

Re: [PATCH] Regular expressions

I never said it would support all the features of his.  I'm saying that I have everything written to do regular expression searching of the pacman database.

Though, when I get some time, I could actually code it while someone timed me.

I also can't guarantee that it'll have no bugs (I can't guarantee that his patch has no bugs either), but I can be pretty sure.  It'll be written in Python.


I have discovered that all of mans unhappiness derives from only one source, not being able to sit quietly in a room
- Blaise Pascal

Offline

#13 2004-08-10 21:24:46

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: [PATCH] Regular expressions

Hey, I'm more impressed if you can prove you did it in 45 seconds than if you actually do it... the proving it part should.... prove interesting.

Goooooooooooooo Xentac!

Dusty

Offline

#14 2004-08-11 20:24:12

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: [PATCH] Regular expressions

i heard a rumour that Xentac wrote srcpac in a few hours. big_smile


arch + gentoo + initng + python = enlisy

Offline

#15 2004-08-11 20:46:32

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: [PATCH] Regular expressions

all these bloody pacman patches must make life a living for all the pacman frontend devs(50+).


arch + gentoo + initng + python = enlisy

Offline

#16 2004-08-11 21:11:11

Xentac
Forum Fellow
From: Victoria, BC
Registered: 2003-01-17
Posts: 1,797
Website

Re: [PATCH] Regular expressions

Ok, I have a recording where I implement it in 53 seconds... I could get it down to 45 seconds, but not right now.

Yes, I did implement srcpac in a few hours.  I think it was 2 or 3... The first version of namcap was written in an evening.  Anything that I've put any significant time into had an initial version out after the first sitting (or at least it was in a workable state).  It's the only way I stay interested in stuff like that.


I have discovered that all of mans unhappiness derives from only one source, not being able to sit quietly in a room
- Blaise Pascal

Offline

#17 2004-08-12 16:15:00

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: [PATCH] Regular expressions

Xentac wrote:

Ok, I have a recording where I implement it in 53 seconds... I could get it down to 45 seconds, but not right now.

Yeah, but it's the first time that counts. Practice doesn't count. You lied. Poor Xentac. :'(

Dusty

Offline

#18 2004-08-13 16:21:57

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: [PATCH] Regular expressions

my first time was clearly under 45s  wink


arch + gentoo + initng + python = enlisy

Offline

Board footer

Powered by FluxBB