static    char    sccsid[]="%Z% %M% %I% %E% %U%";
/*************************************************/
/*  Program name                                 */
/*       int       ColMnTrEndloop                */
/* --------------------------------------------- */
/*  (I/O)                                        */
/*       Out        0   :Normal                  */
/*                 -1   :AbNormal                */
/* --------------------------------------------- */
/*  Function :                                   */
/*     Equote no shori.                          */
/* --------------------------------------------- */
/*************************************************/
#include "colmn.h"

extern condList  CLcList;	/* 情報リスト */
extern tableRoot CLtbl;		/* 解析するタグ及び文字が格納される領域*/
extern CLNCB     CLSTCB;	/* タグの構文解析を行うための領域 */

/*********************************************/
/*                                           */
/*********************************************/
static int _tr_loop_node_check(able,na,loop_name)
int  *able[],na;
char *loop_name;
{
	Leaf *leaf;
	char *p;
	int rc=0;

	rc = cl_tr_node_check_leaf(NULL,0,able,na,&leaf);
	if (rc) rc = ECL_TR_ENDLOOP;
	else if (leaf->cmd.cid==C_LOOP && stricmp(p=leaf->cmd.prmp[0]->prp,loop_name)) {
		/* col_mn_tr_end_loop: ループの開始が[LOOP ]%sになっています。line=%d */
		ERROROUT2(FORMAT(71),p,leaf->cmd.rc);
		rc = ECL_TR_ENDLOOP;
	}
	return rc;
}

/*********************************************/
/*                                           */
/*********************************************/
int col_mn_tr_end_loop()
{
	static int able[]={C_FOR,C_LOOP};
	static int ablewh[]={C_WHILE,C_LOOP};
	static int ableun[]={C_UNTIL,C_LOOP};
	static int abledo[]={C_DO,
		C_LOOP|C_LOOP_DO,C_FOR|C_LOOP_DO,C_WHILE|C_LOOP_DO,C_UNTIL|C_LOOP_DO};
	static int abledl[]={C_DO,C_LOOP};
	int  rc;
	Leaf *leaf;

	if (CLcList.cmd.prmnum > 0) {
		if (CLcList.cmd.cid==C_ENDWHILE || CLcList.cmd.cid==C_ENDUNTIL) {
			rc = cl_tr_node_check_leaf(NULL,0,abledl,2,&leaf);
			if (leaf->cmd.prmnum > 0) {
				/* col_mn_tr_end_loop: ループの開始がDO;またはLOOP;ではありません。line=%d */
				ERROROUT1(FORMAT(74),leaf->cmd.rc);
				return ECL_TR_ENDLOOP;
			}
			leaf->cmd.sub_cid = CLcList.cmd.cid;
			CLcList.cmd.parl[D_DOWHILE_TOP_LEAF].par = (char *)leaf;
			if (!(rc = cl_make_leaf())) rc = cl_push();
			if (rc) return rc;
		}
		else {
			ERROROUT1(FORMAT(41),"col_mn_tr_end_loop");
			return ECL_TR_ENDLOOP;
		}
	}
	else {
		if (CLcList.cmd.cid==C_ENDLOOP) {
			rc = cl_tr_node_check(NULL,0,able,2);
			if (rc) return ECL_TR_ENDLOOP;
		}
		else if (CLcList.cmd.cid==C_NEXT || CLcList.cmd.cid==C_ENDFOR) {
			rc = _tr_loop_node_check(able,2,"FOR");
			if (rc) return ECL_TR_ENDLOOP;
		}
		else if (CLcList.cmd.cid==C_ENDWHILE) {
			rc = _tr_loop_node_check(ablewh,2,"WHILE");
			if (rc) return ECL_TR_ENDLOOP;
		}
		else if (CLcList.cmd.cid==C_ENDUNTIL) {
			rc = _tr_loop_node_check(ableun,2,"UNTIL");
		}
		else {
			rc = cl_tr_node_check_leaf_check(NULL,0,abledo,5,NULL,C_LOOP_DO);
			if (rc) return ECL_TR_ENDLOOP;
		}
#if 0
		rc = cl_loop_close(CLcList.cmd.cid);
		if (rc == 10000) {
			/* col_mn_tr_end_loop: ENDIF または ENDSW がありません。 */
			ERROROUT(FORMAT(72));
			return ECL_TR_ENDLOOP;
		}
#endif
	}
	rc = cl_nest_tag(2);
	if (rc != -1) {
		rc=cl_change_tree( CLSTCB.nestLev2 );
	/*	if (rc) return (rc);	*/
		cl_search_nest(2);
	}
	else return ECL_TR_ENDLOOP;
/*  else clError(30); */
	return NormalEnd;
}

/************************************************/
/*                                              */
/*     cl_loop_close                            */
/*----------------------------------------------*/
/*                                              */
/*                                              */
/************************************************/
/* */
int cl_loop_close(cid)
int cid;
{
	int rc,cmdid;
	Leaf *leaf;

	rc = 0;
	for (;;) {
		if (!(leaf=CLSTCB.nestLev2)) break;
		cmdid = leaf->cmd.cid;
		if ((cid==C_LOOP && cmdid!=C_LOOP) && (cid==C_DO && cmdid!=C_DO)) {
			rc = cl_nest_tag(2);
			if (rc != -1) {
				cl_change_tree(CLSTCB.nestLev2);
				cl_search_nest(2);
			/*	ERROROUT(FORMAT(73));	*/
				rc = 10000;
			}
		}
		else {
			break;
	    }
	}
	return rc;
}

/************************************************/
/*                                              */
/*     cl_search_loop                           */
/*----------------------------------------------*/
/*                                              */
/*                                              */
/************************************************/
/* */
int cl_search_loop(cid)
int cid;
{
	Leaf  *Dummy;
	int cmdid;

	Dummy = CLSTCB.nestLev2;
	for(;;) {
		if (Dummy==CLSTCB.nestLev1 || !Dummy) {
			return ECL_SCRIPT_ERROR;
		}

		cmdid = Dummy->cmd.cid;
		if (cid) {
			if (cmdid==cid) return 0;
		}
		else {
			if (cmdid==C_LOOP || cmdid==C_FOR || cmdid==C_DO
#if 1
			 || cmdid==C_SWITCH
#endif
			) return 0;
		}

		Dummy = Dummy->preleaf;
	}
}
