static int matches(int count, char *args[], wiki_package_t *pkg) { // Display all packages if there's no query if (0 == count) return 1; char *name = NULL; char *description = NULL; name = clib_package_parse_name(pkg->repo); if (NULL == name) goto fail; case_lower(name); for (int i = 0; i < count; i++) { if (strstr(name, args[i])) return 1; } description = str_copy(pkg->description); if (NULL == description) goto fail; case_lower(description); for (int i = 0; i < count; i++) { if (strstr(description, args[i])) { free(description); return 1; } } fail: if (description) free(description); return 0; }
static int matches(int count, char *args[], package_t *pkg) { char **parts = calloc(1, strlen(pkg->repo) - 1); strsplit(pkg->repo, parts, "/"); if (!filter.executables && 0 == strcmp(pkg->category, "executables")) return 0; if (!filter.utilities && 0 == strcmp(pkg->category, "utilities")) return 0; char *author = parts[0]; case_lower(author); if (filter.author != NULL && 0 != strcmp(author, filter.author)) { count = count - 2; return 0; } // Display all packages if there's no query if (count == 0) return 1; char *name = parts[1]; case_lower(name); for (int i = 0; i < count; i++) { char *idx = strstr(name, args[i]); if (idx) return 1; } char *description = strdup(pkg->description); description = case_lower(description); for (int i = 0; i < count; i++) { char *idx = strstr(description, args[i]); if (idx) return 1; } return 0; }
int main(int argc, char *argv[]) { command_t program; command_init(&program, "clib-search", CLIB_VERSION); program.usage = "[options] [query ...]"; command_parse(&program, argc, argv); for (int i = 0; i < program.argc; i++) case_lower(program.argv[i]); char *html = wiki_html_cache(); if (NULL == html) { command_free(&program); fprintf(stderr, "Failed to fetch wiki HTML\n"); return 1; } list_t *pkgs = wiki_registry_parse(html); free(html); list_node_t *node; list_iterator_t *it = list_iterator_new(pkgs, LIST_HEAD); printf("\n"); while ((node = list_iterator_next(it))) { wiki_package_t *pkg = (wiki_package_t *) node->val; if (matches(program.argc, program.argv, pkg)) { cc_fprintf(CC_FG_DARK_CYAN, stdout, " %s\n", pkg->repo); printf(" url: "); cc_fprintf(CC_FG_DARK_GRAY, stdout, "%s\n", pkg->href); printf(" desc: "); cc_fprintf(CC_FG_DARK_GRAY, stdout, "%s\n", pkg->description); printf("\n"); } wiki_package_free(pkg); } list_iterator_destroy(it); list_destroy(pkgs); command_free(&program); return 0; }