Debian Codegraph @

mixgtk/mixgtk_cmd_dispatcher.c

raw symbols SCIP
1 /* -*-c-*- -------------- mixgtk_cmd_dispatcher.c :
2 * Implementation of the functions declared in mixgtk_cmd_dispatcher.h
3 * ------------------------------------------------------------------
4 * Copyright (C) 2001, 2002, 2004, 2006, 2007, 2019 Free Software Foundation, Inc.
5 *
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 3 of the License, or
9 * (at your option) any later version.
10 *
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.
15 *
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.
19 *
20 */
21
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <unistd.h>
27 #include <fcntl.h>
28 #include <errno.h>
29
30 #include <mixlib/mix.h>
31
32 #ifdef HAVE_LIBHISTORY
33 # include <readline/history.h>
34 #endif
35
36 #ifdef MAKE_GUILE
37 # include <mixguile/mixguile.h>
38 #endif
39
40 #include <gdk/gdkkeysyms.h>
41 #include <mixlib/mix_vm_command.h>
42 #include "mixgtk_mixvm.h"
43 #include "mixgtk_mixal.h"
44 #include "mixgtk_fontsel.h"
45 #include "mixgtk_config.h"
46 #include "mixgtk_external.h"
47 #include "mixgtk_cmd_dispatcher.h"
48
49
50 /* a mix vm command dispatcher */
51 typedef struct mixgtk_dispatch_
52 {
53 mix_vm_cmd_dispatcher_t *dispatcher; /* the underlying cmd dispatcher */
54 FILE *out; /* the dispatcher's output file */
55 int fildes[2]; /* pipe for communication with the dispatcher */
56 GtkWidget *prompt; /* the command prompt widget */
57 GtkWidget *log; /* the dispatcher's messages echo area */
58 GtkWidget *status; /* the status bar widget */
59 guint context; /* context of the status bar messages */
60 gchar *last_file;
61 } mixgtk_dispatch_data_t;
62
63 static struct mixgtk_dispatch_ dis_data_ = {NULL};
64
65 static const gchar *TITLE_FORMAT_ = "gmixvm - %s";
66
67 static void
68 log_command_ (mixgtk_dispatch_data_t *dis, const gchar *cmd)
69 {
70 GtkTextBuffer *buf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (dis->log));
71 GtkTextIter end;
72
73 gtk_text_buffer_get_end_iter (buf, &end);
74 gtk_text_buffer_place_cursor (buf, &end);
75 gtk_text_buffer_insert_at_cursor (buf, "MIX> ", -1);
76 gtk_text_buffer_insert_at_cursor (buf, cmd, -1);
77 gtk_text_buffer_insert_at_cursor (buf, "\n", -1);
78
79 #ifdef HAVE_LIBHISTORY
80 add_history ((char *)cmd);
81 /* history_search ((char *)cmd, 0); */
82 history_set_pos (history_base + history_length - 1);
83 #endif
84 }
85
86 static void
87 flush_log_ (mixgtk_dispatch_data_t *dis)
88 {
89 enum {BLKSIZE = 100};
90 static gchar BUFFER[BLKSIZE];
91
92 GtkTextBuffer *buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (dis->log));
93 GtkTextMark *mark;
94 GtkTextIter end;
95 ssize_t k;
96
97 gtk_text_buffer_get_end_iter (buffer, &end);
98 gtk_text_buffer_place_cursor (buffer, &end);
99
100 fflush (dis->out);
101 while ((k = read (dis->fildes[0], BUFFER, BLKSIZE)) != 0)
102 {
103 if (k == -1 && errno != EINTR) break;
104 if (k != -1)
105 gtk_text_buffer_insert_at_cursor (buffer, BUFFER, k);
106 }
107
108 mark = gtk_text_buffer_get_insert (buffer);
109 gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (dis->log), mark, 0,
110 TRUE, 0, 0);
111 }
112
113 /* hooks */
114 static void
115 global_post_hook_ (mix_vm_cmd_dispatcher_t *dis,
116 mix_vm_command_t cmd, const gchar *arg, gpointer data)
117 {
118 flush_log_ ((mixgtk_dispatch_data_t *)data);
119 mixgtk_mixvm_update_vm_widgets ();
120 }
121
122 static void
123 load_post_hook_ (mix_vm_cmd_dispatcher_t *dis, const gchar *arg,
124 gpointer data)
125 {
126 static glong id = -1;
127
128 if (mix_vm_cmd_dispatcher_get_last_result (dis))
129 {
130 GtkWindow *mainw =
131 GTK_WINDOW (mixgtk_widget_factory_get_dialog (MIXGTK_MAIN));
132
133 if (dis_data_.last_file) g_free (dis_data_.last_file);
134 dis_data_.last_file = g_strdup_printf (TITLE_FORMAT_, arg);
135 gtk_window_set_title (mainw, dis_data_.last_file);
136
137 mixgtk_mixal_load_file ();
138 mixgtk_mixal_update ();
139 mixgtk_mixal_update_bp_all ();
140
141 if (id != -1)
142 gtk_statusbar_remove (GTK_STATUSBAR (dis_data_.status),
143 dis_data_.context, (guint)id);
144 id =
145 gtk_statusbar_push (GTK_STATUSBAR (dis_data_.status), dis_data_.context,
146 dis_data_.last_file);
147 }
148 }
149
150 static void
151 run_post_hook_ (mix_vm_cmd_dispatcher_t *dis, const gchar *arg,
152 gpointer data)
153 {
154 mixgtk_mixal_update ();
155 }
156
157 static void
158 next_post_hook_ (mix_vm_cmd_dispatcher_t *dis, const gchar *arg,
159 gpointer data)
160 {
161 mixgtk_mixal_update ();
162 }
163
164 static void
165 linebp_post_hook_ (mix_vm_cmd_dispatcher_t *dis, const gchar *arg,
166 gpointer data)
167 {
168 if (arg && strlen (arg)) mixgtk_mixal_update_bp_at_line (atoi (arg));
169 }
170
171 static void
172 addrbp_post_hook_ (mix_vm_cmd_dispatcher_t *dis, const gchar *arg,
173 gpointer data)
174 {
175 if (arg && strlen (arg)) mixgtk_mixal_update_bp_at_address (atoi (arg));
176 }
177
178 static void
179 allbp_post_hook_ (mix_vm_cmd_dispatcher_t *dis, const gchar *arg,
180 gpointer data)
181 {
182 mixgtk_mixal_update_bp_all ();
183 }
184
185
186 static void
187 install_hooks_ (void)
188 {
189 mix_vm_cmd_dispatcher_global_post_hook (dis_data_.dispatcher,
190 global_post_hook_,
191 (gpointer)(&dis_data_));
192 mix_vm_cmd_dispatcher_post_hook (dis_data_.dispatcher,
193 MIX_CMD_LOAD, load_post_hook_,
194 NULL);
195 mix_vm_cmd_dispatcher_post_hook (dis_data_.dispatcher,
196 MIX_CMD_RUN, run_post_hook_,
197 NULL);
198 mix_vm_cmd_dispatcher_post_hook (dis_data_.dispatcher,
199 MIX_CMD_NEXT, next_post_hook_,
200 NULL);
201 mix_vm_cmd_dispatcher_post_hook (dis_data_.dispatcher,
202 MIX_CMD_SBP, linebp_post_hook_,
203 NULL);
204 mix_vm_cmd_dispatcher_post_hook (dis_data_.dispatcher,
205 MIX_CMD_CBP, linebp_post_hook_,
206 NULL);
207 mix_vm_cmd_dispatcher_post_hook (dis_data_.dispatcher,
208 MIX_CMD_SBPA, addrbp_post_hook_,
209 NULL);
210 mix_vm_cmd_dispatcher_post_hook (dis_data_.dispatcher,
211 MIX_CMD_CBPA, addrbp_post_hook_,
212 NULL);
213 mix_vm_cmd_dispatcher_post_hook (dis_data_.dispatcher,
214 MIX_CMD_CABP, allbp_post_hook_,
215 NULL);
216 }
217
218
219 /* initialise the command dispatcher */
220 gboolean
221 mixgtk_cmd_dispatcher_init (mixgtk_dialog_id_t top)
222 {
223 static gboolean restart = FALSE;
224 gchar *text = NULL;
225
226 dis_data_.prompt =
227 mixgtk_widget_factory_get (top, MIXGTK_WIDGET_PROMPT);
228 g_return_val_if_fail (dis_data_.prompt != NULL, FALSE);
229
230 if (dis_data_.log)
231 text = gtk_editable_get_chars (GTK_EDITABLE (dis_data_.log), 0, -1);
232
233 dis_data_.log =
234 mixgtk_widget_factory_get (top, MIXGTK_WIDGET_LOG);
235
236 g_return_val_if_fail (dis_data_.log != NULL, FALSE);
237
238 if (text)
239 {
240 gtk_text_buffer_insert_at_cursor
241 (gtk_text_view_get_buffer (GTK_TEXT_VIEW (dis_data_.log)), text, -1);
242 g_free (text);
243 }
244
245 if (!dis_data_.dispatcher)
246 {
247 static const gchar *HISTORY_FILE = "gmixvm.history";
248 static gint HISTORY_SIZE = 100;
249 mix_config_t *config = mixgtk_config_get_mix_config ();
250
251 int r = pipe (dis_data_.fildes);
252 g_return_val_if_fail (r == 0, FALSE);
253 /* connect stdout/stderr to the pipe's write end */
254 if (dup2 (dis_data_.fildes[1], STDOUT_FILENO) == -1
255 || dup2 (dis_data_.fildes[1], STDOUT_FILENO) == -1)
256 return FALSE;
257 dis_data_.out = fdopen (dis_data_.fildes[1], "w");
258 g_return_val_if_fail (dis_data_.out != NULL, FALSE);
259 r = fcntl (dis_data_.fildes[0], F_GETFL, 0);
260 g_return_val_if_fail (r != -1, FALSE);
261 r |= O_NONBLOCK;
262 r = fcntl(dis_data_.fildes[0], F_SETFL, r);
263 g_return_val_if_fail (r != -1, FALSE);
264
265 if (!mix_config_get_history_file (config))
266 mix_config_set_history_file (config, HISTORY_FILE);
267 if (mix_config_get_history_size (config) == 0)
268 mix_config_set_history_size (config, HISTORY_SIZE);
269
270 dis_data_.dispatcher =
271 mix_vm_cmd_dispatcher_new_with_config (dis_data_.out,
272 dis_data_.out,
273 config);
274 mix_vm_cmd_dispatcher_print_time (dis_data_.dispatcher, FALSE);
275
276 install_hooks_ ();
277 }
278
279 dis_data_.status =
280 mixgtk_widget_factory_get (MIXGTK_MAIN, MIXGTK_WIDGET_STATUSBAR);
281 g_return_val_if_fail (dis_data_.status != NULL, FALSE);
282 dis_data_.context = gtk_statusbar_get_context_id (GTK_STATUSBAR
283 (dis_data_.status),
284 "cmd_dis_context");
285
286 if (!restart) mixgtk_external_init (dis_data_.dispatcher);
287
288 if (dis_data_.last_file)
289 gtk_window_set_title
290 (GTK_WINDOW (mixgtk_widget_factory_get_dialog (MIXGTK_MAIN)),
291 dis_data_.last_file);
292
293 mixgtk_fontsel_set_font (MIX_FONT_LOG, dis_data_.log);
294 mixgtk_fontsel_set_font (MIX_FONT_PROMPT, dis_data_.prompt);
295
296 restart = TRUE;
297 return TRUE;
298 }
299
300 /* dispatch an externally provided command */
301 #ifdef MAKE_GUILE
302 static gboolean
303 try_guile_ (const gchar *command)
304 {
305 if (command && command[0] == '(' && command[strlen (command) - 1] == ')')
306 {
307 mixguile_interpret_command (command);
308 return TRUE;
309 }
310 return FALSE;
311 }
312 #else
313 # define try_guile_(ignored) FALSE
314 #endif
315
316 void
317 mixgtk_cmd_dispatcher_dispatch (const gchar *command)
318 {
319 GtkWidget *entry = dis_data_.prompt;
320 g_return_if_fail (command != NULL);
321 g_assert (entry != NULL);
322 gtk_entry_set_text (GTK_ENTRY (entry), command);
323 log_command_ (&dis_data_, command);
324 if (!try_guile_ (command))
325 mix_vm_cmd_dispatcher_dispatch_text (dis_data_.dispatcher, command);
326 gtk_entry_set_text (GTK_ENTRY (entry), "");
327 }
328
329 /* get times */
330 void
331 mixgtk_cmd_dispatcher_get_times (gint *uptime, gint *progtime, gint *laptime)
332 {
333 if (uptime != NULL)
334 *uptime = mix_vm_cmd_dispatcher_get_uptime (dis_data_.dispatcher);
335 if (progtime != NULL)
336 *progtime = mix_vm_cmd_dispatcher_get_progtime (dis_data_.dispatcher);
337 if (laptime != NULL)
338 *laptime = mix_vm_cmd_dispatcher_get_laptime (dis_data_.dispatcher);
339 }
340
341 /* get the underlying vm */
342 mix_vm_t *
343 mixgtk_cmd_dispatcher_get_vm (void)
344 {
345 return (mix_vm_t *) mix_vm_cmd_dispatcher_get_vm (dis_data_.dispatcher);
346 }
347
348 /* get the current source file */
349 const gchar *
350 mixgtk_cmd_dispatcher_get_src_path (void)
351 {
352 return mix_vm_cmd_dispatcher_get_src_file_path (dis_data_.dispatcher);
353 }
354
355 /* get the mix cmd dispatcher */
356 mix_vm_cmd_dispatcher_t *
357 mixgtk_cmd_dispatcher_get_mix_dispatcher (void)
358 {
359 return dis_data_.dispatcher;
360 }
361
362 /* process commands */
363 void
364 complete_command_ (void)
365 {
366 GtkEntry *entry = GTK_ENTRY (dis_data_.prompt);
367 gchar *prefix = NULL;
368 const gchar *text = gtk_entry_get_text (entry);
369 const GList *cmds =
370 mix_vm_cmd_dispatcher_complete (dis_data_.dispatcher, text, &prefix);
371
372 if (prefix != NULL)
373 {
374 GtkTextBuffer *buf =
375 gtk_text_view_get_buffer (GTK_TEXT_VIEW (dis_data_.log));
376
377 gtk_entry_set_text (entry, prefix);
378 if (g_list_length ((GList *)cmds) > 1)
379 {
380 static gchar BUFFER[25];
381 gint k = 0;
382 gtk_text_buffer_insert_at_cursor (buf, "Completions:\n", -1);
383 while (cmds)
384 {
385 g_snprintf (BUFFER, 25, "%-12s", (const char*)cmds->data);
386 ++k;
387 gtk_text_buffer_insert_at_cursor (buf, BUFFER, -1);
388 if (k%5 == 0)
389 gtk_text_buffer_insert_at_cursor (buf, "\n", -1);
390
391 cmds = cmds->next;
392 }
393 if (k%5 != 0)
394 gtk_text_buffer_insert_at_cursor (buf, "\n", -1);
395 }
396 else
397 {
398 gint pos = strlen (prefix);
399 gtk_editable_insert_text (GTK_EDITABLE (entry), " ", 1, &pos);
400 }
401 flush_log_ (&dis_data_);
402 g_free (prefix);
403 }
404 }
405
406 int
407 on_command_prompt_key_press_event (GtkEntry *w, GdkEventKey *e, gpointer d)
408 {
409 guint key = e->keyval;
410 gboolean result = FALSE;
411
412 #ifdef HAVE_LIBHISTORY
413 HIST_ENTRY *entry = NULL;
414 if (key == GDK_KEY_Up)
415 {
416 entry = previous_history ();
417 if (entry && entry->line)
418 gtk_entry_set_text (w, entry->line);
419 result = TRUE;
420 }
421 if (key == GDK_KEY_Down)
422 {
423 entry = next_history ();
424 if (entry && entry->line)
425 gtk_entry_set_text (w, entry->line);
426 result = TRUE;
427 }
428 #endif
429
430 if (key == GDK_KEY_Tab)
431 {
432 complete_command_ ();
433 result = TRUE;
434 }
435
436 if (result) gtk_editable_set_position (GTK_EDITABLE (w), -1);
437
438 return result;
439 }
440
441 void
442 on_command_prompt_activate (GtkEntry *prompt, gpointer data)
443 {
444 gchar *text =
445 g_strstrip (gtk_editable_get_chars (GTK_EDITABLE (prompt), 0, -1));
446 if (text && *text)
447 {
448 log_command_ (&dis_data_, text);
449 if (!try_guile_ (text))
450 mix_vm_cmd_dispatcher_dispatch_text (dis_data_.dispatcher, text);
451 gtk_entry_set_text (prompt, "");
452 }
453 g_free (text);
454 }
455
456 void
457 on_log_font_activate ()
458 {
459 GtkWidget *w[] = { dis_data_.log };
460 mixgtk_fontsel_query_font (MIX_FONT_LOG, w, 1);
461 }
462
463 void
464 on_prompt_font_activate ()
465 {
466 GtkWidget *w[] = { dis_data_.prompt };
467 mixgtk_fontsel_query_font (MIX_FONT_PROMPT, w, 1);
468 }
469
470 void
471 mixgtk_cmd_dispatcher_update_fonts (void)
472 {
473 mixgtk_fontsel_set_font (MIX_FONT_LOG, dis_data_.log);
474 mixgtk_fontsel_set_font (MIX_FONT_PROMPT, dis_data_.prompt);
475 }