Updated Spanish translation
[anjuta-git-plugin.git] / plugins / gdb / utilities.c
bloba8f2883c0055731074bf07518ad4b364cab9b69b
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 utilities.c
4 Copyright (C) 2000 Kh. Naba Kumar Singh
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <errno.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <sys/wait.h>
28 #include <sys/stat.h>
29 #include <limits.h>
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <glib.h>
34 #include <gtk/gtk.h>
35 #include <gnome.h>
36 #include <libanjuta/anjuta-launcher.h>
37 #include <libanjuta/interfaces/ianjuta-message-manager.h>
38 #include <libanjuta/interfaces/ianjuta-message-view.h>
39 #include "plugin.h"
40 #include "utilities.h"
42 #define ICON_FILE "anjuta-gdb.plugin.png"
43 #define SRCH_CHAR '\\'
45 static int
46 get_hex_as (const gchar c)
48 if (isdigit (c))
49 return c - '0';
50 else
51 return toupper (c) - 'A' + 10;
54 static gchar
55 get_hex_b (const gchar c1, const gchar c2)
57 return get_hex_as (c1) * 16 + get_hex_as(c2);
60 gchar*
61 gdb_util_get_str_cod (const gchar *szIn)
63 gchar *szRet ;
64 g_return_val_if_fail( NULL != szIn, NULL );
65 szRet = g_malloc( strlen( szIn )+2 );
66 if( NULL != szRet )
68 gchar* szDst = szRet ;
69 while( szIn[0] )
71 if( SRCH_CHAR == szIn[0] )
73 if( SRCH_CHAR == szIn[1] )
75 *szDst++ = *szIn ++ ;
76 szIn ++ ;
77 } else
79 *szDst ++ = get_hex_b (szIn[1], szIn[2]);
80 szIn += 3;
82 } else
84 *szDst++ = *szIn ++ ;
87 szDst [0] = '\0' ;
89 return szRet ;
92 gboolean
93 gdb_util_parse_error_line (const gchar * line, gchar ** filename, guint *lineno)
95 gint i = 0;
96 gint j = 0;
97 gint k = 0;
98 gchar *dummy;
100 while (line[i++] != ':')
102 if (i >= strlen (line) || i >= 512 || line[i - 1] == ' ')
104 goto down;
107 if (isdigit (line[i]))
109 j = i;
110 while (isdigit (line[i++])) ;
111 dummy = g_strndup (&line[j], i - j - 1);
112 *lineno = strtoul (dummy, NULL, 10);
113 if (dummy)
114 g_free (dummy);
115 dummy = g_strndup (line, j - 1);
116 *filename = g_strdup (g_strstrip (dummy));
117 if (dummy)
118 g_free (dummy);
119 return TRUE;
122 down:
123 i = strlen (line) - 1;
124 while (isspace (line[i]) == FALSE)
126 i--;
127 if (i < 0)
129 *filename = NULL;
130 *lineno = 0;
131 return FALSE;
134 k = i++;
135 while (line[i++] != ':')
137 if (i >= strlen (line) || i >= 512 || line[i - 1] == ' ')
139 *filename = NULL;
140 *lineno = 0;
141 return FALSE;
144 if (isdigit (line[i]))
146 j = i;
147 while (isdigit (line[i++])) ;
148 dummy = g_strndup (&line[j], i - j - 1);
149 *lineno = strtoul (dummy, NULL, 10);
150 if (dummy)
151 g_free (dummy);
152 dummy = g_strndup (&line[k], j - k - 1);
153 *filename = g_strdup (g_strstrip (dummy));
154 if (dummy)
155 g_free (dummy);
156 return TRUE;
158 *lineno = 0;
159 *filename = NULL;
160 return FALSE;
163 gchar *
164 gdb_util_remove_white_spaces (const gchar * text)
166 guint src_count, dest_count, tab_count;
167 gchar buff[2048]; /* Let us hope that it does not overflow */
169 tab_count = 8;
170 dest_count = 0;
172 for (src_count = 0; src_count < strlen (text); src_count++)
174 if (text[src_count] == '\t')
176 gint j;
177 for (j = 0; j < tab_count; j++)
178 buff[dest_count++] = ' ';
180 else if (isspace (text[src_count]))
182 buff[dest_count++] = ' ';
184 else
186 buff[dest_count++] = text[src_count];
189 buff[dest_count] = '\0';
190 return g_strdup (buff);
193 GList *
194 gdb_util_remove_blank_lines (const GList * lines)
196 GList *list, *node;
197 gchar *str;
199 if (lines)
200 list = g_list_copy ((GList*)lines);
201 else
202 list = NULL;
204 node = list;
205 while (node)
207 str = node->data;
208 node = g_list_next (node);
209 if (!str)
211 list = g_list_remove (list, str);
212 continue;
214 if (strlen (g_strchomp (str)) < 1)
215 list = g_list_remove (list, str);
217 return list;
220 /* Excluding the final 0 */
221 gint gdb_util_calc_string_len( const gchar *szStr )
223 if( NULL == szStr )
224 return 0;
225 return strlen( szStr )*3 ; /* Leave space for the translated character */
228 gint gdb_util_calc_gnum_len(void)
230 return 24 ; /* size of a stringfied integer */
233 /* Allocates a struct of pointers */
234 gchar **
235 gdb_util_string_parse_separator (const gint nItems, gchar *szStrIn,
236 const gchar chSep)
238 gchar **szAllocPtrs = (char**)g_new( gchar*, nItems );
239 if( NULL != szAllocPtrs )
241 int i ;
242 gboolean bOK = TRUE ;
243 gchar *p = szStrIn ;
244 for( i = 0 ; i < nItems ; i ++ )
246 gchar *szp ;
247 szp = strchr( p, chSep ) ;
248 if( NULL != szp )
250 szAllocPtrs[i] = p ;
251 szp[0] = '\0' ; /* Parse Operation */
252 p = szp + 1 ;
253 } else
255 bOK = FALSE ;
256 break;
259 if( ! bOK )
261 g_free( szAllocPtrs );
262 szAllocPtrs = NULL ;
265 return szAllocPtrs ;
268 gint
269 gdb_util_kill_process (pid_t process_id, const gchar* signal)
271 int status;
272 gchar *pid_str;
273 pid_t pid;
275 pid_str = g_strdup_printf ("%d", process_id);
276 pid = fork();
277 if (pid == 0)
279 execlp ("kill", "kill", "-s", signal, pid_str, NULL);
280 g_warning (_("Cannot execute command: \"%s\""), "kill");
281 _exit(1);
283 g_free (pid_str);
284 if (pid > 0) {
285 waitpid (pid, &status, 0);
286 return 0;
287 } else {
288 return -1;
292 /* Debugger message manager management */
294 #if 0
295 static const gchar * MESSAGE_VIEW_TITLE = N_("Debug");
297 static void
298 on_gdb_util_mesg_view_destroy(GdbPlugin* plugin, gpointer destroyed_view)
300 plugin->mesg_view = NULL;
303 static void
304 on_gdb_util_debug_buffer_flushed (IAnjutaMessageView *view, const gchar* line,
305 AnjutaPlugin *plugin)
307 g_return_if_fail (line != NULL);
309 IAnjutaMessageViewType type = IANJUTA_MESSAGE_VIEW_TYPE_NORMAL;
310 ianjuta_message_view_append (view, type, line, "", NULL);
313 static void
314 on_gdb_util_debug_mesg_clicked (IAnjutaMessageView* view, const gchar* line,
315 AnjutaPlugin* plugin)
317 /* FIXME: Parse the given line */
320 static IAnjutaMessageView *
321 gdb_util_get_message_view (AnjutaPlugin *plugin)
323 GObject *obj;
324 IAnjutaMessageView *message_view;
325 IAnjutaMessageManager *message_manager = NULL;
326 GdbPlugin *gdb_plugin = ANJUTA_PLUGIN_GDB (plugin);
328 g_return_val_if_fail (plugin != NULL, NULL);
330 if (gdb_plugin->mesg_view)
331 return gdb_plugin->mesg_view;
333 /* TODO: error checking */
334 obj = anjuta_shell_get_object (plugin->shell, "IAnjutaMessageManager",
335 NULL);
336 message_manager = IANJUTA_MESSAGE_MANAGER (obj);
337 message_view = ianjuta_message_manager_add_view (
338 message_manager, MESSAGE_VIEW_TITLE, ICON_FILE, NULL);
339 g_object_weak_ref (G_OBJECT (message_view),
340 (GWeakNotify)on_gdb_util_mesg_view_destroy, plugin);
341 g_signal_connect (G_OBJECT (message_view), "buffer-flushed",
342 G_CALLBACK (on_gdb_util_debug_buffer_flushed), plugin);
343 g_signal_connect (G_OBJECT (message_view), "message-clicked",
344 G_CALLBACK (on_gdb_util_debug_mesg_clicked), plugin);
345 ianjuta_message_manager_set_current_view (message_manager, message_view,
346 NULL);
347 gdb_plugin->mesg_view = message_view;
349 return message_view;
352 void
353 gdb_util_append_message (AnjutaPlugin *plugin, const gchar* message)
355 IAnjutaMessageView *message_view = NULL;
357 g_return_if_fail (plugin != NULL);
359 /* TODO: error checking */
360 message_view = gdb_util_get_message_view (plugin);
361 ianjuta_message_view_buffer_append (message_view, message, NULL);
364 void
365 gdb_util_show_messages (AnjutaPlugin *plugin)
367 GObject *obj;
368 IAnjutaMessageManager *message_manager = NULL;
369 IAnjutaMessageView *message_view = NULL;
371 /* TODO: error checking */
372 obj = anjuta_shell_get_object (ANJUTA_PLUGIN (plugin)->shell,
373 "IAnjutaMessageManager", NULL);
374 message_manager = IANJUTA_MESSAGE_MANAGER (obj);
375 message_view = gdb_util_get_message_view (plugin);
376 ianjuta_message_manager_set_current_view (message_manager, message_view,
377 NULL);
380 void
381 gdb_util_clear_messages (AnjutaPlugin *plugin)
383 IAnjutaMessageView *message_view = NULL;
385 g_return_if_fail (plugin != NULL);
387 /* TODO: error checking */
388 message_view = gdb_util_get_message_view (plugin);
389 ianjuta_message_view_clear (message_view, NULL);
391 #endif