| 1 |
/***************************************************************************** |
| 2 |
* Copyright (C) 2010-14 Lawrence Livermore National Security, LLC. |
| 3 |
* Written by Jim Garlick <[email protected]> LLNL-CODE-423279 |
| 4 |
* All Rights Reserved. |
| 5 |
* |
| 6 |
* This file is part of the Distributed I/O Daemon (diod). |
| 7 |
* For details, see http://code.google.com/p/diod. |
| 8 |
* |
| 9 |
* This program is free software; you can redistribute it and/or modify it |
| 10 |
* under the terms of the GNU General Public License as published by the Free |
| 11 |
* Software Foundation; either version 2 of the license, or (at your option) |
| 12 |
* any later version. |
| 13 |
* |
| 14 |
* This program is distributed in the hope that it will be useful, but |
| 15 |
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 16 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 17 |
* for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU General Public License along |
| 20 |
* with this program; if not, write to the Free Software Foundation, |
| 21 |
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 22 |
* See also: http://www.gnu.org/licenses |
| 23 |
*****************************************************************************/ |
| 24 |
|
| 25 |
/* opt.c - handle comma-separated key[=val] lists */ |
| 26 |
|
| 27 |
#if HAVE_CONFIG_H |
| 28 |
#include "config.h" |
| 29 |
#endif |
| 30 |
#include <unistd.h> |
| 31 |
#include <stdlib.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <string.h> |
| 34 |
#include <errno.h> |
| 35 |
#include <stdarg.h> |
| 36 |
#include <inttypes.h> |
| 37 |
|
| 38 |
#include "9p.h" |
| 39 |
#include "npfs.h" |
| 40 |
|
| 41 |
#include "diod_log.h" |
| 42 |
|
| 43 |
#include "opt.h" |
| 44 |
#include "list.h" |
| 45 |
|
| 46 |
#define OPT_MAGIC 0x54545344 |
| 47 |
|
| 48 |
struct opt_struct { |
| 49 |
int magic; |
| 50 |
List list; |
| 51 |
}; |
| 52 |
|
| 53 |
void |
| 54 |
opt_destroy (Opt o) |
| 55 |
{ |
| 56 |
NP_ASSERT (o->magic == OPT_MAGIC); |
| 57 |
if (o->list) |
| 58 |
list_destroy (o->list); |
| 59 |
free (o); |
| 60 |
} |
| 61 |
|
| 62 |
Opt |
| 63 |
opt_create (void) |
| 64 |
{ |
| 65 |
Opt o = malloc (sizeof (*o)); |
| 66 |
|
| 67 |
if (!o) |
| 68 |
msg_exit ("out of memory"); |
| 69 |
o->magic = OPT_MAGIC; |
| 70 |
o->list = list_create ((ListDelF)free); |
| 71 |
if (!o->list) |
| 72 |
msg_exit ("out of memory"); |
| 73 |
|
| 74 |
return o; |
| 75 |
} |
| 76 |
|
| 77 |
char * |
| 78 |
opt_csv (Opt o) |
| 79 |
{ |
| 80 |
ListIterator itr; |
| 81 |
char *item, *s; |
| 82 |
int strsize = 1; |
| 83 |
int n; |
| 84 |
|
| 85 |
NP_ASSERT (o->magic == OPT_MAGIC); |
| 86 |
if (!(itr = list_iterator_create (o->list))) |
| 87 |
msg_exit ("out of memory"); |
| 88 |
while ((item = list_next (itr))) |
| 89 |
strsize += strlen (item) + 1; |
| 90 |
list_iterator_reset (itr); |
| 91 |
if (!(s = malloc (strsize + list_count (o->list) - 1))) |
| 92 |
msg_exit ("out of memory"); |
| 93 |
n = 0; |
| 94 |
while ((item = list_next (itr))) { |
| 95 |
snprintf (s + n, strsize - n, "%s%s", n > 0 ? "," : "", item); |
| 96 |
n = strlen (s); |
| 97 |
} |
| 98 |
list_iterator_destroy (itr); |
| 99 |
|
| 100 |
return s; |
| 101 |
} |
| 102 |
|
| 103 |
static int |
| 104 |
_match_keyval (char *item, char *key) |
| 105 |
{ |
| 106 |
return (strcasecmp(item, key) == 0); |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
static int |
| 111 |
_match_key (char *item, char *key) |
| 112 |
{ |
| 113 |
char *p = strchr (item, '='); |
| 114 |
int n = p ? p - item : strlen (item); |
| 115 |
char *q = strchr (key, '='); |
| 116 |
int m = q ? q - key : strlen (key); |
| 117 |
|
| 118 |
return (m == n && strncasecmp(item, key, n) == 0); |
| 119 |
} |
| 120 |
|
| 121 |
int |
| 122 |
opt_addf (Opt o, const char *fmt, ...) |
| 123 |
{ |
| 124 |
va_list ap; |
| 125 |
char *csv, *item, *cpy; |
| 126 |
char *saveptr = NULL; |
| 127 |
int error; |
| 128 |
|
| 129 |
NP_ASSERT (o->magic == OPT_MAGIC); |
| 130 |
va_start (ap, fmt); |
| 131 |
error = vasprintf (&csv, fmt, ap); |
| 132 |
va_end (ap); |
| 133 |
if (error < 0) |
| 134 |
msg_exit ("out of memory"); |
| 135 |
|
| 136 |
item = strtok_r (csv, ",", &saveptr); |
| 137 |
while (item) { |
| 138 |
if (!(cpy = strdup (item))) |
| 139 |
msg_exit ("out of memory"); |
| 140 |
(void)list_delete_all (o->list, (ListFindF)_match_key, cpy); |
| 141 |
if (!list_append (o->list, cpy)) |
| 142 |
msg_exit ("out of memory"); |
| 143 |
item = strtok_r (NULL, ",", &saveptr); |
| 144 |
} |
| 145 |
free (csv); |
| 146 |
return 1; |
| 147 |
} |
| 148 |
|
| 149 |
/* return option value or empty string (not null) |
| 150 |
*/ |
| 151 |
static char * |
| 152 |
_optstr (char *s) |
| 153 |
{ |
| 154 |
char *p = strrchr (s, '='); |
| 155 |
|
| 156 |
return p ? p + 1 : s + strlen (s); |
| 157 |
} |
| 158 |
|
| 159 |
char * |
| 160 |
opt_find (Opt o, char *key) |
| 161 |
{ |
| 162 |
char *s; |
| 163 |
|
| 164 |
NP_ASSERT (o->magic == OPT_MAGIC); |
| 165 |
|
| 166 |
if (strchr (key, '=')) |
| 167 |
s = list_find_first (o->list, (ListFindF)_match_keyval, key); |
| 168 |
else |
| 169 |
s = list_find_first (o->list, (ListFindF)_match_key, key); |
| 170 |
|
| 171 |
return s ? _optstr (s) : NULL; |
| 172 |
} |
| 173 |
|
| 174 |
/* Returns number of deletions. |
| 175 |
*/ |
| 176 |
int |
| 177 |
opt_delete (Opt o, char *key) |
| 178 |
{ |
| 179 |
NP_ASSERT (o->magic == OPT_MAGIC); |
| 180 |
|
| 181 |
return list_delete_all (o->list, (ListFindF)_match_key, key); |
| 182 |
} |
| 183 |
|
| 184 |
int |
| 185 |
opt_vscanf (Opt o, const char *fmt, va_list ap) |
| 186 |
{ |
| 187 |
ListIterator itr; |
| 188 |
char *item; |
| 189 |
int ret = 0; |
| 190 |
|
| 191 |
NP_ASSERT (o->magic == OPT_MAGIC); |
| 192 |
|
| 193 |
if (!(itr = list_iterator_create (o->list))) |
| 194 |
msg_exit ("out of memory"); |
| 195 |
while ((item = list_next (itr))) { |
| 196 |
va_list vacpy; |
| 197 |
|
| 198 |
va_copy (vacpy, ap); |
| 199 |
ret = vsscanf (item, fmt, vacpy); |
| 200 |
va_end (vacpy); |
| 201 |
|
| 202 |
if (ret > 0) |
| 203 |
break; |
| 204 |
} |
| 205 |
list_iterator_destroy (itr); |
| 206 |
return ret; |
| 207 |
} |
| 208 |
|
| 209 |
int |
| 210 |
opt_scanf (Opt o, const char *fmt, ...) |
| 211 |
{ |
| 212 |
va_list ap; |
| 213 |
int ret; |
| 214 |
|
| 215 |
va_start(ap, fmt); |
| 216 |
ret = opt_vscanf (o, fmt, ap); |
| 217 |
va_end(ap); |
| 218 |
|
| 219 |
return ret; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
int |
| 224 |
opt_check_allowed_csv (Opt o, const char *csv) |
| 225 |
{ |
| 226 |
Opt allow; |
| 227 |
ListIterator itr; |
| 228 |
char *item, *cpy, *p; |
| 229 |
int ret = 0; |
| 230 |
|
| 231 |
NP_ASSERT (o->magic == OPT_MAGIC); |
| 232 |
|
| 233 |
allow = opt_create (); |
| 234 |
opt_addf (allow, "%s", csv); |
| 235 |
|
| 236 |
if (!(itr = list_iterator_create (o->list))) |
| 237 |
msg_exit ("out of memory"); |
| 238 |
while ((item = list_next (itr))) { |
| 239 |
if (!(cpy = strdup (item))) |
| 240 |
msg_exit ("out of memory"); |
| 241 |
if ((p = strchr (cpy, '='))) |
| 242 |
*p = '\0'; |
| 243 |
if (!opt_find (allow, cpy)) { |
| 244 |
ret = 1; |
| 245 |
free (cpy); |
| 246 |
break; |
| 247 |
} |
| 248 |
free (cpy); |
| 249 |
} |
| 250 |
list_iterator_destroy (itr); |
| 251 |
|
| 252 |
opt_destroy (allow); |
| 253 |
return ret; |
| 254 |
} |
| 255 |
|
| 256 |
/* |
| 257 |
* vi:tabstop=4 shiftwidth=4 expandtab |
| 258 |
*/ |