%! e-TeX V1.1;5
%% Revision-date:  1-NOV-1996 22:41:40 /PT
%% Revision-history: (at-end)

% The version/revision number in the first line is compared with the
% corresponding elements in e-TeX, and an error reported if incompatibility is
% detected.  The cycle number (that is, the element(s) following the semi-
% colon) are not used in this comparison, allowing enhancements to be made to 
% this module and recorded in its header without introducing a risk of conflict.
% Other files whose headers must be kept in synch include "etexdefs.lib" and 
% "language.def".

% This is "etex.src", the (e-TeX) source file for the "etex" format file.
% To build the format from it, use one of the following commands or the
% local equivalent:

% Generic: einitex *etex.src \dump
% emTeX:   etex -i *etex.src \dump
% VMS:     etex /init /noformat *"etex.src \dump"

% Assume a purely IniTeX environment and set up a minimal \catcode r'egime

\catcode `\{ = 1
\catcode `\} = 2
\catcode `\# = 6
\catcode `\^ = 7
\catcode `\@ = \catcode `\A

% Define the basic error-reporting and abort mechanisms:

\def \et@xmsg #1#2%
   {\begingroup
    \def \n {^^J}
    \def \ { }
    \newlinechar = \expandafter `\n 
    \if E#1
          \errorcontextlines = 0
          \errmessage {e-TeX error: #2}
    \else
          \message {! e-TeX\if I#1 message%
                           \else \if W#1 warning%
                                 \else \if F#1 fatal error%
                                       \else
                                           unknown (#1)%
                                       \fi
                                 \fi      
                           \fi: #2
                   }
    \fi
    \endgroup
   }

% Note: a future version may report errors in the %<fac>-<s>-<code>[, <text>]
% format, allowing the more verbose <text>s to be read from file rather than
% stored in the format.

\def \et@xabort #1%
    {\et@xmsg {F}{#1}
     \batchmode
     \end
    }

% Make sure this file is being read by e-TeX in extended mode;
% If it is, prepare to check version/revision compatibility, otherwise abort.

\ifx \undefined \eTeXversion
     \et@xabort {this file can be processed only in extended mode;\n
                 \ \ did you perhaps forget the asterisk?
                }
\else 
     \begingroup
     \catcode `\% = 12 
     \xdef \et@xfilehdr 
          {\detokenize {%! e-TeX V}\the \eTeXversion \eTeXrevision}
     \xdef \et@xlibhdr 
          {\detokenize {%! e-TeXlib V}\the \eTeXversion \eTeXrevision}
     \endgroup
\fi

% Assume extended mode, but no additional \catcodes/\defs yet;
% set up a simple e-TeX condition-reporting system:

\def \et@xinf #1{\et@xmsg {I}{#1}} %%% not currently used
\def \et@xwarn #1{\et@xmsg {W}{#1}}
\def \et@xerr   #1#2{{\errhelp = {#2}\et@xmsg {E}{#1}}}

% deactivate the processing of patterns and exceptions; these will be 
% reinstated later, after the \uselanguage mechanism has been defined.

\let \et@xpatterns = \patterns
\let \et@xhyphenation = \hyphenation
\def \patterns 
    {\begingroup 
     \everyeof = {\fi \endgroup} 
     \message {[skipping from \noexpand \patterns to end-of-file...]}
     \iffalse
    }

% Read "plain.tex" (\patterns & \hyphenation will be skipped)

\input plain

% restore the catcode of commercial-at (@), which will just have been clobbered

\catcode `\@ = \catcode `\A

% Assume an extended Plain environment (i.e. there are no longer any
% restrictions on the coding techniques we can use).  First prepare to
% carry out consistency checks on the file headers and the current e-TeX
% version/revision levels:

\newread \et@xinput

\def \etexhdrchk #1#2%
    {\openin \et@xinput = #2
     \ifeof \et@xinput 
            \chardef \etexstatus = 0 % V1.0-1
     \else
            \begingroup
            \endlinechar = -1
            \readline \et@xinput to \et@xbuf        
            \def \p@rtition ##1;##2\endp@rtition {##1}
            \xdef \et@xbuf 
                 {\expandafter \p@rtition \et@xbuf;\endp@rtition}
            \endgroup
            \closein \et@xinput
            \expandafter
            \ifx \csname et@x#1hdr\endcsname \et@xbuf 
                 \chardef \etexstatus = 2 % V1.1;4
            \else
                 \chardef \etexstatus = 1 % V1.1;4
            \fi
     \fi
    }

% Check the version number of this file:

\def \et@xfmtsrc {etex.src}
\etexhdrchk {file} {\et@xfmtsrc}
\ifcase \etexstatus
        \et@xerr {unable to open format source file "\et@xfmtsrc";}
                   {This should not happen; please ensure that your system
                    allows a file to be opened for reading more than once 
                    concurrently.
                   }
\or
        \et@xerr {format source file "\et@xfmtsrc" has wrong header;\n
                    \ \ expected: "\et@xfilehdr"; found: "\et@xbuf";
                   }
                   {You are using a version of e-TeX which is incompatible with 
                    the source for the format file you are trying to compile; 
                    please ensure that you have the most recent version of each.
                   }
\fi

% Module handling now implemented (V1.0-2)

\def \module #1{\iftrue}
\let \endmodule = \fi

\newtoks \et@xtoks

\def \et@xl@@d #1 #2\endl@ad %%% the "#1 #2" code avoids trailing spaces
    {\ifcsname module:#1\endcsname
             \et@xwarn {duplicate module name "#1"}
     \else
        \csname module:#1\endcsname %%% we exploit the side-effect explicitly
        \et@xtoks = \expandafter 
                {\the \et@xtoks 
                 \expandafter \let \csname module:#1\endcsname = \undefined
                }
     \fi
    }

\def \et@xl@ad #1#2,#3\endl@ad %%% the #1#2 code avoids spurious leading spaces
    {\et@xl@@d #1#2 \endl@ad
     \if \relax #3\relax
        \let \n@xt = \relax
     \else
        \def \n@xt {\et@xl@ad #3\endl@ad}
     \fi
     \n@xt
    }

\def \et@xload #1 %%% needed because of the embedded \ifs...
    {\def \module ##1%
         {\unless
          \ifcsname module:##1\endcsname
                 \message {Skipping module "##1";}%
          \else
                 \message {Loading module "##1";}%
         }%
     \input #1
     \def \module ##1{\iftrue}
    }

\def \load #1 from #2 %%% selective module loading from an e-TeX library file
    {\etexhdrchk {lib} {#2}
     \ifcase \etexstatus 
        \et@xerr {unable to open library file "#2"; load aborted.}
                   {I got an <eof> on trying to open your library file;
                    please make sure it exists, is readable and is not locked.
                   }
     \or
        \et@xerr {library file "#2" has wrong header;\n
                    \ \ expected: "\et@xlibhdr"; found: "\et@xbuf";\n
                    \ \ load aborted.
                   }
                   {The first line of your library does not start with
                    the correct e-TeX header; you may need to update the
                    file to be compatible with the current version of e-TeX,
                    or you may simply have specified the wrong file.
                   }
     \else
        \et@xtoks = {\et@xtoks = {}}
        \et@xl@ad #1,\endl@ad     
        \et@xload {#2}
        \the \et@xtoks
     \fi
    }

\load interactionmodes from etexdefs.lib %%% \load <foo>[, <bar>...] from <baz>

\def \@sk #1#2#3%
    {\ifnum \interactionmode = \errorstop %%% 3 (V1.0-2)
        \def \pr@mpt {\csname #1 \endcsname}
        \edef \pr@mptloop {{\escapechar = -1 \global \readline 16 to \pr@mpt}}
        \loop \pr@mptloop
           \ch@ckforyn \pr@mpt {#2}
        \ifb@dresponse
           \message {Please answer Y[es] or N[o]}
        \repeat
     \else
        \def \pr@mpt {#3}
        \ch@ckforyn \pr@mpt {#2}
     \fi
    }

\def \ch@ckforyn #1#2% Bernd Raichle's improved version, V1.0-1
    {\edef \@nswer {#1}%
     \def \p@rse ##1##2\endp@rse
         {\lowercase {\if y##1}\b@dresponsefalse \csname #2true\endcsname
          \else \lowercase {\if n##1}\b@dresponsefalse \csname #2false\endcsname
                \else
                      \b@dresponsetrue
                \fi 
          \fi
         }%
     \expandafter \p@rse \@nswer \endp@rse
    }

\def \usef@llback %%% V1.0-3
    {\message {Using fallback mode (USenglish)}
     \addlanguage {USenglish}{hyphen}{}{3}{2}
    }

% OK, that's all the utilities defined; on with the real work:
% First, re-instate \patterns & \hyphenation:

\let \patterns = \et@xpatterns
\let \hyphenation = \et@xhyphenation

% Define the language-handling commands 

\def \et@xlang {\csname newlanguage\endcsname}

\def \uselanguage #1%
    {\ifcsname lang@#1\endcsname
        \language = \csname lang@#1\endcsname
        \lefthyphenmin = \csname lhm@#1\endcsname 
        \righthyphenmin = \csname rhm@#1\endcsname
        \ifdefined \uselanguage@hook % V1.0-1
            \uselanguage@hook {#1}%  % V1.0-1
        \fi
     \else
        \et@xerr {language #1 undefined.}
                   {You are trying to use a language which has not previously
                    been defined; remember that any language you want to use
                    will need to have been specified at the time the format
                    was created.
                   }               
     \fi
    }

\def \addlanguage #1#2#3#4#5% language patterns exceptions lhm rhm
    {\expandafter \et@xlang \csname lang@#1\endcsname
     \expandafter \chardef \csname lhm@#1\endcsname = #4 % V1.0-1
     \expandafter \chardef \csname rhm@#1\endcsname = #5 % V1.0-1
     \uselanguage {#1}
     \input #2
     \if \relax#3\relax
     \else
        \input #3
     \fi
     \ifdefined \addlanguage@hook % V1.0-4
        \addlanguage@hook {#1}%  % V1.0-4
     \fi
     \edef \fmtversion {\fmtversion/#1}
    }

% Decrement \count 19, because \newlanguage will increment it again

\advance \count 19 by -1

% We are about to try to process a user/site-specific file "language.def",
% which establishes a Babel-like language selection environment.  Since
% there is always a risk of a spurious file of that name being found,
% we look for an e-TeX header in the first line.  If the file can be opened, 
% but doesn't have the right header, we interrogate the user as to whether 
% to use fallback mode; in this mode, we simply establish USenglish as the
% sole language, with the et@x patterns, exceptions and left- and right-
% hyphen minima for TeX.  If we can't interrogate the user (e.g. not in 
% \errorstopmode), or if the user elects not to use fallback, we abort;  
% if the file can't be found/opened, we use fallback unconditionally.

\newif \ifb@dresponse
\newif \ifusef@llback

\def \l@ngdefnfile {language.def}

\etexhdrchk {file} {\l@ngdefnfile}
\ifcase \etexstatus
        \et@xwarn {unable to open file "\l@ngdefnfile";}
        \usef@llback                      % unable to open "language.def"
\or             
        \et@xwarn {file "\l@ngdefnfile" has wrong header;\n
                      \ \ expected: "\et@xfilehdr"; found: "\et@xbuf";
                     }
        \@sk {Use fallback?} {usef@llback} {y}
        \ifusef@llback
                \usef@llback
        \else
                \input \l@ngdefnfile    % use "language.def" after warning
        \fi
\else
        \input \l@ngdefnfile    % "language.def" open & valid
\fi

% All that was just to set up natural language handling...
% The "real" work of "etex.src", however, is to augment the non-primitives 
% of Plain.TeX to incorporate e-TeX specific features, and to add new
% non-primitives to simplify access to new e-TeX specific primitives.

\message {Augmenting the Plain TeX definitions:}
\message {\string \tracingall;}
\edef \tracingall 
     {\tracingall 
      \tracingcommands = \thr@@
      \tracingassigns = \@ne
      \tracinggroups = \@ne
      \tracingifs = \@ne
      \tracingscantokens = \@ne
     }

\message {Adding new e-TeX definitions:}

\message {\string \eTeX,}
\def \eTeX {$\varepsilon$-\TeX} %%% the simple version, not suitable for maths;
                                %%% a more sophisticated one may find its way
                                %%% into "etexdefs.lib" in due course.

\message {\string \loggingall,}
\def \loggingall {\tracingall \tracingonline = \z@}

\message {\string \tracingnone,}
\def \tracingnone
    {\showboxdepth = \thr@@
     \showboxbreadth = 5
     \tracingassigns = \z@
     \tracingcommands = \z@
     \tracingmacros = \z@
     \tracingrestores = \z@
     \tracingifs = \z@
     \tracinggroups = \z@
     \tracingscantokens = \z@
     \tracingparagraphs = \z@
     \tracinglostchars = \@ne
     \tracingoutput = \z@
     \tracingpages = \z@
     \tracingstats = \z@
     \tracingonline = \z@
    }

\message {\string \newmark;}
\newcount \marksc@unt
\marksc@unt = \@cclvi
\outer \def \newmark #1%
    {\global \advance \marksc@unt by \m@ne
     \ifnum \marksc@unt < 1
            \errhelp {Sorry, you can allocate a maximum of 255 new mark
                        classes; the 256th (\marks 0) is reserved, 
                          and is equivalent to TeX's original \mark}
            \errmessage {No room for a new mark}
     \else
            \allocationnumber = \marksc@unt
            \global \chardef #1 = \allocationnumber
            \wlog {\string #1 = \string \marks \the \allocationnumber}
     \fi
    }

% In an attempt to reduce the overheads of e-TeX, we recycle all possible
% resources, including (as a penultimate step) the recycler itself...
% The user can circumvent (or force) this during format creation,
% by \letting \ifrecycle = \iffalse (or \iftrue, to force it).

\def \mayber@cycle {\expandafter \ifrecycle}
\def \forcer@cycle {\expandafter \iftrue}

\ifdefined \ifrecycle 
        \mayber@cycle 
\else 
        \forcer@cycle 
\fi

% There's a concealed \if... lurking here, which explains the
% strange indentation of the code that follows (V1.0-1)

        \def \r@cycle #1%
            {\ifdefined #1
                 \message {\string #1,}\let #1 = \und@fined
             \else
                 \message {\string #1 (not defined),}
             \fi
            }
        {\newlinechar = `\! \message {!Recycling:}}

        \r@cycle \addlanguage 
        \r@cycle \@nswer
        \r@cycle \@sk
        \r@cycle \b@dresponsetrue
        \r@cycle \b@dresponsefalse
        \r@cycle \ch@ckforyn
        \r@cycle \mayber@cycle 
        \r@cycle \et@xabort
        \r@cycle \et@xbuf
        \r@cycle \et@xfmtsrc
        \r@cycle \et@xfilehdr
        \r@cycle \et@xhyphenation
        \r@cycle \et@xinf
        \r@cycle \et@xpatterns
%       \r@cycle \ifb@dresponse
%       \r@cycle \ifusef@llback
        \r@cycle \l@ngdefnfile
        \r@cycle \n@xt
        \r@cycle \p@rse
        \r@cycle \pr@mpt
        \r@cycle \pr@mptloop
        \r@cycle \forcer@cycle 
        \r@cycle \usef@llback
        \r@cycle \usef@llbacktrue
        \r@cycle \usef@llbackfalse
        
% The following are retained, since they may be needed by user code; with a few 
% (regrettable) exceptions, all of these are given the \etex or \et@x prefix, 
% to reduce as far as possible the risk of them clashing with other used-defined
% names.

% The e-TeX team are willing to change the names of the remaining, at-risk,
% control sequences if it is demonstrated that the current names cause problems
% or difficulties.

        \def \r@tain #1%
            {\ifdefined #1
                 \message {\string #1,}
             \else
                 \message {\string #1 (not defined),}
             \fi
            }
        {\newlinechar = `\! \message {!Retaining:}}

        \r@tain \et@xerr
        \r@tain \et@xinput 
        \r@tain \et@xlibhdr 
        \r@tain \et@xmsg
        \r@tain \et@xtoks
        \r@tain \et@xwarn
        \r@tain \et@xl@@d
        \r@tain \et@xl@ad
        \r@tain \et@xload
        \r@tain \et@xlang 
        \r@tain \eTeX
        \r@tain \etexhdrchk
%       \r@tain \endmodule
        \r@tain \etexstatus
        \r@tain \module
        \r@tain \uselanguage

        \r@cycle \r@tain
        \r@cycle \r@cycle

\fi

% And finally re-catcode commercial-at

\catcode `\@ = 12
\endinput

%% Revision-history:

01-NOV-1996: V1.1;5: Final stage? Checking that everything possible
             is recycled whilst nothing that is needed is lost.  Retained
             names given \etex or \et@x prefix wherever possible.  Some
             potentially dangerous cs-names commented out of the recycle/retain
             lists because they were synonyms of members of the if-else-fi set.
             The definition of the e-TeX logo added: this had somehow slipped
             through the net!  Intended to be the final revision before public 
             release, but awaiting comments from other members of the team.

31-OCT-1996: V1.1;4: errors noticed while proof-reading corrected; far
             greater use made of concealed control sequences with
             commercial-at (@); library file headers differentiated
             from ordinary file headers; return-values for header-check
             altered to allow fall-thru' if ok; fallback-use algorithm
             significantly modified (major semantic shift).

29-OCT-1996: V1.1;3: tidying up prior to release; careful check on
             which control words can be re-cycled; \recycle made more
             robust, to check whether one is trying to re-cycle something
             that was never 'cycled' in the first place (PEB suggestion);
             \et@xmsg re-written to use \message or \errmessage as necessary;
             \errhelps provided for all places they might be referenced.
 
27-OCT-1996: V1.1;2: minor version numbers re-introduced, using semi-colon
             as delimiter and stopping pattern matching at the semi-colon;
             \et@xfilehdr defined dynamically, based in \eTeXversion and 
             \eTeXrevision;  \load now uses \etexhdrchk, so library
             files are required to start with an e-TeX header; this will
             have implications when e-TeX -> V2+, so this code should be
             re-considered at this point.
              
17-OCT-1996: V1.1: minor version numbers eliminated, to reduce the
             need for users to update their "language.def" files.

17-OCT-1996: V1.0-4: \addlanguage@hook added, version history moved to end.

17-OCT-1996: Version 1.0-3 does away with the "fallback" file.

16-OCT-1996: Version 1.0-2 implements module handling, so constant "3"
             no longer used (see below).

13-Oct-1996: Replaced \def by \chardef where possible;
             eliminated dependency on "etexdefs.def", hard-coding
             in the constant "3" (with comment) instead;
             used Bernd's \ch@ckforyn;
             re-catcoded `\@ earlier, to reduce number of \[if]csnames;
             commented the strange indentation of the \recycles at end.

11-Oct-1996: language handling commands modified (V1.0): PT/BR
