function sbhndlgrep(cmd)
%SBHNDLGREP
%
%  See also

% Initial coding by A. S. Bozin, 05-08-98.
% Copyright 1997-2000 The MathWorks, Inc.
% $Revision: 1.12 $ $Date: 2000/06/17 17:14:38 $

   %
   % Action depends on CMD
   %
   switch (cmd)
   case 'print'
      %
      % Generate report
      %
      shh = get(0,'ShowHiddenHandles');
      set(0,'ShowHiddenHandles','on');
      kids = get(0,'Children');
      set(0,'ShowHiddenHandles',shh);
      hFig = findobj(kids,'flat','Tag','sb2slTag');
      ud = get(hFig,'UserData');
      state = ud.state;			   	   % Current state
      if (bitand(state,4))
	 sgmName = cat(2,ud.sbName,'.sgml');
	 repopt = ud.repopt;	  	   	   % Report generation options
	 stat = 0;
	 if (bitand(state,32))
	    sbhndlmsgs('write',sprintf('Using existing %s file.',sgmName));
	 else
	    sbhndlmsgs('write',sprintf('Generating %s file ... ', sgmName),'append');
	    [fid,msg] = fopen(sgmName,'wt');
	    if (fid == -1)
	       stat = -1;
	    else
	       catalog = get(ud.catalog,'UserData');
	       if (repopt{4} | repopt{5})
		  kids = find_system('SearchDepth',0);
		  indsys = find(strcmp(kids,ud.slName));
		  if (isempty(indsys) | (get_param(kids{indsys},'Handle') ~= ud.system))
		     msg = {'Converted model has been removed from';...
			    'the memory or. Convert the model and try again.'};
		     if (repopt{4} & repopt{5})
		     elseif (repopt{4})
		     else
		     end
		     repopt{4} = 0;
		     repopt{5} = 0;
		     sbboxerror('write',msg);
		  end
	       end
	       %
	       % Markup requested chapters
	       %
	       task = 0;
	       while (stat == 0)
		  switch (task)
		  case 0			   % Prolog
		     stat = coprolog(fid,ud.sbName,ud.slName,ud.versions);
		  case {1,2,3,4,5}
		     if (repopt{task})
			switch (task)
			case 1
			   stat = cocatalog(fid,ud.node,catalog);
			case 2
			   stat = codetails(fid,ud.node,catalog);
			case 3
			   stat = coworkspace(fid,bitand(state,2),ud.pind,ud.wsList.names,...
					      ud.wsList.pvars,ud.workSpace);
			case 4
			   sBlocks = find_system(ud.system,'Tag','BlockScript');
			   stat = coblkscripts(fid,sBlocks);
			case 5
			   uBlocks = find_system(ud.system,'Tag','Unknown');
			   stat = counsupported(fid,uBlocks);
			end
		     end
		  otherwise		   	   % Trailer
		     stat = cotrailer(fid);
		  end
		  task = task+1;		   % Next task
	       end
	       if (stat == -1)
	       end
	       if (fclose(fid) == -1)
		  stat = -1;
	       end
	    end
	    if (stat == -1)
	       sbhndlmsgs('write','Failed.','newline');
	    else
	       sbhndlmsgs('write','Done.','newline');
	       ud.state = bitor(state,32);
	       set(hFig,'UserData',ud);
	       stat = 0;
	    end
	 end
	 if (stat == 0)
	    conopt = ud.conopt;	 	   	   % Conversion options
	    %
	    % Find out where is the jade
	    %
	    arch = computer;
	    dirsep = filesep;
	    [jexe,os] = jade(arch,dirsep);
	    if (isempty(jexe))
		  
	    else
	       docbook = cat(2,matlabroot,dirsep,'sys',dirsep,'jade',dirsep,'docbook',dirsep);
	       catalogs = {cat(2,docbook,'catalog');
			   cat(2,docbook,'dtd',dirsep,'docbook.cat')};
	       switch (conopt{1})
	       case 1			   	   % HTML
		  backend = 'sgml';
		  dsssl = cat(2,docbook,'html',dirsep,'docbook.dsl');
		  if (conopt{2} == 1)
		     rptName = cat(2,ud.sbName,'.html');
		  else
		     rptName = 'book1.htm';
		  end
	       case 2			   	   % RTF-95
		  backend = 'rtf-95';
		  dsssl = cat(2,docbook,'print',dirsep,'docbook.dsl');
		  rptName = cat(2,ud.sbName,'.rtf');
	       case 3			   	   % RTF-97
		  backend = 'rtf';
		  dsssl = cat(2,docbook,'print',dirsep,'docbook.dsl');
		  rptName = cat(2,ud.sbName,'.rtf');
	       case 4			   	   % TEX
		  backend = 'tex';
		  dsssl = cat(2,docbook,'print',dirsep,'docbook.dsl');
		  rptName = cat(2,ud.sbName,'.tex');
	       otherwise
	       end
	       stylesheet = 'driver.dsl';
	       sbhndlmsgs('write','Creating stylesheet ... ','append');
	       stat = cdriver(stylesheet,backend,dsssl,conopt{2});
	       if (stat == 0)
		  sbhndlmsgs('write','Done.','newline');
		  sbhndlmsgs('write',sprintf('Generating %s file ... ', rptName),'append');
		  if (strcmp(backend,'sgml'))
		     if (conopt{2} == 1)
			if (exist(rptName,'file') == 2)
			   delete(rptName);
			end
			cmd = sprintf('%s -c %s -c %s -t %s -d %s %s > %s',jexe,...
				      catalogs{:},backend,stylesheet,sgmName,rptName);
		     else
			cmd = sprintf('%s -c %s -c %s -t %s -d %s %s',jexe,...
				      catalogs{:},backend,stylesheet,sgmName);
		     end
		  else
		     cmd = sprintf('%s -c %s -c %s -t %s -d %s -o %s %s',jexe,...
				   catalogs{:},backend,stylesheet,rptName,sgmName);
		  end
		  switch (os)
		  case 'nt'
		     [stat,msg] = dos(cmd);
		  case 'unix'
		     [stat,msg] = unix(cmd);
		  otherwise
		     stat = -1;
		  end
		  if (stat == 0)
		     sbhndlmsgs('write','Done.','newline');
		     if (conopt{3})
			switch (backend)
			case 'sgml'
			   stat = web(cat(2,'file:///',pwd,dirsep,rptName));
			otherwise
			end
		     end
		  else
		     sbhndlmsgs('write','Done.','newline');
		  end
		  delete(stylesheet);
	       else
		  sbhndlmsgs('write','Failed.','newline');
	       end
	    end
	 end
      else
	 msg = {'Model not converted.';
		'First convert the model and then try again.'};
	 sbboxerror('write',msg);
      end
   case 'compile'
      % 
      % Compilation message
      % 
      sbhndlmsgs('write','.','append');	   % Show progress
   otherwise
      error('sb2sl: sbhndlgrep called with incorrect argument.');
   end 
   return					   % Last card of SBHNDLGREP

