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
;
197 obj
->priv
->source
= 0;
199 /* Synchronization in progress */
200 obj
->priv
->in_cleanup
= FALSE
;
201 obj
->priv
->completion_check_timeout
= -1;
203 /* Terminate child on child exit */
204 obj
->priv
->terminate_on_exit
= FALSE
;
206 /* Start time of execution */
207 obj
->priv
->start_time
= 0;
209 obj
->priv
->buffered_output
= TRUE
;
210 obj
->priv
->check_for_passwd_prompt
= TRUE
;
212 /* Output callback */
213 obj
->priv
->output_callback
= NULL
;
214 obj
->priv
->callback_data
= NULL
;
217 obj
->priv
->custom_encoding
= FALSE
;
218 obj
->priv
->encoding
= NULL
;
221 obj
->priv
->env
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
226 anjuta_launcher_get_type ()
228 static GType obj_type
= 0;
232 static const GTypeInfo obj_info
=
234 sizeof (AnjutaLauncherClass
),
235 (GBaseInitFunc
) NULL
,
236 (GBaseFinalizeFunc
) NULL
,
237 (GClassInitFunc
) anjuta_launcher_class_init
,
238 (GClassFinalizeFunc
) NULL
,
239 NULL
, /* class_data */
240 sizeof (AnjutaLauncher
),
242 (GInstanceInitFunc
) anjuta_launcher_init
,
243 NULL
/* value_table */
245 obj_type
= g_type_register_static (G_TYPE_OBJECT
,
246 "AnjutaLauncher", &obj_info
, 0);
252 anjuta_launcher_dispose (GObject
*obj
)
254 AnjutaLauncher
*launcher
= ANJUTA_LAUNCHER (obj
);
255 if (anjuta_launcher_is_busy (launcher
))
257 g_source_remove (launcher
->priv
->source
);
258 launcher
->priv
->source
= 0;
260 anjuta_launcher_execution_done_cleanup (launcher
, FALSE
);
262 launcher
->priv
->busy
= FALSE
;
265 GNOME_CALL_PARENT (G_OBJECT_CLASS
, dispose
, (obj
));
269 anjuta_launcher_finalize (GObject
*obj
)
271 AnjutaLauncher
*launcher
= ANJUTA_LAUNCHER (obj
);
272 if (launcher
->priv
->custom_encoding
&& launcher
->priv
->encoding
)
273 g_free (launcher
->priv
->encoding
);
275 g_hash_table_destroy (launcher
->priv
->env
);
277 g_free (launcher
->priv
);
278 GNOME_CALL_PARENT (G_OBJECT_CLASS
, finalize
, (obj
));
282 anjuta_launcher_class_init (AnjutaLauncherClass
* klass
)
284 GObjectClass
*object_class
;
285 g_return_if_fail (klass
!= NULL
);
286 object_class
= (GObjectClass
*) klass
;
288 /* DEBUG_PRINT ("Initializing launcher class"); */
290 parent_class
= g_type_class_peek_parent (klass
);
293 * AnjutaLauncher::child-exited
294 * @launcher: a #AnjutaLancher object.
295 * @child_pid: process ID of the child
296 * @status: status as returned by waitpid function
297 * @time: time in seconds taken by the child
299 * Emitted when the child has exited and all i/o channels have
300 * been closed. If the terminate on exit flag is set, the i/o
301 * channels are automatically closed when the child exit.
302 * You need to use WEXITSTATUS and friend to get the child exit
303 * code from the status returned.
305 launcher_signals
[CHILD_EXITED_SIGNAL
] =
306 g_signal_new ("child-exited",
307 G_TYPE_FROM_CLASS (object_class
),
309 G_STRUCT_OFFSET (AnjutaLauncherClass
,
312 anjuta_cclosure_marshal_VOID__INT_INT_ULONG
,
313 G_TYPE_NONE
, 3, G_TYPE_INT
,
314 G_TYPE_INT
, G_TYPE_ULONG
);
317 * AnjutaLauncher::busy
318 * @launcher: a #AnjutaLancher object.
319 * @busy: TRUE is a child is currently running
321 * Emitted when a child starts after a call to one execute function
322 * (busy is TRUE) or when a child exits and all i/o channels are
323 * closed (busy is FALSE).
325 launcher_signals
[BUSY_SIGNAL
] =
326 g_signal_new ("busy",
327 G_TYPE_FROM_CLASS (object_class
),
329 G_STRUCT_OFFSET (AnjutaLauncherClass
,
332 anjuta_cclosure_marshal_VOID__BOOLEAN
,
333 G_TYPE_NONE
, 1, G_TYPE_BOOLEAN
);
335 object_class
->dispose
= anjuta_launcher_dispose
;
336 object_class
->finalize
= anjuta_launcher_finalize
;
340 anjuta_launcher_init (AnjutaLauncher
* obj
)
342 g_return_if_fail (obj
!= NULL
);
343 obj
->priv
= g_new0 (AnjutaLauncherPriv
, 1);
344 anjuta_launcher_initialize (obj
);
348 * anjuta_launcher_is_busy:
349 * @launcher: a #AnjutaLancher object.
351 * Tells if the laucher is currently executing any command.
353 * Return value: TRUE if launcher is busy, otherwisee FALSE.
356 anjuta_launcher_is_busy (AnjutaLauncher
*launcher
)
358 return launcher
->priv
->busy
;
362 anjuta_launcher_set_busy (AnjutaLauncher
*launcher
, gboolean flag
)
364 gboolean old_busy
= launcher
->priv
->busy
;
365 launcher
->priv
->busy
= flag
;
366 if (old_busy
!= flag
)
367 g_signal_emit_by_name (G_OBJECT (launcher
), "busy", flag
);
371 * anjuta_launcher_send_stdin:
372 * @launcher: a #AnjutaLancher object.
373 * @input_str: The string to send to STDIN of the process.
375 * Sends a string to Standard input of the process currently being executed.
378 anjuta_launcher_send_stdin (AnjutaLauncher
*launcher
, const gchar
* input_str
)
380 g_return_if_fail (launcher
);
381 g_return_if_fail (input_str
);
383 anjuta_launcher_send_ptyin (launcher
, input_str
);
387 * anjuta_launcher_send_stdin:
388 * @launcher: a #AnjutaLancher object.
390 * Sends a EOF to Standard input of the process currently being executed.
394 anjuta_launcher_send_stdin_eof (AnjutaLauncher
*launcher
)
397 g_io_channel_shutdown (launcher
->priv
->pty_channel
, TRUE
,
399 g_io_channel_unref (launcher
->priv
->pty_channel
);
400 launcher
->priv
->pty_channel
= NULL
;
404 g_warning ("g_io_channel_shutdown () failed: %s", err
->message
);
409 * anjuta_launcher_send_ptyin:
410 * @launcher: a #AnjutaLancher object.
411 * @input_str: The string to send to PTY of the process.
413 * Sends a string to TTY input of the process currently being executed.
414 * Mostly useful for entering passwords and other inputs which are directly
415 * read from TTY input of the process.
418 anjuta_launcher_send_ptyin (AnjutaLauncher
*launcher
, const gchar
* input_str
)
423 if (!input_str
|| strlen (input_str
) == 0) return;
427 g_io_channel_write_chars (launcher
->priv
->pty_channel
,
428 input_str
, strlen (input_str
),
429 &bytes_written
, &err
);
430 g_io_channel_flush (launcher
->priv
->pty_channel
, NULL
);
433 g_warning ("Error encountered while writing to PTY!. %s",
439 input_str
+= bytes_written
;
445 * anjuta_launcher_reset:
446 * @launcher: a #AnjutaLancher object.
448 * Resets the launcher and kills (SIGTERM) current process, if it is still
452 anjuta_launcher_reset (AnjutaLauncher
*launcher
)
454 if (anjuta_launcher_is_busy (launcher
))
455 kill (launcher
->priv
->child_pid
, SIGTERM
);
459 * anjuta_launcher_signal:
460 * @launcher: a #AnjutaLancher object.
461 * @sig: kernel signal ID (e.g. SIGTERM).
463 * Sends a kernel signal to the process that is being executed.
466 anjuta_launcher_signal (AnjutaLauncher
*launcher
, int sig
)
468 kill (launcher
->priv
->child_pid
, sig
);
472 * anjuta_launcher_get_child_pid:
473 * @launcher: a #AnjutaLancher object.
475 * Gets the Process ID of the child being executed.
477 * Return value: Process ID of the child.
480 anjuta_launcher_get_child_pid (AnjutaLauncher
*launcher
)
482 if (anjuta_launcher_is_busy (launcher
))
483 return launcher
->priv
->child_pid
;
489 anjuta_launcher_synchronize (AnjutaLauncher
*launcher
)
491 if (launcher
->priv
->in_cleanup
) return;
493 if (launcher
->priv
->child_has_terminated
&&
494 launcher
->priv
->stdout_is_done
&&
495 launcher
->priv
->stderr_is_done
)
497 if (launcher
->priv
->completion_check_timeout
>= 0)
498 g_source_remove (launcher
->priv
->completion_check_timeout
);
499 launcher
->priv
->completion_check_timeout
=
500 g_timeout_add (50, anjuta_launcher_check_for_execution_done
,
504 /* This case is not very good, but it blocks the whole IDE
505 because we never new if the child has finished */
506 else if (launcher
->priv
->stdout_is_done
&&
507 launcher
->priv
->stderr_is_done
)
509 /* DEBUG_PRINT ("Child has't exited yet waiting for 200ms"); */
510 if (launcher
->priv
->completion_check_timeout
>= 0)
511 g_source_remove (launcher
->priv
->completion_check_timeout
);
512 launcher
->priv
->completion_check_timeout
=
513 g_timeout_add(200, anjuta_launcher_check_for_execution_done
,
516 /* Add this case for gdb. It creates child inheriting gdb
517 * pipes which are not closed if gdb crashes */
518 else if (launcher
->priv
->child_has_terminated
&&
519 launcher
->priv
->terminate_on_exit
)
521 if (launcher
->priv
->completion_check_timeout
>= 0)
522 g_source_remove (launcher
->priv
->completion_check_timeout
);
523 launcher
->priv
->completion_check_timeout
=
524 g_timeout_add(0, anjuta_launcher_call_execution_done
,
529 /* Password dialog */
531 create_password_dialog (const gchar
* prompt
)
540 g_return_val_if_fail (prompt
, NULL
);
542 dialog
= gtk_dialog_new_with_buttons (prompt
,
543 NULL
, //FIXME: Pass the parent window here
544 // for transient purpose.
545 GTK_DIALOG_DESTROY_WITH_PARENT
,
546 GTK_STOCK_CANCEL
, GTK_RESPONSE_CANCEL
,
547 GTK_STOCK_OK
, GTK_RESPONSE_OK
, NULL
);
548 gtk_dialog_set_default_response (GTK_DIALOG (dialog
), GTK_RESPONSE_OK
);
550 gtk_window_set_wmclass (GTK_WINDOW (dialog
), "launcher-password-prompt",
552 hbox
= gtk_hbox_new (FALSE
, 10);
553 gtk_widget_show (hbox
);
554 gtk_container_add (GTK_CONTAINER (GTK_DIALOG(dialog
)->vbox
), hbox
);
556 icon
= anjuta_res_get_image (ANJUTA_PIXMAP_PASSWORD
);
557 gtk_widget_show (icon
);
558 gtk_box_pack_start_defaults (GTK_BOX(hbox
), icon
);
560 if (strlen (prompt
) < 20) {
561 box
= gtk_hbox_new (FALSE
, 5);
563 box
= gtk_vbox_new (FALSE
, 5);
565 gtk_widget_show (box
);
566 gtk_box_pack_start_defaults (GTK_BOX (hbox
), box
);
568 label
= gtk_label_new (_(prompt
));
569 gtk_widget_show (label
);
570 gtk_box_pack_start (GTK_BOX (box
), label
, FALSE
, FALSE
, 0);
572 entry
= gtk_entry_new ();
573 gtk_widget_show (entry
);
574 gtk_entry_set_visibility (GTK_ENTRY (entry
), FALSE
);
575 gtk_box_pack_start (GTK_BOX (box
), entry
, FALSE
, FALSE
, 0);
577 gtk_widget_ref (entry
);
578 g_object_set_data_full (G_OBJECT (dialog
), "password_entry",
579 gtk_widget_ref (entry
),
580 (GDestroyNotify
) gtk_widget_unref
);
581 gtk_widget_grab_focus (entry
);
582 gtk_entry_set_activates_default (GTK_ENTRY (entry
), TRUE
);
587 /* pty buffer check for password authentication */
589 anjuta_launcher_check_password_real (AnjutaLauncher
*launcher
,
590 const gchar
* last_line
)
592 if (anjuta_launcher_is_busy (launcher
) == FALSE
)
597 /* DEBUG_PRINT ("(In password) Last line = %s", last_line); */
598 if (is_password_prompt(last_line
)) {
599 /* Password prompt detected */
605 dialog
= create_password_dialog (last_line
);
606 button
= gtk_dialog_run (GTK_DIALOG(dialog
));
608 case GTK_RESPONSE_OK
:
609 passwd
= gtk_entry_get_text (
610 GTK_ENTRY (g_object_get_data (G_OBJECT (dialog
),
612 line
= g_strconcat (passwd
, "\n", NULL
);
613 anjuta_launcher_send_ptyin (launcher
, line
);
616 case GTK_RESPONSE_CANCEL
:
617 anjuta_launcher_send_ptyin (launcher
, "<canceled>\n");
618 anjuta_launcher_reset (launcher
);
623 gtk_widget_destroy (dialog
);
629 anjuta_launcher_check_password (AnjutaLauncher
*launcher
, const gchar
*chars
)
634 if (!chars
|| strlen(chars
) <= 0)
637 /* DEBUG_PRINT ("Chars buffer = %s", chars); */
638 start
= end
= strlen (chars
);
639 while (start
> 0 && chars
[start
-1] != '\n') start
--;
643 last_line
= g_strndup (&chars
[start
], end
- start
+ 1);
645 /* DEBUG_PRINT ("Last line = %s", last_line); */
646 /* Checks for password, again */
647 anjuta_launcher_check_password_real (launcher
, last_line
);
653 is_password_prompt (const gchar
* line
)
655 const gchar
* password
= "assword";
656 const gchar
* passphrase
= "assphrase";
658 if (strlen (line
) < strlen (password
)
659 || strlen (line
) < strlen (passphrase
))
662 if (g_strstr_len(line
, 80, password
) != NULL
663 || g_strstr_len(line
, 80, passphrase
) != NULL
)
666 for (i
= strlen(line
) - 1; i
!= 0; --i
)
670 if (g_ascii_isspace(line
[i
]))
680 anjuta_launcher_buffered_output (AnjutaLauncher
*launcher
,
681 AnjutaLauncherOutputType output_type
,
685 gchar
*incomplete_line
;
688 g_return_if_fail (chars
!= NULL
);
689 g_return_if_fail (strlen (chars
) > 0);
691 if (launcher
->priv
->output_callback
== NULL
)
693 if (launcher
->priv
->buffered_output
== FALSE
)
695 (launcher
->priv
->output_callback
)(launcher
, output_type
, chars
,
696 launcher
->priv
->callback_data
);
701 case ANJUTA_LAUNCHER_OUTPUT_STDOUT
:
702 buffer
= &launcher
->priv
->stdout_buffer
;
704 case ANJUTA_LAUNCHER_OUTPUT_STDERR
:
705 buffer
= &launcher
->priv
->stderr_buffer
;
708 g_warning ("Should not reach here");
712 all_lines
= g_strconcat (*buffer
, chars
, NULL
);
714 all_lines
= g_strdup (chars
);
716 /* Buffer the last incomplete line */
717 incomplete_line
= all_lines
+ strlen (all_lines
);
718 while (incomplete_line
> all_lines
&&
719 *incomplete_line
!= '\n')
721 incomplete_line
= g_utf8_prev_char (incomplete_line
);
723 if (*incomplete_line
== '\n')
726 /* Update line buffer */
729 if (strlen(incomplete_line
))
731 *buffer
= g_strdup (incomplete_line
);
732 /* DEBUG_PRINT ("Line buffer for %d: %s", output_type, incomplete_line); */
734 /* Check for password prompt */
735 if (launcher
->priv
->check_for_passwd_prompt
)
736 anjuta_launcher_check_password (launcher
, incomplete_line
);
738 /* Deliver complete lines */
739 *incomplete_line
= '\0';
740 if (strlen (all_lines
) > 0)
741 (launcher
->priv
->output_callback
)(launcher
, output_type
, all_lines
,
742 launcher
->priv
->callback_data
);
747 anjuta_launcher_scan_output (GIOChannel
*channel
, GIOCondition condition
,
748 AnjutaLauncher
*launcher
)
751 gchar buffer
[FILE_BUFFER_SIZE
];
754 if (condition
& G_IO_IN
)
759 g_io_channel_read_chars (channel
, buffer
, FILE_BUFFER_SIZE
-1, &n
, &err
);
760 if (n
> 0) /* There is output */
762 gchar
*utf8_chars
= NULL
;
764 if (!launcher
->priv
->custom_encoding
)
765 utf8_chars
= anjuta_util_convert_to_utf8 (buffer
);
767 utf8_chars
= g_strdup(buffer
);
768 anjuta_launcher_buffered_output (launcher
,
769 ANJUTA_LAUNCHER_OUTPUT_STDOUT
,
773 /* Ignore illegal characters */
774 if (err
&& err
->domain
== G_CONVERT_ERROR
)
779 /* The pipe is closed on the other side */
780 /* if not related to non blocking read or interrupted syscall */
781 else if (err
&& errno
!= EAGAIN
&& errno
!= EINTR
)
783 launcher
->priv
->stdout_is_done
= TRUE
;
784 anjuta_launcher_synchronize (launcher
);
787 /* Read next chars if buffer was too small
788 * (the maximum length of one character is 6 bytes) */
789 } while (!err
&& (n
> FILE_BUFFER_SIZE
- 7));
793 if ((condition
& G_IO_ERR
) || (condition
& G_IO_HUP
))
795 DEBUG_PRINT ("launcher.c: STDOUT pipe closed");
796 launcher
->priv
->stdout_is_done
= TRUE
;
797 anjuta_launcher_synchronize (launcher
);
804 anjuta_launcher_scan_error (GIOChannel
*channel
, GIOCondition condition
,
805 AnjutaLauncher
*launcher
)
808 gchar buffer
[FILE_BUFFER_SIZE
];
811 if (condition
& G_IO_IN
)
816 g_io_channel_read_chars (channel
, buffer
, FILE_BUFFER_SIZE
-1, &n
, &err
);
817 if (n
> 0) /* There is stderr output */
821 utf8_chars
= anjuta_util_convert_to_utf8 (buffer
);
822 anjuta_launcher_buffered_output (launcher
,
823 ANJUTA_LAUNCHER_OUTPUT_STDERR
,
827 /* Ignore illegal characters */
828 if (err
&& err
->domain
== G_CONVERT_ERROR
)
833 /* The pipe is closed on the other side */
834 /* if not related to non blocking read or interrupted syscall */
835 else if (err
&& errno
!= EAGAIN
&& errno
!= EINTR
)
838 launcher
->priv
->stderr_is_done
= TRUE
;
839 anjuta_launcher_synchronize (launcher
);
842 /* Read next chars if buffer was too small
843 * (the maximum length of one character is 6 bytes) */
844 } while (!err
&& (n
> FILE_BUFFER_SIZE
- 7));
848 if ((condition
& G_IO_ERR
) || (condition
& G_IO_HUP
))
850 DEBUG_PRINT ("launcher.c: STDERR pipe closed");
851 launcher
->priv
->stderr_is_done
= TRUE
;
852 anjuta_launcher_synchronize (launcher
);
859 anjuta_launcher_scan_pty (GIOChannel
*channel
, GIOCondition condition
,
860 AnjutaLauncher
*launcher
)
863 gchar buffer
[FILE_BUFFER_SIZE
];
866 if (condition
& G_IO_IN
)
871 g_io_channel_read_chars (channel
, buffer
, FILE_BUFFER_SIZE
-1, &n
, &err
);
872 if (n
> 0) /* There is stderr output */
875 gchar
*old_str
= launcher
->priv
->pty_output_buffer
;
877 utf8_chars
= anjuta_util_convert_to_utf8 (buffer
);
880 gchar
*str
= g_strconcat (old_str
, utf8_chars
, NULL
);
881 launcher
->priv
->pty_output_buffer
= str
;
885 launcher
->priv
->pty_output_buffer
= g_strdup (utf8_chars
);
888 /* Ignore illegal characters */
889 if (err
&& err
->domain
== G_CONVERT_ERROR
)
894 /* The pipe is closed on the other side */
895 /* if not related to non blocking read or interrupted syscall */
896 else if (err
&& errno
!= EAGAIN
&& errno
!= EINTR
)
900 /* Read next chars if buffer was too small
901 * (the maximum length of one character is 6 bytes) */
902 } while (!err
&& (n
> FILE_BUFFER_SIZE
- 7));
905 if (launcher
->priv
->check_for_passwd_prompt
906 && launcher
->priv
->pty_output_buffer
907 && strlen (launcher
->priv
->pty_output_buffer
) > 0)
909 anjuta_launcher_check_password (launcher
,
910 launcher
->priv
->pty_output_buffer
);
913 /* In pty case, we handle the cases in different invocations */
914 /* Do not hook up for G_IO_HUP */
915 if (condition
& G_IO_ERR
)
917 DEBUG_PRINT ("launcher.c: PTY pipe error!");
924 anjuta_launcher_execution_done_cleanup (AnjutaLauncher
*launcher
,
925 gboolean emit_signal
)
927 gint child_status
, child_pid
;
930 if (launcher
->priv
->in_cleanup
)
933 launcher
->priv
->in_cleanup
= TRUE
;
935 /* Remove pending timeout */
936 if (launcher
->priv
->completion_check_timeout
>= 0)
937 g_source_remove (launcher
->priv
->completion_check_timeout
);
939 /* Make sure all pending I/O are flushed out */
940 while (g_main_context_pending (NULL
))
941 g_main_context_iteration (NULL
, FALSE
);
943 /* Can be called again, while waiting in the previous line
944 * Do nothing if clean up is already done */
945 if (launcher
->priv
->stdout_channel
)
947 g_io_channel_shutdown (launcher
->priv
->stdout_channel
, emit_signal
, NULL
);
948 g_io_channel_unref (launcher
->priv
->stdout_channel
);
949 g_source_remove (launcher
->priv
->stdout_watch
);
952 if (launcher
->priv
->stderr_channel
)
954 g_io_channel_shutdown (launcher
->priv
->stderr_channel
, emit_signal
, NULL
);
955 g_io_channel_unref (launcher
->priv
->stderr_channel
);
956 g_source_remove (launcher
->priv
->stderr_watch
);
959 if (launcher
->priv
->pty_channel
)
961 g_io_channel_shutdown (launcher
->priv
->pty_channel
, emit_signal
, NULL
);
962 g_io_channel_unref (launcher
->priv
->pty_channel
);
964 g_source_remove (launcher
->priv
->pty_watch
);
967 if (launcher
->priv
->pty_output_buffer
)
968 g_free (launcher
->priv
->pty_output_buffer
);
969 if (launcher
->priv
->stdout_buffer
)
970 g_free (launcher
->priv
->stdout_buffer
);
971 if (launcher
->priv
->stderr_buffer
)
972 g_free (launcher
->priv
->stdout_buffer
);
974 /* Save them before we re-initialize */
975 child_status
= launcher
->priv
->child_status
;
976 child_pid
= launcher
->priv
->child_pid
;
977 start_time
= launcher
->priv
->start_time
;
980 anjuta_launcher_set_busy (launcher
, FALSE
);
982 anjuta_launcher_initialize (launcher
);
985 /* Call this here, after set_busy (FALSE) so we are able to
986 launch a new child from the terminate function.
987 (by clubfan 2002-04-07)
989 /* DEBUG_PRINT ("Exit status: %d", child_status); */
991 g_signal_emit_by_name (launcher
, "child-exited", child_pid
,
993 time (NULL
) - start_time
);
996 /* Using this function is necessary because
997 * anjuta_launcher_execution_done_cleanup needs to be called in the same
998 * thread than the gtk main loop */
1000 anjuta_launcher_call_execution_done (gpointer data
)
1002 AnjutaLauncher
*launcher
= data
;
1004 launcher
->priv
->completion_check_timeout
= -1;
1005 anjuta_launcher_execution_done_cleanup (launcher
, TRUE
);
1009 /* monitors closure of stdout stderr and pty through a gtk_timeout_add setup */
1011 anjuta_launcher_check_for_execution_done (gpointer data
)
1013 AnjutaLauncher
*launcher
= data
;
1015 DEBUG_PRINT ("launcher_execution_done STDOUT:%d, STDERR:%d",
1016 launcher->priv->stdout_is_done ? 1 : 0,
1017 launcher->priv->stderr_is_done ? 1 : 0);
1019 if (launcher
->priv
->stdout_is_done
== FALSE
||
1020 launcher
->priv
->stderr_is_done
== FALSE
)
1022 if (launcher
->priv
->child_has_terminated
== FALSE
)
1024 /* DEBUG_PRINT ("launcher: We missed the exit of the child"); */
1026 launcher
->priv
->completion_check_timeout
= -1;
1027 anjuta_launcher_execution_done_cleanup (launcher
, TRUE
);
1032 anjuta_launcher_child_terminated (GPid pid
, gint status
, gpointer data
)
1034 AnjutaLauncher
*launcher
= data
;
1036 g_return_if_fail(ANJUTA_IS_LAUNCHER(launcher
));
1038 /* Save child exit code */
1039 launcher
->priv
->child_status
= status
;
1040 launcher
->priv
->child_has_terminated
= TRUE
;
1041 anjuta_launcher_synchronize (launcher
);
1046 anjuta_launcher_set_encoding_real (AnjutaLauncher
*launcher
, const gchar
*charset
)
1051 g_return_val_if_fail (launcher
!= NULL
, FALSE
);
1052 // charset can be NULL
1054 s
= g_io_channel_set_encoding (launcher
->priv
->stderr_channel
, charset
, NULL
);
1055 if (s
!= G_IO_STATUS_NORMAL
) r
= FALSE
;
1056 s
= g_io_channel_set_encoding (launcher
->priv
->stdout_channel
, charset
, NULL
);
1057 if (s
!= G_IO_STATUS_NORMAL
) r
= FALSE
;
1058 s
= g_io_channel_set_encoding (launcher
->priv
->pty_channel
, charset
, NULL
);
1059 if (s
!= G_IO_STATUS_NORMAL
) r
= FALSE
;
1063 g_warning ("launcher.c: Failed to set channel encoding!");
1070 * anjuta_launcher_set_encoding:
1071 * @launcher: a #AnjutaLancher object.
1072 * @charset: Character set to use for Input/Output with the process.
1074 * Sets the character set to use for Input/Output with the process.
1078 anjuta_launcher_set_encoding (AnjutaLauncher
*launcher
, const gchar
*charset
)
1080 if (launcher
->priv
->custom_encoding
)
1081 g_free (launcher
->priv
->encoding
);
1083 launcher
->priv
->custom_encoding
= TRUE
;
1085 launcher
->priv
->encoding
= g_strdup(charset
);
1087 launcher
->priv
->encoding
= NULL
;
1091 * anjuta_launcher_set_env:
1092 * @launcher: a #AnjutaLancher object.
1093 * @name: Name of the environment variable to set
1094 * @value: Value of the environment variable to set
1096 * Set an environment variable for the forked process
1100 anjuta_launcher_set_env (AnjutaLauncher
*launcher
,
1104 g_return_if_fail (launcher
&& ANJUTA_IS_LAUNCHER(launcher
));
1105 g_return_if_fail (name
!= NULL
);
1106 g_return_if_fail (value
!= NULL
);
1108 g_hash_table_insert (launcher
->priv
->env
,
1114 anjuta_launcher_fork_setenv (const gchar
* name
, const gchar
* value
)
1116 setenv (name
, value
, TRUE
);
1120 anjuta_launcher_fork (AnjutaLauncher
*launcher
, gchar
*const args
[])
1123 int pty_master_fd
, md
;
1124 int stdout_pipe
[2], stderr_pipe
[2];
1126 struct termios termios_flags
;
1128 working_dir
= g_get_current_dir ();
1134 /* Fork the command */
1135 child_pid
= forkpty (&pty_master_fd
, NULL
, NULL
, NULL
);
1139 dup (stderr_pipe
[1]);
1141 dup (stdout_pipe
[1]);
1143 /* Close unnecessary pipes */
1144 close (stderr_pipe
[0]);
1145 close (stdout_pipe
[0]);
1148 if ((ioctl(pty_slave_fd, TIOCSCTTY, NULL))
1149 perror ("Could not set new controlling tty");
1151 /* Set no delays for the write pipes (non_buffered) so
1152 that we get all the outputs immidiately */
1153 if ((md
= fcntl (stdout_pipe
[1], F_GETFL
)) != -1)
1154 fcntl (stdout_pipe
[1], F_SETFL
, O_SYNC
| md
);
1155 if ((md
= fcntl (stderr_pipe
[1], F_GETFL
)) != -1)
1156 fcntl (stderr_pipe
[1], F_SETFL
, O_SYNC
| md
);
1158 /* Set up environment */
1159 g_hash_table_foreach (launcher
->priv
->env
,
1160 (GHFunc
) anjuta_launcher_fork_setenv
,
1163 execvp (args
[0], args
);
1164 g_warning (_("Cannot execute command: \"%s\""), args
[0]);
1165 perror(_("execvp failed"));
1168 g_free (working_dir
);
1170 /* Close parent's side pipes */
1171 close (stderr_pipe
[1]);
1172 close (stdout_pipe
[1]);
1176 g_warning ("launcher.c: Fork failed!");
1177 /* Close parent's side pipes */
1178 close (stderr_pipe
[0]);
1179 close (stdout_pipe
[0]);
1184 * Set pipes none blocking, so we can read big buffers
1185 * in the callback without having to use FIONREAD
1186 * to make sure the callback doesn't block.
1188 if ((md
= fcntl (stdout_pipe
[0], F_GETFL
)) != -1)
1189 fcntl (stdout_pipe
[0], F_SETFL
, O_NONBLOCK
| md
);
1190 if ((md
= fcntl (stderr_pipe
[0], F_GETFL
)) != -1)
1191 fcntl (stderr_pipe
[0], F_SETFL
, O_NONBLOCK
| md
);
1192 if ((md
= fcntl (pty_master_fd
, F_GETFL
)) != -1)
1193 fcntl (pty_master_fd
, F_SETFL
, O_NONBLOCK
| md
);
1195 launcher
->priv
->child_pid
= child_pid
;
1196 launcher
->priv
->stderr_channel
= g_io_channel_unix_new (stderr_pipe
[0]);
1197 launcher
->priv
->stdout_channel
= g_io_channel_unix_new (stdout_pipe
[0]);
1198 launcher
->priv
->pty_channel
= g_io_channel_unix_new (pty_master_fd
);
1200 g_io_channel_set_buffer_size (launcher
->priv
->pty_channel
, FILE_INPUT_BUFFER_SIZE
);
1202 if (!launcher
->priv
->custom_encoding
)
1203 g_get_charset ((const gchar
**)&launcher
->priv
->encoding
);
1204 anjuta_launcher_set_encoding_real (launcher
, launcher
->priv
->encoding
);
1206 tcgetattr(pty_master_fd
, &termios_flags
);
1207 termios_flags
.c_iflag
&= ~(IGNPAR
| INPCK
| INLCR
| IGNCR
| ICRNL
| IXON
|
1209 termios_flags
.c_iflag
|= IGNBRK
| BRKINT
| IMAXBEL
| IXANY
;
1210 termios_flags
.c_oflag
&= ~OPOST
;
1211 // termios_flags.c_oflag |= 0;
1212 termios_flags
.c_cflag
&= ~(CSTOPB
| CREAD
| PARENB
| HUPCL
);
1213 termios_flags
.c_cflag
|= CS8
| CLOCAL
;
1215 if (!launcher
->priv
->terminal_echo_on
)
1217 termios_flags
.c_lflag
&= ~(ECHOKE
| ECHOE
| ECHO
| ECHONL
|
1221 ECHOCTL
| ISIG
| ICANON
| IEXTEN
| NOFLSH
| TOSTOP
);
1223 // termios_flags.c_lflag |= 0;
1224 termios_flags
.c_cc
[VMIN
] = 0;
1225 cfsetospeed(&termios_flags
, __MAX_BAUD
);
1226 tcsetattr(pty_master_fd
, TCSANOW
, &termios_flags
);
1228 launcher
->priv
->stderr_watch
=
1229 g_io_add_watch (launcher
->priv
->stderr_channel
,
1230 G_IO_IN
| G_IO_ERR
| G_IO_HUP
,
1231 (GIOFunc
)anjuta_launcher_scan_error
, launcher
);
1232 launcher
->priv
->stdout_watch
=
1233 g_io_add_watch (launcher
->priv
->stdout_channel
,
1234 G_IO_IN
| G_IO_ERR
| G_IO_HUP
,
1235 (GIOFunc
)anjuta_launcher_scan_output
, launcher
);
1236 launcher
->priv
->pty_watch
=
1237 g_io_add_watch (launcher
->priv
->pty_channel
,
1238 G_IO_IN
| G_IO_ERR
, /* Do not hook up for G_IO_HUP */
1239 (GIOFunc
)anjuta_launcher_scan_pty
, launcher
);
1241 /* DEBUG_PRINT ("Terminal child forked: %d", launcher->priv->child_pid); */
1242 launcher
->priv
->source
= g_child_watch_add (launcher
->priv
->child_pid
,
1243 anjuta_launcher_child_terminated
, launcher
);
1248 * anjuta_launcher_execute_v:
1249 * @launcher: a #AnjutaLancher object.
1250 * @argv: Command args.
1251 * @callback: The callback for delivering output from the process.
1252 * @callback_data: Callback data for the above callback.
1254 * The first of the @args is the command itself. The rest are sent to the
1255 * as it's arguments. This function works similar to anjuta_launcher_execute().
1257 * Return value: TRUE if successfully launched, otherwise FALSE.
1260 anjuta_launcher_execute_v (AnjutaLauncher
*launcher
, gchar
*const argv
[],
1261 AnjutaLauncherOutputCallback callback
,
1262 gpointer callback_data
)
1264 if (anjuta_launcher_is_busy (launcher
))
1267 anjuta_launcher_set_busy (launcher
, TRUE
);
1269 launcher
->priv
->start_time
= time (NULL
);
1270 launcher
->priv
->child_status
= 0;
1271 launcher
->priv
->stdout_is_done
= FALSE
;
1272 launcher
->priv
->stderr_is_done
= FALSE
;
1273 launcher
->priv
->child_has_terminated
= FALSE
;
1274 launcher
->priv
->in_cleanup
= FALSE
;
1275 launcher
->priv
->output_callback
= callback
;
1276 launcher
->priv
->callback_data
= callback_data
;
1278 /* On a fork error perform a cleanup and return */
1279 if (anjuta_launcher_fork (launcher
, argv
) < 0)
1281 anjuta_launcher_initialize (launcher
);
1288 * anjuta_launcher_execute:
1289 * @launcher: a #AnjutaLancher object.
1290 * @command_str: The command to execute.
1291 * @callback: The callback for delivering output from the process.
1292 * @callback_data: Callback data for the above callback.
1294 * Executes a command in the launcher. Both outputs (STDOUT and STDERR) are
1295 * delivered to the above callback. The data are delivered as they arrive
1296 * from the process and could be of any lenght. If the process asks for
1297 * passwords, the user will be automatically prompted with a dialog to enter
1298 * it. Please note that not all formats of the password are recognized. Those
1299 * with the standard 'assword:' substring in the prompt should work well.
1301 * Return value: TRUE if successfully launched, otherwise FALSE.
1304 anjuta_launcher_execute (AnjutaLauncher
*launcher
, const gchar
*command_str
,
1305 AnjutaLauncherOutputCallback callback
,
1306 gpointer callback_data
)
1308 GList
*args_list
, *args_list_ptr
;
1309 gchar
**args
, **args_ptr
;
1312 /* Prepare command args */
1313 args_list
= anjuta_util_parse_args_from_string (command_str
);
1314 args
= g_new (char*, g_list_length (args_list
) + 1);
1315 args_list_ptr
= args_list
;
1317 while (args_list_ptr
)
1319 *args_ptr
= (char*) args_list_ptr
->data
;
1320 args_list_ptr
= g_list_next (args_list_ptr
);
1325 ret
= anjuta_launcher_execute_v (launcher
, args
,
1326 callback
, callback_data
);
1328 anjuta_util_glist_strings_free (args_list
);
1333 * anjuta_launcher_set_buffered_output:
1334 * @launcher: a #AnjutaLancher object.
1335 * @buffered: buffer output.
1337 * Sets if output should buffered or not. By default, it is buffered.
1339 * Return value: Previous flag value
1342 anjuta_launcher_set_buffered_output (AnjutaLauncher
*launcher
, gboolean buffered
)
1344 gboolean past_value
= launcher
->priv
->buffered_output
;
1345 launcher
->priv
->buffered_output
= buffered
;
1350 * anjuta_launcher_set_check_passwd_prompt:
1351 * @launcher: a #AnjutaLancher object.
1352 * @check_passwd: check for password.
1354 * Set if output is checked for a password prompti. A special dialog box
1355 * is use to enter it in this case. By default, this behavior is enabled.
1357 * Return value: Previous flag value
1360 anjuta_launcher_set_check_passwd_prompt (AnjutaLauncher
*launcher
, gboolean check_passwd
)
1362 gboolean past_value
= launcher
->priv
->check_for_passwd_prompt
;
1363 launcher
->priv
->check_for_passwd_prompt
= check_passwd
;
1368 * anjuta_launcher_set_terminal_echo:
1369 * @launcher: a #AnjutaLancher object.
1370 * @echo_on: Echo ON flag.
1372 * Sets if input (those given in STDIN) should enabled or disabled. By default,
1375 * Return value: Previous flag value
1378 anjuta_launcher_set_terminal_echo (AnjutaLauncher
*launcher
,
1381 gboolean past_value
= launcher
->priv
->terminal_echo_on
;
1382 launcher
->priv
->terminal_echo_on
= echo_on
;
1387 * anjuta_launcher_set_terminate_on_exit:
1388 * @launcher: a #AnjutaLancher object.
1389 * @terminate_on_exit: terminate on exit flag
1391 * When this flag is set, al i/o channels are closed and the child-exit
1392 * signal is emitted as soon as the child exit. By default, or when this
1393 * flag is clear, the launcher object wait until the i/o channels are
1396 * Return value: Previous flag value
1399 anjuta_launcher_set_terminate_on_exit (AnjutaLauncher
*launcher
,
1400 gboolean terminate_on_exit
)
1402 gboolean past_value
= launcher
->priv
->terminate_on_exit
;
1403 launcher
->priv
->terminate_on_exit
= terminate_on_exit
;
1408 * anjuta_launcher_new:
1410 * Sets if input (those given in STDIN) should enabled or disabled. By default,
1413 * Return value: a new instance of #AnjutaLancher class.
1416 anjuta_launcher_new ()
1418 return g_object_new (ANJUTA_TYPE_LAUNCHER
, NULL
);