]> git.street.me.uk Git - andy/viking.git/blob - src/babel.c
520c4dd3b4f180818a7f6915e0d8dfb9d831e9ff
[andy/viking.git] / src / babel.c
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
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
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include "viking.h"
33 #include "gpx.h"
34 #include "babel.h"
35 #include <sys/wait.h>
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39 #include <glib.h>
40 #include <glib/gstdio.h>
41
42 /* in the future we could have support for other shells (change command strings), or not use a shell at all */
43 #define BASH_LOCATION "/bin/bash"
44
45 gboolean a_babel_convert( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, gpointer user_data )
46 {
47   int fd_src;
48   FILE *f;
49   gchar *name_src;
50   gboolean ret = FALSE;
51   gchar *bargs = g_strconcat(babelargs, " -i gpx", NULL);
52
53   if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) < 0) {
54     ret = FALSE;
55   } else {
56     f = fdopen(fd_src, "w");
57     a_gpx_write_file(vt, f);
58     fclose(f);
59     ret = a_babel_convert_from ( vt, bargs, cb, name_src, user_data );
60   }
61
62   g_free(bargs);
63   remove(name_src);
64   g_free(name_src);
65   return ret;
66 }
67
68 /* Runs args[0] with the arguments and uses the GPX module
69  * to import the GPX data into layer vt. Assumes that upon
70  * running the command, the data will appear in the (usually
71  * temporary) file name_dst.
72  *
73  * cb: callback that is run upon new data from STDOUT (?)
74  *     (TODO: STDERR would be nice since we usually redirect STDOUT)
75  * user_data: passed along to cb
76  *
77  * returns TRUE on success
78  */
79 gboolean babel_general_convert_from( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_dst, gpointer user_data )
80 {
81   gboolean ret;
82   GPid pid;
83   gint babel_stdin, babel_stdout, babel_stderr;
84   FILE *f;
85
86
87   if (!g_spawn_async_with_pipes (NULL, args, NULL, 0, NULL, NULL, &pid, &babel_stdin, &babel_stdout, &babel_stderr, NULL)) {
88     //    if (!g_spawn_async_with_pipes (NULL, args, NULL, 0, NULL, NULL, NULL, &babel_stdin, &babel_stdout, NULL, NULL)) {
89     ret = FALSE;
90   } else {
91     gchar line[512];
92     FILE *diag;
93     diag = fdopen(babel_stdout, "r");
94     setvbuf(diag, NULL, _IONBF, 0);
95
96     while (fgets(line, sizeof(line), diag)) {
97       if ( cb )
98         cb(BABEL_DIAG_OUTPUT, line, user_data);
99     }
100     if ( cb )
101       cb(BABEL_DONE, NULL, user_data);
102     fclose(diag);
103     waitpid(pid, NULL, 0);
104     g_spawn_close_pid(pid);
105
106     f = fopen(name_dst, "r");
107     a_gpx_read_file ( vt, f );
108      fclose(f);
109     ret = TRUE;
110   }
111     
112   return ret;
113 }
114
115 gboolean a_babel_convert_from( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, const char *from, gpointer user_data )
116 {
117   int fd_dst;
118   gchar *name_dst;
119   gchar *cmd;
120   gboolean ret = FALSE;
121   gchar **args;  
122
123   if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) < 0) {
124     ret = FALSE;
125   } else {
126     gchar *gpsbabel_loc;
127     close(fd_dst);
128
129     gpsbabel_loc = g_find_program_in_path("gpsbabel");
130
131     if (gpsbabel_loc ) {
132       gchar *unbuffer_loc = g_find_program_in_path("unbuffer");
133       cmd = g_strdup_printf ( "%s%s%s %s -o gpx %s %s",
134                               unbuffer_loc ? unbuffer_loc : "",
135                               unbuffer_loc ? " " : "",
136                               gpsbabel_loc,
137                               babelargs,
138                               from,
139                               name_dst );
140
141       if ( unbuffer_loc )
142         g_free ( unbuffer_loc );
143
144       args = g_strsplit(cmd, " ", 0);
145       ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
146       g_strfreev(args);
147       g_free ( cmd );
148     }
149   }
150
151   remove(name_dst);
152   g_free(name_dst);
153   return ret;
154 }
155
156 /* Runs the input command in a shell (bash) and optionally uses GPSBabel to convert from input_file_type.
157  * If input_file_type is NULL, doesn't use GPSBabel. Input must be GPX (or Geocaching *.loc)
158  *
159  * Uses babel_general_convert_from to actually run the command. This function
160  * prepares the command and temporary file, and sets up the arguments for bash.
161  */
162 gboolean a_babel_convert_from_shellcommand ( VikTrwLayer *vt, const char *input_cmd, const char *input_file_type, BabelStatusFunc cb, gpointer user_data )
163 {
164   int fd_dst;
165   gchar *name_dst;
166   gboolean ret = FALSE;
167   gchar **args;  
168
169   if ((fd_dst = g_file_open_tmp("tmp-viking.XXXXXX", &name_dst, NULL)) < 0) {
170     ret = FALSE;
171   } else {
172     gchar *shell_command;
173     if ( input_file_type )
174       shell_command = g_strdup_printf("%s | gpsbabel -i %s -f - -o gpx -F %s", input_cmd, input_file_type, name_dst);
175     else
176       shell_command = g_strdup_printf("%s > %s", input_cmd, name_dst);
177
178     g_debug("%s: %s", __FUNCTION__, shell_command);
179     close(fd_dst);
180
181     args = g_malloc(sizeof(gchar *)*4);
182     args[0] = BASH_LOCATION;
183     args[1] = "-c";
184     args[2] = shell_command;
185     args[3] = NULL;
186
187     ret = babel_general_convert_from ( vt, cb, args, name_dst, user_data );
188     g_free ( args );
189     g_free ( shell_command );
190   }
191
192   remove(name_dst);
193   g_free(name_dst);
194   return ret;
195 }
196
197 gboolean babel_general_convert_to( VikTrwLayer *vt, BabelStatusFunc cb, gchar **args, const gchar *name_src, gpointer user_data )
198 {
199   gboolean ret;
200   GPid pid;
201   gint babel_stdin, babel_stdout, babel_stderr;
202
203   if (!a_file_export(vt, name_src, FILE_TYPE_GPX)) {
204     g_warning("%s(): error exporting to %s", __FUNCTION__, name_src);
205     return(FALSE);
206   }
207
208   if (!g_spawn_async_with_pipes (NULL, args, NULL, 0, NULL, NULL, &pid, &babel_stdin, &babel_stdout, &babel_stderr, NULL)) {
209     ret = FALSE;
210   } else {
211     gchar line[512];
212     FILE *diag;
213     diag = fdopen(babel_stdout, "r");
214     setvbuf(diag, NULL, _IONBF, 0);
215
216     while (fgets(line, sizeof(line), diag)) {
217       if ( cb )
218         cb(BABEL_DIAG_OUTPUT, line, user_data);
219     }
220     if ( cb )
221       cb(BABEL_DONE, NULL, user_data);
222     fclose(diag);
223     waitpid(pid, NULL, 0);
224     g_spawn_close_pid(pid);
225
226     ret = TRUE;
227   }
228     
229   return ret;
230 }
231
232 gboolean a_babel_convert_to( VikTrwLayer *vt, const char *babelargs, BabelStatusFunc cb, const char *to, gpointer user_data )
233 {
234   int fd_src;
235   gchar *name_src;
236   gchar *cmd;
237   gboolean ret = FALSE;
238   gchar **args;  
239
240   if ((fd_src = g_file_open_tmp("tmp-viking.XXXXXX", &name_src, NULL)) < 0) {
241     ret = FALSE;
242   } else {
243     gchar *gpsbabel_loc;
244     close(fd_src);
245
246     gpsbabel_loc = g_find_program_in_path("gpsbabel");
247
248     if (gpsbabel_loc ) {
249       gchar *unbuffer_loc = g_find_program_in_path("unbuffer");
250       cmd = g_strdup_printf ( "%s%s%s %s -i gpx %s %s",
251                               unbuffer_loc ? unbuffer_loc : "",
252                               unbuffer_loc ? " " : "",
253                               gpsbabel_loc,
254                               babelargs,
255                               name_src,
256                               to);
257
258       if ( unbuffer_loc )
259         g_free ( unbuffer_loc );
260       g_debug ( "%s: %s", __FUNCTION__, cmd );
261       args = g_strsplit(cmd, " ", 0);
262       ret = babel_general_convert_to ( vt, cb, args, name_src, user_data );
263       g_strfreev(args);
264       g_free ( cmd );
265     }
266   }
267
268   remove(name_src);
269   g_free(name_src);
270   return ret;
271 }