function stat = coprolog(fid,sbname,slname,versions)
%PROLOG
%
%

   stat = 0;
   %
   % Document prolog
   %
   fprintf(fid,'<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V4.0//EN">\n');
   fprintf(fid,'<BOOK>\n');			   % Start of book
   fprintf(fid,'<TITLE>Simulink Model %s</TITLE>\n',upper(slname));
   fprintf(fid,'<BOOKINFO>\n');
   fprintf(fid,'<TITLE>Based on SystemBuild Model %s</TITLE>\n',upper(sbname));
   fprintf(fid,'<PUBDATE>%s</PUBDATE>\n',date);
   fprintf(fid,'</BOOKINFO>\n');
   fprintf(fid,'<TOC></TOC>\n');
   %
   fprintf(fid,'<PREFACE>\n');
   fprintf(fid,'<TITLE>About This Document</TITLE>\n');
   fprintf(fid,'<PARA>\n');
   fprintf(fid,'This document describes the Simulink model <LITERAL>%s</LITERAL>.\n',slname);
   fprintf(fid,'Model has been translated from the System Build model <LITERAL>%s</LITERAL>.\n',sbname);
   fprintf(fid,'</PARA>\n');
   fprintf(fid,'<ITEMIZEDLIST MARK="bullet" SPACING="normal">\n');
   fprintf(fid,'  <Title>Version information</Title>\n');
   fprintf(fid,'  <LISTITEM><PARA>MATRIXx Ver. %.2f</PARA></LISTITEM>\n',versions(1));
   fprintf(fid,'  <LISTITEM><PARA>System Build Ver. %.2f</PARA></LISTITEM>\n',versions(2));
   fprintf(fid,'</ITEMIZEDLIST>\n');
   fprintf(fid,'</PREFACE>\n');
   return					   % Last card of PROLOG

function stat = cotrailer(fid)
%TRAILER
%
%

   %
   % Document trailer
   %
   fprintf(fid,'</BOOK>\n');			   % End of book
   stat = 1;
   return					   % Last card of TRAILER

