xref: /dflybsd-src/contrib/gdb-7/gdb/common/xml-utils.c (revision a45ae5f869d9cfcb3e41dbab486e10bfa9e336bf)
1*a45ae5f8SJohn Marino /* Shared helper routines for manipulating XML.
2*a45ae5f8SJohn Marino 
3*a45ae5f8SJohn Marino    Copyright (C) 2006-2012 Free Software Foundation, Inc.
4*a45ae5f8SJohn Marino 
5*a45ae5f8SJohn Marino    This file is part of GDB.
6*a45ae5f8SJohn Marino 
7*a45ae5f8SJohn Marino    This program is free software; you can redistribute it and/or modify
8*a45ae5f8SJohn Marino    it under the terms of the GNU General Public License as published by
9*a45ae5f8SJohn Marino    the Free Software Foundation; either version 3 of the License, or
10*a45ae5f8SJohn Marino    (at your option) any later version.
11*a45ae5f8SJohn Marino 
12*a45ae5f8SJohn Marino    This program is distributed in the hope that it will be useful,
13*a45ae5f8SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*a45ae5f8SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*a45ae5f8SJohn Marino    GNU General Public License for more details.
16*a45ae5f8SJohn Marino 
17*a45ae5f8SJohn Marino    You should have received a copy of the GNU General Public License
18*a45ae5f8SJohn Marino    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19*a45ae5f8SJohn Marino 
20*a45ae5f8SJohn Marino #ifdef GDBSERVER
21*a45ae5f8SJohn Marino #include "server.h"
22*a45ae5f8SJohn Marino #else
23*a45ae5f8SJohn Marino #include "defs.h"
24*a45ae5f8SJohn Marino #endif
25*a45ae5f8SJohn Marino 
26*a45ae5f8SJohn Marino #include "xml-utils.h"
27*a45ae5f8SJohn Marino 
28*a45ae5f8SJohn Marino #include <string.h>
29*a45ae5f8SJohn Marino 
30*a45ae5f8SJohn Marino /* Return a malloc allocated string with special characters from TEXT
31*a45ae5f8SJohn Marino    replaced by entity references.  */
32*a45ae5f8SJohn Marino 
33*a45ae5f8SJohn Marino char *
34*a45ae5f8SJohn Marino xml_escape_text (const char *text)
35*a45ae5f8SJohn Marino {
36*a45ae5f8SJohn Marino   char *result;
37*a45ae5f8SJohn Marino   int i, special;
38*a45ae5f8SJohn Marino 
39*a45ae5f8SJohn Marino   /* Compute the length of the result.  */
40*a45ae5f8SJohn Marino   for (i = 0, special = 0; text[i] != '\0'; i++)
41*a45ae5f8SJohn Marino     switch (text[i])
42*a45ae5f8SJohn Marino       {
43*a45ae5f8SJohn Marino       case '\'':
44*a45ae5f8SJohn Marino       case '\"':
45*a45ae5f8SJohn Marino 	special += 5;
46*a45ae5f8SJohn Marino 	break;
47*a45ae5f8SJohn Marino       case '&':
48*a45ae5f8SJohn Marino 	special += 4;
49*a45ae5f8SJohn Marino 	break;
50*a45ae5f8SJohn Marino       case '<':
51*a45ae5f8SJohn Marino       case '>':
52*a45ae5f8SJohn Marino 	special += 3;
53*a45ae5f8SJohn Marino 	break;
54*a45ae5f8SJohn Marino       default:
55*a45ae5f8SJohn Marino 	break;
56*a45ae5f8SJohn Marino       }
57*a45ae5f8SJohn Marino 
58*a45ae5f8SJohn Marino   /* Expand the result.  */
59*a45ae5f8SJohn Marino   result = xmalloc (i + special + 1);
60*a45ae5f8SJohn Marino   for (i = 0, special = 0; text[i] != '\0'; i++)
61*a45ae5f8SJohn Marino     switch (text[i])
62*a45ae5f8SJohn Marino       {
63*a45ae5f8SJohn Marino       case '\'':
64*a45ae5f8SJohn Marino 	strcpy (result + i + special, "&apos;");
65*a45ae5f8SJohn Marino 	special += 5;
66*a45ae5f8SJohn Marino 	break;
67*a45ae5f8SJohn Marino       case '\"':
68*a45ae5f8SJohn Marino 	strcpy (result + i + special, "&quot;");
69*a45ae5f8SJohn Marino 	special += 5;
70*a45ae5f8SJohn Marino 	break;
71*a45ae5f8SJohn Marino       case '&':
72*a45ae5f8SJohn Marino 	strcpy (result + i + special, "&amp;");
73*a45ae5f8SJohn Marino 	special += 4;
74*a45ae5f8SJohn Marino 	break;
75*a45ae5f8SJohn Marino       case '<':
76*a45ae5f8SJohn Marino 	strcpy (result + i + special, "&lt;");
77*a45ae5f8SJohn Marino 	special += 3;
78*a45ae5f8SJohn Marino 	break;
79*a45ae5f8SJohn Marino       case '>':
80*a45ae5f8SJohn Marino 	strcpy (result + i + special, "&gt;");
81*a45ae5f8SJohn Marino 	special += 3;
82*a45ae5f8SJohn Marino 	break;
83*a45ae5f8SJohn Marino       default:
84*a45ae5f8SJohn Marino 	result[i + special] = text[i];
85*a45ae5f8SJohn Marino 	break;
86*a45ae5f8SJohn Marino       }
87*a45ae5f8SJohn Marino   result[i + special] = '\0';
88*a45ae5f8SJohn Marino 
89*a45ae5f8SJohn Marino   return result;
90*a45ae5f8SJohn Marino }
91