% This is the cweb file cef5conv.w of the CJK Package Ver. 2.5  10-Apr-1995
%
% To print this CWEB file you should (but not must) use the CWEAVE of the
% c2cweb-package (found at the CTAN archives, e.g. pip.shsu.edu) and then say
%
%           cweave +a cef5conv.w
%
% This (fully compatible) CWEAVE can transform CWEB-files with alternative
% output rules (look at the position of braces below!) the author (it's me
% too :-) prefer. Otherwise this file will be formatted traditionally.

\def\title{cef5conv (CJK Version 2.5)}

\def\topofcontents{
  \null\vfill
  \centerline{\titlefont The {\ttitlefont cef5conv} program}
  \vskip 20pt
  \centerline{(CJK Version 2.5)}
  \vfill}

\def\botofcontents{
  \vfill
  \noindent
  Copyright \copyright\ 1995 by Werner Lemberg
  \bigskip\noindent
  Permission is granted to make and distribute verbatim copies of this
  document provided that the copyright notice and this permission notice
  are preserved on all copies.

  \smallskip\noindent
  Permission is granted to copy and distribute modified versions of this
  document under the conditions for verbatim copying, provided that the
  entire resulting derived work is distributed under the terms of a
  permission notice identical to this one.}

\pageno=\contentspagenumber \advance\pageno by 1
\let\maybe=\iftrue
\fullpageheight=240mm
\pageheight=223mm
\pagewidth=158mm
\setpage
\frenchspacing

\noinx
\nosecs
\nocon

@* Function and Use.

This small program will convert CNS encoded Chinese using the \\{Chinese
Encoding Framework (CEF)} in the same way \.{CEF5conv.tex} will do. The need
of this program arises from the fact that the standard \TeX--distribution
under \UNIX/, \.{web2c}, will not \.{\\write} out characters |>= 0x80| but
uses the \.{\char94\char94xx} form instead.

Use this program as a filter:

\hskip 2em \.{cef5conv < input\_file > output\_file}


@* The program.
In contrast to \.{cefconv} two tasks will be executed:

Replacing all occurrences of Big 5 encoded characters \.{XY} (\.{X} and \.{Y}
are the first and the second byte of the character) with \.{XZZZ.}, where
\.{ZZZ.} represents the second byte as a decimal number followed by a dot.

Replacing CEF macros of the form \.{\&Cx-yyzz;} (\.{x} stands for the plane, a
number from 1 up to 7; \.{yyzz} is a hexadecimal representation of the code
point in this plane using the Graphic Left form, i.e.~characters in the range
|0x21-0x7E|) with \.{Xyy."0zz.}; \.{X} is \.{x} with the 8th bit set (i.e.
|0x81-0x87|).

Plane 0 is used for Big~5 encoded characters.

Additionally we add two lines defining \TeX\ macros at the very beginning to
signal a preprocessed file.

The following code is very simple. No error detection is done because \TeX\
which will see the output of \.{cef5conv} complains loudly if something is
wrong.


@c
#include <ctype.h>
#include <stdio.h>@#


int main(argc, argv)
  int argc;
  char *argv[];

   {int ch, i;
    unsigned char s[8];
    unsigned char *p;

    fprintf(stdout, "\\def\\CNSpreproc{}\n");
    fprintf(stdout, "\\def\\CJKpreproc{}\n");@#

    ch = fgetc(stdin);@#

    while(!feof(stdin))
       {if(ch >= 0xA1 && ch <= 0xFE)
           {fputc(ch, stdout);@#

            ch = fgetc(stdin);
            if(!feof(stdin))
                fprintf(stdout, "%d.", ch);
           }
        else if(ch == '&')
                       /* the macro test is hardcoded to make things simple */
           {i = 0;
            p = s;
            *p = fgetc(stdin);@#

            if(*p == 'C' && !feof(stdin))
               {i++;
                p++;
                *p = fgetc(stdin);
                if(*p >= '0' && *p <= '7' && !feof(stdin))
                   {i++;
                    p++;
                    *p = fgetc(stdin);
                    if(*p == '-' && !feof(stdin))
                       {i++;
                        p++;
                        *p = fgetc(stdin);
                        if(*p >= '2' && *p <= '7' && !feof(stdin))
                           {i++;
                            p++;
                            *p = fgetc(stdin);
                            if(isxdigit(*p) && *p < 0x80 && !feof(stdin))
                               {i++;
                                p++;
                                *p = fgetc(stdin);
                                if(*p >= '2' && *p <= '7' && !feof(stdin))
                                   {i++;
                                    p++;
                                    *p = fgetc(stdin);
                                    if(isxdigit(*p) && *p < 0x80 &&
                                       !feof(stdin))
                                       {i++;
                                        p++;
                                        *p = fgetc(stdin);
                                        if(*p == ';' && !feof(stdin))
                                           {fprintf(stdout,
                                                    "%c%c%c.\"0%c%c.",@/
                                                    s[1] - '0' + 0x80,
                                                    s[3], toupper(s[4]),
                                                    s[5], toupper(s[6]));@#

                                            ch = fgetc(stdin);
                                            continue;
                                           }
                                       }
                                   }
                               }
                           }
                       }
                   }
               }@#

            ch = *p;@#

            fputc('&', stdout);
            p = s;
            while(i--)
                fputc(*(p++), stdout);
            continue;
           }
        else
            fputc(ch, stdout);@#

        ch = fgetc(stdin);
       }
   }
