Manual pages: mcmcdiffmceditmcview

root/src/diffviewer/ydiff.c

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

DEFINITIONS

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

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

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