1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-26 15:51:15 +02:00

C API: reformat according to proposed clang-format file

This commit is contained in:
Yorick van Pelt 2023-08-28 16:45:02 +02:00 committed by José Luis Lafuente
parent 91e53de7d3
commit e1bb799da9
No known key found for this signature in database
GPG key ID: 8A3455EBE455489A
13 changed files with 1115 additions and 1078 deletions

View file

@ -21,7 +21,8 @@
#include "gc_cpp.h"
#endif
nix_err nix_libexpr_init(nix_c_context *context) {
nix_err nix_libexpr_init(nix_c_context * context)
{
if (context)
context->last_err_code = NIX_OK;
{
@ -40,33 +41,32 @@ nix_err nix_libexpr_init(nix_c_context *context) {
NIXC_CATCH_ERRS
}
nix_err nix_expr_eval_from_string(nix_c_context *context, State *state,
const char *expr, const char *path,
Value *value) {
nix_err
nix_expr_eval_from_string(nix_c_context * context, State * state, const char * expr, const char * path, Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
nix::Expr *parsedExpr = state->state.parseExprFromString(
expr, state->state.rootPath(nix::CanonPath(path)));
nix::Expr * parsedExpr = state->state.parseExprFromString(expr, state->state.rootPath(nix::CanonPath(path)));
state->state.eval(parsedExpr, *(nix::Value *) value);
state->state.forceValue(*(nix::Value *) value, nix::noPos);
}
NIXC_CATCH_ERRS
}
nix_err nix_value_call(nix_c_context *context, State *state, Value *fn,
Value *arg, Value *value) {
nix_err nix_value_call(nix_c_context * context, State * state, Value * fn, Value * arg, Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
state->state.callFunction(*(nix::Value *)fn, *(nix::Value *)arg,
*(nix::Value *)value, nix::noPos);
state->state.callFunction(*(nix::Value *) fn, *(nix::Value *) arg, *(nix::Value *) value, nix::noPos);
state->state.forceValue(*(nix::Value *) value, nix::noPos);
}
NIXC_CATCH_ERRS
}
nix_err nix_value_force(nix_c_context *context, State *state, Value *value) {
nix_err nix_value_force(nix_c_context * context, State * state, Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -75,8 +75,8 @@ nix_err nix_value_force(nix_c_context *context, State *state, Value *value) {
NIXC_CATCH_ERRS
}
nix_err nix_value_force_deep(nix_c_context *context, State *state,
Value *value) {
nix_err nix_value_force_deep(nix_c_context * context, State * state, Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -85,8 +85,8 @@ nix_err nix_value_force_deep(nix_c_context *context, State *state,
NIXC_CATCH_ERRS
}
State *nix_state_create(nix_c_context *context, const char **searchPath_c,
Store *store) {
State * nix_state_create(nix_c_context * context, const char ** searchPath_c, Store * store)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -95,24 +95,29 @@ State *nix_state_create(nix_c_context *context, const char **searchPath_c,
for (size_t i = 0; searchPath_c[i] != nullptr; i++)
searchPath.push_back(searchPath_c[i]);
return new State{
nix::EvalState(nix::SearchPath::parse(searchPath), store->ptr)};
return new State{nix::EvalState(nix::SearchPath::parse(searchPath), store->ptr)};
}
NIXC_CATCH_ERRS_NULL
}
void nix_state_free(State *state) { delete state; }
void nix_state_free(State * state)
{
delete state;
}
#ifdef HAVE_BOEHMGC
std::unordered_map<
const void *, unsigned int, std::hash<const void *>,
const void *,
unsigned int,
std::hash<const void *>,
std::equal_to<const void *>,
traceable_allocator<std::pair<const void * const, unsigned int>>>
nix_refcounts;
std::mutex nix_refcount_lock;
nix_err nix_gc_incref(nix_c_context *context, const void *p) {
nix_err nix_gc_incref(nix_c_context * context, const void * p)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -127,7 +132,8 @@ nix_err nix_gc_incref(nix_c_context *context, const void *p) {
NIXC_CATCH_ERRS
}
nix_err nix_gc_decref(nix_c_context *context, const void *p) {
nix_err nix_gc_decref(nix_c_context * context, const void * p)
{
if (context)
context->last_err_code = NIX_OK;
@ -143,15 +149,20 @@ nix_err nix_gc_decref(nix_c_context *context, const void *p) {
NIXC_CATCH_ERRS
}
void nix_gc_now() { GC_gcollect(); }
void nix_gc_now()
{
GC_gcollect();
}
#else
void nix_gc_incref(nix_c_context *context, const void *) {
void nix_gc_incref(nix_c_context * context, const void *)
{
if (context)
context->last_err_code = NIX_OK;
return NIX_OK;
}
void nix_gc_decref(nix_c_context *context, const void *) {
void nix_gc_decref(nix_c_context * context, const void *)
{
if (context)
context->last_err_code = NIX_OK;
return NIX_OK;
@ -159,8 +170,8 @@ void nix_gc_decref(nix_c_context *context, const void *) {
void nix_gc_now() {}
#endif
void nix_gc_register_finalizer(void *obj, void *cd,
void (*finalizer)(void *obj, void *cd)) {
void nix_gc_register_finalizer(void * obj, void * cd, void (*finalizer)(void * obj, void * cd))
{
#ifdef HAVE_BOEHMGC
GC_REGISTER_FINALIZER(obj, finalizer, cd, 0, 0);
#endif

View file

@ -75,14 +75,14 @@ nix_err nix_libexpr_init(nix_c_context *context);
* @param[in] state The state of the evaluation.
* @param[in] expr The Nix expression to parse.
* @param[in] path The file path to associate with the expression.
* This is required for expressions that contain relative paths (such as `./.`) that are resolved relative to the given directory.
* This is required for expressions that contain relative paths (such as `./.`) that are resolved relative to the given
* directory.
* @param[out] value The result of the evaluation. You should allocate this
* yourself.
* @return NIX_OK if the evaluation was successful, an error code otherwise.
*/
nix_err nix_expr_eval_from_string(nix_c_context *context, State *state,
const char *expr, const char *path,
Value *value);
nix_err
nix_expr_eval_from_string(nix_c_context * context, State * state, const char * expr, const char * path, Value * value);
/**
* @brief Calls a Nix function with an argument.
@ -94,8 +94,7 @@ nix_err nix_expr_eval_from_string(nix_c_context *context, State *state,
* @param[out] value The result of the function call.
* @return NIX_OK if the function call was successful, an error code otherwise.
*/
nix_err nix_value_call(nix_c_context *context, State *state, Value *fn,
Value *arg, Value *value);
nix_err nix_value_call(nix_c_context * context, State * state, Value * fn, Value * arg, Value * value);
/**
* @brief Forces the evaluation of a Nix value.
@ -133,8 +132,7 @@ nix_err nix_value_force(nix_c_context *context, State *state, Value *value);
* @return NIX_OK if the deep force operation was successful, an error code
* otherwise.
*/
nix_err nix_value_force_deep(nix_c_context *context, State *state,
Value *value);
nix_err nix_value_force_deep(nix_c_context * context, State * state, Value * value);
/**
* @brief Create a new Nix language evaluator state.
@ -144,8 +142,7 @@ nix_err nix_value_force_deep(nix_c_context *context, State *state,
* @param[in] store The Nix store to use.
* @return A new Nix state or NULL on failure.
*/
State *nix_state_create(nix_c_context *context, const char **searchPath,
Store *store);
State * nix_state_create(nix_c_context * context, const char ** searchPath, Store * store);
/**
* @brief Frees a Nix state.
@ -203,8 +200,7 @@ void nix_gc_now();
* @param[in] cd the data to pass to the finalizer
* @param[in] finalizer the callback function, called with obj and cd
*/
void nix_gc_register_finalizer(void *obj, void *cd,
void (*finalizer)(void *obj, void *cd));
void nix_gc_register_finalizer(void * obj, void * cd, void (*finalizer)(void * obj, void * cd));
/** @} */
// cffi end

View file

@ -4,11 +4,13 @@
#include "eval.hh"
#include "attr-set.hh"
struct State {
struct State
{
nix::EvalState state;
};
struct BindingsBuilder {
struct BindingsBuilder
{
nix::BindingsBuilder builder;
};

View file

@ -20,24 +20,28 @@
#include "gc_cpp.h"
#endif
struct nix_string_return {
struct nix_string_return
{
std::string str;
};
struct nix_printer {
struct nix_printer
{
std::ostream & s;
};
struct nix_string_context {
struct nix_string_context
{
nix::NixStringContext & ctx;
};
void nix_set_string_return(nix_string_return *str, const char *c) {
void nix_set_string_return(nix_string_return * str, const char * c)
{
str->str = c;
}
nix_err nix_external_print(nix_c_context *context, nix_printer *printer,
const char *c) {
nix_err nix_external_print(nix_c_context * context, nix_printer * printer, const char * c)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -46,9 +50,8 @@ nix_err nix_external_print(nix_c_context *context, nix_printer *printer,
NIXC_CATCH_ERRS
}
nix_err nix_external_add_string_context(nix_c_context *context,
nix_string_context *ctx,
const char *c) {
nix_err nix_external_add_string_context(nix_c_context * context, nix_string_context * ctx, const char * c)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -58,17 +61,24 @@ nix_err nix_external_add_string_context(nix_c_context *context,
NIXC_CATCH_ERRS
}
class NixCExternalValue : public nix::ExternalValueBase {
class NixCExternalValue : public nix::ExternalValueBase
{
NixCExternalValueDesc & desc;
void * v;
public:
NixCExternalValue(NixCExternalValueDesc &desc, void *v) : desc(desc), v(v){};
void *get_ptr() { return v; }
NixCExternalValue(NixCExternalValueDesc & desc, void * v)
: desc(desc)
, v(v){};
void * get_ptr()
{
return v;
}
/**
* Print out the value
*/
virtual std::ostream &print(std::ostream &str) const override {
virtual std::ostream & print(std::ostream & str) const override
{
nix_printer p{str};
desc.print(v, &p);
return str;
@ -77,7 +87,8 @@ public:
/**
* Return a simple string describing the type
*/
virtual std::string showType() const override {
virtual std::string showType() const override
{
nix_string_return res;
desc.showType(v, &res);
return std::move(res.str);
@ -86,7 +97,8 @@ public:
/**
* Return a string to be used in builtins.typeOf
*/
virtual std::string typeOf() const override {
virtual std::string typeOf() const override
{
nix_string_return res;
desc.typeOf(v, &res);
return std::move(res.str);
@ -95,21 +107,18 @@ public:
/**
* Coerce the value to a string.
*/
virtual std::string coerceToString(const nix::Pos &pos,
nix::NixStringContext &context,
bool copyMore,
bool copyToStore) const override {
virtual std::string coerceToString(
const nix::Pos & pos, nix::NixStringContext & context, bool copyMore, bool copyToStore) const override
{
if (!desc.coerceToString) {
return nix::ExternalValueBase::coerceToString(pos, context, copyMore,
copyToStore);
return nix::ExternalValueBase::coerceToString(pos, context, copyMore, copyToStore);
}
nix_string_context ctx{context};
nix_string_return res{""};
// todo: pos, errors
desc.coerceToString(v, &ctx, copyMore, copyToStore, &res);
if (res.str.empty()) {
return nix::ExternalValueBase::coerceToString(pos, context, copyMore,
copyToStore);
return nix::ExternalValueBase::coerceToString(pos, context, copyMore, copyToStore);
}
return std::move(res.str);
}
@ -117,7 +126,8 @@ public:
/**
* Compare to another value of the same type.
*/
virtual bool operator==(const ExternalValueBase &b) const override {
virtual bool operator==(const ExternalValueBase & b) const override
{
if (!desc.equal) {
return false;
}
@ -130,20 +140,17 @@ public:
/**
* Print the value as JSON.
*/
virtual nlohmann::json
printValueAsJSON(nix::EvalState &state, bool strict,
nix::NixStringContext &context,
bool copyToStore = true) const override {
virtual nlohmann::json printValueAsJSON(
nix::EvalState & state, bool strict, nix::NixStringContext & context, bool copyToStore = true) const override
{
if (!desc.printValueAsJSON) {
return nix::ExternalValueBase::printValueAsJSON(state, strict, context,
copyToStore);
return nix::ExternalValueBase::printValueAsJSON(state, strict, context, copyToStore);
}
nix_string_context ctx{context};
nix_string_return res{""};
desc.printValueAsJSON(v, (State *) &state, strict, &ctx, copyToStore, &res);
if (res.str.empty()) {
return nix::ExternalValueBase::printValueAsJSON(state, strict, context,
copyToStore);
return nix::ExternalValueBase::printValueAsJSON(state, strict, context, copyToStore);
}
return nlohmann::json::parse(res.str);
}
@ -151,25 +158,28 @@ public:
/**
* Print the value as XML.
*/
virtual void printValueAsXML(nix::EvalState &state, bool strict,
bool location, nix::XMLWriter &doc,
virtual void printValueAsXML(
nix::EvalState & state,
bool strict,
bool location,
nix::XMLWriter & doc,
nix::NixStringContext & context,
nix::PathSet & drvsSeen,
const nix::PosIdx pos) const override {
const nix::PosIdx pos) const override
{
if (!desc.printValueAsXML) {
return nix::ExternalValueBase::printValueAsXML(
state, strict, location, doc, context, drvsSeen, pos);
return nix::ExternalValueBase::printValueAsXML(state, strict, location, doc, context, drvsSeen, pos);
}
nix_string_context ctx{context};
desc.printValueAsXML(v, (State *)&state, strict, location, &doc, &ctx,
&drvsSeen, *reinterpret_cast<const uint32_t *>(&pos));
desc.printValueAsXML(
v, (State *) &state, strict, location, &doc, &ctx, &drvsSeen, *reinterpret_cast<const uint32_t *>(&pos));
}
virtual ~NixCExternalValue() override{};
};
ExternalValue *nix_create_external_value(nix_c_context *context,
NixCExternalValueDesc *desc, void *v) {
ExternalValue * nix_create_external_value(nix_c_context * context, NixCExternalValueDesc * desc, void * v)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -184,7 +194,8 @@ ExternalValue *nix_create_external_value(nix_c_context *context,
NIXC_CATCH_ERRS_NULL
}
void *nix_get_external_value_content(nix_c_context *context, ExternalValue *b) {
void * nix_get_external_value_content(nix_c_context * context, ExternalValue * b)
{
if (context)
context->last_err_code = NIX_OK;
try {

View file

@ -52,8 +52,7 @@ void nix_set_string_return(nix_string_return *str, const char *c);
* @param[in] str The string to print
* @returns NIX_OK if everything worked
*/
nix_err nix_external_print(nix_c_context *context, nix_printer *printer,
const char *str);
nix_err nix_external_print(nix_c_context * context, nix_printer * printer, const char * str);
/**
* Add string context to the nix_string_context object
@ -62,9 +61,7 @@ nix_err nix_external_print(nix_c_context *context, nix_printer *printer,
* @param[in] c The context string to add
* @returns NIX_OK if everything worked
*/
nix_err nix_external_add_string_context(nix_c_context *context,
nix_string_context *string_context,
const char *c);
nix_err nix_external_add_string_context(nix_c_context * context, nix_string_context * string_context, const char * c);
/**
* @brief Definition for a class of external values
@ -76,7 +73,8 @@ nix_err nix_external_add_string_context(nix_c_context *context,
*
* @see nix_create_external_value
*/
typedef struct NixCExternalValueDesc {
typedef struct NixCExternalValueDesc
{
/**
* @brief Called when printing the external value
*
@ -110,8 +108,8 @@ typedef struct NixCExternalValueDesc {
* @param[out] res the return value. Not touching this, or setting it to the
* empty string, will make the conversion throw an error.
*/
void (*coerceToString)(void *self, nix_string_context *c, int coerceMore,
int copyToStore, nix_string_return *res);
void (*coerceToString)(
void * self, nix_string_context * c, int coerceMore, int copyToStore, nix_string_return * res);
/**
* @brief Try to compare two external values
*
@ -137,9 +135,8 @@ typedef struct NixCExternalValueDesc {
* @param[out] res the return value. Gets parsed as JSON. Not touching this,
* or setting it to the empty string, will make the conversion throw an error.
*/
void (*printValueAsJSON)(void *self, State *, int strict,
nix_string_context *c, bool copyToStore,
nix_string_return *res);
void (*printValueAsJSON)(
void * self, State *, int strict, nix_string_context * c, bool copyToStore, nix_string_return * res);
/**
* @brief Convert the external value to XML
*
@ -156,9 +153,8 @@ typedef struct NixCExternalValueDesc {
* @param[in,out] drvsSeen a path set to avoid duplicating derivations
* @param[in] pos The position of the call.
*/
void (*printValueAsXML)(void *self, State *, int strict, int location,
void *doc, nix_string_context *c, void *drvsSeen,
int pos);
void (*printValueAsXML)(
void * self, State *, int strict, int location, void * doc, nix_string_context * c, void * drvsSeen, int pos);
} NixCExternalValueDesc;
/**
@ -173,8 +169,7 @@ typedef struct NixCExternalValueDesc {
* @returns external value, owned by the garbage collector
* @see nix_set_external
*/
ExternalValue *nix_create_external_value(nix_c_context *context,
NixCExternalValueDesc *desc, void *v);
ExternalValue * nix_create_external_value(nix_c_context * context, NixCExternalValueDesc * desc, void * v);
/**
* @brief Extract the pointer from a nix c external value.

View file

@ -18,22 +18,25 @@
#endif
// Helper function to throw an exception if value is null
static const nix::Value &check_value_not_null(const Value *value) {
static const nix::Value & check_value_not_null(const Value * value)
{
if (!value) {
throw std::runtime_error("Value is null");
}
return *((const nix::Value *) value);
}
static nix::Value &check_value_not_null(Value *value) {
static nix::Value & check_value_not_null(Value * value)
{
if (!value) {
throw std::runtime_error("Value is null");
}
return *((nix::Value *) value);
}
PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity,
const char *name, const char **args, const char *doc) {
PrimOp * nix_alloc_primop(
nix_c_context * context, PrimOpFun fun, int arity, const char * name, const char ** args, const char * doc)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -42,11 +45,7 @@ PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity,
#ifdef HAVE_BOEHMGC
(GC)
#endif
nix::PrimOp{.name = name,
.args = {},
.arity = (size_t)arity,
.doc = doc,
.fun = fun2};
nix::PrimOp{.name = name, .args = {}, .arity = (size_t) arity, .doc = doc, .fun = fun2};
if (args)
for (size_t i = 0; args[i]; i++)
p->args.emplace_back(*args);
@ -56,7 +55,8 @@ PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity,
NIXC_CATCH_ERRS_NULL
}
nix_err nix_register_primop(nix_c_context *context, PrimOp *primOp) {
nix_err nix_register_primop(nix_c_context * context, PrimOp * primOp)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -65,7 +65,8 @@ nix_err nix_register_primop(nix_c_context *context, PrimOp *primOp) {
NIXC_CATCH_ERRS
}
Value *nix_alloc_value(nix_c_context *context, State *state) {
Value * nix_alloc_value(nix_c_context * context, State * state)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -76,7 +77,8 @@ Value *nix_alloc_value(nix_c_context *context, State *state) {
NIXC_CATCH_ERRS_NULL
}
ValueType nix_get_type(nix_c_context *context, const Value *value) {
ValueType nix_get_type(nix_c_context * context, const Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -111,7 +113,8 @@ ValueType nix_get_type(nix_c_context *context, const Value *value) {
NIXC_CATCH_ERRS_RES(NIX_TYPE_NULL);
}
const char *nix_get_typename(nix_c_context *context, const Value *value) {
const char * nix_get_typename(nix_c_context * context, const Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -122,7 +125,8 @@ const char *nix_get_typename(nix_c_context *context, const Value *value) {
NIXC_CATCH_ERRS_NULL
}
bool nix_get_bool(nix_c_context *context, const Value *value) {
bool nix_get_bool(nix_c_context * context, const Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -133,7 +137,8 @@ bool nix_get_bool(nix_c_context *context, const Value *value) {
NIXC_CATCH_ERRS_RES(false);
}
const char *nix_get_string(nix_c_context *context, const Value *value) {
const char * nix_get_string(nix_c_context * context, const Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -144,7 +149,8 @@ const char *nix_get_string(nix_c_context *context, const Value *value) {
NIXC_CATCH_ERRS_NULL
}
const char *nix_get_path_string(nix_c_context *context, const Value *value) {
const char * nix_get_path_string(nix_c_context * context, const Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -155,7 +161,8 @@ const char *nix_get_path_string(nix_c_context *context, const Value *value) {
NIXC_CATCH_ERRS_NULL
}
unsigned int nix_get_list_size(nix_c_context *context, const Value *value) {
unsigned int nix_get_list_size(nix_c_context * context, const Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -166,7 +173,8 @@ unsigned int nix_get_list_size(nix_c_context *context, const Value *value) {
NIXC_CATCH_ERRS_RES(0);
}
unsigned int nix_get_attrs_size(nix_c_context *context, const Value *value) {
unsigned int nix_get_attrs_size(nix_c_context * context, const Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -177,7 +185,8 @@ unsigned int nix_get_attrs_size(nix_c_context *context, const Value *value) {
NIXC_CATCH_ERRS_RES(0);
}
double nix_get_float(nix_c_context *context, const Value *value) {
double nix_get_float(nix_c_context * context, const Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -188,7 +197,8 @@ double nix_get_float(nix_c_context *context, const Value *value) {
NIXC_CATCH_ERRS_RES(NAN);
}
int64_t nix_get_int(nix_c_context *context, const Value *value) {
int64_t nix_get_int(nix_c_context * context, const Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -199,7 +209,8 @@ int64_t nix_get_int(nix_c_context *context, const Value *value) {
NIXC_CATCH_ERRS_RES(0);
}
ExternalValue *nix_get_external(nix_c_context *context, Value *value) {
ExternalValue * nix_get_external(nix_c_context * context, Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -210,8 +221,8 @@ ExternalValue *nix_get_external(nix_c_context *context, Value *value) {
NIXC_CATCH_ERRS_NULL;
}
Value *nix_get_list_byidx(nix_c_context *context, const Value *value,
State *state, unsigned int ix) {
Value * nix_get_list_byidx(nix_c_context * context, const Value * value, State * state, unsigned int ix)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -225,8 +236,8 @@ Value *nix_get_list_byidx(nix_c_context *context, const Value *value,
NIXC_CATCH_ERRS_NULL
}
Value *nix_get_attr_byname(nix_c_context *context, const Value *value,
State *state, const char *name) {
Value * nix_get_attr_byname(nix_c_context * context, const Value * value, State * state, const char * name)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -245,8 +256,8 @@ Value *nix_get_attr_byname(nix_c_context *context, const Value *value,
NIXC_CATCH_ERRS_NULL
}
bool nix_has_attr_byname(nix_c_context *context, const Value *value,
State *state, const char *name) {
bool nix_has_attr_byname(nix_c_context * context, const Value * value, State * state, const char * name)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -261,8 +272,9 @@ bool nix_has_attr_byname(nix_c_context *context, const Value *value,
NIXC_CATCH_ERRS_RES(false);
}
Value *nix_get_attr_byidx(nix_c_context *context, const Value *value,
State *state, unsigned int i, const char **name) {
Value *
nix_get_attr_byidx(nix_c_context * context, const Value * value, State * state, unsigned int i, const char ** name)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -276,8 +288,8 @@ Value *nix_get_attr_byidx(nix_c_context *context, const Value *value,
NIXC_CATCH_ERRS_NULL
}
const char *nix_get_attr_name_byidx(nix_c_context *context, const Value *value,
State *state, unsigned int i) {
const char * nix_get_attr_name_byidx(nix_c_context * context, const Value * value, State * state, unsigned int i)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -288,7 +300,8 @@ const char *nix_get_attr_name_byidx(nix_c_context *context, const Value *value,
NIXC_CATCH_ERRS_NULL
}
nix_err nix_set_bool(nix_c_context *context, Value *value, bool b) {
nix_err nix_set_bool(nix_c_context * context, Value * value, bool b)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -299,7 +312,8 @@ nix_err nix_set_bool(nix_c_context *context, Value *value, bool b) {
}
// todo string context
nix_err nix_set_string(nix_c_context *context, Value *value, const char *str) {
nix_err nix_set_string(nix_c_context * context, Value * value, const char * str)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -309,8 +323,8 @@ nix_err nix_set_string(nix_c_context *context, Value *value, const char *str) {
NIXC_CATCH_ERRS
}
nix_err nix_set_path_string(nix_c_context *context, Value *value,
const char *str) {
nix_err nix_set_path_string(nix_c_context * context, Value * value, const char * str)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -320,7 +334,8 @@ nix_err nix_set_path_string(nix_c_context *context, Value *value,
NIXC_CATCH_ERRS
}
nix_err nix_set_float(nix_c_context *context, Value *value, double d) {
nix_err nix_set_float(nix_c_context * context, Value * value, double d)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -330,7 +345,8 @@ nix_err nix_set_float(nix_c_context *context, Value *value, double d) {
NIXC_CATCH_ERRS
}
nix_err nix_set_int(nix_c_context *context, Value *value, int64_t i) {
nix_err nix_set_int(nix_c_context * context, Value * value, int64_t i)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -340,7 +356,8 @@ nix_err nix_set_int(nix_c_context *context, Value *value, int64_t i) {
NIXC_CATCH_ERRS
}
nix_err nix_set_null(nix_c_context *context, Value *value) {
nix_err nix_set_null(nix_c_context * context, Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -350,8 +367,8 @@ nix_err nix_set_null(nix_c_context *context, Value *value) {
NIXC_CATCH_ERRS
}
nix_err nix_set_external(nix_c_context *context, Value *value,
ExternalValue *val) {
nix_err nix_set_external(nix_c_context * context, Value * value, ExternalValue * val)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -362,8 +379,8 @@ nix_err nix_set_external(nix_c_context *context, Value *value,
NIXC_CATCH_ERRS
}
nix_err nix_make_list(nix_c_context *context, State *s, Value *value,
unsigned int size) {
nix_err nix_make_list(nix_c_context * context, State * s, Value * value, unsigned int size)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -373,8 +390,8 @@ nix_err nix_make_list(nix_c_context *context, State *s, Value *value,
NIXC_CATCH_ERRS
}
nix_err nix_set_list_byidx(nix_c_context *context, Value *value,
unsigned int ix, Value *elem) {
nix_err nix_set_list_byidx(nix_c_context * context, Value * value, unsigned int ix, Value * elem)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -386,7 +403,8 @@ nix_err nix_set_list_byidx(nix_c_context *context, Value *value,
NIXC_CATCH_ERRS
}
nix_err nix_set_primop(nix_c_context *context, Value *value, PrimOp *p) {
nix_err nix_set_primop(nix_c_context * context, Value * value, PrimOp * p)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -396,7 +414,8 @@ nix_err nix_set_primop(nix_c_context *context, Value *value, PrimOp *p) {
NIXC_CATCH_ERRS
}
nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source) {
nix_err nix_copy_value(nix_c_context * context, Value * value, Value * source)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -407,8 +426,8 @@ nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source) {
NIXC_CATCH_ERRS
}
nix_err nix_make_attrs(nix_c_context *context, Value *value,
BindingsBuilder *b) {
nix_err nix_make_attrs(nix_c_context * context, Value * value, BindingsBuilder * b)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -418,8 +437,8 @@ nix_err nix_make_attrs(nix_c_context *context, Value *value,
NIXC_CATCH_ERRS
}
BindingsBuilder *nix_make_bindings_builder(nix_c_context *context, State *state,
size_t capacity) {
BindingsBuilder * nix_make_bindings_builder(nix_c_context * context, State * state, size_t capacity)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -433,8 +452,8 @@ BindingsBuilder *nix_make_bindings_builder(nix_c_context *context, State *state,
NIXC_CATCH_ERRS_NULL
}
nix_err nix_bindings_builder_insert(nix_c_context *context, BindingsBuilder *b,
const char *name, Value *value) {
nix_err nix_bindings_builder_insert(nix_c_context * context, BindingsBuilder * b, const char * name, Value * value)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -445,7 +464,8 @@ nix_err nix_bindings_builder_insert(nix_c_context *context, BindingsBuilder *b,
NIXC_CATCH_ERRS
}
void nix_bindings_builder_free(BindingsBuilder *bb) {
void nix_bindings_builder_free(BindingsBuilder * bb)
{
#if HAVE_BOEHMGC
GC_FREE((nix::BindingsBuilder *) bb);
#else

View file

@ -67,7 +67,8 @@ typedef struct ExternalValue ExternalValue;
/** @brief Function pointer for primops
* @param[in] state Evaluator state
* @param[in] pos Call position, opaque index into the state's position table.
* @param[in] args list of arguments. Note that these can be thunks and should be forced using nix_value_force before use.
* @param[in] args list of arguments. Note that these can be thunks and should be forced using nix_value_force before
* use.
* @param[out] ret return value
* @see nix_alloc_primop, nix_set_primop
*/
@ -87,8 +88,8 @@ typedef void (*PrimOpFun)(State *state, int pos, Value **args, Value *ret);
* @return primop, or null in case of errors
* @see nix_set_primop
*/
PrimOp *nix_alloc_primop(nix_c_context *context, PrimOpFun fun, int arity,
const char *name, const char **args, const char *doc);
PrimOp * nix_alloc_primop(
nix_c_context * context, PrimOpFun fun, int arity, const char * name, const char ** args, const char * doc);
/** @brief add a primop to the `builtins` attribute set
*
@ -197,8 +198,7 @@ ExternalValue *nix_get_external(nix_c_context *context, Value *);
* @param[in] ix list element to get
* @return value, NULL in case of errors
*/
Value *nix_get_list_byidx(nix_c_context *context, const Value *value,
State *state, unsigned int ix);
Value * nix_get_list_byidx(nix_c_context * context, const Value * value, State * state, unsigned int ix);
/** @brief Get an attr by name
*
* Owned by the GC. Use nix_gc_decref when you're done with the pointer
@ -208,8 +208,7 @@ Value *nix_get_list_byidx(nix_c_context *context, const Value *value,
* @param[in] name attribute name
* @return value, NULL in case of errors
*/
Value *nix_get_attr_byname(nix_c_context *context, const Value *value,
State *state, const char *name);
Value * nix_get_attr_byname(nix_c_context * context, const Value * value, State * state, const char * name);
/** @brief Check if an attribute name exists on a value
* @param[out] context Optional, stores error information
@ -218,8 +217,7 @@ Value *nix_get_attr_byname(nix_c_context *context, const Value *value,
* @param[in] name attribute name
* @return value, error info via context
*/
bool nix_has_attr_byname(nix_c_context *context, const Value *value,
State *state, const char *name);
bool nix_has_attr_byname(nix_c_context * context, const Value * value, State * state, const char * name);
/** @brief Get an attribute by index in the sorted bindings
*
@ -233,8 +231,8 @@ bool nix_has_attr_byname(nix_c_context *context, const Value *value,
* @param[out] name will store a pointer to the attribute name
* @return value, NULL in case of errors
*/
Value *nix_get_attr_byidx(nix_c_context *context, const Value *value,
State *state, unsigned int i, const char **name);
Value *
nix_get_attr_byidx(nix_c_context * context, const Value * value, State * state, unsigned int i, const char ** name);
/** @brief Get an attribute name by index in the sorted bindings
*
@ -247,8 +245,7 @@ Value *nix_get_attr_byidx(nix_c_context *context, const Value *value,
* @param[in] i attribute index
* @return name, NULL in case of errors
*/
const char *nix_get_attr_name_byidx(nix_c_context *context, const Value *value,
State *state, unsigned int i);
const char * nix_get_attr_name_byidx(nix_c_context * context, const Value * value, State * state, unsigned int i);
/**@}*/
/** @name Setters
*/
@ -273,8 +270,7 @@ nix_err nix_set_string(nix_c_context *context, Value *value, const char *str);
* @param[in] str the path string, copied
* @return error code, NIX_OK on success.
*/
nix_err nix_set_path_string(nix_c_context *context, Value *value,
const char *str);
nix_err nix_set_path_string(nix_c_context * context, Value * value, const char * str);
/** @brief Set a float
* @param[out] context Optional, stores error information
* @param[out] value Nix value to modify
@ -301,16 +297,14 @@ nix_err nix_set_null(nix_c_context *context, Value *value);
* @param[in] val the external value to set. Will be GC-referenced by the value.
* @return error code, NIX_OK on success.
*/
nix_err nix_set_external(nix_c_context *context, Value *value,
ExternalValue *val);
nix_err nix_set_external(nix_c_context * context, Value * value, ExternalValue * val);
/** @brief Allocate a list
* @param[out] context Optional, stores error information
* @param[out] value Nix value to modify
* @param[in] size size of list
* @return error code, NIX_OK on success.
*/
nix_err nix_make_list(nix_c_context *context, State *s, Value *value,
unsigned int size);
nix_err nix_make_list(nix_c_context * context, State * s, Value * value, unsigned int size);
/** @brief Manipulate a list by index
*
* Don't do this mid-computation.
@ -321,16 +315,14 @@ nix_err nix_make_list(nix_c_context *context, State *s, Value *value,
* @param[in] elem the value to set, will be gc-referenced by the value
* @return error code, NIX_OK on success.
*/
nix_err nix_set_list_byidx(nix_c_context *context, Value *value,
unsigned int ix, Value *elem);
nix_err nix_set_list_byidx(nix_c_context * context, Value * value, unsigned int ix, Value * elem);
/** @brief Create an attribute set from a bindings builder
* @param[out] context Optional, stores error information
* @param[out] value Nix value to modify
* @param[in] b bindings builder to use. Make sure to unref this afterwards.
* @return error code, NIX_OK on success.
*/
nix_err nix_make_attrs(nix_c_context *context, Value *value,
BindingsBuilder *b);
nix_err nix_make_attrs(nix_c_context * context, Value * value, BindingsBuilder * b);
/** @brief Set primop
* @param[out] context Optional, stores error information
* @param[out] value Nix value to modify
@ -356,8 +348,7 @@ nix_err nix_copy_value(nix_c_context *context, Value *value, Value *source);
* @return owned reference to a bindings builder. Make sure to unref when you're
done.
*/
BindingsBuilder *nix_make_bindings_builder(nix_c_context *context, State *state,
size_t capacity);
BindingsBuilder * nix_make_bindings_builder(nix_c_context * context, State * state, size_t capacity);
/** @brief Insert bindings into a builder
* @param[out] context Optional, stores error information
* @param[in] builder BindingsBuilder to insert into
@ -365,9 +356,8 @@ BindingsBuilder *nix_make_bindings_builder(nix_c_context *context, State *state,
* @param[in] value value to give the binding
* @return error code, NIX_OK on success.
*/
nix_err nix_bindings_builder_insert(nix_c_context *context,
BindingsBuilder *builder, const char *name,
Value *value);
nix_err
nix_bindings_builder_insert(nix_c_context * context, BindingsBuilder * builder, const char * name, Value * value);
/** @brief Free a bindings builder
*
* Does not fail.

View file

@ -7,11 +7,13 @@
#include "globals.hh"
struct StorePath {
struct StorePath
{
nix::StorePath path;
};
nix_err nix_libstore_init(nix_c_context *context) {
nix_err nix_libstore_init(nix_c_context * context)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -20,7 +22,8 @@ nix_err nix_libstore_init(nix_c_context *context) {
NIXC_CATCH_ERRS
}
nix_err nix_init_plugins(nix_c_context *context) {
nix_err nix_init_plugins(nix_c_context * context)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -29,8 +32,8 @@ nix_err nix_init_plugins(nix_c_context *context) {
NIXC_CATCH_ERRS
}
Store *nix_store_open(nix_c_context *context, const char *uri,
const char ***params) {
Store * nix_store_open(nix_c_context * context, const char * uri, const char *** params)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -51,10 +54,13 @@ Store *nix_store_open(nix_c_context *context, const char *uri,
NIXC_CATCH_ERRS_NULL
}
void nix_store_unref(Store *store) { delete store; }
void nix_store_unref(Store * store)
{
delete store;
}
nix_err nix_store_get_uri(nix_c_context *context, Store *store, char *dest,
unsigned int n) {
nix_err nix_store_get_uri(nix_c_context * context, Store * store, char * dest, unsigned int n)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -64,8 +70,8 @@ nix_err nix_store_get_uri(nix_c_context *context, Store *store, char *dest,
NIXC_CATCH_ERRS
}
nix_err nix_store_get_version(nix_c_context *context, Store *store, char *dest,
unsigned int n) {
nix_err nix_store_get_version(nix_c_context * context, Store * store, char * dest, unsigned int n)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -73,15 +79,14 @@ nix_err nix_store_get_version(nix_c_context *context, Store *store, char *dest,
if (res) {
return nix_export_std_string(context, *res, dest, n);
} else {
return nix_set_err_msg(context, NIX_ERR_UNKNOWN,
"store does not have a version");
return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "store does not have a version");
}
}
NIXC_CATCH_ERRS
}
bool nix_store_is_valid_path(nix_c_context *context, Store *store,
StorePath *path) {
bool nix_store_is_valid_path(nix_c_context * context, Store * store, StorePath * path)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -90,8 +95,8 @@ bool nix_store_is_valid_path(nix_c_context *context, Store *store,
NIXC_CATCH_ERRS_RES(false);
}
StorePath *nix_store_parse_path(nix_c_context *context, Store *store,
const char *path) {
StorePath * nix_store_parse_path(nix_c_context * context, Store * store, const char * path)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -101,10 +106,13 @@ StorePath *nix_store_parse_path(nix_c_context *context, Store *store,
NIXC_CATCH_ERRS_NULL
}
nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path,
nix_err nix_store_build(
nix_c_context * context,
Store * store,
StorePath * path,
void * userdata,
void (*callback)(void *userdata, const char *,
const char *)) {
void (*callback)(void * userdata, const char *, const char *))
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -115,8 +123,7 @@ nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path,
},
});
if (callback) {
for (auto &[outputName, outputPath] :
store->ptr->queryDerivationOutputMap(path->path)) {
for (auto & [outputName, outputPath] : store->ptr->queryDerivationOutputMap(path->path)) {
auto op = store->ptr->printStorePath(outputPath);
callback(userdata, outputName.c_str(), op.c_str());
}
@ -125,4 +132,7 @@ nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path,
NIXC_CATCH_ERRS
}
void nix_store_path_free(StorePath *sp) { delete sp; }
void nix_store_path_free(StorePath * sp)
{
delete sp;
}

View file

@ -74,8 +74,7 @@ void nix_store_unref(Store *store);
* @param[in] n Maximum size of the returned string.
* @return error code, NIX_OK on success.
*/
nix_err nix_store_get_uri(nix_c_context *context, Store *store, char *dest,
unsigned int n);
nix_err nix_store_get_uri(nix_c_context * context, Store * store, char * dest, unsigned int n);
// returns: owned StorePath*
/**
@ -87,8 +86,7 @@ nix_err nix_store_get_uri(nix_c_context *context, Store *store, char *dest,
* @param[in] path Path string to parse, copied
* @return owned store path, NULL on error
*/
StorePath *nix_store_parse_path(nix_c_context *context, Store *store,
const char *path);
StorePath * nix_store_parse_path(nix_c_context * context, Store * store, const char * path);
/** @brief Deallocate a StorePath
*
@ -98,14 +96,14 @@ StorePath *nix_store_parse_path(nix_c_context *context, Store *store,
void nix_store_path_free(StorePath * p);
/**
* @brief Check if a StorePath is valid (i.e. that corresponding store object and its closure of references exists in the store)
* @brief Check if a StorePath is valid (i.e. that corresponding store object and its closure of references exists in
* the store)
* @param[out] context Optional, stores error information
* @param[in] store Nix Store reference
* @param[in] path Path to check
* @return true or false, error info in context
*/
bool nix_store_is_valid_path(nix_c_context *context, Store *store,
StorePath *path);
bool nix_store_is_valid_path(nix_c_context * context, Store * store, StorePath * path);
// nix_err nix_store_ensure(Store*, const char*);
// nix_err nix_store_build_paths(Store*);
/**
@ -119,10 +117,12 @@ bool nix_store_is_valid_path(nix_c_context *context, Store *store,
* @param[in] userdata data to pass to every callback invocation
* @param[in] callback called for every realised output
*/
nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path,
nix_err nix_store_build(
nix_c_context * context,
Store * store,
StorePath * path,
void * userdata,
void (*callback)(void *userdata, const char *outname,
const char *out));
void (*callback)(void * userdata, const char * outname, const char * out));
/**
* @brief get the version of a nix store
@ -132,8 +132,7 @@ nix_err nix_store_build(nix_c_context *context, Store *store, StorePath *path,
* @param[in] n Maximum size of the returned string.
* @return error code, NIX_OK on success.
*/
nix_err nix_store_get_version(nix_c_context *, Store *store, char *dest,
unsigned int n);
nix_err nix_store_get_version(nix_c_context *, Store * store, char * dest, unsigned int n);
// cffi end
#ifdef __cplusplus

View file

@ -2,7 +2,8 @@
#define NIX_API_STORE_INTERNAL_H
#include "store-api.hh"
struct Store {
struct Store
{
nix::ref<nix::Store> ptr;
};
#endif

View file

@ -7,11 +7,18 @@
#include <cxxabi.h>
#include <typeinfo>
nix_c_context *nix_c_context_create() { return new nix_c_context(); }
nix_c_context * nix_c_context_create()
{
return new nix_c_context();
}
void nix_c_context_free(nix_c_context *context) { delete context; }
void nix_c_context_free(nix_c_context * context)
{
delete context;
}
nix_err nix_context_error(nix_c_context *context) {
nix_err nix_context_error(nix_c_context * context)
{
if (context == nullptr) {
throw;
}
@ -22,8 +29,7 @@ nix_err nix_context_error(nix_c_context *context) {
context->last_err = e.what();
context->info = e.info();
int status;
const char *demangled =
abi::__cxa_demangle(typeid(e).name(), 0, 0, &status);
const char * demangled = abi::__cxa_demangle(typeid(e).name(), 0, 0, &status);
if (demangled) {
context->name = demangled;
// todo: free(demangled);
@ -40,7 +46,8 @@ nix_err nix_context_error(nix_c_context *context) {
// unreachable
}
nix_err nix_set_err_msg(nix_c_context *context, nix_err err, const char *msg) {
nix_err nix_set_err_msg(nix_c_context * context, nix_err err, const char * msg)
{
if (context == nullptr) {
// todo last_err_code
throw nix::Error("Nix C api error: %s", msg);
@ -50,11 +57,14 @@ nix_err nix_set_err_msg(nix_c_context *context, nix_err err, const char *msg) {
return err;
}
const char *nix_version_get() { return PACKAGE_VERSION; }
const char * nix_version_get()
{
return PACKAGE_VERSION;
}
// Implementations
nix_err nix_setting_get(nix_c_context *context, const char *key, char *value,
int n) {
nix_err nix_setting_get(nix_c_context * context, const char * key, char * value, int n)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -69,8 +79,8 @@ nix_err nix_setting_get(nix_c_context *context, const char *key, char *value,
NIXC_CATCH_ERRS
}
nix_err nix_setting_set(nix_c_context *context, const char *key,
const char *value) {
nix_err nix_setting_set(nix_c_context * context, const char * key, const char * value)
{
if (context)
context->last_err_code = NIX_OK;
if (nix::globalConfig.set(key, value))
@ -80,7 +90,8 @@ nix_err nix_setting_set(nix_c_context *context, const char *key,
}
}
nix_err nix_libutil_init(nix_c_context *context) {
nix_err nix_libutil_init(nix_c_context * context)
{
if (context)
context->last_err_code = NIX_OK;
try {
@ -90,8 +101,8 @@ nix_err nix_libutil_init(nix_c_context *context) {
NIXC_CATCH_ERRS
}
const char *nix_err_msg(nix_c_context *context,
const nix_c_context *read_context, unsigned int *n) {
const char * nix_err_msg(nix_c_context * context, const nix_c_context * read_context, unsigned int * n)
{
if (context)
context->last_err_code = NIX_OK;
if (read_context->last_err) {
@ -103,43 +114,38 @@ const char *nix_err_msg(nix_c_context *context,
return nullptr;
}
nix_err nix_err_name(nix_c_context *context, const nix_c_context *read_context,
char *value, int n) {
nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, char * value, int n)
{
if (context)
context->last_err_code = NIX_OK;
if (read_context->last_err_code != NIX_ERR_NIX_ERROR) {
return nix_set_err_msg(context, NIX_ERR_UNKNOWN,
"Last error was not a nix error");
return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Last error was not a nix error");
}
return nix_export_std_string(context, read_context->name, value, n);
}
nix_err nix_err_info_msg(nix_c_context *context,
const nix_c_context *read_context, char *value,
int n) {
nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, char * value, int n)
{
if (context)
context->last_err_code = NIX_OK;
if (read_context->last_err_code != NIX_ERR_NIX_ERROR) {
return nix_set_err_msg(context, NIX_ERR_UNKNOWN,
"Last error was not a nix error");
return nix_set_err_msg(context, NIX_ERR_UNKNOWN, "Last error was not a nix error");
}
return nix_export_std_string(context, read_context->info->msg.str(), value,
n);
return nix_export_std_string(context, read_context->info->msg.str(), value, n);
}
nix_err nix_err_code(const nix_c_context *read_context) {
nix_err nix_err_code(const nix_c_context * read_context)
{
return read_context->last_err_code;
}
// internal
nix_err nix_export_std_string(nix_c_context *context,
const std::string_view str, char *dest,
unsigned int n) {
nix_err nix_export_std_string(nix_c_context * context, const std::string_view str, char * dest, unsigned int n)
{
size_t i = str.copy(dest, n - 1);
dest[i] = 0;
if (i == n - 1) {
return nix_set_err_msg(context, NIX_ERR_OVERFLOW,
"Provided buffer too short");
return nix_set_err_msg(context, NIX_ERR_OVERFLOW, "Provided buffer too short");
} else
return NIX_OK;
}

View file

@ -167,8 +167,7 @@ nix_err nix_libutil_init(nix_c_context *context);
* provided buffer is too short, or NIX_OK if the setting was retrieved
* successfully.
*/
nix_err nix_setting_get(nix_c_context *context, const char *key, char *value,
int n);
nix_err nix_setting_get(nix_c_context * context, const char * key, char * value, int n);
/**
* @brief Sets a setting in the nix global configuration.
@ -184,8 +183,7 @@ nix_err nix_setting_get(nix_c_context *context, const char *key, char *value,
* @return NIX_ERR_KEY if the setting is unknown, or NIX_OK if the setting was
* set successfully.
*/
nix_err nix_setting_set(nix_c_context *context, const char *key,
const char *value);
nix_err nix_setting_set(nix_c_context * context, const char * key, const char * value);
/**
* @}
@ -217,8 +215,7 @@ const char *nix_version_get();
* @return nullptr if no error message was ever set,
* a borrowed pointer to the error message otherwise.
*/
const char *nix_err_msg(nix_c_context *context, const nix_c_context *ctx,
unsigned int *n);
const char * nix_err_msg(nix_c_context * context, const nix_c_context * ctx, unsigned int * n);
/**
* @brief Retrieves the error message from errorInfo in a context.
@ -235,8 +232,7 @@ const char *nix_err_msg(nix_c_context *context, const nix_c_context *ctx,
* @param[in] n Maximum size of the returned string.
* @return NIX_OK if there were no errors, an error code otherwise.
*/
nix_err nix_err_info_msg(nix_c_context *context,
const nix_c_context *read_context, char *value, int n);
nix_err nix_err_info_msg(nix_c_context * context, const nix_c_context * read_context, char * value, int n);
/**
* @brief Retrieves the error name from a context.
@ -253,8 +249,7 @@ nix_err nix_err_info_msg(nix_c_context *context,
* @param[in] n Maximum size of the returned string.
* @return NIX_OK if there were no errors, an error code otherwise.
*/
nix_err nix_err_name(nix_c_context *context, const nix_c_context *read_context,
char *value, int n);
nix_err nix_err_name(nix_c_context * context, const nix_c_context * read_context, char * value, int n);
/**
* @brief Retrieves the most recent error code from a nix_c_context

View file

@ -7,7 +7,8 @@
#include "error.hh"
#include "nix_api_util.h"
struct nix_c_context {
struct nix_c_context
{
nix_err last_err_code = NIX_OK;
std::optional<std::string> last_err = {};
std::optional<nix::ErrorInfo> info = {};
@ -40,18 +41,18 @@ nix_err nix_set_err_msg(nix_c_context *context, nix_err err, const char *msg);
* @return NIX_OK if there were no errors, NIX_ERR_OVERFLOW if the string length
* exceeds `n`.
*/
nix_err nix_export_std_string(nix_c_context *context,
const std::string_view str, char *dest,
unsigned int n);
nix_err nix_export_std_string(nix_c_context * context, const std::string_view str, char * dest, unsigned int n);
#define NIXC_CATCH_ERRS \
catch (...) { \
catch (...) \
{ \
return nix_context_error(context); \
} \
return NIX_OK;
#define NIXC_CATCH_ERRS_RES(def) \
catch (...) { \
catch (...) \
{ \
nix_context_error(context); \
return def; \
}