2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
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.
36 #ifdef HAVE_SYS_WAIT_H
43 #include <glib/gstdio.h>
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"
48 gboolean
a_babel_convert( VikTrwLayer
*vt
, const char *babelargs
, BabelStatusFunc cb
, gpointer user_data
)
54 gchar
*bargs
= g_strconcat(babelargs
, " -i gpx", NULL
);
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
);
61 ret
= a_babel_convert_from ( vt
, bargs
, cb
, name_src
, user_data
);
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.
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
79 * returns TRUE on success
82 gboolean
babel_general_convert_from( VikTrwLayer
*vt
, BabelStatusFunc cb
, gchar
**args
, const gchar
*name_dst
, gpointer user_data
)
90 PROCESS_INFORMATION pi
;
93 ZeroMemory( &si
, sizeof(si
) );
94 ZeroMemory( &pi
, sizeof(pi
) );
96 si
.dwFlags
= STARTF_USESHOWWINDOW
;
97 si
.wShowWindow
= SW_HIDE
;
99 cmd
= g_strjoinv( " ", args
);
100 args2
= g_strsplit(cmd
, "\\", 0);
102 cmd
= g_strjoinv( "\\\\", args2
);
104 args2
= g_strsplit(cmd
, "/", 0);
106 cmd
= g_strjoinv( "\\\\", args2
);
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.
120 g_warning( "CreateProcess failed");
124 WaitForSingleObject(pi
.hProcess
, INFINITE
);
125 WaitForSingleObject(pi
.hThread
, INFINITE
);
127 CloseHandle(pi
.hThread
);
128 CloseHandle(pi
.hProcess
);
131 cb(BABEL_DONE
, NULL
, user_data
);
133 f
= g_fopen(name_dst
, "r");
134 a_gpx_read_file( vt
, f
);
147 gboolean
babel_general_convert_from( VikTrwLayer
*vt
, BabelStatusFunc cb
, gchar
**args
, const gchar
*name_dst
, gpointer user_data
)
149 gboolean ret
= FALSE
;
151 GError
*error
= NULL
;
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
);
163 diag
= fdopen(babel_stdout
, "r");
164 setvbuf(diag
, NULL
, _IONBF
, 0);
166 while (fgets(line
, sizeof(line
), diag
)) {
168 cb(BABEL_DIAG_OUTPUT
, line
, user_data
);
171 cb(BABEL_DONE
, NULL
, user_data
);
174 waitpid(pid
, NULL
, 0);
175 g_spawn_close_pid(pid
);
177 f
= g_fopen(name_dst
, "r");
179 a_gpx_read_file ( vt
, f
);
190 gboolean
a_babel_convert_from( VikTrwLayer
*vt
, const char *babelargs
, BabelStatusFunc cb
, const char *from
, gpointer user_data
)
195 gboolean ret
= FALSE
;
198 if ((fd_dst
= g_file_open_tmp("tmp-viking.XXXXXX", &name_dst
, NULL
)) >= 0) {
202 gpsbabel_loc
= g_find_program_in_path("gpsbabel");
205 gchar
*unbuffer_loc
= g_find_program_in_path("unbuffer");
206 gchar
**sub_args
= g_strsplit(babelargs
, " ", 0);
210 args
[i
++] = unbuffer_loc
;
211 args
[i
++] = gpsbabel_loc
;
212 for (j
= 0; sub_args
[j
]; j
++) {
213 /* some version of gpsbabel can not take extra blank arg */
214 if (sub_args
[j
][0] != '\0')
215 args
[i
++] = sub_args
[j
];
222 args
[i
++] = name_dst
;
225 ret
= babel_general_convert_from ( vt
, cb
, args
, name_dst
, user_data
);
227 g_free ( unbuffer_loc
);
228 g_strfreev(sub_args
);
230 g_warning("gpsbabel not found in PATH");
231 g_free(gpsbabel_loc
);
239 /* Runs the input command in a shell (bash) and optionally uses GPSBabel to convert from input_file_type.
240 * If input_file_type is NULL, doesn't use GPSBabel. Input must be GPX (or Geocaching *.loc)
242 * Uses babel_general_convert_from to actually run the command. This function
243 * prepares the command and temporary file, and sets up the arguments for bash.
245 gboolean
a_babel_convert_from_shellcommand ( VikTrwLayer
*vt
, const char *input_cmd
, const char *input_file_type
, BabelStatusFunc cb
, gpointer user_data
)
249 gboolean ret
= FALSE
;
252 if ((fd_dst
= g_file_open_tmp("tmp-viking.XXXXXX", &name_dst
, NULL
)) >= 0) {
253 gchar
*shell_command
;
254 if ( input_file_type
)
255 shell_command
= g_strdup_printf("%s | gpsbabel -i %s -f - -o gpx -F %s", input_cmd
, input_file_type
, name_dst
);
257 shell_command
= g_strdup_printf("%s > %s", input_cmd
, name_dst
);
259 g_debug("%s: %s", __FUNCTION__
, shell_command
);
262 args
= g_malloc(sizeof(gchar
*)*4);
263 args
[0] = BASH_LOCATION
;
265 args
[2] = shell_command
;
268 ret
= babel_general_convert_from ( vt
, cb
, args
, name_dst
, user_data
);
270 g_free ( shell_command
);
278 gboolean
a_babel_convert_from_url ( VikTrwLayer
*vt
, const char *url
, const char *input_type
, BabelStatusFunc cb
, gpointer user_data
)
280 static DownloadOptions options
= {NULL
, 0, a_check_kml_file
};
283 gboolean ret
= FALSE
;
287 g_debug("%s: input_type=%s url=%s", __FUNCTION__
, input_type
, url
);
289 if ((fd_src
= g_file_open_tmp("tmp-viking.XXXXXX", &name_src
, NULL
)) >= 0) {
293 babelargs
= g_strdup_printf(" -i %s", input_type
);
295 fetch_ret
= a_http_download_get_url(url
, "", name_src
, &options
);
297 ret
= a_babel_convert_from( vt
, babelargs
, NULL
, name_src
, NULL
);
308 gboolean
babel_general_convert_to( VikTrwLayer
*vt
, BabelStatusFunc cb
, gchar
**args
, const gchar
*name_src
, gpointer user_data
)
314 if (!a_file_export(vt
, name_src
, FILE_TYPE_GPX
)) {
315 g_warning("%s(): error exporting to %s", __FUNCTION__
, name_src
);
320 PROCESS_INFORMATION pi
;
322 ZeroMemory( &si
, sizeof(si
) );
323 ZeroMemory( &pi
, sizeof(pi
) );
325 si
.dwFlags
= STARTF_USESHOWWINDOW
;
326 si
.wShowWindow
= SW_HIDE
;
329 cmd
= g_strjoinv( " ", args
);
330 args2
= g_strsplit(cmd
, "\\", 0);
331 cmd
= g_strjoinv( "\\\\", args2
);
333 args2
= g_strsplit(cmd
, "/", 0);
335 cmd
= g_strjoinv( "\\\\", args2
);
338 NULL
, // No module name (use command line).
339 (LPTSTR
)cmd
, // Command line.
340 NULL
, // Process handle not inheritable.
341 NULL
, // Thread handle not inheritable.
342 FALSE
, // Set handle inheritance to FALSE.
343 0, // No creation flags.
344 NULL
, // Use parent's environment block.
345 NULL
, // Use parent's starting directory.
346 &si
, // Pointer to STARTUPINFO structure.
347 &pi
) // Pointer to PROCESS_INFORMATION structure.
349 g_warning( "CreateProcess failed" );
354 WaitForSingleObject(pi
.hProcess
, INFINITE
);
355 WaitForSingleObject(pi
.hThread
, INFINITE
);
357 CloseHandle(pi
.hThread
);
358 CloseHandle(pi
.hProcess
);
361 cb(BABEL_DONE
, NULL
, user_data
);
374 gboolean
babel_general_convert_to( VikTrwLayer
*vt
, BabelStatusFunc cb
, gchar
**args
, const gchar
*name_src
, gpointer user_data
)
376 gboolean ret
= FALSE
;
378 GError
*error
= NULL
;
381 if (!a_file_export(vt
, name_src
, FILE_TYPE_GPX
)) {
382 g_warning("%s(): error exporting to %s", __FUNCTION__
, name_src
);
386 if (!g_spawn_async_with_pipes (NULL
, args
, NULL
, 0, NULL
, NULL
, &pid
, NULL
, &babel_stdout
, NULL
, &error
)) {
387 g_warning("Error : %s", error
->message
);
393 diag
= fdopen(babel_stdout
, "r");
394 setvbuf(diag
, NULL
, _IONBF
, 0);
396 while (fgets(line
, sizeof(line
), diag
)) {
398 cb(BABEL_DIAG_OUTPUT
, line
, user_data
);
401 cb(BABEL_DONE
, NULL
, user_data
);
404 waitpid(pid
, NULL
, 0);
405 g_spawn_close_pid(pid
);
414 gboolean
a_babel_convert_to( VikTrwLayer
*vt
, const char *babelargs
, BabelStatusFunc cb
, const char *to
, gpointer user_data
)
419 gboolean ret
= FALSE
;
422 if ((fd_src
= g_file_open_tmp("tmp-viking.XXXXXX", &name_src
, NULL
)) >= 0) {
426 gpsbabel_loc
= g_find_program_in_path("gpsbabel");
429 gchar
*unbuffer_loc
= g_find_program_in_path("unbuffer");
430 gchar
**sub_args
= g_strsplit(babelargs
, " ", 0);
434 args
[i
++] = unbuffer_loc
;
435 args
[i
++] = gpsbabel_loc
;
438 for (j
= 0; sub_args
[j
]; j
++)
439 /* some version of gpsbabel can not take extra blank arg */
440 if (sub_args
[j
][0] != '\0')
441 args
[i
++] = sub_args
[j
];
443 args
[i
++] = name_src
;
448 ret
= babel_general_convert_to ( vt
, cb
, args
, name_src
, user_data
);
450 g_free ( unbuffer_loc
);
451 g_strfreev(sub_args
);
453 g_warning("gpsbabel not found in PATH");
454 g_free(gpsbabel_loc
);