Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 1 | /* utility routines for printing common objects in the Osmocom world */ |
| 2 | |
| 3 | /* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org> |
| 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | #include <stdint.h> |
| 24 | #include <inttypes.h> |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 25 | #include <string.h> |
Holger Hans Peter Freyther | b321b93 | 2012-09-11 10:39:29 +0200 | [diff] [blame] | 26 | #include <ctype.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 27 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 28 | #include <osmocom/core/linuxlist.h> |
| 29 | #include <osmocom/core/talloc.h> |
| 30 | #include <osmocom/core/timer.h> |
| 31 | #include <osmocom/core/rate_ctr.h> |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 32 | #include <osmocom/core/utils.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 33 | |
| 34 | #include <osmocom/vty/vty.h> |
| 35 | |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 36 | /* \file utils.c */ |
| 37 | |
| 38 | /*! \addtogroup rate_ctr |
| 39 | * @{ |
| 40 | */ |
| 41 | |
| 42 | /*! \brief print a rate counter group to given VTY |
| 43 | * \param[in] vty The VTY to which it should be printed |
| 44 | * \param[in] prefix Any additional log prefix ahead of each line |
| 45 | * \param[in] ctrg Rate counter group to be printed |
| 46 | */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 47 | void vty_out_rate_ctr_group(struct vty *vty, const char *prefix, |
| 48 | struct rate_ctr_group *ctrg) |
| 49 | { |
| 50 | unsigned int i; |
| 51 | |
| 52 | vty_out(vty, "%s%s:%s", prefix, ctrg->desc->group_description, VTY_NEWLINE); |
| 53 | for (i = 0; i < ctrg->desc->num_ctr; i++) { |
| 54 | struct rate_ctr *ctr = &ctrg->ctr[i]; |
| 55 | vty_out(vty, " %s%s: %8" PRIu64 " " |
| 56 | "(%" PRIu64 "/s %" PRIu64 "/m %" PRIu64 "/h %" PRIu64 "/d)%s", |
| 57 | prefix, ctrg->desc->ctr_desc[i].description, ctr->current, |
| 58 | ctr->intv[RATE_CTR_INTV_SEC].rate, |
| 59 | ctr->intv[RATE_CTR_INTV_MIN].rate, |
| 60 | ctr->intv[RATE_CTR_INTV_HOUR].rate, |
| 61 | ctr->intv[RATE_CTR_INTV_DAY].rate, |
| 62 | VTY_NEWLINE); |
| 63 | }; |
| 64 | } |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 65 | |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 66 | /*! \brief Generate a VTY command string from value_string */ |
| 67 | char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals, |
| 68 | const char *prefix, const char *sep, |
| 69 | const char *end, int do_lower) |
| 70 | { |
| 71 | int len = 0, offset = 0, ret, rem; |
Jacob Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 72 | int size = strlen(prefix) + strlen(end); |
| 73 | int sep_len = strlen(sep); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 74 | const struct value_string *vs; |
| 75 | char *str; |
| 76 | |
| 77 | for (vs = vals; vs->value || vs->str; vs++) |
Jacob Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 78 | size += strlen(vs->str) + sep_len; |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 79 | |
| 80 | rem = size; |
| 81 | str = talloc_zero_size(ctx, size); |
| 82 | if (!str) |
| 83 | return NULL; |
| 84 | |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 85 | ret = snprintf(str + offset, rem, "%s", prefix); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 86 | if (ret < 0) |
| 87 | goto err; |
| 88 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 89 | |
| 90 | for (vs = vals; vs->value || vs->str; vs++) { |
| 91 | if (vs->str) { |
| 92 | int j, name_len = strlen(vs->str)+1; |
| 93 | char name[name_len]; |
| 94 | |
| 95 | for (j = 0; j < name_len; j++) |
| 96 | name[j] = do_lower ? |
| 97 | tolower(vs->str[j]) : vs->str[j]; |
| 98 | |
| 99 | name[name_len-1] = '\0'; |
| 100 | ret = snprintf(str + offset, rem, "%s%s", name, sep); |
| 101 | if (ret < 0) |
| 102 | goto err; |
| 103 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 104 | } |
| 105 | } |
Jacob Erlbeck | cd195fa | 2013-08-06 14:29:15 +0200 | [diff] [blame] | 106 | offset -= sep_len; /* to remove the trailing sep */ |
| 107 | rem += sep_len; |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 108 | |
Jacob Erlbeck | ae15a2c | 2013-08-06 14:29:14 +0200 | [diff] [blame] | 109 | ret = snprintf(str + offset, rem, "%s", end); |
Harald Welte | fab0ae9 | 2012-08-17 12:17:38 +0200 | [diff] [blame] | 110 | if (ret < 0) |
| 111 | goto err; |
| 112 | OSMO_SNPRINTF_RET(ret, rem, offset, len); |
| 113 | err: |
| 114 | str[size-1] = '\0'; |
| 115 | return str; |
| 116 | } |
| 117 | |
| 118 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 119 | /*! @} */ |