1 /* Parse JSON files using the JSMN parser. */
4 * Copyright (c) 2014, Intel Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
46 static char *mapfile(const char *fn, size_t *size)
48 unsigned ps = sysconf(_SC_PAGESIZE);
52 int fd = open(fn, O_RDONLY);
54 if (fd < 0 && verbose > 0 && fn) {
55 pr_err("Error opening events file '%s': %s\n", fn,
66 (st.st_size + ps - 1) & ~(ps - 1),
67 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
68 if (map == MAP_FAILED)
75 static void unmapfile(char *map, size_t size)
77 unsigned ps = sysconf(_SC_PAGESIZE);
78 munmap(map, roundup(size, ps));
82 * Parse json file using jsmn. Return array of tokens,
83 * and mapped file. Caller needs to free array.
85 jsmntok_t *parse_json(const char *fn, char **map, size_t *size, int *len)
92 *map = mapfile(fn, size);
101 res = jsmn_parse(&parser, *map, *size, tokens,
102 sz / sizeof(jsmntok_t));
103 if (res != JSMN_SUCCESS) {
104 pr_err("%s: json error %s\n", fn, jsmn_strerror(res));
108 *len = parser.toknext;
113 unmapfile(*map, *size);
117 void free_json(char *map, size_t size, jsmntok_t *tokens)
120 unmapfile(map, size);
123 static int countchar(char *map, char c, int end)
127 for (i = 0; i < end; i++)
133 /* Return line number of a jsmn token */
134 int json_line(char *map, jsmntok_t *t)
136 return countchar(map, '\n', t->start) + 1;
139 static const char * const jsmn_types[] = {
140 [JSMN_PRIMITIVE] = "primitive",
141 [JSMN_ARRAY] = "array",
142 [JSMN_OBJECT] = "object",
143 [JSMN_STRING] = "string"
146 #define LOOKUP(a, i) ((i) < (sizeof(a)/sizeof(*(a))) ? ((a)[i]) : "?")
148 /* Return type name of a jsmn token */
149 const char *json_name(jsmntok_t *t)
151 return LOOKUP(jsmn_types, t->type);
154 int json_len(jsmntok_t *t)
156 return t->end - t->start;
159 /* Is string t equal to s? */
160 int json_streq(char *map, jsmntok_t *t, const char *s)
162 unsigned len = json_len(t);
163 return len == strlen(s) && !strncasecmp(map + t->start, s, len);