summaryrefslogtreecommitdiff
path: root/ipl/progs/nocr.icn
blob: cde499b0c1bbae00b92c2c460a1e1165ce079059 (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
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
############################################################################
#
#	File:     nocr.icn
#
#	Subject:  Program to convert MS-DOS text files to UNIX
#
#	Author:   Richard L. Goerwitz
#
#	Date:     December 30, 1991
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#	Version:  1.4
#
############################################################################
#  
#    This program simply converts \r\n to \n in each line of each of the
#  files supplied as command-line arguments, thereby effecting conversion
#  of MS-DOS format text files to the corresponding UNIX format.
#
#	usage:  nocr file1 [file2 [etc.]]
#
#    No check done to see whether the file is in fact a text file.
#
############################################################################
#
#  Requires:  UNIX or MS-DOS
#
#  See also: yescr.icn
#
############################################################################

procedure main(a)

    local fname, infile, outfile, line, temp_name

    # Static variables, initial clause not really necessary in main().
    static slash, l, ms, DOSos, nok, ok
    initial {

	nok := string(~&letters)
	ok := repl("X",*nok)

	# Find us a place to put temporary files.
	if find("UNIX",&features) then {
	    slash := "/"
	    l := 10
	    ms := ""
	}
	else if find("MS-DOS", &features) then {
	    slash := "\\"
	    l := 8
	    ms := "u"
	    DOSos := 1
	}
	# Don't take this out unless you're sure of what you're doing.
	else stop("nocr:  tested only under UNIX and MS-DOS")
    }

    # Check to see if we have any arguments.
    *a = 0 & stop("usage:  nocr file1 [file2...]")

    # Start popping filenames off of the argument list.
    while fname := pop(a) do {

	# Open input file.
	infile := open(fname,"r") | (er_out(fname), next)
	# Get temporary file name.
	every temp_name :=
	    pathname(fname, slash) ||
	    map(left(basename(fname,slash),l,"X"), nok, ok) ||
	    "." || right(0 to 999,3,"0")
	do close(open(temp_name)) | break
	# Open temporary file.
	outfile := open(\temp_name,"w"||ms) | (er_out(fname), next)

	if \DOSos then {
	    # Infile above was opened in translate mode (removing the CR),
	    # while outfile was opened in untranslate mode (automatically
            # writing the line in UNIX format).
	    while write(outfile,read(infile))
	}
	else {
	    # If not running under DOS, then we're under UNIX (unless
	    # we've been hacked).  Trim CR manually, then write.
	    while line := read(infile) do {
                if line[-1] == "\x0D" then
		    line[-1] := ""
	        write(outfile, line)
            }
	}

	# Close opened input and output files.
	close(infile)  | stop("nocr:  cannot close, ",fname,"; aborting")
	close(outfile) | stop("nocr:  cannot close, ",temp_name,"; aborting")

	# Remove physical input file.
	remove(fname) | stop("nocr:  cannot remove ",fname,"; aborting")

	# Give temp name the same name as the input file, completing the
	# conversion process.
	rename(temp_name,fname) |
	    stop("nocr:  Can't find temp file ",temp_name,"; aborting")
    }

end


procedure er_out(s)
    write(&errout,"nocr:  cannot open ",s," for reading")
    return
end


procedure basename(s,slash)
    s ? {
	while tab(find(slash)+1)
	return tab(0)
    }
end


procedure pathname(s,slash)
    local s2

    s2 := ""
    s ? {
	while s2 ||:= tab(find(slash)+1)
	return s2
    }
end