%			      ospecial.tex
% Compatibility mode between dvi2ps \special command syntax and dvitps
% \special commands. 
%
% NOTE: This is provided only for backward compatibility. In new 
% documents, please use \psfig.
%
% tara@rutgers.edu (2/28/90)
%
%
%    Supported dvi2ps \special syntax is:
%
%
%    The \special string can contain any number of the  following
%    keyword=value pairs, separated by blanks:
%
%    Keyword   Value Type    (dimensions in points: 72 pt = 1 in)
%
%    psfile    string        - PostScript file to include
%    hsize     dimension     - maximum horizontal size (for clip-
%                            ping)
%    vsize     dimension     - maximum vertical size  (for  clip-
%                            ping).   Use   negative   values  to
%                            specify a clipping region below  the
%                            current position.
%    hoffset   dimension     - amount to shift right
%    voffset   dimension     - amount to shift up
%    hscale    number        - scale factor in x-dimension
%    vscale    number        - scale factor in y-dimension
%
%    Thus:
%    \special{psfile=foo.ps hoffset=72 hscale=0.9 vscale=0.9}
%    will shift the graphics produced by file foo.ps right by 1",
%    and will draw it at 0.9 normal size.
%
%    hsize and vsize are given relative to the (0,0) point of the
%    drawing and are unaffected by offsets and scales.
%
%    Offsets are given relative to the point of the \special com-
%    mand, and are unaffected by scales.
%
\catcode`\@=11
% save \special primitive
\let\o@special=\special
%
\def\@SpecialCodeDriver{dvitps: }
%
% Identifying message.
\message{-- using dvi2ps compatible \string\specials}

% Definitions
\def\p@command{-1} 
\def\p@psfile{}    % the file to be included

\def\@hsize{\def\p@command{0}}
\def\@vsize{\def\p@command{1}}
\def\@hoffset{\def\p@command{2}}
\def\@voffset{\def\p@command{3}}
\def\@hscale{\def\p@command{4}}
\def\@vscale{\def\p@command{5}}
\def\@psfile{psfile}

\def\if@strcmp #1#2{%
	\edef\@strone{#1}%
	\edef\@strtwo{#2}%
	\ifx \@strone\@strtwo
}

\def\if@empty #1{%
	\if@strcmp{#1}{}%
}

\def\parse@special #1=#2 {%
        \def\p@command{-1}%
%	\wlog{Parsing special #1 is #2}%
	\if@strcmp{#1}{\@psfile}
            \edef\p@psfile{#2}%
%           \wlog{--> psfile is \p@psfile}
        \else
	    \csname\string @#1\endcsname  %evaluated as \relax if undefined
            \ifcase\p@command
	       \or \or \or \or \or \else
               \errmessage{An option you have given to \string\special
                           \space is unrecognized: #1}	
	    \fi
            \o@special{\@SpecialCodeDriver Literal "#2 \space @#1 \space"}%
%           \wlog{--> file is #2}
	\fi        	
}
	

% Vararg code adapted from example in pre-release edition of
% _Another Look at Tex_,  Stephan v. Bechtolsheim
%
\newcount \va@argno
\def\va@args{%
%	\wlog{}
	\def\p@psfile{}%
%	\wlog{\string\va@args: begin}
	\o@special{\@SpecialCodeDriver Include0 "ospecial.pro"}%
	\o@special{\@SpecialCodeDriver Literal "@beginspecial \space"}%
	\va@argno=0
	\va@argsA
}

\def\va@argsA{%
	\futurelet\va@argstoken\va@argsmore
}

\def\va@argsmore{%
	\ifx\va@argstoken\va@end
	  \let\next = \va@argsdone
	\else
	  \let\next=\va@argsdo
	\fi
	\next
}

\def\va@argsdone \va@end{%
	\o@special{\@SpecialCodeDriver Literal "@setspecial \space"}%
	%empty psfile
	\if@empty{\p@psfile}
	    \errmessage{You must specify a file to be included.}
	\fi
        \o@special{\@SpecialCodeDriver Include1 "\p@psfile"}%
	\o@special{\@SpecialCodeDriver Literal "@endspecial \space"}%
%	\wlog{\string\va@args: end}
}

\def\va@argsdo #1 {%
	\advance\va@argno by 1
%	\wlog{Argument \the\va@argno: "#1"}%
	\parse@special #1 %
	% recursive call; continue to parse arguments
	\va@argsA
}


\def\special#1{\va@args #1 \va@end}

\catcode`\@=12
