root/src/viewer/internal.h

/* [previous][next][first][last][top][bottom][index][help]  */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. mcview_offset_rounddown
  2. mcview_is_in_panel
  3. mcview_may_still_grow
  4. mcview_already_loaded
  5. mcview_get_byte_file
  6. mcview_get_byte
  7. mcview_get_byte_indexed
  8. mcview_count_backspaces
  9. mcview_is_nroff_sequence
  10. mcview_growbuf_read_all_data

   1 #ifndef MC__VIEWER_INTERNAL_H
   2 #define MC__VIEWER_INTERNAL_H
   3 
   4 #include <limits.h>             /* CHAR_BIT */
   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"        /* vfs_path_t */
  14 
  15 #include "src/keymap.h"         /* global_keymap_t */
  16 #include "src/filemanager/dir.h"        /* dir_list */
  17 
  18 #include "mcviewer.h"
  19 
  20 /*** typedefs(not structures) and defined constants **********************************************/
  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 /*** enums ***************************************************************************************/
  28 
  29 /* data sources of the view */
  30 enum view_ds
  31 {
  32     DS_NONE,                    /* No data available */
  33     DS_STDIO_PIPE,              /* Data comes from a pipe using popen/pclose */
  34     DS_VFS_PIPE,                /* Data comes from a piped-in VFS file */
  35     DS_FILE,                    /* Data comes from a VFS file */
  36     DS_STRING                   /* Data comes from a string in memory */
  37 };
  38 
  39 enum ccache_type
  40 {
  41     CCACHE_OFFSET,
  42     CCACHE_LINECOL
  43 };
  44 
  45 typedef enum
  46 {
  47     NROFF_TYPE_NONE = 0,
  48     NROFF_TYPE_BOLD = 1,
  49     NROFF_TYPE_UNDERLINE = 2
  50 } nroff_type_t;
  51 
  52 /*** structures declarations (and typedefs of structures)*****************************************/
  53 
  54 /* A node for building a change list on change_list */
  55 struct hexedit_change_node
  56 {
  57     struct hexedit_change_node *next;
  58     off_t offset;
  59     byte value;
  60 };
  61 
  62 /* A cache entry for mapping offsets into line/column pairs and vice versa.
  63  * cc_offset, cc_line, and cc_column are the 0-based values of the offset,
  64  * line and column of that cache entry. cc_nroff_column is the column
  65  * corresponding to cc_offset in nroff mode.
  66  */
  67 typedef struct
  68 {
  69     off_t cc_offset;
  70     off_t cc_line;
  71     off_t cc_column;
  72     off_t cc_nroff_column;
  73 } coord_cache_entry_t;
  74 
  75 /* TODO: find a better name. This is not actually a "state machine",
  76  * but a "state machine's state", but that sounds silly.
  77  * Could be parser_state, formatter_state... */
  78 typedef struct
  79 {
  80     off_t offset;               /* The file offset at which this is the state. */
  81     off_t unwrapped_column;     /* Columns if the paragraph wasn't wrapped, */
  82     /* used for positioning TABs in wrapped lines */
  83     gboolean nroff_underscore_is_underlined;    /* whether _\b_ is underlined rather than bold */
  84     gboolean print_lonely_combining;    /* whether lonely combining marks are printed on a dotted circle */
  85 } mcview_state_machine_t;
  86 
  87 struct mcview_nroff_struct;
  88 
  89 struct WView
  90 {
  91     Widget widget;
  92 
  93     vfs_path_t *filename_vpath; /* Name of the file */
  94     vfs_path_t *workdir_vpath;  /* Name of the working directory */
  95     char *command;              /* Command used to pipe data in */
  96 
  97     enum view_ds datasource;    /* Where the displayed data comes from */
  98 
  99     /* stdio pipe data source */
 100     mc_pipe_t *ds_stdio_pipe;   /* Output of a shell command */
 101     gboolean pipe_first_err_msg;        /* Show only 1st message from stderr */
 102 
 103     /* vfs pipe data source */
 104     int ds_vfs_pipe;            /* Non-seekable vfs file descriptor */
 105 
 106     /* vfs file data source */
 107     int ds_file_fd;             /* File with random access */
 108     off_t ds_file_filesize;     /* Size of the file */
 109     off_t ds_file_offset;       /* Offset of the currently loaded data */
 110     byte *ds_file_data;         /* Currently loaded data */
 111     size_t ds_file_datalen;     /* Number of valid bytes in file_data */
 112     size_t ds_file_datasize;    /* Number of allocated bytes in file_data */
 113 
 114     /* string data source */
 115     byte *ds_string_data;       /* The characters of the string */
 116     size_t ds_string_len;       /* The length of the string */
 117 
 118     /* Growing buffers information */
 119     gboolean growbuf_in_use;    /* Use the growing buffers? */
 120     GPtrArray *growbuf_blockptr;        /* Pointer to the block pointers */
 121     size_t growbuf_lastindex;   /* Number of bytes in the last page of the
 122                                    growing buffer */
 123     gboolean growbuf_finished;  /* TRUE when all data has been read. */
 124 
 125     mcview_mode_flags_t mode_flags;
 126 
 127     /* Hex editor modes */
 128     gboolean hexedit_mode;      /* Hexview or Hexedit */
 129     const global_keymap_t *hex_keymap;
 130     gboolean hexview_in_text;   /* Is the hexview cursor in the text area? */
 131     int bytes_per_line;         /* Number of bytes per line in hex mode */
 132     off_t hex_cursor;           /* Hexview cursor position in file */
 133     gboolean hexedit_lownibble; /* Are we editing the last significant nibble? */
 134     gboolean locked;            /* We hold lock on current file */
 135 
 136 #ifdef HAVE_CHARSET
 137     gboolean utf8;              /* It's multibyte file codeset */
 138 #endif
 139 
 140     GPtrArray *coord_cache;     /* Cache for mapping offsets to cursor positions */
 141 
 142     /* Display information */
 143     int dpy_frame_size;         /* Size of the frame surrounding the real viewer */
 144     off_t dpy_start;            /* Offset of the displayed data (start of the paragraph in non-hex mode) */
 145     off_t dpy_end;              /* Offset after the displayed data */
 146     off_t dpy_paragraph_skip_lines;     /* Extra lines to skip in wrap mode */
 147     mcview_state_machine_t dpy_state_top;       /* Parser-formatter state at the topmost visible line in wrap mode */
 148     mcview_state_machine_t dpy_state_bottom;    /* Parser-formatter state after the bottomvisible line in wrap mode */
 149     gboolean dpy_wrap_dirty;    /* dpy_state_top needs to be recomputed */
 150     off_t dpy_text_column;      /* Number of skipped columns in non-wrap
 151                                  * text mode */
 152     int cursor_col;             /* Cursor column */
 153     int cursor_row;             /* Cursor row */
 154     struct hexedit_change_node *change_list;    /* Linked list of changes */
 155     WRect status_area;          /* Where the status line is displayed */
 156     WRect ruler_area;           /* Where the ruler is displayed */
 157     WRect data_area;            /* Where the data is displayed */
 158 
 159     ssize_t force_max;          /* Force a max offset, or -1 */
 160 
 161     int dirty;                  /* Number of skipped updates */
 162     gboolean dpy_bbar_dirty;    /* Does the button bar need to be updated? */
 163 
 164 
 165     /* handle of search engine */
 166     mc_search_t *search;
 167     gchar *last_search_string;
 168     struct mcview_nroff_struct *search_nroff_seq;
 169     off_t search_start;         /* First character to start searching from */
 170     off_t search_end;           /* Length of found string or 0 if none was found */
 171     int search_numNeedSkipChar;
 172 
 173     /* Markers */
 174     int marker;                 /* mark to use */
 175     off_t marks[10];            /* 10 marks: 0..9 */
 176 
 177     off_t update_steps;         /* The number of bytes between percent increments */
 178     off_t update_activate;      /* Last point where we updated the status */
 179 
 180     /* converter for translation of text */
 181     GIConv converter;
 182 
 183     GArray *saved_bookmarks;
 184 
 185     dir_list *dir;              /* List of current directory files
 186                                  * to handle CK_FileNext and CK_FilePrev commands */
 187     int *dir_idx;               /* Index of current file in dir structure.
 188                                  * Pointer is used here as reference to WPanel::dir::count */
 189     vfs_path_t *ext_script;     /* Temporary script file created by regex_command_for() */
 190 };
 191 
 192 typedef struct mcview_nroff_struct
 193 {
 194     WView *view;
 195     off_t index;
 196     int char_length;
 197     int current_char;
 198     nroff_type_t type;
 199     nroff_type_t prev_type;
 200 } mcview_nroff_t;
 201 
 202 typedef struct mcview_search_options_t
 203 {
 204     mc_search_type_t type;
 205     gboolean case_sens;
 206     gboolean backwards;
 207     gboolean whole_words;
 208     gboolean all_codepages;
 209 } mcview_search_options_t;
 210 
 211 /*** global variables defined in .c file *********************************************************/
 212 
 213 extern mcview_search_options_t mcview_search_options;
 214 
 215 /*** declarations of public functions ************************************************************/
 216 
 217 /* actions_cmd.c:  */
 218 cb_ret_t mcview_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data);
 219 cb_ret_t mcview_dialog_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm,
 220                                  void *data);
 221 
 222 /* ascii.c: */
 223 void mcview_display_text (WView * view);
 224 void mcview_state_machine_init (mcview_state_machine_t *, off_t);
 225 void mcview_ascii_move_down (WView * view, off_t lines);
 226 void mcview_ascii_move_up (WView * view, off_t lines);
 227 void mcview_ascii_moveto_bol (WView * view);
 228 void mcview_ascii_moveto_eol (WView * view);
 229 
 230 #ifdef MC_ENABLE_DEBUGGING_CODE
 231 void mcview_ccache_dump (WView * view);
 232 #endif
 233 
 234 void mcview_ccache_lookup (WView * view, coord_cache_entry_t * coord, enum ccache_type lookup_what);
 235 
 236 /* datasource.c: */
 237 void mcview_set_datasource_none (WView * view);
 238 off_t mcview_get_filesize (WView * view);
 239 void mcview_update_filesize (WView * view);
 240 char *mcview_get_ptr_file (WView * view, off_t byte_index);
 241 char *mcview_get_ptr_string (WView * view, off_t byte_index);
 242 gboolean mcview_get_utf (WView * view, off_t byte_index, int *ch, int *ch_len);
 243 gboolean mcview_get_byte_string (WView * view, off_t byte_index, int *retval);
 244 gboolean mcview_get_byte_none (WView * view, off_t byte_index, int *retval);
 245 void mcview_set_byte (WView * view, off_t offset, byte b);
 246 void mcview_file_load_data (WView * view, off_t byte_index);
 247 void mcview_close_datasource (WView * view);
 248 void mcview_set_datasource_file (WView * view, int fd, const struct stat *st);
 249 gboolean mcview_load_command_output (WView * view, const char *command);
 250 void mcview_set_datasource_vfs_pipe (WView * view, int fd);
 251 void mcview_set_datasource_string (WView * view, const char *s);
 252 
 253 /* dialog.c: */
 254 gboolean mcview_dialog_search (WView * view);
 255 gboolean mcview_dialog_goto (WView * view, off_t * offset);
 256 
 257 /* display.c: */
 258 void mcview_update (WView * view);
 259 void mcview_display (WView * view);
 260 void mcview_compute_areas (WView * view);
 261 void mcview_update_bytes_per_line (WView * view);
 262 void mcview_display_toggle_ruler (WView * view);
 263 void mcview_display_clean (WView * view);
 264 void mcview_display_ruler (WView * view);
 265 
 266 /* growbuf.c: */
 267 void mcview_growbuf_init (WView * view);
 268 void mcview_growbuf_done (WView * view);
 269 void mcview_growbuf_free (WView * view);
 270 off_t mcview_growbuf_filesize (WView * view);
 271 void mcview_growbuf_read_until (WView * view, off_t ofs);
 272 gboolean mcview_get_byte_growing_buffer (WView * view, off_t byte_index, int *retval);
 273 char *mcview_get_ptr_growing_buffer (WView * view, off_t byte_index);
 274 
 275 /* hex.c: */
 276 void mcview_display_hex (WView * view);
 277 gboolean mcview_hexedit_save_changes (WView * view);
 278 void mcview_toggle_hexedit_mode (WView * view);
 279 void mcview_hexedit_free_change_list (WView * view);
 280 void mcview_enqueue_change (struct hexedit_change_node **head, struct hexedit_change_node *node);
 281 
 282 /* lib.c: */
 283 void mcview_toggle_magic_mode (WView * view);
 284 void mcview_toggle_wrap_mode (WView * view);
 285 void mcview_toggle_nroff_mode (WView * view);
 286 void mcview_toggle_hex_mode (WView * view);
 287 void mcview_init (WView * view);
 288 void mcview_done (WView * view);
 289 #ifdef HAVE_CHARSET
 290 void mcview_select_encoding (WView * view);
 291 void mcview_set_codeset (WView * view);
 292 #endif
 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 /* move.c */
 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 /* nroff.c: */
 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 /* search.c: */
 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, gsize char_offset,
 328                                               int *current_char);
 329 mc_search_cbret_t mcview_search_update_cmd_callback (const void *user_data, gsize char_offset);
 330 void mcview_do_search (WView * view, off_t want_search_start);
 331 
 332 /* --------------------------------------------------------------------------------------------- */
 333 /*** inline functions ****************************************************************************/
 334 /* --------------------------------------------------------------------------------------------- */
 335 
 336 static inline off_t
 337 mcview_offset_rounddown (off_t a, off_t b)
     /* [previous][next][first][last][top][bottom][index][help]  */
 338 {
 339     g_assert (b != 0);
 340     return a - a % b;
 341 }
 342 
 343 /* --------------------------------------------------------------------------------------------- */
 344 
 345 /* {{{ Simple Primitive Functions for WView }}} */
 346 static inline gboolean
 347 mcview_is_in_panel (WView * view)
     /* [previous][next][first][last][top][bottom][index][help]  */
 348 {
 349     return (view->dpy_frame_size != 0);
 350 }
 351 
 352 /* --------------------------------------------------------------------------------------------- */
 353 
 354 static inline gboolean
 355 mcview_may_still_grow (WView * view)
     /* [previous][next][first][last][top][bottom][index][help]  */
 356 {
 357     return (view->growbuf_in_use && !view->growbuf_finished);
 358 }
 359 
 360 /* --------------------------------------------------------------------------------------------- */
 361 
 362 /* returns TRUE if the idx lies in the half-open interval
 363  * [offset; offset + size), FALSE otherwise.
 364  */
 365 static inline gboolean
 366 mcview_already_loaded (off_t offset, off_t idx, size_t size)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 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)
     /* [previous][next][first][last][top][bottom][index][help]  */
 415 {
 416     if (base <= OFFSETTYPE_MAX - ofs)
 417     {
 418         return mcview_get_byte (view, base + ofs, retval);
 419     }
 420     if (retval)
 421         *retval = -1;
 422     return FALSE;
 423 }
 424 
 425 /* --------------------------------------------------------------------------------------------- */
 426 
 427 static inline int
 428 mcview_count_backspaces (WView * view, off_t offset)
     /* [previous][next][first][last][top][bottom][index][help]  */
 429 {
 430     int backspaces = 0;
 431     int c;
 432     while (offset >= 2 * backspaces && mcview_get_byte (view, offset - 2 * backspaces, &c)
 433            && c == '\b')
 434         backspaces++;
 435     return backspaces;
 436 }
 437 
 438 /* --------------------------------------------------------------------------------------------- */
 439 
 440 static inline gboolean
 441 mcview_is_nroff_sequence (WView * view, off_t offset)
     /* [previous][next][first][last][top][bottom][index][help]  */
 442 {
 443     int c0, c1, c2;
 444 
 445     /* The following commands are ordered to speed up the calculation. */
 446 
 447     if (!mcview_get_byte_indexed (view, offset, 1, &c1) || c1 != '\b')
 448         return FALSE;
 449 
 450     if (!mcview_get_byte_indexed (view, offset, 0, &c0) || !g_ascii_isprint (c0))
 451         return FALSE;
 452 
 453     if (!mcview_get_byte_indexed (view, offset, 2, &c2) || !g_ascii_isprint (c2))
 454         return FALSE;
 455 
 456     return (c0 == c2 || c0 == '_' || (c0 == '+' && c2 == 'o'));
 457 }
 458 
 459 /* --------------------------------------------------------------------------------------------- */
 460 
 461 static inline void
 462 mcview_growbuf_read_all_data (WView * view)
     /* [previous][next][first][last][top][bottom][index][help]  */
 463 {
 464     mcview_growbuf_read_until (view, OFFSETTYPE_MAX);
 465 }
 466 
 467 /* --------------------------------------------------------------------------------------------- */
 468 
 469 #endif /* MC__VIEWER_INTERNAL_H */

/* [previous][next][first][last][top][bottom][index][help]  */