/*@ cc -o @R @F
*/
#include <stdio.h>

/* This is an example set of routines that can be used
   to generate replay scripts, using C as your scripting
   language. Youj can either pipe the output of your
   program directly into the replay program, or save it
   as a file, and use the '-f' flag


   The example is not complete.
   It is intended to show how easy it is to produce replay
   scripts in any language you are familiar with.

   The example should indent properly, both for if conditions
   and 'in' statements.


   $ cc -o c_api c_api.c
   $ c_api

*/



int In( char * shell);
int Push( char * widget);
int PushUser( char * widget, char * name,char * attribute);
int Select( char * cascade,char * sel);
int SelectPullright( char * cascade, char * pullright, char * sel);
int If( char * condition);
int Elif( char * condition);
int Else();
int Endif();
char * ApplicationShell();



main()
{
	In(ApplicationShell());
		Push( "mywidget");
		PushUser( "myuserwidget", "yellowone", "center");
		Select( "file", "open");
		SelectPullright( "file", "bingo", "house");

	If( "mywidget->value:'hallo world'");
		Push( "mywidget");
	Elif( "mywidget->value:'bye world'");
		Push( "mywidget");
	Else();
		Push( "mywidget");
	Endif();
}



char * xdsTabs();

char *
ApplicationShell()
{
	return "ApplicationShell";
}

int
In( char * shell)
{
	xdsPopShell();

	printf ( "%sin %s\n", xdsTabs(), shell);

	xdsPushShell();
}


int
Push( char * widget)
{
	if (!widget)
		xdsBadCommand();
	printf ( "%spush %s\n", xdsTabs(), widget);
}

int
PushUser( char * widget, char * name,char * attribute)
{
	if (!widget || !name || !attribute)
		xdsBadCommand();
	printf ( "%spush %s(%s,%s)\n", xdsTabs(), widget, name, attribute);
}

int
Select( char * cascade,char * sel)
{
	char * tabs = xdsTabs();
	if (!cascade || !sel)
		xdsBadCommand();
	printf ( "%scascade %s\n%s\tselect %s\n", tabs, cascade, tabs, sel);
}

int
SelectPullright( char * cascade, char * pullright, char * sel)
{
	char * tabs = xdsTabs();
	if (!cascade || !pullright || !sel)
		xdsBadCommand();
	printf ( "%scascade %s\n%s\tpullright %s\n%s\t\tselect %s\n", tabs, cascade, tabs, pullright, tabs, sel);
}


int
If( char * condition)
{
	if (!condition)
		xdsBadCommand();
	(void) printf ( "%sif %s\n", xdsTabs(), condition);

	xdsPush();
}

int
Elif( char * condition)
{
	if (!condition)
		xdsBadCommand();
	xdsPop();
	(void) printf ( "%selif %s\n", xdsTabs(), condition);
	xdsPush();
}

int
Else()
{
	xdsPop();
	(void) printf ( "%selse\n", xdsTabs());
	xdsPush();
}

int
Endif()
{
	xdsPop();
	(void) printf ( "%sendif\n", xdsTabs());
}



typedef struct indent_s {
	int     level;
	int     in_shell;
	struct  indent_s * next;
} Indent_t;

Indent_t * xdsIndent = (Indent_t*)0;


int
xdsPush()
{
	Indent_t * i = (Indent_t*) malloc ((unsigned) sizeof(Indent_t));

	i->level    = (!xdsIndent) ? 1 : xdsIndent->level + 1;
	i->in_shell = 0;
	i->next     = xdsIndent;

	xdsIndent   = i;
}

int
xdsPop()
{
	Indent_t * i = xdsIndent;

	xdsIndent = xdsIndent->next;

	if (i) {
		free ((void*) i);
	}
}

char *
xdsTabs()
{
	static char tabs[100];
	char * p;
	Indent_t * i;

	if (!xdsIndent) {
		xdsPush();
	}

	for (p = tabs, i = xdsIndent; i; i = i->next) {
		*p++ = '\t';
		if (i->in_shell) {
			*p++ = '\t';
		}
	}

	*p = '\0';

	return tabs;
}

int
xdsPushShell()
{
	xdsIndent->in_shell = 1;
}

int
xdsPopShell()
{
	if (!xdsIndent)
		xdsPush();

	xdsIndent->in_shell = 0;
}

int
xdsBadCommand()
{
	(void) fprintf ( stderr, "Bad command\n");
	exit(-1);
}


/*---------------------------------------------*/
