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 / nocr.icn < prev    next >
Text File  |  2000-07-29  |  4KB  |  136 lines

  1. ############################################################################
  2. #
  3. #    File:     nocr.icn
  4. #
  5. #    Subject:  Program to convert MS-DOS text files to UNIX
  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.4
  18. #
  19. ############################################################################
  20. #  
  21. #    This program simply converts \r\n to \n in each line of each of the
  22. #  files supplied as command-line arguments, thereby effecting conversion
  23. #  of MS-DOS format text files to the corresponding UNIX format.
  24. #
  25. #    usage:  nocr file1 [file2 [etc.]]
  26. #
  27. #    No check done to see whether the file is in fact a text file.
  28. #
  29. ############################################################################
  30. #
  31. #  Requires:  UNIX or MS-DOS
  32. #
  33. #  See also: yescr.icn
  34. #
  35. ############################################################################
  36.  
  37. procedure main(a)
  38.  
  39.     local fname, infile, outfile, line, temp_name
  40.  
  41.     # Static variables, initial clause not really necessary in main().
  42.     static slash, l, ms, DOSos, nok, ok
  43.     initial {
  44.  
  45.     nok := string(~&letters)
  46.     ok := repl("X",*nok)
  47.  
  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("nocr:  tested only under UNIX and MS-DOS")
  62.     }
  63.  
  64.     # Check to see if we have any arguments.
  65.     *a = 0 & stop("usage:  nocr 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") | (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(fname), next)
  80.  
  81.     if \DOSos then {
  82.         # Infile above was opened in translate mode (removing the CR),
  83.         # while outfile was opened in untranslate mode (automatically
  84.             # writing the line in UNIX format).
  85.         while write(outfile,read(infile))
  86.     }
  87.     else {
  88.         # If not running under DOS, then we're under UNIX (unless
  89.         # we've been hacked).  Trim CR manually, then write.
  90.         while line := read(infile) do {
  91.                 if line[-1] == "\x0D" then
  92.             line[-1] := ""
  93.             write(outfile, line)
  94.             }
  95.     }
  96.  
  97.     # Close opened input and output files.
  98.     close(infile)  | stop("nocr:  cannot close, ",fname,"; aborting")
  99.     close(outfile) | stop("nocr:  cannot close, ",temp_name,"; aborting")
  100.  
  101.     # Remove physical input file.
  102.     remove(fname) | stop("nocr:  cannot remove ",fname,"; aborting")
  103.  
  104.     # Give temp name the same name as the input file, completing the
  105.     # conversion process.
  106.     rename(temp_name,fname) |
  107.         stop("nocr:  Can't find temp file ",temp_name,"; aborting")
  108.     }
  109.  
  110. end
  111.  
  112.  
  113. procedure er_out(s)
  114.     write(&errout,"nocr:  cannot open ",s," for reading")
  115.     return
  116. end
  117.  
  118.  
  119. procedure basename(s,slash)
  120.     s ? {
  121.     while tab(find(slash)+1)
  122.     return tab(0)
  123.     }
  124. end
  125.  
  126.  
  127. procedure pathname(s,slash)
  128.     local s2
  129.  
  130.     s2 := ""
  131.     s ? {
  132.     while s2 ||:= tab(find(slash)+1)
  133.     return s2
  134.     }
  135. end
  136.