static void pg_search(const struct req *req, char *path) { size_t tt, ressz; struct manpaths ps; int i, sz, rc; const char *ep, *start; struct res *res; char **cp; struct opts opt; struct expr *expr; if (req->q.manroot < 0 || 0 == req->psz) { resp_search(NULL, 0, (void *)req); return; } memset(&opt, 0, sizeof(struct opts)); ep = req->q.expr; opt.arch = req->q.arch; opt.cat = req->q.sec; rc = -1; sz = 0; cp = NULL; ressz = 0; res = NULL; /* * Begin by chdir()ing into the root of the manpath. * This way we can pick up the database files, which are * relative to the manpath root. */ assert(req->q.manroot < (int)req->psz); if (-1 == (chdir(req->p[req->q.manroot].path))) { perror(req->p[req->q.manroot].path); resp_search(NULL, 0, (void *)req); return; } memset(&ps, 0, sizeof(struct manpaths)); manpath_manconf(&ps, "etc/catman.conf"); /* * Poor man's tokenisation: just break apart by spaces. * Yes, this is half-ass. But it works for now. */ while (ep && isspace((unsigned char)*ep)) ep++; while (ep && '\0' != *ep) { cp = mandoc_realloc(cp, (sz + 1) * sizeof(char *)); start = ep; while ('\0' != *ep && ! isspace((unsigned char)*ep)) ep++; cp[sz] = mandoc_malloc((ep - start) + 1); memcpy(cp[sz], start, ep - start); cp[sz++][ep - start] = '\0'; while (isspace((unsigned char)*ep)) ep++; } /* * Pump down into apropos backend. * The resp_search() function is called with the results. */ expr = req->q.legacy ? termcomp(sz, cp, &tt) : exprcomp(sz, cp, &tt); if (NULL != expr) rc = apropos_search (ps.sz, ps.paths, &opt, expr, tt, (void *)req, &ressz, &res, resp_search); /* ...unless errors occured. */ if (0 == rc) resp_baddb(); else if (-1 == rc) resp_search(NULL, 0, NULL); for (i = 0; i < sz; i++) free(cp[i]); free(cp); resfree(res, ressz); exprfree(expr); manpath_free(&ps); }
int main(int argc, char *argv[]) { int ch, rc, whatis; struct manpaths paths; size_t terms; struct opts opts; struct expr *e; char *defpaths, *auxpaths; char *conf_file; extern int optind; extern char *optarg; progname = strrchr(argv[0], '/'); if (progname == NULL) progname = argv[0]; else ++progname; whatis = 0 == strncmp(progname, "whatis", 6); memset(&paths, 0, sizeof(struct manpaths)); memset(&opts, 0, sizeof(struct opts)); auxpaths = defpaths = NULL; conf_file = NULL; e = NULL; while (-1 != (ch = getopt(argc, argv, "C:M:m:S:s:"))) switch (ch) { case ('C'): conf_file = optarg; break; case ('M'): defpaths = optarg; break; case ('m'): auxpaths = optarg; break; case ('S'): opts.arch = optarg; break; case ('s'): opts.cat = optarg; break; default: usage(); return(EXIT_FAILURE); } argc -= optind; argv += optind; if (0 == argc) return(EXIT_SUCCESS); rc = 0; manpath_parse(&paths, conf_file, defpaths, auxpaths); e = whatis ? termcomp(argc, argv, &terms) : exprcomp(argc, argv, &terms); if (NULL == e) { fprintf(stderr, "%s: Bad expression\n", progname); goto out; } rc = apropos_search (paths.sz, paths.paths, &opts, e, terms, NULL, list); if (0 == rc) fprintf(stderr, "%s: Error reading " "manual database\n", progname); out: manpath_free(&paths); exprfree(e); return(rc ? EXIT_SUCCESS : EXIT_FAILURE); }
int apropos(int argc, char *argv[]) { int ch, rc, whatis; struct res *res; struct manpaths paths; size_t terms, ressz; struct opts opts; struct expr *e; char *defpaths, *auxpaths; char *conf_file; extern char *optarg; extern int optind; progname = strrchr(argv[0], '/'); if (progname == NULL) progname = argv[0]; else ++progname; whatis = (0 == strncmp(progname, "whatis", 6)); memset(&paths, 0, sizeof(struct manpaths)); memset(&opts, 0, sizeof(struct opts)); ressz = 0; res = NULL; auxpaths = defpaths = NULL; conf_file = NULL; e = NULL; while (-1 != (ch = getopt(argc, argv, "C:M:m:S:s:"))) switch (ch) { case ('C'): conf_file = optarg; break; case ('M'): defpaths = optarg; break; case ('m'): auxpaths = optarg; break; case ('S'): opts.arch = optarg; break; case ('s'): opts.cat = optarg; break; default: goto usage; } argc -= optind; argv += optind; if (0 == argc) goto usage; rc = 0; manpath_parse(&paths, conf_file, defpaths, auxpaths); e = whatis ? termcomp(argc, argv, &terms) : exprcomp(argc, argv, &terms); if (NULL == e) { fprintf(stderr, "%s: Bad expression\n", progname); goto out; } rc = apropos_search (paths.sz, paths.paths, &opts, e, terms, NULL, &ressz, &res, list); if (0 == rc) { fprintf(stderr, "%s: Bad database\n", progname); goto out; } out: manpath_free(&paths); resfree(res, ressz); exprfree(e); return(rc ? EXIT_SUCCESS : EXIT_FAILURE); usage: fprintf(stderr, "usage: %s [-C file] [-M path] [-m path] " "[-S arch] [-s section]%s ...\n", progname, whatis ? " name" : "\n expression"); return(EXIT_FAILURE); }