virsh: simplify printing of typed parameters

No need to repeat code for formatting typed parameters.

* tools/virsh.c (vshGetTypedParamValue): Support strings, and exit
on OOM.
(cmdSchedinfo, cmdBlkiotune, cmdMemtune, cmdBlkdeviotune): Use
it for less code.
This commit is contained in:
Eric Blake 2011-12-19 16:06:08 -07:00
parent 60f99824d4
commit f8616336a3

View File

@ -387,7 +387,8 @@ static bool vshConnectionUsability(vshControl *ctl, virConnectPtr conn);
static virTypedParameterPtr vshFindTypedParamByName(const char *name, static virTypedParameterPtr vshFindTypedParamByName(const char *name,
virTypedParameterPtr list, virTypedParameterPtr list,
int count); int count);
static char *vshGetTypedParamValue(vshControl *ctl, virTypedParameterPtr item); static char *vshGetTypedParamValue(vshControl *ctl, virTypedParameterPtr item)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
static char *editWriteToTempFile (vshControl *ctl, const char *doc); static char *editWriteToTempFile (vshControl *ctl, const char *doc);
static int editFile (vshControl *ctl, const char *filename); static int editFile (vshControl *ctl, const char *filename);
@ -1186,8 +1187,7 @@ cmdDomblkstat (vshControl *ctl, const vshCmd *cmd)
nparams))) nparams)))
continue; continue;
if (!(value = vshGetTypedParamValue(ctl, par))) value = vshGetTypedParamValue(ctl, par);
continue;
/* to print other not supported fields, mark the already printed */ /* to print other not supported fields, mark the already printed */
par->field[0] = '\0'; /* set the name to empty string */ par->field[0] = '\0'; /* set the name to empty string */
@ -1214,9 +1214,7 @@ cmdDomblkstat (vshControl *ctl, const vshCmd *cmd)
if (!*params[i].field) if (!*params[i].field)
continue; continue;
if (!(value = vshGetTypedParamValue(ctl, params+i))) value = vshGetTypedParamValue(ctl, params+i);
continue;
vshPrint(ctl, "%s %s %s\n", device, params[i].field, value); vshPrint(ctl, "%s %s %s\n", device, params[i].field, value);
VIR_FREE(value); VIR_FREE(value);
} }
@ -2975,28 +2973,9 @@ cmdSchedinfo(vshControl *ctl, const vshCmd *cmd)
ret_val = true; ret_val = true;
for (i = 0; i < nparams; i++) { for (i = 0; i < nparams; i++) {
switch (params[i].type) { char *str = vshGetTypedParamValue(ctl, &params[i]);
case VIR_TYPED_PARAM_INT: vshPrint(ctl, "%-15s: %s\n", params[i].field, str);
vshPrint(ctl, "%-15s: %d\n", params[i].field, params[i].value.i); VIR_FREE(str);
break;
case VIR_TYPED_PARAM_UINT:
vshPrint(ctl, "%-15s: %u\n", params[i].field, params[i].value.ui);
break;
case VIR_TYPED_PARAM_LLONG:
vshPrint(ctl, "%-15s: %lld\n", params[i].field, params[i].value.l);
break;
case VIR_TYPED_PARAM_ULLONG:
vshPrint(ctl, "%-15s: %llu\n", params[i].field, params[i].value.ul);
break;
case VIR_TYPED_PARAM_DOUBLE:
vshPrint(ctl, "%-15s: %f\n", params[i].field, params[i].value.d);
break;
case VIR_TYPED_PARAM_BOOLEAN:
vshPrint(ctl, "%-15s: %d\n", params[i].field, params[i].value.b);
break;
default:
vshPrint(ctl, "not implemented scheduler parameter type\n");
}
} }
} }
@ -4935,38 +4914,9 @@ cmdBlkiotune(vshControl * ctl, const vshCmd * cmd)
} }
for (i = 0; i < nparams; i++) { for (i = 0; i < nparams; i++) {
switch (params[i].type) { char *str = vshGetTypedParamValue(ctl, &params[i]);
case VIR_TYPED_PARAM_INT: vshPrint(ctl, "%-15s: %s\n", params[i].field, str);
vshPrint(ctl, "%-15s: %d\n", params[i].field, VIR_FREE(str);
params[i].value.i);
break;
case VIR_TYPED_PARAM_UINT:
vshPrint(ctl, "%-15s: %u\n", params[i].field,
params[i].value.ui);
break;
case VIR_TYPED_PARAM_LLONG:
vshPrint(ctl, "%-15s: %lld\n", params[i].field,
params[i].value.l);
break;
case VIR_TYPED_PARAM_ULLONG:
vshPrint(ctl, "%-15s: %llu\n", params[i].field,
params[i].value.ul);
break;
case VIR_TYPED_PARAM_DOUBLE:
vshPrint(ctl, "%-15s: %f\n", params[i].field,
params[i].value.d);
break;
case VIR_TYPED_PARAM_BOOLEAN:
vshPrint(ctl, "%-15s: %d\n", params[i].field,
params[i].value.b);
break;
case VIR_TYPED_PARAM_STRING:
vshPrint(ctl, "%-15s: %s\n", params[i].field,
params[i].value.s);
break;
default:
vshPrint(ctl, "unimplemented blkio parameter type\n");
}
} }
} else { } else {
/* set the blkio parameters */ /* set the blkio parameters */
@ -5112,36 +5062,13 @@ cmdMemtune(vshControl * ctl, const vshCmd * cmd)
} }
for (i = 0; i < nparams; i++) { for (i = 0; i < nparams; i++) {
switch (params[i].type) { if (params[i].type == VIR_TYPED_PARAM_ULLONG &&
case VIR_TYPED_PARAM_INT: params[i].value.ul == VIR_DOMAIN_MEMORY_PARAM_UNLIMITED) {
vshPrint(ctl, "%-15s: %d\n", params[i].field, vshPrint(ctl, "%-15s: %s\n", params[i].field, _("unlimited"));
params[i].value.i); } else {
break; char *str = vshGetTypedParamValue(ctl, &params[i]);
case VIR_TYPED_PARAM_UINT: vshPrint(ctl, "%-15s: %s\n", params[i].field, str);
vshPrint(ctl, "%-15s: %u\n", params[i].field, VIR_FREE(str);
params[i].value.ui);
break;
case VIR_TYPED_PARAM_LLONG:
vshPrint(ctl, "%-15s: %lld\n", params[i].field,
params[i].value.l);
break;
case VIR_TYPED_PARAM_ULLONG:
if (params[i].value.ul == VIR_DOMAIN_MEMORY_PARAM_UNLIMITED)
vshPrint(ctl, "%-15s: unlimited\n", params[i].field);
else
vshPrint(ctl, "%-15s: %llu kB\n", params[i].field,
params[i].value.ul);
break;
case VIR_TYPED_PARAM_DOUBLE:
vshPrint(ctl, "%-15s: %f\n", params[i].field,
params[i].value.d);
break;
case VIR_TYPED_PARAM_BOOLEAN:
vshPrint(ctl, "%-15s: %d\n", params[i].field,
params[i].value.b);
break;
default:
vshPrint(ctl, "unimplemented memory parameter type\n");
} }
} }
@ -6562,34 +6489,9 @@ cmdBlkdeviotune(vshControl *ctl, const vshCmd *cmd)
} }
for (i = 0; i < nparams; i++) { for (i = 0; i < nparams; i++) {
switch(params[i].type) { char *str = vshGetTypedParamValue(ctl, &params[i]);
case VIR_TYPED_PARAM_INT: vshPrint(ctl, "%-15s: %s\n", params[i].field, str);
vshPrint(ctl, "%-15s: %d\n", params[i].field, VIR_FREE(str);
params[i].value.i);
break;
case VIR_TYPED_PARAM_UINT:
vshPrint(ctl, "%-15s: %u\n", params[i].field,
params[i].value.ui);
break;
case VIR_TYPED_PARAM_LLONG:
vshPrint(ctl, "%-15s: %lld\n", params[i].field,
params[i].value.l);
break;
case VIR_TYPED_PARAM_ULLONG:
vshPrint(ctl, "%-15s: %llu\n", params[i].field,
params[i].value.ul);
break;
case VIR_TYPED_PARAM_DOUBLE:
vshPrint(ctl, "%-15s: %f\n", params[i].field,
params[i].value.d);
break;
case VIR_TYPED_PARAM_BOOLEAN:
vshPrint(ctl, "%-15s: %d\n", params[i].field,
params[i].value.b);
break;
default:
vshPrint(ctl, "unimplemented block I/O throttle parameter type\n");
}
} }
ret = true; ret = true;
@ -17052,15 +16954,14 @@ vshDomainStateReasonToString(int state, int reason)
return N_("unknown"); return N_("unknown");
} }
/* Return a non-NULL string representation of a typed parameter; exit
* if we are out of memory. */
static char * static char *
vshGetTypedParamValue(vshControl *ctl, virTypedParameterPtr item) vshGetTypedParamValue(vshControl *ctl, virTypedParameterPtr item)
{ {
int ret = 0; int ret = 0;
char *str = NULL; char *str = NULL;
if (!ctl || !item)
return NULL;
switch(item->type) { switch(item->type) {
case VIR_TYPED_PARAM_INT: case VIR_TYPED_PARAM_INT:
ret = virAsprintf(&str, "%d", item->value.i); ret = virAsprintf(&str, "%d", item->value.i);
@ -17086,12 +16987,18 @@ vshGetTypedParamValue(vshControl *ctl, virTypedParameterPtr item)
ret = virAsprintf(&str, "%s", item->value.b ? _("yes") : _("no")); ret = virAsprintf(&str, "%s", item->value.b ? _("yes") : _("no"));
break; break;
case VIR_TYPED_PARAM_STRING:
str = vshStrdup(ctl, item->value.s);
break;
default: default:
vshError(ctl, _("unimplemented block statistics parameter type")); vshError(ctl, _("unimplemented parameter type %d"), item->type);
} }
if (ret < 0) if (ret < 0) {
vshError(ctl, "%s", _("Out of memory")); vshError(ctl, "%s", _("Out of memory"));
exit(EXIT_FAILURE);
}
return str; return str;
} }