/* dirty.c  MR 20/09/98 
   Generate a calling tree from a C source file that has already
   been filtered through 'clscanc1.sh'.
*/

#include <stdio.h>
#include <string.h>

main()
	{
	char buf[256];
	char k;
	char bufpr[256];
	char kpr;
	char save1[256];
	int roundbrlevel = 0;
	int curlybrlevel = 0;
	int alert1 = 0;

	strcpy( buf, "" );
	k = 0x00;

	while ( kpr = k, 
	   strcpy( bufpr, buf+1 ),
	   fgets( buf, sizeof(buf), stdin ) )
		{
		int len;

		len = strlen( buf );
		if ( len > 0 && buf[len-1] == '\n' ) { buf[len-1] = '\0'; }

		k = buf[0];

		if ( kpr == 'I' && k == '(' )
			{
			if ( curlybrlevel > 0 )
				{ /* Function call inside a function
				     definition body */
				printf( "call_in_of %s %s\n", 
				   save1, bufpr );
				}
			else
				{ /* Possible start of fnc decl/def */
				strcpy( save1, bufpr );
				alert1 = 1;
				}
			}
		else if ( roundbrlevel == 0 && alert1 &&
		   curlybrlevel == 0 && kpr == ')' && k == '{' )
			{ /* Function definition, now at opening '{' */
			printf( "startdef %s\n", save1 );
			}
		else if ( roundbrlevel == 0 && alert1 &&
		   curlybrlevel == 0 && kpr == ')' && k == ';' )
			{ /* Function declaration */
			printf( "funcdecl %s\n", save1 );
			alert1 = 0;
			}
		else if ( alert1 && k == '}' && curlybrlevel == 1 )
			{ /* Function definition, now at closing '{' */
			printf( "enddef %s\n", save1 );
			alert1 = 0;
			}


		if ( k == '(' ) { roundbrlevel++; }
		if ( k == ')' ) { roundbrlevel--; }
		if ( k == '{' ) { curlybrlevel++; }
		if ( k == '}' ) { curlybrlevel--; }

		}

	return 0;
	}