function stat = cocatalog(fid,node,catalog)
%CCATALOG
%
%

   stat = 0;
   %
   % Chapter
   %
   xref = 'cat-table';
   fprintf(fid,'<CHAPTER>\n');			   % Start of the chapter
   fprintf(fid,'<TITLE>Catalog of SuperBlocks</TITLE>\n');
   fprintf(fid,'<PARA>\n');
   fprintf(fid,'Selected top level superblock <LITERAL>%s</LITERAL>.\n',catalog(node).name);
   fprintf(fid,'</PARA>\n');
   fprintf(fid,'<PARA>\n');
   fprintf(fid,'<XREF LINKEND="%s"> gives a list of converted superblocks as well as their types.\n',xref);
   fprintf(fid,'</PARA>\n');
   %
   % Find a list of all included superblocks
   %
   sbind = zeros(1,size(catalog,1));
   sbind(node) = 1;
   ind = find(allkids(cat(1,catalog,catalog(end)),node,sbind));
   fprintf(fid,'<PARA>\n');			   % Table
   fprintf(fid,'<TABLE FRAME="all" ID="%s">\n',xref);
   fprintf(fid,'  <TITLE>Detailed Catalog of SuperBlocks</TITLE>\n');
   fprintf(fid,'  <TGROUP COLS="3" COLSEP="1" ROWSEP="1">\n');
   fprintf(fid,'  <COLSPEC COLNAME="col1" COLWIDTH="160pt">\n');
   fprintf(fid,'  <COLSPEC COLNAME="col2" COLWIDTH="230pt">\n');
   fprintf(fid,'  <COLSPEC COLNAME="col3" COLWIDTH="60pt">\n');
   fprintf(fid,'  <THEAD>\n  <ROW>\n');
   fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">SuperBlock Name</ENTRY>\n');
   fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">Type</ENTRY>\n');
   fprintf(fid,'  <ENTRY ALIGN="right" VALIGN="bottom">Date</ENTRY>\n');
   fprintf(fid,'  </ROW>\n  </THEAD>\n');
   fprintf(fid,'  <TBODY>\n');
   for (k = ind)
      fprintf(fid,'  <ROW ROWSEP="1">\n');
      fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom"><LITERAL>%s</LITERAL></ENTRY>\n',...
	      catalog(k).name);
      fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">%s</ENTRY>\n',...
	      gettype(catalog(k).type,catalog(k).signal,catalog(k).sampleTime));
      fprintf(fid,'  <ENTRY ALIGN="right" VALIGN="bottom">%s</ENTRY>\n',...
	      getdate(catalog(k).date));
      fprintf(fid,'  </ROW>\n');
   end
   fprintf(fid,'  </TBODY>\n');
   fprintf(fid,'  </TGROUP>\n');
   fprintf(fid,'</TABLE>\n');			   % End of table environment
   fprintf(fid,'</PARA>\n');
   fprintf(fid,'</CHAPTER>\n');			   % End of chapter
   return					   % Last card of CCATALOG

