Updated Spanish translation
[anjuta-git-plugin.git] / plugins / project-wizard / autogen.c
blobc1ed028bfd9cbdd508aee3f582b3900a5bcad93e
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 autogen.c
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 *---------------------------------------------------------------------------*/
32 #include <config.h>
34 #include "autogen.h"
36 #include <libanjuta/anjuta-launcher.h>
38 #include <glib/gstdio.h>
39 #include <glib/gfileutils.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.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 /*---------------------------------------------------------------------------*/
55 struct _NPWAutogen
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
61 * autogen marker */
63 /* Output file name and handle used
64 * when autogen output is written
65 * in a file */
66 const gchar* outfilename;
67 FILE* output;
68 gboolean empty;
69 /* Call back function and data used
70 * when autogen output something */
71 NPWAutogenOutputFunc outfunc;
72 gpointer outdata;
73 /* Call back function and data used
74 * when autogen terminate */
75 NPWAutogenFunc endfunc;
76 gpointer enddata;
78 AnjutaLauncher* launcher;
79 gboolean busy; /* For debugging */
82 /*---------------------------------------------------------------------------*/
84 /* Helper functions
85 *---------------------------------------------------------------------------*/
87 /* Check if autogen version 5 is present */
89 gboolean
90 npw_check_autogen (void)
92 gchar* args[] = {"autogen", "-v", NULL};
93 gchar* output;
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))
98 gint ver[3];
99 gchar* ptr;
101 /* Check autogen */
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;
107 ptr += 5;
108 sscanf(ptr,"%d.%d.%d", &ver[0], &ver[1], &ver[2]);
110 return (ver[0] == 5);
113 return FALSE;
116 /* Write definitions
117 *---------------------------------------------------------------------------*/
119 static void
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);
128 else
129 fprintf (def, "%s = \"%s\";\n", name, value);
133 gboolean
134 npw_autogen_write_definition_file (NPWAutogen* this, NPWValueHeap* values)
136 FILE* def;
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);
148 fclose (def);
150 return TRUE;
153 /* Set input and output
154 *---------------------------------------------------------------------------*/
156 gboolean
157 npw_autogen_set_input_file (NPWAutogen* this, const gchar* filename, const gchar* start_marker, const gchar* end_marker)
159 FILE* tpl;
160 FILE* src;
161 gboolean ok;
162 gchar* buffer;
163 guint len;
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;
184 return TRUE;
187 /* Autogen definition is missing, we need to create a temporary file
188 * with them */
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);
201 fputc ('\n', 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);
209 ok = TRUE;
210 for (;!feof (src);)
212 len = fread (buffer, 1, FILE_BUFFER_SIZE, src);
213 if ((len != FILE_BUFFER_SIZE) && !feof (src))
215 ok = FALSE;
216 break;
219 if (len != fwrite (buffer, 1, len, tpl))
221 ok = FALSE;
222 break;
226 g_free (buffer);
227 fclose (src);
228 fclose (tpl);
230 return ok;
233 gboolean
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;
242 return TRUE;
245 gboolean
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;
255 return TRUE;
258 /* Execute autogen
259 *---------------------------------------------------------------------------*/
261 static void
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);
272 this->empty = FALSE;
275 if (this->outfunc != NULL)
277 /* Call a callback function */
278 (this->outfunc)(output, this->outdata);
282 static void
283 on_autogen_terminated (AnjutaLauncher* launcher, gint pid, gint status, gulong time, NPWAutogen* this)
285 this->busy = FALSE;
286 if (this->output != NULL)
288 fclose (this->output);
289 this->output = NULL;
290 /* Delete empty file */
291 if (this->empty == TRUE)
293 g_remove (this->outfilename);
297 if (this->endfunc)
299 (this->endfunc)(this, this->enddata);
303 gboolean
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 */
314 if (func != NULL)
316 this->endfunc = func;
317 this->enddata = data;
319 else
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)
333 g_set_error(
334 error,
335 G_FILE_ERROR,
336 g_file_error_from_errno(errno),
337 "Could not open file \"%s\": %s",
338 this->outfilename,
339 g_strerror(errno)
342 return FALSE;
344 this->empty = TRUE;
347 this->busy = TRUE;
348 if (!anjuta_launcher_execute_v (this->launcher, args, on_autogen_output, this))
350 return FALSE;
352 /* Keep output as it is */
353 anjuta_launcher_set_encoding (this->launcher, NULL);
355 return TRUE;
358 /* Creation and Destruction
359 *---------------------------------------------------------------------------*/
361 NPWAutogen* npw_autogen_new (void)
363 NPWAutogen* this;
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);
374 return this;
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);
404 g_free (this);