Add fnmatch() filtering to output to improve tab-completion.
authorKristian H?gsberg <krh@redhat.com>
Tue Oct 23 01:57:39 2007 -0400 (2007-10-23)
changeset 549e3907688d78
parent 53 a73c2ac05cbe
child 55 b21a4953ff91
Add fnmatch() filtering to output to improve tab-completion.
bash-completion.sh
main.c
razor.c
razor.h
     1.1 --- a/bash-completion.sh	Mon Oct 22 22:55:06 2007 -0400
     1.2 +++ b/bash-completion.sh	Tue Oct 23 01:57:39 2007 -0400
     1.3 @@ -1,17 +1,19 @@
     1.4  __razor_commands () {
     1.5 -    COMPREPLY=($(compgen -W "list-requires list-provides list-files list-file-packages what-requires what-provides import-yum import-rpmdb validate update diff" -- $1))
     1.6 +    local IFS=$'\n'
     1.7 +    COMPREPLY=($(IFS=: compgen -S' ' -W "list-requires:list-provides:list-files:list-file-packages:what-requires:what-provides:import-yum:import-rpmdb:validate:update:diff" -- $1))
     1.8  }
     1.9  
    1.10  __razor_packages () {
    1.11 -    COMPREPLY=($(compgen -W "$(./razor list)" -- $1))
    1.12 +    local IFS=$'\n'
    1.13 +
    1.14 +    COMPREPLY=($(./razor list "$1*" | while read p; do echo "$p "; done))
    1.15  }
    1.16  
    1.17  __razor_files() {
    1.18 -    COMPREPLY=($(compgen -W "$(./razor list-files)" -- $1))
    1.19 +    COMPREPLY=($(./razor list-files "$1*"))
    1.20  }
    1.21  
    1.22  __razor_requires() {
    1.23 -    echo requires
    1.24      COMPREPLY=($(compgen -W "$(./razor list-requires)" -- $1))
    1.25  }
    1.26  
    1.27 @@ -34,4 +36,4 @@
    1.28      fi
    1.29  }
    1.30  
    1.31 -complete -F __razor razor
    1.32 +complete -o nospace -F __razor razor
     2.1 --- a/main.c	Mon Oct 22 22:55:06 2007 -0400
     2.2 +++ b/main.c	Tue Oct 23 01:57:39 2007 -0400
     2.3 @@ -16,7 +16,7 @@
     2.4  	struct razor_set *set;
     2.5  
     2.6  	set = razor_set_open(repo_filename);
     2.7 -	razor_set_list(set);
     2.8 +	razor_set_list(set, argv[0]);
     2.9  	razor_set_destroy(set);
    2.10  
    2.11  	return 0;
     3.1 --- a/razor.c	Mon Oct 22 22:55:06 2007 -0400
     3.2 +++ b/razor.c	Tue Oct 23 01:57:39 2007 -0400
     3.3 @@ -11,6 +11,7 @@
     3.4  #include <fcntl.h>
     3.5  #include <errno.h>
     3.6  #include <ctype.h>
     3.7 +#include <fnmatch.h>
     3.8  
     3.9  #include "razor.h"
    3.10  
    3.11 @@ -834,56 +835,74 @@
    3.12  	array_release(&importer->files);
    3.13  }
    3.14  
    3.15 -static struct razor_entry *
    3.16 -find_entry(struct razor_set *set, struct razor_entry *dir, const char *name)
    3.17 +static const char *
    3.18 +find_dir(struct razor_set *set, struct razor_entry **dir, const char *pattern)
    3.19 +{
    3.20 +	struct razor_entry *e, *end;
    3.21 +	const char *n, *pool = set->string_pool.data;
    3.22 +	int len;
    3.23 +
    3.24 +	e = (struct razor_entry *) set->file_tree.data + (*dir)->start;
    3.25 +	end = e + (*dir)->count;
    3.26 +
    3.27 +	while (e < end) {
    3.28 +		n = pool + e->name;
    3.29 +		len = strlen(n);
    3.30 +		if (len == 0) {
    3.31 +			/* FIXME: Shouldn't have 0-length entries... */
    3.32 +			e++;
    3.33 +			continue;
    3.34 +		}
    3.35 +			
    3.36 +		if (strncmp(pattern + 1, n, len) == 0 &&
    3.37 +		    pattern[len + 1] == '/') {
    3.38 +			*dir = e;
    3.39 +			return find_dir(set, dir, pattern + len + 1);
    3.40 +		}
    3.41 +		e++;
    3.42 +	}
    3.43 +
    3.44 +	return pattern + 1;
    3.45 +}
    3.46 +
    3.47 +static void
    3.48 +list_dir(struct razor_set *set, struct razor_entry *dir,
    3.49 +	 const char *pattern, const char *base)
    3.50  {
    3.51  	struct razor_entry *e, *end;
    3.52  	char *pool = set->string_pool.data;
    3.53 -	char *p, *n;
    3.54  
    3.55 -	if (name == NULL)
    3.56 -		return dir;
    3.57 -
    3.58 -	p = strchr(name + 1, '/');
    3.59  	e = (struct razor_entry *) set->file_tree.data + dir->start;
    3.60  	end = e + dir->count;
    3.61  
    3.62 -	while (e < end) {
    3.63 -		n = pool + e->name;
    3.64 -		if ((p != NULL && strncmp(n, name + 1, p - (name + 1)) == 0) ||
    3.65 -		    (p == NULL && strcmp(n, name + 1) == 0))
    3.66 -			return find_entry(set, e, p);
    3.67 -		e++;
    3.68 -	}
    3.69 -
    3.70 -	return NULL;
    3.71 -}
    3.72 -
    3.73 -static void
    3.74 -list_dir(struct razor_set *set, struct razor_entry *e, int indent)
    3.75 -{
    3.76 -	struct razor_entry *c;
    3.77 -	char *pool = set->string_pool.data;
    3.78 -	int i;
    3.79 -
    3.80 -	for (i = 0; i < indent; i++)
    3.81 -		putchar(' ');
    3.82 -	printf("%s\n", pool + e->name);
    3.83 -	for (i = 0; i < e->count; i++) {
    3.84 -		c = (struct razor_entry *) set->file_tree.data + e->start + i;
    3.85 -		list_dir(set, c, indent + 2);
    3.86 +	for ( ; e < end; e++) {
    3.87 +		if (base && base[0] && fnmatch(base, &pool[e->name], 0) != 0)
    3.88 +			continue;
    3.89 +		if (base && pattern)
    3.90 +			printf("%.*s%s%s\n",
    3.91 +			       base - pattern, pattern, pool + e->name,
    3.92 +			       e->count > 0 ? "/" : "");
    3.93 +		else
    3.94 +			printf("%s%s\n", pool + e->name,
    3.95 +			       e->count > 0 ? "/" : "");
    3.96 +		
    3.97  	}
    3.98  }
    3.99  
   3.100  void
   3.101 -razor_set_list_files(struct razor_set *set, const char *prefix)
   3.102 +razor_set_list_files(struct razor_set *set, const char *pattern)
   3.103  {
   3.104  	struct razor_entry *e;
   3.105 +	const char *base;
   3.106  
   3.107 -	e = find_entry(set, set->file_tree.data, prefix);
   3.108 -	if (e == NULL)
   3.109 +	if (pattern == NULL)
   3.110 +		pattern = "/";
   3.111 +
   3.112 +	e = set->file_tree.data;
   3.113 +	base = find_dir(set, &e, pattern);
   3.114 +	if (base == NULL)
   3.115  		return;
   3.116 -	list_dir(set, e, 2);
   3.117 +	list_dir(set, e, pattern, base);
   3.118  }
   3.119  
   3.120  void
   3.121 @@ -891,11 +910,12 @@
   3.122  {
   3.123  	struct razor_entry *e;
   3.124  	struct razor_package *packages, *p;
   3.125 -	const char *pool;
   3.126 +	const char *pool, *base;
   3.127  	unsigned long *r;
   3.128  
   3.129 -	e = find_entry(set, set->file_tree.data, filename);
   3.130 -	if (e == NULL)
   3.131 +	e = set->file_tree.data;
   3.132 +	base = find_dir(set, &e, filename);
   3.133 +	if (base == NULL)
   3.134  		return;
   3.135  	
   3.136  	r = (unsigned long *) set->package_pool.data + e->packages;
   3.137 @@ -946,15 +966,22 @@
   3.138  }
   3.139  
   3.140  void
   3.141 -razor_set_list(struct razor_set *set)
   3.142 +razor_set_list(struct razor_set *set, const char *pattern)
   3.143  {
   3.144  	struct razor_package *p, *end;
   3.145 +	int with_version = 0;
   3.146  	char *pool;
   3.147  
   3.148  	pool = set->string_pool.data;
   3.149  	end = set->packages.data + set->packages.size;
   3.150 -	for (p = set->packages.data; p < end; p++)
   3.151 -		printf("%s %s\n", &pool[p->name], &pool[p->version]);
   3.152 +	for (p = set->packages.data; p < end; p++) {
   3.153 +		if (pattern && fnmatch(pattern, &pool[p->name], 0) != 0)
   3.154 +		    continue;
   3.155 +		if (with_version)
   3.156 +			printf("%s %s\n", &pool[p->name], &pool[p->version]);
   3.157 +		else
   3.158 +			printf("%s\n", &pool[p->name]);
   3.159 +	}
   3.160  }
   3.161  
   3.162  struct razor_set *bsearch_set;
     4.1 --- a/razor.h	Mon Oct 22 22:55:06 2007 -0400
     4.2 +++ b/razor.h	Tue Oct 23 01:57:39 2007 -0400
     4.3 @@ -9,7 +9,7 @@
     4.4  void razor_set_destroy(struct razor_set *set);
     4.5  int razor_set_write(struct razor_set *set, const char *filename);
     4.6  
     4.7 -void razor_set_list(struct razor_set *set);
     4.8 +void razor_set_list(struct razor_set *set, const char *pattern);
     4.9  void razor_set_list_requires(struct razor_set *set, const char *name);
    4.10  void razor_set_list_provides(struct razor_set *set, const char *name);
    4.11  void razor_set_list_requires_packages(struct razor_set *set,