Changes to handle vfs_path_t object:
[midnight-commander.git] / src / viewer / datasource.c
bloba43b59dbcb38cab3e8803b2c46e2879796702d38
1 /*
2 Internal file viewer for the Midnight Commander
3 Functions for datasources
5 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
6 2004, 2005, 2006, 2007, 2009, 2011
7 The Free Software Foundation, Inc.
9 Written by:
10 Miguel de Icaza, 1994, 1995, 1998
11 Janne Kukonlehto, 1994, 1995
12 Jakub Jelinek, 1995
13 Joseph M. Hinkle, 1996
14 Norbert Warmuth, 1997
15 Pavel Machek, 1998
16 Roland Illig <roland.illig@gmx.de>, 2004, 2005
17 Slava Zanko <slavazanko@google.com>, 2009
18 Andrew Borodin <aborodin@vmail.ru>, 2009
19 Ilia Maslakov <il.smind@gmail.com>, 2009
21 This file is part of the Midnight Commander.
23 The Midnight Commander is free software: you can redistribute it
24 and/or modify it under the terms of the GNU General Public License as
25 published by the Free Software Foundation, either version 3 of the License,
26 or (at your option) any later version.
28 The Midnight Commander is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
33 You should have received a copy of the GNU General Public License
34 along with this program. If not, see <http://www.gnu.org/licenses/>.
38 The data source provides the viewer with data from either a file, a
39 string or the output of a command. The mcview_get_byte() function can be
40 used to get the value of a byte at a specific offset. If the offset
41 is out of range, -1 is returned. The function mcview_get_byte_indexed(a,b)
42 returns the byte at the offset a+b, or -1 if a+b is out of range.
44 The mcview_set_byte() function has the effect that later calls to
45 mcview_get_byte() will return the specified byte for this offset. This
46 function is designed only for use by the hexedit component after
47 saving its changes. Inspect the source before you want to use it for
48 other purposes.
50 The mcview_get_filesize() function returns the current size of the
51 data source. If the growing buffer is used, this size may increase
52 later on. Use the mcview_may_still_grow() function when you want to
53 know if the size can change later.
56 #include <config.h>
58 #include "lib/global.h"
59 #include "lib/vfs/vfs.h"
60 #include "lib/util.h"
61 #include "lib/widget.h" /* D_NORMAL, D_ERROR */
63 #include "internal.h"
65 /*** global variables ****************************************************************************/
67 /*** file scope macro definitions ****************************************************************/
69 /*** file scope type declarations ****************************************************************/
71 /*** file scope variables ************************************************************************/
73 /*** file scope functions ************************************************************************/
74 /* --------------------------------------------------------------------------------------------- */
76 /* --------------------------------------------------------------------------------------------- */
77 /*** public functions ****************************************************************************/
78 /* --------------------------------------------------------------------------------------------- */
80 static void
81 mcview_set_datasource_stdio_pipe (mcview_t * view, FILE * fp)
83 assert (fp != NULL);
84 view->datasource = DS_STDIO_PIPE;
85 view->ds_stdio_pipe = fp;
87 mcview_growbuf_init (view);
90 /* --------------------------------------------------------------------------------------------- */
92 void
93 mcview_set_datasource_none (mcview_t * view)
95 view->datasource = DS_NONE;
98 /* --------------------------------------------------------------------------------------------- */
100 off_t
101 mcview_get_filesize (mcview_t * view)
103 switch (view->datasource)
105 case DS_NONE:
106 return 0;
107 case DS_STDIO_PIPE:
108 case DS_VFS_PIPE:
109 return mcview_growbuf_filesize (view);
110 case DS_FILE:
111 return view->ds_file_filesize;
112 case DS_STRING:
113 return view->ds_string_len;
114 default:
115 assert (!"Unknown datasource type");
116 return 0;
120 /* --------------------------------------------------------------------------------------------- */
122 void
123 mcview_update_filesize (mcview_t * view)
125 if (view->datasource == DS_FILE)
127 struct stat st;
128 if (mc_fstat (view->ds_file_fd, &st) != -1)
129 view->ds_file_filesize = st.st_size;
133 /* --------------------------------------------------------------------------------------------- */
135 char *
136 mcview_get_ptr_file (mcview_t * view, off_t byte_index)
138 assert (view->datasource == DS_FILE);
140 mcview_file_load_data (view, byte_index);
141 if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen))
142 return (char *) (view->ds_file_data + (byte_index - view->ds_file_offset));
143 return NULL;
146 /* --------------------------------------------------------------------------------------------- */
148 char *
149 mcview_get_ptr_string (mcview_t * view, off_t byte_index)
151 assert (view->datasource == DS_STRING);
152 if (byte_index < (off_t) view->ds_string_len)
153 return (char *) (view->ds_string_data + byte_index);
154 return NULL;
157 /* --------------------------------------------------------------------------------------------- */
160 mcview_get_utf (mcview_t * view, off_t byte_index, int *char_width, gboolean * result)
162 gchar *str = NULL;
163 int res = -1;
164 gunichar ch;
165 gchar *next_ch = NULL;
166 gchar utf8buf[UTF8_CHAR_LEN + 1];
168 *char_width = 0;
169 *result = FALSE;
171 switch (view->datasource)
173 case DS_STDIO_PIPE:
174 case DS_VFS_PIPE:
175 str = mcview_get_ptr_growing_buffer (view, byte_index);
176 break;
177 case DS_FILE:
178 str = mcview_get_ptr_file (view, byte_index);
179 break;
180 case DS_STRING:
181 str = mcview_get_ptr_string (view, byte_index);
182 break;
183 case DS_NONE:
184 break;
187 if (str == NULL)
188 return 0;
190 res = g_utf8_get_char_validated (str, -1);
192 if (res < 0)
194 /* Retry with explicit bytes to make sure it's not a buffer boundary */
195 int i;
196 for (i = 0; i < UTF8_CHAR_LEN; i++)
198 if (mcview_get_byte (view, byte_index + i, &res))
199 utf8buf[i] = res;
200 else
202 utf8buf[i] = '\0';
203 break;
206 utf8buf[UTF8_CHAR_LEN] = '\0';
207 str = utf8buf;
208 res = g_utf8_get_char_validated (str, -1);
211 if (res < 0)
213 ch = *str;
214 *char_width = 1;
216 else
218 ch = res;
219 /* Calculate UTF-8 char width */
220 next_ch = g_utf8_next_char (str);
221 if (next_ch)
222 *char_width = next_ch - str;
223 else
224 return 0;
226 *result = TRUE;
227 return ch;
230 /* --------------------------------------------------------------------------------------------- */
232 gboolean
233 mcview_get_byte_string (mcview_t * view, off_t byte_index, int *retval)
235 assert (view->datasource == DS_STRING);
236 if (byte_index < (off_t) view->ds_string_len)
238 if (retval)
239 *retval = view->ds_string_data[byte_index];
240 return TRUE;
242 if (retval)
243 *retval = -1;
244 return FALSE;
247 /* --------------------------------------------------------------------------------------------- */
249 gboolean
250 mcview_get_byte_none (mcview_t * view, off_t byte_index, int *retval)
252 assert (view->datasource == DS_NONE);
253 (void) &view;
254 (void) byte_index;
255 if (retval)
256 *retval = -1;
257 return FALSE;
260 /* --------------------------------------------------------------------------------------------- */
262 void
263 mcview_set_byte (mcview_t * view, off_t offset, byte b)
265 (void) &b;
266 assert (offset < mcview_get_filesize (view));
267 assert (view->datasource == DS_FILE);
268 view->ds_file_datalen = 0; /* just force reloading */
271 /* --------------------------------------------------------------------------------------------- */
273 /*static */
274 void
275 mcview_file_load_data (mcview_t * view, off_t byte_index)
277 off_t blockoffset;
278 ssize_t res;
279 size_t bytes_read;
281 assert (view->datasource == DS_FILE);
283 if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen))
284 return;
286 if (byte_index >= view->ds_file_filesize)
287 return;
289 blockoffset = mcview_offset_rounddown (byte_index, view->ds_file_datasize);
290 if (mc_lseek (view->ds_file_fd, blockoffset, SEEK_SET) == -1)
291 goto error;
293 bytes_read = 0;
294 while (bytes_read < view->ds_file_datasize)
296 res =
297 mc_read (view->ds_file_fd, view->ds_file_data + bytes_read,
298 view->ds_file_datasize - bytes_read);
299 if (res == -1)
300 goto error;
301 if (res == 0)
302 break;
303 bytes_read += (size_t) res;
305 view->ds_file_offset = blockoffset;
306 if ((off_t) bytes_read > view->ds_file_filesize - view->ds_file_offset)
308 /* the file has grown in the meantime -- stick to the old size */
309 view->ds_file_datalen = view->ds_file_filesize - view->ds_file_offset;
311 else
313 view->ds_file_datalen = bytes_read;
315 return;
317 error:
318 view->ds_file_datalen = 0;
321 /* --------------------------------------------------------------------------------------------- */
323 void
324 mcview_close_datasource (mcview_t * view)
326 switch (view->datasource)
328 case DS_NONE:
329 break;
330 case DS_STDIO_PIPE:
331 if (view->ds_stdio_pipe != NULL)
333 (void) pclose (view->ds_stdio_pipe);
334 mcview_display (view);
335 close_error_pipe (D_NORMAL, NULL);
336 view->ds_stdio_pipe = NULL;
338 mcview_growbuf_free (view);
339 break;
340 case DS_VFS_PIPE:
341 if (view->ds_vfs_pipe != -1)
343 (void) mc_close (view->ds_vfs_pipe);
344 view->ds_vfs_pipe = -1;
346 mcview_growbuf_free (view);
347 break;
348 case DS_FILE:
349 (void) mc_close (view->ds_file_fd);
350 view->ds_file_fd = -1;
351 g_free (view->ds_file_data);
352 view->ds_file_data = NULL;
353 break;
354 case DS_STRING:
355 g_free (view->ds_string_data);
356 view->ds_string_data = NULL;
357 break;
358 default:
359 assert (!"Unknown datasource type");
361 view->datasource = DS_NONE;
364 /* --------------------------------------------------------------------------------------------- */
366 void
367 mcview_set_datasource_file (mcview_t * view, int fd, const struct stat *st)
369 view->datasource = DS_FILE;
370 view->ds_file_fd = fd;
371 view->ds_file_filesize = st->st_size;
372 view->ds_file_offset = 0;
373 view->ds_file_data = g_malloc (4096);
374 view->ds_file_datalen = 0;
375 view->ds_file_datasize = 4096;
378 /* --------------------------------------------------------------------------------------------- */
380 gboolean
381 mcview_load_command_output (mcview_t * view, const char *command)
383 FILE *fp;
385 mcview_close_datasource (view);
387 open_error_pipe ();
388 fp = popen (command, "r");
389 if (fp == NULL)
391 /* Avoid two messages. Message from stderr has priority. */
392 mcview_display (view);
393 if (!close_error_pipe (mcview_is_in_panel (view) ? -1 : D_ERROR, NULL))
394 mcview_show_error (view, _("Cannot spawn child process"));
395 return FALSE;
398 /* First, check if filter produced any output */
399 mcview_set_datasource_stdio_pipe (view, fp);
400 if (!mcview_get_byte (view, 0, NULL))
402 mcview_close_datasource (view);
404 /* Avoid two messages. Message from stderr has priority. */
405 mcview_display (view);
406 if (!close_error_pipe (mcview_is_in_panel (view) ? -1 : D_ERROR, NULL))
407 mcview_show_error (view, _("Empty output from child filter"));
408 return FALSE;
410 else
413 * At least something was read correctly. Close stderr and let
414 * program die if it will try to write something there.
416 * Ideally stderr should be read asynchronously to prevent programs
417 * from blocking (poll/select multiplexor).
419 close_error_pipe (D_NORMAL, NULL);
421 return TRUE;
424 /* --------------------------------------------------------------------------------------------- */
426 void
427 mcview_set_datasource_vfs_pipe (mcview_t * view, int fd)
429 assert (fd != -1);
430 view->datasource = DS_VFS_PIPE;
431 view->ds_vfs_pipe = fd;
433 mcview_growbuf_init (view);
436 /* --------------------------------------------------------------------------------------------- */
438 void
439 mcview_set_datasource_string (mcview_t * view, const char *s)
441 view->datasource = DS_STRING;
442 view->ds_string_data = (byte *) g_strdup (s);
443 view->ds_string_len = strlen (s);
446 /* --------------------------------------------------------------------------------------------- */