1/* gmarkup.h - Simple XML-like string parser/writer
2 *
3 * Copyright 2000 Red Hat, Inc.
4 *
5 * SPDX-License-Identifier: LGPL-2.1-or-later
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library 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 GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, see <http://www.gnu.org/licenses/>.
19 */
20
21#ifndef __G_MARKUP_H__
22#define __G_MARKUP_H__
23
24#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
25#error "Only <glib.h> can be included directly."
26#endif
27
28#include <stdarg.h>
29
30#include <glib/gerror.h>
31#include <glib/gslist.h>
32
33G_BEGIN_DECLS
34
35/**
36 * GMarkupError:
37 * @G_MARKUP_ERROR_BAD_UTF8: text being parsed was not valid UTF-8
38 * @G_MARKUP_ERROR_EMPTY: document contained nothing, or only whitespace
39 * @G_MARKUP_ERROR_PARSE: document was ill-formed
40 * @G_MARKUP_ERROR_UNKNOWN_ELEMENT: error should be set by #GMarkupParser
41 * functions; element wasn't known
42 * @G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE: error should be set by #GMarkupParser
43 * functions; attribute wasn't known
44 * @G_MARKUP_ERROR_INVALID_CONTENT: error should be set by #GMarkupParser
45 * functions; content was invalid
46 * @G_MARKUP_ERROR_MISSING_ATTRIBUTE: error should be set by #GMarkupParser
47 * functions; a required attribute was missing
48 *
49 * Error codes returned by markup parsing.
50 */
51typedef enum
52{
53 G_MARKUP_ERROR_BAD_UTF8,
54 G_MARKUP_ERROR_EMPTY,
55 G_MARKUP_ERROR_PARSE,
56 /* The following are primarily intended for specific GMarkupParser
57 * implementations to set.
58 */
59 G_MARKUP_ERROR_UNKNOWN_ELEMENT,
60 G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
61 G_MARKUP_ERROR_INVALID_CONTENT,
62 G_MARKUP_ERROR_MISSING_ATTRIBUTE
63} GMarkupError;
64
65/**
66 * G_MARKUP_ERROR:
67 *
68 * Error domain for markup parsing.
69 * Errors in this domain will be from the #GMarkupError enumeration.
70 * See #GError for information on error domains.
71 */
72#define G_MARKUP_ERROR g_markup_error_quark ()
73
74GLIB_AVAILABLE_IN_ALL
75GQuark g_markup_error_quark (void);
76
77/**
78 * GMarkupParseFlags:
79 * @G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG: Flag you should not use
80 * @G_MARKUP_TREAT_CDATA_AS_TEXT: When this flag is set, CDATA marked
81 * sections are not passed literally to the @passthrough function of
82 * the parser. Instead, the content of the section (without the
83 * `<![CDATA[` and `]]>`) is
84 * passed to the @text function. This flag was added in GLib 2.12
85 * @G_MARKUP_PREFIX_ERROR_POSITION: Normally errors caught by GMarkup
86 * itself have line/column information prefixed to them to let the
87 * caller know the location of the error. When this flag is set the
88 * location information is also prefixed to errors generated by the
89 * #GMarkupParser implementation functions
90 * @G_MARKUP_IGNORE_QUALIFIED: Ignore (don't report) qualified
91 * attributes and tags, along with their contents. A qualified
92 * attribute or tag is one that contains ':' in its name (ie: is in
93 * another namespace). Since: 2.40.
94 *
95 * Flags that affect the behaviour of the parser.
96 */
97typedef enum
98{
99 /**
100 * G_MARKUP_DEFAULT_FLAGS:
101 *
102 * No special behaviour.
103 *
104 * Since: 2.74
105 */
106 G_MARKUP_DEFAULT_FLAGS GLIB_AVAILABLE_ENUMERATOR_IN_2_74 = 0,
107 G_MARKUP_DO_NOT_USE_THIS_UNSUPPORTED_FLAG = 1 << 0,
108 G_MARKUP_TREAT_CDATA_AS_TEXT = 1 << 1,
109 G_MARKUP_PREFIX_ERROR_POSITION = 1 << 2,
110 G_MARKUP_IGNORE_QUALIFIED = 1 << 3
111} G_GNUC_FLAG_ENUM GMarkupParseFlags;
112
113/**
114 * GMarkupParseContext:
115 *
116 * A parse context is used to parse a stream of bytes that
117 * you expect to contain marked-up text.
118 *
119 * See g_markup_parse_context_new(), #GMarkupParser, and so
120 * on for more details.
121 */
122typedef struct _GMarkupParseContext GMarkupParseContext;
123typedef struct _GMarkupParser GMarkupParser;
124
125/**
126 * GMarkupParser:
127 * @start_element: Callback to invoke when the opening tag of an element
128 * is seen. The callback's @attribute_names and @attribute_values parameters
129 * are %NULL-terminated.
130 * @end_element: Callback to invoke when the closing tag of an element
131 * is seen. Note that this is also called for empty tags like
132 * `<empty/>`.
133 * @text: Callback to invoke when some text is seen (text is always
134 * inside an element). Note that the text of an element may be spread
135 * over multiple calls of this function. If the
136 * %G_MARKUP_TREAT_CDATA_AS_TEXT flag is set, this function is also
137 * called for the content of CDATA marked sections.
138 * @passthrough: Callback to invoke for comments, processing instructions
139 * and doctype declarations; if you're re-writing the parsed document,
140 * write the passthrough text back out in the same position. If the
141 * %G_MARKUP_TREAT_CDATA_AS_TEXT flag is not set, this function is also
142 * called for CDATA marked sections.
143 * @error: Callback to invoke when an error occurs.
144 *
145 * Any of the fields in #GMarkupParser can be %NULL, in which case they
146 * will be ignored. Except for the @error function, any of these callbacks
147 * can set an error; in particular the %G_MARKUP_ERROR_UNKNOWN_ELEMENT,
148 * %G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE, and %G_MARKUP_ERROR_INVALID_CONTENT
149 * errors are intended to be set from these callbacks. If you set an error
150 * from a callback, g_markup_parse_context_parse() will report that error
151 * back to its caller.
152 *
153 * Refer to the [GMarkup](../glib/markup.html) documentation to understand
154 * the scope and limitations of `GMarkupParser`. In particular, it is not a
155 * full XML parser and it must not be used to process untrusted data.
156 */
157struct _GMarkupParser
158{
159 /* Called for open tags <foo bar="baz"> */
160 void (*start_element) (GMarkupParseContext *context,
161 const gchar *element_name,
162 const gchar **attribute_names,
163 const gchar **attribute_values,
164 gpointer user_data,
165 GError **error);
166
167 /* Called for close tags </foo> */
168 void (*end_element) (GMarkupParseContext *context,
169 const gchar *element_name,
170 gpointer user_data,
171 GError **error);
172
173 /* Called for character data */
174 /* text is not nul-terminated */
175 void (*text) (GMarkupParseContext *context,
176 const gchar *text,
177 gsize text_len,
178 gpointer user_data,
179 GError **error);
180
181 /* Called for strings that should be re-saved verbatim in this same
182 * position, but are not otherwise interpretable. At the moment
183 * this includes comments and processing instructions.
184 */
185 /* text is not nul-terminated. */
186 void (*passthrough) (GMarkupParseContext *context,
187 const gchar *passthrough_text,
188 gsize text_len,
189 gpointer user_data,
190 GError **error);
191
192 /* Called on error, including one set by other
193 * methods in the vtable. The GError should not be freed.
194 */
195 void (*error) (GMarkupParseContext *context,
196 GError *error,
197 gpointer user_data);
198};
199
200GLIB_AVAILABLE_IN_ALL
201GMarkupParseContext *g_markup_parse_context_new (const GMarkupParser *parser,
202 GMarkupParseFlags flags,
203 gpointer user_data,
204 GDestroyNotify user_data_dnotify);
205GLIB_AVAILABLE_IN_2_36
206GMarkupParseContext *g_markup_parse_context_ref (GMarkupParseContext *context);
207GLIB_AVAILABLE_IN_2_36
208void g_markup_parse_context_unref (GMarkupParseContext *context);
209GLIB_AVAILABLE_IN_ALL
210void g_markup_parse_context_free (GMarkupParseContext *context);
211GLIB_AVAILABLE_IN_ALL
212gboolean g_markup_parse_context_parse (GMarkupParseContext *context,
213 const gchar *text,
214 gssize text_len,
215 GError **error);
216GLIB_AVAILABLE_IN_ALL
217void g_markup_parse_context_push (GMarkupParseContext *context,
218 const GMarkupParser *parser,
219 gpointer user_data);
220GLIB_AVAILABLE_IN_ALL
221gpointer g_markup_parse_context_pop (GMarkupParseContext *context);
222
223GLIB_AVAILABLE_IN_ALL
224gboolean g_markup_parse_context_end_parse (GMarkupParseContext *context,
225 GError **error);
226GLIB_AVAILABLE_IN_ALL
227const gchar * g_markup_parse_context_get_element (GMarkupParseContext *context);
228GLIB_AVAILABLE_IN_ALL
229const GSList * g_markup_parse_context_get_element_stack (GMarkupParseContext *context);
230
231/* For user-constructed error messages, has no precise semantics */
232GLIB_AVAILABLE_IN_ALL
233void g_markup_parse_context_get_position (GMarkupParseContext *context,
234 gint *line_number,
235 gint *char_number);
236GLIB_AVAILABLE_IN_2_88
237gsize g_markup_parse_context_get_offset (GMarkupParseContext *context);
238
239GLIB_AVAILABLE_IN_2_88
240void g_markup_parse_context_get_tag_start (GMarkupParseContext *context,
241 gsize *line_number,
242 gsize *char_number,
243 gsize *offset);
244GLIB_AVAILABLE_IN_2_90
245void g_markup_parse_context_get_attribute_position
246 (GMarkupParseContext *context,
247 unsigned int attr,
248 size_t *start_lines,
249 size_t *start_chars,
250 size_t *start_offset,
251 size_t *end_lines,
252 size_t *end_chars,
253 size_t *end_offset);
254
255GLIB_AVAILABLE_IN_ALL
256gpointer g_markup_parse_context_get_user_data (GMarkupParseContext *context);
257
258/* useful when saving */
259GLIB_AVAILABLE_IN_ALL
260gchar* g_markup_escape_text (const gchar *text,
261 gssize length);
262
263GLIB_AVAILABLE_IN_ALL
264gchar *g_markup_printf_escaped (const char *format,
265 ...) G_GNUC_PRINTF (1, 2);
266GLIB_AVAILABLE_IN_ALL
267gchar *g_markup_vprintf_escaped (const char *format,
268 va_list args) G_GNUC_PRINTF(1, 0);
269
270typedef enum
271{
272 G_MARKUP_COLLECT_INVALID,
273 G_MARKUP_COLLECT_STRING,
274 G_MARKUP_COLLECT_STRDUP,
275 G_MARKUP_COLLECT_BOOLEAN,
276 G_MARKUP_COLLECT_TRISTATE,
277
278 G_MARKUP_COLLECT_OPTIONAL = (1 << 16)
279} GMarkupCollectType;
280
281
282/* useful from start_element */
283GLIB_AVAILABLE_IN_ALL
284gboolean g_markup_collect_attributes (const gchar *element_name,
285 const gchar **attribute_names,
286 const gchar **attribute_values,
287 GError **error,
288 GMarkupCollectType first_type,
289 const gchar *first_attr,
290 ...);
291
292G_END_DECLS
293
294#endif /* __G_MARKUP_H__ */
295