Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file utils.c |
| 2 | * Utility routines for printing common objects in the Osmocom world. */ |
| 3 | /* |
| 4 | * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 5 | * (C) 2013,2015 by sysmocom - s.f.m.c. GmbH |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | * |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along |
| 22 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 23 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include <stdint.h> |
| 28 | #include <inttypes.h> |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 29 | #include <string.h> |
Holger Hans Peter Freyther | b321b93 | 2012-09-11 10:39:29 +0200 | [diff] [blame] | 30 | #include <ctype.h> |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 31 | #include <limits.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 32 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 33 | #include <osmocom/core/linuxlist.h> |
| 34 | #include <osmocom/core/talloc.h> |
| 35 | #include <osmocom/core/timer.h> |
| 36 | #include <osmocom/core/rate_ctr.h> |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 37 | #include <osmocom/core/stat_item.h> |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 38 | #include <osmocom/core/utils.h> |
Harald Welte | 216338c | 2017-10-15 19:46:19 +0200 | [diff] [blame] | 39 | #include <osmocom/core/counter.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 40 | |
| 41 | #include <osmocom/vty/vty.h> |
| 42 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 43 | /*! \addtogroup rate_ctr |
| 44 | * @{ |
| 45 | */ |
| 46 | |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 47 | struct vty_out_context { |
| 48 | struct vty *vty; |
| 49 | const char *prefix; |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 50 | int max_level; |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static int rate_ctr_handler( |
| 54 | struct rate_ctr_group *ctrg, struct rate_ctr *ctr, |
| 55 | const struct rate_ctr_desc *desc, void *vctx_) |
| 56 | { |
| 57 | struct vty_out_context *vctx = vctx_; |
| 58 | struct vty *vty = vctx->vty; |
| 59 | |
| 60 | vty_out(vty, " %s%s: %8" PRIu64 " " |
| 61 | "(%" PRIu64 "/s %" PRIu64 "/m %" PRIu64 "/h %" PRIu64 "/d)%s", |
| 62 | vctx->prefix, desc->description, ctr->current, |
| 63 | ctr->intv[RATE_CTR_INTV_SEC].rate, |
| 64 | ctr->intv[RATE_CTR_INTV_MIN].rate, |
| 65 | ctr->intv[RATE_CTR_INTV_HOUR].rate, |
| 66 | ctr->intv[RATE_CTR_INTV_DAY].rate, |
| 67 | VTY_NEWLINE); |
| 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 72 | /*! print a rate counter group to given VTY |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 73 | * \param[in] vty The VTY to which it should be printed |
| 74 | * \param[in] prefix Any additional log prefix ahead of each line |
| 75 | * \param[in] ctrg Rate counter group to be printed |
| 76 | */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 77 | void vty_out_rate_ctr_group(struct vty *vty, const char *prefix, |
| 78 | struct rate_ctr_group *ctrg) |
| 79 | { |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 80 | struct vty_out_context vctx = {vty, prefix}; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 81 | |
| 82 | vty_out(vty, "%s%s:%s", prefix, ctrg->desc->group_description, VTY_NEWLINE); |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 83 | |
| 84 | rate_ctr_for_each_counter(ctrg, rate_ctr_handler, &vctx); |
| 85 | } |
| 86 | |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 87 | static char * |
| 88 | pad_append_str(char *s, const char *a, int minwidth) |
| 89 | { |
| 90 | s = talloc_asprintf_append(s, "%*s", minwidth, a); |
| 91 | OSMO_ASSERT(s); |
| 92 | return s; |
| 93 | } |
| 94 | |
| 95 | static char * |
Stefan Sperling | 4df2251 | 2018-05-25 13:05:58 +0200 | [diff] [blame] | 96 | pad_append_ctr(char *s, uint64_t ctr, int minwidth) |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 97 | { |
| 98 | s = talloc_asprintf_append(s, "%*" PRIu64, minwidth, ctr); |
| 99 | OSMO_ASSERT(s); |
| 100 | return s; |
| 101 | } |
| 102 | |
| 103 | static int rate_ctr_handler_fmt( |
| 104 | struct rate_ctr_group *ctrg, struct rate_ctr *ctr, |
| 105 | const struct rate_ctr_desc *desc, void *vctx_) |
| 106 | { |
| 107 | struct vty_out_context *vctx = vctx_; |
| 108 | struct vty *vty = vctx->vty; |
| 109 | const char *fmt = vctx->prefix; |
| 110 | char *s = talloc_strdup(vty, ""); |
| 111 | OSMO_ASSERT(s); |
| 112 | |
| 113 | while (*fmt) { |
| 114 | int ch, minwidth = 0, sign = 1; |
| 115 | char *p = strchr(fmt, '%'); |
| 116 | |
| 117 | if (p == NULL) { |
| 118 | /* No further % directives in format string. Copy rest verbatim and exit. */ |
| 119 | s = talloc_strdup_append_buffer(s, fmt); |
| 120 | OSMO_ASSERT(s); |
| 121 | break; |
| 122 | } else { |
| 123 | ptrdiff_t len; |
| 124 | |
| 125 | OSMO_ASSERT(p >= fmt); |
| 126 | len = p - fmt; |
| 127 | if (len) { |
| 128 | /* Copy bytes verbatim until next '%' byte. */ |
| 129 | s = talloc_strndup_append_buffer(s, fmt, len); |
| 130 | OSMO_ASSERT(s); |
| 131 | } |
| 132 | fmt = (const char *)(p + 1); /* skip past '%' */ |
| 133 | if (*fmt == '\0') |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | ch = *fmt++; |
| 138 | if (ch == '-' && isdigit(*fmt)) { |
| 139 | sign = -1; |
| 140 | ch = *fmt++; |
| 141 | } |
| 142 | while (isdigit(ch) && *fmt != '\0') { |
| 143 | minwidth *= 10; |
| 144 | minwidth += (ch - '0'); |
| 145 | ch = *fmt++; |
| 146 | } |
| 147 | minwidth *= sign; |
| 148 | |
| 149 | switch (ch) { |
| 150 | case '%': |
| 151 | s = talloc_asprintf_append(s, "%c", ch); |
| 152 | OSMO_ASSERT(s); |
| 153 | break; |
| 154 | case 'd': |
| 155 | s = pad_append_str(s, desc->description, minwidth); |
| 156 | break; |
| 157 | case 'n': |
| 158 | s = pad_append_str(s, desc->name, minwidth); |
| 159 | break; |
| 160 | case 'c': |
Stefan Sperling | 4df2251 | 2018-05-25 13:05:58 +0200 | [diff] [blame] | 161 | s = pad_append_ctr(s, ctr->current, minwidth); |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 162 | break; |
| 163 | case 'p': |
Stefan Sperling | 4df2251 | 2018-05-25 13:05:58 +0200 | [diff] [blame] | 164 | s = pad_append_ctr(s, ctr->previous, minwidth); |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 165 | break; |
| 166 | case 'S': |
Stefan Sperling | 4df2251 | 2018-05-25 13:05:58 +0200 | [diff] [blame] | 167 | s = pad_append_ctr(s, ctr->intv[RATE_CTR_INTV_SEC].rate, minwidth); |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 168 | break; |
| 169 | case 'M': |
Stefan Sperling | 4df2251 | 2018-05-25 13:05:58 +0200 | [diff] [blame] | 170 | s = pad_append_ctr(s, ctr->intv[RATE_CTR_INTV_MIN].rate, minwidth); |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 171 | break; |
| 172 | case 'H': |
Stefan Sperling | 4df2251 | 2018-05-25 13:05:58 +0200 | [diff] [blame] | 173 | s = pad_append_ctr(s, ctr->intv[RATE_CTR_INTV_HOUR].rate, minwidth); |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 174 | break; |
| 175 | case 'D': |
Stefan Sperling | 4df2251 | 2018-05-25 13:05:58 +0200 | [diff] [blame] | 176 | s = pad_append_ctr(s, ctr->intv[RATE_CTR_INTV_DAY].rate, minwidth); |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 177 | break; |
| 178 | default: |
| 179 | break; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | vty_out(vty, "%s%s", s, VTY_NEWLINE); |
| 184 | talloc_free(s); |
| 185 | |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | /*! print a rate counter group to given VTY, formatting the line for each counter according to a format string. |
| 190 | * |
| 191 | * The following format string directives are supported: |
| 192 | * - %d: The description of the counter |
| 193 | * - %n: The name of the counter |
| 194 | * - %c: The current value of the counter |
| 195 | * - %p: The previous value of the counter |
| 196 | * - %S: The interval of the counter in seconds |
| 197 | * - %M: The interval of the counter in minutes |
| 198 | * - %H: The interval of the counter in hours |
| 199 | * - %D: The interval of the counter in days |
| 200 | * - %%: Print a literal %. |
| 201 | * |
| 202 | * An optional number between % and the letter in a format directive may be used to set a minimum field width. |
| 203 | * If the expanded format directive is smaller than this width (according to strlen()) the string will be |
| 204 | * left-padded (if the number is positive) or right-padded (if the number is negative) with spaces. |
| 205 | * For example, "%25n" prints the counter name left-padded up to a minimum width of 25 columns. |
| 206 | * |
| 207 | * VTY_NEWLINE will be appended to the format string when it is printed. |
| 208 | * |
| 209 | * \param[in] vty The VTY to which it should be printed |
| 210 | * \param[in] ctrg Rate counter group to be printed |
| 211 | * \param[in] fmt A format which may contain the above directives. |
| 212 | */ |
| 213 | void vty_out_rate_ctr_group_fmt(struct vty *vty, const char *fmt, |
| 214 | struct rate_ctr_group *ctrg) |
| 215 | { |
| 216 | struct vty_out_context vctx = {vty, fmt}; |
Stefan Sperling | 97d3da2 | 2018-05-24 16:42:43 +0200 | [diff] [blame] | 217 | rate_ctr_for_each_counter(ctrg, rate_ctr_handler_fmt, &vctx); |
| 218 | } |
| 219 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 220 | static int rate_ctr_group_handler(struct rate_ctr_group *ctrg, void *vctx_) |
| 221 | { |
| 222 | struct vty_out_context *vctx = vctx_; |
| 223 | struct vty *vty = vctx->vty; |
| 224 | |
| 225 | if (ctrg->desc->class_id > vctx->max_level) |
| 226 | return 0; |
| 227 | |
| 228 | if (ctrg->idx) |
| 229 | vty_out(vty, "%s%s (%d):%s", vctx->prefix, |
| 230 | ctrg->desc->group_description, ctrg->idx, VTY_NEWLINE); |
| 231 | else |
| 232 | vty_out(vty, "%s%s:%s", vctx->prefix, |
| 233 | ctrg->desc->group_description, VTY_NEWLINE); |
| 234 | |
| 235 | rate_ctr_for_each_counter(ctrg, rate_ctr_handler, vctx); |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | /*! @} */ |
| 241 | |
| 242 | |
| 243 | /*! \addtogroup stats |
| 244 | * @{ |
| 245 | */ |
| 246 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 247 | static int osmo_stat_item_handler( |
| 248 | struct osmo_stat_item_group *statg, struct osmo_stat_item *item, void *vctx_) |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 249 | { |
| 250 | struct vty_out_context *vctx = vctx_; |
| 251 | struct vty *vty = vctx->vty; |
Jacob Erlbeck | af5bad5 | 2015-11-27 18:54:58 +0100 | [diff] [blame] | 252 | const char *unit = |
| 253 | item->desc->unit != OSMO_STAT_ITEM_NO_UNIT ? |
| 254 | item->desc->unit : ""; |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 255 | |
| 256 | vty_out(vty, " %s%s: %8" PRIi32 " %s%s", |
| 257 | vctx->prefix, item->desc->description, |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 258 | osmo_stat_item_get_last(item), |
Jacob Erlbeck | af5bad5 | 2015-11-27 18:54:58 +0100 | [diff] [blame] | 259 | unit, VTY_NEWLINE); |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 260 | |
| 261 | return 0; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 262 | } |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 263 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 264 | /*! print a stat item group to given VTY |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 265 | * \param[in] vty The VTY to which it should be printed |
| 266 | * \param[in] prefix Any additional log prefix ahead of each line |
| 267 | * \param[in] statg Stat item group to be printed |
| 268 | */ |
| 269 | void vty_out_stat_item_group(struct vty *vty, const char *prefix, |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 270 | struct osmo_stat_item_group *statg) |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 271 | { |
Jacob Erlbeck | aec583f | 2015-10-19 15:06:01 +0200 | [diff] [blame] | 272 | struct vty_out_context vctx = {vty, prefix}; |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 273 | |
| 274 | vty_out(vty, "%s%s:%s", prefix, statg->desc->group_description, |
| 275 | VTY_NEWLINE); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 276 | osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, &vctx); |
Jacob Erlbeck | 738d9e2 | 2015-10-06 15:21:56 +0200 | [diff] [blame] | 277 | } |
| 278 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 279 | static int osmo_stat_item_group_handler(struct osmo_stat_item_group *statg, void *vctx_) |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 280 | { |
| 281 | struct vty_out_context *vctx = vctx_; |
| 282 | struct vty *vty = vctx->vty; |
| 283 | |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 284 | if (statg->desc->class_id > vctx->max_level) |
| 285 | return 0; |
| 286 | |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 287 | if (statg->idx) |
| 288 | vty_out(vty, "%s%s (%d):%s", vctx->prefix, |
| 289 | statg->desc->group_description, statg->idx, |
| 290 | VTY_NEWLINE); |
| 291 | else |
| 292 | vty_out(vty, "%s%s:%s", vctx->prefix, |
| 293 | statg->desc->group_description, VTY_NEWLINE); |
| 294 | |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 295 | osmo_stat_item_for_each_item(statg, osmo_stat_item_handler, vctx); |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 300 | /*! @} */ |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 301 | |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 302 | /*! \addtogroup vty |
| 303 | * @{ |
| 304 | */ |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 305 | |
| 306 | static int handle_counter(struct osmo_counter *counter, void *vctx_) |
| 307 | { |
| 308 | struct vty_out_context *vctx = vctx_; |
| 309 | struct vty *vty = vctx->vty; |
Alexander Couzens | 3e432e1 | 2016-10-04 11:24:02 +0200 | [diff] [blame] | 310 | const char *description = counter->description; |
| 311 | |
| 312 | if (!counter->description) |
| 313 | description = counter->name; |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 314 | |
| 315 | vty_out(vty, " %s%s: %8lu%s", |
Alexander Couzens | 3e432e1 | 2016-10-04 11:24:02 +0200 | [diff] [blame] | 316 | vctx->prefix, description, |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 317 | osmo_counter_get(counter), VTY_NEWLINE); |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 322 | void vty_out_statistics_partial(struct vty *vty, const char *prefix, |
| 323 | int max_level) |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 324 | { |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 325 | struct vty_out_context vctx = {vty, prefix, max_level}; |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 326 | |
| 327 | vty_out(vty, "%sUngrouped counters:%s", prefix, VTY_NEWLINE); |
| 328 | osmo_counters_for_each(handle_counter, &vctx); |
| 329 | rate_ctr_for_each_group(rate_ctr_group_handler, &vctx); |
Jacob Erlbeck | fc9533d | 2015-10-29 00:55:58 +0100 | [diff] [blame] | 330 | osmo_stat_item_for_each_group(osmo_stat_item_group_handler, &vctx); |
Jacob Erlbeck | 7211fe1 | 2015-10-19 15:11:50 +0200 | [diff] [blame] | 331 | } |
| 332 | |
Jacob Erlbeck | 59b90bc | 2015-11-03 16:21:40 +0100 | [diff] [blame] | 333 | void vty_out_statistics_full(struct vty *vty, const char *prefix) |
| 334 | { |
| 335 | vty_out_statistics_partial(vty, prefix, INT_MAX); |
| 336 | } |
| 337 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 338 | /*! Generate a VTY command string from value_string */ |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 339 | char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals, |
| 340 | const char *prefix, const char *sep, |
| 341 | const char *end, int do_lower) |
| 342 | { |
| 343 | int len = 0, offset = 0, ret, rem; |
Jacob Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 344 | int size = strlen(prefix) + strlen(end); |
| 345 | int sep_len = strlen(sep); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 346 | const struct value_string *vs; |
| 347 | char *str; |
| 348 | |
| 349 | for (vs = vals; vs->value || vs->str; vs++) |
Jacob Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 350 | size += strlen(vs->str) + sep_len; |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 351 | |
| 352 | rem = size; |
| 353 | str = talloc_zero_size(ctx, size); |
| 354 | if (!str) |
| 355 | return NULL; |
| 356 | |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 357 | ret = snprintf(str + offset, rem, "%s", prefix); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 358 | if (ret < 0) |
| 359 | goto err; |
| 360 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 361 | |
| 362 | for (vs = vals; vs->value || vs->str; vs++) { |
| 363 | if (vs->str) { |
| 364 | int j, name_len = strlen(vs->str)+1; |
| 365 | char name[name_len]; |
| 366 | |
| 367 | for (j = 0; j < name_len; j++) |
| 368 | name[j] = do_lower ? |
| 369 | tolower(vs->str[j]) : vs->str[j]; |
| 370 | |
| 371 | name[name_len-1] = '\0'; |
| 372 | ret = snprintf(str + offset, rem, "%s%s", name, sep); |
| 373 | if (ret < 0) |
| 374 | goto err; |
| 375 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 376 | } |
| 377 | } |
Jacob Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 378 | offset -= sep_len; /* to remove the trailing sep */ |
| 379 | rem += sep_len; |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 380 | |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 381 | ret = snprintf(str + offset, rem, "%s", end); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 382 | if (ret < 0) |
| 383 | goto err; |
| 384 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 385 | err: |
| 386 | str[size-1] = '\0'; |
| 387 | return str; |
| 388 | } |
| 389 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 390 | /*! @} */ |