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");
41 typedef struct _GMarkupAttribute GMarkupAttribute;
43 struct _GMarkupAttribute
49 static GMarkupAttribute*
50 attribute_new (const gchar *name, const gchar *value)
52 GMarkupAttribute *attr;
54 attr = g_new (GMarkupAttribute, 1);
56 /* name/value are allowed to be NULL */
57 attr->name = g_strdup (name);
58 attr->value = g_strdup (value);
64 attribute_free (GMarkupAttribute *attr)
74 STATE_AFTER_OPEN_ANGLE,
75 STATE_AFTER_CLOSE_ANGLE,
76 STATE_AFTER_ELISION_SLASH, /* the slash that obviates need for end element */
77 STATE_INSIDE_OPEN_TAG_NAME,
78 STATE_INSIDE_ATTRIBUTE_NAME,
79 STATE_BETWEEN_ATTRIBUTES,
80 STATE_AFTER_ATTRIBUTE_EQUALS_SIGN,
81 STATE_INSIDE_ATTRIBUTE_VALUE,
83 STATE_AFTER_CLOSE_TAG_SLASH,
84 STATE_INSIDE_CLOSE_TAG_NAME,
85 STATE_INSIDE_PASSTHROUGH,
89 struct _GMarkupParseContext
91 const GMarkupParser *parser;
93 GMarkupParseFlags flags;
99 GDestroyNotify dnotify;
101 /* A piece of character data or an element that
102 * hasn't "ended" yet so we haven't yet called
103 * the callback for it.
105 GString *partial_chunk;
107 GMarkupParseState state;
111 const gchar *current_text;
112 gint current_text_len;
113 const gchar *current_text_end;
115 GString *leftover_char_portion;
117 /* used to save the start of the last interesting thingy */
122 guint document_empty : 1;
127 * g_markup_parse_context_new:
128 * @parser: a #GMarkupParser
129 * @flags: one or more #GMarkupParseFlags
130 * @user_data: user data to pass to #GMarkupParser functions
131 * @user_data_dnotify: user data destroy notifier called when the parse context is freed
133 * Creates a new parse context. A parse context is used to parse
134 * marked-up documents. You can feed any number of documents into
135 * a context, as long as no errors occur; once an error occurs,
136 * the parse context can't continue to parse text (you have to free it
137 * and create a new parse context).
139 * Return value: a new #GMarkupParseContext
141 GMarkupParseContext *
142 g_markup_parse_context_new (const GMarkupParser *parser,
143 GMarkupParseFlags flags,
145 GDestroyNotify user_data_dnotify)
147 GMarkupParseContext *context;
149 g_return_val_if_fail (parser != NULL, NULL);
151 context = g_new (GMarkupParseContext, 1);
153 context->parser = parser;
154 context->flags = flags;
155 context->user_data = user_data;
156 context->dnotify = user_data_dnotify;
158 context->line_number = 1;
159 context->char_number = 1;
161 context->partial_chunk = NULL;
163 context->state = STATE_START;
164 context->tag_stack = NULL;
165 context->attributes = NULL;
167 context->current_text = NULL;
168 context->current_text_len = -1;
169 context->current_text_end = NULL;
170 context->leftover_char_portion = NULL;
172 context->start = NULL;
173 context->iter = NULL;
175 context->document_empty = TRUE;
176 context->parsing = FALSE;
182 * g_markup_parse_context_free:
183 * @context: a #GMarkupParseContext
185 * Frees a #GMarkupParseContext. Can't be called from inside
186 * one of the #GMarkupParser functions.
190 g_markup_parse_context_free (GMarkupParseContext *context)
192 g_return_if_fail (context != NULL);
193 g_return_if_fail (!context->parsing);
195 if (context->dnotify)
196 (* context->dnotify) (context->user_data);
198 g_slist_foreach (context->attributes, (GFunc)attribute_free, NULL);
199 g_slist_free (context->attributes);
201 g_slist_foreach (context->tag_stack, (GFunc)g_free, NULL);
202 g_slist_free (context->tag_stack);
204 if (context->partial_chunk)
205 g_string_free (context->partial_chunk, TRUE);
207 if (context->leftover_char_portion)
208 g_string_free (context->leftover_char_portion, TRUE);
214 attribute_list_to_arrays (GSList *attributes,
225 len = g_slist_length (attributes);
229 names = g_new (gchar*, len + 1);
237 values = g_new (gchar*, len + 1);
243 /* We want to reverse the list, since it's
244 * backward from the order the attributes appeared
248 tmp_list = attributes;
251 GMarkupAttribute *attr = tmp_list->data;
256 names[i] = g_strdup (attr->name);
259 values[i] = g_strdup (attr->value);
261 tmp_list = g_slist_next (tmp_list);
276 mark_error (GMarkupParseContext *context,
279 context->state = STATE_ERROR;
281 if (context->parser->error)
282 (*context->parser->error) (context, error, context->user_data);
286 set_error (GMarkupParseContext *context,
296 va_start (args, format);
297 s = g_strdup_vprintf (format, args);
300 tmp_error = g_error_new (G_MARKUP_ERROR,
302 _("Error on line %d char %d: %s"),
303 context->line_number,
304 context->char_number,
309 mark_error (context, tmp_error);
311 g_propagate_error (error, tmp_error);
315 is_name_start_char (gunichar c)
317 if (g_unichar_isalpha (c) ||
326 is_name_char (gunichar c)
328 if (g_unichar_isalnum (c) ||
340 char_str (gunichar c,
344 g_unichar_to_utf8 (c, buf);
349 utf8_str (const gchar *utf8,
352 char_str (g_utf8_get_char (utf8), buf);
357 set_unescape_error (GMarkupParseContext *context,
359 const gchar *remaining_text,
360 const gchar *remaining_text_end,
368 gint remaining_newlines;
371 remaining_newlines = 0;
373 while (p != remaining_text_end)
376 ++remaining_newlines;
380 va_start (args, format);
381 s = g_strdup_vprintf (format, args);
384 tmp_error = g_error_new (G_MARKUP_ERROR,
386 _("Error on line %d: %s"),
387 context->line_number - remaining_newlines,
392 mark_error (context, tmp_error);
394 g_propagate_error (error, tmp_error);
400 USTATE_AFTER_AMPERSAND,
401 USTATE_INSIDE_ENTITY_NAME,
402 USTATE_AFTER_CHARREF_HASH
406 unescape_text (GMarkupParseContext *context,
408 const gchar *text_end,
412 #define MAX_ENT_LEN 5
418 str = g_string_new ("");
420 state = USTATE_INSIDE_TEXT;
423 while (p != text_end && context->state != STATE_ERROR)
425 g_assert (p < text_end);
429 case USTATE_INSIDE_TEXT:
431 while (p != text_end && *p != '&')
432 p = g_utf8_next_char (p);
436 g_string_append_len (str, start, p - start);
441 if (p != text_end && *p == '&')
443 p = g_utf8_next_char (p);
444 state = USTATE_AFTER_AMPERSAND;
449 case USTATE_AFTER_AMPERSAND:
453 p = g_utf8_next_char (p);
456 state = USTATE_AFTER_CHARREF_HASH;
458 else if (!is_name_start_char (g_utf8_get_char (p)))
462 set_unescape_error (context, error,
464 G_MARKUP_ERROR_PARSE,
465 _("Empty entity '&;' seen; valid "
466 "entities are: & " < > '"));
472 set_unescape_error (context, error,
474 G_MARKUP_ERROR_PARSE,
475 _("Character '%s' is not valid at "
476 "the start of an entity name; "
477 "the & character begins an entity; "
478 "if this ampersand isn't supposed "
479 "to be an entity, escape it as "
487 state = USTATE_INSIDE_ENTITY_NAME;
493 case USTATE_INSIDE_ENTITY_NAME:
495 gchar buf[MAX_ENT_LEN+1] = {
496 '\0', '\0', '\0', '\0', '\0', '\0'
500 while (p != text_end)
504 else if (!is_name_char (*p))
508 set_unescape_error (context, error,
510 G_MARKUP_ERROR_PARSE,
511 _("Character '%s' is not valid "
512 "inside an entity name"),
517 p = g_utf8_next_char (p);
520 if (context->state != STATE_ERROR)
535 /* move to after semicolon */
536 p = g_utf8_next_char (p);
538 state = USTATE_INSIDE_TEXT;
540 if (strcmp (buf, "lt") == 0)
541 g_string_append_c (str, '<');
542 else if (strcmp (buf, "gt") == 0)
543 g_string_append_c (str, '>');
544 else if (strcmp (buf, "amp") == 0)
545 g_string_append_c (str, '&');
546 else if (strcmp (buf, "quot") == 0)
547 g_string_append_c (str, '"');
548 else if (strcmp (buf, "apos") == 0)
549 g_string_append_c (str, '\'');
552 set_unescape_error (context, error,
554 G_MARKUP_ERROR_PARSE,
555 _("Entity name '%s' is not known"),
561 set_unescape_error (context, error,
562 /* give line number of the & */
564 G_MARKUP_ERROR_PARSE,
565 _("Entity did not end with a semicolon; "
566 "most likely you used an ampersand "
567 "character without intending to start "
568 "an entity - escape ampersand as &"));
574 case USTATE_AFTER_CHARREF_HASH:
576 gboolean is_hex = FALSE;
580 p = g_utf8_next_char (p);
584 while (p != text_end && *p != ';')
585 p = g_utf8_next_char (p);
589 g_assert (*p == ';');
591 /* digit is between start and p */
595 gchar *digit = g_strndup (start, p - start);
598 gchar *digit_end = digit + (p - start);
602 l = strtoul (digit, &end, 16);
604 l = strtoul (digit, &end, 10);
606 if (end != digit_end || errno != 0)
608 set_unescape_error (context, error,
610 G_MARKUP_ERROR_PARSE,
611 _("Failed to parse '%s', which "
612 "should have been a digit "
613 "inside a character reference "
614 "(ê for example) - perhaps "
615 "the digit is too large"),
620 /* characters XML permits */
624 (l >= 0x20 && l <= 0xD7FF) ||
625 (l >= 0xE000 && l <= 0xFFFD) ||
626 (l >= 0x10000 && l <= 0x10FFFF))
629 g_string_append (str,
634 set_unescape_error (context, error,
636 G_MARKUP_ERROR_PARSE,
637 _("Character reference '%s' does not encode a permitted character"),
644 /* Move to next state */
645 p = g_utf8_next_char (p); /* past semicolon */
647 state = USTATE_INSIDE_TEXT;
651 set_unescape_error (context, error,
653 G_MARKUP_ERROR_PARSE,
654 _("Empty character reference; "
655 "should include a digit such as "
661 set_unescape_error (context, error,
663 G_MARKUP_ERROR_PARSE,
664 _("Character reference did not end with a "
666 "most likely you used an ampersand "
667 "character without intending to start "
668 "an entity - escape ampersand as &"));
674 g_assert_not_reached ();
679 /* If no errors, we should have returned to USTATE_INSIDE_TEXT */
680 g_assert (context->state == STATE_ERROR ||
681 state == USTATE_INSIDE_TEXT);
683 if (context->state == STATE_ERROR)
685 g_string_free (str, TRUE);
691 *unescaped = g_string_free (str, FALSE);
699 advance_char (GMarkupParseContext *context)
702 context->iter = g_utf8_next_char (context->iter);
703 context->char_number += 1;
704 if (*context->iter == '\n')
706 context->line_number += 1;
707 context->char_number = 1;
710 return context->iter != context->current_text_end;
714 skip_spaces (GMarkupParseContext *context)
718 if (!g_unichar_isspace (g_utf8_get_char (context->iter)))
721 while (advance_char (context));
725 advance_to_name_end (GMarkupParseContext *context)
729 if (!is_name_char (g_utf8_get_char (context->iter)))
732 while (advance_char (context));
736 add_to_partial (GMarkupParseContext *context,
737 const gchar *text_start,
738 const gchar *text_end)
740 if (context->partial_chunk == NULL)
741 context->partial_chunk = g_string_new ("");
743 if (text_start != text_end)
744 g_string_append_len (context->partial_chunk, text_start,
745 text_end - text_start);
747 /* Invariant here that partial_chunk exists */
751 free_partial (GMarkupParseContext *context)
753 if (context->partial_chunk != NULL)
755 g_string_free (context->partial_chunk, TRUE);
756 context->partial_chunk = NULL;
761 current_element (GMarkupParseContext *context)
763 return context->tag_stack->data;
767 current_attribute (GMarkupParseContext *context)
769 return ((GMarkupAttribute*)context->attributes->data)->name;
773 find_current_text_end (GMarkupParseContext *context)
775 /* This function must be safe (non-segfaulting) on invalid UTF8 */
776 const gchar *end = context->current_text + context->current_text_len;
780 g_assert (context->current_text_len > 0);
782 p = context->current_text;
783 next = g_utf8_find_next_char (p, end);
788 next = g_utf8_find_next_char (p, end);
791 /* p is now the start of the last character or character portion. */
793 next = g_utf8_next_char (p); /* this only touches *p, nothing beyond */
797 /* whole character */
798 context->current_text_end = end;
803 context->leftover_char_portion = g_string_new_len (p, end - p);
804 context->current_text_len -= (end - p);
805 context->current_text_end = p;
810 * g_markup_parse_context_parse:
811 * @context: a #GMarkupParseContext
812 * @text: chunk of text to parse
813 * @text_len: length of @text in bytes
814 * @error: return location for a #GError
816 * Feed some data to the #GMarkupParseContext. The data need not
817 * be valid UTF-8; an error will be signaled if it's invalid.
818 * The data need not be an entire document; you can feed a document
819 * into the parser incrementally, via multiple calls to this function.
820 * Typically, as you receive data from a network connection or file,
821 * you feed each received chunk of data into this function, aborting
822 * the process if an error occurs. Once an error is reported, no further
823 * data may be fed to the #GMarkupParseContext; all errors are fatal.
825 * Return value: %FALSE if an error occurred, %TRUE on success
828 g_markup_parse_context_parse (GMarkupParseContext *context,
833 const gchar *first_invalid;
835 g_return_val_if_fail (context != NULL, FALSE);
836 g_return_val_if_fail (text != NULL, FALSE);
837 g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
838 g_return_val_if_fail (!context->parsing, FALSE);
841 text_len = strlen (text);
846 context->parsing = TRUE;
848 if (context->leftover_char_portion)
850 const gchar *first_char;
852 if ((*text & 0xc0) != 0x80)
855 first_char = g_utf8_find_next_char (text, text + text_len);
859 /* leftover_char_portion was completed. Parse it. */
860 GString *portion = context->leftover_char_portion;
862 g_string_append_len (context->leftover_char_portion,
863 text, first_char - text);
865 /* hacks to allow recursion */
866 context->parsing = FALSE;
867 context->leftover_char_portion = NULL;
869 if (!g_markup_parse_context_parse (context,
870 portion->str, portion->len,
873 g_assert (context->state == STATE_ERROR);
876 g_string_free (portion, TRUE);
877 context->parsing = TRUE;
879 /* Skip the fraction of char that was in this text */
880 text_len -= (first_char - text);
885 /* another little chunk of the leftover char; geez
886 * someone is inefficient.
888 g_string_append_len (context->leftover_char_portion,
891 if (context->leftover_char_portion->len > 7)
893 /* The leftover char portion is too big to be
898 G_MARKUP_ERROR_BAD_UTF8,
899 _("Invalid UTF-8 encoded text"));
906 context->current_text = text;
907 context->current_text_len = text_len;
908 context->iter = context->current_text;
909 context->start = context->iter;
911 /* Nothing left after finishing the leftover char, or nothing
912 * passed in to begin with.
914 if (context->current_text_len == 0)
917 /* find_current_text_end () assumes the string starts at
918 * a character start, so we need to validate at least
919 * that much. It doesn't assume any following bytes
922 if ((*context->current_text & 0xc0) == 0x80) /* not a char start */
926 G_MARKUP_ERROR_BAD_UTF8,
927 _("Invalid UTF-8 encoded text"));
931 /* Initialize context->current_text_end, possibly adjusting
932 * current_text_len, and add any leftover char portion
934 find_current_text_end (context);
936 /* Validate UTF8 (must be done after we find the end, since
937 * we could have a trailing incomplete char)
939 if (!g_utf8_validate (context->current_text,
940 context->current_text_len,
945 p = context->current_text;
946 while (p != context->current_text_end)
953 context->line_number += newlines;
957 G_MARKUP_ERROR_BAD_UTF8,
958 _("Invalid UTF-8 encoded text"));
962 while (context->iter != context->current_text_end)
964 switch (context->state)
967 /* Possible next state: AFTER_OPEN_ANGLE */
969 g_assert (context->tag_stack == NULL);
971 /* whitespace is ignored outside of any elements */
972 skip_spaces (context);
974 if (context->iter != context->current_text_end)
976 if (*context->iter == '<')
978 /* Move after the open angle */
979 advance_char (context);
981 context->state = STATE_AFTER_OPEN_ANGLE;
983 /* this could start a passthrough */
984 context->start = context->iter;
986 /* document is now non-empty */
987 context->document_empty = FALSE;
993 G_MARKUP_ERROR_PARSE,
994 _("Document must begin with an element (e.g. <book>)"));
999 case STATE_AFTER_OPEN_ANGLE:
1000 /* Possible next states: INSIDE_OPEN_TAG_NAME,
1001 * AFTER_CLOSE_TAG_SLASH, INSIDE_PASSTHROUGH
1003 if (*context->iter == '?' ||
1004 *context->iter == '!')
1006 /* include < in the passthrough */
1007 const gchar *openangle = "<";
1008 add_to_partial (context, openangle, openangle + 1);
1009 context->start = context->iter;
1010 context->state = STATE_INSIDE_PASSTHROUGH;
1012 else if (*context->iter == '/')
1015 advance_char (context);
1017 context->state = STATE_AFTER_CLOSE_TAG_SLASH;
1019 else if (is_name_start_char (g_utf8_get_char (context->iter)))
1021 context->state = STATE_INSIDE_OPEN_TAG_NAME;
1023 /* start of tag name */
1024 context->start = context->iter;
1031 G_MARKUP_ERROR_PARSE,
1032 _("'%s' is not a valid character following "
1033 "a '<' character; it may not begin an "
1035 utf8_str (context->iter, buf));
1039 /* The AFTER_CLOSE_ANGLE state is actually sort of
1040 * broken, because it doesn't correspond to a range
1041 * of characters in the input stream as the others do,
1042 * and thus makes things harder to conceptualize
1044 case STATE_AFTER_CLOSE_ANGLE:
1045 /* Possible next states: INSIDE_TEXT, STATE_START */
1046 if (context->tag_stack == NULL)
1048 context->start = NULL;
1049 context->state = STATE_START;
1053 context->start = context->iter;
1054 context->state = STATE_INSIDE_TEXT;
1058 case STATE_AFTER_ELISION_SLASH:
1059 /* Possible next state: AFTER_CLOSE_ANGLE */
1062 /* We need to pop the tag stack and call the end_element
1063 * function, since this is the close tag
1065 GError *tmp_error = NULL;
1067 g_assert (context->tag_stack != NULL);
1070 if (context->parser->end_element)
1071 (* context->parser->end_element) (context,
1072 context->tag_stack->data,
1076 g_free (context->tag_stack->data);
1077 context->tag_stack = g_slist_delete_link (context->tag_stack,
1078 context->tag_stack);
1082 mark_error (context, tmp_error);
1083 g_propagate_error (error, tmp_error);
1087 if (*context->iter == '>')
1089 /* move after the close angle */
1090 advance_char (context);
1091 context->state = STATE_AFTER_CLOSE_ANGLE;
1098 G_MARKUP_ERROR_PARSE,
1099 _("Odd character '%s', expected a '>' character "
1100 "to end the start tag of element '%s'"),
1101 utf8_str (context->iter, buf),
1102 current_element (context));
1108 case STATE_INSIDE_OPEN_TAG_NAME:
1109 /* Possible next states: BETWEEN_ATTRIBUTES */
1111 /* if there's a partial chunk then it's the first part of the
1112 * tag name. If there's a context->start then it's the start
1113 * of the tag name in current_text, the partial chunk goes
1114 * before that start though.
1116 advance_to_name_end (context);
1118 if (context->iter == context->current_text_end)
1120 /* The name hasn't necessarily ended. Merge with
1121 * partial chunk, leave state unchanged.
1123 add_to_partial (context, context->start, context->iter);
1127 /* The name has ended. Combine it with the partial chunk
1128 * if any; push it on the stack; enter next state.
1130 add_to_partial (context, context->start, context->iter);
1131 context->tag_stack =
1132 g_slist_prepend (context->tag_stack,
1133 g_string_free (context->partial_chunk,
1136 context->partial_chunk = NULL;
1138 context->state = STATE_BETWEEN_ATTRIBUTES;
1139 context->start = NULL;
1143 case STATE_INSIDE_ATTRIBUTE_NAME:
1144 /* Possible next states: AFTER_ATTRIBUTE_EQUALS_SIGN */
1146 /* read the full name, if we enter the equals sign state
1147 * then add the attribute to the list (without the value),
1148 * otherwise store a partial chunk to be prepended later.
1150 advance_to_name_end (context);
1152 if (context->iter == context->current_text_end)
1154 /* The name hasn't necessarily ended. Merge with
1155 * partial chunk, leave state unchanged.
1157 add_to_partial (context, context->start, context->iter);
1161 /* The name has ended. Combine it with the partial chunk
1162 * if any; push it on the stack; enter next state.
1164 GMarkupAttribute *attr;
1165 add_to_partial (context, context->start, context->iter);
1167 attr = attribute_new (NULL, NULL);
1169 attr->name = g_string_free (context->partial_chunk,
1172 context->partial_chunk = NULL;
1173 context->start = NULL;
1175 context->attributes =
1176 g_slist_prepend (context->attributes, attr);
1178 if (*context->iter == '=')
1180 advance_char (context);
1181 context->state = STATE_AFTER_ATTRIBUTE_EQUALS_SIGN;
1188 G_MARKUP_ERROR_PARSE,
1189 _("Odd character '%s', expected a '=' after "
1190 "attribute name '%s' of element '%s'"),
1191 utf8_str (context->iter, buf),
1193 current_element (context));
1199 case STATE_BETWEEN_ATTRIBUTES:
1200 /* Possible next states: AFTER_CLOSE_ANGLE,
1201 * AFTER_ELISION_SLASH, INSIDE_ATTRIBUTE_NAME
1203 skip_spaces (context);
1205 if (context->iter != context->current_text_end)
1207 if (*context->iter == '/')
1209 advance_char (context);
1210 context->state = STATE_AFTER_ELISION_SLASH;
1212 else if (*context->iter == '>')
1215 advance_char (context);
1216 context->state = STATE_AFTER_CLOSE_ANGLE;
1218 else if (is_name_start_char (g_utf8_get_char (context->iter)))
1220 context->state = STATE_INSIDE_ATTRIBUTE_NAME;
1221 /* start of attribute name */
1222 context->start = context->iter;
1229 G_MARKUP_ERROR_PARSE,
1230 _("Odd character '%s', expected a '>' or '/' "
1231 "character to end the start tag of "
1232 "element '%s', or optionally an attribute; "
1233 "perhaps you used an invalid character in "
1234 "an attribute name"),
1235 utf8_str (context->iter, buf),
1236 current_element (context));
1239 /* If we're done with attributes, invoke
1240 * the start_element callback
1242 if (context->state == STATE_AFTER_ELISION_SLASH ||
1243 context->state == STATE_AFTER_CLOSE_ANGLE)
1245 const gchar *start_name;
1246 gchar **attr_names = NULL;
1247 gchar **attr_values = NULL;
1250 /* Call user callback for element start */
1251 start_name = current_element (context);
1253 /* this gratuituously copies the attr names/values
1256 attribute_list_to_arrays (context->attributes,
1262 if (context->parser->start_element)
1263 (* context->parser->start_element) (context,
1265 (const gchar **)attr_names,
1266 (const gchar **)attr_values,
1270 g_strfreev (attr_names);
1271 g_strfreev (attr_values);
1273 /* Go ahead and free this. */
1274 g_slist_foreach (context->attributes, (GFunc)attribute_free,
1276 g_slist_free (context->attributes);
1277 context->attributes = NULL;
1279 if (tmp_error != NULL)
1281 mark_error (context, tmp_error);
1282 g_propagate_error (error, tmp_error);
1288 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
1289 /* Possible next state: INSIDE_ATTRIBUTE_VALUE */
1290 if (*context->iter == '"')
1292 advance_char (context);
1293 context->state = STATE_INSIDE_ATTRIBUTE_VALUE;
1294 context->start = context->iter;
1301 G_MARKUP_ERROR_PARSE,
1302 _("Odd character '%s', expected an open quote mark "
1303 "after the equals sign when giving value for "
1304 "attribute '%s' of element '%s'"),
1305 utf8_str (context->iter, buf),
1306 current_attribute (context),
1307 current_element (context));
1311 case STATE_INSIDE_ATTRIBUTE_VALUE:
1312 /* Possible next states: BETWEEN_ATTRIBUTES */
1315 if (*context->iter == '"')
1318 while (advance_char (context));
1320 if (context->iter == context->current_text_end)
1322 /* The value hasn't necessarily ended. Merge with
1323 * partial chunk, leave state unchanged.
1325 add_to_partial (context, context->start, context->iter);
1329 /* The value has ended at the quote mark. Combine it
1330 * with the partial chunk if any; set it for the current
1333 GMarkupAttribute *attr;
1335 add_to_partial (context, context->start, context->iter);
1337 attr = context->attributes->data;
1339 if (unescape_text (context,
1340 context->partial_chunk->str,
1341 context->partial_chunk->str +
1342 context->partial_chunk->len,
1346 /* success, advance past quote and set state. */
1347 advance_char (context);
1348 context->state = STATE_BETWEEN_ATTRIBUTES;
1349 context->start = NULL;
1352 free_partial (context);
1356 case STATE_INSIDE_TEXT:
1357 /* Possible next states: AFTER_OPEN_ANGLE */
1360 if (*context->iter == '<')
1363 while (advance_char (context));
1365 /* The text hasn't necessarily ended. Merge with
1366 * partial chunk, leave state unchanged.
1369 add_to_partial (context, context->start, context->iter);
1371 if (context->iter != context->current_text_end)
1373 gchar *unescaped = NULL;
1375 /* The text has ended at the open angle. Call the text
1379 if (unescape_text (context,
1380 context->partial_chunk->str,
1381 context->partial_chunk->str +
1382 context->partial_chunk->len,
1386 GError *tmp_error = NULL;
1388 if (context->parser->text)
1389 (*context->parser->text) (context,
1397 if (tmp_error == NULL)
1399 /* advance past open angle and set state. */
1400 advance_char (context);
1401 context->state = STATE_AFTER_OPEN_ANGLE;
1402 /* could begin a passthrough */
1403 context->start = context->iter;
1407 mark_error (context, tmp_error);
1408 g_propagate_error (error, tmp_error);
1412 free_partial (context);
1416 case STATE_AFTER_CLOSE_TAG_SLASH:
1417 /* Possible next state: INSIDE_CLOSE_TAG_NAME */
1418 if (is_name_start_char (g_utf8_get_char (context->iter)))
1420 context->state = STATE_INSIDE_CLOSE_TAG_NAME;
1422 /* start of tag name */
1423 context->start = context->iter;
1430 G_MARKUP_ERROR_PARSE,
1431 _("'%s' is not a valid character following "
1432 "the characters '</'; '%s' may not begin an "
1434 utf8_str (context->iter, buf),
1435 utf8_str (context->iter, buf));
1439 case STATE_INSIDE_CLOSE_TAG_NAME:
1440 /* Possible next state: AFTER_CLOSE_ANGLE */
1441 advance_to_name_end (context);
1443 if (context->iter == context->current_text_end)
1445 /* The name hasn't necessarily ended. Merge with
1446 * partial chunk, leave state unchanged.
1448 add_to_partial (context, context->start, context->iter);
1452 /* The name has ended. Combine it with the partial chunk
1453 * if any; check that it matches stack top and pop
1454 * stack; invoke proper callback; enter next state.
1458 add_to_partial (context, context->start, context->iter);
1460 close_name = g_string_free (context->partial_chunk, FALSE);
1461 context->partial_chunk = NULL;
1463 if (context->tag_stack == NULL)
1467 G_MARKUP_ERROR_PARSE,
1468 _("Element '%s' was closed, no element "
1469 "is currently open"),
1472 else if (strcmp (close_name, current_element (context)) != 0)
1476 G_MARKUP_ERROR_PARSE,
1477 _("Element '%s' was closed, but the currently "
1478 "open element is '%s'"),
1480 current_element (context));
1482 else if (*context->iter != '>')
1487 G_MARKUP_ERROR_PARSE,
1488 _("'%s' is not a valid character following "
1489 "the close element name '%s'; the allowed "
1490 "character is '>'"),
1491 utf8_str (context->iter, buf),
1497 advance_char (context);
1498 context->state = STATE_AFTER_CLOSE_ANGLE;
1499 context->start = NULL;
1501 /* call the end_element callback */
1503 if (context->parser->end_element)
1504 (* context->parser->end_element) (context,
1510 /* Pop the tag stack */
1511 g_free (context->tag_stack->data);
1512 context->tag_stack = g_slist_delete_link (context->tag_stack,
1513 context->tag_stack);
1517 mark_error (context, tmp_error);
1518 g_propagate_error (error, tmp_error);
1522 g_free (close_name);
1526 case STATE_INSIDE_PASSTHROUGH:
1527 /* Possible next state: AFTER_CLOSE_ANGLE */
1530 if (*context->iter == '>')
1533 while (advance_char (context));
1535 if (context->iter == context->current_text_end)
1537 /* The passthrough hasn't necessarily ended. Merge with
1538 * partial chunk, leave state unchanged.
1540 add_to_partial (context, context->start, context->iter);
1544 /* The passthrough has ended at the close angle. Combine
1545 * it with the partial chunk if any. Call the passthrough
1546 * callback. Note that the open/close angles are
1547 * included in the text of the passthrough.
1549 GError *tmp_error = NULL;
1551 advance_char (context); /* advance past close angle */
1552 add_to_partial (context, context->start, context->iter);
1554 if (context->parser->passthrough)
1555 (*context->parser->passthrough) (context,
1556 context->partial_chunk->str,
1557 context->partial_chunk->len,
1561 free_partial (context);
1563 if (tmp_error == NULL)
1565 context->state = STATE_AFTER_CLOSE_ANGLE;
1566 context->start = context->iter; /* could begin text */
1570 mark_error (context, tmp_error);
1571 g_propagate_error (error, tmp_error);
1581 g_assert_not_reached ();
1587 context->parsing = FALSE;
1589 return context->state != STATE_ERROR;
1593 * g_markup_parse_context_end_parse:
1594 * @context: a #GMarkupParseContext
1595 * @error: return location for a #GError
1597 * Signals to the #GMarkupParseContext that all data has been
1598 * fed into the parse context with g_markup_parse_context_parse().
1599 * This function reports an error if the document isn't complete,
1600 * for example if elements are still open.
1602 * Return value: %TRUE on success, %FALSE if an error was set
1605 g_markup_parse_context_end_parse (GMarkupParseContext *context,
1608 g_return_val_if_fail (context != NULL, FALSE);
1609 g_return_val_if_fail (!context->parsing, FALSE);
1610 g_return_val_if_fail (context->state != STATE_ERROR, FALSE);
1612 if (context->document_empty)
1614 set_error (context, error, G_MARKUP_ERROR_EMPTY,
1615 _("Document was empty or contained only whitespace"));
1619 context->parsing = TRUE;
1621 switch (context->state)
1627 case STATE_AFTER_OPEN_ANGLE:
1628 set_error (context, error, G_MARKUP_ERROR_PARSE,
1629 _("Document ended unexpectedly just after an open angle bracket '<'"));
1632 case STATE_AFTER_CLOSE_ANGLE:
1633 if (context->tag_stack != NULL)
1635 /* Error message the same as for INSIDE_TEXT */
1636 set_error (context, error, G_MARKUP_ERROR_PARSE,
1637 _("Document ended unexpectedly with elements still open - "
1638 "'%s' was the last element opened"),
1639 current_element (context));
1643 case STATE_AFTER_ELISION_SLASH:
1644 set_error (context, error, G_MARKUP_ERROR_PARSE,
1645 _("Document ended unexpectedly, expected to see a close angle "
1646 "bracket ending the tag <%s/>"), current_element (context));
1649 case STATE_INSIDE_OPEN_TAG_NAME:
1650 set_error (context, error, G_MARKUP_ERROR_PARSE,
1651 _("Document ended unexpectedly inside an element name"));
1654 case STATE_INSIDE_ATTRIBUTE_NAME:
1655 set_error (context, error, G_MARKUP_ERROR_PARSE,
1656 _("Document ended unexpectedly inside an attribute name"));
1659 case STATE_BETWEEN_ATTRIBUTES:
1660 set_error (context, error, G_MARKUP_ERROR_PARSE,
1661 _("Document ended unexpectedly inside an element-opening "
1665 case STATE_AFTER_ATTRIBUTE_EQUALS_SIGN:
1666 set_error (context, error, G_MARKUP_ERROR_PARSE,
1667 _("Document ended unexpectedly after the equals sign "
1668 "following an attribute name; no attribute value"));
1671 case STATE_INSIDE_ATTRIBUTE_VALUE:
1672 set_error (context, error, G_MARKUP_ERROR_PARSE,
1673 _("Document ended unexpectedly while inside an attribute "
1677 case STATE_INSIDE_TEXT:
1678 g_assert (context->tag_stack != NULL);
1679 set_error (context, error, G_MARKUP_ERROR_PARSE,
1680 _("Document ended unexpectedly with elements still open - "
1681 "'%s' was the last element opened"),
1682 current_element (context));
1685 case STATE_AFTER_CLOSE_TAG_SLASH:
1686 case STATE_INSIDE_CLOSE_TAG_NAME:
1687 set_error (context, error, G_MARKUP_ERROR_PARSE,
1688 _("Document ended unexpectedly inside the close tag for"
1689 "element '%s'"), current_element);
1692 case STATE_INSIDE_PASSTHROUGH:
1693 set_error (context, error, G_MARKUP_ERROR_PARSE,
1694 _("Document ended unexpectedly inside a comment or "
1695 "processing instruction"));
1700 g_assert_not_reached ();
1704 context->parsing = FALSE;
1706 return context->state != STATE_ERROR;
1710 * g_markup_parse_context_get_position:
1711 * @context: a #GMarkupParseContext
1712 * @line_number: return location for a line number, or %NULL
1713 * @char_number: return location for a char-on-line number, or %NULL
1715 * Retrieves the current line number and the number of the character on
1716 * that line. Intended for use in error messages; there are no strict
1717 * semantics for what constitutes the "current" line number other than
1718 * "the best number we could come up with for error messages."
1722 g_markup_parse_context_get_position (GMarkupParseContext *context,
1726 g_return_if_fail (context != NULL);
1729 *line_number = context->line_number;
1732 *char_number = context->char_number;
1736 append_escaped_text (GString *str,
1744 end = text + length;
1749 next = g_utf8_next_char (p);
1754 g_string_append (str, "&");
1758 g_string_append (str, "<");
1762 g_string_append (str, ">");
1766 g_string_append (str, "'");
1770 g_string_append (str, """);
1774 g_string_append_len (str, p, next - p);
1783 * g_markup_escape_text:
1784 * @text: some valid UTF-8 text
1785 * @length: length of @text in bytes
1787 * Escapes text so that the markup parser will parse it verbatim.
1788 * Less than, greater than, ampersand, etc. are replaced with the
1789 * corresponding entities. This function would typically be used
1790 * when writing out a file to be parsed with the markup parser.
1792 * Return value: escaped text
1795 g_markup_escape_text (const gchar *text,
1800 g_return_val_if_fail (text != NULL, NULL);
1803 length = strlen (text);
1805 str = g_string_new ("");
1806 append_escaped_text (str, text, length);
1808 return g_string_free (str, FALSE);