Debian Codegraph @

src/ws/cockpittemplate.c

raw
1 /*
2 * Copyright (C) 2014 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 /* This gets logged as part of the (more verbose) protocol logging */
9 #ifdef G_LOG_DOMAIN
10 #undef G_LOG_DOMAIN
11 #endif
12 #define G_LOG_DOMAIN "cockpit-protocol"
13
14 #include "cockpittemplate.h"
15
16 #include "cockpitjson.h"
17
18 #include <glib.h>
19
20 #include <string.h>
21
22 #define IS_WORDCHAR(c) (g_ascii_isalnum (c) || (c) == '_')
23
24 static gchar *
25 find_variable (const gchar *data,
26 const gchar *end,
27 const gchar **before,
28 const gchar **after)
29 {
30 const gchar *a, *b, *c;
31
32 g_return_val_if_fail (before != NULL, NULL);
33 g_return_val_if_fail (after != NULL, NULL);
34 g_return_val_if_fail (data <= end, NULL);
35
36 while ((a = memmem (data, end - data, "${", 2)))
37 {
38 /*
39 * Found "${". Scan for a valid variable name:
40 *
41 * Some text ${variable_name} trailing.
42 * a b c
43 *
44 * a = "${"
45 * b = first char of variable name
46 * c = should be '}'
47 */
48 b = a + 2;
49
50 for (c = b; c < end && IS_WORDCHAR (*c); c++)
51 ;
52
53 if (b < c && c < end && *c == '}')
54 {
55 *before = a;
56 *after = c + 1;
57 return g_strndup (b, c - b);
58 }
59
60 data = c;
61 }
62
63 return NULL;
64 }
65
66 GList *
67 cockpit_template_expand (GBytes *input,
68 CockpitTemplateFunc func,
69 gpointer user_data)
70 {
71 GList *output = NULL;
72 const gchar *data;
73 const gchar *end;
74 const gchar *before;
75 const gchar *after;
76 GBytes *bytes;
77 gchar *name;
78 gboolean escaped;
79 gint before_len;
80
81 g_return_val_if_fail (func != NULL, NULL);
82
83 data = g_bytes_get_data (input, NULL);
84 end = data + g_bytes_get_size (input);
85
86 for (;;)
87 {
88 escaped = FALSE;
89 name = find_variable (data, end, &before, &after);
90 if (name == NULL)
91 break;
92
93 if (before != data)
94 {
95 g_assert (before > data);
96
97 /* Check if the char before the match is the escape char '/' */
98 before_len = before - data;
99 if (data[before_len - 1] == '\\')
100 {
101 escaped = TRUE;
102 before_len--;
103 }
104
105 bytes = g_bytes_new_with_free_func (data, before_len,
106 (GDestroyNotify)g_bytes_unref,
107 g_bytes_ref (input));
108 output = g_list_prepend (output, bytes);
109 }
110
111 if (!escaped)
112 bytes = (func) (name, user_data);
113 else
114 bytes = NULL; // Set bytes to null so it's treated as skipped
115 g_free (name);
116
117 if (!bytes)
118 {
119 g_assert (after > before);
120 bytes = g_bytes_new_with_free_func (before, after - before,
121 (GDestroyNotify)g_bytes_unref,
122 g_bytes_ref (input));
123 }
124 if (g_bytes_get_size (bytes) > 0)
125 output = g_list_prepend (output, bytes);
126 else
127 g_bytes_unref (bytes);
128
129 g_assert (after <= end);
130 data = after;
131 }
132
133 if (data != end)
134 {
135 g_assert (end > data);
136 bytes = g_bytes_new_with_free_func (data, end - data,
137 (GDestroyNotify)g_bytes_unref,
138 g_bytes_ref (input));
139 output = g_list_prepend (output, bytes);
140 }
141
142 return g_list_reverse (output);
143 }