X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;ds=sidebyside;f=src%2Fsearch.c;h=dd324d55a7d1664d0006b283a6f5a6aab292bbd7;hb=b9eb4486d50a2af606c6b776fcb5dfe065ccee9e;hp=9b6ec887cd015f45b88f72e0de32df74d31303a2;hpb=a639d5e86bfa941153e214dc0c44d780072769ea;p=zpackage diff --git a/src/search.c b/src/search.c index 9b6ec88..dd324d5 100644 --- a/src/search.c +++ b/src/search.c @@ -28,6 +28,7 @@ struct search_ctl { int matchinstalled; int suppressinstalled; int onlylocalinstalled; + int foundonly; int verbose; int dbrepos; }; @@ -389,19 +390,39 @@ void checklibs(struct search_ctl *opts, free(pkgfile); } -void print_pkghash(jsw_hash_t *hash, int json) { +void print_pkghash(jsw_hash_t *hash, int json, int idonly) { const char *pkgid, *file; char *fmt; - char *sep; + char *sep = "\n"; int count = 0; - - fmt = json ? "\"%s\": \"%s\"" : "%s:%s"; - sep = json ? ", " : "\n"; + char *start = ""; + char *end = ""; if (json) { - printf("{ "); + sep = ", "; + if (idonly) { + fmt = "\"%s\""; + start = "[ "; + end = " ]\n"; + } else { + fmt = "\"%s\": \"%s\""; + start = "{ "; + end = " }\n"; + } + } else { + if (idonly) { + fmt = "%s"; + start = ""; + end = ""; + } else { + fmt = "%s:%s"; + start = ""; + end = ""; + } } + printf("%s", start); + if (jsw_hsize(hash) > 0) { for (jsw_hreset(hash); jsw_hitem(hash); jsw_hnext(hash)) { pkgid = jsw_hkey(hash); @@ -409,16 +430,20 @@ void print_pkghash(jsw_hash_t *hash, int json) { if (count++) { printf("%s", sep); } - printf(fmt, pkgid, file); + if (idonly) { + printf(fmt, pkgid); + } else { + printf(fmt, pkgid, file); + } } + if (!json) printf("\n"); } - if (json) printf(" }"); - printf("\n"); + printf("%s", end); } int main(int ac, char *av[]) { - int option, argn; - int findlibs = 0, json = 0; + int option, argn, rv = 0; + int findlibs = 0, json = 0, idonly = 0; struct pkgloc *found; jsw_hash_t *packages, *check, *forlibs, *nolib; jsw_atree_t *nfound; @@ -462,7 +487,7 @@ int main(int ac, char *av[]) { */ int output = 1; - while ((option = getopt(ac, av, "ljqPRDvp:r:d:MiIO")) != -1) { + while ((option = getopt(ac, av, "fljqPRDvp:r:d:MiIOn")) != -1) { switch (option) { case 'l': findlibs = 1; break; case 'j': json = 1; break; @@ -472,6 +497,7 @@ int main(int ac, char *av[]) { case 'I': opt.suppressinstalled = 1; break; /* only find localdb pkgs if installed */ case 'O': opt.onlylocalinstalled = 1; break; + case 'f': opt.foundonly = 1; break; case 'd': opt.localdb = optarg; break; case 'p': opt.pkgdir = optarg; break; case 'r': opt.repodir = optarg; break; @@ -484,6 +510,7 @@ int main(int ac, char *av[]) { */ case 'M': opt.matchallpkgfile = 1; break; case 'v': opt.verbose++; break; + case 'n': idonly = 1; break; default: exit(EXIT_FAILURE); break; @@ -559,10 +586,10 @@ int main(int ac, char *av[]) { if (output) { if (jsw_hsize(packages)) { - print_pkghash(packages, json); + print_pkghash(packages, json, idonly); } if (jsw_hsize(forlibs)) { - print_pkghash(forlibs, json); + print_pkghash(forlibs, json, idonly); } } @@ -573,9 +600,10 @@ int main(int ac, char *av[]) { file = jsw_hitem(nolib); fprintf(stderr, "no lib found %s:%s\n", pkgid, file); } + rv = 1; } - if (jsw_asize(nfound) > 0) { + if (jsw_asize(nfound) > 0 && !opt.foundonly) { jsw_atrav_t *i; i = jsw_atnew(); char *pkgstr; @@ -583,7 +611,8 @@ int main(int ac, char *av[]) { for (pkgstr = jsw_atfirst(i, nfound); pkgstr; pkgstr = jsw_atnext(i)) { fprintf(stderr, "%s: not found\n", pkgstr); } + rv = 1; } - return jsw_asize(nfound) > 0 || jsw_hsize(nolib) > 0 ? 1 : 0; + return rv; }