[net.sources] possibly useful toy

mjranum@gouldsd.UUCP (03/26/87)

	This is a quick hack that may come in handy. It takes a .c file 
and tries to chop it up into discrete subroutines. (Note the "TRIES")
If you use it, I suggest using it in a subdirectory of its own, and
on one file at a time. It functions by getting subroutine names, prepending
and "_" and then appending what it thinks is the subroutine. Misc stuff
(that it can't recognize) goes into _header.h
	Hope it comes in handy...
--mjr()
--------------------------cut anywhere you like ------------

:
#FROBOZZOROBOZZO
#the following files are in this bundle: (Use them wisely)
# Makefile shred.l
# To unbundle - sh this file
echo unbundling: Makefile shred.l
echo unbundling Makefile
if [ -f Makefile ] ; then
	echo cannot unbundle Makefile - file exists already
	exit 1
fi
cat >Makefile <<'End of Makefile'
DEST	      = .

LINKER	      = cc

CFLGS		  = -O -s -n

PROGRAM	      = shred

SRCS	      = lex.yy.c

$(PROGRAM):     $(SRCS) 
		$(LINKER) $(CFLGS) -o $(PROGRAM) $(SRCS)
		@echo "done"

lex.yy.c:		 shred.l
		lex shred.l

install:	$(PROGRAM)
		cp $(PROGRAM) $(DEST)
		chmod 555 $(DEST)
End of Makefile
echo done unbundling Makefile
echo unbundling shred.l
if [ -f shred.l ] ; then
	echo cannot unbundle shred.l - file exists already
	exit 1
fi
cat >shred.l <<'End of shred.l'
shred.l:
%e 2000
%p 5000
%n 1000
%k 500
%a 4000
%o 2000


%{
	/* Copyright, 1987, Marcus J Ranum */
	/* All rights reserved. This code can be distributed, modified, */
	/* or altered at will, but it or versions of it may not be sold */
	/* for profit */
	
	/* krufty hack to rip large source modules into subroutine modules */
	/* this program is only semi-intelligent and should be treated as such */

#include <sys/types.h>
#include <sys/stat.h>


int trigger =0;
int bracelev =0;
int incomment =0;
int ininclude =0;
int junkint =0;
struct stat stbuf;
char jfname[400];
char *strcat();

%}

%%
[a-zA-Z1-9 ]+"("[a-zA-Z1-9, ]*")" {
				/* found a function decl ! */
				if(!bracelev && !incomment &&!ininclude) {
					(void)sprintf(jfname,"_%s",yytext);
					for(junkint = 0 ; junkint <strlen(jfname); junkint++) {
						if(jfname[junkint] == ' ' || jfname[junkint] == '(')
							jfname[junkint]='\0';
					}
					(void)strcat(jfname,".c");
					fprintf(stderr,"writing %s\n",jfname);
					(void)fclose(yyout);
					if((yyout = fopen(jfname,"a")) ==NULL) {
						perror("shred");
						exit(9);
					}
					fprintf(yyout,"%s",yytext);
				} else {
					fprintf(yyout,"%s",yytext);
				}
			}
"{"			{
				if(!incomment)
					++bracelev;
				fprintf(yyout,"%s",yytext);
			}
"}"			{
				if(!incomment)
					--bracelev;
				fprintf(yyout,"%s",yytext);
				if(bracelev <0) {
					fprintf(yyout,"too many \"}\" !  unmatched \"{\"\n");
					exit(0);
				}
				/* if we're not in braces, anything goes to _header.h */
				if(!bracelev) {
					fprintf(yyout,"\n");
					(void)fclose(yyout);
					if((yyout = fopen("_header.h","a")) ==NULL) {
						perror("shred");
						exit(9);
					}
				}
		}
\*\/		{
				/*
				 foobar
				*/
				incomment--;
				fprintf(yyout,"%s",yytext);
			}
\/\*		{
				incomment++;
				fprintf(yyout,"%s",yytext);
			}
^[]*"#"		{
				ininclude++;
				/* flush includes */
				fprintf(yyout,"%s",yytext);
			}
\n			{
				ininclude =0;
				fprintf(yyout,"%s",yytext);
			}
.			fprintf(yyout,"%s",yytext);
%%

/* beginning of MAIN */
main(argc,argv)
int argc;
char *argv[];
{
	int index =0;
	
	if(!stat("_header.h",&stbuf)) {
printf("%d braces\n",bracelev);
		fprintf(stderr,"will not overwrite existing %s\n","_header.h");
		exit(1);
	} else {
		if((yyout = fopen("_header.h","w")) ==NULL) {
			perror("shred");
			exit(9);
		}
	}

	for(index = 1; index <argc; index++) {
		if((yyin = fopen(argv[index],"r")) ==NULL) {
			perror("shred");
			exit(1);
		} else { 
			yylex();
		}
	}
}

yywrap()
{
	/*yywrap returns a 1. It can be used to detect end of
		file, and whether to provide more input to
		yyinput.  I get rid of it like this.
		If you have some desire to frob the input,
		here is one place to do it. -mjr */
	return(1);
}



End of shred.l
echo done unbundling shred.l
# end of the bundle !!
-- 
"It is better to shred the bugger than to bugger the shredder."
					-ancient doltic proverb.