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

Annotation of mandoc/apropos.c, Revision 1.21

1.21    ! schwarze    1: /*     $Id: apropos.c,v 1.20 2011/11/27 18:54:01 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.1       kristaps   47:        extern int       optind;
                     48:        extern char     *optarg;
                     49:
                     50:        progname = strrchr(argv[0], '/');
                     51:        if (progname == NULL)
                     52:                progname = argv[0];
                     53:        else
                     54:                ++progname;
                     55:
1.21    ! schwarze   56:        whatis = 0 == strncmp(progname, "whatis", 6);
1.20      kristaps   57:
1.15      kristaps   58:        memset(&paths, 0, sizeof(struct manpaths));
                     59:        memset(&opts, 0, sizeof(struct opts));
                     60:
1.17      kristaps   61:        auxpaths = defpaths = NULL;
1.15      kristaps   62:        e = NULL;
                     63:
1.19      schwarze   64:        while (-1 != (ch = getopt(argc, argv, "M:m:S:s:")))
1.1       kristaps   65:                switch (ch) {
1.17      kristaps   66:                case ('M'):
                     67:                        defpaths = optarg;
                     68:                        break;
1.15      kristaps   69:                case ('m'):
1.17      kristaps   70:                        auxpaths = optarg;
1.15      kristaps   71:                        break;
1.9       kristaps   72:                case ('S'):
1.1       kristaps   73:                        opts.arch = optarg;
                     74:                        break;
1.9       kristaps   75:                case ('s'):
1.1       kristaps   76:                        opts.cat = optarg;
                     77:                        break;
                     78:                default:
                     79:                        usage();
1.20      kristaps   80:                        return(EXIT_FAILURE);
1.1       kristaps   81:                }
                     82:
                     83:        argc -= optind;
                     84:        argv += optind;
                     85:
1.20      kristaps   86:        if (0 == argc)
                     87:                return(EXIT_SUCCESS);
                     88:
                     89:        rc = 0;
1.15      kristaps   90:
1.18      kristaps   91:        manpath_parse(&paths, defpaths, auxpaths);
1.1       kristaps   92:
1.20      kristaps   93:        e = whatis ? termcomp(argc, argv, &terms) :
                     94:                     exprcomp(argc, argv, &terms);
                     95:
                     96:        if (NULL == e) {
1.16      kristaps   97:                fprintf(stderr, "%s: Bad expression\n", progname);
1.15      kristaps   98:                goto out;
1.10      kristaps   99:        }
1.13      kristaps  100:
1.15      kristaps  101:        rc = apropos_search
1.19      schwarze  102:                (paths.sz, paths.paths,
1.15      kristaps  103:                 &opts, e, terms, NULL, list);
                    104:
1.19      schwarze  105:        if (0 == rc)
1.16      kristaps  106:                fprintf(stderr, "%s: Error reading "
                    107:                                "manual database\n", progname);
1.2       kristaps  108:
1.15      kristaps  109: out:
1.18      kristaps  110:        manpath_free(&paths);
1.10      kristaps  111:        exprfree(e);
1.15      kristaps  112:
                    113:        return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
1.1       kristaps  114: }
                    115:
1.8       kristaps  116: /* ARGSUSED */
1.1       kristaps  117: static void
1.14      kristaps  118: list(struct res *res, size_t sz, void *arg)
1.1       kristaps  119: {
1.8       kristaps  120:        int              i;
1.1       kristaps  121:
1.14      kristaps  122:        qsort(res, sz, sizeof(struct res), cmp);
1.1       kristaps  123:
1.8       kristaps  124:        for (i = 0; i < (int)sz; i++)
1.19      schwarze  125:                printf("%s(%s%s%s) - %s\n", res[i].title,
                    126:                                res[i].cat,
1.1       kristaps  127:                                *res[i].arch ? "/" : "",
                    128:                                *res[i].arch ? res[i].arch : "",
                    129:                                res[i].desc);
                    130: }
                    131:
1.8       kristaps  132: static int
                    133: cmp(const void *p1, const void *p2)
                    134: {
                    135:
1.14      kristaps  136:        return(strcmp(((const struct res *)p1)->title,
                    137:                                ((const struct res *)p2)->title));
1.8       kristaps  138: }
                    139:
1.1       kristaps  140: static void
                    141: usage(void)
                    142: {
                    143:
1.15      kristaps  144:        fprintf(stderr, "usage: %s "
1.19      schwarze  145:                        "[-M path] "
                    146:                        "[-m path] "
1.15      kristaps  147:                        "[-S arch] "
                    148:                        "[-s section] "
1.19      schwarze  149:                        "expression...\n",
                    150:                        progname);
1.1       kristaps  151: }

CVSweb