function [line,inargstatus,outargstatus,argcell]= trfuncall(instring,builtinstatus)
%  

%   This software is protected by german copyright and international treaties.             
%   Copyright 2004 Markus Junghöfer & Peter Peyk. All Rights Reserved.                     
%                                                                                          
%   THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE        
%   NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,    
%   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE    
%   OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
%   TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT,       
%   INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE      
%   OR DOCUMENTATION.                                                                      

if nargin<2
    builtinstatus = 0;
end
argcell =cell(1); 
addoutargcell = cell(1);
nrofoutarg = 0;
nrofinarg = 0;
linedebl = deblank(instring);
inargstatus = 0;
outargstatus = 0;
nrofaddinargs = 0;
nrofaddoutargs = 0;

%functionname
if ~isempty(findstr(linedebl,'('))
    functionname = linedebl(findstr(linedebl,'=')+1:findstr(linedebl,'(')-1);
else
    functionname = linedebl(findstr(linedebl,'=')+1:end-1);
end





%argin
if ~isempty(findstr(linedebl,'('))
    inarg = linedebl(findstr(linedebl,'(')+1:max(findstr(linedebl,')'))-1);
    if mod(length(findstr(inarg,'''')),2)==0
        inarg = strrep(inarg,'''','"');
        if ~isempty(findstr(inarg,','))
            nrofinarg = length(findstr(inarg,','))+1;
            invar = cell(1);
            kommata = findstr(inarg,',');
            invar{1}=inarg(1:findstr(inarg,',')-1);
            for i=2:nrofinarg-1
                invar{i}=inarg(kommata(i-1)+1:kommata(i)-1);
            end
            invar{nrofinarg}=inarg(kommata(end)+1:end);
        else
            invar = inarg;
            nrofinarg = 1;
        end
    else
         if ~isempty(findstr(inarg,','))
            nrofinarg = length(findstr(inarg,','));
            invar = cell(1);
            kommata = findstr(inarg,',');
            invar{1}=inarg(1:findstr(inarg,',')-1);
            for i=2:nrofinarg
                invar{i}=inarg(kommata(i-1)+1:kommata(i)-1);
            end
            invar{nrofinarg+1}=inarg(kommata(end)+1:end);
        else
            invar = inarg;
            nrofinarg = 1;
        end
    end
else
    nrofinarg = 0;
    invar=[];
end
%argout
if ~isempty(findstr(linedebl,'='))
    outarg = strrep(strrep(linedebl(1:findstr(linedebl,'=')-1),'[',''),']','');
    if ~isempty(findstr(outarg,','))
        nrofoutarg = length(findstr(outarg,','))+1;
        kommata = findstr(outarg,',');
        outvar = outarg(1:kommata(1)-1);
        
        outinvar = cell(1);
        for i=2:nrofoutarg-1
            if isempty(findstr(outarg(kommata(i-1)+1:kommata(i)-1),char(13)))
                outinvar{i}=outarg(kommata(i-1)+1:kommata(i)-1) ;
            else
                 outinvar{i}=strrep(outarg(kommata(i-1)+1:kommata(i)-1),char(13),'') ;
                 outinvar{i} = [outinvar{i-1},char(10)];
             end
        end
        outinvar{nrofoutarg}=outarg(kommata(end)+1:end);
    else
        outvar = outarg;
        outvar(findstr(outvar,'['))=[];
        outvar(findstr(outvar,']'))=[];
        nrofoutarg = 1;
    end
end


%user m-files
%=====================
if ~builtinstatus
    try
		nrdecloutargs = nargout(functionname);
		nrdeclinargs = nargin(functionname);
    catch
        [line,inargstatus,outargstatus,argcell]= trfuncall(instring,1);
        return
    end
	
	if nrofoutarg~=nrdecloutargs
        
        if nrdecloutargs>nrofoutarg
		outargstatus = 1;
            nrofaddoutargs = nrdecloutargs-nrofoutarg;
            for e=1:nrofaddoutargs
                addoutargcell{e}=['nouse',num2str(e)];
            end
        end
	end
	
	if nrofinarg~=nrdeclinargs
        if nrdeclinargs>nrofinarg
        inargstatus = 1;
            nrofaddinargs = nrdeclinargs-nrofinarg;
            for e=1:nrofaddinargs
                addinargcell{e}=['empty()'];
            end
        end
	end
	
                
            
	%build function call
   
		if nrofoutarg       
            line = [outvar,' ='];
		end
		line = [line functionname];
		if nrofoutarg>1 | nrofaddoutargs>0
            if nrofoutarg>1
                line = [line '(&' outinvar{2}];
                for r=2:nrofoutarg-1
                    line = [line ', &' outinvar{r+1}];
                end
            end
            if  nrofaddoutargs>0
                if nrofoutarg>1
                    line = [line ', &' addoutargcell{1}];
                else
                    line = [line '(&' addoutargcell{1}];
                end
                
                for r=2:nrofaddoutargs
                    line = [line ', &' addoutargcell{r}];
                end
            end
		else
            if nrofinarg | nrofaddoutargs 
                line = [line '('];
            end
		end  
		
		
		if nrofinarg
            if nrofoutarg>1 | nrofaddoutargs>0
                for r=1:nrofinarg
                    if iscell(invar)
                        line = [line ', ' invar{r}];
                    else
                        line = [line ', ' invar];
                    end
                end
                
                for r=1:nrofaddinargs
                    line = [line ',' addinargcell{r}];
                end
                
                line = [line ')'];
            else
                if iscell(invar)
                   line = [line ' ' invar{1}];
                else
                   line = [line ' ' invar];
                end
                 for r=2:nrofinarg
                    if iscell(invar)
                        line = [line ', ' invar{r}];
                    else
                        line = [line ', ' invar];
                    end
                end
                
                for r=1:nrofaddinargs
                    line = [line ', ' addinargcell{r}];
                end
                line = [line ')'];
            end
        end

%built-in functions
%=====================
else

 	           
    %size
	%==============
	if strcmp(deblankex(functionname),'size') &  length(findstr(instring,'['))==1 & length(findstr(instring,']'))==1
        if nrofoutarg       
            indent = getlineindent(instring);
            line = [indent,'size(mwVarargout(',outvar,','];
		end
		if nrofoutarg>1
            line = [line,outinvar{2},'),'];
        end
		
		
		if nrofinarg
            if iscell(invar)
               line = [line, invar{1}];
            else
               line = [line , invar];
            end
            
            line = [line ,')'];
		end
    else      
		%build function call
		if nrofoutarg       
            line = [outvar,' ='];
		end
		line = [line functionname];
		if nrofoutarg>1
            line = [line '(&' outinvar{2}];
            for r=2:nrofoutarg-1
                line = [line ',&' outinvar{r+1}];
            end
		else
            if nrofinarg | nrofaddoutargs | nrofaddinargs
                line = [line '('];
            end
        end  
		
		if nrofinarg
            if nrofoutarg>1 
                for r=1:nrofinarg
                    if iscell(invar)
                        line = [line ', ' invar{r}];
                    else
                        line = [line ', ' invar];
                    end
                end
                
                
                line = [line ')'];
            else
                if iscell(invar)
                   line = [line ' ' invar{1}];
                else
                   line = [line ' ' invar];
                end
                 for r=2:nrofinarg
                    if iscell(invar)
                        line = [line ', ' invar{r}];
                    else
                        line = [line ', ' invar];
                    end
                end
                
                line = [line ')'];
            end
		end
    end
end
line = [line ';',char(10)];





 
    
return
    