function stat = codetails(fid,node,catalog)
%CDETAILS
%
%

   stat = 0;
   %
   % Chapter
   %
   sect1ref = 'list-section';
   sect2ref = 'modh-section';
   fprintf(fid,'<CHAPTER>\n');			   % Start of the chapter
   fprintf(fid,'<TITLE>Detailed SuperBlock Information</TITLE>\n');
   fprintf(fid,'<PARA>\n');
   fprintf(fid,'A more detailed description of the converted superblocks is given in this chapter.\n');
   fprintf(fid,'<ITEMIZEDLIST MARK="bullet" SPACING="normal">\n');
   fprintf(fid,'<LISTITEM><PARA>\n');
   fprintf(fid,['  The section called <XREF LINKEND="%s"> describes the input-output properties of' ...
		' the converted superblocks.\n'],sect1ref); 
   fprintf(fid,'</PARA></LISTITEM>\n');
   fprintf(fid,'<LISTITEM><PARA>\n');
   fprintf(fid,['  Hierarchical structure of the model is given in the section called <XREF LINKEND="' ...
		' %s"> using a tree-like format.\n'],sect2ref); 
   fprintf(fid,'</PARA></LISTITEM>\n');
   fprintf(fid,'</ITEMIZEDLIST>\n');
   fprintf(fid,'</PARA>\n');
   %
   % Section
   %
   xref = 'det-table';
   sect1title = 'List of Superblocks';	   	   % Start of section
   fprintf(fid,'<SECT1 ID="%s" XREFLABEL="%s">\n',sect1ref,sect1title);
   fprintf(fid,'<TITLE>%s</TITLE>\n',sect1title);
   fprintf(fid,'<PARA>\n');
   fprintf(fid,['<XREF LINKEND="%s"> gives a list of converted superblocks together with the' ...
		' associated partitions and the number of inputs, outputs and blocks.\n'],xref); 
   fprintf(fid,'</PARA>\n');
   %
   % Detailed list of supurblocks
   %
   sbind = zeros(1,size(catalog,1));
   sbind(node) = 1;
   sbind(:) = allkids(cat(1,catalog,catalog(end)),node,sbind);
   ind = find(sbind);
   fprintf(fid,'<PARA>\n');			   % Start of table environment
   fprintf(fid,'<TABLE FRAME="all" ID="%s">\n',xref);
   fprintf(fid,'  <TITLE>Catalog of SuperBlocks</TITLE>\n');
   fprintf(fid,'  <TGROUP COLS="5" COLSEP="1" ROWSEP="1">\n');
   fprintf(fid,'  <COLSPEC COLNAME="col1" COLWIDTH="160pt">\n');
   fprintf(fid,'  <COLSPEC COLNAME="col2" COLWIDTH="160pt">\n');
   fprintf(fid,'  <COLSPEC COLNAME="col3" COLWIDTH="50pt">\n');
   fprintf(fid,'  <COLSPEC COLNAME="col4" COLWIDTH="50pt">\n');
   fprintf(fid,'  <COLSPEC COLNAME="col5" COLWIDTH="50pt">\n');
   fprintf(fid,'  <THEAD>\n  <ROW>\n');
   fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">SuperBlock Name</ENTRY>\n');
   fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">Partition</ENTRY>\n');
   fprintf(fid,'  <ENTRY ALIGN="right" VALIGN="bottom">Inputs</ENTRY>\n');
   fprintf(fid,'  <ENTRY ALIGN="right" VALIGN="bottom">Outputs</ENTRY>\n');
   fprintf(fid,'  <ENTRY ALIGN="right" VALIGN="bottom">Blocks</ENTRY>\n');
   fprintf(fid,'  </ROW>\n  </THEAD>\n');
   fprintf(fid,'  <TBODY>\n');
   for (k = ind)
      fprintf(fid,'  <ROW ROWSEP="1">\n');
      fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom"><LITERAL>%s</LITERAL></ENTRY>\n',...
	      catalog(k).name);
      partition = catalog(k).partition;
      if (isempty(partition))
	 partition = '-';
      end
      fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom"><LITERAL>%s</LITERAL></ENTRY>\n',...
	      partition);
      fprintf(fid,'  <ENTRY ALIGN="right" VALIGN="bottom">%d</ENTRY>\n',catalog(k).nInputs);
      fprintf(fid,'  <ENTRY ALIGN="right" VALIGN="bottom">%d</ENTRY>\n',catalog(k).nOutputs);
      fprintf(fid,'  <ENTRY ALIGN="right" VALIGN="bottom">%d</ENTRY>\n',catalog(k).nBlocks);
      fprintf(fid,'  </ROW>\n');
   end
   fprintf(fid,'  </TBODY>\n');
   fprintf(fid,'  </TGROUP>\n');
   fprintf(fid,'</TABLE>\n');			   % End of table environment
   fprintf(fid,'</PARA>\n');
   fprintf(fid,'</SECT1>\n');			   % End of section
   %
   % Section
   %
   xref = 'inh-table';
   sect2title = 'Model Hierarchy';	   	   % Start of section
   fprintf(fid,'<SECT1 ID="%s" XREFLABEL="%s">\n',sect2ref,sect2title);
   fprintf(fid,'<TITLE>%s</TITLE>\n',sect2title);
   fprintf(fid,'<PARA>\n');
   fprintf(fid,'<XREF LINKEND="%s"> lists the superblocks together with their parents and children.\n',xref);
   fprintf(fid,'</PARA>\n');
   fprintf(fid,'<PARA>\n');			   % Start of table environment
   fprintf(fid,'<TABLE FRAME="all" ID="%s">\n',xref);
   fprintf(fid,'  <TITLE>Model Hierarchy</TITLE>\n');
   fprintf(fid,'  <TGROUP COLS="3" COLSEP="1" ROWSEP="1">\n');
   fprintf(fid,'  <COLSPEC COLNAME="col1" COLWIDTH="160pt">\n');
   fprintf(fid,'  <COLSPEC COLNAME="col2" COLWIDTH="160pt">\n');
   fprintf(fid,'  <COLSPEC COLNAME="col3" COLWIDTH="160pt">\n');
   fprintf(fid,'  <THEAD>\n  <ROW>\n');
   fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">SuperBlock Name</ENTRY>\n');
   fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">Parents</ENTRY>\n');
   fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">Children</ENTRY>\n');
   fprintf(fid,'  </ROW>\n  </THEAD>\n');
   fprintf(fid,'  <TBODY>\n');
   for (k = ind)
      fprintf(fid,'  <ROW ROWSEP="1">\n');
      fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="middle">\n');
      fprintf(fid,'  <SIMPLELIST TYPE="vert" COLUMNS="1">\n');
      fprintf(fid,'    <MEMBER><LITERAL>%s</LITERAL></MEMBER>\n',catalog(k).name);
      fprintf(fid,'  </SIMPLELIST>\n');
      fprintf(fid,'  </ENTRY>\n');
      if (k == node)
	 parent = [];
      else
	 bkind = zeros(size(sbind));
	 bkind(catalog(k).parent) = 1;
	 parent = find(bkind & sbind);
      end
      if (isempty(parent))
	 fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="middle">-</ENTRY>\n');
      else
	 fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">\n');
	 fprintf(fid,'  <SIMPLELIST TYPE="vert" COLUMNS="1">\n');
	 for (l = parent)
	    fprintf(fid,'    <MEMBER><LITERAL>%s</LITERAL></MEMBER>\n',catalog(l).name);
	 end
	 fprintf(fid,'  </SIMPLELIST>\n');
	 fprintf(fid,'  </ENTRY>\n');
      end
      bkind = zeros(size(sbind));
      if (isempty(catalog(k).bkChild))
	 bkind(catalog(k).sbChild) = 1;
      else
	 bkind(cat(2,catalog(k).sbChild,catalog(k).bkChild{:})) = 1;
      end
      kids = find(bkind);
      if (isempty(kids))
	 fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="middle">-</ENTRY>\n');
      else
	 fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">\n');
	 fprintf(fid,'  <SIMPLELIST TYPE="vert" COLUMNS="1">\n');
	 for (l = kids)
	    fprintf(fid,'    <MEMBER><LITERAL>%s</LITERAL></MEMBER>\n',catalog(l).name);
	 end
	 fprintf(fid,'  </SIMPLELIST>\n');
	 fprintf(fid,'  </ENTRY>\n');
      end
      fprintf(fid,'  </ROW>\n');
   end
   fprintf(fid,'  </TBODY>\n');
   fprintf(fid,'  </TGROUP>\n');
   fprintf(fid,'</TABLE>\n');			   % End of table environment
   fprintf(fid,'</PARA>\n');
   %
   %
   %
   fprintf(fid,'<PARA>\n');
   fprintf(fid,'The following depth indented listing illustrates the hierarchical structure of the converted model.');
   fprintf(fid,'<PROGRAMLISTING>\n');
   tree = struct('children',[],'name','');
   tree = buildtree(catalog,node,tree,1);
   stat = treeprint(fid,tree,1,' ',0);
   fprintf(fid,'</PROGRAMLISTING>\n');
   fprintf(fid,'</PARA>\n');
   fprintf(fid,'</SECT1>\n');
   fprintf(fid,'</CHAPTER>\n');			   % End of chapter
   return					   % Last card of CDETAILS

