1 /* gmarkup.c - Simple XML-like parser
3 * Copyright 2000 Red Hat, Inc.
5 * GLib is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * GLib is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with GLib; see the file COPYING.LIB. If not,
17 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
32 g_markup_error_quark ()
34 static GQuark error_quark = 0;
37 error_quark = g_quark_from_static_string ("g-markup-error-quark");
42 typedef struct _GMarkupAttribute GMarkupAttribute;
44 struct _GMarkupAttribute
50 static GMarkupAttribute*
51 attribute_new (const gchar *name, const gchar *value)
53 GMarkupAttribute *attr;
55 attr = g_new (GMarkupAttribute, 1);
57 /* name/value are allowed to be NULL */
58 attr->name = g_strdup (name);
59 attr->value = g_strdup (value);
65 attribute_free (GMarkupAttribute *attr)
75 STATE_AFTER_OPEN_ANGLE,
76 STATE_AFTER_CLOSE_ANGLE,
77 STATE_AFTER_ELISION_SLASH, /* the slash that obviates need for end element */
78 STATE_INSIDE_OPEN_TAG_NAME,
79 STATE_INSIDE_ATTRIBUTE_NAME,
80 STATE_BETWEEN_ATTRIBUTES,
81 STATE_AFTER_ATTRIBUTE_EQUALS_SIGN,
82 STATE_INSIDE_ATTRIBUTE_VALUE,
84 STATE_AFTER_CLOSE_TAG_SLASH,
85 STATE_INSIDE_CLOSE_TAG_NAME,
86 STATE_INSIDE_PASSTHROUGH,
90 struct _GMarkupParseContext
92 const GMarkupParser *parser;
94 GMarkupParseFlags flags;
100 GDestroyNotify dnotify;
102 /* A piece of character data or an element that
103 * hasn't "ended" yet so we haven't yet called
104 * the callback for it.
106 GString *partial_chunk;
108 GMarkupParseState state;
112 const gchar *current_text;
113 gint current_text_len;
114 const gchar *current_text_end;
116 GString *leftover_char_portion;
118 /* used to save the start of the last interesting thingy */
123 guint document_empty : 1;
128 * g_markup_parse_context_new:
129 * @parser: a #GMarkupParser
130 * @flags: one or more #GMarkupParseFlags
131 * @user_data: user data to pass to #GMarkupParser functions
132 * @user_data_dnotify: user data destroy notifier called when the parse context is freed
134 * Creates a new parse context. A parse context is used to parse
135 * marked-up documents. You can feed any number of documents into
136 * a context, as long as no errors occur; once an error occurs,
137 * the parse context can't continue to parse text (you have to free it
138 * and create a new parse context).
140 * Return value: a new #GMarkupParseContext
142 GMarkupParseContext *
143 g_markup_parse_context_new (const GMarkupParser *parser,
144 GMarkupParseFlags flags,
146 GDestroyNotify user_data_dnotify)
148 GMarkupParseContext *context;
150 g_return_val_if_fail (parser != NULL, NULL);
152 context = g_new (GMarkupParseContext, 1);
154 context->parser = parser;
155 context->flags = flags;
156 context->user_data = user_data;
157 context->dnotify = user_data_dnotify;
159 context->line_number = 1;
160 context->char_number = 1;
162 context->partial_chunk = NULL;
164 context->state = STATE_START;
165 context->tag_stack = NULL;
166 context->attributes = NULL;
168 context->current_text = NULL;
169 context->current_text_len = -1;
170 context->current_text_end = NULL;
171 context->leftover_char_portion = NULL;
173 context->start = NULL;
174 context->iter = NULL;
176 context->document_empty = TRUE;
177 context->parsing = FALSE;
183 * g_markup_parse_context_free:
184 * @context: a #GMarkupParseContext
186 * Frees a #GMarkupParseContext. Can't be called from inside
187 * one of the #GMarkupParser functions.
191 g_markup_parse_context_free (GMarkupParseContext *context)
193 g_return_if_fail (context != NULL);
194 g_return_if_fail (!context->parsing);
196 if (context->dnotify)
197 (* context->dnotify) (context->user_data);
199 g_slist_foreach (context->attributes, (GFunc)attribute_free, NULL);
200 g_slist_free (context->attributes);
202 g_slist_foreach (context->tag_stack, (GFunc)g_free, NULL);
203 g_slist_free (context->tag_stack);
205 if (context->partial_chunk)
206 g_string_free (context->partial_chunk, TRUE);
208 if (context->leftover_char_portion)
209 g_string_free (context->leftover_char_portion, TRUE);
215 attribute_list_to_arrays (GSList *attributes,
226 len = g_slist_length (attributes);
230 names = g_new (gchar*, len + 1);
238 values = g_new (gchar*, len + 1);
244 /* We want to reverse the list, since it's
245 * backward from the order the attributes appeared
249 tmp_list = attributes;
252 GMarkupAttribute *attr = tmp_list->data;
257 names[i] = g_strdup (attr->name);
260 values[i] = g_strdup (attr->value);
262 tmp_list = g_slist_next (tmp_list);
277 mark_error (GMarkupParseContext *context,
280 context->state = STATE_ERROR;
282 if (context->parser->error)
283 (*context->parser->error) (context, error, context->user_data);
287 set_error (GMarkupParseContext *context,
297 va_start (args, format);
298 s = g_strdup_vprintf (format, args);
301 tmp_error = g_error_new (G_MARKUP_ERROR,
303 _("Error on line %d char %d: %s"),
304 context->line_number,
305 context->char_number,
310 mark_error (context, tmp_error);
312 g_propagate_error (error, tmp_error);
316 is_name_start_char (gunichar c)
318 if (g_unichar_isalpha (c) ||
327 is_name_char (gunichar c)
329 if (g_unichar_isalnum (c) ||
341 char_str (gunichar c,
345 g_unichar_to_utf8 (c, buf);
350 utf8_str (const gchar *utf8,
353 char_str (g_utf8_get_char (utf8), buf);
358 set_unescape_error (GMarkupParseContext *context,
360 const gchar *remaining_text,
361 const gchar *remaining_text_end,
369 gint remaining_newlines;
372 remaining_newlines = 0;
374 while (p != remaining_text_end)
377 ++remaining_newlines;
381 va_start (args, format);
382 s = g_strdup_vprintf (format, args);
385 tmp_error = g_error_new (G_MARKUP_ERROR,
387 _("Error on line %d: %s"),
388 context->line_number - remaining_newlines,
393 mark_error (context, tmp_error);
395 g_propagate_error (error, tmp_error);
401 USTATE_AFTER_AMPERSAND,
402 USTATE_INSIDE_ENTITY_NAME,
403 USTATE_AFTER_CHARREF_HASH
407 unescape_text (GMarkupParseContext *context,
409 const gchar *text_end,
413 #define MAX_ENT_LEN 5
419 str = g_string_new ("");
421 state = USTATE_INSIDE_TEXT;
424 while (p != text_end && context->state != STATE_ERROR)
426 g_assert (p < text_end);
430 case USTATE_INSIDE_TEXT:
432 while (p != text_end && *p != '&')
433 p = g_utf8_next_char (p);
437 g_string_append_len (str, start, p - start);
442 if (p != text_end && *p == '&')
444 p = g_utf8_next_char (p);
445 state = USTATE_AFTER_AMPERSAND;
450 case USTATE_AFTER_AMPERSAND:
454 p = g_utf8_next_char (p);
457 state = USTATE_AFTER_CHARREF_HASH;
459 else if (!is_name_start_char (g_utf8_get_char (p)))
463 set_unescape_error (context, error,
465 G_MARKUP_ERROR_PARSE,
466 _("Empty entity '&;' seen; valid "
467 "entities are: & " < > '"));
473 set_unescape_error (context, error,
475 G_MARKUP_ERROR_PARSE,
476 _("Character '%s' is not valid at "
477 "the start of an entity name; "
478 "the & character begins an entity; "
479 "if this ampersand isn't supposed "
480 "to be an entity, escape it as "
488 state = USTATE_INSIDE_ENTITY_NAME;
494 case USTATE_INSIDE_ENTITY_NAME:
496 gchar buf[MAX_ENT_LEN+1] = {
497 '\0', '\0', '\0', '\0', '\0', '\0'
501 while (p != text_end)
505 else if (!is_name_char (*p))
509 set_unescape_error (context, error,
511 G_MARKUP_ERROR_PARSE,
512 _("Character '%s' is not valid "
513 "inside an entity name"),
518 p = g_utf8_next_char (p);
521 if (context->state != STATE_ERROR)
536 /* move to after semicolon */
537 p = g_utf8_next_char (p);
539 state = USTATE_INSIDE_TEXT;
541 if (strcmp (buf, "lt") == 0)
542 g_string_append_c (str, '<');
543 else if (strcmp (buf, "gt") == 0)
544 g_string_append_c (str, '>');
545 else if (strcmp (buf, "amp") == 0)
546 g_string_append_c (str, '&');
547 else if (strcmp (buf, "quot") == 0)
548 g_string_append_c (str, '"');
549 else if (strcmp (buf, "apos") == 0)
550 g_string_append_c (str, '\'');
553 set_unescape_error (context, error,
555 G_MARKUP_ERROR_PARSE,
556 _("Entity name '%s' is not known"),
562 set_unescape_error (context, error,
563 /* give line number of the & */
565 G_MARKUP_ERROR_PARSE,
566 _("Entity did not end with a semicolon; "
567 "most likely you used an ampersand "
568 "character without intending to start "
569 "an entity - escape ampersand as &"));
575 case USTATE_AFTER_CHARREF_HASH:
577 gboolean is_hex = FALSE;
581 p = g_utf8_next_char (p);
585 while (p != text_end && *p != ';')
586 p = g_utf8_next_char (p);
590 g_assert (*p == ';');
592 /* digit is between start and p */
596 gchar *digit = g_strndup (start, p - start);
599 gchar *digit_end = digit + (p - start);
603 l = strtoul (digit, &end, 16);
605 l = strtoul (digit, &end, 10);
607 if (end != digit_end || errno != 0)
609 set_unescape_error (context, error,
611 G_MARKUP_ERROR_PARSE,
612 _("Failed to parse '%s', which "
613 "should have been a digit "
614 "inside a character reference "
615 "(ê for example) - perhaps "
616 "the digit is too large"),
621 /* characters XML permits */
625 (l >= 0x20 && l <= 0xD7FF) ||
626 (l >= 0xE000 && l <= 0xFFFD) ||
627 (l >= 0x10000 && l <= 0x10FFFF))
630 g_string_append (str,
635 set_unescape_error (context, error,
637 G_MARKUP_ERROR_PARSE,
638 _("Character reference '%s' does not encode a permitted character"),
645 /* Move to next state */
646 p = g_utf8_next_char (p); /* past semicolon */
648 state = USTATE_INSIDE_TEXT;
652 set_unescape_error (context, error,
654 G_MARKUP_ERROR_PARSE,
655 _("Empty character reference; "
656 "should include a digit such as "
662 set_unescape_error (context, error,
664 G_MARKUP_ERROR_PARSE,
665 _("Character reference did not end with a "
667 "most likely you used an ampersand "
668 "character without intending to start "
669 "an entity - escape ampersand as &"));
675 g_assert_not_reached ();
680 /* If no errors, we should have returned to USTATE_INSIDE_TEXT */
681 g_assert (context->state == STATE_ERROR ||
682 state == USTATE_INSIDE_TEXT);
684 if (context->state == STATE_ERROR)
686 g_string_free (str, TRUE);
692 *unescaped = g_string_free (str, FALSE);
700 advance_char (GMarkupParseContext *context)
703 context->iter = g_utf8_next_char (context->iter);
704 context->char_number += 1;
705 if (*context->iter == '\n')
707 context->line_number += 1;
708 context->char_number = 1;
711 return context->iter != context->current_text_end;
715 skip_spaces (GMarkupParseContext *context)
719 if (!g_unichar_isspace (g_utf8_get_char (context->iter)))
722 while (advance_char (context));
726 advance_to_name_end (GMarkupParseContext *context)
730 if (!is_name_char (g_utf8_get_char (context->iter)))
733 while (advance_char (context));
737 add_to_partial (GMarkupParseContext *context,
738 const gchar *text_start,
739 const gchar *text_end)
741 if (context->partial_chunk == NULL)
742 context->partial_chunk = g_string_new ("");
744 if (text_start != text_end)
745 g_string_append_len (context->partial_chunk, text_start,
746 text_end - text_start);
748 /* Invariant here that partial_chunk exists */
752 free_partial (GMarkupParseContext *context)
754 if (context->partial_chunk != NULL)
756 g_string_free (context->partial_chunk, TRUE);
757 context->partial_chunk = NULL;
762 current_element (GMarkupParseContext *context)
764 return context->tag_stack->data;
768 current_attribute (GMarkupParseContext *context)
770 return ((GMarkupAttribute*)context->attributes->data)->name;
774 find_current_text_end (GMarkupParseContext *context)
776 /* This function must be safe (non-segfaulting) on invalid UTF8 */
777 const gchar *end = context->current_text + context->current_text_len;
783 p = context->current_text;
784 next = g_utf8_find_next_char (p, end);
789 next = g_utf8_find_next_char (p, end);
792 /* p is now the start of the last character or character portion. */
794 next = g_utf8_next_char (p); /* this only touches *p, nothing beyond */
798 /* whole character */
799 context->current_text_end = end;
804 context->leftover_char_portion = g_string_new_len (p, end - p);
805 context->current_text_len -= (end - p);
806 context->current_text_end = p;
811 * g_markup_parse_context_parse:
812 * @context: a #GMarkupParseContext
813 * @text: chunk of text to parse
814 * @text_len: length of @text in bytes
815 * @error: return location for a #GError
817 * Feed some data to the #GMarkupParseContext. The data need not
818 * be valid UTF-8; an error will be signaled if it's invalid.
819 * The data need not be an entire document; you can feed a document
820 * into the parser incrementally, via multiple calls to this function.
821 * Typically, as you receive data from a network connection or file,
822 * you feed each received chunk of data into this function, aborting
823 * the process if an error occurs. Once an error is reported, no further
824 * data may be fed to the #GMarkupParseContext; all errors are fatal.
826 * Return value: %FALSE if an error occurred, %TRUE on success
829 g_markup_parse_context_parse (GMarkupParseContext *context,
834 const gchar *first_invalid;
836 g_return_val_if_fail (context != NULL, FALSE);
837 g_return_val_if_fail (text != NULL, FALSE);
838 g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
839 g_return_val_if_fail (!context->parsing, FALSE);
842 text_len = strlen (text);
847 context->parsing = TRUE;
849 if (context->leftover_char_portion)
851 const gchar *first_char;
853 if ((*text & 0xc0) != 0x80)
856 first_char = g_utf8_find_next_char (text, text + text_len);
860 /* leftover_char_portion was completed. Parse it. */
861 GString *portion = context->leftover_char_portion;
863 g_string_append_len (context->leftover_char_portion,
864 text, first_char - text);
866 /* hacks to allow recursion */
867 context->parsing = FALSE;
868 context->leftover_char_portion = NULL;
870 if (!g_markup_parse_context_parse (context,
871 portion->str, portion->len,
874 g_assert (context->state == STATE_ERROR);
877 g_string_free (portion, TRUE);
878 context->parsing = TRUE;
880 /* Skip the fraction of char that was in this text */
881 text_len -= (first_char - text);
886 /* another little chunk of the leftover char; geez
887 * someone is inefficient.
889 g_string_append_len (context->leftover_char_portion,
892 if (context->leftover_char_portion->len > 7)
894 /* The leftover char portion is too big to be
899 G_MARKUP_ERROR_BAD_UTF8,
900 _("Invalid UTF-8 encoded text"));
907 context->current_text = text;
908 context->current_text_len = text_len;
909 context->iter = context->current_text;
910 context->start = context->iter;
912 /* Nothing left after finishing the leftover char, or nothing
913 * passed in to begin with.
915 if (context->current_text_len == 0)
918 /* find_current_text_end () assumes the string starts at
919 * a character start, so we need to validate at least
920 * that much. It doesn't assume any following bytes
923 if ((*context->current_text & 0xc0) == 0x80) /* not a char start */
927 G_MARKUP_ERROR_BAD_UTF8,
928 _("Invalid UTF-8 encoded text"));
932 /* Initialize context->current_text_end, possibly adjusting
933 * current_text_len, and add any leftover char portion
935 find_current_text_end (context);
937 /* Validate UTF8 (must be done after we find the end, since
938 * we could have a trailing incomplete char)
940 if (!g_utf8_validate (context->current_text,
941 context->current_text_len,
946 p = context->current_text;
947 while (p != context->current_text_end)
954 context->line_number += newlines;
958 G_MARKUP_ERROR_BAD_UTF8,
959 _("Invalid UTF-8 encoded text"));
963 while (context->iter != context->current_text_end)
965 switch (context->state)
968 /* Possible next state: AFTER_OPEN_ANGLE */
970 g_assert (context->tag_stack == NULL);
972 /* whitespace is ignored outside of any elements */
973 skip_spaces (context);
975 if (context->iter != context->current_text_end)
977 if (*context->iter == '<')
979 /* Move after the open angle */
980 advance_char (context);
982 context->state = STATE_AFTER_OPEN_ANGLE;
984 /* this could start a passthrough */
985 context->start = context->iter;
987 /* document is now non-empty */
988 context->document_empty = FALSE;
994 G_MARKUP_ERROR_PARSE,
995 _("Document must begin with an element (e.g. <book>)"));
1000 case STATE_AFTER_OPEN_ANGLE:
1001 /* Possible next states: INSIDE_OPEN_TAG_NAME,
1002 * AFTER_CLOSE_TAG_SLASH, INSIDE_PASSTHROUGH
1004 if (*context->iter == '?' ||
1005 *context->iter == '!')
1007 /* include < in the passthrough */
1008 const gchar *openangle = "<";
1009 add_to_partial (context, openangle, openangle + 1);
1010 context->start = context->iter;
1011 context->state = STATE_INSIDE_PASSTHROUGH;
1013 else if (*context->iter == '/')
1016 advance_char (context);
1018 context->state = STATE_AFTER_CLOSE_TAG_SLASH;
1020 else if (is_name_start_char (g_utf8_get_char (context->iter)))
1022 context->state = STATE_INSIDE_OPEN_TAG_NAME;
1024 /* start of tag name */
1025 context->start = context->iter;
1032 G_MARKUP_ERROR_PARSE,
1033 _("'%s' is not a valid character following "
1034 "a '<' character; it may not begin an "
1036 utf8_str (context->iter, buf));
1040 /* The AFTER_CLOSE_ANGLE state is actually sort of
1041 * broken, because it doesn't correspond to a range
1042 * of characters in the input stream as the others do,
1043 * and thus makes things harder to conceptualize
1045 case STATE_AFTER_CLOSE_ANGLE:
1046 /* Possible next states: INSIDE_TEXT, STATE_START */
1047 if (context->tag_stack == NULL)
1049 context->start = NULL;
1050 context->state = STATE_START;
1054 context->start = context->iter;
1055 context->state = STATE_INSIDE_TEXT;
1059 case STATE_AFTER_ELISION_SLASH:
1060 /* Possible next state: AFTER_CLOSE_ANGLE */
1063 /* We need to pop the tag stack and call the end_element
1064 * function, since this is the close tag
1066 GError *tmp_error = NULL;
1068 g_assert (context->tag_stack != NULL);
1071 if (context->parser->end_element)
1072 (* context->parser->end_element) (context,
1073 context->tag_stack->data,
1077 g_free (context->tag_stack->data);
1078 context->tag_stack = g_slist_delete_link (context->tag_stack,
1079 context->tag_stack);
1083 mark_error (context, tmp_error);
1084 g_propagate_error (error, tmp_error);
1088 if (*context->iter == '>')
1090 /* move after the close angle */
1091 advance_char (context);
1092 context->state = STATE_AFTER_CLOSE_ANGLE;
1099 G_MARKUP_ERROR_PARSE,
1100 _("Odd character '%s', expected a '>' character "
1101 "to end the start tag of element '%s'"),
1102 utf8_str (context->iter, buf),
1103 current_element (context));
1109 case STATE_INSIDE_OPEN_TAG_NAME:
1110 /* Possible next states: BETWEEN_ATTRIBUTES */
1112 /* if there's a partial chunk then it's the first part of the
1113 * tag name. If there's a context->start then it's the start
1114 * of the tag name in current_text, the partial chunk goes
1115 * before that start though.
1117 advance_to_name_end (context);
1119 if (context->iter == context->current_text_end)
1121 /* The name hasn't necessarily ended. Merge with
1122 * partial chunk, leave state unchanged.
1124 add_to_partial (context, context->start, context->iter);
1128 /* The name has ended. Combine it with the partial chunk
1129 * if any; push it on the stack; enter next state.
1131 add_to_partial (context, context->start, context->iter);
1132 context->tag_stack =
1133 g_slist_prepend (context->tag_stack,
1134 g_string_free (context->partial_chunk,
1137 context->partial_chunk = NULL;
1139 context->state = STATE_BETWEEN_ATTRIBUTES;
1140 context->start = NULL;
1144 case STATE_INSIDE_ATTRIBUTE_NAME:
1145 /* Possible next states: AFTER_ATTRIBUTE_EQUALS_SIGN */
1147 /* read the full name, if we enter the equals sign state
1148 * then add the attribute to the list (without the value),
1149 * otherwise store a partial chunk to be prepended later.
1151 advance_to_name_end (context);
1153 if (context->iter == context->current_text_end)
1155 /* The name hasn't necessarily ended. Merge with
1156 * partial chunk, leave state unchanged.
1158 add_to_partial (context, context->start, context->iter);
1162 /* The name has ended. Combine it with the partial chunk
1163 * if any; push it on the stack; enter next state.
1165 GMarkupAttribute *attr;
1166 add_to_partial (context, context->start, context->iter);
1168 attr = attribute_new (NULL, NULL);
1170 attr->name = g_string_free (context->partial_chunk,
1173 context->partial_chunk = NULL;
1174 context->start = NULL;
1176 context->attributes =
1177 g_slist_prepend (context->attributes, attr);
1179 if (*context->iter == '=')
1181 advance_char (context);
1182 context->state = STATE_AFTER_ATTRIBUTE_EQUALS_SIGN;
1189 G_MARKUP_ERROR_PARSE,
1190 _("Odd character '%s', expected a '=' after "
1191 "attribute name '%s' of element '%s'"),
1192 utf8_str (context->iter, buf),
1194 current_element (context));
1200 case STATE_BETWEEN_ATTRIBUTES:
1201 /* Possible next states: AFTER_CLOSE_ANGLE,
1202 * AFTER_ELISION_SLASH, INSIDE_ATTRIBUTE_NAME
1204 skip_spaces (context);
1206 if (context->iter != context->current_text_end)
1208 if (*context->iter == '/')
1210 advance_char (context);
1211 context->state = STATE_AFTER_ELISION_SLASH;
1213 else if (*context->iter == '>')
1216 advance_char (context);
1217 context->state = STATE_AFTER_CLOSE_ANGLE;
1219 else if (is_name_start_char (g_utf8_get_char (context->iter)))
1221 context->state = STATE_INSIDE_ATTRIBUTE_NAME;
1222 /* start of attribute name */
1223 context->start = context->iter;
1230 G_MARKUP_ERROR_PARSE,
1231 _("Odd character '%s', expected a '>' or '/' "
1232 "character to end the start tag of "
1233 "element '%s', or optionally an attribute; "
1234 "perhaps you used an invalid character in "
1235 "an attribute name"),
1236 utf8_str (context->iter, buf),
1237 current_element (context));
1240 /* If we're done with attributes, invoke
1241 * the start_element callback
1243 if (context->state == STATE_AFTER_ELISION_SLASH ||
1244 context->state == STATE_AFTER_CLOSE_ANGLE)
1246 const gchar *start_name;
1247 gchar **attr_names = NULL;
1248 gchar **attr_values = NULL;
1251 /* Call user callback for element start */
1252 start_name = current_element (context);
1254 /* this gratuituously copies the attr names/values
1257 attribute_list_to_arrays (context->attributes,
1263 if (context->parser->start_element)
1264 (* context->parser->start_element) (context,
1271 g_strfreev (attr_names);
1272 g_strfreev (attr_values);
1274 /* Go ahead and free this. */
1275 g_slist_foreach (context->attributes, (GFunc)attribute_free,
1277 g_slist_free (context->attributes);
1278 context->attributes = NULL;
1280 if (tmp_error != NULL)
1282 mark_error (context, tmp_error);
1283 g_propagate_error (error, tmp_error);
1289 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
1290 /* Possible next state: INSIDE_ATTRIBUTE_VALUE */
1291 if (*context->iter == '"')
1293 advance_char (context);
1294 context->state = STATE_INSIDE_ATTRIBUTE_VALUE;
1295 context->start = context->iter;
1302 G_MARKUP_ERROR_PARSE,
1303 _("Odd character '%s', expected an open quote mark "
1304 "after the equals sign when giving value for "
1305 "attribute '%s' of element '%s'"),
1306 utf8_str (context->iter, buf),
1307 current_attribute (context),
1308 current_element (context));
1312 case STATE_INSIDE_ATTRIBUTE_VALUE:
1313 /* Possible next states: BETWEEN_ATTRIBUTES */
1316 if (*context->iter == '"')
1319 while (advance_char (context));
1321 if (context->iter == context->current_text_end)
1323 /* The value hasn't necessarily ended. Merge with
1324 * partial chunk, leave state unchanged.
1326 add_to_partial (context, context->start, context->iter);
1330 /* The value has ended at the quote mark. Combine it
1331 * with the partial chunk if any; set it for the current
1334 GMarkupAttribute *attr;
1336 add_to_partial (context, context->start, context->iter);
1338 attr = context->attributes->data;
1340 if (unescape_text (context,
1341 context->partial_chunk->str,
1342 context->partial_chunk->str +
1343 context->partial_chunk->len,
1347 /* success, advance past quote and set state. */
1348 advance_char (context);
1349 context->state = STATE_BETWEEN_ATTRIBUTES;
1350 context->start = NULL;
1353 free_partial (context);
1357 case STATE_INSIDE_TEXT:
1358 /* Possible next states: AFTER_OPEN_ANGLE */
1361 if (*context->iter == '<')
1364 while (advance_char (context));
1366 /* The text hasn't necessarily ended. Merge with
1367 * partial chunk, leave state unchanged.
1370 add_to_partial (context, context->start, context->iter);
1372 if (context->iter != context->current_text_end)
1374 gchar *unescaped = NULL;
1376 /* The text has ended at the open angle. Call the text
1380 if (unescape_text (context,
1381 context->partial_chunk->str,
1382 context->partial_chunk->str +
1383 context->partial_chunk->len,
1387 GError *tmp_error = NULL;
1389 if (context->parser->text)
1390 (*context->parser->text) (context,
1398 if (tmp_error == NULL)
1400 /* advance past open angle and set state. */
1401 advance_char (context);
1402 context->state = STATE_AFTER_OPEN_ANGLE;
1403 /* could begin a passthrough */
1404 context->start = context->iter;
1408 mark_error (context, tmp_error);
1409 g_propagate_error (error, tmp_error);
1413 free_partial (context);
1417 case STATE_AFTER_CLOSE_TAG_SLASH:
1418 /* Possible next state: INSIDE_CLOSE_TAG_NAME */
1419 if (is_name_start_char (g_utf8_get_char (context->iter)))
1421 context->state = STATE_INSIDE_CLOSE_TAG_NAME;
1423 /* start of tag name */
1424 context->start = context->iter;
1431 G_MARKUP_ERROR_PARSE,
1432 _("'%s' is not a valid character following "
1433 "the characters '</'; '%s' may not begin an "
1435 utf8_str (context->iter, buf),
1436 utf8_str (context->iter, buf));
1440 case STATE_INSIDE_CLOSE_TAG_NAME:
1441 /* Possible next state: AFTER_CLOSE_ANGLE */
1442 advance_to_name_end (context);
1444 if (context->iter == context->current_text_end)
1446 /* The name hasn't necessarily ended. Merge with
1447 * partial chunk, leave state unchanged.
1449 add_to_partial (context, context->start, context->iter);
1453 /* The name has ended. Combine it with the partial chunk
1454 * if any; check that it matches stack top and pop
1455 * stack; invoke proper callback; enter next state.
1459 add_to_partial (context, context->start, context->iter);
1461 close_name = g_string_free (context->partial_chunk, FALSE);
1462 context->partial_chunk = NULL;
1464 if (context->tag_stack == NULL)
1468 G_MARKUP_ERROR_PARSE,
1469 _("Element '%s' was closed, no element "
1470 "is currently open"),
1473 else if (strcmp (close_name, current_element (context)) != 0)
1477 G_MARKUP_ERROR_PARSE,
1478 _("Element '%s' was closed, but the currently "
1479 "open element is '%s'"),
1481 current_element (context));
1483 else if (*context->iter != '>')
1488 G_MARKUP_ERROR_PARSE,
1489 _("'%s' is not a valid character following "
1490 "the close element name '%s'; the allowed "
1491 "character is '>'"),
1492 utf8_str (context->iter, buf),
1498 advance_char (context);
1499 context->state = STATE_AFTER_CLOSE_ANGLE;
1500 context->start = NULL;
1502 /* call the end_element callback */
1504 if (context->parser->end_element)
1505 (* context->parser->end_element) (context,
1511 /* Pop the tag stack */
1512 g_free (context->tag_stack->data);
1513 context->tag_stack = g_slist_delete_link (context->tag_stack,
1514 context->tag_stack);
1518 mark_error (context, tmp_error);
1519 g_propagate_error (error, tmp_error);
1523 g_free (close_name);
1527 case STATE_INSIDE_PASSTHROUGH:
1528 /* Possible next state: AFTER_CLOSE_ANGLE */
1531 if (*context->iter == '>')
1534 while (advance_char (context));
1536 if (context->iter == context->current_text_end)
1538 /* The passthrough hasn't necessarily ended. Merge with
1539 * partial chunk, leave state unchanged.
1541 add_to_partial (context, context->start, context->iter);
1545 /* The passthrough has ended at the close angle. Combine
1546 * it with the partial chunk if any. Call the passthrough
1547 * callback. Note that the open/close angles are
1548 * included in the text of the passthrough.
1550 GError *tmp_error = NULL;
1552 advance_char (context); /* advance past close angle */
1553 add_to_partial (context, context->start, context->iter);
1555 if (context->parser->passthrough)
1556 (*context->parser->passthrough) (context,
1557 context->partial_chunk->str,
1558 context->partial_chunk->len,
1562 free_partial (context);
1564 if (tmp_error == NULL)
1566 context->state = STATE_AFTER_CLOSE_ANGLE;
1567 context->start = context->iter; /* could begin text */
1571 mark_error (context, tmp_error);
1572 g_propagate_error (error, tmp_error);
1582 g_assert_not_reached ();
1588 context->parsing = FALSE;
1590 return context->state != STATE_ERROR;
1594 * g_markup_parse_context_end_parse:
1595 * @context: a #GMarkupParseContext
1596 * @error: return location for a #GError
1598 * Signals to the #GMarkupParseContext that all data has been
1599 * fed into the parse context with g_markup_parse_context_parse().
1600 * This function reports an error if the document isn't complete,
1601 * for example if elements are still open.
1603 * Return value: %TRUE on success, %FALSE if an error was set
1606 g_markup_parse_context_end_parse (GMarkupParseContext *context,
1609 g_return_val_if_fail (context != NULL, FALSE);
1610 g_return_val_if_fail (!context->parsing, FALSE);
1611 g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
1613 if (context->document_empty)
1615 set_error (context, error, G_MARKUP_ERROR_EMPTY,
1616 _("Document was empty or contained only whitespace"));
1620 context->parsing = TRUE;
1622 switch (context->state)
1628 case STATE_AFTER_OPEN_ANGLE:
1629 set_error (context, error, G_MARKUP_ERROR_PARSE,
1630 _("Document ended unexpectedly just after an open angle bracket '<'"));
1633 case STATE_AFTER_CLOSE_ANGLE:
1634 if (context->tag_stack != NULL)
1636 /* Error message the same as for INSIDE_TEXT */
1637 set_error (context, error, G_MARKUP_ERROR_PARSE,
1638 _("Document ended unexpectedly with elements still open - "
1639 "'%s' was the last element opened"),
1640 current_element (context));
1644 case STATE_AFTER_ELISION_SLASH:
1645 set_error (context, error, G_MARKUP_ERROR_PARSE,
1646 _("Document ended unexpectedly, expected to see a close angle "
1647 "bracket ending the tag <%s/>"), current_element (context));
1650 case STATE_INSIDE_OPEN_TAG_NAME:
1651 set_error (context, error, G_MARKUP_ERROR_PARSE,
1652 _("Document ended unexpectedly inside an element name"));
1655 case STATE_INSIDE_ATTRIBUTE_NAME:
1656 set_error (context, error, G_MARKUP_ERROR_PARSE,
1657 _("Document ended unexpectedly inside an attribute name"));
1660 case STATE_BETWEEN_ATTRIBUTES:
1661 set_error (context, error, G_MARKUP_ERROR_PARSE,
1662 _("Document ended unexpectedly inside an element-opening "
1666 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
1667 set_error (context, error, G_MARKUP_ERROR_PARSE,
1668 _("Document ended unexpectedly after the equals sign "
1669 "following an attribute name; no attribute value"));
1672 case STATE_INSIDE_ATTRIBUTE_VALUE:
1673 set_error (context, error, G_MARKUP_ERROR_PARSE,
1674 _("Document ended unexpectedly while inside an attribute "
1678 case STATE_INSIDE_TEXT:
1679 g_assert (context->tag_stack != NULL);
1680 set_error (context, error, G_MARKUP_ERROR_PARSE,
1681 _("Document ended unexpectedly with elements still open - "
1682 "'%s' was the last element opened"),
1683 current_element (context));
1686 case STATE_AFTER_CLOSE_TAG_SLASH:
1687 case STATE_INSIDE_CLOSE_TAG_NAME:
1688 set_error (context, error, G_MARKUP_ERROR_PARSE,
1689 _("Document ended unexpectedly inside the close tag for"
1690 "element '%s'"), current_element);
1693 case STATE_INSIDE_PASSTHROUGH:
1694 set_error (context, error, G_MARKUP_ERROR_PARSE,
1695 _("Document ended unexpectedly inside a comment or "
1696 "processing instruction"));
1701 g_assert_not_reached ();
1705 context->parsing = FALSE;
1707 return context->state != STATE_ERROR;
1711 * g_markup_parse_context_get_position:
1712 * @context: a #GMarkupParseContext
1713 * @line_number: return location for a line number, or %NULL
1714 * @char_number: return location for a char-on-line number, or %NULL
1716 * Retrieves the current line number and the number of the character on
1717 * that line. Intended for use in error messages; there are no strict
1718 * semantics for what constitutes the "current" line number other than
1719 * "the best number we could come up with for error messages."
1723 g_markup_parse_context_get_position (GMarkupParseContext *context,
1727 g_return_if_fail (context != NULL);
1730 *line_number = context->line_number;
1733 *char_number = context->char_number;
1737 append_escaped_text (GString *str,
1745 end = text + length;
1750 next = g_utf8_next_char (p);
1755 g_string_append (str, "&");
1759 g_string_append (str, "<");
1763 g_string_append (str, ">");
1767 g_string_append (str, "'");
1771 g_string_append (str, """);
1775 g_string_append_len (str, p, next - p);
1784 * g_markup_escape_text:
1785 * @text: some valid UTF-8 text
1786 * @length: length of @text in bytes
1788 * Escapes text so that the markup parser will parse it verbatim.
1789 * Less than, greater than, ampersand, etc. are replaced with the
1790 * corresponding entities. This function would typically be used
1791 * when writing out a file to be parsed with the markup parser.
1793 * Return value: escaped text
1796 g_markup_escape_text (const gchar *text,
1801 g_return_val_if_fail (text != NULL, NULL);
1804 length = strlen (text);
1806 str = g_string_new ("");
1807 append_escaped_text (str, text, length);
1809 return g_string_free (str, FALSE);