]> git.street.me.uk Git - andy/viking.git/blame - src/babel.c
[MAPS] Add OpenSeaMap source
[andy/viking.git] / src / babel.c
CommitLineData
1d1bc3c1
EB
1/*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3 *
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
a482007a 5 * Copyright (C) 2006, Quy Tonthat <qtonthat@gmail.com>
1d1bc3c1
EB
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
072cbb13
GB
23/**
24 * SECTION:babel
25 * @short_description: running external programs and redirecting to TRWLayers.
26 *
28c82d8b 27 * GPSBabel may not be necessary for everything -- for instance,
072cbb13 28 * use a_babel_convert_from_shellcommand() with input_file_type == %NULL
28c82d8b
EB
29 * for an external program that outputs GPX.
30 */
31
45acf79e
MA
32#ifdef HAVE_CONFIG_H
33#include "config.h"
34#endif
35
1d1bc3c1
EB
36#include "viking.h"
37#include "gpx.h"
38#include "babel.h"
8c060406 39#include <stdio.h>
e81e41fb 40#ifdef HAVE_SYS_WAIT_H
29a5a545 41#include <sys/wait.h>
e81e41fb 42#endif
45acf79e
MA
43#ifdef HAVE_UNISTD_H
44#include <unistd.h>
45#endif
79e0f36e 46#include <string.h>
45acf79e
MA
47#include <glib.h>
48#include <glib/gstdio.h>
1d1bc3c1 49
072cbb13 50/* TODO in the future we could have support for other shells (change command strings), or not use a shell at all */
8cf048bd
EB
51#define BASH_LOCATION "/bin/bash"
52
18ec873d
GB
53/**
54 * Path to gpsbabel
55 */
56static gchar *gpsbabel_loc = NULL;
57
58/**
59 * Path to unbuffer
60 */
61static gchar *unbuffer_loc = NULL;
62
79e0f36e
GB
63/**
64 * List of file formats supported by gpsbabel.
65 */
66GList *a_babel_file_list;
67
68/**
69 * List of device supported by gpsbabel.
70 */
71GList *a_babel_device_list;
72
072cbb13
GB
73/**
74 * a_babel_convert:
75 * @vt: The TRW layer to modify. All data will be deleted, and replaced by what gpsbabel outputs.
76 * @babelargs: A string containing gpsbabel command line filter options. No file types or names should
77 * be specified.
78 * @cb: A callback function.
79 *
80 * This function modifies data in a trw layer using gpsbabel filters. This routine is synchronous;
81 * that is, it will block the calling program until the conversion is done. To avoid blocking, call
82 * this routine from a worker thread.
83 *
84 * Returns: %TRUE on success
85 */
7b3479e3 86gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, gpointer user_data )
1d1bc3c1
EB
87{
88 int fd_src;
89 FILE *f;
18ec873d 90 gchar *name_src = NULL;
1d1bc3c1 91 gboolean ret = FALSE;
3adc68b0 92 gchar *bargs = g_strconcat(babelargs, " -i gpx", NULL);
1d1bc3c1 93
2ecf5998 94 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
1d1bc3c1
EB
95 f = fdopen(fd_src, "w");
96 a_gpx_write_file(vt, f);
97 fclose(f);
8c060406 98 f = NULL;
9181183a 99 ret = a_babel_convert_from ( vt, bargs, name_src, cb, user_data );
18ec873d
GB
100 g_remove(name_src);
101 g_free(name_src);
1d1bc3c1
EB
102 }
103
104 g_free(bargs);
1d1bc3c1
EB
105 return ret;
106}
107
70548902 108#ifdef WINDOWS
883e5ed6 109static gboolean babel_general_convert( BabelStatusFunc cb, gchar **args, gpointer user_data )
70548902
MA
110{
111 gboolean ret;
112 FILE *f;
113 gchar *cmd;
114 gchar **args2;
115
116 STARTUPINFO si;
117 PROCESS_INFORMATION pi;
118
70548902
MA
119 ZeroMemory( &si, sizeof(si) );
120 ZeroMemory( &pi, sizeof(pi) );
121 si.cb = sizeof(si);
122 si.dwFlags = STARTF_USESHOWWINDOW;
123 si.wShowWindow = SW_HIDE;
124
125 cmd = g_strjoinv( " ", args);
126 args2 = g_strsplit(cmd, "\\", 0);
127 g_free(cmd);
128 cmd = g_strjoinv( "\\\\", args2);
129 g_free(args2);
130 args2 = g_strsplit(cmd, "/", 0);
131 g_free(cmd);
132 cmd = g_strjoinv( "\\\\", args2);
133
134 if( !CreateProcess(
135 NULL, // No module name (use command line).
136 (LPTSTR)cmd, // Command line.
137 NULL, // Process handle not inheritable.
138 NULL, // Thread handle not inheritable.
139 FALSE, // Set handle inheritance to FALSE.
140 0, // No creation flags.
141 NULL, // Use parent's environment block.
142 NULL, // Use parent's starting directory.
143 &si, // Pointer to STARTUPINFO structure.
144 &pi ) // Pointer to PROCESS_INFORMATION structure.
145 ){
18ec873d 146 g_error ( "CreateProcess failed" );
70548902
MA
147 ret = FALSE;
148 }
149 else {
150 WaitForSingleObject(pi.hProcess, INFINITE);
151 WaitForSingleObject(pi.hThread, INFINITE);
152
153 CloseHandle(pi.hThread);
154 CloseHandle(pi.hProcess);
155
156 if ( cb )
157 cb(BABEL_DONE, NULL, user_data);
158
70548902
MA
159 ret = TRUE;
160 }
161
162 g_strfreev( args2 );
163 g_free( cmd );
164
165 return ret;
166}
167/* Windows */
168#else
169/* Posix */
883e5ed6 170static gboolean babel_general_convert( BabelStatusFunc cb, gchar **args, gpointer user_data )
8cf048bd 171{
2ecf5998 172 gboolean ret = FALSE;
8cf048bd 173 GPid pid;
6c641b1a
MA
174 GError *error = NULL;
175 gint babel_stdout;
8cf048bd 176
6c641b1a 177 if (!g_spawn_async_with_pipes (NULL, args, NULL, 0, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) {
18ec873d 178 g_error("Async command failed: %s", error->message);
6c641b1a 179 g_error_free(error);
8cf048bd
EB
180 ret = FALSE;
181 } else {
2b756ea0 182
8cf048bd
EB
183 gchar line[512];
184 FILE *diag;
185 diag = fdopen(babel_stdout, "r");
186 setvbuf(diag, NULL, _IONBF, 0);
187
188 while (fgets(line, sizeof(line), diag)) {
7b3479e3
EB
189 if ( cb )
190 cb(BABEL_DIAG_OUTPUT, line, user_data);
8cf048bd 191 }
7b3479e3
EB
192 if ( cb )
193 cb(BABEL_DONE, NULL, user_data);
8cf048bd 194 fclose(diag);
8c060406 195 diag = NULL;
8cf048bd
EB
196 waitpid(pid, NULL, 0);
197 g_spawn_close_pid(pid);
198
2634ad33
GB
199 ret = TRUE;
200 }
201
202 return ret;
203}
204#endif /* Posix */
205
206/**
207 * babel_general_convert_from:
733949a2
RN
208 * @vtl: The TrackWaypoint Layer to save the data into
209 * If it is null it signifies that no data is to be processed,
210 * however the gpsbabel command is still ran as it can be for non-data related options eg:
211 * for use with the power off command - 'command_off'
2634ad33
GB
212 * @cb: callback that is run upon new data from STDOUT (?)
213 * (TODO: STDERR would be nice since we usually redirect STDOUT)
214 * @user_data: passed along to cb
215 *
216 * Runs args[0] with the arguments and uses the GPX module
217 * to import the GPX data into layer vt. Assumes that upon
218 * running the command, the data will appear in the (usually
219 * temporary) file name_dst.
220 *
221 * Returns: %TRUE on success
222 */
223static gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_dst, gpointer user_data )
224{
225 gboolean ret = FALSE;
226 FILE *f = NULL;
227
883e5ed6 228 if (babel_general_convert(cb, args, user_data)) {
2634ad33 229
733949a2
RN
230 /* No data actually required but still need to have run gpsbabel anyway
231 - eg using the device power command_off */
232 if ( vt == NULL )
233 return TRUE;
234
8c060406
MA
235 f = g_fopen(name_dst, "r");
236 if (f) {
237 a_gpx_read_file ( vt, f );
238 fclose(f);
239 f = NULL;
240 ret = TRUE;
2ecf5998 241 }
8cf048bd
EB
242 }
243
244 return ret;
245}
246
072cbb13
GB
247/**
248 * a_babel_convert_from:
249 * @vt: The TRW layer to place data into. Duplicate items will be overwritten.
250 * @babelargs: A string containing gpsbabel command line options. In addition to any filters, this string
251 * must include the input file type (-i) option.
252 * @cb: Optional callback function. Same usage as in a_babel_convert().
253 *
254 * Loads data into a trw layer from a file, using gpsbabel. This routine is synchronous;
255 * that is, it will block the calling program until the conversion is done. To avoid blocking, call
256 * this routine from a worker thread.
257 *
258 * Returns: %TRUE on success
259 */
9181183a 260gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const char *from, BabelStatusFunc cb, gpointer user_data )
1d1bc3c1 261{
f3cd9987 262 int i,j;
1d1bc3c1 263 int fd_dst;
18ec873d 264 gchar *name_dst = NULL;
1d1bc3c1 265 gboolean ret = FALSE;
f3cd9987 266 gchar *args[64];
1d1bc3c1 267
2ecf5998 268 if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
1d1bc3c1
EB
269 close(fd_dst);
270
92255687 271 if (gpsbabel_loc ) {
f3cd9987
QT
272 gchar **sub_args = g_strsplit(babelargs, " ", 0);
273
274 i = 0;
275 if (unbuffer_loc)
276 args[i++] = unbuffer_loc;
277 args[i++] = gpsbabel_loc;
e208e3c0
QT
278 for (j = 0; sub_args[j]; j++) {
279 /* some version of gpsbabel can not take extra blank arg */
280 if (sub_args[j][0] != '\0')
281 args[i++] = sub_args[j];
282 }
f3cd9987
QT
283 args[i++] = "-o";
284 args[i++] = "gpx";
e208e3c0 285 args[i++] = "-f";
8398d878 286 args[i++] = (char *)from;
e208e3c0 287 args[i++] = "-F";
f3cd9987
QT
288 args[i++] = name_dst;
289 args[i] = NULL;
290
291 ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
292
f3cd9987 293 g_strfreev(sub_args);
374b7f45 294 } else
18ec873d
GB
295 g_error("gpsbabel not found in PATH");
296 g_remove(name_dst);
297 g_free(name_dst);
1d1bc3c1
EB
298 }
299
1d1bc3c1
EB
300 return ret;
301}
8cf048bd 302
072cbb13
GB
303/**
304 * a_babel_convert_from_shellcommand:
305 *
306 * Runs the input command in a shell (bash) and optionally uses GPSBabel to convert from input_file_type.
307 * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX (or Geocaching *.loc)
28c82d8b 308 *
072cbb13 309 * Uses babel_general_convert_from() to actually run the command. This function
28c82d8b
EB
310 * prepares the command and temporary file, and sets up the arguments for bash.
311 */
312gboolean a_babel_convert_from_shellcommand ( VikTrwLayer *vt, const char *input_cmd, const char *input_file_type, BabelStatusFunc cb, gpointer user_data )
8cf048bd
EB
313{
314 int fd_dst;
18ec873d 315 gchar *name_dst = NULL;
8cf048bd
EB
316 gboolean ret = FALSE;
317 gchar **args;
318
2ecf5998 319 if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
28c82d8b
EB
320 gchar *shell_command;
321 if ( input_file_type )
18ec873d
GB
322 shell_command = g_strdup_printf("%s | %s -i %s -f - -o gpx -F %s",
323 input_cmd, gpsbabel_loc, input_file_type, name_dst);
28c82d8b
EB
324 else
325 shell_command = g_strdup_printf("%s > %s", input_cmd, name_dst);
326
29a5a545 327 g_debug("%s: %s", __FUNCTION__, shell_command);
8cf048bd
EB
328 close(fd_dst);
329
330 args = g_malloc(sizeof(gchar *)*4);
331 args[0] = BASH_LOCATION;
332 args[1] = "-c";
333 args[2] = shell_command;
334 args[3] = NULL;
335
7b3479e3 336 ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
8cf048bd
EB
337 g_free ( args );
338 g_free ( shell_command );
18ec873d
GB
339 g_remove(name_dst);
340 g_free(name_dst);
8cf048bd
EB
341 }
342
8cf048bd
EB
343 return ret;
344}
345
533bbf34
MA
346gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char *input_type, BabelStatusFunc cb, gpointer user_data )
347{
74fbca98 348 static DownloadMapOptions options = { FALSE, FALSE, NULL, 0, a_check_kml_file};
533bbf34 349 gint fd_src;
8bd9fe17
GB
350 int fetch_ret;
351 gboolean ret = FALSE;
18ec873d
GB
352 gchar *name_src = NULL;
353 gchar *babelargs = NULL;
533bbf34
MA
354
355 g_debug("%s: input_type=%s url=%s", __FUNCTION__, input_type, url);
356
8bd9fe17 357 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
533bbf34
MA
358 close(fd_src);
359 g_remove(name_src);
360
361 babelargs = g_strdup_printf(" -i %s", input_type);
362
825413ba 363 fetch_ret = a_http_download_get_url(url, "", name_src, &options, NULL);
8bd9fe17 364 if (fetch_ret == 0)
9181183a 365 ret = a_babel_convert_from( vt, babelargs, name_src, NULL, NULL);
533bbf34
MA
366
367 g_remove(name_src);
368 g_free(babelargs);
369 g_free(name_src);
370 }
371
372 return ret;
373}
374
8f96e967 375static gboolean babel_general_convert_to( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
70548902 376{
f7f8a0a6 377 if (!a_file_export(vt, name_src, FILE_TYPE_GPX, NULL)) {
18ec873d
GB
378 g_error("Error exporting to %s", name_src);
379 return FALSE;
70548902
MA
380 }
381
883e5ed6 382 return babel_general_convert (cb, args, user_data);
b364d6bc
QT
383}
384
9181183a 385gboolean a_babel_convert_to( VikTrwLayer *vt, const char *babelargs, const char *to, BabelStatusFunc cb, gpointer user_data )
b364d6bc 386{
f3cd9987 387 int i,j;
b364d6bc 388 int fd_src;
18ec873d 389 gchar *name_src = NULL;
b364d6bc 390 gboolean ret = FALSE;
f3cd9987 391 gchar *args[64];
b364d6bc 392
2ecf5998 393 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
b364d6bc
QT
394 close(fd_src);
395
b364d6bc 396 if (gpsbabel_loc ) {
f3cd9987
QT
397 gchar **sub_args = g_strsplit(babelargs, " ", 0);
398
399 i = 0;
400 if (unbuffer_loc)
401 args[i++] = unbuffer_loc;
402 args[i++] = gpsbabel_loc;
403 args[i++] = "-i";
404 args[i++] = "gpx";
405 for (j = 0; sub_args[j]; j++)
e208e3c0
QT
406 /* some version of gpsbabel can not take extra blank arg */
407 if (sub_args[j][0] != '\0')
408 args[i++] = sub_args[j];
409 args[i++] = "-f";
f3cd9987 410 args[i++] = name_src;
e208e3c0 411 args[i++] = "-F";
8398d878 412 args[i++] = (char *)to;
f3cd9987
QT
413 args[i] = NULL;
414
415 ret = babel_general_convert_to ( vt, cb, args, name_src, user_data );
416
f3cd9987 417 g_strfreev(sub_args);
374b7f45 418 } else
18ec873d
GB
419 g_error("gpsbabel not found in PATH");
420 g_remove(name_src);
421 g_free(name_src);
b364d6bc
QT
422 }
423
b364d6bc
QT
424 return ret;
425}
18ec873d 426
79e0f36e
GB
427static void set_mode(BabelMode mode, gchar *smode)
428{
429 mode.waypointsRead = smode[0] == 'r';
430 mode.waypointsWrite = smode[1] == 'w';
431 mode.tracksRead = smode[2] == 'r';
432 mode.tracksWrite = smode[3] == 'w';
433 mode.routesRead = smode[4] == 'r';
434 mode.routesWrite = smode[5] == 'w';
435}
436
437/**
438 * load_feature:
439 *
440 * Load a single feature stored in the given line.
441 */
442static void load_feature_parse_line (gchar *line)
443{
444 gchar **tokens = g_strsplit ( line, "\t", 0 );
445 if ( tokens != NULL
446 && tokens[0] != NULL ) {
447 if ( strcmp("serial", tokens[0]) == 0 ) {
448 if ( tokens[1] != NULL
449 && tokens[2] != NULL
450 && tokens[3] != NULL
451 && tokens[4] != NULL ) {
452 BabelDevice *device = g_malloc ( sizeof (BabelDevice) );
453 set_mode (device->mode, tokens[1]);
454 device->name = g_strdup (tokens[2]);
455 device->label = g_strdup (tokens[4]);
456 a_babel_device_list = g_list_append (a_babel_device_list, device);
457 g_debug ("New gpsbabel device: %s", device->name);
458 } else {
459 g_warning ( "Unexpected gpsbabel format string: %s", line);
460 }
461 } else if ( strcmp("file", tokens[0]) == 0 ) {
462 if ( tokens[1] != NULL
463 && tokens[2] != NULL
464 && tokens[3] != NULL
465 && tokens[4] != NULL ) {
466 BabelFile *file = g_malloc ( sizeof (BabelFile) );
467 set_mode (file->mode, tokens[1]);
468 file->name = g_strdup (tokens[2]);
469 file->ext = g_strdup (tokens[3]);
470 file->label = g_strdup (tokens[4]);
471 a_babel_file_list = g_list_append (a_babel_file_list, file);
472 g_debug ("New gpsbabel file: %s", file->name);
473 } else {
474 g_warning ( "Unexpected gpsbabel format string: %s", line);
475 }
476 } /* else: ignore */
477 } else {
478 g_warning ( "Unexpected gpsbabel format string: %s", line);
479 }
480 g_strfreev ( tokens );
481}
482
483static void load_feature_cb (BabelProgressCode code, gpointer line, gpointer user_data)
484{
485 if (line != NULL)
486 load_feature_parse_line (line);
487}
488
489static gboolean load_feature ()
490{
491 int i;
492 gboolean ret = FALSE;
493 gchar *args[4];
494
495 if ( gpsbabel_loc ) {
496 i = 0;
497 if ( unbuffer_loc )
498 args[i++] = unbuffer_loc;
499 args[i++] = gpsbabel_loc;
500 args[i++] = "-^3";
501 args[i] = NULL;
502
503 ret = babel_general_convert (load_feature_cb, args, NULL);
504 } else
505 g_error("gpsbabel not found in PATH");
506
507 return ret;
508}
18ec873d
GB
509
510void a_babel_init ()
511{
512 /* TODO allow to set gpsbabel path via command line */
513 gpsbabel_loc = g_find_program_in_path( "gpsbabel" );
514 if ( !gpsbabel_loc )
515 g_error( "gpsbabel not found in PATH" );
516 unbuffer_loc = g_find_program_in_path( "unbuffer" );
517 if ( !unbuffer_loc )
518 g_warning( "unbuffer not found in PATH" );
519
79e0f36e 520 load_feature ();
18ec873d
GB
521}
522
523void a_babel_uninit ()
524{
525 g_free ( gpsbabel_loc );
526 g_free ( unbuffer_loc );
67c32f00
RN
527
528 if ( a_babel_file_list ) {
529 GList *gl;
530 for (gl = a_babel_file_list; gl != NULL; gl = g_list_next(gl)) {
531 BabelFile *file = gl->data;
532 g_free ( file->name );
533 g_free ( file->ext );
534 g_free ( file->label );
535 g_free ( gl->data );
536 }
537 g_list_free ( a_babel_file_list );
538 }
539
540 if ( a_babel_device_list ) {
541 GList *gl;
542 for (gl = a_babel_device_list; gl != NULL; gl = g_list_next(gl)) {
543 BabelDevice *device = gl->data;
544 g_free ( device->name );
545 g_free ( device->label );
546 g_free ( gl->data );
547 }
548 g_list_free ( a_babel_device_list );
549 }
550
18ec873d 551}