root/src/diffviewer/ydiff.c

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

DEFINITIONS

This source file includes following definitions.
  1. TAB_SKIP
  2. fill_by_space
  3. rewrite_backup_content
  4. open_temp
  5. dview_fdopen
  6. dview_ffree
  7. dview_ftemp
  8. dview_fopen
  9. dview_fgets
  10. dview_fseek
  11. dview_freset
  12. dview_fwrite
  13. dview_ftrunc
  14. dview_fclose
  15. dview_popen
  16. dview_pclose
  17. dview_get_byte
  18. dview_get_utf
  19. dview_str_utf8_offset_to_pos
  20. scan_deci
  21. scan_line
  22. scan_diff
  23. dff_execute
  24. printer_for
  25. dff_reparse
  26. lcsubstr
  27. hdiff_multi
  28. hdiff_scan
  29. is_inside
  30. cvt_cpy
  31. cvt_ncpy
  32. cvt_mget
  33. cvt_mgeta
  34. cvt_fget
  35. cc_free_elt
  36. printer
  37. redo_diff
  38. destroy_hdiff
  39. get_digits
  40. get_line_numbers
  41. calc_nwidth
  42. find_prev_hunk
  43. find_next_hunk
  44. get_current_hunk
  45. dview_remove_hunk
  46. dview_add_hunk
  47. dview_replace_hunk
  48. do_merge_hunk
  49. dview_compute_split
  50. dview_compute_areas
  51. dview_reread
  52. dview_set_codeset
  53. dview_select_encoding
  54. dview_load_options
  55. dview_save_options
  56. dview_diff_options
  57. dview_init
  58. dview_fini
  59. dview_display_file
  60. dview_status
  61. dview_redo
  62. dview_update
  63. dview_edit
  64. dview_goto_cmd
  65. dview_labels
  66. dview_save
  67. dview_do_save
  68. dview_ok_to_exit
  69. dview_execute_cmd
  70. dview_handle_key
  71. dview_callback
  72. dview_mouse_callback
  73. dview_dialog_callback
  74. dview_get_title
  75. diff_view
  76. dview_diff_cmd

   1 /*
   2    File difference viewer
   3 
   4    Copyright (C) 2007-2025
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Daniel Borca <dborca@yahoo.com>, 2007
   9    Slava Zanko <slavazanko@gmail.com>, 2010, 2013
  10    Andrew Borodin <aborodin@vmail.ru>, 2010-2022
  11    Ilia Maslakov <il.smind@gmail.com>, 2010
  12 
  13    This file is part of the Midnight Commander.
  14 
  15    The Midnight Commander is free software: you can redistribute it
  16    and/or modify it under the terms of the GNU General Public License as
  17    published by the Free Software Foundation, either version 3 of the License,
  18    or (at your option) any later version.
  19 
  20    The Midnight Commander is distributed in the hope that it will be useful,
  21    but WITHOUT ANY WARRANTY; without even the implied warranty of
  22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23    GNU General Public License for more details.
  24 
  25    You should have received a copy of the GNU General Public License
  26    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  27  */
  28 
  29 
  30 #include <config.h>
  31 
  32 #include <ctype.h>
  33 #include <errno.h>
  34 #include <stddef.h>             /* ptrdiff_t */
  35 #include <stdlib.h>
  36 #include <sys/stat.h>
  37 #include <sys/types.h>
  38 #include <sys/wait.h>
  39 
  40 #include "lib/global.h"
  41 #include "lib/tty/tty.h"
  42 #include "lib/tty/color.h"
  43 #include "lib/tty/key.h"
  44 #include "lib/skin.h"           /* EDITOR_NORMAL_COLOR */
  45 #include "lib/vfs/vfs.h"
  46 #include "lib/util.h"
  47 #include "lib/widget.h"
  48 #include "lib/strutil.h"
  49 #ifdef HAVE_CHARSET
  50 #include "lib/charsets.h"
  51 #endif
  52 #include "lib/event.h"          /* mc_event_raise() */
  53 
  54 #include "src/filemanager/cmd.h"        /* edit_file_at_line() */
  55 #include "src/filemanager/panel.h"
  56 #include "src/filemanager/layout.h"     /* Needed for get_current_index and get_other_panel */
  57 
  58 #include "src/execute.h"        /* toggle_subshell() */
  59 #include "src/keymap.h"
  60 #include "src/setup.h"
  61 #include "src/history.h"
  62 #ifdef HAVE_CHARSET
  63 #include "src/selcodepage.h"
  64 #endif
  65 
  66 #include "ydiff.h"
  67 #include "internal.h"
  68 
  69 /*** global variables ****************************************************************************/
  70 
  71 /*** file scope macro definitions ****************************************************************/
  72 
  73 #define FILE_READ_BUF 4096
  74 #define FILE_FLAG_TEMP (1 << 0)
  75 
  76 #define ADD_CH '+'
  77 #define DEL_CH '-'
  78 #define CHG_CH '*'
  79 #define EQU_CH ' '
  80 
  81 #define HDIFF_ENABLE 1
  82 #define HDIFF_MINCTX 5
  83 #define HDIFF_DEPTH 10
  84 
  85 #define FILE_DIRTY(fs) \
  86 do \
  87 { \
  88     (fs)->pos = 0; \
  89     (fs)->len = 0;  \
  90 } \
  91 while (0)
  92 
  93 /*** file scope type declarations ****************************************************************/
  94 
  95 typedef enum
  96 {
  97     FROM_LEFT_TO_RIGHT,
  98     FROM_RIGHT_TO_LEFT
  99 } action_direction_t;
 100 
 101 /*** forward declarations (file scope functions) *************************************************/
 102 
 103 /*** file scope variables ************************************************************************/
 104 
 105 /* --------------------------------------------------------------------------------------------- */
 106 /*** file scope functions ************************************************************************/
 107 /* --------------------------------------------------------------------------------------------- */
 108 
 109 static inline int
 110 TAB_SKIP (int ts, int pos)
     /* [previous][next][first][last][top][bottom][index][help]  */
 111 {
 112     if (ts > 0 && ts < 9)
 113         return ts - pos % ts;
 114     else
 115         return 8 - pos % 8;
 116 }
 117 
 118 /* --------------------------------------------------------------------------------------------- */
 119 
 120 /**
 121  * Fill buffer by spaces
 122  *
 123  * @param buf buffer
 124  * @param n number of spaces
 125  * @param zero_terminate add a nul after @n spaces
 126  */
 127 static void
 128 fill_by_space (char *buf, size_t n, gboolean zero_terminate)
     /* [previous][next][first][last][top][bottom][index][help]  */
 129 {
 130     memset (buf, ' ', n);
 131     if (zero_terminate)
 132         buf[n] = '\0';
 133 }
 134 
 135 /* --------------------------------------------------------------------------------------------- */
 136 
 137 static gboolean
 138 rewrite_backup_content (const vfs_path_t *from_file_name_vpath, const char *to_file_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 139 {
 140     FILE *backup_fd;
 141     char *contents;
 142     gsize length;
 143     const char *from_file_name;
 144 
 145     from_file_name = vfs_path_get_last_path_str (from_file_name_vpath);
 146     if (!g_file_get_contents (from_file_name, &contents, &length, NULL))
 147         return FALSE;
 148 
 149     backup_fd = fopen (to_file_name, "w");
 150     if (backup_fd == NULL)
 151     {
 152         g_free (contents);
 153         return FALSE;
 154     }
 155 
 156     length = fwrite ((const void *) contents, length, 1, backup_fd);
 157 
 158     fflush (backup_fd);
 159     fclose (backup_fd);
 160     g_free (contents);
 161     return TRUE;
 162 }
 163 
 164 /* buffered I/O ************************************************************* */
 165 
 166 /**
 167  * Try to open a temporary file.
 168  * @note the name is not altered if this function fails
 169  *
 170  * @param[out] name address of a pointer to store the temporary name
 171  * @return file descriptor on success, negative on error
 172  */
 173 
 174 static int
 175 open_temp (void **name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 176 {
 177     int fd;
 178     vfs_path_t *diff_file_name = NULL;
 179 
 180     fd = mc_mkstemps (&diff_file_name, "mcdiff", NULL);
 181     if (fd == -1)
 182     {
 183         message (D_ERROR, MSG_ERROR,
 184                  _("Cannot create temporary diff file\n%s"), unix_error_string (errno));
 185         return -1;
 186     }
 187 
 188     *name = vfs_path_free (diff_file_name, FALSE);
 189     return fd;
 190 }
 191 
 192 /* --------------------------------------------------------------------------------------------- */
 193 
 194 /**
 195  * Allocate file structure and associate file descriptor to it.
 196  *
 197  * @param fd file descriptor
 198  * @return file structure
 199  */
 200 
 201 static FBUF *
 202 dview_fdopen (int fd)
     /* [previous][next][first][last][top][bottom][index][help]  */
 203 {
 204     FBUF *fs;
 205 
 206     if (fd < 0)
 207         return NULL;
 208 
 209     fs = g_try_malloc (sizeof (FBUF));
 210     if (fs == NULL)
 211         return NULL;
 212 
 213     fs->buf = g_try_malloc (FILE_READ_BUF);
 214     if (fs->buf == NULL)
 215     {
 216         g_free (fs);
 217         return NULL;
 218     }
 219 
 220     fs->fd = fd;
 221     FILE_DIRTY (fs);
 222     fs->flags = 0;
 223     fs->data = NULL;
 224 
 225     return fs;
 226 }
 227 
 228 /* --------------------------------------------------------------------------------------------- */
 229 
 230 /**
 231  * Free file structure without closing the file.
 232  *
 233  * @param fs file structure
 234  * @return 0 on success, non-zero on error
 235  */
 236 
 237 static int
 238 dview_ffree (FBUF *fs)
     /* [previous][next][first][last][top][bottom][index][help]  */
 239 {
 240     int rv = 0;
 241 
 242     if ((fs->flags & FILE_FLAG_TEMP) != 0)
 243     {
 244         rv = unlink (fs->data);
 245         g_free (fs->data);
 246     }
 247     g_free (fs->buf);
 248     g_free (fs);
 249     return rv;
 250 }
 251 
 252 /* --------------------------------------------------------------------------------------------- */
 253 
 254 /**
 255  * Open a binary temporary file in R/W mode.
 256  * @note the file will be deleted when closed
 257  *
 258  * @return file structure
 259  */
 260 static FBUF *
 261 dview_ftemp (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 262 {
 263     int fd;
 264     FBUF *fs;
 265 
 266     fs = dview_fdopen (0);
 267     if (fs == NULL)
 268         return NULL;
 269 
 270     fd = open_temp (&fs->data);
 271     if (fd < 0)
 272     {
 273         dview_ffree (fs);
 274         return NULL;
 275     }
 276 
 277     fs->fd = fd;
 278     fs->flags = FILE_FLAG_TEMP;
 279     return fs;
 280 }
 281 
 282 /* --------------------------------------------------------------------------------------------- */
 283 
 284 /**
 285  * Open a binary file in specified mode.
 286  *
 287  * @param filename file name
 288  * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
 289  *
 290  * @return file structure
 291  */
 292 
 293 static FBUF *
 294 dview_fopen (const char *filename, int flags)
     /* [previous][next][first][last][top][bottom][index][help]  */
 295 {
 296     int fd;
 297     FBUF *fs;
 298 
 299     fs = dview_fdopen (0);
 300     if (fs == NULL)
 301         return NULL;
 302 
 303     fd = open (filename, flags);
 304     if (fd < 0)
 305     {
 306         dview_ffree (fs);
 307         return NULL;
 308     }
 309 
 310     fs->fd = fd;
 311     return fs;
 312 }
 313 
 314 /* --------------------------------------------------------------------------------------------- */
 315 
 316 /**
 317  * Read a line of bytes from file until newline or EOF.
 318  * @note does not stop on null-byte
 319  * @note buf will not be null-terminated
 320  *
 321  * @param buf destination buffer
 322  * @param size size of buffer
 323  * @param fs file structure
 324  *
 325  * @return number of bytes read
 326  */
 327 
 328 static size_t
 329 dview_fgets (char *buf, size_t size, FBUF *fs)
     /* [previous][next][first][last][top][bottom][index][help]  */
 330 {
 331     size_t j = 0;
 332 
 333     do
 334     {
 335         int i;
 336         gboolean stop = FALSE;
 337 
 338         for (i = fs->pos; j < size && i < fs->len && !stop; i++, j++)
 339         {
 340             buf[j] = fs->buf[i];
 341             if (buf[j] == '\n')
 342                 stop = TRUE;
 343         }
 344         fs->pos = i;
 345 
 346         if (j == size || stop)
 347             break;
 348 
 349         fs->pos = 0;
 350         fs->len = read (fs->fd, fs->buf, FILE_READ_BUF);
 351     }
 352     while (fs->len > 0);
 353 
 354     return j;
 355 }
 356 
 357 /* --------------------------------------------------------------------------------------------- */
 358 
 359 /**
 360  * Seek into file.
 361  * @note avoids thrashing read cache when possible
 362  *
 363  * @param fs file structure
 364  * @param off offset
 365  * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
 366  *
 367  * @return position in file, starting from beginning
 368  */
 369 
 370 static off_t
 371 dview_fseek (FBUF *fs, off_t off, int whence)
     /* [previous][next][first][last][top][bottom][index][help]  */
 372 {
 373     off_t rv;
 374 
 375     if (fs->len != 0 && whence != SEEK_END)
 376     {
 377         rv = lseek (fs->fd, 0, SEEK_CUR);
 378         if (rv != -1)
 379         {
 380             if (whence == SEEK_CUR)
 381             {
 382                 whence = SEEK_SET;
 383                 off += rv - fs->len + fs->pos;
 384             }
 385             if (off - rv >= -fs->len && off - rv <= 0)
 386             {
 387                 fs->pos = fs->len + off - rv;
 388                 return off;
 389             }
 390         }
 391     }
 392 
 393     rv = lseek (fs->fd, off, whence);
 394     if (rv != -1)
 395         FILE_DIRTY (fs);
 396     return rv;
 397 }
 398 
 399 /* --------------------------------------------------------------------------------------------- */
 400 
 401 /**
 402  * Seek to the beginning of file, thrashing read cache.
 403  *
 404  * @param fs file structure
 405  *
 406  * @return 0 if success, non-zero on error
 407  */
 408 
 409 static off_t
 410 dview_freset (FBUF *fs)
     /* [previous][next][first][last][top][bottom][index][help]  */
 411 {
 412     off_t rv;
 413 
 414     rv = lseek (fs->fd, 0, SEEK_SET);
 415     if (rv != -1)
 416         FILE_DIRTY (fs);
 417     return rv;
 418 }
 419 
 420 /* --------------------------------------------------------------------------------------------- */
 421 
 422 /**
 423  * Write bytes to file.
 424  * @note thrashes read cache
 425  *
 426  * @param fs file structure
 427  * @param buf source buffer
 428  * @param size size of buffer
 429  *
 430  * @return number of written bytes, -1 on error
 431  */
 432 
 433 static ssize_t
 434 dview_fwrite (FBUF *fs, const char *buf, size_t size)
     /* [previous][next][first][last][top][bottom][index][help]  */
 435 {
 436     ssize_t rv;
 437 
 438     rv = write (fs->fd, buf, size);
 439     if (rv >= 0)
 440         FILE_DIRTY (fs);
 441     return rv;
 442 }
 443 
 444 /* --------------------------------------------------------------------------------------------- */
 445 
 446 /**
 447  * Truncate file to the current position.
 448  * @note thrashes read cache
 449  *
 450  * @param fs file structure
 451  *
 452  * @return current file size on success, negative on error
 453  */
 454 
 455 static off_t
 456 dview_ftrunc (FBUF *fs)
     /* [previous][next][first][last][top][bottom][index][help]  */
 457 {
 458     off_t off;
 459 
 460     off = lseek (fs->fd, 0, SEEK_CUR);
 461     if (off != -1)
 462     {
 463         int rv;
 464 
 465         rv = ftruncate (fs->fd, off);
 466         if (rv != 0)
 467             off = -1;
 468         else
 469             FILE_DIRTY (fs);
 470     }
 471     return off;
 472 }
 473 
 474 /* --------------------------------------------------------------------------------------------- */
 475 
 476 /**
 477  * Close file.
 478  * @note if this is temporary file, it is deleted
 479  *
 480  * @param fs file structure
 481  * @return 0 on success, non-zero on error
 482  */
 483 
 484 static int
 485 dview_fclose (FBUF *fs)
     /* [previous][next][first][last][top][bottom][index][help]  */
 486 {
 487     int rv = -1;
 488 
 489     if (fs != NULL)
 490     {
 491         rv = close (fs->fd);
 492         dview_ffree (fs);
 493     }
 494 
 495     return rv;
 496 }
 497 
 498 /* --------------------------------------------------------------------------------------------- */
 499 
 500 /**
 501  * Create pipe stream to process.
 502  *
 503  * @param cmd shell command line
 504  * @param flags open mode, either O_RDONLY or O_WRONLY
 505  *
 506  * @return file structure
 507  */
 508 
 509 static FBUF *
 510 dview_popen (const char *cmd, int flags)
     /* [previous][next][first][last][top][bottom][index][help]  */
 511 {
 512     FILE *f;
 513     FBUF *fs;
 514     const char *type = NULL;
 515 
 516     if (flags == O_RDONLY)
 517         type = "r";
 518     else if (flags == O_WRONLY)
 519         type = "w";
 520 
 521     if (type == NULL)
 522         return NULL;
 523 
 524     fs = dview_fdopen (0);
 525     if (fs == NULL)
 526         return NULL;
 527 
 528     f = popen (cmd, type);
 529     if (f == NULL)
 530     {
 531         dview_ffree (fs);
 532         return NULL;
 533     }
 534 
 535     fs->fd = fileno (f);
 536     fs->data = f;
 537     return fs;
 538 }
 539 
 540 /* --------------------------------------------------------------------------------------------- */
 541 
 542 /**
 543  * Close pipe stream.
 544  *
 545  * @param fs structure
 546  * @return 0 on success, non-zero on error
 547  */
 548 
 549 static int
 550 dview_pclose (FBUF *fs)
     /* [previous][next][first][last][top][bottom][index][help]  */
 551 {
 552     int rv = -1;
 553 
 554     if (fs != NULL)
 555     {
 556         rv = pclose (fs->data);
 557         dview_ffree (fs);
 558     }
 559 
 560     return rv;
 561 }
 562 
 563 /* --------------------------------------------------------------------------------------------- */
 564 
 565 /**
 566  * Get one char (byte) from string
 567  *
 568  * @param str ...
 569  * @param ch ...
 570  * @return TRUE on success, FALSE otherwise
 571  */
 572 
 573 static gboolean
 574 dview_get_byte (const char *str, int *ch)
     /* [previous][next][first][last][top][bottom][index][help]  */
 575 {
 576     if (str == NULL)
 577         return FALSE;
 578 
 579     *ch = (unsigned char) (*str);
 580     return TRUE;
 581 }
 582 
 583 /* --------------------------------------------------------------------------------------------- */
 584 
 585 #ifdef HAVE_CHARSET
 586 /**
 587  * Get utf multibyte char from string
 588  *
 589  * @param str ...
 590  * @param ch ...
 591  * @param ch_length ...
 592  * @return TRUE on success, FALSE otherwise
 593  */
 594 
 595 static gboolean
 596 dview_get_utf (const char *str, int *ch, int *ch_length)
     /* [previous][next][first][last][top][bottom][index][help]  */
 597 {
 598     if (str == NULL)
 599         return FALSE;
 600 
 601     *ch = g_utf8_get_char_validated (str, -1);
 602 
 603     if (*ch < 0)
 604     {
 605         *ch = (unsigned char) (*str);
 606         *ch_length = 1;
 607     }
 608     else
 609     {
 610         const char *next_ch;
 611 
 612         /* Calculate UTF-8 char length */
 613         next_ch = g_utf8_next_char (str);
 614         *ch_length = next_ch - str;
 615     }
 616 
 617     return TRUE;
 618 }
 619 
 620 /* --------------------------------------------------------------------------------------------- */
 621 
 622 static int
 623 dview_str_utf8_offset_to_pos (const char *text, size_t length)
     /* [previous][next][first][last][top][bottom][index][help]  */
 624 {
 625     ptrdiff_t result;
 626 
 627     if (text == NULL || text[0] == '\0')
 628         return length;
 629 
 630     if (g_utf8_validate (text, -1, NULL))
 631         result = g_utf8_offset_to_pointer (text, length) - text;
 632     else
 633     {
 634         gunichar uni;
 635         char *tmpbuf, *buffer;
 636 
 637         buffer = tmpbuf = g_strdup (text);
 638         while (tmpbuf[0] != '\0')
 639         {
 640             uni = g_utf8_get_char_validated (tmpbuf, -1);
 641             if ((uni != (gunichar) (-1)) && (uni != (gunichar) (-2)))
 642                 tmpbuf = g_utf8_next_char (tmpbuf);
 643             else
 644             {
 645                 tmpbuf[0] = '.';
 646                 tmpbuf++;
 647             }
 648         }
 649         result = g_utf8_offset_to_pointer (tmpbuf, length) - tmpbuf;
 650         g_free (buffer);
 651     }
 652     return MAX (length, (size_t) result);
 653 }
 654 #endif /*HAVE_CHARSET */
 655 
 656 /* --------------------------------------------------------------------------------------------- */
 657 
 658 /* diff parse *************************************************************** */
 659 
 660 /**
 661  * Read decimal number from string.
 662  *
 663  * @param[in,out] str string to parse
 664  * @param[out] n extracted number
 665  * @return 0 if success, otherwise non-zero
 666  */
 667 
 668 static int
 669 scan_deci (const char **str, int *n)
     /* [previous][next][first][last][top][bottom][index][help]  */
 670 {
 671     const char *p = *str;
 672     char *q;
 673 
 674     errno = 0;
 675     *n = strtol (p, &q, 10);
 676     if (errno != 0 || p == q)
 677         return -1;
 678     *str = q;
 679     return 0;
 680 }
 681 
 682 /* --------------------------------------------------------------------------------------------- */
 683 
 684 /**
 685  * Parse line for diff statement.
 686  *
 687  * @param p string to parse
 688  * @param ops list of diff statements
 689  * @return 0 if success, otherwise non-zero
 690  */
 691 
 692 static int
 693 scan_line (const char *p, GArray *ops)
     /* [previous][next][first][last][top][bottom][index][help]  */
 694 {
 695     DIFFCMD op;
 696 
 697     int f1, f2;
 698     int t1, t2;
 699     int cmd;
 700     gboolean range = FALSE;
 701 
 702     /* handle the following cases:
 703      *  NUMaNUM[,NUM]
 704      *  NUM[,NUM]cNUM[,NUM]
 705      *  NUM[,NUM]dNUM
 706      * where NUM is a positive integer
 707      */
 708 
 709     if (scan_deci (&p, &f1) != 0 || f1 < 0)
 710         return -1;
 711 
 712     f2 = f1;
 713     if (*p == ',')
 714     {
 715         p++;
 716         if (scan_deci (&p, &f2) != 0 || f2 < f1)
 717             return -1;
 718 
 719         range = TRUE;
 720     }
 721 
 722     cmd = *p++;
 723     if (cmd == 'a')
 724     {
 725         if (range)
 726             return -1;
 727     }
 728     else if (cmd != 'c' && cmd != 'd')
 729         return -1;
 730 
 731     if (scan_deci (&p, &t1) != 0 || t1 < 0)
 732         return -1;
 733 
 734     t2 = t1;
 735     range = FALSE;
 736     if (*p == ',')
 737     {
 738         p++;
 739         if (scan_deci (&p, &t2) != 0 || t2 < t1)
 740             return -1;
 741 
 742         range = TRUE;
 743     }
 744 
 745     if (cmd == 'd' && range)
 746         return -1;
 747 
 748     op.a[0][0] = f1;
 749     op.a[0][1] = f2;
 750     op.cmd = cmd;
 751     op.a[1][0] = t1;
 752     op.a[1][1] = t2;
 753     g_array_append_val (ops, op);
 754     return 0;
 755 }
 756 
 757 /* --------------------------------------------------------------------------------------------- */
 758 
 759 /**
 760  * Parse diff output and extract diff statements.
 761  *
 762  * @param f stream to read from
 763  * @param ops list of diff statements to fill
 764  * @return positive number indicating number of hunks, otherwise negative
 765  */
 766 
 767 static int
 768 scan_diff (FBUF *f, GArray *ops)
     /* [previous][next][first][last][top][bottom][index][help]  */
 769 {
 770     int sz;
 771     char buf[BUFSIZ];
 772 
 773     while ((sz = dview_fgets (buf, sizeof (buf) - 1, f)) != 0)
 774     {
 775         if (isdigit (buf[0]))
 776         {
 777             if (buf[sz - 1] != '\n')
 778                 return -1;
 779 
 780             buf[sz] = '\0';
 781             if (scan_line (buf, ops) != 0)
 782                 return -1;
 783         }
 784         else
 785             while (buf[sz - 1] != '\n' && (sz = dview_fgets (buf, sizeof (buf), f)) != 0)
 786                 ;
 787     }
 788 
 789     return ops->len;
 790 }
 791 
 792 /* --------------------------------------------------------------------------------------------- */
 793 
 794 /**
 795  * Invoke diff and extract diff statements.
 796  *
 797  * @param args extra arguments to be passed to diff
 798  * @param extra more arguments to be passed to diff
 799  * @param file1 first file to compare
 800  * @param file2 second file to compare
 801  * @param ops list of diff statements to fill
 802  *
 803  * @return positive number indicating number of hunks, otherwise negative
 804  */
 805 
 806 static int
 807 dff_execute (const char *args, const char *extra, const char *file1, const char *file2, GArray *ops)
     /* [previous][next][first][last][top][bottom][index][help]  */
 808 {
 809     static const char *opt =
 810         " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
 811         " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
 812         " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
 813         " --unchanged-group-format=''";
 814 
 815     int rv;
 816     FBUF *f;
 817     char *cmd;
 818     int code;
 819     char *file1_esc, *file2_esc;
 820 
 821     /* escape potential $ to avoid shell variable substitutions in popen() */
 822     file1_esc = str_shell_escape (file1);
 823     file2_esc = str_shell_escape (file2);
 824     cmd = g_strdup_printf ("diff %s %s %s %s %s", args, extra, opt, file1_esc, file2_esc);
 825     g_free (file1_esc);
 826     g_free (file2_esc);
 827 
 828     if (cmd == NULL)
 829         return -1;
 830 
 831     f = dview_popen (cmd, O_RDONLY);
 832     g_free (cmd);
 833 
 834     if (f == NULL)
 835         return -1;
 836 
 837     rv = scan_diff (f, ops);
 838     code = dview_pclose (f);
 839 
 840     if (rv < 0 || code == -1 || !WIFEXITED (code) || WEXITSTATUS (code) == 2)
 841         rv = -1;
 842 
 843     return rv;
 844 }
 845 
 846 /* --------------------------------------------------------------------------------------------- */
 847 
 848 static gboolean
 849 printer_for (char ch, DFUNC printer, void *ctx, FBUF *f, int *line, off_t *off)
     /* [previous][next][first][last][top][bottom][index][help]  */
 850 {
 851     size_t sz;
 852     char buf[BUFSIZ];
 853 
 854     sz = dview_fgets (buf, sizeof (buf), f);
 855     if (sz == 0)
 856         return FALSE;
 857 
 858     (*line)++;
 859     printer (ctx, ch, *line, *off, sz, buf);
 860     *off += sz;
 861 
 862     while (buf[sz - 1] != '\n')
 863     {
 864         sz = dview_fgets (buf, sizeof (buf), f);
 865         if (sz == 0)
 866         {
 867             printer (ctx, 0, 0, 0, 1, "\n");
 868             break;
 869         }
 870 
 871         printer (ctx, 0, 0, 0, sz, buf);
 872         *off += sz;
 873     }
 874 
 875     return TRUE;
 876 }
 877 
 878 /* --------------------------------------------------------------------------------------------- */
 879 
 880 /**
 881  * Reparse and display file according to diff statements.
 882  *
 883  * @param ord DIFF_LEFT if 1st file is displayed , DIFF_RIGHT if 2nd file is displayed.
 884  * @param filename file name to display
 885  * @param ops list of diff statements
 886  * @param printer printf-like function to be used for displaying
 887  * @param ctx printer context
 888  *
 889  * @return 0 if success, otherwise non-zero
 890  */
 891 
 892 static int
 893 dff_reparse (diff_place_t ord, const char *filename, const GArray *ops, DFUNC printer, void *ctx)
     /* [previous][next][first][last][top][bottom][index][help]  */
 894 {
 895     size_t i;
 896     FBUF *f;
 897     int line = 0;
 898     off_t off = 0;
 899     const DIFFCMD *op;
 900     diff_place_t eff;
 901     int add_cmd, del_cmd;
 902 
 903     f = dview_fopen (filename, O_RDONLY);
 904     if (f == NULL)
 905         return -1;
 906 
 907     if (ord != DIFF_LEFT)
 908         ord = DIFF_RIGHT;
 909     eff = ord;
 910 
 911     if (ord != DIFF_LEFT)
 912     {
 913         add_cmd = 'd';
 914         del_cmd = 'a';
 915     }
 916     else
 917     {
 918         add_cmd = 'a';
 919         del_cmd = 'd';
 920     }
 921 #define F1 a[eff][0]
 922 #define F2 a[eff][1]
 923 #define T1 a[ ord^1 ][0]
 924 #define T2 a[ ord^1 ][1]
 925     for (i = 0; i < ops->len; i++)
 926     {
 927         int n;
 928 
 929         op = &g_array_index (ops, DIFFCMD, i);
 930 
 931         n = op->F1;
 932         if (op->cmd != add_cmd)
 933             n--;
 934 
 935         while (line < n && printer_for (EQU_CH, printer, ctx, f, &line, &off))
 936             ;
 937 
 938         if (line != n)
 939             goto err;
 940 
 941         if (op->cmd == add_cmd)
 942             for (n = op->T2 - op->T1 + 1; n != 0; n--)
 943                 printer (ctx, DEL_CH, 0, 0, 1, "\n");
 944 
 945         if (op->cmd == del_cmd)
 946         {
 947             for (n = op->F2 - op->F1 + 1;
 948                  n != 0 && printer_for (ADD_CH, printer, ctx, f, &line, &off); n--)
 949                 ;
 950 
 951             if (n != 0)
 952                 goto err;
 953         }
 954 
 955         if (op->cmd == 'c')
 956         {
 957             for (n = op->F2 - op->F1 + 1;
 958                  n != 0 && printer_for (CHG_CH, printer, ctx, f, &line, &off); n--)
 959                 ;
 960 
 961             if (n != 0)
 962                 goto err;
 963 
 964             for (n = op->T2 - op->T1 - (op->F2 - op->F1); n > 0; n--)
 965                 printer (ctx, CHG_CH, 0, 0, 1, "\n");
 966         }
 967     }
 968 #undef T2
 969 #undef T1
 970 #undef F2
 971 #undef F1
 972 
 973     while (printer_for (EQU_CH, printer, ctx, f, &line, &off))
 974         ;
 975 
 976     dview_fclose (f);
 977     return 0;
 978 
 979   err:
 980     dview_fclose (f);
 981     return -1;
 982 }
 983 
 984 /* --------------------------------------------------------------------------------------------- */
 985 
 986 /* horizontal diff ********************************************************** */
 987 
 988 /**
 989  * Longest common substring.
 990  *
 991  * @param s first string
 992  * @param m length of first string
 993  * @param t second string
 994  * @param n length of second string
 995  * @param ret list of offsets for longest common substrings inside each string
 996  * @param min minimum length of common substrings
 997  *
 998  * @return 0 if success, nonzero otherwise
 999  */
1000 
1001 static int
1002 lcsubstr (const char *s, int m, const char *t, int n, GArray *ret, int min)
     /* [previous][next][first][last][top][bottom][index][help]  */
1003 {
1004     int i, j;
1005     int *Lprev, *Lcurr;
1006     int z = 0;
1007 
1008     if (m < min || n < min)
1009     {
1010         /* XXX early culling */
1011         return 0;
1012     }
1013 
1014     Lprev = g_try_new0 (int, n + 1);
1015     if (Lprev == NULL)
1016         return -1;
1017 
1018     Lcurr = g_try_new0 (int, n + 1);
1019     if (Lcurr == NULL)
1020     {
1021         g_free (Lprev);
1022         return -1;
1023     }
1024 
1025     for (i = 0; i < m; i++)
1026     {
1027         int *L;
1028 
1029         L = Lprev;
1030         Lprev = Lcurr;
1031         Lcurr = L;
1032 #ifdef USE_MEMSET_IN_LCS
1033         memset (Lcurr, 0, (n + 1) * sizeof (*Lcurr));
1034 #endif
1035         for (j = 0; j < n; j++)
1036         {
1037 #ifndef USE_MEMSET_IN_LCS
1038             Lcurr[j + 1] = 0;
1039 #endif
1040             if (s[i] == t[j])
1041             {
1042                 int v;
1043 
1044                 v = Lprev[j] + 1;
1045                 Lcurr[j + 1] = v;
1046                 if (z < v)
1047                 {
1048                     z = v;
1049                     g_array_set_size (ret, 0);
1050                 }
1051                 if (z == v && z >= min)
1052                 {
1053                     int off0, off1;
1054                     size_t k;
1055 
1056                     off0 = i - z + 1;
1057                     off1 = j - z + 1;
1058 
1059                     for (k = 0; k < ret->len; k++)
1060                     {
1061                         PAIR *p = (PAIR *) g_array_index (ret, PAIR, k);
1062                         if ((*p)[0] == off0 || (*p)[1] >= off1)
1063                             break;
1064                     }
1065                     if (k == ret->len)
1066                     {
1067                         PAIR p2;
1068 
1069                         p2[0] = off0;
1070                         p2[1] = off1;
1071                         g_array_append_val (ret, p2);
1072                     }
1073                 }
1074             }
1075         }
1076     }
1077 
1078     g_free (Lcurr);
1079     g_free (Lprev);
1080     return z;
1081 }
1082 
1083 /* --------------------------------------------------------------------------------------------- */
1084 
1085 /**
1086  * Scan recursively for common substrings and build ranges.
1087  *
1088  * @param s first string
1089  * @param t second string
1090  * @param bracket current limits for both of the strings
1091  * @param min minimum length of common substrings
1092  * @param hdiff list of horizontal diff ranges to fill
1093  * @param depth recursion depth
1094  *
1095  * @return 0 if success, nonzero otherwise
1096  */
1097 
1098 static gboolean
1099 hdiff_multi (const char *s, const char *t, const BRACKET bracket, int min, GArray *hdiff,
     /* [previous][next][first][last][top][bottom][index][help]  */
1100              unsigned int depth)
1101 {
1102     BRACKET p;
1103 
1104     if (depth-- != 0)
1105     {
1106         GArray *ret;
1107         BRACKET b;
1108         int len;
1109 
1110         ret = g_array_new (FALSE, TRUE, sizeof (PAIR));
1111 
1112         len = lcsubstr (s + bracket[DIFF_LEFT].off, bracket[DIFF_LEFT].len,
1113                         t + bracket[DIFF_RIGHT].off, bracket[DIFF_RIGHT].len, ret, min);
1114         if (ret->len != 0)
1115         {
1116             size_t k = 0;
1117             const PAIR *data = (const PAIR *) &g_array_index (ret, PAIR, 0);
1118             const PAIR *data2;
1119 
1120             b[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1121             b[DIFF_LEFT].len = (*data)[0];
1122             b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1123             b[DIFF_RIGHT].len = (*data)[1];
1124             if (!hdiff_multi (s, t, b, min, hdiff, depth))
1125                 return FALSE;
1126 
1127             for (k = 0; k < ret->len - 1; k++)
1128             {
1129                 data = (const PAIR *) &g_array_index (ret, PAIR, k);
1130                 data2 = (const PAIR *) &g_array_index (ret, PAIR, k + 1);
1131                 b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1132                 b[DIFF_LEFT].len = (*data2)[0] - (*data)[0] - len;
1133                 b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1134                 b[DIFF_RIGHT].len = (*data2)[1] - (*data)[1] - len;
1135                 if (!hdiff_multi (s, t, b, min, hdiff, depth))
1136                     return FALSE;
1137             }
1138             data = (const PAIR *) &g_array_index (ret, PAIR, k);
1139             b[DIFF_LEFT].off = bracket[DIFF_LEFT].off + (*data)[0] + len;
1140             b[DIFF_LEFT].len = bracket[DIFF_LEFT].len - (*data)[0] - len;
1141             b[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off + (*data)[1] + len;
1142             b[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len - (*data)[1] - len;
1143             if (!hdiff_multi (s, t, b, min, hdiff, depth))
1144                 return FALSE;
1145 
1146             g_array_free (ret, TRUE);
1147             return TRUE;
1148         }
1149     }
1150 
1151     p[DIFF_LEFT].off = bracket[DIFF_LEFT].off;
1152     p[DIFF_LEFT].len = bracket[DIFF_LEFT].len;
1153     p[DIFF_RIGHT].off = bracket[DIFF_RIGHT].off;
1154     p[DIFF_RIGHT].len = bracket[DIFF_RIGHT].len;
1155     g_array_append_val (hdiff, p);
1156 
1157     return TRUE;
1158 }
1159 
1160 /* --------------------------------------------------------------------------------------------- */
1161 
1162 /**
1163  * Build list of horizontal diff ranges.
1164  *
1165  * @param s first string
1166  * @param m length of first string
1167  * @param t second string
1168  * @param n length of second string
1169  * @param min minimum length of common substrings
1170  * @param hdiff list of horizontal diff ranges to fill
1171  * @param depth recursion depth
1172  *
1173  * @return 0 if success, nonzero otherwise
1174  */
1175 
1176 static gboolean
1177 hdiff_scan (const char *s, int m, const char *t, int n, int min, GArray *hdiff, unsigned int depth)
     /* [previous][next][first][last][top][bottom][index][help]  */
1178 {
1179     int i;
1180     BRACKET b;
1181 
1182     /* dumbscan (single horizontal diff) -- does not compress whitespace */
1183     for (i = 0; i < m && i < n && s[i] == t[i]; i++)
1184         ;
1185     for (; m > i && n > i && s[m - 1] == t[n - 1]; m--, n--)
1186         ;
1187 
1188     b[DIFF_LEFT].off = i;
1189     b[DIFF_LEFT].len = m - i;
1190     b[DIFF_RIGHT].off = i;
1191     b[DIFF_RIGHT].len = n - i;
1192 
1193     /* smartscan (multiple horizontal diff) */
1194     return hdiff_multi (s, t, b, min, hdiff, depth);
1195 }
1196 
1197 /* --------------------------------------------------------------------------------------------- */
1198 
1199 /* read line **************************************************************** */
1200 
1201 /**
1202  * Check if character is inside horizontal diff limits.
1203  *
1204  * @param k rank of character inside line
1205  * @param hdiff horizontal diff structure
1206  * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1207  *
1208  * @return TRUE if inside hdiff limits, FALSE otherwise
1209  */
1210 
1211 static gboolean
1212 is_inside (int k, GArray *hdiff, diff_place_t ord)
     /* [previous][next][first][last][top][bottom][index][help]  */
1213 {
1214     size_t i;
1215     BRACKET *b;
1216 
1217     for (i = 0; i < hdiff->len; i++)
1218     {
1219         int start, end;
1220 
1221         b = &g_array_index (hdiff, BRACKET, i);
1222         start = (*b)[ord].off;
1223         end = start + (*b)[ord].len;
1224         if (k >= start && k < end)
1225             return TRUE;
1226     }
1227     return FALSE;
1228 }
1229 
1230 /* --------------------------------------------------------------------------------------------- */
1231 
1232 /**
1233  * Copy 'src' to 'dst' expanding tabs.
1234  * @note The procedure returns when all bytes are consumed from 'src'
1235  *
1236  * @param dst destination buffer
1237  * @param src source buffer
1238  * @param srcsize size of src buffer
1239  * @param base virtual base of this string, needed to calculate tabs
1240  * @param ts tab size
1241  *
1242  * @return new virtual base
1243  */
1244 
1245 static int
1246 cvt_cpy (char *dst, const char *src, size_t srcsize, int base, int ts)
     /* [previous][next][first][last][top][bottom][index][help]  */
1247 {
1248     int i;
1249 
1250     for (i = 0; srcsize != 0; i++, src++, dst++, srcsize--)
1251     {
1252         *dst = *src;
1253         if (*src == '\t')
1254         {
1255             int j;
1256 
1257             j = TAB_SKIP (ts, i + base);
1258             i += j - 1;
1259             fill_by_space (dst, j, FALSE);
1260             dst += j - 1;
1261         }
1262     }
1263     return i + base;
1264 }
1265 
1266 /* --------------------------------------------------------------------------------------------- */
1267 
1268 /**
1269  * Copy 'src' to 'dst' expanding tabs.
1270  *
1271  * @param dst destination buffer
1272  * @param dstsize size of dst buffer
1273  * @param[in,out] _src source buffer
1274  * @param srcsize size of src buffer
1275  * @param base virtual base of this string, needed to calculate tabs
1276  * @param ts tab size
1277  *
1278  * @return new virtual base
1279  *
1280  * @note The procedure returns when all bytes are consumed from 'src'
1281  *       or 'dstsize' bytes are written to 'dst'
1282  * @note Upon return, 'src' points to the first unwritten character in source
1283  */
1284 
1285 static int
1286 cvt_ncpy (char *dst, int dstsize, const char **_src, size_t srcsize, int base, int ts)
     /* [previous][next][first][last][top][bottom][index][help]  */
1287 {
1288     int i;
1289     const char *src = *_src;
1290 
1291     for (i = 0; i < dstsize && srcsize != 0; i++, src++, dst++, srcsize--)
1292     {
1293         *dst = *src;
1294         if (*src == '\t')
1295         {
1296             int j;
1297 
1298             j = TAB_SKIP (ts, i + base);
1299             if (j > dstsize - i)
1300                 j = dstsize - i;
1301             i += j - 1;
1302             fill_by_space (dst, j, FALSE);
1303             dst += j - 1;
1304         }
1305     }
1306     *_src = src;
1307     return i + base;
1308 }
1309 
1310 /* --------------------------------------------------------------------------------------------- */
1311 
1312 /**
1313  * Read line from memory, converting tabs to spaces and padding with spaces.
1314  *
1315  * @param src buffer to read from
1316  * @param srcsize size of src buffer
1317  * @param dst buffer to read to
1318  * @param dstsize size of dst buffer, excluding trailing null
1319  * @param skip number of characters to skip
1320  * @param ts tab size
1321  * @param show_cr show trailing carriage return as ^M
1322  *
1323  * @return negative on error, otherwise number of bytes except padding
1324  */
1325 
1326 static int
1327 cvt_mget (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts,
     /* [previous][next][first][last][top][bottom][index][help]  */
1328           gboolean show_cr)
1329 {
1330     int sz = 0;
1331 
1332     if (src != NULL)
1333     {
1334         int i;
1335         char *tmp = dst;
1336         const int base = 0;
1337 
1338         for (i = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, src++, srcsize--)
1339         {
1340             if (*src == '\t')
1341             {
1342                 int j;
1343 
1344                 j = TAB_SKIP (ts, i + base);
1345                 i += j - 1;
1346                 while (j-- > 0)
1347                 {
1348                     if (skip > 0)
1349                         skip--;
1350                     else if (dstsize != 0)
1351                     {
1352                         dstsize--;
1353                         *dst++ = ' ';
1354                     }
1355                 }
1356             }
1357             else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1358             {
1359                 if (skip == 0 && show_cr)
1360                 {
1361                     if (dstsize > 1)
1362                     {
1363                         dstsize -= 2;
1364                         *dst++ = '^';
1365                         *dst++ = 'M';
1366                     }
1367                     else
1368                     {
1369                         dstsize--;
1370                         *dst++ = '.';
1371                     }
1372                 }
1373                 break;
1374             }
1375             else if (skip > 0)
1376             {
1377 #ifdef HAVE_CHARSET
1378                 int ch = 0;
1379                 int ch_length = 1;
1380 
1381                 (void) dview_get_utf (src, &ch, &ch_length);
1382 
1383                 if (ch_length > 1)
1384                     skip += ch_length - 1;
1385 #endif
1386 
1387                 skip--;
1388             }
1389             else
1390             {
1391                 dstsize--;
1392                 *dst++ = *src;
1393             }
1394         }
1395         sz = dst - tmp;
1396     }
1397 
1398     fill_by_space (dst, dstsize, TRUE);
1399 
1400     return sz;
1401 }
1402 
1403 /* --------------------------------------------------------------------------------------------- */
1404 
1405 /**
1406  * Read line from memory and build attribute array.
1407  *
1408  * @param src buffer to read from
1409  * @param srcsize size of src buffer
1410  * @param dst buffer to read to
1411  * @param dstsize size of dst buffer, excluding trailing null
1412  * @param skip number of characters to skip
1413  * @param ts tab size
1414  * @param show_cr show trailing carriage return as ^M
1415  * @param hdiff horizontal diff structure
1416  * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1417  * @param att buffer of attributes
1418  *
1419  * @return negative on error, otherwise number of bytes except padding
1420  */
1421 
1422 static int
1423 cvt_mgeta (const char *src, size_t srcsize, char *dst, int dstsize, int skip, int ts,
     /* [previous][next][first][last][top][bottom][index][help]  */
1424            gboolean show_cr, GArray *hdiff, diff_place_t ord, char *att)
1425 {
1426     int sz = 0;
1427 
1428     if (src != NULL)
1429     {
1430         int i, k;
1431         char *tmp = dst;
1432         const int base = 0;
1433 
1434         for (i = 0, k = 0; dstsize != 0 && srcsize != 0 && *src != '\n'; i++, k++, src++, srcsize--)
1435         {
1436             if (*src == '\t')
1437             {
1438                 int j;
1439 
1440                 j = TAB_SKIP (ts, i + base);
1441                 i += j - 1;
1442                 while (j-- > 0)
1443                 {
1444                     if (skip != 0)
1445                         skip--;
1446                     else if (dstsize != 0)
1447                     {
1448                         dstsize--;
1449                         *att++ = is_inside (k, hdiff, ord);
1450                         *dst++ = ' ';
1451                     }
1452                 }
1453             }
1454             else if (src[0] == '\r' && (srcsize == 1 || src[1] == '\n'))
1455             {
1456                 if (skip == 0 && show_cr)
1457                 {
1458                     if (dstsize > 1)
1459                     {
1460                         dstsize -= 2;
1461                         *att++ = is_inside (k, hdiff, ord);
1462                         *dst++ = '^';
1463                         *att++ = is_inside (k, hdiff, ord);
1464                         *dst++ = 'M';
1465                     }
1466                     else
1467                     {
1468                         dstsize--;
1469                         *att++ = is_inside (k, hdiff, ord);
1470                         *dst++ = '.';
1471                     }
1472                 }
1473                 break;
1474             }
1475             else if (skip != 0)
1476             {
1477 #ifdef HAVE_CHARSET
1478                 int ch = 0;
1479                 int ch_length = 1;
1480 
1481                 (void) dview_get_utf (src, &ch, &ch_length);
1482                 if (ch_length > 1)
1483                     skip += ch_length - 1;
1484 #endif
1485 
1486                 skip--;
1487             }
1488             else
1489             {
1490                 dstsize--;
1491                 *att++ = is_inside (k, hdiff, ord);
1492                 *dst++ = *src;
1493             }
1494         }
1495         sz = dst - tmp;
1496     }
1497 
1498     memset (att, '\0', dstsize);
1499     fill_by_space (dst, dstsize, TRUE);
1500 
1501     return sz;
1502 }
1503 
1504 /* --------------------------------------------------------------------------------------------- */
1505 
1506 /**
1507  * Read line from file, converting tabs to spaces and padding with spaces.
1508  *
1509  * @param f file stream to read from
1510  * @param off offset of line inside file
1511  * @param dst buffer to read to
1512  * @param dstsize size of dst buffer, excluding trailing null
1513  * @param skip number of characters to skip
1514  * @param ts tab size
1515  * @param show_cr show trailing carriage return as ^M
1516  *
1517  * @return negative on error, otherwise number of bytes except padding
1518  */
1519 
1520 static int
1521 cvt_fget (FBUF *f, off_t off, char *dst, size_t dstsize, int skip, int ts, gboolean show_cr)
     /* [previous][next][first][last][top][bottom][index][help]  */
1522 {
1523     int base = 0;
1524     int old_base = base;
1525     size_t amount = dstsize;
1526     size_t useful, offset;
1527     size_t i;
1528     size_t sz;
1529     int lastch = '\0';
1530     const char *q = NULL;
1531     char tmp[BUFSIZ];           /* XXX capacity must be >= MAX{dstsize + 1, amount} */
1532     char cvt[BUFSIZ];           /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1533 
1534     if (sizeof (tmp) < amount || sizeof (tmp) <= dstsize || sizeof (cvt) < 8 * amount)
1535     {
1536         /* abnormal, but avoid buffer overflow */
1537         fill_by_space (dst, dstsize, TRUE);
1538         return 0;
1539     }
1540 
1541     dview_fseek (f, off, SEEK_SET);
1542 
1543     while (skip > base)
1544     {
1545         old_base = base;
1546         sz = dview_fgets (tmp, amount, f);
1547         if (sz == 0)
1548             break;
1549 
1550         base = cvt_cpy (cvt, tmp, sz, old_base, ts);
1551         if (cvt[base - old_base - 1] == '\n')
1552         {
1553             q = &cvt[base - old_base - 1];
1554             base = old_base + q - cvt + 1;
1555             break;
1556         }
1557     }
1558 
1559     if (base < skip)
1560     {
1561         fill_by_space (dst, dstsize, TRUE);
1562         return 0;
1563     }
1564 
1565     useful = base - skip;
1566     offset = skip - old_base;
1567 
1568     if (useful <= dstsize)
1569     {
1570         if (useful != 0)
1571             memmove (dst, cvt + offset, useful);
1572 
1573         if (q == NULL)
1574         {
1575             sz = dview_fgets (tmp, dstsize - useful + 1, f);
1576             if (sz != 0)
1577             {
1578                 const char *ptr = tmp;
1579 
1580                 useful += cvt_ncpy (dst + useful, dstsize - useful, &ptr, sz, base, ts) - base;
1581                 if (ptr < tmp + sz)
1582                     lastch = *ptr;
1583             }
1584         }
1585         sz = useful;
1586     }
1587     else
1588     {
1589         memmove (dst, cvt + offset, dstsize);
1590         sz = dstsize;
1591         lastch = cvt[offset + dstsize];
1592     }
1593 
1594     dst[sz] = lastch;
1595     for (i = 0; i < sz && dst[i] != '\n'; i++)
1596         if (dst[i] == '\r' && dst[i + 1] == '\n')
1597         {
1598             if (show_cr)
1599             {
1600                 if (i + 1 < dstsize)
1601                 {
1602                     dst[i++] = '^';
1603                     dst[i++] = 'M';
1604                 }
1605                 else
1606                     dst[i++] = '*';
1607             }
1608             break;
1609         }
1610 
1611     fill_by_space (dst, dstsize, TRUE);
1612 
1613     return sz;
1614 }
1615 
1616 /* --------------------------------------------------------------------------------------------- */
1617 /* diff printers et al ****************************************************** */
1618 
1619 static void
1620 cc_free_elt (gpointer elt)
     /* [previous][next][first][last][top][bottom][index][help]  */
1621 {
1622     DIFFLN *p = (DIFFLN *) elt;
1623 
1624     if (p != NULL)
1625         g_free (p->p);
1626 }
1627 
1628 /* --------------------------------------------------------------------------------------------- */
1629 
1630 static int
1631 printer (void *ctx, int ch, int line, off_t off, size_t sz, const char *str)
     /* [previous][next][first][last][top][bottom][index][help]  */
1632 {
1633     GArray *a = ((PRINTER_CTX *) ctx)->a;
1634     DSRC dsrc = ((PRINTER_CTX *) ctx)->dsrc;
1635 
1636     if (ch != 0)
1637     {
1638         DIFFLN p;
1639 
1640         p.p = NULL;
1641         p.ch = ch;
1642         p.line = line;
1643         p.u.off = off;
1644         if (dsrc == DATA_SRC_MEM && line != 0)
1645         {
1646             if (sz != 0 && str[sz - 1] == '\n')
1647                 sz--;
1648             if (sz > 0)
1649                 p.p = g_strndup (str, sz);
1650             p.u.len = sz;
1651         }
1652         g_array_append_val (a, p);
1653     }
1654     else if (dsrc == DATA_SRC_MEM)
1655     {
1656         DIFFLN *p;
1657 
1658         p = &g_array_index (a, DIFFLN, a->len - 1);
1659         if (sz != 0 && str[sz - 1] == '\n')
1660             sz--;
1661         if (sz != 0)
1662         {
1663             size_t new_size;
1664             char *q;
1665 
1666             new_size = p->u.len + sz;
1667             q = g_realloc (p->p, new_size);
1668             memcpy (q + p->u.len, str, sz);
1669             p->p = q;
1670         }
1671         p->u.len += sz;
1672     }
1673     if (dsrc == DATA_SRC_TMP && (line != 0 || ch == 0))
1674     {
1675         FBUF *f = ((PRINTER_CTX *) ctx)->f;
1676         dview_fwrite (f, str, sz);
1677     }
1678     return 0;
1679 }
1680 
1681 /* --------------------------------------------------------------------------------------------- */
1682 
1683 static int
1684 redo_diff (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
1685 {
1686     FBUF *const *f = dview->f;
1687     PRINTER_CTX ctx;
1688     GArray *ops;
1689     int ndiff;
1690     int rv = 0;
1691     char extra[BUF_MEDIUM];
1692 
1693     extra[0] = '\0';
1694     if (dview->opt.quality == 2)
1695         strcat (extra, " -d");
1696     if (dview->opt.quality == 1)
1697         strcat (extra, " --speed-large-files");
1698     if (dview->opt.strip_trailing_cr)
1699         strcat (extra, " --strip-trailing-cr");
1700     if (dview->opt.ignore_tab_expansion)
1701         strcat (extra, " -E");
1702     if (dview->opt.ignore_space_change)
1703         strcat (extra, " -b");
1704     if (dview->opt.ignore_all_space)
1705         strcat (extra, " -w");
1706     if (dview->opt.ignore_case)
1707         strcat (extra, " -i");
1708 
1709     if (dview->dsrc != DATA_SRC_MEM)
1710     {
1711         dview_freset (f[DIFF_LEFT]);
1712         dview_freset (f[DIFF_RIGHT]);
1713     }
1714 
1715     ops = g_array_new (FALSE, FALSE, sizeof (DIFFCMD));
1716     ndiff = dff_execute (dview->args, extra, dview->file[DIFF_LEFT], dview->file[DIFF_RIGHT], ops);
1717     if (ndiff < 0)
1718     {
1719         if (ops != NULL)
1720             g_array_free (ops, TRUE);
1721         return -1;
1722     }
1723 
1724     ctx.dsrc = dview->dsrc;
1725     ctx.a = dview->a[DIFF_LEFT];
1726     ctx.f = f[DIFF_LEFT];
1727     rv |= dff_reparse (DIFF_LEFT, dview->file[DIFF_LEFT], ops, printer, &ctx);
1728 
1729     ctx.a = dview->a[DIFF_RIGHT];
1730     ctx.f = f[DIFF_RIGHT];
1731     rv |= dff_reparse (DIFF_RIGHT, dview->file[DIFF_RIGHT], ops, printer, &ctx);
1732 
1733     if (ops != NULL)
1734         g_array_free (ops, TRUE);
1735 
1736     if (rv != 0 || dview->a[DIFF_LEFT]->len != dview->a[DIFF_RIGHT]->len)
1737         return -1;
1738 
1739     if (dview->dsrc == DATA_SRC_TMP)
1740     {
1741         dview_ftrunc (f[DIFF_LEFT]);
1742         dview_ftrunc (f[DIFF_RIGHT]);
1743     }
1744 
1745     if (dview->dsrc == DATA_SRC_MEM && HDIFF_ENABLE)
1746     {
1747         size_t i;
1748 
1749         dview->hdiff = g_ptr_array_new ();
1750 
1751         for (i = 0; i < dview->a[DIFF_LEFT]->len; i++)
1752         {
1753             GArray *h = NULL;
1754             const DIFFLN *p;
1755             const DIFFLN *q;
1756 
1757             p = &g_array_index (dview->a[DIFF_LEFT], DIFFLN, i);
1758             q = &g_array_index (dview->a[DIFF_RIGHT], DIFFLN, i);
1759             if (p->line != 0 && q->line != 0 && p->ch == CHG_CH)
1760             {
1761                 gboolean runresult;
1762 
1763                 h = g_array_new (FALSE, FALSE, sizeof (BRACKET));
1764 
1765                 runresult =
1766                     hdiff_scan (p->p, p->u.len, q->p, q->u.len, HDIFF_MINCTX, h, HDIFF_DEPTH);
1767                 if (!runresult)
1768                 {
1769                     g_array_free (h, TRUE);
1770                     h = NULL;
1771                 }
1772             }
1773 
1774             g_ptr_array_add (dview->hdiff, h);
1775         }
1776     }
1777     return ndiff;
1778 }
1779 
1780 /* --------------------------------------------------------------------------------------------- */
1781 
1782 static void
1783 destroy_hdiff (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
1784 {
1785     if (dview->hdiff != NULL)
1786     {
1787         int i;
1788         int len;
1789 
1790         len = dview->a[DIFF_LEFT]->len;
1791 
1792         for (i = 0; i < len; i++)
1793         {
1794             GArray *h;
1795 
1796             h = (GArray *) g_ptr_array_index (dview->hdiff, i);
1797             if (h != NULL)
1798                 g_array_free (h, TRUE);
1799         }
1800         g_ptr_array_free (dview->hdiff, TRUE);
1801         dview->hdiff = NULL;
1802     }
1803 
1804     mc_search_free (dview->search.handle);
1805     dview->search.handle = NULL;
1806     MC_PTR_FREE (dview->search.last_string);
1807 }
1808 
1809 /* --------------------------------------------------------------------------------------------- */
1810 /* stuff ******************************************************************** */
1811 
1812 static int
1813 get_digits (unsigned int n)
     /* [previous][next][first][last][top][bottom][index][help]  */
1814 {
1815     int d = 1;
1816 
1817     while ((n /= 10) != 0)
1818         d++;
1819     return d;
1820 }
1821 
1822 /* --------------------------------------------------------------------------------------------- */
1823 
1824 static int
1825 get_line_numbers (const GArray *a, size_t pos, int *linenum, int *lineofs)
     /* [previous][next][first][last][top][bottom][index][help]  */
1826 {
1827     const DIFFLN *p;
1828 
1829     *linenum = 0;
1830     *lineofs = 0;
1831 
1832     if (a->len != 0)
1833     {
1834         if (pos >= a->len)
1835             pos = a->len - 1;
1836 
1837         p = &g_array_index (a, DIFFLN, pos);
1838 
1839         if (p->line == 0)
1840         {
1841             int n;
1842 
1843             for (n = pos; n > 0; n--)
1844             {
1845                 p--;
1846                 if (p->line != 0)
1847                     break;
1848             }
1849             *lineofs = pos - n + 1;
1850         }
1851 
1852         *linenum = p->line;
1853     }
1854     return 0;
1855 }
1856 
1857 /* --------------------------------------------------------------------------------------------- */
1858 
1859 static int
1860 calc_nwidth (const GArray *const *a)
     /* [previous][next][first][last][top][bottom][index][help]  */
1861 {
1862     int l1, o1;
1863     int l2, o2;
1864 
1865     get_line_numbers (a[DIFF_LEFT], a[DIFF_LEFT]->len - 1, &l1, &o1);
1866     get_line_numbers (a[DIFF_RIGHT], a[DIFF_RIGHT]->len - 1, &l2, &o2);
1867     if (l1 < l2)
1868         l1 = l2;
1869     return get_digits (l1);
1870 }
1871 
1872 /* --------------------------------------------------------------------------------------------- */
1873 
1874 static int
1875 find_prev_hunk (const GArray *a, int pos)
     /* [previous][next][first][last][top][bottom][index][help]  */
1876 {
1877 #if 1
1878     for (; pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH; pos--)
1879         ;
1880     for (; pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH; pos--)
1881         ;
1882     for (; pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH; pos--)
1883         ;
1884     if (pos > 0 && (size_t) pos < a->len)
1885         pos++;
1886 #else
1887     for (; pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch == EQU_CH; pos--)
1888         ;
1889     for (; pos > 0 && ((DIFFLN *) & g_array_index (a, DIFFLN, pos - 1))->ch != EQU_CH; pos--)
1890         ;
1891 #endif
1892 
1893     return pos;
1894 }
1895 
1896 /* --------------------------------------------------------------------------------------------- */
1897 
1898 static size_t
1899 find_next_hunk (const GArray *a, size_t pos)
     /* [previous][next][first][last][top][bottom][index][help]  */
1900 {
1901     for (; pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch != EQU_CH; pos++)
1902         ;
1903     for (; pos < a->len && ((DIFFLN *) & g_array_index (a, DIFFLN, pos))->ch == EQU_CH; pos++)
1904         ;
1905     return pos;
1906 }
1907 
1908 /* --------------------------------------------------------------------------------------------- */
1909 /**
1910  * Find start and end lines of the current hunk.
1911  *
1912  * @param dview WDiff widget
1913  * @return boolean and
1914  * start_line1 first line of current hunk (file[0])
1915  * end_line1 last line of current hunk (file[0])
1916  * start_line1 first line of current hunk (file[0])
1917  * end_line1 last line of current hunk (file[0])
1918  */
1919 
1920 static int
1921 get_current_hunk (WDiff *dview, int *start_line1, int *end_line1, int *start_line2, int *end_line2)
     /* [previous][next][first][last][top][bottom][index][help]  */
1922 {
1923     const GArray *a0 = dview->a[DIFF_LEFT];
1924     const GArray *a1 = dview->a[DIFF_RIGHT];
1925     size_t pos;
1926     int ch;
1927     int res = 0;
1928 
1929     /* Is file empty? */
1930     if (a0->len == 0)
1931         return 0;
1932 
1933     *start_line1 = 1;
1934     *start_line2 = 1;
1935     *end_line1 = 1;
1936     *end_line2 = 1;
1937 
1938     pos = dview->skip_rows;
1939     ch = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch;
1940     if (ch != EQU_CH)
1941     {
1942         switch (ch)
1943         {
1944         case ADD_CH:
1945             res = DIFF_DEL;
1946             break;
1947         case DEL_CH:
1948             res = DIFF_ADD;
1949             break;
1950         case CHG_CH:
1951             res = DIFF_CHG;
1952             break;
1953         default:
1954             break;
1955         }
1956 
1957         for (; pos > 0 && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH; pos--)
1958             ;
1959         if (pos > 0)
1960         {
1961             *start_line1 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line + 1;
1962             *start_line2 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line + 1;
1963         }
1964 
1965         for (pos = dview->skip_rows;
1966              pos < a0->len && ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->ch != EQU_CH; pos++)
1967         {
1968             int l0, l1;
1969 
1970             l0 = ((DIFFLN *) & g_array_index (a0, DIFFLN, pos))->line;
1971             l1 = ((DIFFLN *) & g_array_index (a1, DIFFLN, pos))->line;
1972             if (l0 > 0)
1973                 *end_line1 = MAX (*start_line1, l0);
1974             if (l1 > 0)
1975                 *end_line2 = MAX (*start_line2, l1);
1976         }
1977     }
1978     return res;
1979 }
1980 
1981 /* --------------------------------------------------------------------------------------------- */
1982 /**
1983  * Remove hunk from file.
1984  *
1985  * @param dview           WDiff widget
1986  * @param merge_file      file stream for writing data
1987  * @param from1           first line of hunk
1988  * @param to1             last line of hunk
1989  * @param merge_direction in what direction files should be merged
1990  */
1991 
1992 static void
1993 dview_remove_hunk (WDiff *dview, FILE *merge_file, int from1, int to1,
     /* [previous][next][first][last][top][bottom][index][help]  */
1994                    action_direction_t merge_direction)
1995 {
1996     int line;
1997     char buf[BUF_10K];
1998     FILE *f0;
1999 
2000     if (merge_direction == FROM_RIGHT_TO_LEFT)
2001         f0 = fopen (dview->file[DIFF_RIGHT], "r");
2002     else
2003         f0 = fopen (dview->file[DIFF_LEFT], "r");
2004 
2005     for (line = 0; fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1; line++)
2006         fputs (buf, merge_file);
2007 
2008     while (fgets (buf, sizeof (buf), f0) != NULL)
2009     {
2010         line++;
2011         if (line >= to1)
2012             fputs (buf, merge_file);
2013     }
2014     fclose (f0);
2015 }
2016 
2017 /* --------------------------------------------------------------------------------------------- */
2018 /**
2019  * Add hunk to file.
2020  *
2021  * @param dview           WDiff widget
2022  * @param merge_file      file stream for writing data
2023  * @param from1           first line of source hunk
2024  * @param from2           first line of destination hunk
2025  * @param to1             last line of source hunk
2026  * @param merge_direction in what direction files should be merged
2027  */
2028 
2029 static void
2030 dview_add_hunk (WDiff *dview, FILE *merge_file, int from1, int from2, int to2,
     /* [previous][next][first][last][top][bottom][index][help]  */
2031                 action_direction_t merge_direction)
2032 {
2033     int line;
2034     char buf[BUF_10K];
2035     FILE *f0, *f1;
2036 
2037     if (merge_direction == FROM_RIGHT_TO_LEFT)
2038     {
2039         f0 = fopen (dview->file[DIFF_RIGHT], "r");
2040         f1 = fopen (dview->file[DIFF_LEFT], "r");
2041     }
2042     else
2043     {
2044         f0 = fopen (dview->file[DIFF_LEFT], "r");
2045         f1 = fopen (dview->file[DIFF_RIGHT], "r");
2046     }
2047 
2048     for (line = 0; fgets (buf, sizeof (buf), f0) != NULL && line < from1 - 1; line++)
2049         fputs (buf, merge_file);
2050     for (line = 0; fgets (buf, sizeof (buf), f1) != NULL && line <= to2;)
2051     {
2052         line++;
2053         if (line >= from2)
2054             fputs (buf, merge_file);
2055     }
2056     while (fgets (buf, sizeof (buf), f0) != NULL)
2057         fputs (buf, merge_file);
2058 
2059     fclose (f0);
2060     fclose (f1);
2061 }
2062 
2063 /* --------------------------------------------------------------------------------------------- */
2064 /**
2065  * Replace hunk in file.
2066  *
2067  * @param dview           WDiff widget
2068  * @param merge_file      file stream for writing data
2069  * @param from1           first line of source hunk
2070  * @param to1             last line of source hunk
2071  * @param from2           first line of destination hunk
2072  * @param to2             last line of destination hunk
2073  * @param merge_direction in what direction files should be merged
2074  */
2075 
2076 static void
2077 dview_replace_hunk (WDiff *dview, FILE *merge_file, int from1, int to1, int from2, int to2,
     /* [previous][next][first][last][top][bottom][index][help]  */
2078                     action_direction_t merge_direction)
2079 {
2080     int line1, line2;
2081     char buf[BUF_10K];
2082     FILE *f0, *f1;
2083 
2084     if (merge_direction == FROM_RIGHT_TO_LEFT)
2085     {
2086         f0 = fopen (dview->file[DIFF_RIGHT], "r");
2087         f1 = fopen (dview->file[DIFF_LEFT], "r");
2088     }
2089     else
2090     {
2091         f0 = fopen (dview->file[DIFF_LEFT], "r");
2092         f1 = fopen (dview->file[DIFF_RIGHT], "r");
2093     }
2094 
2095     for (line1 = 0; fgets (buf, sizeof (buf), f0) != NULL && line1 < from1 - 1; line1++)
2096         fputs (buf, merge_file);
2097     for (line2 = 0; fgets (buf, sizeof (buf), f1) != NULL && line2 <= to2;)
2098     {
2099         line2++;
2100         if (line2 >= from2)
2101             fputs (buf, merge_file);
2102     }
2103     while (fgets (buf, sizeof (buf), f0) != NULL)
2104     {
2105         line1++;
2106         if (line1 > to1)
2107             fputs (buf, merge_file);
2108     }
2109     fclose (f0);
2110     fclose (f1);
2111 }
2112 
2113 /* --------------------------------------------------------------------------------------------- */
2114 /**
2115  * Merge hunk.
2116  *
2117  * @param dview           WDiff widget
2118  * @param merge_direction in what direction files should be merged
2119  */
2120 
2121 static void
2122 do_merge_hunk (WDiff *dview, action_direction_t merge_direction)
     /* [previous][next][first][last][top][bottom][index][help]  */
2123 {
2124     int from1, to1, from2, to2;
2125     int hunk;
2126     diff_place_t n_merge = (merge_direction == FROM_RIGHT_TO_LEFT) ? DIFF_RIGHT : DIFF_LEFT;
2127 
2128     if (merge_direction == FROM_RIGHT_TO_LEFT)
2129         hunk = get_current_hunk (dview, &from2, &to2, &from1, &to1);
2130     else
2131         hunk = get_current_hunk (dview, &from1, &to1, &from2, &to2);
2132 
2133     if (hunk > 0)
2134     {
2135         int merge_file_fd;
2136         FILE *merge_file;
2137         vfs_path_t *merge_file_name_vpath = NULL;
2138 
2139         if (!dview->merged[n_merge])
2140         {
2141             dview->merged[n_merge] = mc_util_make_backup_if_possible (dview->file[n_merge], "~~~");
2142             if (!dview->merged[n_merge])
2143             {
2144                 message (D_ERROR, MSG_ERROR,
2145                          _("Cannot create backup file\n%s%s\n%s"),
2146                          dview->file[n_merge], "~~~", unix_error_string (errno));
2147                 return;
2148             }
2149         }
2150 
2151         merge_file_fd = mc_mkstemps (&merge_file_name_vpath, "mcmerge", NULL);
2152         if (merge_file_fd == -1)
2153         {
2154             message (D_ERROR, MSG_ERROR, _("Cannot create temporary merge file\n%s"),
2155                      unix_error_string (errno));
2156             return;
2157         }
2158 
2159         merge_file = fdopen (merge_file_fd, "w");
2160 
2161         switch (hunk)
2162         {
2163         case DIFF_DEL:
2164             if (merge_direction == FROM_RIGHT_TO_LEFT)
2165                 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_RIGHT_TO_LEFT);
2166             else
2167                 dview_remove_hunk (dview, merge_file, from1, to1, FROM_LEFT_TO_RIGHT);
2168             break;
2169         case DIFF_ADD:
2170             if (merge_direction == FROM_RIGHT_TO_LEFT)
2171                 dview_remove_hunk (dview, merge_file, from1, to1, FROM_RIGHT_TO_LEFT);
2172             else
2173                 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_LEFT_TO_RIGHT);
2174             break;
2175         case DIFF_CHG:
2176             dview_replace_hunk (dview, merge_file, from1, to1, from2, to2, merge_direction);
2177             break;
2178         default:
2179             break;
2180         }
2181         fflush (merge_file);
2182         fclose (merge_file);
2183         {
2184             int res;
2185 
2186             res = rewrite_backup_content (merge_file_name_vpath, dview->file[n_merge]);
2187             (void) res;
2188         }
2189         mc_unlink (merge_file_name_vpath);
2190         vfs_path_free (merge_file_name_vpath, TRUE);
2191     }
2192 }
2193 
2194 /* --------------------------------------------------------------------------------------------- */
2195 /* view routines and callbacks ********************************************** */
2196 
2197 static void
2198 dview_compute_split (WDiff *dview, int i)
     /* [previous][next][first][last][top][bottom][index][help]  */
2199 {
2200     dview->bias += i;
2201     if (dview->bias < 2 - dview->half1)
2202         dview->bias = 2 - dview->half1;
2203     if (dview->bias > dview->half2 - 2)
2204         dview->bias = dview->half2 - 2;
2205 }
2206 
2207 /* --------------------------------------------------------------------------------------------- */
2208 
2209 static void
2210 dview_compute_areas (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2211 {
2212     Widget *w = WIDGET (dview);
2213 
2214     dview->height = w->rect.lines - 1;
2215     dview->half1 = w->rect.cols / 2;
2216     dview->half2 = w->rect.cols - dview->half1;
2217 
2218     dview_compute_split (dview, 0);
2219 }
2220 
2221 /* --------------------------------------------------------------------------------------------- */
2222 
2223 static void
2224 dview_reread (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2225 {
2226     int ndiff;
2227 
2228     destroy_hdiff (dview);
2229     if (dview->a[DIFF_LEFT] != NULL)
2230         g_array_free (dview->a[DIFF_LEFT], TRUE);
2231     if (dview->a[DIFF_RIGHT] != NULL)
2232         g_array_free (dview->a[DIFF_RIGHT], TRUE);
2233 
2234     dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2235     g_array_set_clear_func (dview->a[DIFF_LEFT], cc_free_elt);
2236     dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2237     g_array_set_clear_func (dview->a[DIFF_RIGHT], cc_free_elt);
2238 
2239     ndiff = redo_diff (dview);
2240     if (ndiff >= 0)
2241         dview->ndiff = ndiff;
2242 }
2243 
2244 /* --------------------------------------------------------------------------------------------- */
2245 
2246 #ifdef HAVE_CHARSET
2247 static void
2248 dview_set_codeset (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2249 {
2250     const char *encoding_id = NULL;
2251 
2252     dview->utf8 = TRUE;
2253     encoding_id =
2254         get_codepage_id (mc_global.source_codepage >=
2255                          0 ? mc_global.source_codepage : mc_global.display_codepage);
2256     if (encoding_id != NULL)
2257     {
2258         GIConv conv;
2259 
2260         conv = str_crt_conv_from (encoding_id);
2261         if (conv != INVALID_CONV)
2262         {
2263             if (dview->converter != str_cnv_from_term)
2264                 str_close_conv (dview->converter);
2265             dview->converter = conv;
2266         }
2267         dview->utf8 = (gboolean) str_isutf8 (encoding_id);
2268     }
2269 }
2270 
2271 /* --------------------------------------------------------------------------------------------- */
2272 
2273 static void
2274 dview_select_encoding (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2275 {
2276     if (do_select_codepage ())
2277         dview_set_codeset (dview);
2278     dview_reread (dview);
2279     tty_touch_screen ();
2280     repaint_screen ();
2281 }
2282 #endif /* HAVE_CHARSET */
2283 
2284 /* --------------------------------------------------------------------------------------------- */
2285 
2286 static void
2287 dview_load_options (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2288 {
2289     gboolean show_numbers;
2290     int tab_size;
2291 
2292     dview->display_symbols =
2293         mc_config_get_bool (mc_global.main_config, "DiffView", "show_symbols", FALSE);
2294     show_numbers = mc_config_get_bool (mc_global.main_config, "DiffView", "show_numbers", FALSE);
2295     if (show_numbers)
2296         dview->display_numbers = 1;
2297     tab_size = mc_config_get_int (mc_global.main_config, "DiffView", "tab_size", 8);
2298     if (tab_size > 0 && tab_size < 9)
2299         dview->tab_size = tab_size;
2300     else
2301         dview->tab_size = 8;
2302 
2303     dview->opt.quality = mc_config_get_int (mc_global.main_config, "DiffView", "diff_quality", 0);
2304 
2305     dview->opt.strip_trailing_cr =
2306         mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_tws", FALSE);
2307     dview->opt.ignore_all_space =
2308         mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_all_space", FALSE);
2309     dview->opt.ignore_space_change =
2310         mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_space_change", FALSE);
2311     dview->opt.ignore_tab_expansion =
2312         mc_config_get_bool (mc_global.main_config, "DiffView", "diff_tab_expansion", FALSE);
2313     dview->opt.ignore_case =
2314         mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_case", FALSE);
2315 
2316     dview->new_frame = TRUE;
2317 }
2318 
2319 /* --------------------------------------------------------------------------------------------- */
2320 
2321 static void
2322 dview_save_options (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2323 {
2324     mc_config_set_bool (mc_global.main_config, "DiffView", "show_symbols", dview->display_symbols);
2325     mc_config_set_bool (mc_global.main_config, "DiffView", "show_numbers",
2326                         dview->display_numbers != 0);
2327     mc_config_set_int (mc_global.main_config, "DiffView", "tab_size", dview->tab_size);
2328 
2329     mc_config_set_int (mc_global.main_config, "DiffView", "diff_quality", dview->opt.quality);
2330 
2331     mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_tws",
2332                         dview->opt.strip_trailing_cr);
2333     mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_all_space",
2334                         dview->opt.ignore_all_space);
2335     mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_space_change",
2336                         dview->opt.ignore_space_change);
2337     mc_config_set_bool (mc_global.main_config, "DiffView", "diff_tab_expansion",
2338                         dview->opt.ignore_tab_expansion);
2339     mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_case",
2340                         dview->opt.ignore_case);
2341 }
2342 
2343 /* --------------------------------------------------------------------------------------------- */
2344 
2345 static void
2346 dview_diff_options (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2347 {
2348     const char *quality_str[] = {
2349         N_("No&rmal"),
2350         N_("&Fastest (Assume large files)"),
2351         N_("&Minimal (Find a smaller set of change)")
2352     };
2353 
2354     quick_widget_t quick_widgets[] = {
2355         /* *INDENT-OFF* */
2356         QUICK_START_GROUPBOX (N_("Diff algorithm")),
2357             QUICK_RADIO (3, (const char **) quality_str, (int *) &dview->opt.quality, NULL),
2358         QUICK_STOP_GROUPBOX,
2359         QUICK_START_GROUPBOX (N_("Diff extra options")),
2360             QUICK_CHECKBOX (N_("&Ignore case"), &dview->opt.ignore_case, NULL),
2361             QUICK_CHECKBOX (N_("Ignore tab &expansion"), &dview->opt.ignore_tab_expansion, NULL),
2362             QUICK_CHECKBOX (N_("Ignore &space change"), &dview->opt.ignore_space_change, NULL),
2363             QUICK_CHECKBOX (N_("Ignore all &whitespace"), &dview->opt.ignore_all_space, NULL),
2364             QUICK_CHECKBOX (N_("Strip &trailing carriage return"), &dview->opt.strip_trailing_cr,
2365                             NULL),
2366         QUICK_STOP_GROUPBOX,
2367         QUICK_BUTTONS_OK_CANCEL,
2368         QUICK_END
2369         /* *INDENT-ON* */
2370     };
2371 
2372     WRect r = { -1, -1, 0, 56 };
2373 
2374     quick_dialog_t qdlg = {
2375         r, N_("Diff Options"), "[Diff Options]",
2376         quick_widgets, NULL, NULL
2377     };
2378 
2379     if (quick_dialog (&qdlg) != B_CANCEL)
2380         dview_reread (dview);
2381 }
2382 
2383 /* --------------------------------------------------------------------------------------------- */
2384 
2385 static int
2386 dview_init (WDiff *dview, const char *args, const char *file1, const char *file2,
     /* [previous][next][first][last][top][bottom][index][help]  */
2387             const char *label1, const char *label2, DSRC dsrc)
2388 {
2389     FBUF *f[DIFF_COUNT];
2390 
2391     f[DIFF_LEFT] = NULL;
2392     f[DIFF_RIGHT] = NULL;
2393 
2394     if (dsrc == DATA_SRC_TMP)
2395     {
2396         f[DIFF_LEFT] = dview_ftemp ();
2397         if (f[DIFF_LEFT] == NULL)
2398             return -1;
2399 
2400         f[DIFF_RIGHT] = dview_ftemp ();
2401         if (f[DIFF_RIGHT] == NULL)
2402         {
2403             dview_fclose (f[DIFF_LEFT]);
2404             return -1;
2405         }
2406     }
2407     else if (dsrc == DATA_SRC_ORG)
2408     {
2409         f[DIFF_LEFT] = dview_fopen (file1, O_RDONLY);
2410         if (f[DIFF_LEFT] == NULL)
2411             return -1;
2412 
2413         f[DIFF_RIGHT] = dview_fopen (file2, O_RDONLY);
2414         if (f[DIFF_RIGHT] == NULL)
2415         {
2416             dview_fclose (f[DIFF_LEFT]);
2417             return -1;
2418         }
2419     }
2420 
2421     dview->view_quit = FALSE;
2422 
2423     dview->bias = 0;
2424     dview->new_frame = TRUE;
2425     dview->skip_rows = 0;
2426     dview->skip_cols = 0;
2427     dview->display_symbols = FALSE;
2428     dview->display_numbers = 0;
2429     dview->show_cr = TRUE;
2430     dview->tab_size = 8;
2431     dview->ord = DIFF_LEFT;
2432     dview->full = FALSE;
2433 
2434     dview->search.handle = NULL;
2435     dview->search.last_string = NULL;
2436     dview->search.last_found_line = -1;
2437     dview->search.last_accessed_num_line = -1;
2438 
2439     dview_load_options (dview);
2440 
2441     dview->args = args;
2442     dview->file[DIFF_LEFT] = file1;
2443     dview->file[DIFF_RIGHT] = file2;
2444     dview->label[DIFF_LEFT] = g_strdup (label1);
2445     dview->label[DIFF_RIGHT] = g_strdup (label2);
2446     dview->f[DIFF_LEFT] = f[0];
2447     dview->f[DIFF_RIGHT] = f[1];
2448     dview->merged[DIFF_LEFT] = FALSE;
2449     dview->merged[DIFF_RIGHT] = FALSE;
2450     dview->hdiff = NULL;
2451     dview->dsrc = dsrc;
2452 #ifdef HAVE_CHARSET
2453     dview->converter = str_cnv_from_term;
2454     dview_set_codeset (dview);
2455 #endif
2456     dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2457     g_array_set_clear_func (dview->a[DIFF_LEFT], cc_free_elt);
2458     dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2459     g_array_set_clear_func (dview->a[DIFF_RIGHT], cc_free_elt);
2460 
2461     return 0;
2462 }
2463 
2464 /* --------------------------------------------------------------------------------------------- */
2465 
2466 static void
2467 dview_fini (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2468 {
2469     if (dview->dsrc != DATA_SRC_MEM)
2470     {
2471         dview_fclose (dview->f[DIFF_RIGHT]);
2472         dview_fclose (dview->f[DIFF_LEFT]);
2473     }
2474 
2475 #ifdef HAVE_CHARSET
2476     if (dview->converter != str_cnv_from_term)
2477         str_close_conv (dview->converter);
2478 #endif
2479 
2480     destroy_hdiff (dview);
2481     if (dview->a[DIFF_LEFT] != NULL)
2482     {
2483         g_array_free (dview->a[DIFF_LEFT], TRUE);
2484         dview->a[DIFF_LEFT] = NULL;
2485     }
2486     if (dview->a[DIFF_RIGHT] != NULL)
2487     {
2488         g_array_free (dview->a[DIFF_RIGHT], TRUE);
2489         dview->a[DIFF_RIGHT] = NULL;
2490     }
2491 
2492     g_free (dview->label[DIFF_LEFT]);
2493     g_free (dview->label[DIFF_RIGHT]);
2494 }
2495 
2496 /* --------------------------------------------------------------------------------------------- */
2497 
2498 static int
2499 dview_display_file (const WDiff *dview, diff_place_t ord, int r, int c, int height, int width)
     /* [previous][next][first][last][top][bottom][index][help]  */
2500 {
2501     size_t i, k;
2502     int j;
2503     char buf[BUFSIZ];
2504     FBUF *f = dview->f[ord];
2505     int skip = dview->skip_cols;
2506     gboolean display_symbols = dview->display_symbols;
2507     int display_numbers = dview->display_numbers;
2508     gboolean show_cr = dview->show_cr;
2509     int tab_size = 8;
2510     const DIFFLN *p;
2511     int nwidth = display_numbers;
2512     int xwidth;
2513 
2514     xwidth = display_numbers;
2515     if (display_symbols)
2516         xwidth++;
2517     if (dview->tab_size > 0 && dview->tab_size < 9)
2518         tab_size = dview->tab_size;
2519 
2520     if (xwidth != 0)
2521     {
2522         if (xwidth > width && display_symbols)
2523         {
2524             xwidth--;
2525             display_symbols = FALSE;
2526         }
2527         if (xwidth > width && display_numbers != 0)
2528         {
2529             xwidth = width;
2530             display_numbers = width;
2531         }
2532 
2533         xwidth++;
2534         c += xwidth;
2535         width -= xwidth;
2536         if (width < 0)
2537             width = 0;
2538     }
2539 
2540     if ((int) sizeof (buf) <= width || (int) sizeof (buf) <= nwidth)
2541     {
2542         /* abnormal, but avoid buffer overflow */
2543         return -1;
2544     }
2545 
2546     for (i = dview->skip_rows, j = 0; i < dview->a[ord]->len && j < height; j++, i++)
2547     {
2548         int ch;
2549         int next_ch = 0;
2550         int col;
2551         size_t cnt;
2552 
2553         p = (DIFFLN *) & g_array_index (dview->a[ord], DIFFLN, i);
2554         ch = p->ch;
2555         tty_setcolor (NORMAL_COLOR);
2556         if (display_symbols)
2557         {
2558             tty_gotoyx (r + j, c - 2);
2559             tty_print_char (ch);
2560         }
2561         if (p->line != 0)
2562         {
2563             if (display_numbers != 0)
2564             {
2565                 tty_gotoyx (r + j, c - xwidth);
2566                 g_snprintf (buf, display_numbers + 1, "%*d", nwidth, p->line);
2567                 tty_print_string (str_fit_to_term (buf, nwidth, J_LEFT_FIT));
2568             }
2569             if (ch == ADD_CH)
2570                 tty_setcolor (DFF_ADD_COLOR);
2571             if (ch == CHG_CH)
2572                 tty_setcolor (DFF_CHG_COLOR);
2573             if (f == NULL)
2574             {
2575                 if (i == (size_t) dview->search.last_found_line)
2576                     tty_setcolor (MARKED_SELECTED_COLOR);
2577                 else if (dview->hdiff != NULL && g_ptr_array_index (dview->hdiff, i) != NULL)
2578                 {
2579                     char att[BUFSIZ];
2580 
2581 #ifdef HAVE_CHARSET
2582                     if (dview->utf8)
2583                         k = dview_str_utf8_offset_to_pos (p->p, width);
2584                     else
2585 #endif
2586                         k = width;
2587 
2588                     cvt_mgeta (p->p, p->u.len, buf, k, skip, tab_size, show_cr,
2589                                g_ptr_array_index (dview->hdiff, i), ord, att);
2590                     tty_gotoyx (r + j, c);
2591                     col = 0;
2592 
2593                     for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2594                     {
2595                         gboolean ch_res;
2596 
2597 #ifdef HAVE_CHARSET
2598                         if (dview->utf8)
2599                         {
2600                             int ch_length = 0;
2601 
2602                             ch_res = dview_get_utf (buf + cnt, &next_ch, &ch_length);
2603                             if (ch_length > 1)
2604                                 cnt += ch_length - 1;
2605                             if (!g_unichar_isprint (next_ch))
2606                                 next_ch = '.';
2607                         }
2608                         else
2609 #endif
2610                             ch_res = dview_get_byte (buf + cnt, &next_ch);
2611 
2612                         if (ch_res)
2613                         {
2614                             tty_setcolor (att[cnt] ? DFF_CHH_COLOR : DFF_CHG_COLOR);
2615 #ifdef HAVE_CHARSET
2616                             if (mc_global.utf8_display)
2617                             {
2618                                 if (!dview->utf8)
2619                                 {
2620                                     next_ch =
2621                                         convert_from_8bit_to_utf_c ((unsigned char) next_ch,
2622                                                                     dview->converter);
2623                                 }
2624                             }
2625                             else if (dview->utf8)
2626                                 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2627                             else
2628                                 next_ch = convert_to_display_c (next_ch);
2629 #endif
2630                             tty_print_anychar (next_ch);
2631                             col++;
2632                         }
2633                     }
2634                     continue;
2635                 }
2636 
2637                 if (ch == CHG_CH)
2638                     tty_setcolor (DFF_CHH_COLOR);
2639 
2640 #ifdef HAVE_CHARSET
2641                 if (dview->utf8)
2642                     k = dview_str_utf8_offset_to_pos (p->p, width);
2643                 else
2644 #endif
2645                     k = width;
2646                 cvt_mget (p->p, p->u.len, buf, k, skip, tab_size, show_cr);
2647             }
2648             else
2649                 cvt_fget (f, p->u.off, buf, width, skip, tab_size, show_cr);
2650         }
2651         else
2652         {
2653             if (display_numbers != 0)
2654             {
2655                 tty_gotoyx (r + j, c - xwidth);
2656                 fill_by_space (buf, display_numbers, TRUE);
2657                 tty_print_string (buf);
2658             }
2659             if (ch == DEL_CH)
2660                 tty_setcolor (DFF_DEL_COLOR);
2661             if (ch == CHG_CH)
2662                 tty_setcolor (DFF_CHD_COLOR);
2663             fill_by_space (buf, width, TRUE);
2664         }
2665         tty_gotoyx (r + j, c);
2666         /* tty_print_nstring (buf, width); */
2667         col = 0;
2668         for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2669         {
2670             gboolean ch_res;
2671 
2672 #ifdef HAVE_CHARSET
2673             if (dview->utf8)
2674             {
2675                 int ch_length = 0;
2676 
2677                 ch_res = dview_get_utf (buf + cnt, &next_ch, &ch_length);
2678                 if (ch_length > 1)
2679                     cnt += ch_length - 1;
2680                 if (!g_unichar_isprint (next_ch))
2681                     next_ch = '.';
2682             }
2683             else
2684 #endif
2685                 ch_res = dview_get_byte (buf + cnt, &next_ch);
2686 
2687             if (ch_res)
2688             {
2689 #ifdef HAVE_CHARSET
2690                 if (mc_global.utf8_display)
2691                 {
2692                     if (!dview->utf8)
2693                         next_ch =
2694                             convert_from_8bit_to_utf_c ((unsigned char) next_ch, dview->converter);
2695                 }
2696                 else if (dview->utf8)
2697                     next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2698                 else
2699                     next_ch = convert_to_display_c (next_ch);
2700 #endif
2701 
2702                 tty_print_anychar (next_ch);
2703                 col++;
2704             }
2705         }
2706     }
2707     tty_setcolor (NORMAL_COLOR);
2708     k = width;
2709     if (width < xwidth - 1)
2710         k = xwidth - 1;
2711     fill_by_space (buf, k, TRUE);
2712     for (; j < height; j++)
2713     {
2714         if (xwidth != 0)
2715         {
2716             tty_gotoyx (r + j, c - xwidth);
2717             /* tty_print_nstring (buf, xwidth - 1); */
2718             tty_print_string (str_fit_to_term (buf, xwidth - 1, J_LEFT_FIT));
2719         }
2720         tty_gotoyx (r + j, c);
2721         /* tty_print_nstring (buf, width); */
2722         tty_print_string (str_fit_to_term (buf, width, J_LEFT_FIT));
2723     }
2724 
2725     return 0;
2726 }
2727 
2728 /* --------------------------------------------------------------------------------------------- */
2729 
2730 static void
2731 dview_status (const WDiff *dview, diff_place_t ord, int width, int c)
     /* [previous][next][first][last][top][bottom][index][help]  */
2732 {
2733     const char *buf;
2734     int filename_width;
2735     int linenum, lineofs;
2736     vfs_path_t *vpath;
2737     char *path;
2738 
2739     tty_setcolor (STATUSBAR_COLOR);
2740 
2741     tty_gotoyx (0, c);
2742     get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2743 
2744     filename_width = width - 24;
2745     if (filename_width < 8)
2746         filename_width = 8;
2747 
2748     vpath = vfs_path_from_str (dview->label[ord]);
2749     path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
2750     vfs_path_free (vpath, TRUE);
2751     buf = str_term_trim (path, filename_width);
2752     if (ord == DIFF_LEFT)
2753         tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview->merged[ord] ? "* " : "  ", filename_width,
2754                     buf, linenum, lineofs, dview->skip_cols);
2755     else
2756         tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview->merged[ord] ? "* " : "  ", filename_width,
2757                     buf, linenum, lineofs, dview->ndiff);
2758     g_free (path);
2759 }
2760 
2761 /* --------------------------------------------------------------------------------------------- */
2762 
2763 static void
2764 dview_redo (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2765 {
2766     if (dview->display_numbers != 0)
2767     {
2768         int old;
2769 
2770         old = dview->display_numbers;
2771         dview->display_numbers = calc_nwidth ((const GArray * const *) dview->a);
2772         dview->new_frame = (old != dview->display_numbers);
2773     }
2774     dview_reread (dview);
2775 }
2776 
2777 /* --------------------------------------------------------------------------------------------- */
2778 
2779 static void
2780 dview_update (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2781 {
2782     int height = dview->height;
2783     int width1, width2;
2784     int last;
2785 
2786     last = dview->a[DIFF_LEFT]->len - 1;
2787 
2788     if (dview->skip_rows > last)
2789         dview->skip_rows = dview->search.last_accessed_num_line = last;
2790     if (dview->skip_rows < 0)
2791         dview->skip_rows = dview->search.last_accessed_num_line = 0;
2792     if (dview->skip_cols < 0)
2793         dview->skip_cols = 0;
2794 
2795     if (height < 2)
2796         return;
2797 
2798     /* use an actual length of dview->a */
2799     if (dview->display_numbers != 0)
2800         dview->display_numbers = calc_nwidth ((const GArray * const *) dview->a);
2801 
2802     width1 = dview->half1 + dview->bias;
2803     width2 = dview->half2 - dview->bias;
2804     if (dview->full)
2805     {
2806         width1 = COLS;
2807         width2 = 0;
2808     }
2809 
2810     if (dview->new_frame)
2811     {
2812         int xwidth;
2813 
2814         tty_setcolor (NORMAL_COLOR);
2815         xwidth = dview->display_numbers;
2816         if (dview->display_symbols)
2817             xwidth++;
2818         if (width1 > 1)
2819             tty_draw_box (1, 0, height, width1, FALSE);
2820         if (width2 > 1)
2821             tty_draw_box (1, width1, height, width2, FALSE);
2822 
2823         if (xwidth != 0)
2824         {
2825             xwidth++;
2826             if (xwidth < width1 - 1)
2827             {
2828                 tty_gotoyx (1, xwidth);
2829                 tty_print_alt_char (ACS_TTEE, FALSE);
2830                 tty_gotoyx (height, xwidth);
2831                 tty_print_alt_char (ACS_BTEE, FALSE);
2832                 tty_draw_vline (2, xwidth, ACS_VLINE, height - 2);
2833             }
2834             if (xwidth < width2 - 1)
2835             {
2836                 tty_gotoyx (1, width1 + xwidth);
2837                 tty_print_alt_char (ACS_TTEE, FALSE);
2838                 tty_gotoyx (height, width1 + xwidth);
2839                 tty_print_alt_char (ACS_BTEE, FALSE);
2840                 tty_draw_vline (2, width1 + xwidth, ACS_VLINE, height - 2);
2841             }
2842         }
2843         dview->new_frame = FALSE;
2844     }
2845 
2846     if (width1 > 2)
2847     {
2848         dview_status (dview, dview->ord, width1, 0);
2849         dview_display_file (dview, dview->ord, 2, 1, height - 2, width1 - 2);
2850     }
2851     if (width2 > 2)
2852     {
2853         diff_place_t ord;
2854 
2855         ord = dview->ord == DIFF_LEFT ? DIFF_RIGHT : DIFF_LEFT;
2856         dview_status (dview, ord, width2, width1);
2857         dview_display_file (dview, ord, 2, width1 + 1, height - 2, width2 - 2);
2858     }
2859 }
2860 
2861 /* --------------------------------------------------------------------------------------------- */
2862 
2863 static void
2864 dview_edit (WDiff *dview, diff_place_t ord)
     /* [previous][next][first][last][top][bottom][index][help]  */
2865 {
2866     Widget *h;
2867     gboolean h_modal;
2868     int linenum, lineofs;
2869 
2870     if (dview->dsrc == DATA_SRC_TMP)
2871     {
2872         error_dialog (_("Edit"), _("Edit is disabled"));
2873         return;
2874     }
2875 
2876     h = WIDGET (WIDGET (dview)->owner);
2877     h_modal = widget_get_state (h, WST_MODAL);
2878 
2879     get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2880 
2881     /* disallow edit file in several editors */
2882     widget_set_state (h, WST_MODAL, TRUE);
2883 
2884     {
2885         vfs_path_t *tmp_vpath;
2886 
2887         tmp_vpath = vfs_path_from_str (dview->file[ord]);
2888         edit_file_at_line (tmp_vpath, use_internal_edit, linenum);
2889         vfs_path_free (tmp_vpath, TRUE);
2890     }
2891 
2892     widget_set_state (h, WST_MODAL, h_modal);
2893     dview_redo (dview);
2894     dview_update (dview);
2895 }
2896 
2897 /* --------------------------------------------------------------------------------------------- */
2898 
2899 static void
2900 dview_goto_cmd (WDiff *dview, diff_place_t ord)
     /* [previous][next][first][last][top][bottom][index][help]  */
2901 {
2902     static gboolean first_run = TRUE;
2903 
2904     /* *INDENT-OFF* */
2905     static const char *title[2] = {
2906         N_("Goto line (left)"),
2907         N_("Goto line (right)")
2908     };
2909     /* *INDENT-ON* */
2910 
2911     int newline;
2912     char *input;
2913 
2914     input =
2915         input_dialog (_(title[ord]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE,
2916                       first_run ? NULL : INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
2917     if (input != NULL)
2918     {
2919         const char *s = input;
2920 
2921         if (scan_deci (&s, &newline) == 0 && *s == '\0')
2922         {
2923             size_t i = 0;
2924 
2925             if (newline > 0)
2926                 for (; i < dview->a[ord]->len; i++)
2927                 {
2928                     const DIFFLN *p;
2929 
2930                     p = &g_array_index (dview->a[ord], DIFFLN, i);
2931                     if (p->line == newline)
2932                         break;
2933                 }
2934 
2935             dview->skip_rows = dview->search.last_accessed_num_line = (ssize_t) i;
2936         }
2937 
2938         g_free (input);
2939     }
2940 
2941     first_run = FALSE;
2942 }
2943 
2944 /* --------------------------------------------------------------------------------------------- */
2945 
2946 static void
2947 dview_labels (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2948 {
2949     Widget *d = WIDGET (dview);
2950     WButtonBar *b;
2951 
2952     b = buttonbar_find (DIALOG (d->owner));
2953 
2954     buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), d->keymap, d);
2955     buttonbar_set_label (b, 2, Q_ ("ButtonBar|Save"), d->keymap, d);
2956     buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), d->keymap, d);
2957     buttonbar_set_label (b, 5, Q_ ("ButtonBar|Merge"), d->keymap, d);
2958     buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), d->keymap, d);
2959     buttonbar_set_label (b, 9, Q_ ("ButtonBar|Options"), d->keymap, d);
2960     buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), d->keymap, d);
2961 }
2962 
2963 /* --------------------------------------------------------------------------------------------- */
2964 
2965 static gboolean
2966 dview_save (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2967 {
2968     gboolean res = TRUE;
2969 
2970     if (dview->merged[DIFF_LEFT])
2971     {
2972         res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
2973         dview->merged[DIFF_LEFT] = !res;
2974     }
2975     if (dview->merged[DIFF_RIGHT])
2976     {
2977         res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
2978         dview->merged[DIFF_RIGHT] = !res;
2979     }
2980     return res;
2981 }
2982 
2983 /* --------------------------------------------------------------------------------------------- */
2984 
2985 static void
2986 dview_do_save (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2987 {
2988     (void) dview_save (dview);
2989 }
2990 
2991 /* --------------------------------------------------------------------------------------------- */
2992 
2993 /*
2994  * Check if it's OK to close the diff viewer.  If there are unsaved changes,
2995  * ask user.
2996  */
2997 static gboolean
2998 dview_ok_to_exit (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2999 {
3000     gboolean res = TRUE;
3001     int act;
3002 
3003     if (!dview->merged[DIFF_LEFT] && !dview->merged[DIFF_RIGHT])
3004         return res;
3005 
3006     act = query_dialog (_("Quit"), !mc_global.midnight_shutdown ?
3007                         _("File(s) was modified. Save with exit?") :
3008                         _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3009                         D_NORMAL, 2, _("&Yes"), _("&No"));
3010 
3011     /* Esc is No */
3012     if (mc_global.midnight_shutdown || (act == -1))
3013         act = 1;
3014 
3015     switch (act)
3016     {
3017     case -1:                   /* Esc */
3018         res = FALSE;
3019         break;
3020     case 0:                    /* Yes */
3021         (void) dview_save (dview);
3022         res = TRUE;
3023         break;
3024     case 1:                    /* No */
3025         if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_LEFT], "~~~"))
3026             res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
3027         if (mc_util_restore_from_backup_if_possible (dview->file[DIFF_RIGHT], "~~~"))
3028             res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
3029         MC_FALLTHROUGH;
3030     default:
3031         res = TRUE;
3032         break;
3033     }
3034     return res;
3035 }
3036 
3037 /* --------------------------------------------------------------------------------------------- */
3038 
3039 static cb_ret_t
3040 dview_execute_cmd (WDiff *dview, long command)
     /* [previous][next][first][last][top][bottom][index][help]  */
3041 {
3042     cb_ret_t res = MSG_HANDLED;
3043 
3044     switch (command)
3045     {
3046     case CK_ShowSymbols:
3047         dview->display_symbols = !dview->display_symbols;
3048         dview->new_frame = TRUE;
3049         break;
3050     case CK_ShowNumbers:
3051         dview->display_numbers ^= calc_nwidth ((const GArray * const *) dview->a);
3052         dview->new_frame = TRUE;
3053         break;
3054     case CK_SplitFull:
3055         dview->full = !dview->full;
3056         dview->new_frame = TRUE;
3057         break;
3058     case CK_SplitEqual:
3059         if (!dview->full)
3060         {
3061             dview->bias = 0;
3062             dview->new_frame = TRUE;
3063         }
3064         break;
3065     case CK_SplitMore:
3066         if (!dview->full)
3067         {
3068             dview_compute_split (dview, 1);
3069             dview->new_frame = TRUE;
3070         }
3071         break;
3072 
3073     case CK_SplitLess:
3074         if (!dview->full)
3075         {
3076             dview_compute_split (dview, -1);
3077             dview->new_frame = TRUE;
3078         }
3079         break;
3080     case CK_Tab2:
3081         dview->tab_size = 2;
3082         break;
3083     case CK_Tab3:
3084         dview->tab_size = 3;
3085         break;
3086     case CK_Tab4:
3087         dview->tab_size = 4;
3088         break;
3089     case CK_Tab8:
3090         dview->tab_size = 8;
3091         break;
3092     case CK_Swap:
3093         dview->ord ^= 1;
3094         break;
3095     case CK_Redo:
3096         dview_redo (dview);
3097         break;
3098     case CK_HunkNext:
3099         dview->skip_rows = dview->search.last_accessed_num_line =
3100             find_next_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3101         break;
3102     case CK_HunkPrev:
3103         dview->skip_rows = dview->search.last_accessed_num_line =
3104             find_prev_hunk (dview->a[DIFF_LEFT], dview->skip_rows);
3105         break;
3106     case CK_Goto:
3107         dview_goto_cmd (dview, DIFF_RIGHT);
3108         break;
3109     case CK_Edit:
3110         dview_edit (dview, dview->ord);
3111         break;
3112     case CK_Merge:
3113         do_merge_hunk (dview, FROM_LEFT_TO_RIGHT);
3114         dview_redo (dview);
3115         break;
3116     case CK_MergeOther:
3117         do_merge_hunk (dview, FROM_RIGHT_TO_LEFT);
3118         dview_redo (dview);
3119         break;
3120     case CK_EditOther:
3121         dview_edit (dview, dview->ord ^ 1);
3122         break;
3123     case CK_Search:
3124         dview_search_cmd (dview);
3125         break;
3126     case CK_SearchContinue:
3127         dview_continue_search_cmd (dview);
3128         break;
3129     case CK_Top:
3130         dview->skip_rows = dview->search.last_accessed_num_line = 0;
3131         break;
3132     case CK_Bottom:
3133         dview->skip_rows = dview->search.last_accessed_num_line = dview->a[DIFF_LEFT]->len - 1;
3134         break;
3135     case CK_Up:
3136         if (dview->skip_rows > 0)
3137         {
3138             dview->skip_rows--;
3139             dview->search.last_accessed_num_line = dview->skip_rows;
3140         }
3141         break;
3142     case CK_Down:
3143         dview->skip_rows++;
3144         dview->search.last_accessed_num_line = dview->skip_rows;
3145         break;
3146     case CK_PageDown:
3147         if (dview->height > 2)
3148         {
3149             dview->skip_rows += dview->height - 2;
3150             dview->search.last_accessed_num_line = dview->skip_rows;
3151         }
3152         break;
3153     case CK_PageUp:
3154         if (dview->height > 2)
3155         {
3156             dview->skip_rows -= dview->height - 2;
3157             dview->search.last_accessed_num_line = dview->skip_rows;
3158         }
3159         break;
3160     case CK_Left:
3161         dview->skip_cols--;
3162         break;
3163     case CK_Right:
3164         dview->skip_cols++;
3165         break;
3166     case CK_LeftQuick:
3167         dview->skip_cols -= 8;
3168         break;
3169     case CK_RightQuick:
3170         dview->skip_cols += 8;
3171         break;
3172     case CK_Home:
3173         dview->skip_cols = 0;
3174         break;
3175     case CK_Shell:
3176         toggle_subshell ();
3177         break;
3178     case CK_Quit:
3179         dview->view_quit = TRUE;
3180         break;
3181     case CK_Save:
3182         dview_do_save (dview);
3183         break;
3184     case CK_Options:
3185         dview_diff_options (dview);
3186         break;
3187 #ifdef HAVE_CHARSET
3188     case CK_SelectCodepage:
3189         dview_select_encoding (dview);
3190         break;
3191 #endif
3192     case CK_Cancel:
3193         /* don't close diffviewer due to SIGINT */
3194         break;
3195     default:
3196         res = MSG_NOT_HANDLED;
3197     }
3198     return res;
3199 }
3200 
3201 /* --------------------------------------------------------------------------------------------- */
3202 
3203 static cb_ret_t
3204 dview_handle_key (WDiff *dview, int key)
     /* [previous][next][first][last][top][bottom][index][help]  */
3205 {
3206     long command;
3207 
3208 #ifdef HAVE_CHARSET
3209     key = convert_from_input_c (key);
3210 #endif
3211 
3212     command = widget_lookup_key (WIDGET (dview), key);
3213     if (command == CK_IgnoreKey)
3214         return MSG_NOT_HANDLED;
3215 
3216     return dview_execute_cmd (dview, command);
3217 }
3218 
3219 /* --------------------------------------------------------------------------------------------- */
3220 
3221 static cb_ret_t
3222 dview_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
3223 {
3224     WDiff *dview = (WDiff *) w;
3225     WDialog *h = DIALOG (w->owner);
3226     cb_ret_t i;
3227 
3228     switch (msg)
3229     {
3230     case MSG_INIT:
3231         dview_labels (dview);
3232         dview_update (dview);
3233         return MSG_HANDLED;
3234 
3235     case MSG_DRAW:
3236         dview->new_frame = TRUE;
3237         dview_update (dview);
3238         return MSG_HANDLED;
3239 
3240     case MSG_KEY:
3241         i = dview_handle_key (dview, parm);
3242         if (dview->view_quit)
3243             dlg_close (h);
3244         else
3245             dview_update (dview);
3246         return i;
3247 
3248     case MSG_ACTION:
3249         i = dview_execute_cmd (dview, parm);
3250         if (dview->view_quit)
3251             dlg_close (h);
3252         else
3253             dview_update (dview);
3254         return i;
3255 
3256     case MSG_RESIZE:
3257         widget_default_callback (w, NULL, MSG_RESIZE, 0, data);
3258         dview_compute_areas (dview);
3259         return MSG_HANDLED;
3260 
3261     case MSG_DESTROY:
3262         dview_save_options (dview);
3263         dview_fini (dview);
3264         return MSG_HANDLED;
3265 
3266     default:
3267         return widget_default_callback (w, sender, msg, parm, data);
3268     }
3269 }
3270 
3271 /* --------------------------------------------------------------------------------------------- */
3272 
3273 static void
3274 dview_mouse_callback (Widget *w, mouse_msg_t msg, mouse_event_t *event)
     /* [previous][next][first][last][top][bottom][index][help]  */
3275 {
3276     WDiff *dview = (WDiff *) w;
3277 
3278     (void) event;
3279 
3280     switch (msg)
3281     {
3282     case MSG_MOUSE_SCROLL_UP:
3283     case MSG_MOUSE_SCROLL_DOWN:
3284         if (msg == MSG_MOUSE_SCROLL_UP)
3285             dview->skip_rows -= 2;
3286         else
3287             dview->skip_rows += 2;
3288 
3289         dview->search.last_accessed_num_line = dview->skip_rows;
3290         dview_update (dview);
3291         break;
3292 
3293     default:
3294         break;
3295     }
3296 }
3297 
3298 /* --------------------------------------------------------------------------------------------- */
3299 
3300 static cb_ret_t
3301 dview_dialog_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
3302 {
3303     WDiff *dview;
3304     WDialog *h = DIALOG (w);
3305 
3306     switch (msg)
3307     {
3308     case MSG_ACTION:
3309         /* Handle shortcuts. */
3310 
3311         /* Note: the buttonbar sends messages directly to the the WDiff, not to
3312          * here, which is why we can pass NULL in the following call. */
3313         return dview_execute_cmd (NULL, parm);
3314 
3315     case MSG_VALIDATE:
3316         dview = (WDiff *) widget_find_by_type (CONST_WIDGET (h), dview_callback);
3317         /* don't stop the dialog before final decision */
3318         widget_set_state (w, WST_ACTIVE, TRUE);
3319         if (dview_ok_to_exit (dview))
3320             dlg_close (h);
3321         return MSG_HANDLED;
3322 
3323     default:
3324         return dlg_default_callback (w, sender, msg, parm, data);
3325     }
3326 }
3327 
3328 /* --------------------------------------------------------------------------------------------- */
3329 
3330 static char *
3331 dview_get_title (const WDialog *h, size_t len)
     /* [previous][next][first][last][top][bottom][index][help]  */
3332 {
3333     const WDiff *dview;
3334     const char *modified = " (*) ";
3335     const char *notmodified = "     ";
3336     size_t len1;
3337     GString *title;
3338 
3339     dview = (const WDiff *) widget_find_by_type (CONST_WIDGET (h), dview_callback);
3340     len1 = (len - str_term_width1 (_("Diff:")) - strlen (modified) - 3) / 2;
3341 
3342     title = g_string_sized_new (len);
3343     g_string_append (title, _("Diff:"));
3344     g_string_append (title, dview->merged[DIFF_LEFT] ? modified : notmodified);
3345     g_string_append (title, str_term_trim (dview->label[DIFF_LEFT], len1));
3346     g_string_append (title, " | ");
3347     g_string_append (title, dview->merged[DIFF_RIGHT] ? modified : notmodified);
3348     g_string_append (title, str_term_trim (dview->label[DIFF_RIGHT], len1));
3349 
3350     return g_string_free (title, FALSE);
3351 }
3352 
3353 /* --------------------------------------------------------------------------------------------- */
3354 
3355 static int
3356 diff_view (const char *file1, const char *file2, const char *label1, const char *label2)
     /* [previous][next][first][last][top][bottom][index][help]  */
3357 {
3358     int error;
3359     WDiff *dview;
3360     Widget *w;
3361     WDialog *dview_dlg;
3362     Widget *dw;
3363     WRect r;
3364     WGroup *g;
3365 
3366     /* Create dialog and widgets, put them on the dialog */
3367     dview_dlg =
3368         dlg_create (FALSE, 0, 0, 1, 1, WPOS_FULLSCREEN, FALSE, NULL, dview_dialog_callback, NULL,
3369                     "[Diff Viewer]", NULL);
3370     dw = WIDGET (dview_dlg);
3371     widget_want_tab (dw, TRUE);
3372     r = dw->rect;
3373 
3374     g = GROUP (dview_dlg);
3375 
3376     dview = g_new0 (WDiff, 1);
3377     w = WIDGET (dview);
3378     r.lines--;
3379     widget_init (w, &r, dview_callback, dview_mouse_callback);
3380     w->options |= WOP_SELECTABLE;
3381     w->keymap = diff_map;
3382     group_add_widget_autopos (g, w, WPOS_KEEP_ALL, NULL);
3383 
3384     w = WIDGET (buttonbar_new ());
3385     group_add_widget_autopos (g, w, w->pos_flags, NULL);
3386 
3387     dview_dlg->get_title = dview_get_title;
3388 
3389     error = dview_init (dview, "-a", file1, file2, label1, label2, DATA_SRC_MEM);       /* XXX binary diff? */
3390     if (error >= 0)
3391         error = redo_diff (dview);
3392     if (error >= 0)
3393     {
3394         dview->ndiff = error;
3395         dview_compute_areas (dview);
3396         error = 0;
3397     }
3398 
3399     if (error == 0)
3400         dlg_run (dview_dlg);
3401 
3402     if (error != 0 || widget_get_state (dw, WST_CLOSED))
3403         widget_destroy (dw);
3404 
3405     return error == 0 ? 1 : 0;
3406 }
3407 
3408 /*** public functions ****************************************************************************/
3409 /* --------------------------------------------------------------------------------------------- */
3410 
3411 #define GET_FILE_AND_STAMP(n) \
3412 do \
3413 { \
3414     use_copy##n = 0; \
3415     real_file##n = file##n; \
3416     if (!vfs_file_is_local (file##n)) \
3417     { \
3418         real_file##n = mc_getlocalcopy (file##n); \
3419         if (real_file##n != NULL) \
3420         { \
3421             use_copy##n = 1; \
3422             if (mc_stat (real_file##n, &st##n) != 0) \
3423                 use_copy##n = -1; \
3424         } \
3425     } \
3426 } \
3427 while (0)
3428 
3429 #define UNGET_FILE(n) \
3430 do \
3431 { \
3432     if (use_copy##n != 0) \
3433     { \
3434         gboolean changed = FALSE; \
3435         if (use_copy##n > 0) \
3436         { \
3437             time_t mtime; \
3438             mtime = st##n.st_mtime; \
3439             if (mc_stat (real_file##n, &st##n) == 0) \
3440                 changed = (mtime != st##n.st_mtime); \
3441         } \
3442         mc_ungetlocalcopy (file##n, real_file##n, changed); \
3443         vfs_path_free (real_file##n, TRUE); \
3444     } \
3445 } \
3446 while (0)
3447 
3448 gboolean
3449 dview_diff_cmd (const void *f0, const void *f1)
     /* [previous][next][first][last][top][bottom][index][help]  */
3450 {
3451     int rv = 0;
3452     vfs_path_t *file0 = NULL;
3453     vfs_path_t *file1 = NULL;
3454     gboolean is_dir0 = FALSE;
3455     gboolean is_dir1 = FALSE;
3456 
3457     switch (mc_global.mc_run_mode)
3458     {
3459     case MC_RUN_FULL:
3460         {
3461             /* run from panels */
3462             const WPanel *panel0 = (const WPanel *) f0;
3463             const WPanel *panel1 = (const WPanel *) f1;
3464             const file_entry_t *fe0, *fe1;
3465 
3466             fe0 = panel_current_entry (panel0);
3467             if (fe0 == NULL)
3468             {
3469                 message (D_ERROR, MSG_ERROR, "%s", _("File name is empty!"));
3470                 goto ret;
3471             }
3472 
3473             file0 = vfs_path_append_new (panel0->cwd_vpath, fe0->fname->str, (char *) NULL);
3474             is_dir0 = S_ISDIR (fe0->st.st_mode);
3475             if (is_dir0)
3476             {
3477                 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3478                          path_trunc (fe0->fname->str, 30));
3479                 goto ret;
3480             }
3481 
3482             fe1 = panel_current_entry (panel1);
3483             if (fe1 == NULL)
3484             {
3485                 message (D_ERROR, MSG_ERROR, "%s", _("File name is empty!"));
3486                 goto ret;
3487             }
3488             file1 = vfs_path_append_new (panel1->cwd_vpath, fe1->fname->str, (char *) NULL);
3489             is_dir1 = S_ISDIR (fe1->st.st_mode);
3490             if (is_dir1)
3491             {
3492                 message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"),
3493                          path_trunc (fe1->fname->str, 30));
3494                 goto ret;
3495             }
3496             break;
3497         }
3498 
3499     case MC_RUN_DIFFVIEWER:
3500         {
3501             /* run from command line */
3502             const char *p0 = (const char *) f0;
3503             const char *p1 = (const char *) f1;
3504             struct stat st;
3505 
3506             file0 = vfs_path_from_str (p0);
3507             if (mc_stat (file0, &st) == 0)
3508             {
3509                 is_dir0 = S_ISDIR (st.st_mode);
3510                 if (is_dir0)
3511                 {
3512                     message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p0, 30));
3513                     goto ret;
3514                 }
3515             }
3516             else
3517             {
3518                 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3519                          path_trunc (p0, 30), unix_error_string (errno));
3520                 goto ret;
3521             }
3522 
3523             file1 = vfs_path_from_str (p1);
3524             if (mc_stat (file1, &st) == 0)
3525             {
3526                 is_dir1 = S_ISDIR (st.st_mode);
3527                 if (is_dir1)
3528                 {
3529                     message (D_ERROR, MSG_ERROR, _("\"%s\" is a directory"), path_trunc (p1, 30));
3530                     goto ret;
3531                 }
3532             }
3533             else
3534             {
3535                 message (D_ERROR, MSG_ERROR, _("Cannot stat \"%s\"\n%s"),
3536                          path_trunc (p1, 30), unix_error_string (errno));
3537                 goto ret;
3538             }
3539             break;
3540         }
3541 
3542     default:
3543         /* this should not happened */
3544         message (D_ERROR, MSG_ERROR, _("Diff viewer: invalid mode"));
3545         return FALSE;
3546     }
3547 
3548     if (rv == 0)
3549     {
3550         rv = -1;
3551         if (file0 != NULL && file1 != NULL)
3552         {
3553             int use_copy0, use_copy1;
3554             struct stat st0, st1;
3555             vfs_path_t *real_file0, *real_file1;
3556 
3557             GET_FILE_AND_STAMP (0);
3558             GET_FILE_AND_STAMP (1);
3559 
3560             if (real_file0 != NULL && real_file1 != NULL)
3561                 rv = diff_view (vfs_path_as_str (real_file0), vfs_path_as_str (real_file1),
3562                                 vfs_path_as_str (file0), vfs_path_as_str (file1));
3563 
3564             UNGET_FILE (1);
3565             UNGET_FILE (0);
3566         }
3567     }
3568 
3569     if (rv == 0)
3570         message (D_ERROR, MSG_ERROR, _("Two files are needed to compare"));
3571 
3572   ret:
3573     vfs_path_free (file1, TRUE);
3574     vfs_path_free (file0, TRUE);
3575 
3576     return (rv != 0);
3577 }
3578 
3579 #undef GET_FILE_AND_STAMP
3580 #undef UNGET_FILE
3581 
3582 /* --------------------------------------------------------------------------------------------- */

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