summaryrefslogtreecommitdiff
path: root/ipl/progs/diffn.icn
blob: c98d48bc8bc50d9fe0f58d1ecb758e4ce31faaec (plain)
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
############################################################################
#
#	File:     diffn.icn
#
#	Subject:  Program to show differences among files
#
#	Author:   Robert J. Alexander
#
#	Date:     January 3, 1994
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#  
#   This program shows the differences between n files. Is is invoked as
#
#        diffn file1 file2 ... filen
#  
############################################################################
#
#  Links: dif
#
############################################################################
#
#  Most of the work is done by an external procedure, dif().  This
#  program analyzes the command line arguments, sets up a call to
#  dif(), and displays the results.
#


link dif
global f1,f2
record dfile(file,linenbr)

invocable all

procedure main(arg)
  local f, i, files, drec, status
  #
  #  Analyze command line arguments, open the files, and output
  #  some initial display lines.
  #
  if *arg < 2 then stop("usage: diffn file file ...")
  f := list(*arg)
  every i := 1 to *arg do
    	f[i] := dfile(open(arg[i]) | stop("Can't open ",arg[i]),0)
  files := list(*arg)
  every i := 1 to *arg do {
    write("File ",i,": ",arg[i])
    files[i] := diff_proc(myread,f[i])
  }
  #
  #  Invoke dif() and display its generated results.
  #
  every drec := dif(files) do {
    status := "diffs"
    write("==================================")
    every i := 1 to *drec do {
      write("---- File ",i,", ",
      		 (drec[i].pos > f[i].linenbr & "end of file") |
		 "line " || drec[i].pos,
		 " ---- (",arg[i],")")
      listrange(drec[i].diffs,drec[i].pos)
    }
  }
  if /status then write("==== Files match ====")
  return
end


#
#  listrange() -- List a range of differing lines, each preceded by its
#  line number.
#
procedure listrange(dlist,linenbr)
  local x
  every x := !dlist do {
    write(x); linenbr +:= 1
  }
  return
end


#
#  myread() -- Line-reading procedure to pass to dif().
#
procedure myread(x)
  return x.linenbr <- x.linenbr + 1 & read(x.file)
end