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