1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 Copyright (C) 2004 Sebastien Granjoux
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 2 of the License, or
9 (at your option) any later version.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Execute autogen program
24 * This takes cares of everything needed to run autogen:
25 * - generate the definition files from a list of values
26 * - add autogen marker in template file if necessary
27 * - check autogen version
28 * - run autogen with AnjutaLauncher object
29 * Autogen output could be written in a file or pass to callback function.
30 *---------------------------------------------------------------------------*/
36 #include <libanjuta/anjuta-launcher.h>
38 #include <glib/gstdio.h>
39 #include <glib/gfileutils.h>
44 #include <glib/gstdio.h>
46 /*---------------------------------------------------------------------------*/
48 #define TMP_DEF_FILENAME "NPWDEFXXXXXX"
49 #define TMP_TPL_FILENAME "NPWTPLXXXXXX"
51 #define FILE_BUFFER_SIZE 4096
53 /*---------------------------------------------------------------------------*/
57 gchar
* deffilename
; /* name of generated definition file */
58 const gchar
* tplfilename
; /* name of template (input) file */
59 gchar
* temptplfilename
; /* name of generated template if the
60 * previous file doesn't contains
63 /* Output file name and handle used
64 * when autogen output is written
66 const gchar
* outfilename
;
69 /* Call back function and data used
70 * when autogen output something */
71 NPWAutogenOutputFunc outfunc
;
73 /* Call back function and data used
74 * when autogen terminate */
75 NPWAutogenFunc endfunc
;
78 AnjutaLauncher
* launcher
;
79 gboolean busy
; /* For debugging */
82 /*---------------------------------------------------------------------------*/
85 *---------------------------------------------------------------------------*/
87 /* Check if autogen version 5 is present */
90 npw_check_autogen (void)
92 gchar
* args
[] = {"autogen", "-v", NULL
};
95 if (g_spawn_sync (NULL
, args
, NULL
, G_SPAWN_SEARCH_PATH
| G_SPAWN_STDERR_TO_DEV_NULL
,
96 NULL
, NULL
, &output
, NULL
, NULL
, NULL
))
102 if (strstr(output
, "The Automated Program Generator") == NULL
) return FALSE
;
104 /* Get version number */
105 ptr
= strstr(output
, "Ver. ");
106 if (ptr
== NULL
) return FALSE
;
108 sscanf(ptr
,"%d.%d.%d", &ver
[0], &ver
[1], &ver
[2]);
110 return (ver
[0] == 5);
117 *---------------------------------------------------------------------------*/
120 cb_autogen_write_definition (const gchar
* name
, const gchar
* value
, NPWValueTag tag
, gpointer user_data
)
122 FILE* def
= (FILE *)user_data
;
124 if ((tag
& NPW_VALID_VALUE
) && (value
!= NULL
))
126 if(value
[0] == '{') /* Seems to be a list, so do not quote */
127 fprintf(def
, "%s = %s;\n", name
, value
);
129 fprintf (def
, "%s = \"%s\";\n", name
, value
);
134 npw_autogen_write_definition_file (NPWAutogen
* this, NPWValueHeap
* values
)
138 /* Autogen should not be running */
139 g_return_val_if_fail (this->busy
== FALSE
, FALSE
);
141 def
= fopen (this->deffilename
, "wt");
142 if (def
== NULL
) return FALSE
;
144 /* Generate definition data for autogen */
145 fputs ("AutoGen Definitions .;\n",def
);
146 npw_value_heap_foreach_value (values
, cb_autogen_write_definition
, def
);
153 /* Set input and output
154 *---------------------------------------------------------------------------*/
157 npw_autogen_set_input_file (NPWAutogen
* this, const gchar
* filename
, const gchar
* start_marker
, const gchar
* end_marker
)
165 /* Autogen should not be running */
166 g_return_val_if_fail (this->busy
== FALSE
, FALSE
);
168 /* We need to specify start and end marker or nothing */
169 g_return_val_if_fail ((start_marker
&& end_marker
) || (!start_marker
&& !end_marker
), FALSE
);
171 /* Remove previous temporary file if exist */
172 if (this->temptplfilename
!= NULL
)
174 remove (this->temptplfilename
);
175 g_free (this->temptplfilename
);
176 this->temptplfilename
= NULL
;
179 if ((start_marker
== NULL
) && (end_marker
== NULL
))
181 /* input file is really an autogen file, nothig do to */
182 this->tplfilename
= filename
;
187 /* Autogen definition is missing, we need to create a temporary file
190 /* Create temporary file */
191 this->temptplfilename
= g_build_filename (g_get_tmp_dir (), TMP_TPL_FILENAME
, NULL
);
192 mktemp (this->temptplfilename
);
193 this->tplfilename
= this->temptplfilename
;
194 tpl
= fopen (this->tplfilename
, "wt");
195 if (tpl
== NULL
) return FALSE
;
197 /* Add autogen definition */
198 fputs (start_marker
, tpl
);
199 fputs (" autogen5 template ", tpl
);
200 fputs (end_marker
, tpl
);
203 /* Copy source file into this new file */
204 src
= fopen (filename
, "rb");
205 if (src
== NULL
) return FALSE
;
207 buffer
= g_new (gchar
, FILE_BUFFER_SIZE
);
212 len
= fread (buffer
, 1, FILE_BUFFER_SIZE
, src
);
213 if ((len
!= FILE_BUFFER_SIZE
) && !feof (src
))
219 if (len
!= fwrite (buffer
, 1, len
, tpl
))
234 npw_autogen_set_output_file (NPWAutogen
* this, const gchar
* filename
)
236 /* Autogen should not be running */
237 g_return_val_if_fail (this->busy
== FALSE
, FALSE
);
239 this->outfilename
= filename
;
240 this->outfunc
= NULL
;
246 npw_autogen_set_output_callback (NPWAutogen
* this, NPWAutogenOutputFunc func
, gpointer user_data
)
248 /* Autogen should not be running */
249 g_return_val_if_fail (this->busy
== FALSE
, FALSE
);
251 this->outfunc
= func
;
252 this->outdata
= user_data
;
253 this->outfilename
= NULL
;
259 *---------------------------------------------------------------------------*/
262 on_autogen_output (AnjutaLauncher
* launcher
, AnjutaLauncherOutputType type
, const gchar
* output
, gpointer user_data
)
264 NPWAutogen
* this = (NPWAutogen
*)user_data
;
266 if (this->outfilename
!= NULL
)
268 /* Write output in a file */
269 if (this->output
!= NULL
)
271 fputs (output
, this->output
);
275 if (this->outfunc
!= NULL
)
277 /* Call a callback function */
278 (this->outfunc
)(output
, this->outdata
);
283 on_autogen_terminated (AnjutaLauncher
* launcher
, gint pid
, gint status
, gulong time
, NPWAutogen
* this)
286 if (this->output
!= NULL
)
288 fclose (this->output
);
290 /* Delete empty file */
291 if (this->empty
== TRUE
)
293 g_remove (this->outfilename
);
299 (this->endfunc
)(this, this->enddata
);
304 npw_autogen_execute (NPWAutogen
* this, NPWAutogenFunc func
, gpointer data
, GError
** error
)
306 gchar
* args
[] = {"autogen", "-T", NULL
, NULL
, NULL
};
308 /* Autogen should not be running */
309 g_return_val_if_fail (this->busy
== FALSE
, FALSE
);
310 g_return_val_if_fail (this, FALSE
);
311 g_return_val_if_fail (this->launcher
, FALSE
);
313 /* Set output end callback */
316 this->endfunc
= func
;
317 this->enddata
= data
;
321 this->endfunc
= NULL
;
323 args
[2] = (gchar
*)this->tplfilename
;
324 args
[3] = (gchar
*)this->deffilename
;
326 /* Check if output file can be written */
327 if (this->outfilename
!= NULL
)
329 /* Open file if it's not already done */
330 this->output
= fopen (this->outfilename
, "wt");
331 if (this->output
== NULL
)
336 g_file_error_from_errno(errno
),
337 "Could not open file \"%s\": %s",
348 if (!anjuta_launcher_execute_v (this->launcher
, args
, on_autogen_output
, this))
352 /* Keep output as it is */
353 anjuta_launcher_set_encoding (this->launcher
, NULL
);
358 /* Creation and Destruction
359 *---------------------------------------------------------------------------*/
361 NPWAutogen
* npw_autogen_new (void)
365 this = g_new0(NPWAutogen
, 1);
367 this->launcher
= anjuta_launcher_new ();
368 g_signal_connect (G_OBJECT (this->launcher
), "child-exited", G_CALLBACK (on_autogen_terminated
), this);
370 /* Create a temporary file for definitions */
371 this->deffilename
= g_build_filename (g_get_tmp_dir (), TMP_DEF_FILENAME
, NULL
);
372 mktemp (this->deffilename
);
377 void npw_autogen_free (NPWAutogen
* this)
379 g_return_if_fail(this != NULL
);
381 if (this->output
!= NULL
)
383 /* output is not used if a callback function is used */
384 fclose (this->output
);
387 if (this->temptplfilename
!= NULL
)
389 /* temptplfilename is not used if input file already
390 * contains autogen marker */
391 remove (this->temptplfilename
);
392 g_free (this->temptplfilename
);
396 /* deffilename should always be here (created in new) */
397 g_return_if_fail (this->deffilename
);
398 remove (this->deffilename
);
399 g_free (this->deffilename
);
401 g_signal_handlers_disconnect_by_func (G_OBJECT (this->launcher
), G_CALLBACK (on_autogen_terminated
), this);
402 g_object_unref (this->launcher
);