home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / yescr.icn < prev    next >
Text File  |  2000-07-29  |  4KB  |  142 lines

  1. ############################################################################
  2. #
  3. #    File:     yescr.icn
  4. #
  5. #    Subject:  Program to convert UNIX files to DOS format
  6. #
  7. #    Author:   Richard L. Goerwitz
  8. #
  9. #    Date:     December 30, 1991
  10. #
  11. ############################################################################
  12. #
  13. #   This file is in the public domain.
  14. #
  15. ############################################################################
  16. #
  17. #    Version:  1.2
  18. #
  19. ############################################################################
  20. #  
  21. #    This program simply inserts MS-DOS carriage-return+linefeed
  22. #  sequences in place of UNIX newlines.  Effects conversion from the
  23. #  native UNIX text file format to its DOS correspondent.
  24. #
  25. #    usage:  yescr file1 [file2 [etc.]]
  26. #
  27. #  Bug:  Doesn't check to see whether the input files are in fact
  28. #  text files.
  29. #
  30. ############################################################################
  31. #
  32. #  Requires:  UNIX or MS-DOS
  33. #
  34. #  See also: nocr.icn
  35. #
  36. ############################################################################
  37.  
  38.  
  39. procedure main(a)
  40.  
  41.     local fname, infile, outfile, line, temp_name
  42.  
  43.     # Static variables, initial clause not really necessary in main().
  44.     static slash, l, ms, DOSos, nok, ok
  45.     initial {
  46.     nok := string(~&letters)
  47.     ok := repl("X",*nok)
  48.     # Find us a place to put temporary files.
  49.     if find("UNIX",&features) then {
  50.         slash := "/"
  51.         l := 10
  52.         ms := ""
  53.     }
  54.     else if find("MS-DOS", &features) then {
  55.         slash := "\\"
  56.         l := 8
  57.         ms := "u"
  58.         DOSos := 1
  59.     }
  60.     # Don't take this out unless you're sure of what you're doing.
  61.     else stop("yescr:  tested only under UNIX and MS-DOS")
  62.     }
  63.  
  64.     # Check to see if we have any arguments.
  65.     *a = 0 & stop("usage:  yescr file1 [file2...]")
  66.  
  67.     # Start popping filenames off of the argument list.
  68.     while fname := pop(a) do {
  69.  
  70.     # Open input file.
  71.     infile := open(fname,"r"||ms) | (er_out(fname), next)
  72.     # Get temporary file name.
  73.     every temp_name :=
  74.         pathname(fname, slash) ||
  75.         map(left(basename(fname,slash),l,"X"), nok, ok) ||
  76.         "." || right(0 to 999,3,"0")
  77.     do close(open(temp_name)) | break
  78.     # Open temporary file.
  79.     outfile := open(temp_name,"w"||ms) | (er_out(temp_name), next)
  80.  
  81.     if \DOSos then {
  82.         # Read in blocks of 80 chars.
  83.         while line := reads(infile,80) do {
  84.         line ? {
  85.             # Replace ASCII LF with CR+LF, effecting a translation
  86.             # from UNIX to DOS format.
  87.             while writes(outfile, tab(find("\x0A")), "\x0D", move(1))
  88.             writes(outfile, tab(0))
  89.         }
  90.         }
  91.     }
  92.     else {
  93.         # I presume I'm running under UNIX (unless I've been hacked).
  94.         # Convert lines into DOS format by appending a carriage return,
  95.         # and then write()'ing (which automatically adds a newline).
  96.         every line := !infile do {
  97.         if line[-1] == "\x0D"
  98.         then write(outfile, line)
  99.         else write(outfile, line || "\x0D")
  100.         }
  101.     }
  102.  
  103.     # Close opened input and output files.
  104.     close(infile)  | stop("yescr:  cannot close, ",fname,"; aborting")
  105.     close(outfile) | stop("yescr:  cannot close, ",temp_name,"; aborting")
  106.  
  107.     # Remove physical input file.
  108.     remove(fname) | stop("yescr:  cannot remove ",fname,"; aborting")
  109.  
  110.     # Give temp name the same name as the input file, completing the
  111.     # conversion process.
  112.     rename(temp_name,fname) |
  113.         stop("yescr:  Can't find temp file ",temp_name,"; aborting")
  114.     }
  115.  
  116. end
  117.  
  118.  
  119. procedure er_out(s)
  120.     write(&errout,"yescr:  cannot open ",s," for reading")
  121.     return
  122. end
  123.  
  124.  
  125. procedure basename(s,slash)
  126.     s ? {
  127.     while tab(find(slash)+1)
  128.     return tab(0)
  129.     }
  130. end
  131.  
  132.  
  133. procedure pathname(s,slash)
  134.     local s2
  135.  
  136.     s2 := ""
  137.     s ? {
  138.     while s2 ||:= tab(find(slash)+1)
  139.     return s2
  140.     }
  141. end
  142.