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