Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
kdb-cli: rewrite find
Browse files Browse the repository at this point in the history
  • Loading branch information
hannes99 committed Mar 3, 2023
1 parent b1a1f7c commit e9779bd
Show file tree
Hide file tree
Showing 19 changed files with 260 additions and 233 deletions.
16 changes: 12 additions & 4 deletions src/tools/kdb/command.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@
} \
/* debug -> verbose, so logLevel = debug+verbose */ \
bool verbose = debug; \
if (verbose) {}; /* disable unused variable warning */ \
if (verbose) \
{ \
}; /* disable unused variable warning */ \
tmp = GET_OPTION_KEY (options, "verbose"); \
if (tmp != NULL) \
{ \
Expand All @@ -69,7 +71,9 @@
elektraKeyToBoolean (GET_OPTION_KEY (options, "nonewline"), &noNewLine); \
} \
int colorMode = CLI_COLOR_AUTO; \
if (colorMode) {}; /* disable unused variable warning */ \
if (colorMode) \
{ \
}; /* disable unused variable warning */ \
tmp = GET_OPTION_KEY (options, "color"); \
if (tmp != NULL) \
{ \
Expand All @@ -83,14 +87,18 @@
} \
} \
char * fmtBuffer = NULL; \
if (fmtBuffer) {}; /* disable unused variable warning */ \
if (fmtBuffer) \
{ \
}; /* disable unused variable warning */ \
if (!isatty (STDOUT_FILENO)) \
{ \
colorMode = CLI_COLOR_NEVER; \
} \
\
int logLevel = verbose + debug; \
if (logLevel) {}; /* disable unused variable warning */ \
if (logLevel) \
{ \
}; /* disable unused variable warning */ \
keyDel (tmp);

#define EXEC_EXT(prog, argv, status) \
Expand Down
16 changes: 9 additions & 7 deletions src/tools/kdb/cp.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,22 @@ int execCp (KeySet * options, Key * errorKey)
Key * sourceKey = keyNew (sourceName, KEY_END);
Key * destKey = keyNew (destName, KEY_END);

if (keyGetNamespace (sourceKey) == KEY_NS_NONE || keyGetNamespace (sourceKey) == KEY_NS_CASCADING) {
if (keyGetNamespace (sourceKey) == KEY_NS_NONE || keyGetNamespace (sourceKey) == KEY_NS_CASCADING)
{
ret = 1;
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (errorKey, "source key does not specify a namespace");
elektraFree ((void*) sourceName);
elektraFree ((void*) destName);
elektraFree ((void *) sourceName);
elektraFree ((void *) destName);
keyDel (sourceKey);
keyDel (destKey);
return ret;
}
if (keyGetNamespace (destKey) == KEY_NS_NONE || keyGetNamespace (destKey) == KEY_NS_CASCADING) {
if (keyGetNamespace (destKey) == KEY_NS_NONE || keyGetNamespace (destKey) == KEY_NS_CASCADING)
{
ret = 1;
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERROR (errorKey, "destination key does not specify a namespace");
elektraFree ((void*) sourceName);
elektraFree ((void*) destName);
elektraFree ((void *) sourceName);
elektraFree ((void *) destName);
keyDel (sourceKey);
keyDel (destKey);
return ret;
Expand Down Expand Up @@ -116,7 +118,7 @@ int execCp (KeySet * options, Key * errorKey)
strcat (newName, "/");
strcat (newName, &keyName (cur)[sourceNameLen]);
}
CLI_PRINT (CLI_LOG_VERBOSE, "-> moving '%s' to '%s'\n", BOLD(keyName (cur)), BOLD(newName));
CLI_PRINT (CLI_LOG_VERBOSE, "-> moving '%s' to '%s'\n", BOLD (keyName (cur)), BOLD (newName));
Key * tmpKey = keyDup (cur, KEY_CP_ALL);
keySetName (tmpKey, newName);
ksAppendKey (newConf, tmpKey);
Expand Down
38 changes: 19 additions & 19 deletions src/tools/kdb/cp.h
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
/**
* @file
*
* @brief Header for cp command
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
* @file
*
* @brief Header for cp command
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/

#ifndef ELEKTRA_KDB_CP_H
#define ELEKTRA_KDB_CP_H

#include <kdb.h>

/**
* Adds options specification of cp command to @spec
*
* @param spec the base spec where the commands spec should be added
*/
* Adds options specification of cp command to @spec
*
* @param spec the base spec where the commands spec should be added
*/
void addCpSpec (KeySet * spec);

/**
* Executes the cp command
*
* @param options cli options and arguments as specified in addCpSpec()
* @param errorKey key where errors and warnings should be saved
*
* @retval 0 cp command ran without errors
* @retval 1 errors occurred, keyGetMeta (errorKey, "error/reason") for info
*
*/
* Executes the cp command
*
* @param options cli options and arguments as specified in addCpSpec()
* @param errorKey key where errors and warnings should be saved
*
* @retval 0 cp command ran without errors
* @retval 1 errors occurred, keyGetMeta (errorKey, "error/reason") for info
*
*/
int execCp (KeySet * options, Key * errorKey);

// helper functions
Expand Down
2 changes: 0 additions & 2 deletions src/tools/kdb/factory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include <editor.hpp>
#include <export.hpp>
#include <file.hpp>
#include <find.hpp>
#include <gen.hpp>
#include <globalmount.hpp>
#include <globalumount.hpp>
Expand Down Expand Up @@ -80,7 +79,6 @@ class Factory
m_factory.insert (std::make_pair ("mount", std::make_shared<Cnstancer<MountCommand>> ()));
m_factory.insert (std::make_pair ("remount", std::make_shared<Cnstancer<RemountCommand>> ()));
m_factory.insert (std::make_pair ("shell", std::make_shared<Cnstancer<ShellCommand>> ()));
m_factory.insert (std::make_pair ("find", std::make_shared<Cnstancer<FindCommand>> ()));
m_factory.insert (std::make_pair ("plugin-info", std::make_shared<Cnstancer<PluginInfoCommand>> ()));
m_factory.insert (std::make_pair ("test", std::make_shared<Cnstancer<TestCommand>> ()));
m_factory.insert (std::make_pair ("plugin-check", std::make_shared<Cnstancer<PluginCheckCommand>> ()));
Expand Down
101 changes: 101 additions & 0 deletions src/tools/kdb/find.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @file
*
* @brief Implementation of kdb find command
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/

#include <command.h>
#include <find.h>

#include <kdb.h>
#include <kdbassert.h>
#include <kdbease.h>
#include <kdberrors.h>
#include <regex.h>
#include <stdio.h>
#include <string.h>

#define COMMAND_NAME "find"

#define GET_OPTION_KEY(options, name) GET_OPT_KEY (options, COMMAND_BASE_KEY (COMMAND_NAME) "/" name)
#define GET_OPTION(options, name) GET_OPT (options, COMMAND_BASE_KEY (COMMAND_NAME) "/" name)

void addFindSpec (KeySet * spec)
{
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME), KEY_META, "description", "Find keys that match a regex pattern.",
KEY_META, "command", COMMAND_NAME, KEY_END));
ksAppendKey (spec, keyNew (COMMAND_SPEC_KEY (COMMAND_NAME) "/pattern", KEY_META, "description", "The patter that should be matched",
KEY_META, "args", "indexed", KEY_META, "args/index", "0", KEY_END));

