File: bif_diff.c

package info (click to toggle)
virtuoso-opensource 6.1.6+dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 260,992 kB
  • ctags: 125,220
  • sloc: ansic: 652,748; sql: 458,419; xml: 282,834; java: 61,031; sh: 40,031; cpp: 36,890; cs: 25,240; php: 12,692; yacc: 9,523; lex: 7,018; makefile: 6,157; jsp: 4,484; awk: 1,643; perl: 1,013; ruby: 1,003; python: 326
file content (747 lines) | stat: -rw-r--r-- 19,589 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
 *  sqlbif.c
 *
 *  $Id$
 *
 *  Diff functionality
 *
 *  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
 *  project.
 *
 *  Copyright (C) 1998-2012 OpenLink Software
 *
 *  This project is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; only version 2 of the License, dated June 1991.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include "libutil.h"
#include "sqlnode.h"
#include "sqlbif.h"
#include "wi.h"

#define DIFF_MATCH_VAL	(+100)
#define DIFF_DELETE_VAL	(-1)
#define DIFF_INSERT_VAL	(+1)

#define DIFF_MATCH (box_num(DIFF_MATCH_VAL))
#define DIFF_DELETE (box_num(DIFF_DELETE_VAL))
#define DIFF_INSERT (box_num(DIFF_INSERT_VAL))

#define DIFF_MATCH_STR	"matched: "
#define DIFF_DELETE_STR	"delete: "
#define DIFF_INSERT_STR "insert: "

#define DIFF_MODE_GNU		1
#define DIFF_MODE_VIRT		2
#define DIFF_MODE_CTX		3
#define DIFF_MODE_DIFF_E	4 /* not real diff -e output, some mix of -e and --normal */


#define DIFF_CMD_RANGE_DELETE 1
#define DIFF_CMD_RANGE_INSERT 2

#define DIFF_APPLY_STATE_INSERT	101

#define DIFF_CMD_CMD		1
#define DIFF_CMD_DELINS		2
#define DIFF_CMD_UNKNOWN	-1

#define DIFF_MAX_LENGTH	1300
#define DIFF_MAX_LENGTH_STR "1300"

#define NO_NEW_LINE_SUFFIX "\n\\ No newline at end of file\n"

typedef struct diff_cmd_s {
  int	dc_type;
  int	dc_from;
  int	dc_to;
} diff_cmd_t;


ptrlong * LCS_Delta (ptrlong * X, ptrlong * Y)
{
  int cwidth = BOX_ELEMENTS (Y) + 1;
  int ylen = box_length (Y) / sizeof (ptrlong);
  int xlen = box_length (X) / sizeof (ptrlong);
  ptrlong * c = (ptrlong*) dk_alloc_box ((xlen+1)*(1+ylen)*sizeof(ptrlong), DV_ARRAY_OF_LONG);
  ptrlong * b = (ptrlong*) dk_alloc_box ((xlen+1)*(1+ylen)*sizeof(ptrlong), DV_ARRAY_OF_LONG);
  int i,j;
  for (i=1; i<=xlen; i++)
    c[i*cwidth] = 0;
  for (j=0; j<=ylen; j++)
    c[j] = 0;
  for (i=0; i<(1+xlen)*(1+ylen); i++)
    b[i] = 0;
  for (i=1; i<=xlen; i++)
    {
      for (j=1; j<=ylen; j++)
	{
	  if (X[i-1] == Y[j-1])
	    {
	      c[i*cwidth + j] = c[(i-1)*cwidth + j - 1] + 1;
	      b[i*cwidth + j] = 1; /* up-left */
	    }
	  else if (c[(i-1)*cwidth + j] >= c[i*cwidth + j-1])
	    {
	      c[i*cwidth + j] = c[(i-1)*cwidth + j];
	      b[i*cwidth + j] = 2; /* up */
	    }
	  else
	    {
	      c[i*cwidth + j] = c[i*cwidth + j-1];
	      b[i*cwidth + j] = 3; /* left */
	    }
	}
    }
  dk_free_box ((box_t) c);
  return b;
}

