2 Internal file viewer for the Midnight Commander
3 Functions for datasources
5 Copyright (C) 1994-2025
6 Free Software Foundation, Inc.
9 Miguel de Icaza, 1994, 1995, 1998
10 Janne Kukonlehto, 1994, 1995
12 Joseph M. Hinkle, 1996
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 <https://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
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.
57 #include "lib/global.h"
58 #include "lib/vfs/vfs.h"
60 #include "lib/widget.h" /* D_NORMAL, D_ERROR */
64 /*** global variables ****************************************************************************/
66 /*** file scope macro definitions ****************************************************************/
68 /*** file scope type declarations ****************************************************************/
70 /*** forward declarations (file scope functions) *************************************************/
72 /*** file scope variables ************************************************************************/
74 /* --------------------------------------------------------------------------------------------- */
75 /*** file scope functions ************************************************************************/
76 /* --------------------------------------------------------------------------------------------- */
79 mcview_set_datasource_stdio_pipe (WView
*view
, mc_pipe_t
*p
)
81 p
->out
.len
= MC_PIPE_BUFSIZE
;
82 p
->out
.null_term
= FALSE
;
83 p
->err
.len
= MC_PIPE_BUFSIZE
;
84 p
->err
.null_term
= TRUE
;
85 view
->datasource
= DS_STDIO_PIPE
;
86 view
->ds_stdio_pipe
= p
;
87 view
->pipe_first_err_msg
= TRUE
;
89 mcview_growbuf_init (view
);
92 /* --------------------------------------------------------------------------------------------- */
93 /*** public functions ****************************************************************************/
94 /* --------------------------------------------------------------------------------------------- */
97 mcview_set_datasource_none (WView
*view
)
99 view
->datasource
= DS_NONE
;
102 /* --------------------------------------------------------------------------------------------- */
105 mcview_get_filesize (WView
*view
)
107 switch (view
->datasource
)
111 return mcview_growbuf_filesize (view
);
113 return view
->ds_file_filesize
;
115 return view
->ds_string_len
;
121 /* --------------------------------------------------------------------------------------------- */
124 mcview_update_filesize (WView
*view
)
126 if (view
->datasource
== DS_FILE
)
129 if (mc_fstat (view
->ds_file_fd
, &st
) != -1)
130 view
->ds_file_filesize
= st
.st_size
;
134 /* --------------------------------------------------------------------------------------------- */
137 mcview_get_ptr_file (WView
*view
, off_t byte_index
)
139 g_assert (view
->datasource
== DS_FILE
);
141 mcview_file_load_data (view
, byte_index
);
142 if (mcview_already_loaded (view
->ds_file_offset
, byte_index
, view
->ds_file_datalen
))
143 return (char *) (view
->ds_file_data
+ (byte_index
- view
->ds_file_offset
));
147 /* --------------------------------------------------------------------------------------------- */
149 /* Invalid UTF-8 is reported as negative integers (one for each byte),
150 * see ticket 3783. */
152 mcview_get_utf (WView
*view
, off_t byte_index
, int *ch
, int *ch_len
)
156 gchar utf8buf
[UTF8_CHAR_LEN
+ 1];
158 switch (view
->datasource
)
162 str
= mcview_get_ptr_growing_buffer (view
, byte_index
);
165 str
= mcview_get_ptr_file (view
, byte_index
);
168 str
= mcview_get_ptr_string (view
, byte_index
);
180 res
= g_utf8_get_char_validated (str
, -1);
184 /* Retry with explicit bytes to make sure it's not a buffer boundary */
187 for (i
= 0; i
< UTF8_CHAR_LEN
; i
++)
189 if (mcview_get_byte (view
, byte_index
+ i
, &res
))
197 utf8buf
[UTF8_CHAR_LEN
] = '\0';
199 res
= g_utf8_get_char_validated (str
, -1);
204 /* Implicit conversion from signed char to signed int keeps negative values. */
210 gchar
*next_ch
= NULL
;
213 /* Calculate UTF-8 char length */
214 next_ch
= g_utf8_next_char (str
);
215 *ch_len
= next_ch
- str
;
221 /* --------------------------------------------------------------------------------------------- */
224 mcview_get_ptr_string (WView
*view
, off_t byte_index
)
226 g_assert (view
->datasource
== DS_STRING
);
228 if (byte_index
>= 0 && byte_index
< (off_t
) view
->ds_string_len
)
229 return (char *) (view
->ds_string_data
+ byte_index
);
233 /* --------------------------------------------------------------------------------------------- */
236 mcview_get_byte_string (WView
*view
, off_t byte_index
, int *retval
)
243 p
= mcview_get_ptr_string (view
, byte_index
);
248 *retval
= (unsigned char) (*p
);
252 /* --------------------------------------------------------------------------------------------- */
255 mcview_get_byte_none (WView
*view
, off_t byte_index
, int *retval
)
260 g_assert (view
->datasource
== DS_NONE
);
267 /* --------------------------------------------------------------------------------------------- */
270 mcview_set_byte (WView
*view
, off_t offset
, byte b
)
275 g_assert (offset
< mcview_get_filesize (view
));
276 g_assert (view
->datasource
== DS_FILE
);
278 view
->ds_file_datalen
= 0; /* just force reloading */
281 /* --------------------------------------------------------------------------------------------- */
285 mcview_file_load_data (WView
*view
, off_t byte_index
)
291 g_assert (view
->datasource
== DS_FILE
);
293 if (mcview_already_loaded (view
->ds_file_offset
, byte_index
, view
->ds_file_datalen
))
296 if (byte_index
>= view
->ds_file_filesize
)
299 blockoffset
= mcview_offset_rounddown (byte_index
, view
->ds_file_datasize
);
300 if (mc_lseek (view
->ds_file_fd
, blockoffset
, SEEK_SET
) == -1)
304 while (bytes_read
< view
->ds_file_datasize
)
307 mc_read (view
->ds_file_fd
, view
->ds_file_data
+ bytes_read
,
308 view
->ds_file_datasize
- bytes_read
);
313 bytes_read
+= (size_t) res
;
315 view
->ds_file_offset
= blockoffset
;
316 if ((off_t
) bytes_read
> view
->ds_file_filesize
- view
->ds_file_offset
)
318 /* the file has grown in the meantime -- stick to the old size */
319 view
->ds_file_datalen
= view
->ds_file_filesize
- view
->ds_file_offset
;
323 view
->ds_file_datalen
= bytes_read
;
328 view
->ds_file_datalen
= 0;
331 /* --------------------------------------------------------------------------------------------- */
334 mcview_close_datasource (WView
*view
)
336 switch (view
->datasource
)
341 if (view
->ds_stdio_pipe
!= NULL
)
343 mcview_growbuf_done (view
);
344 mcview_display (view
);
346 mcview_growbuf_free (view
);
349 if (view
->ds_vfs_pipe
!= -1)
350 mcview_growbuf_done (view
);
351 mcview_growbuf_free (view
);
354 (void) mc_close (view
->ds_file_fd
);
355 view
->ds_file_fd
= -1;
356 MC_PTR_FREE (view
->ds_file_data
);
359 MC_PTR_FREE (view
->ds_string_data
);
364 view
->datasource
= DS_NONE
;
367 /* --------------------------------------------------------------------------------------------- */
370 mcview_set_datasource_file (WView
*view
, int fd
, const struct stat
*st
)
372 view
->datasource
= DS_FILE
;
373 view
->ds_file_fd
= fd
;
374 view
->ds_file_filesize
= st
->st_size
;
375 view
->ds_file_offset
= 0;
376 view
->ds_file_data
= g_malloc (4096);
377 view
->ds_file_datalen
= 0;
378 view
->ds_file_datasize
= 4096;
381 /* --------------------------------------------------------------------------------------------- */
384 mcview_load_command_output (WView
*view
, const char *command
)
387 GError
*error
= NULL
;
389 mcview_close_datasource (view
);
391 p
= mc_popen (command
, TRUE
, TRUE
, &error
);
394 mcview_display (view
);
395 mcview_show_error (view
, error
->message
);
396 g_error_free (error
);
400 /* Check if filter produced any output */
401 mcview_set_datasource_stdio_pipe (view
, p
);
402 if (!mcview_get_byte (view
, 0, NULL
))
404 mcview_close_datasource (view
);
405 mcview_display (view
);
412 /* --------------------------------------------------------------------------------------------- */
415 mcview_set_datasource_vfs_pipe (WView
*view
, int fd
)
419 view
->datasource
= DS_VFS_PIPE
;
420 view
->ds_vfs_pipe
= fd
;
422 mcview_growbuf_init (view
);
425 /* --------------------------------------------------------------------------------------------- */
428 mcview_set_datasource_string (WView
*view
, const char *s
)
430 view
->datasource
= DS_STRING
;
431 view
->ds_string_len
= strlen (s
);
432 view
->ds_string_data
= (byte
*) g_strndup (s
, view
->ds_string_len
);
435 /* --------------------------------------------------------------------------------------------- */