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.
31 g_markup_error_quark ()
33 static GQuark error_quark = 0;
36 error_quark = g_quark_from_static_string ("g-markup-error-quark");
44 STATE_AFTER_OPEN_ANGLE,
45 STATE_AFTER_CLOSE_ANGLE,
46 STATE_AFTER_ELISION_SLASH, /* the slash that obviates need for end element */
47 STATE_INSIDE_OPEN_TAG_NAME,
48 STATE_INSIDE_ATTRIBUTE_NAME,
49 STATE_BETWEEN_ATTRIBUTES,
50 STATE_AFTER_ATTRIBUTE_EQUALS_SIGN,
51 STATE_INSIDE_ATTRIBUTE_VALUE,
53 STATE_AFTER_CLOSE_TAG_SLASH,
54 STATE_INSIDE_CLOSE_TAG_NAME,
55 STATE_INSIDE_PASSTHROUGH,
59 struct _GMarkupParseContext
61 const GMarkupParser *parser;
63 GMarkupParseFlags flags;
69 GDestroyNotify dnotify;
71 /* A piece of character data or an element that
72 * hasn't "ended" yet so we haven't yet called
73 * the callback for it.
75 GString *partial_chunk;
77 GMarkupParseState state;
84 const gchar *current_text;
85 gssize current_text_len;
86 const gchar *current_text_end;
88 GString *leftover_char_portion;
90 /* used to save the start of the last interesting thingy */
95 guint document_empty : 1;
100 * g_markup_parse_context_new:
101 * @parser: a #GMarkupParser
102 * @flags: one or more #GMarkupParseFlags
103 * @user_data: user data to pass to #GMarkupParser functions
104 * @user_data_dnotify: user data destroy notifier called when the parse context is freed
106 * Creates a new parse context. A parse context is used to parse
107 * marked-up documents. You can feed any number of documents into
108 * a context, as long as no errors occur; once an error occurs,
109 * the parse context can't continue to parse text (you have to free it
110 * and create a new parse context).
112 * Return value: a new #GMarkupParseContext
114 GMarkupParseContext *
115 g_markup_parse_context_new (const GMarkupParser *parser,
116 GMarkupParseFlags flags,
118 GDestroyNotify user_data_dnotify)
120 GMarkupParseContext *context;
122 g_return_val_if_fail (parser != NULL, NULL);
124 context = g_new (GMarkupParseContext, 1);
126 context->parser = parser;
127 context->flags = flags;
128 context->user_data = user_data;
129 context->dnotify = user_data_dnotify;
131 context->line_number = 1;
132 context->char_number = 1;
134 context->partial_chunk = NULL;
136 context->state = STATE_START;
137 context->tag_stack = NULL;
138 context->attr_names = NULL;
139 context->attr_values = NULL;
140 context->cur_attr = -1;
141 context->alloc_attrs = 0;
143 context->current_text = NULL;
144 context->current_text_len = -1;
145 context->current_text_end = NULL;
146 context->leftover_char_portion = NULL;
148 context->start = NULL;
149 context->iter = NULL;
151 context->document_empty = TRUE;
152 context->parsing = FALSE;
158 * g_markup_parse_context_free:
159 * @context: a #GMarkupParseContext
161 * Frees a #GMarkupParseContext. Can't be called from inside
162 * one of the #GMarkupParser functions.
166 g_markup_parse_context_free (GMarkupParseContext *context)
168 g_return_if_fail (context != NULL);
169 g_return_if_fail (!context->parsing);
171 if (context->dnotify)
172 (* context->dnotify) (context->user_data);
174 g_strfreev (context->attr_names);
175 g_strfreev (context->attr_values);
177 g_slist_foreach (context->tag_stack, (GFunc)g_free, NULL);
178 g_slist_free (context->tag_stack);
180 if (context->partial_chunk)
181 g_string_free (context->partial_chunk, TRUE);
183 if (context->leftover_char_portion)
184 g_string_free (context->leftover_char_portion, TRUE);
190 mark_error (GMarkupParseContext *context,
193 context->state = STATE_ERROR;
195 if (context->parser->error)
196 (*context->parser->error) (context, error, context->user_data);
200 set_error (GMarkupParseContext *context,
210 va_start (args, format);
211 s = g_strdup_vprintf (format, args);
214 tmp_error = g_error_new (G_MARKUP_ERROR,
216 _("Error on line %d char %d: %s"),
217 context->line_number,
218 context->char_number,
223 mark_error (context, tmp_error);
225 g_propagate_error (error, tmp_error);
229 is_name_start_char (gunichar c)
231 if (g_unichar_isalpha (c) ||
240 is_name_char (gunichar c)
242 if (g_unichar_isalnum (c) ||
254 char_str (gunichar c,
258 g_unichar_to_utf8 (c, buf);
263 utf8_str (const gchar *utf8,
266 char_str (g_utf8_get_char (utf8), buf);
271 set_unescape_error (GMarkupParseContext *context,
273 const gchar *remaining_text,
274 const gchar *remaining_text_end,
282 gint remaining_newlines;
285 remaining_newlines = 0;
287 while (p != remaining_text_end)
290 ++remaining_newlines;
294 va_start (args, format);
295 s = g_strdup_vprintf (format, args);
298 tmp_error = g_error_new (G_MARKUP_ERROR,
300 _("Error on line %d: %s"),
301 context->line_number - remaining_newlines,
306 mark_error (context, tmp_error);
308 g_propagate_error (error, tmp_error);
314 USTATE_AFTER_AMPERSAND,
315 USTATE_INSIDE_ENTITY_NAME,
316 USTATE_AFTER_CHARREF_HASH
320 unescape_text (GMarkupParseContext *context,
322 const gchar *text_end,
326 #define MAX_ENT_LEN 5
332 str = g_string_new ("");
334 state = USTATE_INSIDE_TEXT;
337 while (p != text_end && context->state != STATE_ERROR)
339 g_assert (p < text_end);
343 case USTATE_INSIDE_TEXT:
345 while (p != text_end && *p != '&')
346 p = g_utf8_next_char (p);
350 g_string_append_len (str, start, p - start);
355 if (p != text_end && *p == '&')
357 p = g_utf8_next_char (p);
358 state = USTATE_AFTER_AMPERSAND;
363 case USTATE_AFTER_AMPERSAND:
367 p = g_utf8_next_char (p);
370 state = USTATE_AFTER_CHARREF_HASH;
372 else if (!is_name_start_char (g_utf8_get_char (p)))
376 set_unescape_error (context, error,
378 G_MARKUP_ERROR_PARSE,
379 _("Empty entity '&;' seen; valid "
380 "entities are: & " < > '"));
386 set_unescape_error (context, error,
388 G_MARKUP_ERROR_PARSE,
389 _("Character '%s' is not valid at "
390 "the start of an entity name; "
391 "the & character begins an entity; "
392 "if this ampersand isn't supposed "
393 "to be an entity, escape it as "
401 state = USTATE_INSIDE_ENTITY_NAME;
407 case USTATE_INSIDE_ENTITY_NAME:
409 gchar buf[MAX_ENT_LEN+1] = {
410 '\0', '\0', '\0', '\0', '\0', '\0'
414 while (p != text_end)
418 else if (!is_name_char (*p))
422 set_unescape_error (context, error,
424 G_MARKUP_ERROR_PARSE,
425 _("Character '%s' is not valid "
426 "inside an entity name"),
431 p = g_utf8_next_char (p);
434 if (context->state != STATE_ERROR)
449 /* move to after semicolon */
450 p = g_utf8_next_char (p);
452 state = USTATE_INSIDE_TEXT;
454 if (strcmp (buf, "lt") == 0)
455 g_string_append_c (str, '<');
456 else if (strcmp (buf, "gt") == 0)
457 g_string_append_c (str, '>');
458 else if (strcmp (buf, "amp") == 0)
459 g_string_append_c (str, '&');
460 else if (strcmp (buf, "quot") == 0)
461 g_string_append_c (str, '"');
462 else if (strcmp (buf, "apos") == 0)
463 g_string_append_c (str, '\'');
466 set_unescape_error (context, error,
468 G_MARKUP_ERROR_PARSE,
469 _("Entity name '%s' is not known"),
475 set_unescape_error (context, error,
476 /* give line number of the & */
478 G_MARKUP_ERROR_PARSE,
479 _("Entity did not end with a semicolon; "
480 "most likely you used an ampersand "
481 "character without intending to start "
482 "an entity - escape ampersand as &"));
488 case USTATE_AFTER_CHARREF_HASH:
490 gboolean is_hex = FALSE;
494 p = g_utf8_next_char (p);
498 while (p != text_end && *p != ';')
499 p = g_utf8_next_char (p);
503 g_assert (*p == ';');
505 /* digit is between start and p */
509 gchar *digit = g_strndup (start, p - start);
512 gchar *digit_end = digit + (p - start);
516 l = strtoul (digit, &end, 16);
518 l = strtoul (digit, &end, 10);
520 if (end != digit_end || errno != 0)
522 set_unescape_error (context, error,
524 G_MARKUP_ERROR_PARSE,
525 _("Failed to parse '%s', which "
526 "should have been a digit "
527 "inside a character reference "
528 "(ê for example) - perhaps "
529 "the digit is too large"),
534 /* characters XML permits */
538 (l >= 0x20 && l <= 0xD7FF) ||
539 (l >= 0xE000 && l <= 0xFFFD) ||
540 (l >= 0x10000 && l <= 0x10FFFF))
543 g_string_append (str, char_str (l, buf));
547 set_unescape_error (context, error,
549 G_MARKUP_ERROR_PARSE,
550 _("Character reference '%s' does not encode a permitted character"),
557 /* Move to next state */
558 p = g_utf8_next_char (p); /* past semicolon */
560 state = USTATE_INSIDE_TEXT;
564 set_unescape_error (context, error,
566 G_MARKUP_ERROR_PARSE,
567 _("Empty character reference; "
568 "should include a digit such as "
574 set_unescape_error (context, error,
576 G_MARKUP_ERROR_PARSE,
577 _("Character reference did not end with a "
579 "most likely you used an ampersand "
580 "character without intending to start "
581 "an entity - escape ampersand as &"));
587 g_assert_not_reached ();
592 /* If no errors, we should have returned to USTATE_INSIDE_TEXT */
593 g_assert (context->state == STATE_ERROR ||
594 state == USTATE_INSIDE_TEXT);
596 if (context->state == STATE_ERROR)
598 g_string_free (str, TRUE);
604 *unescaped = g_string_free (str, FALSE);
612 advance_char (GMarkupParseContext *context)
615 context->iter = g_utf8_next_char (context->iter);
616 context->char_number += 1;
617 if (*context->iter == '\n')
619 context->line_number += 1;
620 context->char_number = 1;
623 return context->iter != context->current_text_end;
627 skip_spaces (GMarkupParseContext *context)
631 if (!g_unichar_isspace (g_utf8_get_char (context->iter)))
634 while (advance_char (context));
638 advance_to_name_end (GMarkupParseContext *context)
642 if (!is_name_char (g_utf8_get_char (context->iter)))
645 while (advance_char (context));
649 add_to_partial (GMarkupParseContext *context,
650 const gchar *text_start,
651 const gchar *text_end)
653 if (context->partial_chunk == NULL)
654 context->partial_chunk = g_string_new ("");
656 if (text_start != text_end)
657 g_string_append_len (context->partial_chunk, text_start,
658 text_end - text_start);
660 /* Invariant here that partial_chunk exists */
664 truncate_partial (GMarkupParseContext *context)
666 if (context->partial_chunk != NULL)
668 context->partial_chunk = g_string_truncate (context->partial_chunk, 0);
673 current_element (GMarkupParseContext *context)
675 return context->tag_stack->data;
679 current_attribute (GMarkupParseContext *context)
681 g_assert (context->cur_attr >= 0);
682 return context->attr_names[context->cur_attr];
686 find_current_text_end (GMarkupParseContext *context)
688 /* This function must be safe (non-segfaulting) on invalid UTF8 */
689 const gchar *end = context->current_text + context->current_text_len;
693 g_assert (context->current_text_len > 0);
695 p = context->current_text;
696 next = g_utf8_find_next_char (p, end);
701 next = g_utf8_find_next_char (p, end);
704 /* p is now the start of the last character or character portion. */
706 next = g_utf8_next_char (p); /* this only touches *p, nothing beyond */
710 /* whole character */
711 context->current_text_end = end;
716 context->leftover_char_portion = g_string_new_len (p, end - p);
717 context->current_text_len -= (end - p);
718 context->current_text_end = p;
723 add_attribute (GMarkupParseContext *context, char *name)
725 if (context->cur_attr + 2 >= context->alloc_attrs)
727 context->alloc_attrs += 5; /* silly magic number */
728 context->attr_names = g_realloc (context->attr_names, sizeof(char*)*context->alloc_attrs);
729 context->attr_values = g_realloc (context->attr_values, sizeof(char*)*context->alloc_attrs);
732 context->attr_names[context->cur_attr] = name;
733 context->attr_values[context->cur_attr] = NULL;
734 context->attr_names[context->cur_attr+1] = NULL;
735 context->attr_values[context->cur_attr+1] = NULL;
739 * g_markup_parse_context_parse:
740 * @context: a #GMarkupParseContext
741 * @text: chunk of text to parse
742 * @text_len: length of @text in bytes
743 * @error: return location for a #GError
745 * Feed some data to the #GMarkupParseContext. The data need not
746 * be valid UTF-8; an error will be signaled if it's invalid.
747 * The data need not be an entire document; you can feed a document
748 * into the parser incrementally, via multiple calls to this function.
749 * Typically, as you receive data from a network connection or file,
750 * you feed each received chunk of data into this function, aborting
751 * the process if an error occurs. Once an error is reported, no further
752 * data may be fed to the #GMarkupParseContext; all errors are fatal.
754 * Return value: %FALSE if an error occurred, %TRUE on success
757 g_markup_parse_context_parse (GMarkupParseContext *context,
762 const gchar *first_invalid;
764 g_return_val_if_fail (context != NULL, FALSE);
765 g_return_val_if_fail (text != NULL, FALSE);
766 g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
767 g_return_val_if_fail (!context->parsing, FALSE);
770 text_len = strlen (text);
775 context->parsing = TRUE;
777 if (context->leftover_char_portion)
779 const gchar *first_char;
781 if ((*text & 0xc0) != 0x80)
784 first_char = g_utf8_find_next_char (text, text + text_len);
788 /* leftover_char_portion was completed. Parse it. */
789 GString *portion = context->leftover_char_portion;
791 g_string_append_len (context->leftover_char_portion,
792 text, first_char - text);
794 /* hacks to allow recursion */
795 context->parsing = FALSE;
796 context->leftover_char_portion = NULL;
798 if (!g_markup_parse_context_parse (context,
799 portion->str, portion->len,
802 g_assert (context->state == STATE_ERROR);
805 g_string_free (portion, TRUE);
806 context->parsing = TRUE;
808 /* Skip the fraction of char that was in this text */
809 text_len -= (first_char - text);
814 /* another little chunk of the leftover char; geez
815 * someone is inefficient.
817 g_string_append_len (context->leftover_char_portion,
820 if (context->leftover_char_portion->len > 7)
822 /* The leftover char portion is too big to be
827 G_MARKUP_ERROR_BAD_UTF8,
828 _("Invalid UTF-8 encoded text"));
835 context->current_text = text;
836 context->current_text_len = text_len;
837 context->iter = context->current_text;
838 context->start = context->iter;
840 /* Nothing left after finishing the leftover char, or nothing
841 * passed in to begin with.
843 if (context->current_text_len == 0)
846 /* find_current_text_end () assumes the string starts at
847 * a character start, so we need to validate at least
848 * that much. It doesn't assume any following bytes
851 if ((*context->current_text & 0xc0) == 0x80) /* not a char start */
855 G_MARKUP_ERROR_BAD_UTF8,
856 _("Invalid UTF-8 encoded text"));
860 /* Initialize context->current_text_end, possibly adjusting
861 * current_text_len, and add any leftover char portion
863 find_current_text_end (context);
865 /* Validate UTF8 (must be done after we find the end, since
866 * we could have a trailing incomplete char)
868 if (!g_utf8_validate (context->current_text,
869 context->current_text_len,
874 p = context->current_text;
875 while (p != context->current_text_end)
882 context->line_number += newlines;
886 G_MARKUP_ERROR_BAD_UTF8,
887 _("Invalid UTF-8 encoded text"));
891 while (context->iter != context->current_text_end)
893 switch (context->state)
896 /* Possible next state: AFTER_OPEN_ANGLE */
898 g_assert (context->tag_stack == NULL);
900 /* whitespace is ignored outside of any elements */
901 skip_spaces (context);
903 if (context->iter != context->current_text_end)
905 if (*context->iter == '<')
907 /* Move after the open angle */
908 advance_char (context);
910 context->state = STATE_AFTER_OPEN_ANGLE;
912 /* this could start a passthrough */
913 context->start = context->iter;
915 /* document is now non-empty */
916 context->document_empty = FALSE;
922 G_MARKUP_ERROR_PARSE,
923 _("Document must begin with an element (e.g. <book>)"));
928 case STATE_AFTER_OPEN_ANGLE:
929 /* Possible next states: INSIDE_OPEN_TAG_NAME,
930 * AFTER_CLOSE_TAG_SLASH, INSIDE_PASSTHROUGH
932 if (*context->iter == '?' ||
933 *context->iter == '!')
935 /* include < in the passthrough */
936 const gchar *openangle = "<";
937 add_to_partial (context, openangle, openangle + 1);
938 context->start = context->iter;
939 context->state = STATE_INSIDE_PASSTHROUGH;
941 else if (*context->iter == '/')
944 advance_char (context);
946 context->state = STATE_AFTER_CLOSE_TAG_SLASH;
948 else if (is_name_start_char (g_utf8_get_char (context->iter)))
950 context->state = STATE_INSIDE_OPEN_TAG_NAME;
952 /* start of tag name */
953 context->start = context->iter;
960 G_MARKUP_ERROR_PARSE,
961 _("'%s' is not a valid character following "
962 "a '<' character; it may not begin an "
964 utf8_str (context->iter, buf));
968 /* The AFTER_CLOSE_ANGLE state is actually sort of
969 * broken, because it doesn't correspond to a range
970 * of characters in the input stream as the others do,
971 * and thus makes things harder to conceptualize
973 case STATE_AFTER_CLOSE_ANGLE:
974 /* Possible next states: INSIDE_TEXT, STATE_START */
975 if (context->tag_stack == NULL)
977 context->start = NULL;
978 context->state = STATE_START;
982 context->start = context->iter;
983 context->state = STATE_INSIDE_TEXT;
987 case STATE_AFTER_ELISION_SLASH:
988 /* Possible next state: AFTER_CLOSE_ANGLE */
991 /* We need to pop the tag stack and call the end_element
992 * function, since this is the close tag
994 GError *tmp_error = NULL;
996 g_assert (context->tag_stack != NULL);
999 if (context->parser->end_element)
1000 (* context->parser->end_element) (context,
1001 context->tag_stack->data,
1007 mark_error (context, tmp_error);
1008 g_propagate_error (error, tmp_error);
1012 if (*context->iter == '>')
1014 /* move after the close angle */
1015 advance_char (context);
1016 context->state = STATE_AFTER_CLOSE_ANGLE;
1023 G_MARKUP_ERROR_PARSE,
1024 _("Odd character '%s', expected a '>' character "
1025 "to end the start tag of element '%s'"),
1026 utf8_str (context->iter, buf),
1027 current_element (context));
1031 g_free (context->tag_stack->data);
1032 context->tag_stack = g_slist_delete_link (context->tag_stack,
1033 context->tag_stack);
1037 case STATE_INSIDE_OPEN_TAG_NAME:
1038 /* Possible next states: BETWEEN_ATTRIBUTES */
1040 /* if there's a partial chunk then it's the first part of the
1041 * tag name. If there's a context->start then it's the start
1042 * of the tag name in current_text, the partial chunk goes
1043 * before that start though.
1045 advance_to_name_end (context);
1047 if (context->iter == context->current_text_end)
1049 /* The name hasn't necessarily ended. Merge with
1050 * partial chunk, leave state unchanged.
1052 add_to_partial (context, context->start, context->iter);
1056 /* The name has ended. Combine it with the partial chunk
1057 * if any; push it on the stack; enter next state.
1059 add_to_partial (context, context->start, context->iter);
1060 context->tag_stack =
1061 g_slist_prepend (context->tag_stack,
1062 g_string_free (context->partial_chunk,
1065 context->partial_chunk = NULL;
1067 context->state = STATE_BETWEEN_ATTRIBUTES;
1068 context->start = NULL;
1072 case STATE_INSIDE_ATTRIBUTE_NAME:
1073 /* Possible next states: AFTER_ATTRIBUTE_EQUALS_SIGN */
1075 /* read the full name, if we enter the equals sign state
1076 * then add the attribute to the list (without the value),
1077 * otherwise store a partial chunk to be prepended later.
1079 advance_to_name_end (context);
1081 if (context->iter == context->current_text_end)
1083 /* The name hasn't necessarily ended. Merge with
1084 * partial chunk, leave state unchanged.
1086 add_to_partial (context, context->start, context->iter);
1090 /* The name has ended. Combine it with the partial chunk
1091 * if any; push it on the stack; enter next state.
1093 add_to_partial (context, context->start, context->iter);
1095 add_attribute (context, g_string_free (context->partial_chunk, FALSE));
1097 context->partial_chunk = NULL;
1098 context->start = NULL;
1100 if (*context->iter == '=')
1102 advance_char (context);
1103 context->state = STATE_AFTER_ATTRIBUTE_EQUALS_SIGN;
1110 G_MARKUP_ERROR_PARSE,
1111 _("Odd character '%s', expected a '=' after "
1112 "attribute name '%s' of element '%s'"),
1113 utf8_str (context->iter, buf),
1114 current_attribute (context),
1115 current_element (context));
1121 case STATE_BETWEEN_ATTRIBUTES:
1122 /* Possible next states: AFTER_CLOSE_ANGLE,
1123 * AFTER_ELISION_SLASH, INSIDE_ATTRIBUTE_NAME
1125 skip_spaces (context);
1127 if (context->iter != context->current_text_end)
1129 if (*context->iter == '/')
1131 advance_char (context);
1132 context->state = STATE_AFTER_ELISION_SLASH;
1134 else if (*context->iter == '>')
1137 advance_char (context);
1138 context->state = STATE_AFTER_CLOSE_ANGLE;
1140 else if (is_name_start_char (g_utf8_get_char (context->iter)))
1142 context->state = STATE_INSIDE_ATTRIBUTE_NAME;
1143 /* start of attribute name */
1144 context->start = context->iter;
1151 G_MARKUP_ERROR_PARSE,
1152 _("Odd character '%s', expected a '>' or '/' "
1153 "character to end the start tag of "
1154 "element '%s', or optionally an attribute; "
1155 "perhaps you used an invalid character in "
1156 "an attribute name"),
1157 utf8_str (context->iter, buf),
1158 current_element (context));
1161 /* If we're done with attributes, invoke
1162 * the start_element callback
1164 if (context->state == STATE_AFTER_ELISION_SLASH ||
1165 context->state == STATE_AFTER_CLOSE_ANGLE)
1167 const gchar *start_name;
1168 /* Ugly, but the current code expects an empty array instead of NULL */
1169 const gchar *empty = NULL;
1170 const gchar **attr_names = ∅
1171 const gchar **attr_values = ∅
1174 /* Call user callback for element start */
1175 start_name = current_element (context);
1177 if (context->cur_attr >= 0)
1179 attr_names = (const gchar**)context->attr_names;
1180 attr_values = (const gchar**)context->attr_values;
1184 if (context->parser->start_element)
1185 (* context->parser->start_element) (context,
1187 (const gchar **)attr_names,
1188 (const gchar **)attr_values,
1192 /* Go ahead and free the attributes. */
1193 for (; context->cur_attr >= 0; context->cur_attr--)
1195 int pos = context->cur_attr;
1196 g_free (context->attr_names[pos]);
1197 g_free (context->attr_values[pos]);
1198 context->attr_names[pos] = context->attr_values[pos] = NULL;
1200 g_assert (context->cur_attr == -1);
1201 g_assert (context->attr_names == NULL ||
1202 context->attr_names[0] == NULL);
1203 g_assert (context->attr_values == NULL ||
1204 context->attr_values[0] == NULL);
1206 if (tmp_error != NULL)
1208 mark_error (context, tmp_error);
1209 g_propagate_error (error, tmp_error);
1215 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
1216 /* Possible next state: INSIDE_ATTRIBUTE_VALUE */
1217 if (*context->iter == '"')
1219 advance_char (context);
1220 context->state = STATE_INSIDE_ATTRIBUTE_VALUE;
1221 context->start = context->iter;
1228 G_MARKUP_ERROR_PARSE,
1229 _("Odd character '%s', expected an open quote mark "
1230 "after the equals sign when giving value for "
1231 "attribute '%s' of element '%s'"),
1232 utf8_str (context->iter, buf),
1233 current_attribute (context),
1234 current_element (context));
1238 case STATE_INSIDE_ATTRIBUTE_VALUE:
1239 /* Possible next states: BETWEEN_ATTRIBUTES */
1242 if (*context->iter == '"')
1245 while (advance_char (context));
1247 if (context->iter == context->current_text_end)
1249 /* The value hasn't necessarily ended. Merge with
1250 * partial chunk, leave state unchanged.
1252 add_to_partial (context, context->start, context->iter);
1256 /* The value has ended at the quote mark. Combine it
1257 * with the partial chunk if any; set it for the current
1260 add_to_partial (context, context->start, context->iter);
1262 g_assert (context->cur_attr >= 0);
1264 if (unescape_text (context,
1265 context->partial_chunk->str,
1266 context->partial_chunk->str +
1267 context->partial_chunk->len,
1268 &context->attr_values[context->cur_attr],
1271 /* success, advance past quote and set state. */
1272 advance_char (context);
1273 context->state = STATE_BETWEEN_ATTRIBUTES;
1274 context->start = NULL;
1277 truncate_partial (context);
1281 case STATE_INSIDE_TEXT:
1282 /* Possible next states: AFTER_OPEN_ANGLE */
1285 if (*context->iter == '<')
1288 while (advance_char (context));
1290 /* The text hasn't necessarily ended. Merge with
1291 * partial chunk, leave state unchanged.
1294 add_to_partial (context, context->start, context->iter);
1296 if (context->iter != context->current_text_end)
1298 gchar *unescaped = NULL;
1300 /* The text has ended at the open angle. Call the text
1304 if (unescape_text (context,
1305 context->partial_chunk->str,
1306 context->partial_chunk->str +
1307 context->partial_chunk->len,
1311 GError *tmp_error = NULL;
1313 if (context->parser->text)
1314 (*context->parser->text) (context,
1322 if (tmp_error == NULL)
1324 /* advance past open angle and set state. */
1325 advance_char (context);
1326 context->state = STATE_AFTER_OPEN_ANGLE;
1327 /* could begin a passthrough */
1328 context->start = context->iter;
1332 mark_error (context, tmp_error);
1333 g_propagate_error (error, tmp_error);
1337 truncate_partial (context);
1341 case STATE_AFTER_CLOSE_TAG_SLASH:
1342 /* Possible next state: INSIDE_CLOSE_TAG_NAME */
1343 if (is_name_start_char (g_utf8_get_char (context->iter)))
1345 context->state = STATE_INSIDE_CLOSE_TAG_NAME;
1347 /* start of tag name */
1348 context->start = context->iter;
1355 G_MARKUP_ERROR_PARSE,
1356 _("'%s' is not a valid character following "
1357 "the characters '</'; '%s' may not begin an "
1359 utf8_str (context->iter, buf),
1360 utf8_str (context->iter, buf));
1364 case STATE_INSIDE_CLOSE_TAG_NAME:
1365 /* Possible next state: AFTER_CLOSE_ANGLE */
1366 advance_to_name_end (context);
1368 if (context->iter == context->current_text_end)
1370 /* The name hasn't necessarily ended. Merge with
1371 * partial chunk, leave state unchanged.
1373 add_to_partial (context, context->start, context->iter);
1377 /* The name has ended. Combine it with the partial chunk
1378 * if any; check that it matches stack top and pop
1379 * stack; invoke proper callback; enter next state.
1383 add_to_partial (context, context->start, context->iter);
1385 close_name = g_string_free (context->partial_chunk, FALSE);
1386 context->partial_chunk = NULL;
1388 if (context->tag_stack == NULL)
1392 G_MARKUP_ERROR_PARSE,
1393 _("Element '%s' was closed, no element "
1394 "is currently open"),
1397 else if (strcmp (close_name, current_element (context)) != 0)
1401 G_MARKUP_ERROR_PARSE,
1402 _("Element '%s' was closed, but the currently "
1403 "open element is '%s'"),
1405 current_element (context));
1407 else if (*context->iter != '>')
1412 G_MARKUP_ERROR_PARSE,
1413 _("'%s' is not a valid character following "
1414 "the close element name '%s'; the allowed "
1415 "character is '>'"),
1416 utf8_str (context->iter, buf),
1422 advance_char (context);
1423 context->state = STATE_AFTER_CLOSE_ANGLE;
1424 context->start = NULL;
1426 /* call the end_element callback */
1428 if (context->parser->end_element)
1429 (* context->parser->end_element) (context,
1435 /* Pop the tag stack */
1436 g_free (context->tag_stack->data);
1437 context->tag_stack = g_slist_delete_link (context->tag_stack,
1438 context->tag_stack);
1442 mark_error (context, tmp_error);
1443 g_propagate_error (error, tmp_error);
1447 g_free (close_name);
1451 case STATE_INSIDE_PASSTHROUGH:
1452 /* Possible next state: AFTER_CLOSE_ANGLE */
1455 if (*context->iter == '>')
1458 while (advance_char (context));
1460 if (context->iter == context->current_text_end)
1462 /* The passthrough hasn't necessarily ended. Merge with
1463 * partial chunk, leave state unchanged.
1465 add_to_partial (context, context->start, context->iter);
1469 /* The passthrough has ended at the close angle. Combine
1470 * it with the partial chunk if any. Call the passthrough
1471 * callback. Note that the open/close angles are
1472 * included in the text of the passthrough.
1474 GError *tmp_error = NULL;
1476 advance_char (context); /* advance past close angle */
1477 add_to_partial (context, context->start, context->iter);
1479 if (context->parser->passthrough)
1480 (*context->parser->passthrough) (context,
1481 context->partial_chunk->str,
1482 context->partial_chunk->len,
1486 truncate_partial (context);
1488 if (tmp_error == NULL)
1490 context->state = STATE_AFTER_CLOSE_ANGLE;
1491 context->start = context->iter; /* could begin text */
1495 mark_error (context, tmp_error);
1496 g_propagate_error (error, tmp_error);
1506 g_assert_not_reached ();
1512 context->parsing = FALSE;
1514 return context->state != STATE_ERROR;
1518 * g_markup_parse_context_end_parse:
1519 * @context: a #GMarkupParseContext
1520 * @error: return location for a #GError
1522 * Signals to the #GMarkupParseContext that all data has been
1523 * fed into the parse context with g_markup_parse_context_parse().
1524 * This function reports an error if the document isn't complete,
1525 * for example if elements are still open.
1527 * Return value: %TRUE on success, %FALSE if an error was set
1530 g_markup_parse_context_end_parse (GMarkupParseContext *context,
1533 g_return_val_if_fail (context != NULL, FALSE);
1534 g_return_val_if_fail (!context->parsing, FALSE);
1535 g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
1537 if (context->partial_chunk != NULL)
1539 g_string_free (context->partial_chunk, TRUE);
1540 context->partial_chunk = NULL;
1543 if (context->document_empty)
1545 set_error (context, error, G_MARKUP_ERROR_EMPTY,
1546 _("Document was empty or contained only whitespace"));
1550 context->parsing = TRUE;
1552 switch (context->state)
1558 case STATE_AFTER_OPEN_ANGLE:
1559 set_error (context, error, G_MARKUP_ERROR_PARSE,
1560 _("Document ended unexpectedly just after an open angle bracket '<'"));
1563 case STATE_AFTER_CLOSE_ANGLE:
1564 if (context->tag_stack != NULL)
1566 /* Error message the same as for INSIDE_TEXT */
1567 set_error (context, error, G_MARKUP_ERROR_PARSE,
1568 _("Document ended unexpectedly with elements still open - "
1569 "'%s' was the last element opened"),
1570 current_element (context));
1574 case STATE_AFTER_ELISION_SLASH:
1575 set_error (context, error, G_MARKUP_ERROR_PARSE,
1576 _("Document ended unexpectedly, expected to see a close angle "
1577 "bracket ending the tag <%s/>"), current_element (context));
1580 case STATE_INSIDE_OPEN_TAG_NAME:
1581 set_error (context, error, G_MARKUP_ERROR_PARSE,
1582 _("Document ended unexpectedly inside an element name"));
1585 case STATE_INSIDE_ATTRIBUTE_NAME:
1586 set_error (context, error, G_MARKUP_ERROR_PARSE,
1587 _("Document ended unexpectedly inside an attribute name"));
1590 case STATE_BETWEEN_ATTRIBUTES:
1591 set_error (context, error, G_MARKUP_ERROR_PARSE,
1592 _("Document ended unexpectedly inside an element-opening "
1596 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
1597 set_error (context, error, G_MARKUP_ERROR_PARSE,
1598 _("Document ended unexpectedly after the equals sign "
1599 "following an attribute name; no attribute value"));
1602 case STATE_INSIDE_ATTRIBUTE_VALUE:
1603 set_error (context, error, G_MARKUP_ERROR_PARSE,
1604 _("Document ended unexpectedly while inside an attribute "
1608 case STATE_INSIDE_TEXT:
1609 g_assert (context->tag_stack != NULL);
1610 set_error (context, error, G_MARKUP_ERROR_PARSE,
1611 _("Document ended unexpectedly with elements still open - "
1612 "'%s' was the last element opened"),
1613 current_element (context));
1616 case STATE_AFTER_CLOSE_TAG_SLASH:
1617 case STATE_INSIDE_CLOSE_TAG_NAME:
1618 set_error (context, error, G_MARKUP_ERROR_PARSE,
1619 _("Document ended unexpectedly inside the close tag for "
1620 "element '%s'"), current_element);
1623 case STATE_INSIDE_PASSTHROUGH:
1624 set_error (context, error, G_MARKUP_ERROR_PARSE,
1625 _("Document ended unexpectedly inside a comment or "
1626 "processing instruction"));
1631 g_assert_not_reached ();
1635 context->parsing = FALSE;
1637 return context->state != STATE_ERROR;
1641 * g_markup_parse_context_get_position:
1642 * @context: a #GMarkupParseContext
1643 * @line_number: return location for a line number, or %NULL
1644 * @char_number: return location for a char-on-line number, or %NULL
1646 * Retrieves the current line number and the number of the character on
1647 * that line. Intended for use in error messages; there are no strict
1648 * semantics for what constitutes the "current" line number other than
1649 * "the best number we could come up with for error messages."
1653 g_markup_parse_context_get_position (GMarkupParseContext *context,
1657 g_return_if_fail (context != NULL);
1660 *line_number = context->line_number;
1663 *char_number = context->char_number;
1667 append_escaped_text (GString *str,
1675 end = text + length;
1680 next = g_utf8_next_char (p);
1685 g_string_append (str, "&");
1689 g_string_append (str, "<");
1693 g_string_append (str, ">");
1697 g_string_append (str, "'");
1701 g_string_append (str, """);
1705 g_string_append_len (str, p, next - p);
1714 * g_markup_escape_text:
1715 * @text: some valid UTF-8 text
1716 * @length: length of @text in bytes
1718 * Escapes text so that the markup parser will parse it verbatim.
1719 * Less than, greater than, ampersand, etc. are replaced with the
1720 * corresponding entities. This function would typically be used
1721 * when writing out a file to be parsed with the markup parser.
1723 * Return value: escaped text
1726 g_markup_escape_text (const gchar *text,
1731 g_return_val_if_fail (text != NULL, NULL);
1734 length = strlen (text);
1736 str = g_string_new ("");
1737 append_escaped_text (str, text, length);
1739 return g_string_free (str, FALSE);