int print_LCS (ptrlong * X, ptrlong * Y, ptrlong * b)
{
  int ylen = box_length (Y) / sizeof (ptrlong);
  int xlen = box_length (X) / sizeof (ptrlong);
  int i, j;
  int cwidth = ylen + 1;
  for (i = 0; i <= xlen; i++)
    {
      for (j = 0; j <= ylen; j++)
	{
	  int r = b[i*cwidth + j];
	  if (r == 1)
	    printf ("%ld,%ld\t", X[i-1], Y[j-1]);
	  else if (r == 2)
	    printf ("up\t");
	  else if (r == 3)
	    printf ("left\t");
	  else
	    printf ("-1\t");
	}
      printf ("\n");
    }
  return 0;
}


caddr_t* build_path_from_LCS (ptrlong * X, ptrlong * Y, ptrlong * b)
{
  int ylen = box_length (Y) / sizeof (ptrlong);
  int xlen = box_length (X) / sizeof (ptrlong);
  int i,j;
  int r;
  int cwidth = ylen + 1;
  dk_set_t path = 0;
  int state = 0;
  i = xlen;
  j = ylen;

  while ( (i >= 1) && (j >= 1))
    {
      r = b[i*cwidth+j];
      if (state && (r != 1))
	{
	  dk_set_push (&path, DIFF_MATCH);
	  dk_set_push (&path, box_num (state));
	  state = 0;
	}
      if (r == 1)
	{
	  state++;
	  --i;
	  --j;
	}
      else if (r == 2)
	{

	  dk_set_push (&path, DIFF_DELETE);
	  dk_set_push (&path, box_num (X[i-1]));
	  --i;
	}
      else if (r == 3)
	{
	  dk_set_push (&path, DIFF_INSERT);
	  dk_set_push (&path, box_num (Y[j-1]));
	  j--;
	}
    }
  if (state)
    {
      dk_set_push (&path, DIFF_MATCH);
      dk_set_push (&path, box_num (state));
    }
  while (i >= 1)
    {
      dk_set_push (&path, DIFF_DELETE);
      dk_set_push (&path, box_num (X[--i]));
    }
  while (j >= 1)
    {
      dk_set_push (&path, DIFF_INSERT);
      dk_set_push (&path, box_num (Y[--j]));
    }
  return (caddr_t*)list_to_array (dk_set_nreverse (path));
}

caddr_t parse_text_to_lines_1 (caddr_t * start, caddr_t end_text,
			       int detect_new_line /* 0 - do not detect
						      1 - just detect new line and write the suffix
						      2 - detect suffix and add new line if needed */ )
{
  caddr_t pointer, line_start;
  line_start = pointer = start[0];
  while (line_start < end_text)
    {
      if ((pointer[0] == '\r') ||
	  (pointer[0] == '\n') ||
	  (pointer == end_text))
	{
	  caddr_t line;
	  caddr_t old_pointer;
	  if ((1 == detect_new_line) && (pointer == end_text)) /* no new line at the end */
	    {
	      line = dk_alloc_box (pointer - line_start + strlen (NO_NEW_LINE_SUFFIX) + 1 /* 0 */, DV_STRING);
	      strncpy (line, line_start, (pointer - line_start));
	      strcpy (line + (pointer - line_start), NO_NEW_LINE_SUFFIX);
	      start[0] = pointer;
	      return line;
	    }
	  old_pointer = pointer;
	  if ('\r' == pointer[0])
	    pointer++;
	  if ('\n' == pointer[0])
	    pointer++;
	  if ((2 == detect_new_line) && ('\\' == pointer[0])) /* no new line suffix */
	    {
	      line = box_dv_short_nchars (line_start, old_pointer - line_start);
	      while ((pointer < end_text) && (pointer[0] != '\n'))
		++pointer;
	    }
	  else
	    {
	      line = dk_alloc_box (old_pointer - line_start + 2 /* \n + 0 */, DV_STRING);
	      strncpy (line, line_start, (old_pointer - line_start));
	      line[old_pointer - line_start] = '\n';
	      line[old_pointer - line_start + 1] = '\0';
	    }
	  start[0] = pointer;
	  return line;
	}
      if ((line_start[0] != '\r') &&
	  (line_start[0] != '\n'))
	pointer++;
    }
  return NULL;
}


#define istext(ch) (((ch) & 0xF8) && ((ch != 0x08)))

static int binstrchr (caddr_t line)
{
  int idx;
  for (idx = 0; idx < box_length(line) - 1; ++idx)
    {
      if (!istext (line[idx]))
	return idx;
    }
  return -1;
}

