]> git.street.me.uk Git - andy/viking.git/blame_incremental - src/babel.c
Merging track: Merge with more than one tracks at a time.
[andy/viking.git] / src / babel.c
... / ...
CommitLineData
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
22/* babel.c: running external programs and redirecting to TRWLayers.
23 * GPSBabel may not be necessary for everything -- for instance,
24 * use a_babel_convert_from_shellcommand with input_file_type == NULL
25 * for an external program that outputs GPX.
26 */
27
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
32#include "viking.h"
33#include "gpx.h"
34#include "babel.h"
35#include <stdio.h>
36#ifdef HAVE_SYS_WAIT_H
37#include <sys/wait.h>
38#endif
39#ifdef HAVE_UNISTD_H
40#include <unistd.h>
41#endif
42#include <glib.h>
43#include <glib/gstdio.h>
44
45/* in the future we could have support for other shells (change command strings), or not use a shell at all */
46#define BASH_LOCATION "/bin/bash"
47
48gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, gpointer user_data )
49{
50 int fd_src;
51 FILE *f;
52 gchar *name_src;
53 gboolean ret = FALSE;
54 gchar *bargs = g_strconcat(babelargs, " -i gpx", NULL);
55
56 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
57 f = fdopen(fd_src, "w");
58 a_gpx_write_file(vt, f);
59 fclose(f);
60 f = NULL;
61 ret = a_babel_convert_from ( vt, bargs, cb, name_src, user_data );
62 }
63
64 g_free(bargs);
65 g_remove(name_src);
66 g_free(name_src);
67 return ret;
68}
69
70/* Runs args[0] with the arguments and uses the GPX module
71 * to import the GPX data into layer vt. Assumes that upon
72 * running the command, the data will appear in the (usually
73 * temporary) file name_dst.
74 *
75 * cb: callback that is run upon new data from STDOUT (?)
76 * (TODO: STDERR would be nice since we usually redirect STDOUT)
77 * user_data: passed along to cb
78 *
79 * returns TRUE on success
80 */
81#ifdef WINDOWS
82gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_dst, gpointer user_data )
83{
84 gboolean ret;
85 FILE *f;
86 gchar *cmd;
87 gchar **args2;
88
89 STARTUPINFO si;
90 PROCESS_INFORMATION pi;
91
92
93 ZeroMemory( &si, sizeof(si) );
94 ZeroMemory( &pi, sizeof(pi) );
95 si.cb = sizeof(si);
96 si.dwFlags = STARTF_USESHOWWINDOW;
97 si.wShowWindow = SW_HIDE;
98
99 cmd = g_strjoinv( " ", args);
100 args2 = g_strsplit(cmd, "\\", 0);
101 g_free(cmd);
102 cmd = g_strjoinv( "\\\\", args2);
103 g_free(args2);
104 args2 = g_strsplit(cmd, "/", 0);
105 g_free(cmd);
106 cmd = g_strjoinv( "\\\\", args2);
107
108 if( !CreateProcess(
109 NULL, // No module name (use command line).
110 (LPTSTR)cmd, // Command line.
111 NULL, // Process handle not inheritable.
112 NULL, // Thread handle not inheritable.
113 FALSE, // Set handle inheritance to FALSE.
114 0, // No creation flags.
115 NULL, // Use parent's environment block.
116 NULL, // Use parent's starting directory.
117 &si, // Pointer to STARTUPINFO structure.
118 &pi ) // Pointer to PROCESS_INFORMATION structure.
119 ){
120 g_warning( "CreateProcess failed");
121 ret = FALSE;
122 }
123 else {
124 WaitForSingleObject(pi.hProcess, INFINITE);
125 WaitForSingleObject(pi.hThread, INFINITE);
126
127 CloseHandle(pi.hThread);
128 CloseHandle(pi.hProcess);
129
130 if ( cb )
131 cb(BABEL_DONE, NULL, user_data);
132
133 f = g_fopen(name_dst, "r");
134 a_gpx_read_file( vt, f );
135 fclose(f);
136 ret = TRUE;
137 }
138
139 g_strfreev( args2 );
140 g_free( cmd );
141
142 return ret;
143}
144/* Windows */
145#else
146/* Posix */
147gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_dst, gpointer user_data )
148{
149 gboolean ret = FALSE;
150 GPid pid;
151 GError *error = NULL;
152 gint babel_stdout;
153 FILE *f;
154
155
156 if (!g_spawn_async_with_pipes (NULL, args, NULL, 0, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) {
157 g_warning("Error : %s", error->message);
158 g_error_free(error);
159 ret = FALSE;
160 } else {
161 gchar line[512];
162 FILE *diag;
163 diag = fdopen(babel_stdout, "r");
164 setvbuf(diag, NULL, _IONBF, 0);
165
166 while (fgets(line, sizeof(line), diag)) {
167 if ( cb )
168 cb(BABEL_DIAG_OUTPUT, line, user_data);
169 }
170 if ( cb )
171 cb(BABEL_DONE, NULL, user_data);
172 fclose(diag);
173 diag = NULL;
174 waitpid(pid, NULL, 0);
175 g_spawn_close_pid(pid);
176
177 f = g_fopen(name_dst, "r");
178 if (f) {
179 a_gpx_read_file ( vt, f );
180 fclose(f);
181 f = NULL;
182 ret = TRUE;
183 }
184 }
185
186 return ret;
187}
188#endif /* Posix */
189
190gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, const char *from, gpointer user_data )
191{
192 int i,j;
193 int fd_dst;
194 gchar *name_dst;
195 gboolean ret = FALSE;
196 gchar *args[64];
197
198 if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
199 gchar *gpsbabel_loc;
200 close(fd_dst);
201
202 gpsbabel_loc = g_find_program_in_path("gpsbabel");
203
204 if (gpsbabel_loc ) {
205 gchar *unbuffer_loc = g_find_program_in_path("unbuffer");
206 gchar **sub_args = g_strsplit(babelargs, " ", 0);
207
208 i = 0;
209 if (unbuffer_loc)
210 args[i++] = unbuffer_loc;
211 args[i++] = gpsbabel_loc;
212 for (j = 0; sub_args[j]; j++)
213 args[i++] = sub_args[j];
214 args[i++] = "-o";
215 args[i++] = "gpx";
216 args[i++] = from;
217 args[i++] = name_dst;
218 args[i] = NULL;
219
220 ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
221
222 g_free ( unbuffer_loc );
223 g_strfreev(sub_args);
224 }
225 g_free(gpsbabel_loc);
226 }
227
228 g_remove(name_dst);
229 g_free(name_dst);
230 /* FIXME: free babelargs ? */
231 return ret;
232}
233
234/* Runs the input command in a shell (bash) and optionally uses GPSBabel to convert from input_file_type.
235 * If input_file_type is NULL, doesn't use GPSBabel. Input must be GPX (or Geocaching *.loc)
236 *
237 * Uses babel_general_convert_from to actually run the command. This function
238 * prepares the command and temporary file, and sets up the arguments for bash.
239 */
240gboolean a_babel_convert_from_shellcommand ( VikTrwLayer *vt, const char *input_cmd, const char *input_file_type, BabelStatusFunc cb, gpointer user_data )
241{
242 int fd_dst;
243 gchar *name_dst;
244 gboolean ret = FALSE;
245 gchar **args;
246
247 if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
248 gchar *shell_command;
249 if ( input_file_type )
250 shell_command = g_strdup_printf("%s | gpsbabel -i %s -f - -o gpx -F %s", input_cmd, input_file_type, name_dst);
251 else
252 shell_command = g_strdup_printf("%s > %s", input_cmd, name_dst);
253
254 g_debug("%s: %s", __FUNCTION__, shell_command);
255 close(fd_dst);
256
257 args = g_malloc(sizeof(gchar *)*4);
258 args[0] = BASH_LOCATION;
259 args[1] = "-c";
260 args[2] = shell_command;
261 args[3] = NULL;
262
263 ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
264 g_free ( args );
265 g_free ( shell_command );
266 }
267
268 g_remove(name_dst);
269 g_free(name_dst);
270 return ret;
271}
272
273gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char *input_type, BabelStatusFunc cb, gpointer user_data )
274{
275 static DownloadOptions options = {NULL, 0, a_check_html_file};
276 gint fd_src;
277 int fetch_ret;
278 gboolean ret = FALSE;
279 gchar *name_src;
280 gchar *babelargs;
281
282 g_debug("%s: input_type=%s url=%s", __FUNCTION__, input_type, url);
283
284 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
285 close(fd_src);
286 g_remove(name_src);
287
288 babelargs = g_strdup_printf(" -i %s", input_type);
289
290 fetch_ret = a_http_download_get_url(url, "", name_src, &options);
291 if (fetch_ret == 0)
292 ret = a_babel_convert_from( vt, babelargs, NULL, name_src, NULL);
293
294 g_remove(name_src);
295 g_free(babelargs);
296 g_free(name_src);
297 }
298
299 return ret;
300}
301
302#ifdef WINDOWS
303gboolean babel_general_convert_to( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
304{
305 gboolean ret;
306 gchar *cmd;
307 gchar **args2;
308
309 if (!a_file_export(vt, name_src, FILE_TYPE_GPX)) {
310 g_warning("%s(): error exporting to %s", __FUNCTION__, name_src);
311 return(FALSE);
312 }
313
314 STARTUPINFO si;
315 PROCESS_INFORMATION pi;
316
317 ZeroMemory( &si, sizeof(si) );
318 ZeroMemory( &pi, sizeof(pi) );
319 si.cb = sizeof(si);
320 si.dwFlags = STARTF_USESHOWWINDOW;
321 si.wShowWindow = SW_HIDE;
322
323
324 cmd = g_strjoinv( " ", args);
325 args2 = g_strsplit(cmd, "\\", 0);
326 cmd = g_strjoinv( "\\\\", args2);
327 g_free(args2);
328 args2 = g_strsplit(cmd, "/", 0);
329 g_free(cmd);
330 cmd = g_strjoinv( "\\\\", args2);
331
332 if( !CreateProcess(
333 NULL, // No module name (use command line).
334 (LPTSTR)cmd, // Command line.
335 NULL, // Process handle not inheritable.
336 NULL, // Thread handle not inheritable.
337 FALSE, // Set handle inheritance to FALSE.
338 0, // No creation flags.
339 NULL, // Use parent's environment block.
340 NULL, // Use parent's starting directory.
341 &si, // Pointer to STARTUPINFO structure.
342 &pi ) // Pointer to PROCESS_INFORMATION structure.
343 ){
344 g_warning( "CreateProcess failed" );
345 ret = FALSE;
346 }
347 else {
348
349 WaitForSingleObject(pi.hProcess, INFINITE);
350 WaitForSingleObject(pi.hThread, INFINITE);
351
352 CloseHandle(pi.hThread);
353 CloseHandle(pi.hProcess);
354
355 if ( cb )
356 cb(BABEL_DONE, NULL, user_data);
357
358 ret = TRUE;
359 }
360
361 g_strfreev(args2);
362 g_free( cmd );
363
364 return ret;
365}
366/* Windows */
367#else
368/* Posix */
369gboolean babel_general_convert_to( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
370{
371 gboolean ret = FALSE;
372 GPid pid;
373 GError *error = NULL;
374 gint babel_stdout;
375
376 if (!a_file_export(vt, name_src, FILE_TYPE_GPX)) {
377 g_warning("%s(): error exporting to %s", __FUNCTION__, name_src);
378 return(FALSE);
379 }
380
381 if (!g_spawn_async_with_pipes (NULL, args, NULL, 0, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) {
382 g_warning("Error : %s", error->message);
383 g_error_free(error);
384 ret = FALSE;
385 } else {
386 gchar line[512];
387 FILE *diag;
388 diag = fdopen(babel_stdout, "r");
389 setvbuf(diag, NULL, _IONBF, 0);
390
391 while (fgets(line, sizeof(line), diag)) {
392 if ( cb )
393 cb(BABEL_DIAG_OUTPUT, line, user_data);
394 }
395 if ( cb )
396 cb(BABEL_DONE, NULL, user_data);
397 fclose(diag);
398 diag = NULL;
399 waitpid(pid, NULL, 0);
400 g_spawn_close_pid(pid);
401
402 ret = TRUE;
403 }
404
405 return ret;
406}
407#endif /* Posix */
408
409gboolean a_babel_convert_to( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, const char *to, gpointer user_data )
410{
411 int i,j;
412 int fd_src;
413 gchar *name_src;
414 gboolean ret = FALSE;
415 gchar *args[64];
416
417 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
418 gchar *gpsbabel_loc;
419 close(fd_src);
420
421 gpsbabel_loc = g_find_program_in_path("gpsbabel");
422
423 if (gpsbabel_loc ) {
424 gchar *unbuffer_loc = g_find_program_in_path("unbuffer");
425 gchar **sub_args = g_strsplit(babelargs, " ", 0);
426
427 i = 0;
428 if (unbuffer_loc)
429 args[i++] = unbuffer_loc;
430 args[i++] = gpsbabel_loc;
431 args[i++] = "-i";
432 args[i++] = "gpx";
433 for (j = 0; sub_args[j]; j++)
434 args[i++] = sub_args[j];
435 args[i++] = name_src;
436 args[i++] = to;
437 args[i] = NULL;
438
439 ret = babel_general_convert_to ( vt, cb, args, name_src, user_data );
440
441 g_free ( unbuffer_loc );
442 g_strfreev(sub_args);
443 }
444 g_free(gpsbabel_loc);
445 }
446
447 g_remove(name_src);
448 g_free(name_src);
449 /* FIXME: free babelargs ? */
450 return ret;
451}