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

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