static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out) { struct instance *o = vo; if (!strcmp(name, "succeeded")) { *out = ncd_make_boolean(mem, o->succeeded, o->i->params->iparams->string_index); return 1; } size_t pos; uintmax_t n; if ((pos = string_begins_with(name, "match")) && parse_unsigned_integer(MemRef_MakeCstr(name + pos), &n)) { if (o->succeeded && n < MAX_MATCHES && o->matches[n].rm_so >= 0) { regmatch_t *m = &o->matches[n]; ASSERT(m->rm_so <= o->input.len) ASSERT(m->rm_eo >= m->rm_so) ASSERT(m->rm_eo <= o->input.len) size_t len = m->rm_eo - m->rm_so; *out = NCDVal_NewStringBinMr(mem, MemRef_Sub(o->input, m->rm_so, len)); return 1; } } return 0; }
static int ipv6_cidr_addr_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out) { struct ipv6_cidr_instance *o = vo; if (name == NCD_STRING_SUCCEEDED) { *out = ncd_make_boolean(mem, o->succeeded, o->i->params->iparams->string_index); return 1; } if (!o->succeeded) { return 0; } char str[IPADDR6_PRINT_MAX]; if (name == NCD_STRING_EMPTY) { ipaddr6_print_ifaddr(o->ifaddr, str); } else if (name == ModuleString(o->i, STRING_ADDR)) { ipaddr6_print_addr(o->ifaddr.addr, str); } else if (name == ModuleString(o->i, STRING_PREFIX)) { sprintf(str, "%d", o->ifaddr.prefix); } else { return 0; } *out = NCDVal_NewString(mem, str); return 1; }
static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out) { struct instance *o = vo; if (!strcmp(name, "")) { *out = ncd_make_boolean(mem, o->value, o->i->params->iparams->string_index); return 1; } return 0; }
static int boolean_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out) { struct boolean_instance *o = vo; if (name == NCD_STRING_EMPTY) { *out = ncd_make_boolean(mem, o->value, o->i->params->iparams->string_index); return 1; } return 0; }
static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out) { struct instance *o = vo; ASSERT(o->state == STATE_EXIST || o->state == STATE_NOEXIST) if (!strcmp(name, "exists")) { *out = ncd_make_boolean(mem, o->state == STATE_EXIST, o->i->params->iparams->string_index); return 1; } return 0; }
static int stat_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out) { struct stat_instance *o = vo; if (name == NCD_STRING_SUCCEEDED) { *out = ncd_make_boolean(mem, o->succeeded, o->i->params->iparams->string_index); return 1; } if (name == NCD_STRING_TYPE) { const char *str; if (!o->succeeded) { str = "failed"; } else if (S_ISREG(o->result.st_mode)) { str = "file"; } else if (S_ISDIR(o->result.st_mode)) { str = "dir"; } else if (S_ISCHR(o->result.st_mode)) { str = "chr"; } else if (S_ISBLK(o->result.st_mode)) { str = "blk"; } else if (S_ISFIFO(o->result.st_mode)) { str = "fifo"; } else if (S_ISLNK(o->result.st_mode)) { str = "link"; } else if (S_ISSOCK(o->result.st_mode)) { str = "socket"; } else { str = "other"; } *out = NCDVal_NewString(mem, str); return 1; } if (name == NCD_STRING_SIZE) { char str[50]; if (!o->succeeded) { strcpy(str, "failed"); } else { generate_decimal_repr_string((uintmax_t)o->result.st_size, str); } *out = NCDVal_NewString(mem, str); return 1; } return 0; }
static void value_compare_eval (NCDCall call, value_compare_func func) { if (NCDCall_ArgCount(&call) != 2) { return FunctionLog(&call, BLOG_ERROR, "value_compare: need two arguments"); } NCDValRef vals[2]; for (int i = 0; i < 2; i++) { vals[i] = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call)); if (NCDVal_IsInvalid(vals[i])) { return; } } int res = func(NCDVal_Compare(vals[0], vals[1])); NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res)); }
static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out) { struct instance *o = vo; if (name == NCD_STRING_SUCCEEDED) { *out = ncd_make_boolean(mem, o->succeeded, o->i->params->iparams->string_index); return 1; } if (o->succeeded && name == NCD_STRING_EMPTY) { *out = NCDVal_NewCopy(mem, o->value); return 1; } return 0; }
static void bool_not_eval (NCDCall call, int negate, char const *name) { if (NCDCall_ArgCount(&call) != 1) { return FunctionLog(&call, BLOG_ERROR, "%s: need one argument", name); } NCDValRef arg = NCDCall_EvalArg(&call, 0, NCDCall_ResMem(&call)); if (NCDVal_IsInvalid(arg)) { return; } int arg_val; if (!ncd_read_boolean(arg, &arg_val)) { return FunctionLog(&call, BLOG_ERROR, "%s: bad argument", name); } int res = (arg_val != negate); NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res)); }
static void integer_compare_eval (NCDCall call, integer_compare_func func) { if (NCDCall_ArgCount(&call) != 2) { return FunctionLog(&call, BLOG_ERROR, "integer_compare: need two arguments"); } uintmax_t ints[2]; for (int i = 0; i < 2; i++) { NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call)); if (NCDVal_IsInvalid(arg)) { return; } if (!ncd_read_uintmax(arg, &ints[i])) { return FunctionLog(&call, BLOG_ERROR, "integer_compare: wrong value"); } } int res = func(ints[0], ints[1]); NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res)); }
static void and_or_eval (NCDCall call, int is_and, char const *name) { size_t count = NCDCall_ArgCount(&call); int res = is_and; for (size_t i = 0; i < count; i++) { NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call)); if (NCDVal_IsInvalid(arg)) { return; } int arg_val; if (!ncd_read_boolean(arg, &arg_val)) { return FunctionLog(&call, BLOG_ERROR, "%s: bad argument", name); } if (arg_val != is_and) { res = !is_and; break; } } NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res)); }
static int number_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValRef *out) { struct number_instance *o = vo; if (name == NCD_STRING_IS_ERROR) { *out = ncd_make_boolean(mem, !!o->error, o->i->params->iparams->string_index); return 1; } if (name == NCD_STRING_EMPTY) { if (o->error) { ModuleLog(o->i, BLOG_ERROR, "%s", o->error); return 0; } *out = ncd_make_uintmax(mem, o->value); return 1; } return 0; }
static void imp_eval (NCDCall call) { if (NCDCall_ArgCount(&call) != 2) { return FunctionLog(&call, BLOG_ERROR, "imp: need two arguments"); } int res = 0; for (size_t i = 0; i < 2; i++) { NCDValRef arg = NCDCall_EvalArg(&call, i, NCDCall_ResMem(&call)); if (NCDVal_IsInvalid(arg)) { return; } int arg_val; if (!ncd_read_boolean(arg, &arg_val)) { return FunctionLog(&call, BLOG_ERROR, "imp: bad argument"); } if (arg_val == i) { res = 1; break; } } NCDCall_SetResult(&call, ncd_make_boolean(NCDCall_ResMem(&call), res)); }