summaryrefslogtreecommitdiff
path: root/support/convert/convert.dtx
blob: adb3db00f94445a5135a9ca0069ecf11a10c7523 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
\def\fileversion{1.0b}
\def\filedate{1991/07/08}
\def\docdate{1991/07/08}

%% \CheckSum{556}
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
%%
%\iffalse
%
%% TeX utility `convert' to use with plainTeX or LaTeX v2.09
%% Copyright (C) 1991 by Bernd Raichle.
%
% Copying of this file is authorized only if either
% (1) you make absolutely no changes to your copy, including name, or
% (2) if you do make changes, you name it something other than
%     convert.tex, convert.doc, or convert.dtx.
% This restriction helps ensure that all utilities developed here
% remain identical.
%
% Error reports please to:
%        Bernd Raichle
%        Stettener Str. 73
%        D-73732 Esslingen
%        Federal Republic of Germany
% Email: raichle@.informatik.uni-stuttgart.de
%
%
%\fi
%
% \changes{v1.0b}{21 april 1999}{small doc changes;
%                 replaced extension doc with dtx;
%                 license conditions changed to LPPL}
% \changes{v1.0a}{8 july 1991}{revised documentation}
% \changes{v0.6}{6 july 1991}{replaced `input by `read
%                             when reading the input file}
% \changes{v0.5}{18 june 1991}{added conversion table in a file}
% \changes{v0.4}{14 may 1991}{added conversion to macros}
% \changes{v0.3}{22 april 1991}{plain\TeX\ version, fixed NUL-char bug}
% \changes{v0.2}{27 march 1991}{added C-I expansion and
%                               C-M/C-J distinction}
% \changes{v0.1}{16 march 1991}{first version (for \LaTeX)}
%
%
% \DoNotIndex{\fileversion,\filedate,\docdate}
% \DoNotIndex{\catcode,\def,\let,\string,\immediate,\write,\read}
% \DoNotIndex{\ifx,\else,\fi,\expandafter,\csname,\endcsname,\end}
% \DoNotIndex{\everypar,\errhelp,\errmessage,\inputlineno,\ifnum}
% \DoNotIndex{\errorcontextlines,\afterassignment,\jobname,\relax}
% \DoNotIndex{\chardef,\openout,\closeout,\openin,\closein,\the}
% \DoNotIndex{\message,\begingroup,\endgroup,\lowercase,\advance}
% \DoNotIndex{\gdef,\lccode,\xdef,\noexpand,\global,\ifcase,\or}
% \DoNotIndex{\uppercase,\uccode,\count,\endlinechar,\ifeof}
% \DoNotIndex{\endinput,\^^@}
% \DoNotIndex{\sixt@@n,\nullfont,\count@,\z@,\@ne,\tw@,\thr@@,\m@ne}
% \DoNotIndex{\sixt@@n,\@cclvi,\loop,\repeat,\space,\empty,\active}
% \DoNotIndex{\undefined,\newcount,\newwrite}
%
%
% \title{Convert -- a \TeX-file converter for
%        \TeX~3.0\thanks{This file has version number \fileversion,
%        last revised \filedate, documentation dated \docdate.}}
% \author{Bernd Raichle}
%
% \address{Stettener Str.~73\\
%          D-73732 Esslingen\\
%          Federal Republic of Germany}
% \netaddress{raichle@informatik.uni-stuttgart.de}
% \signature{\signaturemark Bernd Raichle\\
%         \theaddress1}
%
% \date{June 12, 1991}
%
% \maketitle
%
% \begin{abstract}
%   The release of \TeX~3.0 and use of national character sets
%   arise one problem: the portability of texts.  Users should be
%   able to use an customized \TeX\ which allows the input and
%   output of national characters.
%   The exchange of texts with extended character set is
%   supported by \TeX\ if you are using the \verb+^^x+ notation
%   described in the \TeX{}book.  This utility written in \TeX\
%   converts a text to this notation.
% \end{abstract}
%
%
% \section{External and internal character representation}
%
% \subsection{Basics}
%
% {\small\it
% {\S 17. The character set.}\\
% In order to make \TeX\ readily portable to a wide variety of
% computers, all of its input text is converted to an internal eight-bit
% code that includes standard ASCII, the ``American Standard Code for
% Information Interchange.''  This conversion is done immediately when each
% character is read in. Conversely, characters are converted from ASCII to
% the user's external representation just before they are output to a
% text file.
% \par\smallskip
% {\S 23.}\\
% The ASCII code is ``standard'' only to a certain extent, since many
% computer installations have found it advantageous to have ready access
% to more than 94 printing characters. Appendix~C of {\sl The \TeX book\/}
% gives a complete specification of the intended correspondence between
% characters and \TeX's internal representation.
% \par
% \hbox to\hsize{\hfil\rm from \TeX: The Program.}\par}
%
% \medskip
%
% \TeX\ reads the input file line by line and converts between the internal
% character set (a superset of ASCII) and the user's external character
% set with the help of two arrays named {\tt xord} and {\tt xchr}.
%
% Each character~$c_e$ read from the file or the terminal is replaced
% by the (internal) character $c_i = \mathop{\rm xord}[c_e]$.
%
% When \TeX\ writes a character~$c_i$ to a file or the terminal the
% string {\tt str\_pool$[c_i]$} is used to produce visible strings for
% unprintable characters.  Each (internal) character~$c_j$ from this
% string is replaced by the (external) character
%  $c_e = \mathop{\rm xchr}[c_j]$.
%
% \medskip
%
% The conversions between the internal and the external character set
% are system dependent. A text written on system~$A$ has to be translated
% when it is transferred to system~$B$ with another external character set.
%
% {\catcode`\^=12
% A text is portable only if both systems use the same mapping from their
% external character set to ASCII characters $32-127$\footnote{Additionally
% the mapping to ASCII characters with special functions like NUL, CR,
% LF,\ldots has to be equal too.} and the text uses no other characters.
% \TeX\ supports further the use of non-ASCII characters
% if a special notation is used:  when reading text the character sequence
% \verb=^^x= is replaced by one character with internal code $x-64$ or
% $x+64$.  Additionally \TeX~3 allows the notation \verb=^^xy= where
% \verb=xy= are two lowercase hexadecimal digits. The four character sequence
% is replaced by one character with internal code equal to the
% hexadecimal value.\footnote{The characters in {\tt ^^x} and {\tt ^^xy}
% are first translated to their internal representation before the
% replacement is done.  Additionally the category code of character {\tt ^}
% has to be~5.  For example \LaTeX's {\tt verbatim} environment change the
% category code to~12.}}
%
%
% \subsection{Example}
%
% Assume that you have two \TeX\ versions and the external character set
% uses ASCII for character codes~$< 128$.  The code of the character
% {\tt \"a} is {\it 132} (or hexadecimal \verb="84=) and \TeX's internal
% code should be {\it 228} (or \verb="e4=) for this character.
%
% Version~1 uses idendity as mapping between external and internal
% character codes, all characters not in $32-127$ are printed in
% \verb=^^x= notation.  Version~2 maps the character {\tt \"a} and
% prints this character as {\tt \"a}.
%
% The interesting parts of the arrays in both versions are:
%
% \hbox to\hsize{\hfil
% \begin{tabular}{|rlr|rlr|}
% \hline
% \multicolumn{3}{|l|}{\small Version 1:} &
% \multicolumn{3}{|l|}{\small Version 2:} \\ \hline
%        xord[132]&=&132         &      xord[132]&=&228 \\
%                 & &            &      xchr[228]&=&132 \\
%   str\_pool[132]&=&\verb=^^84= & str\_pool[228]&=&{\tt \"a} \\
% \hline
% \end{tabular}\hfil}
%
% \medskip
%
% If version~1 reads the character {\tt \"a} it is translated to internal
% code~$132$.  When this character is printed \TeX\ writes the string
% \verb=^^84= to the output and translates each of the four characters
% of this string to their external representation.
%
% Using version~2 the character {\tt \"a} is translated to internal
% code~$228$ when it is read.  \TeX\ writes the character~$228$ as string
% {\tt \"a} (an {\tt \"a} with code $228$!).  This character is replaced
% by its external code~$132$ and the user sees the character {\tt \"a}.  
%
% \medskip
%
% The \verb=^^xy= notation can be used in both versions to get the same
% internal character code for the {\tt \"a}.
% \TeX\ replaces the character sequence \verb=^^e4= by one character with
% internal code \verb="e4= ($= 228$).
% This character is written as explained above, i.e., version~1 prints
% \verb=^^e4=, version~2 prints {\tt \"a}.
%
%
% \section{Usage}
%
% To use this utility just type ``\verb=tex convert='' or
% ``\verb=latex convert=''.
% You are prompted for the conversion type and the input and output
% filename.  This tool checks if you want to override your input file
% but their is no check for an existing output file.
% When an empty output filename is given the output is written only to
% the terminal and in the log-file.
%
% In the current version of this tool there are two types for
% conversion.
% Type~1 converts all unprintable characters to the \verb=^^z=
% or \verb=^^xy= notation.
% Type~2 additionally reads the file {\tt convert.tbl} with
% user-defined translation entries.  Both conversion types expands
% {\tt TAB} characters and ignores additional {\tt LF} and {\tt CR}
% characters in a line with a warning.
%
% \bigskip
%
% The translation table in {\tt convert.tbl} is read without
% expansion of the control sequences given.  All characters except
% `invalid' and `ignored' characters can be used in the replacement
% string.  Another exception is the comment character `\%' which can
% be used to allow comments in this file.
%
% For each translation you wrote a line
%
%  \begin{tabular}{p{2cm}p{2cm}l}
%    & \it charnum & \it replacement text
%  \end{tabular}
%
% {\em charnum} is one of the formats allowed for character numbers
% in \TeX. Note that the replacement text can have unbalanced braces.
%
% \medskip
% {\bf Examples:}
%
% You want to replace the character with internal code $228$ by
% the string \verb={\" a}=.
%
%  \begin{tabular}{p{2cm}p{2cm}l}
%     & {\tt 228} & \verb={\" a}=
%  \end{tabular}
%
% If you don't know the internal character code of an {\tt \"a}
% you should specify this character as {\it charnum}.  The
% translation of the {\tt \"a} is done when reading the file
% {\tt convert.tbl} and when reading the input file with the same
% input/output translation.
%
%  \begin{tabular}{p{2cm}p{2cm}l}
%     & {\tt \"a} & \verb={\" a}=
%  \end{tabular}
%
% You have just received a file where all {\tt \"a} characters are
% printed as {\tt \v D} and you want to convert it to your external
% character code.
%
%  \begin{tabular}{p{2cm}p{2cm}l}
%     & {\tt \v D} & {\tt \"a}
%  \end{tabular}
%
%
%\StopEventually{\makesignature\PrintIndex}
%
% \section{The implementation}
%
%    \subsection{Initial Phase}
%
%    Because we want to use internal macros, set the category code
%    of \verb=@= to `letter'.
%    \begin{macrocode}
\catcode`@=11
%    \end{macrocode}
%
%    This utility can be used with plain\TeX\ and \LaTeX.  For plain\TeX\
%    we copy some useful things from `latex.tex'.
%    \begin{macrocode}
\newwrite\@unused
\def\typeout#1{{\let\protect\string\immediate\write\@unused{#1}}}
\def\@strip#1#2 \@gobble{\def #1{#2}}
\def\@defpar{\par}
\def\@iden#1{#1}
\def\@gobble#1{}
\def\typein[#1]#2{\typeout{#2}\read\sixt@@n to#1%
  \ifx #1\@defpar \def#1{}\else
    \@iden{\expandafter\@strip\expandafter #1#1\@gobble\@gobble} \@gobble
  \fi}
%    \end{macrocode}
%
%    The first real thing to do is to write the current version of this file.
%    \begin{macrocode}
\typeout{TeX Utility `convert' version \fileversion\space <\filedate> (br)}
\typeout{English documentation dated <\docdate> (br)}
%    \end{macrocode}
%
%    \LaTeX\ redefines \verb=\end=.  We define a new macro to end this
%    job which works for plain\TeX\ and \LaTeX.  The first \verb=\@@end=
%    has the meaning \verb=\relax= if we are using plain\TeX, otherwise
%    it's plain\TeX's \verb=\end= which is saved in \verb=\@@end= by
%    \LaTeX.
%    \begin{macro}{\endofjob}
%    \begin{macrocode}
\def\endofjob{\csname @@end\endcsname \end}
%    \end{macrocode}
%    \end{macro}
%
%    This utility should never produce a DVI-file.  For the case that this
%    is not true\footnote{This should only happen when this tool is
%    corrupted!}, we produce an error message.
%    \begin{macrocode}
\everypar{\errhelp{This TeX utility should not produce any dvi output.}%
  \errmessage{Internal Error 1. -- Please fix me!}}
\nullfont
%    \end{macrocode}
%
%    We use the fact that \TeX~3 can distingiush 256~different
%    input characters.
%    If the user has started a pre-3 \TeX\ we exit with an error message
%    to inform the user about the need of a newer \TeX\ version.
%    \begin{macrocode}
\def\sorry{%
  \typeout{******************************************}
  \typeout{* Sorry, you need TeX 3 for this Utility *}
  \typeout{******************************************}
  \endofjob}
%    \end{macrocode}
%    The new version of `plain.tex' and `lplain.tex' allocates normal
%    counter for the new \TeX~3 counter if you are using a pre-3 \TeX.
%    We use the fact that \verb=\inputlineno= is set to a negative value
%    after allocating the normal counter and that this counter is not
%    changed.  For \TeX~3 it contains a positive value.
%    \begin{macrocode}
\newif\iftexthree \texthreetrue
\ifx\undefined\inputlineno
  \texthreefalse
\else
  \count@=\inputlineno
  \ifnum\inputlineno<\z@
    \texthreefalse
  \else\ifnum\inputlineno=\count@
    \texthreefalse
  \fi\fi
\fi
\iftexthree \else \expandafter\sorry\fi
%    \end{macrocode}
%
%    If an error occurs it's unnecessary to print more than a few lines.
%    \begin{macrocode}
\errorcontextlines=\m@ne
%    \end{macrocode}
%
%    \subsection{User Input}
%
%    We need the character \verb=^= as a normal character with category
%    code `other'.
%    \begin{macrocode}
\catcode`^=12
%    \end{macrocode}
%
%    Inform the user about the usefullness of this file.
%    \begin{macrocode}
\typeout{}
\typeout{********************************************************}
\typeout{ This program converts text-files with 8bit-characters}
\typeout{ into text-files with TeX's ^^xx syntax or TeX macros}
\typeout{ for these characters.}
\typeout{********************************************************}
\typeout{}
%    \end{macrocode}
%
%    Ask the user what type of conversion should be done.  Currently
%    this file supports the conversion of unprintable characters in
%    \TeX's \verb=^^xx= syntax or user-defined replacement.
%
%    The type of conversion is saved in the counter \verb=\convtype=.
%    \begin{macro}{\convtype}
%    \begin{macrocode}
\newif\iffehler
\newcount\convtype
%    \end{macrocode}
%    \end{macro}
%    The assignment to \verb=\convtype= is not error-prove. If the user
%    types more than an integer \TeX\ reports an error.
%    \begin{macrocode}
\def\getnumber{\afterassignment\gobblerest\count@=}
\def\gobblerest#1\lineend{}
\loop
  \typeout{ Should I convert 8bit characters to...}%
  \typeout{ \space 1) ^^x notation or}%
  \typein[\conversion]%
          { \space 2) macros calls (defined in `convert.tbl')}%
%    \end{macrocode}
%    If the user types nothing, convtype should be set to zero.
%    \begin{macrocode}
  \getnumber\conversion \space 0 \lineend
  \convtype=\count@
  \fehlertrue
  \ifnum\convtype>\z@ \ifnum\convtype<\thr@@ \fehlerfalse \fi\fi
  \iffehler
    \typeout{ !! Please type 1 or 2 !!}\typeout{}%
\repeat
%    \end{macrocode}
%    We ask the user about the input and the new output
%    filename.
%    \begin{macro}{\infilename}
%    \begin{macro}{\outfilename}
%    \begin{macrocode}
\typeout{}
\typein[\infilename]{First type the name of your input file:}
\typeout{}
\typein[\outfilename]{Now type the name of your output file:}
\typeout{}
%    \end{macrocode}
%    \end{macro}
%    \end{macro}
%    We should avoid the case that the user overwrites the input file.
%    \begin{macrocode}
\ifx\infilename\outfilename
  \typeout{}
  \typeout{!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!}
  \typeout{! Not possible to read from and write to the same file !}
  \typeout{!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!}
  \expandafter\endofjob
\fi
%    \end{macrocode}
%
%    If the output filename is empty, we output into the log-file
%    and on the terminal. Otherwise we open the file given in
%    \verb=\outfilename= for output.
%    \begin{macrocode}
\newwrite\outstream
\ifx\outfilename\empty
  \typeout{Output to \jobname.log and online.}
  \typeout{------------------------------------------------------------}
  \chardef\outstream=\sixt@@n
\else
  \immediate\openout\outstream \outfilename \relax
\fi
%    \end{macrocode}
%
%    \subsection{Collect and output one Line}
%
%    To access unprintable characters in a portable way in this file we
%    change the category code of \verb=~= to~7.  Additionally a character
%    with category code `active' is used very often.
%    \begin{macrocode}
\catcode`~=7
\catcode`$=\active
%    \end{macrocode}
%
%    We save the current incomplete line in the token register
%    \verb=\currentline= and maintain an input line counter.
%    \begin{macrocode}
\newtoks\currentline \currentline={}
\newcount\lineno
%    \end{macrocode}
%
%    We output a complete line with \verb=\finish@line=. When the output
%    is written in a file, we inform the user about the current processed
%    input line number.
%    \begin{macro}{\finish@line}
%    \begin{macrocode}
\ifx\outfilename\empty
  \def\finish@line{\immediate\write\outstream{\the\currentline}%
    \currentline{}\outputpos\z@}
\else
  \def\finish@line{\immediate\write\outstream{\the\currentline}%
    \currentline{}\outputpos\z@\message{\the\lineno}}
\fi
%    \end{macrocode}
%    \end{macro}
%
%    Tabs should be expanded so we need another counter.
%    \begin{macrocode}
\newcount\outputpos \outputpos=\z@
%    \end{macrocode}
%
%    The macro \verb=\expand@tab= inserts space characters in the input
%    stream until a tab position is reached.  We need an active space
%    character, so we use a little trick with \verb=\lowercase=.
%    \begin{macro}{\expand@tab}
%    \begin{macrocode}
\begingroup\lccode`$=32
\lowercase{\endgroup
\def\expand@tab{%
  \count@\outputpos
  \loop \ifnum\count@>7 \advance\count@ -8\repeat
  \loop \ifnum\count@<8 \advance\count@\@ne $\repeat
}
} % end of lowercase
%    \end{macrocode}
%    \end{macro}
%
%    With the macro \verb=\defoutstring= we define the character in argument~1
%    to write the string given in argument~2 of length given in argument~3.
%    The definition is done with \verb=\xdef= because the second argument can
%    contain some other macros (e.g. \verb=\hexnum=) which have to be expanded.
%
%    But before this is done there is a special case where
%    \verb=\lowercase= doesn't work.
%    \begin{macrocode}
\begingroup \catcode`\~~@=\active
\gdef~~@{\currentline\expandafter{\the\currentline ^^@}%
     \advance\outputpos\thr@@}
\endgroup
%    \end{macrocode}
%    \begin{macro}{\defoutstring}
%    \begin{macrocode}
\def\defoutstring#1#2#3{\ifnum#1=\z@ \else
  \begingroup \lccode`$=#1\relax
  \lowercase{\endgroup\xdef$}%
    {\currentline\noexpand\expandafter{\noexpand\the\currentline #2}%
    \advance\outputpos\noexpand#3}%
  \fi
}
%    \end{macrocode}
%    \end{macro}
%
%    To assign control sequences to characters we use two additional macros.
%    \begin{macro}{\defout}
%    \begin{macrocode}
\def\defout#1#2{\lccode`$=#1\relax
  \lowercase{\gdef$}{#2}}
%    \end{macrocode}
%    \end{macro}
%    \begin{macro}{\letout}
%    \begin{macrocode}
\def\letout#1#2{\lccode`$=#1\relax
  \lowercase{\global\let$}#2}
%    \end{macrocode}
%    \end{macro}
%
%    For characters greater than~127, we need a hexdecimal representation
%    with lowercase characters. The macro has to be expandable.
%    \begin{macro}{\hexnum}
%    \begin{macrocode}
\def\hexnum#1{\ifcase#1 0\or 1\or 2\or 3\or 4\or 5\or 6\or 7\or
  8\or 9\or a\or b\or c\or d\or e\or f\fi}
%    \end{macrocode}
%    \end{macro}
%
%    \subsection{Definition of active Characters}
%
%    We define macros for all of the 256 characters in TeX's internal
%    character representation.
%
%    Here is the first use of \verb=\defoutstring=: We define the printable
%    character in \TeX's character positions 32--127 to output themself.
%    \begin{macrocode}
\catcode`*=12
\count@=32
\loop
  \lccode`*=\count@
  \ifnum\count@<127
     \lowercase{\defoutstring\count@*{\@ne}}
     \advance\count@\@ne
\repeat
\defoutstring{127}{^^?}\thr@@
%    \end{macrocode}
%
%   The characters in position 0--31 are output in the form \verb=^^X=.
%    \begin{macrocode}
\count@=\@ne \count\thr@@=`A
\loop
  \uccode`*=\count\thr@@
  \ifnum\count@<32
     \uppercase{\defoutstring\count@{^^*}}\thr@@
     \advance\count@\@ne \advance\count\thr@@\@ne
\repeat
%    \end{macrocode}
%
%    The character positions 128--255 (these are new in \TeX~3) are
%    written in the form \verb=^^xx=.
%    \begin{macrocode}
\count@=128
\count\@ne=8 \count\tw@=0 % hex-counter
\loop
  \ifnum\count@<\@cclvi
     \defoutstring\count@
        {^^\hexnum{\the\count\@ne}\hexnum{\the\count\tw@}}4
     \advance\count@\@ne
     \ifnum\count\tw@>14
       \count\tw@\z@ \advance\count\@ne \@ne
     \else
       \advance\count\tw@ \@ne
     \fi
\repeat
%    \end{macrocode}
%
%    \subsection{User-defined character translation}
%
%    The user can define his own character translation.  The translation
%    are specified in a file called `convert.tbl' which contains one
%    translation per line.  Each line begins with the character number
%    (with the syntax of \TeX's \verb=<charnum>=), the rest of the line
%    is the replacement string.
%
%    After the conversion string is read in we need to know the
%    length of this string.
%    \begin{macro}{\getlength}
%    \begin{macro}{\lengthloop}
%    \begin{macrocode}
\newcount\length
\def\getlength{\afterassignment\lengthloop\let\next= }
\def\lengthloop{%
  \ifx\next\empty
  \else
    \advance\length\@ne
    \expandafter\getlength
  \fi
}
%    \end{macrocode}
%    \end{macro}
%    \end{macro}
%
%    For each line we get at first the character number and the length
%    of the translation string.  With the macro \verb=\defoutstring=
%    we redefine the corresponding macro to produce this string.
%    \begin{macro}{\processline}
%    \begin{macrocode}
\def\processline#1 #2\lineend{\length=\z@
  \getlength#2\empty \getnumber#1\space -1 \lineend
  \defoutstring\count@{#2}\length
}
%    \end{macrocode}
%    \end{macro}
%
%    Now we define the macro to read the file `convert.tbl'.
%    When reading the file we have to set most of the letter to
%    category codes `letter' or `other' to suppress expansion and
%    errors.  Only the category codes of a few letters are left
%    unchanged.
%
%    \begin{macro}{\readfile}
%    \begin{macrocode}
\newif\ifmore \moretrue
\def\readfile{\begingroup
  \count@=\z@
  \loop
    \ifnum\count@<\@cclvi
      \ifnum\catcode\count@<9
        \catcode\count@ 12
      \else\ifnum\catcode\count@=\active
        \catcode\count@ 12
      \fi\fi
      \advance\count@ \@ne
  \repeat
  \loop
    \endlinechar\m@ne
    \read\@ne to \curline
    \endlinechar`~~M
    \ifx\curline\empty % ignore empty lines
    \else
      \expandafter\processline\curline\lineend
    \fi
    \ifeof\@ne \morefalse \fi
    \ifmore
  \repeat
\endgroup}
%    \end{macrocode}
%    \end{macro}
%
%    And here is the place where the file is read.  But this is
%    only done if the user has asked for it.
%    \begin{macrocode}
\ifnum\convtype=\tw@
  \openin\@ne convert.tbl\relax
  \ifeof\@ne
     \typeout{I can't find file `convert.tbl' -- ignored.}%
  \else
    \readfile
    \closein\@ne
  \fi
\fi
%    \end{macrocode}
%
%    And the last action: we define macros for characters with special
%    functions.  These are `line feed', `carriage return' and `tab'.
%    \begin{macrocode}
\letout{9}\expand@tab
\defout{10}{%
  \typeout{!!--> Found ^J in line \the\lineno--I'll ignore all ^J.}%
  \letout{10}\relax}
\defout{13}{%
  \typeout{!!--> Found ^M in line \the\lineno--I'll ignore all ^M.}%
  \letout{13}\relax}
%    \end{macrocode}
%
%    \subsection{Convert the file}
%
%    Now after the character translation is set up we have to make
%    all characters active.  We read the file line by line with TeX's
%    \verb+\read+ primitive.  (If we would \verb+\input+ the file we
%    have to set \verb+\endlinechar+ to signal the end-of-line.)
%
%    \begin{macrocode}
\def\readfile{%
  \read\@ne to\next
  \ifeof\@ne
  \else
    \advance\lineno\@ne
    \next\relax
    \finish@line
    \expandafter\readfile
  \fi
}
%    \end{macrocode}
%
%    \begin{macrocode}
\def\makeactiveandinput{\begingroup
  \count@=\z@
  \loop
    \ifnum\count@<\@cclvi
      \catcode\count@\active
      \advance\count@\@ne
  \repeat
%    \end{macrocode}
%    While reading from the file it is useful to switch off the
%    recognition of the end-of-line character.  This saves us stripping
%    off spaces from the contents of the line.
%    \begin{macrocode}
  \endlinechar=-1
  \openin\@ne \infilename\relax
  \ifeof\@ne
    \typeout{No input file \infilename.}%
  \else
    \lineno\z@
    \readfile
    \closein\@ne
  \fi
  \endlinechar=`~~M
  \ifx\outfilename\empty
    \typeout{------------------------------------------------------------}
  \else
    \immediate\closeout\outstream
  \fi
  \endgroup
  \endofjob
}
%    \end{macrocode}
%    The last task: call the macro. 
%    \begin{macrocode}
\makeactiveandinput
%    \end{macrocode}
%
%    Normally we should never reach this point.
%    \begin{macrocode}
\errhelp{This TeX utility should not reach this point.}%
\errmessage{Internal Error 2. -- Please fix me!}
\endinput
%    \end{macrocode}
% \Finale
\endinput