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

Annotation of mandoc/apropos.c, Revision 1.35

1.35    ! schwarze    1: /*     $Id: apropos.c,v 1.34 2013/07/05 09:33:02 schwarze Exp $ */
1.1       kristaps    2: /*
1.31      kristaps    3:  * Copyright (c) 2012 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:  */
1.13      kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
1.27      kristaps   20: #include <sys/param.h>
1.13      kristaps   21:
1.1       kristaps   22: #include <assert.h>
                     23: #include <getopt.h>
1.33      kristaps   24: #include <stdint.h>
1.1       kristaps   25: #include <stdio.h>
                     26: #include <stdlib.h>
                     27: #include <string.h>
1.29      kristaps   28: #include <unistd.h>
1.1       kristaps   29:
1.18      kristaps   30: #include "manpath.h"
1.31      kristaps   31: #include "mansearch.h"
1.1       kristaps   32:
                     33: int
                     34: main(int argc, char *argv[])
                     35: {
1.32      kristaps   36:        int              ch, whatis;
                     37:        struct mansearch search;
1.31      kristaps   38:        size_t           i, sz;
                     39:        struct manpage  *res;
1.15      kristaps   40:        struct manpaths  paths;
1.34      schwarze   41:        char            *defpaths, *auxpaths;
                     42:        char            *conf_file;
1.31      kristaps   43:        char            *progname;
                     44:        extern char     *optarg;
1.1       kristaps   45:        extern int       optind;
                     46:
                     47:        progname = strrchr(argv[0], '/');
                     48:        if (progname == NULL)
                     49:                progname = argv[0];
                     50:        else
                     51:                ++progname;
                     52:
1.34      schwarze   53:        whatis = (0 == strncmp(progname, "whatis", 6));
                     54:
1.15      kristaps   55:        memset(&paths, 0, sizeof(struct manpaths));
1.32      kristaps   56:        memset(&search, 0, sizeof(struct mansearch));
1.34      schwarze   57:
                     58:        auxpaths = defpaths = NULL;
                     59:        conf_file = NULL;
1.15      kristaps   60:
1.24      schwarze   61:        while (-1 != (ch = getopt(argc, argv, "C:M:m:S:s:")))
1.1       kristaps   62:                switch (ch) {
1.24      schwarze   63:                case ('C'):
                     64:                        conf_file = optarg;
                     65:                        break;
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.32      kristaps   73:                        search.arch = optarg;
1.1       kristaps   74:                        break;
1.9       kristaps   75:                case ('s'):
1.32      kristaps   76:                        search.sec = optarg;
1.1       kristaps   77:                        break;
                     78:                default:
1.31      kristaps   79:                        goto usage;
1.1       kristaps   80:                }
                     81:
                     82:        argc -= optind;
                     83:        argv += optind;
                     84:
1.31      kristaps   85:        if (0 == argc)
                     86:                goto usage;
1.15      kristaps   87:
1.32      kristaps   88:        search.deftype = whatis ? TYPE_Nm : TYPE_Nm | TYPE_Nd;
                     89:        search.flags = whatis ? MANSEARCH_WHATIS : 0;
                     90:
1.24      schwarze   91:        manpath_parse(&paths, conf_file, defpaths, auxpaths);
1.32      kristaps   92:        ch = mansearch(&search, &paths, argc, argv, &res, &sz);
1.18      kristaps   93:        manpath_free(&paths);
1.28      kristaps   94:
1.31      kristaps   95:        if (0 == ch)
                     96:                goto usage;
1.28      kristaps   97:
1.31      kristaps   98:        for (i = 0; i < sz; i++) {
1.35    ! schwarze   99:                printf("%s - %s\n", res[i].names, res[i].desc);
        !           100:                free(res[i].file);
        !           101:                free(res[i].names);
1.31      kristaps  102:                free(res[i].desc);
1.28      kristaps  103:        }
                    104:
1.31      kristaps  105:        free(res);
                    106:        return(sz ? EXIT_SUCCESS : EXIT_FAILURE);
                    107: usage:
1.34      schwarze  108:        fprintf(stderr, "usage: %s [-C file] [-M path] [-m path] "
                    109:                        "[-S arch] [-s section]%s ...\n", progname,
                    110:                        whatis ? " name" : "\n               expression");
1.28      kristaps  111:        return(EXIT_FAILURE);
1.1       kristaps  112: }

CVSweb