+static int clua_write (const data_set_t *ds, const value_list_t *vl, /* {{{ */
+ user_data_t *ud)
+{
+ clua_callback_data_t *cb = ud->data;
+ int status;
+
+ pthread_mutex_lock (&cb->lock);
+
+ status = clua_load_callback (cb->lua_state, cb->callback_id);
+ if (status != 0)
+ {
+ ERROR ("lua plugin: Unable to load callback \"%s\" (id %i).",
+ cb->lua_function_name, cb->callback_id);
+ pthread_mutex_unlock (&cb->lock);
+ return (-1);
+ }
+ /* +1 = 1 */
+
+ status = luaC_pushvaluelist (cb->lua_state, ds, vl);
+ if (status != 0)
+ {
+ lua_pop (cb->lua_state, /* nelems = */ 1); /* -1 = 0 */
+ pthread_mutex_unlock (&cb->lock);
+ ERROR ("lua plugin: luaC_pushvaluelist failed.");
+ return (-1);
+ }
+ /* +1 = 2 */
+
+ status = lua_pcall (cb->lua_state,
+ /* nargs = */ 1,
+ /* nresults = */ 1,
+ /* errfunc = */ 0); /* -2+1 = 1 */
+ if (status != 0)
+ {
+ const char *errmsg = lua_tostring (cb->lua_state, /* idx = */ -1);
+ if (errmsg == NULL)
+ ERROR ("lua plugin: Calling the write callback failed. "
+ "In addition, retrieving the error message failed.");
+ else
+ ERROR ("lua plugin: Calling the write callback failed:\n%s", errmsg);
+ lua_pop (cb->lua_state, /* nelems = */ 1); /* -1 = 0 */
+ pthread_mutex_unlock (&cb->lock);
+ return (-1);
+ }
+
+ if (!lua_isnumber (cb->lua_state, /* idx = */ -1))
+ {
+ ERROR ("lua plugin: Write function \"%s\" (id %i) did not return a numeric value.",
+ cb->lua_function_name, cb->callback_id);
+ status = -1;
+ }
+ else
+ {
+ status = (int) lua_tointeger (cb->lua_state, /* idx = */ -1);
+ }
+
+ lua_pop (cb->lua_state, /* nelems = */ 1); /* -1 = 0 */
+ pthread_mutex_unlock (&cb->lock);
+ return (status);
+} /* }}} int clua_write */
+