Some WIDGET-related stuff moved to lib/widget directory
[midnight-commander.git] / src / viewer / datasource.c
blob2d415d2d7195ad2f0e3d62e1fd5082242034ea1f
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 Free Software Foundation, Inc.
8 Written by: 1994, 1995, 1998 Miguel de Icaza
9 1994, 1995 Janne Kukonlehto
10 1995 Jakub Jelinek
11 1996 Joseph M. Hinkle
12 1997 Norbert Warmuth
13 1998 Pavel Machek
14 2004 Roland Illig <roland.illig@gmx.de>
15 2005 Roland Illig <roland.illig@gmx.de>
16 2009 Slava Zanko <slavazanko@google.com>
17 2009 Andrew Borodin <aborodin@vmail.ru>
18 2009 Ilia Maslakov <il.smind@gmail.com>
20 This file is part of the Midnight Commander.
22 The Midnight Commander is free software; you can redistribute it
23 and/or modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2 of the
25 License, or (at your option) any later version.
27 The Midnight Commander is distributed in the hope that it will be
28 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
29 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 General Public License for more details.
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
35 MA 02110-1301, USA.
39 The data source provides the viewer with data from either a file, a
40 string or the output of a command. The mcview_get_byte() function can be
41 used to get the value of a byte at a specific offset. If the offset
42 is out of range, -1 is returned. The function mcview_get_byte_indexed(a,b)
43 returns the byte at the offset a+b, or -1 if a+b is out of range.
45 The mcview_set_byte() function has the effect that later calls to
46 mcview_get_byte() will return the specified byte for this offset. This
47 function is designed only for use by the hexedit component after
48 saving its changes. Inspect the source before you want to use it for
49 other purposes.
51 The mcview_get_filesize() function returns the current size of the
52 data source. If the growing buffer is used, this size may increase
53 later on. Use the mcview_may_still_grow() function when you want to
54 know if the size can change later.
57 #include <config.h>
59 #include "lib/global.h"
60 #include "lib/widget/wtools.h"
61 #include "lib/vfs/mc-vfs/vfs.h"
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 int width = 0;
168 *result = TRUE;
170 switch (view->datasource)
172 case DS_STDIO_PIPE:
173 case DS_VFS_PIPE:
174 str = mcview_get_ptr_growing_buffer (view, byte_index);
175 break;
176 case DS_FILE:
177 str = mcview_get_ptr_file (view, byte_index);
178 break;
179 case DS_STRING:
180 str = mcview_get_ptr_string (view, byte_index);
181 break;
182 case DS_NONE:
183 break;
186 if (str == NULL)
188 *result = FALSE;
189 width = 0;
190 return 0;
193 res = g_utf8_get_char_validated (str, -1);
195 if (res < 0)
197 ch = *str;
198 width = 0;
200 else
202 ch = res;
203 /* Calculate UTF-8 char width */
204 next_ch = g_utf8_next_char (str);
205 if (next_ch)
207 width = next_ch - str;
209 else
211 ch = 0;
212 width = 0;
215 *char_width = width;
216 return ch;
219 /* --------------------------------------------------------------------------------------------- */
221 gboolean
222 mcview_get_byte_string (mcview_t * view, off_t byte_index, int *retval)
224 assert (view->datasource == DS_STRING);
225 if (byte_index < (off_t) view->ds_string_len)
227 if (retval)
228 *retval = view->ds_string_data[byte_index];
229 return TRUE;
231 if (retval)
232 *retval = -1;
233 return FALSE;
236 /* --------------------------------------------------------------------------------------------- */
238 gboolean
239 mcview_get_byte_none (mcview_t * view, off_t byte_index, int *retval)
241 assert (view->datasource == DS_NONE);
242 (void) &view;
243 (void) byte_index;
244 if (retval)
245 *retval = -1;
246 return FALSE;
249 /* --------------------------------------------------------------------------------------------- */
251 void
252 mcview_set_byte (mcview_t * view, off_t offset, byte b)
254 (void) &b;
255 assert (offset < mcview_get_filesize (view));
256 assert (view->datasource == DS_FILE);
257 view->ds_file_datalen = 0; /* just force reloading */
260 /* --------------------------------------------------------------------------------------------- */
262 /*static */
263 void
264 mcview_file_load_data (mcview_t * view, off_t byte_index)
266 off_t blockoffset;
267 ssize_t res;
268 size_t bytes_read;
270 assert (view->datasource == DS_FILE);
272 if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen))
273 return;
275 if (byte_index >= view->ds_file_filesize)
276 return;
278 blockoffset = mcview_offset_rounddown (byte_index, view->ds_file_datasize);
279 if (mc_lseek (view->ds_file_fd, blockoffset, SEEK_SET) == -1)
280 goto error;
282 bytes_read = 0;
283 while (bytes_read < view->ds_file_datasize)
285 res =
286 mc_read (view->ds_file_fd, view->ds_file_data + bytes_read,
287 view->ds_file_datasize - bytes_read);
288 if (res == -1)
289 goto error;
290 if (res == 0)
291 break;
292 bytes_read += (size_t) res;
294 view->ds_file_offset = blockoffset;
295 if ((off_t) bytes_read > view->ds_file_filesize - view->ds_file_offset)
297 /* the file has grown in the meantime -- stick to the old size */
298 view->ds_file_datalen = view->ds_file_filesize - view->ds_file_offset;
300 else
302 view->ds_file_datalen = bytes_read;
304 return;
306 error:
307 view->ds_file_datalen = 0;
310 /* --------------------------------------------------------------------------------------------- */
312 void
313 mcview_close_datasource (mcview_t * view)
315 switch (view->datasource)
317 case DS_NONE:
318 break;
319 case DS_STDIO_PIPE:
320 if (view->ds_stdio_pipe != NULL)
322 (void) pclose (view->ds_stdio_pipe);
323 mcview_display (view);
324 close_error_pipe (D_NORMAL, NULL);
325 view->ds_stdio_pipe = NULL;
327 mcview_growbuf_free (view);
328 break;
329 case DS_VFS_PIPE:
330 if (view->ds_vfs_pipe != -1)
332 (void) mc_close (view->ds_vfs_pipe);
333 view->ds_vfs_pipe = -1;
335 mcview_growbuf_free (view);
336 break;
337 case DS_FILE:
338 (void) mc_close (view->ds_file_fd);
339 view->ds_file_fd = -1;
340 g_free (view->ds_file_data);
341 view->ds_file_data = NULL;
342 break;
343 case DS_STRING:
344 g_free (view->ds_string_data);
345 view->ds_string_data = NULL;
346 break;
347 default:
348 assert (!"Unknown datasource type");
350 view->datasource = DS_NONE;
353 /* --------------------------------------------------------------------------------------------- */
355 void
356 mcview_set_datasource_file (mcview_t * view, int fd, const struct stat *st)
358 view->datasource = DS_FILE;
359 view->ds_file_fd = fd;
360 view->ds_file_filesize = st->st_size;
361 view->ds_file_offset = 0;
362 view->ds_file_data = g_malloc (4096);
363 view->ds_file_datalen = 0;
364 view->ds_file_datasize = 4096;
367 /* --------------------------------------------------------------------------------------------- */
369 gboolean
370 mcview_load_command_output (mcview_t * view, const char *command)
372 FILE *fp;
374 mcview_close_datasource (view);
376 open_error_pipe ();
377 fp = popen (command, "r");
378 if (fp == NULL)
380 /* Avoid two messages. Message from stderr has priority. */
381 mcview_display (view);
382 if (!close_error_pipe (mcview_is_in_panel (view) ? -1 : D_ERROR, NULL))
383 mcview_show_error (view, _("Cannot spawn child process"));
384 return FALSE;
387 /* First, check if filter produced any output */
388 mcview_set_datasource_stdio_pipe (view, fp);
389 if (!mcview_get_byte (view, 0, NULL))
391 mcview_close_datasource (view);
393 /* Avoid two messages. Message from stderr has priority. */
394 mcview_display (view);
395 if (!close_error_pipe (mcview_is_in_panel (view) ? -1 : D_ERROR, NULL))
396 mcview_show_error (view, _("Empty output from child filter"));
397 return FALSE;
399 else
402 * At least something was read correctly. Close stderr and let
403 * program die if it will try to write something there.
405 * Ideally stderr should be read asynchronously to prevent programs
406 * from blocking (poll/select multiplexor).
408 close_error_pipe (D_NORMAL, NULL);
410 return TRUE;
413 /* --------------------------------------------------------------------------------------------- */
415 void
416 mcview_set_datasource_vfs_pipe (mcview_t * view, int fd)
418 assert (fd != -1);
419 view->datasource = DS_VFS_PIPE;
420 view->ds_vfs_pipe = fd;
422 mcview_growbuf_init (view);
425 /* --------------------------------------------------------------------------------------------- */
427 void
428 mcview_set_datasource_string (mcview_t * view, const char *s)
430 view->datasource = DS_STRING;
431 view->ds_string_data = (byte *) g_strdup (s);
432 view->ds_string_len = strlen (s);
435 /* --------------------------------------------------------------------------------------------- */