2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5 * Copyright (C) 2006, Quy Tonthat <qtonthat@gmail.com>
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.
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.
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
25 * @short_description: running external programs and redirecting to TRWLayers.
27 * GPSBabel may not be necessary for everything -- for instance,
28 * use a_babel_convert_from_shellcommand() with input_file_type == %NULL
29 * for an external program that outputs GPX.
40 #ifdef HAVE_SYS_WAIT_H
48 #include <glib/gstdio.h>
50 /* TODO in the future we could have support for other shells (change command strings), or not use a shell at all */
51 #define BASH_LOCATION "/bin/bash"
56 static gchar *gpsbabel_loc = NULL;
61 static gchar *unbuffer_loc = NULL;
64 * List of file formats supported by gpsbabel.
66 GList *a_babel_file_list;
69 * List of device supported by gpsbabel.
71 GList *a_babel_device_list;
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
78 * @cb: A callback function.
79 * @user_data: passed along to cb
80 * @not_used: Must use NULL
82 * This function modifies data in a trw layer using gpsbabel filters. This routine is synchronous;
83 * that is, it will block the calling program until the conversion is done. To avoid blocking, call
84 * this routine from a worker thread.
86 * Returns: %TRUE on success
88 gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, gpointer user_data, gpointer not_used )
92 gchar *name_src = NULL;
94 gchar *bargs = g_strconcat(babelargs, " -i gpx", NULL);
96 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
97 g_debug ("%s: temporary file: %s", __FUNCTION__, name_src);
98 f = fdopen(fd_src, "w");
99 a_gpx_write_file(vt, f, NULL);
102 ret = a_babel_convert_from ( vt, bargs, name_src, cb, user_data, not_used );
112 * Perform any cleanup actions once GPSBabel has completed running
114 static void babel_watch ( GPid pid,
118 g_spawn_close_pid ( pid );
122 * babel_general_convert:
123 * @args: The command line arguments passed to GPSBabel
124 * @cb: callback that is run for each line of GPSBabel output and at completion of the run
125 * callback may be NULL
126 * @user_data: passed along to cb
128 * The function to actually invoke the GPSBabel external command
130 * Returns: %TRUE on successful invocation of GPSBabel command
132 static gboolean babel_general_convert( BabelStatusFunc cb, gchar **args, gpointer user_data )
134 gboolean ret = FALSE;
136 GError *error = NULL;
139 if (!g_spawn_async_with_pipes (NULL, args, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) {
140 g_error("Async command failed: %s", error->message);
147 diag = fdopen(babel_stdout, "r");
148 setvbuf(diag, NULL, _IONBF, 0);
150 while (fgets(line, sizeof(line), diag)) {
152 cb(BABEL_DIAG_OUTPUT, line, user_data);
155 cb(BABEL_DONE, NULL, user_data);
159 g_child_watch_add ( pid, (GChildWatchFunc) babel_watch, NULL );
168 * babel_general_convert_from:
169 * @vtl: The TrackWaypoint Layer to save the data into
170 * If it is null it signifies that no data is to be processed,
171 * however the gpsbabel command is still ran as it can be for non-data related options eg:
172 * for use with the power off command - 'command_off'
173 * @cb: callback that is run upon new data from STDOUT (?)
174 * (TODO: STDERR would be nice since we usually redirect STDOUT)
175 * @user_data: passed along to cb
177 * Runs args[0] with the arguments and uses the GPX module
178 * to import the GPX data into layer vt. Assumes that upon
179 * running the command, the data will appear in the (usually
180 * temporary) file name_dst.
182 * Returns: %TRUE on success
184 static gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_dst, gpointer user_data )
186 gboolean ret = FALSE;
189 if (babel_general_convert(cb, args, user_data)) {
191 /* No data actually required but still need to have run gpsbabel anyway
192 - eg using the device power command_off */
196 f = g_fopen(name_dst, "r");
198 ret = a_gpx_read_file ( vt, f );
208 * a_babel_convert_from:
209 * @vt: The TRW layer to place data into. Duplicate items will be overwritten.
210 * @babelargs: A string containing gpsbabel command line options. In addition to any filters, this string
211 * must include the input file type (-i) option.
212 * @cb: Optional callback function. Same usage as in a_babel_convert().
213 * @user_data: passed along to cb
214 * @not_used: Must use NULL
216 * Loads data into a trw layer from a file, using gpsbabel. This routine is synchronous;
217 * that is, it will block the calling program until the conversion is done. To avoid blocking, call
218 * this routine from a worker thread.
220 * Returns: %TRUE on success
222 gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const char *from, BabelStatusFunc cb, gpointer user_data, gpointer not_used )
226 gchar *name_dst = NULL;
227 gboolean ret = FALSE;
230 if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
231 g_debug ("%s: temporary file: %s", __FUNCTION__, name_dst);
235 gchar **sub_args = g_strsplit(babelargs, " ", 0);
239 args[i++] = unbuffer_loc;
240 args[i++] = gpsbabel_loc;
241 for (j = 0; sub_args[j]; j++) {
242 /* some version of gpsbabel can not take extra blank arg */
243 if (sub_args[j][0] != '\0')
244 args[i++] = sub_args[j];
249 args[i++] = (char *)from;
251 args[i++] = name_dst;
254 ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
256 g_strfreev(sub_args);
258 g_critical("gpsbabel not found in PATH");
267 * a_babel_convert_from_shellcommand:
268 * @vt: The #VikTrwLayer where to insert the collected data
269 * @input_cmd: the command to run
270 * @cb: Optional callback function. Same usage as in a_babel_convert().
271 * @user_data: passed along to cb
272 * @not_used: Must use NULL
274 * Runs the input command in a shell (bash) and optionally uses GPSBabel to convert from input_file_type.
275 * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX (or Geocaching *.loc)
277 * Uses babel_general_convert_from() to actually run the command. This function
278 * prepares the command and temporary file, and sets up the arguments for bash.
280 gboolean a_babel_convert_from_shellcommand ( VikTrwLayer *vt, const char *input_cmd, const char *input_file_type, BabelStatusFunc cb, gpointer user_data, gpointer not_used )
283 gchar *name_dst = NULL;
284 gboolean ret = FALSE;
287 if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
288 g_debug ("%s: temporary file: %s", __FUNCTION__, name_dst);
289 gchar *shell_command;
290 if ( input_file_type )
291 shell_command = g_strdup_printf("%s | %s -i %s -f - -o gpx -F %s",
292 input_cmd, gpsbabel_loc, input_file_type, name_dst);
294 shell_command = g_strdup_printf("%s > %s", input_cmd, name_dst);
296 g_debug("%s: %s", __FUNCTION__, shell_command);
299 args = g_malloc(sizeof(gchar *)*4);
300 args[0] = BASH_LOCATION;
302 args[2] = shell_command;
305 ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
307 g_free ( shell_command );
316 * a_babel_convert_from_url:
317 * @vt: The #VikTrwLayer where to insert the collected data
318 * @url: the URL to fetch
319 * @cb: Optional callback function. Same usage as in a_babel_convert().
320 * @user_data: passed along to cb
321 * @options: download options. Maybe NULL.
323 * Download the file pointed by the URL and optionally uses GPSBabel to convert from input_file_type.
324 * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX.
326 * Returns: %TRUE on successful invocation of GPSBabel or read of the GPX
329 gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char *input_type, BabelStatusFunc cb, gpointer user_data, DownloadMapOptions *options )
331 // If no download options specified, use defaults:
332 DownloadMapOptions myoptions = { FALSE, FALSE, NULL, 0, NULL, NULL };
334 myoptions = *options;
337 gboolean ret = FALSE;
338 gchar *name_src = NULL;
339 gchar *babelargs = NULL;
341 g_debug("%s: input_type=%s url=%s", __FUNCTION__, input_type, url);
343 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
344 g_debug ("%s: temporary file: %s", __FUNCTION__, name_src);
348 fetch_ret = a_http_download_get_url(url, "", name_src, &myoptions, NULL);
349 if (fetch_ret == 0) {
350 if (input_type != NULL) {
351 babelargs = g_strdup_printf(" -i %s", input_type);
352 ret = a_babel_convert_from( vt, babelargs, name_src, NULL, NULL, NULL );
354 /* Process directly the retrieved file */
355 g_debug("%s: directly read GPX file %s", __FUNCTION__, name_src);
356 FILE *f = g_fopen(name_src, "r");
358 ret = a_gpx_read_file ( vt, f );
372 static gboolean babel_general_convert_to( VikTrwLayer *vt, VikTrack *trk, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
374 // Now strips out invisible tracks and waypoints
375 if (!a_file_export(vt, name_src, FILE_TYPE_GPX, trk, FALSE)) {
376 g_critical("Error exporting to %s", name_src);
380 return babel_general_convert (cb, args, user_data);
384 * a_babel_convert_to:
385 * @vt: The TRW layer from which data is taken.
386 * @track: Operate on the individual track if specified. Use NULL when operating on a TRW layer
387 * @babelargs: A string containing gpsbabel command line options. In addition to any filters, this string
388 * must include the input file type (-i) option.
389 * @to: Filename or device the data is written to.
390 * @cb: Optional callback function. Same usage as in a_babel_convert.
391 * @user_data: passed along to cb
393 * Exports data using gpsbabel. This routine is synchronous;
394 * that is, it will block the calling program until the conversion is done. To avoid blocking, call
395 * this routine from a worker thread.
397 * Returns: %TRUE on successful invocation of GPSBabel command
399 gboolean a_babel_convert_to( VikTrwLayer *vt, VikTrack *track, const char *babelargs, const char *to, BabelStatusFunc cb, gpointer user_data )
403 gchar *name_src = NULL;
404 gboolean ret = FALSE;
407 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
408 g_debug ("%s: temporary file: %s", __FUNCTION__, name_src);
412 gchar **sub_args = g_strsplit(babelargs, " ", 0);
416 args[i++] = unbuffer_loc;
417 args[i++] = gpsbabel_loc;
420 for (j = 0; sub_args[j]; j++)
421 /* some version of gpsbabel can not take extra blank arg */
422 if (sub_args[j][0] != '\0')
423 args[i++] = sub_args[j];
425 args[i++] = name_src;
427 args[i++] = (char *)to;
430 ret = babel_general_convert_to ( vt, track, cb, args, name_src, user_data );
432 g_strfreev(sub_args);
434 g_critical("gpsbabel not found in PATH");
442 static void set_mode(BabelMode mode, gchar *smode)
444 mode.waypointsRead = smode[0] == 'r';
445 mode.waypointsWrite = smode[1] == 'w';
446 mode.tracksRead = smode[2] == 'r';
447 mode.tracksWrite = smode[3] == 'w';
448 mode.routesRead = smode[4] == 'r';
449 mode.routesWrite = smode[5] == 'w';
453 * load_feature_parse_line:
455 * Load a single feature stored in the given line.
457 static void load_feature_parse_line (gchar *line)
459 gchar **tokens = g_strsplit ( line, "\t", 0 );
461 && tokens[0] != NULL ) {
462 if ( strcmp("serial", tokens[0]) == 0 ) {
463 if ( tokens[1] != NULL
466 && tokens[4] != NULL ) {
467 BabelDevice *device = g_malloc ( sizeof (BabelDevice) );
468 set_mode (device->mode, tokens[1]);
469 device->name = g_strdup (tokens[2]);
470 device->label = g_strndup (tokens[4], 50); // Limit really long label text
471 a_babel_device_list = g_list_append (a_babel_device_list, device);
472 g_debug ("New gpsbabel device: %s", device->name);
474 g_warning ( "Unexpected gpsbabel format string: %s", line);
476 } else if ( strcmp("file", tokens[0]) == 0 ) {
477 if ( tokens[1] != NULL
480 && tokens[4] != NULL ) {
481 BabelFile *file = g_malloc ( sizeof (BabelFile) );
482 set_mode (file->mode, tokens[1]);
483 file->name = g_strdup (tokens[2]);
484 file->ext = g_strdup (tokens[3]);
485 file->label = g_strdup (tokens[4]);
486 a_babel_file_list = g_list_append (a_babel_file_list, file);
487 g_debug ("New gpsbabel file: %s", file->name);
489 g_warning ( "Unexpected gpsbabel format string: %s", line);
493 g_warning ( "Unexpected gpsbabel format string: %s", line);
495 g_strfreev ( tokens );
498 static void load_feature_cb (BabelProgressCode code, gpointer line, gpointer user_data)
501 load_feature_parse_line (line);
504 static gboolean load_feature ()
507 gboolean ret = FALSE;
510 if ( gpsbabel_loc ) {
513 args[i++] = unbuffer_loc;
514 args[i++] = gpsbabel_loc;
518 ret = babel_general_convert (load_feature_cb, args, NULL);
520 g_critical("gpsbabel not found in PATH");
528 * Initialises babel module.
529 * Mainly check existence of gpsbabel progam
530 * and load all features available in ths version.
534 /* TODO allow to set gpsbabel path via command line */
535 gpsbabel_loc = g_find_program_in_path( "gpsbabel" );
537 g_critical( "gpsbabel not found in PATH" );
538 unbuffer_loc = g_find_program_in_path( "unbuffer" );
540 g_warning( "unbuffer not found in PATH" );
548 * Free resources acquired by a_babel_init.
550 void a_babel_uninit ()
552 g_free ( gpsbabel_loc );
553 g_free ( unbuffer_loc );
555 if ( a_babel_file_list ) {
557 for (gl = a_babel_file_list; gl != NULL; gl = g_list_next(gl)) {
558 BabelFile *file = gl->data;
559 g_free ( file->name );
560 g_free ( file->ext );
561 g_free ( file->label );
564 g_list_free ( a_babel_file_list );
567 if ( a_babel_device_list ) {
569 for (gl = a_babel_device_list; gl != NULL; gl = g_list_next(gl)) {
570 BabelDevice *device = gl->data;
571 g_free ( device->name );
572 g_free ( device->label );
575 g_list_free ( a_babel_device_list );