summaryrefslogtreecommitdiff
path: root/ipl/progs/yescr.icn
blob: 65e6d8bf98ac562b6878553bb785c719333c24e5 (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
136
137
138
139
140
141
############################################################################
#
#	File:     yescr.icn
#
#	Subject:  Program to convert UNIX files to DOS format
#
#	Author:   Richard L. Goerwitz
#
#	Date:     December 30, 1991
#
############################################################################
#
#   This file is in the public domain.
#
############################################################################
#
#	Version:  1.2
#
############################################################################
#  
#    This program simply inserts MS-DOS carriage-return+linefeed
#  sequences in place of UNIX newlines.  Effects conversion from the
#  native UNIX text file format to its DOS correspondent.
#
#	usage:  yescr file1 [file2 [etc.]]
#
#  Bug:  Doesn't check to see whether the input files are in fact
#  text files.
#
############################################################################
#
#  Requires:  UNIX or MS-DOS
#
#  See also: nocr.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("yescr:  tested only under UNIX and MS-DOS")
    }

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

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

	# Open input file.
	infile := open(fname,"r"||ms) | (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(temp_name), next)

	if \DOSos then {
	    # Read in blocks of 80 chars.
	    while line := reads(infile,80) do {
		line ? {
		    # Replace ASCII LF with CR+LF, effecting a translation
		    # from UNIX to DOS format.
		    while writes(outfile, tab(find("\x0A")), "\x0D", move(1))
		    writes(outfile, tab(0))
		}
	    }
	}
	else {
	    # I presume I'm running under UNIX (unless I've been hacked).
	    # Convert lines into DOS format by appending a carriage return,
	    # and then write()'ing (which automatically adds a newline).
	    every line := !infile do {
		if line[-1] == "\x0D"
		then write(outfile, line)
		else write(outfile, line || "\x0D")
	    }
	}

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

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

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

end


procedure er_out(s)
    write(&errout,"yescr:  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