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 (void)
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;
630 return c == ' ' || c == '\t' || c == '\n' || c == '\r';
634 skip_spaces (GMarkupParseContext *context)
638 if (!xml_isspace (*context->iter))
641 while (advance_char (context));
645 advance_to_name_end (GMarkupParseContext *context)
649 if (!is_name_char (g_utf8_get_char (context->iter)))
652 while (advance_char (context));
656 add_to_partial (GMarkupParseContext *context,
657 const gchar *text_start,
658 const gchar *text_end)
660 if (context->partial_chunk == NULL)
661 context->partial_chunk = g_string_new ("");
663 if (text_start != text_end)
664 g_string_append_len (context->partial_chunk, text_start,
665 text_end - text_start);
667 /* Invariant here that partial_chunk exists */
671 truncate_partial (GMarkupParseContext *context)
673 if (context->partial_chunk != NULL)
675 context->partial_chunk = g_string_truncate (context->partial_chunk, 0);
680 current_element (GMarkupParseContext *context)
682 return context->tag_stack->data;
686 current_attribute (GMarkupParseContext *context)
688 g_assert (context->cur_attr >= 0);
689 return context->attr_names[context->cur_attr];
693 find_current_text_end (GMarkupParseContext *context)
695 /* This function must be safe (non-segfaulting) on invalid UTF8 */
696 const gchar *end = context->current_text + context->current_text_len;
700 g_assert (context->current_text_len > 0);
702 p = context->current_text;
703 next = g_utf8_find_next_char (p, end);
708 next = g_utf8_find_next_char (p, end);
711 /* p is now the start of the last character or character portion. */
713 next = g_utf8_next_char (p); /* this only touches *p, nothing beyond */
717 /* whole character */
718 context->current_text_end = end;
723 context->leftover_char_portion = g_string_new_len (p, end - p);
724 context->current_text_len -= (end - p);
725 context->current_text_end = p;
730 add_attribute (GMarkupParseContext *context, char *name)
732 if (context->cur_attr + 2 >= context->alloc_attrs)
734 context->alloc_attrs += 5; /* silly magic number */
735 context->attr_names = g_realloc (context->attr_names, sizeof(char*)*context->alloc_attrs);
736 context->attr_values = g_realloc (context->attr_values, sizeof(char*)*context->alloc_attrs);
739 context->attr_names[context->cur_attr] = name;
740 context->attr_values[context->cur_attr] = NULL;
741 context->attr_names[context->cur_attr+1] = NULL;
742 context->attr_values[context->cur_attr+1] = NULL;
746 * g_markup_parse_context_parse:
747 * @context: a #GMarkupParseContext
748 * @text: chunk of text to parse
749 * @text_len: length of @text in bytes
750 * @error: return location for a #GError
752 * Feed some data to the #GMarkupParseContext. The data need not
753 * be valid UTF-8; an error will be signaled if it's invalid.
754 * The data need not be an entire document; you can feed a document
755 * into the parser incrementally, via multiple calls to this function.
756 * Typically, as you receive data from a network connection or file,
757 * you feed each received chunk of data into this function, aborting
758 * the process if an error occurs. Once an error is reported, no further
759 * data may be fed to the #GMarkupParseContext; all errors are fatal.
761 * Return value: %FALSE if an error occurred, %TRUE on success
764 g_markup_parse_context_parse (GMarkupParseContext *context,
769 const gchar *first_invalid;
771 g_return_val_if_fail (context != NULL, FALSE);
772 g_return_val_if_fail (text != NULL, FALSE);
773 g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
774 g_return_val_if_fail (!context->parsing, FALSE);
777 text_len = strlen (text);
782 context->parsing = TRUE;
784 if (context->leftover_char_portion)
786 const gchar *first_char;
788 if ((*text & 0xc0) != 0x80)
791 first_char = g_utf8_find_next_char (text, text + text_len);
795 /* leftover_char_portion was completed. Parse it. */
796 GString *portion = context->leftover_char_portion;
798 g_string_append_len (context->leftover_char_portion,
799 text, first_char - text);
801 /* hacks to allow recursion */
802 context->parsing = FALSE;
803 context->leftover_char_portion = NULL;
805 if (!g_markup_parse_context_parse (context,
806 portion->str, portion->len,
809 g_assert (context->state == STATE_ERROR);
812 g_string_free (portion, TRUE);
813 context->parsing = TRUE;
815 /* Skip the fraction of char that was in this text */
816 text_len -= (first_char - text);
821 /* another little chunk of the leftover char; geez
822 * someone is inefficient.
824 g_string_append_len (context->leftover_char_portion,
827 if (context->leftover_char_portion->len > 7)
829 /* The leftover char portion is too big to be
834 G_MARKUP_ERROR_BAD_UTF8,
835 _("Invalid UTF-8 encoded text"));
842 context->current_text = text;
843 context->current_text_len = text_len;
844 context->iter = context->current_text;
845 context->start = context->iter;
847 /* Nothing left after finishing the leftover char, or nothing
848 * passed in to begin with.
850 if (context->current_text_len == 0)
853 /* find_current_text_end () assumes the string starts at
854 * a character start, so we need to validate at least
855 * that much. It doesn't assume any following bytes
858 if ((*context->current_text & 0xc0) == 0x80) /* not a char start */
862 G_MARKUP_ERROR_BAD_UTF8,
863 _("Invalid UTF-8 encoded text"));
867 /* Initialize context->current_text_end, possibly adjusting
868 * current_text_len, and add any leftover char portion
870 find_current_text_end (context);
872 /* Validate UTF8 (must be done after we find the end, since
873 * we could have a trailing incomplete char)
875 if (!g_utf8_validate (context->current_text,
876 context->current_text_len,
881 p = context->current_text;
882 while (p != context->current_text_end)
889 context->line_number += newlines;
893 G_MARKUP_ERROR_BAD_UTF8,
894 _("Invalid UTF-8 encoded text"));
898 while (context->iter != context->current_text_end)
900 switch (context->state)
903 /* Possible next state: AFTER_OPEN_ANGLE */
905 g_assert (context->tag_stack == NULL);
907 /* whitespace is ignored outside of any elements */
908 skip_spaces (context);
910 if (context->iter != context->current_text_end)
912 if (*context->iter == '<')
914 /* Move after the open angle */
915 advance_char (context);
917 context->state = STATE_AFTER_OPEN_ANGLE;
919 /* this could start a passthrough */
920 context->start = context->iter;
922 /* document is now non-empty */
923 context->document_empty = FALSE;
929 G_MARKUP_ERROR_PARSE,
930 _("Document must begin with an element (e.g. <book>)"));
935 case STATE_AFTER_OPEN_ANGLE:
936 /* Possible next states: INSIDE_OPEN_TAG_NAME,
937 * AFTER_CLOSE_TAG_SLASH, INSIDE_PASSTHROUGH
939 if (*context->iter == '?' ||
940 *context->iter == '!')
942 /* include < in the passthrough */
943 const gchar *openangle = "<";
944 add_to_partial (context, openangle, openangle + 1);
945 context->start = context->iter;
946 context->state = STATE_INSIDE_PASSTHROUGH;
948 else if (*context->iter == '/')
951 advance_char (context);
953 context->state = STATE_AFTER_CLOSE_TAG_SLASH;
955 else if (is_name_start_char (g_utf8_get_char (context->iter)))
957 context->state = STATE_INSIDE_OPEN_TAG_NAME;
959 /* start of tag name */
960 context->start = context->iter;
967 G_MARKUP_ERROR_PARSE,
968 _("'%s' is not a valid character following "
969 "a '<' character; it may not begin an "
971 utf8_str (context->iter, buf));
975 /* The AFTER_CLOSE_ANGLE state is actually sort of
976 * broken, because it doesn't correspond to a range
977 * of characters in the input stream as the others do,
978 * and thus makes things harder to conceptualize
980 case STATE_AFTER_CLOSE_ANGLE:
981 /* Possible next states: INSIDE_TEXT, STATE_START */
982 if (context->tag_stack == NULL)
984 context->start = NULL;
985 context->state = STATE_START;
989 context->start = context->iter;
990 context->state = STATE_INSIDE_TEXT;
994 case STATE_AFTER_ELISION_SLASH:
995 /* Possible next state: AFTER_CLOSE_ANGLE */
998 /* We need to pop the tag stack and call the end_element
999 * function, since this is the close tag
1001 GError *tmp_error = NULL;
1003 g_assert (context->tag_stack != NULL);
1006 if (context->parser->end_element)
1007 (* context->parser->end_element) (context,
1008 context->tag_stack->data,
1014 mark_error (context, tmp_error);
1015 g_propagate_error (error, tmp_error);
1019 if (*context->iter == '>')
1021 /* move after the close angle */
1022 advance_char (context);
1023 context->state = STATE_AFTER_CLOSE_ANGLE;
1030 G_MARKUP_ERROR_PARSE,
1031 _("Odd character '%s', expected a '>' character "
1032 "to end the start tag of element '%s'"),
1033 utf8_str (context->iter, buf),
1034 current_element (context));
1038 g_free (context->tag_stack->data);
1039 context->tag_stack = g_slist_delete_link (context->tag_stack,
1040 context->tag_stack);
1044 case STATE_INSIDE_OPEN_TAG_NAME:
1045 /* Possible next states: BETWEEN_ATTRIBUTES */
1047 /* if there's a partial chunk then it's the first part of the
1048 * tag name. If there's a context->start then it's the start
1049 * of the tag name in current_text, the partial chunk goes
1050 * before that start though.
1052 advance_to_name_end (context);
1054 if (context->iter == context->current_text_end)
1056 /* The name hasn't necessarily ended. Merge with
1057 * partial chunk, leave state unchanged.
1059 add_to_partial (context, context->start, context->iter);
1063 /* The name has ended. Combine it with the partial chunk
1064 * if any; push it on the stack; enter next state.
1066 add_to_partial (context, context->start, context->iter);
1067 context->tag_stack =
1068 g_slist_prepend (context->tag_stack,
1069 g_string_free (context->partial_chunk,
1072 context->partial_chunk = NULL;
1074 context->state = STATE_BETWEEN_ATTRIBUTES;
1075 context->start = NULL;
1079 case STATE_INSIDE_ATTRIBUTE_NAME:
1080 /* Possible next states: AFTER_ATTRIBUTE_EQUALS_SIGN */
1082 /* read the full name, if we enter the equals sign state
1083 * then add the attribute to the list (without the value),
1084 * otherwise store a partial chunk to be prepended later.
1086 advance_to_name_end (context);
1088 if (context->iter == context->current_text_end)
1090 /* The name hasn't necessarily ended. Merge with
1091 * partial chunk, leave state unchanged.
1093 add_to_partial (context, context->start, context->iter);
1097 /* The name has ended. Combine it with the partial chunk
1098 * if any; push it on the stack; enter next state.
1100 add_to_partial (context, context->start, context->iter);
1102 add_attribute (context, g_string_free (context->partial_chunk, FALSE));
1104 context->partial_chunk = NULL;
1105 context->start = NULL;
1107 if (*context->iter == '=')
1109 advance_char (context);
1110 context->state = STATE_AFTER_ATTRIBUTE_EQUALS_SIGN;
1117 G_MARKUP_ERROR_PARSE,
1118 _("Odd character '%s', expected a '=' after "
1119 "attribute name '%s' of element '%s'"),
1120 utf8_str (context->iter, buf),
1121 current_attribute (context),
1122 current_element (context));
1128 case STATE_BETWEEN_ATTRIBUTES:
1129 /* Possible next states: AFTER_CLOSE_ANGLE,
1130 * AFTER_ELISION_SLASH, INSIDE_ATTRIBUTE_NAME
1132 skip_spaces (context);
1134 if (context->iter != context->current_text_end)
1136 if (*context->iter == '/')
1138 advance_char (context);
1139 context->state = STATE_AFTER_ELISION_SLASH;
1141 else if (*context->iter == '>')
1144 advance_char (context);
1145 context->state = STATE_AFTER_CLOSE_ANGLE;
1147 else if (is_name_start_char (g_utf8_get_char (context->iter)))
1149 context->state = STATE_INSIDE_ATTRIBUTE_NAME;
1150 /* start of attribute name */
1151 context->start = context->iter;
1158 G_MARKUP_ERROR_PARSE,
1159 _("Odd character '%s', expected a '>' or '/' "
1160 "character to end the start tag of "
1161 "element '%s', or optionally an attribute; "
1162 "perhaps you used an invalid character in "
1163 "an attribute name"),
1164 utf8_str (context->iter, buf),
1165 current_element (context));
1168 /* If we're done with attributes, invoke
1169 * the start_element callback
1171 if (context->state == STATE_AFTER_ELISION_SLASH ||
1172 context->state == STATE_AFTER_CLOSE_ANGLE)
1174 const gchar *start_name;
1175 /* Ugly, but the current code expects an empty array instead of NULL */
1176 const gchar *empty = NULL;
1177 const gchar **attr_names = ∅
1178 const gchar **attr_values = ∅
1181 /* Call user callback for element start */
1182 start_name = current_element (context);
1184 if (context->cur_attr >= 0)
1186 attr_names = (const gchar**)context->attr_names;
1187 attr_values = (const gchar**)context->attr_values;
1191 if (context->parser->start_element)
1192 (* context->parser->start_element) (context,
1194 (const gchar **)attr_names,
1195 (const gchar **)attr_values,
1199 /* Go ahead and free the attributes. */
1200 for (; context->cur_attr >= 0; context->cur_attr--)
1202 int pos = context->cur_attr;
1203 g_free (context->attr_names[pos]);
1204 g_free (context->attr_values[pos]);
1205 context->attr_names[pos] = context->attr_values[pos] = NULL;
1207 g_assert (context->cur_attr == -1);
1208 g_assert (context->attr_names == NULL ||
1209 context->attr_names[0] == NULL);
1210 g_assert (context->attr_values == NULL ||
1211 context->attr_values[0] == NULL);
1213 if (tmp_error != NULL)
1215 mark_error (context, tmp_error);
1216 g_propagate_error (error, tmp_error);
1222 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
1223 /* Possible next state: INSIDE_ATTRIBUTE_VALUE_[SQ/DQ] */
1224 if (*context->iter == '"')
1226 advance_char (context);
1227 context->state = STATE_INSIDE_ATTRIBUTE_VALUE_DQ;
1228 context->start = context->iter;
1230 else if (*context->iter == '\'')
1232 advance_char (context);
1233 context->state = STATE_INSIDE_ATTRIBUTE_VALUE_SQ;
1234 context->start = context->iter;
1241 G_MARKUP_ERROR_PARSE,
1242 _("Odd character '%s', expected an open quote mark "
1243 "after the equals sign when giving value for "
1244 "attribute '%s' of element '%s'"),
1245 utf8_str (context->iter, buf),
1246 current_attribute (context),
1247 current_element (context));
1251 case STATE_INSIDE_ATTRIBUTE_VALUE_SQ:
1252 case STATE_INSIDE_ATTRIBUTE_VALUE_DQ:
1253 /* Possible next states: BETWEEN_ATTRIBUTES */
1257 if (context->state == STATE_INSIDE_ATTRIBUTE_VALUE_SQ)
1268 if (*context->iter == delim)
1271 while (advance_char (context));
1273 if (context->iter == context->current_text_end)
1275 /* The value hasn't necessarily ended. Merge with
1276 * partial chunk, leave state unchanged.
1278 add_to_partial (context, context->start, context->iter);
1282 /* The value has ended at the quote mark. Combine it
1283 * with the partial chunk if any; set it for the current
1286 add_to_partial (context, context->start, context->iter);
1288 g_assert (context->cur_attr >= 0);
1290 if (unescape_text (context,
1291 context->partial_chunk->str,
1292 context->partial_chunk->str +
1293 context->partial_chunk->len,
1294 &context->attr_values[context->cur_attr],
1297 /* success, advance past quote and set state. */
1298 advance_char (context);
1299 context->state = STATE_BETWEEN_ATTRIBUTES;
1300 context->start = NULL;
1303 truncate_partial (context);
1307 case STATE_INSIDE_TEXT:
1308 /* Possible next states: AFTER_OPEN_ANGLE */
1311 if (*context->iter == '<')
1314 while (advance_char (context));
1316 /* The text hasn't necessarily ended. Merge with
1317 * partial chunk, leave state unchanged.
1320 add_to_partial (context, context->start, context->iter);
1322 if (context->iter != context->current_text_end)
1324 gchar *unescaped = NULL;
1326 /* The text has ended at the open angle. Call the text
1330 if (unescape_text (context,
1331 context->partial_chunk->str,
1332 context->partial_chunk->str +
1333 context->partial_chunk->len,
1337 GError *tmp_error = NULL;
1339 if (context->parser->text)
1340 (*context->parser->text) (context,
1348 if (tmp_error == NULL)
1350 /* advance past open angle and set state. */
1351 advance_char (context);
1352 context->state = STATE_AFTER_OPEN_ANGLE;
1353 /* could begin a passthrough */
1354 context->start = context->iter;
1358 mark_error (context, tmp_error);
1359 g_propagate_error (error, tmp_error);
1363 truncate_partial (context);
1367 case STATE_AFTER_CLOSE_TAG_SLASH:
1368 /* Possible next state: INSIDE_CLOSE_TAG_NAME */
1369 if (is_name_start_char (g_utf8_get_char (context->iter)))
1371 context->state = STATE_INSIDE_CLOSE_TAG_NAME;
1373 /* start of tag name */
1374 context->start = context->iter;
1381 G_MARKUP_ERROR_PARSE,
1382 _("'%s' is not a valid character following "
1383 "the characters '</'; '%s' may not begin an "
1385 utf8_str (context->iter, buf),
1386 utf8_str (context->iter, buf));
1390 case STATE_INSIDE_CLOSE_TAG_NAME:
1391 /* Possible next state: AFTER_CLOSE_ANGLE */
1392 advance_to_name_end (context);
1394 if (context->iter == context->current_text_end)
1396 /* The name hasn't necessarily ended. Merge with
1397 * partial chunk, leave state unchanged.
1399 add_to_partial (context, context->start, context->iter);
1403 /* The name has ended. Combine it with the partial chunk
1404 * if any; check that it matches stack top and pop
1405 * stack; invoke proper callback; enter next state.
1409 add_to_partial (context, context->start, context->iter);
1411 close_name = g_string_free (context->partial_chunk, FALSE);
1412 context->partial_chunk = NULL;
1414 if (*context->iter != '>')
1419 G_MARKUP_ERROR_PARSE,
1420 _("'%s' is not a valid character following "
1421 "the close element name '%s'; the allowed "
1422 "character is '>'"),
1423 utf8_str (context->iter, buf),
1426 else if (context->tag_stack == NULL)
1430 G_MARKUP_ERROR_PARSE,
1431 _("Element '%s' was closed, no element "
1432 "is currently open"),
1435 else if (strcmp (close_name, current_element (context)) != 0)
1439 G_MARKUP_ERROR_PARSE,
1440 _("Element '%s' was closed, but the currently "
1441 "open element is '%s'"),
1443 current_element (context));
1448 advance_char (context);
1449 context->state = STATE_AFTER_CLOSE_ANGLE;
1450 context->start = NULL;
1452 /* call the end_element callback */
1454 if (context->parser->end_element)
1455 (* context->parser->end_element) (context,
1461 /* Pop the tag stack */
1462 g_free (context->tag_stack->data);
1463 context->tag_stack = g_slist_delete_link (context->tag_stack,
1464 context->tag_stack);
1468 mark_error (context, tmp_error);
1469 g_propagate_error (error, tmp_error);
1473 g_free (close_name);
1477 case STATE_INSIDE_PASSTHROUGH:
1478 /* Possible next state: AFTER_CLOSE_ANGLE */
1481 if (*context->iter == '>')
1484 while (advance_char (context));
1486 if (context->iter == context->current_text_end)
1488 /* The passthrough hasn't necessarily ended. Merge with
1489 * partial chunk, leave state unchanged.
1491 add_to_partial (context, context->start, context->iter);
1495 /* The passthrough has ended at the close angle. Combine
1496 * it with the partial chunk if any. Call the passthrough
1497 * callback. Note that the open/close angles are
1498 * included in the text of the passthrough.
1500 GError *tmp_error = NULL;
1502 advance_char (context); /* advance past close angle */
1503 add_to_partial (context, context->start, context->iter);
1505 if (context->parser->passthrough)
1506 (*context->parser->passthrough) (context,
1507 context->partial_chunk->str,
1508 context->partial_chunk->len,
1512 truncate_partial (context);
1514 if (tmp_error == NULL)
1516 context->state = STATE_AFTER_CLOSE_ANGLE;
1517 context->start = context->iter; /* could begin text */
1521 mark_error (context, tmp_error);
1522 g_propagate_error (error, tmp_error);
1532 g_assert_not_reached ();
1538 context->parsing = FALSE;
1540 return context->state != STATE_ERROR;
1544 * g_markup_parse_context_end_parse:
1545 * @context: a #GMarkupParseContext
1546 * @error: return location for a #GError
1548 * Signals to the #GMarkupParseContext that all data has been
1549 * fed into the parse context with g_markup_parse_context_parse().
1550 * This function reports an error if the document isn't complete,
1551 * for example if elements are still open.
1553 * Return value: %TRUE on success, %FALSE if an error was set
1556 g_markup_parse_context_end_parse (GMarkupParseContext *context,
1559 g_return_val_if_fail (context != NULL, FALSE);
1560 g_return_val_if_fail (!context->parsing, FALSE);
1561 g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
1563 if (context->partial_chunk != NULL)
1565 g_string_free (context->partial_chunk, TRUE);
1566 context->partial_chunk = NULL;
1569 if (context->document_empty)
1571 set_error (context, error, G_MARKUP_ERROR_EMPTY,
1572 _("Document was empty or contained only whitespace"));
1576 context->parsing = TRUE;
1578 switch (context->state)
1584 case STATE_AFTER_OPEN_ANGLE:
1585 set_error (context, error, G_MARKUP_ERROR_PARSE,
1586 _("Document ended unexpectedly just after an open angle bracket '<'"));
1589 case STATE_AFTER_CLOSE_ANGLE:
1590 if (context->tag_stack != NULL)
1592 /* Error message the same as for INSIDE_TEXT */
1593 set_error (context, error, G_MARKUP_ERROR_PARSE,
1594 _("Document ended unexpectedly with elements still open - "
1595 "'%s' was the last element opened"),
1596 current_element (context));
1600 case STATE_AFTER_ELISION_SLASH:
1601 set_error (context, error, G_MARKUP_ERROR_PARSE,
1602 _("Document ended unexpectedly, expected to see a close angle "
1603 "bracket ending the tag <%s/>"), current_element (context));
1606 case STATE_INSIDE_OPEN_TAG_NAME:
1607 set_error (context, error, G_MARKUP_ERROR_PARSE,
1608 _("Document ended unexpectedly inside an element name"));
1611 case STATE_INSIDE_ATTRIBUTE_NAME:
1612 set_error (context, error, G_MARKUP_ERROR_PARSE,
1613 _("Document ended unexpectedly inside an attribute name"));
1616 case STATE_BETWEEN_ATTRIBUTES:
1617 set_error (context, error, G_MARKUP_ERROR_PARSE,
1618 _("Document ended unexpectedly inside an element-opening "
1622 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
1623 set_error (context, error, G_MARKUP_ERROR_PARSE,
1624 _("Document ended unexpectedly after the equals sign "
1625 "following an attribute name; no attribute value"));
1628 case STATE_INSIDE_ATTRIBUTE_VALUE_SQ:
1629 case STATE_INSIDE_ATTRIBUTE_VALUE_DQ:
1630 set_error (context, error, G_MARKUP_ERROR_PARSE,
1631 _("Document ended unexpectedly while inside an attribute "
1635 case STATE_INSIDE_TEXT:
1636 g_assert (context->tag_stack != NULL);
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));
1643 case STATE_AFTER_CLOSE_TAG_SLASH:
1644 case STATE_INSIDE_CLOSE_TAG_NAME:
1645 set_error (context, error, G_MARKUP_ERROR_PARSE,
1646 _("Document ended unexpectedly inside the close tag for "
1647 "element '%s'"), current_element);
1650 case STATE_INSIDE_PASSTHROUGH:
1651 set_error (context, error, G_MARKUP_ERROR_PARSE,
1652 _("Document ended unexpectedly inside a comment or "
1653 "processing instruction"));
1658 g_assert_not_reached ();
1662 context->parsing = FALSE;
1664 return context->state != STATE_ERROR;
1668 * g_markup_parse_context_get_position:
1669 * @context: a #GMarkupParseContext
1670 * @line_number: return location for a line number, or %NULL
1671 * @char_number: return location for a char-on-line number, or %NULL
1673 * Retrieves the current line number and the number of the character on
1674 * that line. Intended for use in error messages; there are no strict
1675 * semantics for what constitutes the "current" line number other than
1676 * "the best number we could come up with for error messages."
1680 g_markup_parse_context_get_position (GMarkupParseContext *context,
1684 g_return_if_fail (context != NULL);
1687 *line_number = context->line_number;
1690 *char_number = context->char_number;
1694 append_escaped_text (GString *str,
1702 end = text + length;
1707 next = g_utf8_next_char (p);
1712 g_string_append (str, "&");
1716 g_string_append (str, "<");
1720 g_string_append (str, ">");
1724 g_string_append (str, "'");
1728 g_string_append (str, """);
1732 g_string_append_len (str, p, next - p);
1741 * g_markup_escape_text:
1742 * @text: some valid UTF-8 text
1743 * @length: length of @text in bytes
1745 * Escapes text so that the markup parser will parse it verbatim.
1746 * Less than, greater than, ampersand, etc. are replaced with the
1747 * corresponding entities. This function would typically be used
1748 * when writing out a file to be parsed with the markup parser.
1750 * Return value: escaped text
1753 g_markup_escape_text (const gchar *text,
1758 g_return_val_if_fail (text != NULL, NULL);
1761 length = strlen (text);
1763 str = g_string_new ("");
1764 append_escaped_text (str, text, length);
1766 return g_string_free (str, FALSE);