Skip to content

Commit

Permalink
clean up opt package
Browse files Browse the repository at this point in the history
  • Loading branch information
garlick committed Mar 21, 2011
1 parent c947d07 commit 787009b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 121 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
@@ -1,5 +1,7 @@
2011-03-20 Jim Garlick <garlick@llnl.gov>

* utils/opt.[ch], utils/diodmount.c : Clean up opt package.

* diod/diod.c, diodctl/serv.c, libdiod/diod_conf.[ch] : Drop
atomicmax option to diod that is not currently relevant.

Expand Down
1 change: 0 additions & 1 deletion diod/diod.c
Expand Up @@ -115,7 +115,6 @@ main(int argc, char **argv)
diod_conf_init ();

/* Command line overrides defaults.
* Diod does not look at the config file.
*/
optind = 0;
opterr = 0;
Expand Down
18 changes: 9 additions & 9 deletions tests/misc/topt.c
Expand Up @@ -24,25 +24,25 @@ main (int argc, char *argv[])

diod_log_init (argv[0]);

opt_add (o, "mickey=%d", 42);
opt_add (o, "goofey=%s", "yes");
opt_add (o, "donald");
opt_add_cslist (o, "foo,bar,baz");
opt_add (o, "lastone");
opt_addf (o, "mickey=%d", 42);
opt_addf (o, "goofey=%s", "yes");
opt_addf (o, "donald");
opt_addf (o, "foo,bar,baz");
opt_addf (o, "lastone");

s = opt_string (o);
s = opt_csv (o);
msg ("opt string='%s'", s);
free (s);

assert (opt_find (o, "mickey"));
assert (opt_find (o, "bar"));
assert (!opt_find (o, "barn"));

assert (opt_scan (o, "mickey=%d", &i));
assert (opt_scanf (o, "mickey=%d", &i));
assert (i == 42);

opt_add_cslist_override (o, "mickey=string,foo=12,bar=15,baz");
s = opt_string (o);
opt_addf (o, "mickey=string,foo=%d,bar=%d,baz", 12, 15);
s = opt_csv (o);
msg ("opt string='%s'", s);
free (s);

