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

Annotation of mandoc/apropos.c, Revision 1.10

1.10    ! kristaps    1: /*     $Id: apropos.c,v 1.9 2011/11/09 10:53:48 kristaps 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.8       kristaps   24: #include "apropos.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.10    ! kristaps   36:        int              ch, cs;
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.10    ! kristaps   50:        cs = 0;
        !            51:
        !            52:        while (-1 != (ch = getopt(argc, argv, "S:s:I")))
1.1       kristaps   53:                switch (ch) {
1.9       kristaps   54:                case ('S'):
1.1       kristaps   55:                        opts.arch = optarg;
                     56:                        break;
1.9       kristaps   57:                case ('s'):
1.1       kristaps   58:                        opts.cat = optarg;
                     59:                        break;
                     60:                case ('I'):
1.10    ! kristaps   61:                        cs = 1;
1.1       kristaps   62:                        break;
                     63:                default:
                     64:                        usage();
                     65:                        return(EXIT_FAILURE);
                     66:                }
                     67:
                     68:        argc -= optind;
                     69:        argv += optind;
                     70:
1.10    ! kristaps   71:        if (0 == argc)
1.8       kristaps   72:                return(EXIT_SUCCESS);
1.1       kristaps   73:
1.10    ! kristaps   74:        if (NULL == (e = exprcomp(cs, argv, argc))) {
        !            75:                fprintf(stderr, "Bad expression\n");
        !            76:                return(EXIT_FAILURE);
        !            77:        }
1.1       kristaps   78:
1.2       kristaps   79:        /*
                     80:         * Configure databases.
                     81:         * The keyword database is a btree that allows for duplicate
                     82:         * entries.
                     83:         * The index database is a recno.
                     84:         */
                     85:
1.10    ! kristaps   86:        apropos_search(&opts, e, NULL, list);
        !            87:        exprfree(e);
1.8       kristaps   88:        return(EXIT_SUCCESS);
1.1       kristaps   89: }
                     90:
1.8       kristaps   91: /* ARGSUSED */
1.1       kristaps   92: static void
1.8       kristaps   93: list(struct rec *res, size_t sz, void *arg)
1.1       kristaps   94: {
1.8       kristaps   95:        int              i;
1.1       kristaps   96:
1.8       kristaps   97:        qsort(res, sz, sizeof(struct rec), cmp);
1.1       kristaps   98:
1.8       kristaps   99:        for (i = 0; i < (int)sz; i++)
1.1       kristaps  100:                printf("%s(%s%s%s) - %s\n", res[i].title,
                    101:                                res[i].cat,
                    102:                                *res[i].arch ? "/" : "",
                    103:                                *res[i].arch ? res[i].arch : "",
                    104:                                res[i].desc);
                    105: }
                    106:
1.8       kristaps  107: static int
                    108: cmp(const void *p1, const void *p2)
                    109: {
                    110:
                    111:        return(strcmp(((const struct rec *)p1)->title,
                    112:                                ((const struct rec *)p2)->title));
                    113: }
                    114:
1.1       kristaps  115: static void
                    116: usage(void)
                    117: {
                    118:
                    119:        fprintf(stderr, "usage: %s "
1.8       kristaps  120:                        "[-I] "
1.9       kristaps  121:                        "[-S arch] "
                    122:                        "[-s section] "
1.10    ! kristaps  123:                        "EXPR\n",
        !           124:                        progname);
1.1       kristaps  125: }

CVSweb