Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!seismo!rutgers!sri-spam!ames!ptsfa!ihnp4!ihlpf!straka
From: straka@ihlpf.ATT.COM (Straka)
Newsgroups: comp.sys.mac
Subject: Re: Binhex section delimiters (BHCOMB source)
Message-ID: <1779@ihlpf.ATT.COM>
Date: Mon, 6-Jul-87 16:20:42 EDT
Article-I.D.: ihlpf.1779
Posted: Mon Jul  6 16:20:42 1987
Date-Received: Wed, 8-Jul-87 00:58:22 EDT
References: <3977@utai.UUCP> <278@intvax.UUCP> <6582@dartvax.UUCP>
Reply-To: straka@ihlpf.UUCP (55223-Straka,R.J.)
Organization: AT&T Bell Laboratories
Lines: 252

I couldn't resist: All of this discussion on a way to do the job in a
very UNrobust fashion.  I offer (again, and even slightly revised) my
solution to the combining problem: bhcomb.c.  Anyone with a c compiler
and stdio can use it.  And it's reasonably robust, too.

Try it; you'll like it.  Totally automated.  Gives diagnostics.
Output works with xbin, macput, etc.  Free support from the author. :-)

Yes, I know this probably belongs in comp.sources.mac, BUT:

In article <6582@dartvax.UUCP> earleh@dartvax.UUCP (Earle R. Horton) writes:
>In article <278@intvax.UUCP>, loucks@intvax.UUCP (Cliff Loucks) writes:
>> cat $1 | sed -n -e '/ /d' -e '/	/d' -e '/^---$/d' -e '/./p' > $1.hqx
>> rm $1
>cat $HOME/thisfile $* |\
>         sed -n -e 1,2p -e '/ /d' -e '/	/d' -e '/^---$/d' -e '/./p' > $1.hqx
>The trick is to do all the files at once, and still get the crucial
>"(This file...)", while using the minimal number of programs/CPU time.

snip--- snip--- snip--- snip--- snip--- snip--- snip--- snip--- snip--- snip---

/*	bhcomb.c: combine and strip header information from BinHexed files.
	          for MacIntosh file transfer.
	Author: R. J. Straka
	Revision 1.1
	Date: July 6, 1987

	Bhcomb is a program that takes a BinHexed MacIntosh file that
	has been broken into several pieces to avoid electronic mailer
	handling problems and splices them back together again.  Bhcomb
	does this process in a totally automated fashion (when
	accompanied by an appropriate shell script), and attempts to be
	fairly rigorous by:

	1) Looking for the logical start of the file (delimited by the
	   string: "(This file ...)"
	2) Checking each line of the input for proper length and validity
	   of all characters.
	3) Looking for the logical end of the file.

	Bhcomb was developed under UNIX SVR2, and uses stdin, stdout
	   and stderr exclusively:
		Stdin is used for the input.
		Stdout is used for the valid file output.
		Stderr is used for the garbage and diagnostics.

	After bhcombing, the user would typically use xmodem (or
	  similar) or macput on the resulting file.

	Bhcomb assumes the following BinHex file structure:

	several lines of unrelated header
	(This file must be converted by BinHex 4.0)
	:123456789012345678901234567890123456789012345678901234567890123
	1234567890123456789012345678901234567890123456789012345678901234
	.
	.
	.
	1234567890123456789012345678901234567890123456789012345678901234
	1234...4321:
	several lines of unrelated footer

	Additional unrelated headers and/or footers may be present
	   within the data stream.
	The actual data is prepended with "(This file... BinHex 4.0)"
	   and an extra blank line.
	The actual data begins with a framing ":" (not checked)
	The actual data must end with a framing ":"
	All data lines (except potentially the last) are of the same
	   length (default=64).
	The last data line is of random length, and ends with a ":".
	Certain characters are never seen within the BinHex portion:
		nothing < \012
		nothing > \012, yet < \040
		no spaces
		no . / ; < = > ? O g n o s t u v w x y z { } characters
		no | ~ \ ] ^ _ characters
		nothing > \176

	Data is gathered through stdin.
	Good data is sent to stdout.
	Bad data and diagnostics are sent to stderr.

	A shell line (or procedure) of the following form is recommended:

	   bhcomb foo 2>foo.doc || echo ^G bhcomb Failed!

	Where the input filenames are foo1.net, foo2.net, ...  The shell
	   should put the files in the proper order given proper naming
	   convention by the files' creator.

	BUGS:
		More than one BinHexed file per invocation ignores all
		  but the first BinHexed file.
		Does not check for additional ":"s inside of the valid
		  portion of the data.
		Has no way to check for files in inappropriate order
		  (except for the first and last)
		Could be made more efficient by being table driven.
		No manual page.  (You can tell I don't write
		  applications code for a living.)
	
	Revision Notes:
		1.0:	Original Release
		1.1:	Now recognizes last line of exactly LENGTH chars
			  without complaining.
			Minor check added for out of sequence input files.
*/

