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

Commit

Permalink
regexdispatcher: fix plugin data handling in combination with pluginp…
Browse files Browse the repository at this point in the history
…rocess
  • Loading branch information
e1528532 committed Jun 5, 2018
1 parent 0dd7dca commit 96be930
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/bindings/haskell/libelektra-haskell.cabal.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ library
, Elektra.Ease
, Elektra.Errors
, Elektra.Invoke
, Elektra.PluginProcess
build-depends: base >= 4.9 && < 4.12
includes: kdb.h
, kdbplugin.h
, kdbease.h
, kdbinvoke.h
, kdberrors.h
, hskdberrors.h
, kdbpluginprocess.h
include-dirs: @CABAL_INCLUDE_DIRS@
, "@CMAKE_CURRENT_SOURCE_DIR@/src/include"
c-sources: "@CMAKE_CURRENT_BINARY_DIR@/src/c/hskdberrors.c"
Expand Down
2 changes: 1 addition & 1 deletion src/bindings/haskell/src/Elektra/Plugin.chs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
-- @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
--
module Elektra.Plugin (
Plugin, PluginStatus (..), PluginData (..),
Plugin, withPlugin, PluginStatus (..), PluginData (..),
elektraPluginGetConfig,
elektraPluginSetData, elektraPluginGetData,
elektraPluginOpenWith, elektraPluginCloseWith,
Expand Down
30 changes: 30 additions & 0 deletions src/bindings/haskell/src/Elektra/Pluginprocess.chs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
--
-- @file
--
-- @brief PluginProcess Haskell bindings
--
-- @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
--
module Elektra.PluginProcess (
elektraPluginProcessGetData,
elektraPluginProcessSetData
) where

{#import Elektra.Plugin#}
{#import Elektra.Invoke#}

import Foreign.Ptr (Ptr)

#include <kdbpluginprocess.h>

-- ***
-- PLUGINPROCESS METHODS
-- ***

elektraPluginProcessSetData :: PluginData d => Plugin -> d -> IO ()
elektraPluginProcessSetData p = store (elektraPluginProcessSetDataRaw p)
{#fun unsafe elektraPluginProcessSetData as elektraPluginProcessSetDataRaw {`Plugin', `Ptr ()'} -> `()' #}

elektraPluginProcessGetData :: PluginData d => Plugin -> IO (Maybe d)
elektraPluginProcessGetData p = retrieve <$> elektraPluginProcessGetDataRaw p
{#fun unsafe elektraPluginProcessGetData as elektraPluginProcessGetDataRaw {`Plugin'} -> `Ptr ()' #}
3 changes: 3 additions & 0 deletions src/include/kdbpluginprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ int elektraPluginProcessSend (const ElektraPluginProcess *, pluginprocess_t, Key

int elektraPluginProcessClose (ElektraPluginProcess *, Key *);

void elektraPluginProcessSetData (Plugin * handle, void * data);
void * elektraPluginProcessGetData (Plugin * handle);

#ifdef __cplusplus
}
}
Expand Down
33 changes: 33 additions & 0 deletions src/libs/pluginprocess/pluginprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct _ElektraPluginProcess
int pid;
int counter;
ElektraInvokeHandle * dump;
void * pluginData;
};

static void cleanupPluginData (ElektraPluginProcess * pp, Key * errorKey)
Expand Down Expand Up @@ -333,6 +334,7 @@ ElektraPluginProcess * elektraPluginProcessInit (Key * errorKey)
pp->resultPipe = getPipename (pp->pipeDirectory, "/result");
pp->dump = elektraInvokeOpen ("dump", 0, errorKey);
pp->counter = 0;
pp->pluginData = NULL;

if (pp->pipeDirectory && pp->commandPipe && pp->resultPipe && pp->dump)
{
Expand Down Expand Up @@ -401,3 +403,34 @@ int elektraPluginProcessClose (ElektraPluginProcess * pp, Key * errorKey)
if (done) cleanupPluginData (pp, errorKey);
return done;
}

/** Store a pointer to any plugin related data that is being executed inside an own process.
*
* @param plugin a pointer to the plugin
* @param data the pointer to the data
*/
void elektraPluginProcessSetData (Plugin * handle, void * data)
{
ElektraPluginProcess * pp = elektraPluginGetData (handle);
if (pp)
{
pp->pluginData = data;
}
}

/** Get a pointer to any plugin related data stored before.
*
* If elektraPluginProcessSetData was not called earlier, NULL will be returned.
*
* @param plugin a pointer to the plugin
* @retval a pointer to the data
*/
void * elektraPluginProcessGetData (Plugin * handle)
{
ElektraPluginProcess * pp = elektraPluginGetData (handle);
if (pp)
{
return pp->pluginData;
}
return NULL;
}
8 changes: 5 additions & 3 deletions src/plugins/typechecker/Elektra/Typechecker.hs.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Elektra.Key
import Elektra.KeySet
import Elektra.KDB
import Elektra.Plugin
import Elektra.PluginProcess
import Elektra.Ease
import Elektra.Invoke
import Elektra.SpecTranslator
Expand Down Expand Up @@ -41,6 +42,7 @@ typecheck p ks k c = withGlobalLogging (LogConfig Nothing ("@ENABLE_LOGGER@" ==
metakeys <- keyListMeta dk
forM_ metakeys (logDebugT . show)

logDebugT "getting config"
conf <- elektraPluginGetConfig p
logDebugT $ "the configuration is " ++ show conf

Expand Down Expand Up @@ -101,7 +103,7 @@ typecheck p ks k c = withGlobalLogging (LogConfig Nothing ("@ENABLE_LOGGER@" ==
readPrelude p name location = do
prelude <- ksNew 0
baseKey <- keyNewWithValue (name ++ "/elektra/spec/") location
ini <- elektraPluginGetData p
ini <- elektraPluginProcessGetData p
-- the handle is set otherwise opening the plugin would fail already
-- likewise we could assume the data is set as well and use fromJust, but lets stay functional
maybe (ksNew 0) (\i -> elektraInvoke2Args i "get" prelude baseKey >> ksDup prelude) ini
Expand Down Expand Up @@ -133,12 +135,12 @@ logDebugT = logDebug . T.pack
openIni :: Plugin -> Key -> IO PluginStatus
openIni p k = do
logDebugT "Opening ini"
elektraInvokeOpen "ini" Nothing (Just k) >>= ifHandle (return Error) (\i -> elektraPluginSetData p i >> return Success)
elektraInvokeOpen "ini" Nothing (Just k) >>= ifHandle (return Error) (\i -> elektraPluginProcessSetData p i >> return Success)

closeIni :: Plugin -> Key -> IO PluginStatus
closeIni p k = do
logDebugT "Closing ini"
elektraPluginGetData p >>= maybe (return Success) closeHandle
elektraPluginProcessGetData p >>= maybe (return Success) closeHandle
where
closeHandle = ifHandle (return Success) (\i -> elektraInvokeClose i (Just k) >> return Success)

Expand Down

0 comments on commit 96be930

Please sign in to comment.