-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubnom.c
301 lines (290 loc) · 9.37 KB
/
subnom.c
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* 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.
*/
#include <err.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#ifndef NOMBRE_H
#include "nombre.h"
#endif
#ifndef NOMBRE_INITDB_H
#include "initdb.h"
#endif
#ifndef NOMBRE_PARSECMD_H
#include "parsecmd.h"
#endif
#ifndef NOMBRE_SUBNOM_H
#include "subnom.h"
#endif
extern char *__progname;
extern char **environ;
extern bool dbg;
/*
* This function handles the handoff to other functions as needed to build the appropriate SQL
* statements to do what the user asked of us. As a manner of convention, the
* group directive must be present before any other commands.
*/
int
buildcmd(nomcmd * restrict cmdbuf, const char ** restrict argstr) {
int retc;
uint32_t andmask;
retc = 0;
andmask = (unsigned int)(~grpcmd);
if (dbg) {
NOMDBG("Entering with cmdbuf = %p, argstr = %p, andmask = %X\n", (void *)cmdbuf, (const void *)argstr, andmask);
}
if ((cmdbuf == NULL) || (argstr == NULL)) {
retc = BADARGS;
} else {
/* Since we can't be sure we have a valid database connection at this time, open one */
if (cmdbuf->dbcon == NULL) {
/* Likely use the functions in initdb.h to connect */
if ((retc = nom_getdbn(cmdbuf->filedata[NOMBRE_DBFILE])) == NOM_OK) {
retc = nom_dbconn(cmdbuf);
}
}
}
retc = parsecmd(cmdbuf, *argstr++);
if (cmdbuf->command == grpcmd) {
retc = parsecmd(cmdbuf, *argstr);
/* Only increment again if there's a good subcommand given */
if (retc == NOM_OK) {
argstr++;
}
}
/*
* Set our andmask to unset the 30th bit
* called functions will be able to check for this bit at entry
*/
if (dbg) {
NOMDBG("cmdbuf->command = %X (%u), andmask = %X (%u), (cmdbuf->command & andmask) = %X (%u)\n",
cmdbuf->command, cmdbuf->command, andmask, andmask, (cmdbuf->command & andmask), (cmdbuf->command & andmask));
}
/* Now that we know we have a good database connection, determine what we need to do next */
switch (cmdbuf->command & andmask) {
case (lookup):
retc = nombre_lookup(cmdbuf, argstr);
break;
/*
* This may be a bit deceptively named, but I'm sticking with it for now.
* It's meant to be interpreted in the sense of the user defining a term, not
* the user looking for a term's definition
*/
case (define):
retc = nombre_newdef(cmdbuf, argstr);
break;
case (search):
retc = nombre_ksearch(cmdbuf, argstr);
break;
case (delete):
retc = nombre_delete(cmdbuf, argstr);
break;
case (new):
retc = nombre_newgrp(cmdbuf, argstr);
break;
case (import):
break;
case (export):
break;
case (dumpdb):
retc = nombre_dbdump(cmdbuf, argstr);
break;
case (addsrc):
break;
case (update):
break;
case (vquery):
break;
case (catscn):
break;
/*
* Assume the user just didn't type "def"
* Push the pointer back to the first argument in the string
*/
default:
/* XXX: It should not be possible to reach this code */
retc = nombre_lookup(cmdbuf, --argstr);
break;
}
if (retc == 0) {
retc = runcmd(cmdbuf, (int)strlen(cmdbuf->gensql));
}
if (dbg) {
NOMDBG("Returning %d to caller\n", retc);
}
return(retc);
}
int
runcmd(nomcmd * restrict cmdbuf, int genlen) {
int retc;
const char *sqltail;
sqlite3_stmt *stmt;
retc = 0;
sqltail = NULL; stmt = NULL;
if (dbg) {
NOMDBG("Entering with cmdbuf->command = %d, genlen = %d, cmdbuf->gensql = %p\n", cmdbuf->command, genlen, (void *)cmdbuf->gensql);
}
if (cmdbuf == NULL) {
NOMERR("%s", "Given invalid input!\n");
retc = BADARGS;
}
if (dbg) {
NOMDBG("Attempting to run generated SQL = %s\n", cmdbuf->gensql);
}
if ((retc = sqlite3_prepare_v2(cmdbuf->dbcon, cmdbuf->gensql, genlen, &stmt, &sqltail)) != SQLITE_OK) {
NOMERR("Error compiling SQL (%s)!\n", sqlite3_errstr(sqlite3_errcode(cmdbuf->dbcon)));
}
/*
* Determine how to best proceed with processing the statement based on
* the value of cmdbuf->command
*/
switch (cmdbuf->command & (unsigned int)(~grpcmd)) {
case (lookup):
retc = sqlite3_step(stmt);
if (retc == SQLITE_DONE) {
fprintf(stdout,"%s: unknown\n", cmdbuf->defdata[NOMBRE_DBTERM]);
return(retc ^= retc);
}
for (register uint_fast16_t i = 1; retc == SQLITE_ROW; i++, retc = sqlite3_step(stmt)) {
if (i > 1) {
/* Because the size here is apparently inconsistent across platforms */
#if defined(__linux__)
fprintf(stdout," #%zu: %s\n", i, sqlite3_column_text(stmt,0));
#else
fprintf(stdout," #%u: %s\n", i, sqlite3_column_text(stmt,0));
#endif
} else {
fprintf(stdout,"%s: %s\n", cmdbuf->defdata[NOMBRE_DBTERM], sqlite3_column_text(stmt,0));
}
}
retc ^= retc;
break;
case (define):
retc = sqlite3_step(stmt);
if (retc == SQLITE_DONE) {
sqlite3_finalize(stmt);
if ((cmdbuf->command & grpcmd) == grpcmd) {
fprintf(stdout,"Added definition for %s/%s\n",cmdbuf->defdata[NOMBRE_DBCATG], cmdbuf->defdata[NOMBRE_DBTERM]);
} else {
fprintf(stdout,"Added definition for %s\n", cmdbuf->defdata[NOMBRE_DBTERM]);
}
retc ^= retc;
} else if (retc == SQLITE_CONSTRAINT) {
/* Definition already exists, reset VM and run altdef function */
sqlite3_reset(stmt); sqlite3_finalize(stmt);
sqltail = NULL; stmt = NULL;
retc = nombre_altdef(cmdbuf);
if (retc != 0) {
NOMERR("Error creating altdef for %s: returned: %d\n", cmdbuf->defdata[NOMBRE_DBTERM], retc);
break;
}
retc = sqlite3_prepare_v2(cmdbuf->dbcon, cmdbuf->gensql, -1, &stmt, &sqltail);
if (retc != SQLITE_OK) { NOMERR("%s %d\n", sqlite3_errstr(retc), retc); } /* Appears to solve a potential race condition */
retc = sqlite3_step(stmt);
if (retc == SQLITE_MISUSE) { NOMERR("%s","You dun fucked up in the SQL reset\n"); }
if (retc == SQLITE_DONE) {
fprintf(stdout, "Added new alternative definition for %s\n", cmdbuf->defdata[NOMBRE_DBTERM]);
retc ^= retc;
}
}
break;
case (delete):
retc = sqlite3_step(stmt);
if (retc == SQLITE_DONE) {
sqlite3_finalize(stmt);
if ((cmdbuf->command & grpcmd) == grpcmd) {
fprintf(stdout, "This path is not yet built.\n");
} else {
fprintf(stdout, "Deleted \"definitions\" entry for %s\n", cmdbuf->defdata[NOMBRE_DBTERM]);
}
retc ^= retc;
} else {
NOMERR("%s", sqlite3_errstr(retc));
}
break;
case (dumpdb):
fprintf(stdout,"Here's what I know:\n");
retc = sqlite3_step(stmt);
for (register uint_fast16_t i = 1; retc == SQLITE_ROW; i++, retc = sqlite3_step(stmt)) {
fprintf(stdout," (%s/%s): %s\n", sqlite3_column_text(stmt,0), sqlite3_column_text(stmt,1), sqlite3_column_text(stmt,2));
}
if (retc != SQLITE_DONE) {
NOMERR("Error processing command! (%s)\n", sqlite3_errstr(sqlite3_errcode(cmdbuf->dbcon)));
} else {
retc ^= retc;
}
sqlite3_finalize(stmt);
break;
case (search):
fprintf(stdout,"Found the following matches:\n");
retc = sqlite3_step(stmt);
for (; retc == SQLITE_ROW; retc = sqlite3_step(stmt)) {
fprintf(stdout," %s: %s\n", sqlite3_column_text(stmt,0), sqlite3_column_text(stmt,1));
}
if (retc != SQLITE_DONE) {
NOMERR("Error processing command! (%s)\n", sqlite3_errstr(sqlite3_errcode(cmdbuf->dbcon)));
} else {
retc ^= retc;
}
sqlite3_finalize(stmt);
break;
default:
/* Should not be reachable */
NOMERR("%s\n", "It should not be possible to reach this code!");
sqlite3_finalize(stmt);
return(retc);
}
if (dbg) {
NOMDBG("Returning %d to caller\n", retc);
}
return(retc);
}
int
nomdb_impt(nomcmd * restrict cmdbuf) {
int retc;
retc = 0;
if (dbg) {
NOMDBG("Entering with cmdbuf = %p\n", (void *)cmdbuf);
}
if (cmdbuf == NULL) {
NOMERR("%s", "Given invalid input!\n");
retc = BADARGS;
}
if (dbg) {
NOMDBG("Returning %d to caller\n", retc);
}
return(retc);
}