function [stat,post] = blkscript(path,prop,bstr,bint,bflt,parent,name,catalog,ws,node)
%BLKSCRIPT Adds a general programming functionality to the algebraic and logical
%          expression blocks.
%
%  See also BLKUSER, BLKZCROSS

% Initial coding by A S Bozin, 9-1-97
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.8 $ $Date: 2000/09/11 15:48:49 $

   stat = 0;					   % Error flag
   nInputs = bint(5);
   nOutputs = bint(6);
   nStates = bint(7);
   %
   % Add mux and demux blocks if necessary
   %
   [post,rect] = blkio('port',nInputs,nOutputs,prop,path);
   %
   % Get the pseudo code and the parameter vector
   %
   indInt = bint(18);				   % Skip icon data
   if (bint(indInt+1) == 3)
      indInt = bint(21)-2;
   else
      indInt = indInt+bint(indInt+4)-2;
   end
   nParams = bint(indInt);			   % Number of parameters
   if (nParams)
      indInt = bint(18);
      indInt = bint(indInt+1)+indInt;		   % Skip icon data
      nParam3 = 3*nParams;
      dims = bint(indInt+(1:nParam3));
      ind = 1:3:nParam3;
      pType = dims(ind);
      rSize = dims(ind+1);
      cSize = dims(ind+2);
      nCoeffs = rSize.*cSize;
   end
   indInt = bint(15)+1;
   indStr = bint(17)+1;
   mode = bint(indInt);
   npar = nParams+1;
   symInd = zeros(1,npar);
   str = cell(npar,1);			   	   % Initial conditions included
   nnzic = bint(indInt+1);			   % Number of non-zero initial conditions
   if (bitand(mode,8))
      ind = mfindstr(bstr,indStr,'|');
      str{npar} = bstr(indStr:ind-1);
      indStr = ind+1;				   % Adjust string pointer
      symInd(npar) = 1;
   end
   if (bitand(mode,16))
      indVar = indInt+2*(nnzic+1);
      iRange = 1:bint(indVar);
      kRange = bint(indVar+iRange);
      kRange(find(kRange > 10)) = [];		   % Maximum 10 parameters allowed
      symInd(kRange) = 1;
      for (k = kRange)
	 ind = mfindstr(bstr,indStr,'|');
	 str{k} = bstr(indStr:ind-1);
	 indStr = ind+1;		   	   % Adjust string pointer
      end
   end
   %
   % Get the block parameter variables
   %
   [symInd,str] = mgetscope(symInd,str,catalog,ws,node);
   if (symInd(npar) == 0)
      indFlt = bint(16);
      x0 = zeros(nStates,1);
      if (~isempty(x0) & nnzic)
	 cRange = 1:nnzic;
	 indInt = indInt+nnzic+1;
	 x0(bint(indInt+cRange)) = bflt(indFlt+cRange);
      end
      str{npar} = mat2char(x0);
   end
   ind = mfindstr(bstr,bint(20)+3,'`%',bint(23)-bint(20));
   data = bstr(bint(20)+3:ind-1);
   ind = find(data == '`');
   CR = char(10);
   SP = char(32);
   if (~isempty(ind))
      data(ind) = CR;
   end
   pName = cell(nParams,1);
   indData = 1;
   indInt = bint(18);
   indInt = bint(indInt+1)+indInt;
   indFlt = bint(19);
   for (k = 1:nParams)
      ind = mfindstr(data,indData,'%');
      pName{k} = data(indData:ind-1);
      indData = ind+1;
      if (~symInd(k))
	 cRange = 1:nCoeffs(k);
	 coeffs = zeros(rSize(k),cSize(k));
	 if (~isempty(coeffs))
	    if (bitand(pType(k),3) == 3)
	       coeffs(:) = bflt(indFlt+cRange);
	    else
	       coeffs(:) = bint(indInt+cRange);
	    end
	 end
	 str{k} = mat2char(coeffs);
      end
      if (bitand(pType(k),3) == 3)
	 indFlt = indFlt+nCoeffs(k);
      else
	 indInt = indInt+nCoeffs(k);
      end
   end
   %
   % BLockScript block
   %
   if (nInputs == 0)
      bh = add_block('libsb2sl/USR/ZIBlockScript',path);
   else
      bh = add_block('libsb2sl/USR/BlockScript',path);
   end
   if (nParams == 0)
      bsargs = '';
      bspara = '';
   else
      vargs = strcat(str(1:nParams),',');
      bsargs = strcat(vargs{:});
      bsargs(end) = '';				   % Remove the last comma
      vargs = strcat(pName(:),',');
      bspara = strcat(vargs{:});
      bspara(end) = '';				   % Remove the last comma
   end
   %
   % Remove the white spaces from the block name
   %
   cname = name;
   ind = find((cname == SP) | (cname == CR));
   if (~isempty(ind))
      cname(ind) = '_';
   end
   ind = find((cname == '(') | (cname == ')'));
   if (~isempty(ind))
      cname(ind) = '';
   end
   if (cname(1) == '_')
      cname = cat(2,'unknown',cname);
   end
   %
   % Set additional block properties
   %
   set_param(bh,'Tag','BlockScript',...
		'ForegroundColor',prop.fgColor,...
		'BackgroundColor',prop.bgColor,...
		'Orientation',prop.orientation,...
		'DropShadow',prop.border,...
		'Position',rect,...
		'BSNAME',cname,...
		'SIZES',mat2char([nInputs nOutputs nStates]),...
		'BSPARA',bspara,...
		'BSARGS',bsargs,...
		'X0',str{npar});
   %
   % Blockscript to s-fun conversion
   %
   errFlag = bs2sf(cat(2,'-s ',cname),data(indData:end),cat(2,cname,'.c'));
   if (errFlag)
      blkname = path;
      ind = find(path == CR);
      if (~isempty(ind))
	 blkname(ind) = SP;
      end
      msg = {'Conversion error encountered while processing';
	     cat(2,blkname,' block.');};
      sbboxerror('write',msg);
   end
   %
   % Connect the current blockscript block with the mux and demux blocks
   %
   blkio('line',nInputs,nOutputs,post,parent,name);
   return					   % Last card of BLKSCRIPT
