diff --git a/common/include/villas/buffer.h b/common/include/villas/buffer.h index 4709c6151..2bf2e2b54 100644 --- a/common/include/villas/buffer.h +++ b/common/include/villas/buffer.h @@ -30,7 +30,7 @@ #include struct buffer { - enum state state; + enum State state; char *buf; size_t len; diff --git a/common/include/villas/common.h b/common/include/villas/common.h index 66a51ec17..7387b5950 100644 --- a/common/include/villas/common.h +++ b/common/include/villas/common.h @@ -24,25 +24,25 @@ #pragma once /* Common states for most objects in VILLAScommon (paths, nodes, hooks, plugins) */ -enum state { - STATE_DESTROYED = 0, - STATE_INITIALIZED = 1, - STATE_PARSED = 2, - STATE_CHECKED = 3, - STATE_STARTED = 4, - STATE_LOADED = 4, /* alias for STATE_STARTED used by struct plugin */ - STATE_OPENED = 4, /* alias for STATE_STARTED used by struct io */ - STATE_STOPPED = 5, - STATE_UNLOADED = 5, /* alias for STATE_STARTED used by struct plugin */ - STATE_CLOSED = 5, /* alias for STATE_STARTED used by struct io */ - STATE_PENDING_CONNECT = 6, - STATE_CONNECTED = 7, - STATE_PAUSED = 8, - STATE_STARTING = 9, - STATE_STOPPING = 10, - STATE_PAUSING = 11, - STATE_RESUMING = 12, - STATE_PREPARED = 13 +enum class State { + DESTROYED = 0, + INITIALIZED = 1, + PARSED = 2, + CHECKED = 3, + STARTED = 4, + LOADED = 4, /* alias for STARTED used by struct plugin */ + OPENED = 4, /* alias for STARTED used by struct io */ + STOPPED = 5, + UNLOADED = 5, /* alias for STARTED used by struct plugin */ + CLOSED = 5, /* alias for STARTED used by struct io */ + PENDING_CONNECT = 6, + CONNECTED = 7, + PAUSED = 8, + STARTING = 9, + STOPPING = 10, + PAUSING = 11, + RESUMING = 12, + PREPARED = 13 }; /** Callback to destroy list elements. @@ -52,4 +52,4 @@ enum state { typedef int (*dtor_cb_t)(void *); /** Convert state enum to human readable string. */ -const char * state_print(enum state s); +const char * state_print(enum State s); diff --git a/common/include/villas/list.h b/common/include/villas/list.h index 45b7a234f..a92fbcc12 100644 --- a/common/include/villas/list.h +++ b/common/include/villas/list.h @@ -39,7 +39,7 @@ /** Static list initialization */ #define LIST_INIT_STATIC(l) \ __attribute__((constructor(105))) static void UNIQUE(__ctor)() {\ - if ((l)->state == STATE_DESTROYED) \ + if ((l)->state == State::DESTROYED) \ vlist_init(l); \ } \ __attribute__((destructor(105))) static void UNIQUE(__dtor)() { \ @@ -58,7 +58,7 @@ typedef int (*cmp_cb_t)(const void *, const void *); /* The list data structure. */ struct vlist { - enum state state; /**< The state of this list. */ + enum State state; /**< The state of this list. */ void **array; /**< Array of pointers to list elements */ size_t capacity; /**< Size of list::array in elements */ size_t length; /**< Number of elements of list::array which are in use */ diff --git a/common/include/villas/table.hpp b/common/include/villas/table.hpp index 90c9a5d19..53a6f5019 100644 --- a/common/include/villas/table.hpp +++ b/common/include/villas/table.hpp @@ -37,7 +37,7 @@ class TableColumn { friend Table; public: - enum align { + enum class Alignment { LEFT, RIGHT }; @@ -48,20 +48,20 @@ protected: int width; /**< Width of the column. */ public: - TableColumn(int w, enum align a, const std::string &t, const std::string &f, const std::string &u = "") : + TableColumn(int w, enum Alignment a, const std::string &t, const std::string &f, const std::string &u = "") : width(w), title(t), format(f), unit(u), align(a) { } - + std::string title; /**< The title as shown in the table header. */ std::string format; /**< The format which is used to print the table rows. */ std::string unit; /**< An optional unit which will be shown in the table header. */ - enum align align; - + enum Alignment align; + int getWidth() const { return _width; @@ -74,9 +74,9 @@ protected: int resize(int w); int width; - + std::vector columns; - + public: Table(const std::vector &cols) : width(-1), diff --git a/common/lib/buffer.cpp b/common/lib/buffer.cpp index 486555d90..bf5fc02ea 100644 --- a/common/lib/buffer.cpp +++ b/common/lib/buffer.cpp @@ -34,7 +34,7 @@ int buffer_init(struct buffer *b, size_t size) if (!b->buf) return -1; - b->state = STATE_INITIALIZED; + b->state = State::INITIALIZED; return 0; } @@ -44,7 +44,7 @@ int buffer_destroy(struct buffer *b) if (b->buf) free(b->buf); - b->state = STATE_DESTROYED; + b->state = State::DESTROYED; return 0; } diff --git a/common/lib/common.cpp b/common/lib/common.cpp index e5f527e0a..584d21a72 100644 --- a/common/lib/common.cpp +++ b/common/lib/common.cpp @@ -25,42 +25,42 @@ #include -const char * state_print(enum state s) +const char * state_print(enum State s) { switch (s) { - case STATE_DESTROYED: + case State::DESTROYED: return "destroyed"; break; - case STATE_INITIALIZED: + case State::INITIALIZED: return "initialized"; break; - case STATE_PARSED: + case State::PARSED: return "parsed"; break; - case STATE_CHECKED: + case State::CHECKED: return "checked"; break; - case STATE_STARTED: + case State::STARTED: return "running"; break; - case STATE_STOPPED: + case State::STOPPED: return "stopped"; break; - case STATE_PENDING_CONNECT: + case State::PENDING_CONNECT: return "pending-connect"; break; - case STATE_CONNECTED: + case State::CONNECTED: return "connected"; break; - case STATE_PAUSED: + case State::PAUSED: return "paused"; break; diff --git a/common/lib/hist.cpp b/common/lib/hist.cpp index fc710e7ab..02990a61a 100644 --- a/common/lib/hist.cpp +++ b/common/lib/hist.cpp @@ -165,9 +165,9 @@ void Hist::plot() const } std::vector cols = { - { -9, TableColumn::align::RIGHT, "Value", "%+9.3g" }, - { -6, TableColumn::align::RIGHT, "Count", "%6ju" }, - { 0, TableColumn::align::LEFT, "Plot", "%s", "occurences" } + { -9, TableColumn::Alignment::RIGHT, "Value", "%+9.3g" }, + { -6, TableColumn::Alignment::RIGHT, "Count", "%6ju" }, + { 0, TableColumn::Alignment::LEFT, "Plot", "%s", "occurences" } }; Table table = Table(cols); diff --git a/common/lib/list.cpp b/common/lib/list.cpp index 32cab1e44..c9a23145f 100644 --- a/common/lib/list.cpp +++ b/common/lib/list.cpp @@ -55,14 +55,14 @@ static int cmp_sort(const void *a, const void *b, void *thunk) { int vlist_init(struct vlist *l) { - assert(l->state == STATE_DESTROYED); + assert(l->state == State::DESTROYED); pthread_mutex_init(&l->lock, nullptr); l->length = 0; l->capacity = 0; l->array = nullptr; - l->state = STATE_INITIALIZED; + l->state = State::INITIALIZED; return 0; } @@ -71,7 +71,7 @@ int vlist_destroy(struct vlist *l, dtor_cb_t destructor, bool release) { pthread_mutex_lock(&l->lock); - assert(l->state != STATE_DESTROYED); + assert(l->state != State::DESTROYED); for (size_t i = 0; i < vlist_length(l); i++) { void *e = vlist_at(l, i); @@ -87,7 +87,7 @@ int vlist_destroy(struct vlist *l, dtor_cb_t destructor, bool release) l->length = -1; l->capacity = 0; l->array = nullptr; - l->state = STATE_DESTROYED; + l->state = State::DESTROYED; pthread_mutex_unlock(&l->lock); pthread_mutex_destroy(&l->lock); @@ -99,7 +99,7 @@ void vlist_push(struct vlist *l, void *p) { pthread_mutex_lock(&l->lock); - assert(l->state == STATE_INITIALIZED); + assert(l->state == State::INITIALIZED); /* Resize array if out of capacity */ if (l->length >= l->capacity) { @@ -117,7 +117,7 @@ int vlist_remove(struct vlist *l, size_t idx) { pthread_mutex_lock(&l->lock); - assert(l->state == STATE_INITIALIZED); + assert(l->state == State::INITIALIZED); if (idx >= l->length) return -1; @@ -139,7 +139,7 @@ int vlist_insert(struct vlist *l, size_t idx, void *p) pthread_mutex_lock(&l->lock); - assert(l->state == STATE_INITIALIZED); + assert(l->state == State::INITIALIZED); if (idx >= l->length) return -1; @@ -171,7 +171,7 @@ void vlist_remove_all(struct vlist *l, void *p) pthread_mutex_lock(&l->lock); - assert(l->state == STATE_INITIALIZED); + assert(l->state == State::INITIALIZED); for (size_t i = 0; i < vlist_length(l); i++) { if (vlist_at(l, i) == p) @@ -211,7 +211,7 @@ int vlist_count(struct vlist *l, cmp_cb_t cmp, void *ctx) pthread_mutex_lock(&l->lock); - assert(l->state == STATE_INITIALIZED); + assert(l->state == State::INITIALIZED); for (size_t i = 0; i < vlist_length(l); i++) { e = vlist_at(l, i); @@ -230,7 +230,7 @@ void * vlist_search(struct vlist *l, cmp_cb_t cmp, void *ctx) pthread_mutex_lock(&l->lock); - assert(l->state == STATE_INITIALIZED); + assert(l->state == State::INITIALIZED); for (size_t i = 0; i < vlist_length(l); i++) { e = vlist_at(l, i); @@ -249,7 +249,7 @@ void vlist_sort(struct vlist *l, cmp_cb_t cmp) { pthread_mutex_lock(&l->lock); - assert(l->state == STATE_INITIALIZED); + assert(l->state == State::INITIALIZED); #ifdef __APPLE__ qsort_r(l->array, l->length, sizeof(void *), (void *) cmp, cmp_sort); @@ -277,7 +277,7 @@ ssize_t vlist_index(struct vlist *l, void *p) pthread_mutex_lock(&l->lock); - assert(l->state == STATE_INITIALIZED); + assert(l->state == State::INITIALIZED); for (size_t i = 0; i < vlist_length(l); i++) { e = vlist_at(l, i); diff --git a/common/lib/plugin.cpp b/common/lib/plugin.cpp index 75bbc0d40..99dd3fb7c 100644 --- a/common/lib/plugin.cpp +++ b/common/lib/plugin.cpp @@ -55,7 +55,7 @@ Plugin::parse(json_t *cfg) return -1; this->path = std::string(path); - this->state = STATE_PARSED; + this->state = State::PARSED; return 0; } @@ -63,7 +63,7 @@ Plugin::parse(json_t *cfg) int Plugin::load() { - assert(this->state == STATE_PARSED); + assert(this->state == State::PARSED); assert(not this->path.empty()); this->handle = dlopen(this->path.c_str(), RTLD_NOW); @@ -71,7 +71,7 @@ Plugin::load() if (this->handle == nullptr) return -1; - this->state = STATE_LOADED; + this->state = State::LOADED; return 0; } @@ -81,13 +81,13 @@ Plugin::unload() { int ret; - assert(this->state == STATE_LOADED); + assert(this->state == State::LOADED); ret = dlclose(this->handle); if (ret != 0) return -1; - this->state = STATE_UNLOADED; + this->state = State::UNLOADED; return 0; } diff --git a/common/lib/popen.cpp b/common/lib/popen.cpp index d2811f9ad..68dbae31b 100644 --- a/common/lib/popen.cpp +++ b/common/lib/popen.cpp @@ -141,8 +141,13 @@ void Popen::open() } /* Change working directory */ - if (!working_dir.empty()) - chdir(working_dir.c_str()); + if (!working_dir.empty()) { + int ret; + + ret = chdir(working_dir.c_str()); + if (ret) + exit(127); + } execvpe(shell ? _PATH_BSHELL : command.c_str(), (char * const *) argv.data(), (char * const *) envp.data()); exit(127); diff --git a/common/lib/table.cpp b/common/lib/table.cpp index bfd4d44b6..db3db233e 100644 --- a/common/lib/table.cpp +++ b/common/lib/table.cpp @@ -83,7 +83,7 @@ void Table::header() w = columns[i]._width + strlen(col) - strlenp(col); u = columns[i]._width + strlen(unit) - strlenp(unit); - if (columns[i].align == TableColumn::align::LEFT) { + if (columns[i].align == TableColumn::Alignment::LEFT) { strcatf(&line1, " %-*.*s\e[0m", w, w, col); strcatf(&line2, " %-*.*s\e[0m", u, u, unit); } @@ -133,7 +133,7 @@ void Table::row(int count, ...) int r = strlen(col); int w = columns[i]._width + r - l; - if (columns[i].align == TableColumn::align::LEFT) + if (columns[i].align == TableColumn::Alignment::LEFT) strcatf(&line, " %-*.*s\e[0m ", w, w, col); else strcatf(&line, " %*.*s\e[0m ", w, w, col); diff --git a/common/tests/unit/list.cpp b/common/tests/unit/list.cpp index 8963e316a..2cd865df8 100644 --- a/common/tests/unit/list.cpp +++ b/common/tests/unit/list.cpp @@ -39,7 +39,7 @@ TestSuite(list, .description = "List datastructure"); Test(list, vlist_lookup) { struct vlist l; - l.state = STATE_DESTROYED; + l.state = State::DESTROYED; vlist_init(&l); @@ -62,7 +62,7 @@ Test(list, vlist_lookup) Test(list, vlist_search) { struct vlist l; - l.state = STATE_DESTROYED; + l.state = State::DESTROYED; vlist_init(&l); @@ -103,7 +103,7 @@ static int dtor(void *ptr) Test(list, destructor) { struct vlist l; - l.state = STATE_DESTROYED; + l.state = State::DESTROYED; struct content elm; elm.destroyed = 0; @@ -123,7 +123,7 @@ Test(list, basics) uintptr_t i; int ret; struct vlist l; - l.state = STATE_DESTROYED; + l.state = State::DESTROYED; vlist_init(&l);