Expand Down
54 changes: 27 additions & 27 deletions utils/diodmount.c
Expand Up @@ -136,7 +136,7 @@ main (int argc, char *argv[])
vopt++;
break;
case 'o': /* --options OPT[,OPT]... */
opt_add_cslist_override (o, optarg);
opt_addf (o, optarg);
break;
default:
usage ();
Expand All @@ -157,7 +157,7 @@ main (int argc, char *argv[])
* Take care of it here and exit.
*/
if (opt_find (o, "remount")) {
if (opt_check_allowed_cslist (o, "ro,rw,aname,remount"))
if (opt_check_allowed_csv (o, "ro,rw,aname,remount"))
msg_exit ("-oremount can only be used with ro,rw");
_diod_remount (o, spec, dir, vopt, fopt);
goto done;
Expand All @@ -173,34 +173,34 @@ main (int argc, char *argv[])
* is passed to the kernel via -orfdno,wfdno.
*/
if (!opt_find (o, "trans"))
opt_add (o, "trans=fd");
opt_addf (o, "trans=%s", "fd");
else if (!opt_find (o, "trans=fd"))
msg_exit ("only -otrans=fd transport is supported");

/* Set msize if not already set. Validate it later.
*/
if (!opt_find (o, "msize"))
opt_add (o, "msize=%d", DIOD_DEFAULT_MSIZE);
opt_addf (o, "msize=%d", DIOD_DEFAULT_MSIZE);

/* Only .L version is supported.
*/
if (!opt_find (o, "version"))
opt_add (o, "version=9p2000.L");
opt_addf (o, "version=%s", "9p2000.L");
else if (!opt_find (o, "version=9p2000.L"))
msg_exit ("only -oversion=9p2000.L is supported (little p, big L)");

/* Set debug level.
*/
if (!opt_find (o, "debug"))
opt_add (o, "debug=0");
opt_addf (o, "debug=%d", 0);

/* Server is on an inherited file descriptor.
* For testing, we start server on a socketpair duped to fd 0.
*/
if (opt_find (o, "rfdno") || opt_find (o, "wfdno")) {
int rfd, wfd;

if (!opt_scan (o, "rfdno=%d", &rfd) || !opt_scan (o, "wfdno=%d", &wfd))
if (!opt_scanf (o, "rfdno=%d", &rfd) || !opt_scanf (o, "wfdno=%d", &wfd))
msg_exit ("-orfdno,wfdno must be used together");
if (rfd != wfd)
msg_exit ("-orfdno,wfdno must have same value");
Expand Down Expand Up @@ -229,8 +229,8 @@ main (int argc, char *argv[])
if (sfd < 0)
msg_exit ("could not contact diod server(s)");
opt_delete (o, "port");
opt_add (o, "rfdno=%d", sfd);
opt_add (o, "wfdno=%d", sfd);
opt_addf (o, "rfdno=%d", sfd);
opt_addf (o, "wfdno=%d", sfd);

/* Try diodctl server on each host until one responds.
* Negotiate a port to connect to based on user and jobid.
Expand All @@ -253,20 +253,20 @@ main (int argc, char *argv[])
hostlist_iterator_destroy (hi);
if (sfd < 0)
msg_exit ("failed to establish connection with server");
opt_add (o, "rfdno=%d", sfd);
opt_add (o, "wfdno=%d", sfd);
opt_addf (o, "rfdno=%d", sfd);
opt_addf (o, "wfdno=%d", sfd);
if (jobid)
opt_delete (o, "jobid");
}

assert (opt_find (o, "trans=fd"));
assert (opt_scan (o, "msize=%d", &i));
assert (opt_scanf (o, "msize=%d", &i));
assert (opt_find (o, "version=9p2000.L"));
assert (opt_scan (o, "debug=%d", &i));
assert (opt_scan (o, "wfdno=%d", &i) && opt_scan (o, "rfdno=%d", &i));
assert (opt_scanf (o, "debug=%d", &i));
assert (opt_scanf (o, "wfdno=%d", &i) && opt_scanf (o, "rfdno=%d", &i));
assert (opt_find (o, "aname"));
assert ((opt_find (o, "access=user") && opt_find(o, "uname=root"))
|| (opt_scan (o, "access=%d", &i) && opt_find(o, "uname")));
|| (opt_scanf (o, "access=%d", &i) && opt_find(o, "uname")));

assert (!opt_find (o, "port"));
assert (!opt_find (o, "jobid"));
Expand Down Expand Up @@ -298,7 +298,7 @@ _parse_spec (char *spec, Opt o)
msg_exit ("no host specified");
if (!aname || strlen (aname) == 0)
aname = opt_find (o, "aname");
else if (!opt_add (o, "aname=%s", aname))
else if (!opt_addf (o, "aname=%s", aname))
msg_exit ("you cannot have both -oaname and spec=host:aname");
if (!aname || strlen (aname) == 0)
msg_exit ("no aname specified");
Expand Down Expand Up @@ -341,28 +341,28 @@ _parse_uname_access (Opt o)
msg_exit ("could not look up uname='%s'", uname);
uname_uid = pw->pw_uid;
}
if (access && opt_scan (o, "access=%d", &access_uid)) {
if (access && opt_scanf (o, "access=%d", &access_uid)) {
if (!(pw = getpwuid (access_uid)))
msg_exit ("could not look up access='%d'", access_uid);
if (!(access_name = strdup (pw->pw_name)))
msg_exit ("out of memory");
}

if (!uname && !access) {
opt_add (o, "uname=root");
opt_add (o, "access=user");
opt_addf (o, "uname=%s", "root");
opt_addf (o, "access=%s", "user");

} else if (uname && !access) {
if (uname_uid == 0)
opt_add (o, "access=user");
opt_addf (o, "access=%s", "user");
else
opt_add (o, "access=%d", uname_uid);
opt_addf (o, "access=%d", uname_uid);

} else if (!uname && access) {
if (strcmp (access, "user") == 0)
opt_add (o, "uname=root");
opt_addf (o, "uname=%s", "root");
else if (access_name) /* access=<uid> */
opt_add (o, "uname=%s", access_name);
opt_addf (o, "uname=%s", access_name);
else
msg_exit ("unsupported -oaccess=%s", access);
} else { /* if (uname && access) */
Expand Down Expand Up @@ -433,7 +433,7 @@ _getflags (Opt o, unsigned long *flags)
static void
_diod_remount (Opt o, char *spec, char *dir, int vopt, int fopt)
{
char *options = opt_string (o);
char *options = opt_csv (o);
unsigned long mountflags = 0;

_getflags (o, &mountflags);
Expand Down Expand Up @@ -489,13 +489,13 @@ _diod_mount (Opt o, int fd, char *spec, char *dir, int vopt, int fopt, int nopt)
Npcfsys *fs;
unsigned long mountflags = 0;

options = opt_string (o);
options = opt_csv (o);
_getflags (o, &mountflags);
options9p = opt_string (o); /* after mountflags removed from opt list */
options9p = opt_csv (o); /* after mountflags removed from opt list */

if (!(aname = opt_find (o, "aname")))
msg_exit ("aname is not set"); /* can't happen */
if (!opt_scan (o, "msize=%d", &msize) || msize < P9_IOHDRSZ)
if (!opt_scanf (o, "msize=%d", &msize) || msize < P9_IOHDRSZ)
msg_exit ("msize must be set to integer >= %d", P9_IOHDRSZ);

if (vopt)
Expand Down
121 changes: 44 additions & 77 deletions utils/opt.c
Expand Up @@ -72,7 +72,7 @@ opt_create (void)
}

char *
opt_string (Opt o)
opt_csv (Opt o)
{
ListIterator itr;
char *item, *s;
Expand Down Expand Up @@ -116,80 +116,31 @@ _match_key (char *item, char *key)
}

int
opt_add (Opt o, const char *fmt, ...)
opt_addf (Opt o, const char *fmt, ...)
{
va_list ap;
char *item;
char *csv, *item, *cpy;
char *saveptr = NULL;
int error;

assert (o->magic == OPT_MAGIC);
va_start (ap, fmt);
error = vasprintf (&item, fmt, ap);
error = vasprintf (&csv, fmt, ap);
va_end (ap);
if (error < 0)
msg_exit ("out of memory");
if (list_find_first (o->list, (ListFindF)_match_key, item))
return 0;
if (!list_append (o->list, item))
msg_exit ("out of memory");
return 1;
}

void
opt_add_override (Opt o, const char *fmt, ...)
{
va_list ap;
char *item;
int error;

assert (o->magic == OPT_MAGIC);
va_start (ap, fmt);
error = vasprintf (&item, fmt, ap);
va_end (ap);
if (error < 0)
msg_exit ("out of memory");
list_delete_all (o->list, (ListFindF)_match_key, item);
if (!list_append (o->list, item))
msg_exit ("out of memory");
}

int
opt_add_cslist (Opt o, const char *s)
{
char *cpy = strdup (s);
char *item;
int ret = 0;

if (!cpy)
msg_exit ("out of memory");

item = strtok (cpy, ",");
item = strtok_r (csv, ",", &saveptr);
while (item) {
if (!opt_add (o, "%s", item))
goto done;
item = strtok (NULL, ",");
}
ret = 1;
done:
free (cpy);
return ret;
}

void
opt_add_cslist_override (Opt o, const char *s)
{
char *cpy = strdup (s);
char *item;

if (!cpy)
msg_exit ("out of memory");

item = strtok (cpy, ",");
while (item) {
opt_add_override (o, "%s", item);
item = strtok (NULL, ",");
if (!(cpy = strdup (item)))
msg_exit ("out of memory");
(void)list_delete_all (o->list, (ListFindF)_match_key, cpy);
if (!list_append (o->list, cpy))
msg_exit ("out of memory");
item = strtok_r (NULL, ",", &saveptr);
}
free (cpy);
free (csv);
return 1;
}

/* return option value or empty string (not null)
Expand Down Expand Up @@ -228,30 +179,46 @@ opt_delete (Opt o, char *key)
}

int
opt_scan (Opt o, const char *fmt, ...)
opt_vscanf (Opt o, const char *fmt, va_list ap)
{
va_list ap;
char *s, *val, *key;
ListIterator itr;
char *item;
int ret = 0;

assert (o->magic == OPT_MAGIC);

if (!(key = strdup (fmt)))
if (!(itr = list_iterator_create (o->list)))
msg_exit ("out of memory");
if (!(val = strchr (key, '=')))
msg_exit ("opt_scan used incorrectly, fmt='%s'", fmt);
*val++ = '\0';
if ((s = opt_find (o, key))) {
va_start (ap, fmt);
ret = vsscanf(s, val, ap);
va_end (ap);
while ((item = list_next (itr))) {
va_list vacpy;

va_copy (vacpy, ap);
ret = vsscanf (item, fmt, vacpy);
va_end (vacpy);

if (ret > 0)
break;
}
free (key);
list_iterator_destroy (itr);
return ret;
}

int
opt_scanf (Opt o, const char *fmt, ...)
{
va_list ap;
int ret;

va_start(ap, fmt);
ret = opt_vscanf (o, fmt, ap);
va_end(ap);

return ret;
}


int
opt_check_allowed_cslist (Opt o, const char *s)
opt_check_allowed_csv (Opt o, const char *csv)
{
Opt allow;
ListIterator itr;
Expand All @@ -261,7 +228,7 @@ opt_check_allowed_cslist (Opt o, const char *s)
assert (o->magic == OPT_MAGIC);

allow = opt_create ();
opt_add_cslist_override (allow, s);
opt_addf (allow, csv);

if (!(itr = list_iterator_create (o->list)))
msg_exit ("out of memory");
Expand Down

0 comments on commit 787009b

Please sign in to comment.