1 /*        $NetBSD: apropos-utils.h,v 1.15 2019/05/18 07:56:43 abhinav Exp $     */
2 /*-
3  * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay@gmail.com>
4  * All rights reserved.
5  *
6  * This code was developed as part of Google's Summer of Code 2011 program.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
23  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #ifndef APROPOS_UTILS_H
34 #define APROPOS_UTILS_H
35 
36 #include "sqlite3.h"
37 
38 #define MANCONF "/etc/man.conf"
39 
40 /* Flags for opening the database */
41 typedef enum mandb_access_mode {
42           MANDB_READONLY = SQLITE_OPEN_READONLY,
43           MANDB_WRITE = SQLITE_OPEN_READWRITE,
44           MANDB_CREATE = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE
45 } mandb_access_mode;
46 
47 
48 #define APROPOS_SCHEMA_VERSION 20190518
49 
50 /*
51  * Used to identify the section of a man(7) page.
52  * This is similar to the enum mdoc_sec defined in mdoc.h from mdocml project.
53  */
54 enum man_sec {
55           MANSEC_NAME = 0,
56           MANSEC_SYNOPSIS,
57           MANSEC_LIBRARY,
58           MANSEC_ERRORS,
59           MANSEC_FILES,
60           MANSEC_RETURN_VALUES,
61           MANSEC_EXIT_STATUS,
62           MANSEC_DESCRIPTION,
63           MANSEC_ENVIRONMENT,
64           MANSEC_DIAGNOSTICS,
65           MANSEC_EXAMPLES,
66           MANSEC_STANDARDS,
67           MANSEC_HISTORY,
68           MANSEC_BUGS,
69           MANSEC_AUTHORS,
70           MANSEC_COPYRIGHT,
71           MANSEC_NONE
72 };
73 
74 typedef struct query_callback_args {
75           const char *name;
76           const char *section;
77           const char *machine;
78           const char *name_desc;
79           const char *snippet;
80           size_t snippet_length;
81           void *other_data;
82 } query_callback_args;
83 
84 typedef struct query_args {
85           const char *search_str;                 // user query
86           char **sections;              // Sections in which to do the search
87           int nrec;                     // number of records to fetch
88           int offset;                   //From which position to start processing the records
89           int legacy;
90           const char *machine;
91           int (*callback) (query_callback_args *);
92           void *callback_data;          // data to pass to the callback function
93           char **errmsg;                // buffer for storing the error msg
94 } query_args;
95 
96 
97 typedef enum query_format {
98     APROPOS_NONE,
99     APROPOS_PAGER,
100     APROPOS_TERM,
101     APROPOS_HTML
102 } query_format;
103 
104 char *lower(char *);
105 void concat(char **, const char *);
106 void concat2(char **, const char *, size_t);
107 sqlite3 *init_db(mandb_access_mode, const char *);
108 void close_db(sqlite3 *);
109 char *get_dbpath(const char *);
110 int run_query(sqlite3 *, query_format, query_args *);
111 #endif
112