1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) 2003 Naba Kumar <naba@gnome.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * SECTION:anjuta-launcher
23 * @short_description: External process launcher with async input/output
25 * @stability: Unstable
26 * @include: libanjuta/anjuta-launcher.h
39 #if !defined(__sun) && !defined(__NetBSD__)
46 #include "anjuta-utils-priv.h"
53 #include <glib/gi18n.h>
56 #include <libgnome/gnome-macros.h>
58 #include "anjuta-utils.h"
59 #include "anjuta-marshal.h"
60 #include "resources.h"
61 #include "anjuta-launcher.h"
62 #include "anjuta-debug.h"
64 #define ANJUTA_PIXMAP_PASSWORD "password.png"
65 #define FILE_BUFFER_SIZE 1024
66 #define FILE_INPUT_BUFFER_SIZE 1048576
69 #define __MAX_BAUD B256000
71 #define __MAX_BAUD B460800
77 anjuta_launcher_pty_check_child_exit_code (AnjutaLauncher *launcher,
80 struct _AnjutaLauncherPriv
83 * Busy flag is TRUE if the Launcher
84 * is currently executing a child.
88 /* These flags are used to synchronize the IO operations. */
89 gboolean stdout_is_done
;
90 gboolean stderr_is_done
;
93 GIOChannel
*stdout_channel
;
94 GIOChannel
*stderr_channel
;
95 /*GIOChannel *stdin_channel;*/
96 GIOChannel
*pty_channel
;
98 /* GIO watch handles */
103 /* Output line buffers */
104 gchar
*stdout_buffer
;
105 gchar
*stderr_buffer
;
107 /* Output of the pty is constantly stored here.*/
108 gchar
*pty_output_buffer
;
111 gboolean terminal_echo_on
;
117 gboolean child_has_terminated
;
119 /* Synchronization in progress */
121 guint completion_check_timeout
;
123 /* Terminate child on child exit */
124 gboolean terminate_on_exit
;
126 /* Start time of execution */
129 /* Should the outputs be buffered */
130 gboolean buffered_output
;
132 /* Should we check for password prompts in stdout and pty */
133 gboolean check_for_passwd_prompt
;
135 /* Output callback */
136 AnjutaLauncherOutputCallback output_callback
;
139 gpointer callback_data
;
142 gboolean custom_encoding
;
151 /* OUTPUT_ARRIVED_SIGNAL, */
157 static void anjuta_launcher_class_init (AnjutaLauncherClass
* klass
);
158 static void anjuta_launcher_init (AnjutaLauncher
* obj
);
159 static gboolean
anjuta_launcher_call_execution_done (gpointer data
);
160 static gboolean
anjuta_launcher_check_for_execution_done (gpointer data
);
161 static void anjuta_launcher_execution_done_cleanup (AnjutaLauncher
*launcher
,
162 gboolean emit_signal
);
164 static gboolean
is_password_prompt(const gchar
* line
);
166 static guint launcher_signals
[LAST_SIGNAL
] = { 0 };
167 static AnjutaLauncherClass
*parent_class
;
170 anjuta_launcher_initialize (AnjutaLauncher
*obj
)
173 obj
->priv
->busy
= FALSE
;
175 /* These flags are used to synchronize the IO operations. */
176 obj
->priv
->stdout_is_done
= FALSE
;
177 obj
->priv
->stderr_is_done
= FALSE
;
180 obj
->priv
->stdout_channel
= NULL
;
181 obj
->priv
->stderr_channel
= NULL
;
182 obj
->priv
->pty_channel
= NULL
;
184 /* Output line buffers */
185 obj
->priv
->stdout_buffer
= NULL
;
186 obj
->priv
->stderr_buffer
= NULL
;
189 obj
->priv
->pty_output_buffer
= NULL
;
191 obj
->priv
->terminal_echo_on
= TRUE
;
194 obj
->priv
->child_pid
= 0;
195 obj
->priv
->child_status
= -1;
196 obj
->priv
->child_has_terminated
= TRUE
;
198 /* Synchronization in progress */
199 obj
->priv
->in_cleanup
= FALSE
;
200 obj
->priv
->completion_check_timeout
= -1;
202 /* Terminate child on child exit */
203 obj
->priv
->terminate_on_exit
= FALSE
;
205 /* Start time of execution */
206 obj
->priv
->start_time
= 0;
208 obj
->priv
->buffered_output
= TRUE
;
209 obj
->priv
->check_for_passwd_prompt
= TRUE
;
211 /* Output callback */
212 obj
->priv
->output_callback
= NULL
;
213 obj
->priv
->callback_data
= NULL
;
216 obj
->priv
->custom_encoding
= FALSE
;
217 obj
->priv
->encoding
= NULL
;
220 obj
->priv
->env
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
225 anjuta_launcher_get_type ()
227 static GType obj_type
= 0;
231 static const GTypeInfo obj_info
=
233 sizeof (AnjutaLauncherClass
),
234 (GBaseInitFunc
) NULL
,
235 (GBaseFinalizeFunc
) NULL
,
236 (GClassInitFunc
) anjuta_launcher_class_init
,
237 (GClassFinalizeFunc
) NULL
,
238 NULL
, /* class_data */
239 sizeof (AnjutaLauncher
),
241 (GInstanceInitFunc
) anjuta_launcher_init
,
242 NULL
/* value_table */
244 obj_type
= g_type_register_static (G_TYPE_OBJECT
,
245 "AnjutaLauncher", &obj_info
, 0);
251 anjuta_launcher_dispose (GObject
*obj
)
253 AnjutaLauncher
*launcher
= ANJUTA_LAUNCHER (obj
);
254 if (anjuta_launcher_is_busy (launcher
))
256 pid_t child_pid_save
= launcher
->priv
->child_pid
;
257 guint child_source
= launcher
->priv
->source
;
258 g_source_remove (child_source
);
259 anjuta_launcher_execution_done_cleanup (launcher
, FALSE
);
261 /* We can not call anjuta_launcher_reset (launcher) to kill the
262 * running child because launcher has been initialized in cleanup
264 kill (child_pid_save
, SIGTERM
);
265 launcher
->priv
->busy
= FALSE
;
268 GNOME_CALL_PARENT (G_OBJECT_CLASS
, dispose
, (obj
));
272 anjuta_launcher_finalize (GObject
*obj
)
274 AnjutaLauncher
*launcher
= ANJUTA_LAUNCHER (obj
);
275 if (launcher
->priv
->custom_encoding
&& launcher
->priv
->encoding
)
276 g_free (launcher
->priv
->encoding
);
278 g_hash_table_destroy (launcher
->priv
->env
);
280 g_free (launcher
->priv
);
281 GNOME_CALL_PARENT (G_OBJECT_CLASS
, finalize
, (obj
));
285 anjuta_launcher_class_init (AnjutaLauncherClass
* klass
)
287 GObjectClass
*object_class
;
288 g_return_if_fail (klass
!= NULL
);
289 object_class
= (GObjectClass
*) klass
;
291 /* DEBUG_PRINT ("Initializing launcher class"); */
293 parent_class
= g_type_class_peek_parent (klass
);
296 * AnjutaLauncher::child-exited
297 * @launcher: a #AnjutaLancher object.
298 * @child_pid: process ID of the child
299 * @status: status as returned by waitpid function
300 * @time: time in seconds taken by the child
302 * Emitted when the child has exited and all i/o channels have
303 * been closed. If the terminate on exit flag is set, the i/o
304 * channels are automatically closed when the child exit.
305 * You need to use WEXITSTATUS and friend to get the child exit
306 * code from the status returned.
308 launcher_signals
[CHILD_EXITED_SIGNAL
] =
309 g_signal_new ("child-exited",
310 G_TYPE_FROM_CLASS (object_class
),
312 G_STRUCT_OFFSET (AnjutaLauncherClass
,
315 anjuta_cclosure_marshal_VOID__INT_INT_ULONG
,
316 G_TYPE_NONE
, 3, G_TYPE_INT
,
317 G_TYPE_INT
, G_TYPE_ULONG
);
320 * AnjutaLauncher::busy
321 * @launcher: a #AnjutaLancher object.
322 * @busy: TRUE is a child is currently running
324 * Emitted when a child starts after a call to one execute function
325 * (busy is TRUE) or when a child exits and all i/o channels are
326 * closed (busy is FALSE).
328 launcher_signals
[BUSY_SIGNAL
] =
329 g_signal_new ("busy",
330 G_TYPE_FROM_CLASS (object_class
),
332 G_STRUCT_OFFSET (AnjutaLauncherClass
,
335 anjuta_cclosure_marshal_VOID__BOOLEAN
,
336 G_TYPE_NONE
, 1, G_TYPE_BOOLEAN
);
338 object_class
->dispose
= anjuta_launcher_dispose
;
339 object_class
->finalize
= anjuta_launcher_finalize
;
343 anjuta_launcher_init (AnjutaLauncher
* obj
)
345 g_return_if_fail (obj
!= NULL
);
346 obj
->priv
= g_new0 (AnjutaLauncherPriv
, 1);
347 anjuta_launcher_initialize (obj
);
351 * anjuta_launcher_is_busy:
352 * @launcher: a #AnjutaLancher object.
354 * Tells if the laucher is currently executing any command.
356 * Return value: TRUE if launcher is busy, otherwisee FALSE.
359 anjuta_launcher_is_busy (AnjutaLauncher
*launcher
)
361 return launcher
->priv
->busy
;
365 anjuta_launcher_set_busy (AnjutaLauncher
*launcher
, gboolean flag
)
367 gboolean old_busy
= launcher
->priv
->busy
;
368 launcher
->priv
->busy
= flag
;
369 if (old_busy
!= flag
)
370 g_signal_emit_by_name (G_OBJECT (launcher
), "busy", flag
);
374 * anjuta_launcher_send_stdin:
375 * @launcher: a #AnjutaLancher object.
376 * @input_str: The string to send to STDIN of the process.
378 * Sends a string to Standard input of the process currently being executed.
381 anjuta_launcher_send_stdin (AnjutaLauncher
*launcher
, const gchar
* input_str
)
383 g_return_if_fail (launcher
);
384 g_return_if_fail (input_str
);
386 anjuta_launcher_send_ptyin (launcher
, input_str
);
390 * anjuta_launcher_send_stdin:
391 * @launcher: a #AnjutaLancher object.
393 * Sends a EOF to Standard input of the process currently being executed.
397 anjuta_launcher_send_stdin_eof (AnjutaLauncher
*launcher
)
400 g_io_channel_shutdown (launcher
->priv
->pty_channel
, TRUE
,
402 g_io_channel_unref (launcher
->priv
->pty_channel
);
403 launcher
->priv
->pty_channel
= NULL
;
407 g_warning ("g_io_channel_shutdown () failed: %s", err
->message
);
412 * anjuta_launcher_send_ptyin:
413 * @launcher: a #AnjutaLancher object.
414 * @input_str: The string to send to PTY of the process.
416 * Sends a string to TTY input of the process currently being executed.
417 * Mostly useful for entering passwords and other inputs which are directly
418 * read from TTY input of the process.
421 anjuta_launcher_send_ptyin (AnjutaLauncher
*launcher
, const gchar
* input_str
)
426 if (!input_str
|| strlen (input_str
) == 0) return;
430 g_io_channel_write_chars (launcher
->priv
->pty_channel
,
431 input_str
, strlen (input_str
),
432 &bytes_written
, &err
);
433 g_io_channel_flush (launcher
->priv
->pty_channel
, NULL
);
436 g_warning ("Error encountered while writing to PTY!. %s",
442 input_str
+= bytes_written
;
448 * anjuta_launcher_reset:
449 * @launcher: a #AnjutaLancher object.
451 * Resets the launcher and kills (SIGTERM) current process, if it is still
455 anjuta_launcher_reset (AnjutaLauncher
*launcher
)
457 if (anjuta_launcher_is_busy (launcher
))
458 kill (launcher
->priv
->child_pid
, SIGTERM
);
462 * anjuta_launcher_signal:
463 * @launcher: a #AnjutaLancher object.
464 * @sig: kernel signal ID (e.g. SIGTERM).
466 * Sends a kernel signal to the process that is being executed.
469 anjuta_launcher_signal (AnjutaLauncher
*launcher
, int sig
)
471 kill (launcher
->priv
->child_pid
, sig
);
475 * anjuta_launcher_get_child_pid:
476 * @launcher: a #AnjutaLancher object.
478 * Gets the Process ID of the child being executed.
480 * Return value: Process ID of the child.
483 anjuta_launcher_get_child_pid (AnjutaLauncher
*launcher
)
485 if (anjuta_launcher_is_busy (launcher
))
486 return launcher
->priv
->child_pid
;
492 anjuta_launcher_synchronize (AnjutaLauncher
*launcher
)
494 if (launcher
->priv
->in_cleanup
) return;
496 if (launcher
->priv
->child_has_terminated
&&
497 launcher
->priv
->stdout_is_done
&&
498 launcher
->priv
->stderr_is_done
)
500 if (launcher
->priv
->completion_check_timeout
>= 0)
501 g_source_remove (launcher
->priv
->completion_check_timeout
);
502 launcher
->priv
->completion_check_timeout
=
503 g_timeout_add (50, anjuta_launcher_check_for_execution_done
,
507 /* This case is not very good, but it blocks the whole IDE
508 because we never new if the child has finished */
509 else if (launcher
->priv
->stdout_is_done
&&
510 launcher
->priv
->stderr_is_done
)
512 /* DEBUG_PRINT ("Child has't exited yet waiting for 200ms"); */
513 if (launcher
->priv
->completion_check_timeout
>= 0)
514 g_source_remove (launcher
->priv
->completion_check_timeout
);
515 launcher
->priv
->completion_check_timeout
=
516 g_timeout_add(200, anjuta_launcher_check_for_execution_done
,
519 /* Add this case for gdb. It creates child inheriting gdb
520 * pipes which are not closed if gdb crashes */
521 else if (launcher
->priv
->child_has_terminated
&&
522 launcher
->priv
->terminate_on_exit
)
524 if (launcher
->priv
->completion_check_timeout
>= 0)
525 g_source_remove (launcher
->priv
->completion_check_timeout
);
526 launcher
->priv
->completion_check_timeout
=
527 g_timeout_add(0, anjuta_launcher_call_execution_done
,
532 /* Password dialog */
534 create_password_dialog (const gchar
* prompt
)
543 g_return_val_if_fail (prompt
, NULL
);
545 dialog
= gtk_dialog_new_with_buttons (prompt
,
546 NULL
, //FIXME: Pass the parent window here
547 // for transient purpose.
548 GTK_DIALOG_DESTROY_WITH_PARENT
,
549 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
550 GTK_STOCK_OK
, GTK_RESPONSE_OK
, NULL
);
551 gtk_dialog_set_default_response (GTK_DIALOG (dialog
), GTK_RESPONSE_OK
);
553 gtk_window_set_wmclass (GTK_WINDOW (dialog
), "launcher-password-prompt",
555 hbox
= gtk_hbox_new (FALSE
, 10);
556 gtk_widget_show (hbox
);
557 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog
)->vbox
), hbox
);
559 icon
= anjuta_res_get_image (ANJUTA_PIXMAP_PASSWORD
);
560 gtk_widget_show (icon
);
561 gtk_box_pack_start_defaults (GTK_BOX(hbox
), icon
);
563 if (strlen (prompt
) < 20) {
564 box
= gtk_hbox_new (FALSE
, 5);
566 box
= gtk_vbox_new (FALSE
, 5);
568 gtk_widget_show (box
);
569 gtk_box_pack_start_defaults (GTK_BOX (hbox
), box
);
571 label
= gtk_label_new (_(prompt
));
572 gtk_widget_show (label
);
573 gtk_box_pack_start (GTK_BOX (box
), label
, FALSE
, FALSE
, 0);
575 entry
= gtk_entry_new ();
576 gtk_widget_show (entry
);
577 gtk_entry_set_visibility (GTK_ENTRY (entry
), FALSE
);
578 gtk_box_pack_start (GTK_BOX (box
), entry
, FALSE
, FALSE
, 0);
580 gtk_widget_ref (entry
);
581 g_object_set_data_full (G_OBJECT (dialog
), "password_entry",
582 gtk_widget_ref (entry
),
583 (GDestroyNotify
) gtk_widget_unref
);
584 gtk_widget_grab_focus (entry
);
585 gtk_entry_set_activates_default (GTK_ENTRY (entry
), TRUE
);
590 /* pty buffer check for password authentication */
592 anjuta_launcher_check_password_real (AnjutaLauncher
*launcher
,
593 const gchar
* last_line
)
595 if (anjuta_launcher_is_busy (launcher
) == FALSE
)
600 /* DEBUG_PRINT ("(In password) Last line = %s", last_line); */
601 if (is_password_prompt(last_line
)) {
602 /* Password prompt detected */
608 dialog
= create_password_dialog (last_line
);
609 button
= gtk_dialog_run (GTK_DIALOG(dialog
));
611 case GTK_RESPONSE_OK
:
612 passwd
= gtk_entry_get_text (
613 GTK_ENTRY (g_object_get_data (G_OBJECT (dialog
),
615 line
= g_strconcat (passwd
, "\n", NULL
);
616 anjuta_launcher_send_ptyin (launcher
, line
);
619 case GTK_RESPONSE_CANCEL
:
620 anjuta_launcher_send_ptyin (launcher
, "<canceled>\n");
621 anjuta_launcher_reset (launcher
);
626 gtk_widget_destroy (dialog
);
632 anjuta_launcher_check_password (AnjutaLauncher
*launcher
, const gchar
*chars
)
637 if (!chars
|| strlen(chars
) <= 0)
640 /* DEBUG_PRINT ("Chars buffer = %s", chars); */
641 start
= end
= strlen (chars
);
642 while (start
> 0 && chars
[start
-1] != '\n') start
--;
646 last_line
= g_strndup (&chars
[start
], end
- start
+ 1);
648 /* DEBUG_PRINT ("Last line = %s", last_line); */
649 /* Checks for password, again */
650 anjuta_launcher_check_password_real (launcher
, last_line
);
656 is_password_prompt (const gchar
* line
)
658 const gchar
* password
= "assword";
659 const gchar
* passphrase
= "assphrase";
661 if (strlen (line
) < strlen (password
)
662 || strlen (line
) < strlen (passphrase
))
665 if (g_strstr_len(line
, 80, password
) != NULL
666 || g_strstr_len(line
, 80, passphrase
) != NULL
)
669 for (i
= strlen(line
) - 1; i
!= 0; --i
)
673 if (g_ascii_isspace(line
[i
]))
683 anjuta_launcher_buffered_output (AnjutaLauncher
*launcher
,
684 AnjutaLauncherOutputType output_type
,
688 gchar
*incomplete_line
;
691 g_return_if_fail (chars
!= NULL
);
692 g_return_if_fail (strlen (chars
) > 0);
694 if (launcher
->priv
->output_callback
== NULL
)
696 if (launcher
->priv
->buffered_output
== FALSE
)
698 (launcher
->priv
->output_callback
)(launcher
, output_type
, chars
,
699 launcher
->priv
->callback_data
);
704 case ANJUTA_LAUNCHER_OUTPUT_STDOUT
:
705 buffer
= &launcher
->priv
->stdout_buffer
;
707 case ANJUTA_LAUNCHER_OUTPUT_STDERR
:
708 buffer
= &launcher
->priv
->stderr_buffer
;
711 g_warning ("Should not reach here");
715 all_lines
= g_strconcat (*buffer
, chars
, NULL
);
717 all_lines
= g_strdup (chars
);
719 /* Buffer the last incomplete line */
720 incomplete_line
= all_lines
+ strlen (all_lines
);
721 while (incomplete_line
> all_lines
&&
722 *incomplete_line
!= '\n')
724 incomplete_line
= g_utf8_prev_char (incomplete_line
);
726 if (*incomplete_line
== '\n')
729 /* Update line buffer */
732 if (strlen(incomplete_line
))
734 *buffer
= g_strdup (incomplete_line
);
735 /* DEBUG_PRINT ("Line buffer for %d: %s", output_type, incomplete_line); */
737 /* Check for password prompt */
738 if (launcher
->priv
->check_for_passwd_prompt
)
739 anjuta_launcher_check_password (launcher
, incomplete_line
);
741 /* Deliver complete lines */
742 *incomplete_line
= '\0';
743 if (strlen (all_lines
) > 0)
744 (launcher
->priv
->output_callback
)(launcher
, output_type
, all_lines
,
745 launcher
->priv
->callback_data
);
750 anjuta_launcher_scan_output (GIOChannel
*channel
, GIOCondition condition
,
751 AnjutaLauncher
*launcher
)
754 gchar buffer
[FILE_BUFFER_SIZE
];
757 if (condition
& G_IO_IN
)
762 g_io_channel_read_chars (channel
, buffer
, FILE_BUFFER_SIZE
-1, &n
, &err
);
763 if (n
> 0) /* There is output */
765 gchar
*utf8_chars
= NULL
;
767 if (!launcher
->priv
->custom_encoding
)
768 utf8_chars
= anjuta_util_convert_to_utf8 (buffer
);
770 utf8_chars
= g_strdup(buffer
);
771 anjuta_launcher_buffered_output (launcher
,
772 ANJUTA_LAUNCHER_OUTPUT_STDOUT
,
776 /* Ignore illegal characters */
777 if (err
&& err
->domain
== G_CONVERT_ERROR
)
782 /* The pipe is closed on the other side */
783 /* if not related to non blocking read or interrupted syscall */
784 else if (err
&& errno
!= EAGAIN
&& errno
!= EINTR
)
786 launcher
->priv
->stdout_is_done
= TRUE
;
787 anjuta_launcher_synchronize (launcher
);
790 /* Read next chars if buffer was too small
791 * (the maximum length of one character is 6 bytes) */
792 } while (!err
&& (n
> FILE_BUFFER_SIZE
- 7));
796 if ((condition
& G_IO_ERR
) || (condition
& G_IO_HUP
))
798 DEBUG_PRINT ("launcher.c: STDOUT pipe closed");
799 launcher
->priv
->stdout_is_done
= TRUE
;
800 anjuta_launcher_synchronize (launcher
);
807 anjuta_launcher_scan_error (GIOChannel
*channel
, GIOCondition condition
,
808 AnjutaLauncher
*launcher
)
811 gchar buffer
[FILE_BUFFER_SIZE
];
814 if (condition
& G_IO_IN
)
819 g_io_channel_read_chars (channel
, buffer
, FILE_BUFFER_SIZE
-1, &n
, &err
);
820 if (n
> 0) /* There is stderr output */
824 utf8_chars
= anjuta_util_convert_to_utf8 (buffer
);
825 anjuta_launcher_buffered_output (launcher
,
826 ANJUTA_LAUNCHER_OUTPUT_STDERR
,
830 /* Ignore illegal characters */
831 if (err
&& err
->domain
== G_CONVERT_ERROR
)
836 /* The pipe is closed on the other side */
837 /* if not related to non blocking read or interrupted syscall */
838 else if (err
&& errno
!= EAGAIN
&& errno
!= EINTR
)
841 launcher
->priv
->stderr_is_done
= TRUE
;
842 anjuta_launcher_synchronize (launcher
);
845 /* Read next chars if buffer was too small
846 * (the maximum length of one character is 6 bytes) */
847 } while (!err
&& (n
> FILE_BUFFER_SIZE
- 7));
851 if ((condition
& G_IO_ERR
) || (condition
& G_IO_HUP
))
853 DEBUG_PRINT ("launcher.c: STDERR pipe closed");
854 launcher
->priv
->stderr_is_done
= TRUE
;
855 anjuta_launcher_synchronize (launcher
);
862 anjuta_launcher_scan_pty (GIOChannel
*channel
, GIOCondition condition
,
863 AnjutaLauncher
*launcher
)
866 gchar buffer
[FILE_BUFFER_SIZE
];
869 if (condition
& G_IO_IN
)
874 g_io_channel_read_chars (channel
, buffer
, FILE_BUFFER_SIZE
-1, &n
, &err
);
875 if (n
> 0) /* There is stderr output */
878 gchar
*old_str
= launcher
->priv
->pty_output_buffer
;
880 utf8_chars
= anjuta_util_convert_to_utf8 (buffer
);
883 gchar
*str
= g_strconcat (old_str
, utf8_chars
, NULL
);
884 launcher
->priv
->pty_output_buffer
= str
;
888 launcher
->priv
->pty_output_buffer
= g_strdup (utf8_chars
);
891 /* Ignore illegal characters */
892 if (err
&& err
->domain
== G_CONVERT_ERROR
)
897 /* The pipe is closed on the other side */
898 /* if not related to non blocking read or interrupted syscall */
899 else if (err
&& errno
!= EAGAIN
&& errno
!= EINTR
)
903 /* Read next chars if buffer was too small
904 * (the maximum length of one character is 6 bytes) */
905 } while (!err
&& (n
> FILE_BUFFER_SIZE
- 7));
908 if (launcher
->priv
->check_for_passwd_prompt
909 && launcher
->priv
->pty_output_buffer
910 && strlen (launcher
->priv
->pty_output_buffer
) > 0)
912 anjuta_launcher_check_password (launcher
,
913 launcher
->priv
->pty_output_buffer
);
916 /* In pty case, we handle the cases in different invocations */
917 /* Do not hook up for G_IO_HUP */
918 if (condition
& G_IO_ERR
)
920 DEBUG_PRINT ("launcher.c: PTY pipe error!");
927 anjuta_launcher_execution_done_cleanup (AnjutaLauncher
*launcher
,
928 gboolean emit_signal
)
930 gint child_status
, child_pid
;
933 if (launcher
->priv
->in_cleanup
)
936 launcher
->priv
->in_cleanup
= TRUE
;
938 /* Remove pending timeout */
939 if (launcher
->priv
->completion_check_timeout
>= 0)
940 g_source_remove (launcher
->priv
->completion_check_timeout
);
942 /* Make sure all pending I/O are flushed out */
943 while (g_main_context_pending (NULL
))
944 g_main_context_iteration (NULL
, FALSE
);
946 /* Can be called again, while waiting in the previous line
947 * Do nothing if clean up is already done */
948 if (launcher
->priv
->stdout_channel
)
950 g_io_channel_shutdown (launcher
->priv
->stdout_channel
, emit_signal
, NULL
);
951 g_io_channel_unref (launcher
->priv
->stdout_channel
);
952 g_source_remove (launcher
->priv
->stdout_watch
);
955 if (launcher
->priv
->stderr_channel
)
957 g_io_channel_shutdown (launcher
->priv
->stderr_channel
, emit_signal
, NULL
);
958 g_io_channel_unref (launcher
->priv
->stderr_channel
);
959 g_source_remove (launcher
->priv
->stderr_watch
);
962 if (launcher
->priv
->pty_channel
)
964 g_io_channel_shutdown (launcher
->priv
->pty_channel
, emit_signal
, NULL
);
965 g_io_channel_unref (launcher
->priv
->pty_channel
);
967 g_source_remove (launcher
->priv
->pty_watch
);
970 if (launcher
->priv
->pty_output_buffer
)
971 g_free (launcher
->priv
->pty_output_buffer
);
972 if (launcher
->priv
->stdout_buffer
)
973 g_free (launcher
->priv
->stdout_buffer
);
974 if (launcher
->priv
->stderr_buffer
)
975 g_free (launcher
->priv
->stdout_buffer
);
977 /* Save them before we re-initialize */
978 child_status
= launcher
->priv
->child_status
;
979 child_pid
= launcher
->priv
->child_pid
;
980 start_time
= launcher
->priv
->start_time
;
983 anjuta_launcher_set_busy (launcher
, FALSE
);
985 anjuta_launcher_initialize (launcher
);
988 /* Call this here, after set_busy (FALSE) so we are able to
989 launch a new child from the terminate function.
990 (by clubfan 2002-04-07)
992 /* DEBUG_PRINT ("Exit status: %d", child_status); */
994 g_signal_emit_by_name (launcher
, "child-exited", child_pid
,
996 time (NULL
) - start_time
);
999 /* Using this function is necessary because
1000 * anjuta_launcher_execution_done_cleanup needs to be called in the same
1001 * thread than the gtk main loop */
1003 anjuta_launcher_call_execution_done (gpointer data
)
1005 AnjutaLauncher
*launcher
= data
;
1007 launcher
->priv
->completion_check_timeout
= -1;
1008 anjuta_launcher_execution_done_cleanup (launcher
, TRUE
);
1012 /* monitors closure of stdout stderr and pty through a gtk_timeout_add setup */
1014 anjuta_launcher_check_for_execution_done (gpointer data
)
1016 AnjutaLauncher
*launcher
= data
;
1018 DEBUG_PRINT ("launcher_execution_done STDOUT:%d, STDERR:%d",
1019 launcher->priv->stdout_is_done ? 1 : 0,
1020 launcher->priv->stderr_is_done ? 1 : 0);
1022 if (launcher
->priv
->stdout_is_done
== FALSE
||
1023 launcher
->priv
->stderr_is_done
== FALSE
)
1025 if (launcher
->priv
->child_has_terminated
== FALSE
)
1027 /* DEBUG_PRINT ("launcher: We missed the exit of the child"); */
1029 launcher
->priv
->completion_check_timeout
= -1;
1030 anjuta_launcher_execution_done_cleanup (launcher
, TRUE
);
1035 anjuta_launcher_child_terminated (GPid pid
, gint status
, gpointer data
)
1037 AnjutaLauncher
*launcher
= data
;
1039 g_return_if_fail(ANJUTA_IS_LAUNCHER(launcher
));
1041 /* Save child exit code */
1042 launcher
->priv
->child_status
= status
;
1043 launcher
->priv
->child_has_terminated
= TRUE
;
1044 anjuta_launcher_synchronize (launcher
);
1049 anjuta_launcher_set_encoding_real (AnjutaLauncher
*launcher
, const gchar
*charset
)
1054 g_return_val_if_fail (launcher
!= NULL
, FALSE
);
1055 // charset can be NULL
1057 s
= g_io_channel_set_encoding (launcher
->priv
->stderr_channel
, charset
, NULL
);
1058 if (s
!= G_IO_STATUS_NORMAL
) r
= FALSE
;
1059 s
= g_io_channel_set_encoding (launcher
->priv
->stdout_channel
, charset
, NULL
);
1060 if (s
!= G_IO_STATUS_NORMAL
) r
= FALSE
;
1061 s
= g_io_channel_set_encoding (launcher
->priv
->pty_channel
, charset
, NULL
);
1062 if (s
!= G_IO_STATUS_NORMAL
) r
= FALSE
;
1066 g_warning ("launcher.c: Failed to set channel encoding!");
1073 * anjuta_launcher_set_encoding:
1074 * @launcher: a #AnjutaLancher object.
1075 * @charset: Character set to use for Input/Output with the process.
1077 * Sets the character set to use for Input/Output with the process.
1081 anjuta_launcher_set_encoding (AnjutaLauncher
*launcher
, const gchar
*charset
)
1083 if (launcher
->priv
->custom_encoding
)
1084 g_free (launcher
->priv
->encoding
);
1086 launcher
->priv
->custom_encoding
= TRUE
;
1088 launcher
->priv
->encoding
= g_strdup(charset
);
1090 launcher
->priv
->encoding
= NULL
;
1094 * anjuta_launcher_set_env:
1095 * @launcher: a #AnjutaLancher object.
1096 * @name: Name of the environment variable to set
1097 * @value: Value of the environment variable to set
1099 * Set an environment variable for the forked process
1103 anjuta_launcher_set_env (AnjutaLauncher
*launcher
,
1107 g_return_if_fail (launcher
&& ANJUTA_IS_LAUNCHER(launcher
));
1108 g_return_if_fail (name
!= NULL
);
1109 g_return_if_fail (value
!= NULL
);
1111 g_hash_table_insert (launcher
->priv
->env
,
1117 anjuta_launcher_fork_setenv (const gchar
* name
, const gchar
* value
)
1119 setenv (name
, value
, TRUE
);
1123 anjuta_launcher_fork (AnjutaLauncher
*launcher
, gchar
*const args
[])
1126 int pty_master_fd
, md
;
1127 int stdout_pipe
[2], stderr_pipe
[2];
1129 struct termios termios_flags
;
1131 working_dir
= g_get_current_dir ();
1137 /* Fork the command */
1138 child_pid
= forkpty (&pty_master_fd
, NULL
, NULL
, NULL
);
1142 dup (stderr_pipe
[1]);
1144 dup (stdout_pipe
[1]);
1146 /* Close unnecessary pipes */
1147 close (stderr_pipe
[0]);
1148 close (stdout_pipe
[0]);
1151 if ((ioctl(pty_slave_fd, TIOCSCTTY, NULL))
1152 perror ("Could not set new controlling tty");
1154 /* Set no delays for the write pipes (non_buffered) so
1155 that we get all the outputs immidiately */
1156 if ((md
= fcntl (stdout_pipe
[1], F_GETFL
)) != -1)
1157 fcntl (stdout_pipe
[1], F_SETFL
, O_SYNC
| md
);
1158 if ((md
= fcntl (stderr_pipe
[1], F_GETFL
)) != -1)
1159 fcntl (stderr_pipe
[1], F_SETFL
, O_SYNC
| md
);
1161 /* Set up environment */
1162 g_hash_table_foreach (launcher
->priv
->env
,
1163 (GHFunc
) anjuta_launcher_fork_setenv
,
1166 execvp (args
[0], args
);
1167 g_warning (_("Cannot execute command: \"%s\""), args
[0]);
1168 perror(_("execvp failed"));
1171 g_free (working_dir
);
1173 /* Close parent's side pipes */
1174 close (stderr_pipe
[1]);
1175 close (stdout_pipe
[1]);
1179 g_warning ("launcher.c: Fork failed!");
1180 /* Close parent's side pipes */
1181 close (stderr_pipe
[0]);
1182 close (stdout_pipe
[0]);
1187 * Set pipes none blocking, so we can read big buffers
1188 * in the callback without having to use FIONREAD
1189 * to make sure the callback doesn't block.
1191 if ((md
= fcntl (stdout_pipe
[0], F_GETFL
)) != -1)
1192 fcntl (stdout_pipe
[0], F_SETFL
, O_NONBLOCK
| md
);
1193 if ((md
= fcntl (stderr_pipe
[0], F_GETFL
)) != -1)
1194 fcntl (stderr_pipe
[0], F_SETFL
, O_NONBLOCK
| md
);
1195 if ((md
= fcntl (pty_master_fd
, F_GETFL
)) != -1)
1196 fcntl (pty_master_fd
, F_SETFL
, O_NONBLOCK
| md
);
1198 launcher
->priv
->child_pid
= child_pid
;
1199 launcher
->priv
->stderr_channel
= g_io_channel_unix_new (stderr_pipe
[0]);
1200 launcher
->priv
->stdout_channel
= g_io_channel_unix_new (stdout_pipe
[0]);
1201 launcher
->priv
->pty_channel
= g_io_channel_unix_new (pty_master_fd
);
1203 g_io_channel_set_buffer_size (launcher
->priv
->pty_channel
, FILE_INPUT_BUFFER_SIZE
);
1205 if (!launcher
->priv
->custom_encoding
)
1206 g_get_charset ((const gchar
**)&launcher
->priv
->encoding
);
1207 anjuta_launcher_set_encoding_real (launcher
, launcher
->priv
->encoding
);
1209 tcgetattr(pty_master_fd
, &termios_flags
);
1210 termios_flags
.c_iflag
&= ~(IGNPAR
| INPCK
| INLCR
| IGNCR
| ICRNL
| IXON
|
1212 termios_flags
.c_iflag
|= IGNBRK
| BRKINT
| IMAXBEL
| IXANY
;
1213 termios_flags
.c_oflag
&= ~OPOST
;
1214 // termios_flags.c_oflag |= 0;
1215 termios_flags
.c_cflag
&= ~(CSTOPB
| CREAD
| PARENB
| HUPCL
);
1216 termios_flags
.c_cflag
|= CS8
| CLOCAL
;
1218 if (!launcher
->priv
->terminal_echo_on
)
1220 termios_flags
.c_lflag
&= ~(ECHOKE
| ECHOE
| ECHO
| ECHONL
|
1224 ECHOCTL
| ISIG
| ICANON
| IEXTEN
| NOFLSH
| TOSTOP
);
1226 // termios_flags.c_lflag |= 0;
1227 termios_flags
.c_cc
[VMIN
] = 0;
1228 cfsetospeed(&termios_flags
, __MAX_BAUD
);
1229 tcsetattr(pty_master_fd
, TCSANOW
, &termios_flags
);
1231 launcher
->priv
->stderr_watch
=
1232 g_io_add_watch (launcher
->priv
->stderr_channel
,
1233 G_IO_IN
| G_IO_ERR
| G_IO_HUP
,
1234 (GIOFunc
)anjuta_launcher_scan_error
, launcher
);
1235 launcher
->priv
->stdout_watch
=
1236 g_io_add_watch (launcher
->priv
->stdout_channel
,
1237 G_IO_IN
| G_IO_ERR
| G_IO_HUP
,
1238 (GIOFunc
)anjuta_launcher_scan_output
, launcher
);
1239 launcher
->priv
->pty_watch
=
1240 g_io_add_watch (launcher
->priv
->pty_channel
,
1241 G_IO_IN
| G_IO_ERR
, /* Do not hook up for G_IO_HUP */
1242 (GIOFunc
)anjuta_launcher_scan_pty
, launcher
);
1244 /* DEBUG_PRINT ("Terminal child forked: %d", launcher->priv->child_pid); */
1245 launcher
->priv
->source
= g_child_watch_add (launcher
->priv
->child_pid
,
1246 anjuta_launcher_child_terminated
, launcher
);
1251 * anjuta_launcher_execute_v:
1252 * @launcher: a #AnjutaLancher object.
1253 * @argv: Command args.
1254 * @callback: The callback for delivering output from the process.
1255 * @callback_data: Callback data for the above callback.
1257 * The first of the @args is the command itself. The rest are sent to the
1258 * as it's arguments. This function works similar to anjuta_launcher_execute().
1260 * Return value: TRUE if successfully launched, otherwise FALSE.
1263 anjuta_launcher_execute_v (AnjutaLauncher
*launcher
, gchar
*const argv
[],
1264 AnjutaLauncherOutputCallback callback
,
1265 gpointer callback_data
)
1267 if (anjuta_launcher_is_busy (launcher
))
1270 anjuta_launcher_set_busy (launcher
, TRUE
);
1272 launcher
->priv
->start_time
= time (NULL
);
1273 launcher
->priv
->child_status
= 0;
1274 launcher
->priv
->stdout_is_done
= FALSE
;
1275 launcher
->priv
->stderr_is_done
= FALSE
;
1276 launcher
->priv
->child_has_terminated
= FALSE
;
1277 launcher
->priv
->in_cleanup
= FALSE
;
1278 launcher
->priv
->output_callback
= callback
;
1279 launcher
->priv
->callback_data
= callback_data
;
1281 /* On a fork error perform a cleanup and return */
1282 if (anjuta_launcher_fork (launcher
, argv
) < 0)
1284 anjuta_launcher_initialize (launcher
);
1291 * anjuta_launcher_execute:
1292 * @launcher: a #AnjutaLancher object.
1293 * @command_str: The command to execute.
1294 * @callback: The callback for delivering output from the process.
1295 * @callback_data: Callback data for the above callback.
1297 * Executes a command in the launcher. Both outputs (STDOUT and STDERR) are
1298 * delivered to the above callback. The data are delivered as they arrive
1299 * from the process and could be of any lenght. If the process asks for
1300 * passwords, the user will be automatically prompted with a dialog to enter
1301 * it. Please note that not all formats of the password are recognized. Those
1302 * with the standard 'assword:' substring in the prompt should work well.
1304 * Return value: TRUE if successfully launched, otherwise FALSE.
1307 anjuta_launcher_execute (AnjutaLauncher
*launcher
, const gchar
*command_str
,
1308 AnjutaLauncherOutputCallback callback
,
1309 gpointer callback_data
)
1311 GList
*args_list
, *args_list_ptr
;
1312 gchar
**args
, **args_ptr
;
1315 /* Prepare command args */
1316 args_list
= anjuta_util_parse_args_from_string (command_str
);
1317 args
= g_new (char*, g_list_length (args_list
) + 1);
1318 args_list_ptr
= args_list
;
1320 while (args_list_ptr
)
1322 *args_ptr
= (char*) args_list_ptr
->data
;
1323 args_list_ptr
= g_list_next (args_list_ptr
);
1328 ret
= anjuta_launcher_execute_v (launcher
, args
,
1329 callback
, callback_data
);
1331 anjuta_util_glist_strings_free (args_list
);
1336 * anjuta_launcher_set_buffered_output:
1337 * @launcher: a #AnjutaLancher object.
1338 * @buffered: buffer output.
1340 * Sets if output should buffered or not. By default, it is buffered.
1342 * Return value: Previous flag value
1345 anjuta_launcher_set_buffered_output (AnjutaLauncher
*launcher
, gboolean buffered
)
1347 gboolean past_value
= launcher
->priv
->buffered_output
;
1348 launcher
->priv
->buffered_output
= buffered
;
1353 * anjuta_launcher_set_check_passwd_prompt:
1354 * @launcher: a #AnjutaLancher object.
1355 * @check_passwd: check for password.
1357 * Set if output is checked for a password prompti. A special dialog box
1358 * is use to enter it in this case. By default, this behavior is enabled.
1360 * Return value: Previous flag value
1363 anjuta_launcher_set_check_passwd_prompt (AnjutaLauncher
*launcher
, gboolean check_passwd
)
1365 gboolean past_value
= launcher
->priv
->check_for_passwd_prompt
;
1366 launcher
->priv
->check_for_passwd_prompt
= check_passwd
;
1371 * anjuta_launcher_set_terminal_echo:
1372 * @launcher: a #AnjutaLancher object.
1373 * @echo_on: Echo ON flag.
1375 * Sets if input (those given in STDIN) should enabled or disabled. By default,
1378 * Return value: Previous flag value
1381 anjuta_launcher_set_terminal_echo (AnjutaLauncher
*launcher
,
1384 gboolean past_value
= launcher
->priv
->terminal_echo_on
;
1385 launcher
->priv
->terminal_echo_on
= echo_on
;
1390 * anjuta_launcher_set_terminate_on_exit:
1391 * @launcher: a #AnjutaLancher object.
1392 * @terminate_on_exit: terminate on exit flag
1394 * When this flag is set, al i/o channels are closed and the child-exit
1395 * signal is emitted as soon as the child exit. By default, or when this
1396 * flag is clear, the launcher object wait until the i/o channels are
1399 * Return value: Previous flag value
1402 anjuta_launcher_set_terminate_on_exit (AnjutaLauncher
*launcher
,
1403 gboolean terminate_on_exit
)
1405 gboolean past_value
= launcher
->priv
->terminate_on_exit
;
1406 launcher
->priv
->terminate_on_exit
= terminate_on_exit
;
1411 * anjuta_launcher_new:
1413 * Sets if input (those given in STDIN) should enabled or disabled. By default,
1416 * Return value: a new instance of #AnjutaLancher class.
1419 anjuta_launcher_new ()
1421 return g_object_new (ANJUTA_TYPE_LAUNCHER
, NULL
);