^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include "util/debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include "util/parse-sublevel-options.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) static int parse_one_sublevel_option(const char *str,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) struct sublevel_option *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) struct sublevel_option *opt = opts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) char *vstr, *s = strdup(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) int v = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) if (!s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) pr_err("no memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) vstr = strchr(s, '=');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) if (vstr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *vstr++ = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) while (opt->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (!strcmp(s, opt->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) opt++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (!opt->name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) pr_err("Unknown option name '%s'\n", s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) free(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if (vstr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) v = atoi(vstr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) *opt->value_ptr = v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) free(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* parse options like --foo a=<n>,b,c... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int perf_parse_sublevel_options(const char *str, struct sublevel_option *opts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) char *s = strdup(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) char *p = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (!s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) pr_err("no memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) p = strtok(s, ",");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) while (p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) ret = parse_one_sublevel_option(p, opts);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) free(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) p = strtok(NULL, ",");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) free(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }