extfs: tester: fix indentation.
[midnight-commander.git] / src / viewer / datasource.c
blobc90caf32e482214b8b7ceef24f7c2fb5eb9d61da
1 /*
2 Internal file viewer for the Midnight Commander
3 Functions for datasources
5 Copyright (C) 1994-2017
6 Free Software Foundation, Inc.
8 Written by:
9 Miguel de Icaza, 1994, 1995, 1998
10 Janne Kukonlehto, 1994, 1995
11 Jakub Jelinek, 1995
12 Joseph M. Hinkle, 1996
13 Norbert Warmuth, 1997
14 Pavel Machek, 1998
15 Roland Illig <roland.illig@gmx.de>, 2004, 2005
16 Slava Zanko <slavazanko@google.com>, 2009
17 Andrew Borodin <aborodin@vmail.ru>, 2009
18 Ilia Maslakov <il.smind@gmail.com>, 2009
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 3 of the License,
25 or (at your option) any later version.
27 The Midnight Commander is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU 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, see <http://www.gnu.org/licenses/>.
37 The data source provides the viewer with data from either a file, a
38 string or the output of a command. The mcview_get_byte() function can be
39 used to get the value of a byte at a specific offset. If the offset
40 is out of range, -1 is returned. The function mcview_get_byte_indexed(a,b)
41 returns the byte at the offset a+b, or -1 if a+b is out of range.
43 The mcview_set_byte() function has the effect that later calls to
44 mcview_get_byte() will return the specified byte for this offset. This
45 function is designed only for use by the hexedit component after
46 saving its changes. Inspect the source before you want to use it for
47 other purposes.
49 The mcview_get_filesize() function returns the current size of the
50 data source. If the growing buffer is used, this size may increase
51 later on. Use the mcview_may_still_grow() function when you want to
52 know if the size can change later.
55 #include <config.h>
57 #include "lib/global.h"
58 #include "lib/vfs/vfs.h"
59 #include "lib/util.h"
60 #include "lib/widget.h" /* D_NORMAL, D_ERROR */
62 #include "internal.h"
64 /*** global variables ****************************************************************************/
66 /*** file scope macro definitions ****************************************************************/
68 /*** file scope type declarations ****************************************************************/
70 /*** file scope variables ************************************************************************/
72 /* --------------------------------------------------------------------------------------------- */
73 /*** file scope functions ************************************************************************/
74 /* --------------------------------------------------------------------------------------------- */
76 static void
77 mcview_set_datasource_stdio_pipe (WView * view, mc_pipe_t * p)
79 p->out.len = MC_PIPE_BUFSIZE;
80 p->out.null_term = FALSE;
81 p->err.len = MC_PIPE_BUFSIZE;
82 p->err.null_term = TRUE;
83 view->datasource = DS_STDIO_PIPE;
84 view->ds_stdio_pipe = p;
85 view->pipe_first_err_msg = TRUE;
87 mcview_growbuf_init (view);
90 /* --------------------------------------------------------------------------------------------- */
91 /*** public functions ****************************************************************************/
92 /* --------------------------------------------------------------------------------------------- */
94 void
95 mcview_set_datasource_none (WView * view)
97 view->datasource = DS_NONE;
100 /* --------------------------------------------------------------------------------------------- */
102 off_t
103 mcview_get_filesize (WView * view)
105 switch (view->datasource)
107 case DS_NONE:
108 return 0;
109 case DS_STDIO_PIPE:
110 case DS_VFS_PIPE:
111 return mcview_growbuf_filesize (view);
112 case DS_FILE:
113 return view->ds_file_filesize;
114 case DS_STRING:
115 return view->ds_string_len;
116 default:
117 g_assert (!"Unknown datasource type");
118 return 0;
122 /* --------------------------------------------------------------------------------------------- */
124 void
125 mcview_update_filesize (WView * view)
127 if (view->datasource == DS_FILE)
129 struct stat st;
130 if (mc_fstat (view->ds_file_fd, &st) != -1)
131 view->ds_file_filesize = st.st_size;
135 /* --------------------------------------------------------------------------------------------- */
137 char *
138 mcview_get_ptr_file (WView * view, off_t byte_index)
140 g_assert (view->datasource == DS_FILE);
142 mcview_file_load_data (view, byte_index);
143 if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen))
144 return (char *) (view->ds_file_data + (byte_index - view->ds_file_offset));
145 return NULL;
148 /* --------------------------------------------------------------------------------------------- */
150 gboolean
151 mcview_get_utf (WView * view, off_t byte_index, int *ch, int *ch_len)
153 gchar *str = NULL;
154 int res;
155 gchar utf8buf[UTF8_CHAR_LEN + 1];
157 switch (view->datasource)
159 case DS_STDIO_PIPE:
160 case DS_VFS_PIPE:
161 str = mcview_get_ptr_growing_buffer (view, byte_index);
162 break;
163 case DS_FILE:
164 str = mcview_get_ptr_file (view, byte_index);
165 break;
166 case DS_STRING:
167 str = mcview_get_ptr_string (view, byte_index);
168 break;
169 case DS_NONE:
170 default:
171 break;
174 *ch = 0;
176 if (str == NULL)
177 return FALSE;
179 res = g_utf8_get_char_validated (str, -1);
181 if (res < 0)
183 /* Retry with explicit bytes to make sure it's not a buffer boundary */
184 int i;
186 for (i = 0; i < UTF8_CHAR_LEN; i++)
188 if (mcview_get_byte (view, byte_index + i, &res))
189 utf8buf[i] = res;
190 else
192 utf8buf[i] = '\0';
193 break;
196 utf8buf[UTF8_CHAR_LEN] = '\0';
197 str = utf8buf;
198 res = g_utf8_get_char_validated (str, -1);
201 if (res < 0)
203 *ch = (unsigned char) (*str);
204 *ch_len = 1;
206 else
208 gchar *next_ch = NULL;
210 *ch = res;
211 /* Calculate UTF-8 char length */
212 next_ch = g_utf8_next_char (str);
213 *ch_len = next_ch - str;
216 return TRUE;
219 /* --------------------------------------------------------------------------------------------- */
221 char *
222 mcview_get_ptr_string (WView * view, off_t byte_index)
224 g_assert (view->datasource == DS_STRING);
226 if (byte_index >= 0 && byte_index < (off_t) view->ds_string_len)
227 return (char *) (view->ds_string_data + byte_index);
228 return NULL;
231 /* --------------------------------------------------------------------------------------------- */
233 gboolean
234 mcview_get_byte_string (WView * view, off_t byte_index, int *retval)
236 char *p;
238 if (retval != NULL)
239 *retval = -1;
241 p = mcview_get_ptr_string (view, byte_index);
242 if (p == NULL)
243 return FALSE;
245 if (retval != NULL)
246 *retval = (unsigned char) (*p);
247 return TRUE;
250 /* --------------------------------------------------------------------------------------------- */
252 gboolean
253 mcview_get_byte_none (WView * view, off_t byte_index, int *retval)
255 (void) &view;
256 (void) byte_index;
258 g_assert (view->datasource == DS_NONE);
260 if (retval != NULL)
261 *retval = -1;
262 return FALSE;
265 /* --------------------------------------------------------------------------------------------- */
267 void
268 mcview_set_byte (WView * view, off_t offset, byte b)
270 (void) &b;
272 g_assert (offset < mcview_get_filesize (view));
273 g_assert (view->datasource == DS_FILE);
275 view->ds_file_datalen = 0; /* just force reloading */
278 /* --------------------------------------------------------------------------------------------- */
280 /*static */
281 void
282 mcview_file_load_data (WView * view, off_t byte_index)
284 off_t blockoffset;
285 ssize_t res;
286 size_t bytes_read;
288 g_assert (view->datasource == DS_FILE);
290 if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen))
291 return;
293 if (byte_index >= view->ds_file_filesize)
294 return;
296 blockoffset = mcview_offset_rounddown (byte_index, view->ds_file_datasize);
297 if (mc_lseek (view->ds_file_fd, blockoffset, SEEK_SET) == -1)
298 goto error;
300 bytes_read = 0;
301 while (bytes_read < view->ds_file_datasize)
303 res =
304 mc_read (view->ds_file_fd, view->ds_file_data + bytes_read,
305 view->ds_file_datasize - bytes_read);
306 if (res == -1)
307 goto error;
308 if (res == 0)
309 break;
310 bytes_read += (size_t) res;
312 view->ds_file_offset = blockoffset;
313 if ((off_t) bytes_read > view->ds_file_filesize - view->ds_file_offset)
315 /* the file has grown in the meantime -- stick to the old size */
316 view->ds_file_datalen = view->ds_file_filesize - view->ds_file_offset;
318 else
320 view->ds_file_datalen = bytes_read;
322 return;
324 error:
325 view->ds_file_datalen = 0;
328 /* --------------------------------------------------------------------------------------------- */
330 void
331 mcview_close_datasource (WView * view)
333 switch (view->datasource)
335 case DS_NONE:
336 break;
337 case DS_STDIO_PIPE:
338 if (view->ds_stdio_pipe != NULL)
340 mcview_growbuf_done (view);
341 mcview_display (view);
343 mcview_growbuf_free (view);
344 break;
345 case DS_VFS_PIPE:
346 if (view->ds_vfs_pipe != -1)
347 mcview_growbuf_done (view);
348 mcview_growbuf_free (view);
349 break;
350 case DS_FILE:
351 (void) mc_close (view->ds_file_fd);
352 view->ds_file_fd = -1;
353 MC_PTR_FREE (view->ds_file_data);
354 break;
355 case DS_STRING:
356 MC_PTR_FREE (view->ds_string_data);
357 break;
358 default:
359 g_assert (!"Unknown datasource type");
361 view->datasource = DS_NONE;
364 /* --------------------------------------------------------------------------------------------- */
366 void
367 mcview_set_datasource_file (WView * 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 (WView * view, const char *command)
383 mc_pipe_t *p;
384 GError *error = NULL;
386 mcview_close_datasource (view);
388 p = mc_popen (command, &error);
389 if (p == NULL)
391 mcview_display (view);
392 mcview_show_error (view, error->message);
393 g_error_free (error);
394 return FALSE;
397 /* Check if filter produced any output */
398 mcview_set_datasource_stdio_pipe (view, p);
399 if (!mcview_get_byte (view, 0, NULL))
401 mcview_close_datasource (view);
402 mcview_display (view);
403 return FALSE;
406 return TRUE;
409 /* --------------------------------------------------------------------------------------------- */
411 void
412 mcview_set_datasource_vfs_pipe (WView * view, int fd)
414 g_assert (fd != -1);
416 view->datasource = DS_VFS_PIPE;
417 view->ds_vfs_pipe = fd;
419 mcview_growbuf_init (view);
422 /* --------------------------------------------------------------------------------------------- */
424 void
425 mcview_set_datasource_string (WView * view, const char *s)
427 view->datasource = DS_STRING;
428 view->ds_string_len = strlen (s);
429 view->ds_string_data = (byte *) g_strndup (s, view->ds_string_len);
432 /* --------------------------------------------------------------------------------------------- */