############################################################################
#
#	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
