/** * call-seq: * search(*queries, ...) → an_array * * Search the database with POSIX regular expressions for packages. * === Parameters * [*queries (splat)] * A list of strings interpreted as POSIX regular expressions. * For a package to be found, it must match _all_ queries terms, * not just a single one. Each query is matched against both * the package name and the package description, where only * one needs to match for the package to be considered. * * Note that the match is not performed by Ruby or even Oniguruma/Onigmo, * but directly in libalpm via the +regexp+ library in C. * * === Return value * An array of Package instances whose names matched _all_ regular expressions. */ static VALUE search(int argc, VALUE argv[], VALUE self) { alpm_db_t* p_db = NULL; alpm_list_t* targets = NULL; alpm_list_t* packages = NULL; alpm_list_t* item = NULL; VALUE result = rb_ary_new(); int i; Data_Get_Struct(self, alpm_db_t, p_db); /* Convert our Ruby array to an alpm_list with C strings */ for(i=0; i < argc; i++) { VALUE term = rb_check_string_type(argv[i]); if (!RTEST(term)) { rb_raise(rb_eTypeError, "Argument is not a string (#to_str)"); return Qnil; } targets = alpm_list_add(targets, StringValuePtr(term)); } /* Perform the query */ packages = alpm_db_search(p_db, targets); if (!packages) return result; for(item=packages; item; item = alpm_list_next(item)) rb_ary_push(result, Data_Wrap_Struct(rb_cAlpm_Package, NULL, NULL, item->data)); alpm_list_free(targets); return result; }
static alpm_list_t *search_packages(alpm_list_t *dbs, alpm_list_t *targets) { alpm_list_t *i, *packages = NULL; for (i = dbs; i; i = i->next) { packages = alpm_list_join(packages, alpm_db_search(i->data, targets)); } return packages; }
/* search the local database for a matching package */ static int query_search(alpm_list_t *targets) { alpm_list_t *i, *searchlist; int freelist; alpm_db_t *db_local = alpm_option_get_localdb(config->handle); /* if we have a targets list, search for packages matching it */ if(targets) { searchlist = alpm_db_search(db_local, targets); freelist = 1; } else { searchlist = alpm_db_get_pkgcache(db_local); freelist = 0; } if(searchlist == NULL) { return 1; } for(i = searchlist; i; i = alpm_list_next(i)) { alpm_list_t *grp; alpm_pkg_t *pkg = alpm_list_getdata(i); if(!config->quiet) { printf("local/%s %s", alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg)); } else { printf("%s", alpm_pkg_get_name(pkg)); } if(!config->quiet) { if((grp = alpm_pkg_get_groups(pkg)) != NULL) { alpm_list_t *k; printf(" ("); for(k = grp; k; k = alpm_list_next(k)) { const char *group = alpm_list_getdata(k); printf("%s", group); if(alpm_list_next(k)) { /* only print a spacer if there are more groups */ printf(" "); } } printf(")"); } /* we need a newline and initial indent first */ printf("\n "); indentprint(alpm_pkg_get_desc(pkg), 4); } printf("\n"); } /* we only want to free if the list was a search list */ if(freelist) { alpm_list_free(searchlist); } return 0; }
VALUE db_search ( VALUE self, VALUE wordsary ) { alpm_list_t * found, * wordslist; INITDBPTR; wordslist = ary_to_alpmstrlist( wordsary ); found = alpm_db_search( db, wordslist ); alpm_list_free( wordslist ); return alpmpkglist_to_ary( found ); }
alpm_list_t *alpm_all_backups(int everything) /* {{{ */ { alpm_list_t *backups = NULL; const alpm_list_t *i; alpm_db_t *db = alpm_get_localdb(pmhandle); alpm_list_t *targets = cfg.targets ? alpm_db_search(db, cfg.targets) : alpm_db_get_pkgcache(db); for (i = targets; i; i = i->next) { alpm_list_t *pkg_backups = alpm_find_backups(i->data, everything); backups = alpm_list_join(backups, pkg_backups); } return backups; } /* }}} */
/** * Display the details of a search. * @param db the database we're searching * @param targets the targets we're searching for * @param show_status show if the package is also in the local db */ int dump_pkg_search(alpm_db_t *db, alpm_list_t *targets, int show_status) { int freelist = 0; alpm_db_t *db_local; alpm_list_t *i, *searchlist; unsigned short cols; const colstr_t *colstr = &config->colstr; if(show_status) { db_local = alpm_get_localdb(config->handle); } /* if we have a targets list, search for packages matching it */ if(targets) { searchlist = alpm_db_search(db, targets); freelist = 1; } else { searchlist = alpm_db_get_pkgcache(db); freelist = 0; } if(searchlist == NULL) { return 1; } cols = getcols(fileno(stdout)); for(i = searchlist; i; i = alpm_list_next(i)) { alpm_list_t *grp; alpm_pkg_t *pkg = i->data; if(config->quiet) { fputs(alpm_pkg_get_name(pkg), stdout); } else { printf("%s%s/%s%s %s%s%s", colstr->repo, alpm_db_get_name(db), colstr->title, alpm_pkg_get_name(pkg), colstr->version, alpm_pkg_get_version(pkg), colstr->nocolor); if((grp = alpm_pkg_get_groups(pkg)) != NULL) { alpm_list_t *k; printf(" %s(", colstr->groups); for(k = grp; k; k = alpm_list_next(k)) { const char *group = k->data; fputs(group, stdout); if(alpm_list_next(k)) { /* only print a spacer if there are more groups */ putchar(' '); } } printf(")%s", colstr->nocolor); } if(show_status) { print_installed(db_local, pkg); } /* we need a newline and initial indent first */ fputs("\n ", stdout); indentprint(alpm_pkg_get_desc(pkg), 4, cols); } fputc('\n', stdout); } /* we only want to free if the list was a search list */ if(freelist) { alpm_list_free(searchlist); } return 0; }