aboutsummaryrefslogtreecommitdiffstats
path: root/sbin/geom/misc
diff options
context:
space:
mode:
authorPawel Jakub Dawidek <pjd@FreeBSD.org>2005-12-07 01:38:27 +0000
committerPawel Jakub Dawidek <pjd@FreeBSD.org>2005-12-07 01:38:27 +0000
commitf13942a7469cd93e7f22f5fdc49d866ba488a982 (patch)
treeb5b01ee00715a779cd75c12e2ae3797f95f5124c /sbin/geom/misc
parent5612eab7447a9a5cb68a9fed39fc4696f4de0aa3 (diff)
downloadsrc-f13942a7469cd93e7f22f5fdc49d866ba488a982.tar.gz
src-f13942a7469cd93e7f22f5fdc49d866ba488a982.zip
- The geom(8) utility only uses three types of arguments: string (char *),
value (intmax_t) and boolean (int). Based on that provide three functions: - gctl_get_ascii() - gctl_get_int() - gctl_get_intmax() - Hide gctl_get_param() function, as it is only used internally in subr.c. - Allow to provide argument name as (fmt, ...). - Assert geom(8) bugs (missing argument is a geom(8) bug). - Clean-up and simplify the code by using new functions and assumtions (no more checking for missing argument). Tested by: regression tests
Notes
Notes: svn path=/head/; revision=153190
Diffstat (limited to 'sbin/geom/misc')
-rw-r--r--sbin/geom/misc/subr.c100
-rw-r--r--sbin/geom/misc/subr.h8
2 files changed, 60 insertions, 48 deletions
diff --git a/sbin/geom/misc/subr.c b/sbin/geom/misc/subr.c
index 599223a80e5f..f47523eea21f 100644
--- a/sbin/geom/misc/subr.c
+++ b/sbin/geom/misc/subr.c
@@ -315,66 +315,76 @@ gctl_error(struct gctl_req *req, const char *error, ...)
va_end(ap);
}
-void *
-gctl_get_param(struct gctl_req *req, const char *param, int *len)
+static void *
+gctl_get_param(struct gctl_req *req, size_t len, const char *pfmt, va_list ap)
{
- unsigned i;
+ struct gctl_req_arg *argp;
+ char param[256];
void *p;
- struct gctl_req_arg *ap;
+ unsigned i;
+ vsnprintf(param, sizeof(param), pfmt, ap);
for (i = 0; i < req->narg; i++) {
- ap = &req->arg[i];
- if (strcmp(param, ap->name))
+ argp = &req->arg[i];
+ if (strcmp(param, argp->name))
continue;
- if (!(ap->flag & GCTL_PARAM_RD))
+ if (!(argp->flag & GCTL_PARAM_RD))
continue;
- p = ap->value;
- if (len != NULL)
- *len = ap->len;
+ p = argp->value;
+ if (len == 0) {
+ /* We are looking for a string. */
+ if (argp->len < 1) {
+ fprintf(stderr, "No length argument (%s).\n",
+ param);
+ abort();
+ }
+ if (((char *)p)[argp->len - 1] != '\0') {
+ fprintf(stderr, "Unterminated argument (%s).\n",
+ param);
+ abort();
+ }
+ } else if ((int)len != argp->len) {
+ fprintf(stderr, "Wrong length %s argument.\n", param);
+ abort();
+ }
return (p);
}
- return (NULL);
+ fprintf(stderr, "No such argument (%s).\n", param);
+ abort();
}
-char const *
-gctl_get_asciiparam(struct gctl_req *req, const char *param)
+int
+gctl_get_int(struct gctl_req *req, const char *pfmt, ...)
{
- unsigned i;
- char const *p;
- struct gctl_req_arg *ap;
+ int *p;
+ va_list ap;
- for (i = 0; i < req->narg; i++) {
- ap = &req->arg[i];
- if (strcmp(param, ap->name))
- continue;
- if (!(ap->flag & GCTL_PARAM_RD))
- continue;
- p = ap->value;
- if (ap->len < 1) {
- gctl_error(req, "No length argument (%s)", param);
- return (NULL);
- }
- if (p[ap->len - 1] != '\0') {
- gctl_error(req, "Unterminated argument (%s)", param);
- return (NULL);
- }
- return (p);
- }
- return (NULL);
+ va_start(ap, pfmt);
+ p = gctl_get_param(req, sizeof(int), pfmt, ap);
+ va_end(ap);
+ return (*p);
}
-void *
-gctl_get_paraml(struct gctl_req *req, const char *param, int len)
+intmax_t
+gctl_get_intmax(struct gctl_req *req, const char *pfmt, ...)
{
- int i;
- void *p;
+ intmax_t *p;
+ va_list ap;
- p = gctl_get_param(req, param, &i);
- if (p == NULL)
- gctl_error(req, "Missing %s argument", param);
- else if (i != len) {
- p = NULL;
- gctl_error(req, "Wrong length %s argument", param);
- }
+ va_start(ap, pfmt);
+ p = gctl_get_param(req, sizeof(intmax_t), pfmt, ap);
+ va_end(ap);
+ return (*p);
+}
+
+const char *
+gctl_get_ascii(struct gctl_req *req, const char *pfmt, ...)
+{
+ const char *p;
+ va_list ap;
+
+ va_start(ap, pfmt);
+ p = gctl_get_param(req, 0, pfmt, ap);
+ va_end(ap);
return (p);
}
diff --git a/sbin/geom/misc/subr.h b/sbin/geom/misc/subr.h
index 164c3b46dc0e..ad02969a3d26 100644
--- a/sbin/geom/misc/subr.h
+++ b/sbin/geom/misc/subr.h
@@ -28,6 +28,8 @@
#ifndef _SUBR_H_
#define _SUBR_H_
+#include <stdint.h>
+
unsigned g_lcm(unsigned a, unsigned b);
uint32_t bitcount32(uint32_t x);
@@ -40,7 +42,7 @@ int g_metadata_store(const char *name, u_char *md, size_t size);
int g_metadata_clear(const char *name, const char *magic);
void gctl_error(struct gctl_req *req, const char *error, ...);
-void *gctl_get_param(struct gctl_req *req, const char *param, int *len);
-char const *gctl_get_asciiparam(struct gctl_req *req, const char *param);
-void *gctl_get_paraml(struct gctl_req *req, const char *param, int len);
+int gctl_get_int(struct gctl_req *req, const char *pfmt, ...);
+intmax_t gctl_get_intmax(struct gctl_req *req, const char *pfmt, ...);
+const char *gctl_get_ascii(struct gctl_req *req, const char *pfmt, ...);
#endif /* !_SUBR_H_ */