glib/gtester.c:Small -Wall fix
[platform/upstream/glib.git] / glib / gtester.c
1 /* This file is part of gtester
2  *
3  * AUTHORS
4  *     Sven Herzberg  <herzi@gnome-de.org>
5  *
6  * Copyright (C) 2007  Sven Herzberg
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License as
10  * published by the Free Software Foundation; either version 2.1 of the
11  * License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  * USA
22  */
23
24 #include <glib.h>
25
26 /* the read buffer size in bytes */
27 #define READ_BUFFER_SIZE 1024
28
29 static GIOChannel* out = NULL;
30
31 static gboolean
32 child_out_cb (GIOChannel  * source,
33               GIOCondition  condition,
34               gpointer      data)
35 {
36   GError* error = NULL;
37   gsize length = 0;
38   gchar buffer[READ_BUFFER_SIZE];
39   GIOStatus status = G_IO_STATUS_NORMAL;
40
41   while (status == G_IO_STATUS_NORMAL) {
42     status = g_io_channel_read_chars (source, buffer, sizeof (buffer), &length, &error);
43
44     switch (status) {
45     case G_IO_STATUS_NORMAL:
46             // FIXME: this is where the parsing happens
47             g_print ("%d\n", length);
48             break;
49     case G_IO_STATUS_AGAIN:
50             /* retry later */
51             break;
52     case G_IO_STATUS_ERROR:
53             /* fall through into EOF */
54             g_warning ("Error while reading data: %s",
55                        error->message);
56             g_error_free (error);
57     case G_IO_STATUS_EOF:
58             return FALSE;
59     }
60   }
61
62   return TRUE;
63 }
64
65 static void
66 child_watch_cb (GPid     pid,
67                 gint     status,
68                 gpointer data)
69 {
70   GMainLoop* loop = data;
71
72   g_spawn_close_pid (pid);
73
74   /* read the remaining data - also stops the io watch from being polled */
75   child_out_cb (out, G_IO_IN, data);
76   g_main_loop_quit (loop);
77 }
78
79 int
80 main (int   argc,
81       char**argv)
82 {
83   GMainLoop* loop;
84   GError   * error = NULL;
85   GPid       pid = 0;
86   gchar    * working_folder;
87   gchar    * child_argv[] = {
88     "git-annotate",
89     "--incremental",
90     "ChangeLog",
91     NULL
92   };
93   gint        child_out;
94
95   working_folder = g_strdup ("/home/herzi/Hacking/Imendio/WebKit/WebCore"); //g_get_current_dir ();
96   g_spawn_async_with_pipes (working_folder,
97                  child_argv, NULL /* envp */,
98                  G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH,
99                  NULL, NULL,
100                  &pid,
101                  NULL,
102                  &child_out,
103                  NULL,
104                  &error);
105   g_free (working_folder);
106
107   if (error)
108     {
109       g_error ("Couldn't execute child: %s", error->message);
110       /* doesn't return */
111     }
112
113   loop = g_main_loop_new (NULL, FALSE);
114
115   g_child_watch_add (pid,
116                      child_watch_cb,
117                      loop);
118
119   out = g_io_channel_unix_new (child_out);
120   g_io_channel_set_flags (out, G_IO_FLAG_NONBLOCK, NULL); // FIXME: GError
121   g_io_add_watch (out, G_IO_IN,
122                   child_out_cb, loop);
123
124   g_main_loop_run (loop);
125   g_main_loop_unref (loop);
126   return 0;
127 }
128