function stat = coworkspace(fid,populated,pind,names,pvars,ws)
%CWORKSPACE
%
%

   stat = 0;
   %
   % Chapter
   %
   fprintf(fid,'<CHAPTER>\n');			   % Start of the chapter
   fprintf(fid,'<TITLE>Partitions and Parameters</TITLE>\n');
   fprintf(fid,'<PARA>\n');
   fprintf(fid,'Selected top level partition <LITERAL>%s</LITERAL>.\n',names{pind});
   fprintf(fid,'</PARA>\n');
   if (populated)
      fprintf(fid,'<PARA>\n');
      npart = size(names,1);
      if (npart == 1)
	 plural = '';
      else
	 plural = 's';
      end
      xref = 'par-table';
      fprintf(fid,'Model contains %d partition%s.\n',npart,plural);
      fprintf(fid,'Partition%s and the associated parameters are given in <XREF LINKEND="%s">.\n',...
	      plural,xref);
      fprintf(fid,'</PARA>\n');
      %
      %
      %
      fprintf(fid,'<PARA>\n');			   % Start of table environment
      fprintf(fid,'<TABLE FRAME="all" ID="%s">\n',xref);
      fprintf(fid,'  <TITLE>List of Partitions and Variables</TITLE>\n');
      fprintf(fid,'  <TGROUP COLS="3" COLSEP="1" ROWSEP="1">\n');
      fprintf(fid,'  <COLSPEC COLNAME="col1" COLWIDTH="160pt">\n');
      fprintf(fid,'  <COLSPEC COLNAME="col2" COLWIDTH="160pt">\n');
      fprintf(fid,'  <COLSPEC COLNAME="col3" COLWIDTH="70pt">\n');
      fprintf(fid,'  <THEAD>\n  <ROW>\n');
      fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">Partition</ENTRY>\n');
      fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">Variable</ENTRY>\n');
      fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">Dimension</ENTRY>\n');
      fprintf(fid,'  </ROW>\n  </THEAD>\n');
      fprintf(fid,'  <TBODY>\n');
      npart = size(names,1);
      for (k = 1:npart)
	 partition = getfield(ws,names{k});
	 fprintf(fid,'  <ROW ROWSEP="1">\n');
	 fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="top">\n');
	 fprintf(fid,'  <SIMPLELIST TYPE="vert" COLUMNS="1">\n');
	 fprintf(fid,'    <MEMBER><LITERAL>%s</LITERAL></MEMBER>\n',names{k});
	 fprintf(fid,'  </SIMPLELIST>\n');
	 fprintf(fid,'  </ENTRY>\n');
	 fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">\n');
	 vars = pvars{k};
	 nvars = size(vars,1);
	 fprintf(fid,'  <SIMPLELIST TYPE="vert" COLUMNS="1">\n');
	 for (l = 1:nvars)
	    fprintf(fid,'    <MEMBER><LITERAL>%s</LITERAL></MEMBER>\n',vars{l});
	 end
	 fprintf(fid,'  </SIMPLELIST>\n');
	 fprintf(fid,'  </ENTRY>\n');
	 fprintf(fid,'  <ENTRY ALIGN="char" CHAROFF="50" CHAR="x" VALIGN="bottom">\n');
	 fprintf(fid,'  <SIMPLELIST TYPE="vert" COLUMNS="1">\n');
	 for (l = 1:nvars)
	    [m,n] = size(getfield(partition,vars{l}));
	    fprintf(fid,'    <MEMBER><LITERAL>%d x %d</LITERAL></MEMBER>\n',m,n);
	 end
	 fprintf(fid,'  </SIMPLELIST>\n');
	 fprintf(fid,'  </ENTRY>\n');
	 fprintf(fid,'  </ROW>\n');
      end
      fprintf(fid,'  </TBODY>\n  </TGROUP>\n');
      fprintf(fid,'</TABLE>\n');			   % End of table environment
      fprintf(fid,'</PARA>\n');
   else
      fprintf(fid,'<PARA>\n');
      fprintf(fid,'Model does not contain parameters.\n');
      fprintf(fid,'</PARA>\n');
   end
   fprintf(fid,'</CHAPTER>\n');			   % End of chapter
   return					   % Last card of CWORKSPACE

