]> git.street.me.uk Git - andy/viking.git/blame - src/babel.c
Tidy up variable names
[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
c3ac9df8
RN
108/**
109 * Perform any cleanup actions once GPSBabel has completed running
110 */
111static void babel_watch ( GPid pid,
112 gint status,
113 gpointer user_data )
70548902 114{
c3ac9df8 115 g_spawn_close_pid ( pid );
70548902 116}
c3ac9df8
RN
117
118/**
119 * babel_general_convert:
120 * @args: The command line arguments passed to GPSBabel
121 * @cb: callback that is run for each line of GPSBabel output and at completion of the run
122 * callback may be NULL
123 * @user_data: passed along to cb
124 *
125 * The function to actually invoke the GPSBabel external command
126 *
127 * Returns: %TRUE on successful invocation of GPSBabel command
128 */
883e5ed6 129static gboolean babel_general_convert( BabelStatusFunc cb, gchar **args, gpointer user_data )
8cf048bd 130{
2ecf5998 131 gboolean ret = FALSE;
8cf048bd 132 GPid pid;
6c641b1a
MA
133 GError *error = NULL;
134 gint babel_stdout;
8cf048bd 135
c3ac9df8 136 if (!g_spawn_async_with_pipes (NULL, args, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) {
18ec873d 137 g_error("Async command failed: %s", error->message);
6c641b1a 138 g_error_free(error);
8cf048bd
EB
139 ret = FALSE;
140 } else {
2b756ea0 141
8cf048bd
EB
142 gchar line[512];
143 FILE *diag;
144 diag = fdopen(babel_stdout, "r");
145 setvbuf(diag, NULL, _IONBF, 0);
146
147 while (fgets(line, sizeof(line), diag)) {
7b3479e3
EB
148 if ( cb )
149 cb(BABEL_DIAG_OUTPUT, line, user_data);
8cf048bd 150 }
7b3479e3
EB
151 if ( cb )
152 cb(BABEL_DONE, NULL, user_data);
8cf048bd 153 fclose(diag);
8c060406 154 diag = NULL;
c3ac9df8
RN
155
156 g_child_watch_add ( pid, (GChildWatchFunc) babel_watch, NULL );
8cf048bd 157
2634ad33
GB
158 ret = TRUE;
159 }
160
161 return ret;
162}
2634ad33
GB
163
164/**
165 * babel_general_convert_from:
733949a2
RN
166 * @vtl: The TrackWaypoint Layer to save the data into
167 * If it is null it signifies that no data is to be processed,
168 * however the gpsbabel command is still ran as it can be for non-data related options eg:
169 * for use with the power off command - 'command_off'
2634ad33
GB
170 * @cb: callback that is run upon new data from STDOUT (?)
171 * (TODO: STDERR would be nice since we usually redirect STDOUT)
172 * @user_data: passed along to cb
173 *
174 * Runs args[0] with the arguments and uses the GPX module
175 * to import the GPX data into layer vt. Assumes that upon
176 * running the command, the data will appear in the (usually
177 * temporary) file name_dst.
178 *
179 * Returns: %TRUE on success
180 */
181static gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_dst, gpointer user_data )
182{
183 gboolean ret = FALSE;
184 FILE *f = NULL;
185
883e5ed6 186 if (babel_general_convert(cb, args, user_data)) {
2634ad33 187
733949a2
RN
188 /* No data actually required but still need to have run gpsbabel anyway
189 - eg using the device power command_off */
190 if ( vt == NULL )
191 return TRUE;
192
8c060406
MA
193 f = g_fopen(name_dst, "r");
194 if (f) {
efe9a9c3 195 ret = a_gpx_read_file ( vt, f );
8c060406
MA
196 fclose(f);
197 f = NULL;
2ecf5998 198 }
8cf048bd
EB
199 }
200
201 return ret;
202}
203
072cbb13
GB
204/**
205 * a_babel_convert_from:
206 * @vt: The TRW layer to place data into. Duplicate items will be overwritten.
207 * @babelargs: A string containing gpsbabel command line options. In addition to any filters, this string
208 * must include the input file type (-i) option.
209 * @cb: Optional callback function. Same usage as in a_babel_convert().
210 *
211 * Loads data into a trw layer from a file, using gpsbabel. This routine is synchronous;
212 * that is, it will block the calling program until the conversion is done. To avoid blocking, call
213 * this routine from a worker thread.
214 *
215 * Returns: %TRUE on success
216 */
9181183a 217gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const char *from, BabelStatusFunc cb, gpointer user_data )
1d1bc3c1 218{
f3cd9987 219 int i,j;
1d1bc3c1 220 int fd_dst;
18ec873d 221 gchar *name_dst = NULL;
1d1bc3c1 222 gboolean ret = FALSE;
f3cd9987 223 gchar *args[64];
1d1bc3c1 224
2ecf5998 225 if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
1d1bc3c1
EB
226 close(fd_dst);
227
92255687 228 if (gpsbabel_loc ) {
f3cd9987
QT
229 gchar **sub_args = g_strsplit(babelargs, " ", 0);
230
231 i = 0;
232 if (unbuffer_loc)
233 args[i++] = unbuffer_loc;
234 args[i++] = gpsbabel_loc;
e208e3c0
QT
235 for (j = 0; sub_args[j]; j++) {
236 /* some version of gpsbabel can not take extra blank arg */
237 if (sub_args[j][0] != '\0')
238 args[i++] = sub_args[j];
239 }
f3cd9987
QT
240 args[i++] = "-o";
241 args[i++] = "gpx";
e208e3c0 242 args[i++] = "-f";
8398d878 243 args[i++] = (char *)from;
e208e3c0 244 args[i++] = "-F";
f3cd9987
QT
245 args[i++] = name_dst;
246 args[i] = NULL;
247
248 ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
249
f3cd9987 250 g_strfreev(sub_args);
374b7f45 251 } else
24ff9c7b 252 g_critical("gpsbabel not found in PATH");
18ec873d
GB
253 g_remove(name_dst);
254 g_free(name_dst);
1d1bc3c1
EB
255 }
256
1d1bc3c1
EB
257 return ret;
258}
8cf048bd 259
072cbb13
GB
260/**
261 * a_babel_convert_from_shellcommand:
262 *
263 * Runs the input command in a shell (bash) and optionally uses GPSBabel to convert from input_file_type.
264 * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX (or Geocaching *.loc)
28c82d8b 265 *
072cbb13 266 * Uses babel_general_convert_from() to actually run the command. This function
28c82d8b
EB
267 * prepares the command and temporary file, and sets up the arguments for bash.
268 */
269gboolean a_babel_convert_from_shellcommand ( VikTrwLayer *vt, const char *input_cmd, const char *input_file_type, BabelStatusFunc cb, gpointer user_data )
8cf048bd
EB
270{
271 int fd_dst;
18ec873d 272 gchar *name_dst = NULL;
8cf048bd
EB
273 gboolean ret = FALSE;
274 gchar **args;
275
2ecf5998 276 if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
28c82d8b
EB
277 gchar *shell_command;
278 if ( input_file_type )
18ec873d
GB
279 shell_command = g_strdup_printf("%s | %s -i %s -f - -o gpx -F %s",
280 input_cmd, gpsbabel_loc, input_file_type, name_dst);
28c82d8b
EB
281 else
282 shell_command = g_strdup_printf("%s > %s", input_cmd, name_dst);
283
29a5a545 284 g_debug("%s: %s", __FUNCTION__, shell_command);
8cf048bd
EB
285 close(fd_dst);
286
287 args = g_malloc(sizeof(gchar *)*4);
288 args[0] = BASH_LOCATION;
289 args[1] = "-c";
290 args[2] = shell_command;
291 args[3] = NULL;
292
7b3479e3 293 ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
8cf048bd
EB
294 g_free ( args );
295 g_free ( shell_command );
18ec873d
GB
296 g_remove(name_dst);
297 g_free(name_dst);
8cf048bd
EB
298 }
299
8cf048bd
EB
300 return ret;
301}
302
533bbf34
MA
303gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char *input_type, BabelStatusFunc cb, gpointer user_data )
304{
74fbca98 305 static DownloadMapOptions options = { FALSE, FALSE, NULL, 0, a_check_kml_file};
533bbf34 306 gint fd_src;
8bd9fe17
GB
307 int fetch_ret;
308 gboolean ret = FALSE;
18ec873d
GB
309 gchar *name_src = NULL;
310 gchar *babelargs = NULL;
533bbf34
MA
311
312 g_debug("%s: input_type=%s url=%s", __FUNCTION__, input_type, url);
313
8bd9fe17 314 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
533bbf34
MA
315 close(fd_src);
316 g_remove(name_src);
317
318 babelargs = g_strdup_printf(" -i %s", input_type);
319
825413ba 320 fetch_ret = a_http_download_get_url(url, "", name_src, &options, NULL);
8bd9fe17 321 if (fetch_ret == 0)
9181183a 322 ret = a_babel_convert_from( vt, babelargs, name_src, NULL, NULL);
533bbf34
MA
323
324 g_remove(name_src);
325 g_free(babelargs);
326 g_free(name_src);
327 }
328
329 return ret;
330}
331
8f96e967 332static gboolean babel_general_convert_to( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
70548902 333{
f7f8a0a6 334 if (!a_file_export(vt, name_src, FILE_TYPE_GPX, NULL)) {
24ff9c7b 335 g_critical("Error exporting to %s", name_src);
18ec873d 336 return FALSE;
70548902
MA
337 }
338
883e5ed6 339 return babel_general_convert (cb, args, user_data);
b364d6bc
QT
340}
341
9181183a 342gboolean a_babel_convert_to( VikTrwLayer *vt, const char *babelargs, const char *to, BabelStatusFunc cb, gpointer user_data )
b364d6bc 343{
f3cd9987 344 int i,j;
b364d6bc 345 int fd_src;
18ec873d 346 gchar *name_src = NULL;
b364d6bc 347 gboolean ret = FALSE;
f3cd9987 348 gchar *args[64];
b364d6bc 349
2ecf5998 350 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
b364d6bc
QT
351 close(fd_src);
352
b364d6bc 353 if (gpsbabel_loc ) {
f3cd9987
QT
354 gchar **sub_args = g_strsplit(babelargs, " ", 0);
355
356 i = 0;
357 if (unbuffer_loc)
358 args[i++] = unbuffer_loc;
359 args[i++] = gpsbabel_loc;
360 args[i++] = "-i";
361 args[i++] = "gpx";
362 for (j = 0; sub_args[j]; j++)
e208e3c0
QT
363 /* some version of gpsbabel can not take extra blank arg */
364 if (sub_args[j][0] != '\0')
365 args[i++] = sub_args[j];
366 args[i++] = "-f";
f3cd9987 367 args[i++] = name_src;
e208e3c0 368 args[i++] = "-F";
8398d878 369 args[i++] = (char *)to;
f3cd9987
QT
370 args[i] = NULL;
371
372 ret = babel_general_convert_to ( vt, cb, args, name_src, user_data );
373
f3cd9987 374 g_strfreev(sub_args);
374b7f45 375 } else
24ff9c7b 376 g_critical("gpsbabel not found in PATH");
18ec873d
GB
377 g_remove(name_src);
378 g_free(name_src);
b364d6bc
QT
379 }
380
b364d6bc
QT
381 return ret;
382}
18ec873d 383
79e0f36e
GB
384static void set_mode(BabelMode mode, gchar *smode)
385{
386 mode.waypointsRead = smode[0] == 'r';
387 mode.waypointsWrite = smode[1] == 'w';
388 mode.tracksRead = smode[2] == 'r';
389 mode.tracksWrite = smode[3] == 'w';
390 mode.routesRead = smode[4] == 'r';
391 mode.routesWrite = smode[5] == 'w';
392}
393
394/**
395 * load_feature:
396 *
397 * Load a single feature stored in the given line.
398 */
399static void load_feature_parse_line (gchar *line)
400{
401 gchar **tokens = g_strsplit ( line, "\t", 0 );
402 if ( tokens != NULL
403 && tokens[0] != NULL ) {
404 if ( strcmp("serial", tokens[0]) == 0 ) {
405 if ( tokens[1] != NULL
406 && tokens[2] != NULL
407 && tokens[3] != NULL
408 && tokens[4] != NULL ) {
409 BabelDevice *device = g_malloc ( sizeof (BabelDevice) );
410 set_mode (device->mode, tokens[1]);
411 device->name = g_strdup (tokens[2]);
431624c5 412 device->label = g_strndup (tokens[4], 50); // Limit really long label text
79e0f36e
GB
413 a_babel_device_list = g_list_append (a_babel_device_list, device);
414 g_debug ("New gpsbabel device: %s", device->name);
415 } else {
416 g_warning ( "Unexpected gpsbabel format string: %s", line);
417 }
418 } else if ( strcmp("file", tokens[0]) == 0 ) {
419 if ( tokens[1] != NULL
420 && tokens[2] != NULL
421 && tokens[3] != NULL
422 && tokens[4] != NULL ) {
423 BabelFile *file = g_malloc ( sizeof (BabelFile) );
424 set_mode (file->mode, tokens[1]);
425 file->name = g_strdup (tokens[2]);
426 file->ext = g_strdup (tokens[3]);
427 file->label = g_strdup (tokens[4]);
428 a_babel_file_list = g_list_append (a_babel_file_list, file);
429 g_debug ("New gpsbabel file: %s", file->name);
430 } else {
431 g_warning ( "Unexpected gpsbabel format string: %s", line);
432 }
433 } /* else: ignore */
434 } else {
435 g_warning ( "Unexpected gpsbabel format string: %s", line);
436 }
437 g_strfreev ( tokens );
438}
439
440static void load_feature_cb (BabelProgressCode code, gpointer line, gpointer user_data)
441{
442 if (line != NULL)
443 load_feature_parse_line (line);
444}
445
446static gboolean load_feature ()
447{
448 int i;
449 gboolean ret = FALSE;
450 gchar *args[4];
451
452 if ( gpsbabel_loc ) {
453 i = 0;
454 if ( unbuffer_loc )
455 args[i++] = unbuffer_loc;
456 args[i++] = gpsbabel_loc;
457 args[i++] = "-^3";
458 args[i] = NULL;
459
460 ret = babel_general_convert (load_feature_cb, args, NULL);
461 } else
24ff9c7b 462 g_critical("gpsbabel not found in PATH");
79e0f36e
GB
463
464 return ret;
465}
18ec873d
GB
466
467void a_babel_init ()
468{
469 /* TODO allow to set gpsbabel path via command line */
470 gpsbabel_loc = g_find_program_in_path( "gpsbabel" );
471 if ( !gpsbabel_loc )
24ff9c7b 472 g_critical( "gpsbabel not found in PATH" );
18ec873d
GB
473 unbuffer_loc = g_find_program_in_path( "unbuffer" );
474 if ( !unbuffer_loc )
475 g_warning( "unbuffer not found in PATH" );
476
79e0f36e 477 load_feature ();
18ec873d
GB
478}
479
480void a_babel_uninit ()
481{
482 g_free ( gpsbabel_loc );
483 g_free ( unbuffer_loc );
67c32f00
RN
484
485 if ( a_babel_file_list ) {
486 GList *gl;
487 for (gl = a_babel_file_list; gl != NULL; gl = g_list_next(gl)) {
488 BabelFile *file = gl->data;
489 g_free ( file->name );
490 g_free ( file->ext );
491 g_free ( file->label );
492 g_free ( gl->data );
493 }
494 g_list_free ( a_babel_file_list );
495 }
496
497 if ( a_babel_device_list ) {
498 GList *gl;
499 for (gl = a_babel_device_list; gl != NULL; gl = g_list_next(gl)) {
500 BabelDevice *device = gl->data;
501 g_free ( device->name );
502 g_free ( device->label );
503 g_free ( gl->data );
504 }
505 g_list_free ( a_babel_device_list );
506 }
507
18ec873d 508}