int parse_text_to_lines (caddr_t text, id_hash_t* lhash, ptrlong** res_array, ptrlong* currid, caddr_t ** line_array)
{
  caddr_t pointer, line_start, end_text;
  dk_set_t res_set = NULL;
  int res = -1;
  line_start = pointer = text;
  end_text = text + box_length (text) - 1;
  while (line_start < end_text)
    {
      caddr_t line = parse_text_to_lines_1 (&line_start, end_text, 1);
      if (line)
	{
	  ptrlong * idptr;
	  ptrlong id;
	  if (0 <= binstrchr (line))
	    {
	      dk_free_box ((box_t) line);
	      goto ret;
	    }
	  idptr = (ptrlong*) id_hash_get (lhash, (caddr_t) &line);
	  if (!idptr)
	    {
	      id = ++currid[0];
	      id_hash_set (lhash, (caddr_t)(&line), (caddr_t)(&id));
	    }
	  else
	    {
	      id = idptr[0];
	      dk_free_box ((box_t) line);
	    }
	  dk_set_push (&res_set, (caddr_t)id);
	}
      else
	break;
    }
  res = 1;
 ret:
  res_array [0] = (ptrlong*) list_to_array (res_set);
  if (line_array)
    {
      id_hash_iterator_t hit;
      ptrlong* id;
      caddr_t* line;
      line_array[0] = (caddr_t*) dk_alloc_box ((1+currid[0]) * sizeof(caddr_t), DV_ARRAY_OF_POINTER);
      memset (line_array[0], 0, (1+currid[0]) * sizeof(caddr_t));
      id_hash_iterator (&hit, lhash);

      while (hit_next (&hit, (char **)&line, (char **)&id))
	{
	  if (id && line)
	    line_array[0][id[0]] = line[0];
	}
    }
  return res;
}


