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

Annotation of mandoc/apropos.c, Revision 1.12

1.12    ! schwarze    1: /*     $Id: apropos.c,v 1.11 2011/11/13 10:12:05 schwarze Exp $ */
1.1       kristaps    2: /*
1.8       kristaps    3:  * Copyright (c) 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: #include <assert.h>
                     18: #include <getopt.h>
                     19: #include <limits.h>
                     20: #include <stdio.h>
                     21: #include <stdlib.h>
                     22: #include <string.h>
                     23:
1.11      schwarze   24: #include "apropos_db.h"
1.1       kristaps   25: #include "mandoc.h"
                     26:
1.8       kristaps   27: static int      cmp(const void *, const void *);
                     28: static void     list(struct rec *, size_t, void *);
1.1       kristaps   29: static void     usage(void);
                     30:
1.2       kristaps   31: static char    *progname;
1.1       kristaps   32:
                     33: int
                     34: main(int argc, char *argv[])
                     35: {
1.12    ! schwarze   36:        int              ch;
1.1       kristaps   37:        struct opts      opts;
1.10      kristaps   38:        struct expr     *e;
1.1       kristaps   39:        extern int       optind;
                     40:        extern char     *optarg;
                     41:
                     42:        memset(&opts, 0, sizeof(struct opts));
                     43:
                     44:        progname = strrchr(argv[0], '/');
                     45:        if (progname == NULL)
                     46:                progname = argv[0];
                     47:        else
                     48:                ++progname;
                     49:
1.12    ! schwarze   50:        while (-1 != (ch = getopt(argc, argv, "S:s:")))
1.1       kristaps   51:                switch (ch) {
1.9       kristaps   52:                case ('S'):
1.1       kristaps   53:                        opts.arch = optarg;
                     54:                        break;
1.9       kristaps   55:                case ('s'):
1.1       kristaps   56:                        opts.cat = optarg;
                     57:                        break;
                     58:                default:
                     59:                        usage();
                     60:                        return(EXIT_FAILURE);
                     61:                }
                     62:
                     63:        argc -= optind;
                     64:        argv += optind;
                     65:
1.10      kristaps   66:        if (0 == argc)
1.8       kristaps   67:                return(EXIT_SUCCESS);
1.1       kristaps   68:
1.12    ! schwarze   69:        if (NULL == (e = exprcomp(argc, argv))) {
1.10      kristaps   70:                fprintf(stderr, "Bad expression\n");
                     71:                return(EXIT_FAILURE);
                     72:        }
1.1       kristaps   73:
1.2       kristaps   74:        /*
                     75:         * Configure databases.
                     76:         * The keyword database is a btree that allows for duplicate
                     77:         * entries.
                     78:         * The index database is a recno.
                     79:         */
                     80:
1.10      kristaps   81:        apropos_search(&opts, e, NULL, list);
                     82:        exprfree(e);
1.8       kristaps   83:        return(EXIT_SUCCESS);
1.1       kristaps   84: }
                     85:
1.8       kristaps   86: /* ARGSUSED */
1.1       kristaps   87: static void
1.8       kristaps   88: list(struct rec *res, size_t sz, void *arg)
1.1       kristaps   89: {
1.8       kristaps   90:        int              i;
1.1       kristaps   91:
1.8       kristaps   92:        qsort(res, sz, sizeof(struct rec), cmp);
1.1       kristaps   93:
1.8       kristaps   94:        for (i = 0; i < (int)sz; i++)
1.1       kristaps   95:                printf("%s(%s%s%s) - %s\n", res[i].title,
                     96:                                res[i].cat,
                     97:                                *res[i].arch ? "/" : "",
                     98:                                *res[i].arch ? res[i].arch : "",
                     99:                                res[i].desc);
                    100: }
                    101:
1.8       kristaps  102: static int
                    103: cmp(const void *p1, const void *p2)
                    104: {
                    105:
                    106:        return(strcmp(((const struct rec *)p1)->title,
                    107:                                ((const struct rec *)p2)->title));
                    108: }
                    109:
1.1       kristaps  110: static void
                    111: usage(void)
                    112: {
                    113:
                    114:        fprintf(stderr, "usage: %s "
1.8       kristaps  115:                        "[-I] "
1.9       kristaps  116:                        "[-S arch] "
                    117:                        "[-s section] "
1.10      kristaps  118:                        "EXPR\n",
                    119:                        progname);
1.1       kristaps  120: }

CVSweb