static    char    sccsid[]="%Z% %M% %I% %E% %U%";
/********************************************************************/
/* <cllex.c>														*/
/*		字句解														*/
/*			引用符の扱い変更										*/
/*			文字列長チェック長さ変更								*/
/********************************************************************/

#include "colmn.h"

#define YEN '\\'
#define WKBUFLEN	BUFLEN*8	/* 512*8 */
#define WKBUFMAX	WKBUFLEN-1

extern CLCOMMON CLcommon;
extern GlobalCt *pGlobTable;
extern int giOptions[];
extern XHASHB *xhp_main_def;

extern condList  CLcList;	/* 情報リスト */
extern CLNCB     CLSTCB;	/* タグの構文解析を行うための領域 */

extern char cmp_sep[];

static int _parmset();
static int _kugiri();
static int _proc_pragma();
static int _rep_define();
static int _resume_define();
static int _include_define();

static XHASHB *sqp_lex_def=NULL;
static XHASHB *xhp_lex_def=NULL;
static char comstr[24];
static char cM_QUOTE1, cM_QUOTE2;
static char *def_sep=" \t'\";(){}[]";

typedef struct {
	int  pra_opt;	/* 0:rep_def, 1:include */
	char *pra_defkey;
	SSPL_S pra_sspl;
	condList pra_cList;	/* 情報リスト */
} tdtPragmaStack;

#define CLISTSTACKMAX	20
static int iListStack=0;
static int iListStack0=0;
static tdtPragmaStack tPragmaStack[CLISTSTACKMAX+1];

#define MAXIFNEST	20
static int ifnest_cnt,ifnest[MAXIFNEST],iftrue[MAXIFNEST];

#define DEF_OPTC_ICASE	'*'
#define DEFICASEMAX	20

/************************************/
/* cl_chk_pos						*/
/************************************/
static int _chk_pos(psspl,pos)
SSPL_S *psspl;
int pos;
{
	int i,len,l,rc,lend;
	char *buf,*p;

	buf = psspl->wd;
	i   = psspl->sp;
	len = psspl->wdmax;
/*
printf("cl_chk_pos Enter:pos=%d i=%d len=%d attr[3]=%d\n",pos,i,len,psspl->attr[3]);
*/
DEBUGOUTL4(LEX_LOG_GROUP+255,"cl_chk_pos enter:pos=%d i=%d len=%d attr[3]=%d",pos,i,len,psspl->attr[3]);

	if (i>=len && psspl->attr[3]) return -1;
	if (i+pos >= len) {
		l = len - i;
		if (l>0 && i>0) {
			memcpy(buf,buf+i,l);
			len = l;
		}
		else len = 0;
		buf[len] = '\0';
		psspl->sp = 0;
		i = psspl->sp + pos;
		p = buf + len;
		while (i>=len && !psspl->attr[3]) {
			if (len>0 && *p=='\n') {
				len--;
				*(--p) = '\n';
			}
			rc = cl_get_str(p);
			if (rc) {
				psspl->attr[3] = 1;		/* File End */
				if (!len) return -1;
			}
			else {
				l = strlen(p);
				len += l;
				p   += l;
			}
			 
		}
		psspl->wdmax = len;
/*
printf("cl_chk_pos ret  :i=%d len=%d sp=%d attr[3]=%d buf=[%s]\n",i,len,psspl->sp,psspl->attr[3],buf);
*/
DEBUGOUTL4(LEX_LOG_GROUP+124,"cl_chk_pos ret  :i=%d len=%d attr[3]=%d buf=[%s]",i,len,psspl->attr[3],buf);

	}
	return psspl->sp;
}

static int cl_chk_pos(psspl,pos)
SSPL_S *psspl;
int pos;
{
	int i;

	if ((i=_chk_pos(psspl,pos)) < 0) {
		if (_resume_define(psspl)) i = psspl->sp;
		else i = ECL_END_OF_FILE;
	}
	return i;
}
#if 0
static int _set_han(hp)
HASHB *hp;
{
	int i,len,l,rc,lend;
	char *buf,*p,*pp,*buf1;

	buf1 = hp->pha_key;
	p = buf1 + hp->ha_aux;
	rc = cl_get_str(p);
	len1 = hp->ha_aux + strlen(p);
	while (len1 > 0) {
	}
}

static int cl_get_char(wrk,hp)
char *wrk;
HASHB *hp;
{
	int i,len,l,rc,lend;
	char *buf,*p;
/*
	int 	ha_maxreg;
	int 	ha_prereg;
	char	*ha_key;
	int 	ha_aux;
*/
	buf = hp->ha_reg;
	i   = hp->ha_hix;
	len = hp->ha_keylen;
	p = buf + i;
/*
printf("cl_get_char:Enter i=%d len=%d ha_id[0]=%d\n",i,len,hp->ha_id[0]));
*/
	if (i >= len) {
		if (_set_han(hp) < 0) return -1;	/* EOF */
		i   = hp->ha_hix;
		len = hp->ha_keylen;
		p = buf + i;
	}
	m = akxqkanjilen2(p,len-i);
	memzcpy(wrk,p,m);
	psspl->sp += m;

	retuen m;
}
#endif
/************************************/
/* _alloc_cmd_parl					*/
/************************************/
static int _alloc_cmd_parl()
{
	char *p;

	if (!CLcList.cmd.parnum) {
		if (!(CLcList.cmd.parl=(ParList *)cl_const_ct_malloc(CLcList.ConstCt,sizeof(ParList)*MAX_PARL))) {
 			ERROROUT("Malloc CLcList_parl");
			return ECL_MALLOC_ERROR;
		}
		CLcList.cmd.parnum = MAX_PARL;
		memset(CLcList.cmd.parl,0,sizeof(ParList)*CLcList.cmd.parnum);
		CLcList.cmd.parl[1].par = CLcList.fullname;
		CLcList.cmd.parl[1].parlen = 0;
/*
DEBUGOUTL1(LEX_LOG_GROUP+120,"_lex_cmd_init: CLcList.fullname=[%s]",CLcList.fullname);
*/
	}
	return 0;
}

/************************************/
/* cl_lex_snln						*/
/************************************/
char *cl_lex_snln()
{
	static char *fmt="at script=[%s] line=%d";
	static char wrk[128],*p0=NULL;
	char *buf,*name,sname[33];
	int len;

	if (!(buf=CLcList.fullname)) buf = CLcList.fname;
	name = akxt_get_last_name("\\/",buf);
	len = strlen(name) + 30;
	if ((len=akxmemwork(len,&buf,&p0,wrk,sizeof(wrk))) <= 0) {
		buf = wrk;
		strncpy(sname,name,32);
		name = sname;
	}
DEBUGOUTL1(LEX_LOG_GROUP+120,"cl_lex_snln: name=[%s]",name);
	sprintf(buf,fmt,name,CLcList.line);
	return buf;
}

/***************************************************/
/* _parmset (構造体 cmdInfo へのパラメータの設定)  */
/***************************************************/
static int _parmset(pp,len,opt)
char *pp;
int  len,opt;
{
	parmList *lp;
	int rc;

	if (CLcList.cmd.prmnum+2 >= CLcList.max_prmnum) {
		CLcList.max_prmnum += 64;	/*PM_MAX;*/
		if (!(CLcList.cmd.prmp=(parmList **)Realloc(CLcList.cmd.prmp,sizeof(parmList *)*(CLcList.max_prmnum)))) {
			ERROROUT("Realloc CLcList_prmp");
			return ECL_MALLOC_ERROR;
		}
/*
printf("_parmset: CLcList.max_prmnum=%d\n",CLcList.max_prmnum);
*/
	}
	lp = (parmList *)cl_const_ct_malloc(CLcList.ConstCt,sizeof(parmList));
	if (!lp) {
		ERROROUT2("cllex:malloc error.%s parm[%s]",cl_lex_snln(),pp);
		return ECL_MALLOC_ERROR;
	}
	memset(lp,0,sizeof(parmList));
	lp->prmlen = len;
	lp->prp = (char *)pp;
/*
printf("_parmset: prmnum=%d len=%d pp=[%s]\n",CLcList.cmd.prmnum,len,pp);
*/
	CLcList.cmd.prmp[CLcList.cmd.prmnum] = lp;
	CLcList.cmd.prmnum++;
	if (opt) {
		if (CLcList.mcat->mc_ipos > 0) {
			rc = akxtmcats(CLcList.mcat," ");
		}
		else rc = 0;
		if (rc >= 0) rc = akxtmcat(CLcList.mcat,pp,len);
		if (rc < 0) {
			ERROROUT2("cllex:malloc error.%s parm[%s]",cl_lex_snln(),pp);
			return ECL_MALLOC_ERROR;
		}
	}
	rc = _alloc_cmd_parl();
	return rc;
}

/************************************/
/* clparmset						*/
/************************************/
int clparmsetopt(wkstr, parmlen, opt)
char *wkstr;
int parmlen,opt;
{
	char *parm;
	int rc;

	parm = cl_const_ct_malloc(CLcList.ConstCt,parmlen+1);
	if (parm) {
		memzcpy(parm,wkstr,parmlen);
		parmlen = strlen(parm);
		rc = _parmset(parm,parmlen,opt);
	}
	else {
		ERROROUT2("cllex:malloc error.%s parm[%s]",cl_lex_snln(),wkstr);
		rc = ECL_MALLOC_ERROR;
	}
	return rc;
}

int clparmset(wkstr, parmlen)
char *wkstr;
int parmlen;
{
	return clparmsetopt(wkstr,parmlen,0);
}

/************************************/
/* clparmset2						*/
/************************************/
int clparmset2()
{
	char *p;
	int len,lenc,rc=0;

	lenc = strlen(comstr);
	len = CLcList.mcat->mc_ipos;
	p = cl_const_ct_malloc(CLcList.ConstCt,len+lenc+2);
	if (p) {
		if (rc=_alloc_cmd_parl()) return rc;
		memzcpy(p,CLcList.mcat->mc_bufp,len);
		CLcList.cmd.parl[0].par = p;
		CLcList.cmd.parl[0].parlen = len;
		CLcList.mcat->mc_ipos = 0;
#if 1	/* SET_CMD_NAME */
		p += len + 1;
		memzcpy(p,comstr,lenc);
/*		p = Strdup(cl_gets_cmd_name(CLcList.cmd.cid));	*/
		CLcList.cmd.parl[2].par = p;
		CLcList.cmd.parl[2].parlen = lenc;
/*
printf("clparmset2: parl[2].par=[%s]\n",p);
*/
#endif
	}
	else {
		ERROROUT2("cllex:malloc error.%s parm[%s]",cl_lex_snln(),CLcList.mcat->mc_bufp);
		rc = ECL_MALLOC_ERROR;
	}
	return rc;
}

/***************************************************/
/* clparmclr (構造体 cmdInfo のパラメータの消去)　 */
/***************************************************/
int clparmclr()
{
	int i;

	CLcList.cmd.prmnum = 0;
	CLcList.mcat->mc_ipos = 0;
	return 0;
}

/***************************************************/
/* cl_syn_main                                     */
/***************************************************/
int cl_syn_main()
{
	int rc;
	char *pCmdLine;

if (DEBUGOUTCHECK(LEX_LOG_GROUP+120)) {
	if (!cl_get_cmd_line(&CLcList.cmd,&pCmdLine)) DEBUGOUTL1(LEX_LOG_GROUP+120,"cl_syn_main:[%s]",pCmdLine);
}
	rc = cl_tree_main();	/* 木構造作成 */
	if (rc) {
		ERROROUT2("cllex:Create tree error=%d %s",rc,cl_lex_snln());
		if (!cl_get_cmd_line(&CLcList.cmd,&pCmdLine)) {
			if (!pCmdLine) pCmdLine = "";
			ERROROUT1(" ---> [%s]",pCmdLine);
		}
	}
	return rc;
}

/************************************/
/* _to_han_type						*/
/*  flag :  = 0 : 半角に変換された	*/
/*			= 1 : 全角のまま使う	*/
/************************************/
static int _to_han_type(moji,quote,wrk,type)
char *moji,quote,*wrk;
int  type;
{
	int flag=1;	/*  */
	char c;

	if (!(pGlobTable->options[8] & 0x02)) {
		if (akxctohan_type_opt(1,moji,wrk,type,0x10) == 1) {
			c = wrk[0];
			wrk[1] = '.';
			if (pGlobTable->options[8] & 0x01) flag = 0;	/* 全ての英数記号を半角に変換する */
			else if (!quote/* || c=='\'' || c=='"'*/) flag = 0;
		}
	}
	return flag;
}
#if 0	/* 2021.4.5 */
/************************************/
/* _to_han							*/
/************************************/
static int _to_han(moji,quote,wrk)
char *moji,quote,*wrk;
{
	return _to_han_type(moji,quote,wrk,CD_TYPE_SJIS);
}
#endif
/************************************/
/* _is_han_type						*/
/************************************/
static char _is_han_type(moji,quote,wrk,type)
char *moji,quote,*wrk;
int  type;
{
	int lenc,c;

	if (lenc=akxqismbs(type,moji)) {
		c = '\0';
		if (!_to_han_type(moji,quote,wrk+1,type)) c = wrk[1];
	}
	else {
		lenc = 1;
		c = moji[0];
	}
	wrk[0] = lenc;
/*
printf("_is_han: lenc=%d c=%c\n",lenc,c);
*/
	return c;
}
#if 0	/* 2021.4.5 */
/************************************/
/* _is_han							*/
/************************************/
static char _is_han(moji,quote,wrk)
char *moji,quote,*wrk;
{
	return _is_han_type(moji,quote,wrk,CD_TYPE_SJIS);
}
#endif
/************************************/
/* _skip_line						*/
/************************************/
static int _skip_line(buf,psspl)
char *buf;
SSPL_S *psspl;
{
	int len,flg,rc,i;
	char c1;

	len = psspl->wdmax;
/*
printf("_skip_line:Enter len=%d\n",len);
*/
	flg = 1;
	while (flg) {
		if ((c1=buf[len-1])=='\n' || c1=='\r') flg = 0;
		rc = cl_get_str(buf);	/* 現在行を捨てて、次の行を読込む */
		if (rc != 0) {
			if (_resume_define(psspl)) {
				i = psspl->sp;
				return i;
			}
			else return ECL_END_OF_FILE;	/* File End */
		}
		len = strlen(buf);
/*
printf("_skip_line: len=%d buf=[%s]\n",len,buf);
*/
	}
	i = 0;
	psspl->wdmax = len;
	return i;
}

/************************************/
/* _add_LF							*/
/************************************/
static void _add_LF(cont,cont_len)
char *cont;
int cont_len;
{
	char *p,c1;

	p = cont + cont_len;
	if (cont_len > 0) {
		if (!((c1=cont[cont_len-1])=='\n' || c1=='\r')) strcpy(p,"\n");
	}
	else strcpy(p,"\n");
}

/************************************/
/* _get_def_cont					*/
/* opt = 0 : 全角半角変換後の処理	*/
/*           改行はそのまま読み込む */
/*     <>0 : 全角半角変換処理       */
/*           改行継続処理あり       */
/************************************/
#if 1	/* 2022.1.17 */
static int _get_def_cont(wbuf,len,cont,cont_max,sc_code,opt,buf,psspl)
char *wbuf,*buf;
int len;
char *cont;
int cont_max,sc_code,opt;
SSPL_S *psspl;
{
	int flg,rc,i,cont_rem,m1,m2,ret,cont_len,cmlevel;
	char c1,c2,*p,quot,cM_QUOTE1,cM_QUOTE2,*pp,wrk[10],quot1,quot2;

	cM_QUOTE1 = pGlobTable->Quot[0];
	cM_QUOTE2 = pGlobTable->Quot[1];
	cont_rem = cont_max;
	cont_len = 0;
	p = cont;
/*
printf("_get_def_cont:Enter opt=%d len=%d buf=[%s]\n",opt,len,buf);
*/
/*
 * "KEY (a,...)"のときは、引数処理なしとするため、KEY直後の空白文字はスキップしない。
	i = akxnskipin(buf+pos,len-pos," \t");
	pos += i;
*/
	quot1 = quot2 = quot = '\0';
	cmlevel = ret = 0;
	i = 0;
	while (i < len) {
		pp = wbuf + i;
		if (cmlevel) {
			m1 = akxqmbsnlen(sc_code,pp,len-i);
			if (m1 > 1) {
				i += m1;
				continue;
			}
			else c1 = *pp;
		}
		else {
			c1 = _is_han_type(pp,quot1,wrk,sc_code);
			m1 = wrk[0];
		}
		i += m1;
		if (!c1) {
/*
printf("_get_def_cont: i=%d m1=%d pp=[%s]\n",i,m1,pp);
*/
			if (!cmlevel) {
				memcpy(p,pp,m1);
				p += m1;
			}
			continue;
		}
/*
printf("_get_def_cont: i=%d m1=%d c1=[%c]\n",i,m1,c1);
*/
		if (c1=='\n' || c1=='\r' || c1=='\\') {
			flg = 0;
			if (c1 == '\\') {
				pp = wbuf + i;
				c2 = _is_han_type(pp,quot1,wrk,sc_code);
				m2 = wrk[0];
				i += m2;
/*
akxcxtocn(pp,3,wrk,10,0);
printf("_get_def_cont: i=%d m=%d pp=[%s] wrk=[%s]\n",i,m,pp,wrk);
*/
				if (!c2) {
					*p++ = c1;
					memcpy(p,pp,m2);
					p += m2;
				}
				else {
					if (c2=='\n' || c2=='\r') flg = 1;
					else {
						*p++ = c1;
						*p++ = c2;
					}
				}
			}
			else if (quot || cmlevel>0) {
				if (quot) *p++ = c1;
				flg = 1;
			}
			else if (opt) break;
			else {
				continue;
			}
			if (flg && opt) {
				rc = cl_get_str(buf);	/* 次の行を読込む */
				if (rc != 0) {
					if (!_resume_define(psspl)) break;
				}
				len = strlen(buf);

DEBUGOUTL2(LEX_LOG_GROUP+122,"_get_def_cont: len=%d buf=[%s]\n",len,buf);
/*
printf("_get_def_cont: len=%d buf=[%s]\n",len,buf);
*/
				psspl->wdmax = len;
				wbuf = buf;
				i = 0;
			}
		}
		else if (cmlevel) {
			if (c1 == '*') {
				pp = wbuf + i;
				c2 = _is_han_type(pp,'\0',wrk,sc_code);
				m2 = wrk[0];
/*
printf("_get_def_cont:CM i=%d cmlevel=%d m2=%d c2=[%c]\n",i,cmlevel,m2,c2);
*/
				if (c2 == '/') {
					cmlevel--;
					if (cmlevel < 0) break;
					i += m2;
/*
printf("_get_def_cont:CM i=%d cmlevel=%d\n",i,cmlevel);
*/
				}
			}
		}
		else if (quot) {
			if (c1 == quot) {
				if (c1 == cM_QUOTE2) {
					*p++ = c1;
					quot = '\0';
				}
				else {
					pp = wbuf + i;
					c2 = _is_han_type(pp,'\0',wrk,sc_code);
					m2 = wrk[0];
					if (c2 == quot) {
						i += m2;
						*p++ = c1;
					}
					else {
						quot = quot2;
						quot1 = quot2 = '\0';
					}
					*p++ = c1;
				}
			}
			else {
				memcpy(p,pp,m1);
				p += m1;
			}
		}
		else if (c1 == '/') {
			pp = wbuf + i;
			c2 = _is_han_type(pp,'\0',wrk,sc_code);
			m2 = wrk[0];
			if (c2 == '/') {
				break;
			}
			else if (c2 == '*') {
				cmlevel++;
				i += m2;
/*
printf("_get_def_cont:CM i=%d cmlevel=%d\n",i,cmlevel);
*/
			}
			else {
				*p++ = c1;
			}
		}
		else if (c1 == '*') {
			pp = wbuf + i;
			c2 = _is_han_type(pp,quot1,wrk,sc_code);
			m2 = wrk[0];
			if (c2 == '/') {
				cmlevel--;
				if (cmlevel < 0) break;
				i += m2;
/*
printf("_get_def_cont:CM i=%d cmlevel=%d pos=%d\n",i,cmlevel,pos);
*/
			}
			else {
				*p++ = c1;
			}
		}
		else if (c1==cM_QUOTE1 || c1==cM_QUOTE2) {
			quot = c1;
			*p++ = c1;
			if (c1 == cM_QUOTE1) quot1 = c1;
		}
		else *p++ = c1;
	}
	if (quot || cmlevel) {
		*p = '\0';
		ret = -1;
/*
printf("_get_def_cont: quot=[%c] %02x cmlevel=%d\n",quot,quot,cmlevel);
*/
	}
	else {
		*p = '\0';
		akxtstrim(0,cont,strlen(cont),NULL);
	}
/*
printf("_get_def_cont:Exit cont=[%s]\n",cont);
*/
	return ret;
}
#else
static int _get_def_cont(buf,pos,psspl,cont,cont_max,sc_code)
char *buf;
int pos;
SSPL_S *psspl;
char *cont;
int cont_max,sc_code;
{
	int flg,rc,len,i,cont_rem,m,ret,cont_len,cmlevel;
	char c1,c2,*p,quot,cM_QUOTE1,cM_QUOTE2,*pp,wrk[10];

	cM_QUOTE1 = pGlobTable->Quot[0];
	cM_QUOTE2 = pGlobTable->Quot[1];
	cont_rem = cont_max;
	cont_len = 0;
	p = cont;
	len = psspl->wdmax;
/*
printf("_get_def_cont:Enter pos=%d len=%d buf=[%s]\n",pos,len,buf);
*/
/*
 * "KEY (a,...)"のときは、引数処理なしとするため、KEY直後の空白文字はスキップしない。
	i = akxnskipin(buf+pos,len-pos," \t");
	pos += i;
*/
	quot = '\0';
	cmlevel = ret = 0;
	i = pos;
	while (i <= len) {
		pp = buf + i;
		m = akxqmbsnlen(sc_code,pp,len-i);
		if (m > 1) {
/*
printf("_get_def_cont: i=%d m=%d pp=[%s]\n",i,m,pp);
*/
			if (!cmlevel) {
				memcpy(p,pp,m);
				p += m;
			}
			i += m;
			continue;
		}
		else c1 = buf[i++];
		if (c1=='\n' || c1=='\r' || c1=='\\') {
			flg = 0;
			if (c1 == '\\') {
				pp = buf + i;
				m = akxqmbsnlen(sc_code,pp,len-i);
/*
akxcxtocn(pp,3,wrk,10,0);
printf("_get_def_cont: i=%d m=%d pp=[%s] wrk=[%s]\n",i,m,pp,wrk);
*/
				if (m > 1) {
					memcpy(p,pp,m);
					i += m;
					p += m;
				}
				else {
					c2 = buf[i++];
					if (c2=='\n' || c2=='\r') flg = 1;
					else {
						*p++ = c1;
						*p++ = c2;
					}
				}
			}
			else if (quot || cmlevel>0) {
				if (quot) *p++ = c1;
				flg = 1;
			}
			else {
				break;
			}
			if (flg) {
				rc = cl_get_str(buf);	/* 次の行を読込む */
				if (rc != 0) {
					if (!_resume_define(psspl)) break;
				}
				len = strlen(buf);

DEBUGOUTL2(LEX_LOG_GROUP+122,"_get_def_cont: len=%d buf=[%s]\n",len,buf);
/*
printf("_get_def_cont: pos=%d len=%d buf=[%s]\n",pos,len,buf);
*/
				psspl->wdmax = len;
				i = pos = 0;
			}
		}
		else if (cmlevel) {
			if (c1 == '*') {
				if ((c2=buf[i]) == '/') {
					cmlevel--;
					if (cmlevel < 0) break;
					i++;
/*
printf("_get_def_cont:CM i=%d cmlevel=%d pos=%d\n",i,cmlevel,pos);
*/
				}
			}
		}
		else if (quot) {
			if (c1 == quot) {
				if (c1 == cM_QUOTE2) {
					*p++ = c1;
					quot = '\0';
				}
				else {
					if ((c2=buf[i]) == quot) {
						i++;
						*p++ = c1;
					}
					else {
						quot = '\0';
					}
					*p++ = c1;
				}
			}
			else *p++ = c1;
		}
		else if (c1 == '/') {
			if ((c2=buf[i]) == '/') {
				break;
			}
			else if (c2 == '*') {
				cmlevel++;
				i++;
/*
printf("_get_def_cont:CM i=%d cmlevel=%d\n",i,cmlevel);
*/
			}
			else *p++ = c1;
		}
#if 1	/* 2022.1.7 */
		else if (c1 == '*') {
			if ((c2=buf[i]) == '/') {
				cmlevel--;
				if (cmlevel < 0) break;
				i++;
/*
printf("_get_def_cont:CM i=%d cmlevel=%d pos=%d\n",i,cmlevel,pos);
*/
			}
			else *p++ = c1;
		}
#endif
		else if (c1==cM_QUOTE1 || c1==cM_QUOTE2) {
			quot = c1;
			*p++ = c1;
		}
		else *p++ = c1;
	}
	if (quot || cmlevel) {
		*p = '\0';
		ret = -1;
/*
printf("_get_def_cont: quot=[%c] %02x cmlevel=%d\n",quot,quot,cmlevel);
*/
	}
	else {
		*p = '\0';
		akxtstrim(0,cont,strlen(cont),NULL);
	}
/*
printf("_get_def_cont:Exit cont=[%s]\n",cont);
*/
	return ret;
}
#endif

/************************************/
/* かっこのスタックによる管理		*/
/************************************/
#define _SP_EXT	64
static int _sp_max=0;
static int *_stack=NULL;
static int _sp=0;

/****************************/
/* _push					*/
/****************************/
static int _push(x)
int x;
{
	if (_sp >= _sp_max) {
		_sp_max += _SP_EXT;
		if (_stack) _stack = (int *)Realloc(_stack,_sp_max*sizeof(int));
		else        _stack = (int *)Malloc(_sp_max*sizeof(int));
		if (!_stack) return -4;
	}
	_stack[_sp++] = x;
	return 0;
}

/****************************/
/* _pop						*/
/****************************/
static int _pop()
{
	if (_sp <= 0) return -5;
	return _stack[--_sp];
}

/****************************/
/* _peek					*/
/****************************/
static int _peek()
{
	if (_sp <= 0) return 0;
	return _stack[_sp-1];
}

/****************************/
/* _chk_act					*/
/****************************/
static int _chk_act(c)
char c;
{
	int i,j,rc;

	if (c < 0) {
		_sp = 0;
		return 0;
	}

	if      (c == '(') j = 1;
	else if (c == '{') j = 2;
	else if (c == '[') j = 3;
	else if (c == ')') j = 4;
	else if (c == '}') j = 5;
	else if (c == ']') j = 6;
	else return -2;

	rc = 0;
	if (j <= 3) rc = _push(j);
	else {
		i = _peek();
/*
printf("_chk_act: j=%d i=%d\n",j,i);
*/
		if (i<1 || i>3) rc = -3;
		else if (i == j-3) rc = _pop();
		else rc = -11;
	}
	if (rc > 0) rc = 0;
	return rc;
}

/****************************/
/* _is_sjis_hankaku_kana	*/
/****************************/
static int _is_sjis_hankaku_kana(c)
uchar c;
{
	int rc;

	if (c>=161 && c<=223) rc = 1;
	else rc = 0;
	return rc;
}

/************************************/
/* cl_lex (ＧＭＬタグ字句解析)		*/
/************************************/
int cl_lex()
{
	int   kugiri_flg = 0,iKUGIRI;
	char  buff[BUFLEN*2];
	char  wkstr[BUFLEN*8];
	int	exit_flag=0, line_cont;
	ushort sjis;
	char  comstr[24],wrk[4];
	char  *parm,*p;
	int   len,cut;
	int   rc,rc_def;
	int   i,tree,h,parmlen,cnum,j,flag,lenc,len8,ex_opt,sc_code,cmnd;
	char  code,info,quote,c,c1,quote1,quote2;
	unsigned char uc;
	int   new_lex = 0;
	int   moji_const = 0;	/* BEXP $a = '//null/%s';の//をコメントにしない */
	SSPL_S sspl;
	int   cm_level=0;	/* ／＊　＊／形式のコメントのネストレベル */
	int   kk_level=0;	/* ()のネストレベル */
	int   k2_level=0;	/* {}のネストレベル */
	int   k3_level=0;	/* []のネストレベル */
	int   ka[3];
	int   def_parm;
	ScrPrCT *scrptct;
	int   inter_mode;

	if ( CLcList.fp == NULL ) return(-1);
	CLcList.rbuf = buff;
	rc = cl_get_str(buff);
	if (rc) {
		ERROROUT(FORMAT(40));	/* cllex:スクリプト・ファイルが空です。 */
		goto FEND2;					/* File End */
	}
	if (!memcmp(buff,"#!",2)) {
		rc = cl_get_str(buff);		/* 現在行を捨てて、次の行を読込む */
		if (rc != 0) goto FEND2;	/* File End */
	}
	len = strlen(buff);
	def_parm = 0;
	h = 0;
	i = 0;
	info  = 'N';					/* NewLine */
	quote1 = quote2 = quote = '\0';
	clparmclr();
	CLSTCB.nestLev1 = NULL;
	CLSTCB.nestLev2 = NULL;
	cl_cmd_init(&CLcList.cmd);		/* 初期設定 */
	CLcList.mcat->mc_ipos = 0;
	if (CLcList.option & D_SCRPT_NEW_LEX) new_lex = 1;

	if (scrptct = cl_search_src_ct()) inter_mode = scrptct->pFlag & D_SCRPT_INTERACTIVE;
	else inter_mode = 0;

	cM_QUOTE1 = pGlobTable->Quot[0];
	cM_QUOTE2 = pGlobTable->Quot[1];

	if ((rc=_alloc_cmd_parl()) < 0) return rc;
	if (rc=_proc_pragma(NULL,0,0,0,0)) return rc;

	if (CLcList.option & D_CLST_OPT_USE_DTYPE) {
		sc_code = (CLcList.option & D_CLST_OPT_DTYPE)>>16;
/*
printf("cllex: CLcList.option=%08x sc_code=%d\n",CLcList.option,sc_code);
*/
	}
	else {
		sc_code = CD_TYPE_SJIS;
		ex_opt = cl_get_option(21,0);
/*
printf("cllex: ex_opt=%08x\n",ex_opt);
*/
		ex_opt = (ex_opt>>20) & CD_TYPE_CODE;	/* file */
		if (ex_opt) sc_code = ex_opt;
/*
printf("cllex: sc_code=%d\n",sc_code);
*/
	}

	_chk_act(-1);
	sspl.wd = buff;
	sspl.wdmax = len;
	sspl.attr[3] = 0;
	while (!exit_flag) {
		if (h > BUFLEN*8) break;
/*
printf("cllex:1 len=%d h=%d i=%d c=%c\n",len,h,i,buff[i]);
*/
		sspl.sp = i;
		if ((i=cl_chk_pos(&sspl,0)) < 0) goto FEND;		/* File End */
/*
printf("cllex:2 len=%d h=%d i=%d buff=[%s]\n",len,h,i,buff+i);
*/
#ifdef CYGWIN_U8	/* 2020.5.5 */
		uc = *(buff+i);
		if (lenc=akxqismbs(sc_code,buff+i)) {
#else
		if (lenc=iskanji(buff+i)) {
#endif
			flag = 1;
			if (h > BUFLEN*8-2) exit_flag = 1;
			else {
				if (!cm_level || (pGlobTable->options[8] & 0x03)==1) {
					flag = _to_han_type(buff+i,quote1,wrk,sc_code);
					c = wrk[0];
/*
printf("cllex: lenc=%d flag=%d wrk=%02x %02x\n",lenc,flag,wrk[0] & 0xff,wrk[1] & 0xff);
*/
					if (sc_code == CD_TYPE_SJIS) {
						if (flag) {	/* 入力値をそのまま使う */
							sjis = *(buff+i);
							sjis = sjis<<8 | (uchar)*(buff+i+1);
							len8 = akxc_sj_to_utf8(sjis,wkstr+h,NULL);
/*
printf("cllex:zenkaku flga=%d len8=%d\n",flag,len8);
*/
							h += len8;
						}
						else if (_is_sjis_hankaku_kana(c)) {
							sjis = uc;
							len8 = akxc_sj_to_utf8(sjis,wkstr+h,NULL);
/*
printf("cllex:_hankaku_kana flga=%d len8=%d\n",flag,len8);
*/
							h += len8;
							i += lenc;
							continue;
						}
					}
					else {
						if (flag) {	/* 入力値をそのまま使う */
							memcpy(wkstr+h,buff+i,lenc);
							h += lenc;
						}
					}
				}
				i += lenc;
			}
			if (flag) continue;
		}
#ifdef CYGWIN_U8	/* 2020.5.6 */
		else if (!cm_level && sc_code==CD_TYPE_SJIS && _is_sjis_hankaku_kana(uc=*(buff+i))) {
			sjis = uc;
			len8 = akxc_sj_to_utf8(sjis,wkstr+h,NULL);
/*
printf("cllex:_hankaku_kana len8=%d\n",len8);
*/
			h += len8;
			i++;
			continue;
		}
#endif
		else {
			c = buff[i++];
		}
		len = sspl.wdmax;

DEBUGOUTL3(LEX_LOG_GROUP+255,"cllex:len=%d h=%d i=%d",len,h,i);
DEBUGOUTL3(LEX_LOG_GROUP+255,"cllex:c=%c %c%c",c,buff[i],buff[i+1]);
DEBUGOUTL4(LEX_LOG_GROUP+255,"cllex:def_parm=%d kk_level=%d k2_level=%d k3_level=%d",def_parm,kk_level,k2_level,k3_level);
/*
if (CLcList.option & D_SCRPT_MEMORY) printf("cllex: len=%d h=%d i=%d c=%c\n",len,h,i,c);
*/
		iKUGIRI = 0;
/*
printf("cllex:0 i=%d c=[%c] quote=[%c] quote2=[%c]\n",i,c,quote,quote2);
*/
		switch(c) {
			case '#':
				if (cm_level) break;
				else if (!quote && (info=='N')) {
					sspl.sp = i;
					if ((i=cl_chk_pos(&sspl,0)) < 0) goto FEND;	/* 1-->0 2021.5.16 */
					len = sspl.wdmax;
					c1 = _is_han_type(buff+i,'\0',wrk,sc_code);
					if (c1=='!') {
						if ((i=_skip_line(buff,&sspl)) < 0) goto FEND;
						break;
					}
					else if (strchr(M_PRAGMA,c1)) {
						i += wrk[0];
						if ((i=_proc_pragma(buff,i,&sspl,c1,sc_code)) < 0) {
							if (i == ECL_END_OF_FILE) {
								if (!_resume_define(&sspl)) goto FEND;		/* File End */
								i = sspl.sp;
							}
							else {
								rc = i;
								goto ERR;
							}
						}
						break;
					}
				}
				wkstr[h++] = c;
				break;
			case '*':
				sspl.sp = i;
				if ((i=cl_chk_pos(&sspl,0)) < 0) goto FEND;	/* 1-->0 2021.5.16 */
				len = sspl.wdmax;
				c1 = _is_han_type(buff+i,quote,wrk,sc_code);
				if (!quote && c1=='/') {
					if (cm_level > 0) {
						cm_level--;
/*
printf("cllex:i=%d len=%d buf=[%s] cm_level=%d(end)\n",i,len,buff,cm_level);
*/
						i += wrk[0];
					/*	i++;	*/
					}
					else {
						ERROROUT1("cllex: unpaired '*/' at line=%d",CLcList.line);
						return ECL_LEX;
					}
				}
				else if (!cm_level) {
					wkstr[h++] = c;
				}
				break;
			case '/':
				sspl.sp = i;
				if ((i=cl_chk_pos(&sspl,0)) < 0) goto FEND;
				len = sspl.wdmax;
				c1 = _is_han_type(buff+i,quote,wrk,sc_code);

DEBUGOUTL4(LEX_LOG_GROUP+255,"cllex:i=%d len=%d c1=%c buff=[%s]",i,len,c1,buff);
DEBUGOUTL2(LEX_LOG_GROUP+255,"cllex:moji_const=%d quote=%02x",moji_const,quote);

				if (moji_const || quote || (c1!='/' && c1!='*')) {
					if (!cm_level) {
						wkstr[h++] = c;
					}
					break;
				}
				if (c1 == '*') {
/*
printf("cllex:i=%d len=%d buf=[%s] cm_level=%d(start)\n",i,len,buff,cm_level);
*/
					cm_level++;
					i += wrk[0];
					break;
				}
				if (!cm_level) {
					new_lex = 1;
					CLcList.option |= D_SCRPT_NEW_LEX;
				}
			case M_COMMENT:
				if (new_lex && quote) {
					wkstr[h++] = c;
					break;
				}
				if ((i=_skip_line(buff,&sspl)) < 0) goto FEND;
				break;
			case ')':
			case '}':
			case ']':
				if (!cm_level) {
					if (!quote) {
						rc = 0;
/*
printf("cllex:)}]:1 kk_level=%d k2_level=%d k3_level=%d c=%c\n",kk_level,k2_level,k3_level,c);
*/
						if (c==')' && kk_level>0) {
							kk_level--;
							if (!kk_level && CLcList.cmd.cid==C_TRY) {
								wkstr[h++] = c;
								iKUGIRI = 1;
							/*	rc = _kugiri(M_KUGIRI,h,wkstr,&info);
								h = 0;
								moji_const = 0;	2021.10.31 */
								break;
							}
						}
						else if (c=='}' && k2_level>0/* && !def_parm*/) k2_level--;
						else if (c==']' && k3_level>0/* && !def_parm*/) k3_level--;
						else rc = -11;
						if (!rc/* && (c==')' || !def_parm)*/) rc = _chk_act(c);
						if (rc) {
							ERROROUT3("cllex: unpaired '%c' %s rc=%d",c,cl_lex_snln(),rc);
							return ECL_LEX;
						}
/*
printf("cllex:)}]:2 kk_level=%d k2_level=%d k3_level=%d c=%c\n",kk_level,k2_level,k3_level,c);
*/
						if (!kk_level && !k2_level && !k3_level) {
							sspl.sp = i;
							if ((i=cl_chk_pos(&sspl,0)) < 0) goto FEND;	/* 1-->0 2021.5.16 */
							len = sspl.wdmax;
							c1 = _is_han_type(buff+i,'\0',wrk,sc_code);
/*
printf("cllex:i=%d c1=%c kk_level=%d k2_level=%d k3_level=%d\n",i,c1,kk_level,k2_level,k3_level);
*/
							/* 2022.1.5 */
							if (info == 'C') {
								cmnd = CLcList.cmd.cid;
								/* 2022.1.5 */
								if (cmnd == C_LET) ;
								else if (c1!=' ' && (cmnd==C_IF || cmnd==C_ELSEIF ||
								     cmnd==C_LOOP || cmnd==C_FOR || cmnd==C_WHILE || cmnd==C_UNTIL ||
								     cmnd==C_DO || cmnd==C_SWITCH)) {
									iKUGIRI = 1;
									wkstr[h++] = c;
									break;
								}
							}
						}
					}
					wkstr[h++] = c;
				}
				break;
			case '{':
				if (!cm_level) {
					if (!quote && !def_parm) {
						k2_level++;
						if (rc=_chk_act(c)) goto ERR;
					}
					wkstr[h++] = c;
				}
				break;
			case '\n':
			case '\r':
/*
printf("cllex:CR/LF: new_lex=%d inter_mode=%d i=%d len=%d c=%c buff=[%s]\n",new_lex,inter_mode,i,len,c,buff);
*/
				if (!new_lex) break;
				if (len==1 && inter_mode) goto FEND;
			case '\t':
			case '(':
			case '[':
			case M_SPACE:
			case M_KUGIRI:
				if (cm_level) break;
				if (!quote) {
					if (c=='(' || c=='[') {
						if (c == '(') kk_level++;
						else if (c=='[') k3_level++;
						if (rc=_chk_act(c)) goto ERR;
					}
				}
/*
printf("cllex:i=%d h=%d c=%c kk_level=%d\n",i,h,c,kk_level);
*/
				if (quote || (info=='C' && (c=='[' || c=='('))) {
				/*	if (buff[i] == '\t')
						buff[i] = ' ';	*/
					wkstr[h++] = c;
				}
				else if (c==M_KUGIRI && kk_level>0) {
					wkstr[h++] = c;
				}
				else {
					iKUGIRI = 2;
				}
				break;
			case M_QUOTE1:	/* ' */
			case M_QUOTE2:	/* " */
				/* 2022.3.10 */
				if (cm_level) {
					if ((c==cM_QUOTE2 && quote==cM_QUOTE2) ||
					    (c==cM_QUOTE1 && quote==cM_QUOTE1)) {
						sspl.sp = i;
						if ((i=cl_chk_pos(&sspl,0)) < 0) goto FEND;	/* 1-->0 2021.5.16 */
						len = sspl.wdmax;
						/* 半角引用符の次が全角引用符のときは、連続する半角引用符と見なす。 */
						/* その為にquoteではなく'\0'を指定して半角に変換されるようにする。 */
						c1 = _is_han_type(buff+i,'\0',wrk,sc_code);
						if (c1 == cM_QUOTE1) ;
						else {
							quote = quote2;
							quote1 = quote2 = '\0';
						}
					}
					else {
						if (!quote) {
							quote = c;
							if (c != cM_QUOTE2) quote1 = quote;
						}
						else if (quote==cM_QUOTE2 && !quote2 && c==cM_QUOTE1) {
							quote2 = quote;
							quote1 = quote = c;
						}
					}
					break;
				}
				if (c==cM_QUOTE1) {
					if (cm_level) break;
					if (!new_lex) {
						if (h == 0) moji_const = 1;	/* add 2001.8.20 Koba */
						wkstr[h++] = c;
						break;
					}
				}
				if (cm_level) break;
/*
printf("cllex:i=%d c=[%c]\n",i,c);
*/
				if ((c==cM_QUOTE2 && quote==cM_QUOTE2) ||
				    (c==cM_QUOTE1 && quote==cM_QUOTE1)) {
					if (c == cM_QUOTE1) {
						wkstr[h++] = c;
						if (h >= BUFLEN*8) {
							exit_flag = 1;
							break;
						}
						sspl.sp = i;
						if ((i=cl_chk_pos(&sspl,0)) < 0) goto FEND;	/* 1-->0 2021.5.16 */
						len = sspl.wdmax;
					/*	c1 = buff[i];	*/
						/* 半角引用符の次が全角引用符のときは、連続する半角引用符と見なす。 */
						/* その為にquoteではなく'\0'を指定して半角に変換されるようにする。 */
						c1 = _is_han_type(buff+i,'\0',wrk,sc_code);
/*
printf("cllex:i=%d len=%d c1=%c buff+i=[%s]\n",i,len,c1,buff+i);
*/
						if (c1 == cM_QUOTE1) {
						/*	wkstr[h++] = buff[i++];	*/
							wkstr[h++] = c1;
							i += wrk[0];
							break;
						}
						/* 2021.10.31 *//* 0 -> 1 */
						else {
							quote = quote2;
							quote1 = quote2 = '\0';
/*
printf("cllex:1 i=%d c=[%c] quote=[%c] quote2=[%c]\n",i,c,quote,quote2);
*/
							if (quote)  break;
							else if (c1!='\t' && c1!=M_SPACE && c1!=M_KUGIRI) {
								moji_const = 0;
							/*	quote = '\0';	*/
/*
printf("cllex:moji_const=%d quote=%02x\n",moji_const,quote);
*/
								/* 2022.1.6 */
								if (info == 'C') {
									cmnd = CLcList.cmd.cid;
									if (cmnd == C_LET) break;
									else if (c1!=' ' && (cmnd==C_IF || cmnd==C_ELSEIF ||
									     cmnd==C_LOOP || cmnd==C_FOR || cmnd==C_WHILE || cmnd==C_UNTIL ||
									     cmnd==C_DO || cmnd==C_SWITCH)) ;
									else break;
								}
								else break;
							}
						}
						c = c1;
					}
/*
printf("cllex:moji_const=%d info=[%c] h=%d\n",moji_const,info,h);
*/
					iKUGIRI = 3;
				}
				else {
					if (!quote) {
						if (h>0 && (c==cM_QUOTE2 ||
						            (c==cM_QUOTE1 && info=='N' && new_lex))) {
							wkstr[h] = '\0';
							iKUGIRI = 4;
							break;
						}
						quote = c;
						if (c == cM_QUOTE2) {
							break;
						}
						else quote1 = quote;
					}
					else if (quote==cM_QUOTE2 && !quote2 && c == cM_QUOTE1) {
						quote2 = quote;
						quote1 = quote = c;
					}
/*
printf("cllex:2 i=%d c=[%c] quote=[%c] quote2=[%c]\n",i,c,quote,quote2);
*/
					wkstr[h++] = c;
				}
				break;
			case YEN:
				sspl.sp = i;
				if ((i=cl_chk_pos(&sspl,0)) < 0) goto FEND;	/* 2-->0 2021.5.16 */
				len = sspl.wdmax;
				if (i < len) {
					if (iskanji(buff+i)) break;
					c=buff[i];
					if (new_lex) {
						if (quote==cM_QUOTE1) {
							if (c=='\r' || c=='\n') i++;
							else if (!cm_level) wkstr[h++] = YEN;
						}
						else {
							if (c=='\r' || c=='\n') i++;
							else {
								if (!cm_level) {
									wkstr[h++] = c;
								}
								i++;
							}
						}
					}
					else {
						if (c!='\t' && c!='\n' && c!=M_SPACE) {
							if (!cm_level) {
								wkstr[h++] = c;
							}
							i++;
						}
					}
				}
				break;
			default:
				if (!cm_level) {
					wkstr[h++] = c;
				}
				break;
		}
		/* 2021.10.31 */
		if (iKUGIRI) {
/*
printf("cllex: cl_syn_main iKUGIRI=%d\n",iKUGIRI);
*/
			if ((rc=_rep_define(&sspl,wkstr,h,kk_level,k2_level,k3_level,ka)) < 0) {
				goto ERR;
			}
			else if (rc == 1) {
				iKUGIRI = 0;
				h = strlen(wkstr);
				i = sspl.sp;
				kk_level = ka[0];
				k2_level = ka[1];
				k3_level = ka[2];
			}
			if (iKUGIRI == 1) {
				rc = _kugiri(' ',h,wkstr,&info);
				h = 0;
				moji_const = 0;
			}
			else if (iKUGIRI == 2) {
				if (info=='C' && CLcList.cmd.cid==C_DO &&
				    !CLcList.cmd.prmnum && stricmp(wkstr,"AS")) {
					if (rc = _kugiri(';',0,wkstr,&info)) return rc;
				}
				rc = _kugiri(c,h,wkstr,&info);
				if (rc == C_PARM_CONTINUE) {
					wkstr[h++] = c;
				}
				else if (rc == C_PARM_START) {
					h = 0;
					wkstr[h++] = c;
					moji_const = 0;
				}
				else if (rc == C_PARM_HEREDOC) {
					sspl.wdmax = 1;
					*buff = '\n';
					if ((i=_skip_line(buff,&sspl)) < 0) goto FEND;
/*
printf("cllex: buff=[%s]\n",buff);
*/
					h = 0;
					moji_const = 0;
				}
				else if (rc) return rc;
				else {
					h = 0;
					moji_const = 0;	/* add 2001.8.20 Koba */
				}
			}
			else if (iKUGIRI == 3) {
				if (info == 'N') {	/* コマンド */
					if (rc = _kugiri(c,h,wkstr,&info)) return rc;
				}
				else {
					parmlen = h;
					wkstr[h] = '\0';
					if (rc = clparmsetopt(wkstr,parmlen,1)) return rc;
				}
				h = 0;
				moji_const = 0;	/* add 2001.8.20 Koba */
				quote2 = quote1 = quote = '\0';
			}
			else if (iKUGIRI == 4) {
				if (rc = _kugiri(c,h,wkstr,&info)) return rc;
				h = 0;
				moji_const = 0;	/* add 2001.8.20 Koba */
				quote = c;
				if (c != cM_QUOTE2) {
					wkstr[h++] = c;
					quote1 = c;
				}
			}
			iKUGIRI = 0;
		}
	}
	ERROROUT2("cllex:String too long.[%s] %s", wkstr,cl_lex_snln());
	return ECL_LEX;
FEND:
	p = cl_lex_snln();
	if (kk_level || k2_level || k3_level) {
					/* cllex: カッコ('(')が閉じていません(%s)。kk_level=%d */
		if (kk_level) ERROROUT2(FORMAT(31),p,kk_level);
					/* cllex: 中カッコ('{')が閉じていません(%s)。k2_level=%d */
		if (k2_level) ERROROUT2(FORMAT(38),p,k2_level);
					/* cllex: 大カッコ('[')が閉じていません(%s)。k3_level=% */
		if (k3_level) ERROROUT2(FORMAT(39),p,k3_level);
		return ECL_LEX;
	}
	else if (cm_level) {
		/* cllex: コメントが閉じていません(%s)。cm_level=%d */
		ERROROUT2(FORMAT(32),p,cm_level);
		return ECL_LEX;
	}
	else if (quote) {
		/* "cllex: 引用符(%c)が閉じていません(%s)。 */
		ERROROUT2(FORMAT(33),quote,p);
		return ECL_LEX;
	}
	else if (info=='C' || h>0) {
		if (h>0) {
			wkstr[h] = '\0';
			clparmsetopt(wkstr,h,1);
		}
		ERROROUT1(FORMAT(34),p);	/* cllex: 文が終了していません(%s)。 */
		if (!cl_get_cmd_line(&CLcList.cmd,&parm)) ERROROUT1(" ---> [%s]",parm);
		else ERROROUT1(FORMAT(35),wkstr);	/* cllex: [%s]は有効な文ではありません。 */
		return ECL_LEX;
	}
FEND2:
	cl_cmd_init(&CLcList.cmd);
	clparmclr();
	CLcList.cmd.cid = C_FEND;	/* ファイル終了処理 */
	CLcList.cmd.type = 0;
	rc = cl_syn_main();
/*
printf("cllex: cl_syn_main rc=%d\n",rc);
*/
	cl_cmd_init(&CLcList.cmd);
	clparmclr();
	CLSTCB.nestLev1 = NULL;
	CLSTCB.nestLev2 = NULL;
	info = 'N';
	return rc;
ERR:
	ERROROUT2("cllex:rc=%d %s",rc,cl_lex_snln());
	wkstr[h] = '\0';
	if (cl_get_cmd_line(&CLcList.cmd,&parm)) parm = "";
	ERROROUT2(" ---> [%s %s]",parm,wkstr);
	return rc;
}

/************************************/
/* if level のスタックによる管理	*/
/************************************/
#define IF_LEVEL_EXT	16
static int if_level_max=0;
static int if_level=0;
static char *if_stack=NULL;

/************************************/
/* _set_if_level					*/
/************************************/
static int _set_if_level(up,thenl)
int up,thenl;
{
	if (up >= 0) {
		if (up) if_level++;
		if (if_level >= if_level_max) {
			if_level_max += IF_LEVEL_EXT;
			if_stack = MRealloc(if_stack,if_level_max);
			if (!if_stack) return -1;
		}
		if_stack[if_level] = thenl;
	}
	else if (up < 0) {
		if (if_level > 0) if_level--;
		if (if_level > 0) thenl = if_stack[if_level];
		else thenl = 0;
	}
/*
printf("_set_if_level: up=%d thenl=%d if_level=%d\n",up,thenl,if_level);
*/
	return thenl;
}

/************************************/
/* _get_if_level					*/
/************************************/
static int _get_if_level()
{
	int thenl;

	if (if_stack && if_level>0) thenl = if_stack[if_level];
	else thenl = 0;
/*
printf("_get_if_level: thenl=%d if_level=%d\n",thenl,if_level);
*/
	return thenl;
}

/************************************/
/* _insert_ENDIF					*/
/************************************/
static int _insert_ENDIF(n)
int n;
{
	int rc=0;
	cmdTable *pcmd;
	condList TmpcList;
	parmList *tmplp;
/*
printf("cllex:_insert_ENDIF: n=%d\n",n);
*/
	if (n > 0) {
		TmpcList = CLcList;
		tmplp = CLcList.cmd.prmp[0];
		strcpy(comstr,"ENDIF");
		pcmd = cl_cmd_chk(comstr);
		cl_cmd_init(&CLcList.cmd);
		CLcList.cmd.cid  = pcmd->cmdid;
		CLcList.cmd.type = pcmd->type;
		CLcList.cmd.rc   = CLcList.line;
		while (n-- > 0) {
/*
printf("cllex:_insert_ENDIF: comstr=[ENDIF] at line=%d\n",CLcList.line);
*/
			CLcList.cmd.prmnum = 0;
			if (rc = clparmset("",0)) break;
			CLcList.cmd.prmnum--;
			if (rc = clparmset2()) break;
			if (rc = cl_syn_main()) break;
		}
		CLcList = TmpcList;
		CLcList.cmd.prmp[0] = tmplp;
	}
	return rc;
}

/************************************/
/* _chk_heredoc						*/
/************************************/
int _chk_heredoc(cid)
int cid;
{
	int i,parm_pos,len,nam_len,rc,pos,del_tab_space;
	char *p,*name,buf[BUFLEN];
	parmList **prmp,*lp;

	prmp = CLcList.cmd.prmp;
	rc = nam_len = del_tab_space = 0;
	name = NULL;
	for (i=0;i<CLcList.cmd.prmnum;i++) {
		lp = prmp[i];
		nam_len = lp->prmlen;
		p = lp->prp;
/*
printf("_chk_heredoc: i=%d p=[%s]\n",i,p);
*/
		if (nam_len>=2 && !memcmp(p,"<<",2)) {
			nam_len -= 2;
			if (nam_len > 0) {
				p += 2;
				pos = akxnskipin(p,nam_len,"-=!");
/*
printf("_chk_heredoc: i=%d pos=%d\n",i,pos);
*/
				if (pos > 0) {
					del_tab_space = D_PFLAG2_DEL_TAB;
					if (inmemchars(p,pos,"=")) del_tab_space |= D_PFLAG2_DEL_SPACE;
/*
printf("_chk_heredoc: i=%d rc=%d del_tab_space=%02x\n",i,rc,del_tab_space);
*/
					p += pos;
					nam_len -= pos;
				}
			}
			if (nam_len > 0) name = akxttrim2(0,p,&nam_len);
			if (nam_len > 0) break;
			else if (i < CLcList.cmd.prmnum - 1) {
				lp = prmp[i+1];
				nam_len = lp->prmlen;
				name = akxttrim2(0,lp->prp,&nam_len);
				if (nam_len > 0) break;
				return -1;
			}
			else return -1;
		}
	}
	if (name) {
		del_tab_space |= cl_get_option(25,0) & (AKX_TRIM_DEL_TAB | AKX_TRIM_DEL_SPACE);
		name = strmem(name,nam_len);
/*
printf("_chk_heredoc: name=[%s]\n",name);
*/
		rc = 0;
		if (instrchar("$%#'",*name)) {
					/* %s: heredocの終端文字列[%s]が誤っています。*/
			ERROROUT2(FORMAT(632),"_chk_heredoc",name);
			rc = -1;
		}
		while (!rc) {
			if (rc = cl_get_str(buf)) {
						/* %s: heredocの終了行[%s]がありません。*/
				ERROROUT2(FORMAT(631),"_chk_heredoc",name);
				rc = -1;
				break;
			}
			len = strlen(buf);
			p = buf + len - 1;
			if (*p == '\n') *p = '\0';
/*
printf("_chk_heredoc: buf=[%s]\n",buf);
*/
			if (del_tab_space) {
				len = strlen(buf);
				p = akxttrim2(del_tab_space|2,buf,&len);
/*
printf("_chk_heredoc: p=[%s]\n",p);
*/
			}
			else p = buf;
			if (!strcmp(p,name)) {
				rc = 1;
				len = cmn_i_to_a(CLcList.cmd.rc,buf);
				nam_len = lp->prmlen;
				name = akxttrim2(0,lp->prp,&nam_len);
				p = cl_const_ct_malloc(CLcList.ConstCt,nam_len+len+2);
				memcpy(p,name,nam_len);
				lp->prp = p;
				p += nam_len;
				*p++ = ' ';
				memzcpy(p,buf,len);
				lp->prmlen = nam_len + len + 1;
/*
printf("_chk_heredoc: lp->prp=[%s]\n",lp->prp);
*/
				break;
			}
		}
	}
/*
printf("_chk_heredoc:Exit rc=%d\n",rc);
*/
	return rc;
}

/************************************/
/* _kugiri							*/
/************************************/
static int _kugiri(kc,h,wkstr,pinfo)
char kc,*wkstr,*pinfo;
int  h;
{
	int rc,parmlen,cnum,cid,no_kugiri,n,i,thenl,elsel,cmd_count,scno,pos,heredoc;
	cmdTable *pcmd;
	char *p,wrk[2];

	parmlen = h;
	wkstr[parmlen] = '\0';

DEBUGOUTL5(LEX_LOG_GROUP+124,"_kugiri: kc=%c h=%d wkstr=[%s] info=%c cid=%08x",kc,h,wkstr,*pinfo,CLcList.cmd.cid);

/*
printf("_kugiri: kc=%c h=%d wkstr=[%s] info=%c cid=%08x\n",kc,h,wkstr,*pinfo,CLcList.cmd.cid);
*/
/*
printf("_kugiri: cid=%08x thenl_level=%d\n",cid,thenl_level);
*/
	if (*pinfo == 'N') CLcList.cmd.cid = C_UNKNOWN;
	thenl = 0;
	elsel = 0;
	heredoc = 0;
	if (parmlen>0 || (!parmlen && (kc=='(' || kc=='['))) {
		if (*pinfo == 'N'){	/* コマンド */
			cl_cmd_init(&CLcList.cmd);
			pcmd = cl_cmd_chk(wkstr);
/*
printf("_kugiri: wkstr=[%s] at line=%d\n",wkstr,CLcList.line);
*/
			if (pcmd->cmdid == C_UNKNOWN) {
				strcpy(comstr,"LET");
				pcmd = cl_cmd_chk(comstr);
				CLcList.cmd.cid = cid = pcmd->cmdid;
				CLcList.cmd.type = pcmd->type;
				CLcList.cmd.rc  =  CLcList.line;
				*pinfo = 'C';
				if (kc=='[' || kc=='(') return C_PARM_CONTINUE;
				if (rc=clparmsetopt(wkstr, parmlen,1)) return rc;
			}
			else {
				CLcList.cmd.cid = cid = pcmd->cmdid;
				CLcList.cmd.type = pcmd->type;
				CLcList.cmd.rc  =  CLcList.line;/*add 95.1.4 Koba*/
				*pinfo = 'C';
				strcpy(comstr,wkstr);
				if (kc == '(') return C_PARM_START;
				if ((cid==C_ELSE || cid==C_DEFAULT || cid==C_ELSEL ||
				     cid==C_FINALLY) &&
				    (kc==' ' || kc=='\t' || kc=='\n')) {
					kc = M_KUGIRI;
					if (cid == C_ELSEL) {
						CLcList.cmd.cid = cid = C_ELSE;
						elsel = 1;
					}
				}
				else if (cid==C_LABEL && CLcList.cmd.type==9) {
					kc = M_KUGIRI;
					if (rc=clparmsetopt(wkstr,parmlen-1,1)) return rc;
				}
			}
		}
		else {	/* パラメータ */
			CLcList.cmd.rc  =  CLcList.line;
			no_kugiri = 1;
			cid = CLcList.cmd.cid;
			if (cid==C_IF || cid==C_ELSEIF) {
				if (!stricmp(wkstr,"THEN")) {
					no_kugiri = 0;
					kc = M_KUGIRI;
				}
				else if (!stricmp(wkstr,"THENL")) {
					no_kugiri = 0;
					kc = M_KUGIRI;
					thenl = 1;
				}
			}
			else if ((cid==C_LOOP || cid==C_FOR || cid==C_WHILE || cid==C_UNTIL) &&
			     !stricmp(wkstr,"DO")) {
				no_kugiri = 0;
				kc = M_KUGIRI;
				CLcList.cmd.sub_cid = C_LOOP_DO;
			}
			else if (cid == C_TRY) {
				pos= akxnskipin(wkstr,strlen(wkstr)," \t");
				if (!CLcList.cmd.prmnum && wkstr[pos]!='(') {
					if ((rc=_kugiri(M_KUGIRI,0,wrk,pinfo)) < 0) return rc;
					return _kugiri(kc,h,wkstr,pinfo);
				}
			}
			if (no_kugiri) {
				if (rc=clparmsetopt(wkstr,parmlen,1)) return rc;
			}
		}
	}
	cid = CLcList.cmd.cid;
	if (kc==M_KUGIRI && cid!=C_UNKNOWN) {
		if (cid==C_EXEC || cid==C_CALL) {
			if ((rc=_chk_heredoc(cid)) < 0) return rc;
			else if (rc > 0) heredoc = 1;
		}
/*
printf("_kugiri: comstr=[%s] at line=%d\n",comstr,CLcList.line);
*/
		if (rc = clparmset("",0)) return rc;	/* ワークエリアを最後に付ける */
		if (rc = clparmset("",0)) return rc;	/* 2020.10.29 add */
		CLcList.cmd.prmnum -= 2;				/* パラメータ数に上記は入れない */
		if (rc = clparmset2()) return rc;
		cid = CLcList.cmd.cid;
/*
printf("_kugiri: thenl_level=%d else_level=%d cmd_count=%d\n",
thenl_level,else_level,cmd_count);
*/
		if ((cmd_count=_get_if_level()) > 0) {
			if (cid==C_ELSE || cid==C_ELSEIF ||
			    (cid==C_END && !stricmp(wkstr,"IF")) || cmd_count>1) {
				while (_get_if_level() > 0) {
					if (rc=_insert_ENDIF(1)) return rc;
					_set_if_level(-1,0);
				}
				elsel = 0;
			}
		}
		if (rc = cl_syn_main()) {	/* 解析木作成 */
			return rc;
		}
		scno = CLcList.cmd.sub_cid;
		if ((cid==C_LET || cid==C_DEFINE) && (scno==CS_OPTIONS || scno==CS_OPTION)) {
			cM_QUOTE1 = pGlobTable->Quot[0];
			cM_QUOTE2 = pGlobTable->Quot[1];
		}
		/* "end if" は、cl_syn_main()の中でC_ENDIFに変換されるのでcidを再設定する */
		cid = CLcList.cmd.cid;
/*
printf("_kugiri: comstr=[%s] at line=%d thenl=%d elsel=%d thenl_level=%d\n",
comstr,CLcList.line,thenl,elsel,_get_if_level());
*/
		if (cid == C_IF) {
			if (_get_if_level() > 0) _set_if_level(0,2);
			_set_if_level(1,thenl);
		}
		else if (cid == C_ELSEIF) _set_if_level(0,thenl);
		else if (cid == C_ELSE)   _set_if_level(0,elsel);
		else if (cid == C_ENDIF)  _set_if_level(-1,0);
		else {
			while (_get_if_level() > 0) {
				if (rc = _insert_ENDIF(1)) return rc;
				_set_if_level(-1,0);
			}
		}
		*pinfo = 'N';
		cl_cmd_init(&CLcList.cmd);
		clparmclr();
	}
	if (heredoc) rc = C_PARM_HEREDOC;
	else rc = 0;
	return rc;
}

/************************************/
/* _nest_up							*/
/************************************/
static int _nest_up(ifnest_cnt)
int ifnest_cnt;
{
	ifnest_cnt++;
	if (ifnest_cnt>=MAXIFNEST) {
		ERROROUT("if nest over!!");
		return -1;
	}
	return ifnest_cnt;
}

/************************************/
/* _check_defined					*/
/************************************/
static int _check_defined(word,pp)
char *word,**pp;
{
	int ix;

	ix = 0;
	if (       sqp_lex_def)  ix = akxs_xseqs2(sqp_lex_def,'R',word,pp);
	if (!ix && xhp_main_def) ix = akxs_xhash2(xhp_main_def,'R',word,pp);
	if (!ix && xhp_lex_def)  ix = akxs_xhash2(xhp_lex_def,'R',word,pp);
/*
printf("_check_defined: ix=%d\n",ix);
*/
	return ix;
}

/************************************/
/* _PragmaStackInit					*/
/************************************/
static int _PragmaStackInit()
{
	SSPL_S *pspSTK;
	tdtInfoParm *pInfoParm;
	condList *pCLcList;
	tdtPragmaStack *pPrag;
	char *p;

	while (iListStack > 0) {
		pPrag = &tPragmaStack[iListStack];
		pCLcList = &pPrag->pra_cList;
		if (pCLcList->option & D_SCRPT_MEMORY) {
			pInfoParm = (tdtInfoParm *)pCLcList->fp;
			Free(pInfoParm->pi_data);
			Free(pInfoParm->pi_paux);
			Free(pInfoParm);
		}
		iListStack--;
		pPrag = &tPragmaStack[iListStack];
		if (p=pPrag->pra_defkey) Free(p);
		pspSTK = &pPrag->pra_sspl;
		if (p=pspSTK->wd) Free(p);
	}
	p = akxt_get_last_name("\\/",CLcList.fullname);
/*
printf("_PragmaStackInit: p=[%s]\n",p);
*/
	pPrag = &tPragmaStack[0];
	pPrag->pra_opt = 1;
	pPrag->pra_defkey = Strdup(p);
	pspSTK = &pPrag->pra_sspl;
	pspSTK->wd = NULL;
	iListStack0=1;
	iListStack=1;
	if (xhp_lex_def) {
		akxs_xhash_free(xhp_lex_def);
		xhp_lex_def = NULL;
	}
	if (sqp_lex_def) {
		akxs_xseqs_free(sqp_lex_def);
		sqp_lex_def = NULL;
	}
	return 0;
}

/************************************/
/* _proc_pragma						*/
/************************************/
static int _proc_pragma(buf,pos,psspl,optc,sc_code)
char *buf;
int pos,sc_code;
SSPL_S *psspl;
char optc;
{
	static char *_fn_="_proc_pragma";
	static char *command[]=
		{"define","undef","ifdef","ifndef","if","elif","else","endif","elifdef","elifndef","include",NULL};
	SSP_S ssp;
	char *p,word0[BUFLEN*8],cmd,c,*key,*wbuf,*word;
	int len,i,ix,icmd,iTrue,tr,ifflg,word_len,buf_len;
	int op,stack[2],istack,v,rc;

	if (!buf) {
		ifnest_cnt=0;
		ifnest[0]=1;
		iftrue[0]=0;
		return _PragmaStackInit();
	}
	word = word0;
	word_len = sizeof(word0);
	wbuf = buf;
	buf_len = psspl->wdmax;
#if 1	/* 2022.1.17 */
	_get_def_cont(buf+pos,buf_len-pos,word,word_len,sc_code,1,buf,psspl);
	wbuf = word;
	pos = 0;
	buf_len = strlen(wbuf);
	word += buf_len + 1;
	word_len -= buf_len + 1;
#endif
	CLcList.cmd.cid = C_PRAGMA;
	ssp.sp = pos;
	ssp.wd = word;
	if ((len=akxtgwsp(wbuf,&ssp)) > 0) {

DEBUGOUTL1(LEX_LOG_GROUP+122,"_proc_pragma: word=[%s]",ssp.wd);

		clparmclr();
		clparmset(wbuf+pos,psspl->wdmax-pos);
		icmd = i = 0;
		while (p=command[i++]) {
			if (!strcmp(word,p)) break;
		}
		if (p) {
			icmd=i;
		}
		else {
			ERROROUT1("_proc_pragma: invalid command(%s).",word);
			return -1;
		}
		CLcList.cmd.sub_cid = i;
/*
printf("_proc_pragma: icmd=%d ifnest_cnt=%d ifnest=%d\n",icmd,ifnest_cnt,ifnest[ifnest_cnt]);
*/
		if (icmd==1 || icmd==2) {			/* define, undef */
/*
printf("_proc_pragma: icmd=%d optc=%c\n",icmd,optc);
*/
			if (optc == DEF_OPTC_ICASE) {
				if (!sqp_lex_def) {
					if (!(sqp_lex_def=akxs_xseqs_new2(0,DEFICASEMAX,0,0))) return -1;
					sqp_lex_def->xha_prereg = AKX_ARGV_USE_ICMP;
				}
			}
			else {
				if (!xhp_lex_def) {
					if (!(xhp_lex_def=akxs_xhash_new2(0,10,7,0))) return -1;
				}
			}
			if ((len=akxtgwse(wbuf,&ssp," \t(",0x01)) > 0) {
DEBUGOUTL2(LEX_LOG_GROUP+122,"_proc_pragma: icmd=%d word=[%s]",icmd,word);
#if 1	/* 2021.11.2 */
				if ((i=cl_chk_name_pos(word,len)) != 0) {
#else
				if ((i=akxnskipto(word,len," \t'\"()")) < len) {
#endif
					/* %s: キー[%s]中(pos=%d)に使用できない文字があります。 */
					ERROROUT3(FORMAT(52),_fn_,word,i);	/* i+1 ==> i 2021.11.2 */
					return -1;
				}
				if (icmd==1) {
/*
printf("_proc_pragma: word=[%s]\n",word);
*/
					if (optc == DEF_OPTC_ICASE)
						ix = akxs_xseqs(sqp_lex_def,'R',word);
					else
						ix = akxs_xhash(xhp_lex_def,'R',word);
					if (ix < 0) return ix;
#if 0	/* 2021.7.18 del 上書きに変更 */
					else if (ix > 0) {
						/* %s: キー[%s]が定義済みです。 */
						ERROROUT2(FORMAT(53),_fn_,word);
						return -1;
					}
#endif
					cmd = 'S';
					ssp.wd += len + 1;
					/* 2022.1.17 */
					i = _get_def_cont(wbuf+ssp.sp,buf_len-ssp.sp,ssp.wd,word_len-(len+1),sc_code,0,buf,psspl);
/*
printf("_proc_pragma: ssp.wd=[%s]\n",ssp.wd);
*/
					if (i < 0) return i;
				}
				else cmd = 'D';
				if (optc == DEF_OPTC_ICASE)
					ix = akxs_xseqs2(sqp_lex_def,cmd,word,ssp.wd);
				else
					ix = akxs_xhash2(xhp_lex_def,cmd,word,ssp.wd);
				if (ix < 0) return ix;
				ssp.wd = word;
			}
		}
		else if (icmd == 11) {	/* include */
			if ((len=akxtgwse(wbuf,&ssp," \t",0x03)) <= 0) {
				/* %s: ファイル名が指定されていません。 */
				ERROROUT1(FORMAT(54),_fn_);
				return -1;
			}
			return _include_define(psspl,word);
		}
		else if (icmd > 0) {
			if (icmd>=6 && ifnest_cnt<=0) {
				/* %s: if,ifdef,ifndefがないのに、[%s]が指定されました。 */
				ERROROUT2(FORMAT(54),_fn_,word);
				return -1;
			}
			tr = iTrue = iftrue[ifnest_cnt];
/*
printf("_proc_pragma: iTrue=%d icmd=%d\n",iTrue,icmd);
*/
			if (icmd==7) {		/* else */
				if (iTrue) v = 0;
				else v = 1;
				ifnest[ifnest_cnt] = v;
				iftrue[ifnest_cnt] = 1;
/*
printf("_proc_pragma: else ifnest_cnt=%d ifnest=%d\n",ifnest_cnt,ifnest[ifnest_cnt]);
*/
			}
			else if (icmd==8) {	/* endif */
				ifnest_cnt--;
			}
			else {
/*
printf("_proc_pragma: word=[%s]\n",word);
*/
				if (icmd==3 || icmd==4 ||		/* ifdef, ifndef */
				    icmd==9 || icmd==10) {		/* elifdef, elifndef */
					if ((len=akxtgwse(wbuf,&ssp," \t(",0x01)) > 0) {
DEBUGOUTL2(LEX_LOG_GROUP+122,"_proc_pragma: icmd=%d word=[%s]\n",icmd,word);
						if (icmd==3 || icmd==4) {
							if ((ifnest_cnt=_nest_up(ifnest_cnt)) < 0) return ifnest_cnt;
						}
						if ((icmd==9 || icmd==10) && iTrue) ix = 0;
						else {
							ix = _check_defined(word,NULL);
							if (ix < 0) return ix;
							if (icmd==4 || icmd==10) {
								if (ix) ix = 0;
								else ix = 1;
							}
							tr = ix;
						}
						ifnest[ifnest_cnt] = ix;
						iftrue[ifnest_cnt] = tr;
/*
printf("_proc_pragma: icmd=%d ifnest_cnt=%d ifnest=%d iftrue=%d\n",icmd,ifnest_cnt,v,tr);
*/
					}
				}
				else if (icmd==5 || icmd==6) {	/* if, elif */
					if ((len=akxtgwsp(wbuf,&ssp)) > 0) {
DEBUGOUTL2(LEX_LOG_GROUP+122,"_proc_pragma: icmd=%d word=[%s]\n",icmd,word);
						if (icmd==5) {
							if ((ifnest_cnt=_nest_up(ifnest_cnt)) < 0) return ifnest_cnt;
						}
						if (icmd==6 && iTrue) v = 0;
						else {
							if (((c=*word)>='0' && c<='9') || c=='+' || c=='-') {
		 						tr = v = atoi(word);
							}
							else {
								tr = v = istack = 0;
								while (len>0) {
									if (!strcmp(word,"defined")) {
										len=akxtgwsp(wbuf,&ssp);	/* '(' */
										len=akxtgwsp(wbuf,&ssp);

DEBUGOUTL1(LEX_LOG_GROUP+122,"_proc_pragma:if: word=[%s]\n",word);

										ix=_check_defined(word,NULL);
										if (ix>0) v = 1;
										else v = 0;
										tr = v;
										len = akxtgwsp(wbuf,&ssp);	/* ')' */
										stack[istack++] = v;
										if (istack == 2) {
											if (op) v = stack[0] && stack[1];
											else    v = stack[0] || stack[1];
											ifnest[ifnest_cnt] = v;

DEBUGOUTL3(LEX_LOG_GROUP+122,"if: stack[0]=%d stack[1]=%d v=%d",stack[0],stack[1],v);

											istack = 0;
										}
									}
									else {
										/* %s: if syntax error!! defined only(word=[%s]). */
										ERROROUT2(FORMAT(551),_fn_,word);
										return -1;
									}
									if ((len=akxtgwsp(wbuf,&ssp)) > 0) {
										if (!strcmp(word,"||")) op = 0;
										else if (!strcmp(word,"&&")) op = 1;
										else {
											/* %s: if syntax error!! op=[%s] */
											ERROROUT2(FORMAT(552),_fn_,word);
											return -1;
										}
/*
printf("if: op=%s %d\n",w,op);
*/
										len = akxtgwsp(wbuf,&ssp);
									}
								}
							}
						}
						ifnest[ifnest_cnt] = v;
						iftrue[ifnest_cnt] = tr;
/*
printf("_proc_pragma: icmd=%d ifnest_cnt=%d ifnest=%d iftrue=%d\n",icmd,ifnest_cnt,v,tr);
*/
					}
				}
			}
			ifflg = 1;
			for (i=1;i<=ifnest_cnt;i++) {
				if (!ifnest[i]) {
					ifflg = 0;
					break;
				}
			}
/*
printf("_proc_pragma: icmd=%d ifflg=%d\n",icmd,ifflg);
*/
			i = _skip_line(buf,psspl);
			if (!ifflg) {
				while (i>=0) {
					if (buf[0]=='#' && strchr(M_PRAGMA,buf[1])) break;
					i = _skip_line(buf,psspl);
				}
			}
			return i;
		}
	}
	i = _skip_line(buf,psspl);
/*
printf("_proc_pragma:Exit i=%d\n",i);
*/
	return i;
}

/************************************/
/* _rep_define						*/
/************************************/
/* 2021.10.31 */
static int _rep_define_sub(mcat0,s0,n,ssp0)
MCAT *mcat0;
char *s0;
int n;
SSPL_S *ssp0;
{
	static char *_fn_="_rep_define_sub";
	MCAT mcat;
	int i,ix,rc,dlen,ret,len,sp1,sp2,is;
	char *s,*p,*buff,word[256];
	condList *pclSTK;
	SSPL_S *pspSTK,ssp;
	tdtInfoParm *pInfoParm;
	tdtPragmaStack *pPrag;

	rc = 0;
	s0[n] = '\0';

DEBUGOUTL2(LEX_LOG_GROUP+106,"_rep_define_sub: n=%d s0=[%s]",n,s0);

	if (n>0 && (sqp_lex_def || xhp_main_def || xhp_lex_def)) {
/*
printf("_rep_define_sub:Enter n=%d s0=[%s]\n",n,s0);
*/
		s = s0;
		sp1 = ssp0->sp;
		while ((len=akxtgwnsl(s,n,ssp0,cmp_sep,0x41)) > 0) {
/*
printf("%s: len=%d sp=%d wd=[%s] sp1=%d\n",_fn_,len,ssp0->sp,ssp0->wd,sp1);
*/
			if (ssp0->attr[0] == 1) {
				ix = _check_defined(ssp0->wd,&p);
				if (ix > 0) {
					akxtmcat(mcat0,s+sp1,ssp0->sp-len-sp1);
					sp1 = ssp0->sp;
/*
printf("%s: iListStack=%d ix=%d s=[%s] p=[%s]\n",_fn_,iListStack,ix,s,p);
*/
					pPrag = &tPragmaStack[0];
					for (i=0;i<iListStack;i++,pPrag++) {
/*
printf("_rep_define: defStack[%d]=[%s]\n",i,pPrag->pra_defkey);
*/
						if (!pPrag->pra_opt && !strcmp(pPrag->pra_defkey,s)) {
							/* %s: define word[%s] is loop!! */
							ERROROUT2(FORMAT(553),_fn_,s);
							return -1;
						}
					}
					if ((dlen=strlen(p)) <= 0) return C_NO_DEF_PARM;
					is = akxs_in_str_opt(p,"(",0);
					memset(&mcat,0,sizeof(MCAT));
					mcat.mc_extlen = 128;
					if (is > 0) {
						if (*(s+ssp0->sp)!='(') {
							/* %s: パラメータが必要です。 */
							ERROROUT1(FORMAT(44),_fn_);
							return -1;
						}
						if ((rc=_rep_def_parm(&mcat,s0+sp1-len,p,ssp0)) < 0) return rc;
						p = mcat.mc_bufp;
						dlen = mcat.mc_ipos;
					}
					memset(&ssp,0,sizeof(SSP_S));
					ssp.wd = word;
					ssp.wdmax = sizeof(word);
					ret = _rep_define_sub(mcat0,p,dlen,&ssp);
					if (p=mcat.mc_bufp) Free(p);
					rc = 1;
				}
				else akxtmcat(mcat0,s+sp1,ssp0->sp-sp1);
			}
			else akxtmcat(mcat0,s+sp1,ssp0->sp-sp1);
			sp1 = ssp0->sp;
		}
	}
	return rc;
}

static int _adjust_kakko(s0,n,ka)
char *s0;
int n,ka[];
{
	static char *KKO = "({[)}]";
	static char *RKKO = ")}]({[";
	int len,k1,k2,k3,i,pos,rc;
	char c,work[256],*p;
	SSPL_S ssp;
/*
printf("_adjust_kakko:Enter n=%d s0=[%s]\n",n,s0);
*/
	k1 = k2 = k3 = 0;
	memset(&ssp,0,sizeof(SSPL_S));
	i = 0;
	while ((len=akxtgwnsl(s0,n,&ssp,cmp_sep,1)) > 0) {
		if ((c=*ssp.wd) == '(') {
			k1++;
			work[i++] = c;
		}
		else if (c == ')') {
		/*	if (k1 <= 0) return -1;	*/
			k1--;
			work[i++] = c;
		}
		else if (c == '{') {
			k2++;
			work[i++] = c;
		}
		else if (c == '}') {
		/*	if (k1 <= 0) return -1;	*/
			k2--;
			work[i++] = c;
		}
		else if (c == '[') {
			k3++;
			work[i++] = c;
		}
		else if (c == ']') {
		/*	if (k1 <= 0) return -1;	*/
			k3--;
			work[i++] = c;
		}
	}
/*
printf("_adjust_kakko: k1=%d k2=%d k3=%d i=%d\n",k1,k2,k3,i);
*/
	ka[0] -= k1;
	ka[1] -= k2;
	ka[2] -= k3;
	i--;
	for (;i>= 0;i--) {
		c = work[i];
		pos = instrchar(KKO,c);
		rc = _chk_act(*(RKKO+pos-1));
	}
	return 0;
}

static int _rep_define(psspl,s0,n,k1,k2,k3,ka)
SSPL_S *psspl;
char *s0;
int n;
int k1,k2,k3,ka[];
{
	static char *_fn_="_rep_define";
	static MCAT mcat={'M','C',256,0,0,0,NULL,0};
	int i,ix,rc,len,sp1,nn,len1,i0,m,mm;
	char *p,word[256],*pp,*buf;
	SSPL_S ssp;

	rc = 0;
	s0[n] = '\0';
	ka[0] = k1;
	ka[1] = k2;
	ka[2] = k3;
	if (n > 0) {
/*
printf("_rep_define:Enter k1=%d k2=%d k3=%d n=%d s0=[%s]\n",k1,k2,k3,n,s0);
*/
		memset(&ssp,0,sizeof(SSPL_S));
		ssp.wd = word;
		ssp.wdmax = sizeof(word);
		mcat.mc_ipos = 0;
		if (mcat.mc_bufp) *mcat.mc_bufp = '\0';
		while ((len=akxtgwnsl(s0,n,&ssp,cmp_sep,0x41)) > 0) {
/*
printf("%s: len=%d sp=%d wd=[%s]\n",_fn_,len,ssp.sp,ssp.wd);
*/
			if (ssp.attr[0] == 1) {
				ix = _check_defined(ssp.wd,&p);
				if (ix > 0) {
					ssp.sp = ssp.sp - len;
					akxtmcat(&mcat,s0,ssp.sp);
					rc = _rep_define_sub(&mcat,s0,n,&ssp,ka);
					if (rc == 1) {
						_adjust_kakko(s0,n,ka);
						akxtmcats(&mcat,"");
						m = mcat.mc_ipos;
/*
printf("%s: mc_ipos=%d mc_bufp=[%s]\n",_fn_,m,mcat.mc_bufp);
*/
						buf  = psspl->wd;
						i    = psspl->sp;
						len1 = psspl->wdmax;
/*
printf("%s: i=%d len=%d buf=[%s]\n",_fn_,i,psspl->wdmax,buf);
*/
						i0 = i - n;
						nn = n - m;
						if (i0 < 0) {
							nn += i0;
							i0 = 0;
						}
						if (nn > 0) {
							memzcpy(buf+i-nn,buf+i,len1-i);
							psspl->wdmax -= nn;
						}
						else if (nn < 0) {
							psspl->wdmax -= nn;
							p = buf + len1 + 1;
							pp = p - nn;
							if (i0 < 0) {
								psspl->wdmax -= i0;
								pp += -i0;
								i0 = 0;
							}
							nn = len1 - i + 2;
							while (nn-- > 0) {
								*pp-- = *p--;
							}
						}
						psspl->sp = i0;
						memcpy(buf+i0,mcat.mc_bufp,m);
/*
printf("%s: i0=%d len=%d buf=[%s]\n",_fn_,i0,psspl->wdmax,buf);
*/
						s0[0] = '\0';
					}
					break;
				}
			}
		}
	}
	return rc;
}

/************************************/
/* _resume_define					*/
/************************************/
static int _resume_define(psspl)
SSPL_S *psspl;
{
	int ix,rc,ret=0;
	char *p;
	condList *pclSTK;
	SSPL_S *pspSTK;
	tdtInfoParm *pInfoParm;
	SSP_S *ssp;
	condList *pCLcList;
	tdtPragmaStack *pPrag;
/*
printf("_resume_define:1: iListStack=%d psspl->sp=%d psspl->wd=[%s]\n",
iListStack,psspl->sp,psspl->wd);
*/
	if (iListStack > iListStack0) {
		pPrag = &tPragmaStack[iListStack];
		pCLcList = &pPrag->pra_cList;
		if (pCLcList->option & D_SCRPT_MEMORY) {
			pInfoParm = (tdtInfoParm *)pCLcList->fp;
			Free(pInfoParm->pi_data);
			Free(pInfoParm->pi_paux);
			Free(pInfoParm);
		}
		iListStack--;
		pPrag = &tPragmaStack[iListStack];
		if (p=pPrag->pra_defkey) Free(p);
		pPrag->pra_defkey = NULL;
		pCLcList = &pPrag->pra_cList;
		CLcList.fp = pCLcList->fp;
		CLcList.line = pCLcList->line;
		CLcList.option = pCLcList->option;
		CLcList.fullname = pCLcList->fullname;
		pspSTK = &pPrag->pra_sspl;
		p = psspl->wd;
		memcpy(psspl,pspSTK,sizeof(SSPL_S));
		psspl->wd = p;
		memcpy(p,pspSTK->wd,psspl->wdmax);
		Free(pspSTK->wd);
		ret = 1;

DEBUGOUTL3(LEX_LOG_GROUP+122,"_resume_define:2: iListStack=%d psspl->sp=%d psspl->wd=[%s]",
iListStack,psspl->sp,psspl->wd);

	}
	return ret;
}

/************************************/
/* _rep_def_parm					*/
/************************************/
int _rep_def_parm(mcat,s,parm,ssp0)
MCAT *mcat;
char *s,*parm;
SSPL_S *ssp0;
{
	int is,ie,n,np,i,len,parmlen,attr0,opt,kk;
	char *argv[21],buf[128];
	char *argvp[21],bufp[256],word[256],*p,c;
	SSPL_S sspl;

	is = akxs_in_str_opt(s,"(",0);
	memset(&sspl,0,sizeof(SSPL_S));
	sspl.wd = word;
	sspl.wdmax = sizeof(word);
	/* 2021.11.2 */
	sspl.sp = is;
	kk = 1;
	n = strlen(s);
	while ((len=akxtgwnsl(s,n,&sspl," \t'\"()",1)) > 0) {
		if ((c=*sspl.wd) == '(') kk++;
		else if (c == ')') {
			kk--;
			if (kk <= 0) break;
		}
	}
	ie = sspl.sp;
	ssp0->sp += ie;
	n = akxtgetargvn2(s+is,ie-is-1,argv,20,buf,sizeof(buf),AKX_ARGV_DELM_COM|AKX_ARGV_NOT_CUT_QUOT);
	parmlen = strlen(parm);
	is = akxs_in_str_opt(parm,"(",0);
	ie = akxs_in_str_opt(parm,")",0);
	np = akxtgetargvn2(parm+is,ie-is-1,argvp,20,bufp,sizeof(bufp),AKX_ARGV_DELM_COM);
	if (n != np) {
		/* _rep_def_parm: 実引数の数(%d)と仮引数の数(%d)が合っていません。 */
		ERROROUT2(FORMAT(555),n,np);
		return -1;
	}
	sspl.sp = ie + 1;
	mcat->mc_ipos = 0;
	attr0 = 100;
	if (pGlobTable->options[8] & 0x04) opt = 0x101;
	else opt = 0x201;
	for (;;) {
		if ((len=akxtgwnsl(parm,parmlen,&sspl,NULL,opt)) < 0) break;
		if (attr0<10 && sspl.attr[0]<10) akxtmcats(mcat," ");
		for (i=0;i<np;i++) {
			if (!strcmp(sspl.wd,argvp[i])) {
				break;
			}
		}
		if (i < np) p = argv[i];
		else p = sspl.wd;
		akxtmcats(mcat,p);
		attr0 = sspl.attr[0];
	}
	return 0;
}

/************************************/
/* _include_define					*/
/************************************/
static int _include_define(psspl,name)
SSPL_S *psspl;
char *name;
{
	static char *_fn_="_include_define";
	static char *fmt="***** Include file ( %s ) *****";
	char *scrname,dir[256];
	int i,ix,rc,dlen,ret=0;
	char *buff;
	condList *pclSTK;
	SSPL_S *pspSTK;
	tdtPragmaStack *pPrag;
	FILE *fp;

	if (!(scrname = cl_set_script_name_extension(name,strlen(name)))) return -1;

	pPrag = &tPragmaStack[0];
	for (i=0;i<iListStack;i++,pPrag++) {
/*
printf("_include_define: defStack[%d]=[%s]\n",i,pPrag->pra_defkey);
*/
		if (pPrag->pra_opt && !strcmp(pPrag->pra_defkey,scrname)) {
			/* %s: include file[%s] is loop!! */
			ERROROUT2(FORMAT(556),_fn_,scrname);
			return -1;
		}
	}
	if (iListStack >= CLISTSTACKMAX) {
		/* %s: define stack over(%d)!! */
		ERROROUT2(FORMAT(557),_fn_,iListStack);
		return -1;
	}
/*	PRINTOUT1(fmt,scrname);
	DEBUGOUTL1(1,fmt,scrname);	*/
	PRDBGOUTL1(1,fmt,scrname);

	fp = cl_lex_file_open(scrname,dir,_fn_);
	if (!fp) return ECL_SCR_NOTFOUND;	/* ファイルオープンエラー */
	if ((i=_skip_line(psspl->wd,psspl)) >= 0) psspl->sp = 0;
	pPrag = &tPragmaStack[iListStack];
	pPrag->pra_opt = 1;
	pPrag->pra_defkey = Strdup(scrname);
	memcpy(pclSTK=&pPrag->pra_cList,&CLcList,sizeof(condList));
	memcpy(pspSTK=&pPrag->pra_sspl,psspl,sizeof(SSPL_S));
	pspSTK->wd = Memdup(buff=psspl->wd,psspl->wdmax);
	CLcList.fp = fp;
	CLcList.line = 0;
	CLcList.option = 0;
	if (!(CLcList.fullname = cl_const_ct_malloc(CLcList.ConstCt,strlen(dir)+1))) {
		Free(pPrag->pra_defkey);
		pPrag->pra_defkey = NULL;
		Free(pspSTK->wd);
		pspSTK->wd = NULL;
		return ECL_MALLOC_ERROR;
	}
	strcpy(CLcList.fullname,dir);
/*
printf("_include_define: iListStack=%d scrname=[%s] dir=[%s]\n",iListStack,scrname,dir);
*/
	CLcList.cmd.parl[1].par = CLcList.fullname;
	iListStack++;
	memcpy(&tPragmaStack[iListStack].pra_cList,&CLcList,sizeof(condList));
	if (cl_get_str(buff)) {
		_resume_define(psspl);
	}
	else {
		psspl->sp = 0;
		psspl->wdmax = strlen(buff);
		psspl->attr[3] = 0;
DEBUGOUTL3(LEX_LOG_GROUP+122,"_include_define: iListStack=%d scrname=[%s] buf=[%s]",
iListStack,scrname,buff);
	}
/*
printf("_include_define:Exit \n");
*/
	return 0;
}
