xref: /dflybsd-src/contrib/gdb-7/gdb/common/buffer.c (revision de8e141f24382815c10a4012d209bbbf7abf1112)
1a45ae5f8SJohn Marino /* A simple growing buffer for GDB.
2a45ae5f8SJohn Marino 
3*ef5ccd6cSJohn Marino    Copyright (C) 2009-2013 Free Software Foundation, Inc.
4a45ae5f8SJohn Marino 
5a45ae5f8SJohn Marino    This file is part of GDB.
6a45ae5f8SJohn Marino 
7a45ae5f8SJohn Marino    This program is free software; you can redistribute it and/or modify
8a45ae5f8SJohn Marino    it under the terms of the GNU General Public License as published by
9a45ae5f8SJohn Marino    the Free Software Foundation; either version 3 of the License, or
10a45ae5f8SJohn Marino    (at your option) any later version.
11a45ae5f8SJohn Marino 
12a45ae5f8SJohn Marino    This program is distributed in the hope that it will be useful,
13a45ae5f8SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
14a45ae5f8SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15a45ae5f8SJohn Marino    GNU General Public License for more details.
16a45ae5f8SJohn Marino 
17a45ae5f8SJohn Marino    You should have received a copy of the GNU General Public License
18a45ae5f8SJohn Marino    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19a45ae5f8SJohn Marino 
20a45ae5f8SJohn Marino #ifdef GDBSERVER
21a45ae5f8SJohn Marino #include "server.h"
22a45ae5f8SJohn Marino #else
23a45ae5f8SJohn Marino #include "defs.h"
24a45ae5f8SJohn Marino #endif
25a45ae5f8SJohn Marino 
26a45ae5f8SJohn Marino #include "xml-utils.h"
27a45ae5f8SJohn Marino #include "buffer.h"
28*ef5ccd6cSJohn Marino #include "inttypes.h"
29a45ae5f8SJohn Marino 
30a45ae5f8SJohn Marino #include <stdlib.h>
31a45ae5f8SJohn Marino #include <string.h>
32a45ae5f8SJohn Marino #include <stdio.h>
33*ef5ccd6cSJohn Marino #include <stdint.h>
34a45ae5f8SJohn Marino 
35a45ae5f8SJohn Marino void
buffer_grow(struct buffer * buffer,const char * data,size_t size)36a45ae5f8SJohn Marino buffer_grow (struct buffer *buffer, const char *data, size_t size)
37a45ae5f8SJohn Marino {
38a45ae5f8SJohn Marino   char *new_buffer;
39a45ae5f8SJohn Marino   size_t new_buffer_size;
40a45ae5f8SJohn Marino 
41a45ae5f8SJohn Marino   if (size == 0)
42a45ae5f8SJohn Marino     return;
43a45ae5f8SJohn Marino 
44a45ae5f8SJohn Marino   new_buffer_size = buffer->buffer_size;
45a45ae5f8SJohn Marino 
46a45ae5f8SJohn Marino   if (new_buffer_size == 0)
47a45ae5f8SJohn Marino     new_buffer_size = 1;
48a45ae5f8SJohn Marino 
49a45ae5f8SJohn Marino   while (buffer->used_size + size > new_buffer_size)
50a45ae5f8SJohn Marino     new_buffer_size *= 2;
51a45ae5f8SJohn Marino   new_buffer = xrealloc (buffer->buffer, new_buffer_size);
52a45ae5f8SJohn Marino   memcpy (new_buffer + buffer->used_size, data, size);
53a45ae5f8SJohn Marino   buffer->buffer = new_buffer;
54a45ae5f8SJohn Marino   buffer->buffer_size = new_buffer_size;
55a45ae5f8SJohn Marino   buffer->used_size += size;
56a45ae5f8SJohn Marino }
57a45ae5f8SJohn Marino 
58a45ae5f8SJohn Marino void
buffer_free(struct buffer * buffer)59a45ae5f8SJohn Marino buffer_free (struct buffer *buffer)
60a45ae5f8SJohn Marino {
61a45ae5f8SJohn Marino   if (!buffer)
62a45ae5f8SJohn Marino     return;
63a45ae5f8SJohn Marino 
64a45ae5f8SJohn Marino   xfree (buffer->buffer);
65a45ae5f8SJohn Marino   buffer->buffer = NULL;
66a45ae5f8SJohn Marino   buffer->buffer_size = 0;
67a45ae5f8SJohn Marino   buffer->used_size = 0;
68a45ae5f8SJohn Marino }
69a45ae5f8SJohn Marino 
70a45ae5f8SJohn Marino void
buffer_init(struct buffer * buffer)71a45ae5f8SJohn Marino buffer_init (struct buffer *buffer)
72a45ae5f8SJohn Marino {
73a45ae5f8SJohn Marino   memset (buffer, 0, sizeof (*buffer));
74a45ae5f8SJohn Marino }
75a45ae5f8SJohn Marino 
76a45ae5f8SJohn Marino char*
buffer_finish(struct buffer * buffer)77a45ae5f8SJohn Marino buffer_finish (struct buffer *buffer)
78a45ae5f8SJohn Marino {
79a45ae5f8SJohn Marino   char *ret = buffer->buffer;
80a45ae5f8SJohn Marino   buffer->buffer = NULL;
81a45ae5f8SJohn Marino   buffer->buffer_size = 0;
82a45ae5f8SJohn Marino   buffer->used_size = 0;
83a45ae5f8SJohn Marino   return ret;
84a45ae5f8SJohn Marino }
85a45ae5f8SJohn Marino 
86a45ae5f8SJohn Marino void
buffer_xml_printf(struct buffer * buffer,const char * format,...)87a45ae5f8SJohn Marino buffer_xml_printf (struct buffer *buffer, const char *format, ...)
88a45ae5f8SJohn Marino {
89a45ae5f8SJohn Marino   va_list ap;
90a45ae5f8SJohn Marino   const char *f;
91a45ae5f8SJohn Marino   const char *prev;
92a45ae5f8SJohn Marino   int percent = 0;
93a45ae5f8SJohn Marino 
94a45ae5f8SJohn Marino   va_start (ap, format);
95a45ae5f8SJohn Marino 
96a45ae5f8SJohn Marino   prev = format;
97a45ae5f8SJohn Marino   for (f = format; *f; f++)
98a45ae5f8SJohn Marino     {
99a45ae5f8SJohn Marino       if (percent)
100a45ae5f8SJohn Marino 	{
101a45ae5f8SJohn Marino 	  char buf[32];
102a45ae5f8SJohn Marino 	  char *p;
103a45ae5f8SJohn Marino 	  char *str = buf;
104*ef5ccd6cSJohn Marino 	  const char *f_old = f;
105a45ae5f8SJohn Marino 
106a45ae5f8SJohn Marino 	  switch (*f)
107a45ae5f8SJohn Marino 	    {
108a45ae5f8SJohn Marino 	    case 's':
109a45ae5f8SJohn Marino 	      str = va_arg (ap, char *);
110a45ae5f8SJohn Marino 	      break;
111a45ae5f8SJohn Marino 	    case 'd':
112a45ae5f8SJohn Marino 	      sprintf (str, "%d", va_arg (ap, int));
113a45ae5f8SJohn Marino 	      break;
114a45ae5f8SJohn Marino 	    case 'u':
115a45ae5f8SJohn Marino 	      sprintf (str, "%u", va_arg (ap, unsigned int));
116a45ae5f8SJohn Marino 	      break;
117a45ae5f8SJohn Marino 	    case 'x':
118a45ae5f8SJohn Marino 	      sprintf (str, "%x", va_arg (ap, unsigned int));
119a45ae5f8SJohn Marino 	      break;
120a45ae5f8SJohn Marino 	    case 'o':
121a45ae5f8SJohn Marino 	      sprintf (str, "%o", va_arg (ap, unsigned int));
122a45ae5f8SJohn Marino 	      break;
123*ef5ccd6cSJohn Marino 	    case 'l':
124*ef5ccd6cSJohn Marino 	      f++;
125*ef5ccd6cSJohn Marino 	      switch (*f)
126*ef5ccd6cSJohn Marino 		{
127*ef5ccd6cSJohn Marino 		case 'd':
128*ef5ccd6cSJohn Marino 		  sprintf (str, "%ld", va_arg (ap, long));
129*ef5ccd6cSJohn Marino 		  break;
130*ef5ccd6cSJohn Marino 		case 'u':
131*ef5ccd6cSJohn Marino 		  sprintf (str, "%lu", va_arg (ap, unsigned long));
132*ef5ccd6cSJohn Marino 		  break;
133*ef5ccd6cSJohn Marino 		case 'x':
134*ef5ccd6cSJohn Marino 		  sprintf (str, "%lx", va_arg (ap, unsigned long));
135*ef5ccd6cSJohn Marino 		  break;
136*ef5ccd6cSJohn Marino 		case 'o':
137*ef5ccd6cSJohn Marino 		  sprintf (str, "%lo", va_arg (ap, unsigned long));
138*ef5ccd6cSJohn Marino 		  break;
139*ef5ccd6cSJohn Marino 		case 'l':
140*ef5ccd6cSJohn Marino 		  f++;
141*ef5ccd6cSJohn Marino 		  switch (*f)
142*ef5ccd6cSJohn Marino 		    {
143*ef5ccd6cSJohn Marino 		    case 'd':
144*ef5ccd6cSJohn Marino 		      sprintf (str, "%" PRId64,
145*ef5ccd6cSJohn Marino 			       (int64_t) va_arg (ap, long long));
146*ef5ccd6cSJohn Marino 		      break;
147*ef5ccd6cSJohn Marino 		    case 'u':
148*ef5ccd6cSJohn Marino 		      sprintf (str, "%" PRIu64,
149*ef5ccd6cSJohn Marino 			       (uint64_t) va_arg (ap, unsigned long long));
150*ef5ccd6cSJohn Marino 		      break;
151*ef5ccd6cSJohn Marino 		    case 'x':
152*ef5ccd6cSJohn Marino 		      sprintf (str, "%" PRIx64,
153*ef5ccd6cSJohn Marino 			       (uint64_t) va_arg (ap, unsigned long long));
154*ef5ccd6cSJohn Marino 		      break;
155*ef5ccd6cSJohn Marino 		    case 'o':
156*ef5ccd6cSJohn Marino 		      sprintf (str, "%" PRIo64,
157*ef5ccd6cSJohn Marino 			       (uint64_t) va_arg (ap, unsigned long long));
158*ef5ccd6cSJohn Marino 		      break;
159*ef5ccd6cSJohn Marino 		    default:
160*ef5ccd6cSJohn Marino 		      str = 0;
161*ef5ccd6cSJohn Marino 		      break;
162*ef5ccd6cSJohn Marino 		    }
163*ef5ccd6cSJohn Marino 		  break;
164*ef5ccd6cSJohn Marino 		default:
165*ef5ccd6cSJohn Marino 		  str = 0;
166*ef5ccd6cSJohn Marino 		  break;
167*ef5ccd6cSJohn Marino 		}
168*ef5ccd6cSJohn Marino 	      break;
169a45ae5f8SJohn Marino 	    default:
170a45ae5f8SJohn Marino 	      str = 0;
171a45ae5f8SJohn Marino 	      break;
172a45ae5f8SJohn Marino 	    }
173a45ae5f8SJohn Marino 
174a45ae5f8SJohn Marino 	  if (str)
175a45ae5f8SJohn Marino 	    {
176*ef5ccd6cSJohn Marino 	      buffer_grow (buffer, prev, f_old - prev - 1);
177a45ae5f8SJohn Marino 	      p = xml_escape_text (str);
178a45ae5f8SJohn Marino 	      buffer_grow_str (buffer, p);
179a45ae5f8SJohn Marino 	      xfree (p);
180a45ae5f8SJohn Marino 	      prev = f + 1;
181a45ae5f8SJohn Marino 	    }
182a45ae5f8SJohn Marino 	  percent = 0;
183a45ae5f8SJohn Marino 	}
184a45ae5f8SJohn Marino       else if (*f == '%')
185a45ae5f8SJohn Marino 	percent = 1;
186a45ae5f8SJohn Marino     }
187a45ae5f8SJohn Marino 
188a45ae5f8SJohn Marino   buffer_grow_str (buffer, prev);
189a45ae5f8SJohn Marino   va_end (ap);
190a45ae5f8SJohn Marino }
191a45ae5f8SJohn Marino 
192