caddr_t path_to_text (caddr_t* path, caddr_t * line_array, ptrlong mode)
{
  dk_session_t * ses;
  caddr_t text;
  int inx = 0;
  long line_A_counter = 0, line_B_counter = 0;
  ses = strses_allocate ();

  if ((mode != DIFF_MODE_GNU) &&
      (mode != DIFF_MODE_VIRT) &&
      (mode != DIFF_MODE_DIFF_E))
    return box_string ("not supported!!!");

  while (inx < BOX_ELEMENTS (path))
    {
      ptrlong op = unbox (path[inx]);
      if (op == DIFF_MATCH_VAL)
	{
	  ptrlong skip = unbox (path[++inx]);
	  if ((DIFF_MODE_GNU == mode) ||
	      (DIFF_MODE_DIFF_E == mode))
	    {
	      line_A_counter+=skip;
	      line_B_counter+=skip;
	    }
	  else if (mode == DIFF_MODE_VIRT)
	    {
	      char tmp[255];
	      sprintf (tmp, "%ld\n", skip);
	      session_buffered_write (ses, DIFF_MATCH_STR, strlen (DIFF_MATCH_STR));
	      session_buffered_write (ses, tmp, strlen(tmp));
	    }
	  ++inx;
	  continue;
	}
      else
	{
	  ptrlong id;
	  caddr_t line;
	  if ((op != DIFF_INSERT_VAL)  &&
	      (op != DIFF_DELETE_VAL))
	    GPF_T;
	  if (inx + 1 >= BOX_ELEMENTS (path))
	    GPF_T;
	  id = unbox (path[++inx]);
	  line = line_array[id];
	  if (op == DIFF_DELETE_VAL)
	    ++line_A_counter;
	  else if (op == DIFF_INSERT_VAL)
	    ++line_B_counter;
	  if (mode == DIFF_MODE_VIRT)
	    {
	      if (op == DIFF_INSERT_VAL)
		session_buffered_write (ses, DIFF_INSERT_STR, strlen (DIFF_INSERT_STR));
	      else if (op == DIFF_DELETE_VAL)
		session_buffered_write (ses, DIFF_DELETE_STR, strlen (DIFF_DELETE_STR));
	      else
		GPF_T;
	      if (id >= BOX_ELEMENTS (line_array))
		GPF_T;
	      session_buffered_write (ses, line, strlen (line));
/* 	      session_buffered_write_char ('\n', ses);  */
	    }
	  else if ( (DIFF_MODE_GNU == mode) ||
		    (DIFF_MODE_DIFF_E == mode) )
	    {
	      char tmp[255];
	      char prefix[3];
	      int forward_inx = inx - 1;
	      int delins_ops;
	      while (((forward_inx + 2) < BOX_ELEMENTS (path)) &&
		     (unbox (path[forward_inx+2]) == op))
		forward_inx += 2;
	      delins_ops = 1 + (forward_inx - inx + 1)/2;
	      if (DIFF_MODE_GNU == mode)
		{
		  if(1 == delins_ops) /* deleted, inserted one line */
		    sprintf (tmp, "%ld%s%ld\n", line_A_counter, op == DIFF_INSERT_VAL ? "a" : "d", line_B_counter);
		  else if (op == DIFF_DELETE_VAL)
		    sprintf (tmp, "%ld,%ldd%ld\n", line_A_counter, line_A_counter + delins_ops - 1, line_B_counter);
		  else if (op == DIFF_INSERT_VAL)
		    sprintf (tmp, "%lda%ld,%ld\n", line_A_counter, line_B_counter, line_B_counter + delins_ops - 1);
		  if (DIFF_DELETE_VAL == op)
		    line_A_counter += delins_ops - 1;
		  else if (DIFF_INSERT_VAL == op)
		    line_B_counter += delins_ops - 1;
		}
	      else
		{
		  if(1 == delins_ops) /* deleted, inserted one line */
		    sprintf (tmp, "%ld%s\n", line_A_counter, op == DIFF_INSERT_VAL ? "a" : "d");
		  else if (op == DIFF_DELETE_VAL)
		    sprintf (tmp, "%ld,%ldd\n", line_A_counter, line_A_counter + delins_ops - 1);
		  else if (op == DIFF_INSERT_VAL)
		    sprintf (tmp, "%lda\n", line_A_counter);
		  if (DIFF_DELETE_VAL == op)
		    line_A_counter += delins_ops - 1;
		  else if (DIFF_INSERT_VAL == op)
		    line_B_counter += delins_ops - 1;
		}
	      session_buffered_write (ses, tmp, strlen (tmp));
	      if (op == DIFF_INSERT_VAL)
		strcpy (prefix, "> ");
	      else
		strcpy (prefix, "< ");
	      if ((DIFF_MODE_GNU == mode) ||
		  ( (DIFF_MODE_DIFF_E == mode) &&
		    (DIFF_INSERT_VAL == op) ))
		{
		  while (1)
		    {
		      if ((op != DIFF_INSERT_VAL)  &&
			  (op != DIFF_DELETE_VAL))
			GPF_T;
		      line = line_array[id];
		      session_buffered_write (ses, prefix, strlen (prefix));
		      session_buffered_write (ses, line, strlen (line));
/* 		      session_buffered_write_char ('\n', ses); */

		      ++inx; ++inx;
		      if (inx > (forward_inx + 1))
			break;
		      id = unbox (path[inx]);
		      op = unbox (path[inx-1]);
		    }
		}
	      inx = forward_inx + 1;
	    }
	}
      inx++;
    }
  text = strses_string (ses);
  strses_free (ses);
  return text;
}


static
int diff_cmd_get (diff_cmd_t * cmd, caddr_t cmd_line)
{
  caddr_t _to;
  if ((cmd_line[0] == '>') ||
      (cmd_line[0] == '<') )
    return DIFF_CMD_DELINS;
  if ( (_to = strchr (cmd_line, 'd')) )
    {
      caddr_t _comma = strchr (cmd_line, ',');
      cmd->dc_type = DIFF_CMD_RANGE_DELETE;
      cmd->dc_from = atoi (cmd_line);
      if (_comma)
	cmd->dc_to = atoi (_comma + 1);
      else
	cmd->dc_to = cmd->dc_from;
      return DIFF_CMD_CMD;
    }
  else if ( (_to = strchr (cmd_line, 'a')) )
    {
      cmd->dc_type = DIFF_CMD_RANGE_INSERT;
      cmd->dc_from = atoi (cmd_line);
      cmd->dc_to = cmd->dc_from;
      return DIFF_CMD_CMD;
    }
  return DIFF_CMD_UNKNOWN;
}

static
void diff_apply_fill_text (caddr_t * curr_text, caddr_t end_text, int * curr_line, int from, dk_session_t * ses)
{
  int lines = from - curr_line[0] + 1;
  while ( (from < 0) || (lines--))
    {
      caddr_t line = parse_text_to_lines_1 (curr_text, end_text, 0);
      if (line)
	{
	  session_buffered_write (ses, line, strlen(line));
/* 	  session_buffered_write_char ('\n', ses); */
	  curr_line[0]++;
	  dk_free_box ((box_t) line);
	}
      else
	break;
    }
}

