Newsgroups: comp.binaries.apple2
Path: news.uiowa.edu!uunet!europa.eng.gtefsd.com!howland.reston.ans.net!math.ohio-state.edu!news.cyberstore.ca!nntp.cs.ubc.ca!unixg.ubc.ca!acs.ucalgary.ca!sbdocker
From: sbdocker@acs.ucalgary.ca (Sean Brendan Dockery)
Subject: UNIX based text conversion script (atou) bug fixes
Message-ID: <Dec5.035544.52564@acs.ucalgary.ca>
Date: Sun, 5 Dec 1993 03:55:44 GMT
References: <CHAKEI.9pB@griffin.cuc.ab.ca>
Organization: Griffin Software Development
Lines: 201

I recently posted this, but after working with some IBM formatted
files, I found that mtou and mtoa were replacing the CR/LF sequence
with a LR/apostrophe and CR/apostrophe sequences, respectively.

The only fix which I could come up with is to replace the CR/LF
sequence with a <SPACE>/LF and <SPACE>/CR instead.  Unfortunately,
this will have the side effect of making lines in your file one
character longer each time you convert the file back to an IBM text
file.

Another bug that pops up ever once in a while is that the script exits
with error status of 1.  All the files are handled properly, so I
haven't been able to hunt down that bug yet, although I'm inclined to
ignore it as it doesn't seem to be pathogenic.  :-)

Repost (with bugs fixes) begins...
========================================================================
This script automates a couple conversion for text file owners.

-it will mask bit 7 of all bytes in a file
 -it will convert newline characters between the following systems:
   apple to unix, unix to apple, apple to ms-dos, ms-dos to apple, unix
   to ms-dos, and ms-dos to unix.
 
 It requires a copy of basename, find, mv, sh, and tr on your system.
 
Place this in your private bin directory under the name atou.
------------------------------------------------------------------------
#!/bin/sh
 
########################################################################
# atou (convert apple to unix)
#  Version: 1.0.1
#  Copyright (C) 1993 by Sean Dockery.
#
# 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
#  mtoa: MS-DOS newlines to Apple newlines
#  utom: UNIX newlines to MS-DOS newlines
#  mtou: MS-DOS newlines to UNIX newlines
#  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'
              ;;
        mtoa) # MS-DOS to Apple
              oct1='\015\012'
              oct2=' \015'
              ;;
        utom) # UNIX to MS-DOS
              oct1='\012'
              oct2='\015\012'
              ;;
        mtou) # MS-DOS to UNIX
              oct1='\015\012'
              oct2=' \012'
              ;;
    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
                                        rm "$command_$$"
                                        [ $verbose -eq 1 ] && echo "done."
                                    else
                                        tr "$oct1" "$oct2" < $file
                                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"
fi
------------------------------------------------------------------------
-- 
Sean Dockery

dockery@griffin.cuc.ab.ca (preferred)
sbdocker@acs.ucalgary.ca (pending expiration)
