-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnombre.h
129 lines (116 loc) · 5.31 KB
/
nombre.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* Copyright (c) 2019, Exile Heavy Industries
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted (subject to the limitations in the disclaimer
* below) provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its contributors may be used
* to endorse or promote products derived from this software without specific
* prior written permission.
*
* NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS
* LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
#define NOMBRE_H
#include <stdint.h>
#include <sqlite3.h>
/*
* Default location for the database file to live
* should evaluate to ~/.local/nombre.db
* can be overridden by the '-d' flag
*/
#define NOMBRE_DB_PREFIX() getenv("HOME")
/* Set the default mode to 1750 */
#define NOMBRE_DB_DIRECT_MODE S_ISVTX|S_IRWXU|S_IRGRP|S_IXGRP
#define NOMBRE_DB_DIRECT "/.local/"
#define NOMBRE_DB_NAME "nombre.db"
#define NOMBRE_ENV_VAR "NOMBREDB"
#define DIRSEP '/'
/* Some buffer size settings */
#define BUFSIZE 4096
#define PATHMAX 512
#define DEFLEN 512
/* Some general return mnemonics */
#define NOM_OK 0
#define BADARGS -1
#define NOM_USECWD -2
#define NOM_FIO_FAIL -3
#define NOM_FAIL 1
/* Are these necessary? */
#define NOM_INVALID -4
#define NOM_INCOMPLETE -32
/* Simple not implemented message */
#define NOTIMP(a) fprintf(stderr,"-%c is not yet implemented!\n",a)
/* Simple message that the flag isn't recognized */
#define BADFLAG(a) fprintf(stderr,"[ERR] %s: \'%s\' is not a valid flag!\n",__progname, a)
/* Status reporting macros */
/* INFO macro meant for general status reporting, so file/line aren't as important */
#define NOMINF(fstr, ...) fprintf(stdout, "[INF] %s [%s]: " fstr, __progname, __func__, __VA_ARGS__)
#define NOMDBG(fstr, ...) fprintf(stderr, "[DBG] %s [%s:%u] %s: " fstr, __progname, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define NOMWRN(fstr, ...) fprintf(stderr, "[WRN] %s [%s:%u] %s: " fstr, __progname, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define NOMERR(fstr, ...) fprintf(stderr, "[ERR] %s [%s:%u] %s: " fstr, __progname, __FILE__, __LINE__, __func__, __VA_ARGS__)
/* Create a "new" type called "byte" */
typedef uint8_t byte;
/* Define the values of the subcommonds */
typedef enum subcom_t {
unknown= 0, /* The starting point, doubles as late-stage error detection */
lookup = (0x01 << 0 ), /* Most likely command, look up given term */
define = (0x01 << 1 ), /* Add a definition */
search = (0x01 << 2 ), /* Perform a keyword search */
delete = (0x01 << 3 ), /* Delete an entry from the database */
dumpdb = (0x01 << 4 ), /* Dump contents to stdout */
new = (0x01 << 5 ), /* Create a new group/category (only valid as group subcommand modifier) */
import = (0x01 << 6 ), /* Import definitions from file */
export = (0x01 << 7 ), /* Export definitons to file */
addsrc = (0x01 << 8 ), /* Add an entry for the definition source */
update = (0x01 << 9 ), /* Update a definition */
vquery = (0x01 << 10), /* Lookup with sources */
/* XXX: Replace with more useful meaning */
catscn = (0x01 << 11), /* Dump the definitions for the given category to stdout */
grpcmd = (0x01 << 13) /* Operating on a group */
} subcom;
#define CMDCOUNT 13
/*
* Define data structure for command parsing
* It's a bit larger than I'd like, but there only ever needs to be one per invocation
* and then we can just pass the pointer around.
*/
typedef struct nombre_cmd_t {
subcom command;
char filedata[3][PATHMAX]; /* File argument array holder */
/* assume we're using ASCII for now, full UTF-8 will be a stretch goal */
char defdata[2][DEFLEN]; /* Fold term/category into single 2D member */
char gensql[PATHMAX]; /* Generated SQL statement, capped at 1/4 PAGE_SIZE assuming 4k pages */
sqlite3 *dbcon; /* database connection */
} nomcmd;
/*
* NOTE: Current struct size is ~3.152KiB, assuming a 48-bit pointer, but that's probably wrong,
* this size needs to remain under 4k to ensure that it's possible to fit in a single page.
*/
/*
* Add macros to help with array indexing
*/
#define NOMBRE_DBFILE 0x00
#define NOMBRE_INITSQL 0x01
#define NOMBRE_IOFILE 0x02
#define NOMBRE_DBTERM 0x00
#define NOMBRE_DBCATG 0x01