[BACK]Return to apropos.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/apropos.c, Revision 1.27

1.27    ! kristaps    1: /*     $Id: apropos.c,v 1.26 2012/03/23 02:52:33 kristaps Exp $ */
1.1       kristaps    2: /*
1.8       kristaps    3:  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.15      kristaps    4:  * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
1.13      kristaps   18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
1.27    ! kristaps   21: #include <sys/param.h>
1.13      kristaps   22:
1.1       kristaps   23: #include <assert.h>
                     24: #include <getopt.h>
                     25: #include <stdio.h>
                     26: #include <stdlib.h>
                     27: #include <string.h>
                     28:
1.11      schwarze   29: #include "apropos_db.h"
1.1       kristaps   30: #include "mandoc.h"
1.18      kristaps   31: #include "manpath.h"
1.15      kristaps   32:
1.8       kristaps   33: static int      cmp(const void *, const void *);
1.14      kristaps   34: static void     list(struct res *, size_t, void *);
1.1       kristaps   35: static void     usage(void);
                     36:
1.2       kristaps   37: static char    *progname;
1.1       kristaps   38:
                     39: int
                     40: main(int argc, char *argv[])
                     41: {
1.20      kristaps   42:        int              ch, rc, whatis;
1.27    ! kristaps   43:        struct res      *res;
1.15      kristaps   44:        struct manpaths  paths;
1.27    ! kristaps   45:        size_t           terms, ressz;
1.1       kristaps   46:        struct opts      opts;
1.10      kristaps   47:        struct expr     *e;
1.17      kristaps   48:        char            *defpaths, *auxpaths;
1.24      schwarze   49:        char            *conf_file;
1.1       kristaps   50:        extern int       optind;
                     51:        extern char     *optarg;
                     52:
                     53:        progname = strrchr(argv[0], '/');
                     54:        if (progname == NULL)
                     55:                progname = argv[0];
                     56:        else
                     57:                ++progname;
                     58:
1.21      schwarze   59:        whatis = 0 == strncmp(progname, "whatis", 6);
1.20      kristaps   60:
1.15      kristaps   61:        memset(&paths, 0, sizeof(struct manpaths));
                     62:        memset(&opts, 0, sizeof(struct opts));
                     63:
1.27    ! kristaps   64:        ressz = 0;
        !            65:        res = NULL;
1.17      kristaps   66:        auxpaths = defpaths = NULL;
1.24      schwarze   67:        conf_file = NULL;
1.15      kristaps   68:        e = NULL;
                     69:
1.24      schwarze   70:        while (-1 != (ch = getopt(argc, argv, "C:M:m:S:s:")))
1.1       kristaps   71:                switch (ch) {
1.24      schwarze   72:                case ('C'):
                     73:                        conf_file = optarg;
                     74:                        break;
1.17      kristaps   75:                case ('M'):
                     76:                        defpaths = optarg;
                     77:                        break;
1.15      kristaps   78:                case ('m'):
1.17      kristaps   79:                        auxpaths = optarg;
1.15      kristaps   80:                        break;
1.9       kristaps   81:                case ('S'):
1.1       kristaps   82:                        opts.arch = optarg;
                     83:                        break;
1.9       kristaps   84:                case ('s'):
1.1       kristaps   85:                        opts.cat = optarg;
                     86:                        break;
                     87:                default:
                     88:                        usage();
1.20      kristaps   89:                        return(EXIT_FAILURE);
1.1       kristaps   90:                }
                     91:
                     92:        argc -= optind;
                     93:        argv += optind;
                     94:
1.20      kristaps   95:        if (0 == argc)
                     96:                return(EXIT_SUCCESS);
                     97:
                     98:        rc = 0;
1.15      kristaps   99:
1.24      schwarze  100:        manpath_parse(&paths, conf_file, defpaths, auxpaths);
1.1       kristaps  101:
1.20      kristaps  102:        e = whatis ? termcomp(argc, argv, &terms) :
                    103:                     exprcomp(argc, argv, &terms);
                    104:
                    105:        if (NULL == e) {
1.16      kristaps  106:                fprintf(stderr, "%s: Bad expression\n", progname);
1.15      kristaps  107:                goto out;
1.10      kristaps  108:        }
1.13      kristaps  109:
1.15      kristaps  110:        rc = apropos_search
1.27    ! kristaps  111:                (paths.sz, paths.paths, &opts,
        !           112:                 e, terms, NULL, &ressz, &res, list);
1.2       kristaps  113:
1.27    ! kristaps  114:        if (0 == rc) {
        !           115:                fprintf(stderr, "%s: Bad database\n", progname);
        !           116:                goto out;
        !           117:        }
1.15      kristaps  118: out:
1.18      kristaps  119:        manpath_free(&paths);
1.27    ! kristaps  120:        resfree(res, ressz);
1.10      kristaps  121:        exprfree(e);
1.15      kristaps  122:        return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
1.1       kristaps  123: }
                    124:
1.8       kristaps  125: /* ARGSUSED */
1.1       kristaps  126: static void
1.14      kristaps  127: list(struct res *res, size_t sz, void *arg)
1.1       kristaps  128: {
1.26      kristaps  129:        size_t           i;
1.1       kristaps  130:
1.14      kristaps  131:        qsort(res, sz, sizeof(struct res), cmp);
1.1       kristaps  132:
1.27    ! kristaps  133:        for (i = 0; i < sz; i++) {
        !           134:                if ( ! res[i].matched)
        !           135:                        continue;
1.26      kristaps  136:                printf("%s(%s%s%s) - %.70s\n",
                    137:                                res[i].title,
1.19      schwarze  138:                                res[i].cat,
1.1       kristaps  139:                                *res[i].arch ? "/" : "",
                    140:                                *res[i].arch ? res[i].arch : "",
                    141:                                res[i].desc);
1.27    ! kristaps  142:        }
1.1       kristaps  143: }
                    144:
1.8       kristaps  145: static int
                    146: cmp(const void *p1, const void *p2)
                    147: {
                    148:
1.23      kristaps  149:        return(strcasecmp(((const struct res *)p1)->title,
1.14      kristaps  150:                                ((const struct res *)p2)->title));
1.8       kristaps  151: }
                    152:
1.1       kristaps  153: static void
                    154: usage(void)
                    155: {
                    156:
1.15      kristaps  157:        fprintf(stderr, "usage: %s "
1.24      schwarze  158:                        "[-C file] "
1.22      kristaps  159:                        "[-M manpath] "
                    160:                        "[-m manpath] "
1.15      kristaps  161:                        "[-S arch] "
                    162:                        "[-s section] "
1.24      schwarze  163:                        "expression ...\n",
1.19      schwarze  164:                        progname);
1.1       kristaps  165: }

CVSweb