use nix build & meson & zlog
This commit is contained in:
parent
0123623742
commit
346768a144
60 changed files with 13739 additions and 0 deletions
371
src/lib/utils.c
Executable file
371
src/lib/utils.c
Executable file
|
|
@ -0,0 +1,371 @@
|
|||
#include <ctype.h>
|
||||
#include <errno.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#if defined(__GLIBC__) && (__GLIBC__ == 2 && __GLIBC_MINOR__ < 28)
|
||||
#include <pthread.h>
|
||||
#else
|
||||
#include <threads.h>
|
||||
#endif
|
||||
#include <sys/param.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#if defined(__linux__)
|
||||
#include <execinfo.h>
|
||||
#endif
|
||||
|
||||
#include "config.h"
|
||||
#include "controller.capnp.h"
|
||||
#include <capnp_c.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include "xce/errnox.h"
|
||||
#include "xce/types.h"
|
||||
|
||||
static capn_text chars_to_text_internal(const char* chars)
|
||||
{
|
||||
capn_text data = {
|
||||
.len = strlen(chars),
|
||||
.str = chars,
|
||||
.seg = NULL,
|
||||
};
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
capn_text chars_to_text(const char* chars)
|
||||
{
|
||||
if (chars == NULL) {
|
||||
chars = "";
|
||||
}
|
||||
|
||||
return chars_to_text_internal(chars);
|
||||
}
|
||||
|
||||
#define ARGV_INCR_DELTA (16)
|
||||
|
||||
#define FILL_ARGV(rc, argv, argc, maxargc, delta, s) \
|
||||
do { \
|
||||
(rc) = 0; \
|
||||
char* s1; \
|
||||
if (((argc) >= (maxargc))) { \
|
||||
char** p; \
|
||||
int nc, i; \
|
||||
nc = (maxargc) + (delta); \
|
||||
p = (char**)realloc((argv), nc * sizeof(char*)); \
|
||||
if (p == NULL) { \
|
||||
(rc) = -ENOMEM; \
|
||||
break; \
|
||||
} \
|
||||
for (i = (maxargc); i < nc; i++) { \
|
||||
p[i] = NULL; \
|
||||
} \
|
||||
(argv) = p; \
|
||||
(maxargc) = nc; \
|
||||
} \
|
||||
s1 = strdup(s); \
|
||||
if (s1 == NULL) { \
|
||||
(rc) = -ENOMEM; \
|
||||
break; \
|
||||
} \
|
||||
(argv)[argc] = s1; \
|
||||
(argc)++; \
|
||||
} while (0)
|
||||
|
||||
#define FREE_ARGV(argv, argc) \
|
||||
do { \
|
||||
int i; \
|
||||
if ((argv) == NULL) { \
|
||||
break; \
|
||||
} \
|
||||
for (i = 0; i < (argc); i++) { \
|
||||
if ((argv)[i] != NULL) { \
|
||||
free((argv)[i]); \
|
||||
(argv)[i] = NULL; \
|
||||
} \
|
||||
} \
|
||||
free(argv); \
|
||||
} while (0)
|
||||
|
||||
int parse_argv(char* input,
|
||||
char*** argv)
|
||||
{
|
||||
int max_argc = 0;
|
||||
char* arg = NULL;
|
||||
int i = 0;
|
||||
int in_quotes = 0;
|
||||
char quote_char = 0;
|
||||
int argc = 0;
|
||||
int rc = 0;
|
||||
|
||||
rc = 0;
|
||||
arg = (char*)malloc(strlen(input) + 1);
|
||||
if (arg == NULL) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
*argv = NULL;
|
||||
while (*input) {
|
||||
if (in_quotes) {
|
||||
if (*input == quote_char) {
|
||||
in_quotes = 0;
|
||||
} else if (*input == '\\') {
|
||||
input++;
|
||||
if (*input) {
|
||||
arg[i++] = *input;
|
||||
}
|
||||
} else {
|
||||
arg[i++] = *input;
|
||||
}
|
||||
} else {
|
||||
if (isspace(*input)) {
|
||||
if (i > 0) {
|
||||
arg[i] = '\0';
|
||||
FILL_ARGV(rc, *argv, argc, max_argc,
|
||||
ARGV_INCR_DELTA, arg);
|
||||
if (rc < 0) {
|
||||
FREE_ARGV(*argv, argc);
|
||||
free(arg);
|
||||
return rc;
|
||||
}
|
||||
i = 0;
|
||||
}
|
||||
} else if ((*input == '"') || (*input == '\'')) {
|
||||
in_quotes = 1;
|
||||
quote_char = *input;
|
||||
} else if (*input == '\\') {
|
||||
input++;
|
||||
if (*input) {
|
||||
arg[i++] = *input;
|
||||
}
|
||||
} else {
|
||||
arg[i++] = *input;
|
||||
}
|
||||
}
|
||||
input++;
|
||||
}
|
||||
|
||||
if (i > 0) {
|
||||
arg[i] = '\0';
|
||||
FILL_ARGV(rc, *argv, argc, max_argc,
|
||||
ARGV_INCR_DELTA, arg);
|
||||
if (rc < 0) {
|
||||
FREE_ARGV(*argv, argc);
|
||||
free(arg);
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
free(arg);
|
||||
return argc;
|
||||
}
|
||||
|
||||
void free_argv(char** argv, int argc)
|
||||
{
|
||||
FREE_ARGV(argv, argc);
|
||||
}
|
||||
|
||||
#if 0
|
||||
void freeup_kvs(int count,
|
||||
xce_kv_t* vals)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (vals == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
if (vals[i].value.which != XceFieldValue_data_svalue) {
|
||||
continue;
|
||||
}
|
||||
|
||||
FREEUP(vals[i].value.u.svalue);
|
||||
}
|
||||
|
||||
FREEUP(vals);
|
||||
}
|
||||
#endif
|
||||
|
||||
char* frame_symbol(int k)
|
||||
{
|
||||
#if defined(__linux__)
|
||||
void* array[k + 1];
|
||||
size_t size = backtrace(array, k + 1);
|
||||
char** symbols = backtrace_symbols(array, size);
|
||||
char* s = NULL;
|
||||
|
||||
if (symbols == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
s = strdup(symbols[MIN(size - 1, k)]);
|
||||
free(symbols);
|
||||
return s;
|
||||
#else
|
||||
return NULL;
|
||||
#endif
|
||||
}
|
||||
|
||||
int xce_vprintf(const char* fmt, va_list ap)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
/* FIXME: */
|
||||
return rc;
|
||||
}
|
||||
|
||||
static _Thread_local ectx_t ectx;
|
||||
|
||||
void set_error(char* file,
|
||||
char* func,
|
||||
int line,
|
||||
int code)
|
||||
{
|
||||
ectx.file = file;
|
||||
ectx.func = func;
|
||||
ectx.line = line;
|
||||
ectx.code = code;
|
||||
errno = code;
|
||||
}
|
||||
|
||||
ectx_t* get_ectx()
|
||||
{
|
||||
return &ectx;
|
||||
}
|
||||
|
||||
static char* xce_err_str[] = {
|
||||
"invalid job",
|
||||
"invalid worker",
|
||||
"duplicated jobs",
|
||||
"duplicated workers",
|
||||
"bad opcode",
|
||||
"unknown role",
|
||||
"unknown resource",
|
||||
"unknown queue",
|
||||
"unknown group",
|
||||
"internal error",
|
||||
NULL
|
||||
};
|
||||
|
||||
int xce_strerror_r(int errnum,
|
||||
char* buf,
|
||||
size_t bufsize)
|
||||
{
|
||||
if (errnum < XCE_ERRNO_BASE) {
|
||||
return strerror_r(errnum, buf, bufsize);
|
||||
}
|
||||
|
||||
if (errnum >= E_XCE_MAX) {
|
||||
strncpy(buf, "unknown error", bufsize);
|
||||
return -1;
|
||||
}
|
||||
|
||||
strncpy(buf, xce_err_str[errnum - XCE_ERRNO_BASE],
|
||||
bufsize);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int copy_int_list(int** dst,
|
||||
int* src,
|
||||
int count)
|
||||
{
|
||||
int i;
|
||||
|
||||
FREEUP(*dst);
|
||||
if (count == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
*dst = (int*)calloc(count, sizeof(int));
|
||||
if ((*dst) == NULL) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
(*dst)[i] = (int)src[i];
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
int copy_strlist(xce_strlist_t* dst,
|
||||
xce_strlist_t src)
|
||||
{
|
||||
if (!dst) {
|
||||
return -1;
|
||||
}
|
||||
for (int i = 0; i < dst->count; i++) {
|
||||
FREEUP(dst->data[i]);
|
||||
}
|
||||
FREEUP(dst->data);
|
||||
dst->count = src.count;
|
||||
if (dst->count == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
dst->data = (char**)calloc(dst->count, sizeof(char*));
|
||||
if (dst->data == NULL) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
for (int i = 0; i < src.count; i++) {
|
||||
if (src.data[i]) {
|
||||
dst->data[i] = strndup(src.data[i], strlen(src.data[i]));
|
||||
if (dst->data[i] == NULL) {
|
||||
return -ENOMEM;
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
void free_resval_with_idx_list(xce_val_with_idx_t*** vals,
|
||||
int count)
|
||||
{
|
||||
xce_val_with_idx_t** resvals = *vals;
|
||||
int i;
|
||||
|
||||
if (resvals == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (count <= 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = 0; i < count; i++) {
|
||||
xce_val_with_idx_t* val = resvals[i];
|
||||
|
||||
if (val == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (val->val->which == Value_u_svalue) {
|
||||
FREEUP(val->val->u.svalue);
|
||||
}
|
||||
|
||||
FREEUP(val->val);
|
||||
FREEUP(val);
|
||||
}
|
||||
|
||||
FREEUP(resvals);
|
||||
*vals = NULL;
|
||||
}
|
||||
|
||||
void free_strlist(xce_strlist_t *l) {
|
||||
int i;
|
||||
|
||||
if ( l == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((l->count <= 0) || (l->data == NULL)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for(i = 0; i < l->count; i ++) {
|
||||
FREEUP(l->data[i]);
|
||||
}
|
||||
|
||||
FREEUP(l->data);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue