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