Debian Codegraph @

madrisan-nagios-plugins-linux/nagios-plugins-linux/lib/json_helpers.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-3.0-or-later
2 /*
3 * License: GPLv3+
4 * Copyright (c) 2020 Davide Madrisan <[email protected]>
5 *
6 * A library containing some helper functions for parsing data in JSON format.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "jsmn.h"
26 #include "json_helpers.h"
27 #include "logging.h"
28 #include "system.h"
29 #include "xalloc.h"
30
31 int
32 json_token_streq (const char *json, jsmntok_t *tok, const char *s)
33 {
34 if (tok->type == JSMN_STRING && (int) strlen (s) == tok->end - tok->start &&
35 strncmp (json + tok->start, s, tok->end - tok->start) == 0)
36 return 0;
37
38 return -1;
39 }
40
41 char *
42 json_token_tostr (char *json, jsmntok_t *t)
43 {
44 return xsubstrdup (json + t->start, t->end - t->start);
45 }
46
47 jsmntok_t *
48 json_tokenise (const char *json, size_t *ntoken)
49 {
50 jsmn_parser parser;
51 int r, ret;
52
53 assert (NULL != json);
54
55 jsmn_init (&parser);
56 r = jsmn_parse (&parser, json, strlen (json), NULL, 0);
57 if (r < 0)
58 return NULL;
59
60 dbg ("number of json tokens: %d\n", r);
61 jsmntok_t *tokens = xnmalloc (r, sizeof (jsmntok_t));
62
63 jsmn_init (&parser);
64 ret = jsmn_parse (&parser, json, strlen (json), tokens, r);
65 assert (ret >= 0);
66
67 *ntoken = r;
68 return tokens;
69 }
70
71 static int
72 json_dump (const char *json, jsmntok_t *t, size_t count, int indent,
73 FILE *stream)
74 {
75 int i, j, k;
76
77 if (t->type == JSMN_PRIMITIVE)
78 {
79 fprintf (stream, "%.*s", t->end - t->start, json + t->start);
80 if (t->size == 0)
81 fprintf (stream, "\n");
82 return 1;
83 }
84 else if (t->type == JSMN_STRING)
85 {
86 fprintf (stream, "%.*s", t->end - t->start, json + t->start);
87 if (t->size == 0)
88 fprintf (stream, "\n");
89 return 1;
90 }
91 else if (t->type == JSMN_OBJECT)
92 {
93 j = 0;
94 jsmntok_t *token;
95
96 fprintf (stream, "\n");
97
98 for (i = 0; i < t->size; i++)
99 {
100 for (k = 0; k < indent; k++)
101 fprintf (stream, " ");
102
103 token = t + 1 + j;
104 j += json_dump (json, token, count - j, indent + 1, stream);
105
106 /* get the value of the previous JSON label */
107 if (token->size > 0)
108 {
109 fprintf (stream, ": ");
110 j += json_dump (json, t + 1 + j, count - j, indent + 1, stream);
111 }
112 }
113 return j + 1;
114 }
115 else if (t->type == JSMN_ARRAY)
116 {
117 j = 0;
118 fprintf (stream, "\n");
119
120 for (i = 0; i < t->size; i++)
121 {
122 for (k = 0; k < indent - 1; k++)
123 fprintf (stream, " ");
124
125 fprintf (stream, " - ");
126 j += json_dump (json, t + 1 + j, count - j, indent + 1, stream);
127 }
128
129 return j + 1;
130 }
131
132 return 0;
133 }
134
135 void
136 json_dump_pretty (const char *json, char **dump, int indent)
137 {
138 size_t ntoken, size;
139 FILE *stream = open_memstream (dump, &size);
140
141 jsmntok_t *tok = json_tokenise (json, &ntoken);
142 json_dump (json, tok, tok->size, indent, stream);
143
144 fclose (stream);
145 }
146
147 int
148 json_search (const char *json, const char *path, char **value)
149 {
150 char *p, *object, *label, *saveptr;
151 size_t i, ntoken;
152 bool found_label = false, found_object = false;
153
154 dbg ("checking for %s in the json data...\n", path);
155 jsmntok_t *tok = json_tokenise (json, &ntoken);
156
157 p = xstrdup (path);
158 object = strtok_r (p, ".", &saveptr);
159 label = strtok_r (0, ".", &saveptr);
160
161 if (object == NULL || label == NULL)
162 return -1;
163
164 dbg ("ckeckin for label \"%s\" in the object \"%s\"\n", label, object);
165
166 /* FIXME: the search algorithm sucks :( */
167 for (i = 0; i < ntoken; i++)
168 {
169 if (0 == json_token_streq (json, &tok[i], object))
170 {
171 dbg ("found object \"%s\"\n", object);
172 found_object = true;
173 }
174 else if (found_object && (tok[i].type == JSMN_STRING)
175 && (0 == json_token_streq (json, &tok[i], label)))
176 {
177 dbg ("found label \"%s\"\n", label);
178 found_label = true;
179 /* get the associated value */
180 size_t strsize = tok[i + 1].end - tok[i + 1].start;
181 *value = xmalloc (strsize + 1);
182 memcpy (*value, json + tok[i + 1].start, strsize);
183 break;
184 }
185 }
186 if (found_label && found_object && NULL != *value)
187 dbg ("found label \"%s\" in the object \"%s\" with value \"%s\"\n", label,
188 object, *value);
189
190 return 0;
191 }