root/src/diffviewer/ydiff.c

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

DEFINITIONS

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

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

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