root/src/diffviewer/ydiff.c

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

DEFINITIONS

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

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

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