function stat = coblkscripts(fid,hblks)
%CBLKSCRIPTS
%
%

   stat = 0;
   %
   % Chapter
   %
   fprintf(fid,'<CHAPTER>\n');			   % Start of the chapter
   fprintf(fid,'<TITLE>List of BlockScript Blocks</TITLE>\n');
   if (isempty(hblks))
      fprintf(fid,'<PARA>\n');
      fprintf(fid,'Model does not contain BlockScript blocks.\n');
      fprintf(fid,'</PARA>\n');
   else
      fprintf(fid,'<PARA>\n');
      fprintf(fid,'Model contains the following BlockScript blocks:\n');
      fprintf(fid,'</PARA>\n');
      %
      %
      %
      xref = 'bsc-table';
      fprintf(fid,'<PARA>\n');
      fprintf(fid,'<XREF LINKEND="%s">.\n',xref);
      fprintf(fid,'</PARA>\n');
      fprintf(fid,'<PARA>\n');			   % Start of table environment
      fprintf(fid,'<TABLE FRAME="all" ID="%s">\n',xref);
      fprintf(fid,'  <TITLE>List of BlockScript Blocks</TITLE>\n');
      fprintf(fid,'  <TGROUP COLS="2" COLSEP="1" ROWSEP="1">\n');
      fprintf(fid,'  <COLSPEC COLNAME="col1" COLWIDTH="110pt*">\n');
      fprintf(fid,'  <COLSPEC COLNAME="col2" COLWIDTH="260pt">\n');
      fprintf(fid,'  <THEAD>\n  <ROW>\n');
      fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">File Name</ENTRY>\n');
      fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">Full Block Path Name</ENTRY>\n');
      fprintf(fid,'  </ROW>\n  </THEAD>\n');
      fprintf(fid,'  <TBODY>\n');
      blkName = cellstr(get_param(hblks,'Name'));
      blkPath = cellstr(get_param(hblks,'Parent'));
      blkFull = strcat(blkPath,{'/'},blkName);
      fileName = cellstr(get_param(hblks,'BSNAME'));
      LF = char(10);
      SP = char(32);
      nblk = size(fileName,1);
      for (k = 1:nblk)
	 fprintf(fid,'  <ROW ROWSEP="1">\n');
	 fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom"><LITERAL>%s.c</LITERAL></ENTRY>\n',...
		 fileName{k});
	 fblkpath = blkFull{k};
	 ind = find(fblkpath == LF);
	 fblkpath(ind) = SP;
	 fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom"><LITERAL>%s</LITERAL></ENTRY>\n',...
		 fblkpath);
	 fprintf(fid,'  </ROW>\n');
      end
      fprintf(fid,'  </TBODY>\n');
      fprintf(fid,'  </TGROUP>\n');
      fprintf(fid,'</TABLE>\n');			   % End of table environment
      fprintf(fid,'</PARA>\n');
   end
   fprintf(fid,'</CHAPTER>\n');			   % End of chapter
   return					   % Last card of CBLKSCRIPTS

