Re: Executing external process goes into infinite loop



Hi,

thanks a lot for your tip. Replacing the gdk_input_add() call with
g_id_add_watch() seems to have cured the problem.

Greetings/Thanks
Robert

On Mon, May 4, 2009 at 5:35 PM, Ardhan Madras <ajhwb knac com> wrote:
In execute_input() i can't see a code to make a infinite loop, If you are curious, see codes of 
gdk_input_add() and find out why the GdkInputFunction recalled, btw have you try g_io_add_watch() instead 
by making a call to gdk_input_add() the deprecated one?.

--- ajhwb


--- rgasch gmail com wrote:

From: RNG <rgasch gmail com>
To: gtk-app-devel-list gnome org
Subject: Executing external process goes into infinite loop
Date: Mon, 4 May 2009 08:52:56 +0200

Hi,

I have inherited an old version of a glade-based program which I am
trying to clean up. I have applied some fixes and for the most part
things are going well, but 1 function in particular is giving me
trouble and I will readily admit that I don't quite understand how
this is working and how it should work.

The function in question calls a rendering process (povray) by calling
the execute_command() function. This works but upon completion of the
rendering process, my program goes into an infinite loop, continually
calling the execute_input() function as if the end of the povray
process is never detected. Below is the relevant code, I would be
grateful as to any ideas what might be wrong here because as stated
above, I am stumped.

Thanks
Robert


-------------------------------------------------------------------------


typedef struct ExecData ExecData;
struct ExecData
{
 int           input;
 gchar        *text;
 gchar        *error_message;
 FinishedFunc  finish_func;
 gpointer      user_data;
 gint          pid;
 gint          input_id;
};

static ExecData *
exec_data_new (int           in,
              gchar        *error_msg,
              FinishedFunc  func,
              gpointer      user_data,
              int           pid)
{
 ExecData *data;

 data = g_new0 (ExecData, 1);
 data->input = in;
 data->text = g_strdup ("");
 data->error_message = g_strdup (error_msg);
 data->finish_func = func;
 data->user_data = user_data;
 data->pid = pid;

 return data;
}

static void
exec_data_free (ExecData *data)
{
 close (data->input);
 g_free (data->error_message);
 g_free (data->text);
 g_free (data);
}

static void
execute_input (gpointer          user_data,
              gint              source_fd,
              GdkInputCondition condition)
{
 ExecData *data = (ExecData*) user_data;
 gchar     buf[1025];
 gint      length;

 fcntl (data->input, F_SETFL, O_NONBLOCK);
 length = read (data->input, buf, 1024);
 fcntl (data->input, F_SETFL, 0);
 if (length <= 0)
   {
     if (length == 0 && errno != EAGAIN)
       {
         gchar *open_paren;
         gint   status;

         open_paren = strrchr (data->text, '(');
         status = -1;
         if (open_paren != NULL)
         if (sscanf (&open_paren[1], "%i", &status) != 1)
           status = -1;

         /* POV-Ray returns 512 on an aborted trace.
            Don't popup an error dialog box. */
         if (status != 0 && status != 512)
           {
             GtkWidget *window;

             window = create_execution_error_window ();

             set_text (window, "error_label", data->error_message);
             set_text (window, "error_text", data->text);
             gtk_widget_show (window);
           }

         if (data->finish_func != NULL)
           data->finish_func (data->user_data);

         waitpid (data->pid, NULL, 0);

         gdk_input_remove (data->input_id);
         close (data->input);
         exec_data_free (data);
       }
   }
 else
   {
     buf[length] = '\0';
     data->text =
       g_renew (gchar, data->text, length + strlen (data->text) + 1);
     strcat (data->text, buf);
   }
}

void
execute_command (gchar        *command,
                gchar        *error_message,
                FinishedFunc  func,
                gpointer      user_data)
{
 int       in;
 ExecData *data;
 int       pid;

 in = proc_open (command, &pid);
 data = exec_data_new (in, error_message, func, user_data, pid);

 g_free (data->text);
 data->text = g_strdup_printf ("Executing the command: '%s'\n\n", command);

 data->input_id =
   gdk_input_add (in,
                  GDK_INPUT_READ,
                  (GdkInputFunction) execute_input,
                  (gpointer) data);
}
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list




_____________________________________________________________
Listen to KNAC, Hit the Home page and Tune In Live! ---> http://www.knac.com




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]