你解释一下这个函数呢 int config_plugin_values_init_block(server * const srv, const array * const ca, const config_plugin_keys_t * const cpk, const char * const mname, config_plugin_value_t *cpv) {
/*(cpv must be list with sufficient elements to store all matches + 1)*/
int rc = 1; /* default is success */
for (int i = 0; cpk[i].ktype != T_CONFIG_UNSET; ++i) {
const data_unset * const du =
array_get_element_klen(ca, cpk[i].k, cpk[i].klen);
if (NULL == du) continue; /* not found */
cpv->k_id = i;
cpv->vtype = cpk[i].ktype;
switch (cpk[i].ktype) {
case T_CONFIG_ARRAY:
case T_CONFIG_ARRAY_KVANY:
case T_CONFIG_ARRAY_KVARRAY:
case T_CONFIG_ARRAY_KVSTRING:
case T_CONFIG_ARRAY_VLIST:
if (du->type == TYPE_ARRAY) {
cpv->v.a = &((const data_array *)du)->value;
}
else {
log_error(srv->errh, __FILE__, __LINE__,
"%s should have been a list like "
"%s = ( \"...\" )", cpk[i].k, cpk[i].k);
rc = 0;
continue;
}
switch (cpk[i].ktype) {
case T_CONFIG_ARRAY_KVANY:
if (!array_is_kvany(cpv->v.a)) {
log_error(srv->errh, __FILE__, __LINE__,
"%s should have been a list of key => values like "
"%s = ( \"...\" => \"...\", \"...\" => \"...\" )",
cpk[i].k, cpk[i].k);
rc = 0;
continue;
}
break;
case T_CONFIG_ARRAY_KVARRAY:
if (!array_is_kvarray(cpv->v.a)) {
log_error(srv->errh, __FILE__, __LINE__,
"%s should have been a list of key => list like "
"%s = ( \"...\" => ( \"...\" => \"...\" ) )",
cpk[i].k, cpk[i].k);
rc = 0;
continue;
}
break;
case T_CONFIG_ARRAY_KVSTRING:
if (!array_is_kvstring(cpv->v.a)) {
log_error(srv->errh, __FILE__, __LINE__,
"%s should have been a list of key => string values like "
"%s = ( \"...\" => \"...\", \"...\" => \"...\" )",
cpk[i].k, cpk[i].k);
rc = 0;
continue;
}
break;
case T_CONFIG_ARRAY_VLIST:
if (!array_is_vlist(cpv->v.a)) {
log_error(srv->errh, __FILE__, __LINE__,
"%s should have been a list of string values like "
"%s = ( \"...\", \"...\" )",
cpk[i].k, cpk[i].k);
rc = 0;
continue;
}
break;
/*case T_CONFIG_ARRAY:*/
default:
break;
}
break;
case T_CONFIG_STRING:
if (du->type == TYPE_STRING) {
cpv->v.b = &((const data_string *)du)->value;
}
else {
log_error(srv->errh, __FILE__, __LINE__,
"%s should have been a string like ... = \"...\"", cpk[i].k);
rc = 0;
continue;
}
break;
case T_CONFIG_SHORT:
switch(du->type) {
case TYPE_INTEGER:
cpv->v.shrt =
(unsigned short)((const data_integer *)du)->value;
break;
case TYPE_STRING: {
/* If the value came from an environment variable, then it is
* a data_string, although it may contain a number in ASCII
* decimal format. We try to interpret the string as a decimal
* short before giving up, in order to support setting numeric
* values with environment variables (e.g. port number).
*/
const char * const v = ((const data_string *)du)->value.ptr;
if (v && *v) {
char *e;
long l = strtol(v, &e, 10);
if (e != v && !*e && l >= 0 && l <= 65535) {
cpv->v.shrt = (unsigned short)l;
break;
}
}
log_error(srv->errh, __FILE__, __LINE__,
"got a string but expected a short: %s %s", cpk[i].k, v);
rc = 0;
continue;
}
default:
log_error(srv->errh, __FILE__, __LINE__,
"unexpected type for key: %s %d expected a short integer, "
"range 0 ... 65535", cpk[i].k, du->type);
rc = 0;
continue;
}
break;
case T_CONFIG_INT:
switch(du->type) {
case TYPE_INTEGER:
cpv->v.u = ((const data_integer *)du)->value;
break;
case TYPE_STRING: {
const char * const v = ((const data_string *)du)->value.ptr;
if (v && *v) {
char *e;
long l = strtol(v, &e, 10);
if (e != v && !*e && l >= 0) {
cpv->v.u = (unsigned int)l;
break;
}
}
log_error(srv->errh, __FILE__, __LINE__,
"got a string but expected an integer: %s %s",cpk[i].k,v);
rc = 0;
continue;
}
default:
log_error(srv->errh, __FILE__, __LINE__,
"unexpected type for key: %s %d expected an integer, "
"range 0 ... 4294967295", cpk[i].k, du->type);
rc = 0;
continue;
}
break;
case T_CONFIG_BOOL:
{
int v = config_plugin_value_tobool(du, -1);
if (-1 == v) {
log_error(srv->errh, __FILE__, __LINE__,
"ERROR: unexpected type for key: %s (string) "
"\"(enable|disable)\"", cpk[i].k);
rc = 0;
continue;
}
cpv->v.u = v;
}
break;
case T_CONFIG_LOCAL:
case T_CONFIG_UNSET:
continue;
case T_CONFIG_UNSUPPORTED:
log_error(srv->errh, __FILE__, __LINE__,
"ERROR: found unsupported key: %s (%s)", cpk[i].k, mname);
srv->srvconf.config_unsupported = 1;
continue;
case T_CONFIG_DEPRECATED:
log_error(srv->errh, __FILE__, __LINE__,
"ERROR: found deprecated key: %s (%s)", cpk[i].k, mname);
srv->srvconf.config_deprecated = 1;
continue;
}
++cpv;
}
cpv->k_id = -1; /* indicate list end */
return rc;
}
最新发布