static
void diff_apply_delete (caddr_t * curr_text, caddr_t end_text, int * curr_line)
{
  caddr_t line = parse_text_to_lines_1 (curr_text, end_text, 0);
  curr_line[0]++;
  dk_free_box ((box_t) line);
}



static
void diff_apply_insert (char * insert_line,
			dk_session_t * ses)
{
  session_buffered_write (ses, insert_line, strlen (insert_line));
}

static
caddr_t diff_apply (caddr_t text, caddr_t patch, ptrlong mode)
{
  caddr_t curr_text;
  caddr_t start = curr_text = text;
  caddr_t end = start + box_length (start) - 1;
  dk_session_t * ses = strses_allocate ();
  caddr_t res_text;
  int state = 0;
  int curr_line = 1;
  caddr_t patch_start = patch;
  caddr_t patch_end = patch_start + box_length (patch_start) - 1;
  dk_set_t cmds = NULL;
  s_node_t * el;
  while (patch_start < patch_end)
    {
      caddr_t cmd_line = parse_text_to_lines_1 (&patch_start, patch_end, 2);
      dk_set_push (&cmds, cmd_line);
    }
  cmds = dk_set_nreverse (cmds);
  el = cmds;
  while (el)
    {
      caddr_t cmd_line = (caddr_t) el->data;
      if (cmd_line)
	{
	  diff_cmd_t cmd;
	  switch (diff_cmd_get (&cmd, cmd_line))
	    {
	    case DIFF_CMD_CMD:
	      {
		state = 0;
		if (cmd.dc_type == DIFF_CMD_RANGE_DELETE)
		  {
		    diff_apply_fill_text (&curr_text, end, &curr_line, cmd.dc_from - 1, ses);
		    while (curr_line <= cmd.dc_to)
		      diff_apply_delete (&curr_text, end, &curr_line);
		  }
		else if (cmd.dc_type == DIFF_CMD_RANGE_INSERT)
		  {
		    diff_apply_fill_text (&curr_text, end, &curr_line, cmd.dc_from, ses);
		    state = DIFF_APPLY_STATE_INSERT;
		  }
	      } break;
	    case DIFF_CMD_DELINS:
	      {
		if (DIFF_APPLY_STATE_INSERT == state)
		  {
		    diff_apply_insert (cmd_line + 2 /* skip "> " prefix */,
				       ses);
		  }
	      } break;
	    default:
	      break;
	    }
	  dk_free_box ((box_t) cmd_line);
	}
      el = el->next;
    }
  dk_set_free (cmds);
  diff_apply_fill_text (&curr_text, end, &curr_line, -1, ses);
  res_text = strses_string (ses);
  strses_free (ses);
  return res_text;
}

static
ptrlong bif_diff_mode_arg (caddr_t *qst, state_slot_t ** args, int nth, const char* funcname)
{
  ptrlong mode = DIFF_MODE_GNU;
  caddr_t mode_str = bif_string_arg (qst, args, nth, funcname);
  if (!strcmp (mode_str, "--virt2"))
    mode = DIFF_MODE_DIFF_E;
  else if (!strcmp (mode_str, "--normal"))
    mode = DIFF_MODE_GNU;
  else if (!strcmp (mode_str, "--virt"))
    mode = DIFF_MODE_VIRT;
  return mode;
}

static
long count_lines (caddr_t text)
{
  caddr_t pointer = text;
  long cnt = 0;
  while ( (pointer = strchr(pointer, '\n')) )
    {
      ++pointer;
      ++cnt;
    }
  return cnt;
}

