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.
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.
84 * Returns: %TRUE on success
86 gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, gpointer user_data )
90 gchar *name_src = NULL;
92 gchar *bargs = g_strconcat(babelargs, " -i gpx", NULL);
94 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
95 f = fdopen(fd_src, "w");
96 a_gpx_write_file(vt, f);
99 ret = a_babel_convert_from ( vt, bargs, name_src, cb, user_data );
109 static gboolean babel_general_convert( BabelStatusFunc cb, gchar **args, gpointer user_data )
117 PROCESS_INFORMATION pi;
119 ZeroMemory( &si, sizeof(si) );
120 ZeroMemory( &pi, sizeof(pi) );
122 si.dwFlags = STARTF_USESHOWWINDOW;
123 si.wShowWindow = SW_HIDE;
125 cmd = g_strjoinv( " ", args);
126 args2 = g_strsplit(cmd, "\\", 0);
128 cmd = g_strjoinv( "\\\\", args2);
130 args2 = g_strsplit(cmd, "/", 0);
132 cmd = g_strjoinv( "\\\\", args2);
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.
146 g_error ( "CreateProcess failed" );
150 WaitForSingleObject(pi.hProcess, INFINITE);
151 WaitForSingleObject(pi.hThread, INFINITE);
153 CloseHandle(pi.hThread);
154 CloseHandle(pi.hProcess);
157 cb(BABEL_DONE, NULL, user_data);
170 static gboolean babel_general_convert( BabelStatusFunc cb, gchar **args, gpointer user_data )
172 gboolean ret = FALSE;
174 GError *error = NULL;
177 if (!g_spawn_async_with_pipes (NULL, args, NULL, 0, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) {
178 g_error("Async command failed: %s", error->message);
185 diag = fdopen(babel_stdout, "r");
186 setvbuf(diag, NULL, _IONBF, 0);
188 while (fgets(line, sizeof(line), diag)) {
190 cb(BABEL_DIAG_OUTPUT, line, user_data);
193 cb(BABEL_DONE, NULL, user_data);
196 waitpid(pid, NULL, 0);
197 g_spawn_close_pid(pid);
207 * babel_general_convert_from:
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'
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
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.
221 * Returns: %TRUE on success
223 static gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_dst, gpointer user_data )
225 gboolean ret = FALSE;
228 if (babel_general_convert(cb, args, user_data)) {
230 /* No data actually required but still need to have run gpsbabel anyway
231 - eg using the device power command_off */
235 f = g_fopen(name_dst, "r");
237 ret = a_gpx_read_file ( vt, f );
247 * a_babel_convert_from:
248 * @vt: The TRW layer to place data into. Duplicate items will be overwritten.
249 * @babelargs: A string containing gpsbabel command line options. In addition to any filters, this string
250 * must include the input file type (-i) option.
251 * @cb: Optional callback function. Same usage as in a_babel_convert().
253 * Loads data into a trw layer from a file, using gpsbabel. This routine is synchronous;
254 * that is, it will block the calling program until the conversion is done. To avoid blocking, call
255 * this routine from a worker thread.
257 * Returns: %TRUE on success
259 gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, const char *from, BabelStatusFunc cb, gpointer user_data )
263 gchar *name_dst = NULL;
264 gboolean ret = FALSE;
267 if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
271 gchar **sub_args = g_strsplit(babelargs, " ", 0);
275 args[i++] = unbuffer_loc;
276 args[i++] = gpsbabel_loc;
277 for (j = 0; sub_args[j]; j++) {
278 /* some version of gpsbabel can not take extra blank arg */
279 if (sub_args[j][0] != '\0')
280 args[i++] = sub_args[j];
285 args[i++] = (char *)from;
287 args[i++] = name_dst;
290 ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
292 g_strfreev(sub_args);
294 g_error("gpsbabel not found in PATH");
303 * a_babel_convert_from_shellcommand:
305 * Runs the input command in a shell (bash) and optionally uses GPSBabel to convert from input_file_type.
306 * If input_file_type is %NULL, doesn't use GPSBabel. Input must be GPX (or Geocaching *.loc)
308 * Uses babel_general_convert_from() to actually run the command. This function
309 * prepares the command and temporary file, and sets up the arguments for bash.
311 gboolean a_babel_convert_from_shellcommand ( VikTrwLayer *vt, const char *input_cmd, const char *input_file_type, BabelStatusFunc cb, gpointer user_data )
314 gchar *name_dst = NULL;
315 gboolean ret = FALSE;
318 if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
319 gchar *shell_command;
320 if ( input_file_type )
321 shell_command = g_strdup_printf("%s | %s -i %s -f - -o gpx -F %s",
322 input_cmd, gpsbabel_loc, input_file_type, name_dst);
324 shell_command = g_strdup_printf("%s > %s", input_cmd, name_dst);
326 g_debug("%s: %s", __FUNCTION__, shell_command);
329 args = g_malloc(sizeof(gchar *)*4);
330 args[0] = BASH_LOCATION;
332 args[2] = shell_command;
335 ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
337 g_free ( shell_command );
345 gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char *input_type, BabelStatusFunc cb, gpointer user_data )
347 static DownloadMapOptions options = { FALSE, FALSE, NULL, 0, a_check_kml_file};
350 gboolean ret = FALSE;
351 gchar *name_src = NULL;
352 gchar *babelargs = NULL;
354 g_debug("%s: input_type=%s url=%s", __FUNCTION__, input_type, url);
356 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
360 babelargs = g_strdup_printf(" -i %s", input_type);
362 fetch_ret = a_http_download_get_url(url, "", name_src, &options, NULL);
364 ret = a_babel_convert_from( vt, babelargs, name_src, NULL, NULL);
374 static gboolean babel_general_convert_to( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
376 if (!a_file_export(vt, name_src, FILE_TYPE_GPX, NULL)) {
377 g_error("Error exporting to %s", name_src);
381 return babel_general_convert (cb, args, user_data);
384 gboolean a_babel_convert_to( VikTrwLayer *vt, const char *babelargs, const char *to, BabelStatusFunc cb, gpointer user_data )
388 gchar *name_src = NULL;
389 gboolean ret = FALSE;
392 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
396 gchar **sub_args = g_strsplit(babelargs, " ", 0);
400 args[i++] = unbuffer_loc;
401 args[i++] = gpsbabel_loc;
404 for (j = 0; sub_args[j]; j++)
405 /* some version of gpsbabel can not take extra blank arg */
406 if (sub_args[j][0] != '\0')
407 args[i++] = sub_args[j];
409 args[i++] = name_src;
411 args[i++] = (char *)to;
414 ret = babel_general_convert_to ( vt, cb, args, name_src, user_data );
416 g_strfreev(sub_args);
418 g_error("gpsbabel not found in PATH");
426 static void set_mode(BabelMode mode, gchar *smode)
428 mode.waypointsRead = smode[0] == 'r';
429 mode.waypointsWrite = smode[1] == 'w';
430 mode.tracksRead = smode[2] == 'r';
431 mode.tracksWrite = smode[3] == 'w';
432 mode.routesRead = smode[4] == 'r';
433 mode.routesWrite = smode[5] == 'w';
439 * Load a single feature stored in the given line.
441 static void load_feature_parse_line (gchar *line)
443 gchar **tokens = g_strsplit ( line, "\t", 0 );
445 && tokens[0] != NULL ) {
446 if ( strcmp("serial", tokens[0]) == 0 ) {
447 if ( tokens[1] != NULL
450 && tokens[4] != NULL ) {
451 BabelDevice *device = g_malloc ( sizeof (BabelDevice) );
452 set_mode (device->mode, tokens[1]);
453 device->name = g_strdup (tokens[2]);
454 device->label = g_strdup (tokens[4]);
455 a_babel_device_list = g_list_append (a_babel_device_list, device);
456 g_debug ("New gpsbabel device: %s", device->name);
458 g_warning ( "Unexpected gpsbabel format string: %s", line);
460 } else if ( strcmp("file", tokens[0]) == 0 ) {
461 if ( tokens[1] != NULL
464 && tokens[4] != NULL ) {
465 BabelFile *file = g_malloc ( sizeof (BabelFile) );
466 set_mode (file->mode, tokens[1]);
467 file->name = g_strdup (tokens[2]);
468 file->ext = g_strdup (tokens[3]);
469 file->label = g_strdup (tokens[4]);
470 a_babel_file_list = g_list_append (a_babel_file_list, file);
471 g_debug ("New gpsbabel file: %s", file->name);
473 g_warning ( "Unexpected gpsbabel format string: %s", line);
477 g_warning ( "Unexpected gpsbabel format string: %s", line);
479 g_strfreev ( tokens );
482 static void load_feature_cb (BabelProgressCode code, gpointer line, gpointer user_data)
485 load_feature_parse_line (line);
488 static gboolean load_feature ()
491 gboolean ret = FALSE;
494 if ( gpsbabel_loc ) {
497 args[i++] = unbuffer_loc;
498 args[i++] = gpsbabel_loc;
502 ret = babel_general_convert (load_feature_cb, args, NULL);
504 g_error("gpsbabel not found in PATH");
511 /* TODO allow to set gpsbabel path via command line */
512 gpsbabel_loc = g_find_program_in_path( "gpsbabel" );
514 g_error( "gpsbabel not found in PATH" );
515 unbuffer_loc = g_find_program_in_path( "unbuffer" );
517 g_warning( "unbuffer not found in PATH" );
522 void a_babel_uninit ()
524 g_free ( gpsbabel_loc );
525 g_free ( unbuffer_loc );
527 if ( a_babel_file_list ) {
529 for (gl = a_babel_file_list; gl != NULL; gl = g_list_next(gl)) {
530 BabelFile *file = gl->data;
531 g_free ( file->name );
532 g_free ( file->ext );
533 g_free ( file->label );
536 g_list_free ( a_babel_file_list );
539 if ( a_babel_device_list ) {
541 for (gl = a_babel_device_list; gl != NULL; gl = g_list_next(gl)) {
542 BabelDevice *device = gl->data;
543 g_free ( device->name );
544 g_free ( device->label );
547 g_list_free ( a_babel_device_list );