Manual pages: mcmcdiffmceditmcview

root/src/diffviewer/ydiff.c

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

DEFINITIONS

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

   1 /*
   2    File difference viewer
   3 
   4    Copyright (C) 2007-2025
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Daniel Borca <dborca@yahoo.com>, 2007
   9    Slava Zanko <slavazanko@gmail.com>, 2010, 2013
  10    Andrew Borodin <aborodin@vmail.ru>, 2010-2022
  11    Ilia Maslakov <il.smind@gmail.com>, 2010
  12 
  13    This file is part of the Midnight Commander.
  14 
  15    The Midnight Commander is free software: you can redistribute it
  16    and/or modify it under the terms of the GNU General Public License as
  17    published by the Free Software Foundation, either version 3 of the License,
  18    or (at your option) any later version.
  19 
  20    The Midnight Commander is distributed in the hope that it will be useful,
  21    but WITHOUT ANY WARRANTY; without even the implied warranty of
  22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23    GNU General Public License for more details.
  24 
  25    You should have received a copy of the GNU General Public License
  26    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  27  */
  28 
  29 #include <config.h>
  30 
  31 #include <ctype.h>
  32 #include <errno.h>
  33 #include <stddef.h>  // ptrdiff_t
  34 #include <stdlib.h>
  35 #include <sys/stat.h>
  36 #include <sys/types.h>
  37 #include <sys/wait.h>
  38 
  39 #include "lib/global.h"
  40 #include "lib/tty/tty.h"
  41 #include "lib/tty/color.h"
  42 #include "lib/tty/key.h"
  43 #include "lib/skin.h"  // EDITOR_NORMAL_COLOR
  44 #include "lib/vfs/vfs.h"
  45 #include "lib/util.h"
  46 #include "lib/widget.h"
  47 #include "lib/strutil.h"
  48 #include "lib/charsets.h"
  49 #include "lib/event.h"  // mc_event_raise()
  50 
  51 #include "src/filemanager/cmd.h"  // edit_file_at_line()
  52 #include "src/filemanager/panel.h"
  53 #include "src/filemanager/layout.h"  // Needed for get_current_index and get_other_panel
  54 
  55 #include "src/execute.h"  // toggle_subshell()
  56 #include "src/keymap.h"
  57 #include "src/setup.h"
  58 #include "src/history.h"
  59 #include "src/selcodepage.h"
  60 #include "src/util.h"  // file_error_message()
  61 
  62 #include "ydiff.h"
  63 #include "internal.h"
  64 
  65 /*** global variables ****************************************************************************/
  66 
  67 /*** file scope macro definitions ****************************************************************/
  68 
  69 #define FILE_READ_BUF  4096
  70 #define FILE_FLAG_TEMP (1 << 0)
  71 
  72 #define ADD_CH         '+'
  73 #define DEL_CH         '-'
  74 #define CHG_CH         '*'
  75 #define EQU_CH         ' '
  76 
  77 #define HDIFF_ENABLE   1
  78 #define HDIFF_MINCTX   5
  79 #define HDIFF_DEPTH    10
  80 
  81 #define FILE_DIRTY(fs)                                                                             \
  82     do                                                                                             \
  83     {                                                                                              \
  84         (fs)->pos = 0;                                                                             \
  85         (fs)->len = 0;                                                                             \
  86     }                                                                                              \
  87     while (0)
  88 
  89 /*** file scope type declarations ****************************************************************/
  90 
  91 typedef enum
  92 {
  93     FROM_LEFT_TO_RIGHT,
  94     FROM_RIGHT_TO_LEFT
  95 } action_direction_t;
  96 
  97 /*** forward declarations (file scope functions) *************************************************/
  98 
  99 /*** file scope variables ************************************************************************/
 100 
 101 /* --------------------------------------------------------------------------------------------- */
 102 /*** file scope functions ************************************************************************/
 103 /* --------------------------------------------------------------------------------------------- */
 104 
 105 static inline int
 106 TAB_SKIP (int ts, int pos)
     /* [previous][next][first][last][top][bottom][index][help]  */
 107 {
 108     if (ts > 0 && ts < 9)
 109         return ts - pos % ts;
 110     else
 111         return 8 - pos % 8;
 112 }
 113 
 114 /* --------------------------------------------------------------------------------------------- */
 115 
 116 /**
 117  * Fill buffer by spaces
 118  *
 119  * @param buf buffer
 120  * @param n number of spaces
 121  * @param zero_terminate add a nul after @n spaces
 122  */
 123 static void
 124 fill_by_space (char *buf, size_t n, gboolean zero_terminate)
     /* [previous][next][first][last][top][bottom][index][help]  */
 125 {
 126     memset (buf, ' ', n);
 127     if (zero_terminate)
 128         buf[n] = '\0';
 129 }
 130 
 131 /* --------------------------------------------------------------------------------------------- */
 132 
 133 static gboolean
 134 rewrite_backup_content (const vfs_path_t *from_file_name_vpath, const char *to_file_name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 135 {
 136     FILE *backup_fd;
 137     char *contents;
 138     gsize length;
 139     const char *from_file_name;
 140 
 141     from_file_name = vfs_path_get_last_path_str (from_file_name_vpath);
 142     if (!g_file_get_contents (from_file_name, &contents, &length, NULL))
 143         return FALSE;
 144 
 145     backup_fd = fopen (to_file_name, "w");
 146     if (backup_fd == NULL)
 147     {
 148         g_free (contents);
 149         return FALSE;
 150     }
 151 
 152     length = fwrite ((const void *) contents, length, 1, backup_fd);
 153 
 154     fflush (backup_fd);
 155     fclose (backup_fd);
 156     g_free (contents);
 157     return TRUE;
 158 }
 159 
 160 /* buffered I/O ************************************************************* */
 161 
 162 /**
 163  * Try to open a temporary file.
 164  * @note the name is not altered if this function fails
 165  *
 166  * @param[out] name address of a pointer to store the temporary name
 167  * @return file descriptor on success, negative on error
 168  */
 169 
 170 static int
 171 open_temp (void **name)
     /* [previous][next][first][last][top][bottom][index][help]  */
 172 {
 173     int fd;
 174     vfs_path_t *diff_file_name = NULL;
 175 
 176     fd = mc_mkstemps (&diff_file_name, "mcdiff", NULL);
 177     if (fd == -1)
 178     {
 179         file_error_message (_ ("Cannot create temporary diff file"), NULL);
 180         return -1;
 181     }
 182 
 183     *name = vfs_path_free (diff_file_name, FALSE);
 184     return fd;
 185 }
 186 
 187 /* --------------------------------------------------------------------------------------------- */
 188 
 189 /**
 190  * Allocate file structure and associate file descriptor to it.
 191  *
 192  * @param fd file descriptor
 193  * @return file structure
 194  */
 195 
 196 static FBUF *
 197 dview_fdopen (int fd)
     /* [previous][next][first][last][top][bottom][index][help]  */
 198 {
 199     FBUF *fs;
 200 
 201     if (fd < 0)
 202         return NULL;
 203 
 204     fs = g_try_malloc (sizeof (FBUF));
 205     if (fs == NULL)
 206         return NULL;
 207 
 208     fs->buf = g_try_malloc (FILE_READ_BUF);
 209     if (fs->buf == NULL)
 210     {
 211         g_free (fs);
 212         return NULL;
 213     }
 214 
 215     fs->fd = fd;
 216     FILE_DIRTY (fs);
 217     fs->flags = 0;
 218     fs->data = NULL;
 219 
 220     return fs;
 221 }
 222 
 223 /* --------------------------------------------------------------------------------------------- */
 224 
 225 /**
 226  * Free file structure without closing the file.
 227  *
 228  * @param fs file structure
 229  * @return 0 on success, non-zero on error
 230  */
 231 
 232 static int
 233 dview_ffree (FBUF *fs)
     /* [previous][next][first][last][top][bottom][index][help]  */
 234 {
 235     int rv = 0;
 236 
 237     if ((fs->flags & FILE_FLAG_TEMP) != 0)
 238     {
 239         rv = unlink (fs->data);
 240         g_free (fs->data);
 241     }
 242     g_free (fs->buf);
 243     g_free (fs);
 244     return rv;
 245 }
 246 
 247 /* --------------------------------------------------------------------------------------------- */
 248 
 249 /**
 250  * Open a binary temporary file in R/W mode.
 251  * @note the file will be deleted when closed
 252  *
 253  * @return file structure
 254  */
 255 static FBUF *
 256 dview_ftemp (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 257 {
 258     int fd;
 259     FBUF *fs;
 260 
 261     fs = dview_fdopen (0);
 262     if (fs == NULL)
 263         return NULL;
 264 
 265     fd = open_temp (&fs->data);
 266     if (fd < 0)
 267     {
 268         dview_ffree (fs);
 269         return NULL;
 270     }
 271 
 272     fs->fd = fd;
 273     fs->flags = FILE_FLAG_TEMP;
 274     return fs;
 275 }
 276 
 277 /* --------------------------------------------------------------------------------------------- */
 278 
 279 /**
 280  * Open a binary file in specified mode.
 281  *
 282  * @param filename file name
 283  * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
 284  *
 285  * @return file structure
 286  */
 287 
 288 static FBUF *
 289 dview_fopen (const char *filename, int flags)
     /* [previous][next][first][last][top][bottom][index][help]  */
 290 {
 291     int fd;
 292     FBUF *fs;
 293 
 294     fs = dview_fdopen (0);
 295     if (fs == NULL)
 296         return NULL;
 297 
 298     fd = open (filename, flags);
 299     if (fd < 0)
 300     {
 301         dview_ffree (fs);
 302         return NULL;
 303     }
 304 
 305     fs->fd = fd;
 306     return fs;
 307 }
 308 
 309 /* --------------------------------------------------------------------------------------------- */
 310 
 311 /**
 312  * Read a line of bytes from file until newline or EOF.
 313  * @note does not stop on null-byte
 314  * @note buf will not be null-terminated
 315  *
 316  * @param buf destination buffer
 317  * @param size size of buffer
 318  * @param fs file structure
 319  *
 320  * @return number of bytes read
 321  */
 322 
 323 static size_t
 324 dview_fgets (char *buf, size_t size, FBUF *fs)
     /* [previous][next][first][last][top][bottom][index][help]  */
 325 {
 326     size_t j = 0;
 327 
 328     do
 329     {
 330         int i;
 331         gboolean stop = FALSE;
 332 
 333         for (i = fs->pos; j < size && i < fs->len && !stop; i++, j++)
 334         {
 335             buf[j] = fs->buf[i];
 336             if (buf[j] == '\n')
 337                 stop = TRUE;
 338         }
 339         fs->pos = i;
 340 
 341         if (j == size || stop)
 342             break;
 343 
 344         fs->pos = 0;
 345         fs->len = read (fs->fd, fs->buf, FILE_READ_BUF);
 346     }
 347     while (fs->len > 0);
 348 
 349     return j;
 350 }
 351 
 352 /* --------------------------------------------------------------------------------------------- */
 353 
 354 /**
 355  * Seek into file.
 356  * @note avoids thrashing read cache when possible
 357  *
 358  * @param fs file structure
 359  * @param off offset
 360  * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
 361  *
 362  * @return position in file, starting from beginning
 363  */
 364 
 365 static off_t
 366 dview_fseek (FBUF *fs, off_t off, int whence)
     /* [previous][next][first][last][top][bottom][index][help]  */
 367 {
 368     off_t rv;
 369 
 370     if (fs->len != 0 && whence != SEEK_END)
 371     {
 372         rv = lseek (fs->fd, 0, SEEK_CUR);
 373         if (rv != -1)
 374         {
 375             if (whence == SEEK_CUR)
 376             {
 377                 whence = SEEK_SET;
 378                 off += rv - fs->len + fs->pos;
 379             }
 380             if (off - rv >= -fs->len && off - rv <= 0)
 381             {
 382                 fs->pos = fs->len + off - rv;
 383                 return off;
 384             }
 385         }
 386     }
 387 
 388     rv = lseek (fs->fd, off, whence);
 389     if (rv != -1)
 390         FILE_DIRTY (fs);
 391     return rv;
 392 }
 393 
 394 /* --------------------------------------------------------------------------------------------- */
 395 
 396 /**
 397  * Seek to the beginning of file, thrashing read cache.
 398  *
 399  * @param fs file structure
 400  *
 401  * @return 0 if success, non-zero on error
 402  */
 403 
 404 static off_t
 405 dview_freset (FBUF *fs)
     /* [previous][next][first][last][top][bottom][index][help]  */
 406 {
 407     off_t rv;
 408 
 409     rv = lseek (fs->fd, 0, SEEK_SET);
 410     if (rv != -1)
 411         FILE_DIRTY (fs);
 412     return rv;
 413 }
 414 
 415 /* --------------------------------------------------------------------------------------------- */
 416 
 417 /**
 418  * Write bytes to file.
 419  * @note thrashes read cache
 420  *
 421  * @param fs file structure
 422  * @param buf source buffer
 423  * @param size size of buffer
 424  *
 425  * @return number of written bytes, -1 on error
 426  */
 427 
 428 static ssize_t
 429 dview_fwrite (FBUF *fs, const char *buf, size_t size)
     /* [previous][next][first][last][top][bottom][index][help]  */
 430 {
 431     ssize_t rv;
 432 
 433     rv = write (fs->fd, buf, size);
 434     if (rv >= 0)
 435         FILE_DIRTY (fs);
 436     return rv;
 437 }
 438 
 439 /* --------------------------------------------------------------------------------------------- */
 440 
 441 /**
 442  * Truncate file to the current position.
 443  * @note thrashes read cache
 444  *
 445  * @param fs file structure
 446  *
 447  * @return current file size on success, negative on error
 448  */
 449 
 450 static off_t
 451 dview_ftrunc (FBUF *fs)
     /* [previous][next][first][last][top][bottom][index][help]  */
 452 {
 453     off_t off;
 454 
 455     off = lseek (fs->fd, 0, SEEK_CUR);
 456     if (off != -1)
 457     {
 458         int rv;
 459 
 460         rv = ftruncate (fs->fd, off);
 461         if (rv != 0)
 462             off = -1;
 463         else
 464             FILE_DIRTY (fs);
 465     }
 466     return off;
 467 }
 468 
 469 /* --------------------------------------------------------------------------------------------- */
 470 
 471 /**
 472  * Close file.
 473  * @note if this is temporary file, it is deleted
 474  *
 475  * @param fs file structure
 476  * @return 0 on success, non-zero on error
 477  */
 478 
 479 static int
 480 dview_fclose (FBUF *fs)
     /* [previous][next][first][last][top][bottom][index][help]  */
 481 {
 482     int rv = -1;
 483 
 484     if (fs != NULL)
 485     {
 486         rv = close (fs->fd);
 487         dview_ffree (fs);
 488     }
 489 
 490     return rv;
 491 }
 492 
 493 /* --------------------------------------------------------------------------------------------- */
 494 
 495 /**
 496  * Create pipe stream to process.
 497  *
 498  * @param cmd shell command line
 499  * @param flags open mode, either O_RDONLY or O_WRONLY
 500  *
 501  * @return file structure
 502  */
 503 
 504 static FBUF *
 505 dview_popen (const char *cmd, int flags)
     /* [previous][next][first][last][top][bottom][index][help]  */
 506 {
 507     FILE *f;
 508     FBUF *fs;
 509     const char *type = NULL;
 510 
 511     if (flags == O_RDONLY)
 512         type = "r";
 513     else if (flags == O_WRONLY)
 514         type = "w";
 515 
 516     if (type == NULL)
 517         return NULL;
 518 
 519     fs = dview_fdopen (0);
 520     if (fs == NULL)
 521         return NULL;
 522 
 523     f = popen (cmd, type);
 524     if (f == NULL)
 525     {
 526         dview_ffree (fs);
 527         return NULL;
 528     }
 529 
 530     fs->fd = fileno (f);
 531     fs->data = f;
 532     return fs;
 533 }
 534 
 535 /* --------------------------------------------------------------------------------------------- */
 536 
 537 /**
 538  * Close pipe stream.
 539  *
 540  * @param fs structure
 541  * @return 0 on success, non-zero on error
 542  */
 543 
 544 static int
 545 dview_pclose (FBUF *fs)
     /* [previous][next][first][last][top][bottom][index][help]  */
 546 {
 547     int rv = -1;
 548 
 549     if (fs != NULL)
 550     {
 551         rv = pclose (fs->data);
 552         dview_ffree (fs);
 553     }
 554 
 555     return rv;
 556 }
 557 
 558 /* --------------------------------------------------------------------------------------------- */
 559 
 560 /**
 561  * Get one 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                 file_error_message (_ ("Cannot create backup file\n%s~~~"), dview->file[n_merge]);
2133                 return;
2134             }
2135         }
2136 
2137         merge_file_fd = mc_mkstemps (&merge_file_name_vpath, "mcmerge", NULL);
2138         if (merge_file_fd == -1)
2139         {
2140             file_error_message (_ ("Cannot create temporary merge file"), NULL);
2141             return;
2142         }
2143 
2144         merge_file = fdopen (merge_file_fd, "w");
2145 
2146         switch (hunk)
2147         {
2148         case DIFF_DEL:
2149             if (merge_direction == FROM_RIGHT_TO_LEFT)
2150                 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_RIGHT_TO_LEFT);
2151             else
2152                 dview_remove_hunk (dview, merge_file, from1, to1, FROM_LEFT_TO_RIGHT);
2153             break;
2154         case DIFF_ADD:
2155             if (merge_direction == FROM_RIGHT_TO_LEFT)
2156                 dview_remove_hunk (dview, merge_file, from1, to1, FROM_RIGHT_TO_LEFT);
2157             else
2158                 dview_add_hunk (dview, merge_file, from1, from2, to2, FROM_LEFT_TO_RIGHT);
2159             break;
2160         case DIFF_CHG:
2161             dview_replace_hunk (dview, merge_file, from1, to1, from2, to2, merge_direction);
2162             break;
2163         default:
2164             break;
2165         }
2166         fflush (merge_file);
2167         fclose (merge_file);
2168         {
2169             int res;
2170 
2171             res = rewrite_backup_content (merge_file_name_vpath, dview->file[n_merge]);
2172             (void) res;
2173         }
2174         mc_unlink (merge_file_name_vpath);
2175         vfs_path_free (merge_file_name_vpath, TRUE);
2176     }
2177 }
2178 
2179 /* --------------------------------------------------------------------------------------------- */
2180 /* view routines and callbacks ********************************************** */
2181 
2182 static void
2183 dview_compute_split (WDiff *dview, int i)
     /* [previous][next][first][last][top][bottom][index][help]  */
2184 {
2185     dview->bias += i;
2186     if (dview->bias < 2 - dview->half1)
2187         dview->bias = 2 - dview->half1;
2188     if (dview->bias > dview->half2 - 2)
2189         dview->bias = dview->half2 - 2;
2190 }
2191 
2192 /* --------------------------------------------------------------------------------------------- */
2193 
2194 static void
2195 dview_compute_areas (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2196 {
2197     Widget *w = WIDGET (dview);
2198 
2199     dview->height = w->rect.lines - 1;
2200     dview->half1 = w->rect.cols / 2;
2201     dview->half2 = w->rect.cols - dview->half1;
2202 
2203     dview_compute_split (dview, 0);
2204 }
2205 
2206 /* --------------------------------------------------------------------------------------------- */
2207 
2208 static void
2209 dview_reread (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2210 {
2211     int ndiff;
2212 
2213     destroy_hdiff (dview);
2214     if (dview->a[DIFF_LEFT] != NULL)
2215         g_array_free (dview->a[DIFF_LEFT], TRUE);
2216     if (dview->a[DIFF_RIGHT] != NULL)
2217         g_array_free (dview->a[DIFF_RIGHT], TRUE);
2218 
2219     dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2220     g_array_set_clear_func (dview->a[DIFF_LEFT], cc_free_elt);
2221     dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2222     g_array_set_clear_func (dview->a[DIFF_RIGHT], cc_free_elt);
2223 
2224     ndiff = redo_diff (dview);
2225     if (ndiff >= 0)
2226         dview->ndiff = ndiff;
2227 }
2228 
2229 /* --------------------------------------------------------------------------------------------- */
2230 
2231 static void
2232 dview_set_codeset (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2233 {
2234     const char *encoding_id = NULL;
2235 
2236     dview->utf8 = TRUE;
2237     encoding_id = get_codepage_id (mc_global.source_codepage >= 0 ? mc_global.source_codepage
2238                                                                   : mc_global.display_codepage);
2239     if (encoding_id != NULL)
2240     {
2241         GIConv conv;
2242 
2243         conv = str_crt_conv_from (encoding_id);
2244         if (conv != INVALID_CONV)
2245         {
2246             if (dview->converter != str_cnv_from_term)
2247                 str_close_conv (dview->converter);
2248             dview->converter = conv;
2249         }
2250         dview->utf8 = (gboolean) str_isutf8 (encoding_id);
2251     }
2252 }
2253 
2254 /* --------------------------------------------------------------------------------------------- */
2255 
2256 static void
2257 dview_select_encoding (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2258 {
2259     if (do_select_codepage ())
2260         dview_set_codeset (dview);
2261     dview_reread (dview);
2262     tty_touch_screen ();
2263     repaint_screen ();
2264 }
2265 
2266 /* --------------------------------------------------------------------------------------------- */
2267 
2268 static void
2269 dview_load_options (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2270 {
2271     gboolean show_numbers;
2272     int tab_size;
2273 
2274     dview->display_symbols =
2275         mc_config_get_bool (mc_global.main_config, "DiffView", "show_symbols", FALSE);
2276     show_numbers = mc_config_get_bool (mc_global.main_config, "DiffView", "show_numbers", FALSE);
2277     if (show_numbers)
2278         dview->display_numbers = 1;
2279     tab_size = mc_config_get_int (mc_global.main_config, "DiffView", "tab_size", 8);
2280     if (tab_size > 0 && tab_size < 9)
2281         dview->tab_size = tab_size;
2282     else
2283         dview->tab_size = 8;
2284 
2285     dview->opt.quality = mc_config_get_int (mc_global.main_config, "DiffView", "diff_quality", 0);
2286 
2287     dview->opt.strip_trailing_cr =
2288         mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_tws", FALSE);
2289     dview->opt.ignore_all_space =
2290         mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_all_space", FALSE);
2291     dview->opt.ignore_space_change =
2292         mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_space_change", FALSE);
2293     dview->opt.ignore_tab_expansion =
2294         mc_config_get_bool (mc_global.main_config, "DiffView", "diff_tab_expansion", FALSE);
2295     dview->opt.ignore_case =
2296         mc_config_get_bool (mc_global.main_config, "DiffView", "diff_ignore_case", FALSE);
2297 
2298     dview->new_frame = TRUE;
2299 }
2300 
2301 /* --------------------------------------------------------------------------------------------- */
2302 
2303 static void
2304 dview_save_options (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2305 {
2306     mc_config_set_bool (mc_global.main_config, "DiffView", "show_symbols", dview->display_symbols);
2307     mc_config_set_bool (mc_global.main_config, "DiffView", "show_numbers",
2308                         dview->display_numbers != 0);
2309     mc_config_set_int (mc_global.main_config, "DiffView", "tab_size", dview->tab_size);
2310 
2311     mc_config_set_int (mc_global.main_config, "DiffView", "diff_quality", dview->opt.quality);
2312 
2313     mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_tws",
2314                         dview->opt.strip_trailing_cr);
2315     mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_all_space",
2316                         dview->opt.ignore_all_space);
2317     mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_space_change",
2318                         dview->opt.ignore_space_change);
2319     mc_config_set_bool (mc_global.main_config, "DiffView", "diff_tab_expansion",
2320                         dview->opt.ignore_tab_expansion);
2321     mc_config_set_bool (mc_global.main_config, "DiffView", "diff_ignore_case",
2322                         dview->opt.ignore_case);
2323 }
2324 
2325 /* --------------------------------------------------------------------------------------------- */
2326 
2327 static void
2328 dview_diff_options (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2329 {
2330     const char *quality_str[] = {
2331         N_ ("No&rmal"),
2332         N_ ("&Fastest (Assume large files)"),
2333         N_ ("&Minimal (Find a smaller set of change)"),
2334     };
2335 
2336     quick_widget_t quick_widgets[] = {
2337         // clang-format off
2338         QUICK_START_GROUPBOX (N_ ("Diff algorithm")),
2339             QUICK_RADIO (3, (const char **) quality_str, (int *) &dview->opt.quality, NULL),
2340         QUICK_STOP_GROUPBOX,
2341         QUICK_START_GROUPBOX (N_ ("Diff extra options")),
2342             QUICK_CHECKBOX (N_ ("&Ignore case"), &dview->opt.ignore_case, NULL),
2343             QUICK_CHECKBOX (N_ ("Ignore tab &expansion"), &dview->opt.ignore_tab_expansion, NULL),
2344             QUICK_CHECKBOX (N_ ("Ignore &space change"), &dview->opt.ignore_space_change, NULL),
2345             QUICK_CHECKBOX (N_ ("Ignore all &whitespace"), &dview->opt.ignore_all_space, NULL),
2346             QUICK_CHECKBOX (N_ ("Strip &trailing carriage return"), &dview->opt.strip_trailing_cr,
2347                             NULL),
2348         QUICK_STOP_GROUPBOX,
2349         QUICK_BUTTONS_OK_CANCEL,
2350         QUICK_END,
2351         // clang-format on
2352     };
2353 
2354     WRect r = { -1, -1, 0, 56 };
2355 
2356     quick_dialog_t qdlg = {
2357         .rect = r,
2358         .title = N_ ("Diff Options"),
2359         .help = "[Diff Options]",
2360         .widgets = quick_widgets,
2361         .callback = NULL,
2362         .mouse_callback = NULL,
2363     };
2364 
2365     if (quick_dialog (&qdlg) != B_CANCEL)
2366         dview_reread (dview);
2367 }
2368 
2369 /* --------------------------------------------------------------------------------------------- */
2370 
2371 static int
2372 dview_init (WDiff *dview, const char *args, const char *file1, const char *file2,
     /* [previous][next][first][last][top][bottom][index][help]  */
2373             const char *label1, const char *label2, DSRC dsrc)
2374 {
2375     FBUF *f[DIFF_COUNT];
2376 
2377     f[DIFF_LEFT] = NULL;
2378     f[DIFF_RIGHT] = NULL;
2379 
2380     if (dsrc == DATA_SRC_TMP)
2381     {
2382         f[DIFF_LEFT] = dview_ftemp ();
2383         if (f[DIFF_LEFT] == NULL)
2384             return -1;
2385 
2386         f[DIFF_RIGHT] = dview_ftemp ();
2387         if (f[DIFF_RIGHT] == NULL)
2388         {
2389             dview_fclose (f[DIFF_LEFT]);
2390             return -1;
2391         }
2392     }
2393     else if (dsrc == DATA_SRC_ORG)
2394     {
2395         f[DIFF_LEFT] = dview_fopen (file1, O_RDONLY);
2396         if (f[DIFF_LEFT] == NULL)
2397             return -1;
2398 
2399         f[DIFF_RIGHT] = dview_fopen (file2, O_RDONLY);
2400         if (f[DIFF_RIGHT] == NULL)
2401         {
2402             dview_fclose (f[DIFF_LEFT]);
2403             return -1;
2404         }
2405     }
2406 
2407     dview->view_quit = FALSE;
2408 
2409     dview->bias = 0;
2410     dview->new_frame = TRUE;
2411     dview->skip_rows = 0;
2412     dview->skip_cols = 0;
2413     dview->display_symbols = FALSE;
2414     dview->display_numbers = 0;
2415     dview->show_cr = TRUE;
2416     dview->tab_size = 8;
2417     dview->ord = DIFF_LEFT;
2418     dview->full = FALSE;
2419 
2420     dview->search.handle = NULL;
2421     dview->search.last_string = NULL;
2422     dview->search.last_found_line = -1;
2423     dview->search.last_accessed_num_line = -1;
2424 
2425     dview_load_options (dview);
2426 
2427     dview->args = args;
2428     dview->file[DIFF_LEFT] = file1;
2429     dview->file[DIFF_RIGHT] = file2;
2430     dview->label[DIFF_LEFT] = g_strdup (label1);
2431     dview->label[DIFF_RIGHT] = g_strdup (label2);
2432     dview->f[DIFF_LEFT] = f[0];
2433     dview->f[DIFF_RIGHT] = f[1];
2434     dview->merged[DIFF_LEFT] = FALSE;
2435     dview->merged[DIFF_RIGHT] = FALSE;
2436     dview->hdiff = NULL;
2437     dview->dsrc = dsrc;
2438     dview->converter = str_cnv_from_term;
2439     dview_set_codeset (dview);
2440     dview->a[DIFF_LEFT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2441     g_array_set_clear_func (dview->a[DIFF_LEFT], cc_free_elt);
2442     dview->a[DIFF_RIGHT] = g_array_new (FALSE, FALSE, sizeof (DIFFLN));
2443     g_array_set_clear_func (dview->a[DIFF_RIGHT], cc_free_elt);
2444 
2445     return 0;
2446 }
2447 
2448 /* --------------------------------------------------------------------------------------------- */
2449 
2450 static void
2451 dview_fini (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2452 {
2453     if (dview->dsrc != DATA_SRC_MEM)
2454     {
2455         dview_fclose (dview->f[DIFF_RIGHT]);
2456         dview_fclose (dview->f[DIFF_LEFT]);
2457     }
2458 
2459     if (dview->converter != str_cnv_from_term)
2460         str_close_conv (dview->converter);
2461 
2462     destroy_hdiff (dview);
2463     if (dview->a[DIFF_LEFT] != NULL)
2464     {
2465         g_array_free (dview->a[DIFF_LEFT], TRUE);
2466         dview->a[DIFF_LEFT] = NULL;
2467     }
2468     if (dview->a[DIFF_RIGHT] != NULL)
2469     {
2470         g_array_free (dview->a[DIFF_RIGHT], TRUE);
2471         dview->a[DIFF_RIGHT] = NULL;
2472     }
2473 
2474     g_free (dview->label[DIFF_LEFT]);
2475     g_free (dview->label[DIFF_RIGHT]);
2476 }
2477 
2478 /* --------------------------------------------------------------------------------------------- */
2479 
2480 static int
2481 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]  */
2482 {
2483     size_t i, k;
2484     int j;
2485     char buf[BUFSIZ];
2486     FBUF *f = dview->f[ord];
2487     int skip = dview->skip_cols;
2488     gboolean display_symbols = dview->display_symbols;
2489     int display_numbers = dview->display_numbers;
2490     gboolean show_cr = dview->show_cr;
2491     int tab_size = 8;
2492     const DIFFLN *p;
2493     int nwidth = display_numbers;
2494     int xwidth;
2495 
2496     xwidth = display_numbers;
2497     if (display_symbols)
2498         xwidth++;
2499     if (dview->tab_size > 0 && dview->tab_size < 9)
2500         tab_size = dview->tab_size;
2501 
2502     if (xwidth != 0)
2503     {
2504         if (xwidth > width && display_symbols)
2505         {
2506             xwidth--;
2507             display_symbols = FALSE;
2508         }
2509         if (xwidth > width && display_numbers != 0)
2510         {
2511             xwidth = width;
2512             display_numbers = width;
2513         }
2514 
2515         xwidth++;
2516         c += xwidth;
2517         width -= xwidth;
2518         if (width < 0)
2519             width = 0;
2520     }
2521 
2522     if ((int) sizeof (buf) <= width || (int) sizeof (buf) <= nwidth)
2523     {
2524         // abnormal, but avoid buffer overflow
2525         return -1;
2526     }
2527 
2528     for (i = dview->skip_rows, j = 0; i < dview->a[ord]->len && j < height; j++, i++)
2529     {
2530         int ch;
2531         int next_ch = 0;
2532         int col;
2533         size_t cnt;
2534 
2535         p = (DIFFLN *) &g_array_index (dview->a[ord], DIFFLN, i);
2536         ch = p->ch;
2537         tty_setcolor (NORMAL_COLOR);
2538         if (display_symbols)
2539         {
2540             tty_gotoyx (r + j, c - 2);
2541             tty_print_char (ch);
2542         }
2543         if (p->line != 0)
2544         {
2545             if (display_numbers != 0)
2546             {
2547                 tty_gotoyx (r + j, c - xwidth);
2548                 g_snprintf (buf, display_numbers + 1, "%*d", nwidth, p->line);
2549                 tty_print_string (str_fit_to_term (buf, nwidth, J_LEFT_FIT));
2550             }
2551             if (ch == ADD_CH)
2552                 tty_setcolor (DFF_ADD_COLOR);
2553             if (ch == CHG_CH)
2554                 tty_setcolor (DFF_CHG_COLOR);
2555             if (f == NULL)
2556             {
2557                 if (i == (size_t) dview->search.last_found_line)
2558                     tty_setcolor (MARKED_SELECTED_COLOR);
2559                 else if (dview->hdiff != NULL && g_ptr_array_index (dview->hdiff, i) != NULL)
2560                 {
2561                     char att[BUFSIZ];
2562 
2563                     if (dview->utf8)
2564                         k = dview_str_utf8_offset_to_pos (p->p, width);
2565                     else
2566                         k = width;
2567 
2568                     cvt_mgeta (p->p, p->u.len, buf, k, skip, tab_size, show_cr,
2569                                g_ptr_array_index (dview->hdiff, i), ord, att);
2570                     tty_gotoyx (r + j, c);
2571                     col = 0;
2572 
2573                     for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2574                     {
2575                         gboolean ch_res;
2576 
2577                         if (dview->utf8)
2578                         {
2579                             int ch_length = 0;
2580 
2581                             ch_res = dview_get_utf (buf + cnt, &next_ch, &ch_length);
2582                             if (ch_length > 1)
2583                                 cnt += ch_length - 1;
2584                             if (!g_unichar_isprint (next_ch))
2585                                 next_ch = '.';
2586                         }
2587                         else
2588                             ch_res = dview_get_byte (buf + cnt, &next_ch);
2589 
2590                         if (ch_res)
2591                         {
2592                             tty_setcolor (att[cnt] ? DFF_CHH_COLOR : DFF_CHG_COLOR);
2593                             if (mc_global.utf8_display)
2594                             {
2595                                 if (!dview->utf8)
2596                                 {
2597                                     next_ch = convert_from_8bit_to_utf_c ((unsigned char) next_ch,
2598                                                                           dview->converter);
2599                                 }
2600                             }
2601                             else if (dview->utf8)
2602                                 next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2603                             else
2604                                 next_ch = convert_to_display_c (next_ch);
2605 
2606                             tty_print_anychar (next_ch);
2607                             col++;
2608                         }
2609                     }
2610                     continue;
2611                 }
2612 
2613                 if (ch == CHG_CH)
2614                     tty_setcolor (DFF_CHH_COLOR);
2615 
2616                 if (dview->utf8)
2617                     k = dview_str_utf8_offset_to_pos (p->p, width);
2618                 else
2619                     k = width;
2620                 cvt_mget (p->p, p->u.len, buf, k, skip, tab_size, show_cr);
2621             }
2622             else
2623                 cvt_fget (f, p->u.off, buf, width, skip, tab_size, show_cr);
2624         }
2625         else
2626         {
2627             if (display_numbers != 0)
2628             {
2629                 tty_gotoyx (r + j, c - xwidth);
2630                 fill_by_space (buf, display_numbers, TRUE);
2631                 tty_print_string (buf);
2632             }
2633             if (ch == DEL_CH)
2634                 tty_setcolor (DFF_DEL_COLOR);
2635             if (ch == CHG_CH)
2636                 tty_setcolor (DFF_CHD_COLOR);
2637             fill_by_space (buf, width, TRUE);
2638         }
2639         tty_gotoyx (r + j, c);
2640         // tty_print_nstring (buf, width);
2641         col = 0;
2642         for (cnt = 0; cnt < strlen (buf) && col < width; cnt++)
2643         {
2644             gboolean ch_res;
2645 
2646             if (dview->utf8)
2647             {
2648                 int ch_length = 0;
2649 
2650                 ch_res = dview_get_utf (buf + cnt, &next_ch, &ch_length);
2651                 if (ch_length > 1)
2652                     cnt += ch_length - 1;
2653                 if (!g_unichar_isprint (next_ch))
2654                     next_ch = '.';
2655             }
2656             else
2657                 ch_res = dview_get_byte (buf + cnt, &next_ch);
2658 
2659             if (ch_res)
2660             {
2661                 if (mc_global.utf8_display)
2662                 {
2663                     if (!dview->utf8)
2664                         next_ch =
2665                             convert_from_8bit_to_utf_c ((unsigned char) next_ch, dview->converter);
2666                 }
2667                 else if (dview->utf8)
2668                     next_ch = convert_from_utf_to_current_c (next_ch, dview->converter);
2669                 else
2670                     next_ch = convert_to_display_c (next_ch);
2671 
2672                 tty_print_anychar (next_ch);
2673                 col++;
2674             }
2675         }
2676     }
2677     tty_setcolor (NORMAL_COLOR);
2678     k = width;
2679     if (width < xwidth - 1)
2680         k = xwidth - 1;
2681     fill_by_space (buf, k, TRUE);
2682     for (; j < height; j++)
2683     {
2684         if (xwidth != 0)
2685         {
2686             tty_gotoyx (r + j, c - xwidth);
2687             // tty_print_nstring (buf, xwidth - 1);
2688             tty_print_string (str_fit_to_term (buf, xwidth - 1, J_LEFT_FIT));
2689         }
2690         tty_gotoyx (r + j, c);
2691         // tty_print_nstring (buf, width);
2692         tty_print_string (str_fit_to_term (buf, width, J_LEFT_FIT));
2693     }
2694 
2695     return 0;
2696 }
2697 
2698 /* --------------------------------------------------------------------------------------------- */
2699 
2700 static void
2701 dview_status (const WDiff *dview, diff_place_t ord, int width, int c)
     /* [previous][next][first][last][top][bottom][index][help]  */
2702 {
2703     const char *buf;
2704     int filename_width;
2705     int linenum, lineofs;
2706     vfs_path_t *vpath;
2707     char *path;
2708 
2709     tty_setcolor (STATUSBAR_COLOR);
2710 
2711     tty_gotoyx (0, c);
2712     get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2713 
2714     filename_width = width - 24;
2715     if (filename_width < 8)
2716         filename_width = 8;
2717 
2718     vpath = vfs_path_from_str (dview->label[ord]);
2719     path = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_HOME | VPF_STRIP_PASSWORD);
2720     vfs_path_free (vpath, TRUE);
2721     buf = str_term_trim (path, filename_width);
2722     if (ord == DIFF_LEFT)
2723         tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview->merged[ord] ? "* " : "  ", filename_width,
2724                     buf, linenum, lineofs, dview->skip_cols);
2725     else
2726         tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview->merged[ord] ? "* " : "  ", filename_width,
2727                     buf, linenum, lineofs, dview->ndiff);
2728     g_free (path);
2729 }
2730 
2731 /* --------------------------------------------------------------------------------------------- */
2732 
2733 static void
2734 dview_redo (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2735 {
2736     if (dview->display_numbers != 0)
2737     {
2738         int old;
2739 
2740         old = dview->display_numbers;
2741         dview->display_numbers = calc_nwidth ((const GArray *const *) dview->a);
2742         dview->new_frame = (old != dview->display_numbers);
2743     }
2744     dview_reread (dview);
2745 }
2746 
2747 /* --------------------------------------------------------------------------------------------- */
2748 
2749 static void
2750 dview_update (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2751 {
2752     int height = dview->height;
2753     int width1, width2;
2754     int last;
2755 
2756     last = dview->a[DIFF_LEFT]->len - 1;
2757 
2758     if (dview->skip_rows > last)
2759         dview->skip_rows = dview->search.last_accessed_num_line = last;
2760     if (dview->skip_rows < 0)
2761         dview->skip_rows = dview->search.last_accessed_num_line = 0;
2762     if (dview->skip_cols < 0)
2763         dview->skip_cols = 0;
2764 
2765     if (height < 2)
2766         return;
2767 
2768     // use an actual length of dview->a
2769     if (dview->display_numbers != 0)
2770         dview->display_numbers = calc_nwidth ((const GArray *const *) dview->a);
2771 
2772     width1 = dview->half1 + dview->bias;
2773     width2 = dview->half2 - dview->bias;
2774     if (dview->full)
2775     {
2776         width1 = COLS;
2777         width2 = 0;
2778     }
2779 
2780     if (dview->new_frame)
2781     {
2782         int xwidth;
2783 
2784         tty_setcolor (NORMAL_COLOR);
2785         xwidth = dview->display_numbers;
2786         if (dview->display_symbols)
2787             xwidth++;
2788         if (width1 > 1)
2789             tty_draw_box (1, 0, height, width1, FALSE);
2790         if (width2 > 1)
2791             tty_draw_box (1, width1, height, width2, FALSE);
2792 
2793         if (xwidth != 0)
2794         {
2795             xwidth++;
2796             if (xwidth < width1 - 1)
2797             {
2798                 tty_gotoyx (1, xwidth);
2799                 tty_print_alt_char (ACS_TTEE, FALSE);
2800                 tty_gotoyx (height, xwidth);
2801                 tty_print_alt_char (ACS_BTEE, FALSE);
2802                 tty_draw_vline (2, xwidth, ACS_VLINE, height - 2);
2803             }
2804             if (xwidth < width2 - 1)
2805             {
2806                 tty_gotoyx (1, width1 + xwidth);
2807                 tty_print_alt_char (ACS_TTEE, FALSE);
2808                 tty_gotoyx (height, width1 + xwidth);
2809                 tty_print_alt_char (ACS_BTEE, FALSE);
2810                 tty_draw_vline (2, width1 + xwidth, ACS_VLINE, height - 2);
2811             }
2812         }
2813         dview->new_frame = FALSE;
2814     }
2815 
2816     if (width1 > 2)
2817     {
2818         dview_status (dview, dview->ord, width1, 0);
2819         dview_display_file (dview, dview->ord, 2, 1, height - 2, width1 - 2);
2820     }
2821     if (width2 > 2)
2822     {
2823         diff_place_t ord;
2824 
2825         ord = dview->ord == DIFF_LEFT ? DIFF_RIGHT : DIFF_LEFT;
2826         dview_status (dview, ord, width2, width1);
2827         dview_display_file (dview, ord, 2, width1 + 1, height - 2, width2 - 2);
2828     }
2829 }
2830 
2831 /* --------------------------------------------------------------------------------------------- */
2832 
2833 static void
2834 dview_edit (WDiff *dview, diff_place_t ord)
     /* [previous][next][first][last][top][bottom][index][help]  */
2835 {
2836     Widget *h;
2837     gboolean h_modal;
2838     int linenum, lineofs;
2839 
2840     if (dview->dsrc == DATA_SRC_TMP)
2841     {
2842         error_dialog (_ ("Edit"), _ ("Edit is disabled"));
2843         return;
2844     }
2845 
2846     h = WIDGET (WIDGET (dview)->owner);
2847     h_modal = widget_get_state (h, WST_MODAL);
2848 
2849     get_line_numbers (dview->a[ord], dview->skip_rows, &linenum, &lineofs);
2850 
2851     // disallow edit file in several editors
2852     widget_set_state (h, WST_MODAL, TRUE);
2853 
2854     {
2855         vfs_path_t *tmp_vpath;
2856 
2857         tmp_vpath = vfs_path_from_str (dview->file[ord]);
2858         edit_file_at_line (tmp_vpath, use_internal_edit, linenum);
2859         vfs_path_free (tmp_vpath, TRUE);
2860     }
2861 
2862     widget_set_state (h, WST_MODAL, h_modal);
2863     dview_redo (dview);
2864     dview_update (dview);
2865 }
2866 
2867 /* --------------------------------------------------------------------------------------------- */
2868 
2869 static void
2870 dview_goto_cmd (WDiff *dview, diff_place_t ord)
     /* [previous][next][first][last][top][bottom][index][help]  */
2871 {
2872     static gboolean first_run = TRUE;
2873 
2874     static const char *title[2] = {
2875         N_ ("Goto line (left)"),
2876         N_ ("Goto line (right)"),
2877     };
2878 
2879     int newline;
2880     char *input;
2881 
2882     input = input_dialog (_ (title[ord]), _ ("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE,
2883                           first_run ? NULL : INPUT_LAST_TEXT, INPUT_COMPLETE_NONE);
2884     if (input != NULL)
2885     {
2886         const char *s = input;
2887 
2888         if (scan_deci (&s, &newline) == 0 && *s == '\0')
2889         {
2890             size_t i = 0;
2891 
2892             if (newline > 0)
2893                 for (; i < dview->a[ord]->len; i++)
2894                 {
2895                     const DIFFLN *p;
2896 
2897                     p = &g_array_index (dview->a[ord], DIFFLN, i);
2898                     if (p->line == newline)
2899                         break;
2900                 }
2901 
2902             dview->skip_rows = dview->search.last_accessed_num_line = (ssize_t) i;
2903         }
2904 
2905         g_free (input);
2906     }
2907 
2908     first_run = FALSE;
2909 }
2910 
2911 /* --------------------------------------------------------------------------------------------- */
2912 
2913 static void
2914 dview_labels (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2915 {
2916     Widget *d = WIDGET (dview);
2917     WButtonBar *b;
2918 
2919     b = buttonbar_find (DIALOG (d->owner));
2920 
2921     buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), d->keymap, d);
2922     buttonbar_set_label (b, 2, Q_ ("ButtonBar|Save"), d->keymap, d);
2923     buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), d->keymap, d);
2924     buttonbar_set_label (b, 5, Q_ ("ButtonBar|Merge"), d->keymap, d);
2925     buttonbar_set_label (b, 7, Q_ ("ButtonBar|Search"), d->keymap, d);
2926     buttonbar_set_label (b, 9, Q_ ("ButtonBar|Options"), d->keymap, d);
2927     buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), d->keymap, d);
2928 }
2929 
2930 /* --------------------------------------------------------------------------------------------- */
2931 
2932 static gboolean
2933 dview_save (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2934 {
2935     gboolean res = TRUE;
2936 
2937     if (dview->merged[DIFF_LEFT])
2938     {
2939         res = mc_util_unlink_backup_if_possible (dview->file[DIFF_LEFT], "~~~");
2940         dview->merged[DIFF_LEFT] = !res;
2941     }
2942     if (dview->merged[DIFF_RIGHT])
2943     {
2944         res = mc_util_unlink_backup_if_possible (dview->file[DIFF_RIGHT], "~~~");
2945         dview->merged[DIFF_RIGHT] = !res;
2946     }
2947     return res;
2948 }
2949 
2950 /* --------------------------------------------------------------------------------------------- */
2951 
2952 static void
2953 dview_do_save (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2954 {
2955     (void) dview_save (dview);
2956 }
2957 
2958 /* --------------------------------------------------------------------------------------------- */
2959 
2960 /*
2961  * Check if it's OK to close the diff viewer.  If there are unsaved changes,
2962  * ask user.
2963  */
2964 static gboolean
2965 dview_ok_to_exit (WDiff *dview)
     /* [previous][next][first][last][top][bottom][index][help]  */
2966 {
2967     gboolean res = TRUE;
2968     int act;
2969     char *text;
2970 
2971     if (!dview->merged[DIFF_LEFT] && !dview->merged[DIFF_RIGHT])
2972         return res;
2973 
2974     text = g_strdup_printf (!mc_global.midnight_shutdown
2975                                 ? _ ("File(s) was modified. Save with exit?")
2976                                 : _ ("%s is being shut down.\nSave modified file(s)?"),
2977                             PACKAGE_NAME);
2978     act = query_dialog (_ ("Quit"), text, D_NORMAL, 2, _ ("&Yes"), _ ("&No"));
2979     g_free (text);
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, const ssize_t width)
     /* [previous][next][first][last][top][bottom][index][help]  */
3298 {
3299     const WDiff *dview;
3300     const char *modified = " (*) ";
3301     const char *notmodified = "     ";
3302     ssize_t width1;
3303     GString *title;
3304 
3305     dview = (const WDiff *) widget_find_by_type (CONST_WIDGET (h), dview_callback);
3306     width1 = (width - str_term_width1 (_ ("Diff:")) - strlen (modified) - 3) / 2;
3307     if (width1 < 0)
3308     {
3309         // It's very unlikely that width1 becomes negative at some time.
3310         // This means that width is too small and dialog window is too narrow.
3311         // Set width1 to some reasonable value.
3312         width1 = width / 2;
3313     }
3314 
3315     title = g_string_sized_new (width);
3316     g_string_append (title, _ ("Diff:"));
3317     g_string_append (title, dview->merged[DIFF_LEFT] ? modified : notmodified);
3318     g_string_append (title, str_term_trim (dview->label[DIFF_LEFT], width1));
3319     g_string_append (title, " | ");
3320     g_string_append (title, dview->merged[DIFF_RIGHT] ? modified : notmodified);
3321     g_string_append (title, str_term_trim (dview->label[DIFF_RIGHT], width1));
3322 
3323     return g_string_free (title, FALSE);
3324 }
3325 
3326 /* --------------------------------------------------------------------------------------------- */
3327 
3328 static int
3329 diff_view (const char *file1, const char *file2, const char *label1, const char *label2)
     /* [previous][next][first][last][top][bottom][index][help]  */
3330 {
3331     int error;
3332     WDiff *dview;
3333     Widget *w;
3334     WDialog *dview_dlg;
3335     Widget *dw;
3336     WRect r;
3337     WGroup *g;
3338 
3339     // Create dialog and widgets, put them on the dialog
3340     dview_dlg = dlg_create (FALSE, 0, 0, 1, 1, WPOS_FULLSCREEN, FALSE, NULL, dview_dialog_callback,
3341                             NULL, "[Diff Viewer]", NULL);
3342     dw = WIDGET (dview_dlg);
3343     widget_want_tab (dw, TRUE);
3344     r = dw->rect;
3345 
3346     g = GROUP (dview_dlg);
3347 
3348     dview = g_new0 (WDiff, 1);
3349     w = WIDGET (dview);
3350     r.lines--;
3351     widget_init (w, &r, dview_callback, dview_mouse_callback);
3352     w->options |= WOP_SELECTABLE;
3353     w->keymap = diff_map;
3354     group_add_widget_autopos (g, w, WPOS_KEEP_ALL, NULL);
3355 
3356     w = WIDGET (buttonbar_new ());
3357     group_add_widget_autopos (g, w, w->pos_flags, NULL);
3358 
3359     dview_dlg->get_title = dview_get_title;
3360 
3361     error =
3362         dview_init (dview, "-a", file1, file2, label1, label2, DATA_SRC_MEM);  // XXX binary diff?
3363     if (error >= 0)
3364         error = redo_diff (dview);
3365     if (error >= 0)
3366     {
3367         dview->ndiff = error;
3368         dview_compute_areas (dview);
3369         error = 0;
3370     }
3371 
3372     if (error == 0)
3373         dlg_run (dview_dlg);
3374 
3375     if (error != 0 || widget_get_state (dw, WST_CLOSED))
3376         widget_destroy (dw);
3377 
3378     return error == 0 ? 1 : 0;
3379 }
3380 
3381 /*** public functions ****************************************************************************/
3382 /* --------------------------------------------------------------------------------------------- */
3383 
3384 #define GET_FILE_AND_STAMP(n)                                                                      \
3385     do                                                                                             \
3386     {                                                                                              \
3387         use_copy##n = 0;                                                                           \
3388         real_file##n = file##n;                                                                    \
3389         if (!vfs_file_is_local (file##n))                                                          \
3390         {                                                                                          \
3391             real_file##n = mc_getlocalcopy (file##n);                                              \
3392             if (real_file##n != NULL)                                                              \
3393             {                                                                                      \
3394                 use_copy##n = 1;                                                                   \
3395                 if (mc_stat (real_file##n, &st##n) != 0)                                           \
3396                     use_copy##n = -1;                                                              \
3397             }                                                                                      \
3398         }                                                                                          \
3399     }                                                                                              \
3400     while (0)
3401 
3402 #define UNGET_FILE(n)                                                                              \
3403     do                                                                                             \
3404     {                                                                                              \
3405         if (use_copy##n != 0)                                                                      \
3406         {                                                                                          \
3407             gboolean changed = FALSE;                                                              \
3408             if (use_copy##n > 0)                                                                   \
3409             {                                                                                      \
3410                 time_t mtime;                                                                      \
3411                 mtime = st##n.st_mtime;                                                            \
3412                 if (mc_stat (real_file##n, &st##n) == 0)                                           \
3413                     changed = (mtime != st##n.st_mtime);                                           \
3414             }                                                                                      \
3415             mc_ungetlocalcopy (file##n, real_file##n, changed);                                    \
3416             vfs_path_free (real_file##n, TRUE);                                                    \
3417         }                                                                                          \
3418     }                                                                                              \
3419     while (0)
3420 
3421 gboolean
3422 dview_diff_cmd (const void *f0, const void *f1)
     /* [previous][next][first][last][top][bottom][index][help]  */
3423 {
3424     int rv = 0;
3425     vfs_path_t *file0 = NULL;
3426     vfs_path_t *file1 = NULL;
3427     gboolean is_dir0 = FALSE;
3428     gboolean is_dir1 = FALSE;
3429 
3430     switch (mc_global.mc_run_mode)
3431     {
3432     case MC_RUN_FULL:
3433     {
3434         // run from panels
3435         const WPanel *panel0 = (const WPanel *) f0;
3436         const WPanel *panel1 = (const WPanel *) f1;
3437         const file_entry_t *fe0, *fe1;
3438 
3439         fe0 = panel_current_entry (panel0);
3440         if (fe0 == NULL)
3441         {
3442             message (D_ERROR, MSG_ERROR, "%s", _ ("File name is empty!"));
3443             goto ret;
3444         }
3445 
3446         file0 = vfs_path_append_new (panel0->cwd_vpath, fe0->fname->str, (char *) NULL);
3447         is_dir0 = S_ISDIR (fe0->st.st_mode);
3448         if (is_dir0)
3449         {
3450             message (D_ERROR, MSG_ERROR, _ ("%s\nis a directory"), fe0->fname->str);
3451             goto ret;
3452         }
3453 
3454         fe1 = panel_current_entry (panel1);
3455         if (fe1 == NULL)
3456         {
3457             message (D_ERROR, MSG_ERROR, "%s", _ ("File name is empty!"));
3458             goto ret;
3459         }
3460         file1 = vfs_path_append_new (panel1->cwd_vpath, fe1->fname->str, (char *) NULL);
3461         is_dir1 = S_ISDIR (fe1->st.st_mode);
3462         if (is_dir1)
3463         {
3464             message (D_ERROR, MSG_ERROR, _ ("%s\nis a directory"), fe1->fname->str);
3465             goto ret;
3466         }
3467         break;
3468     }
3469 
3470     case MC_RUN_DIFFVIEWER:
3471     {
3472         // run from command line
3473         const char *p0 = (const char *) f0;
3474         const char *p1 = (const char *) f1;
3475         struct stat st;
3476 
3477         file0 = vfs_path_from_str (p0);
3478         if (mc_stat (file0, &st) == 0)
3479         {
3480             is_dir0 = S_ISDIR (st.st_mode);
3481             if (is_dir0)
3482             {
3483                 message (D_ERROR, MSG_ERROR, _ ("%s\nis a directory"), p0);
3484                 goto ret;
3485             }
3486         }
3487         else
3488         {
3489             file_error_message (_ ("Cannot stat\n%s"), p0);
3490             goto ret;
3491         }
3492 
3493         file1 = vfs_path_from_str (p1);
3494         if (mc_stat (file1, &st) == 0)
3495         {
3496             is_dir1 = S_ISDIR (st.st_mode);
3497             if (is_dir1)
3498             {
3499                 message (D_ERROR, MSG_ERROR, _ ("%s\nis a directory"), p1);
3500                 goto ret;
3501             }
3502         }
3503         else
3504         {
3505             file_error_message (_ ("Cannot stat\n%s"), p1);
3506             goto ret;
3507         }
3508         break;
3509     }
3510 
3511     default:
3512         // this should not happened
3513         message (D_ERROR, MSG_ERROR, _ ("Diff viewer: invalid mode"));
3514         return FALSE;
3515     }
3516 
3517     if (rv == 0)
3518     {
3519         rv = -1;
3520         if (file0 != NULL && file1 != NULL)
3521         {
3522             int use_copy0, use_copy1;
3523             struct stat st0, st1;
3524             vfs_path_t *real_file0, *real_file1;
3525 
3526             GET_FILE_AND_STAMP (0);
3527             GET_FILE_AND_STAMP (1);
3528 
3529             if (real_file0 != NULL && real_file1 != NULL)
3530                 rv = diff_view (vfs_path_as_str (real_file0), vfs_path_as_str (real_file1),
3531                                 vfs_path_as_str (file0), vfs_path_as_str (file1));
3532 
3533             UNGET_FILE (1);
3534             UNGET_FILE (0);
3535         }
3536     }
3537 
3538     if (rv == 0)
3539         message (D_ERROR, MSG_ERROR, _ ("Two files are needed to compare"));
3540 
3541 ret:
3542     vfs_path_free (file1, TRUE);
3543     vfs_path_free (file0, TRUE);
3544 
3545     return (rv != 0);
3546 }
3547 
3548 #undef GET_FILE_AND_STAMP
3549 #undef UNGET_FILE
3550 
3551 /* --------------------------------------------------------------------------------------------- */

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