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_SQ,
52 STATE_INSIDE_ATTRIBUTE_VALUE_DQ,
54 STATE_AFTER_CLOSE_TAG_SLASH,
55 STATE_INSIDE_CLOSE_TAG_NAME,
56 STATE_INSIDE_PASSTHROUGH,
60 struct _GMarkupParseContext
62 const GMarkupParser *parser;
64 GMarkupParseFlags flags;
70 GDestroyNotify dnotify;
72 /* A piece of character data or an element that
73 * hasn't "ended" yet so we haven't yet called
74 * the callback for it.
76 GString *partial_chunk;
78 GMarkupParseState state;
85 const gchar *current_text;
86 gssize current_text_len;
87 const gchar *current_text_end;
89 GString *leftover_char_portion;
91 /* used to save the start of the last interesting thingy */
96 guint document_empty : 1;
101 * g_markup_parse_context_new:
102 * @parser: a #GMarkupParser
103 * @flags: one or more #GMarkupParseFlags
104 * @user_data: user data to pass to #GMarkupParser functions
105 * @user_data_dnotify: user data destroy notifier called when the parse context is freed
107 * Creates a new parse context. A parse context is used to parse
108 * marked-up documents. You can feed any number of documents into
109 * a context, as long as no errors occur; once an error occurs,
110 * the parse context can't continue to parse text (you have to free it
111 * and create a new parse context).
113 * Return value: a new #GMarkupParseContext
115 GMarkupParseContext *
116 g_markup_parse_context_new (const GMarkupParser *parser,
117 GMarkupParseFlags flags,
119 GDestroyNotify user_data_dnotify)
121 GMarkupParseContext *context;
123 g_return_val_if_fail (parser != NULL, NULL);
125 context = g_new (GMarkupParseContext, 1);
127 context->parser = parser;
128 context->flags = flags;
129 context->user_data = user_data;
130 context->dnotify = user_data_dnotify;
132 context->line_number = 1;
133 context->char_number = 1;
135 context->partial_chunk = NULL;
137 context->state = STATE_START;
138 context->tag_stack = NULL;
139 context->attr_names = NULL;
140 context->attr_values = NULL;
141 context->cur_attr = -1;
142 context->alloc_attrs = 0;
144 context->current_text = NULL;
145 context->current_text_len = -1;
146 context->current_text_end = NULL;
147 context->leftover_char_portion = NULL;
149 context->start = NULL;
150 context->iter = NULL;
152 context->document_empty = TRUE;
153 context->parsing = FALSE;
159 * g_markup_parse_context_free:
160 * @context: a #GMarkupParseContext
162 * Frees a #GMarkupParseContext. Can't be called from inside
163 * one of the #GMarkupParser functions.
167 g_markup_parse_context_free (GMarkupParseContext *context)
169 g_return_if_fail (context != NULL);
170 g_return_if_fail (!context->parsing);
172 if (context->dnotify)
173 (* context->dnotify) (context->user_data);
175 g_strfreev (context->attr_names);
176 g_strfreev (context->attr_values);
178 g_slist_foreach (context->tag_stack, (GFunc)g_free, NULL);
179 g_slist_free (context->tag_stack);
181 if (context->partial_chunk)
182 g_string_free (context->partial_chunk, TRUE);
184 if (context->leftover_char_portion)
185 g_string_free (context->leftover_char_portion, TRUE);
191 mark_error (GMarkupParseContext *context,
194 context->state = STATE_ERROR;
196 if (context->parser->error)
197 (*context->parser->error) (context, error, context->user_data);
201 set_error (GMarkupParseContext *context,
211 va_start (args, format);
212 s = g_strdup_vprintf (format, args);
215 tmp_error = g_error_new (G_MARKUP_ERROR,
217 _("Error on line %d char %d: %s"),
218 context->line_number,
219 context->char_number,
224 mark_error (context, tmp_error);
226 g_propagate_error (error, tmp_error);
230 is_name_start_char (gunichar c)
232 if (g_unichar_isalpha (c) ||
241 is_name_char (gunichar c)
243 if (g_unichar_isalnum (c) ||
255 char_str (gunichar c,
259 g_unichar_to_utf8 (c, buf);
264 utf8_str (const gchar *utf8,
267 char_str (g_utf8_get_char (utf8), buf);
272 set_unescape_error (GMarkupParseContext *context,
274 const gchar *remaining_text,
275 const gchar *remaining_text_end,
283 gint remaining_newlines;
286 remaining_newlines = 0;
288 while (p != remaining_text_end)
291 ++remaining_newlines;
295 va_start (args, format);
296 s = g_strdup_vprintf (format, args);
299 tmp_error = g_error_new (G_MARKUP_ERROR,
301 _("Error on line %d: %s"),
302 context->line_number - remaining_newlines,
307 mark_error (context, tmp_error);
309 g_propagate_error (error, tmp_error);
315 USTATE_AFTER_AMPERSAND,
316 USTATE_INSIDE_ENTITY_NAME,
317 USTATE_AFTER_CHARREF_HASH
321 unescape_text (GMarkupParseContext *context,
323 const gchar *text_end,
327 #define MAX_ENT_LEN 5
333 str = g_string_new ("");
335 state = USTATE_INSIDE_TEXT;
338 while (p != text_end && context->state != STATE_ERROR)
340 g_assert (p < text_end);
344 case USTATE_INSIDE_TEXT:
346 while (p != text_end && *p != '&')
347 p = g_utf8_next_char (p);
351 g_string_append_len (str, start, p - start);
356 if (p != text_end && *p == '&')
358 p = g_utf8_next_char (p);
359 state = USTATE_AFTER_AMPERSAND;
364 case USTATE_AFTER_AMPERSAND:
368 p = g_utf8_next_char (p);
371 state = USTATE_AFTER_CHARREF_HASH;
373 else if (!is_name_start_char (g_utf8_get_char (p)))
377 set_unescape_error (context, error,
379 G_MARKUP_ERROR_PARSE,
380 _("Empty entity '&;' seen; valid "
381 "entities are: & " < > '"));
387 set_unescape_error (context, error,
389 G_MARKUP_ERROR_PARSE,
390 _("Character '%s' is not valid at "
391 "the start of an entity name; "
392 "the & character begins an entity; "
393 "if this ampersand isn't supposed "
394 "to be an entity, escape it as "
402 state = USTATE_INSIDE_ENTITY_NAME;
408 case USTATE_INSIDE_ENTITY_NAME:
410 gchar buf[MAX_ENT_LEN+1] = {
411 '\0', '\0', '\0', '\0', '\0', '\0'
415 while (p != text_end)
419 else if (!is_name_char (*p))
423 set_unescape_error (context, error,
425 G_MARKUP_ERROR_PARSE,
426 _("Character '%s' is not valid "
427 "inside an entity name"),
432 p = g_utf8_next_char (p);
435 if (context->state != STATE_ERROR)
450 /* move to after semicolon */
451 p = g_utf8_next_char (p);
453 state = USTATE_INSIDE_TEXT;
455 if (strcmp (buf, "lt") == 0)
456 g_string_append_c (str, '<');
457 else if (strcmp (buf, "gt") == 0)
458 g_string_append_c (str, '>');
459 else if (strcmp (buf, "amp") == 0)
460 g_string_append_c (str, '&');
461 else if (strcmp (buf, "quot") == 0)
462 g_string_append_c (str, '"');
463 else if (strcmp (buf, "apos") == 0)
464 g_string_append_c (str, '\'');
467 set_unescape_error (context, error,
469 G_MARKUP_ERROR_PARSE,
470 _("Entity name '%s' is not known"),
476 set_unescape_error (context, error,
477 /* give line number of the & */
479 G_MARKUP_ERROR_PARSE,
480 _("Entity did not end with a semicolon; "
481 "most likely you used an ampersand "
482 "character without intending to start "
483 "an entity - escape ampersand as &"));
489 case USTATE_AFTER_CHARREF_HASH:
491 gboolean is_hex = FALSE;
495 p = g_utf8_next_char (p);
499 while (p != text_end && *p != ';')
500 p = g_utf8_next_char (p);
504 g_assert (*p == ';');
506 /* digit is between start and p */
510 gchar *digit = g_strndup (start, p - start);
513 gchar *digit_end = digit + (p - start);
517 l = strtoul (digit, &end, 16);
519 l = strtoul (digit, &end, 10);
521 if (end != digit_end || errno != 0)
523 set_unescape_error (context, error,
525 G_MARKUP_ERROR_PARSE,
526 _("Failed to parse '%s', which "
527 "should have been a digit "
528 "inside a character reference "
529 "(ê for example) - perhaps "
530 "the digit is too large"),
535 /* characters XML permits */
539 (l >= 0x20 && l <= 0xD7FF) ||
540 (l >= 0xE000 && l <= 0xFFFD) ||
541 (l >= 0x10000 && l <= 0x10FFFF))
544 g_string_append (str, char_str (l, buf));
548 set_unescape_error (context, error,
550 G_MARKUP_ERROR_PARSE,
551 _("Character reference '%s' does not encode a permitted character"),
558 /* Move to next state */
559 p = g_utf8_next_char (p); /* past semicolon */
561 state = USTATE_INSIDE_TEXT;
565 set_unescape_error (context, error,
567 G_MARKUP_ERROR_PARSE,
568 _("Empty character reference; "
569 "should include a digit such as "
575 set_unescape_error (context, error,
577 G_MARKUP_ERROR_PARSE,
578 _("Character reference did not end with a "
580 "most likely you used an ampersand "
581 "character without intending to start "
582 "an entity - escape ampersand as &"));
588 g_assert_not_reached ();
593 /* If no errors, we should have returned to USTATE_INSIDE_TEXT */
594 g_assert (context->state == STATE_ERROR ||
595 state == USTATE_INSIDE_TEXT);
597 if (context->state == STATE_ERROR)
599 g_string_free (str, TRUE);
605 *unescaped = g_string_free (str, FALSE);
613 advance_char (GMarkupParseContext *context)
616 context->iter = g_utf8_next_char (context->iter);
617 context->char_number += 1;
618 if (*context->iter == '\n')
620 context->line_number += 1;
621 context->char_number = 1;
624 return context->iter != context->current_text_end;
628 skip_spaces (GMarkupParseContext *context)
632 if (!g_unichar_isspace (g_utf8_get_char (context->iter)))
635 while (advance_char (context));
639 advance_to_name_end (GMarkupParseContext *context)
643 if (!is_name_char (g_utf8_get_char (context->iter)))
646 while (advance_char (context));
650 add_to_partial (GMarkupParseContext *context,
651 const gchar *text_start,
652 const gchar *text_end)
654 if (context->partial_chunk == NULL)
655 context->partial_chunk = g_string_new ("");
657 if (text_start != text_end)
658 g_string_append_len (context->partial_chunk, text_start,
659 text_end - text_start);
661 /* Invariant here that partial_chunk exists */
665 truncate_partial (GMarkupParseContext *context)
667 if (context->partial_chunk != NULL)
669 context->partial_chunk = g_string_truncate (context->partial_chunk, 0);
674 current_element (GMarkupParseContext *context)
676 return context->tag_stack->data;
680 current_attribute (GMarkupParseContext *context)
682 g_assert (context->cur_attr >= 0);
683 return context->attr_names[context->cur_attr];
687 find_current_text_end (GMarkupParseContext *context)
689 /* This function must be safe (non-segfaulting) on invalid UTF8 */
690 const gchar *end = context->current_text + context->current_text_len;
694 g_assert (context->current_text_len > 0);
696 p = context->current_text;
697 next = g_utf8_find_next_char (p, end);
702 next = g_utf8_find_next_char (p, end);
705 /* p is now the start of the last character or character portion. */
707 next = g_utf8_next_char (p); /* this only touches *p, nothing beyond */
711 /* whole character */
712 context->current_text_end = end;
717 context->leftover_char_portion = g_string_new_len (p, end - p);
718 context->current_text_len -= (end - p);
719 context->current_text_end = p;
724 add_attribute (GMarkupParseContext *context, char *name)
726 if (context->cur_attr + 2 >= context->alloc_attrs)
728 context->alloc_attrs += 5; /* silly magic number */
729 context->attr_names = g_realloc (context->attr_names, sizeof(char*)*context->alloc_attrs);
730 context->attr_values = g_realloc (context->attr_values, sizeof(char*)*context->alloc_attrs);
733 context->attr_names[context->cur_attr] = name;
734 context->attr_values[context->cur_attr] = NULL;
735 context->attr_names[context->cur_attr+1] = NULL;
736 context->attr_values[context->cur_attr+1] = NULL;
740 * g_markup_parse_context_parse:
741 * @context: a #GMarkupParseContext
742 * @text: chunk of text to parse
743 * @text_len: length of @text in bytes
744 * @error: return location for a #GError
746 * Feed some data to the #GMarkupParseContext. The data need not
747 * be valid UTF-8; an error will be signaled if it's invalid.
748 * The data need not be an entire document; you can feed a document
749 * into the parser incrementally, via multiple calls to this function.
750 * Typically, as you receive data from a network connection or file,
751 * you feed each received chunk of data into this function, aborting
752 * the process if an error occurs. Once an error is reported, no further
753 * data may be fed to the #GMarkupParseContext; all errors are fatal.
755 * Return value: %FALSE if an error occurred, %TRUE on success
758 g_markup_parse_context_parse (GMarkupParseContext *context,
763 const gchar *first_invalid;
765 g_return_val_if_fail (context != NULL, FALSE);
766 g_return_val_if_fail (text != NULL, FALSE);
767 g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
768 g_return_val_if_fail (!context->parsing, FALSE);
771 text_len = strlen (text);
776 context->parsing = TRUE;
778 if (context->leftover_char_portion)
780 const gchar *first_char;
782 if ((*text & 0xc0) != 0x80)
785 first_char = g_utf8_find_next_char (text, text + text_len);
789 /* leftover_char_portion was completed. Parse it. */
790 GString *portion = context->leftover_char_portion;
792 g_string_append_len (context->leftover_char_portion,
793 text, first_char - text);
795 /* hacks to allow recursion */
796 context->parsing = FALSE;
797 context->leftover_char_portion = NULL;
799 if (!g_markup_parse_context_parse (context,
800 portion->str, portion->len,
803 g_assert (context->state == STATE_ERROR);
806 g_string_free (portion, TRUE);
807 context->parsing = TRUE;
809 /* Skip the fraction of char that was in this text */
810 text_len -= (first_char - text);
815 /* another little chunk of the leftover char; geez
816 * someone is inefficient.
818 g_string_append_len (context->leftover_char_portion,
821 if (context->leftover_char_portion->len > 7)
823 /* The leftover char portion is too big to be
828 G_MARKUP_ERROR_BAD_UTF8,
829 _("Invalid UTF-8 encoded text"));
836 context->current_text = text;
837 context->current_text_len = text_len;
838 context->iter = context->current_text;
839 context->start = context->iter;
841 /* Nothing left after finishing the leftover char, or nothing
842 * passed in to begin with.
844 if (context->current_text_len == 0)
847 /* find_current_text_end () assumes the string starts at
848 * a character start, so we need to validate at least
849 * that much. It doesn't assume any following bytes
852 if ((*context->current_text & 0xc0) == 0x80) /* not a char start */
856 G_MARKUP_ERROR_BAD_UTF8,
857 _("Invalid UTF-8 encoded text"));
861 /* Initialize context->current_text_end, possibly adjusting
862 * current_text_len, and add any leftover char portion
864 find_current_text_end (context);
866 /* Validate UTF8 (must be done after we find the end, since
867 * we could have a trailing incomplete char)
869 if (!g_utf8_validate (context->current_text,
870 context->current_text_len,
875 p = context->current_text;
876 while (p != context->current_text_end)
883 context->line_number += newlines;
887 G_MARKUP_ERROR_BAD_UTF8,
888 _("Invalid UTF-8 encoded text"));
892 while (context->iter != context->current_text_end)
894 switch (context->state)
897 /* Possible next state: AFTER_OPEN_ANGLE */
899 g_assert (context->tag_stack == NULL);
901 /* whitespace is ignored outside of any elements */
902 skip_spaces (context);
904 if (context->iter != context->current_text_end)
906 if (*context->iter == '<')
908 /* Move after the open angle */
909 advance_char (context);
911 context->state = STATE_AFTER_OPEN_ANGLE;
913 /* this could start a passthrough */
914 context->start = context->iter;
916 /* document is now non-empty */
917 context->document_empty = FALSE;
923 G_MARKUP_ERROR_PARSE,
924 _("Document must begin with an element (e.g. <book>)"));
929 case STATE_AFTER_OPEN_ANGLE:
930 /* Possible next states: INSIDE_OPEN_TAG_NAME,
931 * AFTER_CLOSE_TAG_SLASH, INSIDE_PASSTHROUGH
933 if (*context->iter == '?' ||
934 *context->iter == '!')
936 /* include < in the passthrough */
937 const gchar *openangle = "<";
938 add_to_partial (context, openangle, openangle + 1);
939 context->start = context->iter;
940 context->state = STATE_INSIDE_PASSTHROUGH;
942 else if (*context->iter == '/')
945 advance_char (context);
947 context->state = STATE_AFTER_CLOSE_TAG_SLASH;
949 else if (is_name_start_char (g_utf8_get_char (context->iter)))
951 context->state = STATE_INSIDE_OPEN_TAG_NAME;
953 /* start of tag name */
954 context->start = context->iter;
961 G_MARKUP_ERROR_PARSE,
962 _("'%s' is not a valid character following "
963 "a '<' character; it may not begin an "
965 utf8_str (context->iter, buf));
969 /* The AFTER_CLOSE_ANGLE state is actually sort of
970 * broken, because it doesn't correspond to a range
971 * of characters in the input stream as the others do,
972 * and thus makes things harder to conceptualize
974 case STATE_AFTER_CLOSE_ANGLE:
975 /* Possible next states: INSIDE_TEXT, STATE_START */
976 if (context->tag_stack == NULL)
978 context->start = NULL;
979 context->state = STATE_START;
983 context->start = context->iter;
984 context->state = STATE_INSIDE_TEXT;
988 case STATE_AFTER_ELISION_SLASH:
989 /* Possible next state: AFTER_CLOSE_ANGLE */
992 /* We need to pop the tag stack and call the end_element
993 * function, since this is the close tag
995 GError *tmp_error = NULL;
997 g_assert (context->tag_stack != NULL);
1000 if (context->parser->end_element)
1001 (* context->parser->end_element) (context,
1002 context->tag_stack->data,
1008 mark_error (context, tmp_error);
1009 g_propagate_error (error, tmp_error);
1013 if (*context->iter == '>')
1015 /* move after the close angle */
1016 advance_char (context);
1017 context->state = STATE_AFTER_CLOSE_ANGLE;
1024 G_MARKUP_ERROR_PARSE,
1025 _("Odd character '%s', expected a '>' character "
1026 "to end the start tag of element '%s'"),
1027 utf8_str (context->iter, buf),
1028 current_element (context));
1032 g_free (context->tag_stack->data);
1033 context->tag_stack = g_slist_delete_link (context->tag_stack,
1034 context->tag_stack);
1038 case STATE_INSIDE_OPEN_TAG_NAME:
1039 /* Possible next states: BETWEEN_ATTRIBUTES */
1041 /* if there's a partial chunk then it's the first part of the
1042 * tag name. If there's a context->start then it's the start
1043 * of the tag name in current_text, the partial chunk goes
1044 * before that start though.
1046 advance_to_name_end (context);
1048 if (context->iter == context->current_text_end)
1050 /* The name hasn't necessarily ended. Merge with
1051 * partial chunk, leave state unchanged.
1053 add_to_partial (context, context->start, context->iter);
1057 /* The name has ended. Combine it with the partial chunk
1058 * if any; push it on the stack; enter next state.
1060 add_to_partial (context, context->start, context->iter);
1061 context->tag_stack =
1062 g_slist_prepend (context->tag_stack,
1063 g_string_free (context->partial_chunk,
1066 context->partial_chunk = NULL;
1068 context->state = STATE_BETWEEN_ATTRIBUTES;
1069 context->start = NULL;
1073 case STATE_INSIDE_ATTRIBUTE_NAME:
1074 /* Possible next states: AFTER_ATTRIBUTE_EQUALS_SIGN */
1076 /* read the full name, if we enter the equals sign state
1077 * then add the attribute to the list (without the value),
1078 * otherwise store a partial chunk to be prepended later.
1080 advance_to_name_end (context);
1082 if (context->iter == context->current_text_end)
1084 /* The name hasn't necessarily ended. Merge with
1085 * partial chunk, leave state unchanged.
1087 add_to_partial (context, context->start, context->iter);
1091 /* The name has ended. Combine it with the partial chunk
1092 * if any; push it on the stack; enter next state.
1094 add_to_partial (context, context->start, context->iter);
1096 add_attribute (context, g_string_free (context->partial_chunk, FALSE));
1098 context->partial_chunk = NULL;
1099 context->start = NULL;
1101 if (*context->iter == '=')
1103 advance_char (context);
1104 context->state = STATE_AFTER_ATTRIBUTE_EQUALS_SIGN;
1111 G_MARKUP_ERROR_PARSE,
1112 _("Odd character '%s', expected a '=' after "
1113 "attribute name '%s' of element '%s'"),
1114 utf8_str (context->iter, buf),
1115 current_attribute (context),
1116 current_element (context));
1122 case STATE_BETWEEN_ATTRIBUTES:
1123 /* Possible next states: AFTER_CLOSE_ANGLE,
1124 * AFTER_ELISION_SLASH, INSIDE_ATTRIBUTE_NAME
1126 skip_spaces (context);
1128 if (context->iter != context->current_text_end)
1130 if (*context->iter == '/')
1132 advance_char (context);
1133 context->state = STATE_AFTER_ELISION_SLASH;
1135 else if (*context->iter == '>')
1138 advance_char (context);
1139 context->state = STATE_AFTER_CLOSE_ANGLE;
1141 else if (is_name_start_char (g_utf8_get_char (context->iter)))
1143 context->state = STATE_INSIDE_ATTRIBUTE_NAME;
1144 /* start of attribute name */
1145 context->start = context->iter;
1152 G_MARKUP_ERROR_PARSE,
1153 _("Odd character '%s', expected a '>' or '/' "
1154 "character to end the start tag of "
1155 "element '%s', or optionally an attribute; "
1156 "perhaps you used an invalid character in "
1157 "an attribute name"),
1158 utf8_str (context->iter, buf),
1159 current_element (context));
1162 /* If we're done with attributes, invoke
1163 * the start_element callback
1165 if (context->state == STATE_AFTER_ELISION_SLASH ||
1166 context->state == STATE_AFTER_CLOSE_ANGLE)
1168 const gchar *start_name;
1169 /* Ugly, but the current code expects an empty array instead of NULL */
1170 const gchar *empty = NULL;
1171 const gchar **attr_names = ∅
1172 const gchar **attr_values = ∅
1175 /* Call user callback for element start */
1176 start_name = current_element (context);
1178 if (context->cur_attr >= 0)
1180 attr_names = (const gchar**)context->attr_names;
1181 attr_values = (const gchar**)context->attr_values;
1185 if (context->parser->start_element)
1186 (* context->parser->start_element) (context,
1188 (const gchar **)attr_names,
1189 (const gchar **)attr_values,
1193 /* Go ahead and free the attributes. */
1194 for (; context->cur_attr >= 0; context->cur_attr--)
1196 int pos = context->cur_attr;
1197 g_free (context->attr_names[pos]);
1198 g_free (context->attr_values[pos]);
1199 context->attr_names[pos] = context->attr_values[pos] = NULL;
1201 g_assert (context->cur_attr == -1);
1202 g_assert (context->attr_names == NULL ||
1203 context->attr_names[0] == NULL);
1204 g_assert (context->attr_values == NULL ||
1205 context->attr_values[0] == NULL);
1207 if (tmp_error != NULL)
1209 mark_error (context, tmp_error);
1210 g_propagate_error (error, tmp_error);
1216 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
1217 /* Possible next state: INSIDE_ATTRIBUTE_VALUE_[SQ/DQ] */
1218 if (*context->iter == '"')
1220 advance_char (context);
1221 context->state = STATE_INSIDE_ATTRIBUTE_VALUE_DQ;
1222 context->start = context->iter;
1224 else if (*context->iter == '\'')
1226 advance_char (context);
1227 context->state = STATE_INSIDE_ATTRIBUTE_VALUE_SQ;
1228 context->start = context->iter;
1235 G_MARKUP_ERROR_PARSE,
1236 _("Odd character '%s', expected an open quote mark "
1237 "after the equals sign when giving value for "
1238 "attribute '%s' of element '%s'"),
1239 utf8_str (context->iter, buf),
1240 current_attribute (context),
1241 current_element (context));
1245 case STATE_INSIDE_ATTRIBUTE_VALUE_SQ:
1246 case STATE_INSIDE_ATTRIBUTE_VALUE_DQ:
1247 /* Possible next states: BETWEEN_ATTRIBUTES */
1251 if (context->state == STATE_INSIDE_ATTRIBUTE_VALUE_SQ)
1262 if (*context->iter == delim)
1265 while (advance_char (context));
1267 if (context->iter == context->current_text_end)
1269 /* The value hasn't necessarily ended. Merge with
1270 * partial chunk, leave state unchanged.
1272 add_to_partial (context, context->start, context->iter);
1276 /* The value has ended at the quote mark. Combine it
1277 * with the partial chunk if any; set it for the current
1280 add_to_partial (context, context->start, context->iter);
1282 g_assert (context->cur_attr >= 0);
1284 if (unescape_text (context,
1285 context->partial_chunk->str,
1286 context->partial_chunk->str +
1287 context->partial_chunk->len,
1288 &context->attr_values[context->cur_attr],
1291 /* success, advance past quote and set state. */
1292 advance_char (context);
1293 context->state = STATE_BETWEEN_ATTRIBUTES;
1294 context->start = NULL;
1297 truncate_partial (context);
1301 case STATE_INSIDE_TEXT:
1302 /* Possible next states: AFTER_OPEN_ANGLE */
1305 if (*context->iter == '<')
1308 while (advance_char (context));
1310 /* The text hasn't necessarily ended. Merge with
1311 * partial chunk, leave state unchanged.
1314 add_to_partial (context, context->start, context->iter);
1316 if (context->iter != context->current_text_end)
1318 gchar *unescaped = NULL;
1320 /* The text has ended at the open angle. Call the text
1324 if (unescape_text (context,
1325 context->partial_chunk->str,
1326 context->partial_chunk->str +
1327 context->partial_chunk->len,
1331 GError *tmp_error = NULL;
1333 if (context->parser->text)
1334 (*context->parser->text) (context,
1342 if (tmp_error == NULL)
1344 /* advance past open angle and set state. */
1345 advance_char (context);
1346 context->state = STATE_AFTER_OPEN_ANGLE;
1347 /* could begin a passthrough */
1348 context->start = context->iter;
1352 mark_error (context, tmp_error);
1353 g_propagate_error (error, tmp_error);
1357 truncate_partial (context);
1361 case STATE_AFTER_CLOSE_TAG_SLASH:
1362 /* Possible next state: INSIDE_CLOSE_TAG_NAME */
1363 if (is_name_start_char (g_utf8_get_char (context->iter)))
1365 context->state = STATE_INSIDE_CLOSE_TAG_NAME;
1367 /* start of tag name */
1368 context->start = context->iter;
1375 G_MARKUP_ERROR_PARSE,
1376 _("'%s' is not a valid character following "
1377 "the characters '</'; '%s' may not begin an "
1379 utf8_str (context->iter, buf),
1380 utf8_str (context->iter, buf));
1384 case STATE_INSIDE_CLOSE_TAG_NAME:
1385 /* Possible next state: AFTER_CLOSE_ANGLE */
1386 advance_to_name_end (context);
1388 if (context->iter == context->current_text_end)
1390 /* The name hasn't necessarily ended. Merge with
1391 * partial chunk, leave state unchanged.
1393 add_to_partial (context, context->start, context->iter);
1397 /* The name has ended. Combine it with the partial chunk
1398 * if any; check that it matches stack top and pop
1399 * stack; invoke proper callback; enter next state.
1403 add_to_partial (context, context->start, context->iter);
1405 close_name = g_string_free (context->partial_chunk, FALSE);
1406 context->partial_chunk = NULL;
1408 if (*context->iter != '>')
1413 G_MARKUP_ERROR_PARSE,
1414 _("'%s' is not a valid character following "
1415 "the close element name '%s'; the allowed "
1416 "character is '>'"),
1417 utf8_str (context->iter, buf),
1420 else if (context->tag_stack == NULL)
1424 G_MARKUP_ERROR_PARSE,
1425 _("Element '%s' was closed, no element "
1426 "is currently open"),
1429 else if (strcmp (close_name, current_element (context)) != 0)
1433 G_MARKUP_ERROR_PARSE,
1434 _("Element '%s' was closed, but the currently "
1435 "open element is '%s'"),
1437 current_element (context));
1442 advance_char (context);
1443 context->state = STATE_AFTER_CLOSE_ANGLE;
1444 context->start = NULL;
1446 /* call the end_element callback */
1448 if (context->parser->end_element)
1449 (* context->parser->end_element) (context,
1455 /* Pop the tag stack */
1456 g_free (context->tag_stack->data);
1457 context->tag_stack = g_slist_delete_link (context->tag_stack,
1458 context->tag_stack);
1462 mark_error (context, tmp_error);
1463 g_propagate_error (error, tmp_error);
1467 g_free (close_name);
1471 case STATE_INSIDE_PASSTHROUGH:
1472 /* Possible next state: AFTER_CLOSE_ANGLE */
1475 if (*context->iter == '>')
1478 while (advance_char (context));
1480 if (context->iter == context->current_text_end)
1482 /* The passthrough hasn't necessarily ended. Merge with
1483 * partial chunk, leave state unchanged.
1485 add_to_partial (context, context->start, context->iter);
1489 /* The passthrough has ended at the close angle. Combine
1490 * it with the partial chunk if any. Call the passthrough
1491 * callback. Note that the open/close angles are
1492 * included in the text of the passthrough.
1494 GError *tmp_error = NULL;
1496 advance_char (context); /* advance past close angle */
1497 add_to_partial (context, context->start, context->iter);
1499 if (context->parser->passthrough)
1500 (*context->parser->passthrough) (context,
1501 context->partial_chunk->str,
1502 context->partial_chunk->len,
1506 truncate_partial (context);
1508 if (tmp_error == NULL)
1510 context->state = STATE_AFTER_CLOSE_ANGLE;
1511 context->start = context->iter; /* could begin text */
1515 mark_error (context, tmp_error);
1516 g_propagate_error (error, tmp_error);
1526 g_assert_not_reached ();
1532 context->parsing = FALSE;
1534 return context->state != STATE_ERROR;
1538 * g_markup_parse_context_end_parse:
1539 * @context: a #GMarkupParseContext
1540 * @error: return location for a #GError
1542 * Signals to the #GMarkupParseContext that all data has been
1543 * fed into the parse context with g_markup_parse_context_parse().
1544 * This function reports an error if the document isn't complete,
1545 * for example if elements are still open.
1547 * Return value: %TRUE on success, %FALSE if an error was set
1550 g_markup_parse_context_end_parse (GMarkupParseContext *context,
1553 g_return_val_if_fail (context != NULL, FALSE);
1554 g_return_val_if_fail (!context->parsing, FALSE);
1555 g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
1557 if (context->partial_chunk != NULL)
1559 g_string_free (context->partial_chunk, TRUE);
1560 context->partial_chunk = NULL;
1563 if (context->document_empty)
1565 set_error (context, error, G_MARKUP_ERROR_EMPTY,
1566 _("Document was empty or contained only whitespace"));
1570 context->parsing = TRUE;
1572 switch (context->state)
1578 case STATE_AFTER_OPEN_ANGLE:
1579 set_error (context, error, G_MARKUP_ERROR_PARSE,
1580 _("Document ended unexpectedly just after an open angle bracket '<'"));
1583 case STATE_AFTER_CLOSE_ANGLE:
1584 if (context->tag_stack != NULL)
1586 /* Error message the same as for INSIDE_TEXT */
1587 set_error (context, error, G_MARKUP_ERROR_PARSE,
1588 _("Document ended unexpectedly with elements still open - "
1589 "'%s' was the last element opened"),
1590 current_element (context));
1594 case STATE_AFTER_ELISION_SLASH:
1595 set_error (context, error, G_MARKUP_ERROR_PARSE,
1596 _("Document ended unexpectedly, expected to see a close angle "
1597 "bracket ending the tag <%s/>"), current_element (context));
1600 case STATE_INSIDE_OPEN_TAG_NAME:
1601 set_error (context, error, G_MARKUP_ERROR_PARSE,
1602 _("Document ended unexpectedly inside an element name"));
1605 case STATE_INSIDE_ATTRIBUTE_NAME:
1606 set_error (context, error, G_MARKUP_ERROR_PARSE,
1607 _("Document ended unexpectedly inside an attribute name"));
1610 case STATE_BETWEEN_ATTRIBUTES:
1611 set_error (context, error, G_MARKUP_ERROR_PARSE,
1612 _("Document ended unexpectedly inside an element-opening "
1616 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
1617 set_error (context, error, G_MARKUP_ERROR_PARSE,
1618 _("Document ended unexpectedly after the equals sign "
1619 "following an attribute name; no attribute value"));
1622 case STATE_INSIDE_ATTRIBUTE_VALUE_SQ:
1623 case STATE_INSIDE_ATTRIBUTE_VALUE_DQ:
1624 set_error (context, error, G_MARKUP_ERROR_PARSE,
1625 _("Document ended unexpectedly while inside an attribute "
1629 case STATE_INSIDE_TEXT:
1630 g_assert (context->tag_stack != NULL);
1631 set_error (context, error, G_MARKUP_ERROR_PARSE,
1632 _("Document ended unexpectedly with elements still open - "
1633 "'%s' was the last element opened"),
1634 current_element (context));
1637 case STATE_AFTER_CLOSE_TAG_SLASH:
1638 case STATE_INSIDE_CLOSE_TAG_NAME:
1639 set_error (context, error, G_MARKUP_ERROR_PARSE,
1640 _("Document ended unexpectedly inside the close tag for "
1641 "element '%s'"), current_element);
1644 case STATE_INSIDE_PASSTHROUGH:
1645 set_error (context, error, G_MARKUP_ERROR_PARSE,
1646 _("Document ended unexpectedly inside a comment or "
1647 "processing instruction"));
1652 g_assert_not_reached ();
1656 context->parsing = FALSE;
1658 return context->state != STATE_ERROR;
1662 * g_markup_parse_context_get_position:
1663 * @context: a #GMarkupParseContext
1664 * @line_number: return location for a line number, or %NULL
1665 * @char_number: return location for a char-on-line number, or %NULL
1667 * Retrieves the current line number and the number of the character on
1668 * that line. Intended for use in error messages; there are no strict
1669 * semantics for what constitutes the "current" line number other than
1670 * "the best number we could come up with for error messages."
1674 g_markup_parse_context_get_position (GMarkupParseContext *context,
1678 g_return_if_fail (context != NULL);
1681 *line_number = context->line_number;
1684 *char_number = context->char_number;
1688 append_escaped_text (GString *str,
1696 end = text + length;
1701 next = g_utf8_next_char (p);
1706 g_string_append (str, "&");
1710 g_string_append (str, "<");
1714 g_string_append (str, ">");
1718 g_string_append (str, "'");
1722 g_string_append (str, """);
1726 g_string_append_len (str, p, next - p);
1735 * g_markup_escape_text:
1736 * @text: some valid UTF-8 text
1737 * @length: length of @text in bytes
1739 * Escapes text so that the markup parser will parse it verbatim.
1740 * Less than, greater than, ampersand, etc. are replaced with the
1741 * corresponding entities. This function would typically be used
1742 * when writing out a file to be parsed with the markup parser.
1744 * Return value: escaped text
1747 g_markup_escape_text (const gchar *text,
1752 g_return_val_if_fail (text != NULL, NULL);
1755 length = strlen (text);
1757 str = g_string_new ("");
1758 append_escaped_text (str, text, length);
1760 return g_string_free (str, FALSE);