ADD_BASIC_OPTIONS (spec, COMMAND_SPEC_KEY (COMMAND_NAME))
}

int execFind (KeySet * options, Key * errorKey)
{
int ret = 0;
GET_BASIC_OPTIONS

const char * patternString = GET_OPTION (options, "pattern");

regex_t regex;
if (regcomp (&regex, patternString, 0))
{
ret = 1;
ELEKTRA_SET_VALIDATION_SYNTACTIC_ERRORF (errorKey, "could not compile regex pattern: '%s'", patternString);
return ret;
}

Key * root = keyNew ("/", KEY_END);

KeySet * conf = ksNew (0, KS_END);
KDB * handle = kdbOpen (NULL, errorKey);
CLI_PRINT (CLI_LOG_DEBUG, "loading %s\n", "/");

if (kdbGet (handle, conf, root) == -1)
{
ELEKTRA_SET_VALIDATION_SEMANTIC_ERRORF (errorKey, "could not load key database", GET_ERR (root));
ret = 1;
goto cleanup;
}
CLI_PRINT (CLI_LOG_DEBUG, "loaded %s\n", "/");

Key * cur = NULL;

int regexResult;
int count = 0;
char errorMessageBuff[100];

for (elektraCursor it = 0; it < ksGetSize (conf); ++it)
{
cur = ksAtCursor (conf, it);
regexResult = regexec (&regex, keyName (cur), 0, NULL, 0);
CLI_PRINT (CLI_LOG_DEBUG, "matching '%s' -> %d\n", keyName (cur), regexResult);

if (!regexResult)
{
CLI_PRINT (CLI_LOG_NONE, "%s\n", keyName (cur));
count++;
}
else if (regexResult != REG_NOMATCH)
{
regerror (regexResult, &regex, errorMessageBuff, sizeof (errorMessageBuff));
CLI_PRINT (CLI_LOG_VERBOSE, "matching '%s' with '%s' failed: %s", keyName (cur), patternString, errorMessageBuff);
}
}

CLI_PRINT (CLI_LOG_VERBOSE, "\nfound %ld keys", count);

cleanup:
if (!noNewLine)
{
printf ("\n");
}
keyDel (root);
elektraFree ((void *) fmtBuffer);
ksDel (conf);
regfree (&regex);
kdbClose (handle, errorKey);
return ret;
}
76 changes: 0 additions & 76 deletions src/tools/kdb/find.cpp

This file was deleted.

36 changes: 36 additions & 0 deletions src/tools/kdb/find.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @file
*
* @brief Header for find command
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/

#ifndef ELEKTRA_KDB_FIND_H
#define ELEKTRA_KDB_FIND_H

#include <kdb.h>

/**
* Adds options specification of find command to @spec
*
* @param spec the base spec where the commands spec should be added
*/
void addFindSpec (KeySet * spec);

/**
* Executes the find command
*
* @param options cli options and arguments as specified in addFindSpec()
* @param errorKey key where errors and warnings should be saved
*
* @retval 0 find command ran without errors
* @retval 1 errors occurred, keyGetMeta (errorKey, "error/reason") for info
*
*/
int execFind (KeySet * options, Key * errorKey);

// helper functions
int getKeyNameDepth (const char * name);

#endif // ELEKTRA_KDB_FIND_H
Loading

0 comments on commit e9779bd

Please sign in to comment.