% kb2lex.tex                               An idea of Bernard Gaulle 92/19/07
%                                                          V3.1 of 2006/11/06
%                                     Copyright Bernard Gaulle as in french.doc
%
% Running this code will produce two files: kbtex7a8.lex and kbtex8a7.lex
% These files are the result of the understanding of xxx.kbc in which
% the user has specified the special 8-bit characters he is using via
% his own keyboard. "xxx" is generally provided by the file keyboard.dat. 
% A document typed in with this encoding can't be send
% to anyone else except people having exactly the same keyboard.
% kb2lex is a way to produce programs that will allow to translate .tex
% files from 8-bit to single 7-bit ASCII standard that everyone normally
% is able to use.
%
% Of course you need the (Unix) pgm "lex" to generate the appropriate pgms.
% But if you don't have it, I've given in this directory kbtex7a8.c and
% kb8a7.c that were produced on my machine with the default keyboard.dat.
%
%  1- Configurate your keyboard.dat to your input encoding ("xxx").
%     You can create your own keyboard.dat and even define new a xxx.kbc file.
%  2- TeX or LaTeX this file (with any TeX V3.14xxx engine)
%     if not OK go back to 1- for corrections (notice that it must run
%                                            with the distributed keyboard.dat)
%     All processed characters are displayed to your screen (and in the .log 
%     file). Check if no character is displayed in TeX hexadecimal format 
%     (^^xx). In which case, you must be aware that these will never be 
%     converted to 7-bit. Since you can't display it as usual on your screen
%     and probably you can't also input it asis, you should  chose to delete
%     them from  xxx.kbc. Do it and rerun this step.
%  3- Type in: 
%              (as i type in Unix but may need to be adapted...)
%              lex -v kb8to7.lex;cc -o kb8to7 lex.yy.c -ll
%         and: lex -v kb7to8.lex;cc -o kb7to8 lex.yy.c -ll;rm lex.yy.c
%     (if any pb on your platform try flex instead of lex)
%  4- Test kb7to8 and kb8to7 to your modified kbto.tex file:
%              kb7to8 <kbto.tex
%              kb8to7 <kbto.tex
%  5- Copy the pgms kb8to7 and kb7to8 
%     in a public (/local/bin) directory so they will be available for 
%     everybody.
%
%%
%%      checksum        = "36341 141 732 6638"
%%
\def\languagename{french}% to allow loading french.*.kbc/french.keyboard.dat 
\global\newlinechar`\^^J%
\newif\ifLEX\LEXtrue
\let\ifLEXhic\iftrue% for any debugging
\newwrite\kbseven\immediate\openout\kbseven=kb8to7.lex
\newwrite\kbeight\immediate\openout\kbeight=kb7to8.lex
{\catcode`\%=11
\immediate\write\kbseven{%% /* kb8to7.lex converts my 8-bit TeX documents 
                               to a standard 7-bit ASCII */}
\immediate\write\kbeight{%% /* kb7to8.lex converts std 7-bit ASCII TeX
                               documents to my prefered 8-bit */} 
}
\let\protect\empty% nothing need to be protect in the output
                             \def\tempfmt{lplain}%
                             \ifx\fmtname\tempfmt\else\def\tempfmt{LaTeX2e}\fi%
\catcode`\@=11
                             \ifx\fmtname\tempfmt% case LaTeX
\documentclass[titlepage]{article}
\IfFileExists{msg-msg.tex}{%
\usepackage[tracefiles]{msg}%
                         }{\let\kbAissue\relax}%
%\usepackage[T1]{fontenc}% L'encodage ne devrait rien changer.
%\usepackage[frenchle]{babel}% Will enter in an infinite loop!
%\usepackage{keyboard}% see below
\nofiles
\begin{document} 
   \let\messag@\@gobble%
% For debugging one can remove "msg" access, just uncomment
%\let\kbAissue\relax% this line.
   \ifx\kbAissue\undefined%
% Firstly we add the material to use the "msg" package for localization.
% (but don't use "msg" inside "msg").
      \def\kb@issue#1#2{\kb@issue@[#1]#2\void}% The local \issuemsg macro.
                                      % which will call the real one;
                                      % #1 is the macro message required.
                                      % #2 is the message header + msg number
                                      %    such as "^^J -234-", just message 
                                      %    number (234) is kept. 
      \def\kb@issue@[#1]#2-#3-#4\void{\issuemsg[#1]#3(keyboard)}%
      \let\kbAissue\kb@issue%
   \else\let\kbAissue\relax\let\messag@\message% Avoid second level loop.
%%%        \@tempc%
   \fi%
                              \else% This is Plain
                                   \let\kbAissue\relax\let\messag@\message%
                              \fi% LaTeX case
\kbAissue%
\message{^^J -40- Writing kb8to7.lex and kb7to8.lex}
\ifx\@@input\undefined\let\@@input\input\fi%
% (Re)define \kbencoding to force loading of kbconfig.tex
\def\kbencoding#1{\let\@prevkbencoding\relax%
                  \def\@kbencoding{#1}\@@input kbconfig.tex %
                      }%
\catcode`@=11% 
%%%% Check if we can output 8bit without translation to ^^codes
\def\@tempc{\ifEightBitOutput\else%
\kbAissue%
\message{^^J -47- i must stop because your TeX engine is unable to 
         generate 8bit output codes, sorry!}
\fi%
\stop}
\ifx\EightBitOuputfalse\undefined\else\expandafter\@tempc\fi%
\global\let\@tempc=\undefined% so if someone use it...
%
\let\kbAissueORI\kbAissue%
\input keyboard.dat % load kbconfig directly.
\let\ifLEX\iffalse%
\let\kbAissue\kbAissueORI%
\let\@kb@msgXXIX\relax%
\kbAissue%
\message{-33- CHECK the list for hexadecimal TeX codes you can't display}
\messag@{     and decide if they must be eliminated from \@kbencoding.kbc}
\messag@{     assuming your ''locale'' settings are okay on your system.}
% Thanks to Denis Barbier for the following (thus removing main.patchlex):
\begingroup\catcode`\%=11
\catcode`\{=11 \catcode`\}=11
\catcode`\[=1 \catcode`\]=2
\catcode`\#=12
\immediate\write\kbseven[%%]
\immediate\write\kbseven[#ifndef yywrap]
\immediate\write\kbseven[int yywrap () { return 1; }]
\immediate\write\kbseven[#endif]
\immediate\write\kbseven[int main() { return yylex(); }]
\immediate\write\kbeight[%%]
\immediate\write\kbeight[#ifndef yywrap]
\immediate\write\kbeight[int yywrap () { return 1; }]
\immediate\write\kbeight[#endif]
\immediate\write\kbeight[int main() { return yylex(); }]
\endgroup
%
\gdef\folio{}%
                                        \ifx\fmtname\tempfmt\def\tempa
{\end{document}}% LaTeX end
                                        \else\def\tempa{\csname bye\endcsname}
                                        \fi\tempa%
\endinput%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
