]> git.street.me.uk Git - andy/viking.git/blame - src/babel.c
Use a more generic error message
[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>
5 *
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.
10 *
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.
15 *
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
19 *
20 */
21
28c82d8b
EB
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.
26 */
27
45acf79e
MA
28#ifdef HAVE_CONFIG_H
29#include "config.h"
30#endif
31
1d1bc3c1
EB
32#include "viking.h"
33#include "gpx.h"
34#include "babel.h"
8c060406 35#include <stdio.h>
e81e41fb 36#ifdef HAVE_SYS_WAIT_H
29a5a545 37#include <sys/wait.h>
e81e41fb 38#endif
45acf79e
MA
39#ifdef HAVE_UNISTD_H
40#include <unistd.h>
41#endif
42#include <glib.h>
43#include <glib/gstdio.h>
1d1bc3c1 44
8cf048bd
EB
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"
47
7b3479e3 48gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, gpointer user_data )
1d1bc3c1
EB
49{
50 int fd_src;
51 FILE *f;
52 gchar *name_src;
53 gboolean ret = FALSE;
3adc68b0 54 gchar *bargs = g_strconcat(babelargs, " -i gpx", NULL);
1d1bc3c1 55
2ecf5998 56 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
1d1bc3c1
EB
57 f = fdopen(fd_src, "w");
58 a_gpx_write_file(vt, f);
59 fclose(f);
8c060406 60 f = NULL;
7b3479e3 61 ret = a_babel_convert_from ( vt, bargs, cb, name_src, user_data );
1d1bc3c1
EB
62 }
63
64 g_free(bargs);
8c060406 65 g_remove(name_src);
1d1bc3c1
EB
66 g_free(name_src);
67 return ret;
68}
69
28c82d8b
EB
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.
74 *
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
78 *
79 * returns TRUE on success
80 */
7b3479e3 81gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_dst, gpointer user_data )
8cf048bd 82{
2ecf5998 83 gboolean ret = FALSE;
8cf048bd 84 GPid pid;
6c641b1a
MA
85 GError *error = NULL;
86 gint babel_stdout;
8cf048bd
EB
87 FILE *f;
88
89
6c641b1a
MA
90 if (!g_spawn_async_with_pipes (NULL, args, NULL, 0, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) {
91 g_warning("Error : %s", error->message);
92 g_error_free(error);
8cf048bd
EB
93 ret = FALSE;
94 } else {
95 gchar line[512];
96 FILE *diag;
97 diag = fdopen(babel_stdout, "r");
98 setvbuf(diag, NULL, _IONBF, 0);
99
100 while (fgets(line, sizeof(line), diag)) {
7b3479e3
EB
101 if ( cb )
102 cb(BABEL_DIAG_OUTPUT, line, user_data);
8cf048bd 103 }
7b3479e3
EB
104 if ( cb )
105 cb(BABEL_DONE, NULL, user_data);
8cf048bd 106 fclose(diag);
8c060406 107 diag = NULL;
8cf048bd
EB
108 waitpid(pid, NULL, 0);
109 g_spawn_close_pid(pid);
110
8c060406
MA
111 f = g_fopen(name_dst, "r");
112 if (f) {
113 a_gpx_read_file ( vt, f );
114 fclose(f);
115 f = NULL;
116 ret = TRUE;
2ecf5998 117 }
8cf048bd
EB
118 }
119
120 return ret;
121}
122
7b3479e3 123gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, const char *from, gpointer user_data )
1d1bc3c1
EB
124{
125 int fd_dst;
1d1bc3c1 126 gchar *name_dst;
92255687 127 gchar *cmd;
1d1bc3c1
EB
128 gboolean ret = FALSE;
129 gchar **args;
c5b782e3 130 gint nb_args;
1d1bc3c1 131
2ecf5998 132 if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
92255687 133 gchar *gpsbabel_loc;
1d1bc3c1
EB
134 close(fd_dst);
135
92255687
EB
136 gpsbabel_loc = g_find_program_in_path("gpsbabel");
137
138 if (gpsbabel_loc ) {
139 gchar *unbuffer_loc = g_find_program_in_path("unbuffer");
140 cmd = g_strdup_printf ( "%s%s%s %s -o gpx %s %s",
5f304fd7 141 unbuffer_loc ? unbuffer_loc : "",
92255687
EB
142 unbuffer_loc ? " " : "",
143 gpsbabel_loc,
144 babelargs,
145 from,
146 name_dst );
147
148 if ( unbuffer_loc )
149 g_free ( unbuffer_loc );
150
c5b782e3
GB
151 if ( g_shell_parse_argv(cmd, &nb_args, &args, NULL) ) {
152 ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
153 g_strfreev(args);
154 }
92255687
EB
155 g_free ( cmd );
156 }
1d1bc3c1
EB
157 }
158
8c060406 159 g_remove(name_dst);
1d1bc3c1
EB
160 g_free(name_dst);
161 return ret;
162}
8cf048bd 163
28c82d8b
EB
164/* Runs the input command in a shell (bash) and optionally uses GPSBabel to convert from input_file_type.
165 * If input_file_type is NULL, doesn't use GPSBabel. Input must be GPX (or Geocaching *.loc)
166 *
167 * Uses babel_general_convert_from to actually run the command. This function
168 * prepares the command and temporary file, and sets up the arguments for bash.
169 */
170gboolean a_babel_convert_from_shellcommand ( VikTrwLayer *vt, const char *input_cmd, const char *input_file_type, BabelStatusFunc cb, gpointer user_data )
8cf048bd
EB
171{
172 int fd_dst;
173 gchar *name_dst;
174 gboolean ret = FALSE;
175 gchar **args;
176
2ecf5998 177 if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) >= 0) {
28c82d8b
EB
178 gchar *shell_command;
179 if ( input_file_type )
180 shell_command = g_strdup_printf("%s | gpsbabel -i %s -f - -o gpx -F %s", input_cmd, input_file_type, name_dst);
181 else
182 shell_command = g_strdup_printf("%s > %s", input_cmd, name_dst);
183
29a5a545 184 g_debug("%s: %s", __FUNCTION__, shell_command);
8cf048bd
EB
185 close(fd_dst);
186
187 args = g_malloc(sizeof(gchar *)*4);
188 args[0] = BASH_LOCATION;
189 args[1] = "-c";
190 args[2] = shell_command;
191 args[3] = NULL;
192
7b3479e3 193 ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
8cf048bd
EB
194 g_free ( args );
195 g_free ( shell_command );
196 }
197
8c060406 198 g_remove(name_dst);
8cf048bd
EB
199 g_free(name_dst);
200 return ret;
201}
202
533bbf34
MA
203gboolean a_babel_convert_from_url ( VikTrwLayer *vt, const char *url, const char *input_type, BabelStatusFunc cb, gpointer user_data )
204{
205 gint fd_src;
8bd9fe17
GB
206 int fetch_ret;
207 gboolean ret = FALSE;
533bbf34
MA
208 gchar *name_src;
209 gchar *babelargs;
210
211 g_debug("%s: input_type=%s url=%s", __FUNCTION__, input_type, url);
212
8bd9fe17 213 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
533bbf34
MA
214 close(fd_src);
215 g_remove(name_src);
216
217 babelargs = g_strdup_printf(" -i %s", input_type);
218
8bd9fe17
GB
219 fetch_ret = a_http_download_get_url(url, "", name_src, NULL);
220 if (fetch_ret == 0)
221 ret = a_babel_convert_from( vt, babelargs, NULL, name_src, NULL);
533bbf34
MA
222
223 g_remove(name_src);
224 g_free(babelargs);
225 g_free(name_src);
226 }
227
228 return ret;
229}
230
b364d6bc
QT
231gboolean babel_general_convert_to( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
232{
2ecf5998 233 gboolean ret = FALSE;
b364d6bc 234 GPid pid;
6c641b1a
MA
235 GError *error = NULL;
236 gint babel_stdout;
b364d6bc
QT
237
238 if (!a_file_export(vt, name_src, FILE_TYPE_GPX)) {
29a5a545 239 g_warning("%s(): error exporting to %s", __FUNCTION__, name_src);
b364d6bc
QT
240 return(FALSE);
241 }
242
6c641b1a
MA
243 if (!g_spawn_async_with_pipes (NULL, args, NULL, 0, NULL, NULL, &pid, NULL, &babel_stdout, NULL, &error)) {
244 g_warning("Error : %s", error->message);
245 g_error_free(error);
b364d6bc
QT
246 ret = FALSE;
247 } else {
248 gchar line[512];
249 FILE *diag;
250 diag = fdopen(babel_stdout, "r");
251 setvbuf(diag, NULL, _IONBF, 0);
252
253 while (fgets(line, sizeof(line), diag)) {
254 if ( cb )
255 cb(BABEL_DIAG_OUTPUT, line, user_data);
256 }
257 if ( cb )
258 cb(BABEL_DONE, NULL, user_data);
259 fclose(diag);
8c060406 260 diag = NULL;
b364d6bc
QT
261 waitpid(pid, NULL, 0);
262 g_spawn_close_pid(pid);
263
264 ret = TRUE;
265 }
266
267 return ret;
268}
269
270gboolean a_babel_convert_to( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, const char *to, gpointer user_data )
271{
272 int fd_src;
273 gchar *name_src;
274 gchar *cmd;
275 gboolean ret = FALSE;
276 gchar **args;
c5b782e3 277 gint nb_args;
b364d6bc 278
2ecf5998 279 if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) >= 0) {
b364d6bc
QT
280 gchar *gpsbabel_loc;
281 close(fd_src);
282
283 gpsbabel_loc = g_find_program_in_path("gpsbabel");
284
285 if (gpsbabel_loc ) {
286 gchar *unbuffer_loc = g_find_program_in_path("unbuffer");
287 cmd = g_strdup_printf ( "%s%s%s %s -i gpx %s %s",
288 unbuffer_loc ? unbuffer_loc : "",
289 unbuffer_loc ? " " : "",
290 gpsbabel_loc,
291 babelargs,
292 name_src,
293 to);
294
295 if ( unbuffer_loc )
296 g_free ( unbuffer_loc );
29a5a545 297 g_debug ( "%s: %s", __FUNCTION__, cmd );
c5b782e3
GB
298 if ( g_shell_parse_argv(cmd, &nb_args, &args, NULL) ) {
299 ret = babel_general_convert_to ( vt, cb, args, name_src, user_data );
300 g_strfreev(args);
301 }
b364d6bc
QT
302 g_free ( cmd );
303 }
304 }
305
8c060406 306 g_remove(name_src);
b364d6bc
QT
307 g_free(name_src);
308 return ret;
309}