2014-05-07 04:26:05 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2014, Timothy Stack
|
|
|
|
*
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted 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 Timothy Stack nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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;
|
2022-03-16 22:38:08 +00:00
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
2014-05-07 04:26:05 +00:00
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
#include "environ_vtab.hh"
|
2014-05-07 04:26:05 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2022-04-12 23:07:13 +00:00
|
|
|
#include "base/auto_mem.hh"
|
2019-05-08 12:30:59 +00:00
|
|
|
#include "base/lnav_log.hh"
|
2022-03-16 22:38:08 +00:00
|
|
|
#include "config.h"
|
2014-05-07 04:26:05 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
extern char** environ;
|
2014-05-07 04:26:05 +00:00
|
|
|
|
2022-03-25 23:38:11 +00:00
|
|
|
const char* const ENVIRON_CREATE_STMT = R"(
|
2020-10-29 04:23:46 +00:00
|
|
|
-- Access lnav's environment variables through this table.
|
|
|
|
CREATE TABLE environ (
|
2022-05-23 03:44:18 +00:00
|
|
|
name TEXT PRIMARY KEY,
|
|
|
|
value TEXT
|
2020-10-29 04:23:46 +00:00
|
|
|
);
|
|
|
|
)";
|
2014-05-07 04:26:05 +00:00
|
|
|
|
2022-09-19 08:50:54 +00:00
|
|
|
struct env_vtab {
|
2022-03-16 22:38:08 +00:00
|
|
|
sqlite3_vtab base;
|
|
|
|
sqlite3* db;
|
2014-05-07 04:26:05 +00:00
|
|
|
};
|
|
|
|
|
2022-08-29 06:07:55 +00:00
|
|
|
struct env_vtab_cursor {
|
2022-03-16 22:38:08 +00:00
|
|
|
sqlite3_vtab_cursor base;
|
|
|
|
char** env_cursor;
|
2014-05-07 04:26:05 +00:00
|
|
|
};
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static int vt_destructor(sqlite3_vtab* p_svt);
|
2014-05-07 04:26:05 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static int
|
|
|
|
vt_create(sqlite3* db,
|
|
|
|
void* pAux,
|
|
|
|
int argc,
|
|
|
|
const char* const* argv,
|
|
|
|
sqlite3_vtab** pp_vt,
|
|
|
|
char** pzErr)
|
2014-05-07 04:26:05 +00:00
|
|
|
{
|
2022-09-19 08:50:54 +00:00
|
|
|
env_vtab* p_vt;
|
2014-05-07 04:26:05 +00:00
|
|
|
|
|
|
|
/* Allocate the sqlite3_vtab/vtab structure itself */
|
2022-09-19 08:50:54 +00:00
|
|
|
p_vt = (env_vtab*) sqlite3_malloc(sizeof(*p_vt));
|
2014-05-07 04:26:05 +00:00
|
|
|
|
|
|
|
if (p_vt == NULL) {
|
|
|
|
return SQLITE_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&p_vt->base, 0, sizeof(sqlite3_vtab));
|
|
|
|
p_vt->db = db;
|
|
|
|
|
|
|
|
*pp_vt = &p_vt->base;
|
|
|
|
|
|
|
|
int rc = sqlite3_declare_vtab(db, ENVIRON_CREATE_STMT);
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static int
|
|
|
|
vt_destructor(sqlite3_vtab* p_svt)
|
2014-05-07 04:26:05 +00:00
|
|
|
{
|
2022-09-19 08:50:54 +00:00
|
|
|
env_vtab* p_vt = (env_vtab*) p_svt;
|
2014-05-07 04:26:05 +00:00
|
|
|
|
|
|
|
/* Free the SQLite structure */
|
|
|
|
sqlite3_free(p_vt);
|
|
|
|
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static int
|
|
|
|
vt_connect(sqlite3* db,
|
|
|
|
void* p_aux,
|
|
|
|
int argc,
|
|
|
|
const char* const* argv,
|
|
|
|
sqlite3_vtab** pp_vt,
|
|
|
|
char** pzErr)
|
2014-05-07 04:26:05 +00:00
|
|
|
{
|
|
|
|
return vt_create(db, p_aux, argc, argv, pp_vt, pzErr);
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static int
|
|
|
|
vt_disconnect(sqlite3_vtab* pVtab)
|
2014-05-07 04:26:05 +00:00
|
|
|
{
|
|
|
|
return vt_destructor(pVtab);
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static int
|
|
|
|
vt_destroy(sqlite3_vtab* p_vt)
|
2014-05-07 04:26:05 +00:00
|
|
|
{
|
|
|
|
return vt_destructor(p_vt);
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static int vt_next(sqlite3_vtab_cursor* cur);
|
2014-05-07 04:26:05 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static int
|
|
|
|
vt_open(sqlite3_vtab* p_svt, sqlite3_vtab_cursor** pp_cursor)
|
2014-05-07 04:26:05 +00:00
|
|
|
{
|
2022-09-19 08:50:54 +00:00
|
|
|
env_vtab* p_vt = (env_vtab*) p_svt;
|
2014-05-07 04:26:05 +00:00
|
|
|
|
|
|
|
p_vt->base.zErrMsg = NULL;
|
|
|
|
|
2022-08-29 06:07:55 +00:00
|
|
|
env_vtab_cursor* p_cur = (env_vtab_cursor*) new env_vtab_cursor();
|
2014-05-07 04:26:05 +00:00
|
|
|
|
2015-03-29 01:41:06 +00:00
|
|
|
if (p_cur == NULL) {
|
|
|
|
return SQLITE_NOMEM;
|
|
|
|
} else {
|
2022-03-16 22:38:08 +00:00
|
|
|
*pp_cursor = (sqlite3_vtab_cursor*) p_cur;
|
2014-05-07 04:26:05 +00:00
|
|
|
|
2015-03-29 01:41:06 +00:00
|
|
|
p_cur->base.pVtab = p_svt;
|
|
|
|
p_cur->env_cursor = environ;
|
|
|
|
}
|
2014-05-07 04:26:05 +00:00
|
|
|
|
2015-03-29 01:41:06 +00:00
|
|
|
return SQLITE_OK;
|
2014-05-07 04:26:05 +00:00
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static int
|
|
|
|
vt_close(sqlite3_vtab_cursor* cur)
|
2014-05-07 04:26:05 +00:00
|
|
|
{
|
2022-08-29 06:07:55 +00:00
|
|
|
env_vtab_cursor* p_cur = (env_vtab_cursor*) cur;
|
2014-05-07 04:26:05 +00:00
|
|
|
|
|
|
|
/* Free cursor struct. */
|
|
|
|
delete p_cur;
|
|
|
|
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static int
|
|
|
|
vt_eof(sqlite3_vtab_cursor* cur)
|
2014-05-07 04:26:05 +00:00
|
|
|
{
|
2022-08-29 06:07:55 +00:00
|
|
|
env_vtab_cursor* vc = (env_vtab_cursor*) cur;
|
2014-05-07 04:26:05 +00:00
|
|
|
|
|
|
|
return vc->env_cursor[0] == NULL;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static int
|
|
|
|
vt_next(sqlite3_vtab_cursor* cur)
|
2014-05-07 04:26:05 +00:00
|
|
|
{
|
2022-08-29 06:07:55 +00:00
|
|
|
env_vtab_cursor* vc = (env_vtab_cursor*) cur;
|
2014-05-07 04:26:05 +00:00
|
|
|
|
|
|
|
if (vc->env_cursor[0] != NULL) {
|
|
|
|
vc->env_cursor += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static int
|
|
|
|
vt_column(sqlite3_vtab_cursor* cur, sqlite3_context* ctx, int col)
|
2014-05-07 04:26:05 +00:00
|
|
|
{
|
2022-08-29 06:07:55 +00:00
|
|
|
env_vtab_cursor* vc = (env_vtab_cursor*) cur;
|
2022-03-16 22:38:08 +00:00
|
|
|
const char* eq = strchr(vc->env_cursor[0], '=');
|
2014-05-07 04:26:05 +00:00
|
|
|
|
|
|
|
switch (col) {
|
2022-03-16 22:38:08 +00:00
|
|
|
case 0:
|
|
|
|
sqlite3_result_text(ctx,
|
|
|
|
vc->env_cursor[0],
|
|
|
|
eq - vc->env_cursor[0],
|
|
|
|
SQLITE_TRANSIENT);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
sqlite3_result_text(ctx, eq + 1, -1, SQLITE_TRANSIENT);
|
|
|
|
break;
|
2014-05-07 04:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static int
|
|
|
|
vt_rowid(sqlite3_vtab_cursor* cur, sqlite_int64* p_rowid)
|
2014-05-07 04:26:05 +00:00
|
|
|
{
|
2022-08-29 06:07:55 +00:00
|
|
|
env_vtab_cursor* p_cur = (env_vtab_cursor*) cur;
|
2014-05-07 04:26:05 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
*p_rowid = (int64_t) p_cur->env_cursor[0];
|
2014-05-07 04:26:05 +00:00
|
|
|
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static int
|
|
|
|
vt_best_index(sqlite3_vtab* tab, sqlite3_index_info* p_info)
|
2014-05-07 04:26:05 +00:00
|
|
|
{
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static int
|
|
|
|
vt_filter(sqlite3_vtab_cursor* p_vtc,
|
|
|
|
int idxNum,
|
|
|
|
const char* idxStr,
|
|
|
|
int argc,
|
|
|
|
sqlite3_value** argv)
|
2014-05-07 04:26:05 +00:00
|
|
|
{
|
|
|
|
return SQLITE_OK;
|
|
|
|
}
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
static int
|
|
|
|
vt_update(sqlite3_vtab* tab,
|
|
|
|
int argc,
|
|
|
|
sqlite3_value** argv,
|
|
|
|
sqlite_int64* rowid)
|
2014-05-07 04:26:05 +00:00
|
|
|
{
|
2022-03-16 22:38:08 +00:00
|
|
|
const char* name
|
|
|
|
= (argc > 2 ? (const char*) sqlite3_value_text(argv[2]) : nullptr);
|
2022-09-19 08:50:54 +00:00
|
|
|
env_vtab* p_vt = (env_vtab*) tab;
|
2015-03-21 22:28:40 +00:00
|
|
|
int retval = SQLITE_ERROR;
|
2014-05-07 04:26:05 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
if (argc != 1
|
|
|
|
&& (argc < 3 || sqlite3_value_type(argv[2]) == SQLITE_NULL
|
|
|
|
|| sqlite3_value_type(argv[3]) == SQLITE_NULL
|
|
|
|
|| sqlite3_value_text(argv[2])[0] == '\0'))
|
|
|
|
{
|
2014-05-07 04:26:05 +00:00
|
|
|
tab->zErrMsg = sqlite3_mprintf(
|
|
|
|
"A non-empty name and value must be provided when inserting an "
|
|
|
|
"environment variable");
|
|
|
|
|
|
|
|
return SQLITE_ERROR;
|
|
|
|
}
|
2020-11-10 06:17:17 +00:00
|
|
|
if (name != nullptr && strchr(name, '=') != nullptr) {
|
2014-05-07 04:26:05 +00:00
|
|
|
tab->zErrMsg = sqlite3_mprintf(
|
|
|
|
"Environment variable names cannot contain an equals sign (=)");
|
|
|
|
|
|
|
|
return SQLITE_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sqlite3_value_type(argv[0]) != SQLITE_NULL) {
|
|
|
|
int64_t index = sqlite3_value_int64(argv[0]);
|
2022-03-16 22:38:08 +00:00
|
|
|
const char* var = (const char*) index;
|
|
|
|
const char* eq = strchr(var, '=');
|
2014-05-07 04:26:05 +00:00
|
|
|
size_t namelen = eq - var;
|
|
|
|
char name[namelen + 1];
|
|
|
|
|
|
|
|
memcpy(name, var, namelen);
|
|
|
|
name[namelen] = '\0';
|
|
|
|
unsetenv(name);
|
|
|
|
|
|
|
|
retval = SQLITE_OK;
|
2020-11-10 06:17:17 +00:00
|
|
|
} else if (name != nullptr && getenv(name) != nullptr) {
|
2014-06-14 04:39:03 +00:00
|
|
|
#ifdef SQLITE_FAIL
|
2014-05-07 04:26:05 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = sqlite3_vtab_on_conflict(p_vt->db);
|
|
|
|
switch (rc) {
|
2022-03-16 22:38:08 +00:00
|
|
|
case SQLITE_FAIL:
|
|
|
|
case SQLITE_ABORT:
|
|
|
|
tab->zErrMsg = sqlite3_mprintf(
|
|
|
|
"An environment variable with the name '%s' already exists",
|
|
|
|
name);
|
|
|
|
return rc;
|
|
|
|
case SQLITE_IGNORE:
|
|
|
|
return SQLITE_OK;
|
|
|
|
case SQLITE_REPLACE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return rc;
|
2014-05-07 04:26:05 +00:00
|
|
|
}
|
2014-06-14 04:39:03 +00:00
|
|
|
#endif
|
2014-05-07 04:26:05 +00:00
|
|
|
}
|
|
|
|
|
2020-11-10 06:17:17 +00:00
|
|
|
if (name != nullptr && argc == 4) {
|
2022-03-16 22:38:08 +00:00
|
|
|
const unsigned char* value = sqlite3_value_text(argv[3]);
|
2014-05-07 04:26:05 +00:00
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
setenv((const char*) name, (const char*) value, 1);
|
2014-05-07 04:26:05 +00:00
|
|
|
|
2015-03-22 04:21:07 +00:00
|
|
|
return SQLITE_OK;
|
2014-05-07 04:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sqlite3_module vtab_module = {
|
2022-03-16 22:38:08 +00:00
|
|
|
0, /* iVersion */
|
|
|
|
vt_create, /* xCreate - create a vtable */
|
|
|
|
vt_connect, /* xConnect - associate a vtable with a connection */
|
|
|
|
vt_best_index, /* xBestIndex - best index */
|
|
|
|
vt_disconnect, /* xDisconnect - disassociate a vtable with a connection */
|
|
|
|
vt_destroy, /* xDestroy - destroy a vtable */
|
|
|
|
vt_open, /* xOpen - open a cursor */
|
|
|
|
vt_close, /* xClose - close a cursor */
|
|
|
|
vt_filter, /* xFilter - configure scan constraints */
|
|
|
|
vt_next, /* xNext - advance a cursor */
|
|
|
|
vt_eof, /* xEof - inidicate end of result set*/
|
|
|
|
vt_column, /* xColumn - read data */
|
|
|
|
vt_rowid, /* xRowid - read data */
|
|
|
|
vt_update, /* xUpdate - write data */
|
|
|
|
NULL, /* xBegin - begin transaction */
|
|
|
|
NULL, /* xSync - sync transaction */
|
|
|
|
NULL, /* xCommit - commit transaction */
|
|
|
|
NULL, /* xRollback - rollback transaction */
|
|
|
|
NULL, /* xFindFunction - function overloading */
|
2014-05-07 04:26:05 +00:00
|
|
|
};
|
|
|
|
|
2022-03-16 22:38:08 +00:00
|
|
|
int
|
|
|
|
register_environ_vtab(sqlite3* db)
|
2014-05-07 04:26:05 +00:00
|
|
|
{
|
|
|
|
auto_mem<char, sqlite3_free> errmsg;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = sqlite3_create_module(db, "environ_vtab_impl", &vtab_module, NULL);
|
2022-03-13 22:49:41 +00:00
|
|
|
ensure(rc == SQLITE_OK);
|
2022-03-16 22:38:08 +00:00
|
|
|
if ((rc = sqlite3_exec(
|
|
|
|
db,
|
2014-05-07 04:26:05 +00:00
|
|
|
"CREATE VIRTUAL TABLE environ USING environ_vtab_impl()",
|
2022-03-16 22:38:08 +00:00
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
errmsg.out()))
|
|
|
|
!= SQLITE_OK)
|
|
|
|
{
|
2020-05-07 14:08:59 +00:00
|
|
|
fprintf(stderr, "unable to create environ table %s\n", errmsg.in());
|
2014-05-07 04:26:05 +00:00
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|