function stat = counsupported(fid,hblks)
%CUNSUPPORTED
%
%

   stat = 0;
   %
   % Chapter
   %
   fprintf(fid,'<CHAPTER>\n');			   % Start of the chapter
   fprintf(fid,'<TITLE>List of Missing Blocks</TITLE>\n');
   if (isempty(hblks))
      fprintf(fid,'<PARA>\n');
      fprintf(fid,'SB2SL translator has converted all blocks.\n');
      fprintf(fid,'</PARA>\n');
   else
      fprintf(fid,'<PARA>\n');
      nblk = size(hblks,1);
      if (nblk == 1)
	 plural = '';
      else
	 plural = 's';
      end
      xref = 'unc-table';
      fprintf(fid,'SB2SL translator could not convert %d block%s.\n',nblk,plural);
      fprintf(fid,'For more details see <XREF LINKEND="%s">.\n',xref);
      fprintf(fid,'</PARA>\n');
      %
      %
      %
      fprintf(fid,'<PARA>\n');			   % Start of table environment
      fprintf(fid,'<TABLE FRAME="all" ID="%s">\n',xref);
      fprintf(fid,'  <TITLE>List of Missing Blocks</TITLE>\n');
      fprintf(fid,'  <TGROUP COLS="2" COLSEP="1" ROWSEP="1">\n');
      fprintf(fid,'  <COLSPEC COLNAME="col1" COLWIDTH="120pt">\n');
      fprintf(fid,'  <COLSPEC COLNAME="col2" COLWIDTH="260pt">\n');
      fprintf(fid,'  <THEAD>\n  <ROW>\n');
      fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">Block Type</ENTRY>\n');
      fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">Full Block Path Name</ENTRY>\n');
      fprintf(fid,'  </ROW>\n  </THEAD>\n');
      fprintf(fid,'  <TBODY>\n');
      blkName = cellstr(get_param(hblks,'Name'));
      blkPath = cellstr(get_param(hblks,'Parent'));
      blkFull = strcat(blkPath,{'/'},blkName);
      blkType = cellstr(get_param(hblks,'MaskType'));
      LF = char(10);
      SP = char(32);
      nblk = size(blkType,1);
      for (k = 1:nblk)
	 fprintf(fid,'  <ROW ROWSEP="1">\n');
	 fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom">%s</ENTRY>\n',blkType{k});
	 fblkpath = blkFull{k};
	 ind = find(fblkpath == LF);
	 fblkpath(ind) = SP;
	 fprintf(fid,'  <ENTRY ALIGN="left" VALIGN="bottom"><LITERAL>%s</LITERAL></ENTRY>\n',...
		 fblkpath);
	 fprintf(fid,'  </ROW>\n');
      end
      fprintf(fid,'  </TBODY>\n');
      fprintf(fid,'  </TGROUP>\n');
      fprintf(fid,'</TABLE>\n');			   % End of table environment
      fprintf(fid,'</PARA>\n');
   end
   fprintf(fid,'</CHAPTER>\n');			   % End of chapter
   return					   % Last card of CUNSUPPORTED

function ind = allkids(catalog,node,ind)
%ALLKIDS Returns a list of all children.
%
%
   kids = catalog(node).sbChild;
   if (~isempty(kids))
      todo = find(ind(kids) == 0);
      ind(kids) = 1;
      for (k = kids(todo))
	 ind = allkids(catalog,k,ind);
      end
   end
   parents = catalog(node).bkChild;
   if (~isempty(parents))
      sibling = catalog(node).sibling;
      npr = size(parents,2);
      for (pr = 1:npr)
	 kids = parents{pr};
	 if (sibling(pr))
	    catalog(end).sbChild = kids;
	    ind = allkids(catalog,size(catalog,1),ind);
	 else
	    todo = find(ind(kids) == 0);
	    ind(kids) = 1;
	    for (k = kids(todo))
	       ind = allkids(catalog,k,ind);
	    end
	 end
      end
   end
   return					   % Last card of ALLKIDS

function tree = buildtree(catalog,node,tree,root)
%BUILDTREE Build a hirarchical model structure.
%
%

   tree(root).name = catalog(node).name;
   %
   % Get siblings
   %
   if (isempty(catalog(node).bkChild))
      siblings = catalog(node).sbChild;
   else
      siblings = cat(2,catalog(node).sbChild,catalog(node).bkChild{:});
   end
   if (~isempty(siblings))
      %
      % Remove duplicated superblocks
      %
      sbind = zeros(1,size(catalog,1));
      sbind(siblings) = 1;
      siblings = find(sbind);
      last = size(tree,1);
      nsbl = size(siblings,2);
      ind = 1:nsbl;
      kids = last+ind;
      tree(root).children = kids;
      tree(kids,1) = struct('children',cell(nsbl,1),'name','');
      for (k = ind)
	 tree = buildtree(catalog,siblings(k),tree,kids(k));
      end
   end
   return					   % Last card of BUILDTREE

function stat = treeprint(fid,tree,node,indent,last)
%TREEPRINT Print out the model hierarchy.
%
%

   stat = 0;
   %
   %
   %
   level = size(indent,2)-1;
   strlen = 4*level;
   if (level)
      str = cat(2,blanks(strlen),' +--');
      str(2:4:strlen) = indent(1:level);
   else
      str = ' +--';
   end
   if (last)
      str(strlen+2) = '`';
   end
   fprintf(fid,'%s%s\n',str,tree(node).name);
   kids = tree(node).children;
   if (~isempty(kids))
      kend = kids(end);
      indent(1,end+1) = '|';
      for (k = kids)
	 islast = k == kend;
	 if (islast)
	    indent(end) = ' ';
	 end
	 stat = treeprint(fid,tree,k,indent,islast);
      end
   end
   return					   % Last card of TREEPRINT

