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

Annotation of mandoc/apropos.c, Revision 1.24

1.24    ! schwarze    1: /*     $Id: apropos.c,v 1.23 2011/12/07 16:08:55 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
                     21:
1.1       kristaps   22: #include <assert.h>
                     23: #include <getopt.h>
                     24: #include <stdio.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
                     27:
1.11      schwarze   28: #include "apropos_db.h"
1.1       kristaps   29: #include "mandoc.h"
1.18      kristaps   30: #include "manpath.h"
1.15      kristaps   31:
1.8       kristaps   32: static int      cmp(const void *, const void *);
1.14      kristaps   33: static void     list(struct res *, size_t, void *);
1.1       kristaps   34: static void     usage(void);
                     35:
1.2       kristaps   36: static char    *progname;
1.1       kristaps   37:
                     38: int
                     39: main(int argc, char *argv[])
                     40: {
1.20      kristaps   41:        int              ch, rc, whatis;
1.15      kristaps   42:        struct manpaths  paths;
1.14      kristaps   43:        size_t           terms;
1.1       kristaps   44:        struct opts      opts;
1.10      kristaps   45:        struct expr     *e;
1.17      kristaps   46:        char            *defpaths, *auxpaths;
1.24    ! schwarze   47:        char            *conf_file;
1.1       kristaps   48:        extern int       optind;
                     49:        extern char     *optarg;
                     50:
                     51:        progname = strrchr(argv[0], '/');
                     52:        if (progname == NULL)
                     53:                progname = argv[0];
                     54:        else
                     55:                ++progname;
                     56:
1.21      schwarze   57:        whatis = 0 == strncmp(progname, "whatis", 6);
1.20      kristaps   58:
1.15      kristaps   59:        memset(&paths, 0, sizeof(struct manpaths));
                     60:        memset(&opts, 0, sizeof(struct opts));
                     61:
1.17      kristaps   62:        auxpaths = defpaths = NULL;
1.24    ! schwarze   63:        conf_file = NULL;
1.15      kristaps   64:        e = NULL;
                     65:
1.24    ! schwarze   66:        while (-1 != (ch = getopt(argc, argv, "C:M:m:S:s:")))
1.1       kristaps   67:                switch (ch) {
1.24    ! schwarze   68:                case ('C'):
        !            69:                        conf_file = optarg;
        !            70:                        break;
1.17      kristaps   71:                case ('M'):
                     72:                        defpaths = optarg;
                     73:                        break;
1.15      kristaps   74:                case ('m'):
1.17      kristaps   75:                        auxpaths = optarg;
1.15      kristaps   76:                        break;
1.9       kristaps   77:                case ('S'):
1.1       kristaps   78:                        opts.arch = optarg;
                     79:                        break;
1.9       kristaps   80:                case ('s'):
1.1       kristaps   81:                        opts.cat = optarg;
                     82:                        break;
                     83:                default:
                     84:                        usage();
1.20      kristaps   85:                        return(EXIT_FAILURE);
1.1       kristaps   86:                }
                     87:
                     88:        argc -= optind;
                     89:        argv += optind;
                     90:
1.20      kristaps   91:        if (0 == argc)
                     92:                return(EXIT_SUCCESS);
                     93:
                     94:        rc = 0;
1.15      kristaps   95:
1.24    ! schwarze   96:        manpath_parse(&paths, conf_file, defpaths, auxpaths);
1.1       kristaps   97:
1.20      kristaps   98:        e = whatis ? termcomp(argc, argv, &terms) :
                     99:                     exprcomp(argc, argv, &terms);
                    100:
                    101:        if (NULL == e) {
1.16      kristaps  102:                fprintf(stderr, "%s: Bad expression\n", progname);
1.15      kristaps  103:                goto out;
1.10      kristaps  104:        }
1.13      kristaps  105:
1.15      kristaps  106:        rc = apropos_search
1.19      schwarze  107:                (paths.sz, paths.paths,
1.15      kristaps  108:                 &opts, e, terms, NULL, list);
                    109:
1.19      schwarze  110:        if (0 == rc)
1.16      kristaps  111:                fprintf(stderr, "%s: Error reading "
                    112:                                "manual database\n", progname);
1.2       kristaps  113:
1.15      kristaps  114: out:
1.18      kristaps  115:        manpath_free(&paths);
1.10      kristaps  116:        exprfree(e);
1.15      kristaps  117:
                    118:        return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
1.1       kristaps  119: }
                    120:
1.8       kristaps  121: /* ARGSUSED */
1.1       kristaps  122: static void
1.14      kristaps  123: list(struct res *res, size_t sz, void *arg)
1.1       kristaps  124: {
1.8       kristaps  125:        int              i;
1.1       kristaps  126:
1.14      kristaps  127:        qsort(res, sz, sizeof(struct res), cmp);
1.1       kristaps  128:
1.8       kristaps  129:        for (i = 0; i < (int)sz; i++)
1.19      schwarze  130:                printf("%s(%s%s%s) - %s\n", res[i].title,
                    131:                                res[i].cat,
1.1       kristaps  132:                                *res[i].arch ? "/" : "",
                    133:                                *res[i].arch ? res[i].arch : "",
                    134:                                res[i].desc);
                    135: }
                    136:
1.8       kristaps  137: static int
                    138: cmp(const void *p1, const void *p2)
                    139: {
                    140:
1.23      kristaps  141:        return(strcasecmp(((const struct res *)p1)->title,
1.14      kristaps  142:                                ((const struct res *)p2)->title));
1.8       kristaps  143: }
                    144:
1.1       kristaps  145: static void
                    146: usage(void)
                    147: {
                    148:
1.15      kristaps  149:        fprintf(stderr, "usage: %s "
1.24    ! schwarze  150:                        "[-C file] "
1.22      kristaps  151:                        "[-M manpath] "
                    152:                        "[-m manpath] "
1.15      kristaps  153:                        "[-S arch] "
                    154:                        "[-s section] "
1.24    ! schwarze  155:                        "expression ...\n",
1.19      schwarze  156:                        progname);
1.1       kristaps  157: }

CVSweb