Debian Codegraph @

doc/recode.texi

raw
1 \input texinfo @c -*-texinfo-*- -*- coding: latin-1 -*-
2 @c %**start of header
3 @setfilename recode.info
4 @settitle The Recode reference manual
5
6 @c An index for command-line options
7 @defcodeindex op
8 @c Put variable and function names together
9 @syncodeindex vr fn
10 @finalout
11 @c %**end of header
12
13 @include version.texi
14
15 @dircategory Internationalization and character sets
16 @direntry
17 * recode: (recode). Conversion between character sets and surfaces.
18 @end direntry
19
20 @ifinfo
21 This file documents the Recode program and library, which has the purpose of
22 converting files between various character sets and surfaces.
23
24 Copyright (C) 1990--2023 Free Software Foundation, Inc.
25
26 Permission is granted to make and distribute verbatim copies of
27 this manual provided the copyright notice and this permission notice
28 are preserved on all copies.
29
30 @ignore
31 Permission is granted to process this file through TeX and print the
32 results, provided the printed document carries copying permission
33 notice identical to this one except for the removal of this paragraph
34 (this paragraph not being relevant to the printed manual).
35
36 @end ignore
37 Permission is granted to copy and distribute modified versions of this
38 manual under the conditions for verbatim copying, provided that the entire
39 resulting derived work is distributed under the terms of a permission
40 notice identical to this one.
41
42 Permission is granted to copy and distribute translations of this manual
43 into another language, under the above conditions for modified versions,
44 except that this permission notice may be stated in a translation approved
45 by the Foundation.
46 @end ifinfo
47
48 @titlepage
49 @title Recode, version @value{VERSION}
50 @subtitle The character set converter
51 @subtitle Edition @value{EDITION}, @value{UPDATED}
52 @author Fran@,{c}ois Pinard
53
54 @page
55 @vskip 0pt plus 1filll
56 Copyright @copyright{} 1993--2018 Free Software Foundation, Inc.
57
58 Permission is granted to make and distribute verbatim copies of
59 this manual provided the copyright notice and this permission notice
60 are preserved on all copies.
61
62 Permission is granted to copy and distribute modified versions of this
63 manual under the conditions for verbatim copying, provided that the entire
64 resulting derived work is distributed under the terms of a permission
65 notice identical to this one.
66
67 Permission is granted to copy and distribute translations of this manual
68 into another language, under the above conditions for modified versions,
69 except that this permission notice may be stated in a translation approved
70 by the Foundation.
71 @end titlepage
72
73 @ifnottex
74 @node Top, Tutorial, (dir), (dir)
75 @top Recode
76
77 @c @item @b{Recode} @value{hfillkludge} (UtilT, SrcCD)
78 @c
79 This recoding library converts files between various coded character
80 sets and surface encodings. When this cannot be achieved exactly, it
81 may get rid of the offending characters or fall back on approximations.
82 The library recognises or produces more than 300 different character sets
83 and is able to convert files between almost any pair. Most @w{RFC 1345}
84 character sets, and all character sets from a pre-installed @code{iconv}
85 library, are supported.
86 The @code{recode} program is a handy front-end to the library.
87
88 This manual documents Recode @value{VERSION}.
89
90 @menu
91 * Tutorial:: Quick Tutorial
92 * Introduction:: Terminology and purpose
93 * Invoking recode:: How to use this program
94 * Library:: A recoding library
95 * Universal:: The universal charset
96 * iconv:: The @code{iconv} library
97 * Tabular:: Tabular sources (@w{RFC 1345})
98 * ASCII misc:: ASCII and some derivatives
99 * IBM and MS:: Some IBM or Microsoft charsets
100 * CDC:: Charsets for CDC machines
101 * Micros:: Other micro-computer charsets
102 * Miscellaneous:: Various other charsets
103 * Surfaces:: All about surfaces
104 * Internals:: Internal aspects
105 * Concept Index:: Concept Index
106 * Option Index:: Option Index
107 * Library Index:: Library Index
108 * Charset and Surface Index:: Charset and Surface Index
109
110 @detailmenu
111 --- The Detailed Node Listing ---
112
113 Terminology and purpose
114
115 * Charset overview:: Overview of charsets
116 * Surface overview:: Overview of surfaces
117 * Contributing:: Contributions and bug reports
118
119 How to use this program
120
121 * Synopsis:: Synopsis of @code{recode} call
122 * Requests:: The @var{request} parameter
123 * Listings:: Asking for various lists
124 * Recoding:: Controlling how files are recoded
125 * Reversibility:: Reversibility issues
126 * Sequencing:: Selecting sequencing methods
127 * Mixed:: Using mixed charset input
128 * Emacs:: Using Recode within Emacs
129 * Debugging:: Debugging considerations
130
131 A recoding library
132
133 * Outer level:: Outer level functions
134 * Request level:: Request level functions
135 * Task level:: Task level functions
136 * Charset level:: Charset level functions
137 * Errors:: Handling errors
138
139 The universal charset
140
141 * UCS-2:: Universal Character Set, 2 bytes
142 * UCS-4:: Universal Character Set, 4 bytes
143 * UTF-7:: Universal Transformation Format, 7 bits
144 * UTF-8:: Universal Transformation Format, 8 bits
145 * UTF-16:: Universal Transformation Format, 16 bits
146 * count-characters:: Frequency count of characters
147 * dump-with-names:: Fully interpreted UCS dump
148
149 ASCII and some derivatives
150
151 * ASCII:: Usual ASCII
152 * ISO 8859:: ASCII extended by Latin Alphabets
153 * ASCII-BS:: ASCII 7-bits, @kbd{BS} to overstrike
154 * flat:: ASCII without diacritics nor underline
155
156 Some IBM or Microsoft charsets
157
158 * EBCDIC:: EBCDIC codes
159 * IBM-PC:: IBM's PC code
160 * Icon-QNX:: Unisys' Icon code
161
162 Charsets for CDC machines
163
164 * Display Code:: Control Data's Display Code
165 * CDC-NOS:: ASCII 6/12 from NOS
166 * Bang-Bang:: ASCII ``bang bang''
167
168 Other micro-computer charsets
169
170 * Mac OS:: Apple's Macintosh code
171 * AtariST:: Atari ST code
172
173 Various other charsets
174
175 * HTML:: World Wide Web representations
176 * LaTeX:: LaTeX macro calls
177 * BibTeX:: BibTeX macro calls
178 * Texinfo:: GNU project documentation files
179 * ISO 5426 and ANSEL:: Bibliographic character sets
180 * Vietnamese:: Vietnamese charsets
181 * African:: African charsets
182 * Others:: Cyrillic and other charsets
183 * Java:: Java code
184 * Texte:: Easy French conventions
185 * Mule:: Mule as a multiplexed charset
186
187 All about surfaces
188
189 * Permutations:: Permuting groups of bytes
190 * End lines:: Representation for end of lines
191 * MIME:: MIME contents encodings
192 * Dump:: Interpreted character dumps
193 * Test:: Artificial data for testing
194
195 Internal aspects
196
197 * Main flow:: Overall organisation
198 * New charsets:: Adding new charsets
199 * New surfaces:: Adding new surfaces
200 * Design:: Comments on the library design
201
202 @end detailmenu
203 @end menu
204
205 @end ifnottex
206
207 @node Tutorial, Introduction, Top, Top
208 @chapter Quick Tutorial
209
210 @cindex Recode use, a tutorial
211 @cindex tutorial
212 So, really, you just are in a hurry to use Recode, and do not
213 feel like studying this manual? Even reading this paragraph slows you down?
214 We might have a problem, as you will have to do some guess work, and might
215 not become very proficient unless you have a very solid intuition@dots{}.
216
217 Let me use here, as a quick tutorial, an actual reply of mine to a
218 Recode user, who writes:
219
220 @quotation
221 My situation is this---I occasionally get email with special characters
222 in it. Sometimes this mail is from a user using IBM software and sometimes
223 it is a user using Mac software. I myself am on a SPARC Solaris machine.
224 @end quotation
225
226 Your situation is similar to mine, except that I @emph{often} receive
227 email needing recoding, that is, much more than @emph{occasionally}!
228 The usual recodings I do are Mac to @w{Latin-1}, IBM page codes to @w{Latin-1},
229 Easy-French to @w{Latin-1}, remove Quoted-Printable, remove Base64. These are
230 so frequent that I made myself a few two-keystroke Emacs commands to filter
231 the Emacs region. This is very convenient for me. I also resort to many
232 other email conversions, yet more rarely than the frequent cases above.
233
234 @quotation
235 It @emph{seems} like this should be doable using Recode. However,
236 when I try something like @samp{recode mac macfile.txt} I get nothing
237 out---no error, no output, nothing.
238 @end quotation
239
240 @strong{Note:} For the following discussion to be true, you should have
241 something like @samp{export LANG=fr_FR.ISO-8859-1} in your environment,
242 the important bit here being the specification of an preferred charset.
243
244 Presuming you are using some recent version of Recode, the command:
245
246 @example
247 recode mac macfile.txt
248 @end example
249
250 @noindent
251 is a request for recoding @file{macfile.txt} over itself, overwriting the
252 original, from Macintosh usual character code and Macintosh end of lines,
253 to @w{Latin-1} and Unix end of lines. This is overwrite mode. If you want
254 to use Recode as a filter, which is probably what you need, rather do:
255
256 @example
257 recode mac
258 @end example
259
260 @noindent
261 and give your Macintosh file as standard input, you'll get the @w{Latin-1}
262 file on standard output. The above command is an abbreviation for any of:
263
264 @example
265 recode mac..
266 recode mac..l1
267 recode mac..Latin-1
268 recode mac/CR..Latin-1/
269 recode Macintosh..ISO_8859-1
270 recode Macintosh/CR..ISO_8859-1/
271 @end example
272
273 That is, a @code{CR} surface, encoding newlines with ASCII @key{CR}, is
274 first to be removed (this is a default surface for @samp{mac}), then the
275 Macintosh charset is converted to @w{Latin-1} and no surface is added to the
276 result (there is no default surface for @samp{l1}). If you want @samp{mac}
277 code converted, but you know that newlines are already coded the Unix way,
278 just do:
279
280 @example
281 recode mac/
282 @end example
283
284 @noindent
285 the slash then overriding the default surface with empty, that is, none.
286 Here are other easy recipes:
287
288 @example
289 recode pc to filter IBM-PC code and CR-LF (default) to Latin-1
290 recode pc/ to filter IBM-PC code to Latin-1
291 recode 850 to filter code page 850 and CR-LF (default) to Latin-1
292 recode 850/ to filter code page 850 to Latin-1
293 recode /qp to remove quoted printable
294 @end example
295
296 The last one is indeed equivalent to any of:
297
298 @example
299 recode /qp..
300 recode l1/qp..l1/
301 recode ISO_8859-1/Quoted-Printable..ISO_8859-1/
302 @end example
303
304 Here are some reverse recipes:
305
306 @example
307 recode ..mac to filter Latin-1 to Macintosh code and CR (default)
308 recode ..mac/ to filter Latin-1 to Macintosh code
309 recode ..pc to filter Latin-1 to IBM-PC code and CR-LF (default)
310 recode ..pc/ to filter Latin-1 to IBM-PC code
311 recode ..850 to filter Latin-1 to code page 850 and CR-LF (default)
312 recode ..850/ to filter Latin-1 to code page 850
313 recode ../qp to force quoted printable
314 @end example
315
316 In all the above calls, replace @samp{recode} by @samp{recode -f} if you
317 want to proceed despite recoding errors. If you do not use @samp{-f}
318 and there is an error, the recoding output will be interrupted after first
319 error in filter mode, or the file will not be replaced by a recoded copy
320 in overwrite mode.
321
322 You may use @samp{recode -l} to get a list of available charsets and
323 surfaces, and @samp{recode --help} to get a quick summary of options.
324 The above output is meant for those having already read this manual, so
325 let me dare a suggestion: why could not you find a few more minutes in
326 your schedule to peek further down, right into the following chapters!
327
328 @node Introduction, Invoking recode, Tutorial, Top
329 @chapter Terminology and purpose
330
331 A few terms are used over and over in this manual, our wise reader will
332 learn their meaning right away. Both ISO (International Organization for
333 Standardisation) and IETF (Internet Engineering Task Force) have their
334 own terminology, this document does not try to stick to either one in a
335 strict way, while it does not want to throw more confusion in the field.
336 On the other hand, it would not be efficient using paraphrases all the time,
337 so Recode coins a few short words, which are explained below.
338
339 @cindex charset, what it is
340 A @dfn{charset}, in the context of Recode, is a particular association
341 between computer codes on one side, and a repertoire of intended characters
342 on the other side. Codes are usually taken from a set of consecutive
343 small integers, starting at 0. Some characters have a graphical appearance
344 (glyph) or displayable effect, others have special uses like, for example,
345 to control devices or to interact with neighbouring codes to specify them
346 more precisely. So, a @emph{charset} is roughly one of those tables,
347 giving a meaning to each of the codes from the set of allowable values.
348 MIME also uses the term charset with approximately the same meaning.
349 It does @emph{not} exactly corresponds to what ISO calls a @dfn{coded
350 character set}, that is, a set of characters with an encoding for them.
351 An coded character set does not necessarily use all available code positions,
352 while a MIME charset usually tries to specify them all. A MIME charset
353 might be the union of a few disjoint coded character sets.
354
355 @cindex surface, what it is
356 A @dfn{surface} is a term used in Recode only, and is a short for
357 surface transformation of a charset stream. This is any kind of mapping,
358 usually reversible, which associates physical bits in some medium for
359 a stream of characters taken from one or more charsets (usually one).
360 A surface is a kind of varnish added over a charset so it fits in actual
361 bits and bytes. How end of lines are exactly encoded is not really
362 pertinent to the charset, and so, there is surface for end of lines.
363 @code{Base64} is also a surface, as we may encode any charset in it.
364 Other examples would @code{DES} enciphering, or @code{gzip} compression
365 (even if Recode does not offer them currently): these are ways to give
366 a real life to theoretical charsets. The @dfn{trivial} surface consists
367 into putting characters into fixed width little chunks of bits, usually
368 eight such bits per character. But things are not always that simple.
369
370 This Recode library, and the program by that name, have the purpose
371 of converting files between various charsets and surfaces. When this
372 cannot be done in exact ways, as it is often the case, the program may
373 get rid of the offending characters or fall back on approximations.
374 This library recognises or produces around 175 such charsets under 500
375 names, and handle a dozen surfaces. Since it can convert each charset to
376 almost any other one, many thousands of different conversions are possible.
377
378 The Recode program and library do not usually know how to split and
379 sort out textual and non-textual information which may be mixed in a single
380 input file. For example, there is no surface which currently addresses the
381 problem of how lines are blocked into physical records, when the blocking
382 information is added as binary markers or counters within files. So,
383 Recode should be given textual streams which are rather @emph{pure}.
384
385 This tool pays special attention to superimposition of diacritics for
386 some French representations. This orientation is mostly historical, it
387 does not impair the usefulness, generality or extensibility of the program.
388 @samp{recode} is both a French and English word. For those who pay attention
389 to those things, the proper pronunciation is French (that is, @samp{racud},
390 with @samp{a} like in @samp{above}, and @samp{u} like in @samp{cut}).
391
392 The Recode program and library has been written by Fran@,{c}ois Pinard.
393 With time, it got to reuse works from other contributors, and notably,
394 those of Keld Simonsen and Bruno Haible.
395
396 @menu
397 * Charset overview:: Overview of charsets
398 * Surface overview:: Overview of surfaces
399 * Contributing:: Contributions and bug reports
400 @end menu
401
402 @node Charset overview, Surface overview, Introduction, Introduction
403 @section Overview of charsets
404
405 @cindex charsets, overview
406 Recoding is currently possible between many charsets, the bulk of which
407 is described by @w{RFC 1345} tables or available in a pre-installed
408 external @code{iconv} library. @xref{Tabular}, and
409 @pxref{iconv}@footnote{Because @code{iconv} can vary from system to
410 system, and is itself a complex tool, it can cause recode to behave in
411 unexpected ways. Therefore, by default it is only used when a conversion
412 would not be possible without it. To request that @code{iconv} be used,
413 use @code{--prefer-iconv}; see @xref{prefer-iconv}. Conversely, you can
414 disable it with the @code{-x:} option; see @xref{disable-iconv}.}. The
415 Recode library also handles some charsets in some specialised ways.
416 These are:
417
418 @itemize @bullet
419 @item
420 6-bit charsets based on CDC display code: 6/12 code from NOS; bang-bang
421 code from Universit@'e de Montr@'eal;
422
423 @item
424 7-bit ASCII: without any diacritics, or else: using backspace for
425 overstriking; Unisys' Icon convention; @TeX{}/La@TeX{} coding; easy
426 French conventions for electronic mail;
427
428 @item
429 8-bit extensions to ASCII: ISO @w{Latin-1}, Atari ST code, IBM's code for
430 the PC, Apple's code for the Macintosh, ISO 5426 and ANSEL library codes;
431
432 @item
433 8-bit non-ASCII codes: three flavours of EBCDIC;
434
435 @item
436 16-bit or 31-bit universal characters, and their transfer encodings.
437 @end itemize
438
439 The introduction of @w{RFC 1345} in Recode has brought with it a few
440 charsets having the functionality of older ones, but yet being different
441 in subtle ways. The effects have not been fully investigated yet, so for
442 now, clashes are avoided, the old and new charsets are kept well separate.
443
444 @cindex unavailable conversions
445 @cindex conversions, unavailable
446 @cindex impossible conversions
447 @cindex unreachable charsets
448 @cindex exceptions to available conversions
449 @cindex pseudo-charsets
450 @tindex flat@r{, not as before charset}
451 @tindex count-characters@r{, not as before charset}
452 @tindex dump-with-names@r{, not as before charset}
453 @tindex data@r{, not with charsets}
454 @tindex iconv@r{, not in requests}
455 Conversion is possible between almost any pair of charsets. Here is a
456 list of the exceptions. One may not recode @emph{from} the @code{flat},
457 @code{count-characters} or @code{dump-with-names} charsets, nor @emph{from}
458 or @emph{to} the @code{data} or @code{:iconv:} charsets.
459 Also, if we except the @code{data} pseudo-charset, charsets
460 and surfaces live in disjoint recoding spaces, one cannot really transform
461 a surface into a charset or vice-versa, as surfaces are only meant to be
462 applied over charsets, or removed from them.
463
464 @node Surface overview, Contributing, Charset overview, Introduction
465 @section Overview of surfaces
466
467 @cindex surfaces, overview
468 For various practical considerations, it sometimes happens that the codes
469 making up a text, written in a particular charset, cannot simply be put
470 out in a file one after another without creating problems or breaking
471 other things. Sometimes, 8-bit codes cannot be written on a 7-bit medium,
472 variable length codes need kind of envelopes, newlines require special
473 treatment, etc. We sometimes have to apply @dfn{surfaces} to a stream
474 of codes, which surfaces are kind of tricks used to fit the charset into
475 those practical constraints. Moreover, similar surfaces or tricks may
476 be useful for many unrelated charsets, and many surfaces can be used at
477 once over a single charset.
478
479 @cindex pure charset
480 @cindex charset, pure
481 So, Recode has machinery to describe a combination of a charset with
482 surfaces used over it in a file. We would use the expression @dfn{pure
483 charset} for referring to a charset free of any surface, that is, the
484 conceptual association between integer codes and character intents.
485
486 It is not always clear if some transformation will yield a charset or a
487 surface, especially for those transformations which are only meaningful
488 over a single charset. The Recode library is not overly picky as
489 identifying surfaces as such: when it is practical to consider a specialised
490 surface as if it were a charset, this is preferred, and done.
491
492 @node Contributing, , Surface overview, Introduction
493 @section Contributions and bug reports
494
495 @cindex contributing charsets
496 Even being the Recode author and current maintainer, I am no
497 specialist in charset standards. I only made Recode along the
498 years to solve my own needs, but felt it was applicable for the needs
499 of others. Some FSF people liked the program structure and suggested
500 to make it more widely available. I often rely on Recode users
501 suggestions to decide what is best to be done next.
502
503 Properly protecting Recode about possible copyright fights is a
504 pain for me and for contributors, but we cannot avoid addressing the issue
505 in the long run. Besides, the Free Software Foundation, which mandates
506 the GNU project, is very sensible to this matter. GNU standards suggest
507 that we stay cautious before looking at copyrighted code. The safest and
508 simplest way for me is to gather ideas and reprogram them anew, even if
509 this might slow me down considerably. For contributions going beyond a
510 few lines of code here and there, the FSF definitely requires employer
511 disclaimers and copyright assignments in writing.
512
513 When you contribute something to Recode, @emph{please} explain what
514 it is about. Do not take for granted that I know those charsets which
515 are familiar to you. Once again, I'm no expert, and you have to help me.
516 Your explanations could well find their way into this documentation, too.
517 Also, for contributing new charsets or new surfaces, as much as possible,
518 please provide good, solid, verifiable references for the tables you
519 used@footnote{I'm not prone to accept a charset you just invented,
520 and which nobody uses yet: convince your friends and community first!}.
521
522 Many users contributed to Recode already, I am grateful to them for
523 their interest and involvement. Some suggestions can be integrated quickly
524 while some others have to be delayed, I have to draw a line somewhere when
525 time comes to make a new release, about what would go in it and what would
526 go in the next.
527
528 @cindex bug reports, where to send
529 @cindex reporting bugs
530 Please report suggestions, documentation errors and bugs at
531 @uref{https://github.com/rrthomas/recode}. Do not be afraid
532 to report details, because this program is the mere aggregation of
533 hundreds of details.
534
535 @node Invoking recode, Library, Introduction, Top
536 @chapter How to use this program
537
538 With the synopsis of the @code{recode} call, we stress the difference
539 between using this program as a file filter, or recoding many files
540 at once. The first parameter of any call states the recoding request,
541 and this deserves a section on its own. Options are then presented,
542 but somewhat grouped according to the related functionalities they
543 control.
544
545 @menu
546 * Synopsis:: Synopsis of @code{recode} call
547 * Requests:: The @var{request} parameter
548 * Listings:: Asking for various lists
549 * Recoding:: Controlling how files are recoded
550 * Reversibility:: Reversibility issues
551 * Sequencing:: Selecting sequencing methods
552 * Mixed:: Using mixed charset input
553 * Emacs:: Using Recode within Emacs
554 * Debugging:: Debugging considerations
555 @end menu
556
557 @node Synopsis, Requests, Invoking recode, Invoking recode
558 @section Synopsis of @code{recode} call
559
560 @cindex @code{recode}, synopsis of invocation
561 @cindex invocation of @code{recode}, synopsis
562 The general format of the program call is one of:
563
564 @example
565 recode [@var{option}]@dots{} [@var{charset} | @var{request} [@var{file}]@dots{} ]
566 @end example
567
568 Some calls are used only to obtain lists produced by Recode itself,
569 without actually recoding any file. They are recognised through the
570 usage of listing options, and these options decide what meaning should
571 be given to an optional @var{charset} parameter. @xref{Listings}.
572
573 In other calls, the first parameter (@var{request}) always explains which
574 transformations are expected on the files. There are many variations to
575 the aspect of this parameter. We will discuss more complex situations
576 later (@pxref{Requests}), but for many simple cases, this parameter
577 merely looks like this@footnote{In previous versions of Recode, a single
578 colon @samp{:} was used instead of the two dots @samp{..} for separating
579 charsets, but this created problems, because colons are allowed in
580 official charset names.}:
581
582 @example
583 @var{before}..@var{after}
584 @end example
585
586 @noindent
587 where @var{before} and @var{after} each gives the name of a charset. Each
588 @var{file} will be read assuming it is coded with charset @var{before}, it
589 will be recoded over itself so to use the charset @var{after}. If there
590 is no @var{file} on the @code{recode} command, the program rather acts
591 as a Unix filter and transforms standard input onto standard output.
592 @cindex filter operation
593 @cindex @code{recode}, operation as filter
594
595 The capability of recoding many files at once is very convenient.
596 For example, one could easily prepare a distribution from @w{Latin-1} to MSDOS,
597 this way:
598
599 @example
600 mkdir package
601 cp -p Makefile *.[ch] package
602 recode Latin-1..MSDOS package/*
603 zoo ah package.zoo package/*
604 rm -rf package
605 @end example
606
607 @noindent
608 (In this example, the non-mandatory @samp{-p} option to @code{cp} is for
609 preserving timestamps, and the @code{zoo} program is an archiver from
610 Rahul Dhesi which once was quite popular.)
611
612 The filter operation is especially useful when the input files should
613 not be altered. Let us make an example to illustrate this point.
614 Suppose that someone has a file named @file{datum.txt}, which is almost
615 a @TeX{} file, except that diacriticised characters are written using
616 @w{Latin-1}. To complete the recoding of the diacriticised characters
617 @emph{only} and produce a file @file{datum.tex}, without destroying
618 the original, one could do:
619
620 @example
621 cp -p datum.txt datum.tex
622 recode -d l1..tex datum.tex
623 @end example
624
625 However, using @code{recode} as a filter will achieve the same goal more
626 neatly:
627
628 @example
629 recode -d l1..tex <datum.txt >datum.tex
630 @end example
631
632 This example also shows that @code{l1} could be used instead of
633 @code{Latin-1}; charset names often have such aliases.
634
635 @cindex exit status
636 @cindex status code
637 Recode has three modes are for when to set the exit status to non-zero:
638
639 @itemize @bullet
640 @item
641 The most lenient setting is activated with option @samp{-f}, in which
642 case only system errors or library mis-usage causes the exit status to
643 be set.
644
645 @item
646 By default, without @samp{-f} nor @samp{-s}, Recode sets the exit
647 status as above, and also in case of invalid or untranslatable input.
648 It also tries (but not always succeed) to detect if output is going to
649 be ambiguous at some later recode-back time.
650
651 @item
652 The stricter setting is activated with @samp{-s}, Recode then sets the
653 exit status as above, or if input is not canonically coded (and it also
654 prevents itself from @emph{completing} recoding tables for making the
655 recoding reversible).
656
657 @end itemize
658
659 @node Requests, Listings, Synopsis, Invoking recode
660 @section The @var{request} parameter
661
662 In the case where the @var{request} is merely written as
663 @var{before}..@var{after}, then @var{before} and @var{after} specify the
664 start charset and the goal charset for the recoding.
665
666 @cindex charset names, valid characters
667 @cindex valid characters in charset names
668 For Recode, charset names may contain any character, besides a
669 comma, a forward slash, or two periods in a row. But in practice, charset
670 names are currently limited to alphabetic letters (upper or lower case),
671 digits, hyphens, underlines, periods, colons or round parentheses.
672
673 @cindex request, syntax
674 @cindex Recode request syntax
675 The complete syntax for a valid @var{request} allows for unusual things,
676 which might be surprising at first. (Do not pay too much attention to
677 these facilities on first reading.) For example, @var{request} may also
678 contain intermediate charsets, like in the following example:
679
680 @example
681 @var{before}..@var{interim1}..@var{interim2}..@var{after}
682 @end example
683
684 @noindent
685 @cindex intermediate charsets
686 @cindex chaining of charsets in a request
687 @cindex charsets, chaining in a request
688 meaning that Recode should internally produce the @var{interim1}
689 charset from the start charset, then work out of this @var{interim1}
690 charset to internally produce @var{interim2}, and from there towards the
691 goal charset. In fact, Recode internally combines recipes and
692 automatically uses interim charsets, when there is no direct recipe for
693 transforming @var{before} into @var{after}. But there might be many ways
694 to do it. When many routes are possible, the above @dfn{chaining} syntax
695 may be used to more precisely force the program towards a particular route,
696 which it might not have naturally selected otherwise. On the other hand,
697 because Recode tries to choose good routes, chaining is only needed
698 to achieve some rare, unusual effects.
699
700 Moreover, many such requests (sub-requests, more precisely) may be
701 separated with commas (but no spaces at all), indicating a sequence
702 of recodings, where the output of one has to serve as the input of the
703 following one. For example, the two following requests are equivalent:
704
705 @example
706 @var{before}..@var{interim1}..@var{interim2}..@var{after}
707 @var{before}..@var{interim1},@var{interim1}..@var{interim2},@var{interim2}..@var{after}
708 @end example
709
710 @noindent
711 In this example, the charset input for any recoding sub-request is identical
712 to the charset output by the preceding sub-request. But it does not have
713 to be so in the general case. One might wonder what would be the meaning
714 of declaring the charset input for a recoding sub-request of being of
715 different nature than the charset output by a preceding sub-request, when
716 recodings are chained in this way. Such a strange usage might have a
717 meaning and be useful for the Recode expert, but they are quite
718 uncommon in practice.
719
720 @cindex surfaces, syntax
721 More useful is the distinction between the concept of charset, and
722 the concept of surfaces. An encoded charset is represented by:
723
724 @example
725 @var{pure-charset}/@var{surface1}/@var{surface2}@dots{}
726 @end example
727
728 @noindent
729 @cindex surfaces, commutativity
730 @cindex commutativity of surfaces
731 using slashes to introduce surfaces, if any. The order of application
732 of surfaces is usually important, they cannot be freely commuted. In the
733 given example, @var{surface1} is first applied over the @var{pure-charset},
734 then @var{surface2} is applied over the result. Given this request:
735
736 @example
737 @var{before}/@var{surface1}/@var{surface2}..@var{after}/@var{surface3}
738 @end example
739
740 @noindent
741 Recode will understand that the input files should
742 have @var{surface2} removed first (because it was applied last), then
743 @var{surface1} should be removed. The next step will be to translate the
744 codes from charset @var{before} to charset @var{after}, prior to applying
745 @var{surface3} over the result.
746
747 @cindex implied surfaces
748 @cindex surfaces, implied
749 @tindex IBM-PC charset, and CR-LF surface
750 Some charsets have one or more @emph{implied} surfaces. In this case, the
751 implied surfaces are automatically handled merely by naming the charset,
752 without any explicit surface to qualify it. Let's take an example to
753 illustrate this feature. The request @samp{pc..l1} will indeed decode MS-DOS
754 end of lines prior to converting IBM-PC codes to @w{Latin-1}, because @samp{pc}
755 is the name of a charset@footnote{More precisely, @code{pc} is an alias for
756 the charset @code{IBM-PC}.} which has @code{CR-LF} for its usual surface.
757 The request @samp{pc/..l1} will @emph{not} decode end of lines, since
758 the slash introduces surfaces, and even if the surface list is empty, it
759 effectively defeats the automatic removal of surfaces for this charset.
760 So, empty surfaces are useful, indeed!
761
762 @cindex aliases
763 @cindex alternate names for charsets and surfaces
764 @cindex charsets, aliases
765 @cindex surfaces, aliases
766 Both charsets and surfaces may have predefined alternate names, or aliases.
767 However, and this is rather important to understand, implied surfaces
768 are attached to individual aliases rather than on genuine charsets.
769 Consequently, the official charset name and all of its aliases do not
770 necessarily share the same implied surfaces. The charset and all its
771 aliases may each have its own different set of implied surfaces.
772
773 @cindex abbreviated names for charsets and surfaces
774 @cindex names of charsets and surfaces, abbreviation
775 Charset names, surface names, or their aliases may always be abbreviated
776 to any unambiguous prefix. Internally in Recode, disambiguating
777 tables are kept separate for charset names and surface names.
778
779 @cindex letter case, in charset and surface names
780 While recognising a charset name or a surface name (or aliases thereof),
781 Recode ignores all characters besides letters and digits, so for
782 example, the hyphens and underlines being part of an official charset
783 name may safely be omitted (no need to un-confuse them!). There is also
784 no distinction between upper and lower case for charset or surface names.
785
786 One of the @var{before} or @var{after} keywords may be omitted. If the
787 double dot separator is omitted too, then the charset is interpreted as
788 the @var{before} charset.@footnote{Both @var{before} and @var{after} may
789 be omitted, in which case the double dot separator is mandatory. This is
790 not very useful, as the recoding reduces to a mere copy in that case.}
791
792 @cindex default charset
793 @cindex charset, default
794 @vindex DEFAULT_CHARSET
795 @tindex ASCII
796 @tindex CHAR
797 When a charset name is omitted or left empty, the value of the
798 @code{DEFAULT_CHARSET} variable in the environment is used instead.
799 If this variable is not defined, the Recode library uses the current locale's
800 encoding. On POSIX systems, this depends on the first non-empty value
801 among the environment variables @code{LC_ALL}, @code{LC_CTYPE},
802 and @code{LANG}, and can be determined through the
803 command @samp{locale charmap}. If the current locale's encoding may not
804 be resolved, then Recode presumes @code{ASCII}.
805
806 If the charset name is omitted but followed by surfaces, the surfaces
807 then qualify the usual or default charset. For example, the request
808 @samp{../x} is sufficient for applying an hexadecimal surface to the input
809 text@footnote{MS-DOS is one of those systems for which the default charset
810 has implied surfaces, @code{CR-LF} here. Such surfaces are automatically
811 removed or applied whenever the default charset is read or written,
812 exactly as it would go for any other charset. In the example above, on
813 such systems, the hexadecimal surface would then @emph{replace} the implied
814 surfaces. For @emph{adding} an hexadecimal surface without removing any,
815 one should write the request as @samp{/../x}.}.
816
817 The allowable values for @var{before} or @var{after} charsets, and various
818 surfaces, are described in the remainder of this document.
819
820 @node Listings, Recoding, Requests, Invoking recode
821 @section Asking for various lists
822
823 Many options control listing output generated by Recode itself,
824 they are not meant to accompany actual file recodings. These options are:
825
826 @table @samp
827
828 @item --version
829 @opindex --version
830 @cindex Recode version, printing
831 The program merely prints its version numbers on standard output, and
832 exits without doing anything else.
833
834 @item --help
835 @opindex --help
836 @cindex help page, printing
837 The program merely prints a page of help on standard output, and exits
838 without doing any recoding.
839
840 @item -C
841 @itemx --copyright
842 @opindex -C
843 @opindex --copyright
844 @cindex copyright conditions, printing
845 Given this option, all other parameters and options are ignored. The
846 program prints briefly the copyright and copying conditions. See the
847 file @file{COPYING} in the distribution for full statement of the
848 Copyright and copying conditions.
849
850 @item -h[@var{language}/][@var{name}]
851 @itemx --header[=[@var{language}/][@var{name}]]
852 @opindex -h
853 @opindex --header
854 @cindex source file generation
855 @cindex programming language support
856 @cindex languages, programming
857 @cindex supported programming languages
858 Instead of recoding files, Recode writes a @var{language} source
859 file on standard output and exits. This source is meant to be included
860 in a regular program written in the same programming @var{language}:
861 its purpose is to declare and initialise an array, named @var{name},
862 which represents the requested recoding. The only acceptable values for
863 @var{language} are @samp{c} or @samp{perl}, and may may be abbreviated.
864 If @var{language} is not specified, @samp{c} is assumed. If @var{name}
865 is not specified, then it defaults to @samp{@var{before}_@var{after}}.
866 Strings @var{before} and @var{after} are cleaned before being used according
867 to the syntax of @var{language}.
868
869 Even if Recode tries its best, this option does not always succeed in
870 producing the requested source table, it then prints @samp{Recoding
871 is too complex for a mere table}. It will succeed however, provided
872 the recoding can be internally represented by only one step after the
873 optimisation phase, and if this merged step conveys a one-to-one or
874 a one-to-many explicit table. To increase the probability that this
875 happens, @code{iconv} initialisation is currently inhibited whenever
876 this option is used. Also, when attempting to produce sources tables,
877 Recode relaxes its checking a tiny bit: it ignores the algorithmic part
878 of some tabular recodings, it also avoids the processing of implied
879 surfaces. But this is all fairly technical. Better try and see!
880
881 Most tables are produced using decimal numbers to refer to character
882 values@footnote{The author of Recode by far prefer expressing numbers
883 in decimal than octal or hexadecimal, as he considers that the current
884 state of technology should not force users anymore in such strange things.
885 But Unicode people see things differently, to the point Recode
886 cannot escape being tainted with some hexadecimal.}. Yet, users who know
887 all Recode tricks and stunts could indeed force octal or hexadecimal
888 output for the table contents. For example:
889
890 @example
891 recode ibm297/test8..cp1252/x < /dev/null
892 @end example
893
894 @noindent
895 produces a sequence of hexadecimal values which represent a conversion
896 table from @code{IBM297} to @code{CP1252}.
897
898 Beware that other options might affect the produced source tables, these
899 are: @samp{-d}, @samp{-g} and, particularly, @samp{-s}.
900
901 @item -k @var{pairs}
902 @itemx --known=@var{pairs}
903 @opindex -k
904 @opindex --known=
905 @cindex unknown charsets
906 @cindex guessing charsets
907 @cindex charsets, guessing
908 This particular option is meant to help identifying an unknown charset,
909 using as hints some already identified characters of the charset. Some
910 examples will help introducing the idea.
911
912 Let's presume here that Recode is run in a UTF-8 locale, and
913 that @code{DEFAULT_CHARSET} is unset in the environment.
914 Suppose you have guessed that code 130 (decimal) of the unknown charset
915 represents a lower case @samp{e} with an acute accent. That is to say
916 that this code should map to code 233 (decimal) in the usual charset.
917 By executing:
918
919 @example
920 recode -k 130:233
921 @end example
922
923 @noindent
924 you should obtain a listing similar to:
925
926 @example
927 AtariST
928 CWI cp-hu CWI-2
929 IBM437/CR-LF 437/CR-LF CP437/CR-LF
930 IBM850/CR-LF 850/CR-LF CP850/CR-LF
931 IBM851/CR-LF 851/CR-LF CP851/CR-LF
932 IBM852/CR-LF 852/CR-LF CP852/CR-LF pcl2 pclatin2
933 IBM857/CR-LF 857/CR-LF CP857/CR-LF
934 IBM860/CR-LF 860/CR-LF CP860/CR-LF
935 IBM861/CR-LF 861/CR-LF CP861/CR-LF cp-is
936 IBM863/CR-LF 863/CR-LF CP863/CR-LF
937 IBM865/CR-LF 865/CR-LF CP865/CR-LF
938 @end example
939
940 You can give more than one clue at once, to restrict the list further.
941 Suppose you have @emph{also} guessed that code 211 of the unknown
942 charset represents an upper case @samp{E} with diaeresis, that is, code
943 203 in the usual charset. By requesting:
944
945 @example
946 recode -k 130:233,211:203
947 @end example
948
949 @noindent
950 you should obtain:
951
952 @example
953 IBM850/CR-LF 850/CR-LF CP850/CR-LF
954 IBM852/CR-LF 852/CR-LF CP852/CR-LF pcl2 pclatin2
955 IBM857/CR-LF 857/CR-LF CP857/CR-LF
956 @end example
957
958 The usual charset may be overridden by specifying one non-option argument.
959 For example, to request the list of charsets for which code 130 maps to
960 code 142 for the Macintosh, you may ask:
961
962 @example
963 recode -k 130:142 mac
964 @end example
965
966 @noindent
967 and get:
968
969 @example
970 AtariST
971 CWI cp-hu CWI-2
972 IBM437/CR-LF 437/CR-LF CP437/CR-LF
973 IBM850/CR-LF 850/CR-LF CP850/CR-LF
974 IBM851/CR-LF 851/CR-LF CP851/CR-LF
975 IBM852/CR-LF 852/CR-LF CP852/CR-LF pcl2 pclatin2
976 IBM857/CR-LF 857/CR-LF CP857/CR-LF
977 IBM860/CR-LF 860/CR-LF CP860/CR-LF
978 IBM861/CR-LF 861/CR-LF CP861/CR-LF cp-is
979 IBM863/CR-LF 863/CR-LF CP863/CR-LF
980 IBM865/CR-LF 865/CR-LF CP865/CR-LF
981 @end example
982
983 @noindent
984 which, of course, is identical to the result of the first example, since
985 the code 142 for the Macintosh is a small @samp{e} with acute.
986
987 More formally, option @samp{-k} lists all possible @emph{before}
988 charsets for the @emph{after} charset given as the sole non-option
989 argument to @code{recode}, but subject to restrictions given in
990 @var{pairs}. If there is no non-option argument, the @emph{after}
991 charset is taken to be the default charset for this @code{recode}.
992
993 The restrictions are given as a comma separated list of pairs, each pair
994 consisting of two numbers separated by a colon. The numbers are taken
995 as decimal when the initial digit is between @samp{1} and @samp{9};
996 @samp{0x} starts an hexadecimal number, or else @samp{0} starts an
997 octal number. The first number is a code in any @emph{before} charset,
998 while the second number is a code in the specified @emph{after} charset.
999 If the first number would not be transformed into the second number by
1000 recoding from some @emph{before} charset to the @emph{after} charset,
1001 then this @emph{before} charset is rejected. A @emph{before} charset is
1002 listed only if it is not rejected by any pair. The program will only test
1003 those @emph{before} charsets having a tabular style internal description
1004 (@pxref{Tabular}), so should be the selected @emph{after} charset.
1005
1006 The produced list is in fact a subset of the list produced by the
1007 option @samp{-l}. As for option @samp{-l}, the non-option argument
1008 is interpreted as a charset name, possibly abbreviated to any non
1009 ambiguous prefix.
1010
1011 @item -l[@var{format}]
1012 @itemx --list[=@var{format}]
1013 @opindex -l
1014 @opindex --list
1015 @cindex listing charsets
1016 @cindex information about charsets
1017 This option asks for information about all charsets, or about one
1018 particular charset. No file will be recoded.
1019
1020 If there is no non-option arguments, Recode ignores the @var{format}
1021 value of the option, it writes a sorted list of charset names on standard
1022 output, one per line. When a charset name have aliases or synonyms,
1023 they follow the true charset name on its line, sorted from left to right.
1024 Each charset or alias is followed by its implied surfaces, if any. This list
1025 is over two hundred lines. It is best used with @samp{grep -i}, as in:
1026
1027 @example
1028 recode -l | grep -i greek
1029 @end example
1030
1031 @anchor{disable-iconv}
1032 Within a collection of names for a single charset, the Recode
1033 library distinguishes one of them as being the genuine charset name,
1034 while the others are said to be aliases. The list normally integrates
1035 all charsets from the external @code{iconv} library, unless this is
1036 defeated through options like @samp{--ignore=:iconv:} or @samp{-x:}.
1037 The portable @code{libiconv} library relates its own aliases of a same
1038 charset, and for a given set of aliases, if none of them are known to
1039 Recode already, then Recode will pick one as being the
1040 genuine charset. The @code{iconv} library within GNU @code{libc} makes
1041 all aliases appear as different charsets, and each will be presented as
1042 a charset by Recode, unless it is known otherwise.
1043
1044 There might be one non-option argument, in which case it is interpreted
1045 as a charset name, possibly abbreviated to any non ambiguous prefix.
1046 This particular usage of the @samp{-l} option is obeyed @emph{only} for
1047 charsets having a tabular style internal description (@pxref{Tabular}).
1048 Even if most charsets have this property, some do not, and the option
1049 @samp{-l} cannot be used to detail these particular charsets. For knowing
1050 if a particular charset can be listed this way, you should merely try
1051 and see if this works. The @var{format} value of the option is a keyword
1052 from the following list. Keywords may be abbreviated by dropping suffix
1053 letters, and even reduced to the first letter only:
1054
1055 @table @samp
1056 @item decimal
1057 This format asks for the production on standard output of a concise
1058 tabular display of the charset, in which character code values are
1059 expressed in decimal.
1060
1061 @item octal
1062 This format uses octal instead of decimal in the concise tabular display
1063 of the charset.
1064
1065 @item hexadecimal
1066 This format uses hexadecimal instead of decimal in the concise tabular
1067 display of the charset.
1068
1069 @item full
1070 This format requests an extensive display of the charset on standard output,
1071 using one line per character showing its decimal, hexadecimal, octal and
1072 @code{UCS-2} code values, and also a descriptive comment which should be
1073 the 10646 name for the character.
1074
1075 @vindex LC_MESSAGES@r{, when listing charsets}
1076 @cindex French description of charsets
1077 The descriptive comment is given in English and ASCII, yet if the English
1078 description is not available but a French one is, then the French description
1079 is given instead, using @w{Latin-1}. However, if the @code{LC_MESSAGES}
1080 environment variable begins with the letters @samp{fr}, then listing
1081 preference goes to French when both descriptions are available.
1082 @end table
1083
1084 When option @samp{-l} is used together with a @var{charset} argument,
1085 the @var{format} defaults to @code{decimal}.
1086
1087 @item -T
1088 @itemx --find-subsets
1089 @opindex -T
1090 @opindex --find-subsets
1091 @cindex identifying subsets in charsets
1092 @cindex subsets in charsets
1093 This option is a maintainer tool for evaluating the redundancy of those
1094 charsets, in Recode, which are internally represented by an @code{UCS-2}
1095 data table. After the listing has been produced, the program exits
1096 without doing any recoding. The output is meant to be sorted, like
1097 this: @w{@samp{recode -T | sort}}. The option triggers Recode into
1098 comparing all pairs of charsets, seeking those which are subsets of others.
1099 The concept and results are better explained through a few examples.
1100 Consider these three sample lines from @samp{-T} output:
1101
1102 @example
1103 [ 0] IBM891 == IBM903
1104 [ 1] IBM1004 < CP1252
1105 [ 12] INVARIANT < CSA_Z243.4-1985-1
1106 @end example
1107
1108 @noindent
1109 The first line means that @code{IBM891} and @code{IBM903} are completely
1110 identical as far as Recode is concerned, so one is fully redundant
1111 to the other. The second line says that @code{IBM1004} is wholly
1112 contained within @code{CP1252}, yet there is a single character which is
1113 in @code{CP1252} without being in @code{IBM1004}. The third line says
1114 that @code{INVARIANT} is wholly contained within @code{CSA_Z243.4-1985-1},
1115 but twelve characters are in @code{CSA_Z243.4-1985-1} without being in
1116 @code{INVARIANT}. The whole output might most probably be reduced and
1117 made more significant through a transitivity study.
1118 @end table
1119
1120 @node Recoding, Reversibility, Listings, Invoking recode
1121 @section Controlling how files are recoded
1122
1123 The following options have the purpose of giving the user some fine
1124 grain control over the recoding operation themselves.
1125
1126 @table @samp
1127
1128 @item -c
1129 @itemx --colons
1130 @opindex -c
1131 @opindex --colons
1132 @cindex diaeresis
1133 With @code{Texte} Easy French conventions, use the column @kbd{:}
1134 instead of the double-quote @kbd{"} for marking diaeresis.
1135 @xref{Texte}.
1136
1137 @item -g
1138 @itemx --graphics
1139 @opindex -g
1140 @opindex --graphics
1141 @cindex IBM graphics characters
1142 @cindex box-drawing characters
1143 This option is only meaningful while getting @emph{out} of the
1144 @code{IBM-PC} charset. In this charset, characters 176 to 223 are used
1145 for constructing rulers and boxes, using simple or double horizontal or
1146 vertical lines. This option forces the automatic selection of ASCII
1147 characters for approximating these rulers and boxes, at cost of making
1148 the transformation irreversible. Option @samp{-g} implies @samp{-f}.
1149
1150 @item -t
1151 @itemx --touch
1152 @opindex -t
1153 @opindex --touch
1154 @cindex time stamps of files
1155 @cindex file time stamps
1156 The @emph{touch} option is meaningful only when files are recoded over
1157 themselves. Without it, the time-stamps associated with files are
1158 preserved, to reflect the fact that changing the code of a file does not
1159 really alter its informational contents. When the user wants the
1160 recoded files to be time-stamped at the recoding time, this option
1161 inhibits the automatic protection of the time-stamps.
1162
1163 @item -v
1164 @itemx --verbose
1165 @opindex -v
1166 @opindex --verbose
1167 @cindex verbose operation
1168 @cindex details about recoding
1169 @cindex recoding details
1170 @cindex quality of recoding
1171 Before doing any recoding, the program will first print on the @code{stderr}
1172 stream the list of all intermediate charsets planned for recoding, starting
1173 with the @var{before} charset and ending with the @var{after} charset.
1174 It also prints an indication of the recoding quality, as one of the word
1175 @samp{reversible}, @samp{one to one}, @samp{one to many}, @samp{many to
1176 one} or @samp{many to many}.
1177
1178 This information will appear once or twice. It is shown a second time
1179 only when the optimisation and step merging phase succeeds in replacing
1180 many single steps by a new one.
1181
1182 This option also has a second effect. The program will print on
1183 @code{stderr} one message per recoded @var{file}, so as to keep the user
1184 informed of the progress of its command.
1185
1186 An easy way to know beforehand the sequence or quality of a recoding is
1187 by using the command such as:
1188
1189 @example
1190 recode -v @var{before}..@var{after} < /dev/null
1191 @end example
1192
1193 @noindent
1194 using the fact that, in Recode, an empty input file produces
1195 an empty output file.
1196
1197 @item -x @var{charset}
1198 @itemx --ignore=@var{charset}
1199 @opindex -x
1200 @opindex --ignore
1201 @cindex ignore charsets
1202 @cindex recoding path, rejection
1203 This option tells the program to ignore any recoding path through the
1204 specified @var{charset}, so disabling any single step using this charset
1205 as a start or end point. This may be used when the user wants to force
1206 Recode into using an alternate recoding path (yet using chained
1207 requests offers a finer control, @pxref{Requests}).
1208
1209 @var{charset} may be abbreviated to any unambiguous prefix.
1210 @end table
1211
1212 @node Reversibility, Sequencing, Recoding, Invoking recode
1213 @section Reversibility issues
1214
1215 The following options are somewhat related to reversibility issues:
1216
1217 @table @samp
1218 @item -f
1219 @itemx --force
1220 @opindex -f
1221 @opindex --force
1222 @cindex force recoding
1223 @cindex irreversible recoding
1224 With this option, irreversible or otherwise erroneous recodings are run
1225 to completion, and @code{recode} does not exit with a non-zero status if
1226 it would be only because irreversibility matters. @xref{Reversibility}.
1227
1228 Without this option, Recode tries to protect you against recoding
1229 a file irreversibly over itself@footnote{There are still some cases of
1230 ambiguous output which are rather difficult to detect, and for which
1231 the protection is not active.}. Whenever an irreversible recoding is
1232 met, or any other recoding error, @code{recode} produces a warning on
1233 standard error. The current input file does not get replaced by its
1234 recoded version, and @code{recode} then proceeds with the recoding of
1235 the next file.
1236
1237 When the program is merely used as a filter, standard output will have
1238 received a partially recoded copy of standard input, up to the first
1239 error point. After all recodings have been done or attempted, and if
1240 some recoding has been aborted, @code{recode} exits with a non-zero status.
1241
1242 @item -q
1243 @itemx --quiet
1244 @itemx --silent
1245 @opindex -q
1246 @opindex --quiet
1247 @opindex --silent
1248 @cindex suppressing diagnostic messages
1249 @cindex error messages, suppressing
1250 @cindex silent operation
1251 This option has the sole purpose of inhibiting warning messages about
1252 irreversible recodings, and other such diagnostics. It has no other
1253 effect, in particular, it does @emph{not} prevent recodings to be aborted
1254 or @code{recode} to return a non-zero exit status when irreversible
1255 recodings are met.
1256
1257 This option is set automatically for the children processes, when recode
1258 splits itself in many collaborating copies. Doing so, the diagnostic is
1259 issued only once by the parent. See option @samp{-p}.
1260
1261 @item -s
1262 @itemx --strict
1263 @opindex -s
1264 @opindex --strict
1265 @cindex strict operation
1266 @cindex map filling, disable
1267 @cindex disable map filling
1268 By using this option, the user requests that Recode be very strict
1269 while recoding a file, merely losing in the transformation any character
1270 which is not explicitly mapped from a charset to another. Such a loss is
1271 not reversible and so, will bring Recode to fail, unless the option
1272 @samp{-f} is also given as a kind of counter-measure.
1273
1274 Using @samp{-s} without @samp{-f} might render Recode very susceptible
1275 to the slighest file abnormalities. Despite the fact that it might be
1276 irritating to some users, such paranoia is sometimes wanted and useful.
1277
1278 @end table
1279
1280 @cindex reversibility of recoding
1281 Even if Recode tries hard to keep the recodings reversible,
1282 you should not develop an unconditional confidence in its ability to
1283 do so. You @emph{ought} to keep only reasonable expectations about
1284 reverse recodings. In particular, consider:
1285
1286 @itemize @bullet
1287 @item
1288 Most transformations are fully reversible for all inputs, but lose this
1289 property whenever @samp{-s} is specified.
1290
1291 @item
1292 A few transformations are not meant to be reversible, by design.
1293
1294 @item
1295 Reversibility sometimes depends on actual file contents and cannot
1296 be ascertained beforehand, without reading the file.
1297
1298 @item
1299 Reversibility is never absolute across successive versions of this
1300 program. Even correcting a small bug in a mapping could induce slight
1301 discrepancies later.
1302
1303 @item
1304 Reversibility is easily lost by merging. This is best explained through
1305 an example. If you reversibly recode a file from charset @var{A} to
1306 charset @var{B}, then you reversibly recode the result from charset
1307 @var{B} to charset @var{C}, you cannot expect to recover the original
1308 file by merely recoding from charset @var{C} directly to charset @var{A}.
1309 You will instead have to recode from charset @var{C} back to charset
1310 @var{B}, and only then from charset @var{B} to charset @var{A}.
1311
1312 @item
1313 Faulty files create a particular problem. Consider an example, recoding
1314 from @code{IBM-PC} to @code{Latin-1}. End of lines are represented as
1315 @samp{\r\n} in @code{IBM-PC} and as @samp{\n} in @code{Latin-1}. There
1316 is no way by which a faulty @code{IBM-PC} file containing a @samp{\n}
1317 not preceded by @samp{\r} be translated into a @code{Latin-1} file, and
1318 then back.
1319
1320 @item
1321 There is another difficulty arising from code equivalences. For
1322 example, in a @code{LaTeX} charset file, the string @samp{\^\i@{@}}
1323 could be recoded back and forth through another charset and become
1324 @samp{\^@{\i@}}. Even if the resulting file is equivalent to the
1325 original one, it is not identical.
1326 @end itemize
1327
1328 @cindex map filling
1329 Unless option @samp{-s} is used, Recode automatically tries to
1330 fill mappings with invented correspondences, often making them fully
1331 reversible. This filling is not made at random. The algorithm tries to
1332 stick to the identity mapping and, when this is not possible, it prefers
1333 generating many small permutation cycles, each involving only a few
1334 codes.
1335
1336 For example, here is how @code{IBM-PC} code 186 gets translated to
1337 @kbd{control-U} in @code{Latin-1}. @kbd{Control-U} is 21. Code 21 is the
1338 @code{IBM-PC} section sign, which is 167 in @code{Latin-1}. Recode
1339 cannot reciprocate 167 to 21, because 167 is the masculine ordinal indicator
1340 within @code{IBM-PC}, which is 186 in @code{Latin-1}. Code 186 within
1341 @code{IBM-PC} has no @code{Latin-1} equivalent; by assigning it back to 21,
1342 Recode closes this short permutation loop.
1343
1344 As a consequence of this map filling, Recode may sometimes produce
1345 @emph{funny} characters. They may look annoying, they are nevertheless
1346 helpful when one changes his (her) mind and wants to revert to the prior
1347 recoding. If you cannot stand these, use option @samp{-s}, which asks
1348 for a very strict recoding.
1349
1350 This map filling sometimes has a few surprising consequences, which
1351 some users wrongly interpreted as bugs. Here are two examples.
1352
1353 @enumerate
1354 @item
1355 In some cases, Recode seems to copy a file without recoding it.
1356 But in fact, it does. Consider a request:
1357
1358 @example
1359 recode l1..us < File-Latin1 > File-ASCII
1360 cmp File-Latin1 File-ASCII
1361 @end example
1362
1363 @noindent
1364 then @code{cmp} will not report any difference. This is quite normal.
1365 @w{@code{Latin-1}} gets correctly recoded to ASCII for charsets commonalities
1366 (which are the first 128 characters, in this case). The remaining last
1367 128 @w{@code{Latin-1}} characters have no ASCII correspondent. Instead
1368 of losing
1369 them, Recode elects to map them to unspecified characters of ASCII, so
1370 making the recoding reversible. The simplest way of achieving this is
1371 merely to keep those last 128 characters unchanged. The overall effect
1372 is copying the file verbatim.
1373
1374 If you feel this behaviour is too generous and if you do not wish to
1375 care about reversibility, simply use option @samp{-s}. By doing so,
1376 Recode will strictly map only those @w{@code{Latin-1}} characters
1377 which have
1378 an ASCII equivalent, and will merely drop those which do not. Then,
1379 there is more chance that you will observe a difference between the
1380 input and the output file.
1381
1382 @item
1383 Recoding the wrong way could sometimes give the false impression that
1384 recoding has @emph{almost} been done properly. Consider the requests:
1385
1386 @example
1387 recode 437..l1 < File-Latin1 > Temp1
1388 recode 437..l1 < Temp1 > Temp2
1389 @end example
1390
1391 @noindent
1392 so declaring wrongly @file{File-Latin1} to be an IBM-PC file, and
1393 recoding to @code{Latin-1}. This is surely ill defined and not meaningful.
1394 Yet, if you repeat this step a second time, you might notice that
1395 many (not all) characters in @file{Temp2} are identical to those in
1396 @file{File-Latin1}. Sometimes, people try to discover how Recode
1397 works by experimenting a little at random, rather than reading and
1398 understanding the documentation; results such as this are surely confusing,
1399 as they provide those people with a false feeling that they understood
1400 something.
1401
1402 Reversible codings have this property that, if applied several times
1403 in the same direction, they will eventually bring any character back
1404 to its original value. Since Recode seeks small permutation
1405 cycles when creating reversible codings, besides characters unchanged
1406 by the recoding, most permutation cycles will be of length 2, and
1407 fewer of length 3, etc. So, it is just expectable that applying the
1408 recoding twice in the same direction will recover most characters,
1409 but will fail to recover those participating in permutation cycles of
1410 length 3. On the other end, recoding six times in the same direction
1411 would recover all characters in cycles of length 1, 2, 3 or 6.
1412 @end enumerate
1413
1414 @node Sequencing, Mixed, Reversibility, Invoking recode
1415 @section Selecting sequencing methods
1416
1417 @cindex sequencing
1418 Recode can split itself into multiple parallel processes when it is
1419 discovered that many passes are needed to comply with the @var{request}.
1420 For example, suppose that four elementary steps were selected at
1421 recoding path optimisation time. Then Recode will split itself into
1422 four different interconnected tasks, logically equivalent to:
1423
1424 @example
1425 @var{step1} <@var{input} | @var{step2} | @var{step3} | @var{step4} >@var{output}
1426 @end example
1427
1428 On systems where the pipes method is not available, the steps are
1429 performed in series.
1430
1431 @table @samp
1432 @item --sequence=memory
1433 @opindex --sequence
1434 @cindex memory sequencing
1435 When the recoding requires a combination of two or more elementary
1436 recoding steps, this option forces many passes over the data, using
1437 in-memory buffers to hold all intermediate results.
1438 If this option is selected in filter
1439 mode, that is, when the program reads standard input and writes standard
1440 output, it might take longer for programs further down the pipe chain to
1441 start receiving some recoded data.
1442
1443 @item -p
1444 @itemx --sequence=pipe
1445 @opindex -p
1446 @cindex pipe sequencing
1447 When the recoding requires a combination of two or more elementary
1448 recoding steps, this option forces the program to fork itself into a few
1449 copies interconnected with pipes, using the @code{pipe(2)} system call.
1450 All copies of the program operate in parallel. This is the default
1451 behaviour in filter mode. If this option is used when files are recoded
1452 over themselves, this should also save disk space because some temporary
1453 files might not be needed, at the cost of more system overhead.
1454
1455 @item -i
1456 @itemx --sequence=files
1457 @opindex -i
1458 @cindex file sequencing
1459 This option is accepted for backwards compatibility, and acts like
1460 @samp{--sequence=memory}.
1461 @end table
1462
1463 @node Mixed, Emacs, Sequencing, Invoking recode
1464 @section Using mixed charset input
1465
1466 In real life and practice, textual files are often made up of many charsets
1467 at once. Some parts of the file encode one charset, while other parts
1468 encode another charset, and so forth. Usually, a file does not toggle
1469 between more than two or three charsets. The means to distinguish
1470 which charsets are encoded at various places is not always available.
1471 Recode is able to handle only a few simple cases
1472 of mixed input.
1473
1474 The default Recode behaviour is to expect pure charset files, to
1475 be recoded as other pure charset files. However, the following options
1476 allow for a few precise kinds of mixed charset files.
1477
1478 @table @samp
1479 @item -d
1480 @itemx --diacritics
1481 @opindex -d
1482 @opindex --diacritics
1483 @cindex convert a subset of characters
1484 @cindex partial conversion
1485 While converting to or from one of @code{HTML}, @code{LaTeX} or @code{BibTeX}
1486 charset, limit conversion to some subset of all characters.
1487 For @code{HTML}, limit conversion to the subset of all non-ASCII
1488 characters. For @code{LaTeX} or @code{BibTeX}, limit conversion to the subset of all
1489 non-English letters. This is particularly useful, for example, when
1490 people create what would be valid @code{HTML}, @TeX{} or La@TeX{}
1491 files, if only they were using provided sequences for applying
1492 diacritics instead of using the diacriticised characters directly
1493 from the underlying character set.
1494
1495 While converting to @code{HTML}, @code{LaTeX} or @code{BibTeX} charset, this option
1496 assumes that characters not in the said subset are properly coded
1497 or protected already; Recode then transmits them literally.
1498 While converting the other way, this option prevents translating back
1499 coded or protected versions of characters not in the said subset.
1500 @xref{HTML}. @xref{LaTeX}. @xref{BibTeX}.
1501
1502 @ignore
1503 @item -M
1504 @itemx --message
1505 @opindex -M
1506 @opindex --message
1507 Option @samp{-M} would be for messages, it would ideally process @w{RFC
1508 1522} inserts
1509 in ASCII headers, converting them to the goal code, rewriting some MIME
1510 header line too, and stopping its special work at the first empty line.
1511 A special combination of both capabilities would be for the recoding of
1512 PO files, in which the header, and @code{msgid} and @code{msgstr} strings, might
1513 all use different charsets. Recoding some PO files currently looks like
1514 a nightmare, which I would like Recode to repair.
1515 @end ignore
1516
1517 @item -S[@var{language}]
1518 @itemx --source[=@var{language}]
1519 @opindex -S
1520 @opindex --source
1521 @cindex convert strings and comments
1522 @cindex string and comments conversion
1523 The bulk of the input file is expected to be written in @code{ASCII},
1524 except for parts, like comments and string constants, which are written
1525 using another charset than @code{ASCII}. When @var{language} is @samp{c},
1526 the recoding will proceed only with the contents of comments or strings,
1527 while everything else will be copied without recoding. When @var{language}
1528 is @samp{po}, the recoding will proceed only within translator comments
1529 (those having whitespace immediately following the initial @samp{#})
1530 and with the contents of @code{msgstr} strings.
1531
1532 For the above things to work, the non-@code{ASCII} encoding of the comment
1533 or string should be such that an @code{ASCII} scan will successfully find
1534 where the comment or string ends.
1535
1536 Even if @code{ASCII} is the usual charset for writing programs, some
1537 compilers are able to directly read other charsets, like @code{UTF-8}, say.
1538 There is currently no provision in Recode for reading mixed charset
1539 sources which are not based on @code{ASCII}. It is probable that the need
1540 for mixed recoding is not as pressing in such cases.
1541
1542 For example, after one does:
1543
1544 @example
1545 recode -Spo pc/..u8 < @var{input}.po > @var{output}.po
1546 @end example
1547
1548 @noindent
1549 file @file{@var{output}.po} holds a copy of @file{@var{input}.po} in which
1550 @emph{only} translator comments and the contents of @code{msgstr} strings
1551 have been recoded from the @code{IBM-PC} charset to pure @code{UTF-8},
1552 without attempting conversion of end-of-lines. Machine generated comments
1553 and original @code{msgid} strings are not to be touched by this recoding.
1554
1555 If @var{language} is not specified, @samp{c} is assumed.
1556 @end table
1557
1558 @node Emacs, Debugging, Mixed, Invoking recode
1559 @section Using Recode within Emacs
1560
1561 The fact the @code{recode} program acts as a filter, when given no
1562 file arguments, makes it quite easy to use from within GNU Emacs. For
1563 example, recoding the whole buffer from the @code{IBM-PC} charset to
1564 current charset (for example, @w{@code{UTF-8}} on Unix) is easily done
1565 with:
1566
1567 @example
1568 C-x h C-u M-| recode ibmpc RET
1569 @end example
1570
1571 @noindent
1572 @samp{C-x h} selects the whole buffer, and @samp{C-u M-|} filters and
1573 replaces the current region through the given shell command. Here is
1574 another example, binding the keys @w{@samp{C-c T}} to the recoding of
1575 the current region from Easy French to @w{@code{Latin-1}} (on Unix) and the key
1576 @w{@samp{C-u C-c T}} from @w{@code{Latin-1}} (on Unix) to Easy French:
1577
1578 @example
1579 (global-set-key "\C-cT" 'recode-texte)
1580
1581 (defun recode-texte (flag)
1582 (interactive "P")
1583 (shell-command-on-region
1584 (region-beginning) (region-end)
1585 (concat "recode " (if flag "..txte" "txte")) t)
1586 (exchange-point-and-mark))
1587 @end example
1588
1589 @node Debugging, , Emacs, Invoking recode
1590 @section Debugging considerations
1591
1592 It is our experience that when Recode does not provide satisfying
1593 results, either the @code{recode} program was not called properly,
1594 correct results raised some doubts nevertheless, or files to recode were
1595 somewhat mangled. Genuine bugs are surely possible.
1596
1597 Unless you already are a Recode expert, it might be a good idea to
1598 quickly revisit the tutorial (@pxref{Tutorial}) or the prior sections in this
1599 chapter, to make sure that you properly formatted your recoding request.
1600 In the case you intended to use Recode as a filter, make sure that you
1601 did not forget to redirect your standard input (through using the @kbd{<}
1602 symbol in the shell, say). Some Recode false mysteries are also
1603 easily explained, @xref{Reversibility}.
1604
1605 For the other cases, some investigation is needed. To illustrate how to
1606 proceed, let's presume that you want to recode the @file{nicepage} file,
1607 coded @code{UTF-8}, into @code{HTML}. The problem is that the command
1608 @samp{recode u8..h nicepage} yields:
1609
1610 @example
1611 recode: Invalid input in step `UTF-8..ISO-10646-UCS-2'
1612 @end example
1613
1614 One good trick is to use @code{recode} in filter mode instead of in file
1615 replacement mode, @xref{Synopsis}. Another good trick is to use the
1616 @samp{-v} option asking for a verbose description of the recoding steps.
1617 We could rewrite our recoding call as @samp{recode -v u8..h <nicepage},
1618 to get something like:
1619
1620 @example
1621 Request: UTF-8..:iconv:..ISO-10646-UCS-2..HTML_4.0
1622 Shrunk to: UTF-8..ISO-10646-UCS-2..HTML_4.0
1623 [@dots{}@var{some output}@dots{}]
1624 recode: Invalid input in step `UTF-8..ISO-10646-UCS-2'
1625 @end example
1626
1627 This might help you to better understand what the diagnostic means. The
1628 recoding request is achieved in two steps, the first recodes @code{UTF-8}
1629 into @code{UCS-2}, the second recodes @code{UCS-2} into @code{HTML}.
1630 The problem occurs within the first of these two steps, and since, the
1631 input of this step is the input file given to Recode, this is
1632 this overall input file which seems to be invalid. Also, when used in
1633 filter mode, Recode processes as much input as possible before the
1634 error occurs and sends the result of this processing to standard output.
1635 Since the standard output has not been redirected to a file, it is merely
1636 displayed on the user screen. By inspecting near the end of the resulting
1637 @code{HTML} output, that is, what was recoding a bit before the recoding
1638 was interrupted, you may infer about where the error stands in the real
1639 @code{UTF-8} input file.
1640
1641 If you have the proper tools to examine the intermediate recoding data,
1642 you might also prefer to reduce the problem to a single step to better
1643 study it. This is what I usually do. For example, the last @code{recode}
1644 call above is more or less equivalent to:
1645
1646 @example
1647 recode -v UTF-8..ISO_10646-UCS-2 <nicepage >temporary
1648 recode -v ISO_10646-UCS-2..HTML_4.0 <temporary
1649 rm temporary
1650 @end example
1651
1652 If you know that the problem is within the first step, you might prefer to
1653 concentrate on using the first @code{recode} line. If you know that the
1654 problem is within the second step, you might execute the first @code{recode}
1655 line once and for all, and then play with the second @code{recode} call,
1656 repeatedly using the @file{temporary} file created once by the first call.
1657
1658 Note that the @samp{-f} switch may be used to force the production of
1659 @code{HTML} output despite invalid input, it might be satisfying enough
1660 for you, and easier than repairing the input file. That depends on how
1661 strict you would like to be about the precision of the recoding process.
1662
1663 If you later see that your HTML file begins with @samp{@@lt;html@@gt;} when
1664 you expected @samp{<html>}, then Recode might have done a bit more
1665 that you wanted. In this case, your input file was half-@code{UTF-8},
1666 half-@code{HTML} already, that is, a mixed file (@pxref{Mixed}). There is a
1667 special @code{-d} switch for this case. So, your might be end up calling
1668 @samp{recode -fd nicepage}. Until you are quite sure that you accept
1669 overwriting your input file whatever what, I recommend that you stick with
1670 filter mode.
1671
1672 If, after such experiments, you seriously think that Recode does not
1673 behave properly, there might be a genuine bug either in the program or
1674 the library itself, in which case I invite you to to contribute a bug
1675 report, @xref{Contributing}.
1676
1677 @node Library, Universal, Invoking recode, Top
1678 @chapter A recoding library
1679
1680 @cindex recoding library
1681 The program named @code{recode} is just an application of its recoding
1682 library. The recoding library is available separately for other C
1683 programs. A good way to acquire some familiarity with the recoding
1684 library is to get acquainted with the @code{recode} program itself.
1685
1686 @cindex @code{recode.h} header
1687 @cindex @code{program_name} variable
1688 To use the recoding library once it is installed, a C program needs to
1689 have the following lines:
1690
1691 @example
1692 #include <stdlib.h>
1693 #include <stdbool.h>
1694 #include <recode.h>
1695
1696 const char *program_name;
1697 @end example
1698
1699 @noindent
1700 near its beginning, and the user should have @samp{-lrecode} on the
1701 linking call, so modules from the recoding library are found.
1702
1703 The library contains
1704 four identifiable sets of routines: the outer level functions, the
1705 request level functions, the task level functions and the charset level
1706 functions. There are discussed in separate sections. For effectively
1707 using the recoding library in most applications, it should be rarely
1708 needed to study anything beyond the main initialisation function at
1709 outer level, and then, various functions at request level.
1710
1711 @menu
1712 * Outer level:: Outer level functions
1713 * Request level:: Request level functions
1714 * Task level:: Task level functions
1715 * Charset level:: Charset level functions
1716 * Errors:: Handling errors
1717 @end menu
1718
1719 @node Outer level, Request level, Library, Library
1720 @section Outer level functions
1721
1722 @cindex outer level functions
1723
1724 The outer level functions mainly prepare the whole recoding library for
1725 use, or do actions which are unrelated to specific recodings. Here is
1726 an example of a program which does not really make anything useful.
1727
1728 @example
1729 @group
1730 #include <stdbool.h>
1731 #include <stdlib.h>
1732 #include <recode.h>
1733
1734 const char *program_name;
1735
1736 int
1737 main (int argc, char *const *argv)
1738 @{
1739 program_name = argv[0];
1740 RECODE_OUTER outer = recode_new_outer (RECODE_AUTO_ABORT_FLAG);
1741
1742 recode_delete_outer (outer);
1743 exit (EXIT_SUCCESS);
1744 @}
1745 @end group
1746 @end example
1747
1748 @vindex RECODE_OUTER structure
1749 The header file @code{recode.h} declares an opaque @code{RECODE_OUTER}
1750 structure, which the programmer should use for allocating a variable in
1751 his program (let's assume the programmer is a male, here, no prejudice
1752 intended). This @samp{outer} variable is given as a first argument to
1753 all outer level functions.
1754
1755 @cindex speed considerations
1756 The @code{RECODE_OUTER} structure is really meant to be initialised only
1757 once in the life of a program, and terminated with the program itself.
1758 Program interfaces should pay attention to initialise it only once,
1759 would it be only for speed considerations. A good deal of overhead goes
1760 to outer level initialization, and if the outer level was initialized
1761 afresh for each and every string translated, say, the Recode library
1762 would appear immensely much slower that it was meant to be!
1763
1764 @cindex memory leaks
1765 @cindex leaks, memory
1766 Because outer level initialization is meant to be done only once, not so
1767 much attention has been paid to avoid memory leaks at this level within
1768 Recode. This is hardly a reason for not plugging such leaks at any
1769 level: in the long run, they should all be chased and repaired.
1770
1771 @itemize @bullet
1772 @item Initialisation functions
1773 @cindex initialisation functions, outer
1774
1775 @example
1776 RECODE_OUTER recode_new_outer (@var{flags});
1777 bool recode_delete_outer (@var{outer});
1778 @end example
1779
1780 @findex recode_new_outer
1781 @findex recode_delete_outer
1782 The recoding library absolutely needs to be initialised before being used,
1783 and @code{recode_new_outer} has to be called once, first. Besides the
1784 @var{outer} it is meant to initialise, the function accepts an integer value
1785 holding zero or more flags. If no flags, use @samp{0}. If more than one
1786 flag, they should be combined with the bitwise-or (@samp{|}) operator.
1787 The possible flags are:
1788
1789 @table @code
1790 @item RECODE_AUTO_ABORT_FLAG
1791
1792 When this flag is set, the library later issues diagnostics itself, and
1793 aborts the calling program on errors. This is merely a convenience,
1794 because if this flag was not given, the calling program should always
1795 take care of checking the return value of all other calls to the
1796 recoding library functions, and when any error is detected, issue a
1797 diagnostic and abort processing itself.
1798
1799 @item RECODE_NO_ICONV_FLAG
1800
1801 When this flag is set, the library does not initialize or use the
1802 external @code{iconv} library. This means that the charsets and aliases
1803 provided by the @code{iconv} external library and not by Recode
1804 itself are not available.
1805
1806 @item RECODE_STRICT_MAPPING_FLAG
1807
1808 When this flag is set (corresponding to the @samp{--strict} command-line
1809 option), untranslatable characters are discarded, but an error is
1810 returned on completion unless @samp{RECODE_FORCE_FLAG} is also set.
1811
1812 @item RECODE_FORCE_FLAG
1813
1814 When this flag is set (corresponding to the @samp{--force} command-line
1815 option), errors caused by untranslatable characters are ignored.
1816
1817 @end table
1818
1819 In previous incarnations of the Recode library, @var{flags}
1820 was a Boolean instead of a collection of flags, meant to set
1821 @code{RECODE_AUTO_ABORT_FLAG}. This still works, but is deprecated.
1822
1823 Regardless of the setting of @code{RECODE_AUTO_ABORT}, all recoding
1824 library functions return a success status. Most functions are
1825 geared for returning @code{false} for an error, and @code{true} if
1826 everything went fine. Functions returning structures or strings return
1827 @code{NULL} instead of the result, when the result cannot be produced.
1828 If @var{RECODE_AUTO_ABORT} is selected, functions either return
1829 @code{true}, or do not return at all.
1830
1831 As in the example above, @code{recode_new_outer} is called only once in
1832 most cases. Calling @code{recode_new_outer} implies some overhead, so
1833 calling it more than once should preferably be avoided.
1834
1835 The termination function @code{recode_delete_outer} reclaims the memory
1836 allocated by @code{recode_new_outer} for a given @var{outer} variable.
1837 Calling @code{recode_delete_outer} prior to program termination is more
1838 aesthetic then useful, as all memory resources are automatically reclaimed
1839 when the program ends. You may spare this terminating call if you prefer.
1840
1841 @item The @code{program_name} declaration
1842
1843 @cindex @code{program_name} variable
1844 As we just explained, the user may set the Recode library so that,
1845 in case of problems error, it issues the diagnostic itself and aborts the
1846 whole processing. This capability may be quite convenient. When this
1847 feature is used, the aborting routine includes the name of the running
1848 program in the diagnostic. On the other hand, when this feature is not
1849 used, the library merely return error codes, giving the library user fuller
1850 control over all this. This behaviour is more like what usual libraries
1851 do: they return codes and never abort. However, I would rather not force
1852 library users to necessarily check all return codes themselves, by leaving
1853 no other choice. In most simple applications, letting the library diagnose
1854 and abort is much easier, and quite welcome. This is precisely because
1855 both possibilities exist that the @code{program_name} variable is needed: it
1856 may be used by the library @emph{when} the user sets it to diagnose itself.
1857 @end itemize
1858
1859 @node Request level, Task level, Outer level, Library
1860 @section Request level functions
1861
1862 @cindex request level functions
1863 The request level functions are meant to cover most recoding needs
1864 programmers may have; they should provide all usual functionality.
1865 Their API is almost stable by now.
1866
1867 To get started with request level functions, here is a full example of
1868 a program which sole job is to filter @code{ibmpc} code on its standard
1869 input into @code{latin1} code on its standard output.
1870
1871 @example
1872 @group
1873 #include <stdbool.h>
1874 #include <stdio.h>
1875 #include <stdlib.h>
1876 #include <recode.h>
1877
1878 const char *program_name;
1879
1880 int
1881 main (int argc, char *const *argv)
1882 @{
1883 program_name = argv[0];
1884 RECODE_OUTER outer = recode_new_outer (RECODE_AUTO_ABORT_FLAG);
1885 RECODE_REQUEST request = recode_new_request (outer);
1886 bool success;
1887
1888 recode_scan_request (request, "ibmpc..latin1");
1889
1890 success = recode_file_to_file (request, stdin, stdout);
1891
1892 recode_delete_request (request);
1893 recode_delete_outer (outer);
1894
1895 exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
1896 @}
1897 @end group
1898 @end example
1899
1900 @vindex RECODE_REQUEST structure
1901 The header file @code{recode.h} declares a @code{RECODE_REQUEST} structure,
1902 which the programmer should use for allocating a variable in his program.
1903 This @var{request} variable is given as a first argument to all request
1904 level functions, and in most cases, may be considered as opaque.
1905
1906 @cindex speed considerations
1907 Suppose an application is doing a lot of recoding using only a few
1908 different requests. For speed considerations, the @code{RECODE_REQUEST}
1909 structure should ideally be cached for each kind of request, so the
1910 request level initialisation is not redone for each and every string
1911 translated. The speedup should be more apparent when Recode is able
1912 to optimize the work by building on the fly, within the structure, new
1913 specialized recoding steps and their associated data tables.
1914
1915 @itemize @bullet
1916 @item Initialisation functions
1917 @cindex initialisation functions, request
1918
1919 @example
1920 RECODE_REQUEST recode_new_request (@var{outer});
1921 bool recode_delete_request (@var{request});
1922 @end example
1923
1924 @findex recode_new_request
1925 @findex recode_delete_request
1926 No @var{request} variable may not be used in other request level
1927 functions of the recoding library before having been initialised by
1928 @code{recode_new_request}. There may be many such @var{request}
1929 variables, in which case, they are independent of one another and
1930 they all need to be initialised separately. To avoid memory leaks, a
1931 @var{request} variable should not be initialised a second time without
1932 calling @code{recode_delete_request} to ``un-initialise'' it.
1933
1934 Like for @code{recode_delete_outer}, calling @code{recode_delete_request}
1935 prior to program termination, in the example above, may be left out.
1936
1937 @item Fields of @code{struct recode_request}
1938 @vindex recode_request structure
1939
1940 Here are the fields of a @code{struct recode_request} which may be
1941 meaningfully changed, once a @var{request} has been initialised by
1942 @code{recode_new_request}, but before it gets used. It is not very frequent,
1943 in practice, that these fields need to be changed. To access the fields,
1944 you need to include @file{recodext.h} @emph{instead} of @file{recode.h},
1945 in which case there also is a greater chance that you need to recompile
1946 your programs if a new version of the recoding library gets installed.
1947
1948 @table @code
1949 @item verbose_flag
1950 @vindex verbose_flag
1951 This field is initially @code{false}. When set to @code{true}, the
1952 library will echo to stderr the sequence of elementary recoding steps
1953 needed to achieve the requested recoding.
1954
1955 @item diaeresis_char
1956 @vindex diaeresis_char
1957 This field is initially the ASCII value of a double quote @kbd{"},
1958 but it may also be the ASCII value of a colon @kbd{:}. In @code{texte}
1959 charset, some countries use double quotes to mark diaeresis, while other
1960 countries prefer colons. This field contains the diaeresis character
1961 for the @code{texte} charset.
1962
1963 @item make_header_flag
1964 @vindex make_header_flag
1965 This field is initially @code{false}. When set to @code{true}, it
1966 indicates that the program is merely trying to produce a recoding table in
1967 source form rather than completing any actual recoding. In such a case,
1968 the optimisation of step sequence can be attempted much more aggressively.
1969 If the step sequence cannot be reduced to a single step, table production
1970 will fail.
1971
1972 @item diacritics_only
1973 @vindex diacritics_only
1974 This field is initially @code{false}. For @code{HTML}, @code{LaTeX} and @code{BibTeX}
1975 charset, it is often convenient to recode the diacriticized characters
1976 only, while just not recoding other HTML code using ampersands or angular
1977 brackets, or La@TeX{} code using backslashes. Set the field to @code{true}
1978 for getting this behaviour. In the other charset, one can edit text as
1979 well as HTML or La@TeX{} directives.
1980
1981 @item ascii_graphics
1982 @vindex ascii_graphics
1983 This field is initially @code{false}, and relate to characters 176 to
1984 223 in the @code{ibmpc} charset, which are use to draw boxes. When set
1985 to @code{true}, while getting out of @code{ibmpc}, ASCII characters are
1986 selected so to graphically approximate these boxes.
1987 @end table
1988
1989 @item Study of request strings
1990
1991 @example
1992 bool recode_scan_request (@var{request}, "@var{string}");
1993 @end example
1994
1995 @findex recode_scan_request
1996 The main role of a @var{request} variable is to describe a set of
1997 recoding transformations. Function @code{recode_scan_request} studies
1998 the given @var{string}, and stores an internal representation of it into
1999 @var{request}. Note that @var{string} may be a full-fledged Recode
2000 request, possibly including surfaces specifications, intermediary
2001 charsets, sequences, aliases or abbreviations (@pxref{Requests}).
2002
2003 The internal representation automatically receives some pre-conditioning
2004 and optimisation, so the @var{request} may then later be used many times
2005 to achieve many actual recodings. It would not be efficient calling
2006 @code{recode_scan_request} many times with the same @var{string}, it is
2007 better having many @var{request} variables instead.
2008
2009 @item Actual recoding jobs
2010
2011 Once the @var{request} variable holds the description of a recoding
2012 transformation, a few functions use it for achieving an actual recoding.
2013 Either input or output of a recoding may be string, an in-memory buffer,
2014 or a file.
2015
2016 Functions with names like
2017 @code{recode_@var{input-type}_to_@var{output-type}} request an actual
2018 recoding, and are described below. It is easy to remember which arguments
2019 each function accepts, once grasped some simple principles for each
2020 possible @var{type}. However, one of the recoding function escapes these
2021 principles and is discussed separately, first.
2022
2023 @example
2024 recode_string (@var{request}, @var{string});
2025 @end example
2026
2027 @findex recode_string
2028 The function @code{recode_string} recodes @var{string} according
2029 to @var{request}, and directly returns the resulting recoded string
2030 freshly allocated, or @code{NULL} if the recoding could not succeed for
2031 some reason. When this function is used, it is the responsibility of
2032 the programmer to ensure that the memory used by the returned string is
2033 later reclaimed.
2034
2035 @findex recode_string_to_buffer
2036 @findex recode_string_to_file
2037 @findex recode_buffer_to_buffer
2038 @findex recode_buffer_to_file
2039 @findex recode_file_to_buffer
2040 @findex recode_file_to_file
2041 @example
2042 char *recode_string_to_buffer (@var{request},
2043 @var{input_string},
2044 &@var{output_buffer}, &@var{output_length}, &@var{output_allocated});
2045 bool recode_string_to_file (@var{request},
2046 @var{input_file},
2047 @var{output_file});
2048 bool recode_buffer_to_buffer (@var{request},
2049 @var{input_buffer}, @var{input_length},
2050 &@var{output_buffer}, &@var{output_length}, &@var{output_allocated});
2051 bool recode_buffer_to_file (@var{request},
2052 @var{input_buffer}, @var{input_length},
2053 @var{output_file});
2054 bool recode_file_to_buffer (@var{request},
2055 @var{input_file},
2056 &@var{output_buffer}, &@var{output_length}, &@var{output_allocated});
2057 bool recode_file_to_file (@var{request},
2058 @var{input_file},
2059 @var{output_file});
2060 @end example
2061
2062 All these functions return a @code{bool} result, @code{false} meaning that
2063 the recoding was not successful, often because of reversibility issues.
2064 The name of the function well indicates on which types it reads and which
2065 type it produces. Let's discuss these three types in turn.
2066
2067 @table @asis
2068 @item string
2069
2070 A string is merely an in-memory buffer which is terminated by a @code{NUL}
2071 character (using as many bytes as needed), instead of being described
2072 by a byte length. For input, a pointer to the buffer is given through
2073 one argument.
2074
2075 It is notable that there is no @code{to_string} functions. Only one
2076 function recodes into a string, and it is @code{recode_string}, which
2077 has already been discussed separately, above.
2078
2079 @item buffer
2080
2081 A buffer is a sequence of bytes held in computer memory. For input, two
2082 arguments provide a pointer to the start of the buffer and its byte size.
2083 Note that for charsets using many bytes per character, the size is given
2084 in bytes, not in characters.
2085
2086 For output, three arguments provide the address of three variables, which
2087 will receive the buffer pointer, the used buffer size in bytes, and the
2088 allocated buffer size in bytes. If at the time of the call, the buffer
2089 pointer is @code{NULL}, then the allocated buffer size should also be zero,
2090 and the buffer will be allocated afresh by the recoding functions. However,
2091 if the buffer pointer is not @code{NULL}, it should be already allocated,
2092 the allocated buffer size then gives its size. If the allocated size
2093 gets exceeded while the recoding goes, the buffer will be automatically
2094 reallocated bigger, probably elsewhere, and the allocated buffer size will
2095 be adjusted accordingly.
2096
2097 The second variable, giving the in-memory buffer size, will receive the
2098 exact byte size which was needed for the recoding. A @code{NUL} character
2099 is guaranteed at the end of the produced buffer, but is not counted in the
2100 byte size of the recoding. Beyond that @code{NUL}, there might be some
2101 extra space after the recoded data, extending to the allocated buffer size.
2102
2103 @item file
2104
2105 @findex recode_filter_open@r{, not available}
2106 @findex recode_filter_close@r{, not available}
2107 A file is a sequence of bytes held outside computer memory, but
2108 buffered through it. For input, one argument provides a pointer to a
2109 file already opened for read. The file is then read and recoded from its
2110 current position until the end of the file, effectively swallowing it in
2111 memory if the destination of the recoding is a buffer. For reading a file
2112 filtered through the recoding library, but only a little bit at a time, one
2113 should rather use @code{recode_filter_open} and @code{recode_filter_close}
2114 (these two functions are not yet available).
2115
2116 For output, one argument provides a pointer to a file already opened
2117 for write. The result of the recoding is written to that file starting
2118 at its current position.
2119 @end table
2120 @end itemize
2121
2122 @findex recode_format_table
2123 The following special function is still subject to change:
2124
2125 @example
2126 void recode_format_table (@var{request}, @var{language}, "@var{name}");
2127 @end example
2128
2129 @noindent
2130 and is not documented anymore for now.
2131
2132 @node Task level, Charset level, Request level, Library
2133 @section Task level functions
2134 @cindex task level functions
2135
2136 The task level functions are used internally by the request level
2137 functions, they allow more explicit control over files and memory
2138 buffers holding input and output to recoding processes. The interface
2139 specification of task level functions is still subject to change a bit.
2140
2141 To get started with task level functions, here is a full example of a
2142 program which sole job is to filter @code{ibmpc} code on its standard input
2143 into @code{latin1} code on its standard output. That is, this program has
2144 the same goal as the one from the previous section, but does its things
2145 a bit differently.
2146
2147 @example
2148 @group
2149 #include <stdbool.h>
2150 #include <stdlib.h>
2151 #include <recodext.h>
2152
2153 const char *program_name;
2154
2155 int
2156 main (int argc, char *const *argv)
2157 @{
2158 program_name = argv[0];
2159 RECODE_OUTER outer = recode_new_outer (0);
2160 RECODE_REQUEST request = recode_new_request (outer);
2161 RECODE_TASK task;
2162 bool success;
2163
2164 recode_scan_request (request, "ibmpc..latin1");
2165
2166 task = recode_new_task (request);
2167 task->input.name = "";
2168 task->output.name = "";
2169 success = recode_perform_task (task);
2170
2171 recode_delete_task (task);
2172 recode_delete_request (request);
2173 recode_delete_outer (outer);
2174
2175 exit (success ? EXIT_SUCCESS : EXIT_FAILURE);
2176 @}
2177 @end group
2178 @end example
2179
2180 @cindex @code{recodext.h} header
2181 Note that in the example above, @code{recodext.h} header is used instead
2182 of @code{recode.h}. By doing so, the various structures are not opaque
2183 anymore, and their fields may be accessed by name.
2184
2185 @vindex RECODE_TASK structure
2186 The header file @code{recode.h} declares a @code{RECODE_TASK}
2187 structure, which the programmer should use for allocating a variable in
2188 his program. This @code{task} variable is given as a first argument to
2189 all task level functions. The programmer ought to change and possibly
2190 consult a few fields in this structure, using special functions.
2191
2192 @itemize @bullet
2193 @item Initialisation functions
2194 @cindex initialisation functions, task
2195
2196 @findex recode_new_task
2197 @findex recode_delete_task
2198 @example
2199 RECODE_TASK recode_new_task (@var{request});
2200 bool recode_delete_task (@var{task});
2201 @end example
2202
2203 No @var{task} variable may be used in other task level functions
2204 of the recoding library without having first been initialised with
2205 @code{recode_new_task}. There may be many such @var{task} variables,
2206 in which case, they are independent of one another and they all need to be
2207 initialised separately. To avoid memory leaks, a @var{task} variable should
2208 not be initialised a second time without calling @code{recode_delete_task} to
2209 ``un-initialise'' it. This function also accepts a @var{request} argument
2210 and associates the request to the task. In fact, a task is essentially
2211 a set of recoding transformations with the specification for its current
2212 input and its current output.
2213
2214 The @var{request} variable may be scanned before or after the call to
2215 @code{recode_new_task}, it does not matter so far. Immediately after
2216 initialisation, before further changes, the @var{task} variable associates
2217 @var{request} empty in-memory buffers for both input and output.
2218 The output buffer will later get allocated automatically on the fly,
2219 as needed, by various task processors.
2220
2221 Even if a call to @code{recode_delete_task} is not strictly mandatory
2222 before ending the program, it is cleaner to always include it. Moreover,
2223 in some future version of the recoding library, it might become required.
2224
2225 @item Fields of @code{struct task_request}
2226 @vindex task_request structure
2227
2228 Here are the fields of a @code{struct task_request} which may be meaningfully
2229 changed, once a @var{task} has been initialised by @code{recode_new_task}.
2230 In fact, fields are expected to change. Once again, to access the fields,
2231 you need to include @file{recodext.h} @emph{instead} of @file{recode.h},
2232 in which case there also is a greater chance that you need to recompile
2233 your programs if a new version of the recoding library gets installed.
2234
2235 @table @code
2236 @item request
2237
2238 The field @code{request} points to the current recoding request, but may
2239 be changed as needed between recoding calls, for example when there is
2240 a need to achieve the construction of a resulting text made up of many
2241 pieces, each being recoded differently.
2242
2243 @item input.name
2244 @itemx input.file
2245
2246 If @code{input.name} is not @code{NULL} at start of a recoding, this is
2247 a request that a file by that name be first opened for reading and later
2248 automatically closed once the whole file has been read. If the file name is
2249 not @code{NULL} but an empty string, it means that standard input is to
2250 be used. The opened file pointer is then held into @code{input.file}.
2251
2252 If @code{input.name} is @code{NULL} and @code{input.file} is not, than
2253 @code{input.file} should point to a file already opened for read, which
2254 is meant to be recoded.
2255
2256 @item input.buffer
2257 @itemx input.cursor
2258 @itemx input.limit
2259
2260 When both @code{input.name} and @code{input.file} are @code{NULL}, three
2261 pointers describe an in-memory buffer containing the text to be recoded.
2262 The buffer extends from @code{input.buffer} to @code{input.limit},
2263 yet the text to be recoded only extends from @code{input.cursor} to
2264 @code{input.limit}. In most situations, @code{input.cursor} starts with
2265 the value that @code{input.buffer} has. (Its value will internally advance
2266 as the recoding goes, until it reaches the value of @code{input.limit}.)
2267
2268 @item output.name
2269 @itemx output.file
2270
2271 If @code{output.name} is not @code{NULL} at start of a recoding, this
2272 is a request that a file by that name be opened for write and later
2273 automatically closed after the recoding is done. If the file name is
2274 not @code{NULL} but an empty string, it means that standard output is to
2275 be used. The opened file pointer is then held into @code{output.file}.
2276 If several passes with intermediate files are needed to produce the
2277 recoding, the @code{output.name} file is opened only for the final pass.
2278
2279 If @code{output.name} is @code{NULL} and @code{output.file} is not, then
2280 @code{output.file} should point to a file already opened for write, which
2281 will receive the result of the recoding.
2282
2283 @item output.buffer
2284 @itemx output.cursor
2285 @itemx output.limit
2286
2287 When both @code{output.name} and @code{output.file} are @code{NULL}, three
2288 pointers describe an in-memory buffer meant to receive the text, once it
2289 is recoded. The buffer is already allocated from @code{output.buffer}
2290 to @code{output.limit}. In most situations, @code{output.cursor} starts
2291 with the value that @code{output.buffer} has. Once the recoding is done,
2292 @code{output.cursor} will point at the next free byte in the buffer,
2293 just after the recoded text, so another recoding could be called without
2294 changing any of these three pointers, for appending new information to it.
2295 The number of recoded bytes in the buffer is the difference between
2296 @code{output.cursor} and @code{output.buffer}.
2297
2298 Each time @code{output.cursor} reaches @code{output.limit}, the buffer
2299 is reallocated bigger, possibly at a different location in memory, always
2300 held up-to-date in @code{output.buffer}. It is still possible to call a
2301 task level function with no output buffer at all to start with, in which
2302 case all three fields should have @code{NULL} as a value. This is the
2303 situation immediately after a call to @code{recode_new_task}.
2304
2305 @item byte_order_mark
2306 @vindex byte_order_mark
2307 This field, which is preset to @code{true}, indicates that a byte order
2308 mark is to be expected at the beginning of any canonical @code{UCS-2}
2309 or @code{UTF-16} text, and that such a byte order mark should be also
2310 produced for these charsets.
2311
2312 @item fail_level
2313 @vindex fail_level
2314 This field, which is of type @code{enum recode_error} (@pxref{Errors}),
2315 sets the error level at which task level functions should report a failure.
2316 If an error being detected is equal or greater than @code{fail_level},
2317 the function will eventually return @code{false} instead of @code{true}.
2318 The preset value for this field is @code{RECODE_NOT_CANONICAL}, that means
2319 that if not reset to another value, the library will report failure on
2320 @emph{any} error.
2321
2322 @item abort_level
2323 @vindex abort_level
2324 @vindex RECODE_MAXIMUM_ERROR
2325 This field, which is of type @code{enum recode_error} (@pxref{Errors}), sets
2326 the error level at which task level functions should immediately interrupt
2327 their processing. If an error being detected is equal or greater than
2328 @code{abort_level}, the function returns immediately, but the returned
2329 value (@code{true} or @code{false}) is still is decided from the setting
2330 of @code{fail_level}, not @code{abort_level}. The preset value for this
2331 field is @code{RECODE_MAXIMUM_ERROR}, that means that is not reset to
2332 another value, the library will never interrupt a recoding task.
2333
2334 @item error_so_far
2335 @vindex error_so_far
2336 This field, which is of type @code{enum recode_error} (@pxref{Errors}),
2337 maintains the maximum error level met so far while the recoding task
2338 was proceeding. The preset value is @code{RECODE_NO_ERROR}.
2339 @end table
2340
2341 @item Task execution
2342 @cindex task execution
2343
2344 @findex recode_perform_task
2345 @findex recode_filter_open
2346 @findex recode_filter_close
2347 @example
2348 recode_perform_task (@var{task});
2349 recode_filter_open (@var{task}, @var{file});
2350 recode_filter_close (@var{task});
2351 @end example
2352
2353 The function @code{recode_perform_task} reads as much input as possible,
2354 and recode all of it on prescribed output, given a properly initialised
2355 @var{task}.
2356
2357 Functions @code{recode_filter_open} and @code{recode_filter_close} are
2358 only planned for now. They are meant to read input in piecemeal ways.
2359 Even if functionality already exists informally in the library, it has
2360 not been made available yet through such interface functions.
2361 @end itemize
2362
2363 @node Charset level, Errors, Task level, Library
2364 @section Charset level functions
2365 @cindex charset level functions
2366
2367 @cindex internal functions
2368 Many functions are internal to the recoding library. Some of them
2369 have been made external and available, for the @code{recode} program
2370 had to retain all its previous functionality while being transformed
2371 into a mere application of the recoding library. These functions are
2372 not really documented here for the time being, as we hope that many of
2373 them will vanish over time. When this set of routines will stabilise,
2374 it would be convenient to document them as an API for handling charset
2375 names and contents.
2376
2377 @findex find_charset
2378 @findex list_all_charsets
2379 @findex list_concise_charset
2380 @findex list_full_charset
2381 @example
2382 RECODE_CHARSET find_charset (@var{name}, @var{cleaning-type});
2383 bool list_all_charsets (@var{charset});
2384 bool list_concise_charset (@var{charset}, @var{list-format});
2385 bool list_full_charset (@var{charset});
2386 @end example
2387
2388 @node Errors, , Charset level, Library
2389 @section Handling errors
2390 @cindex error handling
2391 @cindex handling errors
2392
2393 @cindex error messages
2394 The @code{recode} program, while using the Recode library, needs to
2395 control whether recoding problems are reported or not, and then reflect
2396 these in the exit status. The program should also instruct the library
2397 whether the recoding should be abruptly interrupted when an error is
2398 met (so sparing processing when it is known in advance that a wrong
2399 result would be discarded anyway), or if it should proceed nevertheless.
2400 Here is how the library groups errors into levels, listed here in order
2401 of increasing severity.
2402
2403 @table @code
2404 @item RECODE_NO_ERROR
2405 @vindex RECODE_NO_ERROR
2406
2407 No error was met on previous library calls.
2408
2409 @item RECODE_NOT_CANONICAL
2410 @vindex RECODE_NOT_CANONICAL
2411 @cindex non canonical input, error message
2412
2413 The input text was using one of the many alternative codings for some
2414 phenomenon, but not the one Recode would have canonically generated.
2415 So, if the reverse recoding is later attempted, it would produce a text
2416 having the same @emph{meaning} as the original text, yet not being byte
2417 identical.
2418
2419 For example, a @code{Base64} block in which end-of-lines appear elsewhere
2420 that at every 76 characters is not canonical. An e-circumflex in @TeX{}
2421 which is coded as @samp{\^@{e@}} instead of @samp{\^e} is not canonical.
2422
2423 @item RECODE_AMBIGUOUS_OUTPUT
2424 @vindex RECODE_AMBIGUOUS_OUTPUT
2425 @cindex ambiguous output, error message
2426
2427 It has been discovered that if the reverse recoding was attempted on
2428 the text output by this recoding, we would not obtain the original text,
2429 only because an ambiguity was generated by accident in the output text.
2430 This ambiguity would then cause the wrong interpretation to be taken.
2431
2432 Here are a few examples. If the @code{Latin-1} sequence @samp{e^}
2433 is converted to Easy French and back, the result will be interpreted
2434 as e-circumflex and so, will not reflect the intent of the original two
2435 characters. Recoding an @code{IBM-PC} text to @code{Latin-1} and back,
2436 where the input text contained an isolated @kbd{LF}, will have a spurious
2437 @kbd{CR} inserted before the @kbd{LF}.
2438
2439 Currently, there are many cases in the library where the production of
2440 ambiguous output is not properly detected, as it is sometimes a difficult
2441 problem to accomplish this detection, or to do it speedily.
2442
2443 @item RECODE_UNTRANSLATABLE
2444 @vindex RECODE_UNTRANSLATABLE
2445 @cindex untranslatable input, error message
2446
2447 One or more input character could not be recoded, because there is just
2448 no representation for this character in the output charset.
2449
2450 Here are a few examples. Non-strict mode often allows Recode to
2451 compute on-the-fly mappings for unrepresentable characters, but strict
2452 mode prohibits such attribution of reversible translations: so strict
2453 mode might often trigger such an error. Most @code{UCS-2} codes used to
2454 represent Asian characters cannot be expressed in various Latin charsets.
2455
2456 Since iconv does not distinguish untranslatable from invalid input,
2457 Recode has to use a workaround to detect when input is untranslatable.
2458 Unfortunately, it cannot currently tell how much input is untranslatable,
2459 so it cannot reliably skip such input: typically the input is then diagnosed
2460 as invalid. Two possible workarounds are to set the @code{abort_level}
2461 to @code{RECODE_UNTRANSLATABLE}, or not to use iconv.
2462
2463 @item RECODE_INVALID_INPUT
2464 @vindex RECODE_INVALID_INPUT
2465 @cindex invalid input, error message
2466
2467 The input text does not comply with the coding it is declared to hold. So,
2468 there is no way by which a reverse recoding would reproduce this text,
2469 because Recode should never produce invalid output.
2470
2471 Here are a few examples. In strict mode, @code{ASCII} text is not allowed
2472 to contain characters with the eight bit set. @code{UTF-8} encodings
2473 ought to be minimal@footnote{The minimality of an @code{UTF-8} encoding
2474 is guaranteed on output, but currently, it is not checked on input.}.
2475
2476 @item RECODE_SYSTEM_ERROR
2477 @vindex RECODE_SYSTEM_ERROR
2478 @cindex system detected problem, error message
2479
2480 The underlying system reported an error while the recoding was going on,
2481 likely an input/output error.
2482 (This error symbol is currently unused in the library.)
2483
2484 @item RECODE_USER_ERROR
2485 @vindex RECODE_USER_ERROR
2486 @cindex misuse of recoding library, error message
2487
2488 The programmer or user requested something the recoding library is unable
2489 to provide, or used the API wrongly.
2490 (This error symbol is currently unused in the library.)
2491
2492 @item RECODE_INTERNAL_ERROR
2493 @vindex RECODE_INTERNAL_ERROR
2494 @cindex internal recoding bug, error message
2495
2496 Something really wrong, which should normally never happen, was detected
2497 within the recoding library. This might be due to genuine bugs in the
2498 library, or maybe due to un-initialised or overwritten arguments to
2499 the API.
2500 (This error symbol is currently unused in the library.)
2501
2502 @item RECODE_MAXIMUM_ERROR
2503 @vindex RECODE_MAXIMUM_ERROR
2504
2505 This error code should never be returned, it is only internally used as
2506 a sentinel for the list of all possible error codes.
2507 @end table
2508
2509 @cindex error level threshold
2510 @cindex threshold for error reporting
2511 One should be able to set the error level threshold for returning failure
2512 at end of recoding, and also the threshold for immediate interruption.
2513 If many errors occur while the recoding proceed, which are not severe
2514 enough to interrupt the recoding, then the most severe error is retained,
2515 while others are forgotten@footnote{Another approach would have been
2516 to define the level symbols as masks instead, and to give masks to
2517 threshold setting routines, and to retain all errors---yet I never
2518 met myself such a need in practice, and so I fear it would be overkill.
2519 On the other hand, it might be interesting to maintain counters about
2520 how many times each kind of error occurred.}. So, in case of an error,
2521 the possible actions currently are:
2522
2523 @itemize @bullet
2524 @item do nothing and let go, returning success at end of recoding,
2525 @item just let go for now, but return failure at end of recoding,
2526 @item interrupt recoding right away and return failure now.
2527 @end itemize
2528
2529 @noindent
2530 @xref{Task level}, and particularly the description of the fields
2531 @code{fail_level}, @code{abort_level} and @code{error_so_far}, for more
2532 information about how errors are handled.
2533
2534 @ignore
2535 @c FIXME: Take a look at these matters, indeed.
2536
2537 A last topic around errors is where the error related fields are kept.
2538 To work nicely with threads, my feeling is that the main API levels (based
2539 on either of @code{struct recode_outer}, @code{struct recode_request}
2540 or @code{struct recode_task}) should each have their error thresholds,
2541 values, and last explicit message strings. Thresholds would be inherited
2542 by requests from outers, and by tasks from requests. Error values and
2543 strings would be automatically propagated out from tasks to requests,
2544 for these request level routines which internally set up and use recoding
2545 tasks.
2546
2547 One simple way to avoid locking while sparing the initialisation of many
2548 identical requests, a programmer could prepare the common request before
2549 splitting threads, and merely @emph{copy} the @code{struct recode_request}
2550 so each thread has its own copy---either using a mere assignment or
2551 @code{memcpy}. The same could be said for @code{struct recode_outer}
2552 or @code{struct recode_task} blocks, yet it makes less sense to me to do
2553 so in practice.
2554 @end ignore
2555
2556 @node Universal, iconv, Library, Top
2557 @chapter The universal charset
2558
2559 @cindex ISO 10646
2560 Standard @w{ISO 10646} defines a universal character set, intended to encompass
2561 in the long run all languages written on this planet. It is based on
2562 wide characters, and offer possibilities for two billion characters
2563 (@math{2^31}).
2564
2565 @tindex UCS
2566 This charset was to become available in Recode under the name
2567 @code{UCS}, with many external surfaces for it. But in the current
2568 version, only surfaces of @code{UCS} are offered, each presented as a
2569 genuine charset rather than a surface. Such surfaces are only meaningful
2570 for the @code{UCS} charset, so it is not that useful to draw a line
2571 between the surfaces and the only charset to which they may apply.
2572
2573 @tindex UTF-1
2574 @code{UCS} stands for Universal Character Set. @code{UCS-2} and
2575 @code{UCS-4} are fixed length encodings, using two or four bytes per
2576 character respectively. @code{UTF} stands for @code{UCS} Transformation
2577 Format, and are variable length encodings dedicated to @code{UCS}.
2578 @code{UTF-1} was based on @w{ISO 2022}, it did not succeed@footnote{It is not
2579 probable that Recode will ever support @code{UTF-1}.}. @code{UTF-2}
2580 replaced it, it has been called @code{UTF-FSS} (File System Safe) in
2581 Unicode or Plan9 context, but is better known today as @code{UTF-8}.
2582 To complete the picture, there is @code{UTF-16} based on 16 bits bytes,
2583 and @code{UTF-7} which is meant for transmissions limited to 7-bit bytes.
2584 Most often, one might see @code{UTF-8} used for external storage, and
2585 @code{UCS-2} used for internal storage.
2586
2587 @c FIXME: the manual never explains what the U+NNNN notation means!
2588 When Recode is producing any representation of @code{UCS},
2589 it uses the replacement character @code{U+FFFD} for any @emph{valid}
2590 character which is not representable in the goal charset@footnote{This
2591 is when the goal charset allows for 16-bits. For shorter charsets,
2592 the @samp{--strict} (@samp{-s}) option decides what happens: either the
2593 character is dropped, or a reversible mapping is produced on the fly.}.
2594 This happens, for example, when @code{UCS-2} is not capable to echo a
2595 wide @code{UCS-4} character, or for a similar reason, an @code{UTF-8}
2596 sequence using more than three bytes. The replacement character is
2597 meant to represent an existing character. So, it is never produced to
2598 represent an invalid sequence or ill-formed character in the input text.
2599 In such cases, Recode just gets rid of the noise, while taking note
2600 of the error in its usual ways.
2601
2602 Even if @code{UTF-8} is an encoding, really, it is the encoding of a single
2603 character set, and nothing else. It is useful to distinguish between an
2604 encoding (a @emph{surface} within Recode) and a charset, but only
2605 when the surface may be applied to several charsets. Specifying a charset
2606 is a bit simpler than specifying a surface in a Recode request.
2607 There would not be a practical advantage at imposing a more complex syntax
2608 to Recode users, when it is simple to assimilate @code{UTF-8} to
2609 a charset. Similar considerations apply for @code{UCS-2}, @code{UCS-4},
2610 @code{UTF-16} and @code{UTF-7}. These are all considered to be charsets.
2611
2612 @menu
2613 * UCS-2:: Universal Character Set, 2 bytes
2614 * UCS-4:: Universal Character Set, 4 bytes
2615 * UTF-7:: Universal Transformation Format, 7 bits
2616 * UTF-8:: Universal Transformation Format, 8 bits
2617 * UTF-16:: Universal Transformation Format, 16 bits
2618 * count-characters:: Frequency count of characters
2619 * dump-with-names:: Fully interpreted UCS dump
2620 @end menu
2621
2622 @node UCS-2, UCS-4, Universal, Universal
2623 @section Universal Character Set, 2 bytes
2624
2625 @tindex UCS-2
2626 @cindex Unicode
2627 One surface of @code{UCS} is usable for the subset defined by its first
2628 sixty thousand characters (in fact, @math{31 * 2^11} codes), and uses
2629 exactly two bytes per character. It is a mere dump of the internal
2630 memory representation which is @emph{natural} for this subset and as such,
2631 conveys with it endianness problems.
2632
2633 @cindex byte order mark
2634 A non-empty @code{UCS-2} file normally begins with a so called @dfn{byte
2635 order mark}, having value @code{0xFEFF}. The value @code{0xFFFE} is not an
2636 @code{UCS} character, so if this value is seen at the beginning of a file,
2637 Recode reacts by swapping all pairs of bytes. The library also
2638 properly reacts to other occurrences of @code{0xFEFF} or @code{0xFFFE}
2639 elsewhere than at the beginning, because concatenation of @code{UCS-2}
2640 files should stay a simple matter, but it might trigger a diagnostic
2641 about non canonical input.
2642
2643 By default, when producing an @code{UCS-2} file, Recode always outputs
2644 the high order byte before the low order byte. Note that this may not
2645 be the case if @code{iconv} is used; in that case, you may be able to
2646 use the charset @code{UCS-2BE} to specify big-endian UCS-2. The order
2647 can also be easily overridden through the @code{21-Permutation} surface
2648 (@pxref{Permutations}). For example, the command:
2649
2650 @example
2651 recode u8..u2/21 < @var{input} > @var{output}
2652 @end example
2653
2654 @noindent
2655 asks for an @code{UTF-8} to @code{UCS-2} conversion, with swapped byte
2656 output.
2657
2658 @tindex ISO-10646-UCS-2, and aliases
2659 @tindex BMP
2660 @tindex rune
2661 @tindex u2
2662 Use @code{UCS-2} as a genuine charset. This charset is available in
2663 Recode under the name @code{ISO-10646-UCS-2}. Accepted aliases
2664 are @code{UCS-2}, @code{BMP}, @code{rune} and @code{u2}.
2665
2666 @tindex combined-UCS-2
2667 @cindex combining characters
2668 The Recode library is able to combine @code{UCS-2} some sequences
2669 of codes into single code characters, to represent a few diacriticized
2670 characters, ligatures or diphtongs which have been included to ease
2671 mapping with other existing charsets. It is also able to explode
2672 such single code characters into the corresponding sequence of codes.
2673 The request syntax for triggering such operations is rudimentary and
2674 temporary. The @code{combined-UCS-2} pseudo character set is a special
2675 form of @code{UCS-2} in which known combinings have been replaced by the
2676 simpler code. Using @code{combined-UCS-2} instead of @code{UCS-2} in an
2677 @emph{after} position of a request forces a combining step, while using
2678 @code{combined-UCS-2} instead of @code{UCS-2} in a @emph{before} position
2679 of a request forces an exploding step. For the time being, one has to
2680 resort to advanced request syntax to achieve other effects. For example:
2681
2682 @example
2683 recode u8..co,u2..u8 < @var{input} > @var{output}
2684 @end example
2685
2686 @noindent
2687 copies an @code{UTF-8} @var{input} over @var{output}, still to be in
2688 @code{UTF-8}, yet merging combining characters into single codes whenever
2689 possible.
2690
2691 @node UCS-4, UTF-7, UCS-2, Universal
2692 @section Universal Character Set, 4 bytes
2693
2694 @tindex UCS-4
2695 Another surface of @code{UCS} uses exactly four bytes per character, and is
2696 a mere dump of the internal memory representation which is @emph{natural}
2697 for the whole charset and as such, conveys with it endianness problems.
2698
2699 @tindex ISO-10646-UCS-4, and aliases
2700 @tindex ISO_10646
2701 @tindex 10646
2702 @tindex u4
2703 Use it as a genuine charset. This charset is available in Recode
2704 under the name @code{ISO-10646-UCS-4}. Accepted aliases are @code{UCS},
2705 @code{UCS-4}, @code{ISO_10646}, @code{10646} and @code{u4}.
2706
2707 @node UTF-7, UTF-8, UCS-4, Universal
2708 @section Universal Transformation Format, 7 bits
2709
2710 @tindex UTF-7
2711 @code{UTF-7} comes from IETF rather than ISO, and is described by @w{RFC
2712 2152}, in the MIME series. The @code{UTF-7} encoding is meant to fit
2713 @code{UCS-2} over channels limited to seven bits per byte. It proceeds
2714 from a mix between the spirit of @code{Quoted-Printable} and methods of
2715 @code{Base64}, adapted to Unicode contexts.
2716
2717 @tindex UNICODE-1-1-UTF-7, and aliases
2718 @tindex TF-7
2719 @tindex u7
2720 This charset is available in Recode under the name
2721 @code{UNICODE-1-1-UTF-7}. Accepted aliases are @code{UTF-7}, @code{TF-7}
2722 and @code{u7}.
2723
2724 @node UTF-8, UTF-16, UTF-7, Universal
2725 @section Universal Transformation Format, 8 bits
2726
2727 @tindex UTF-8
2728 Even if @code{UTF-8} does not originally come from IETF, there is now
2729 @w{RFC 2279} to describe it. In letters sent on 1995-01-21 and 1995-04-20,
2730 Markus Kuhn writes:
2731
2732 @quotation
2733 @code{UTF-8} is an @code{ASCII} compatible multi-byte encoding of the @w{ISO
2734 10646} universal character set (@code{UCS}). @code{UCS} is a 31-bit superset
2735 of all other character set standards. The first 256 characters of @code{UCS}
2736 are identical to those of @w{ISO 8859-1} (@w{Latin-1}). The @code{UCS-2}
2737 encoding of UCS is a sequence of bigendian 16-bit words, the @code{UCS-4}
2738 encoding is a sequence of bigendian 32-bit words. The @code{UCS-2} subset
2739 of @w{ISO 10646} is also known as ``Unicode''. As both @code{UCS-2}
2740 and @code{UCS-4} require heavy modifications to traditional @code{ASCII}
2741 oriented system designs (e.g. Unix), the @code{UTF-8} encoding has been
2742 designed for these applications.
2743
2744 In @code{UTF-8}, only @code{ASCII} characters are encoded using bytes
2745 below 128. All other non-ASCII characters are encoded as multi-byte
2746 sequences consisting only of bytes in the range 128-253. This avoids
2747 critical bytes like @kbd{NUL} and @kbd{/} in @code{UTF-8} strings, which
2748 makes the @code{UTF-8} encoding suitable for being handled by the standard
2749 C string library and being used in Unix file names. Other properties
2750 include the preserved lexical sorting order and that @code{UTF-8} allows
2751 easy self-synchronisation of software receiving @code{UTF-8} strings.
2752 @end quotation
2753
2754 @code{UTF-8} is the most common external surface of @code{UCS}, each
2755 character uses from one to six bytes, and is able to encode all @math{2^31}
2756 characters of the @code{UCS}. It is implemented as a charset, with the
2757 following properties:
2758
2759 @itemize @bullet
2760 @item
2761 Strict 7-bit @code{ASCII} is completely invariant under @code{UTF-8},
2762 and those are the only one-byte characters. @code{UCS} values and
2763 @code{ASCII} values coincide. No multi-byte characters ever contain bytes
2764 less than 128. @code{NUL} @emph{is} @code{NUL}. A multi-byte character
2765 always starts with a byte of 192 or more, and is always followed by a
2766 number of bytes between 128 to 191. That means that you may read at
2767 random on disk or memory, and easily discover the start of the current,
2768 next or previous character. You can count, skip or extract characters
2769 with this only knowledge.
2770
2771 @item
2772 If you read the first byte of a multi-byte character in binary, it contains
2773 many @samp{1} bits in successions starting with the most significant one
2774 (from the left), at least two. The length of this @samp{1} sequence equals
2775 the byte size of the character. All succeeding bytes start by @samp{10}.
2776 This is a lot of redundancy, making it fairly easy to guess that a file
2777 is valid @code{UTF-8}, or to safely state that it is not.
2778
2779 @item
2780 In a multi-byte character, if you remove all leading @samp{1} bits of the
2781 first byte of a multi-byte character, and the initial @samp{10} bits of
2782 all remaining bytes (so keeping 6 bits per byte for those), the remaining
2783 bits concatenated are the UCS value.
2784 @end itemize
2785
2786 @noindent
2787 These properties also have a few nice consequences:
2788
2789 @itemize @bullet
2790 @item
2791 Conversion to/from values is algorithmically simple, and reasonably speedy.
2792
2793 @item
2794 A sequence of @var{N} bytes can hold characters needing up to 2 + 5@var{N}
2795 bits in their @code{UCS} representation. Here, @var{N} is a number between
2796 1 and 6. So, @code{UTF-8} is most economical when mapping ASCII (1 byte),
2797 followed by @code{UCS-2} (1 to 3 bytes) and @code{UCS-4} (1 to 6 bytes).
2798
2799 @item
2800 The lexicographic sorting order of @code{UCS} strings is preserved.
2801
2802 @item
2803 Bytes with value 254 or 255 never appear, and because of that, these are
2804 sometimes used when escape mechanisms are needed.
2805 @end itemize
2806
2807 In some case, when little processing is done on a lot of strings, one may
2808 choose for efficiency reasons to handle @code{UTF-8} strings directly even
2809 if variable length, as it is easy to get start of characters. Character
2810 insertion or replacement might require moving the remainder of the string
2811 in either direction. In most cases, it is faster and easier to convert
2812 from @code{UTF-8} to @code{UCS-2} or @code{UCS-4} prior to processing.
2813
2814 @tindex UTF-8, aliases
2815 @tindex UTF-FSS
2816 @tindex FSS_UTF
2817 @tindex TF-8
2818 @tindex u8
2819 This charset is available in Recode under the name @code{UTF-8}.
2820 Accepted aliases are @code{UTF-2}, @code{UTF-FSS}, @code{FSS_UTF},
2821 @code{TF-8} and @code{u8}.
2822
2823 @node UTF-16, count-characters, UTF-8, Universal
2824 @section Universal Transformation Format, 16 bits
2825
2826 @tindex UTF-16, and aliases
2827 Another external surface of @code{UCS} is also variable length, each
2828 character using either two or four bytes. It is usable for the subset
2829 defined by the first million characters (@math{17 * 2^16}) of @code{UCS}.
2830
2831 Martin J. D@"urst writes (to @uref{comp.std.internat}, on 1995-03-28):
2832
2833 @quotation
2834 @code{UTF-16} is another method that reserves two times 1024 codepoints in
2835 Unicode and uses them to index around one million additional characters.
2836 @code{UTF-16} is a little bit like former multibyte codes, but quite
2837 not so, as both the first and the second 16-bit code clearly show what
2838 they are. The idea is that one million codepoints should be enough for
2839 all the rare Chinese ideograms and historical scripts that do not fit
2840 into the Base Multilingual Plane of @w{ISO 10646} (with just about 63,000
2841 positions available, now that 2,000 are gone).
2842 @end quotation
2843
2844 @tindex Unicode, an alias for UTF-16
2845 @tindex TF-16
2846 @tindex u6
2847 This charset is available in Recode under the name @code{UTF-16}.
2848 Accepted aliases are @code{Unicode}, @code{TF-16} and @code{u6}.
2849
2850 @node count-characters, dump-with-names, UTF-16, Universal
2851 @section Frequency count of characters
2852
2853 @tindex count-characters
2854 @cindex counting characters
2855 A device may be used to obtain a list of characters in a file, and how many
2856 times each character appears. Each count is followed by the @code{UCS-2}
2857 value of the character and, when known, the @w{RFC 1345} mnemonic for that
2858 character.
2859
2860 This charset is available in Recode under the name
2861 @code{count-characters}.
2862
2863 This @code{count} feature has been implemented as a charset. This may
2864 change in some later version, as it would sometimes be convenient to count
2865 original bytes, instead of their @code{UCS-2} equivalent.
2866
2867 @node dump-with-names, , count-characters, Universal
2868 @section Fully interpreted UCS dump
2869
2870 @tindex dump-with-names
2871 @cindex dumping characters, with description
2872 @cindex character streams, description
2873 @cindex description of individual characters
2874 Another device may be used to get fully interpreted dumps of an @code{UCS-2}
2875 stream of characters, with one @code{UCS-2} character displayed on a full
2876 output line. Each line receives the @w{RFC 1345} mnemonic for the character
2877 if it exists, the @code{UCS-2} value of the character, and a descriptive
2878 comment for that character. As each input character produces its own
2879 output line, beware that the output file from this conversion may be much,
2880 much bigger than the input file.
2881
2882 This charset is available in Recode under the name
2883 @code{dump-with-names}.
2884
2885 This @code{dump-with-names} feature has been implemented as a charset rather
2886 than a surface. This is surely debatable. The current implementation
2887 allows for dumping charsets other than @code{UCS-2}. For example, the
2888 command @w{@samp{recode l2..full < @var{input}}} implies a necessary
2889 conversion from @code{Latin-2} to @code{UCS-2}, as @code{dump-with-names}
2890 is only connected out from @code{UCS-2}. In such cases, Recode
2891 does not display the original @code{Latin-2} codes in the dump, only the
2892 corresponding @code{UCS-2} values. To give a simpler example, the command
2893
2894 @example
2895 echo 'Hello, world!' | recode us..dump
2896 @end example
2897
2898 @noindent
2899 produces the following output:
2900
2901 @example
2902 UCS2 Mne Description
2903
2904 0048 H latin capital letter h
2905 0065 e latin small letter e
2906 006C l latin small letter l
2907 006C l latin small letter l
2908 006F o latin small letter o
2909 002C , comma
2910 0020 SP space
2911 0077 w latin small letter w
2912 006F o latin small letter o
2913 0072 r latin small letter r
2914 006C l latin small letter l
2915 0064 d latin small letter d
2916 0021 ! exclamation mark
2917 000A LF line feed (lf)
2918 @end example
2919
2920 The descriptive comment is given in English and @code{ASCII},
2921 yet if the English description is not available but a French one is, then
2922 the French description is given instead, using @code{Latin-1}. However,
2923 if the @code{LC_MESSAGES} environment variable begins with the letters
2924 @samp{fr}, then listing preference goes to French when both descriptions
2925 are available.
2926
2927 Here is another example. To get the long description of the code 237 in
2928 @w{Latin-5} table, one may use the following command.
2929
2930 @example
2931 echo -n 237 | recode l5/d..dump
2932 @end example
2933
2934 @noindent
2935 If your @code{echo} does not grok @samp{-n}, use @samp{echo 237\c} instead.
2936 Here is how to see what Unicode @code{U+03C6} means, while getting rid of
2937 the title lines.
2938
2939 @example
2940 echo -n 0x03C6 | recode u2/x2..dump | tail +3
2941 @end example
2942
2943 @node iconv, Tabular, Universal, Top
2944 @chapter The @code{iconv} library
2945
2946 @cindex @code{iconv} library
2947 @cindex library, @code{iconv}
2948 @cindex @code{libiconv}
2949 @cindex interface, with @code{iconv} library
2950 @cindex Haible, Bruno
2951 The Recode library is able to use the capabilities of an
2952 external, pre-installed @code{iconv} library, usually as provided by GNU
2953 @code{libc} or the portable @code{libiconv} written by Bruno Haible. In
2954 fact, many capabilities of the Recode library are duplicated in
2955 an external @code{iconv} library, as they likely share many charsets.
2956 We discuss, here, the issues related to this duplication, and other
2957 peculiarities specific to the @code{iconv} library.
2958
2959 The @code{RECODE_STRICT_MAPPING_FLAG} option, corresponding to the
2960 @samp{--strict} flag, is implemented by adding @code{iconv} option
2961 @code{//IGNORE} to the @samp{after} encoding. This has the side effect
2962 that untranslatable input is only signalled at the end of the
2963 conversion, whereas with Recode's built-in conversion routines the error
2964 will be signalled immediately.
2965
2966 If the string @code{-translit} is appended to the @var{after} encoding,
2967 characters being converted are transliterated when needed and possible.
2968 This means that when a character cannot be represented in the target
2969 character set, it can be approximated through one or several similar
2970 looking characters. Characters that are outside of the target character
2971 set and cannot be transliterated are replaced with a question mark (?)
2972 in the output. This corresponds to the @code{iconv} option
2973 @code{//TRANSLIT}.
2974
2975 To check whether @code{iconv} is used for a particular conversion,
2976 just use the @samp{-v} or @samp{--verbose} option, @pxref{Recoding}, and
2977 check whether @samp{:iconv:} appears as an intermediate charset.
2978
2979 @tindex iconv
2980
2981 @anchor{prefer-iconv}
2982 The @code{:iconv:} charset represents a conceptual pivot charset within
2983 the external @code{iconv} library (in fact, this pivot exists, but is
2984 not directly reachable). This charset has a @code{:} (a mere colon) and
2985 @code{:libiconv:} for aliases. It is not allowed to recode from or to
2986 this charset directly. But when this charset is selected as an
2987 intermediate, usually by automatic means, then the external @code{iconv}
2988 Recode library is called to handle the transformations. By using an
2989 @samp{--ignore=:iconv:} option on the @code{recode} call or
2990 equivalently, but more simply, @samp{-x:}, Recode is instructed to avoid
2991 this charset as an intermediate, with the consequence that the external
2992 @code{iconv} library is not used. You can also use
2993 @code{--prefer-iconv} to use @code{iconv} if possible. Consider these
2994 calls:
2995
2996 @example
2997 recode l1..1250 < @var{input} > @var{output}
2998 recode -x: l1..1250 < @var{input} > @var{output}
2999 recode --prefer-iconv l1..1250 < @var{input} > @var{output}
3000 @end example
3001
3002 @noindent
3003 All should transform @var{input} from @code{ISO-8859-1} to @code{CP1250}
3004 on @var{output}. The first call might use the external @code{iconv}
3005 library, while the second call definitely avoids it. The third call
3006 will use the external @code{iconv} library if it supports the required
3007 conversion. Whatever the path used, the results should normally be
3008 identical. However, there might be observable differences. Most of
3009 them might result from reversibility issues, as the external
3010 @code{iconv} engine does not likely address reversibility in the same
3011 way. Even if much less likely, some differences might result from
3012 slight errors in the tables used, such differences should then be
3013 reported as bugs.
3014
3015 Discrepancies might be seen in the area of error detection and recovery.
3016 The Recode library usually tries to detect canonicity errors in
3017 input, and production of ambiguous output, but the external @code{iconv}
3018 library does not necessarily do it the same way. Moreover, the
3019 Recode library may not always recover as nicely as possible when
3020 the external @code{iconv} has no translation for a given character.
3021
3022 The external @code{iconv} libraries may offer different sets of charsets
3023 and aliases from one library to another, and also between successive
3024 versions of a single library. Best is to check the documentation of
3025 the external @code{iconv} library, as of the time Recode was
3026 installed, to know which charsets and aliases are being provided.
3027
3028 The @samp{--ignore=:iconv:} or @samp{-x:} options might be useful when
3029 there is a need to make a recoding more exactly repeatable between
3030 machines or installations, the idea being here to remove the variance
3031 possibly introduced by the various implementations of an external
3032 @code{iconv} library. These options might also help deciding whether if
3033 some recoding problem is genuine to Recode, or is induced by the
3034 external @code{iconv} library.
3035
3036 @node Tabular, ASCII misc, iconv, Top
3037 @chapter Tabular sources (@w{RFC 1345})
3038
3039 @cindex RFC 1345
3040 @cindex character mnemonics, documentation
3041 @cindex @code{chset} tools
3042 An important part of the tabular charset knowledge in Recode
3043 comes from @w{RFC 1345} or, alternatively, from the @code{chset} tools,
3044 both maintained by Keld Simonsen. The @w{RFC 1345} document:
3045
3046 @quotation
3047 ``Character Mnemonics & Character Sets'', K. Simonsen, Request for
3048 Comments no. 1345, Network Working Group, June 1992.
3049 @end quotation
3050
3051 @noindent
3052 @cindex deviations from RFC 1345
3053 defines many character mnemonics and character sets. The Recode
3054 library implements most of @w{RFC 1345}, however:
3055
3056 @itemize @bullet
3057 @item
3058 @tindex dk-us@r{, not recognised by }recode
3059 @tindex us-dk@r{, not recognised by }recode
3060 It does not recognise those charsets which overload character positions:
3061 @code{dk-us} and @code{us-dk}. However, @xref{Mixed}.
3062
3063 @item
3064 @tindex ANSI_X3.110-1983@r{, not recognised by }recode
3065 @tindex ISO_6937-2-add@r{, not recognised by }recode
3066 @tindex T.101-G2@r{, not recognised by }recode
3067 @tindex T.61-8bit@r{, not recognised by }recode
3068 @tindex iso-ir-90@r{, not recognised by }recode
3069 It does not recognise those charsets which combine two characters for
3070 representing a third: @code{ANSI_X3.110-1983}, @code{ISO_6937-2-add},
3071 @code{T.101-G2}, @code{T.61-8bit}, @code{iso-ir-90} and
3072 @code{videotex-suppl}.
3073
3074 @item
3075 @tindex GB_2312-80@r{, not recognised by }recode
3076 @tindex JIS_C6226-1978@r{, not recognised by }recode
3077 @tindex JIS_X0212-1990@r{, not recognised by }recode
3078 @tindex KS_C_5601-1987@r{, not recognised by }recode
3079 It does not recognise 16-bits charsets: @code{GB_2312-80},
3080 @code{JIS_C6226-1978}, @code{JIS_C6226-1983}, @code{JIS_X0212-1990} and
3081 @code{KS_C_5601-1987}.
3082
3083 @item
3084 @tindex isoir91
3085 @tindex isoir92
3086 It interprets the charset @code{isoir91} as @code{NATS-DANO} (alias
3087 @code{iso-ir-9-1}), @emph{not} as @code{JIS_C6229-1984-a} (alias
3088 @code{iso-ir-91}). It also interprets the charset @code{isoir92}
3089 as @code{NATS-DANO-ADD} (alias @code{iso-ir-9-2}), @emph{not} as
3090 @code{JIS_C6229-1984-b} (alias @code{iso-ir-92}). It might be better
3091 just avoiding these two alias names.
3092 @end itemize
3093
3094 Keld Simonsen @email{keld@@dkuug.dk} did most of @w{RFC 1345} himself, with
3095 some funding from Danish Standards and Nordic standards (INSTA) project.
3096 He also did the character set design work, with substantial input from
3097 Olle Jaernefors. Keld typed in almost all of the tables, some have been
3098 contributed. A number of people have checked the tables in various
3099 ways. The RFC lists a number of people who helped.
3100
3101 @cindex Recode, and RFC 1345
3102 Keld and the Recode maintainer have an arrangement by which any new
3103 discovered information submitted by Recode users, about tabular
3104 charsets, is forwarded to Keld, eventually merged into Keld's work,
3105 and only then, reimported into Recode. Recode
3106 does not try to compete, nor even establish itself as
3107 an alternate or diverging reference: @w{RFC 1345} and its new drafts stay the
3108 genuine source for most tabular information conveyed by Recode.
3109 Keld has been more than collaborative so far, so there is no reason that
3110 we act otherwise. In a word, Recode should be perceived as the
3111 application of external references, but not as a reference in itself.
3112
3113 @tindex RFC1345@r{, a charset, and its aliases}
3114 @tindex 1345
3115 @tindex mnemonic@r{, an alias for RFC1345 charset}
3116 Internally, @w{RFC 1345} associates which each character an unambiguous
3117 mnemonic of a few characters, taken from @w{ISO 646}, which is a minimal
3118 ASCII subset of 83 characters. The charset made up by these mnemonics
3119 is available in Recode under the name @code{RFC1345}. It has
3120 @code{mnemonic} and @code{1345} for aliases. As implemened, this charset
3121 exactly corresponds to @code{mnemonic+ascii+38}, using @w{RFC 1345}
3122 nomenclature. Roughly said, @w{ISO 646} characters represent themselves,
3123 except for the ampersand (@kbd{&}) which appears doubled. A prefix of a
3124 single ampersand introduces a mnemonic. For mnemonics using two characters,
3125 the prefix is immediately by the mnemonic. For longer mnemonics, the prefix
3126 is followed by an underline (@kbd{_}), the mmemonic, and another underline.
3127 Conversions to this charset are usually reversible.
3128
3129 Currently, Recode does not offer any of the many other possible
3130 variations of this family of representations.
3131
3132 @table @code
3133 @include rfc1345.texi
3134 @end table
3135
3136 @node ASCII misc, IBM and MS, Tabular, Top
3137 @chapter ASCII and some derivatives
3138
3139 @menu
3140 * ASCII:: Usual ASCII
3141 * ISO 8859:: ASCII extended by Latin Alphabets
3142 * ASCII-BS:: ASCII 7-bits, @kbd{BS} to overstrike
3143 * flat:: ASCII without diacritics nor underline
3144 @end menu
3145
3146 @node ASCII, ISO 8859, ASCII misc, ASCII misc
3147 @section Usual ASCII
3148
3149 @tindex ASCII@r{, an alias for the }ANSI_X3.4-1968@r{ charset}
3150 @tindex ANSI_X3.4-1968@r{, and its aliases}
3151 @tindex IBM367
3152 @tindex US-ASCII
3153 @tindex cp367
3154 @tindex iso-ir-6
3155 @tindex us
3156 This charset is available in Recode under the name @code{ASCII}.
3157 In fact, its true name is @code{ANSI_X3.4-1968} as per @w{RFC 1345},
3158 accepted aliases being @code{ANSI_X3.4-1986}, @code{ASCII},
3159 @code{IBM367}, @code{ISO646-US}, @code{ISO_646.irv:1991},
3160 @code{US-ASCII}, @code{cp367}, @code{iso-ir-6} and @code{us}. The
3161 shortest way of specifying it in Recode is @code{us}.
3162
3163 @cindex ASCII table, recreating with Recode
3164 This documentation used to include ASCII tables. They have been removed
3165 since the @code{recode} program can now recreate these easily:
3166
3167 @example
3168 recode -lf us for commented ASCII
3169 recode -ld us for concise decimal table
3170 recode -lo us for concise octal table
3171 recode -lh us for concise hexadecimal table
3172 @end example
3173
3174 @node ISO 8859, ASCII-BS, ASCII, ASCII misc
3175 @section ASCII extended by Latin Alphabets
3176
3177 @cindex Latin charsets
3178 There are many Latin charsets. The following has been written by Tim
3179 Lasko @email{lasko@@video.dec.com}, a long while ago:
3180
3181 @quotation
3182 ISO @w{Latin-1}, or more completely ISO Latin Alphabet No 1, is now an
3183 international standard as of February 1987 (IS 8859, Part 1). For those
3184 American USEnet'rs that care, the 8-bit ASCII standard, which is essentially
3185 the same code, is going through the final administrative processes prior
3186 to publication. ISO @w{Latin-1} (IS 8859/1) is actually one of an entire
3187 family of eight-bit one-byte character sets, all having ASCII on the left
3188 hand side, and with varying repertoires on the right hand side:
3189
3190 @itemize @bullet
3191 @item
3192 Latin Alphabet No 1 (caters to Western Europe - now approved).
3193 @item
3194 Latin Alphabet No 2 (caters to Eastern Europe - now approved).
3195 @item
3196 Latin Alphabet No 3 (caters to SE Europe + others - in draft ballot).
3197 @item
3198 Latin Alphabet No 4 (caters to Northern Europe - in draft ballot).
3199 @item
3200 Latin-Cyrillic alphabet (right half all Cyrillic - processing currently
3201 suspended pending USSR input).
3202 @item
3203 Latin-Arabic alphabet (right half all Arabic - now approved).
3204 @item
3205 Latin-Greek alphabet (right half Greek + symbols - in draft ballot).
3206 @item
3207 Latin-Hebrew alphabet (right half Hebrew + symbols - proposed).
3208 @end itemize
3209 @end quotation
3210
3211 @tindex Latin-1
3212 The ISO Latin Alphabet 1 is available as a charset in Recode under
3213 the name @code{Latin-1}. In fact, its true name is @code{ISO_8859-1:1987}
3214 as per @w{RFC 1345}, accepted aliases being @code{CP819}, @code{IBM819},
3215 @code{ISO-8859-1}, @code{ISO_8859-1}, @code{iso-ir-100}, @code{l1}
3216 and @code{Latin-1}. The shortest way of specifying it in Recode
3217 is @code{l1}.
3218
3219 @cindex Latin-1 table, recreating with Recode
3220 It is an eight-bit code which coincides with ASCII for the lower half.
3221 This documentation used to include @w{Latin-1} tables. They have been removed
3222 since the @code{recode} program can now recreate these easily:
3223
3224 @example
3225 recode -lf l1 for commented ISO Latin-1
3226 recode -ld l1 for concise decimal table
3227 recode -lo l1 for concise octal table
3228 recode -lh l1 for concise hexadecimal table
3229 @end example
3230
3231 @node ASCII-BS, flat, ISO 8859, ASCII misc
3232 @section ASCII 7-bits, @kbd{BS} to overstrike
3233
3234 @tindex ASCII-BS@r{, and its aliases}
3235 @tindex BS@r{, an alias for }ASCII-BS@r{ charset}
3236 This charset is available in Recode under the name
3237 @code{ASCII-BS}, with @code{BS} as an acceptable alias.
3238
3239 @cindex diacritics, with @code{ASCII-BS} charset
3240 The file is straight ASCII, seven bits only. According to the definition
3241 of ASCII, diacritics are applied by a sequence of three characters: the
3242 letter, one @kbd{BS}, the diacritic mark. We deviate slightly from this
3243 by exchanging the diacritic mark and the letter so, on a screen device, the
3244 diacritic will disappear and let the letter alone. At recognition time,
3245 both methods are acceptable.
3246
3247 The French quotes are coded by the sequences: @w{@kbd{< BS "}} or @w{@kbd{"
3248 BS <}} for the opening quote and @w{@kbd{> BS "}} or @w{@kbd{" BS >}}
3249 for the closing quote. This artificial convention was inherited in
3250 straight @code{ASCII-BS} from habits around @code{Bang-Bang} entry, and
3251 is not well known. But we decided to stick to it so that @code{ASCII-BS}
3252 charset will not lose French quotes.
3253
3254 The @code{ASCII-BS} charset is independent of @code{ASCII}, and
3255 different. The following examples demonstrate this, knowing at advance
3256 that @samp{!2} is the @code{Bang-Bang} way of representing an @kbd{e}
3257 with an acute accent. Compare:
3258
3259 @example
3260 % echo \!2 | recode -v bang..l1/d
3261 Request: Bang-Bang..ISO-8859-1/Decimal-1
3262 233, 10
3263 @end example
3264
3265 @noindent
3266 with:
3267
3268 @example
3269 % echo \!2 | recode -v bang..bs/d
3270 Request: Bang-Bang..ISO-8859-1..ASCII-BS/Decimal-1
3271 39, 8, 101, 10
3272 @end example
3273
3274 In the first case, the @kbd{e} with an acute accent is merely
3275 transmitted by the @code{Latin-1..ASCII} mapping, not having a special
3276 recoding rule for it. In the @code{Latin-1..ASCII-BS} case, the acute
3277 accent is applied over the @kbd{e} with a backspace: diacriticised
3278 characters have special rules. For the @code{ASCII-BS} charset,
3279 reversibility is still possible, but there might be difficult cases.
3280
3281 @node flat, , ASCII-BS, ASCII misc
3282 @section ASCII without diacritics nor underline
3283 @tindex flat@r{, a charset}
3284
3285 This charset is available in Recode under the name @code{flat}.
3286
3287 @cindex diacritics and underlines, removing
3288 @cindex removing diacritics and underlines
3289 This code is ASCII expunged of all diacritics and underlines, as long as
3290 they are applied using three character sequences, with @kbd{BS} in the
3291 middle. Also, despite slightly unrelated, each control character is
3292 represented by a sequence of two or three graphic characters. The newline
3293 character, however, keeps its functionality and is not represented.
3294
3295 Note that charset @code{flat} is a terminal charset. We can convert
3296 @emph{to} @code{flat}, but not @emph{from} it.
3297
3298 @node IBM and MS, CDC, ASCII misc, Top
3299 @chapter Some IBM or Microsoft charsets
3300
3301 @cindex IBM codepages
3302 @cindex codepages
3303 Recode provides various IBM or Microsoft code pages (@pxref{Tabular}).
3304 An easy way to find them all at once out of Recode itself is through the
3305 command:
3306
3307 @example
3308 recode -l | egrep -i '(CP|IBM)[0-9]'
3309 @end example
3310
3311 @noindent
3312 But also, see few special charsets presented in the incoming sections.
3313
3314 @menu
3315 * EBCDIC:: EBCDIC codes
3316 * IBM-PC:: IBM's PC code
3317 * Icon-QNX:: Unisys' Icon code
3318 @end menu
3319
3320 @node EBCDIC, IBM-PC, IBM and MS, IBM and MS
3321 @section EBCDIC code
3322
3323 @cindex EBCDIC charsets
3324 This charset is the IBM's External Binary Coded Decimal for Interchange
3325 Coding. This is an eight bits code. The following three variants were
3326 implemented in Recode independently of @w{RFC 1345}:
3327
3328 @table @code
3329 @item EBCDIC
3330 @tindex EBCDIC@r{, a charset}
3331 In Recode, the @code{us..ebcdic} conversion is identical to @samp{dd
3332 conv=ebcdic} conversion, and Recode @code{ebcdic..us} conversion is
3333 identical to @samp{dd conv=ascii} conversion. This charset also represents
3334 the way Control Data Corporation relates EBCDIC to 8-bits ASCII.
3335
3336 @item EBCDIC-CCC
3337 @tindex EBCDIC-CCC
3338 In Recode, the @code{us..ebcdic-ccc} or @code{ebcdic-ccc..us}
3339 conversions represent the way Concurrent Computer Corporation (formerly
3340 Perkin Elmer) relates EBCDIC to 8-bits ASCII.
3341
3342 @item EBCDIC-IBM
3343 @tindex EBCDIC-IBM
3344 In Recode, the @code{us..ebcdic-ibm} conversion is @emph{almost}
3345 identical to the GNU @samp{dd conv=ibm} conversion. Given the exact
3346 @samp{dd conv=ibm} conversion table, Recode once said:
3347
3348 @example
3349 Codes 91 and 213 both recode to 173
3350 Codes 93 and 229 both recode to 189
3351 No character recodes to 74
3352 No character recodes to 106
3353 @end example
3354
3355 So I arbitrarily chose to recode 213 by 74 and 229 by 106. This makes the
3356 @code{EBCDIC-IBM} recoding reversible, but this is not necessarily the best
3357 correction. In any case, I think that GNU @code{dd} should be amended.
3358 @code{dd} and Recode should ideally agree on the same correction.
3359 So, this table might change once again.
3360 @end table
3361
3362 @w{RFC 1345} brings into Recode 15 other EBCDIC charsets, and 21 other
3363 charsets having EBCDIC in at least one of their alias names. You can
3364 get a list of all these by executing:
3365
3366 @example
3367 recode -l | grep -i ebcdic
3368 @end example
3369
3370 Note that Recode may convert a pure stream of EBCDIC characters,
3371 but it does not know how to handle binary data between records which
3372 is sometimes used to delimit them and build physical blocks. If end of
3373 lines are not marked, fixed record size may produce something readable,
3374 but @code{VB} or @code{VBS} blocking is likely to yield some garbage in
3375 the converted results.
3376
3377 @node IBM-PC, Icon-QNX, EBCDIC, IBM and MS
3378 @section IBM's PC code
3379
3380 @tindex IBM-PC
3381 @cindex MS-DOS charsets
3382 @tindex MSDOS
3383 @tindex dos
3384 @tindex pc
3385 This charset is available in Recode under the name @code{IBM-PC},
3386 with @code{dos}, @code{MSDOS} and @code{pc} as acceptable aliases.
3387 The shortest way of specifying it in Recode is @code{pc}.
3388
3389 The charset is aimed towards a PC microcomputer from IBM or any compatible.
3390 This is an eight-bit code. This charset is fairly old in Recode,
3391 its tables were produced a long while ago by mere inspection of a printed
3392 chart of the IBM-PC codes and glyph.
3393
3394 It has @code{CR-LF} as its implied surface. This means that, if the original
3395 end of lines have to be preserved while going out of @code{IBM-PC}, they
3396 should currently be added back through the usage of a surface on the other
3397 charset, or better, just never removed. Here are examples for both cases:
3398
3399 @example
3400 recode pc..l2/cl < @var{input} > @var{output}
3401 recode pc/..l2 < @var{input} > @var{output}
3402 @end example
3403
3404 @w{RFC 1345} brings into Recode 44 @samp{IBM} charsets or code pages,
3405 and also 8 other code pages. You can get a list of these all these by
3406 executing:@footnote{On DOS/Windows, stock shells do not know that apostrophes
3407 quote special characters like @kbd{|}, so one needs to use double quotes
3408 instead of apostrophes.}
3409
3410 @example
3411 recode -l | egrep -i '(CP|IBM)[0-9]'
3412 @end example
3413
3414 @noindent
3415 @cindex CR-LF surface, in IBM-PC charsets
3416 @tindex IBM819@r{, and CR-LF surface}
3417 All charset or aliases beginning with letters @samp{CP} or @samp{IBM}
3418 also have @code{CR-LF} as their implied surface. The same is true for a
3419 purely numeric alias in the same family. For example, all of @code{819},
3420 @code{CP819} and @code{IBM819} imply @code{CR-LF} as a surface. Note that
3421 @code{ISO-8859-1} does @emph{not} imply a surface, despite it shares the
3422 same tabular data as @code{819}.
3423
3424 @tindex ibm437
3425 There are a few discrepancies between this @code{IBM-PC} charset and the
3426 very similar @w{RFC 1345} charset @code{ibm437}. The @code{IBM-PC} charset
3427 has two extra characters at positions 20 (Latin-1 @code{0xB6}, Pilcrow)
3428 and 21 (Latin-1 @code{0xA7}, Section sign); further, it has position 250
3429 as @code{0xB7}, middle dot, while @code{ibm437} has middle dot at position
3430 249. According to this comparison of code tables:
3431 @uref{https://www.haible.de/bruno/charsets/conversion-tables/CP437.html}
3432 the source for @w{RFC 1345}, @file{dkuug.dk/IBM437.TXT} is the only
3433 source that thus defines this mapping.
3434
3435 @node Icon-QNX, , IBM-PC, IBM and MS
3436 @section Unisys' Icon code
3437
3438 @tindex Icon-QNX@r{, and aliases}
3439 @tindex QNX@r{, an alias for a charset}
3440 This charset is available in Recode under the name
3441 @code{Icon-QNX}, with @code{QNX} as an acceptable alias.
3442
3443 The file is using Unisys' Icon way to represent diacritics with code 25
3444 escape sequences, under the system QNX. This is a seven-bit code, even
3445 if eight-bit codes can flow through as part of IBM-PC charset.
3446
3447 @node CDC, Micros, IBM and MS, Top
3448 @chapter Charsets for CDC machines
3449
3450 @cindex CDC charsets
3451 @cindex charsets for CDC machines
3452 What is now Recode evolved out, through many transformations
3453 really, from a set of programs which were originally written in
3454 @dfn{COMPASS}, Control Data Corporation's assembler, with bits in FORTRAN,
3455 and later rewritten in CDC 6000 Pascal. The CDC heritage shows by the
3456 fact some old CDC charsets are still supported.
3457
3458 The Recode author used to be familiar with CDC Scope-NOS/BE and
3459 Kronos-NOS, and many CDC formats. Reading CDC tapes directly on other
3460 machines is often a challenge, and Recode does not always solve
3461 it. It helps having tapes created in coded mode instead of binary mode,
3462 and using @code{S} (Stranger) tapes instead of @code{I} (Internal) tapes.
3463 ANSI labels and multi-file tapes might be the source of trouble. There are
3464 ways to handle a few Cyber Record Manager formats, but some of them might
3465 be quite difficult to decode properly after the transfer is done.
3466
3467 Recode is usable only for a small subset of NOS text
3468 formats, and surely not with binary textual formats, like @code{UPDATE}
3469 or @code{MODIFY} sources, for example. Recode is not especially
3470 suited for reading 8/12 or 56/60 packing, yet this could easily arranged
3471 if there was a demand for it. It does not have the ability to translate
3472 Display Code directly, as the ASCII conversion implied by tape drivers
3473 or FTP does the initial approximation. Recode can decode 6/12
3474 caret notation over Display Code already mapped to ASCII.
3475
3476 @menu
3477 * Display Code:: Control Data's Display Code
3478 * CDC-NOS:: ASCII 6/12 from NOS
3479 * Bang-Bang:: ASCII ``bang bang''
3480 @end menu
3481
3482 @node Display Code, CDC-NOS, CDC, CDC
3483 @section Control Data's Display Code
3484
3485 @cindex CDC Display Code, a table
3486 This code is not available in Recode, but repeated here for
3487 reference. This is a 6-bit code used on CDC mainframes.
3488
3489 @example
3490 Octal display code to graphic Octal display code to octal ASCII
3491
3492 00 : 20 P 40 5 60 # 00 072 20 120 40 065 60 043
3493 01 A 21 Q 41 6 61 [ 01 101 21 121 41 066 61 133
3494 02 B 22 R 42 7 62 ] 02 102 22 122 42 067 62 135
3495 03 C 23 S 43 8 63 % 03 103 23 123 43 070 63 045
3496 04 D 24 T 44 9 64 " 04 104 24 124 44 071 64 042
3497 05 E 25 U 45 + 65 _ 05 105 25 125 45 053 65 137
3498 06 F 26 V 46 - 66 ! 06 106 26 126 46 055 66 041
3499 07 G 27 W 47 * 67 & 07 107 27 127 47 052 67 046
3500 10 H 30 X 50 / 70 ' 10 110 30 130 50 057 70 047
3501 11 I 31 Y 51 ( 71 ? 11 111 31 131 51 050 71 077
3502 12 J 32 Z 52 ) 72 < 12 112 32 132 52 051 72 074
3503 13 K 33 0 53 $ 73 > 13 113 33 060 53 044 73 076
3504 14 L 34 1 54 = 74 @@ 14 114 34 061 54 075 74 100
3505 15 M 35 2 55 75 \ 15 115 35 062 55 040 75 134
3506 16 N 36 3 56 , 76 ^ 16 116 36 063 56 054 76 136
3507 17 O 37 4 57 . 77 ; 17 117 37 064 57 056 77 073
3508 @end example
3509
3510 In older times, @kbd{:} used octal 63, and octal 0 was not a character.
3511 The table above shows the ASCII glyph interpretation of codes 60 to 77,
3512 yet these 16 codes were once defined differently.
3513
3514 There is no explicit end of line in Display Code, and the Cyber Record
3515 Manager introduced many new ways to represent them, the traditional end of
3516 lines being reachable by setting @code{RT} to @samp{Z}. If 6-bit bytes
3517 in a file are sequentially counted from 1, a traditional end of line
3518 does exist if bytes 10*@var{n}+9 and 10@var{n}+10 are both zero for a
3519 given @var{n}, in which case these two bytes are not to be interpreted as
3520 @kbd{::}. Also, up to 9 immediately preceeding zero bytes, going backward,
3521 are to be considered as part of the end of line and not interpreted as
3522 @kbd{:}@footnote{This convention replaced an older one saying that up to 4
3523 immediately preceeding @emph{pairs} of zero bytes, going backward, are to
3524 be considered as part of the end of line and not interpreted as @kbd{::}.}.
3525
3526 @node CDC-NOS, Bang-Bang, Display Code, CDC
3527 @section ASCII 6/12 from NOS
3528
3529 @tindex CDC-NOS@r{, and its aliases}
3530 @tindex NOS
3531 This charset is available in Recode under the name
3532 @code{CDC-NOS}, with @code{NOS} as an acceptable alias.
3533
3534 @cindex NOS 6/12 code
3535 @cindex caret ASCII code
3536 This is one of the charsets in use on CDC Cyber NOS systems to represent
3537 ASCII, sometimes named @dfn{NOS 6/12} code for coding ASCII. This code is
3538 also known as @dfn{caret ASCII}. It is based on a six bits character set
3539 in which small letters and control characters are coded using a @kbd{^}
3540 escape and, sometimes, a @kbd{@@} escape.
3541
3542 The routines given here presume that the six bits code is already expressed
3543 in ASCII by the communication channel, with embedded ASCII @kbd{^} and
3544 @kbd{@@} escapes.
3545
3546 Here is a table showing which characters are being used to encode each
3547 ASCII character.
3548
3549 @example
3550 000 ^5 020 ^# 040 060 0 100 @@A 120 P 140 @@G 160 ^P
3551 001 ^6 021 ^[ 041 ! 061 1 101 A 121 Q 141 ^A 161 ^Q
3552 002 ^7 022 ^] 042 " 062 2 102 B 122 R 142 ^B 162 ^R
3553 003 ^8 023 ^% 043 # 063 3 103 C 123 S 143 ^C 163 ^S
3554 004 ^9 024 ^" 044 $ 064 4 104 D 124 T 144 ^D 164 ^T
3555 005 ^+ 025 ^_ 045 % 065 5 105 E 125 U 145 ^E 165 ^U
3556 006 ^- 026 ^! 046 & 066 6 106 F 126 V 146 ^F 166 ^V
3557 007 ^* 027 ^& 047 ' 067 7 107 G 127 W 147 ^G 167 ^W
3558 010 ^/ 030 ^' 050 ( 070 8 110 H 130 X 150 ^H 170 ^X
3559 011 ^( 031 ^? 051 ) 071 9 111 I 131 Y 151 ^I 171 ^Y
3560 012 ^) 032 ^< 052 * 072 @@D 112 J 132 Z 152 ^J 172 ^Z
3561 013 ^$ 033 ^> 053 + 073 ; 113 K 133 [ 153 ^K 173 ^0
3562 014 ^= 034 ^@@ 054 , 074 < 114 L 134 \ 154 ^L 174 ^1
3563 015 ^ 035 ^\ 055 - 075 = 115 M 135 ] 155 ^M 175 ^2
3564 016 ^, 036 ^^ 056 . 076 > 116 N 136 @@B 156 ^N 176 ^3
3565 017 ^. 037 ^; 057 / 077 ? 117 O 137 _ 157 ^O 177 ^4
3566 @end example
3567
3568 @node Bang-Bang, , CDC-NOS, CDC
3569 @section ASCII ``bang bang''
3570
3571 @tindex Bang-Bang
3572 This charset is available in Recode under the name @code{Bang-Bang}.
3573
3574 This code, in use on Cybers at Universit@'e de Montr@'eal mainly, served
3575 to code a lot of French texts. The original name of this charset is
3576 @dfn{ASCII cod@'e Display}. This code is also known as @dfn{Bang-bang}.
3577 It is based on a six bits character set in which capitals, French
3578 diacritics and a few others are coded using an @kbd{!} escape followed
3579 by a single character, and control characters using a double @kbd{!}
3580 escape followed by a single character.
3581
3582 The routines given here presume that the six bits code is already expressed
3583 in ASCII by the communication channel, with embedded ASCII @kbd{!}
3584 escapes.
3585
3586 Here is a table showing which characters are being used to encode each
3587 ASCII character.
3588
3589 @example
3590 000 !!@@ 020 !!P 040 060 0 100 @@ 120 !P 140 !@@ 160 P
3591 001 !!A 021 !!Q 041 !" 061 1 101 !A 121 !Q 141 A 161 Q
3592 002 !!B 022 !!R 042 " 062 2 102 !B 122 !R 142 B 162 R
3593 003 !!C 023 !!S 043 # 063 3 103 !C 123 !S 143 C 163 S
3594 004 !!D 024 !!T 044 $ 064 4 104 !D 124 !T 144 D 164 T
3595 005 !!E 025 !!U 045 % 065 5 105 !E 125 !U 145 E 165 U
3596 006 !!F 026 !!V 046 & 066 6 106 !F 126 !V 146 F 166 V
3597 007 !!G 027 !!W 047 ' 067 7 107 !G 127 !W 147 G 167 W
3598 010 !!H 030 !!X 050 ( 070 8 110 !H 130 !X 150 H 170 X
3599 011 !!I 031 !!Y 051 ) 071 9 111 !I 131 !Y 151 I 171 Y
3600 012 !!J 032 !!Z 052 * 072 : 112 !J 132 !Z 152 J 172 Z
3601 013 !!K 033 !![ 053 + 073 ; 113 !K 133 [ 153 K 173 ![
3602 014 !!L 034 !!\ 054 , 074 < 114 !L 134 \ 154 L 174 !\
3603 015 !!M 035 !!] 055 - 075 = 115 !M 135 ] 155 M 175 !]
3604 016 !!N 036 !!^ 056 . 076 > 116 !N 136 ^ 156 N 176 !^
3605 017 !!O 037 !!_ 057 / 077 ? 117 !O 137 _ 157 O 177 !_
3606 @end example
3607
3608 @node Micros, Miscellaneous, CDC, Top
3609 @chapter Other micro-computer charsets
3610
3611 @menu
3612 * Mac OS:: Apple's Macintosh code
3613 * AtariST:: Atari ST code
3614 @end menu
3615
3616 @node Mac OS, AtariST, Micros, Micros
3617 @section Apple's Macintosh code
3618
3619 @w{RFC 1345} brings 2 Macintosh charsets. You can
3620 discover them by using @code{grep} over the output of @samp{recode -l}:
3621
3622 @example
3623 recode -l | grep -i mac
3624 @end example
3625
3626 @noindent
3627 @tindex macintosh@r{, a charset, and its aliases}
3628 @tindex macintosh_ce@r{, and its aliases}
3629 @tindex mac
3630 @tindex macce
3631 Charsets @code{macintosh} and @code{macintosh_ce}, as well as their aliases
3632 @code{mac} and @code{macce} have @code{CR} as their implied surface.
3633
3634 @node AtariST, , Mac OS, Micros
3635 @section Atari ST code
3636
3637 @tindex AtariST
3638 This charset is available in Recode under the name @code{AtariST}.
3639
3640 This is the character set used on the Atari ST/TT/Falcon. This is similar
3641 to @code{IBM-PC}, but differs in some details: it includes some more accented
3642 characters, the graphic characters are mostly replaced by Hebrew characters,
3643 and there is a true German @kbd{sharp s} different from Greek @kbd{beta}.
3644
3645 About the end-of-line conversions: the canonical end-of-line on the
3646 Atari is @samp{\r\n}, but unlike @code{IBM-PC}, the OS makes no
3647 difference between text and binary input/output; it is up to the
3648 application how to interpret the data. In fact, most of the libraries
3649 that come with compilers can grok both @samp{\r\n} and @samp{\n} as end
3650 of lines. Many of the users who also have access to Unix systems prefer
3651 @samp{\n} to ease porting Unix utilities. So, for easing reversibility,
3652 Recode tries to let @samp{\r} undisturbed through recodings.
3653
3654 @node Miscellaneous, Surfaces, Micros, Top
3655 @chapter Various other charsets
3656
3657 A few charsets do not fit well in the previous chapters, and are grouped
3658 here. Some of them were added to Recode long ago, at a time this tool
3659 was mainly meant for handling texts written in French. The bias still
3660 shows when these charsets are linked to @code{Latin-1} instead of the
3661 wider @code{Unicode}, but this is being corrected as Recode evolves.
3662
3663 @menu
3664 * HTML:: World Wide Web representations
3665 * LaTeX:: LaTeX macro calls
3666 * BibTeX:: BibTeX macro calls
3667 * Texinfo:: GNU project documentation files
3668 * ISO 5426 and ANSEL:: Bibliographic character sets
3669 * Vietnamese:: Vietnamese charsets
3670 * African:: African charsets
3671 * Others:: Cyrillic and other charsets
3672 * Java:: Java code
3673 * Texte:: Easy French conventions
3674 * Mule:: Mule as a multiplexed charset
3675 @end menu
3676
3677 @node HTML, LaTeX, Miscellaneous, Miscellaneous
3678 @section World Wide Web representations
3679
3680 @cindex HTML
3681 @cindex SGML
3682 @cindex XML
3683 @cindex Web
3684 @cindex World Wide Web
3685 @cindex WWW
3686 @cindex markup language
3687 @cindex entities
3688 @cindex character entities
3689 @cindex character entity references
3690 @cindex numeric character references
3691 Character entities have been introduced by SGML and made widely popular
3692 through HTML, the markup language in use for the World Wide Web, or Web or
3693 WWW for short. For representing @emph{unusual} characters, HTML texts use
3694 special sequences, beginning with an ampersand @kbd{&} and ending with a
3695 semicolon @kbd{;}. The sequence may itself start with a number sigh @kbd{#}
3696 and be followed by digits, so forming a @dfn{numeric character reference},
3697 or else be an alphabetic identifier, so forming a @dfn{character entity
3698 reference}.
3699
3700 The HTML standards have been revised into different HTML levels over time,
3701 and the list of allowable character entities differ in them. The later XML,
3702 meant to simplify many things, has an option (@samp{standalone=yes}) which
3703 much restricts that list. The Recode library is able to convert
3704 character references between their mnemonic form and their numeric form,
3705 depending on aimed HTML standard level. It also can, of course, convert
3706 between HTML and various other charsets.
3707
3708 Here is a list of those HTML variants which Recode
3709 supports. Some notes have been provided by Fran@,{c}ois Yergeau
3710 @email{yergeau@@alis.com}.
3711
3712 @table @code
3713 @item XML-standalone
3714 @tindex h0
3715 @tindex XML-standalone
3716 This charset is available in Recode under the name
3717 @code{XML-standalone}, with @code{h0} as an acceptable alias. It is
3718 documented in section 4.1 of @uref{http://www.w3.org/TR/REC-xml}.
3719 It only knows @samp{&amp;}, @samp{&gt;}, @samp{&lt;}, @samp{&quot;}
3720 and @samp{&apos;}.
3721
3722 @item HTML_1.1
3723 @tindex HTML_1.1
3724 @tindex h1
3725 This charset is available in Recode under the name @code{HTML_1.1},
3726 with @code{h1} as an acceptable alias. HTML 1.0 was never really documented.
3727
3728 @item HTML_2.0
3729 @tindex HTML_2.0
3730 @tindex RFC1866
3731 @tindex 1866
3732 @tindex h2
3733 This charset is available in Recode under the name @code{HTML_2.0},
3734 and has @code{RFC1866}, @code{1866} and @code{h2} for aliases. HTML 2.0
3735 entities are listed in @w{RFC 1866}. Basically, there is an entity for
3736 each @emph{alphabetical} character in the right part of @w{ISO 8859-1}.
3737 In addition, there are four entities for syntax-significant ASCII characters:
3738 @samp{&amp;}, @samp{&gt;}, @samp{&lt;} and @samp{&quot;}.
3739
3740 @item HTML-i18n
3741 @tindex HTML-i18n
3742 @tindex RFC2070
3743 @tindex 2070
3744 This charset is available in Recode under the name
3745 @code{HTML-i18n}, and has @code{RFC2070} and @code{2070} for
3746 aliases. @w{RFC 2070} added entities to cover the whole right
3747 part of @w{ISO 8859-1}. The list is conveniently accessible at
3748 @uref{http://www.alis.com:8085/ietf/html/html-latin1.sgml}. In addition,
3749 four i18n-related entities were added: @samp{&zwnj;} (@samp{&#8204;}),
3750 @samp{&zwj;} (@samp{&#8205;}), @samp{&lrm;} (@samp{&#8206}) and @samp{&rlm;}
3751 (@samp{&#8207;}).
3752
3753 @item HTML_3.2
3754 @tindex HTML_3.2
3755 @tindex h3
3756 This charset is available in Recode under the name
3757 @code{HTML_3.2}, with @code{h3} as an acceptable alias.
3758 @uref{http://www.w3.org/TR/REC-html32.html, HTML 3.2} took up the full
3759 @w{Latin-1} list but not the i18n-related entities from @w{RFC 2070}.
3760
3761 @item HTML_4.0
3762 @tindex h4
3763 @tindex h
3764 This charset is available in Recode under the name @code{HTML_4.0},
3765 and has @code{h4} and @code{h} for aliases. Beware that the particular
3766 alias @code{h} is not @emph{tied} to HTML 4.0, but to the highest HTML
3767 level supported by Recode; so it might later represent HTML level
3768 5 if this is ever created. @uref{http://www.w3.org/TR/REC-html40/,
3769 HTML 4.0} has the whole @w{Latin-1} list, a set of entities for
3770 symbols, mathematical symbols, and Greek letters, and another set for
3771 markup-significant and internationalization characters comprising the
3772 4 ASCII entities, the 4 i18n-related from @w{RFC 2070} plus some more.
3773 See @uref{http://www.w3.org/TR/REC-html40/sgml/entities.html}.
3774
3775 @end table
3776
3777 Printable characters from @w{Latin-1} may be used directly in an HTML text.
3778 However, partly because people have deficient keyboards, partly because
3779 people want to transmit HTML texts over non 8-bit clean channels while not
3780 using MIME, it is common (yet debatable) to use character entity references
3781 even for @w{Latin-1} characters, when they fall outside ASCII (that is,
3782 when they have the 8th bit set).
3783
3784 When you recode from another charset to @code{HTML}, beware that all
3785 occurrences of double quotes, ampersands, and left or right angle brackets
3786 are translated into special sequences. However, in practice, people often
3787 use ampersands and angle brackets in the other charset for introducing
3788 HTML commands, compromising it: it is not pure HTML, not it is pure
3789 other charset. These particular translations can be rather inconvenient,
3790 they may be specifically inhibited through the command option @samp{-d}
3791 (@pxref{Mixed}).
3792
3793 Codes not having a mnemonic entity are output by Recode using the
3794 @samp{&#@var{nnn};} notation, where @var{nnn} is a decimal representation
3795 of the UCS code value. When there is an entity name for a character, it
3796 is always preferred over a numeric character reference. ASCII printable
3797 characters are always generated directly. So is the newline. While reading
3798 HTML, Recode supports numeric character reference as alternate
3799 writings, even when written as hexadecimal numbers, as in @samp{&#xfffd}.
3800 This is documented in:
3801
3802 @example
3803 http://www.w3.org/TR/REC-html40/intro/sgmltut.html#h-3.2.3
3804 @end example
3805
3806 When Recode translates to HTML, the translation occurs according to
3807 the HTML level as selected by the goal charset. When translating @emph{from}
3808 HTML, Recode not only accepts the character entity references known at
3809 that level, but also those of all other levels, as well as a few alternative
3810 special sequences, to be forgiving to files using other HTML standards.
3811
3812 @cindex normilise an HTML file
3813 @cindex HTML normalization
3814 Recode can be used to @emph{normalise} an HTML file using
3815 oldish conventions. For example, it accepts @samp{&AE;}, as this once was a
3816 valid writing, somewhere. However, it should always produce @samp{&AElig;}
3817 instead of @samp{&AE;}. Yet, this is not completely true. If one does:
3818
3819 @example
3820 recode h3..h3 < @var{input}
3821 @end example
3822
3823 @noindent
3824 the operation will be optimised into a mere copy, and you can get @samp{&AE;}
3825 this way, if you had some in your input file. But if you explicitly defeat
3826 the optimisation, like this maybe:
3827
3828 @example
3829 recode h3..u2,u2..h3 < @var{input}
3830 @end example
3831
3832 @noindent
3833 then @samp{&AE;} should be normalised into @samp{&AElig;} by the operation.
3834
3835 @node LaTeX, BibTeX, HTML, Miscellaneous
3836 @section La@TeX{} macro calls
3837
3838 @tindex LaTeX@r{, a charset}
3839 @tindex ltex
3840 @cindex La@TeX{} files
3841 @cindex @TeX{} files
3842 This charset is available in Recode under the name @code{LaTeX}
3843 and has @code{ltex} as an alias. It is used for ASCII files coded to be
3844 read by La@TeX{} or, in certain cases, by @TeX{}.
3845
3846 Whenever you recode from another charset to @code{LaTeX}, beware that all
3847 occurrences of backslashes @kbd{\} are translated into the string
3848 @samp{\backslash@{@}}. However, in practice, people often use backslashes
3849 in the other charset for introducing @TeX{} commands, compromising it:
3850 it is not pure @TeX{}, nor it is pure other charset. This translation
3851 of backslashes into @samp{\backslash@{@}} can be rather inconvenient,
3852 it may be inhibited through the command option @samp{-d} (@pxref{Mixed}).
3853
3854 @node BibTeX, Texinfo, LaTeX, Miscellaneous
3855 @section Bib@TeX{} macro calls
3856
3857 @tindex BibTeX@r{, a charset}
3858 @tindex btex
3859 @cindex Bib@TeX{} files
3860 @cindex @TeX{} files
3861 This charset is available in Recode under the name @code{BibTeX}
3862 with aliases @code{bibtex} and @code{btex}. It is used for ASCII files coded to be
3863 read by Bib@TeX{} or, in certain cases, by La@TeX{} or @TeX{}.
3864
3865 This charset is very similar to @code{LaTeX}. The only difference is that
3866 diacritics are enclosed between @samp{@{@}}. Refer to @code{LaTeX}
3867 charset for further information. @xref{LaTeX}.
3868
3869 @node Texinfo, ISO 5426 and ANSEL, BibTeX, Miscellaneous
3870 @section GNU project documentation files
3871
3872 @tindex Texinfo@r{, a charset}
3873 @tindex texi
3874 @tindex ti
3875 @cindex Texinfo files
3876 This charset is available in Recode under the name @code{Texinfo}
3877 and has @code{texi} and @code{ti} for aliases. It is used by the GNU
3878 project for its documentation. Texinfo files may be converted into Info
3879 files by the @code{makeinfo} program and into nice printed manuals by
3880 the @TeX{} system.
3881
3882 Even if Recode may transform other charsets to Texinfo, it may
3883 not read Texinfo files yet. In these times, usages are also changing
3884 between versions of Texinfo, and Recode only partially succeeds
3885 in correctly following these changes. So, for now, Texinfo support in
3886 Recode should be considered as work still in progress (!).
3887
3888 @node ISO 5426 and ANSEL, Vietnamese, Texinfo, Miscellaneous
3889 @section Bibliographic character sets
3890
3891 @tindex ISO 5426@r{, a charset}
3892 @tindex ANSEL@r{, a charset}
3893 @cindex Bibliographic charsets
3894 These two character sets are intended to work with ASCII for exchange of
3895 bibliographic information.
3896
3897 @node Vietnamese, African, ISO 5426 and ANSEL, Miscellaneous
3898 @section Vietnamese charsets
3899
3900 @cindex Vietnamese charsets
3901 We are currently trying the implementation, in Recode, of a few
3902 character sets and transliterated forms to handle the Vietnamese language.
3903 They are quite briefly summarised, here.
3904
3905 @table @code
3906 @item TCVN
3907 @tindex TCVN@r{, for Vietnamese}
3908 @tindex VN1@r{, maybe not available}
3909 @tindex VN2@r{, maybe not available}
3910 @tindex VN3@r{, maybe not available}
3911 The TCVN charset has an incomplete name. It might be one of the three
3912 charset @code{VN1}, @code{VN2} or @code{VN3}. Yes @code{VN2} might be a
3913 second version of @code{VISCII}. To be clarified.
3914
3915 @item VISCII
3916 @tindex VISCII
3917 This is an 8-bit character set which seems to be rather popular for
3918 writing Vietnamese.
3919
3920 @item VPS
3921 @tindex VPS
3922 This is an 8-bit character set for Vietnamese. No much reference.
3923
3924 @item VIQR
3925 @tindex VIQR
3926 The VIQR convention is a 7-bit, @code{ASCII} transliteration for Vietnamese.
3927
3928 @item VNI
3929 @tindex VNI
3930 The VNI convention is a 8-bit, @code{Latin-1} transliteration for Vietnamese.
3931 @end table
3932
3933 @tindex 1129@r{, not available}
3934 @tindex CP1129@r{, not available}
3935 @tindex 1258@r{, not available}
3936 @tindex CP1258@r{, not available}
3937 Still lacking for Vietnamese in Recode, are the charsets @code{CP1129}
3938 and @code{CP1258}.
3939
3940 @node African, Others, Vietnamese, Miscellaneous
3941 @section African charsets
3942
3943 @cindex African charsets
3944 Some African character sets are available for a few languages, when these
3945 are heavily used in countries where French is also currently spoken.
3946
3947 @tindex AFRFUL-102-BPI_OCIL@r{, and aliases}
3948 @tindex bambara
3949 @tindex bra
3950 @tindex ewondo
3951 @tindex fulfude
3952 @tindex AFRFUL-103-BPI_OCIL@r{, and aliases}
3953 @tindex t-bambara
3954 @tindex t-bra
3955 @tindex t-ewondo
3956 @tindex t-fulfude
3957 One African charset is usable for Bambara, Ewondo and Fulfude, as well
3958 as for French. This charset is available in Recode under the name
3959 @code{AFRFUL-102-BPI_OCIL}. Accepted aliases are @code{bambara}, @code{bra},
3960 @code{ewondo} and @code{fulfude}. Transliterated forms of the same are
3961 available under the name @code{AFRFUL-103-BPI_OCIL}. Accepted aliases
3962 are @code{t-bambara}, @code{t-bra}, @code{t-ewondo} and @code{t-fulfude}.
3963
3964 @tindex AFRLIN-104-BPI_OCIL
3965 @tindex lingala
3966 @tindex lin
3967 @tindex sango
3968 @tindex wolof
3969 @tindex AFRLIN-105-BPI_OCIL
3970 @tindex t-lingala
3971 @tindex t-lin
3972 @tindex t-sango
3973 @tindex t-wolof
3974 Another African charset is usable for Lingala, Sango and Wolof, as well
3975 as for French. This charset is available in Recode under the
3976 name @code{AFRLIN-104-BPI_OCIL}. Accepted aliases are @code{lingala},
3977 @code{lin}, @code{sango} and @code{wolof}. Transliterated forms of the same
3978 are available under the name @code{AFRLIN-105-BPI_OCIL}. Accepted aliases
3979 are @code{t-lingala}, @code{t-lin}, @code{t-sango} and @code{t-wolof}.
3980
3981 @tindex AFRL1-101-BPI_OCIL
3982 @tindex t-francais
3983 @tindex t-fra
3984 To ease exchange with @code{ISO-8859-1}, there is a charset conveying
3985 transliterated forms for @w{Latin-1} in a way which is compatible with the other
3986 African charsets in this series. This charset is available in Recode
3987 under the name @code{AFRL1-101-BPI_OCIL}. Accepted aliases are @code{t-fra}
3988 and @code{t-francais}.
3989
3990 @node Others, Java, African, Miscellaneous
3991 @section Cyrillic and other charsets
3992
3993 @cindex Cyrillic charsets
3994 The following Cyrillic charsets are already available in Recode
3995 through @w{RFC 1345} tables: @code{CP1251} with aliases @code{1251}, @code{
3996 ms-cyrl} and @code{windows-1251}; @code{CSN_369103} with aliases
3997 @code{ISO-IR-139} and @code{KOI8_L2}; @code{ECMA-cyrillic} with aliases
3998 @code{ECMA-113}, @code{ECMA-113:1986} and @code{iso-ir-111}, @code{IBM880}
3999 with aliases @code{880}, @code{CP880} and @code{EBCDIC-Cyrillic};
4000 @code{INIS-cyrillic} with alias @code{iso-ir-51}; @code{ISO-8859-5} with
4001 aliases @code{cyrillic}, @code{ ISO-8859-5:1988} and @code{iso-ir-144};
4002 @code{KOI-7}; @code{KOI-8} with alias @code{GOST_19768-74}; @code{KOI8-R};
4003 @code{KOI8-RU} and finally @code{KOI8-U}.
4004
4005 There seems to remain some confusion in Roman charsets for Cyrillic
4006 languages, and because a few users requested it repeatedly, Recode
4007 now offers special services in that area. Consider these charsets as
4008 experimental and debatable, as the extraneous tables describing them are
4009 still a bit fuzzy or non-standard. Hopefully, in the long run, these
4010 charsets will be covered in Keld Simonsen's works to the satisfaction of
4011 everybody, and this section will merely disappear.
4012
4013 @table @code
4014 @item KEYBCS2
4015 @tindex KEYBCS2
4016 @tindex Kamenicky
4017 This charset is available under the name @code{KEYBCS2}, with
4018 @code{Kamenicky} as an accepted alias.
4019
4020 @item CORK
4021 @tindex CORK
4022 @tindex T1
4023 This charset is available under the name @code{CORK}, with @code{T1}
4024 as an accepted alias.
4025
4026 @item KOI-8_CS2
4027 @tindex KOI-8_CS2
4028 This charset is available under the name @code{KOI-8_CS2}.
4029 @end table
4030
4031 @node Java, Texte, Others, Miscellaneous
4032 @section Java code
4033
4034 @tindex Java
4035 This charset is available under the name @code{Java}, and should be
4036 considered experimental for now.
4037
4038 ASCII characters represent themselves. Character outside ASCII are
4039 coded as @samp{\uNNNN}, where @samp{NNNN} stands for the four-digit
4040 hexadecimal value of the character within Unicode. The canonical
4041 representation uses lower case for the @samp{u} prefix and for the
4042 hexadecimal digits, yet Recode also accepts upper case.
4043
4044 There is currently no attempt to distinguish Java comments from Java
4045 strings while the recoding goes, and this may be corrected some day.
4046
4047 @node Texte, Mule, Java, Miscellaneous
4048 @section Easy French conventions
4049
4050 @tindex Texte
4051 @tindex txte
4052 This charset is available in Recode under the name @code{Texte}
4053 and has @code{txte} for an alias. It is a seven bits code, identical
4054 to @code{ASCII-BS}, save for French diacritics which are noted using a
4055 slightly different convention.
4056
4057 At text entry time, these conventions provide a little speed up. At read
4058 time, they slightly improve the readability over a few alternate ways
4059 of coding diacritics. Of course, it would better to have a specialised
4060 keyboard to make direct eight bits entries and fonts for immediately
4061 displaying eight bit ISO @w{Latin-1} characters. But not everybody is so
4062 fortunate. In a few mailing environments, and sadly enough, it still
4063 happens that the eight bit is often willing-fully destroyed.
4064
4065 @cindex Easy French
4066 Easy French has been in use in France for a while. I only slightly
4067 adapted it (the diaeresis option) to make it more comfortable to several
4068 usages in Qu@'ebec originating from Universit@'e de Montr@'eal. In fact,
4069 the main problem for me was not to necessarily to invent Easy French, but
4070 to recognise the ``best'' convention to use, (best is not being defined,
4071 here) and to try to solve the main pitfalls associated with the selected
4072 convention. Shortly said, we have:
4073
4074 @table @kbd
4075 @item e'
4076 for @kbd{e} (and some other vowels) with an acute accent,
4077 @item e`
4078 for @kbd{e} (and some other vowels) with a grave accent,
4079 @item e^
4080 for @kbd{e} (and some other vowels) with a circumflex accent,
4081 @item e"
4082 for @kbd{e} (and some other vowels) with a diaeresis,
4083 @item c,
4084 for @kbd{c} with a cedilla.
4085 @end table
4086
4087 @noindent
4088 There is no attempt at expressing the @kbd{ae} and @kbd{oe} diphthongs.
4089 French also uses tildes over @kbd{n} and @kbd{a}, but seldomly, and this
4090 is not represented either. In some countries, @kbd{:} is used instead
4091 of @kbd{"} to mark diaeresis. Recode supports only one convention
4092 per call, depending on the @samp{-c} option of the @code{recode} command.
4093 French quotes (sometimes called ``angle quotes'') are noted the same way
4094 English quotes are noted in @TeX{}, @emph{id est} by @kbd{``} and @kbd{''}.
4095 No effort has been put to preserve Latin ligatures (@kbd{@ae{}}, @kbd{@oe{}})
4096 which are representable in several other charsets. So, these ligatures
4097 may be lost through Easy French conventions.
4098
4099 The convention is prone to losing information, because the diacritic
4100 meaning overloads some characters that already have other uses. To
4101 alleviate this, some knowledge of the French language is boosted into
4102 the recognition routines. So, the following subtleties are systematically
4103 obeyed by the various recognisers.
4104
4105 @enumerate
4106 @item
4107 A comma which follows a @kbd{c} is interpreted as a cedilla only if it is
4108 followed by one of the vowels @kbd{a}, @kbd{o} or @kbd{u}.
4109
4110 @item
4111 A single quote which follows a @kbd{e} does not necessarily means an acute
4112 accent if it is followed by a single other one. For example:
4113
4114 @table @kbd
4115 @item e'
4116 will give an @kbd{e} with an acute accent.
4117 @item e''
4118 will give a simple @kbd{e}, with a closing quotation mark.
4119 @item e'''
4120 will give an @kbd{e} with an acute accent, followed by a closing quotation
4121 mark.
4122 @end table
4123
4124 There is a problem induced by this convention if there are English
4125 quotations with a French text. In sentences like:
4126
4127 @example
4128 There's a meeting at Archie's restaurant.
4129 @end example
4130
4131 the single quotes will be mistaken twice for acute accents. So English
4132 contractions and suffix possessives could be mangled.
4133
4134 @item
4135 A double quote or colon, depending on @samp{-c} option, which follows a
4136 vowel is interpreted as diaeresis only if it is followed by another letter.
4137 But there are in French several words that @emph{end} with a diaeresis,
4138 and the Recode library is aware of them. There are words ending in
4139 ``igue'', either feminine words without a relative masculine (besaigu@"e
4140 and cigu@"e), or feminine words with a relative masculine@footnote{There
4141 are supposed to be seven words in this case. So, one is missing.}
4142 (aigu@"e, ambigu@"e, contigu@"e, exigu@"e, subaigu@"e and suraigu@"e).
4143 There are also words not ending in ``igue'', but instead, either ending by
4144 ``i''@footnote{Look at one of the following sentences (the second has to
4145 be interpreted with the @samp{-c} option):
4146
4147 @example
4148 "Ai"e! Voici le proble`me que j'ai"
4149 Ai:e! Voici le proble`me que j'ai:
4150 @end example
4151
4152 There is an ambiguity between an
4153 @tex
4154 a\"\i,
4155 @end tex
4156 @ifinfo
4157 ai",
4158 @end ifinfo
4159 @c FIXME: why not use @dotless{} here? It works, AFAIK.
4160 @ignore
4161 a@"{@dotless{i}},
4162 @end ignore
4163 the small animal, and the indicative future of @emph{avoir} (first person
4164 singular), when followed by what could be a diaeresis mark. Hopefully,
4165 the case is solved by the fact that an apostrophe always precedes the
4166 verb and almost never the animal.}
4167 @tex
4168 (a\"\i, conga\"\i, go\"\i, ha\"\i ka\"\i, inou\"\i, sa\"\i, samura\"\i,
4169 tha\"\i{} and toka\"\i),
4170 @end tex
4171 @ifinfo
4172 (ai", congai", goi", hai"kai", inoui", sai", samurai", thai" and tokai"),
4173 @end ifinfo
4174 @ignore
4175 (a@"{@dotless{i}}, conga@"{@dotless{i}}, go@"{@dotless{i}},
4176 ha@"{@dotless{i}}ka@"{@dotless{i}}, inou@"{@dotless{i}}, sa@"{@dotless{i}},
4177 samura@"{@dotless{i}}, tha@"{@dotless{i}} and toka@"{@dotless{i}}),
4178 @end ignore
4179 ending by ``e'' (cano@"e) or ending by ``u''@footnote{I did not pay
4180 attention to proper nouns, but this one showed up as being fairly evident.}
4181 (Esa@"u).
4182
4183 Just to complete this topic, note that it would be wrong to make a rule
4184 for all words ending in ``igue'' as needing a diaerisis, as there are
4185 counter-examples (becfigue, b@`esigue, bigue, bordigue, bourdigue, brigue,
4186 contre-digue, digue, d'intrigue, fatigue, figue, garrigue, gigue, igue,
4187 intrigue, ligue, prodigue, sarigue and zigue).
4188 @end enumerate
4189
4190 @node Mule, , Texte, Miscellaneous
4191 @section Mule as a multiplexed charset
4192
4193 @tindex Mule@r{, a charset}
4194 @cindex multiplexed charsets
4195 @cindex super-charsets
4196 This version of Recode barely starts supporting multiplexed or
4197 super-charsets, that is, those encoding methods by which a single text
4198 stream may contain a combination of more than one constituent charset.
4199 The only multiplexed charset in Recode is @code{Mule}, and even
4200 then, it is only very partially implemented: the only correspondence
4201 available is with @code{Latin-1}. The author quickly implemented this
4202 only because he needed this for himself. However, it is intended that
4203 Mule support to become more real in subsequent releases of Recode.
4204
4205 Multiplexed charsets are not to be confused with mixed charset texts
4206 (@pxref{Mixed}). For mixed charset input, the rules allowing to distinguish
4207 which charset is current, at any given place, are kind of informal, and
4208 driven from the semantics of what the file contains. On the other side,
4209 multiplexed charsets are @emph{designed} to be interpreted fairly precisely,
4210 and quite independently of any informational context.
4211
4212 @cindex MULE, in Emacs
4213 The spelling @code{Mule} originally stood for @cite{@emph{mul}tilingual
4214 @emph{e}nhancement to GNU Emacs}, it is the result of a collective
4215 effort orchestrated by Handa Ken'ichi since 1993. When @code{Mule} got
4216 rewritten in the main development stream of GNU Emacs 20, the FSF renamed
4217 it @code{MULE}, meaning @cite{@emph{mul}tilingual @emph{e}nvironment
4218 in GNU Emacs}. Even if the charset @code{Mule} is meant to stay
4219 internal to GNU Emacs, it sometimes breaks loose in external files,
4220 and as a consequence, a recoding tool is sometimes needed. Within Emacs,
4221 @code{Mule} comes with @code{Leim}, which stands for @cite{@emph{l}ibraries
4222 of @emph{e}macs @emph{i}nput @emph{m}ethods}. One of these libraries is
4223 named @code{quail}@footnote{Usually, quail means quail egg in Japanese,
4224 while egg alone is usually chicken egg. Both quail egg and chicken
4225 egg are popular food in Japan. The @code{quail} input system has
4226 been named because it is smaller than the previous @code{EGG} system.
4227 As for @code{EGG}, it is the translation of @code{TAMAGO}. This word
4228 comes from the Japanese sentence @cite{@emph{ta}kusan @emph{ma}tasete
4229 @emph{go}mennasai}, meaning @cite{sorry to have let you wait so long}.
4230 Of course, the publication of @code{EGG} has been delayed many times@dots{}
4231 (Story by Takahashi Naoto)}.
4232
4233 @node Surfaces, Internals, Miscellaneous, Top
4234 @chapter All about surfaces
4235 @cindex surface, what it is
4236
4237 A @dfn{surface} is the varnish added over a charset so it fits in actual
4238 bits and bytes. How end of lines are exactly encoded is not really
4239 pertinent to the charset, and so, there is surface for end of lines.
4240 @code{Base64} is also a surface, as we may encode any charset in it.
4241 Other examples would DES enciphering, or gzip compression (even if
4242 Recode does not offer them currently): these are ways to give a real
4243 life to theoretical charsets.
4244
4245 @cindex trivial surface
4246 The @dfn{trivial surface} consists of using a fixed number of bits
4247 (often eight) for each character, the bits together hold the integer
4248 value of the index for the character in its charset table. There are
4249 many kinds of surfaces, beyond the trivial one, all having the purpose
4250 of increasing selected qualities for the storage or transmission.
4251 For example, surfaces might increase the resistance to channel limits
4252 (@code{Base64}), the transmission speed (@code{gzip}), the information
4253 privacy (@code{DES}), the conformance to operating system conventions
4254 (@code{CR-LF}), the blocking into records (@code{VB}), and surely other
4255 things as well@footnote{These are mere examples to explain the concept,
4256 Recode only has @code{Base64} and @code{CR-LF}, actually.}.
4257 Many surfaces may be applied to a stream of characters from a charset,
4258 the order of application of surfaces is important, and surfaces
4259 should be removed in the reverse order of their application.
4260
4261 Even if surfaces may generally be applied to various charsets, some
4262 surfaces were specifically designed for a particular charset, and would
4263 not make much sense if applied to other charsets. In such cases, these
4264 conceptual surfaces have been implemented as Recode charsets,
4265 instead of as surfaces. This choice yields to cleaner syntax
4266 and usage. @xref{Universal}.
4267
4268 @cindex surfaces, implementation in Recode
4269 @tindex data@r{, a special charset}
4270 Surfaces are implemented within Recode as special charsets
4271 which may only transform to or from the @code{data}
4272 special charset. Clever users may use this knowledge for writing
4273 surface names in requests exactly as if they were pure charsets, when
4274 the only need is to change surfaces without any kind of recoding between
4275 real charsets. In such contexts @code{data} may also be used as if it
4276 were some kind of generic, anonymous charset: the
4277 request @samp{data..@var{surface}} merely adds the given @var{surface},
4278 while the request @samp{@var{surface}..data} removes it.
4279
4280 This chapter presents all surfaces currently available.
4281
4282 @menu
4283 * Permutations:: Permuting groups of bytes
4284 * End lines:: Representation for end of lines
4285 * MIME:: MIME contents encodings
4286 * Dump:: Interpreted character dumps
4287 * Test:: Artificial data for testing
4288 @end menu
4289
4290 @node Permutations, End lines, Surfaces, Surfaces
4291 @section Permuting groups of bytes
4292 @cindex permutations of groups of bytes
4293
4294 @cindex byte order swapping
4295 @cindex endiannes, changing
4296 A permutation is a surface transformation which reorders groups of
4297 eight-bit bytes. A @emph{21} permutation exchanges pairs of successive
4298 bytes. If the text contains an odd number of bytes, the last byte is
4299 merely copied. An @emph{4321} permutation inverts the order of quadruples
4300 of bytes. If the text does not contains a multiple of four bytes, the
4301 remaining bytes are nevertheless permuted as @emph{321} if there are
4302 three bytes, @emph{21} if there are two bytes, or merely copied otherwise.
4303
4304 @table @code
4305 @item 21
4306 @tindex 21-Permutation
4307 @tindex swabytes
4308 This surface is available in Recode under the name
4309 @code{21-Permutation} and has @code{swabytes} for an alias.
4310
4311 @item 4321
4312 @tindex 4321-Permutation
4313 This surface is available in Recode under the name
4314 @code{4321-Permutation}.
4315 @end table
4316
4317 @node End lines, MIME, Permutations, Surfaces
4318 @section Representation for end of lines
4319 @cindex end of line format
4320
4321 The same charset might slightly differ, from one system to another, for
4322 the single fact that end of lines are not represented identically on all
4323 systems. The representation for an end of line within Recode
4324 is the @code{ASCII} or @code{UCS} code with value 10, or @kbd{LF}. Other
4325 conventions for representing end of lines are available through surfaces.
4326
4327 @table @code
4328 @item CR
4329 @tindex CR@r{, a surface}
4330 This convention is popular on Apple's Macintosh machines. When this
4331 surface is applied, each line is terminated by @kbd{CR}, which has
4332 @code{ASCII} value 13. Unless the library is operating in strict mode,
4333 adding or removing the surface will in fact @emph{exchange} @kbd{CR} and
4334 @kbd{LF}, for better reversibility. However, in strict mode, the exchange
4335 does not happen, any @kbd{CR} will be copied verbatim while applying
4336 the surface, and any @kbd{LF} will be copied verbatim while removing it.
4337
4338 This surface is available in Recode under the name @code{CR},
4339 it does not have any aliases. This is the implied surface for the Apple
4340 Macintosh related charsets.
4341
4342 @item CR-LF
4343 @tindex CR-LF@r{, a surface}
4344 This convention is popular on Microsoft systems running on IBM PCs and
4345 compatible. When this surface is applied, each line is terminated by
4346 a sequence of two characters: one @kbd{CR} followed by one @kbd{LF},
4347 in that order.
4348
4349 @cindex Ctrl-Z, discarding
4350 For compatibility with oldish MS-DOS systems, removing a @code{CR-LF}
4351 surface will discard the first encountered @kbd{C-z}, which has
4352 @code{ASCII} value 26, and everything following it in the text.
4353 Adding this surface will not, however, append a @kbd{C-z} to the result.
4354
4355 @tindex cl
4356 This surface is available in Recode under the name @code{CR-LF}
4357 and has @code{cl} for an alias. This is the implied surface for the IBM
4358 or Microsoft related charsets or code pages.
4359 @end table
4360
4361 Some other charsets might have their own representation for an end of
4362 line, which is different from @kbd{LF}. For example, this is the case
4363 of various @code{EBCDIC} charsets, or @code{Icon-QNX}. The recoding of
4364 end of lines is intimately tied into such charsets, it is not available
4365 separately as surfaces.
4366
4367 @node MIME, Dump, End lines, Surfaces
4368 @section MIME contents encodings
4369 @cindex MIME encodings
4370
4371 @cindex RFC 2045
4372 @w{RFC 2045} defines two 7-bit surfaces, meant to prepare 8-bit messages for
4373 transmission. Base64 is especially usable for binary entities, while
4374 Quoted-Printable is especially usable for text entities, in those case
4375 the lower 128 characters of the underlying charset coincide with ASCII.
4376
4377 @table @code
4378 @tindex Base64
4379 @tindex b64
4380 @tindex 64
4381 @item Base64
4382 This surface is available in Recode under the name @code{Base64},
4383 with @code{b64} and @code{64} as acceptable aliases.
4384
4385 @item Quoted-Printable
4386 @tindex Quoted-Printable
4387 @tindex quote-printable
4388 @tindex QP
4389 This surface is available in Recode under the name
4390 @code{Quoted-Printable}, with @code{quote-printable} and @code{QP} as
4391 acceptable aliases.
4392 @end table
4393
4394 Note that @code{UTF-7}, which may be also considered as a MIME surface,
4395 is provided as a genuine charset instead, as it necessary relates to
4396 @code{UCS-2} and nothing else. @xref{UTF-7}.
4397
4398 A little historical note, also showing the three levels of acceptance of
4399 Internet standards. MIME changed from a ``Proposed Standard'' (@w{RFC
4400 1341--1344}, 1992) to a ``Draft Standard'' (@w{RFC 1521--1523}) in 1993,
4401 and was @emph{recycled} as a ``Draft Standard'' in 1996-11. It is not yet a
4402 ``Full Standard''.
4403
4404 @node Dump, Test, MIME, Surfaces
4405 @section Interpreted character dumps
4406
4407 @cindex dumping characters
4408 Dumps are surfaces meant to express, in ways which are a bit more readable,
4409 the bit patterns used to represent characters. They allow the inspection
4410 or debugging of character streams, but also, they may assist a bit the
4411 production of C source code which, once compiled, would hold in memory a
4412 copy of the original coding. However, Recode does not attempt, in
4413 any way, to produce complete C source files in dumps. User hand editing
4414 or @file{Makefile} trickery is still needed for adding missing lines.
4415 Dumps may be given in decimal, hexadecimal and octal, and be based over
4416 chunks of either one, two or four eight-bit bytes. Formatting has been
4417 chosen to respect the C language syntax for number constants, with commas
4418 and newlines inserted appropriately.
4419
4420 However, when dumping two or four byte chunks, the last chunk may be
4421 incomplete. This is observable through the usage of narrower expression
4422 for that last chunk only. Such a shorter chunk would not be compiled
4423 properly within a C initialiser, as all members of an array share a single
4424 type, and so, have identical sizes.
4425
4426 @table @code
4427 @item Octal-1
4428 @tindex Octal-1
4429 @tindex o1
4430 This surface corresponds to an octal expression of each input byte.
4431
4432 It is available in Recode under the name @code{Octal-1},
4433 with @code{o1} and @code{o} as acceptable aliases.
4434
4435 @item Octal-2
4436 @tindex Octal-2
4437 @tindex o2
4438 This surface corresponds to an octal expression of each pair of
4439 input bytes, except for the last pair, which may be short.
4440
4441 It is available in Recode under the name @code{Octal-2}
4442 and has @code{o2} for an alias.
4443
4444 @item Octal-4
4445 @tindex Octal-4
4446 @tindex o4
4447 This surface corresponds to an octal expression of each quadruple of
4448 input bytes, except for the last quadruple, which may be short.
4449
4450 It is available in Recode under the name @code{Octal-4}
4451 and has @code{o4} for an alias.
4452
4453 @item Decimal-1
4454 @tindex Decimal-1
4455 @tindex d1
4456 This surface corresponds to an decimal expression of each input byte.
4457
4458 It is available in Recode under the name @code{Decimal-1},
4459 with @code{d1} and @code{d} as acceptable aliases.
4460
4461 @item Decimal-2
4462 @tindex Decimal-2
4463 @tindex d2
4464 This surface corresponds to an decimal expression of each pair of
4465 input bytes, except for the last pair, which may be short.
4466
4467 It is available in Recode under the name @code{Decimal-2}
4468 and has @code{d2} for an alias.
4469
4470 @item Decimal-4
4471 @tindex Decimal-4
4472 @tindex d4
4473 This surface corresponds to an decimal expression of each quadruple of
4474 input bytes, except for the last quadruple, which may be short.
4475
4476 It is available in Recode under the name @code{Decimal-4}
4477 and has @code{d4} for an alias.
4478
4479 @item Hexadecimal-1
4480 @tindex Hexadecimal-1
4481 @tindex x1
4482 This surface corresponds to an hexadecimal expression of each input byte.
4483
4484 It is available in Recode under the name @code{Hexadecimal-1},
4485 with @code{x1} and @code{x} as acceptable aliases.
4486
4487 @item Hexadecimal-2
4488 @tindex Hexadecimal-2
4489 @tindex x2
4490 This surface corresponds to an hexadecimal expression of each pair of
4491 input bytes, except for the last pair, which may be short.
4492
4493 It is available in Recode under the name @code{Hexadecimal-2},
4494 with @code{x2} for an alias.
4495
4496 @item Hexadecimal-4
4497 @tindex Hexadecimal-4
4498 @tindex x4
4499 This surface corresponds to an hexadecimal expression of each quadruple of
4500 input bytes, except for the last quadruple, which may be short.
4501
4502 It is available in Recode under the name @code{Hexadecimal-4},
4503 with @code{x4} for an alias.
4504 @end table
4505
4506 When removing a dump surface, that is, when reading a dump results back
4507 into a sequence of bytes, the narrower expression for a short last chunk
4508 is recognised, so dumping is a fully reversible operation. However, in
4509 case you want to produce dumps by other means than through Recode,
4510 beware that for decimal dumps, the library has to rely on the number of
4511 spaces to establish the original byte size of the chunk.
4512
4513 Although the library might report reversibility errors, removing a dump
4514 surface is a rather forgiving process: one may mix bases, group a variable
4515 number of data per source line, or use shorter chunks in places other
4516 than at the
4517 far end. Also, source lines not beginning with a number are skipped. So,
4518 Recode should often be able to read a whole C header file, wrapping
4519 the results of a previous dump, and regenerate the original byte string.
4520
4521 @node Test, , Dump, Surfaces
4522 @section Artificial data for testing
4523
4524 A few pseudo-surfaces exist to generate debugging data out of thin air.
4525 These surfaces are only meant for the expert Recode user, and are
4526 only useful in a few contexts, like for generating binary permutations
4527 from the recoding or acting on them.
4528
4529 @cindex debugging surfaces
4530 Debugging surfaces, @emph{when removed}, insert their generated data
4531 at the beginning of the output stream, and copy all the input stream
4532 after the generated data, unchanged. This strange removal constraint
4533 comes from the fact that debugging surfaces are usually specified in the
4534 @emph{before} position instead of the @emph{after} position within a request.
4535 With debugging surfaces, one often recodes file @file{/dev/null} in filter
4536 mode. Specifying many debugging surfaces at once has an accumulation
4537 effect on the output, and since surfaces are removed from right to left,
4538 each generating its data at the beginning of previous output, the net
4539 effect is an @emph{impression} that debugging surfaces are generated from
4540 left to right, each appending to the result of the previous. In any case,
4541 any real input data gets appended after what was generated.
4542
4543 @table @code
4544 @item test7
4545 @tindex test7
4546 When removed, this surface produces 128 single bytes, the first having
4547 value 0, the second having value 1, and so forth until all 128 values have
4548 been generated.
4549
4550 @item test8
4551 @tindex test8
4552 When removed, this surface produces 256 single bytes, the first having
4553 value 0, the second having value 1, and so forth until all 256 values have
4554 been generated.
4555
4556 @item test15
4557 @tindex test15
4558 When removed, this surface produces 64509 double bytes, the first having
4559 value 0, the second having value 1, and so forth until all values have been
4560 generated, but excluding risky @code{UCS-2} values, like all codes from
4561 the surrogate @code{UCS-2} area (for @code{UTF-16}), the byte order mark,
4562 and values known as invalid @code{UCS-2}.
4563
4564 @item test16
4565 @tindex test16
4566 When removed, this surface produces 65536 double bytes, the first having
4567 value 0, the second having value 1, and so forth until all 65536 values
4568 have been generated.
4569 @end table
4570
4571 As an example, the command @samp{recode l5/test8..dump < /dev/null} is a
4572 convoluted way to produce an output similar to @samp{recode -lf l5}. It says
4573 to generate all possible 256 bytes and interpret them as @code{ISO-8859-9}
4574 codes, while converting them to @code{UCS-2}. Resulting @code{UCS-2}
4575 characters are dumped one per line, accompanied with their explicative name.
4576
4577 @node Internals, Concept Index, Surfaces, Top
4578 @chapter Internal aspects
4579
4580 @cindex Recode internals
4581 @cindex internals
4582 The incoming explanations of the internals of Recode should
4583 help people who want to dive into Recode sources for adding new
4584 charsets. Adding new charsets does not require much knowledge about
4585 the overall organisation of Recode. You can rather concentrate
4586 of your new charset, letting the remainder of the Recode
4587 mechanics take care of interconnecting it with all others charsets.
4588
4589 If you intend to play seriously at modifying Recode, beware that
4590 you may need some other GNU tools which were not required when you first
4591 installed Recode. If you modify or create any @file{.l} file,
4592 then you need Flex, and some better @code{awk} like @code{mawk},
4593 GNU @code{awk}, or @code{nawk}. If you modify the documentation (and
4594 you should!), you need @code{makeinfo}. If you are really audacious,
4595 you may also want Perl for modifying tabular processing, then @code{m4},
4596 Autoconf, Automake and @code{libtool} for adjusting configuration matters.
4597
4598 @menu
4599 * Main flow:: Overall organisation
4600 * New charsets:: Adding new charsets
4601 * New surfaces:: Adding new surfaces
4602 * Design:: Comments on the library design
4603 @end menu
4604
4605 @node Main flow, New charsets, Internals, Internals
4606 @section Overall organisation
4607 @cindex Recode, main flow of operation
4608
4609 The Recode mechanics slowly evolved for many years, and it
4610 would be tedious to explain all problems I met and mistakes I did all
4611 along, yielding the current behaviour. Surely, one of the key choices
4612 was to stop trying to do all conversions in memory, one line or one
4613 buffer at a time. It has been fruitful to use the character stream
4614 paradigm, and the elementary recoding steps now convert a whole stream
4615 to another. Most of the control complexity in Recode exists
4616 so that each elementary recoding step stays simple, making easier
4617 to add new ones. The whole point of Recode, as I see it, is
4618 providing a comfortable nest for growing new charset conversions.
4619
4620 @cindex single step
4621 The main Recode driver constructs, while initialising all
4622 conversion modules, a table giving all the conversion routines
4623 available (@dfn{single step}s) and for each, the starting charset and
4624 the ending charset. If we consider these charsets as being the nodes
4625 of a directed graph, each single step may be considered as oriented
4626 arc from one node to the other. A cost is attributed to each arc:
4627 for example, a high penalty is given to single steps which are prone
4628 to losing characters, a lower penalty is given to those which need
4629 studying more than one input character for producing an output
4630 character, etc.
4631
4632 Given a starting code and a goal code, Recode computes the most
4633 economical route through the elementary recodings, that is, the best
4634 sequence of conversions that will transform the input charset into the
4635 final charset. To speed up execution, Recode looks for
4636 subsequences of conversions which are simple enough to be merged, and
4637 then dynamically creates new single steps to represent these mergings.
4638
4639 @cindex double step
4640 A @dfn{double step} in Recode is a special concept representing a
4641 sequence of two single steps, the output of the first single step being the
4642 special charset @code{UCS-2}, the input of the second single step being
4643 also @code{UCS-2}. Special Recode machinery dynamically produces
4644 efficient, reversible, merge-able single steps out of these double steps.
4645
4646 @cindex recoding steps, statistics
4647 @cindex average number of recoding steps
4648 I made some statistics about how many internal recoding steps are required
4649 between any two charsets chosen at random. The initial recoding layout,
4650 before optimisation, always uses between 1 and 5 steps. Optimisation could
4651 sometimes produce mere copies, which are counted as no steps at all.
4652 In other cases, optimisation is unable to save any step. The number of
4653 steps after optimisation is currently between 0 and 5 steps. Of course,
4654 the @emph{expected} number of steps is affected by optimisation: it drops
4655 from 2.8 to 1.8. This means that Recode uses a theoretical average
4656 of a bit less than one step per recoding job. This looks good. This was
4657 computed using reversible recodings. In strict mode, optimisation might
4658 be defeated somewhat. Number of steps run between 1 and 6, both before
4659 and after optimisation, and the expected number of steps decreases by a
4660 lesser amount, going from 2.2 to 1.3. This is still manageable.
4661
4662 @node New charsets, New surfaces, Main flow, Internals
4663 @section Adding new charsets
4664 @cindex adding new charsets
4665 @cindex new charsets, how to add
4666
4667 The main part of Recode is written in C, as are most single
4668 steps. A few single steps need to recognise sequences of multiple
4669 characters, they are often better written in Flex. It is easy for a
4670 programmer to add a new charset to Recode. All it requires
4671 is making a few functions kept in a single @file{.c} file,
4672 adjusting @file{Makefile.am} and remaking Recode.
4673
4674 One of the function should convert from any previous charset to the
4675 new one. Any previous charset will do, but try to select it so you will
4676 not lose too much information while converting. The other function should
4677 convert from the new charset to any older one. You do not have to select
4678 the same old charset than what you selected for the previous routine.
4679 Once again, select any charset for which you will not lose too much
4680 information while converting.
4681
4682 If, for any of these two functions, you have to read multiple bytes of the
4683 old charset before recognising the character to produce, you might prefer
4684 programming it in Flex in a separate @file{.l} file. Prototype your
4685 C or Flex files after one of those which exist already, so to keep the
4686 sources uniform. Besides, at @code{make} time, all @file{.l} files are
4687 automatically merged into a single big one by the script @file{mergelex.awk}.
4688
4689 There are a few hidden rules about how to write new Recode
4690 modules, for allowing the automatic creation of @file{decsteps.h}
4691 and @file{initsteps.h} at @code{make} time, or the proper merging of
4692 all Flex files. Mimetism is a simple approach which relieves me of
4693 explaining all these rules! Start with a module closely resembling
4694 what you intend to do. Here is some advice for picking up a model.
4695 First decide if your new charset module is to be be driven by algorithms
4696 rather than by tables. For algorithmic recodings, see @file{iconqnx.c} for
4697 C code, or @file{txtelat1.l} for Flex code. For table driven recodings,
4698 see @file{ebcdic.c} for one-to-one style recodings, @file{lat1html.c}
4699 for one-to-many style recodings, or @file{atarist.c} for double-step
4700 style recodings. Just select an example from the style that better fits
4701 your application.
4702
4703 Each of your source files should have its own initialisation function,
4704 named @code{module_@var{charset}}, which is meant to be executed
4705 @emph{quickly} once, prior to any recoding. It should declare the
4706 name of your charsets and the single steps (or elementary recodings)
4707 you provide, by calling @code{declare_step} one or more times.
4708 Besides the charset names, @code{declare_step} expects a description
4709 of the recoding quality (see @file{recodext.h}) and two functions you
4710 also provide.
4711
4712 The first such function has the purpose of allocating structures,
4713 pre-conditioning conversion tables, etc. It is also the way of further
4714 modifying the @code{STEP} structure. This function is executed if and
4715 only if the single step is retained in an actual recoding sequence.
4716 If you do not need such delayed initialisation, merely use @code{NULL}
4717 for the function argument.
4718
4719 The second function executes the elementary recoding on a whole file.
4720
4721 If you have a recoding table handy in a suitable format but do not use
4722 one of the predefined recoding functions, it is still a good idea to use
4723 a delayed initialisation to save it anyway, because @code{recode} option
4724 @samp{-h} will take advantage of this information when available.
4725
4726 Finally, edit @file{Makefile.am} to add the source file name of your routines
4727 to the @code{C_STEPS} or @code{L_STEPS} macro definition, depending on
4728 whether your routines are written in C or Flex.
4729
4730 @node New surfaces, Design, New charsets, Internals
4731 @section Adding new surfaces
4732 @cindex adding new surfaces
4733 @cindex new surfaces, how to add
4734
4735 Adding a new surface is technically quite similar to adding a new charset.
4736 @xref{New charsets}. A surface is provided as a set of two transformations:
4737 one from the predefined special charset @code{data} to the
4738 new surface, meant to apply the surface, the other from the new surface
4739 to the predefined special charset @code{data}, meant to remove the surface.
4740
4741 @findex declare_step
4742 Internally in Recode, function @code{declare_step} especially
4743 recognises when a charset is so related to @code{data},
4744 and then takes appropriate actions so that charset gets indeed installed
4745 as a surface.
4746
4747 @node Design, , New surfaces, Internals
4748 @section Comments on the library design
4749
4750 @itemize @bullet
4751 @item Why a shared library?
4752 @cindex shared library implementation
4753
4754 There are many different approaches to reduce system requirements to
4755 handle all tables needed in the Recode library. One of them is to
4756 have the tables in an external format and only read them in on demand.
4757 After having pondered this for a while, I finally decided against it,
4758 mainly because it involves its own kind of installation complexity, and
4759 it is not clear to me that it would be as interesting as I first imagined.
4760
4761 It looks more efficient to see all tables and algorithms already mapped
4762 into virtual memory from the start of the execution, yet not loaded in
4763 actual memory, than to go through many disk accesses for opening various
4764 data files once the program is already started, as this would be needed
4765 with other solutions. Using a shared library also has the indirect effect
4766 of making various algorithms handily available, right in the same modules
4767 providing the tables. This alleviates much the burden of the maintenance.
4768
4769 Of course, I would like to later make an exception for only a few tables,
4770 built locally by users for their own particular needs once Recode
4771 is installed. Recode should just go and fetch them. But I do not
4772 perceive this as very urgent, yet useful enough to be worth implementing.
4773
4774 Currently, all tables needed for recoding are precompiled into binaries,
4775 and all these binaries are then made into a shared library. As an initial
4776 step, I turned Recode into a main program and a non-shared library,
4777 this allowed me to tidy up the API, get rid of all global variables, etc.
4778 It required a surprising amount of program source massaging. But once
4779 this cleaned enough, it was easy to use Gordon Matzigkeit's @code{libtool}
4780 package, and take advantage of the Automake interface to neatly turn the
4781 non-shared library into a shared one.
4782
4783 Sites linking with the Recode library, whose system does not
4784 support any form of shared libraries, might end up with bulky executables.
4785 Surely, the Recode library will have to be used statically, and
4786 might not very nicely usable on such systems. It seems that progress
4787 has a price for those being slow at it.
4788
4789 There is a locality problem I did not address yet. Currently, the
4790 Recode library takes many cycles to initialise itself, calling
4791 each module in turn for it to set up associated knowledge about charsets,
4792 aliases, elementary steps, recoding weights, etc. @emph{Then}, the
4793 recoding sequence is decided out of the command given.
4794 One thing to do would have Recode to pre-load all tables
4795 and dump them at installation time. The result would then be compiled and
4796 added to the library. This would spare many initialisation cycles, but more
4797 importantly, would avoid calling all library modules, scattered through the
4798 virtual memory, and so, possibly causing many spurious page exceptions each
4799 time the initialisation is requested, at least once per program execution.
4800
4801 @item Why not a central charset?
4802
4803 It would be simpler, and I would like, if something like @w{ISO 10646} was
4804 used as a turning template for all charsets in Recode. Even if
4805 I think it could help to a certain extent, I'm still not fully sure it
4806 would be sufficient in all cases. Moreover, some people disagree about
4807 using @w{ISO 10646} as the central charset, to the point I cannot totally
4808 ignore them, and surely, Recode is not a means for me to force my
4809 own opinions on people. I would like that Recode be practical
4810 more than dogmatic, and reflect usage more than religions.
4811
4812 Currently, if you ask Recode to go from @var{charset1} to
4813 @var{charset2} chosen at random, it is highly probable that the best path
4814 will be quickly found as:
4815
4816 @example
4817 @var{charset1}..@code{UCS-2}..@var{charset2}
4818 @end example
4819
4820 That is, it will almost always use the @code{UCS} as a trampoline between
4821 charsets. However, @code{UCS-2} will be immediately be optimised out,
4822 and @var{charset1}..@var{charset2} will often be performed in a single
4823 step through a permutation table generated on the fly for the circumstance
4824 @footnote{If strict mapping is requested, another efficient device will
4825 be used instead of a permutation.}.
4826
4827 In those few cases where @code{UCS-2} is not selected as a conceptual
4828 intermediate, I plan to study if it could be made so. But I guess some cases
4829 will remain where @code{UCS-2} is not a proper choice. Even if @code{UCS} is
4830 often the good choice, I do not intend to forcefully restrain Recode
4831 around @code{UCS-2} (nor @code{UCS-4}) for now. We might come to that
4832 one day, but it will come out of the natural evolution of Recode.
4833 It will then reflect a fact, rather than a preset dogma.
4834
4835 @item Why not @code{iconv}?
4836
4837 @cindex @code{iconv}
4838 The @code{iconv} routine and library allows for converting characters
4839 from an input buffer to an input buffer, synchronously advancing both
4840 buffer cursors. If the output buffer is not big enough to receive
4841 all of the conversion, the routine returns with the input cursor set at
4842 the position where the conversion could later be resumed, and the output
4843 cursor set to indicate until where the output buffer has been filled.
4844 Despite this scheme is simple and nice, the Recode library does
4845 not offer it currently. Why not?
4846
4847 When long sequences of decodings, stepwise recodings, and re-encodings
4848 are involved, as it happens in true life, synchronising the input buffer
4849 back to where it should have stopped, when the output buffer becomes full,
4850 is a difficult problem. Oh, we could make it simpler at the expense of
4851 losing space or speed: by inserting markers between each input character
4852 and counting them at the output end; by processing only one character in a
4853 time through the whole sequence; by repeatedly attempting to recode various
4854 subsets of the input buffer, binary searching on their length until the
4855 output just fits. The overhead of such solutions looks prohibitive
4856 to me, and the gain very minimal. I do not see a real advantage, nowadays,
4857 imposing a fixed length to an output buffer. It makes things so much
4858 simpler and efficient to just let the output buffer size float a bit.
4859
4860 Of course, if the above problem was solved, the @code{iconv} library
4861 should be easily emulated, given that Recode has similar knowledge
4862 about charsets, of course. This either solved or not, the @code{iconv}
4863 program remains trivial (given similar knowledge about charsets).
4864 I also presume that the @code{genxlt} program would be easy too, but
4865 I do not have enough detailed specifications of it to be sure.
4866
4867 A lot of years ago, Recode was using a similar scheme, and I found
4868 it rather hard to manage for some cases. I rethought the overall structure
4869 of Recode for getting away from that scheme, and never regretted it.
4870 I perceive @code{iconv} as an artificial solution which surely has some
4871 elegances and virtues, but I do not find it really useful as it stands: one
4872 always has to wrap @code{iconv} into something more refined, extending it
4873 for real cases. From past experience, I think it is unduly hard to fully
4874 implement this scheme. It would be awkward that we do contortions for
4875 the sole purpose of implementing exactly its specification, without real,
4876 properly grounded reasons (other then the fact some people once thought it
4877 was worth standardising). It is much better to immediately aim for the
4878 refinement we need, without uselessly forcing us into the dubious detour
4879 @code{iconv} represents.
4880
4881 Some may argue that if Recode was using a comprehensive charset
4882 as a turning template, as discussed in a previous point, this would make
4883 @code{iconv} easier to implement. Some may be tempted to say that the
4884 cases which are hard to handle are not really needed, nor interesting,
4885 anyway. I feel and fear a bit some pressure wanting that Recode
4886 be split into the part that well fits the @code{iconv} model, and the part
4887 that does not fit, considering this second part less important, with the
4888 idea of dropping it one of these days, maybe. My guess is that users of
4889 the Recode library, whatever its form, would not like to have such
4890 arbitrary limitations. In the long run, we should not have to explain
4891 to our users that some recodings may not be made available just because
4892 they do not fit the simple model we had in mind when we did it. Instead,
4893 we should try to stay open to the difficulties of real life. There is
4894 still a lot of complex needs for Asian people, say, that Recode
4895 does not currently address, while it should. Not only the doors should
4896 stay open, but we should force them wider!
4897 @end itemize
4898
4899 @node Concept Index, Option Index, Internals, Top
4900 @unnumbered Concept Index
4901
4902 @printindex cp
4903
4904 @node Option Index, Library Index, Concept Index, Top
4905 @unnumbered Option Index
4906
4907 This is an alphabetical list of all command-line options accepted by
4908 @code{recode}.
4909
4910 @printindex op
4911
4912 @node Library Index, Charset and Surface Index, Option Index, Top
4913 @unnumbered Library Index
4914
4915 This is an alphabetical index of important functions, data structures,
4916 and variables in the Recode library.
4917
4918 @printindex fn
4919
4920 @node Charset and Surface Index, , Library Index, Top
4921 @unnumbered Charset and Surface Index
4922
4923 This is an alphabetical list of all the charsets and surfaces supported
4924 by Recode, and their aliases.
4925
4926 @printindex tp
4927
4928 @contents
4929 @bye
4930
4931 @c Local Variables:
4932 @c texinfo-column-for-description: 24
4933 @c End: