static	char	sccsid[]="%Z% %M% %I% %E% %U%";
/********************************************************************
*                                                                   *
*      処理目的　　：  							                    *
*                                                                   *
*      関数名　　　：　int cl_scparmset( pLeaf )			        *
*                                                                   *
*      引き数      ：　(I)Leaf          * pLeaf                     *
*                                                                   *
*                                                                   *
*      戻り値　　　：　ERROR                                        *
*                      NORMAL                                       *
*                                                                   *
*      処理概要　　：　                                             *
*                                                                   *
********************************************************************/
#include <colmn.h>

/****************************************************/
/* 引数 : proc	: EXECコマンドを実行しているproc	*/
/****************************************************/
static int _get_pTBL(proc,VaryC,prp,prmlen,ppTBL,val)
ProcCT *proc;
VarTBL *VaryC;
char *prp;
int prmlen;
tdtInfoParm ****ppTBL;
int val[];
{
	char *argv[3],parm[128],*p,c;
	int ret,varmax,varall,n,*pSize1,k0,len;
	tdtInfoParm ***pTBL;

	pTBL = NULL;
	ret = varall = varmax = k0 = 0;
	if (akxtgetargvns2(prp,prmlen,argv,3,parm,sizeof(parm)," \t",0) == 2) {
		c = *(p=argv[1]);
		len = strlen(p);
		if (!stricmp(argv[0],"LOCAL") && c=='%' && *(p+len-1)=='*') {
			varall = 1;
			varmax = proc->varnam_pasento;
			pTBL   = proc->pTBL_pasento;
		}
	}
	else {
		p = prp;
		len = prmlen;
		c = *prp;
		if ((c=='%' || c=='#' || c=='$') && *(p+len-1)=='*') {
			if (c == '%') {
				varall = 1;
				pTBL   = VaryC->pTBL_pasento;
			}
			else if (c == '#') {
				varall = 2;
				pTBL   = VaryC->pTBL_igeta;
			}
			else {
				varall = 3;
				pTBL   = VaryC->pTBL_dolu;
			}
		}
	}
/*
printf("_get_pTBL: varall=%d\n",varall);
*/
	if (varall) {
		pSize1 = (int *)pTBL[0];
		varmax = pSize1[7];
		if (len > 2) {
			k0 = atoi(p+1);
/*
printf("_get_pTBL: k0=%d\n",k0);
*/
			if (k0<1/* || k0>varmax*/) {
				ERROROUT1(FORMAT(222),k0);	/* 開始位置(%d)が不正です。*/
				ret = ECL_SCRIPT_ERROR;
			}
#if 0
			else if (k0 > pSize1[2]) {
						/* %s: %s開始位置(%d)が最大値(%d)を超えています。*/
				ERROROUT4(FORMAT(621),"cl_scparmset","(W)",k0,varmax);
				k0 = pSize1[2];
			}
#endif
			k0--;
		}
	}
	*ppTBL = pTBL;
	val[0] = varall;
	val[1] = varmax;
	val[2] = k0;
/*
printf("_get_pTBL: varall=%d varmax=%d k0=%d\n",varall,varmax,k0);
*/
	return ret;
}

#if 1	/* 2021.8.18 */
/********************************************************/
/* 引数 : proc		: EXECコマンドを実行しているproc	*/
/*		  pScCT		: 実行するSCRIPTのScrCt				*/
/********************************************************/
int cl_scparmset(proc,pScCT,stdiow,parl)
ProcCT  *proc;
ScrPrCT *pScCT;
FILE *stdiow[];
ParList2 *parl;
{
	int         rc,i,j,*pSize,len,opt,opt_n;
	tdtInfoParm	**ppmstk,*pInfoParmW,*pInfoParm;
	VarTBL      *VaryW;
	char        *pBuff,c,*p,*pHereDocnm;
	FILE *pHereDocfp;

DEBUGOUTL2(194,"cl_scparmset: proc=%08x pScCT=%08x",proc,pScCT);

	if (!proc || !pScCT) return -1;

	if (proc) ppmstk = proc->ppExecParm;
	if (!ppmstk) return 0;

	VaryW = pScCT->Vary;
	for (i=0,j=0 ;pInfoParm=ppmstk[i];i++) {
DEBUGOUT_InfoParm(194,"cl_scparmset: i=%d pScCT=%08x",
pInfoParm,j,&pInfoParm->pi_len);
		if (pInfoParm->pi_id==D_DATA_ID_PNAME && (rc=cl_is_redirect_fp(pInfoParm->pi_paux,0))>0) {
			pInfoParm = (tdtInfoParm *)pInfoParm->pi_pos;
			if (pInfoParm->pi_id == 'S') pInfoParm = (tdtInfoParm *)pInfoParm->pi_pos;
			p = pInfoParm->pi_data;
			len = pInfoParm->pi_dlen - sizeof(FILE *) - sizeof(int);
			memcpy(&pBuff,p,sizeof(FILE *));
#if defined(_LP64)
			pHereDocfp = (FILE *)x_ntohll((long)pBuff);
#else
			pHereDocfp = (FILE *)ntohl((uint)pBuff);
#endif
			p += sizeof(FILE *);
			memcpy(&opt_n,p,sizeof(int));
			opt = ntohl(opt_n);
			p += sizeof(int);
/*
printf("cl_scparmset: pHereDocfp=%08x rc=%d len=%d opt=%02x p=[%s]\n",pHereDocfp,rc,len,opt,p);
*/
			rc--;
#if 1	/* 2022.5.10 */
			if (rc) {
				stdiow[rc] = pHereDocfp;
			}
			else {
				stdiow[0] = pHereDocfp;
				parl->par = p;
				parl->parlen = len;
				parl->option = opt;
			}
#else
			if (rc) {
				pScCT->stdio[rc] = pHereDocfp;
			}
			else {
				pScCT->stdio[3] = pScCT->stdio[0];
				pScCT->stdio[0] = pHereDocfp;
				pScCT->HereDoc_nm = Malloc(len+1);
				memzcpy(pScCT->HereDoc_nm,p+sizeof(FILE *),len);
				pScCT->pFlag2 |= D_PFLAG2_ALC_HDNM;
			}
#endif
			continue;
		}
		pInfoParmW = cl_get_var_ent_opt(VaryW->pTBL_pasento,++j,'s');
#if 1	/* 2021.10.27 */
		if (cl_gx_rep_info_als(pInfoParmW,pInfoParm,1)) return ECL_SCRIPT_ERROR;
#else
		if (cl_gx_rep_info_set(pInfoParmW,pInfoParm,1)) return ECL_SCRIPT_ERROR;
#endif
		pInfoParmW->pi_aux[1] |= D_AUX1_PROTECTED;
		if ((c=pInfoParmW->pi_id)=='A' || c=='R' || c=='L' || c=='N') {
			pInfoParmW->pi_scale &= ~0x20;
		}
DEBUGOUT_InfoParm(194,"cl_prparmset:W: j=%d pScCT=%08x",
pInfoParmW,j,pInfoParmW->pi_len);
	}
/*
printf("cl_scparmset: j=%d\n",j);
*/
	VaryW->varnam_pasento = j;
	pSize = (int *)VaryW->pTBL_pasento[0];
	cl_set_parm_bin(cl_var_size_parm(pSize),j);
DEBUGOUT_InfoParm(194,"cl_scparmset:j: j=%d rc=%d",cl_var_size_parm(pSize),j,rc);
	return 0;
}
#else
int cl_scparmset(pprmp,prmnum,proc)
parmList **pprmp;
int  prmnum;
ProcCT  *proc;
{
	int         i,rc,k,j;
	ScrPrCT     *pScCT;
	ScrPrCT     *pPreScCT;
	ProcCT      *pProcW;
	int         parmpos,parmnum,*pSize,varmax,varall,n,*pSize1,len,k0,val[3];
	tdtInfoParm prm_list,*pprmListW,*pprmListC,*pInfoParm;
	VarTBL      *VaryW,*VaryC;
	parmList    *prmp;
	char        *pBuff,c,*p,*argv[3],parm[128],*pHereDocnm;
	tdtInfoParm ***pTBL;
	FILE *pHereDocfp;

	if (!pprmp) {
		ERROROUT1(FORMAT(28),"cl_scparmset");	/* cl_scparmset: リンクエラー */
		return ERROR;
	}
	if (prmnum < 0) {
		ERROROUT(FORMAT(376));	/* パラメータ数に誤りがあります。 */
		return ERROR;
	}

	pScCT = cl_search_src_ct();	/* 実行するSCRIPTのScrCt */
	pPreScCT = pScCT->preScCT;	/* EXECコマンドを実行しているSctCt */
DEBUGOUTL2(194,"cl_scparmset: pPreScCT=%08x pScCT=%08x",pPreScCT,pScCT);

	pSize = (int *)pScCT->Vary->pTBL_pasento[0];
	VaryW = pScCT->Vary;
	VaryC = pScCT->Vary = pPreScCT->Vary;
#if 1	/* 2021.6.26 */
/*
printf("cl_scparmset: pScCT=%08x proc=%08x pPreProc=%08x\n",pScCT,proc,pPreScCT->ProCT);
*/
	pProcW = pScCT->CurProc;
	pScCT->CurProc = proc;
#endif

	for (i=0, parmpos=0, j=0 ;i<prmnum; i++,parmpos++) {
		prmp = pprmp[parmpos];
		p = prmp->prp;
		len = prmp->prmlen;
/*
printf("cl_scparmset: len=%d p=[%s]\n",len,p);
*/
		if ((rc=_get_pTBL(proc,VaryC,p,len,&pTBL,val)) < 0) goto LErr;
		varall = val[0];
		if (varall) {
			varmax = val[1];
			k0 = val[2];
			pSize1 = (int *)pTBL[0];
			for (k=k0;k<varmax;k++) {
				if (j>=pSize1[2]) {
#if 1	/* 2021.7.24 */
					rc = ECL_SCRIPT_ERROR;
					goto LErr;
#else
#if 1	/* 2021.6.26 */
					pScCT->CurProc = pProcW;
#endif
					pScCT->Vary = VaryW;
					pScCT->Vary->varnam_pasento = j;
					cl_set_parm_bin(cl_var_size_parm(pSize),j);
					return ECL_SCRIPT_ERROR;
#endif
				}
				pprmListW = cl_get_var_ent_opt(VaryW->pTBL_pasento,++j,'s');
				pprmListC = cl_get_var_ent(pTBL,k+1);
				if (rc = cl_gx_rep_info_set(pprmListW,pprmListC,1)) goto LErr;
				pprmListW->pi_aux[1] |= D_AUX1_PROTECTED;
			}
		/*	pSize1 = (int *)VaryW->pTBL_pasento[0];	*/
			VaryW->varnam_pasento = pSize[7];
		}
		else {
			if ((rc=cl_gx_exp_obj(1,&pprmp[parmpos],proc->Obj,&prm_list))<0) {
#if 1	/* 2021.7.24 */
				goto LErr;
#else
#if 1	/* 2021.6.26 */
				pScCT->CurProc = pProcW;
#endif
				pScCT->Vary = VaryW;
				pScCT->Vary->varnam_pasento = j;
				cl_set_parm_bin(cl_var_size_parm(pSize),j);
				return ECL_SCRIPT_ERROR;
#endif
			}
			if (rc==100) cl_null_parm(&prm_list);
DEBUGOUT_InfoParm(194,"cl_scparmset: i=%d pScCT=%08x",
&prm_list,i,&prm_list.pi_len);
			if (prm_list.pi_id==D_DATA_ID_PNAME && (rc=cl_is_heredoc_fp(prm_list.pi_paux,0))>0) {
				pInfoParm = (tdtInfoParm *)prm_list.pi_pos;
#if 1	/*  2021.8.11 */
				if (pInfoParm->pi_id == 'S') pInfoParm = (tdtInfoParm *)pInfoParm->pi_pos;
#endif
				p = pInfoParm->pi_data;
				len = pInfoParm->pi_dlen - sizeof(FILE *);
				memcpy(&pBuff,p,sizeof(FILE *));
#if defined(_LP64)
				pHereDocfp = (FILE *)x_ntohll((long)pBuff);
#else
				pHereDocfp = (FILE *)ntohl((uint)pBuff);
#endif
/*
printf("cl_scparmset: pHereDocfp=%08x rc=%d\n",pHereDocfp,rc);
*/
				rc--;
				pScCT->stdio[rc] = pHereDocfp;
				if (!rc) {
					pScCT->stdio[3] = pHereDocfp;
					pScCT->HereDoc_nm = Malloc(len+1);
					memzcpy(pScCT->HereDoc_nm,p+sizeof(FILE *),len);
				}
				continue;
			}
			pprmListW = cl_get_var_ent_opt(VaryW->pTBL_pasento,++j,'s');
			if (rc = cl_gx_rep_info_set(pprmListW,&prm_list,1)) goto LErr;
			pprmListW->pi_aux[1] |= D_AUX1_PROTECTED;
			if ((c=pprmListW->pi_id)=='A' || c=='R' || c=='L' || c=='N') {
				pprmListW->pi_len = (long)pPreScCT;
				pprmListW->pi_scale &= ~0x20;
			}
DEBUGOUT_InfoParm(194,"cl_scparmset:W: i=%d pScCT=%08x",
pprmListW,i,pprmListW->pi_len);
		}
	}
	rc = 0;
 LErr:
#if 1	/* 2021.6.26 */
	pScCT->CurProc = pProcW;
#endif
	pScCT->Vary = VaryW;
	pScCT->Vary->varnam_pasento = j;
	cl_set_parm_bin(cl_var_size_parm(pSize),j);
DEBUGOUT_InfoParm(194,"cl_scparmset:j: j=%d rc=%d",cl_var_size_parm(pSize),j,rc);

	return rc;
}
#endif

/****************************************/
/*										*/
/****************************************/
int cl_prparmset(/*pLeaf,*/pProc,pExProcName)
/*Leaf *pLeaf ;*/
ProcCT  *pProc;
char *pExProcName;
{
	int         rc,j;
	ScrPrCT     *pScCT;
	int         *pSize; 
	tdtInfoParm	*pInfoParmW,*pInfoParm;
	VarTBL      *VaryC;
	char        c;
	ProcCT      *proc;
	tdtInfoParm **ppmstk;

	if (!(proc = cl_search_proc_ct())) return -2;
	if (proc->varnam_pasento > 0) return 0;
	if (rc=cl_mk_pr_var_set(proc)) return rc;
	if (pProc) ppmstk = pProc->ppExecParm;
	else ppmstk = NULL;
	if (!ppmstk || !strcmp(pExProcName,"main")) {
		pScCT = cl_search_src_ct();
		VaryC = pScCT->Vary;
		proc->varnam_pasento = VaryC->varnam_pasento;
		proc->pTBL_pasento = VaryC->pTBL_pasento;
		proc->pFlag |= D_PFLAG_PARM_COPY;
		return 0;
	}

	for (j=0 ;pInfoParm=ppmstk[j];) {
		pInfoParmW = cl_get_var_ent_opt(proc->pTBL_pasento,++j,'s');
#if 1	/* 2021.10.27 */
		if (cl_gx_rep_info_als(pInfoParmW,pInfoParm,1)) return ECL_SCRIPT_ERROR;
#else
		if (cl_gx_rep_info_set(pInfoParmW,pInfoParm,1)) return ECL_SCRIPT_ERROR;
#endif
		pInfoParmW->pi_aux[1] |= D_AUX1_PROTECTED;
		if ((c=pInfoParmW->pi_id)=='A' || c=='R' || c=='L' || c=='N') {
			pInfoParmW->pi_scale &= ~0x20;
		}
DEBUGOUT_InfoParm(194,"cl_prparmset:W: j=%d pScCT=%08x",
pInfoParmW,j,pInfoParmW->pi_len);
	}
	proc->varnam_pasento = j;
	pSize = (int *)proc->pTBL_pasento[0];
	cl_set_parm_bin(cl_var_size_parm(pSize),j);

	return NORMAL;
}

/****************************************/
/*										*/
/****************************************/
int cl_set_exec_parm(pprmp,parmnum,pProc)
parmList **pprmp;
int  parmnum;
ProcCT  *pProc;
{
	int         i,rc,k,j,maxnum,n,numinc,inc;
	ScrPrCT     *pScCT;
	int         parmpos;
	int         *pSize,varmax,varall,val[3],k0;
	tdtInfoParm	tInfoParm,*pInfoParmW;
	VarTBL      *VaryC;
	parmList    *parm;
	char        *p;
	tdtObjHead  *pObj;
	ProcCT      *proc;
	tdtInfoParm **ppmstk,***pTBL;

DEBUGOUTL3(120,"cl_set_exec_parm:Enter pprmp=%08x parmnum=%d pProc=%08x",pprmp,parmnum,pProc);

	if (!pprmp || !pProc) {
		ERROROUT1(FORMAT(28),"cl_set_exec_parm");	/* cl_set_exec_parm: リンクエラー */
		return ERROR;
	}
	if (!(pScCT = cl_search_src_ct())) {
		ERROROUT("cl_set_exec_parm: ScCT not found.");
		return ERROR;
	}
/* del 2020.6.16
	if (!(proc = cl_search_proc_ct())) {
		ERROROUT("cl_set_exec_parm: proc not found.");
		return -1;
	}
*/
	VaryC = pScCT->Vary;
	maxnum = parmnum;
	if (pProc) pObj = pProc->Obj;
/*	pSize = (int *)proc->pTBL_pasento[0];	del 2020.6.16 */
	if (maxnum > 0) {
		numinc = VaryC->varnam_pasento;	/* '%*'の数を加えておく */
		maxnum += numinc;
	}

DEBUGOUTL1(120,"cl_set_exec_parm:0: maxnum=%d",maxnum);
/*
printf("cl_set_exec_parm:ret:0: maxnum=%d\n",maxnum);
*/
	if (!(p=cl_tmp_const_malloc(sizeof(tdtInfoParm *)*(maxnum+1)))) {
		return ECL_MALLOC_ERROR;
	}
	ppmstk = (tdtInfoParm **)p;
	for (i=0, parmpos=0, j=0 ;i<parmnum; i++,parmpos++)  {
		parm = pprmp[parmpos];
DEBUGOUTL2(120,"cl_set_exec_parm: i=%d parm->prp=[%s]",i,parm->prp);
		/* 2021.7.8 */
		if ((rc=_get_pTBL(proc,VaryC,parm->prp,parm->prmlen,&pTBL,val)) < 0) return rc;
		inc = 1;
		varall = val[0];
		if (varall) {
			varmax = val[1];
			inc = varmax;
		}
		if (j+inc-1 >= maxnum) {
			numinc = 16;
			if (inc > numinc) numinc = inc;
			n = maxnum + numinc;
			if (!(p=cl_tmp_const_malloc(sizeof(tdtInfoParm *)*(n+1)))) {
				return -1;
			}
			memcpy(p,ppmstk,sizeof(tdtInfoParm *)*maxnum);
			maxnum = n;

DEBUGOUTL1(120,"cl_set_exec_parm:1: maxnum=%d",maxnum);
/*
printf("cl_set_exec_parm:ret:1: inc=%d maxnum=%d\n",inc,maxnum);
*/
			ppmstk = (tdtInfoParm **)p;
		}
		if (varall) {
			k0 = val[2];
			pSize = (int *)pTBL[0];
/*
printf("cl_set_exec_parm: pSize[2]=%d\n",pSize[2]);
*/
			for (k=k0;k<varmax;k++) {
				if (j>=pSize[2]) {
					return ECL_SCRIPT_ERROR;
				}
				ppmstk[j++] = cl_get_var_ent(pTBL,k+1);
			}
		}
		else {
			if ((rc=cl_gx_exp_obj(1,&pprmp[parmpos],pObj,&tInfoParm))<0) {
				return ECL_SCRIPT_ERROR;
			}
			if (rc==100) cl_null_parm(&tInfoParm);
DEBUGOUT_InfoParm(194,"cl_set_exec_parm: : i=%d pScCT=%08x",
&tInfoParm,i,&tInfoParm.pi_len);
			if (!(pInfoParmW=(tdtInfoParm *)cl_tmp_const_malloc(sizeof(tdtInfoParm)))) {
				return ECL_MALLOC_ERROR;
			}
			cl_gx_copy_info(pInfoParmW,&tInfoParm);
DEBUGOUT_InfoParm(194,"cl_set_exec_parm:W: i=%d pScCT=%08x",
pInfoParmW,i,pInfoParmW->pi_len);
			ppmstk[j++] = pInfoParmW;
		}
	}
	ppmstk[j] = NULL;
	pProc->ppExecParm = ppmstk;
/*
printf("cl_set_exec_parm:Exit j=%d\n",j);
*/
	return NORMAL;
}
