% version 1.15

% Note that you must have shell_escape set to true (anything that doesn't
% start with 'n' or '0' AFAIK), either in general, or in a texmf.cnf that's
% in your TEXMFCNF path, or in a wrapper or something, for this to work.
% That's a potential security hole, of course.

% to use this, do
% \outsrc[exec]{tag}
% material for the script, perhaps arbitrary Perl code
% \endsrc
% where ``\endsrc'' must be on a line by itself.
% The material between \outsrc and \endsrc is interpreted verbatim by 
% default, but you can switch explicitly between this behavior and 
% ``interpret it normally as TeX'' with \messysrc (interpreted) and 
% \cleansrc (verbatim).  Again, to use either of these it must be on the 
% line by itself.  ``Interpret it normally'' isn't complete; for one thing, 
% it happens a line at a time, and for another, it only expands as much as 
% \write does, which is only as much as \edef does.  I'm not sure \messysrc
% is ever really useful, but I implemented it before realizing how limited this
% expansion really is.  In particular, \input doesn't DTRT at all.

% Whatever material this ends up having between \outsrc and \endsrc
% (ie verbatim stuff + processed stuff if you use \messysrc) is
% saved with the tag [in /var/tmp/blahblahblahtag].  If you specified
% the optional [exec] argument, exec is immediately run on the file
% (for instance, if you'd put in Perl code you might use [perl] or
% [/usr/athena/bin/perl] as [exec]); STDOUT is captured to another temp
% file and \input, being then interpreted normally.

% If you omit the [exec] argument, the script is written to the tagged
% file but not executed.  Whether or not you included an [exec] originally,
% you can (re)run your script with \callsrc{exec}{tag}; potentially you
% could even \callsrc the same tag'd script with different exec's, if
% you're feeling particularly insane.  Anyway, \callsrc calls exec on
% the file with that tag and sucks in the results.

% If you're not planning on \callsrc'ing it later, {tag} can be {}.
% (Well, I suppose you could \callsrc that too if you wanted....)

% Experienced TeX users can, of course, find many ways to shoot themselves
% in the foot with this, even without \messysrc.

\newwrite\@out

\xdef\@prefix{/var/tmp/src.\jobname.\the\year.\the\month.\the\day.\the\time}
\newtoks\@code
\newtoks\@results  \global\@results=\expandafter{\@prefix.results}
\newtoks\@tag

\gdef\@sanitize{\def\do##1{\catcode`##1=12 }\dospecials}

\newcount\@sane  
\gdef\outsrc{\futurelet\@nexttok\@startsrc}
\gdef\@startsrc{\gdef\@exec{}%
  \ifcat\@nexttok[\let\@next\@grabexec\else\let\@next\@start\fi\@next}
\gdef\@grabexec[#1]{\gdef\@exec{#1}\@start}
\gdef\@start#1{\@tag={#1}\@code=\expandafter{\@prefix.code.#1}%
  \immediate\openout\@out=\the\@code \@sane=1 \@dosrc}
\gdef\@dosrc{\begingroup\ifodd\@sane\@sanitize\fi\catcode`\^^M=12 \@aux}

\gdef\@endInsane{\endsrc}
\gdef\@nutssane{\cleansrc}
\gdef\@nutsnuts{\messysrc}
{\catcode`\$=0 \catcode`\\=12
	$gdef$@endSane{\endsrc}
	$gdef$@sanesane{\cleansrc}
	$gdef$@sanenuts{\messysrc}
}

\gdef\@is#1#2#3{\ifx#1#2 1\else\ifx#1#3 1\else 0\fi\fi}

{\catcode`\^^M=12 \gdef\@aux#1^^M{\def\@temp{#1}%
\ifodd\@is\@temp\@endSane\@endInsane\gdef\@next{\@finishsrc}%
\else\ifodd\@is\@temp\@sanesane\@nutssane\global\@sane=1\gdef\@next{\@dosrc}%
\else\ifodd\@is\@temp\@sanenuts\@nutsnuts\global\@sane=0\gdef\@next{\@dosrc}%
\else\immediate\write\@out{#1}\gdef\@next{\@dosrc}%
\fi\fi\fi\endgroup\@next}}

\gdef\@finishsrc{\immediate\closeout\@out%
\ifx\@exec\empty\else\callsrc{\@exec}{\the\@tag}\fi}

\gdef\callsrc#1#2{\immediate\write18{#1 \@prefix.code.#2 > \the\@results}%
\immediate\input\the\@results\relax}
