| 1 |
% This is webmerge.tex |
| 2 |
% Copyright (C) 1995,96 by the NTS team; all rights are reserved. |
| 3 |
% |
| 4 |
\def\fileversion{V 1.1} |
| 5 |
\def\filedate{29 Mar 96} |
| 6 |
% |
| 7 |
% In order to generate e-TeX several change files have to be applied |
| 8 |
% (one after the other) to tex.web. This can be done efficiently with |
| 9 |
% programs such as PATCHWEB or TIE. If neither of these is available |
| 10 |
% the present program WEBMERGE can be used to merge several change |
| 11 |
% files into one change file that can then be used with TANGLE. |
| 12 |
% |
| 13 |
% If the web file or one of the change files contains tab of form feed |
| 14 |
% characters there is, however, a problem that can't be solved in a |
| 15 |
% satisfactory way. This should not be a real problem since the |
| 16 |
% original tex.web and etex.ch don't contain these characters and there |
| 17 |
% is no real good reason why the system-dependent change files should |
| 18 |
% (except may be that some editors insist on converting sequences of |
| 19 |
% space characters into tabs). |
| 20 |
% |
| 21 |
% The problem is due to the fact that (an unmodified) TeX cannot write |
| 22 |
% tabs or form feeds to the output file. Therefore WEBMERGE offers two |
| 23 |
% alternatives: if \ifallowtabs is false (by default) then tab and form |
| 24 |
% feed characters are invalid and lead to error messages; if this |
| 25 |
% happens one may use the command |
| 26 |
% \allowtabstrue |
| 27 |
% to allow tab and form feed as valid input characters. If they are |
| 28 |
% written to the output file they will appear as '^^I' and '^^L'. This |
| 29 |
% must then be changed manually with a suitable editor before the output |
| 30 |
% from WEBMERGE can be used as input for TANGLE. |
| 31 |
% |
| 32 |
% Typically three change files are required to generate e-TeX, e.g., |
| 33 |
% 1. etex.ch (system independent changes for e-TeX) |
| 34 |
% 2. tex.ch (system dependent changes for TeX) |
| 35 |
% 3. tex.ech (additional system dependent changes for e-TeX) |
| 36 |
% The sequence commands (to be used with plain TeX) |
| 37 |
% \input webmerge |
| 38 |
% \webfile{tex.web} % web file |
| 39 |
% \changefile{etex.ch} % 1. change file |
| 40 |
% \changefile{tex.ch} % 2. change file |
| 41 |
% \changefile{tex.ech} % 3. change file |
| 42 |
% \outfile{etex.ch} % output file, start processing |
| 43 |
% creates a combined change file `etex.ch'. |
| 44 |
% Webmerge uses a temporary file with default name `tmp.tmp', |
| 45 |
% the command |
| 46 |
% \tempfile{<temp name>} |
| 47 |
% can be used to change that name. |
| 48 |
% |
| 49 |
% Webmerge is slow, therefore PATCHWEB or TIE should be used whenever |
| 50 |
% possible. The program checks for correct change files and gives error |
| 51 |
% messages similar to those of TANGLE and WEAVE. The error recovery is, |
| 52 |
% however, rather limited. |
| 53 |
% |
| 54 |
% In case of problems please contact: |
| 55 |
% Peter Breitenlohner [email protected] |
| 56 |
|
| 57 |
% We make @ signs act like letters, temporarily. |
| 58 |
\catcode`\@=11 |
| 59 |
|
| 60 |
\newif\ifallowtabs % initially false |
| 61 |
\def\allowtabs{\catcode`\^^I=12 \catcode`\^^L=12 } |
| 62 |
\def\forbidtabs{\catcode`\^^I=15 \catcode`\^^L=15 } |
| 63 |
|
| 64 |
\toksdef\toks@ii=2 |
| 65 |
|
| 66 |
% First we redefine plain.tex's \loop to allow the construction |
| 67 |
% \loop ... \if... \else ... \repeat |
| 68 |
% |
| 69 |
\def\@iterate{\@body \expandafter\@iterate\fi} |
| 70 |
% |
| 71 |
% and to allow nested loops such as |
| 72 |
% \loop{... \loop ... \if... \repeat ... \if...}\repeat |
| 73 |
% where the braces do not imply grouping |
| 74 |
% |
| 75 |
\def\loop#1\repeat{% |
| 76 |
\toks@\expandafter{\@body}% |
| 77 |
\toks@ii\expandafter{\@@body}% |
| 78 |
\edef\@@body{\def\noexpand\@body{\the\toks@}% |
| 79 |
\def\noexpand\@@body{\the\toks@ii}}% |
| 80 |
\def\@body{#1}\@iterate \@@body} |
| 81 |
\let\@body=\empty |
| 82 |
\let\@@body=\empty |
| 83 |
|
| 84 |
% |
| 85 |
\def\@msg{\immediate\write\sixt@@n} |
| 86 |
\@msg{*** webmerge \fileversion\space <\filedate> ***} |
| 87 |
% |
| 88 |
% Conceptually the web file (web_0) is combined with the first change |
| 89 |
% file, ch_1, in order to produce a ficticious web file web_1. Then |
| 90 |
% web_1 is combined with ch_2 in order to produce web_2 etc. |
| 91 |
|
| 92 |
% The logic of merging is that of TANGLE and WEAVE. |
| 93 |
% With several change files there may, however, be changes on top of |
| 94 |
% changes, i.e., a line changed by one change file may be changed again |
| 95 |
% by another change file. |
| 96 |
|
| 97 |
% The program below uses quite a few control sequences, many of them |
| 98 |
% constructed dynamically. |
| 99 |
% The most important ones are \<i>read (<i>=0,1,...) used to obtain the |
| 100 |
% next line from web_<i>. For reasons of efficiency they are \let to |
| 101 |
% either \<i>w (changing=false), \<i>c (changing=true), of \<i>e (file |
| 102 |
% has ended). |
| 103 |
% \<i>g is used to obtain the next line from ch_<i> and test for |
| 104 |
% @x/@y/@z, |
| 105 |
% \<i>prime (prime the change buffer) scans for the next @x from ch_<i>, |
| 106 |
% and \<i>match discards matching lines from web_<i-1> and ch_<i> until |
| 107 |
% an @y is found. |
| 108 |
|
| 109 |
\def\tempfile#1{\def\t@n{#1}} |
| 110 |
\def\t@n{tmp.tmp} % default tempfile name |
| 111 |
\newread\t@r % read tempfile |
| 112 |
\newwrite\t@w % write tempfile |
| 113 |
\def\t@o{\immediate\write\t@w} % write to tempfile |
| 114 |
\newwrite\o@w % write output file |
| 115 |
\def\o@o{\immediate\write\o@w} % write to outfile |
| 116 |
|
| 117 |
\newcount\@nch % number of change files |
| 118 |
\newcount\@num % number of active changes |
| 119 |
\newcount\@res % result from @x/@y/@z test |
| 120 |
\begingroup % \@pct expands to `% ' |
| 121 |
\lccode`\1=`\% |
| 122 |
\lowercase{\endgroup \def\@pct{1 }} |
| 123 |
% |
| 124 |
|
| 125 |
% We need macros to define read streams, count registers, and control |
| 126 |
% sequences dynamically (inside \edef) |
| 127 |
|
| 128 |
\def\@nrd#1{\ifx#1\relax \csname newread\endcsname#1\fi} |
| 129 |
\def\@nct#1{\ifx#1\relax \csname newcount\endcsname#1\else #1\z@ \fi} |
| 130 |
\def\@cs#1{\csname#1\endcsname} |
| 131 |
\def\@csi#1{\csname\@i#1\endcsname} |
| 132 |
\def\@dcs#1{\expandafter\def\csname#1\endcsname} |
| 133 |
\def\@ecsi#1{\expandafter\edef\csname\@i#1\endcsname} |
| 134 |
\def\@ncsi#1{\expandafter\noexpand\csname\@i#1\endcsname} |
| 135 |
\def\@read{\expandafter\noexpand\csname\@ii read\endcsname} |
| 136 |
\def\@ifx{\noexpand\ifx} |
| 137 |
\def\@ifnum{\noexpand\ifnum} |
| 138 |
\def\@ifeof{\noexpand\ifeof\@csi r} |
| 139 |
\def\@else{\noexpand\else} |
| 140 |
\def\@fi{\noexpand\fi} |
| 141 |
\def\@loop{\noexpand\loop} |
| 142 |
\def\@repeat{\noexpand\repeat} |
| 143 |
\def\@expa{\noexpand\expandafter\noexpand} |
| 144 |
\def\@expai#1{\expandafter\@expa\csname\@i#1\endcsname} |
| 145 |
|
| 146 |
\def\webfile#1{% define webfile |
| 147 |
\ifnum\@nch=\m@ne \@nch\z@ \@dcs{0n}{#1}% |
| 148 |
\else \@msg{\string\webfile{#1} ignored (out of order)}% |
| 149 |
\fi} |
| 150 |
\def\changefile#1{% define a changefile |
| 151 |
\ifnum\@nch<\z@ |
| 152 |
\@msg{\string\changefile{#1} ignored (missing \string\webfile)}% |
| 153 |
\else \ifnum\@nch>8 \@msg{\string\changefile{#1} ignored (too many)}% |
| 154 |
\else \advance\@nch\@ne \@dcs{\number\@nch n}{#1}% |
| 155 |
\fi \fi} |
| 156 |
\def\outfile#1{% define outfile and process |
| 157 |
\ifnum\@nch<\@ne |
| 158 |
\@msg{\string\outfile{#1} ignored (missing \string\changefile)}% |
| 159 |
\else \def\o@n{#1}\@init \@merge \@done |
| 160 |
\fi} |
| 161 |
\def\@init{% initialize |
| 162 |
\@msg{}\@msg{webmerge \fileversion\space <\filedate>}% |
| 163 |
\immediate\openout\o@w=\o@n |
| 164 |
\o@o{\@pct This is \o@n, a WEB change file produced by webmerge.tex}% |
| 165 |
\begingroup |
| 166 |
\def\do##1{\catcode`##1=12 }\dospecials |
| 167 |
\ifallowtabs \allowtabs \else \forbidtabs \fi |
| 168 |
\endlinechar=\m@ne |
| 169 |
\count@\z@ |
| 170 |
\loop \edef\@i{\number\count@}\@@init |
| 171 |
\ifnum\count@<\@nch \advance\count@\@ne \let\@ii\@i |
| 172 |
\repeat |
| 173 |
\o@o{}% |
| 174 |
\@msg{out=\o@n, merging ...}} |
| 175 |
% |
| 176 |
% Here now is the quite complicated macro \@@init |
| 177 |
% its main purpose is to dynamically construct |
| 178 |
% the macro \<i>read that returns the next line of web_<i> in \@web |
| 179 |
% as well as various auxiliary macros \<i>... |
| 180 |
% |
| 181 |
\def\@@init{% initialize input file <i> |
| 182 |
\edef\x{% define \read streams and \count registers |
| 183 |
\noexpand\@nrd\@ncsi r% \newread\<i>r |
| 184 |
\noexpand\@nct\@ncsi l% \newcount\<i>l (line number) |
| 185 |
\noexpand\@nct\@ncsi s% \newcount\<i>s (status) |
| 186 |
}\x \@csi l\z@ \@csi s\z@ % \<i>l=0 \<i>s=0 |
| 187 |
\openin\@csi r\@csi n % \openin\<i>r=\<i>n |
| 188 |
\@msg{\ifeof\@csi runable to open input file |
| 189 |
\else \ifnum\@i=\z@ web\else change \@i\fi =\fi \@csi n}% |
| 190 |
\ifnum\count@=\z@ % <i>=0 for web file |
| 191 |
\let\@web\relax |
| 192 |
%% |
| 193 |
%% \def\0w{% return web_0 line (file not yet ended) |
| 194 |
%% \read\0r to\@web \0s=0 % read from web_0, mark as unchanged |
| 195 |
%% \ifeof\0r \0e \else \advance\0l by 1 \fi} |
| 196 |
%% |
| 197 |
\@ecsi w{% return web_0 line (file not yet ended) |
| 198 |
\read\@csi rto\@web \@csi s\z@ |
| 199 |
\@ifeof \@ncsi e\@else \advance\@csi l\@ne \@fi}% |
| 200 |
%% |
| 201 |
%% \def\0e{% return web_0 line (file has ended) |
| 202 |
%% \let\0read=\0e \let\@web=\relax} |
| 203 |
%% |
| 204 |
\@ecsi e{% return web_0 line (file has ended) |
| 205 |
\let\@ncsi{read}\@ncsi e\let\@web\relax}% |
| 206 |
%% |
| 207 |
%% \ifeof\0r \0e \else \let\0read=\0w |
| 208 |
%% |
| 209 |
\ifeof\@csi r\@csi e% |
| 210 |
\else \edef\x{\let\@ncsi{read}\@ncsi w}\x |
| 211 |
\fi |
| 212 |
\o@o{\@pct to be applied to \@csi n}% |
| 213 |
\o@o{\@pct combining the changes (one after the other) from}% |
| 214 |
\else % <i>=1,2,3,... for change files |
| 215 |
\o@o{\@pct \@i. \@csi n}% |
| 216 |
%% |
| 217 |
%% \def\<i>g#1{% read change file and test for @x/@y/@z |
| 218 |
%% \ifeof\<i>r \let\<i>x=\relax \@res=#1 |
| 219 |
%% \else \@res=0 \read\<i>r to\<i>x |
| 220 |
%% \advance\<i>l by 1 \expandafter\@test\<i>x ab\@#1<i> |
| 221 |
%% \fi} |
| 222 |
%% |
| 223 |
\@ecsi g##1{% read change file and test for @x/@y/@z |
| 224 |
\@ifeof \let\@ncsi x\relax \@res##1% |
| 225 |
\@else \@res\z@ \read\@csi rto\@ncsi x% |
| 226 |
\advance\@csi l\@ne \@expa\@test\@ncsi xab\noexpand\@##1\@i |
| 227 |
\@fi}% |
| 228 |
%% |
| 229 |
%% \def\<i>w{% return web_i line (changing is false) |
| 230 |
%% \<i-1>read % get web_<i-1> line |
| 231 |
%% \ifx\@web\<i>x % test for match |
| 232 |
%% \expandafter\<i>match % match lines from web_i-i and ch_i |
| 233 |
%% \fi} % else return web_<i-1> line |
| 234 |
%% |
| 235 |
\@ecsi w{% return web_i line (changing is false) |
| 236 |
\@read \@ifx\@web\@ncsi x\@expai{match}\@fi}% |
| 237 |
%% |
| 238 |
%% \def\<i>c{% return web_i line (changing is true) |
| 239 |
%% \<i>g 3 % get ch_i line and test for @z |
| 240 |
%% \ifnum\@res=3 % @z found |
| 241 |
%% \@echg <i> % deactivate a change |
| 242 |
%% \<i>prime % prime the change buffer |
| 243 |
%% \expandafter\<i>read % read again, now from web_<i-1> |
| 244 |
%% \else \@mod\<i>x % return ch_i line, mark as changed (\0s=1) |
| 245 |
%% \fi} |
| 246 |
%% |
| 247 |
\@ecsi c{% return web_i line (changing is true) |
| 248 |
\@ncsi g\thr@@ |
| 249 |
\@ifnum\@res=\thr@@ |
| 250 |
\noexpand\@echg\@i \@ncsi{prime}\@expai{read}% |
| 251 |
\@else \noexpand\@mod\@ncsi x% |
| 252 |
\@fi}% |
| 253 |
%% |
| 254 |
%% \def\<i>e{% return web_i line (change file has ended) |
| 255 |
%% \<i-1>read} % return web_<i-1> line |
| 256 |
%% |
| 257 |
\@ecsi e{% return web_i line (change file has ended) |
| 258 |
\@read}% |
| 259 |
%% |
| 260 |
%% \def\<i>prime{% prime the change buffer |
| 261 |
%% \loop |
| 262 |
%% \<i>g 1 % get ch_i line and test for @x |
| 263 |
%% \ifnum\@res=1 \else \repeat % repeat until found |
| 264 |
%% \loop |
| 265 |
%% \<i>g 0 % get ch_i line |
| 266 |
%% \ifx\<i>x\empty \repeat % repeat until not blank line |
| 267 |
%% \ifx\<x>\relax \let\<i>read=\<i>e % change file has ended |
| 268 |
%% \else \let\<i>read=\<i>w % |
| 269 |
%% |
| 270 |
\@ecsi{prime}{% prime the change buffer |
| 271 |
\@loop \@ncsi g\@ne \@ifnum\@res=\@ne \@else \@repeat |
| 272 |
\@loop \@ncsi g\z@ \@ifx\@ncsi x\noexpand\empty \@repeat |
| 273 |
\@ifx\@ncsi x\relax \let\@ncsi{read}\@ncsi e% |
| 274 |
\@else \let\@ncsi{read}\@ncsi w% |
| 275 |
\@fi}% |
| 276 |
%% |
| 277 |
%% \def\<i>match{% match lines from web_<i-1> and ch_<i> |
| 278 |
%% \ifx\@web\relax % web_<i-1> and ch_<i> have ended |
| 279 |
%% \let\<i>read=\<i>e \<i> % indicate web_<i> has ended |
| 280 |
%% \else \@bchg % activate a change |
| 281 |
%% \loop \@chg % write a matching line to output (maybe) |
| 282 |
%% \<i>g 2 % get ch_i line and test for @y |
| 283 |
%% \ifnum\@res=2 \@endm <i> % end of match found |
| 284 |
%% \else \<i-1>read % get web_<i-1> line |
| 285 |
%% \ifx\@web\relax % test for end of web file |
| 286 |
%% \@err <i>{Web file ended during change} |
| 287 |
%% \fi |
| 288 |
%% \ifx\@web\<i>x % test for matching lines |
| 289 |
%% \else \advance\<i>s by 1 \fi % count mismatches |
| 290 |
%% \repeat |
| 291 |
%% \let\<i>read=\<i>c % now changing is true |
| 292 |
%% \fi |
| 293 |
%% \<i>read} % get next web_<i> line again |
| 294 |
%% |
| 295 |
\@ecsi{match}{% match lines from web_<i-1> and ch_<i> |
| 296 |
\@ifx\@web\relax |
| 297 |
\let\@ncsi{read}\@ncsi e% |
| 298 |
\@else \noexpand\@bchg |
| 299 |
\@loop \noexpand\@chg \@ncsi g\tw@ |
| 300 |
\@ifnum\@res=\tw@ \noexpand\@endm\@i% |
| 301 |
\@else \@read |
| 302 |
\@ifx\@web\relax |
| 303 |
\noexpand\@err\@i{Web file ended during change}% |
| 304 |
\@fi |
| 305 |
\@ifx\@web\@ncsi x\@else \advance\@csi s\@ne \@fi |
| 306 |
\@repeat |
| 307 |
\let\@ncsi{read}\@ncsi c% |
| 308 |
\@fi |
| 309 |
\@ncsi{read}}% |
| 310 |
%% |
| 311 |
%% \<i>prime % prime the change buffer |
| 312 |
%% |
| 313 |
\@csi{prime}% prime the change buffer |
| 314 |
\fi} |
| 315 |
\def\@done{%terminate |
| 316 |
\count@\z@ |
| 317 |
\loop \edef\@i{\number\count@}% |
| 318 |
\ifnum\count@=\z@ \else % change file |
| 319 |
\ifeof\@csi r\else \@@err{Change file entry didn't match}\fi |
| 320 |
\fi |
| 321 |
\closein\@csi r% close input file <i> |
| 322 |
\ifnum\count@<\@nch \advance\count@\@ne |
| 323 |
\repeat |
| 324 |
\endgroup |
| 325 |
\immediate\closeout\o@w |
| 326 |
\@nch\m@ne % prepare for next \webfile |
| 327 |
\@msg{... done}\@msg{}} |
| 328 |
|
| 329 |
\catcode`\0=11 % for \0s |
| 330 |
\def\@merge{% process |
| 331 |
\@num\z@ |
| 332 |
\expandafter\loop\csname\number\@nch read\endcsname % read web_<n> |
| 333 |
\ifnum\0s=\@ne \t@o{\@web}\fi |
| 334 |
\ifx\@web\relax |
| 335 |
\else |
| 336 |
\repeat} |
| 337 |
\def\@chg{\ifnum\0s=\z@ \o@o{\@web}\fi} |
| 338 |
\def\@mod{\0s\@ne \let\@web} |
| 339 |
\catcode`\0=12 |
| 340 |
|
| 341 |
\def\@err#1{\def\@i{#1}\@@err} |
| 342 |
\def\@@err#1{\@msg{! #1}% |
| 343 |
\@msg{ ... change file \@i\space (\@csi n) line \the\@csi l}} |
| 344 |
|
| 345 |
\def\@test#1#2#3\@{\if#1@ \csname set@#2\endcsname \fi \@eat} |
| 346 |
\def\@eat#1#2{} |
| 347 |
\def\set@x{\@res\@ne \expandafter\@xyz} |
| 348 |
\def\set@y{\@res\tw@ \expandafter\@xyz} |
| 349 |
\def\set@z{\@res\thr@@ \expandafter\@xyz} |
| 350 |
\let\set@X=\set@x |
| 351 |
\let\set@Y=\set@y |
| 352 |
\let\set@Z=\set@z |
| 353 |
\def\@xyz\@eat#1#2{% |
| 354 |
\ifnum#1=\@res |
| 355 |
\else \@err#2{Extra \@@xyz{\@res} ignored (expecting \@@xyz#1)}% |
| 356 |
\@res\z@ |
| 357 |
\fi} |
| 358 |
\def\@@xyz#1{@\ifcase#1\or x\or y\or z\fi} |
| 359 |
|
| 360 |
\def\@endm#1{% |
| 361 |
\ifnum\csname#1s\endcsname>\z@ |
| 362 |
\@err#1{Hmm... \the\@csi s of the preceding lines failed to match}% |
| 363 |
\fi |
| 364 |
\csname#1s\endcsname\z@} |
| 365 |
|
| 366 |
\def\@bchg{% activate a change |
| 367 |
\ifnum\@num=\z@ % first change activated, start writing to temp |
| 368 |
\immediate\openout\t@w=\t@n\space |
| 369 |
\o@o{@x l.\number\csname 0l\endcsname} |
| 370 |
\fi |
| 371 |
\advance\@num\@ne} |
| 372 |
\def\@echg#1{% deactivate a change |
| 373 |
\expandafter\ifx\csname#1x\endcsname\relax |
| 374 |
\@err#1{Change file ended during change}% |
| 375 |
\fi |
| 376 |
\advance\@num\m@ne |
| 377 |
\ifnum\@num=\z@ % last change deactivated |
| 378 |
\t@o{@z}% |
| 379 |
\immediate\closeout\t@w % close temp file |
| 380 |
\openin\t@r=\t@n\space |
| 381 |
\o@o{@y} |
| 382 |
\loop \read\t@r to\t@x \o@o{\t@x}% copy temp to output |
| 383 |
\ifeof\t@r \closein\t@r |
| 384 |
\else |
| 385 |
\repeat |
| 386 |
\fi} |
| 387 |
|
| 388 |
\@nch=\m@ne |
| 389 |
|
| 390 |
\catcode`\@=12 % at signs are no longer letters |
| 391 |
|
| 392 |
\endinput |