#include 
#include 
#define	LENGTH	64			/* LENGTH = default BinHex line
					     length = 64
					*/
main(argc,argv)
{
int valid=0, started=0, lth;
					/* started = "we have started
					     collecting valid BinHex data"
					   valid = "the last line encountered
					     was a valid BinHex line"
					   lth = line length
					*/
char inline[256];
while (gets (inline) >0)
	{
	if (strncmp (inline,"(This file must be converted with BinHex 4.0)",45)==0)
		{
		if (started != 0)	/* Have we already started? */
					/* If so, something is wrong! */
			{
			started=0;	/* Unused hook for multiple files */
			fprintf(stderr,"%s\n",inline);		  /* Print it */
			fprintf(stderr,"More than one BinHex file!\n");/*ERROR*/
			exit (1);
			}
		else
			{
			printf("%s\n",inline);
			gets (inline);		/* read dummy blank line */
			printf("\n");		/* put out dummy blank line */
			valid=1;		/* This line of data is valid */
			started=1;		/* We started data gathering
			*/
			}
		}
	else
		{
		lth=strlen (inline);
		if (badchars (inline,lth) != 0)	/* Do we have illegal chars? */
			{
			fprintf(stderr,"%s\n",inline);	/* Put to stderr */
			valid=0;			/* Line not valid */
			}
		else
			{				/* All chars OK */
			if (strlen (inline) != LENGTH)	/* if bad line length */
				{
				if (valid!=1)	/*not expecting last line with :*/
					{
					fprintf(stderr,"%s\n",inline); /*bad line*/
					valid=0;	/* Line not valid */
					}
				else			/*expecting last line with : */
					{
					if (findcolon (inline) == 0)
					/* if colon at end of line */
						{
						printf("%s\n",inline); /* last line */
						started=2;	/* FINISHED */
						exit (0);    /* NORMAL EXIT */
						}
					else
						{
						fprintf(stderr,"%s\n",inline);
						valid=0;	/* bad line */
						}
					}
				}
			else
				{
				if (started != 1)
					{
					fprintf(stderr,"%s\n",inline);	/* Print it */
					fprintf(stderr,"No beginning BinHex message; files may be out of order.\n");  /*ERROR*/
					fprintf(stderr,"Out of phase, get help. :-)\n");  /*ERROR*/
					exit (1);
					}
				else
					{
					printf("%s\n",inline);	/* Good line */
					valid=1;
					if (findcolon (inline) == 0)
					/* if colon at end of
					   this 64 character line */
						{
						started=2;	/* FINISHED */
						exit (0);    /* NORMAL EXIT */
						}
					}
				}
			}
		}
	}
fprintf(stderr,"Improper EOF; no ending colon!\n");  /* should never get here */
exit (2);
}

badchars(lptr,length)			/* Look for illegal characters */
char *lptr;
int length;
{
int badchar, p;
char c;
c='a';
badchar=0;
for (p=0;p '\n' && c < '!') {badchar=1; break;}
	if (c > '-' && c < 0)    {badchar=1; break;}
	if (c > ':' && c < '@')  {badchar=1; break;}
	if (c == 'O')            {badchar=1; break;}
	if (c > '[' && c < '`')  {badchar=1; break;}
	if (c == 'g')            {badchar=1; break;}
	if (c > 'n' && c < 'o')  {badchar=1; break;}
	if (c >  's')            {badchar=1; break;}
	}
return (badchar);
}

findcolon(lptr)			/* Look for : at end of line */
char *lptr;
{
int p;
p=strlen(lptr);
while (lptr[p--]=='\n') ;	/* get rid of all possible trailing \n_s */
if (lptr[p]==':')
	{
	return (0);
	}
else
	{
	return (1);
	}
}

-- 
Rich Straka     ihnp4!ihlpf!straka

Advice for the day: "MSDOS - just say no."