[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: LF to CR Conversion



moxie@panix.com wrote the following article:

| It's me again (Moxie, the guy who started this thread.)  I decided that 
| since Ron Higgins submitted the 'best' LF/CR fixer (it's a script that 
| converts MS-DOS, Apple, and Unix text files to a text file that is usable 
| on an MS-DOS, Apple, or Unix machine.  Thanks, Ron!

<blush>  :)

Be warned that the script is a little defective for those exploring the
full spectrum of its purported functions.  See below for details.

[ thwack ! ]

| ---------------------------cut here--------------------------
| #!/bin/sh
|  
| ########################################################################
| # atou (convert apple to unix)
| #  Version: 1.0.1
| #  Copyright (C) 1993 by Sean Dockery.

This script is actually based on a second script (possibly by the same
name) by another author which was lost.  Be assured that this was written
from scratch.  (ie: I am not a Dr. Tom.)

| #
| # History:
| #  93.12.04: Correction of mtou and mtoa conversion bug.
| #  93.11.22: Inception of the script.
| #
| # Description:
| #  This script will convert the following files when invoked with a given
| #  command name:
| #
| #  atou: Apple newlines to UNIX newlines
| #  utoa: UNIX newlines to Apple newlines
| #  atom: Apple newlines to MS-DOS newlines

This function does not work.  It is a tr limitation.

| #  mtoa: MS-DOS newlines to Apple newlines

This can be improved.  See below.

| #  utom: UNIX newlines to MS-DOS newlines

This function does not work.  It is a tr limitation.

| #  mtou: MS-DOS newlines to UNIX newlines

This can be improved.  See below.

| #  maketext: binary characters to text characters
| #
| # Installation:
| #  You require the following commands to be present on your UNIX
| #  system to use this script:
| #      -basename
| #      -find
| #      -mv
| #      -sh
| #      -tr
| #
| #  Install this script in your own $HOME/bin directory--make sure that
| #  $HOME/bin is part of your $PATH variable (preferably located early
| #  in the $PATH priority,) under the name 'atou'.  Create soft links
| #  to this script by using the 'ln' command.
| #
| #  Example:
| #    $ ln -s atou utoa
| #
| #  Do this for all of the above named commands (utoa, atom, etc ...)
| #  If your UNIX system does not have a link command, use 'cp' instead.
| #
| # Syntax:
| #    $ atou [-v] [-c] <filespec>
| #  or
| #    $ cat foo | atou [-v] -
| #
| # Primary Options:
| #        -v: Give terse diagnostic output.
| #   default: Normally no diagnostic output is given.
| #
| # Secondary Options:
| #         -: Input will be taken from stdin and output directed to stdout.
| #             No files may be declared when this option is used.
| #        -c: Input will be taken from files and output directed to stdout.
| #             Files must also be declared with this option.
| #
| # Bugs and Limitations:
| #  This script will reset the file permissions of the file(s) that it
| #  acts upon to the user's umask value.
| #
| #  This script also has no way to know if a file that it is dealing with
| #  is binary in nature; such files will always be corrupted as a result.
| #
| ########################################################################
| 
| # Determine the base command name.
| command=`basename $0`
| 
| # Determine (based on the command name) which type of conversion that
| # we will be undertaking.
| case $command in
|         atou) # Apple to UNIX
|               oct1='\015'
|               oct2='\012'
|               ;;
|         utoa) # UNIX to Apple
|               oct1='\012'
|               oct2='\015'
|               ;;
|         atom) # Apple to MS-DOS
|               oct1='\015'
|               oct2='\015\012'
|               ;;

Delete the above (atom) segment.  It doesn't work.

|         mtoa) # MS-DOS to Apple
|               oct1='\015\012'
|               oct2=' \015'
|               ;;

Change the above segment to read:

	  mtoa)	# MS-DOS to Apple
		oct1=-d
		oct2='\012'
		;;

It worked fine before, but it also added a space to each line.

|         utom) # UNIX to MS-DOS
|               oct1='\012'
|               oct2='\015\012'
|               ;;

Delete the above (utom) segment.  It doesn't work.

|         mtou) # MS-DOS to UNIX
|               oct1='\015\012'
|               oct2=' \012'
|               ;;

Change the above segment to read:

	  mtou)	# MS-DOS to UNIX
		oct1=-d
		oct2='\015'
		;;

It worked fine before, but it also added a space to each line.

|     maketext) # binary to text
|               oct1='[\201-\377]'
|               oct2='[\001-\177]'
|               ;;
|            *) # unknown
|               echo "$command: unknown function \"$command\""
|               exit 1
|               ;;
| esac
| 
| # Determine if we are to be quiet or loud.
| verbose=0
| case $1 in
|     -v) # verbose mode
|         verbose=1
|         shift
|         ;;
| esac
| 
| # Determine if we are in stdin, stdout, or file mode.
| if ( [ $# -ne 0 ] && [ $1 = '-c' ] )
|     then
|         stdin=0
|         stdout=1
|         shift
|     elif ( [ $# -ne 0 ] && [ $1 = '-' ] )
|         then
|             stdin=1
|             stdout=1
|             shift
|     else
|             stdin=0
|             stdout=0
| fi
| 
| # Make sure we have the correct number of arguements for the mode we are in.
| if ( [ $stdin -eq 1 ] && [ $# -gt 0 ] ) || ( [ $stdin -eq 0 ] && [ $# -eq 0 ] )
|     then
|         echo "usage: $command [-v] [-c] <filespec>, or cat <filespec> | $command [-v] -"
|         exit 1
| fi
| 
| if [ $stdin -eq 0 ]
|     then
|         for file in $*
|             do
|                 if [ `find . -name $file -print` ]
|                     then
|                         if [ -f $file ]
|                             then
|                                 if [ $stdout -eq 0 ]
|                                     then
|                                         mv $file "$command_$$"
|                                         [ $verbose -eq 1 ] && echo -n "$command: $file..."
|                                         tr "$oct1" "$oct2" < "$command_$$" > $file

The above 'tr' line should be kept the same except that the single quotes
should be removed.

|                                         rm "$command_$$"
|                                         [ $verbose -eq 1 ] && echo "done."
|                                     else
|                                         tr "$oct1" "$oct2" < $file

Remove the single quotes from the above line as well.

|                                 fi
|                             else
|                                 [ $verbose -eq 1 ] && echo "$command: $file is a directory."
|                         fi
|                     else
|                         [ $verbose -eq 1 ] && echo "$command: $file does not exist."
|                 fi
|         done
|     else
|         tr "$oct1" "$oct2"

Again, no single quotes in this line either.

| fi
| 
| -------------------------------cut here-----------------------

In order to overcome the tr limitation, I attempted to use a program called
udl that appeared on the binaries a LONG time ago.  Alas, I had problems
getting it to compile and run without crashing on this NeXTCUBE that I
currently frequent.  It appeared to be written explicitly for the ORCA/C
environment.  :(

It was a nice idea, so I wrote a similar utility (again from scratch)
called ndl (there is credit for the idea in the README file) in clean ANSI
C (no complaints from gcc with the -pedantic flag enabled.)  It currently
only works on single files, but there is a (Bourne) script bundled with it
that provides file batch processing.  If anyone is interested, I'll post
the files to the binaries.
-- 
Sean Dockery             |"Now my charms are all o'erthrown, and what strength
dockery@griffin.cuc.ab.ca| I have's mine own." -Prospero, "The Temptest"