This source file includes following definitions.
- mcview_offset_rounddown
- mcview_is_in_panel
- mcview_may_still_grow
- mcview_already_loaded
- mcview_get_byte_file
- mcview_get_byte
- mcview_get_byte_indexed
- mcview_count_backspaces
- mcview_is_nroff_sequence
- mcview_growbuf_read_all_data
1 #ifndef MC__VIEWER_INTERNAL_H
2 #define MC__VIEWER_INTERNAL_H
3
4 #include <limits.h>
5 #include <stdlib.h>
6 #include <stdio.h>
7 #include <sys/types.h>
8
9 #include "lib/global.h"
10
11 #include "lib/search.h"
12 #include "lib/widget.h"
13 #include "lib/vfs/vfs.h"
14
15 #include "src/keymap.h"
16 #include "src/filemanager/dir.h"
17
18 #include "mcviewer.h"
19
20
21
22 #define OFF_T_BITWIDTH ((unsigned int) (sizeof (off_t) * CHAR_BIT - 1))
23 #define OFFSETTYPE_MAX (((off_t) 1 << (OFF_T_BITWIDTH - 1)) - 1)
24
25 typedef unsigned char byte;
26
27
28 typedef unsigned int screen_dimen;
29
30
31
32
33 enum view_ds
34 {
35 DS_NONE,
36 DS_STDIO_PIPE,
37 DS_VFS_PIPE,
38 DS_FILE,
39 DS_STRING
40 };
41
42 enum ccache_type
43 {
44 CCACHE_OFFSET,
45 CCACHE_LINECOL
46 };
47
48 typedef enum
49 {
50 NROFF_TYPE_NONE = 0,
51 NROFF_TYPE_BOLD = 1,
52 NROFF_TYPE_UNDERLINE = 2
53 } nroff_type_t;
54
55
56
57
58 struct hexedit_change_node
59 {
60 struct hexedit_change_node *next;
61 off_t offset;
62 byte value;
63 };
64
65 struct area
66 {
67 screen_dimen top, left;
68 screen_dimen height, width;
69 };
70
71
72
73
74
75
76 typedef struct
77 {
78 off_t cc_offset;
79 off_t cc_line;
80 off_t cc_column;
81 off_t cc_nroff_column;
82 } coord_cache_entry_t;
83
84 typedef struct
85 {
86 size_t size;
87 size_t capacity;
88 coord_cache_entry_t **cache;
89 } coord_cache_t;
90
91
92
93
94 typedef struct
95 {
96 off_t offset;
97 off_t unwrapped_column;
98
99 gboolean nroff_underscore_is_underlined;
100 gboolean print_lonely_combining;
101 } mcview_state_machine_t;
102
103 struct mcview_nroff_struct;
104
105 struct WView
106 {
107 Widget widget;
108
109 vfs_path_t *filename_vpath;
110 vfs_path_t *workdir_vpath;
111 char *command;
112
113 enum view_ds datasource;
114
115
116 mc_pipe_t *ds_stdio_pipe;
117 gboolean pipe_first_err_msg;
118
119
120 int ds_vfs_pipe;
121
122
123 int ds_file_fd;
124 off_t ds_file_filesize;
125 off_t ds_file_offset;
126 byte *ds_file_data;
127 size_t ds_file_datalen;
128 size_t ds_file_datasize;
129
130
131 byte *ds_string_data;
132 size_t ds_string_len;
133
134
135 gboolean growbuf_in_use;
136 GPtrArray *growbuf_blockptr;
137 size_t growbuf_lastindex;
138
139 gboolean growbuf_finished;
140
141 mcview_mode_flags_t mode_flags;
142
143
144 gboolean hexedit_mode;
145 const global_keymap_t *hex_keymap;
146 gboolean hexview_in_text;
147 int bytes_per_line;
148 off_t hex_cursor;
149 gboolean hexedit_lownibble;
150 gboolean locked;
151
152 #ifdef HAVE_CHARSET
153 gboolean utf8;
154 #endif
155
156 coord_cache_t *coord_cache;
157
158
159 screen_dimen dpy_frame_size;
160 off_t dpy_start;
161 off_t dpy_end;
162 off_t dpy_paragraph_skip_lines;
163 mcview_state_machine_t dpy_state_top;
164 mcview_state_machine_t dpy_state_bottom;
165 gboolean dpy_wrap_dirty;
166 off_t dpy_text_column;
167
168 screen_dimen cursor_col;
169 screen_dimen cursor_row;
170 struct hexedit_change_node *change_list;
171 struct area status_area;
172 struct area ruler_area;
173 struct area data_area;
174
175 ssize_t force_max;
176
177 int dirty;
178 gboolean dpy_bbar_dirty;
179
180
181
182 mc_search_t *search;
183 gchar *last_search_string;
184 struct mcview_nroff_struct *search_nroff_seq;
185 off_t search_start;
186 off_t search_end;
187 int search_numNeedSkipChar;
188
189
190 int marker;
191 off_t marks[10];
192
193 off_t update_steps;
194 off_t update_activate;
195
196
197 GIConv converter;
198
199 GArray *saved_bookmarks;
200
201 dir_list *dir;
202
203 int *dir_idx;
204
205 vfs_path_t *ext_script;
206 };
207
208 typedef struct mcview_nroff_struct
209 {
210 WView *view;
211 off_t index;
212 int char_length;
213 int current_char;
214 nroff_type_t type;
215 nroff_type_t prev_type;
216 } mcview_nroff_t;
217
218 typedef struct mcview_search_options_t
219 {
220 mc_search_type_t type;
221 gboolean case_sens;
222 gboolean backwards;
223 gboolean whole_words;
224 gboolean all_codepages;
225 } mcview_search_options_t;
226
227
228
229 extern mcview_search_options_t mcview_search_options;
230
231
232
233
234 cb_ret_t mcview_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data);
235 cb_ret_t mcview_dialog_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm,
236 void *data);
237
238
239 void mcview_display_text (WView * view);
240 void mcview_state_machine_init (mcview_state_machine_t *, off_t);
241 void mcview_ascii_move_down (WView * view, off_t lines);
242 void mcview_ascii_move_up (WView * view, off_t lines);
243 void mcview_ascii_moveto_bol (WView * view);
244 void mcview_ascii_moveto_eol (WView * view);
245
246
247 coord_cache_t *coord_cache_new (void);
248 void coord_cache_free (coord_cache_t * cache);
249
250 #ifdef MC_ENABLE_DEBUGGING_CODE
251 void mcview_ccache_dump (WView * view);
252 #endif
253
254 void mcview_ccache_lookup (WView * view, coord_cache_entry_t * coord, enum ccache_type lookup_what);
255
256
257 void mcview_set_datasource_none (WView * view);
258 off_t mcview_get_filesize (WView * view);
259 void mcview_update_filesize (WView * view);
260 char *mcview_get_ptr_file (WView * view, off_t byte_index);
261 char *mcview_get_ptr_string (WView * view, off_t byte_index);
262 gboolean mcview_get_utf (WView * view, off_t byte_index, int *ch, int *ch_len);
263 gboolean mcview_get_byte_string (WView * view, off_t byte_index, int *retval);
264 gboolean mcview_get_byte_none (WView * view, off_t byte_index, int *retval);
265 void mcview_set_byte (WView * view, off_t offset, byte b);
266 void mcview_file_load_data (WView * view, off_t byte_index);
267 void mcview_close_datasource (WView * view);
268 void mcview_set_datasource_file (WView * view, int fd, const struct stat *st);
269 gboolean mcview_load_command_output (WView * view, const char *command);
270 void mcview_set_datasource_vfs_pipe (WView * view, int fd);
271 void mcview_set_datasource_string (WView * view, const char *s);
272
273
274 gboolean mcview_dialog_search (WView * view);
275 gboolean mcview_dialog_goto (WView * view, off_t * offset);
276
277
278 void mcview_update (WView * view);
279 void mcview_display (WView * view);
280 void mcview_compute_areas (WView * view);
281 void mcview_update_bytes_per_line (WView * view);
282 void mcview_display_toggle_ruler (WView * view);
283 void mcview_display_clean (WView * view);
284 void mcview_display_ruler (WView * view);
285
286
287 void mcview_growbuf_init (WView * view);
288 void mcview_growbuf_done (WView * view);
289 void mcview_growbuf_free (WView * view);
290 off_t mcview_growbuf_filesize (WView * view);
291 void mcview_growbuf_read_until (WView * view, off_t ofs);
292 gboolean mcview_get_byte_growing_buffer (WView * view, off_t byte_index, int *retval);
293 char *mcview_get_ptr_growing_buffer (WView * view, off_t byte_index);
294
295
296 void mcview_display_hex (WView * view);
297 gboolean mcview_hexedit_save_changes (WView * view);
298 void mcview_toggle_hexedit_mode (WView * view);
299 void mcview_hexedit_free_change_list (WView * view);
300 void mcview_enqueue_change (struct hexedit_change_node **head, struct hexedit_change_node *node);
301
302
303 void mcview_toggle_magic_mode (WView * view);
304 void mcview_toggle_wrap_mode (WView * view);
305 void mcview_toggle_nroff_mode (WView * view);
306 void mcview_toggle_hex_mode (WView * view);
307 void mcview_init (WView * view);
308 void mcview_done (WView * view);
309 #ifdef HAVE_CHARSET
310 void mcview_select_encoding (WView * view);
311 void mcview_set_codeset (WView * view);
312 #endif
313 void mcview_show_error (WView * view, const char *error);
314 off_t mcview_bol (WView * view, off_t current, off_t limit);
315 off_t mcview_eol (WView * view, off_t current);
316 char *mcview_get_title (const WDialog * h, size_t len);
317 int mcview_calc_percent (WView * view, off_t p);
318
319
320 void mcview_move_up (WView * view, off_t lines);
321 void mcview_move_down (WView * view, off_t lines);
322 void mcview_move_left (WView * view, off_t columns);
323 void mcview_move_right (WView * view, off_t columns);
324 void mcview_moveto_top (WView * view);
325 void mcview_moveto_bottom (WView * view);
326 void mcview_moveto_bol (WView * view);
327 void mcview_moveto_eol (WView * view);
328 void mcview_moveto_offset (WView * view, off_t offset);
329 void mcview_moveto (WView * view, off_t, off_t col);
330 void mcview_coord_to_offset (WView * view, off_t * ret_offset, off_t line, off_t column);
331 void mcview_offset_to_coord (WView * view, off_t * ret_line, off_t * ret_column, off_t offset);
332 void mcview_place_cursor (WView * view);
333 void mcview_moveto_match (WView * view);
334
335
336 int mcview__get_nroff_real_len (WView * view, off_t start, off_t length);
337 mcview_nroff_t *mcview_nroff_seq_new_num (WView * view, off_t lc_index);
338 mcview_nroff_t *mcview_nroff_seq_new (WView * view);
339 void mcview_nroff_seq_free (mcview_nroff_t ** nroff);
340 nroff_type_t mcview_nroff_seq_info (mcview_nroff_t * nroff);
341 int mcview_nroff_seq_next (mcview_nroff_t * nroff);
342 int mcview_nroff_seq_prev (mcview_nroff_t * nroff);
343
344
345 gboolean mcview_search_init (WView * view);
346 void mcview_search_deinit (WView * view);
347 mc_search_cbret_t mcview_search_cmd_callback (const void *user_data, gsize char_offset,
348 int *current_char);
349 mc_search_cbret_t mcview_search_update_cmd_callback (const void *user_data, gsize char_offset);
350 void mcview_do_search (WView * view, off_t want_search_start);
351
352
353
354
355
356 static inline off_t
357 mcview_offset_rounddown (off_t a, off_t b)
358 {
359 g_assert (b != 0);
360 return a - a % b;
361 }
362
363
364
365
366 static inline gboolean
367 mcview_is_in_panel (WView * view)
368 {
369 return (view->dpy_frame_size != 0);
370 }
371
372
373
374 static inline gboolean
375 mcview_may_still_grow (WView * view)
376 {
377 return (view->growbuf_in_use && !view->growbuf_finished);
378 }
379
380
381
382
383
384
385 static inline gboolean
386 mcview_already_loaded (off_t offset, off_t idx, size_t size)
387 {
388 return (offset <= idx && idx - offset < (off_t) size);
389 }
390
391
392
393 static inline gboolean
394 mcview_get_byte_file (WView * view, off_t byte_index, int *retval)
395 {
396 g_assert (view->datasource == DS_FILE);
397
398 mcview_file_load_data (view, byte_index);
399 if (mcview_already_loaded (view->ds_file_offset, byte_index, view->ds_file_datalen))
400 {
401 if (retval)
402 *retval = view->ds_file_data[byte_index - view->ds_file_offset];
403 return TRUE;
404 }
405 if (retval)
406 *retval = -1;
407 return FALSE;
408 }
409
410
411
412 static inline gboolean
413 mcview_get_byte (WView * view, off_t offset, int *retval)
414 {
415 switch (view->datasource)
416 {
417 case DS_STDIO_PIPE:
418 case DS_VFS_PIPE:
419 return mcview_get_byte_growing_buffer (view, offset, retval);
420 case DS_FILE:
421 return mcview_get_byte_file (view, offset, retval);
422 case DS_STRING:
423 return mcview_get_byte_string (view, offset, retval);
424 case DS_NONE:
425 return mcview_get_byte_none (view, offset, retval);
426 default:
427 return FALSE;
428 }
429 }
430
431
432
433 static inline gboolean
434 mcview_get_byte_indexed (WView * view, off_t base, off_t ofs, int *retval)
435 {
436 if (base <= OFFSETTYPE_MAX - ofs)
437 {
438 return mcview_get_byte (view, base + ofs, retval);
439 }
440 if (retval)
441 *retval = -1;
442 return FALSE;
443 }
444
445
446
447 static inline int
448 mcview_count_backspaces (WView * view, off_t offset)
449 {
450 int backspaces = 0;
451 int c;
452 while (offset >= 2 * backspaces && mcview_get_byte (view, offset - 2 * backspaces, &c)
453 && c == '\b')
454 backspaces++;
455 return backspaces;
456 }
457
458
459
460 static inline gboolean
461 mcview_is_nroff_sequence (WView * view, off_t offset)
462 {
463 int c0, c1, c2;
464
465
466
467 if (!mcview_get_byte_indexed (view, offset, 1, &c1) || c1 != '\b')
468 return FALSE;
469
470 if (!mcview_get_byte_indexed (view, offset, 0, &c0) || !g_ascii_isprint (c0))
471 return FALSE;
472
473 if (!mcview_get_byte_indexed (view, offset, 2, &c2) || !g_ascii_isprint (c2))
474 return FALSE;
475
476 return (c0 == c2 || c0 == '_' || (c0 == '+' && c2 == 'o'));
477 }
478
479
480
481 static inline void
482 mcview_growbuf_read_all_data (WView * view)
483 {
484 mcview_growbuf_read_until (view, OFFSETTYPE_MAX);
485 }
486
487
488
489 #endif