function sbtype = gettype(code,signal,h)
%GETTYPE Map the superblock number into the string.
%
%

   codes = [0;1;33;73;129;257;513;769;1025;1281;1537];
   types = {'Continuous';...
            'Discrete';...
	    'State Transition Diagram';...
	    'Data Store';...
	    'Triggered';...
	    'Standard Procedure';...
	    'Startup Procedure';...
	    'Background Procedure';...
	    'Interrupt Procedure';...
	    'Macro Procedure';...
	    'Inline Procedure'};
   ind = find(codes == code);
   if (isempty(ind))
      sbtype = 'Unknown';
   else
      switch (code)
      case 1
	 switch (signal)
	 case -1
	    sbtype = sprintf('%s (%g,%g), Free-running',types{ind},h(1),h(2));
	 case 0
	    sbtype = sprintf('%s (%g,%g), Enable signal: Parent',types{ind},h(1),h(2));
	 otherwise
	    sbtype = sprintf('%s (%g,%g), Enable signal: Pin %d',types{ind},h(1),h(2),signal);
	 end
      case 129
	 posting = [' (ATR)';
		    ' (SAF)';
		    ' (ANT)'];
	 switch (signal(1))
	 case -1
	    sbtype = sprintf('%s %s, Trigger signal: None',types{ind},posting(signal(2),:));
	 case 0
	    sbtype = sprintf('%s %s, Trigger signal: Parent',types{ind},posting(signal(2),:));
	 otherwise
	    sbtype = sprintf('%s %s, Trigger signal: Pin %d',types{ind},posting(signal(2),:),signal(1));
	 end
      otherwise
	 sbtype = types{ind};
      end
   end
   return					   % Last card of GETTYPE

function sdate = getdate(ndate)
%GETDATE
%
%
   if (ndate == 0)
      sdate = '??-???-??';
   else
      d = zeros(3,1);
      for (k = 1:3)
	 d(k) = rem(ndate,100);
	 ndate = fix(ndate/100);
      end
      mths = ['Jan';'Feb';'Mar';'Apr';'May';'Jun';'Jul';'Aug';'Sep';'Oct';'Nov';'Dec'];
      sdate = sprintf('%d-%s-%d',d(1),mths(d(2),:),d(3));
   end
   return					   % Last card of GETDATE

function [jexe,os] = jade(arch,dirsep)
%JADE Find where is the jade executable
%
%

   jd = cat(2,matlabroot,dirsep,'sys',dirsep,'jade',dirsep,'bin',dirsep);
   switch (arch)
   case 'PCWIN'
      os = 'nt';
      jexe = cat(2,jd,'jade.exe');
      if (exist(jexe,'file') ~= 2)
	 jexe = cat(2,jd,os,dirsep,'jade.exe');
	 if (exist(jexe,'file') ~= 2)
	    jexe = '';
	 end
      end
   case {'SUN4','SOL2','SGI','SGI64','ALPHA','GLNX86','LNX86'}
      os = 'unix';
      jexe = cat(2,jd,lower(arch),dirsep,'jade');
      if (exist(jexe,'file') ~= 2)
	 jexe = '';
      end
   otherwise
      jexe = '';
      os = 'unsupported';
   end
   return					   % Last card of JADE
   
function [stat,msg] = cdriver(stylesheet,backend,dsssl,opt)
%CDRIVER
%
%
      
   [fid,msg] = fopen(stylesheet,'wt');
   if (fid == -1)
      stat = -1;
   else
      fprintf(fid,'<!DOCTYPE style-sheet PUBLIC "-//James Clark//DTD DSSSL Style Sheet//EN" [\n');
      fprintf(fid,'<!ENTITY dbstyle SYSTEM "%s" CDATA DSSSL>\n',dsssl);
      fprintf(fid,']>\n');
      fprintf(fid,'<STYLE-SHEET>\n');
      fprintf(fid,'<STYLE-SPECIFICATION USE="docbook">\n');
      fprintf(fid,'<STYLE-SPECIFICATION-BODY>\n');
      switch (backend)
      case 'sgml'
	 if (opt == 1)
	    fprintf(fid,'(define nochunks #t)\n');
	 else
	    fprintf(fid,'(define nochunks #f)\n');
	 end
      case {'rtf-95','rtf','tex'}
	 switch (opt)
	 case 1
	    fprintf(fid,'(define %%visual-acuity%% "normal")\n');
	 case 2
	    fprintf(fid,'(define %%generate-book-titlepage%% #f)\n');
	    fprintf(fid,'(define %%generate-book-toc%% #f)\n');
	    fprintf(fid,'(define($generate-book-lot-list$) (list (normalize "equation"))))\n');
	 case 3
	    fprintf(fid,'(define %%visual-acuity%% "large-type")\n');
	 otherwise
	 end
      otherwise
      end
      fprintf(fid,'</STYLE-SPECIFICATION-BODY>\n');
      fprintf(fid,'</STYLE-SPECIFICATION>\n');
      fprintf(fid,'<EXTERNAL-SPECIFICATION ID="docbook" DOCUMENT="dbstyle">\n');
      fprintf(fid,'</STYLE-SHEET>\n');
      stat = fclose(fid);
   end
   return					   % Last card of CDRIVER