static
caddr_t bif_diff (caddr_t *qst, caddr_t * err_ret, state_slot_t ** args)
{
  caddr_t source_doc = bif_string_arg  (qst, args, 0, "bif_diff");
  caddr_t dest_doc = bif_string_arg (qst, args, 1, "bif_diff");
  id_hash_t * line_hash = id_hash_allocate (1021, sizeof (caddr_t), sizeof (ptrlong), strhash, strhashcmp);
  caddr_t * line_array;
  ptrlong * source_array = 0;
  ptrlong * dest_array = 0;
  ptrlong * LCS;
  caddr_t path_text;
  ptrlong currid = -1;
  caddr_t * path;
  ptrlong mode = DIFF_MODE_GNU;
  if (BOX_ELEMENTS (args) > 2)
    mode = bif_diff_mode_arg (qst, args, 2, "bif_diff");

  if ((count_lines (source_doc) > DIFF_MAX_LENGTH) ||
      (count_lines (dest_doc) > DIFF_MAX_LENGTH))
    sqlr_new_error ("DF001", "SR479", "Too long document, must not exceed %d lines",  DIFF_MAX_LENGTH);

  if (0 > parse_text_to_lines (source_doc, line_hash, &source_array, &currid, NULL))
    {
      dk_free_box ((box_t) source_array);
      sqlr_new_error ("DF002", "SR480", "Source file is in binary format");
    }

  if (0 > parse_text_to_lines (dest_doc, line_hash, &dest_array, &currid, &line_array))
    {
      dk_free_box ((box_t) source_array);
      dk_free_box ((box_t) dest_array);
      sqlr_new_error ("DF002", "SR481", "Destination file is in binary format");
    }

  LCS = LCS_Delta (source_array, dest_array);
  path = build_path_from_LCS (source_array, dest_array, LCS);
  dk_free_box ((box_t) source_array);
  dk_free_box ((box_t) dest_array);
  dk_free_box ((box_t) LCS);

  path_text = path_to_text (path, line_array, mode);
  id_hash_free (line_hash);
  dk_free_tree ((box_t) line_array);
  dk_free_tree ((box_t) path);
  return path_text;
}

static
caddr_t bif_diff_apply (caddr_t *qst, caddr_t * err_ret, state_slot_t ** args)
{
  caddr_t text = bif_string_arg (qst, args, 0, "diff_apply");
  caddr_t patch = bif_string_arg (qst, args, 1, "diff_apply");
  ptrlong mode = DIFF_MODE_GNU;
  if (BOX_ELEMENTS (args) > 2)
    mode = bif_diff_mode_arg (qst, args, 2, "diff_apply");

  return diff_apply (text, patch, mode);
}

static
void reverse_cmd_line (char * line, char * res_line, size_t sz)
{
  int d1 = atoi (line);
  char * cmd_sep = strchr (line, 'd');
  if (!cmd_sep)
    cmd_sep = strchr (line, 'a');
  if (cmd_sep)
    {
      int d2 = atoi (cmd_sep + 1);
      if (d2)
	{
	  sprintf (res_line, "%d%c%d", d2, cmd_sep[0], d1);
	  return;
	}
    }
  strncpy (res_line, line, sz - 1);
  return;
}

static
caddr_t bif_diff_reverse (caddr_t *qst, caddr_t * err_ret, state_slot_t ** args)
{
  caddr_t patch = bif_string_arg (qst, args, 0, "diff_reverse");
  caddr_t end = patch + box_length(patch) - 1;
  caddr_t pointer = patch;
  dk_session_t * ses = strses_allocate();
  dk_set_t diff_set = 0;
  caddr_t res = 0;
  while (1)
    {
      caddr_t line = parse_text_to_lines_1 (&pointer, end, 0);
      if (line)
	{
	  if ((line[0] == '>') || (line[0] == '<') || (line[0] == '\\'))
	    {
	      session_buffered_write (ses, line, strlen(line));
	      session_buffered_write_char ('\n', ses);
	    }
	  else
	    {
	      char tmp[255];
	      memset (tmp, 0, sizeof (tmp));
	      dk_set_push (&diff_set, strses_string (ses));
	      strses_free (ses);
	      ses = strses_allocate ();
	      reverse_cmd_line (line, tmp, sizeof(tmp));
	      session_buffered_write (ses, tmp, strlen(tmp));
	      session_buffered_write_char ('\n', ses);
	    }
	  dk_free_box ((box_t) line);
	}
      else
	break;
    }
  dk_set_push (&diff_set, strses_string (ses));
  strses_free (ses);
  ses = strses_allocate ();
  DO_SET (caddr_t, diff_el, &diff_set)
    {
      session_buffered_write (ses, diff_el, strlen (diff_el));
    }
  END_DO_SET();
  res = strses_string (ses);
  strses_free (ses);
  dk_free_tree (list_to_array (diff_set));
  return res;
}

void
bif_diff_init (void)
{
  bif_define ("diff", bif_diff);
  bif_define ("diff_apply", bif_diff_apply);
  bif_define ("diff_reverse", bif_diff_reverse);
}